OSDN Git Service

TweenMain.Disposeでのイベントリスナーの解除漏れを修正
[opentween/open-tween.git] / OpenTween / SearchWordDialog.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 //           (c) 2008-2011 Moz (@syo68k)
4 //           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 //           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 //           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 //           (c) 2011      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
9 //
10 // This file is part of OpenTween.
11 //
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
15 // any later version.
16 //
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
26
27 #nullable enable
28
29 using System;
30 using System.Collections.Generic;
31 using System.ComponentModel;
32 using System.Data;
33 using System.Drawing;
34 using System.Linq;
35 using System.Text;
36 using System.Windows.Forms;
37
38 namespace OpenTween
39 {
40     public partial class SearchWordDialog : OTBaseForm
41     {
42         public enum SearchType
43         {
44             /// <summary>
45             /// タイムライン内を検索
46             /// </summary>
47             Timeline,
48
49             /// <summary>
50             /// Twitter検索
51             /// </summary>
52             Public,
53         }
54
55         public record SearchOptions(
56             SearchType Type,
57             string Query,
58             bool NewTab, // タイムライン内検索のみで使用する
59             bool CaseSensitive,
60             bool UseRegex
61         );
62
63         private SearchOptions? resultOptions = null;
64
65         public SearchOptions? ResultOptions
66         {
67             get => this.resultOptions;
68             set
69             {
70                 this.resultOptions = value;
71
72                 if (value == null)
73                 {
74                     this.Reset();
75                     return;
76                 }
77
78                 switch (value.Type)
79                 {
80                     case SearchType.Timeline:
81                         this.tabControl.SelectedTab = this.tabPageTimeline;
82                         this.textSearchTimeline.Text = value.Query;
83                         this.checkTimelineCaseSensitive.Checked = value.CaseSensitive;
84                         this.checkTimelineRegex.Checked = value.UseRegex;
85                         break;
86                     case SearchType.Public:
87                         this.tabControl.SelectedTab = this.tabPagePublic;
88                         this.textSearchTimeline.Text = value.Query;
89                         break;
90                     default:
91                         throw new InvalidEnumArgumentException("value", (int)value.Type, typeof(SearchType));
92                 }
93             }
94         }
95
96         private bool disableNetTabButton = false;
97
98         public bool DisableNewTabButton
99         {
100             get => this.disableNetTabButton;
101             set
102             {
103                 this.disableNetTabButton = value;
104                 this.buttonSearchTimelineNew.Enabled = !value;
105             }
106         }
107
108         public SearchWordDialog()
109             => this.InitializeComponent();
110
111         public void Reset()
112         {
113             this.tabControl.SelectedTab = this.tabPageTimeline;
114
115             this.textSearchTimeline.ResetText();
116             this.checkTimelineCaseSensitive.Checked = false;
117             this.checkTimelineRegex.Checked = false;
118
119             this.textSearchPublic.ResetText();
120         }
121
122         private void SearchWordDialog_Shown(object sender, EventArgs e)
123             => this.ActivateSelectedTabPage();
124
125         private void TabControl_SelectedIndexChanged(object sender, EventArgs e)
126             => this.ActivateSelectedTabPage();
127
128         private void ActivateSelectedTabPage()
129         {
130             if (this.tabControl.SelectedTab == this.tabPageTimeline)
131             {
132                 this.AcceptButton = this.buttonSearchTimeline;
133                 this.textSearchTimeline.SelectAll();
134                 this.textSearchTimeline.Focus();
135             }
136             else
137             {
138                 this.AcceptButton = this.buttonSearchPublic;
139                 this.textSearchPublic.SelectAll();
140                 this.textSearchPublic.Focus();
141             }
142         }
143
144         private void ButtonSearchTimeline_Click(object sender, EventArgs e)
145         {
146             if (MyCommon.IsNullOrEmpty(this.textSearchTimeline.Text))
147             {
148                 this.DialogResult = DialogResult.Cancel;
149                 return;
150             }
151
152             this.DialogResult = DialogResult.OK;
153
154             this.ResultOptions = new SearchOptions(
155                 SearchType.Timeline,
156                 this.textSearchTimeline.Text,
157                 false,
158                 this.checkTimelineCaseSensitive.Checked,
159                 this.checkTimelineRegex.Checked
160             );
161         }
162
163         private void ButtonSearchTimelineNew_Click(object sender, EventArgs e)
164         {
165             if (MyCommon.IsNullOrEmpty(this.textSearchTimeline.Text))
166             {
167                 this.DialogResult = DialogResult.Cancel;
168                 return;
169             }
170
171             this.DialogResult = DialogResult.OK;
172
173             this.ResultOptions = new SearchOptions(
174                 SearchType.Timeline,
175                 this.textSearchTimeline.Text,
176                 true,
177                 this.checkTimelineCaseSensitive.Checked,
178                 this.checkTimelineRegex.Checked
179             );
180         }
181
182         private void ButtonSearchPublic_Click(object sender, EventArgs e)
183         {
184             if (MyCommon.IsNullOrEmpty(this.textSearchPublic.Text))
185             {
186                 this.DialogResult = DialogResult.Cancel;
187                 return;
188             }
189
190             this.DialogResult = DialogResult.OK;
191
192             this.ResultOptions = new SearchOptions(
193                 SearchType.Public,
194                 this.textSearchPublic.Text,
195                 true,
196                 false,
197                 false
198             );
199         }
200
201         private async void LinkLabelSearchHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
202         {
203             // 「検索オプションの使い方」ページのURL
204             const string PublicSearchHelpUrl = "https://support.twitter.com/articles/249059";
205             await MyCommon.OpenInBrowserAsync(this, PublicSearchHelpUrl);
206         }
207
208         private void SearchWordDialog_KeyDown(object sender, KeyEventArgs e)
209         {
210             if (e.KeyCode == Keys.Escape)
211                 this.DialogResult = DialogResult.Cancel;
212         }
213     }
214 }