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) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
10 // This file is part of OpenTween.
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)
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
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.
28 using System.Collections.Generic;
29 using System.ComponentModel;
32 using System.Globalization;
35 using System.Windows.Forms;
39 public partial class HashtagManage : OTBaseForm
41 public string UseHash { get; private set; } = "";
42 public bool IsPermanent { get; private set; } = false;
43 public bool IsHead { get; private set; } = false;
44 public bool IsNotAddToAtReply { get; private set; } = true;
47 /// エラー時にダイアログを表示させない (ユニットテストなどで使用)
49 public bool RunSilent { get; set; }
52 private readonly AtIdSupplement _hashSupl;
55 private bool _isAdd = false;
57 private void ChangeMode(bool isEdit)
59 this.GroupHashtag.Enabled = !isEdit;
60 this.GroupDetail.Enabled = isEdit;
61 this.TableLayoutButtons.Enabled = !isEdit;
63 this.UseHashText.Focus();
65 this.HistoryHashList.Focus();
68 private void Cancel_Button_Click(object sender, EventArgs e)
70 this.DialogResult = DialogResult.Cancel;
74 private void AddButton_Click(object sender, EventArgs e)
76 this.UseHashText.Text = "";
81 private void EditButton_Click(object sender, EventArgs e)
83 if (this.HistoryHashList.SelectedIndices.Count == 0) return;
84 this.UseHashText.Text = this.HistoryHashList.SelectedItems[0].ToString();
89 private void DeleteButton_Click(object sender, EventArgs e)
91 if (this.HistoryHashList.SelectedIndices.Count == 0) return;
92 if (!this.RunSilent &&
93 MessageBox.Show(Properties.Resources.DeleteHashtagsMessage1,
95 MessageBoxButtons.OKCancel,
96 MessageBoxIcon.Question) == DialogResult.Cancel)
101 // 削除によってインデックス番号が変わらないように逆順に処理する
102 var selectedIndices = this.HistoryHashList.SelectedIndices.Cast<int>()
103 .OrderByDescending(x => x).ToArray();
105 foreach (var idx in selectedIndices)
107 if (UseHashText.Text == HistoryHashList.Items[idx].ToString()) UseHashText.Text = "";
108 HistoryHashList.Items.RemoveAt(idx);
110 if (HistoryHashList.Items.Count > 0)
112 HistoryHashList.SelectedIndex = 0;
116 private void UnSelectButton_Click(object sender, EventArgs e)
120 HistoryHashList.SelectedIndices.Clear();
121 } while (HistoryHashList.SelectedIndices.Count > 0);
124 private int GetIndexOf(ListBox.ObjectCollection list, string value)
126 if (string.IsNullOrEmpty(value)) return -1;
130 foreach (var l in list)
132 var src = l as string;
133 if (string.IsNullOrEmpty(src))
138 if (string.Compare(src, value, StringComparison.OrdinalIgnoreCase) == 0)
149 public void AddHashToHistory(string hash, bool isIgnorePermanent)
152 if (!string.IsNullOrEmpty(hash))
154 if (isIgnorePermanent || !this.IsPermanent)
157 var idx = GetIndexOf(HistoryHashList.Items, hash);
159 if (idx != -1) HistoryHashList.Items.RemoveAt(idx);
160 HistoryHashList.Items.Insert(0, hash);
165 var idx = GetIndexOf(HistoryHashList.Items, hash);
166 if (this.IsPermanent)
170 //重複アイテムが2行目以降にあれば2行目へ
171 HistoryHashList.Items.RemoveAt(idx);
172 HistoryHashList.Items.Insert(1, hash);
177 if (HistoryHashList.Items.Count == 0)
180 HistoryHashList.Items.Add(hash);
185 HistoryHashList.Items.Insert(1, hash);
193 private void HashtagManage_Shown(object sender, EventArgs e)
196 this.CheckPermanent.Checked = this.IsPermanent;
197 this.RadioHead.Checked = this.IsHead;
198 this.RadioLast.Checked = !this.IsHead;
200 if (this.HistoryHashList.Items.Contains(this.UseHash))
202 this.HistoryHashList.SelectedItem = this.UseHash;
206 if (this.HistoryHashList.Items.Count > 0)
207 this.HistoryHashList.SelectedIndex = 0;
209 this.ChangeMode(false);
212 public HashtagManage(AtIdSupplement hashSuplForm, string[] history, string permanentHash, bool IsPermanent, bool IsHead, bool IsNotAddToAtReply)
214 // この呼び出しは、Windows フォーム デザイナで必要です。
215 InitializeComponent();
217 // InitializeComponent() 呼び出しの後で初期化を追加します。
219 _hashSupl = hashSuplForm;
220 HistoryHashList.Items.AddRange(history);
221 this.UseHash = permanentHash;
222 this.IsPermanent = IsPermanent;
223 this.IsHead = IsHead;
224 this.IsNotAddToAtReply = IsNotAddToAtReply;
227 private void UseHashText_KeyPress(object sender, KeyPressEventArgs e)
229 if (e.KeyChar == '#')
231 _hashSupl.ShowDialog();
232 if (!string.IsNullOrEmpty(_hashSupl.inputText))
236 var selStart = UseHashText.SelectionStart;
239 fHalf = UseHashText.Text.Substring(0, selStart);
241 if (selStart < UseHashText.Text.Length)
243 eHalf = UseHashText.Text.Substring(selStart);
245 UseHashText.Text = fHalf + _hashSupl.inputText + eHalf;
246 UseHashText.SelectionStart = selStart + _hashSupl.inputText.Length;
252 private void HistoryHashList_DoubleClick(object sender, EventArgs e)
253 => this.OK_Button_Click(null, null);
255 public void ToggleHash()
257 if (string.IsNullOrEmpty(this.UseHash))
259 if (this.HistoryHashList.Items.Count > 0)
260 this.UseHash = this.HistoryHashList.Items[0].ToString();
268 public List<string> HashHistories
272 var hash = new List<string>();
273 foreach (string item in HistoryHashList.Items)
281 public void ClearHashtag()
282 => this.UseHash = "";
284 public void SetPermanentHash(string hash)
287 this.UseHash = hash.Trim();
288 this.AddHashToHistory(UseHash, false);
289 this.IsPermanent = true;
292 private void PermOK_Button_Click(object sender, EventArgs e)
295 var hashStr = UseHashText.Text;
296 if (!this.AdjustHashtags(ref hashStr, !this.RunSilent)) return;
298 UseHashText.Text = hashStr;
300 if (!this._isAdd && this.HistoryHashList.SelectedIndices.Count > 0)
302 idx = this.HistoryHashList.SelectedIndices[0];
303 this.HistoryHashList.Items.RemoveAt(idx);
306 this.HistoryHashList.SelectedIndices.Clear();
307 } while (this.HistoryHashList.SelectedIndices.Count > 0);
308 this.HistoryHashList.Items.Insert(idx, hashStr);
309 this.HistoryHashList.SelectedIndex = idx;
313 this.AddHashToHistory(hashStr, false);
316 this.HistoryHashList.SelectedIndices.Clear();
317 } while (this.HistoryHashList.SelectedIndices.Count > 0);
318 this.HistoryHashList.SelectedIndex = this.HistoryHashList.Items.IndexOf(hashStr);
324 private void PermCancel_Button_Click(object sender, EventArgs e)
326 if (this.HistoryHashList.Items.Count > 0 && this.HistoryHashList.SelectedIndices.Count > 0)
327 this.UseHashText.Text = this.HistoryHashList.Items[this.HistoryHashList.SelectedIndices[0]].ToString();
329 this.UseHashText.Text = "";
334 private void HistoryHashList_KeyDown(object sender, KeyEventArgs e)
336 if (e.KeyCode == Keys.Delete)
337 this.DeleteButton_Click(null, null);
338 else if (e.KeyCode == Keys.Insert)
339 this.AddButton_Click(null, null);
342 private bool AdjustHashtags(ref string hashtag, bool isShowWarn)
345 hashtag = hashtag.Trim();
346 if (string.IsNullOrEmpty(hashtag))
348 if (isShowWarn) MessageBox.Show("emply hashtag.", "Hashtag warning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
351 hashtag = hashtag.Replace("#", "#");
352 hashtag = hashtag.Replace(" ", " ");
354 foreach (var hash in hashtag.Split(' '))
358 if (!hash.StartsWith("#", StringComparison.Ordinal))
360 if (isShowWarn) MessageBox.Show("Invalid hashtag. -> " + hash, "Hashtag warning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
363 if (hash.Length == 1)
365 if (isShowWarn) MessageBox.Show("empty hashtag.", "Hashtag warning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
369 adjust += hash + " ";
372 hashtag = adjust.Trim();
376 private void OK_Button_Click(object sender, EventArgs e)
379 foreach (string hs in this.HistoryHashList.SelectedItems)
384 if (!string.IsNullOrEmpty(hash))
386 this.AddHashToHistory(hash, true);
387 this.IsPermanent = this.CheckPermanent.Checked;
391 //使用ハッシュが未選択ならば、固定オプション外す
392 this.IsPermanent = false;
394 this.IsHead = this.RadioHead.Checked;
397 this.DialogResult = DialogResult.OK;
401 private void HashtagManage_KeyDown(object sender, KeyEventArgs e)
403 if (e.KeyCode == Keys.Enter)
406 if (this.GroupDetail.Enabled)
407 this.PermOK_Button_Click(null, null);
409 this.OK_Button_Click(null, null);
411 else if (e.KeyCode == Keys.Escape)
414 if (this.GroupDetail.Enabled)
415 this.PermCancel_Button_Click(null, null);
417 this.Cancel_Button_Click(null, null);
421 private void CheckNotAddToAtReply_CheckedChanged(object sender, EventArgs e)
422 => this.IsNotAddToAtReply = CheckNotAddToAtReply.Checked;