OSDN Git Service

バージョン v3.13.1-dev 開発開始
[opentween/open-tween.git] / OpenTween / AtIdSupplement.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      Egtra (@egtra) <http://dev.activebasic.com/egtra/>
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 AtIdSupplement : OTBaseForm
41     {
42         public string StartsWith { get; set; } = "";
43
44         public string InputText { get; set; } = "";
45
46         private bool isBack = false;
47         private readonly string startChar = "";
48
49         public void AddItem(string id)
50         {
51             if (!this.TextId.AutoCompleteCustomSource.Contains(id))
52             {
53                 this.TextId.AutoCompleteCustomSource.Add(id);
54             }
55         }
56
57         public void AddRangeItem(string[] ids)
58         {
59             foreach (var id in ids)
60             {
61                 this.AddItem(id);
62             }
63         }
64
65         public List<string> GetItemList()
66         {
67             var ids = new List<string>();
68             for (var i = 0; i < this.TextId.AutoCompleteCustomSource.Count; ++i)
69             {
70                 ids.Add(this.TextId.AutoCompleteCustomSource[i]);
71             }
72             return ids;
73         }
74
75         public int ItemCount
76             => this.TextId.AutoCompleteCustomSource.Count;
77
78         private void ButtonOK_Click(object sender, EventArgs e)
79         {
80             this.InputText = this.TextId.Text;
81             this.isBack = false;
82         }
83
84         private void ButtonCancel_Click(object sender, EventArgs e)
85         {
86             this.InputText = "";
87             this.isBack = false;
88         }
89
90         private void TextId_KeyDown(object sender, KeyEventArgs e)
91         {
92             if (e.KeyCode == Keys.Back && MyCommon.IsNullOrEmpty(this.TextId.Text))
93             {
94                 this.InputText = "";
95                 this.isBack = true;
96                 this.Close();
97             }
98             else if (e.KeyCode == Keys.Space || e.KeyCode == Keys.Tab)
99             {
100                 this.InputText = this.TextId.Text + " ";
101                 this.isBack = false;
102                 this.Close();
103             }
104             else if (e.Control && e.KeyCode == Keys.Delete)
105             {
106                 if (!MyCommon.IsNullOrEmpty(this.TextId.Text))
107                 {
108                     var idx = this.TextId.AutoCompleteCustomSource.IndexOf(this.TextId.Text);
109                     if (idx > -1)
110                     {
111                         this.TextId.Text = "";
112                         this.TextId.AutoCompleteCustomSource.RemoveAt(idx);
113                     }
114                 }
115             }
116         }
117
118         private void AtIdSupplement_Load(object sender, EventArgs e)
119         {
120             if (this.startChar == "#")
121             {
122                 this.ClientSize = new Size(this.TextId.Width, this.TextId.Height); // プロパティで切り替えできるように
123                 this.TextId.ImeMode = ImeMode.Inherit;
124             }
125         }
126
127         private void AtIdSupplement_Shown(object sender, EventArgs e)
128         {
129             this.TextId.Text = this.startChar;
130             if (!MyCommon.IsNullOrEmpty(this.StartsWith))
131             {
132                 this.TextId.Text += this.StartsWith.Substring(0, this.StartsWith.Length);
133             }
134             this.TextId.SelectionStart = this.TextId.Text.Length;
135             this.TextId.Focus();
136         }
137
138         public AtIdSupplement()
139             => this.InitializeComponent();
140
141         public AtIdSupplement(List<string> itemList, string startCharacter)
142         {
143             this.InitializeComponent();
144
145             for (var i = 0; i < itemList.Count; ++i)
146             {
147                 this.TextId.AutoCompleteCustomSource.Add(itemList[i]);
148             }
149             this.startChar = startCharacter;
150         }
151
152         private void TextId_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
153         {
154             if (e.KeyCode == Keys.Tab)
155             {
156                 this.InputText = this.TextId.Text + " ";
157                 this.isBack = false;
158                 this.Close();
159             }
160         }
161
162         private void AtIdSupplement_FormClosed(object sender, FormClosedEventArgs e)
163         {
164             this.StartsWith = "";
165             if (this.isBack)
166             {
167                 this.DialogResult = DialogResult.Cancel;
168             }
169             else
170             {
171                 this.DialogResult = DialogResult.OK;
172             }
173         }
174     }
175 }