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;
299 if (!this._isAdd && this.HistoryHashList.SelectedIndices.Count > 0)
301 var idx = this.HistoryHashList.SelectedIndices[0];
302 this.HistoryHashList.Items.RemoveAt(idx);
305 this.HistoryHashList.SelectedIndices.Clear();
306 } while (this.HistoryHashList.SelectedIndices.Count > 0);
307 this.HistoryHashList.Items.Insert(idx, hashStr);
308 this.HistoryHashList.SelectedIndex = idx;
312 this.AddHashToHistory(hashStr, false);
315 this.HistoryHashList.SelectedIndices.Clear();
316 } while (this.HistoryHashList.SelectedIndices.Count > 0);
317 this.HistoryHashList.SelectedIndex = this.HistoryHashList.Items.IndexOf(hashStr);
323 private void PermCancel_Button_Click(object sender, EventArgs e)
325 if (this.HistoryHashList.Items.Count > 0 && this.HistoryHashList.SelectedIndices.Count > 0)
326 this.UseHashText.Text = this.HistoryHashList.Items[this.HistoryHashList.SelectedIndices[0]].ToString();
328 this.UseHashText.Text = "";
333 private void HistoryHashList_KeyDown(object sender, KeyEventArgs e)
335 if (e.KeyCode == Keys.Delete)
336 this.DeleteButton_Click(null, null);
337 else if (e.KeyCode == Keys.Insert)
338 this.AddButton_Click(null, null);
341 private bool AdjustHashtags(ref string hashtag, bool isShowWarn)
344 hashtag = hashtag.Trim();
345 if (string.IsNullOrEmpty(hashtag))
347 if (isShowWarn) MessageBox.Show("emply hashtag.", "Hashtag warning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
350 hashtag = hashtag.Replace("#", "#");
351 hashtag = hashtag.Replace(" ", " ");
353 foreach (var hash in hashtag.Split(' '))
357 if (!hash.StartsWith("#", StringComparison.Ordinal))
359 if (isShowWarn) MessageBox.Show("Invalid hashtag. -> " + hash, "Hashtag warning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
362 if (hash.Length == 1)
364 if (isShowWarn) MessageBox.Show("empty hashtag.", "Hashtag warning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
368 adjust += hash + " ";
371 hashtag = adjust.Trim();
375 private void OK_Button_Click(object sender, EventArgs e)
378 foreach (string hs in this.HistoryHashList.SelectedItems)
383 if (!string.IsNullOrEmpty(hash))
385 this.AddHashToHistory(hash, true);
386 this.IsPermanent = this.CheckPermanent.Checked;
390 //使用ハッシュが未選択ならば、固定オプション外す
391 this.IsPermanent = false;
393 this.IsHead = this.RadioHead.Checked;
396 this.DialogResult = DialogResult.OK;
400 private void HashtagManage_KeyDown(object sender, KeyEventArgs e)
402 if (e.KeyCode == Keys.Enter)
405 if (this.GroupDetail.Enabled)
406 this.PermOK_Button_Click(null, null);
408 this.OK_Button_Click(null, null);
410 else if (e.KeyCode == Keys.Escape)
413 if (this.GroupDetail.Enabled)
414 this.PermCancel_Button_Click(null, null);
416 this.Cancel_Button_Click(null, null);
420 private void CheckNotAddToAtReply_CheckedChanged(object sender, EventArgs e)
421 => this.IsNotAddToAtReply = CheckNotAddToAtReply.Checked;