OSDN Git Service

非公式RTで絵文字のHTMLタグを除去できていない不具合を修正 (thx @380Nayuta!)
[opentween/open-tween.git] / OpenTween / Tween.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 //コンパイル後コマンド
28 //"c:\Program Files\Microsoft.NET\SDK\v2.0\Bin\sgen.exe" /f /a:"$(TargetPath)"
29 //"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\sgen.exe" /f /a:"$(TargetPath)"
30
31 using System;
32 using System.Collections.Concurrent;
33 using System.Collections.Generic;
34 using System.ComponentModel;
35 using System.Diagnostics;
36 using System.Drawing;
37 using System.Globalization;
38 using System.IO;
39 using System.Linq;
40 using System.Media;
41 using System.Net;
42 using System.Net.Http;
43 using System.Reflection;
44 using System.Runtime.InteropServices;
45 using System.Text;
46 using System.Text.RegularExpressions;
47 using System.Threading;
48 using System.Threading.Tasks;
49 using System.Windows.Forms;
50 using OpenTween.Api;
51 using OpenTween.Api.DataModel;
52 using OpenTween.Connection;
53 using OpenTween.Models;
54 using OpenTween.OpenTweenCustomControl;
55 using OpenTween.Setting;
56 using OpenTween.Thumbnail;
57
58 namespace OpenTween
59 {
60     public partial class TweenMain : OTBaseForm
61     {
62         //各種設定
63         private Size _mySize;           //画面サイズ
64         private Point _myLoc;           //画面位置
65         private int _mySpDis;           //区切り位置
66         private int _mySpDis2;          //発言欄区切り位置
67         private int _mySpDis3;          //プレビュー区切り位置
68         private int _iconSz;            //アイコンサイズ(現在は16、24、48の3種類。将来直接数字指定可能とする 注:24x24の場合に26と指定しているのはMSゴシック系フォントのための仕様)
69         private bool _iconCol;          //1列表示の時true(48サイズのとき)
70
71         //雑多なフラグ類
72         private bool _initial;         //true:起動時処理中
73         private bool _initialLayout = true;
74         private bool _ignoreConfigSave;         //true:起動時処理中
75         private bool _tabDrag;           //タブドラッグ中フラグ(DoDragDropを実行するかの判定用)
76         private TabPage _beforeSelectedTab; //タブが削除されたときに前回選択されていたときのタブを選択する為に保持
77         private Point _tabMouseDownPoint;
78         private string _rclickTabName;      //右クリックしたタブの名前(Tabコントロール機能不足対応)
79         private readonly object _syncObject = new object();    //ロック用
80
81         private const string detailHtmlFormatHeaderMono = 
82             "<html><head><meta http-equiv=\"X-UA-Compatible\" content=\"IE=8\">"
83             + "<style type=\"text/css\"><!-- "
84             + "body, p, pre {margin: 0;} "
85             + "pre {font-family: \"%FONT_FAMILY%\", sans-serif; font-size: %FONT_SIZE%pt; background-color:rgb(%BG_COLOR%); word-wrap: break-word; color:rgb(%FONT_COLOR%);} "
86             + "a:link, a:visited, a:active, a:hover {color:rgb(%LINK_COLOR%); } "
87             + "img.emoji {width: 1em; height: 1em; margin: 0 .05em 0 .1em; vertical-align: -0.1em; border: none;} "
88             + ".quote-tweet {border: 1px solid #ccc; margin: 1em; padding: 0.5em;} "
89             + ".quote-tweet.reply {border-color: #f33;} "
90             + ".quote-tweet-link {color: inherit !important; text-decoration: none;}"
91             + "--></style>"
92             + "</head><body><pre>";
93         private const string detailHtmlFormatFooterMono = "</pre></body></html>";
94         private const string detailHtmlFormatHeaderColor = 
95             "<html><head><meta http-equiv=\"X-UA-Compatible\" content=\"IE=8\">"
96             + "<style type=\"text/css\"><!-- "
97             + "body, p, pre {margin: 0;} "
98             + "body {font-family: \"%FONT_FAMILY%\", sans-serif; font-size: %FONT_SIZE%pt; background-color:rgb(%BG_COLOR%); margin: 0; word-wrap: break-word; color:rgb(%FONT_COLOR%);} "
99             + "a:link, a:visited, a:active, a:hover {color:rgb(%LINK_COLOR%); } "
100             + "img.emoji {width: 1em; height: 1em; margin: 0 .05em 0 .1em; vertical-align: -0.1em; border: none;} "
101             + ".quote-tweet {border: 1px solid #ccc; margin: 1em; padding: 0.5em;} "
102             + ".quote-tweet.reply {border-color: rgb(%BG_REPLY_COLOR%);} "
103             + ".quote-tweet-link {color: inherit !important; text-decoration: none;}"
104             + "--></style>"
105             + "</head><body><p>";
106         private const string detailHtmlFormatFooterColor = "</p></body></html>";
107         private string detailHtmlFormatHeader;
108         private string detailHtmlFormatFooter;
109
110         private bool _myStatusError = false;
111         private bool _myStatusOnline = false;
112         private bool soundfileListup = false;
113         private FormWindowState _formWindowState = FormWindowState.Normal; // フォームの状態保存用 通知領域からアイコンをクリックして復帰した際に使用する
114
115         //twitter解析部
116         private TwitterApi twitterApi = new TwitterApi();
117         private Twitter tw;
118
119         //Growl呼び出し部
120         private GrowlHelper gh = new GrowlHelper(ApplicationSettings.ApplicationName);
121
122         //サブ画面インスタンス
123         internal SearchWordDialog SearchDialog = new SearchWordDialog();     //検索画面インスタンス
124         private OpenURL UrlDialog = new OpenURL();
125         public AtIdSupplement AtIdSupl;     //@id補助
126         public AtIdSupplement HashSupl;    //Hashtag補助
127         public HashtagManage HashMgr;
128         private EventViewerDialog evtDialog;
129
130         //表示フォント、色、アイコン
131         private Font _fntUnread;            //未読用フォント
132         private Color _clUnread;            //未読用文字色
133         private Font _fntReaded;            //既読用フォント
134         private Color _clReaded;            //既読用文字色
135         private Color _clFav;               //Fav用文字色
136         private Color _clOWL;               //片思い用文字色
137         private Color _clRetweet;               //Retweet用文字色
138         private Color _clHighLight = Color.FromKnownColor(KnownColor.HighlightText);         //選択中の行用文字色
139         private Font _fntDetail;            //発言詳細部用フォント
140         private Color _clDetail;              //発言詳細部用色
141         private Color _clDetailLink;          //発言詳細部用リンク文字色
142         private Color _clDetailBackcolor;     //発言詳細部用背景色
143         private Color _clSelf;              //自分の発言用背景色
144         private Color _clAtSelf;            //自分宛返信用背景色
145         private Color _clTarget;            //選択発言者の他の発言用背景色
146         private Color _clAtTarget;          //選択発言中の返信先用背景色
147         private Color _clAtFromTarget;      //選択発言者への返信発言用背景色
148         private Color _clAtTo;              //選択発言の唯一@先
149         private Color _clListBackcolor;       //リスト部通常発言背景色
150         private Color _clInputBackcolor;      //入力欄背景色
151         private Color _clInputFont;           //入力欄文字色
152         private Font _fntInputFont;           //入力欄フォント
153         private ImageCache IconCache;        //アイコン画像リスト
154         private Icon NIconAt;               //At.ico             タスクトレイアイコン:通常時
155         private Icon NIconAtRed;            //AtRed.ico          タスクトレイアイコン:通信エラー時
156         private Icon NIconAtSmoke;          //AtSmoke.ico        タスクトレイアイコン:オフライン時
157         private Icon[] NIconRefresh = new Icon[4];       //Refresh.ico        タスクトレイアイコン:更新中(アニメーション用に4種類を保持するリスト)
158         private Icon TabIcon;               //Tab.ico            未読のあるタブ用アイコン
159         private Icon MainIcon;              //Main.ico           画面左上のアイコン
160         private Icon ReplyIcon;               //5g
161         private Icon ReplyIconBlink;          //6g
162
163         private ImageList _listViewImageList = new ImageList();    //ListViewItemの高さ変更用
164
165         private PostClass _anchorPost;
166         private bool _anchorFlag;        //true:関連発言移動中(関連移動以外のオペレーションをするとfalseへ。trueだとリスト背景色をアンカー発言選択中として描画)
167
168         private List<StatusTextHistory> _history = new List<StatusTextHistory>();   //発言履歴
169         private int _hisIdx;                  //発言履歴カレントインデックス
170
171         //発言投稿時のAPI引数(発言編集時に設定。手書きreplyでは設定されない)
172         private (long StatusId, string ScreenName)? inReplyTo = null; // リプライ先のステータスID・スクリーン名
173
174         //時速表示用
175         private List<DateTimeUtc> _postTimestamps = new List<DateTimeUtc>();
176         private List<DateTimeUtc> _favTimestamps = new List<DateTimeUtc>();
177
178         // 以下DrawItem関連
179         private SolidBrush _brsHighLight = new SolidBrush(Color.FromKnownColor(KnownColor.Highlight));
180         private SolidBrush _brsBackColorMine;
181         private SolidBrush _brsBackColorAt;
182         private SolidBrush _brsBackColorYou;
183         private SolidBrush _brsBackColorAtYou;
184         private SolidBrush _brsBackColorAtFromTarget;
185         private SolidBrush _brsBackColorAtTo;
186         private SolidBrush _brsBackColorNone;
187         private SolidBrush _brsDeactiveSelection = new SolidBrush(Color.FromKnownColor(KnownColor.ButtonFace)); //Listにフォーカスないときの選択行の背景色
188         private StringFormat sfTab = new StringFormat();
189
190         //////////////////////////////////////////////////////////////////////////////////////////////////////////
191         private TabInformations _statuses;
192
193         /// <summary>
194         /// 現在表示している発言一覧の <see cref="ListView"/> に対するキャッシュ
195         /// </summary>
196         /// <remarks>
197         /// キャッシュクリアのために null が代入されることがあるため、
198         /// 使用する場合には <see cref="_listItemCache"/> に対して直接メソッド等を呼び出さずに
199         /// 一旦ローカル変数に代入してから参照すること。
200         /// </remarks>
201         private ListViewItemCache _listItemCache = null;
202
203         internal class ListViewItemCache
204         {
205             /// <summary>アイテムをキャッシュする対象の <see cref="ListView"/></summary>
206             public ListView TargetList { get; set; }
207
208             /// <summary>キャッシュする範囲の開始インデックス</summary>
209             public int StartIndex { get; set; }
210
211             /// <summary>キャッシュする範囲の終了インデックス</summary>
212             public int EndIndex { get; set; }
213
214             /// <summary>キャッシュされた <see cref="ListViewItem"/> インスタンス</summary>
215             public ListViewItem[] ListItem { get; set; }
216
217             /// <summary>キャッシュされた範囲に対応する <see cref="PostClass"/> インスタンス</summary>
218             public PostClass[] Post { get; set; }
219
220             /// <summary>キャッシュされたアイテムの件数</summary>
221             public int Count
222                 => this.EndIndex - this.StartIndex + 1;
223
224             /// <summary>指定されたインデックスがキャッシュの範囲内であるか判定します</summary>
225             /// <returns><paramref name="index"/> がキャッシュの範囲内であれば true、それ以外は false</returns>
226             public bool Contains(int index)
227                 => index >= this.StartIndex && index <= this.EndIndex;
228
229             /// <summary>指定されたインデックスの範囲が全てキャッシュの範囲内であるか判定します</summary>
230             /// <returns><paramref name="rangeStart"/> から <paramref name="rangeEnd"/> の範囲が全てキャッシュの範囲内であれば true、それ以外は false</returns>
231             public bool IsSupersetOf(int rangeStart, int rangeEnd)
232                 => rangeStart >= this.StartIndex && rangeEnd <= this.EndIndex;
233
234             /// <summary>指定されたインデックスの <see cref="ListViewItem"/> と <see cref="PostClass"/> をキャッシュから取得することを試みます</summary>
235             /// <returns>取得に成功すれば true、それ以外は false</returns>
236             public bool TryGetValue(int index, out ListViewItem item, out PostClass post)
237             {
238                 if (this.Contains(index))
239                 {
240                     item = this.ListItem[index - this.StartIndex];
241                     post = this.Post[index - this.StartIndex];
242                     return true;
243                 }
244                 else
245                 {
246                     item = null;
247                     post = null;
248                     return false;
249                 }
250             }
251         }
252
253         private TabPage _curTab;
254         private int _curItemIndex;
255         private DetailsListView _curList;
256         private PostClass _curPost;
257         private bool _isColumnChanged = false;
258
259         private const int MAX_WORKER_THREADS = 20;
260         private SemaphoreSlim workerSemaphore = new SemaphoreSlim(MAX_WORKER_THREADS);
261         private CancellationTokenSource workerCts = new CancellationTokenSource();
262         private IProgress<string> workerProgress;
263
264         private int UnreadCounter = -1;
265         private int UnreadAtCounter = -1;
266
267         private string[] ColumnOrgText = new string[9];
268         private string[] ColumnText = new string[9];
269
270         private bool _DoFavRetweetFlags = false;
271         private bool osResumed = false;
272
273         //////////////////////////////////////////////////////////////////////////////////////////////////////////
274         private bool _colorize = false;
275
276         private System.Timers.Timer TimerTimeline = new System.Timers.Timer();
277         private ThrottlingTimer RefreshThrottlingTimer;
278
279         private string recommendedStatusFooter;
280         private bool urlMultibyteSplit = false;
281         private bool preventSmsCommand = true;
282
283         //URL短縮のUndo用
284         private struct urlUndo
285         {
286             public string Before;
287             public string After;
288         }
289
290         private List<urlUndo> urlUndoBuffer = null;
291
292         private struct ReplyChain
293         {
294             public long OriginalId;
295             public long InReplyToId;
296             public TabPage OriginalTab;
297
298             public ReplyChain(long originalId, long inReplyToId, TabPage originalTab)
299             {
300                 this.OriginalId = originalId;
301                 this.InReplyToId = inReplyToId;
302                 this.OriginalTab = originalTab;
303             }
304         }
305
306         private Stack<ReplyChain> replyChains; //[, ]でのリプライ移動の履歴
307         private Stack<(TabPage, PostClass)> selectPostChains = new Stack<(TabPage, PostClass)>(); //ポスト選択履歴
308
309         //検索処理タイプ
310         internal enum SEARCHTYPE
311         {
312             DialogSearch,
313             NextSearch,
314             PrevSearch,
315         }
316
317         private class StatusTextHistory
318         {
319             public string status = "";
320             public (long StatusId, string ScreenName)? inReplyTo = null;
321             public string imageService = "";      //画像投稿サービス名
322             public IMediaItem[] mediaItems = null;
323             public StatusTextHistory()
324             {
325             }
326             public StatusTextHistory(string status, (long StatusId, string ScreenName)? inReplyTo)
327             {
328                 this.status = status;
329                 this.inReplyTo = inReplyTo;
330             }
331         }
332
333         private void TweenMain_Activated(object sender, EventArgs e)
334         {
335             //画面がアクティブになったら、発言欄の背景色戻す
336             if (StatusText.Focused)
337             {
338                 this.StatusText_Enter(this.StatusText, System.EventArgs.Empty);
339             }
340         }
341
342         private bool disposed = false;
343
344         /// <summary>
345         /// 使用中のリソースをすべてクリーンアップします。
346         /// </summary>
347         /// <param name="disposing">マネージ リソースが破棄される場合 true、破棄されない場合は false です。</param>
348         protected override void Dispose(bool disposing)
349         {
350             base.Dispose(disposing);
351
352             if (this.disposed)
353                 return;
354
355             if (disposing)
356             {
357                 this.components?.Dispose();
358
359                 //後始末
360                 SearchDialog.Dispose();
361                 UrlDialog.Dispose();
362                 NIconAt?.Dispose();
363                 NIconAtRed?.Dispose();
364                 NIconAtSmoke?.Dispose();
365                 foreach (var iconRefresh in this.NIconRefresh)
366                 {
367                     iconRefresh?.Dispose();
368                 }
369                 TabIcon?.Dispose();
370                 MainIcon?.Dispose();
371                 ReplyIcon?.Dispose();
372                 ReplyIconBlink?.Dispose();
373                 _listViewImageList.Dispose();
374                 _brsHighLight.Dispose();
375                 _brsBackColorMine?.Dispose();
376                 _brsBackColorAt?.Dispose();
377                 _brsBackColorYou?.Dispose();
378                 _brsBackColorAtYou?.Dispose();
379                 _brsBackColorAtFromTarget?.Dispose();
380                 _brsBackColorAtTo?.Dispose();
381                 _brsBackColorNone?.Dispose();
382                 _brsDeactiveSelection?.Dispose();
383                 //sf.Dispose();
384                 sfTab.Dispose();
385
386                 this.workerCts.Cancel();
387
388                 if (IconCache != null)
389                 {
390                     this.IconCache.CancelAsync();
391                     this.IconCache.Dispose();
392                 }
393
394                 this.thumbnailTokenSource?.Dispose();
395
396                 this.tw.Dispose();
397                 this.twitterApi.Dispose();
398                 this._hookGlobalHotkey.Dispose();
399             }
400
401             // 終了時にRemoveHandlerしておかないとメモリリークする
402             // http://msdn.microsoft.com/ja-jp/library/microsoft.win32.systemevents.powermodechanged.aspx
403             Microsoft.Win32.SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
404             Microsoft.Win32.SystemEvents.TimeChanged -= SystemEvents_TimeChanged;
405
406             this.disposed = true;
407         }
408
409         private void LoadIcons()
410         {
411             // Icons フォルダ以下のアイコンを読み込み(着せ替えアイコン対応)
412             var iconsDir = Path.Combine(Application.StartupPath, "Icons");
413
414             // ウィンドウ左上のアイコン
415             var iconMain = this.LoadIcon(Path.Combine(iconsDir, "MIcon.ico"));
416
417             // タブ見出し未読表示アイコン
418             var iconTab = this.LoadIcon(Path.Combine(iconsDir, "Tab.ico"));
419
420             // タスクトレイ: 通常時アイコン
421             var iconAt = this.LoadIcon(Path.Combine(iconsDir, "At.ico"));
422
423             // タスクトレイ: エラー時アイコン
424             var iconAtRed = this.LoadIcon(Path.Combine(iconsDir, "AtRed.ico"));
425
426             // タスクトレイ: オフライン時アイコン
427             var iconAtSmoke = this.LoadIcon(Path.Combine(iconsDir, "AtSmoke.ico"));
428
429             // タスクトレイ: Reply通知アイコン (最大2枚でアニメーション可能)
430             var iconReply = this.LoadIcon(Path.Combine(iconsDir, "Reply.ico"));
431             var iconReplyBlink = this.LoadIcon(Path.Combine(iconsDir, "ReplyBlink.ico"));
432
433             // タスクトレイ: 更新中アイコン (最大4枚でアニメーション可能)
434             var iconRefresh1 = this.LoadIcon(Path.Combine(iconsDir, "Refresh.ico"));
435             var iconRefresh2 = this.LoadIcon(Path.Combine(iconsDir, "Refresh2.ico"));
436             var iconRefresh3 = this.LoadIcon(Path.Combine(iconsDir, "Refresh3.ico"));
437             var iconRefresh4 = this.LoadIcon(Path.Combine(iconsDir, "Refresh4.ico"));
438
439             // 読み込んだアイコンを設定 (不足するアイコンはデフォルトのものを設定)
440
441             this.MainIcon = iconMain ?? Properties.Resources.MIcon;
442             this.TabIcon = iconTab ?? Properties.Resources.TabIcon;
443             this.NIconAt = iconAt ?? iconMain ?? Properties.Resources.At;
444             this.NIconAtRed = iconAtRed ?? Properties.Resources.AtRed;
445             this.NIconAtSmoke = iconAtSmoke ?? Properties.Resources.AtSmoke;
446
447             if (iconReply != null && iconReplyBlink != null)
448             {
449                 this.ReplyIcon = iconReply;
450                 this.ReplyIconBlink = iconReplyBlink;
451             }
452             else
453             {
454                 this.ReplyIcon = iconReply ?? iconReplyBlink ?? Properties.Resources.Reply;
455                 this.ReplyIconBlink = this.NIconAt;
456             }
457
458             if (iconRefresh1 == null)
459             {
460                 this.NIconRefresh = new[] {
461                     Properties.Resources.Refresh, Properties.Resources.Refresh2,
462                     Properties.Resources.Refresh3, Properties.Resources.Refresh4,
463                 };
464             }
465             else if (iconRefresh2 == null)
466             {
467                 this.NIconRefresh = new[] { iconRefresh1 };
468             }
469             else if (iconRefresh3 == null)
470             {
471                 this.NIconRefresh = new[] { iconRefresh1, iconRefresh2 };
472             }
473             else if (iconRefresh4 == null)
474             {
475                 this.NIconRefresh = new[] { iconRefresh1, iconRefresh2, iconRefresh3 };
476             }
477             else // iconRefresh1 から iconRefresh4 まで全て揃っている
478             {
479                 this.NIconRefresh = new[] { iconRefresh1, iconRefresh2, iconRefresh3, iconRefresh4 };
480             }
481         }
482
483         private Icon LoadIcon(string filePath)
484         {
485             if (!File.Exists(filePath))
486                 return null;
487
488             try
489             {
490                 return new Icon(filePath);
491             }
492             catch (Exception)
493             {
494                 return null;
495             }
496         }
497
498         private void InitColumns(ListView list, bool startup)
499         {
500             this.InitColumnText();
501
502             ColumnHeader[] columns = null;
503             try
504             {
505                 if (this._iconCol)
506                 {
507                     columns = new[]
508                     {
509                         new ColumnHeader(), // アイコン
510                         new ColumnHeader(), // 本文
511                     };
512
513                     columns[0].Text = this.ColumnText[0];
514                     columns[1].Text = this.ColumnText[2];
515
516                     if (startup)
517                     {
518                         var widthScaleFactor = this.CurrentAutoScaleDimensions.Width / SettingManager.Local.ScaleDimension.Width;
519
520                         columns[0].Width = ScaleBy(widthScaleFactor, SettingManager.Local.Width1);
521                         columns[1].Width = ScaleBy(widthScaleFactor, SettingManager.Local.Width3);
522                         columns[0].DisplayIndex = 0;
523                         columns[1].DisplayIndex = 1;
524                     }
525                     else
526                     {
527                         var idx = 0;
528                         foreach (var curListColumn in this._curList.Columns.Cast<ColumnHeader>())
529                         {
530                             columns[idx].Width = curListColumn.Width;
531                             columns[idx].DisplayIndex = curListColumn.DisplayIndex;
532                             idx++;
533                         }
534                     }
535                 }
536                 else
537                 {
538                     columns = new[]
539                     {
540                         new ColumnHeader(), // アイコン
541                         new ColumnHeader(), // ニックネーム
542                         new ColumnHeader(), // 本文
543                         new ColumnHeader(), // 日付
544                         new ColumnHeader(), // ユーザID
545                         new ColumnHeader(), // 未読
546                         new ColumnHeader(), // マーク&プロテクト
547                         new ColumnHeader(), // ソース
548                     };
549
550                     foreach (var i in Enumerable.Range(0, columns.Length))
551                         columns[i].Text = this.ColumnText[i];
552
553                     if (startup)
554                     {
555                         var widthScaleFactor = this.CurrentAutoScaleDimensions.Width / SettingManager.Local.ScaleDimension.Width;
556
557                         columns[0].Width = ScaleBy(widthScaleFactor, SettingManager.Local.Width1);
558                         columns[1].Width = ScaleBy(widthScaleFactor, SettingManager.Local.Width2);
559                         columns[2].Width = ScaleBy(widthScaleFactor, SettingManager.Local.Width3);
560                         columns[3].Width = ScaleBy(widthScaleFactor, SettingManager.Local.Width4);
561                         columns[4].Width = ScaleBy(widthScaleFactor, SettingManager.Local.Width5);
562                         columns[5].Width = ScaleBy(widthScaleFactor, SettingManager.Local.Width6);
563                         columns[6].Width = ScaleBy(widthScaleFactor, SettingManager.Local.Width7);
564                         columns[7].Width = ScaleBy(widthScaleFactor, SettingManager.Local.Width8);
565
566                         var displayIndex = new[] {
567                             SettingManager.Local.DisplayIndex1, SettingManager.Local.DisplayIndex2,
568                             SettingManager.Local.DisplayIndex3, SettingManager.Local.DisplayIndex4,
569                             SettingManager.Local.DisplayIndex5, SettingManager.Local.DisplayIndex6,
570                             SettingManager.Local.DisplayIndex7, SettingManager.Local.DisplayIndex8
571                         };
572
573                         foreach (var i in Enumerable.Range(0, displayIndex.Length))
574                         {
575                             columns[i].DisplayIndex = displayIndex[i];
576                         }
577                     }
578                     else
579                     {
580                         var idx = 0;
581                         foreach (var curListColumn in this._curList.Columns.Cast<ColumnHeader>())
582                         {
583                             columns[idx].Width = curListColumn.Width;
584                             columns[idx].DisplayIndex = curListColumn.DisplayIndex;
585                             idx++;
586                         }
587                     }
588                 }
589
590                 list.Columns.AddRange(columns);
591
592                 columns = null;
593             }
594             finally
595             {
596                 if (columns != null)
597                 {
598                     foreach (var column in columns)
599                         column?.Dispose();
600                 }
601             }
602         }
603
604         private void InitColumnText()
605         {
606             ColumnText[0] = "";
607             ColumnText[1] = Properties.Resources.AddNewTabText2;
608             ColumnText[2] = Properties.Resources.AddNewTabText3;
609             ColumnText[3] = Properties.Resources.AddNewTabText4_2;
610             ColumnText[4] = Properties.Resources.AddNewTabText5;
611             ColumnText[5] = "";
612             ColumnText[6] = "";
613             ColumnText[7] = "Source";
614
615             ColumnOrgText[0] = "";
616             ColumnOrgText[1] = Properties.Resources.AddNewTabText2;
617             ColumnOrgText[2] = Properties.Resources.AddNewTabText3;
618             ColumnOrgText[3] = Properties.Resources.AddNewTabText4_2;
619             ColumnOrgText[4] = Properties.Resources.AddNewTabText5;
620             ColumnOrgText[5] = "";
621             ColumnOrgText[6] = "";
622             ColumnOrgText[7] = "Source";
623
624             int c = 0;
625             switch (_statuses.SortMode)
626             {
627                 case ComparerMode.Nickname:  //ニックネーム
628                     c = 1;
629                     break;
630                 case ComparerMode.Data:  //本文
631                     c = 2;
632                     break;
633                 case ComparerMode.Id:  //時刻=発言Id
634                     c = 3;
635                     break;
636                 case ComparerMode.Name:  //名前
637                     c = 4;
638                     break;
639                 case ComparerMode.Source:  //Source
640                     c = 7;
641                     break;
642             }
643
644             if (_iconCol)
645             {
646                 if (_statuses.SortOrder == SortOrder.Descending)
647                 {
648                     // U+25BE BLACK DOWN-POINTING SMALL TRIANGLE
649                     ColumnText[2] = ColumnOrgText[2] + "▾";
650                 }
651                 else
652                 {
653                     // U+25B4 BLACK UP-POINTING SMALL TRIANGLE
654                     ColumnText[2] = ColumnOrgText[2] + "▴";
655                 }
656             }
657             else
658             {
659                 if (_statuses.SortOrder == SortOrder.Descending)
660                 {
661                     // U+25BE BLACK DOWN-POINTING SMALL TRIANGLE
662                     ColumnText[c] = ColumnOrgText[c] + "▾";
663                 }
664                 else
665                 {
666                     // U+25B4 BLACK UP-POINTING SMALL TRIANGLE
667                     ColumnText[c] = ColumnOrgText[c] + "▴";
668                 }
669             }
670         }
671
672         private void InitializeTraceFrag()
673         {
674 #if DEBUG
675             TraceOutToolStripMenuItem.Checked = true;
676             MyCommon.TraceFlag = true;
677 #endif
678             if (!MyCommon.FileVersion.EndsWith("0", StringComparison.Ordinal))
679             {
680                 TraceOutToolStripMenuItem.Checked = true;
681                 MyCommon.TraceFlag = true;
682             }
683         }
684
685         private void TweenMain_Load(object sender, EventArgs e)
686         {
687             _ignoreConfigSave = true;
688             this.Visible = false;
689
690             if (MyApplication.StartupOptions.ContainsKey("d"))
691                 MyCommon.TraceFlag = true;
692
693             InitializeTraceFrag();
694
695             //Win32Api.SetProxy(HttpConnection.ProxyType.Specified, "127.0.0.1", 8080, "user", "pass")
696
697             MyCommon.TwitterApiInfo.AccessLimitUpdated += TwitterApiStatus_AccessLimitUpdated;
698             Microsoft.Win32.SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
699             Microsoft.Win32.SystemEvents.TimeChanged += SystemEvents_TimeChanged;
700
701             Regex.CacheSize = 100;
702
703             //発言保持クラス
704             _statuses = TabInformations.GetInstance();
705
706             //アイコン設定
707             LoadIcons();
708             this.Icon = MainIcon;              //メインフォーム(TweenMain)
709             NotifyIcon1.Icon = NIconAt;      //タスクトレイ
710             TabImage.Images.Add(TabIcon);    //タブ見出し
711
712             //<<<<<<<<<設定関連>>>>>>>>>
713             ////設定読み出し
714             LoadConfig();
715
716             // 現在の DPI と設定保存時の DPI との比を取得する
717             var configScaleFactor = SettingManager.Local.GetConfigScaleFactor(this.CurrentAutoScaleDimensions);
718
719             // UIフォント設定
720             var fontUIGlobal = SettingManager.Local.FontUIGlobal;
721             if (fontUIGlobal != null)
722             {
723                 OTBaseForm.GlobalFont = fontUIGlobal;
724                 this.Font = fontUIGlobal;
725             }
726
727             //不正値チェック
728             if (!MyApplication.StartupOptions.ContainsKey("nolimit"))
729             {
730                 if (SettingManager.Common.TimelinePeriod < 15 && SettingManager.Common.TimelinePeriod > 0)
731                     SettingManager.Common.TimelinePeriod = 15;
732
733                 if (SettingManager.Common.ReplyPeriod < 15 && SettingManager.Common.ReplyPeriod > 0)
734                     SettingManager.Common.ReplyPeriod = 15;
735
736                 if (SettingManager.Common.DMPeriod < 15 && SettingManager.Common.DMPeriod > 0)
737                     SettingManager.Common.DMPeriod = 15;
738
739                 if (SettingManager.Common.PubSearchPeriod < 30 && SettingManager.Common.PubSearchPeriod > 0)
740                     SettingManager.Common.PubSearchPeriod = 30;
741
742                 if (SettingManager.Common.UserTimelinePeriod < 15 && SettingManager.Common.UserTimelinePeriod > 0)
743                     SettingManager.Common.UserTimelinePeriod = 15;
744
745                 if (SettingManager.Common.ListsPeriod < 15 && SettingManager.Common.ListsPeriod > 0)
746                     SettingManager.Common.ListsPeriod = 15;
747             }
748
749             if (!Twitter.VerifyApiResultCount(MyCommon.WORKERTYPE.Timeline, SettingManager.Common.CountApi))
750                 SettingManager.Common.CountApi = 60;
751             if (!Twitter.VerifyApiResultCount(MyCommon.WORKERTYPE.Reply, SettingManager.Common.CountApiReply))
752                 SettingManager.Common.CountApiReply = 40;
753
754             if (SettingManager.Common.MoreCountApi != 0 && !Twitter.VerifyMoreApiResultCount(SettingManager.Common.MoreCountApi))
755                 SettingManager.Common.MoreCountApi = 200;
756             if (SettingManager.Common.FirstCountApi != 0 && !Twitter.VerifyFirstApiResultCount(SettingManager.Common.FirstCountApi))
757                 SettingManager.Common.FirstCountApi = 100;
758
759             if (SettingManager.Common.FavoritesCountApi != 0 && !Twitter.VerifyApiResultCount(MyCommon.WORKERTYPE.Favorites, SettingManager.Common.FavoritesCountApi))
760                 SettingManager.Common.FavoritesCountApi = 40;
761             if (SettingManager.Common.ListCountApi != 0 && !Twitter.VerifyApiResultCount(MyCommon.WORKERTYPE.List, SettingManager.Common.ListCountApi))
762                 SettingManager.Common.ListCountApi = 100;
763             if (SettingManager.Common.SearchCountApi != 0 && !Twitter.VerifyApiResultCount(MyCommon.WORKERTYPE.PublicSearch, SettingManager.Common.SearchCountApi))
764                 SettingManager.Common.SearchCountApi = 100;
765             if (SettingManager.Common.UserTimelineCountApi != 0 && !Twitter.VerifyApiResultCount(MyCommon.WORKERTYPE.UserTimeline, SettingManager.Common.UserTimelineCountApi))
766                 SettingManager.Common.UserTimelineCountApi = 20;
767
768             //廃止サービスが選択されていた場合ux.nuへ読み替え
769             if (SettingManager.Common.AutoShortUrlFirst < 0)
770                 SettingManager.Common.AutoShortUrlFirst = MyCommon.UrlConverter.Uxnu;
771
772             TwitterApiConnection.RestApiHost = SettingManager.Common.TwitterApiHost;
773             this.tw = new Twitter(this.twitterApi);
774
775             //認証関連
776             if (string.IsNullOrEmpty(SettingManager.Common.Token)) SettingManager.Common.UserName = "";
777             tw.Initialize(SettingManager.Common.Token, SettingManager.Common.TokenSecret, SettingManager.Common.UserName, SettingManager.Common.UserId);
778
779             _initial = true;
780
781             Networking.Initialize();
782
783             bool saveRequired = false;
784             bool firstRun = false;
785
786             //ユーザー名、パスワードが未設定なら設定画面を表示(初回起動時など)
787             if (string.IsNullOrEmpty(tw.Username))
788             {
789                 saveRequired = true;
790                 firstRun = true;
791
792                 //設定せずにキャンセルされたか、設定されたが依然ユーザー名が未設定ならプログラム終了
793                 if (ShowSettingDialog(showTaskbarIcon: true) != DialogResult.OK ||
794                     string.IsNullOrEmpty(tw.Username))
795                 {
796                     Application.Exit();  //強制終了
797                     return;
798                 }
799             }
800
801             //Twitter用通信クラス初期化
802             Networking.DefaultTimeout = TimeSpan.FromSeconds(SettingManager.Common.DefaultTimeOut);
803             Networking.UploadImageTimeout = TimeSpan.FromSeconds(SettingManager.Common.UploadImageTimeout);
804             Networking.SetWebProxy(SettingManager.Local.ProxyType,
805                 SettingManager.Local.ProxyAddress, SettingManager.Local.ProxyPort,
806                 SettingManager.Local.ProxyUser, SettingManager.Local.ProxyPassword);
807             Networking.ForceIPv4 = SettingManager.Common.ForceIPv4;
808
809             TwitterApiConnection.RestApiHost = SettingManager.Common.TwitterApiHost;
810             tw.RestrictFavCheck = SettingManager.Common.RestrictFavCheck;
811             tw.ReadOwnPost = SettingManager.Common.ReadOwnPost;
812             tw.TrackWord = SettingManager.Common.TrackWord;
813             TrackToolStripMenuItem.Checked = !String.IsNullOrEmpty(tw.TrackWord);
814             tw.AllAtReply = SettingManager.Common.AllAtReply;
815             AllrepliesToolStripMenuItem.Checked = tw.AllAtReply;
816             ShortUrl.Instance.DisableExpanding = !SettingManager.Common.TinyUrlResolve;
817             ShortUrl.Instance.BitlyAccessToken = SettingManager.Common.BitlyAccessToken;
818             ShortUrl.Instance.BitlyId = SettingManager.Common.BilyUser;
819             ShortUrl.Instance.BitlyKey = SettingManager.Common.BitlyPwd;
820
821             // アクセストークンが有効であるか確認する
822             // ここが Twitter API への最初のアクセスになるようにすること
823             try
824             {
825                 this.tw.VerifyCredentials();
826             }
827             catch (WebApiException ex)
828             {
829                 MessageBox.Show(this, string.Format(Properties.Resources.StartupAuthError_Text, ex.Message),
830                     ApplicationSettings.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
831             }
832
833             //サムネイル関連の初期化
834             //プロキシ設定等の通信まわりの初期化が済んでから処理する
835             ThumbnailGenerator.InitializeGenerator();
836
837             var imgazyobizinet = ThumbnailGenerator.ImgAzyobuziNetInstance;
838             imgazyobizinet.Enabled = SettingManager.Common.EnableImgAzyobuziNet;
839             imgazyobizinet.DisabledInDM = SettingManager.Common.ImgAzyobuziNetDisabledInDM;
840
841             Thumbnail.Services.TonTwitterCom.GetApiConnection = () => this.twitterApi.Connection;
842
843             //画像投稿サービス
844             ImageSelector.Initialize(tw, this.tw.Configuration, SettingManager.Common.UseImageServiceName, SettingManager.Common.UseImageService);
845
846             //ハッシュタグ/@id関連
847             AtIdSupl = new AtIdSupplement(SettingManager.AtIdList.AtIdList, "@");
848             HashSupl = new AtIdSupplement(SettingManager.Common.HashTags, "#");
849             HashMgr = new HashtagManage(HashSupl,
850                                     SettingManager.Common.HashTags.ToArray(),
851                                     SettingManager.Common.HashSelected,
852                                     SettingManager.Common.HashIsPermanent,
853                                     SettingManager.Common.HashIsHead,
854                                     SettingManager.Common.HashIsNotAddToAtReply);
855             if (!string.IsNullOrEmpty(HashMgr.UseHash) && HashMgr.IsPermanent) HashStripSplitButton.Text = HashMgr.UseHash;
856
857             //アイコンリスト作成
858             this.IconCache = new ImageCache();
859             this.tweetDetailsView.IconCache = this.IconCache;
860
861             //フォント&文字色&背景色保持
862             _fntUnread = SettingManager.Local.FontUnread;
863             _clUnread = SettingManager.Local.ColorUnread;
864             _fntReaded = SettingManager.Local.FontRead;
865             _clReaded = SettingManager.Local.ColorRead;
866             _clFav = SettingManager.Local.ColorFav;
867             _clOWL = SettingManager.Local.ColorOWL;
868             _clRetweet = SettingManager.Local.ColorRetweet;
869             _fntDetail = SettingManager.Local.FontDetail;
870             _clDetail = SettingManager.Local.ColorDetail;
871             _clDetailLink = SettingManager.Local.ColorDetailLink;
872             _clDetailBackcolor = SettingManager.Local.ColorDetailBackcolor;
873             _clSelf = SettingManager.Local.ColorSelf;
874             _clAtSelf = SettingManager.Local.ColorAtSelf;
875             _clTarget = SettingManager.Local.ColorTarget;
876             _clAtTarget = SettingManager.Local.ColorAtTarget;
877             _clAtFromTarget = SettingManager.Local.ColorAtFromTarget;
878             _clAtTo = SettingManager.Local.ColorAtTo;
879             _clListBackcolor = SettingManager.Local.ColorListBackcolor;
880             _clInputBackcolor = SettingManager.Local.ColorInputBackcolor;
881             _clInputFont = SettingManager.Local.ColorInputFont;
882             _fntInputFont = SettingManager.Local.FontInputFont;
883
884             _brsBackColorMine = new SolidBrush(_clSelf);
885             _brsBackColorAt = new SolidBrush(_clAtSelf);
886             _brsBackColorYou = new SolidBrush(_clTarget);
887             _brsBackColorAtYou = new SolidBrush(_clAtTarget);
888             _brsBackColorAtFromTarget = new SolidBrush(_clAtFromTarget);
889             _brsBackColorAtTo = new SolidBrush(_clAtTo);
890             //_brsBackColorNone = new SolidBrush(Color.FromKnownColor(KnownColor.Window));
891             _brsBackColorNone = new SolidBrush(_clListBackcolor);
892
893             // StringFormatオブジェクトへの事前設定
894             //sf.Alignment = StringAlignment.Near;             // Textを近くへ配置(左から右の場合は左寄せ)
895             //sf.LineAlignment = StringAlignment.Near;         // Textを近くへ配置(上寄せ)
896             //sf.FormatFlags = StringFormatFlags.LineLimit;    // 
897             sfTab.Alignment = StringAlignment.Center;
898             sfTab.LineAlignment = StringAlignment.Center;
899
900             InitDetailHtmlFormat();
901
902             //Regex statregex = new Regex("^0*");
903             this.recommendedStatusFooter = " [TWNv" + Regex.Replace(MyCommon.FileVersion.Replace(".", ""), "^0*", "") + "]";
904
905             _history.Add(new StatusTextHistory());
906             _hisIdx = 0;
907             this.inReplyTo = null;
908
909             //各種ダイアログ設定
910             SearchDialog.Owner = this;
911             UrlDialog.Owner = this;
912
913             //新着バルーン通知のチェック状態設定
914             NewPostPopMenuItem.Checked = SettingManager.Common.NewAllPop;
915             this.NotifyFileMenuItem.Checked = NewPostPopMenuItem.Checked;
916
917             //新着取得時のリストスクロールをするか。trueならスクロールしない
918             ListLockMenuItem.Checked = SettingManager.Common.ListLock;
919             this.LockListFileMenuItem.Checked = SettingManager.Common.ListLock;
920             //サウンド再生(タブ別設定より優先)
921             this.PlaySoundMenuItem.Checked = SettingManager.Common.PlaySound;
922             this.PlaySoundFileMenuItem.Checked = SettingManager.Common.PlaySound;
923
924             //ウィンドウ設定
925             this.ClientSize = ScaleBy(configScaleFactor, SettingManager.Local.FormSize);
926             _mySize = this.ClientSize; // サイズ保持(最小化・最大化されたまま終了した場合の対応用)
927             _myLoc = SettingManager.Local.FormLocation;
928             //タイトルバー領域
929             if (this.WindowState != FormWindowState.Minimized)
930             {
931                 Rectangle tbarRect = new Rectangle(this._myLoc, new Size(_mySize.Width, SystemInformation.CaptionHeight));
932                 bool outOfScreen = true;
933                 if (Screen.AllScreens.Length == 1)    //ハングするとの報告
934                 {
935                     foreach (Screen scr in Screen.AllScreens)
936                     {
937                         if (!Rectangle.Intersect(tbarRect, scr.Bounds).IsEmpty)
938                         {
939                             outOfScreen = false;
940                             break;
941                         }
942                     }
943
944                     if (outOfScreen)
945                         this._myLoc = new Point(0, 0);
946                 }
947                 this.DesktopLocation = this._myLoc;
948             }
949             this.TopMost = SettingManager.Common.AlwaysTop;
950             _mySpDis = ScaleBy(configScaleFactor.Height, SettingManager.Local.SplitterDistance);
951             _mySpDis2 = ScaleBy(configScaleFactor.Height, SettingManager.Local.StatusTextHeight);
952             if (SettingManager.Local.PreviewDistance == -1)
953             {
954                 _mySpDis3 = _mySize.Width - ScaleBy(this.CurrentScaleFactor.Width, 150);
955                 if (_mySpDis3 < 1) _mySpDis3 = ScaleBy(this.CurrentScaleFactor.Width, 50);
956                 SettingManager.Local.PreviewDistance = _mySpDis3;
957             }
958             else
959             {
960                 _mySpDis3 = ScaleBy(configScaleFactor.Width, SettingManager.Local.PreviewDistance);
961             }
962             //this.Tween_ClientSizeChanged(this, null);
963             this.PlaySoundMenuItem.Checked = SettingManager.Common.PlaySound;
964             this.PlaySoundFileMenuItem.Checked = SettingManager.Common.PlaySound;
965             //入力欄
966             StatusText.Font = _fntInputFont;
967             StatusText.ForeColor = _clInputFont;
968
969             // SplitContainer2.Panel2MinSize を一行表示の入力欄の高さに合わせる (MS UI Gothic 12pt (96dpi) の場合は 19px)
970             this.StatusText.Multiline = false; // SettingManager.Local.StatusMultiline の設定は後で反映される
971             this.SplitContainer2.Panel2MinSize = this.StatusText.Height;
972
973             // 必要であれば、発言一覧と発言詳細部・入力欄の上下を入れ替える
974             SplitContainer1.IsPanelInverted = !SettingManager.Common.StatusAreaAtBottom;
975
976             //全新着通知のチェック状態により、Reply&DMの新着通知有効無効切り替え(タブ別設定にするため削除予定)
977             if (SettingManager.Common.UnreadManage == false)
978             {
979                 ReadedStripMenuItem.Enabled = false;
980                 UnreadStripMenuItem.Enabled = false;
981             }
982
983             //リンク先URL表示部の初期化(画面左下)
984             StatusLabelUrl.Text = "";
985             //状態表示部の初期化(画面右下)
986             StatusLabel.Text = "";
987             StatusLabel.AutoToolTip = false;
988             StatusLabel.ToolTipText = "";
989             //文字カウンタ初期化
990             lblLen.Text = this.GetRestStatusCount(this.FormatStatusTextExtended("")).ToString();
991
992             this.JumpReadOpMenuItem.ShortcutKeyDisplayString = "Space";
993             CopySTOTMenuItem.ShortcutKeyDisplayString = "Ctrl+C";
994             CopyURLMenuItem.ShortcutKeyDisplayString = "Ctrl+Shift+C";
995             CopyUserIdStripMenuItem.ShortcutKeyDisplayString = "Shift+Alt+C";
996
997             // SourceLinkLabel のテキストが SplitContainer2.Panel2.AccessibleName にセットされるのを防ぐ
998             // (タブオーダー順で SourceLinkLabel の次にある PostBrowser が TabStop = false となっているため、
999             // さらに次のコントロールである SplitContainer2.Panel2 の AccessibleName がデフォルトで SourceLinkLabel のテキストになってしまう)
1000             this.SplitContainer2.Panel2.AccessibleName = "";
1001
1002             ////////////////////////////////////////////////////////////////////////////////
1003             var sortOrder = (SortOrder)SettingManager.Common.SortOrder;
1004             var mode = ComparerMode.Id;
1005             switch (SettingManager.Common.SortColumn)
1006             {
1007                 case 0:    //0:アイコン,5:未読マーク,6:プロテクト・フィルターマーク
1008                 case 5:
1009                 case 6:
1010                     //ソートしない
1011                     mode = ComparerMode.Id;  //Idソートに読み替え
1012                     break;
1013                 case 1:  //ニックネーム
1014                     mode = ComparerMode.Nickname;
1015                     break;
1016                 case 2:  //本文
1017                     mode = ComparerMode.Data;
1018                     break;
1019                 case 3:  //時刻=発言Id
1020                     mode = ComparerMode.Id;
1021                     break;
1022                 case 4:  //名前
1023                     mode = ComparerMode.Name;
1024                     break;
1025                 case 7:  //Source
1026                     mode = ComparerMode.Source;
1027                     break;
1028             }
1029             _statuses.SetSortMode(mode, sortOrder);
1030             ////////////////////////////////////////////////////////////////////////////////
1031
1032             ApplyListViewIconSize(SettingManager.Common.IconSize);
1033
1034             //<<<<<<<<タブ関連>>>>>>>
1035             // タブの位置を調整する
1036             SetTabAlignment();
1037
1038             //デフォルトタブの存在チェック、ない場合には追加
1039             if (this._statuses.GetTabByType<HomeTabModel>() == null)
1040                 this._statuses.AddTab(new HomeTabModel());
1041
1042             if (this._statuses.GetTabByType<MentionsTabModel>() == null)
1043                 this._statuses.AddTab(new MentionsTabModel());
1044
1045             if (this._statuses.GetTabByType<DirectMessagesTabModel>() == null)
1046                 this._statuses.AddTab(new DirectMessagesTabModel());
1047
1048             if (this._statuses.GetTabByType<FavoritesTabModel>() == null)
1049                 this._statuses.AddTab(new FavoritesTabModel());
1050
1051             if (this._statuses.GetTabByType<MuteTabModel>() == null)
1052                 this._statuses.AddTab(new MuteTabModel());
1053
1054             foreach (var tab in _statuses.Tabs.Values)
1055             {
1056                 // ミュートタブは表示しない
1057                 if (tab.TabType == MyCommon.TabUsageType.Mute)
1058                     continue;
1059
1060                 if (!AddNewTab(tab, startup: true))
1061                     throw new TabException(Properties.Resources.TweenMain_LoadText1);
1062             }
1063
1064             _curTab = ListTab.SelectedTab;
1065             _curItemIndex = -1;
1066             _curList = (DetailsListView)_curTab.Tag;
1067
1068             if (SettingManager.Common.TabIconDisp)
1069             {
1070                 ListTab.DrawMode = TabDrawMode.Normal;
1071             }
1072             else
1073             {
1074                 ListTab.DrawMode = TabDrawMode.OwnerDrawFixed;
1075                 ListTab.DrawItem += ListTab_DrawItem;
1076                 ListTab.ImageList = null;
1077             }
1078
1079             if (SettingManager.Common.HotkeyEnabled)
1080             {
1081                 //////グローバルホットキーの登録
1082                 HookGlobalHotkey.ModKeys modKey = HookGlobalHotkey.ModKeys.None;
1083                 if ((SettingManager.Common.HotkeyModifier & Keys.Alt) == Keys.Alt)
1084                     modKey |= HookGlobalHotkey.ModKeys.Alt;
1085                 if ((SettingManager.Common.HotkeyModifier & Keys.Control) == Keys.Control)
1086                     modKey |= HookGlobalHotkey.ModKeys.Ctrl;
1087                 if ((SettingManager.Common.HotkeyModifier & Keys.Shift) == Keys.Shift)
1088                     modKey |= HookGlobalHotkey.ModKeys.Shift;
1089                 if ((SettingManager.Common.HotkeyModifier & Keys.LWin) == Keys.LWin)
1090                     modKey |= HookGlobalHotkey.ModKeys.Win;
1091
1092                 _hookGlobalHotkey.RegisterOriginalHotkey(SettingManager.Common.HotkeyKey, SettingManager.Common.HotkeyValue, modKey);
1093             }
1094
1095             if (SettingManager.Common.IsUseNotifyGrowl)
1096                 gh.RegisterGrowl();
1097
1098             StatusLabel.Text = Properties.Resources.Form1_LoadText1;       //画面右下の状態表示を変更
1099
1100             SetMainWindowTitle();
1101             SetNotifyIconText();
1102
1103             if (!SettingManager.Common.MinimizeToTray || this.WindowState != FormWindowState.Minimized)
1104             {
1105                 this.Visible = true;
1106             }
1107
1108             //タイマー設定
1109
1110             var streamingRefreshInterval = TimeSpan.FromSeconds(SettingManager.Common.UserstreamPeriod);
1111             this.RefreshThrottlingTimer = new ThrottlingTimer(streamingRefreshInterval,
1112                 () => this.InvokeAsync(() => this.RefreshTimeline()));
1113
1114             TimerTimeline.AutoReset = true;
1115             TimerTimeline.SynchronizingObject = this;
1116             //Recent取得間隔
1117             TimerTimeline.Interval = 1000;
1118             TimerTimeline.Enabled = true;
1119             //更新中アイコンアニメーション間隔
1120             TimerRefreshIcon.Interval = 200;
1121             TimerRefreshIcon.Enabled = true;
1122
1123             _ignoreConfigSave = false;
1124             this.TweenMain_Resize(null, null);
1125             if (saveRequired) SaveConfigsAll(false);
1126
1127             foreach (var ua in SettingManager.Common.UserAccounts)
1128             {
1129                 if (ua.UserId == 0 && ua.Username.Equals(tw.Username, StringComparison.InvariantCultureIgnoreCase))
1130                 {
1131                     ua.UserId = tw.UserId;
1132                     break;
1133                 }
1134             }
1135
1136             if (firstRun)
1137             {
1138                 // 初回起動時だけ右下のメニューを目立たせる
1139                 HashStripSplitButton.ShowDropDown();
1140             }
1141         }
1142
1143         private void InitDetailHtmlFormat()
1144         {
1145             if (SettingManager.Common.IsMonospace)
1146             {
1147                 detailHtmlFormatHeader = detailHtmlFormatHeaderMono;
1148                 detailHtmlFormatFooter = detailHtmlFormatFooterMono;
1149             }
1150             else
1151             {
1152                 detailHtmlFormatHeader = detailHtmlFormatHeaderColor;
1153                 detailHtmlFormatFooter = detailHtmlFormatFooterColor;
1154             }
1155
1156             detailHtmlFormatHeader = detailHtmlFormatHeader
1157                     .Replace("%FONT_FAMILY%", _fntDetail.Name)
1158                     .Replace("%FONT_SIZE%", _fntDetail.Size.ToString())
1159                     .Replace("%FONT_COLOR%", $"{_clDetail.R},{_clDetail.G},{_clDetail.B}")
1160                     .Replace("%LINK_COLOR%", $"{_clDetailLink.R},{_clDetailLink.G},{_clDetailLink.B}")
1161                     .Replace("%BG_COLOR%", $"{_clDetailBackcolor.R},{_clDetailBackcolor.G},{_clDetailBackcolor.B}")
1162                     .Replace("%BG_REPLY_COLOR%", $"{_clAtTo.R}, {_clAtTo.G}, {_clAtTo.B}");
1163         }
1164
1165         private void ListTab_DrawItem(object sender, DrawItemEventArgs e)
1166         {
1167             string txt;
1168             try
1169             {
1170                 txt = ListTab.TabPages[e.Index].Text;
1171             }
1172             catch (Exception)
1173             {
1174                 return;
1175             }
1176
1177             e.Graphics.FillRectangle(System.Drawing.SystemBrushes.Control, e.Bounds);
1178             if (e.State == DrawItemState.Selected)
1179             {
1180                 e.DrawFocusRectangle();
1181             }
1182             Brush fore;
1183             try
1184             {
1185                 if (_statuses.Tabs[txt].UnreadCount > 0)
1186                     fore = Brushes.Red;
1187                 else
1188                     fore = System.Drawing.SystemBrushes.ControlText;
1189             }
1190             catch (Exception)
1191             {
1192                 fore = System.Drawing.SystemBrushes.ControlText;
1193             }
1194             e.Graphics.DrawString(txt, e.Font, fore, e.Bounds, sfTab);
1195         }
1196
1197         private void LoadConfig()
1198         {
1199             SettingManager.Local = SettingManager.Local;
1200
1201             // v1.2.4 以前の設定には ScaleDimension の項目がないため、現在の DPI と同じとして扱う
1202             if (SettingManager.Local.ScaleDimension.IsEmpty)
1203                 SettingManager.Local.ScaleDimension = this.CurrentAutoScaleDimensions;
1204
1205             var tabSettings = SettingManager.Tabs;
1206             foreach (var tabSetting in tabSettings.Tabs)
1207             {
1208                 TabModel tab;
1209                 switch (tabSetting.TabType)
1210                 {
1211                     case MyCommon.TabUsageType.Home:
1212                         tab = new HomeTabModel(tabSetting.TabName);
1213                         break;
1214                     case MyCommon.TabUsageType.Mentions:
1215                         tab = new MentionsTabModel(tabSetting.TabName);
1216                         break;
1217                     case MyCommon.TabUsageType.DirectMessage:
1218                         tab = new DirectMessagesTabModel(tabSetting.TabName);
1219                         break;
1220                     case MyCommon.TabUsageType.Favorites:
1221                         tab = new FavoritesTabModel(tabSetting.TabName);
1222                         break;
1223                     case MyCommon.TabUsageType.UserDefined:
1224                         tab = new FilterTabModel(tabSetting.TabName);
1225                         break;
1226                     case MyCommon.TabUsageType.UserTimeline:
1227                         tab = new UserTimelineTabModel(tabSetting.TabName, tabSetting.User);
1228                         break;
1229                     case MyCommon.TabUsageType.PublicSearch:
1230                         tab = new PublicSearchTabModel(tabSetting.TabName)
1231                         {
1232                             SearchWords = tabSetting.SearchWords,
1233                             SearchLang = tabSetting.SearchLang,
1234                         };
1235                         break;
1236                     case MyCommon.TabUsageType.Lists:
1237                         tab = new ListTimelineTabModel(tabSetting.TabName, tabSetting.ListInfo);
1238                         break;
1239                     case MyCommon.TabUsageType.Mute:
1240                         tab = new MuteTabModel(tabSetting.TabName);
1241                         break;
1242                     default:
1243                         continue;
1244                 }
1245
1246                 tab.UnreadManage = tabSetting.UnreadManage;
1247                 tab.Protected = tabSetting.Protected;
1248                 tab.Notify = tabSetting.Notify;
1249                 tab.SoundFile = tabSetting.SoundFile;
1250
1251                 if (tab.IsDistributableTabType)
1252                 {
1253                     var filterTab = (FilterTabModel)tab;
1254                     filterTab.FilterArray = tabSetting.FilterArray;
1255                     filterTab.FilterModified = false;
1256                 }
1257
1258                 if (this._statuses.ContainsTab(tab.TabName))
1259                     tab.TabName = this._statuses.MakeTabName("MyTab");
1260
1261                 this._statuses.AddTab(tab);
1262             }
1263             if (_statuses.Tabs.Count == 0)
1264             {
1265                 _statuses.AddTab(new HomeTabModel());
1266                 _statuses.AddTab(new MentionsTabModel());
1267                 _statuses.AddTab(new DirectMessagesTabModel());
1268                 _statuses.AddTab(new FavoritesTabModel());
1269             }
1270         }
1271
1272         private void TimerInterval_Changed(object sender, IntervalChangedEventArgs e) //Handles SettingDialog.IntervalChanged
1273         {
1274             if (!TimerTimeline.Enabled) return;
1275
1276             if (e.UserStream)
1277             {
1278                 var interval = TimeSpan.FromSeconds(SettingManager.Common.UserstreamPeriod);
1279                 var newTimer = new ThrottlingTimer(interval, () => this.InvokeAsync(() => this.RefreshTimeline()));
1280                 var oldTimer = Interlocked.Exchange(ref this.RefreshThrottlingTimer, newTimer);
1281                 oldTimer.Dispose();
1282             }
1283
1284             ResetTimers = e;
1285         }
1286
1287         private IntervalChangedEventArgs ResetTimers = IntervalChangedEventArgs.ResetAll;
1288
1289         private static int homeCounter = 0;
1290         private static int mentionCounter = 0;
1291         private static int dmCounter = 0;
1292         private static int pubSearchCounter = 0;
1293         private static int userTimelineCounter = 0;
1294         private static int listsCounter = 0;
1295         private static int ResumeWait = 0;
1296         private static int refreshFollowers = 0;
1297
1298         private async void TimerTimeline_Elapsed(object sender, EventArgs e)
1299         {
1300             if (homeCounter > 0) Interlocked.Decrement(ref homeCounter);
1301             if (mentionCounter > 0) Interlocked.Decrement(ref mentionCounter);
1302             if (dmCounter > 0) Interlocked.Decrement(ref dmCounter);
1303             if (pubSearchCounter > 0) Interlocked.Decrement(ref pubSearchCounter);
1304             if (userTimelineCounter > 0) Interlocked.Decrement(ref userTimelineCounter);
1305             if (listsCounter > 0) Interlocked.Decrement(ref listsCounter);
1306             Interlocked.Increment(ref refreshFollowers);
1307
1308             var refreshTasks = new List<Task>();
1309
1310             ////タイマー初期化
1311             if (ResetTimers.Timeline || homeCounter <= 0 && SettingManager.Common.TimelinePeriod > 0)
1312             {
1313                 Interlocked.Exchange(ref homeCounter, SettingManager.Common.TimelinePeriod);
1314                 if (!tw.IsUserstreamDataReceived && !ResetTimers.Timeline)
1315                     refreshTasks.Add(this.RefreshTabAsync<HomeTabModel>());
1316                 ResetTimers.Timeline = false;
1317             }
1318             if (ResetTimers.Reply || mentionCounter <= 0 && SettingManager.Common.ReplyPeriod > 0)
1319             {
1320                 Interlocked.Exchange(ref mentionCounter, SettingManager.Common.ReplyPeriod);
1321                 if (!tw.IsUserstreamDataReceived && !ResetTimers.Reply)
1322                     refreshTasks.Add(this.RefreshTabAsync<MentionsTabModel>());
1323                 ResetTimers.Reply = false;
1324             }
1325             if (ResetTimers.DirectMessage || dmCounter <= 0 && SettingManager.Common.DMPeriod > 0)
1326             {
1327                 Interlocked.Exchange(ref dmCounter, SettingManager.Common.DMPeriod);
1328                 if (!tw.IsUserstreamDataReceived && !ResetTimers.DirectMessage)
1329                     refreshTasks.Add(this.RefreshTabAsync<DirectMessagesTabModel>());
1330                 ResetTimers.DirectMessage = false;
1331             }
1332             if (ResetTimers.PublicSearch || pubSearchCounter <= 0 && SettingManager.Common.PubSearchPeriod > 0)
1333             {
1334                 Interlocked.Exchange(ref pubSearchCounter, SettingManager.Common.PubSearchPeriod);
1335                 if (!ResetTimers.PublicSearch)
1336                     refreshTasks.Add(this.RefreshTabAsync<PublicSearchTabModel>());
1337                 ResetTimers.PublicSearch = false;
1338             }
1339             if (ResetTimers.UserTimeline || userTimelineCounter <= 0 && SettingManager.Common.UserTimelinePeriod > 0)
1340             {
1341                 Interlocked.Exchange(ref userTimelineCounter, SettingManager.Common.UserTimelinePeriod);
1342                 if (!ResetTimers.UserTimeline)
1343                     refreshTasks.Add(this.RefreshTabAsync<UserTimelineTabModel>());
1344                 ResetTimers.UserTimeline = false;
1345             }
1346             if (ResetTimers.Lists || listsCounter <= 0 && SettingManager.Common.ListsPeriod > 0)
1347             {
1348                 Interlocked.Exchange(ref listsCounter, SettingManager.Common.ListsPeriod);
1349                 if (!ResetTimers.Lists)
1350                     refreshTasks.Add(this.RefreshTabAsync<ListTimelineTabModel>());
1351                 ResetTimers.Lists = false;
1352             }
1353             if (refreshFollowers > 6 * 3600)
1354             {
1355                 Interlocked.Exchange(ref refreshFollowers, 0);
1356                 refreshTasks.AddRange(new[]
1357                 {
1358                     this.doGetFollowersMenu(),
1359                     this.RefreshNoRetweetIdsAsync(),
1360                     this.RefreshTwitterConfigurationAsync(),
1361                 });
1362             }
1363             if (osResumed)
1364             {
1365                 Interlocked.Increment(ref ResumeWait);
1366                 if (ResumeWait > 30)
1367                 {
1368                     osResumed = false;
1369                     Interlocked.Exchange(ref ResumeWait, 0);
1370                     refreshTasks.AddRange(new[]
1371                     {
1372                         this.RefreshTabAsync<HomeTabModel>(),
1373                         this.RefreshTabAsync<MentionsTabModel>(),
1374                         this.RefreshTabAsync<DirectMessagesTabModel>(),
1375                         this.RefreshTabAsync<PublicSearchTabModel>(),
1376                         this.RefreshTabAsync<UserTimelineTabModel>(),
1377                         this.RefreshTabAsync<ListTimelineTabModel>(),
1378                         this.doGetFollowersMenu(),
1379                         this.RefreshTwitterConfigurationAsync(),
1380                     });
1381                 }
1382             }
1383
1384             await Task.WhenAll(refreshTasks);
1385         }
1386
1387         private void RefreshTimeline()
1388         {
1389             var curTabModel = this._statuses.Tabs[this._curTab.Text];
1390
1391             // 現在表示中のタブのスクロール位置を退避
1392             var curListScroll = this.SaveListViewScroll(this._curList, curTabModel);
1393
1394             // 各タブのリスト上の選択位置などを退避
1395             var listSelections = this.SaveListViewSelection();
1396
1397             //更新確定
1398             int addCount;
1399             addCount = _statuses.SubmitUpdate(out var soundFile, out var notifyPosts,
1400                 out var newMentionOrDm, out var isDelete);
1401
1402             if (MyCommon._endingFlag) return;
1403
1404             // リストに反映&選択状態復元
1405             foreach (var tabPage in this.ListTab.TabPages.Cast<TabPage>())
1406             {
1407                 var listView = (DetailsListView)tabPage.Tag;
1408                 var tabModel = this._statuses.Tabs[tabPage.Text];
1409
1410                 if (listView.VirtualListSize != tabModel.AllCount || isDelete)
1411                 {
1412                     using (ControlTransaction.Update(listView))
1413                     {
1414                         if (listView == this._curList)
1415                             this.PurgeListViewItemCache();
1416
1417                         try
1418                         {
1419                             // リスト件数更新
1420                             listView.VirtualListSize = tabModel.AllCount;
1421                         }
1422                         catch (NullReferenceException ex)
1423                         {
1424                             // WinForms 内部で ListView.set_TopItem が発生させている例外
1425                             // https://ja.osdn.net/ticket/browse.php?group_id=6526&tid=36588
1426                             MyCommon.TraceOut(ex, $"TabType: {tabModel.TabType}, Count: {tabModel.AllCount}, ListSize: {listView.VirtualListSize}");
1427                         }
1428
1429                         // 選択位置などを復元
1430                         this.RestoreListViewSelection(listView, tabModel, listSelections[tabModel.TabName]);
1431                     }
1432                 }
1433             }
1434
1435             if (addCount > 0)
1436             {
1437                 if (SettingManager.Common.TabIconDisp)
1438                 {
1439                     foreach (var tabPage in this.ListTab.TabPages.Cast<TabPage>())
1440                     {
1441                         var tabModel = this._statuses.Tabs[tabPage.Text];
1442                         if (tabModel.UnreadCount > 0 && tabPage.ImageIndex != 0)
1443                             tabPage.ImageIndex = 0; // 未読アイコン
1444                     }
1445                 }
1446                 else
1447                 {
1448                     this.ListTab.Refresh();
1449                 }
1450             }
1451
1452             // スクロール位置を復元
1453             this.RestoreListViewScroll(this._curList, curTabModel, curListScroll);
1454
1455             //新着通知
1456             NotifyNewPosts(notifyPosts, soundFile, addCount, newMentionOrDm);
1457
1458             SetMainWindowTitle();
1459             if (!StatusLabelUrl.Text.StartsWith("http", StringComparison.Ordinal)) SetStatusLabelUrl();
1460
1461             HashSupl.AddRangeItem(tw.GetHashList());
1462
1463         }
1464
1465         internal struct ListViewScroll
1466         {
1467             public ScrollLockMode ScrollLockMode { get; set; }
1468             public long? TopItemStatusId { get; set; }
1469         }
1470
1471         internal enum ScrollLockMode
1472         {
1473             /// <summary>固定しない</summary>
1474             None,
1475
1476             /// <summary>最上部に固定する</summary>
1477             FixedToTop,
1478
1479             /// <summary>最下部に固定する</summary>
1480             FixedToBottom,
1481
1482             /// <summary><see cref="ListViewScroll.TopItemStatusId"/> の位置に固定する</summary>
1483             FixedToItem,
1484         }
1485
1486         /// <summary>
1487         /// <see cref="ListView"/> のスクロール位置に関する情報を <see cref="ListViewScroll"/> として返します
1488         /// </summary>
1489         private ListViewScroll SaveListViewScroll(DetailsListView listView, TabModel tab)
1490         {
1491             var listScroll = new ListViewScroll
1492             {
1493                 ScrollLockMode = this.GetScrollLockMode(listView),
1494             };
1495
1496             if (listScroll.ScrollLockMode == ScrollLockMode.FixedToItem)
1497             {
1498                 var topItemIndex = listView.TopItem?.Index ?? -1;
1499                 if (topItemIndex != -1 && topItemIndex < tab.AllCount)
1500                     listScroll.TopItemStatusId = tab.GetStatusIdAt(topItemIndex);
1501             }
1502
1503             return listScroll;
1504         }
1505
1506         private ScrollLockMode GetScrollLockMode(DetailsListView listView)
1507         {
1508             if (this._statuses.SortMode == ComparerMode.Id)
1509             {
1510                 if (this._statuses.SortOrder == SortOrder.Ascending)
1511                 {
1512                     // Id昇順
1513                     if (this.ListLockMenuItem.Checked)
1514                         return ScrollLockMode.None;
1515
1516                     // 最下行が表示されていたら、最下行へ強制スクロール。最下行が表示されていなかったら制御しない
1517
1518                     // 一番下に表示されているアイテム
1519                     var bottomItem = listView.GetItemAt(0, listView.ClientSize.Height - 1);
1520                     if (bottomItem == null || bottomItem.Index == listView.VirtualListSize - 1)
1521                         return ScrollLockMode.FixedToBottom;
1522                     else
1523                         return ScrollLockMode.None;
1524                 }
1525                 else
1526                 {
1527                     // Id降順
1528                     if (this.ListLockMenuItem.Checked)
1529                         return ScrollLockMode.FixedToItem;
1530
1531                     // 最上行が表示されていたら、制御しない。最上行が表示されていなかったら、現在表示位置へ強制スクロール
1532                     var topItem = listView.TopItem;
1533                     if (topItem == null || topItem.Index == 0)
1534                         return ScrollLockMode.FixedToTop;
1535                     else
1536                         return ScrollLockMode.FixedToItem;
1537                 }
1538             }
1539             else
1540             {
1541                 return ScrollLockMode.FixedToItem;
1542             }
1543         }
1544
1545         internal struct ListViewSelection
1546         {
1547             public long[] SelectedStatusIds { get; set; }
1548             public long? SelectionMarkStatusId { get; set; }
1549             public long? FocusedStatusId { get; set; }
1550         }
1551
1552         /// <summary>
1553         /// <see cref="ListView"/> の選択状態を <see cref="ListViewSelection"/> として返します
1554         /// </summary>
1555         private IReadOnlyDictionary<string, ListViewSelection> SaveListViewSelection()
1556         {
1557             var listsDict = new Dictionary<string, ListViewSelection>();
1558
1559             foreach (var tabPage in this.ListTab.TabPages.Cast<TabPage>())
1560             {
1561                 var listView = (DetailsListView)tabPage.Tag;
1562                 var tab = _statuses.Tabs[tabPage.Text];
1563
1564                 listsDict[tab.TabName] = this.SaveListViewSelection(listView, tab);
1565             }
1566
1567             return listsDict;
1568         }
1569
1570         /// <summary>
1571         /// <see cref="ListView"/> の選択状態を <see cref="ListViewSelection"/> として返します
1572         /// </summary>
1573         private ListViewSelection SaveListViewSelection(DetailsListView listView, TabModel tab)
1574         {
1575             if (listView.VirtualListSize == 0)
1576             {
1577                 return new ListViewSelection
1578                 {
1579                     SelectedStatusIds = Array.Empty<long>(),
1580                     SelectionMarkStatusId = null,
1581                     FocusedStatusId = null,
1582                 };
1583             }
1584
1585             return new ListViewSelection
1586             {
1587                 SelectedStatusIds = this.GetSelectedStatusIds(listView, tab),
1588                 FocusedStatusId = this.GetFocusedStatusId(listView, tab),
1589                 SelectionMarkStatusId = this.GetSelectionMarkStatusId(listView, tab),
1590             };
1591         }
1592
1593         private long[] GetSelectedStatusIds(DetailsListView listView, TabModel tab)
1594         {
1595             var selectedIndices = listView.SelectedIndices;
1596             if (selectedIndices.Count > 0 && selectedIndices.Count < 61)
1597                 return tab.GetStatusIdAt(selectedIndices.Cast<int>());
1598             else
1599                 return null;
1600         }
1601
1602         private long? GetFocusedStatusId(DetailsListView listView, TabModel tab)
1603         {
1604             var index = listView.FocusedItem?.Index ?? -1;
1605
1606             return index != -1 && index < tab.AllCount ? tab.GetStatusIdAt(index) : (long?)null;
1607         }
1608
1609         private long? GetSelectionMarkStatusId(DetailsListView listView, TabModel tab)
1610         {
1611             var index = listView.SelectionMark;
1612
1613             return index != -1 && index < tab.AllCount ? tab.GetStatusIdAt(index) : (long?)null;
1614         }
1615
1616         /// <summary>
1617         /// <see cref="SaveListViewScroll"/> によって保存されたスクロール位置を復元します
1618         /// </summary>
1619         private void RestoreListViewScroll(DetailsListView listView, TabModel tab, ListViewScroll listScroll)
1620         {
1621             if (listView.VirtualListSize == 0)
1622                 return;
1623
1624             switch (listScroll.ScrollLockMode)
1625             {
1626                 case ScrollLockMode.FixedToTop:
1627                     listView.EnsureVisible(0);
1628                     break;
1629                 case ScrollLockMode.FixedToBottom:
1630                     listView.EnsureVisible(listView.VirtualListSize - 1);
1631                     break;
1632                 case ScrollLockMode.FixedToItem:
1633                     var topIndex = listScroll.TopItemStatusId != null ? tab.IndexOf(listScroll.TopItemStatusId.Value) : -1;
1634                     if (topIndex != -1)
1635                     {
1636                         var topItem = listView.Items[topIndex];
1637                         try
1638                         {
1639                             listView.TopItem = topItem;
1640                         }
1641                         catch (NullReferenceException)
1642                         {
1643                             listView.EnsureVisible(listView.VirtualListSize - 1);
1644                             listView.EnsureVisible(topIndex);
1645                         }
1646                     }
1647                     break;
1648                 case ScrollLockMode.None:
1649                 default:
1650                     break;
1651             }
1652         }
1653
1654         /// <summary>
1655         /// <see cref="SaveListViewSelection"/> によって保存された選択状態を復元します
1656         /// </summary>
1657         private void RestoreListViewSelection(DetailsListView listView, TabModel tab, ListViewSelection listSelection)
1658         {
1659             // status_id から ListView 上のインデックスに変換
1660             int[] selectedIndices = null;
1661             if (listSelection.SelectedStatusIds != null)
1662                 selectedIndices = tab.IndexOf(listSelection.SelectedStatusIds).Where(x => x != -1).ToArray();
1663
1664             var focusedIndex = -1;
1665             if (listSelection.FocusedStatusId != null)
1666                 focusedIndex = tab.IndexOf(listSelection.FocusedStatusId.Value);
1667
1668             var selectionMarkIndex = -1;
1669             if (listSelection.SelectionMarkStatusId != null)
1670                 selectionMarkIndex = tab.IndexOf(listSelection.SelectionMarkStatusId.Value);
1671
1672             this.SelectListItem(listView, selectedIndices, focusedIndex, selectionMarkIndex);
1673         }
1674
1675         private bool BalloonRequired()
1676         {
1677             var ev = new Twitter.FormattedEvent
1678             {
1679                 Eventtype = MyCommon.EVENTTYPE.None,
1680             };
1681
1682             return BalloonRequired(ev);
1683         }
1684
1685         private bool IsEventNotifyAsEventType(MyCommon.EVENTTYPE type)
1686         {
1687             if (type == MyCommon.EVENTTYPE.None)
1688                 return true;
1689
1690             if (!SettingManager.Common.EventNotifyEnabled)
1691                 return false;
1692
1693             return SettingManager.Common.EventNotifyFlag.HasFlag(type);
1694         }
1695
1696         private bool IsMyEventNotityAsEventType(Twitter.FormattedEvent ev)
1697         {
1698             if (!ev.IsMe)
1699                 return true;
1700
1701             return SettingManager.Common.IsMyEventNotifyFlag.HasFlag(ev.Eventtype);
1702         }
1703
1704         private bool BalloonRequired(Twitter.FormattedEvent ev)
1705         {
1706             if (this._initial)
1707                 return false;
1708
1709             if (NativeMethods.IsScreenSaverRunning())
1710                 return false;
1711
1712             // 「新着通知」が無効
1713             if (!this.NewPostPopMenuItem.Checked)
1714             {
1715                 // 「新着通知が無効でもイベントを通知する」にも該当しない
1716                 if (!SettingManager.Common.ForceEventNotify || ev.Eventtype == MyCommon.EVENTTYPE.None)
1717                     return false;
1718             }
1719
1720             // 「画面最小化・アイコン時のみバルーンを表示する」が有効
1721             if (SettingManager.Common.LimitBalloon)
1722             {
1723                 if (this.WindowState != FormWindowState.Minimized && this.Visible && Form.ActiveForm != null)
1724                     return false;
1725             }
1726
1727             return this.IsEventNotifyAsEventType(ev.Eventtype) && this.IsMyEventNotityAsEventType(ev);
1728         }
1729
1730         private void NotifyNewPosts(PostClass[] notifyPosts, string soundFile, int addCount, bool newMentions)
1731         {
1732             if (SettingManager.Common.ReadOwnPost)
1733             {
1734                 if (notifyPosts != null && notifyPosts.Length > 0 && notifyPosts.All(x => x.UserId == tw.UserId))
1735                     return;
1736             }
1737
1738             //新着通知
1739             if (BalloonRequired())
1740             {
1741                 if (notifyPosts != null && notifyPosts.Length > 0)
1742                 {
1743                     //Growlは一個ずつばらして通知。ただし、3ポスト以上あるときはまとめる
1744                     if (SettingManager.Common.IsUseNotifyGrowl)
1745                     {
1746                         StringBuilder sb = new StringBuilder();
1747                         bool reply = false;
1748                         bool dm = false;
1749
1750                         foreach (PostClass post in notifyPosts)
1751                         {
1752                             if (!(notifyPosts.Length > 3))
1753                             {
1754                                 sb.Clear();
1755                                 reply = false;
1756                                 dm = false;
1757                             }
1758                             if (post.IsReply && !post.IsExcludeReply) reply = true;
1759                             if (post.IsDm) dm = true;
1760                             if (sb.Length > 0) sb.Append(System.Environment.NewLine);
1761                             switch (SettingManager.Common.NameBalloon)
1762                             {
1763                                 case MyCommon.NameBalloonEnum.UserID:
1764                                     sb.Append(post.ScreenName).Append(" : ");
1765                                     break;
1766                                 case MyCommon.NameBalloonEnum.NickName:
1767                                     sb.Append(post.Nickname).Append(" : ");
1768                                     break;
1769                             }
1770                             sb.Append(post.TextFromApi);
1771                             if (notifyPosts.Length > 3)
1772                             {
1773                                 if (notifyPosts.Last() != post) continue;
1774                             }
1775
1776                             StringBuilder title = new StringBuilder();
1777                             GrowlHelper.NotifyType nt;
1778                             if (SettingManager.Common.DispUsername)
1779                             {
1780                                 title.Append(tw.Username);
1781                                 title.Append(" - ");
1782                             }
1783                             else
1784                             {
1785                                 //title.Clear();
1786                             }
1787                             if (dm)
1788                             {
1789                                 //NotifyIcon1.BalloonTipIcon = ToolTipIcon.Warning;
1790                                 //NotifyIcon1.BalloonTipTitle += Application.ProductName + " [DM] " + Properties.Resources.RefreshDirectMessageText1 + " " + addCount.ToString() + Properties.Resources.RefreshDirectMessageText2;
1791                                 title.Append(ApplicationSettings.ApplicationName);
1792                                 title.Append(" [DM] ");
1793                                 title.AppendFormat(Properties.Resources.RefreshTimeline_NotifyText, addCount);
1794                                 nt = GrowlHelper.NotifyType.DirectMessage;
1795                             }
1796                             else if (reply)
1797                             {
1798                                 //NotifyIcon1.BalloonTipIcon = ToolTipIcon.Warning;
1799                                 //NotifyIcon1.BalloonTipTitle += Application.ProductName + " [Reply!] " + Properties.Resources.RefreshTimelineText1 + " " + addCount.ToString() + Properties.Resources.RefreshTimelineText2;
1800                                 title.Append(ApplicationSettings.ApplicationName);
1801                                 title.Append(" [Reply!] ");
1802                                 title.AppendFormat(Properties.Resources.RefreshTimeline_NotifyText, addCount);
1803                                 nt = GrowlHelper.NotifyType.Reply;
1804                             }
1805                             else
1806                             {
1807                                 //NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
1808                                 //NotifyIcon1.BalloonTipTitle += Application.ProductName + " " + Properties.Resources.RefreshTimelineText1 + " " + addCount.ToString() + Properties.Resources.RefreshTimelineText2;
1809                                 title.Append(ApplicationSettings.ApplicationName);
1810                                 title.Append(" ");
1811                                 title.AppendFormat(Properties.Resources.RefreshTimeline_NotifyText, addCount);
1812                                 nt = GrowlHelper.NotifyType.Notify;
1813                             }
1814                             string bText = sb.ToString();
1815                             if (string.IsNullOrEmpty(bText)) return;
1816
1817                             var image = this.IconCache.TryGetFromCache(post.ImageUrl);
1818                             gh.Notify(nt, post.StatusId.ToString(), title.ToString(), bText, image?.Image, post.ImageUrl);
1819                         }
1820                     }
1821                     else
1822                     {
1823                         StringBuilder sb = new StringBuilder();
1824                         bool reply = false;
1825                         bool dm = false;
1826                         foreach (PostClass post in notifyPosts)
1827                         {
1828                             if (post.IsReply && !post.IsExcludeReply) reply = true;
1829                             if (post.IsDm) dm = true;
1830                             if (sb.Length > 0) sb.Append(System.Environment.NewLine);
1831                             switch (SettingManager.Common.NameBalloon)
1832                             {
1833                                 case MyCommon.NameBalloonEnum.UserID:
1834                                     sb.Append(post.ScreenName).Append(" : ");
1835                                     break;
1836                                 case MyCommon.NameBalloonEnum.NickName:
1837                                     sb.Append(post.Nickname).Append(" : ");
1838                                     break;
1839                             }
1840                             sb.Append(post.TextFromApi);
1841
1842                         }
1843                         //if (SettingDialog.DispUsername) { NotifyIcon1.BalloonTipTitle = tw.Username + " - "; } else { NotifyIcon1.BalloonTipTitle = ""; }
1844                         StringBuilder title = new StringBuilder();
1845                         ToolTipIcon ntIcon;
1846                         if (SettingManager.Common.DispUsername)
1847                         {
1848                             title.Append(tw.Username);
1849                             title.Append(" - ");
1850                         }
1851                         else
1852                         {
1853                             //title.Clear();
1854                         }
1855                         if (dm)
1856                         {
1857                             //NotifyIcon1.BalloonTipIcon = ToolTipIcon.Warning;
1858                             //NotifyIcon1.BalloonTipTitle += Application.ProductName + " [DM] " + Properties.Resources.RefreshDirectMessageText1 + " " + addCount.ToString() + Properties.Resources.RefreshDirectMessageText2;
1859                             ntIcon = ToolTipIcon.Warning;
1860                             title.Append(ApplicationSettings.ApplicationName);
1861                             title.Append(" [DM] ");
1862                             title.AppendFormat(Properties.Resources.RefreshTimeline_NotifyText, addCount);
1863                         }
1864                         else if (reply)
1865                         {
1866                             //NotifyIcon1.BalloonTipIcon = ToolTipIcon.Warning;
1867                             //NotifyIcon1.BalloonTipTitle += Application.ProductName + " [Reply!] " + Properties.Resources.RefreshTimelineText1 + " " + addCount.ToString() + Properties.Resources.RefreshTimelineText2;
1868                             ntIcon = ToolTipIcon.Warning;
1869                             title.Append(ApplicationSettings.ApplicationName);
1870                             title.Append(" [Reply!] ");
1871                             title.AppendFormat(Properties.Resources.RefreshTimeline_NotifyText, addCount);
1872                         }
1873                         else
1874                         {
1875                             //NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
1876                             //NotifyIcon1.BalloonTipTitle += Application.ProductName + " " + Properties.Resources.RefreshTimelineText1 + " " + addCount.ToString() + Properties.Resources.RefreshTimelineText2;
1877                             ntIcon = ToolTipIcon.Info;
1878                             title.Append(ApplicationSettings.ApplicationName);
1879                             title.Append(" ");
1880                             title.AppendFormat(Properties.Resources.RefreshTimeline_NotifyText, addCount);
1881                         }
1882                         string bText = sb.ToString();
1883                         if (string.IsNullOrEmpty(bText)) return;
1884                         //NotifyIcon1.BalloonTipText = sb.ToString();
1885                         //NotifyIcon1.ShowBalloonTip(500);
1886                         NotifyIcon1.BalloonTipTitle = title.ToString();
1887                         NotifyIcon1.BalloonTipText = bText;
1888                         NotifyIcon1.BalloonTipIcon = ntIcon;
1889                         NotifyIcon1.ShowBalloonTip(500);
1890                     }
1891                 }
1892             }
1893
1894             //サウンド再生
1895             if (!_initial && SettingManager.Common.PlaySound && !string.IsNullOrEmpty(soundFile))
1896             {
1897                 try
1898                 {
1899                     string dir = Application.StartupPath;
1900                     if (Directory.Exists(Path.Combine(dir, "Sounds")))
1901                     {
1902                         dir = Path.Combine(dir, "Sounds");
1903                     }
1904                     using (SoundPlayer player = new SoundPlayer(Path.Combine(dir, soundFile)))
1905                     {
1906                         player.Play();
1907                     }
1908                 }
1909                 catch (Exception)
1910                 {
1911                 }
1912             }
1913
1914             //mentions新着時に画面ブリンク
1915             if (!_initial && SettingManager.Common.BlinkNewMentions && newMentions && Form.ActiveForm == null)
1916             {
1917                 NativeMethods.FlashMyWindow(this.Handle, NativeMethods.FlashSpecification.FlashTray, 3);
1918             }
1919         }
1920
1921         private void MyList_SelectedIndexChanged(object sender, EventArgs e)
1922         {
1923             if (_curList == null || !_curList.Equals(sender) || _curList.SelectedIndices.Count != 1) return;
1924
1925             _curItemIndex = _curList.SelectedIndices[0];
1926             if (_curItemIndex > _curList.VirtualListSize - 1) return;
1927
1928             try
1929             {
1930                 this._curPost = GetCurTabPost(_curItemIndex);
1931             }
1932             catch (ArgumentException)
1933             {
1934                 return;
1935             }
1936
1937             this.PushSelectPostChain();
1938
1939             this._statuses.SetReadAllTab(_curPost.StatusId, read: true);
1940             //キャッシュの書き換え
1941             ChangeCacheStyleRead(true, _curItemIndex);   //既読へ(フォント、文字色)
1942
1943             ColorizeList();
1944             _colorize = true;
1945         }
1946
1947         private void ChangeCacheStyleRead(bool Read, int Index)
1948         {
1949             var tabInfo = _statuses.Tabs[_curTab.Text];
1950             //Read:true=既読 false=未読
1951             //未読管理していなかったら既読として扱う
1952             if (!tabInfo.UnreadManage ||
1953                !SettingManager.Common.UnreadManage) Read = true;
1954
1955             var listCache = this._listItemCache;
1956             if (listCache == null)
1957                 return;
1958
1959             // キャッシュに含まれていないアイテムは対象外
1960             if (!listCache.TryGetValue(Index, out var itm, out var post))
1961                 return;
1962
1963             ChangeItemStyleRead(Read, itm, post, ((DetailsListView)_curTab.Tag));
1964         }
1965
1966         private void ChangeItemStyleRead(bool Read, ListViewItem Item, PostClass Post, DetailsListView DList)
1967         {
1968             Font fnt;
1969             //フォント
1970             if (Read)
1971             {
1972                 fnt = _fntReaded;
1973                 Item.SubItems[5].Text = "";
1974             }
1975             else
1976             {
1977                 fnt = _fntUnread;
1978                 Item.SubItems[5].Text = "★";
1979             }
1980             //文字色
1981             Color cl;
1982             if (Post.IsFav)
1983                 cl = _clFav;
1984             else if (Post.RetweetedId != null)
1985                 cl = _clRetweet;
1986             else if (Post.IsOwl && (Post.IsDm || SettingManager.Common.OneWayLove))
1987                 cl = _clOWL;
1988             else if (Read || !SettingManager.Common.UseUnreadStyle)
1989                 cl = _clReaded;
1990             else
1991                 cl = _clUnread;
1992
1993             if (DList == null || Item.Index == -1)
1994             {
1995                 Item.ForeColor = cl;
1996                 if (SettingManager.Common.UseUnreadStyle)
1997                     Item.Font = fnt;
1998             }
1999             else
2000             {
2001                 DList.Update();
2002                 if (SettingManager.Common.UseUnreadStyle)
2003                     DList.ChangeItemFontAndColor(Item.Index, cl, fnt);
2004                 else
2005                     DList.ChangeItemForeColor(Item.Index, cl);
2006                 //if (_itemCache != null) DList.RedrawItems(_itemCacheIndex, _itemCacheIndex + _itemCache.Length - 1, false);
2007             }
2008         }
2009
2010         private void ColorizeList()
2011         {
2012             //Index:更新対象のListviewItem.Index。Colorを返す。
2013             //-1は全キャッシュ。Colorは返さない(ダミーを戻す)
2014             PostClass _post;
2015             if (_anchorFlag)
2016                 _post = _anchorPost;
2017             else
2018                 _post = _curPost;
2019
2020             if (_post == null) return;
2021
2022             var listCache = this._listItemCache;
2023             if (listCache == null)
2024                 return;
2025
2026             var index = listCache.StartIndex;
2027             foreach (var cachedPost in listCache.Post)
2028             {
2029                 var backColor = this.JudgeColor(_post, cachedPost);
2030                 this._curList.ChangeItemBackColor(index++, backColor);
2031             }
2032         }
2033
2034         private void ColorizeList(ListViewItem Item, int Index)
2035         {
2036             //Index:更新対象のListviewItem.Index。Colorを返す。
2037             //-1は全キャッシュ。Colorは返さない(ダミーを戻す)
2038             PostClass _post;
2039             if (_anchorFlag)
2040                 _post = _anchorPost;
2041             else
2042                 _post = _curPost;
2043
2044             PostClass tPost = GetCurTabPost(Index);
2045
2046             if (_post == null) return;
2047
2048             if (Item.Index == -1)
2049                 Item.BackColor = JudgeColor(_post, tPost);
2050             else
2051                 _curList.ChangeItemBackColor(Item.Index, JudgeColor(_post, tPost));
2052         }
2053
2054         private Color JudgeColor(PostClass BasePost, PostClass TargetPost)
2055         {
2056             Color cl;
2057             if (TargetPost.StatusId == BasePost.InReplyToStatusId)
2058                 //@先
2059                 cl = _clAtTo;
2060             else if (TargetPost.IsMe)
2061                 //自分=発言者
2062                 cl = _clSelf;
2063             else if (TargetPost.IsReply)
2064                 //自分宛返信
2065                 cl = _clAtSelf;
2066             else if (BasePost.ReplyToList.Any(x => x.UserId == TargetPost.UserId))
2067                 //返信先
2068                 cl = _clAtFromTarget;
2069             else if (TargetPost.ReplyToList.Any(x => x.UserId == BasePost.UserId))
2070                 //その人への返信
2071                 cl = _clAtTarget;
2072             else if (TargetPost.ScreenName.Equals(BasePost.ScreenName, StringComparison.OrdinalIgnoreCase))
2073                 //発言者
2074                 cl = _clTarget;
2075             else
2076                 //その他
2077                 cl = _clListBackcolor;
2078
2079             return cl;
2080         }
2081
2082         private void StatusTextHistoryBack()
2083         {
2084             if (!string.IsNullOrWhiteSpace(this.StatusText.Text))
2085                 this._history[_hisIdx] = new StatusTextHistory(this.StatusText.Text, this.inReplyTo);
2086
2087             this._hisIdx -= 1;
2088             if (this._hisIdx < 0)
2089                 this._hisIdx = 0;
2090
2091             var historyItem = this._history[this._hisIdx];
2092             this.inReplyTo = historyItem.inReplyTo;
2093             this.StatusText.Text = historyItem.status;
2094             this.StatusText.SelectionStart = this.StatusText.Text.Length;
2095         }
2096
2097         private void StatusTextHistoryForward()
2098         {
2099             if (!string.IsNullOrWhiteSpace(this.StatusText.Text))
2100                 this._history[this._hisIdx] = new StatusTextHistory(this.StatusText.Text, this.inReplyTo);
2101
2102             this._hisIdx += 1;
2103             if (this._hisIdx > this._history.Count - 1)
2104                 this._hisIdx = this._history.Count - 1;
2105
2106             var historyItem = this._history[this._hisIdx];
2107             this.inReplyTo = historyItem.inReplyTo;
2108             this.StatusText.Text = historyItem.status;
2109             this.StatusText.SelectionStart = this.StatusText.Text.Length;
2110         }
2111
2112         private async void PostButton_Click(object sender, EventArgs e)
2113         {
2114             if (StatusText.Text.Trim().Length == 0)
2115             {
2116                 if (!ImageSelector.Enabled)
2117                 {
2118                     await this.DoRefresh();
2119                     return;
2120                 }
2121             }
2122
2123             if (this.ExistCurrentPost && StatusText.Text.Trim() == string.Format("RT @{0}: {1}", _curPost.ScreenName, _curPost.TextFromApi))
2124             {
2125                 DialogResult rtResult = MessageBox.Show(string.Format(Properties.Resources.PostButton_Click1, Environment.NewLine),
2126                                                                "Retweet",
2127                                                                MessageBoxButtons.YesNoCancel,
2128                                                                MessageBoxIcon.Question);
2129                 switch (rtResult)
2130                 {
2131                     case DialogResult.Yes:
2132                         StatusText.Text = "";
2133                         await this.doReTweetOfficial(false);
2134                         return;
2135                     case DialogResult.Cancel:
2136                         return;
2137                 }
2138             }
2139
2140             if (TextContainsOnlyMentions(this.StatusText.Text))
2141             {
2142                 var message = string.Format(Properties.Resources.PostConfirmText, this.StatusText.Text);
2143                 var ret = MessageBox.Show(message, ApplicationSettings.ApplicationName, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
2144
2145                 if (ret != DialogResult.OK)
2146                     return;
2147             }
2148
2149             _history[_history.Count - 1] = new StatusTextHistory(StatusText.Text, this.inReplyTo);
2150
2151             if (SettingManager.Common.Nicoms)
2152             {
2153                 StatusText.SelectionStart = StatusText.Text.Length;
2154                 await UrlConvertAsync(MyCommon.UrlConverter.Nicoms);
2155             }
2156             //if (SettingDialog.UrlConvertAuto)
2157             //{
2158             //    StatusText.SelectionStart = StatusText.Text.Length;
2159             //    UrlConvertAutoToolStripMenuItem_Click(null, null);
2160             //}
2161             //else if (SettingDialog.Nicoms)
2162             //{
2163             //    StatusText.SelectionStart = StatusText.Text.Length;
2164             //    UrlConvert(UrlConverter.Nicoms);
2165             //}
2166             StatusText.SelectionStart = StatusText.Text.Length;
2167             CheckReplyTo(StatusText.Text);
2168
2169             var status = new PostStatusParams();
2170
2171             var statusTextCompat = this.FormatStatusText(this.StatusText.Text);
2172             if (this.GetRestStatusCount(statusTextCompat) >= 0)
2173             {
2174                 // auto_populate_reply_metadata や attachment_url を使用しなくても 140 字以内に
2175                 // 収まる場合はこれらのオプションを使用せずに投稿する
2176                 status.Text = statusTextCompat;
2177                 status.InReplyToStatusId = this.inReplyTo?.StatusId;
2178             }
2179             else
2180             {
2181                 status.Text = this.FormatStatusTextExtended(this.StatusText.Text, out var autoPopulatedUserIds, out var attachmentUrl);
2182                 status.InReplyToStatusId = this.inReplyTo?.StatusId;
2183
2184                 status.AttachmentUrl = attachmentUrl;
2185
2186                 // リプライ先がセットされていても autoPopulatedUserIds が空の場合は auto_populate_reply_metadata を有効にしない
2187                 //  (非公式 RT の場合など)
2188                 var replyToPost = this.inReplyTo != null ? this._statuses[this.inReplyTo.Value.StatusId] : null;
2189                 if (replyToPost != null && autoPopulatedUserIds.Length != 0)
2190                 {
2191                     status.AutoPopulateReplyMetadata = true;
2192
2193                     // ReplyToList のうち autoPopulatedUserIds に含まれていないユーザー ID を抽出
2194                     status.ExcludeReplyUserIds = replyToPost.ReplyToList.Select(x => x.UserId).Except(autoPopulatedUserIds)
2195                         .ToArray();
2196                 }
2197             }
2198
2199             if (this.GetRestStatusCount(status.Text) < 0)
2200             {
2201                 // 文字数制限を超えているが強制的に投稿するか
2202                 var ret = MessageBox.Show(Properties.Resources.PostLengthOverMessage1, Properties.Resources.PostLengthOverMessage2, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
2203                 if (ret != DialogResult.OK)
2204                     return;
2205             }
2206
2207             IMediaUploadService uploadService = null;
2208             IMediaItem[] uploadItems = null;
2209             if (ImageSelector.Visible)
2210             {
2211                 //画像投稿
2212                 if (!ImageSelector.TryGetSelectedMedia(out var serviceName, out uploadItems))
2213                     return;
2214
2215                 uploadService = this.ImageSelector.GetService(serviceName);
2216             }
2217
2218             this.inReplyTo = null;
2219             StatusText.Text = "";
2220             _history.Add(new StatusTextHistory());
2221             _hisIdx = _history.Count - 1;
2222             if (!SettingManager.Common.FocusLockToStatusText)
2223                 ((Control)ListTab.SelectedTab.Tag).Focus();
2224             urlUndoBuffer = null;
2225             UrlUndoToolStripMenuItem.Enabled = false;  //Undoをできないように設定
2226
2227             //Google検索(試験実装)
2228             if (StatusText.Text.StartsWith("Google:", StringComparison.OrdinalIgnoreCase) && StatusText.Text.Trim().Length > 7)
2229             {
2230                 string tmp = string.Format(Properties.Resources.SearchItem2Url, Uri.EscapeDataString(StatusText.Text.Substring(7)));
2231                 await this.OpenUriInBrowserAsync(tmp);
2232             }
2233
2234             await this.PostMessageAsync(status, uploadService, uploadItems);
2235         }
2236
2237         private void EndToolStripMenuItem_Click(object sender, EventArgs e)
2238         {
2239             MyCommon._endingFlag = true;
2240             this.Close();
2241         }
2242
2243         private void TweenMain_FormClosing(object sender, FormClosingEventArgs e)
2244         {
2245             if (!SettingManager.Common.CloseToExit && e.CloseReason == CloseReason.UserClosing && MyCommon._endingFlag == false)
2246             {
2247                 //_endingFlag=false:フォームの×ボタン
2248                 e.Cancel = true;
2249                 this.Visible = false;
2250             }
2251             else
2252             {
2253                 _hookGlobalHotkey.UnregisterAllOriginalHotkey();
2254                 _ignoreConfigSave = true;
2255                 MyCommon._endingFlag = true;
2256                 TimerTimeline.Enabled = false;
2257                 TimerRefreshIcon.Enabled = false;
2258             }
2259         }
2260
2261         private void NotifyIcon1_BalloonTipClicked(object sender, EventArgs e)
2262         {
2263             this.Visible = true;
2264             if (this.WindowState == FormWindowState.Minimized)
2265             {
2266                 this.WindowState = FormWindowState.Normal;
2267             }
2268             this.Activate();
2269             this.BringToFront();
2270         }
2271
2272         private static int errorCount = 0;
2273
2274         private static bool CheckAccountValid()
2275         {
2276             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
2277             {
2278                 errorCount += 1;
2279                 if (errorCount > 5)
2280                 {
2281                     errorCount = 0;
2282                     Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
2283                     return true;
2284                 }
2285                 return false;
2286             }
2287             errorCount = 0;
2288             return true;
2289         }
2290
2291         /// <summary>指定された型 <typeparamref name="T"/> に合致する全てのタブを更新します</summary>
2292         private Task RefreshTabAsync<T>() where T : TabModel
2293             => this.RefreshTabAsync<T>(backward: false);
2294
2295         /// <summary>指定された型 <typeparamref name="T"/> に合致する全てのタブを更新します</summary>
2296         private Task RefreshTabAsync<T>(bool backward) where T : TabModel
2297         {
2298             var loadTasks =
2299                 from tab in this._statuses.GetTabsByType<T>()
2300                 select this.RefreshTabAsync(tab, backward);
2301
2302             return Task.WhenAll(loadTasks);
2303         }
2304
2305         /// <summary>指定されたタブ <paramref name="tab"/> を更新します</summary>
2306         private Task RefreshTabAsync(TabModel tab)
2307             => this.RefreshTabAsync(tab, backward: false);
2308
2309         /// <summary>指定されたタブ <paramref name="tab"/> を更新します</summary>
2310         private async Task RefreshTabAsync(TabModel tab, bool backward)
2311         {
2312             await this.workerSemaphore.WaitAsync();
2313
2314             try
2315             {
2316                 await Task.Run(() => tab.RefreshAsync(this.tw, backward, this._initial, this.workerProgress));
2317                 this.RefreshTimeline();
2318             }
2319             catch (WebApiException ex)
2320             {
2321                 this._myStatusError = true;
2322
2323                 string tabType;
2324                 switch (tab)
2325                 {
2326                     case HomeTabModel _:
2327                         tabType = "GetTimeline";
2328                         break;
2329                     case MentionsTabModel _:
2330                         tabType = "GetTimeline";
2331                         break;
2332                     case DirectMessagesTabModel _:
2333                         tabType = "GetDirectMessage";
2334                         break;
2335                     case FavoritesTabModel _:
2336                         tabType = "GetFavorites";
2337                         break;
2338                     case PublicSearchTabModel _:
2339                         tabType = "GetSearch";
2340                         break;
2341                     case UserTimelineTabModel _:
2342                         tabType = "GetUserTimeline";
2343                         break;
2344                     case ListTimelineTabModel _:
2345                         tabType = "GetListStatus";
2346                         break;
2347                     case RelatedPostsTabModel _:
2348                         tabType = "GetRelatedTweets";
2349                         break;
2350                     default:
2351                         tabType = tab.GetType().Name.Replace("Model", "");
2352                         break;
2353                 }
2354
2355                 this.StatusLabel.Text = $"Err:{ex.Message}({tabType})";
2356             }
2357             finally
2358             {
2359                 this.workerSemaphore.Release();
2360             }
2361         }
2362
2363         private async Task FavAddAsync(long statusId, TabModel tab)
2364         {
2365             await this.workerSemaphore.WaitAsync();
2366
2367             try
2368             {
2369                 var progress = new Progress<string>(x => this.StatusLabel.Text = x);
2370
2371                 await this.FavAddAsyncInternal(progress, this.workerCts.Token, statusId, tab);
2372             }
2373             catch (WebApiException ex)
2374             {
2375                 this._myStatusError = true;
2376                 this.StatusLabel.Text = $"Err:{ex.Message}(PostFavAdd)";
2377             }
2378             finally
2379             {
2380                 this.workerSemaphore.Release();
2381             }
2382         }
2383
2384         private async Task FavAddAsyncInternal(IProgress<string> p, CancellationToken ct, long statusId, TabModel tab)
2385         {
2386             if (ct.IsCancellationRequested)
2387                 return;
2388
2389             if (!CheckAccountValid())
2390                 throw new WebApiException("Auth error. Check your account");
2391
2392             if (!tab.Posts.TryGetValue(statusId, out var post))
2393                 return;
2394
2395             if (post.IsFav)
2396                 return;
2397
2398             await Task.Run(async () =>
2399             {
2400                 p.Report(string.Format(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText15, 0, 1, 0));
2401
2402                 try
2403                 {
2404                     try
2405                     {
2406                         await this.twitterApi.FavoritesCreate(post.RetweetedId ?? post.StatusId)
2407                             .IgnoreResponse()
2408                             .ConfigureAwait(false);
2409                     }
2410                     catch (TwitterApiException ex)
2411                         when (ex.ErrorResponse.Errors.All(x => x.Code == TwitterErrorCode.AlreadyFavorited))
2412                     {
2413                         // エラーコード 139 のみの場合は成功と見なす
2414                     }
2415
2416                     if (SettingManager.Common.RestrictFavCheck)
2417                     {
2418                         var status = await this.twitterApi.StatusesShow(post.RetweetedId ?? post.StatusId)
2419                             .ConfigureAwait(false);
2420
2421                         if (status.Favorited != true)
2422                             throw new WebApiException("NG(Restricted?)");
2423                     }
2424
2425                     this._favTimestamps.Add(DateTimeUtc.Now);
2426
2427                     // TLでも取得済みならfav反映
2428                     if (this._statuses.ContainsKey(statusId))
2429                     {
2430                         var postTl = this._statuses[statusId];
2431                         postTl.IsFav = true;
2432
2433                         var favTab = this._statuses.GetTabByType(MyCommon.TabUsageType.Favorites);
2434                         favTab.AddPostQueue(postTl);
2435                     }
2436
2437                     // 検索,リスト,UserTimeline,Relatedの各タブに反映
2438                     foreach (var tb in this._statuses.GetTabsInnerStorageType())
2439                     {
2440                         if (tb.Contains(statusId))
2441                             tb.Posts[statusId].IsFav = true;
2442                     }
2443
2444                     p.Report(string.Format(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText15, 1, 1, 0));
2445                 }
2446                 catch (WebApiException)
2447                 {
2448                     p.Report(string.Format(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText15, 1, 1, 1));
2449                     throw;
2450                 }
2451
2452                 // 時速表示用
2453                 var oneHour = DateTimeUtc.Now - TimeSpan.FromHours(1);
2454                 foreach (var i in MyCommon.CountDown(this._favTimestamps.Count - 1, 0))
2455                 {
2456                     if (this._favTimestamps[i] < oneHour)
2457                         this._favTimestamps.RemoveAt(i);
2458                 }
2459
2460                 this._statuses.DistributePosts();
2461             });
2462
2463             if (ct.IsCancellationRequested)
2464                 return;
2465
2466             this.RefreshTimeline();
2467
2468             if (this._curList != null && this._curTab != null && this._curTab.Text == tab.TabName)
2469             {
2470                 using (ControlTransaction.Update(this._curList))
2471                 {
2472                     var idx = tab.IndexOf(statusId);
2473                     if (idx != -1)
2474                         this.ChangeCacheStyleRead(post.IsRead, idx);
2475                 }
2476
2477                 if (statusId == this._curPost.StatusId)
2478                     await this.DispSelectedPost(true); // 選択アイテム再表示
2479             }
2480         }
2481
2482         private async Task FavRemoveAsync(IReadOnlyList<long> statusIds, TabModel tab)
2483         {
2484             await this.workerSemaphore.WaitAsync();
2485
2486             try
2487             {
2488                 var progress = new Progress<string>(x => this.StatusLabel.Text = x);
2489
2490                 await this.FavRemoveAsyncInternal(progress, this.workerCts.Token, statusIds, tab);
2491             }
2492             catch (WebApiException ex)
2493             {
2494                 this._myStatusError = true;
2495                 this.StatusLabel.Text = $"Err:{ex.Message}(PostFavRemove)";
2496             }
2497             finally
2498             {
2499                 this.workerSemaphore.Release();
2500             }
2501         }
2502
2503         private async Task FavRemoveAsyncInternal(IProgress<string> p, CancellationToken ct, IReadOnlyList<long> statusIds, TabModel tab)
2504         {
2505             if (ct.IsCancellationRequested)
2506                 return;
2507
2508             if (!CheckAccountValid())
2509                 throw new WebApiException("Auth error. Check your account");
2510
2511             var successIds = new List<long>();
2512
2513             await Task.Run(async () =>
2514             {
2515                 //スレッド処理はしない
2516                 var allCount = 0;
2517                 var failedCount = 0;
2518                 foreach (var statusId in statusIds)
2519                 {
2520                     allCount++;
2521
2522                     var post = tab.Posts[statusId];
2523
2524                     p.Report(string.Format(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText17, allCount, statusIds.Count, failedCount));
2525
2526                     if (!post.IsFav)
2527                         continue;
2528
2529                     try
2530                     {
2531                         await this.twitterApi.FavoritesDestroy(post.RetweetedId ?? post.StatusId)
2532                             .IgnoreResponse()
2533                             .ConfigureAwait(false);
2534                     }
2535                     catch (WebApiException)
2536                     {
2537                         failedCount++;
2538                         continue;
2539                     }
2540
2541                     successIds.Add(statusId);
2542                     post.IsFav = false; // リスト再描画必要
2543
2544                     if (this._statuses.ContainsKey(statusId))
2545                     {
2546                         this._statuses[statusId].IsFav = false;
2547                     }
2548
2549                     // 検索,リスト,UserTimeline,Relatedの各タブに反映
2550                     foreach (var tb in this._statuses.GetTabsInnerStorageType())
2551                     {
2552                         if (tb.Contains(statusId))
2553                             tb.Posts[statusId].IsFav = false;
2554                     }
2555                 }
2556             });
2557
2558             if (ct.IsCancellationRequested)
2559                 return;
2560
2561             var favTab = this._statuses.GetTabByType(MyCommon.TabUsageType.Favorites);
2562             foreach (var statusId in successIds)
2563             {
2564                 // ツイートが削除された訳ではないので IsDeleted はセットしない
2565                 favTab.EnqueueRemovePost(statusId, setIsDeleted: false);
2566             }
2567
2568             this.RefreshTimeline();
2569
2570             if (this._curList != null && this._curTab != null && this._curTab.Text == tab.TabName)
2571             {
2572                 if (tab.TabType == MyCommon.TabUsageType.Favorites)
2573                 {
2574                     // 色変えは不要
2575                 }
2576                 else
2577                 {
2578                     using (ControlTransaction.Update(this._curList))
2579                     {
2580                         foreach (var statusId in successIds)
2581                         {
2582                             var idx = tab.IndexOf(statusId);
2583                             if (idx == -1)
2584                                 continue;
2585
2586                             var post = tab.Posts[statusId];
2587                             this.ChangeCacheStyleRead(post.IsRead, idx);
2588                         }
2589                     }
2590
2591                     if (successIds.Contains(this._curPost.StatusId))
2592                         await this.DispSelectedPost(true); // 選択アイテム再表示
2593                 }
2594             }
2595         }
2596
2597         private async Task PostMessageAsync(PostStatusParams postParams, IMediaUploadService uploadService, IMediaItem[] uploadItems)
2598         {
2599             await this.workerSemaphore.WaitAsync();
2600
2601             try
2602             {
2603                 var progress = new Progress<string>(x => this.StatusLabel.Text = x);
2604
2605                 await this.PostMessageAsyncInternal(progress, this.workerCts.Token, postParams, uploadService, uploadItems);
2606             }
2607             catch (WebApiException ex)
2608             {
2609                 this._myStatusError = true;
2610                 this.StatusLabel.Text = $"Err:{ex.Message}(PostMessage)";
2611             }
2612             finally
2613             {
2614                 this.workerSemaphore.Release();
2615             }
2616         }
2617
2618         private async Task PostMessageAsyncInternal(IProgress<string> p, CancellationToken ct, PostStatusParams postParams,
2619             IMediaUploadService uploadService, IMediaItem[] uploadItems)
2620         {
2621             if (ct.IsCancellationRequested)
2622                 return;
2623
2624             if (!CheckAccountValid())
2625                 throw new WebApiException("Auth error. Check your account");
2626
2627             p.Report("Posting...");
2628
2629             PostClass post = null;
2630             var errMsg = "";
2631
2632             try
2633             {
2634                 await Task.Run(async () =>
2635                 {
2636                     var postParamsWithMedia = postParams;
2637
2638                     if (uploadService != null && uploadItems != null && uploadItems.Length > 0)
2639                     {
2640                         postParamsWithMedia = await uploadService.UploadAsync(uploadItems, postParamsWithMedia)
2641                             .ConfigureAwait(false);
2642                     }
2643
2644                     post = await this.tw.PostStatus(postParamsWithMedia)
2645                         .ConfigureAwait(false);
2646                 });
2647
2648                 p.Report(Properties.Resources.PostWorker_RunWorkerCompletedText4);
2649             }
2650             catch (WebApiException ex)
2651             {
2652                 // 処理は中断せずエラーの表示のみ行う
2653                 errMsg = $"Err:{ex.Message}(PostMessage)";
2654                 p.Report(errMsg);
2655                 this._myStatusError = true;
2656             }
2657             catch (UnauthorizedAccessException ex)
2658             {
2659                 // アップロード対象のファイルが開けなかった場合など
2660                 errMsg = $"Err:{ex.Message}(PostMessage)";
2661                 p.Report(errMsg);
2662                 this._myStatusError = true;
2663             }
2664             finally
2665             {
2666                 // 使い終わった MediaItem は破棄する
2667                 if (uploadItems != null)
2668                 {
2669                     foreach (var disposableItem in uploadItems.OfType<IDisposable>())
2670                     {
2671                         disposableItem.Dispose();
2672                     }
2673                 }
2674             }
2675
2676             if (ct.IsCancellationRequested)
2677                 return;
2678
2679             if (!string.IsNullOrEmpty(errMsg) &&
2680                 !errMsg.StartsWith("OK:", StringComparison.Ordinal) &&
2681                 !errMsg.StartsWith("Warn:", StringComparison.Ordinal))
2682             {
2683                 var message = string.Format(Properties.Resources.StatusUpdateFailed, errMsg, postParams.Text);
2684
2685                 var ret = MessageBox.Show(
2686                     message,
2687                     "Failed to update status",
2688                     MessageBoxButtons.RetryCancel,
2689                     MessageBoxIcon.Question);
2690
2691                 if (ret == DialogResult.Retry)
2692                 {
2693                     await this.PostMessageAsync(postParams, uploadService, uploadItems);
2694                 }
2695                 else
2696                 {
2697                     this.StatusTextHistoryBack();
2698                     this.StatusText.Focus();
2699
2700                     // 連投モードのときだけEnterイベントが起きないので強制的に背景色を戻す
2701                     if (SettingManager.Common.FocusLockToStatusText)
2702                         this.StatusText_Enter(this.StatusText, EventArgs.Empty);
2703                 }
2704                 return;
2705             }
2706
2707             this._postTimestamps.Add(DateTimeUtc.Now);
2708
2709             var oneHour = DateTimeUtc.Now - TimeSpan.FromHours(1);
2710             foreach (var i in MyCommon.CountDown(this._postTimestamps.Count - 1, 0))
2711             {
2712                 if (this._postTimestamps[i] < oneHour)
2713                     this._postTimestamps.RemoveAt(i);
2714             }
2715
2716             if (!this.HashMgr.IsPermanent && !string.IsNullOrEmpty(this.HashMgr.UseHash))
2717             {
2718                 this.HashMgr.ClearHashtag();
2719                 this.HashStripSplitButton.Text = "#[-]";
2720                 this.HashTogglePullDownMenuItem.Checked = false;
2721                 this.HashToggleMenuItem.Checked = false;
2722             }
2723
2724             this.SetMainWindowTitle();
2725
2726             // TLに反映
2727             if (!this.tw.UserStreamActive)
2728             {
2729                 if (SettingManager.Common.PostAndGet)
2730                     await this.RefreshTabAsync<HomeTabModel>();
2731                 else
2732                 {
2733                     if (post != null)
2734                     {
2735                         this._statuses.AddPost(post);
2736                         this._statuses.DistributePosts();
2737                         this.RefreshTimeline();
2738                     }
2739                 }
2740             }
2741         }
2742
2743         private async Task RetweetAsync(IReadOnlyList<long> statusIds)
2744         {
2745             await this.workerSemaphore.WaitAsync();
2746
2747             try
2748             {
2749                 var progress = new Progress<string>(x => this.StatusLabel.Text = x);
2750
2751                 await this.RetweetAsyncInternal(progress, this.workerCts.Token, statusIds);
2752             }
2753             catch (WebApiException ex)
2754             {
2755                 this._myStatusError = true;
2756                 this.StatusLabel.Text = $"Err:{ex.Message}(PostRetweet)";
2757             }
2758             finally
2759             {
2760                 this.workerSemaphore.Release();
2761             }
2762         }
2763
2764         private async Task RetweetAsyncInternal(IProgress<string> p, CancellationToken ct, IReadOnlyList<long> statusIds)
2765         {
2766             if (ct.IsCancellationRequested)
2767                 return;
2768
2769             if (!CheckAccountValid())
2770                 throw new WebApiException("Auth error. Check your account");
2771
2772             bool read;
2773             if (!SettingManager.Common.UnreadManage)
2774                 read = true;
2775             else
2776                 read = this._initial && SettingManager.Common.Read;
2777
2778             p.Report("Posting...");
2779
2780             var posts = new List<PostClass>();
2781
2782             await Task.Run(async () =>
2783             {
2784                 foreach (var statusId in statusIds)
2785                 {
2786                     var post = await this.tw.PostRetweet(statusId, read).ConfigureAwait(false);
2787                     if (post != null) posts.Add(post);
2788                 }
2789             });
2790
2791             if (ct.IsCancellationRequested)
2792                 return;
2793
2794             p.Report(Properties.Resources.PostWorker_RunWorkerCompletedText4);
2795
2796             this._postTimestamps.Add(DateTimeUtc.Now);
2797
2798             var oneHour = DateTimeUtc.Now - TimeSpan.FromHours(1);
2799             foreach (var i in MyCommon.CountDown(this._postTimestamps.Count - 1, 0))
2800             {
2801                 if (this._postTimestamps[i] < oneHour)
2802                     this._postTimestamps.RemoveAt(i);
2803             }
2804
2805             // TLに反映
2806             if (!this.tw.UserStreamActive)
2807             {
2808                 // 自分のRTはTLの更新では取得できない場合があるので、
2809                 // 投稿時取得の有無に関わらず追加しておく
2810                 posts.ForEach(post => this._statuses.AddPost(post));
2811
2812                 if (SettingManager.Common.PostAndGet)
2813                     await this.RefreshTabAsync<HomeTabModel>();
2814                 else
2815                 {
2816                     this._statuses.DistributePosts();
2817                     this.RefreshTimeline();
2818                 }
2819             }
2820         }
2821
2822         private async Task RefreshFollowerIdsAsync()
2823         {
2824             await this.workerSemaphore.WaitAsync();
2825             try
2826             {
2827                 this.StatusLabel.Text = Properties.Resources.UpdateFollowersMenuItem1_ClickText1;
2828
2829                 await this.tw.RefreshFollowerIds();
2830
2831                 this.StatusLabel.Text = Properties.Resources.UpdateFollowersMenuItem1_ClickText3;
2832
2833                 this.RefreshTimeline();
2834                 this.PurgeListViewItemCache();
2835                 this._curList?.Refresh();
2836             }
2837             catch (WebApiException ex)
2838             {
2839                 this.StatusLabel.Text = $"Err:{ex.Message}(RefreshFollowersIds)";
2840             }
2841             finally
2842             {
2843                 this.workerSemaphore.Release();
2844             }
2845         }
2846
2847         private async Task RefreshNoRetweetIdsAsync()
2848         {
2849             await this.workerSemaphore.WaitAsync();
2850             try
2851             {
2852                 await this.tw.RefreshNoRetweetIds();
2853
2854                 this.StatusLabel.Text = "NoRetweetIds refreshed";
2855             }
2856             catch (WebApiException ex)
2857             {
2858                 this.StatusLabel.Text = $"Err:{ex.Message}(RefreshNoRetweetIds)";
2859             }
2860             finally
2861             {
2862                 this.workerSemaphore.Release();
2863             }
2864         }
2865
2866         private async Task RefreshBlockIdsAsync()
2867         {
2868             await this.workerSemaphore.WaitAsync();
2869             try
2870             {
2871                 this.StatusLabel.Text = Properties.Resources.UpdateBlockUserText1;
2872
2873                 await this.tw.RefreshBlockIds();
2874
2875                 this.StatusLabel.Text = Properties.Resources.UpdateBlockUserText3;
2876             }
2877             catch (WebApiException ex)
2878             {
2879                 this.StatusLabel.Text = $"Err:{ex.Message}(RefreshBlockIds)";
2880             }
2881             finally
2882             {
2883                 this.workerSemaphore.Release();
2884             }
2885         }
2886
2887         private async Task RefreshTwitterConfigurationAsync()
2888         {
2889             await this.workerSemaphore.WaitAsync();
2890             try
2891             {
2892                 await this.tw.RefreshConfiguration();
2893
2894                 if (this.tw.Configuration.PhotoSizeLimit != 0)
2895                 {
2896                     foreach (var service in this.ImageSelector.GetServices())
2897                     {
2898                         service.UpdateTwitterConfiguration(this.tw.Configuration);
2899                     }
2900                 }
2901
2902                 this.PurgeListViewItemCache();
2903
2904                 this._curList?.Refresh();
2905             }
2906             catch (WebApiException ex)
2907             {
2908                 this.StatusLabel.Text = $"Err:{ex.Message}(RefreshConfiguration)";
2909             }
2910             finally
2911             {
2912                 this.workerSemaphore.Release();
2913             }
2914         }
2915
2916         private async Task RefreshMuteUserIdsAsync()
2917         {
2918             this.StatusLabel.Text = Properties.Resources.UpdateMuteUserIds_Start;
2919
2920             try
2921             {
2922                 await tw.RefreshMuteUserIdsAsync();
2923             }
2924             catch (WebApiException ex)
2925             {
2926                 this.StatusLabel.Text = string.Format(Properties.Resources.UpdateMuteUserIds_Error, ex.Message);
2927                 return;
2928             }
2929
2930             this.StatusLabel.Text = Properties.Resources.UpdateMuteUserIds_Finish;
2931         }
2932
2933         private void NotifyIcon1_MouseClick(object sender, MouseEventArgs e)
2934         {
2935             if (e.Button == MouseButtons.Left)
2936             {
2937                 this.Visible = true;
2938                 if (this.WindowState == FormWindowState.Minimized)
2939                 {
2940                     this.WindowState = _formWindowState;
2941                 }
2942                 this.Activate();
2943                 this.BringToFront();
2944             }
2945         }
2946
2947         private async void MyList_MouseDoubleClick(object sender, MouseEventArgs e)
2948         {
2949             switch (SettingManager.Common.ListDoubleClickAction)
2950             {
2951                 case 0:
2952                     MakeReplyOrDirectStatus();
2953                     break;
2954                 case 1:
2955                     await this.FavoriteChange(true);
2956                     break;
2957                 case 2:
2958                     if (_curPost != null)
2959                         await this.ShowUserStatus(_curPost.ScreenName, false);
2960                     break;
2961                 case 3:
2962                     await ShowUserTimeline();
2963                     break;
2964                 case 4:
2965                     ShowRelatedStatusesMenuItem_Click(null, null);
2966                     break;
2967                 case 5:
2968                     MoveToHomeToolStripMenuItem_Click(null, null);
2969                     break;
2970                 case 6:
2971                     StatusOpenMenuItem_Click(null, null);
2972                     break;
2973                 case 7:
2974                     //動作なし
2975                     break;
2976             }
2977         }
2978
2979         private async void FavAddToolStripMenuItem_Click(object sender, EventArgs e)
2980             => await this.FavoriteChange(true);
2981
2982         private async void FavRemoveToolStripMenuItem_Click(object sender, EventArgs e)
2983             => await this.FavoriteChange(false);
2984
2985
2986         private async void FavoriteRetweetMenuItem_Click(object sender, EventArgs e)
2987             => await this.FavoritesRetweetOfficial();
2988
2989         private async void FavoriteRetweetUnofficialMenuItem_Click(object sender, EventArgs e)
2990             => await this.FavoritesRetweetUnofficial();
2991
2992         private async Task FavoriteChange(bool FavAdd, bool multiFavoriteChangeDialogEnable = true)
2993         {
2994             if (!this._statuses.Tabs.TryGetValue(this._curTab.Text, out var tab))
2995                 return;
2996
2997             //trueでFavAdd,falseでFavRemove
2998             if (tab.TabType == MyCommon.TabUsageType.DirectMessage || _curList.SelectedIndices.Count == 0
2999                 || !this.ExistCurrentPost) return;
3000
3001             if (this._curList.SelectedIndices.Count > 1)
3002             {
3003                 if (FavAdd)
3004                 {
3005                     // 複数ツイートの一括ふぁぼは禁止
3006                     // https://support.twitter.com/articles/76915#favoriting
3007                     MessageBox.Show(string.Format(Properties.Resources.FavoriteLimitCountText, 1));
3008                     _DoFavRetweetFlags = false;
3009                     return;
3010                 }
3011                 else
3012                 {
3013                     if (multiFavoriteChangeDialogEnable)
3014                     {
3015                         var confirm = MessageBox.Show(Properties.Resources.FavRemoveToolStripMenuItem_ClickText1,
3016                             Properties.Resources.FavRemoveToolStripMenuItem_ClickText2,
3017                             MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
3018
3019                         if (confirm == DialogResult.Cancel)
3020                             return;
3021                     }
3022                 }
3023             }
3024
3025             if (FavAdd)
3026             {
3027                 var selectedPost = this.GetCurTabPost(_curList.SelectedIndices[0]);
3028                 if (selectedPost.IsFav)
3029                 {
3030                     this.StatusLabel.Text = Properties.Resources.FavAddToolStripMenuItem_ClickText4;
3031                     return;
3032                 }
3033
3034                 await this.FavAddAsync(selectedPost.StatusId, tab);
3035             }
3036             else
3037             {
3038                 var selectedPosts = this._curList.SelectedIndices.Cast<int>()
3039                     .Select(x => this.GetCurTabPost(x))
3040                     .Where(x => x.IsFav);
3041
3042                 var statusIds = selectedPosts.Select(x => x.StatusId).ToArray();
3043                 if (statusIds.Length == 0)
3044                 {
3045                     this.StatusLabel.Text = Properties.Resources.FavRemoveToolStripMenuItem_ClickText4;
3046                     return;
3047                 }
3048
3049                 await this.FavRemoveAsync(statusIds, tab);
3050             }
3051         }
3052
3053         private PostClass GetCurTabPost(int Index)
3054         {
3055             var listCache = this._listItemCache;
3056             if (listCache != null)
3057             {
3058                 if (listCache.TryGetValue(Index, out var item, out var post))
3059                     return post;
3060             }
3061
3062             return _statuses.Tabs[_curTab.Text][Index];
3063         }
3064
3065         private async void MoveToHomeToolStripMenuItem_Click(object sender, EventArgs e)
3066         {
3067             if (_curList.SelectedIndices.Count > 0)
3068                 await this.OpenUriInBrowserAsync(MyCommon.TwitterUrl + GetCurTabPost(_curList.SelectedIndices[0]).ScreenName);
3069             else if (_curList.SelectedIndices.Count == 0)
3070                 await this.OpenUriInBrowserAsync(MyCommon.TwitterUrl);
3071         }
3072
3073         private async void MoveToFavToolStripMenuItem_Click(object sender, EventArgs e)
3074         {
3075             if (_curList.SelectedIndices.Count > 0)
3076                 await this.OpenUriInBrowserAsync(MyCommon.TwitterUrl + "#!/" + GetCurTabPost(_curList.SelectedIndices[0]).ScreenName + "/favorites");
3077         }
3078
3079         private void TweenMain_ClientSizeChanged(object sender, EventArgs e)
3080         {
3081             if ((!_initialLayout) && this.Visible)
3082             {
3083                 if (this.WindowState == FormWindowState.Normal)
3084                 {
3085                     _mySize = this.ClientSize;
3086                     _mySpDis = this.SplitContainer1.SplitterDistance;
3087                     _mySpDis3 = this.SplitContainer3.SplitterDistance;
3088                     if (StatusText.Multiline) _mySpDis2 = this.StatusText.Height;
3089                     ModifySettingLocal = true;
3090                 }
3091             }
3092         }
3093
3094         private void MyList_ColumnClick(object sender, ColumnClickEventArgs e)
3095         {
3096             var comparerMode = this.GetComparerModeByColumnIndex(e.Column);
3097             if (comparerMode == null)
3098                 return;
3099
3100             this.SetSortColumn(comparerMode.Value);
3101         }
3102
3103         /// <summary>
3104         /// 列インデックスからソートを行う ComparerMode を求める
3105         /// </summary>
3106         /// <param name="columnIndex">ソートを行うカラムのインデックス (表示上の順序とは異なる)</param>
3107         /// <returns>ソートを行う ComparerMode。null であればソートを行わない</returns>
3108         private ComparerMode? GetComparerModeByColumnIndex(int columnIndex)
3109         {
3110             if (this._iconCol)
3111                 return ComparerMode.Id;
3112
3113             switch (columnIndex)
3114             {
3115                 case 1: // ニックネーム
3116                     return ComparerMode.Nickname;
3117                 case 2: // 本文
3118                     return ComparerMode.Data;
3119                 case 3: // 時刻=発言Id
3120                     return ComparerMode.Id;
3121                 case 4: // 名前
3122                     return ComparerMode.Name;
3123                 case 7: // Source
3124                     return ComparerMode.Source;
3125                 default:
3126                     // 0:アイコン, 5:未読マーク, 6:プロテクト・フィルターマーク
3127                     return null;
3128             }
3129         }
3130
3131         /// <summary>
3132         /// 発言一覧の指定した位置の列でソートする
3133         /// </summary>
3134         /// <param name="columnIndex">ソートする列の位置 (表示上の順序で指定)</param>
3135         private void SetSortColumnByDisplayIndex(int columnIndex)
3136         {
3137             // 表示上の列の位置から ColumnHeader を求める
3138             var col = this._curList.Columns.Cast<ColumnHeader>()
3139                 .FirstOrDefault(x => x.DisplayIndex == columnIndex);
3140
3141             if (col == null)
3142                 return;
3143
3144             var comparerMode = this.GetComparerModeByColumnIndex(col.Index);
3145             if (comparerMode == null)
3146                 return;
3147
3148             this.SetSortColumn(comparerMode.Value);
3149         }
3150
3151         /// <summary>
3152         /// 発言一覧の最後列の項目でソートする
3153         /// </summary>
3154         private void SetSortLastColumn()
3155         {
3156             // 表示上の最後列にある ColumnHeader を求める
3157             var col = this._curList.Columns.Cast<ColumnHeader>()
3158                 .OrderByDescending(x => x.DisplayIndex)
3159                 .First();
3160
3161             var comparerMode = this.GetComparerModeByColumnIndex(col.Index);
3162             if (comparerMode == null)
3163                 return;
3164
3165             this.SetSortColumn(comparerMode.Value);
3166         }
3167
3168         /// <summary>
3169         /// 発言一覧を指定された ComparerMode に基づいてソートする
3170         /// </summary>
3171         private void SetSortColumn(ComparerMode sortColumn)
3172         {
3173             if (SettingManager.Common.SortOrderLock)
3174                 return;
3175
3176             this._statuses.ToggleSortOrder(sortColumn);
3177             this.InitColumnText();
3178
3179             var list = this._curList;
3180             if (_iconCol)
3181             {
3182                 list.Columns[0].Text = this.ColumnText[0];
3183                 list.Columns[1].Text = this.ColumnText[2];
3184             }
3185             else
3186             {
3187                 for (var i = 0; i <= 7; i++)
3188                 {
3189                     list.Columns[i].Text = this.ColumnText[i];
3190                 }
3191             }
3192
3193             this.PurgeListViewItemCache();
3194
3195             var tab = this._statuses.Tabs[this._curTab.Text];
3196             if (tab.AllCount > 0 && this._curPost != null)
3197             {
3198                 var idx = tab.IndexOf(this._curPost.StatusId);
3199                 if (idx > -1)
3200                 {
3201                     this.SelectListItem(list, idx);
3202                     list.EnsureVisible(idx);
3203                 }
3204             }
3205             list.Refresh();
3206
3207             this.ModifySettingCommon = true;
3208         }
3209
3210         private void TweenMain_LocationChanged(object sender, EventArgs e)
3211         {
3212             if (this.WindowState == FormWindowState.Normal && !_initialLayout)
3213             {
3214                 _myLoc = this.DesktopLocation;
3215                 ModifySettingLocal = true;
3216             }
3217         }
3218
3219         private void ContextMenuOperate_Opening(object sender, CancelEventArgs e)
3220         {
3221             if (ListTab.SelectedTab == null) return;
3222             if (_statuses == null || _statuses.Tabs == null || !_statuses.Tabs.ContainsKey(ListTab.SelectedTab.Text)) return;
3223             if (!this.ExistCurrentPost)
3224             {
3225                 ReplyStripMenuItem.Enabled = false;
3226                 ReplyAllStripMenuItem.Enabled = false;
3227                 DMStripMenuItem.Enabled = false;
3228                 ShowProfileMenuItem.Enabled = false;
3229                 ShowUserTimelineContextMenuItem.Enabled = false;
3230                 ListManageUserContextToolStripMenuItem2.Enabled = false;
3231                 MoveToFavToolStripMenuItem.Enabled = false;
3232                 TabMenuItem.Enabled = false;
3233                 IDRuleMenuItem.Enabled = false;
3234                 SourceRuleMenuItem.Enabled = false;
3235                 ReadedStripMenuItem.Enabled = false;
3236                 UnreadStripMenuItem.Enabled = false;
3237             }
3238             else
3239             {
3240                 ShowProfileMenuItem.Enabled = true;
3241                 ListManageUserContextToolStripMenuItem2.Enabled = true;
3242                 ReplyStripMenuItem.Enabled = true;
3243                 ReplyAllStripMenuItem.Enabled = true;
3244                 DMStripMenuItem.Enabled = true;
3245                 ShowUserTimelineContextMenuItem.Enabled = true;
3246                 MoveToFavToolStripMenuItem.Enabled = true;
3247                 TabMenuItem.Enabled = true;
3248                 IDRuleMenuItem.Enabled = true;
3249                 SourceRuleMenuItem.Enabled = true;
3250                 ReadedStripMenuItem.Enabled = true;
3251                 UnreadStripMenuItem.Enabled = true;
3252             }
3253             if (_statuses.Tabs[ListTab.SelectedTab.Text].TabType == MyCommon.TabUsageType.DirectMessage || !this.ExistCurrentPost || _curPost.IsDm)
3254             {
3255                 FavAddToolStripMenuItem.Enabled = false;
3256                 FavRemoveToolStripMenuItem.Enabled = false;
3257                 StatusOpenMenuItem.Enabled = false;
3258                 ShowRelatedStatusesMenuItem.Enabled = false;
3259
3260                 ReTweetStripMenuItem.Enabled = false;
3261                 ReTweetUnofficialStripMenuItem.Enabled = false;
3262                 QuoteStripMenuItem.Enabled = false;
3263                 FavoriteRetweetContextMenu.Enabled = false;
3264                 FavoriteRetweetUnofficialContextMenu.Enabled = false;
3265             }
3266             else
3267             {
3268                 FavAddToolStripMenuItem.Enabled = true;
3269                 FavRemoveToolStripMenuItem.Enabled = true;
3270                 StatusOpenMenuItem.Enabled = true;
3271                 ShowRelatedStatusesMenuItem.Enabled = true;  //PublicSearchの時問題出るかも
3272
3273                 if (!_curPost.CanRetweetBy(this.twitterApi.CurrentUserId))
3274                 {
3275                     ReTweetStripMenuItem.Enabled = false;
3276                     ReTweetUnofficialStripMenuItem.Enabled = false;
3277                     QuoteStripMenuItem.Enabled = false;
3278                     FavoriteRetweetContextMenu.Enabled = false;
3279                     FavoriteRetweetUnofficialContextMenu.Enabled = false;
3280                 }
3281                 else
3282                 {
3283                     ReTweetStripMenuItem.Enabled = true;
3284                     ReTweetUnofficialStripMenuItem.Enabled = true;
3285                     QuoteStripMenuItem.Enabled = true;
3286                     FavoriteRetweetContextMenu.Enabled = true;
3287                     FavoriteRetweetUnofficialContextMenu.Enabled = true;
3288                 }
3289             }
3290             //if (_statuses.Tabs[ListTab.SelectedTab.Text].TabType != MyCommon.TabUsageType.Favorites)
3291             //{
3292             //    RefreshMoreStripMenuItem.Enabled = true;
3293             //}
3294             //else
3295             //{
3296             //    RefreshMoreStripMenuItem.Enabled = false;
3297             //}
3298             if (!this.ExistCurrentPost
3299                 || _curPost.InReplyToStatusId == null)
3300             {
3301                 RepliedStatusOpenMenuItem.Enabled = false;
3302             }
3303             else
3304             {
3305                 RepliedStatusOpenMenuItem.Enabled = true;
3306             }
3307             if (!this.ExistCurrentPost || string.IsNullOrEmpty(_curPost.RetweetedBy))
3308             {
3309                 MoveToRTHomeMenuItem.Enabled = false;
3310             }
3311             else
3312             {
3313                 MoveToRTHomeMenuItem.Enabled = true;
3314             }
3315
3316             if (this.ExistCurrentPost)
3317             {
3318                 this.DeleteStripMenuItem.Enabled = this._curPost.CanDeleteBy(this.tw.UserId);
3319                 if (this._curPost.RetweetedByUserId == this.tw.UserId)
3320                     this.DeleteStripMenuItem.Text = Properties.Resources.DeleteMenuText2;
3321                 else
3322                     this.DeleteStripMenuItem.Text = Properties.Resources.DeleteMenuText1;
3323             }
3324         }
3325
3326         private void ReplyStripMenuItem_Click(object sender, EventArgs e)
3327             => this.MakeReplyOrDirectStatus(false, true);
3328
3329         private void DMStripMenuItem_Click(object sender, EventArgs e)
3330             => this.MakeReplyOrDirectStatus(false, false);
3331
3332         private async Task doStatusDelete()
3333         {
3334             if (this._curTab == null || this._curList == null)
3335                 return;
3336
3337             if (this._curList.SelectedIndices.Count == 0)
3338                 return;
3339
3340             var posts = this._curList.SelectedIndices.Cast<int>()
3341                 .Select(x => this.GetCurTabPost(x))
3342                 .ToArray();
3343
3344             // 選択されたツイートの中に削除可能なものが一つでもあるか
3345             if (!posts.Any(x => x.CanDeleteBy(this.tw.UserId)))
3346                 return;
3347
3348             var ret = MessageBox.Show(this,
3349                 string.Format(Properties.Resources.DeleteStripMenuItem_ClickText1, Environment.NewLine),
3350                 Properties.Resources.DeleteStripMenuItem_ClickText2,
3351                 MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
3352
3353             if (ret != DialogResult.OK)
3354                 return;
3355
3356             var focusedIndex = this._curList.FocusedItem?.Index ?? this._curList.TopItem?.Index ?? 0;
3357
3358             using (ControlTransaction.Cursor(this, Cursors.WaitCursor))
3359             {
3360                 Exception lastException = null;
3361                 foreach (var post in posts)
3362                 {
3363                     if (!post.CanDeleteBy(this.tw.UserId))
3364                         continue;
3365
3366                     try
3367                     {
3368                         if (post.IsDm)
3369                         {
3370                             await this.twitterApi.DirectMessagesEventsDestroy(post.StatusId.ToString(CultureInfo.InvariantCulture));
3371                         }
3372                         else
3373                         {
3374                             if (post.RetweetedByUserId == this.tw.UserId)
3375                             {
3376                                 // 自分が RT したツイート (自分が RT した自分のツイートも含む)
3377                                 //   => RT を取り消し
3378                                 await this.twitterApi.StatusesDestroy(post.StatusId)
3379                                     .IgnoreResponse();
3380                             }
3381                             else
3382                             {
3383                                 if (post.UserId == this.tw.UserId)
3384                                 {
3385                                     if (post.RetweetedId != null)
3386                                         // 他人に RT された自分のツイート
3387                                         //   => RT 元の自分のツイートを削除
3388                                         await this.twitterApi.StatusesDestroy(post.RetweetedId.Value)
3389                                             .IgnoreResponse();
3390                                     else
3391                                         // 自分のツイート
3392                                         //   => ツイートを削除
3393                                         await this.twitterApi.StatusesDestroy(post.StatusId)
3394                                             .IgnoreResponse();
3395                                 }
3396                             }
3397                         }
3398                     }
3399                     catch (WebApiException ex)
3400                     {
3401                         lastException = ex;
3402                         continue;
3403                     }
3404
3405                     this._statuses.RemovePostFromAllTabs(post.StatusId, setIsDeleted: true);
3406                 }
3407
3408                 if (lastException == null)
3409                     this.StatusLabel.Text = Properties.Resources.DeleteStripMenuItem_ClickText4; // 成功
3410                 else
3411                     this.StatusLabel.Text = Properties.Resources.DeleteStripMenuItem_ClickText3; // 失敗
3412
3413                 this.PurgeListViewItemCache();
3414                 this._curPost = null;
3415                 this._curItemIndex = -1;
3416
3417                 foreach (var tabPage in this.ListTab.TabPages.Cast<TabPage>())
3418                 {
3419                     var listView = (DetailsListView)tabPage.Tag;
3420                     var tab = this._statuses.Tabs[tabPage.Text];
3421
3422                     using (ControlTransaction.Update(listView))
3423                     {
3424                         listView.VirtualListSize = tab.AllCount;
3425
3426                         if (tabPage == this._curTab)
3427                         {
3428                             listView.SelectedIndices.Clear();
3429
3430                             if (tab.AllCount != 0)
3431                             {
3432                                 int selectedIndex;
3433                                 if (tab.AllCount - 1 > focusedIndex && focusedIndex > -1)
3434                                     selectedIndex = focusedIndex;
3435                                 else
3436                                     selectedIndex = tab.AllCount - 1;
3437
3438                                 listView.SelectedIndices.Add(selectedIndex);
3439                                 listView.EnsureVisible(selectedIndex);
3440                                 listView.FocusedItem = listView.Items[selectedIndex];
3441                             }
3442                         }
3443                     }
3444
3445                     if (SettingManager.Common.TabIconDisp && tab.UnreadCount == 0)
3446                     {
3447                         if (tabPage.ImageIndex == 0)
3448                             tabPage.ImageIndex = -1; // タブアイコン
3449                     }
3450                 }
3451
3452                 if (!SettingManager.Common.TabIconDisp)
3453                     this.ListTab.Refresh();
3454             }
3455         }
3456
3457         private async void DeleteStripMenuItem_Click(object sender, EventArgs e)
3458             => await this.doStatusDelete();
3459
3460         private void ReadedStripMenuItem_Click(object sender, EventArgs e)
3461         {
3462             using (ControlTransaction.Update(this._curList))
3463             {
3464                 foreach (int idx in _curList.SelectedIndices)
3465                 {
3466                     var post = this._statuses.Tabs[this._curTab.Text][idx];
3467                     this._statuses.SetReadAllTab(post.StatusId, read: true);
3468                     ChangeCacheStyleRead(true, idx);
3469                 }
3470                 ColorizeList();
3471             }
3472             foreach (TabPage tb in ListTab.TabPages)
3473             {
3474                 if (_statuses.Tabs[tb.Text].UnreadCount == 0)
3475                 {
3476                     if (SettingManager.Common.TabIconDisp)
3477                     {
3478                         if (tb.ImageIndex == 0) tb.ImageIndex = -1; //タブアイコン
3479                     }
3480                 }
3481             }
3482             if (!SettingManager.Common.TabIconDisp) ListTab.Refresh();
3483         }
3484
3485         private void UnreadStripMenuItem_Click(object sender, EventArgs e)
3486         {
3487             using (ControlTransaction.Update(this._curList))
3488             {
3489                 foreach (int idx in _curList.SelectedIndices)
3490                 {
3491                     var post = this._statuses.Tabs[this._curTab.Text][idx];
3492                     this._statuses.SetReadAllTab(post.StatusId, read: false);
3493                     ChangeCacheStyleRead(false, idx);
3494                 }
3495                 ColorizeList();
3496             }
3497             foreach (TabPage tb in ListTab.TabPages)
3498             {
3499                 if (_statuses.Tabs[tb.Text].UnreadCount > 0)
3500                 {
3501                     if (SettingManager.Common.TabIconDisp)
3502                     {
3503                         if (tb.ImageIndex == -1) tb.ImageIndex = 0; //タブアイコン
3504                     }
3505                 }
3506             }
3507             if (!SettingManager.Common.TabIconDisp) ListTab.Refresh();
3508         }
3509
3510         private async void RefreshStripMenuItem_Click(object sender, EventArgs e)
3511             => await this.DoRefresh();
3512
3513         private async Task DoRefresh()
3514         {
3515             if (_curTab != null)
3516             {
3517                 if (!this._statuses.Tabs.TryGetValue(this._curTab.Text, out var tab))
3518                     return;
3519
3520                 await this.RefreshTabAsync(tab);
3521             }
3522             else
3523             {
3524                 await this.RefreshTabAsync<HomeTabModel>();
3525             }
3526         }
3527
3528         private async Task DoRefreshMore()
3529         {
3530             if (_curTab != null)
3531             {
3532                 if (!this._statuses.Tabs.TryGetValue(this._curTab.Text, out var tab))
3533                     return;
3534
3535                 await this.RefreshTabAsync(tab, backward: true);
3536             }
3537             else
3538             {
3539                 await this.RefreshTabAsync<HomeTabModel>(backward: true);
3540             }
3541         }
3542
3543         private DialogResult ShowSettingDialog(bool showTaskbarIcon = false)
3544         {
3545             DialogResult result = DialogResult.Abort;
3546
3547             using (var settingDialog = new AppendSettingDialog())
3548             {
3549                 settingDialog.Icon = this.MainIcon;
3550                 settingDialog.Owner = this;
3551                 settingDialog.ShowInTaskbar = showTaskbarIcon;
3552                 settingDialog.IntervalChanged += this.TimerInterval_Changed;
3553
3554                 settingDialog.tw = this.tw;
3555                 settingDialog.twitterApi = this.twitterApi;
3556
3557                 settingDialog.LoadConfig(SettingManager.Common, SettingManager.Local);
3558
3559                 try
3560                 {
3561                     result = settingDialog.ShowDialog(this);
3562                 }
3563                 catch (Exception)
3564                 {
3565                     return DialogResult.Abort;
3566                 }
3567
3568                 if (result == DialogResult.OK)
3569                 {
3570                     lock (_syncObject)
3571                     {
3572                         settingDialog.SaveConfig(SettingManager.Common, SettingManager.Local);
3573                     }
3574                 }
3575             }
3576
3577             return result;
3578         }
3579
3580         private async void SettingStripMenuItem_Click(object sender, EventArgs e)
3581         {
3582             // 設定画面表示前のユーザー情報
3583             var oldUser = new { tw.AccessToken, tw.AccessTokenSecret, tw.Username, tw.UserId };
3584
3585             var oldIconSz = SettingManager.Common.IconSize;
3586
3587             if (ShowSettingDialog() == DialogResult.OK)
3588             {
3589                 lock (_syncObject)
3590                 {
3591                     tw.RestrictFavCheck = SettingManager.Common.RestrictFavCheck;
3592                     tw.ReadOwnPost = SettingManager.Common.ReadOwnPost;
3593                     ShortUrl.Instance.DisableExpanding = !SettingManager.Common.TinyUrlResolve;
3594                     ShortUrl.Instance.BitlyAccessToken = SettingManager.Common.BitlyAccessToken;
3595                     ShortUrl.Instance.BitlyId = SettingManager.Common.BilyUser;
3596                     ShortUrl.Instance.BitlyKey = SettingManager.Common.BitlyPwd;
3597                     TwitterApiConnection.RestApiHost = SettingManager.Common.TwitterApiHost;
3598
3599                     Networking.DefaultTimeout = TimeSpan.FromSeconds(SettingManager.Common.DefaultTimeOut);
3600                     Networking.UploadImageTimeout = TimeSpan.FromSeconds(SettingManager.Common.UploadImageTimeout);
3601                     Networking.SetWebProxy(SettingManager.Local.ProxyType,
3602                         SettingManager.Local.ProxyAddress, SettingManager.Local.ProxyPort,
3603                         SettingManager.Local.ProxyUser, SettingManager.Local.ProxyPassword);
3604                     Networking.ForceIPv4 = SettingManager.Common.ForceIPv4;
3605
3606                     ImageSelector.Reset(tw, this.tw.Configuration);
3607
3608                     try
3609                     {
3610                         if (SettingManager.Common.TabIconDisp)
3611                         {
3612                             ListTab.DrawItem -= ListTab_DrawItem;
3613                             ListTab.DrawMode = TabDrawMode.Normal;
3614                             ListTab.ImageList = this.TabImage;
3615                         }
3616                         else
3617                         {
3618                             ListTab.DrawItem -= ListTab_DrawItem;
3619                             ListTab.DrawItem += ListTab_DrawItem;
3620                             ListTab.DrawMode = TabDrawMode.OwnerDrawFixed;
3621                             ListTab.ImageList = null;
3622                         }
3623                     }
3624                     catch (Exception ex)
3625                     {
3626                         ex.Data["Instance"] = "ListTab(TabIconDisp)";
3627                         ex.Data["IsTerminatePermission"] = false;
3628                         throw;
3629                     }
3630
3631                     try
3632                     {
3633                         if (!SettingManager.Common.UnreadManage)
3634                         {
3635                             ReadedStripMenuItem.Enabled = false;
3636                             UnreadStripMenuItem.Enabled = false;
3637                             if (SettingManager.Common.TabIconDisp)
3638                             {
3639                                 foreach (TabPage myTab in ListTab.TabPages)
3640                                 {
3641                                     myTab.ImageIndex = -1;
3642                                 }
3643                             }
3644                         }
3645                         else
3646                         {
3647                             ReadedStripMenuItem.Enabled = true;
3648                             UnreadStripMenuItem.Enabled = true;
3649                         }
3650                     }
3651                     catch (Exception ex)
3652                     {
3653                         ex.Data["Instance"] = "ListTab(UnreadManage)";
3654                         ex.Data["IsTerminatePermission"] = false;
3655                         throw;
3656                     }
3657
3658                     // タブの表示位置の決定
3659                     SetTabAlignment();
3660
3661                     SplitContainer1.IsPanelInverted = !SettingManager.Common.StatusAreaAtBottom;
3662
3663                     var imgazyobizinet = ThumbnailGenerator.ImgAzyobuziNetInstance;
3664                     imgazyobizinet.Enabled = SettingManager.Common.EnableImgAzyobuziNet;
3665                     imgazyobizinet.DisabledInDM = SettingManager.Common.ImgAzyobuziNetDisabledInDM;
3666
3667                     this.PlaySoundMenuItem.Checked = SettingManager.Common.PlaySound;
3668                     this.PlaySoundFileMenuItem.Checked = SettingManager.Common.PlaySound;
3669                     _fntUnread = SettingManager.Local.FontUnread;
3670                     _clUnread = SettingManager.Local.ColorUnread;
3671                     _fntReaded = SettingManager.Local.FontRead;
3672                     _clReaded = SettingManager.Local.ColorRead;
3673                     _clFav = SettingManager.Local.ColorFav;
3674                     _clOWL = SettingManager.Local.ColorOWL;
3675                     _clRetweet = SettingManager.Local.ColorRetweet;
3676                     _fntDetail = SettingManager.Local.FontDetail;
3677                     _clDetail = SettingManager.Local.ColorDetail;
3678                     _clDetailLink = SettingManager.Local.ColorDetailLink;
3679                     _clDetailBackcolor = SettingManager.Local.ColorDetailBackcolor;
3680                     _clSelf = SettingManager.Local.ColorSelf;
3681                     _clAtSelf = SettingManager.Local.ColorAtSelf;
3682                     _clTarget = SettingManager.Local.ColorTarget;
3683                     _clAtTarget = SettingManager.Local.ColorAtTarget;
3684                     _clAtFromTarget = SettingManager.Local.ColorAtFromTarget;
3685                     _clAtTo = SettingManager.Local.ColorAtTo;
3686                     _clListBackcolor = SettingManager.Local.ColorListBackcolor;
3687                     _clInputBackcolor = SettingManager.Local.ColorInputBackcolor;
3688                     _clInputFont = SettingManager.Local.ColorInputFont;
3689                     _fntInputFont = SettingManager.Local.FontInputFont;
3690                     _brsBackColorMine.Dispose();
3691                     _brsBackColorAt.Dispose();
3692                     _brsBackColorYou.Dispose();
3693                     _brsBackColorAtYou.Dispose();
3694                     _brsBackColorAtFromTarget.Dispose();
3695                     _brsBackColorAtTo.Dispose();
3696                     _brsBackColorNone.Dispose();
3697                     _brsBackColorMine = new SolidBrush(_clSelf);
3698                     _brsBackColorAt = new SolidBrush(_clAtSelf);
3699                     _brsBackColorYou = new SolidBrush(_clTarget);
3700                     _brsBackColorAtYou = new SolidBrush(_clAtTarget);
3701                     _brsBackColorAtFromTarget = new SolidBrush(_clAtFromTarget);
3702                     _brsBackColorAtTo = new SolidBrush(_clAtTo);
3703                     _brsBackColorNone = new SolidBrush(_clListBackcolor);
3704
3705                     try
3706                     {
3707                         if (StatusText.Focused) StatusText.BackColor = _clInputBackcolor;
3708                         StatusText.Font = _fntInputFont;
3709                         StatusText.ForeColor = _clInputFont;
3710                     }
3711                     catch (Exception ex)
3712                     {
3713                         MessageBox.Show(ex.Message);
3714                     }
3715
3716                     try
3717                     {
3718                         InitDetailHtmlFormat();
3719                     }
3720                     catch (Exception ex)
3721                     {
3722                         ex.Data["Instance"] = "Font";
3723                         ex.Data["IsTerminatePermission"] = false;
3724                         throw;
3725                     }
3726
3727                     try
3728                     {
3729                         foreach (TabPage tb in ListTab.TabPages)
3730                         {
3731                             if (SettingManager.Common.TabIconDisp)
3732                             {
3733                                 if (_statuses.Tabs[tb.Text].UnreadCount == 0)
3734                                     tb.ImageIndex = -1;
3735                                 else
3736                                     tb.ImageIndex = 0;
3737                             }
3738                         }
3739                     }
3740                     catch (Exception ex)
3741                     {
3742                         ex.Data["Instance"] = "ListTab(TabIconDisp no2)";
3743                         ex.Data["IsTerminatePermission"] = false;
3744                         throw;
3745                     }
3746
3747                     try
3748                     {
3749                         var oldIconCol = _iconCol;
3750
3751                         if (SettingManager.Common.IconSize != oldIconSz)
3752                             ApplyListViewIconSize(SettingManager.Common.IconSize);
3753
3754                         foreach (TabPage tp in ListTab.TabPages)
3755                         {
3756                             DetailsListView lst = (DetailsListView)tp.Tag;
3757
3758                             using (ControlTransaction.Update(lst))
3759                             {
3760                                 lst.GridLines = SettingManager.Common.ShowGrid;
3761                                 lst.Font = _fntReaded;
3762                                 lst.BackColor = _clListBackcolor;
3763
3764                                 if (_iconCol != oldIconCol)
3765                                     ResetColumns(lst);
3766                             }
3767                         }
3768                     }
3769                     catch (Exception ex)
3770                     {
3771                         ex.Data["Instance"] = "ListView(IconSize)";
3772                         ex.Data["IsTerminatePermission"] = false;
3773                         throw;
3774                     }
3775
3776                     SetMainWindowTitle();
3777                     SetNotifyIconText();
3778
3779                     this.PurgeListViewItemCache();
3780                     _curList?.Refresh();
3781                     ListTab.Refresh();
3782
3783                     _hookGlobalHotkey.UnregisterAllOriginalHotkey();
3784                     if (SettingManager.Common.HotkeyEnabled)
3785                     {
3786                         ///グローバルホットキーの登録。設定で変更可能にするかも
3787                         HookGlobalHotkey.ModKeys modKey = HookGlobalHotkey.ModKeys.None;
3788                         if ((SettingManager.Common.HotkeyModifier & Keys.Alt) == Keys.Alt)
3789                             modKey |= HookGlobalHotkey.ModKeys.Alt;
3790                         if ((SettingManager.Common.HotkeyModifier & Keys.Control) == Keys.Control)
3791                             modKey |= HookGlobalHotkey.ModKeys.Ctrl;
3792                         if ((SettingManager.Common.HotkeyModifier & Keys.Shift) == Keys.Shift)
3793                             modKey |=  HookGlobalHotkey.ModKeys.Shift;
3794                         if ((SettingManager.Common.HotkeyModifier & Keys.LWin) == Keys.LWin)
3795                             modKey |= HookGlobalHotkey.ModKeys.Win;
3796
3797                         _hookGlobalHotkey.RegisterOriginalHotkey(SettingManager.Common.HotkeyKey, SettingManager.Common.HotkeyValue, modKey);
3798                     }
3799
3800                     if (SettingManager.Common.IsUseNotifyGrowl) gh.RegisterGrowl();
3801                     try
3802                     {
3803                         StatusText_TextChanged(null, null);
3804                     }
3805                     catch (Exception)
3806                     {
3807                     }
3808                 }
3809             }
3810             else
3811             {
3812                 // キャンセル時は Twitter クラスの認証情報を画面表示前の状態に戻す
3813                 this.tw.Initialize(oldUser.AccessToken, oldUser.AccessTokenSecret, oldUser.Username, oldUser.UserId);
3814             }
3815
3816             Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3817
3818             this.TopMost = SettingManager.Common.AlwaysTop;
3819             SaveConfigsAll(false);
3820
3821             if (tw.UserId != oldUser.UserId)
3822                 await this.doGetFollowersMenu();
3823         }
3824
3825         /// <summary>
3826         /// タブの表示位置を設定する
3827         /// </summary>
3828         private void SetTabAlignment()
3829         {
3830             var newAlignment = SettingManager.Common.ViewTabBottom ? TabAlignment.Bottom : TabAlignment.Top;
3831             if (ListTab.Alignment == newAlignment) return;
3832
3833             // 各タブのリスト上の選択位置などを退避
3834             var listSelections = this.SaveListViewSelection();
3835
3836             ListTab.Alignment = newAlignment;
3837
3838             foreach (TabPage tab in ListTab.TabPages)
3839             {
3840                 DetailsListView lst = (DetailsListView)tab.Tag;
3841                 TabModel tabInfo = _statuses.Tabs[tab.Text];
3842                 using (ControlTransaction.Update(lst))
3843                 {
3844                     // 選択位置などを復元
3845                     this.RestoreListViewSelection(lst, tabInfo, listSelections[tabInfo.TabName]);
3846                 }
3847             }
3848         }
3849
3850         private void ApplyListViewIconSize(MyCommon.IconSizes iconSz)
3851         {
3852             // アイコンサイズの再設定
3853             _iconCol = false;
3854             switch (iconSz)
3855             {
3856                 case MyCommon.IconSizes.IconNone:
3857                     _iconSz = 0;
3858                     break;
3859                 case MyCommon.IconSizes.Icon16:
3860                     _iconSz = 16;
3861                     break;
3862                 case MyCommon.IconSizes.Icon24:
3863                     _iconSz = 26;
3864                     break;
3865                 case MyCommon.IconSizes.Icon48:
3866                     _iconSz = 48;
3867                     break;
3868                 case MyCommon.IconSizes.Icon48_2:
3869                     _iconSz = 48;
3870                     _iconCol = true;
3871                     break;
3872             }
3873
3874             if (_iconSz > 0)
3875             {
3876                 // ディスプレイの DPI 設定を考慮したサイズを設定する
3877                 _listViewImageList.ImageSize = new Size(
3878                     1,
3879                     (int)Math.Ceiling(this._iconSz * this.CurrentScaleFactor.Height));
3880             }
3881             else
3882             {
3883                 _listViewImageList.ImageSize = new Size(1, 1);
3884             }
3885         }
3886
3887         private void ResetColumns(DetailsListView list)
3888         {
3889             using (ControlTransaction.Update(list))
3890             using (ControlTransaction.Layout(list, false))
3891             {
3892                 // カラムヘッダの再設定
3893                 list.ColumnClick -= MyList_ColumnClick;
3894                 list.DrawColumnHeader -= MyList_DrawColumnHeader;
3895                 list.ColumnReordered -= MyList_ColumnReordered;
3896                 list.ColumnWidthChanged -= MyList_ColumnWidthChanged;
3897
3898                 var cols = list.Columns.Cast<ColumnHeader>().ToList();
3899                 list.Columns.Clear();
3900                 cols.ForEach(col => col.Dispose());
3901                 cols.Clear();
3902
3903                 InitColumns(list, true);
3904
3905                 list.ColumnClick += MyList_ColumnClick;
3906                 list.DrawColumnHeader += MyList_DrawColumnHeader;
3907                 list.ColumnReordered += MyList_ColumnReordered;
3908                 list.ColumnWidthChanged += MyList_ColumnWidthChanged;
3909             }
3910         }
3911
3912         public void AddNewTabForSearch(string searchWord)
3913         {
3914             //同一検索条件のタブが既に存在すれば、そのタブアクティブにして終了
3915             foreach (var tb in _statuses.GetTabsByType<PublicSearchTabModel>())
3916             {
3917                 if (tb.SearchWords == searchWord && string.IsNullOrEmpty(tb.SearchLang))
3918                 {
3919                     foreach (TabPage tp in ListTab.TabPages)
3920                     {
3921                         if (tb.TabName == tp.Text)
3922                         {
3923                             ListTab.SelectedTab = tp;
3924                             return;
3925                         }
3926                     }
3927                 }
3928             }
3929             //ユニークなタブ名生成
3930             string tabName = searchWord;
3931             for (int i = 0; i <= 100; i++)
3932             {
3933                 if (_statuses.ContainsTab(tabName))
3934                     tabName += "_";
3935                 else
3936                     break;
3937             }
3938             //タブ追加
3939             var tab = new PublicSearchTabModel(tabName);
3940             _statuses.AddTab(tab);
3941             AddNewTab(tab, startup: false);
3942             //追加したタブをアクティブに
3943             ListTab.SelectedIndex = ListTab.TabPages.Count - 1;
3944             //検索条件の設定
3945             ComboBox cmb = (ComboBox)ListTab.SelectedTab.Controls["panelSearch"].Controls["comboSearch"];
3946             cmb.Items.Add(searchWord);
3947             cmb.Text = searchWord;
3948             SaveConfigsTabs();
3949             //検索実行
3950             this.SearchButton_Click(ListTab.SelectedTab.Controls["panelSearch"].Controls["comboSearch"], null);
3951         }
3952
3953         private async Task ShowUserTimeline()
3954         {
3955             if (!this.ExistCurrentPost) return;
3956             await this.AddNewTabForUserTimeline(_curPost.ScreenName);
3957         }
3958
3959         private void SearchComboBox_KeyDown(object sender, KeyEventArgs e)
3960         {
3961             if (e.KeyCode == Keys.Escape)
3962             {
3963                 TabPage relTp = ListTab.SelectedTab;
3964                 RemoveSpecifiedTab(relTp.Text, false);
3965                 SaveConfigsTabs();
3966                 e.SuppressKeyPress = true;
3967             }
3968         }
3969
3970         public async Task AddNewTabForUserTimeline(string user)
3971         {
3972             //同一検索条件のタブが既に存在すれば、そのタブアクティブにして終了
3973             foreach (var tb in _statuses.GetTabsByType<UserTimelineTabModel>())
3974             {
3975                 if (tb.ScreenName == user)
3976                 {
3977                     foreach (TabPage tp in ListTab.TabPages)
3978                     {
3979                         if (tb.TabName == tp.Text)
3980                         {
3981                             ListTab.SelectedTab = tp;
3982                             return;
3983                         }
3984                     }
3985                 }
3986             }
3987             //ユニークなタブ名生成
3988             string tabName = "user:" + user;
3989             while (_statuses.ContainsTab(tabName))
3990             {
3991                 tabName += "_";
3992             }
3993             //タブ追加
3994             var tab = new UserTimelineTabModel(tabName, user);
3995             this._statuses.AddTab(tab);
3996             this.AddNewTab(tab, startup: false);
3997             //追加したタブをアクティブに
3998             ListTab.SelectedIndex = ListTab.TabPages.Count - 1;
3999             SaveConfigsTabs();
4000             //検索実行
4001             await this.RefreshTabAsync(tab);
4002         }
4003
4004         public bool AddNewTab(TabModel tab, bool startup)
4005         {
4006             //重複チェック
4007             foreach (TabPage tb in ListTab.TabPages)
4008             {
4009                 if (tb.Text == tab.TabName) return false;
4010             }
4011
4012             //新規タブ名チェック
4013             if (tab.TabName == Properties.Resources.AddNewTabText1) return false;
4014
4015             var _tabPage = new TabPage();
4016             var _listCustom = new DetailsListView();
4017
4018             int cnt = ListTab.TabPages.Count;
4019
4020             ///ToDo:Create and set controls follow tabtypes
4021
4022             using (ControlTransaction.Update(_listCustom))
4023             using (ControlTransaction.Layout(this.SplitContainer1.Panel1, false))
4024             using (ControlTransaction.Layout(this.SplitContainer1.Panel2, false))
4025             using (ControlTransaction.Layout(this.SplitContainer1, false))
4026             using (ControlTransaction.Layout(this.ListTab, false))
4027             using (ControlTransaction.Layout(this))
4028             using (ControlTransaction.Layout(_tabPage, false))
4029             {
4030                 _tabPage.Controls.Add(_listCustom);
4031
4032                 /// UserTimeline関連
4033                 var userTab = tab as UserTimelineTabModel;
4034                 var listTab = tab as ListTimelineTabModel;
4035                 var searchTab = tab as PublicSearchTabModel;
4036
4037                 if (userTab != null || listTab != null)
4038                 {
4039                     var label = new Label
4040                     {
4041                         Dock = DockStyle.Top,
4042                         Name = "labelUser",
4043                         TabIndex = 0,
4044                     };
4045
4046                     if (listTab != null)
4047                     {
4048                         label.Text = listTab.ListInfo.ToString();
4049                     }
4050                     else if (userTab != null)
4051                     {
4052                         label.Text = userTab.ScreenName + "'s Timeline";
4053                     }
4054                     label.TextAlign = ContentAlignment.MiddleLeft;
4055                     using (ComboBox tmpComboBox = new ComboBox())
4056                     {
4057                         label.Height = tmpComboBox.Height;
4058                     }
4059                     _tabPage.Controls.Add(label);
4060                 }
4061                 /// 検索関連の準備
4062                 else if (searchTab != null)
4063                 {
4064                     var pnl = new Panel();
4065
4066                     var lbl = new Label();
4067                     var cmb = new ComboBox();
4068                     var btn = new Button();
4069                     var cmbLang = new ComboBox();
4070
4071                     using (ControlTransaction.Layout(pnl, false))
4072                     {
4073                         pnl.Controls.Add(cmb);
4074                         pnl.Controls.Add(cmbLang);
4075                         pnl.Controls.Add(btn);
4076                         pnl.Controls.Add(lbl);
4077                         pnl.Name = "panelSearch";
4078                         pnl.TabIndex = 0;
4079                         pnl.Dock = DockStyle.Top;
4080                         pnl.Height = cmb.Height;
4081                         pnl.Enter += SearchControls_Enter;
4082                         pnl.Leave += SearchControls_Leave;
4083
4084                         cmb.Text = "";
4085                         cmb.Anchor = AnchorStyles.Left | AnchorStyles.Right;
4086                         cmb.Dock = DockStyle.Fill;
4087                         cmb.Name = "comboSearch";
4088                         cmb.DropDownStyle = ComboBoxStyle.DropDown;
4089                         cmb.ImeMode = ImeMode.NoControl;
4090                         cmb.TabStop = false;
4091                         cmb.TabIndex = 1;
4092                         cmb.AutoCompleteMode = AutoCompleteMode.None;
4093                         cmb.KeyDown += SearchComboBox_KeyDown;
4094
4095                         cmbLang.Text = "";
4096                         cmbLang.Anchor = AnchorStyles.Left | AnchorStyles.Right;
4097                         cmbLang.Dock = DockStyle.Right;
4098                         cmbLang.Width = 50;
4099                         cmbLang.Name = "comboLang";
4100                         cmbLang.DropDownStyle = ComboBoxStyle.DropDownList;
4101                         cmbLang.TabStop = false;
4102                         cmbLang.TabIndex = 2;
4103                         cmbLang.Items.Add("");
4104                         cmbLang.Items.Add("ja");
4105                         cmbLang.Items.Add("en");
4106                         cmbLang.Items.Add("ar");
4107                         cmbLang.Items.Add("da");
4108                         cmbLang.Items.Add("nl");
4109                         cmbLang.Items.Add("fa");
4110                         cmbLang.Items.Add("fi");
4111                         cmbLang.Items.Add("fr");
4112                         cmbLang.Items.Add("de");
4113                         cmbLang.Items.Add("hu");
4114                         cmbLang.Items.Add("is");
4115                         cmbLang.Items.Add("it");
4116                         cmbLang.Items.Add("no");
4117                         cmbLang.Items.Add("pl");
4118                         cmbLang.Items.Add("pt");
4119                         cmbLang.Items.Add("ru");
4120                         cmbLang.Items.Add("es");
4121                         cmbLang.Items.Add("sv");
4122                         cmbLang.Items.Add("th");
4123
4124                         lbl.Text = "Search(C-S-f)";
4125                         lbl.Name = "label1";
4126                         lbl.Dock = DockStyle.Left;
4127                         lbl.Width = 90;
4128                         lbl.Height = cmb.Height;
4129                         lbl.TextAlign = ContentAlignment.MiddleLeft;
4130                         lbl.TabIndex = 0;
4131
4132                         btn.Text = "Search";
4133                         btn.Name = "buttonSearch";
4134                         btn.UseVisualStyleBackColor = true;
4135                         btn.Dock = DockStyle.Right;
4136                         btn.TabStop = false;
4137                         btn.TabIndex = 3;
4138                         btn.Click += SearchButton_Click;
4139
4140                         if (!string.IsNullOrEmpty(searchTab.SearchWords))
4141                         {
4142                             cmb.Items.Add(searchTab.SearchWords);
4143                             cmb.Text = searchTab.SearchWords;
4144                         }
4145
4146                         cmbLang.Text = searchTab.SearchLang;
4147
4148                         _tabPage.Controls.Add(pnl);
4149                     }
4150                 }
4151
4152                 _tabPage.Tag = _listCustom;
4153                 this.ListTab.Controls.Add(_tabPage);
4154
4155                 _tabPage.Location = new Point(4, 4);
4156                 _tabPage.Name = "CTab" + cnt;
4157                 _tabPage.Size = new Size(380, 260);
4158                 _tabPage.TabIndex = 2 + cnt;
4159                 _tabPage.Text = tab.TabName;
4160                 _tabPage.UseVisualStyleBackColor = true;
4161                 _tabPage.AccessibleRole = AccessibleRole.PageTab;
4162
4163                 _listCustom.AccessibleName = Properties.Resources.AddNewTab_ListView_AccessibleName;
4164                 _listCustom.TabIndex = 1;
4165                 _listCustom.AllowColumnReorder = true;
4166                 _listCustom.ContextMenuStrip = this.ContextMenuOperate;
4167                 _listCustom.ColumnHeaderContextMenuStrip = this.ContextMenuColumnHeader;
4168                 _listCustom.Dock = DockStyle.Fill;
4169                 _listCustom.FullRowSelect = true;
4170                 _listCustom.HideSelection = false;
4171                 _listCustom.Location = new Point(0, 0);
4172                 _listCustom.Margin = new Padding(0);
4173                 _listCustom.Name = "CList" + Environment.TickCount;
4174                 _listCustom.ShowItemToolTips = true;
4175                 _listCustom.Size = new Size(380, 260);
4176                 _listCustom.UseCompatibleStateImageBehavior = false;
4177                 _listCustom.View = View.Details;
4178                 _listCustom.OwnerDraw = true;
4179                 _listCustom.VirtualMode = true;
4180                 _listCustom.Font = _fntReaded;
4181                 _listCustom.BackColor = _clListBackcolor;
4182
4183                 _listCustom.GridLines = SettingManager.Common.ShowGrid;
4184                 _listCustom.AllowDrop = true;
4185
4186                 _listCustom.SmallImageList = _listViewImageList;
4187
4188                 InitColumns(_listCustom, startup);
4189
4190                 _listCustom.SelectedIndexChanged += MyList_SelectedIndexChanged;
4191                 _listCustom.MouseDoubleClick += MyList_MouseDoubleClick;
4192                 _listCustom.ColumnClick += MyList_ColumnClick;
4193                 _listCustom.DrawColumnHeader += MyList_DrawColumnHeader;
4194                 _listCustom.DragDrop += TweenMain_DragDrop;
4195                 _listCustom.DragEnter += TweenMain_DragEnter;
4196                 _listCustom.DragOver += TweenMain_DragOver;
4197                 _listCustom.DrawItem += MyList_DrawItem;
4198                 _listCustom.MouseClick += MyList_MouseClick;
4199                 _listCustom.ColumnReordered += MyList_ColumnReordered;
4200                 _listCustom.ColumnWidthChanged += MyList_ColumnWidthChanged;
4201                 _listCustom.CacheVirtualItems += MyList_CacheVirtualItems;
4202                 _listCustom.RetrieveVirtualItem += MyList_RetrieveVirtualItem;
4203                 _listCustom.DrawSubItem += MyList_DrawSubItem;
4204                 _listCustom.HScrolled += MyList_HScrolled;
4205             }
4206
4207             return true;
4208         }
4209
4210         public bool RemoveSpecifiedTab(string TabName, bool confirm)
4211         {
4212             var tabInfo = _statuses.GetTabByName(TabName);
4213             if (tabInfo.IsDefaultTabType || tabInfo.Protected) return false;
4214
4215             if (confirm)
4216             {
4217                 string tmp = string.Format(Properties.Resources.RemoveSpecifiedTabText1, Environment.NewLine);
4218                 if (MessageBox.Show(tmp, TabName + " " + Properties.Resources.RemoveSpecifiedTabText2,
4219                                  MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Cancel)
4220                 {
4221                     return false;
4222                 }
4223             }
4224
4225             var _tabPage = ListTab.TabPages.Cast<TabPage>().FirstOrDefault(tp => tp.Text == TabName);
4226             if (_tabPage == null) return false;
4227
4228             SetListProperty();   //他のタブに列幅等を反映
4229
4230             //オブジェクトインスタンスの削除
4231             DetailsListView _listCustom = (DetailsListView)_tabPage.Tag;
4232             _tabPage.Tag = null;
4233
4234             using (ControlTransaction.Layout(this.SplitContainer1.Panel1, false))
4235             using (ControlTransaction.Layout(this.SplitContainer1.Panel2, false))
4236             using (ControlTransaction.Layout(this.SplitContainer1, false))
4237             using (ControlTransaction.Layout(this.ListTab, false))
4238             using (ControlTransaction.Layout(this))
4239             using (ControlTransaction.Layout(_tabPage, false))
4240             {
4241                 if (this.ListTab.SelectedTab == _tabPage)
4242                 {
4243                     this.ListTab.SelectTab((this._beforeSelectedTab != null && this.ListTab.TabPages.Contains(this._beforeSelectedTab)) ? this._beforeSelectedTab : this.ListTab.TabPages[0]);
4244                     this._beforeSelectedTab = null;
4245                 }
4246                 this.ListTab.Controls.Remove(_tabPage);
4247
4248                 // 後付けのコントロールを破棄
4249                 if (tabInfo.TabType == MyCommon.TabUsageType.UserTimeline || tabInfo.TabType == MyCommon.TabUsageType.Lists)
4250                 {
4251                     using (Control label = _tabPage.Controls["labelUser"])
4252                     {
4253                         _tabPage.Controls.Remove(label);
4254                     }
4255                 }
4256                 else if (tabInfo.TabType == MyCommon.TabUsageType.PublicSearch)
4257                 {
4258                     using (Control pnl = _tabPage.Controls["panelSearch"])
4259                     {
4260                         pnl.Enter -= SearchControls_Enter;
4261                         pnl.Leave -= SearchControls_Leave;
4262                         _tabPage.Controls.Remove(pnl);
4263
4264                         foreach (Control ctrl in pnl.Controls)
4265                         {
4266                             if (ctrl.Name == "buttonSearch")
4267                             {
4268                                 ctrl.Click -= SearchButton_Click;
4269                             }
4270                             else if (ctrl.Name == "comboSearch")
4271                             {
4272                                 ctrl.KeyDown -= SearchComboBox_KeyDown;
4273                             }
4274                             pnl.Controls.Remove(ctrl);
4275                             ctrl.Dispose();
4276                         }
4277                     }
4278                 }
4279
4280                 _tabPage.Controls.Remove(_listCustom);
4281
4282                 _listCustom.SelectedIndexChanged -= MyList_SelectedIndexChanged;
4283                 _listCustom.MouseDoubleClick -= MyList_MouseDoubleClick;
4284                 _listCustom.ColumnClick -= MyList_ColumnClick;
4285                 _listCustom.DrawColumnHeader -= MyList_DrawColumnHeader;
4286                 _listCustom.DragDrop -= TweenMain_DragDrop;
4287                 _listCustom.DragEnter -= TweenMain_DragEnter;
4288                 _listCustom.DragOver -= TweenMain_DragOver;
4289                 _listCustom.DrawItem -= MyList_DrawItem;
4290                 _listCustom.MouseClick -= MyList_MouseClick;
4291                 _listCustom.ColumnReordered -= MyList_ColumnReordered;
4292                 _listCustom.ColumnWidthChanged -= MyList_ColumnWidthChanged;
4293                 _listCustom.CacheVirtualItems -= MyList_CacheVirtualItems;
4294                 _listCustom.RetrieveVirtualItem -= MyList_RetrieveVirtualItem;
4295                 _listCustom.DrawSubItem -= MyList_DrawSubItem;
4296                 _listCustom.HScrolled -= MyList_HScrolled;
4297
4298                 var cols = _listCustom.Columns.Cast<ColumnHeader>().ToList<ColumnHeader>();
4299                 _listCustom.Columns.Clear();
4300                 cols.ForEach(col => col.Dispose());
4301                 cols.Clear();
4302
4303                 _listCustom.ContextMenuStrip = null;
4304                 _listCustom.ColumnHeaderContextMenuStrip = null;
4305                 _listCustom.Font = null;
4306
4307                 _listCustom.SmallImageList = null;
4308                 _listCustom.ListViewItemSorter = null;
4309
4310                 //キャッシュのクリア
4311                 if (_curTab.Equals(_tabPage))
4312                 {
4313                     _curTab = null;
4314                     _curItemIndex = -1;
4315                     _curList = null;
4316                     _curPost = null;
4317                 }
4318                 this.PurgeListViewItemCache();
4319             }
4320
4321             _tabPage.Dispose();
4322             _listCustom.Dispose();
4323             _statuses.RemoveTab(TabName);
4324
4325             foreach (TabPage tp in ListTab.TabPages)
4326             {
4327                 DetailsListView lst = (DetailsListView)tp.Tag;
4328                 var count = _statuses.Tabs[tp.Text].AllCount;
4329                 lst.VirtualListSize = count;
4330             }
4331
4332             return true;
4333         }
4334
4335         private void ListTab_Deselected(object sender, TabControlEventArgs e)
4336         {
4337             this.PurgeListViewItemCache();
4338             _beforeSelectedTab = e.TabPage;
4339         }
4340
4341         private void ListTab_MouseMove(object sender, MouseEventArgs e)
4342         {
4343             //タブのD&D
4344
4345             if (!SettingManager.Common.TabMouseLock && e.Button == MouseButtons.Left && _tabDrag)
4346             {
4347                 string tn = "";
4348                 Rectangle dragEnableRectangle = new Rectangle(_tabMouseDownPoint.X - (SystemInformation.DragSize.Width / 2), _tabMouseDownPoint.Y - (SystemInformation.DragSize.Height / 2), SystemInformation.DragSize.Width, SystemInformation.DragSize.Height);
4349                 if (!dragEnableRectangle.Contains(e.Location))
4350                 {
4351                     //タブが多段の場合にはMouseDownの前の段階で選択されたタブの段が変わっているので、このタイミングでカーソルの位置からタブを判定出来ない。
4352                     tn = ListTab.SelectedTab.Text;
4353                 }
4354
4355                 if (string.IsNullOrEmpty(tn)) return;
4356
4357                 foreach (TabPage tb in ListTab.TabPages)
4358                 {
4359                     if (tb.Text == tn)
4360                     {
4361                         ListTab.DoDragDrop(tb, DragDropEffects.All);
4362                         break;
4363                     }
4364                 }
4365             }
4366             else
4367             {
4368                 _tabDrag = false;
4369             }
4370
4371             Point cpos = new Point(e.X, e.Y);
4372             for (int i = 0; i < ListTab.TabPages.Count; i++)
4373             {
4374                 Rectangle rect = ListTab.GetTabRect(i);
4375                 if (rect.Left <= cpos.X & cpos.X <= rect.Right &
4376                    rect.Top <= cpos.Y & cpos.Y <= rect.Bottom)
4377                 {
4378                     _rclickTabName = ListTab.TabPages[i].Text;
4379                     break;
4380                 }
4381             }
4382         }
4383
4384         private async void ListTab_SelectedIndexChanged(object sender, EventArgs e)
4385         {
4386             //_curList.Refresh();
4387             SetMainWindowTitle();
4388             SetStatusLabelUrl();
4389             SetApiStatusLabel();
4390             if (ListTab.Focused || ((Control)ListTab.SelectedTab.Tag).Focused) this.Tag = ListTab.Tag;
4391             TabMenuControl(ListTab.SelectedTab.Text);
4392             this.PushSelectPostChain();
4393             await DispSelectedPost();
4394         }
4395
4396         private void SetListProperty()
4397         {
4398             //削除などで見つからない場合は処理せず
4399             if (_curList == null) return;
4400             if (!_isColumnChanged) return;
4401
4402             int[] dispOrder = new int[_curList.Columns.Count];
4403             for (int i = 0; i < _curList.Columns.Count; i++)
4404             {
4405                 for (int j = 0; j < _curList.Columns.Count; j++)
4406                 {
4407                     if (_curList.Columns[j].DisplayIndex == i)
4408                     {
4409                         dispOrder[i] = j;
4410                         break;
4411                     }
4412                 }
4413             }
4414
4415             //列幅、列並びを他のタブに設定
4416             foreach (TabPage tb in ListTab.TabPages)
4417             {
4418                 if (!tb.Equals(_curTab))
4419                 {
4420                     if (tb.Tag != null && tb.Controls.Count > 0)
4421                     {
4422                         DetailsListView lst = (DetailsListView)tb.Tag;
4423                         for (int i = 0; i < lst.Columns.Count; i++)
4424                         {
4425                             lst.Columns[dispOrder[i]].DisplayIndex = i;
4426                             lst.Columns[i].Width = _curList.Columns[i].Width;
4427                         }
4428                     }
4429                 }
4430             }
4431
4432             _isColumnChanged = false;
4433         }
4434
4435         private void StatusText_KeyPress(object sender, KeyPressEventArgs e)
4436         {
4437             if (e.KeyChar == '@')
4438             {
4439                 if (!SettingManager.Common.UseAtIdSupplement) return;
4440                 //@マーク
4441                 int cnt = AtIdSupl.ItemCount;
4442                 ShowSuplDialog(StatusText, AtIdSupl);
4443                 if (cnt != AtIdSupl.ItemCount) ModifySettingAtId = true;
4444                 e.Handled = true;
4445             }
4446             else if (e.KeyChar == '#')
4447             {
4448                 if (!SettingManager.Common.UseHashSupplement) return;
4449                 ShowSuplDialog(StatusText, HashSupl);
4450                 e.Handled = true;
4451             }
4452         }
4453
4454         public void ShowSuplDialog(TextBox owner, AtIdSupplement dialog)
4455             => this.ShowSuplDialog(owner, dialog, 0, "");
4456
4457         public void ShowSuplDialog(TextBox owner, AtIdSupplement dialog, int offset)
4458             => this.ShowSuplDialog(owner, dialog, offset, "");
4459
4460         public void ShowSuplDialog(TextBox owner, AtIdSupplement dialog, int offset, string startswith)
4461         {
4462             dialog.StartsWith = startswith;
4463             if (dialog.Visible)
4464             {
4465                 dialog.Focus();
4466             }
4467             else
4468             {
4469                 dialog.ShowDialog();
4470             }
4471             this.TopMost = SettingManager.Common.AlwaysTop;
4472             int selStart = owner.SelectionStart;
4473             string fHalf = "";
4474             string eHalf = "";
4475             if (dialog.DialogResult == DialogResult.OK)
4476             {
4477                 if (!string.IsNullOrEmpty(dialog.inputText))
4478                 {
4479                     if (selStart > 0)
4480                     {
4481                         fHalf = owner.Text.Substring(0, selStart - offset);
4482                     }
4483                     if (selStart < owner.Text.Length)
4484                     {
4485                         eHalf = owner.Text.Substring(selStart);
4486                     }
4487                     owner.Text = fHalf + dialog.inputText + eHalf;
4488                     owner.SelectionStart = selStart + dialog.inputText.Length;
4489                 }
4490             }
4491             else
4492             {
4493                 if (selStart > 0)
4494                 {
4495                     fHalf = owner.Text.Substring(0, selStart);
4496                 }
4497                 if (selStart < owner.Text.Length)
4498                 {
4499                     eHalf = owner.Text.Substring(selStart);
4500                 }
4501                 owner.Text = fHalf + eHalf;
4502                 if (selStart > 0)
4503                 {
4504                     owner.SelectionStart = selStart;
4505                 }
4506             }
4507             owner.Focus();
4508         }
4509
4510         private void StatusText_KeyUp(object sender, KeyEventArgs e)
4511         {
4512             //スペースキーで未読ジャンプ
4513             if (!e.Alt && !e.Control && !e.Shift)
4514             {
4515                 if (e.KeyCode == Keys.Space || e.KeyCode == Keys.ProcessKey)
4516                 {
4517                     bool isSpace = false;
4518                     foreach (char c in StatusText.Text)
4519                     {
4520                         if (c == ' ' || c == ' ')
4521                         {
4522                             isSpace = true;
4523                         }
4524                         else
4525                         {
4526                             isSpace = false;
4527                             break;
4528                         }
4529                     }
4530                     if (isSpace)
4531                     {
4532                         e.Handled = true;
4533                         StatusText.Text = "";
4534                         JumpUnreadMenuItem_Click(null, null);
4535                     }
4536                 }
4537             }
4538             this.StatusText_TextChanged(null, null);
4539         }
4540
4541         private void StatusText_TextChanged(object sender, EventArgs e)
4542         {
4543             //文字数カウント
4544             int pLen = this.GetRestStatusCount(this.FormatStatusTextExtended(this.StatusText.Text));
4545             lblLen.Text = pLen.ToString();
4546             if (pLen < 0)
4547             {
4548                 StatusText.ForeColor = Color.Red;
4549             }
4550             else
4551             {
4552                 StatusText.ForeColor = _clInputFont;
4553             }
4554
4555             this.StatusText.AccessibleDescription = string.Format(Properties.Resources.StatusText_AccessibleDescription, pLen);
4556
4557             if (string.IsNullOrEmpty(StatusText.Text))
4558             {
4559                 this.inReplyTo = null;
4560             }
4561         }
4562
4563         /// <summary>
4564         /// メンション以外の文字列が含まれていないテキストであるか判定します
4565         /// </summary>
4566         internal static bool TextContainsOnlyMentions(string text)
4567         {
4568             var mentions = TweetExtractor.ExtractMentionEntities(text).OrderBy(x => x.Indices[0]);
4569             var startIndex = 0;
4570
4571             foreach (var mention in mentions)
4572             {
4573                 var textPart = text.Substring(startIndex, mention.Indices[0] - startIndex);
4574
4575                 if (!string.IsNullOrWhiteSpace(textPart))
4576                     return false;
4577
4578                 startIndex = mention.Indices[1];
4579             }
4580
4581             var textPartLast = text.Substring(startIndex);
4582
4583             if (!string.IsNullOrWhiteSpace(textPartLast))
4584                 return false;
4585
4586             return true;
4587         }
4588
4589         /// <summary>
4590         /// 投稿時に auto_populate_reply_metadata オプションによって自動で追加されるメンションを除去します
4591         /// </summary>
4592         private string RemoveAutoPopuratedMentions(string statusText, out long[] autoPopulatedUserIds)
4593         {
4594             List<long> _autoPopulatedUserIds = new List<long>();
4595
4596             var replyToPost = this.inReplyTo != null ? this._statuses[this.inReplyTo.Value.StatusId] : null;
4597             if (replyToPost != null)
4598             {
4599                 if (statusText.StartsWith($"@{replyToPost.ScreenName} ", StringComparison.Ordinal))
4600                 {
4601                     statusText = statusText.Substring(replyToPost.ScreenName.Length + 2);
4602                     _autoPopulatedUserIds.Add(replyToPost.UserId);
4603
4604                     foreach (var (userId, screenName) in replyToPost.ReplyToList)
4605                     {
4606                         if (statusText.StartsWith($"@{screenName} ", StringComparison.Ordinal))
4607                         {
4608                             statusText = statusText.Substring(screenName.Length + 2);
4609                             _autoPopulatedUserIds.Add(userId);
4610                         }
4611                     }
4612                 }
4613             }
4614
4615             autoPopulatedUserIds = _autoPopulatedUserIds.ToArray();
4616
4617             return statusText;
4618         }
4619
4620         /// <summary>
4621         /// attachment_url に指定可能な URL が含まれていれば除去
4622         /// </summary>
4623         private string RemoveAttachmentUrl(string statusText, out string attachmentUrl)
4624         {
4625             attachmentUrl = null;
4626
4627             // attachment_url は media_id と同時に使用できない
4628             if (this.ImageSelector.Visible && this.ImageSelector.SelectedService is TwitterPhoto)
4629                 return statusText;
4630
4631             var match = Twitter.AttachmentUrlRegex.Match(statusText);
4632             if (!match.Success)
4633                 return statusText;
4634
4635             attachmentUrl = match.Value;
4636
4637             // マッチした URL を空白に置換
4638             statusText = statusText.Substring(0, match.Index);
4639
4640             // テキストと URL の間にスペースが含まれていれば除去
4641             return statusText.TrimEnd(' ');
4642         }
4643
4644         private string FormatStatusTextExtended(string statusText)
4645             => this.FormatStatusTextExtended(statusText, out var autoPopulatedUserIds, out var attachmentUrl);
4646
4647         /// <summary>
4648         /// <see cref="FormatStatusText"/> に加えて、拡張モードで140字にカウントされない文字列の除去を行います
4649         /// </summary>
4650         private string FormatStatusTextExtended(string statusText, out long[] autoPopulatedUserIds, out string attachmentUrl)
4651         {
4652             statusText = this.RemoveAutoPopuratedMentions(statusText, out autoPopulatedUserIds);
4653
4654             statusText = this.RemoveAttachmentUrl(statusText, out attachmentUrl);
4655
4656             return this.FormatStatusText(statusText);
4657         }
4658
4659         /// <summary>
4660         /// ツイート投稿前のフッター付与などの前処理を行います
4661         /// </summary>
4662         private string FormatStatusText(string statusText)
4663         {
4664             statusText = statusText.Replace("\r\n", "\n");
4665
4666             if (this.urlMultibyteSplit)
4667             {
4668                 // URLと全角文字の切り離し
4669                 statusText = Regex.Replace(statusText, @"https?:\/\/[-_.!~*'()a-zA-Z0-9;\/?:\@&=+\$,%#^]+", "$& ");
4670             }
4671
4672             if (SettingManager.Common.WideSpaceConvert)
4673             {
4674                 // 文中の全角スペースを半角スペース1個にする
4675                 statusText = statusText.Replace(" ", " ");
4676             }
4677
4678             // DM の場合はこれ以降の処理を行わない
4679             if (statusText.StartsWith("D ", StringComparison.OrdinalIgnoreCase))
4680                 return statusText;
4681
4682             bool disableFooter;
4683             if (SettingManager.Common.PostShiftEnter)
4684             {
4685                 disableFooter = MyCommon.IsKeyDown(Keys.Control);
4686             }
4687             else
4688             {
4689                 if (this.StatusText.Multiline && !SettingManager.Common.PostCtrlEnter)
4690                     disableFooter = MyCommon.IsKeyDown(Keys.Control);
4691                 else
4692                     disableFooter = MyCommon.IsKeyDown(Keys.Shift);
4693             }
4694
4695             if (statusText.Contains("RT @"))
4696                 disableFooter = true;
4697
4698             // 自分宛のリプライの場合は先頭の「@screen_name 」の部分を除去する (in_reply_to_status_id は維持される)
4699             if (this.inReplyTo != null && this.inReplyTo.Value.ScreenName == this.tw.Username)
4700             {
4701                 var mentionSelf = $"@{this.tw.Username} ";
4702                 if (statusText.StartsWith(mentionSelf, StringComparison.OrdinalIgnoreCase))
4703                 {
4704                     if (statusText.Length > mentionSelf.Length || this.GetSelectedImageService() != null)
4705                         statusText = statusText.Substring(mentionSelf.Length);
4706                 }
4707             }
4708
4709             var header = "";
4710             var footer = "";
4711
4712             var hashtag = this.HashMgr.UseHash;
4713             if (!string.IsNullOrEmpty(hashtag) && !(this.HashMgr.IsNotAddToAtReply && this.inReplyTo != null))
4714             {
4715                 if (HashMgr.IsHead)
4716                     header = HashMgr.UseHash + " ";
4717                 else
4718                     footer = " " + HashMgr.UseHash;
4719             }
4720
4721             if (!disableFooter)
4722             {
4723                 if (SettingManager.Local.UseRecommendStatus)
4724                 {
4725                     // 推奨ステータスを使用する
4726                     footer += this.recommendedStatusFooter;
4727                 }
4728                 else if (!string.IsNullOrEmpty(SettingManager.Local.StatusText))
4729                 {
4730                     // テキストボックスに入力されている文字列を使用する
4731                     footer += " " + SettingManager.Local.StatusText.Trim();
4732                 }
4733             }
4734
4735             statusText = header + statusText + footer;
4736
4737             if (this.preventSmsCommand)
4738             {
4739                 // ツイートが意図せず SMS コマンドとして解釈されることを回避 (D, DM, M のみ)
4740                 // 参照: https://support.twitter.com/articles/14020
4741
4742                 if (Regex.IsMatch(statusText, @"^[+\-\[\]\s\\.,*/(){}^~|='&%$#""<>?]*(d|dm|m)([+\-\[\]\s\\.,*/(){}^~|='&%$#""<>?]+|$)", RegexOptions.IgnoreCase)
4743                     && !Twitter.DMSendTextRegex.IsMatch(statusText))
4744                 {
4745                     // U+200B (ZERO WIDTH SPACE) を先頭に加えて回避
4746                     statusText = '\u200b' + statusText;
4747                 }
4748             }
4749
4750             return statusText;
4751         }
4752
4753         /// <summary>
4754         /// 投稿欄に表示する入力可能な文字数を計算します
4755         /// </summary>
4756         private int GetRestStatusCount(string statusText)
4757         {
4758             var remainCount = this.tw.GetTextLengthRemain(statusText);
4759
4760             var uploadService = this.GetSelectedImageService();
4761             if (uploadService != null)
4762             {
4763                 // TODO: ImageSelector で選択中の画像の枚数が mediaCount 引数に渡るようにする
4764                 remainCount -= uploadService.GetReservedTextLength(1);
4765             }
4766
4767             return remainCount;
4768         }
4769
4770         private IMediaUploadService GetSelectedImageService()
4771             => this.ImageSelector.Visible ? this.ImageSelector.SelectedService : null;
4772
4773         private void MyList_CacheVirtualItems(object sender, CacheVirtualItemsEventArgs e)
4774         {
4775             if (sender != this._curList)
4776                 return;
4777
4778             var listCache = this._listItemCache;
4779             if (listCache?.TargetList == sender && listCache.IsSupersetOf(e.StartIndex, e.EndIndex))
4780             {
4781                 // If the newly requested cache is a subset of the old cache,
4782                 // no need to rebuild everything, so do nothing.
4783                 return;
4784             }
4785
4786             // Now we need to rebuild the cache.
4787             this.CreateCache(e.StartIndex, e.EndIndex);
4788         }
4789
4790         private void MyList_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
4791         {
4792             var listCache = this._listItemCache;
4793             if (listCache?.TargetList == sender)
4794             {
4795                 if (listCache.TryGetValue(e.ItemIndex, out var item, out var cacheItemPost))
4796                 {
4797                     e.Item = item;
4798                     return;
4799                 }
4800             }
4801
4802             // A cache miss, so create a new ListViewItem and pass it back.
4803             TabPage tb = (TabPage)((DetailsListView)sender).Parent;
4804             try
4805             {
4806                 e.Item = this.CreateItem(tb, _statuses.Tabs[tb.Text][e.ItemIndex], e.ItemIndex);
4807             }
4808             catch (Exception)
4809             {
4810                 // 不正な要求に対する間に合わせの応答
4811                 string[] sitem = {"", "", "", "", "", "", "", ""};
4812                 e.Item = new ImageListViewItem(sitem);
4813             }
4814         }
4815
4816         private void CreateCache(int startIndex, int endIndex)
4817         {
4818             var tabInfo = this._statuses.Tabs[this._curTab.Text];
4819
4820             if (tabInfo.AllCount == 0)
4821                 return;
4822
4823             // インデックスを 0...(tabInfo.AllCount - 1) の範囲内にする
4824             int FilterRange(int index)
4825                 => Math.Max(Math.Min(index, tabInfo.AllCount - 1), 0);
4826
4827             // キャッシュ要求(要求範囲±30を作成)
4828             startIndex = FilterRange(startIndex - 30);
4829             endIndex = FilterRange(endIndex + 30);
4830
4831             var cacheLength = endIndex - startIndex + 1;
4832
4833             var posts = tabInfo[startIndex, endIndex]; //配列で取得
4834             var listItems = Enumerable.Range(0, cacheLength)
4835                 .Select(x => this.CreateItem(this._curTab, posts[x], startIndex + x))
4836                 .ToArray();
4837
4838             var listCache = new ListViewItemCache
4839             {
4840                 TargetList = this._curList,
4841                 StartIndex = startIndex,
4842                 EndIndex = endIndex,
4843                 Post = posts,
4844                 ListItem = listItems,
4845             };
4846
4847             Interlocked.Exchange(ref this._listItemCache, listCache);
4848         }
4849
4850         /// <summary>
4851         /// DetailsListView のための ListViewItem のキャッシュを消去する
4852         /// </summary>
4853         private void PurgeListViewItemCache()
4854             => Interlocked.Exchange(ref this._listItemCache, null);
4855
4856         private ListViewItem CreateItem(TabPage Tab, PostClass Post, int Index)
4857         {
4858             StringBuilder mk = new StringBuilder();
4859             //if (Post.IsDeleted) mk.Append("×");
4860             //if (Post.IsMark) mk.Append("♪");
4861             //if (Post.IsProtect) mk.Append("Ю");
4862             //if (Post.InReplyToStatusId != null) mk.Append("⇒");
4863             if (Post.FavoritedCount > 0) mk.Append("+" + Post.FavoritedCount);
4864             ImageListViewItem itm;
4865             if (Post.RetweetedId == null)
4866             {
4867                 string[] sitem= {"",
4868                                  Post.Nickname,
4869                                  Post.IsDeleted ? "(DELETED)" : Post.AccessibleText.Replace('\n', ' '),
4870                                  Post.CreatedAt.ToLocalTimeString(SettingManager.Common.DateTimeFormat),
4871                                  Post.ScreenName,
4872                                  "",
4873                                  mk.ToString(),
4874                                  Post.Source};
4875                 itm = new ImageListViewItem(sitem, this.IconCache, Post.ImageUrl);
4876             }
4877             else
4878             {
4879                 string[] sitem = {"",
4880                                   Post.Nickname,
4881                                   Post.IsDeleted ? "(DELETED)" : Post.AccessibleText.Replace('\n', ' '),
4882                                   Post.CreatedAt.ToLocalTimeString(SettingManager.Common.DateTimeFormat),
4883                                   Post.ScreenName + Environment.NewLine + "(RT:" + Post.RetweetedBy + ")",
4884                                   "",
4885                                   mk.ToString(),
4886                                   Post.Source};
4887                 itm = new ImageListViewItem(sitem, this.IconCache, Post.ImageUrl);
4888             }
4889             itm.StateIndex = Post.StateIndex;
4890             itm.Tag = Post;
4891
4892             bool read = Post.IsRead;
4893             //未読管理していなかったら既読として扱う
4894             if (!_statuses.Tabs[Tab.Text].UnreadManage || !SettingManager.Common.UnreadManage) read = true;
4895             ChangeItemStyleRead(read, itm, Post, null);
4896             if (Tab.Equals(_curTab)) ColorizeList(itm, Index);
4897             return itm;
4898         }
4899
4900         /// <summary>
4901         /// 全てのタブの振り分けルールを反映し直します
4902         /// </summary>
4903         private void ApplyPostFilters()
4904         {
4905             using (ControlTransaction.Cursor(this, Cursors.WaitCursor))
4906             {
4907                 this.PurgeListViewItemCache();
4908                 this._curPost = null;
4909                 this._curItemIndex = -1;
4910                 this._statuses.FilterAll();
4911
4912                 foreach (TabPage tabPage in this.ListTab.TabPages)
4913                 {
4914                     var tab = this._statuses.Tabs[tabPage.Text];
4915
4916                     var listview = (DetailsListView)tabPage.Tag;
4917                     using (ControlTransaction.Update(listview))
4918                     {
4919                         listview.VirtualListSize = tab.AllCount;
4920                     }
4921
4922                     if (SettingManager.Common.TabIconDisp)
4923                     {
4924                         if (tab.UnreadCount > 0)
4925                             tabPage.ImageIndex = 0;
4926                         else
4927                             tabPage.ImageIndex = -1;
4928                     }
4929                 }
4930
4931                 if (!SettingManager.Common.TabIconDisp)
4932                     this.ListTab.Refresh();
4933
4934                 SetMainWindowTitle();
4935                 SetStatusLabelUrl();
4936             }
4937         }
4938
4939         private void MyList_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
4940             => e.DrawDefault = true;
4941
4942         private void MyList_HScrolled(object sender, EventArgs e)
4943             => ((DetailsListView)sender).Refresh();
4944
4945         private void MyList_DrawItem(object sender, DrawListViewItemEventArgs e)
4946         {
4947             if (e.State == 0) return;
4948             e.DrawDefault = false;
4949
4950             SolidBrush brs2 = null;
4951             if (!e.Item.Selected)     //e.ItemStateでうまく判定できない???
4952             {
4953                 if (e.Item.BackColor == _clSelf)
4954                     brs2 = _brsBackColorMine;
4955                 else if (e.Item.BackColor == _clAtSelf)
4956                     brs2 = _brsBackColorAt;
4957                 else if (e.Item.BackColor == _clTarget)
4958                     brs2 = _brsBackColorYou;
4959                 else if (e.Item.BackColor == _clAtTarget)
4960                     brs2 = _brsBackColorAtYou;
4961                 else if (e.Item.BackColor == _clAtFromTarget)
4962                     brs2 = _brsBackColorAtFromTarget;
4963                 else if (e.Item.BackColor == _clAtTo)
4964                     brs2 = _brsBackColorAtTo;
4965                 else
4966                     brs2 = _brsBackColorNone;
4967             }
4968             else
4969             {
4970                 //選択中の行
4971                 if (((Control)sender).Focused)
4972                     brs2 = _brsHighLight;
4973                 else
4974                     brs2 = _brsDeactiveSelection;
4975             }
4976             e.Graphics.FillRectangle(brs2, e.Bounds);
4977             e.DrawFocusRectangle();
4978             this.DrawListViewItemIcon(e);
4979         }
4980
4981         private void MyList_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
4982         {
4983             if (e.ItemState == 0) return;
4984
4985             if (e.ColumnIndex > 0)
4986             {
4987                 //アイコン以外の列
4988                 var post = (PostClass)e.Item.Tag;
4989
4990                 RectangleF rct = e.Bounds;
4991                 rct.Width = e.Header.Width;
4992                 int fontHeight = e.Item.Font.Height;
4993                 if (_iconCol)
4994                 {
4995                     rct.Y += fontHeight;
4996                     rct.Height -= fontHeight;
4997                 }
4998
4999                 int drawLineCount = Math.Max(1, Math.DivRem((int)rct.Height, fontHeight, out var heightDiff));
5000
5001                 //if (heightDiff > fontHeight * 0.7)
5002                 //{
5003                 //    rct.Height += fontHeight;
5004                 //    drawLineCount += 1;
5005                 //}
5006
5007                 //フォントの高さの半分を足してるのは保険。無くてもいいかも。
5008                 if (!_iconCol && drawLineCount <= 1)
5009                 {
5010                     //rct.Inflate(0, heightDiff / -2);
5011                     //rct.Height += fontHeight / 2;
5012                 }
5013                 else if (heightDiff < fontHeight * 0.7)
5014                 {
5015                     //最終行が70%以上欠けていたら、最終行は表示しない
5016                     //rct.Height = (float)((fontHeight * drawLineCount) + (fontHeight / 2));
5017                     rct.Height = (fontHeight * drawLineCount) - 1;
5018                 }
5019                 else
5020                 {
5021                     drawLineCount += 1;
5022                 }
5023
5024                 //if (!_iconCol && drawLineCount > 1)
5025                 //{
5026                 //    rct.Y += fontHeight * 0.2;
5027                 //    if (heightDiff >= fontHeight * 0.8) rct.Height -= fontHeight * 0.2;
5028                 //}
5029
5030                 if (rct.Width > 0)
5031                 {
5032                     Color color = (!e.Item.Selected) ? e.Item.ForeColor :   //選択されていない行
5033                         (((Control)sender).Focused) ? _clHighLight :        //選択中の行
5034                         _clUnread;
5035
5036                     if (_iconCol)
5037                     {
5038                         Rectangle rctB = e.Bounds;
5039                         rctB.Width = e.Header.Width;
5040                         rctB.Height = fontHeight;
5041
5042                         using (Font fnt = new Font(e.Item.Font, FontStyle.Bold))
5043                         {
5044                             TextRenderer.DrawText(e.Graphics,
5045                                                     post.IsDeleted ? "(DELETED)" : post.TextSingleLine,
5046                                                     e.Item.Font,
5047                                                     Rectangle.Round(rct),
5048                                                     color,
5049                                                     TextFormatFlags.WordBreak |
5050                                                     TextFormatFlags.EndEllipsis |
5051                                                     TextFormatFlags.GlyphOverhangPadding |
5052                                                     TextFormatFlags.NoPrefix);
5053                             TextRenderer.DrawText(e.Graphics,
5054                                                     e.Item.SubItems[4].Text + " / " + e.Item.SubItems[1].Text + " (" + e.Item.SubItems[3].Text + ") " + e.Item.SubItems[5].Text + e.Item.SubItems[6].Text + " [" + e.Item.SubItems[7].Text + "]",
5055                                                     fnt,
5056                                                     rctB,
5057                                                     color,
5058                                                     TextFormatFlags.SingleLine |
5059                                                     TextFormatFlags.EndEllipsis |
5060                                                     TextFormatFlags.GlyphOverhangPadding |
5061                                                     TextFormatFlags.NoPrefix);
5062                         }
5063                     }
5064                     else
5065                     {
5066                         string text;
5067                         if (e.ColumnIndex != 2)
5068                             text = e.SubItem.Text;
5069                         else
5070                             text = post.IsDeleted ? "(DELETED)" : post.TextSingleLine;
5071
5072                         if (drawLineCount == 1)
5073                         {
5074                             TextRenderer.DrawText(e.Graphics,
5075                                                     text,
5076                                                     e.Item.Font,
5077                                                     Rectangle.Round(rct),
5078                                                     color,
5079                                                     TextFormatFlags.SingleLine |
5080                                                     TextFormatFlags.EndEllipsis |
5081                                                     TextFormatFlags.GlyphOverhangPadding |
5082                                                     TextFormatFlags.NoPrefix |
5083                                                     TextFormatFlags.VerticalCenter);
5084                         }
5085                         else
5086                         {
5087                             TextRenderer.DrawText(e.Graphics,
5088                                                     text,
5089                                                     e.Item.Font,
5090                                                     Rectangle.Round(rct),
5091                                                     color,
5092                                                     TextFormatFlags.WordBreak |
5093                                                     TextFormatFlags.EndEllipsis |
5094                                                     TextFormatFlags.GlyphOverhangPadding |
5095                                                     TextFormatFlags.NoPrefix);
5096                         }
5097                     }
5098                     //if (e.ColumnIndex == 6) this.DrawListViewItemStateIcon(e, rct);
5099                 }
5100             }
5101         }
5102
5103         private void DrawListViewItemIcon(DrawListViewItemEventArgs e)
5104         {
5105             if (_iconSz == 0) return;
5106
5107             ImageListViewItem item = (ImageListViewItem)e.Item;
5108
5109             //e.Bounds.Leftが常に0を指すから自前で計算
5110             Rectangle itemRect = item.Bounds;
5111             var col0 = e.Item.ListView.Columns[0];
5112             itemRect.Width = col0.Width;
5113
5114             if (col0.DisplayIndex > 0)
5115             {
5116                 foreach (ColumnHeader clm in e.Item.ListView.Columns)
5117                 {
5118                     if (clm.DisplayIndex < col0.DisplayIndex)
5119                         itemRect.X += clm.Width;
5120                 }
5121             }
5122
5123             // ディスプレイの DPI 設定を考慮したアイコンサイズ
5124             var realIconSize = new SizeF(this._iconSz * this.CurrentScaleFactor.Width, this._iconSz * this.CurrentScaleFactor.Height).ToSize();
5125             var realStateSize = new SizeF(16 * this.CurrentScaleFactor.Width, 16 * this.CurrentScaleFactor.Height).ToSize();
5126
5127             Rectangle iconRect;
5128             var img = item.Image;
5129             if (img != null)
5130             {
5131                 iconRect = Rectangle.Intersect(new Rectangle(e.Item.GetBounds(ItemBoundsPortion.Icon).Location, realIconSize), itemRect);
5132                 iconRect.Offset(0, Math.Max(0, (itemRect.Height - realIconSize.Height) / 2));
5133
5134                 if (iconRect.Width > 0)
5135                 {
5136                     e.Graphics.FillRectangle(Brushes.White, iconRect);
5137                     e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
5138                     try
5139                     {
5140                         e.Graphics.DrawImage(img.Image, iconRect);
5141                     }
5142                     catch (ArgumentException)
5143                     {
5144                         item.RefreshImageAsync();
5145                     }
5146                 }
5147             }
5148             else
5149             {
5150                 iconRect = Rectangle.Intersect(new Rectangle(e.Item.GetBounds(ItemBoundsPortion.Icon).Location, new Size(1, 1)), itemRect);
5151                 //iconRect.Offset(0, Math.Max(0, (itemRect.Height - realIconSize.Height) / 2));
5152
5153                 item.GetImageAsync();
5154             }
5155
5156             if (item.StateIndex > -1)
5157             {
5158                 Rectangle stateRect = Rectangle.Intersect(new Rectangle(new Point(iconRect.X + realIconSize.Width + 2, iconRect.Y), realStateSize), itemRect);
5159                 if (stateRect.Width > 0)
5160                 {
5161                     //e.Graphics.FillRectangle(Brushes.White, stateRect);
5162                     //e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.High;
5163                     e.Graphics.DrawImage(this.PostStateImageList.Images[item.StateIndex], stateRect);
5164                 }
5165             }
5166         }
5167
5168         protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
5169         {
5170             base.ScaleControl(factor, specified);
5171
5172             ScaleChildControl(this.TabImage, factor);
5173
5174             var tabpages = this.ListTab.TabPages.Cast<TabPage>();
5175             var listviews = tabpages.Select(x => x.Tag).Cast<ListView>();
5176
5177             foreach (var listview in listviews)
5178             {
5179                 ScaleChildControl(listview, factor);
5180             }
5181         }
5182
5183         //private void DrawListViewItemStateIcon(DrawListViewSubItemEventArgs e, RectangleF rct)
5184         //{
5185         //    ImageListViewItem item = (ImageListViewItem)e.Item;
5186         //    if (item.StateImageIndex > -1)
5187         //    {
5188         //        ////e.Bounds.Leftが常に0を指すから自前で計算
5189         //        //Rectangle itemRect = item.Bounds;
5190         //        //itemRect.Width = e.Item.ListView.Columns[4].Width;
5191
5192         //        //foreach (ColumnHeader clm in e.Item.ListView.Columns)
5193         //        //{
5194         //        //    if (clm.DisplayIndex < e.Item.ListView.Columns[4].DisplayIndex)
5195         //        //    {
5196         //        //        itemRect.X += clm.Width;
5197         //        //    }
5198         //        //}
5199
5200         //        //Rectangle iconRect = Rectangle.Intersect(new Rectangle(e.Item.GetBounds(ItemBoundsPortion.Icon).Location, new Size(_iconSz, _iconSz)), itemRect);
5201         //        //iconRect.Offset(0, Math.Max(0, (itemRect.Height - _iconSz) / 2));
5202
5203         //        if (rct.Width > 0)
5204         //        {
5205         //            RectangleF stateRect = RectangleF.Intersect(rct, new RectangleF(rct.Location, new Size(18, 16)));
5206         //            //e.Graphics.FillRectangle(Brushes.White, rct);
5207         //            //e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.High;
5208         //            e.Graphics.DrawImage(this.PostStateImageList.Images(item.StateImageIndex), stateRect);
5209         //        }
5210         //    }
5211         //}
5212
5213         internal void DoTabSearch(string searchWord, bool caseSensitive, bool useRegex, SEARCHTYPE searchType)
5214         {
5215             var tab = this._statuses.Tabs[this._curTab.Text];
5216
5217             if (tab.AllCount == 0)
5218             {
5219                 MessageBox.Show(Properties.Resources.DoTabSearchText2, Properties.Resources.DoTabSearchText3, MessageBoxButtons.OK, MessageBoxIcon.Information);
5220                 return;
5221             }
5222
5223             var selectedIndex = this._curList.SelectedIndices.Count != 0 ? this._curList.SelectedIndices[0] : -1;
5224
5225             int startIndex;
5226             switch (searchType)
5227             {
5228                 case SEARCHTYPE.NextSearch: // 次を検索
5229                     if (selectedIndex != -1)
5230                         startIndex = Math.Min(selectedIndex + 1, tab.AllCount - 1);
5231                     else
5232                         startIndex = 0;
5233                     break;
5234                 case SEARCHTYPE.PrevSearch: // 前を検索
5235                     if (selectedIndex != -1)
5236                         startIndex = Math.Max(selectedIndex - 1, 0);
5237                     else
5238                         startIndex = tab.AllCount - 1;
5239                     break;
5240                 case SEARCHTYPE.DialogSearch: // ダイアログからの検索
5241                 default:
5242                     if (selectedIndex != -1)
5243                         startIndex = selectedIndex;
5244                     else
5245                         startIndex = 0;
5246                     break;
5247             }
5248
5249             Func<string, bool> stringComparer;
5250             try
5251             {
5252                 stringComparer = this.CreateSearchComparer(searchWord, useRegex, caseSensitive);
5253             }
5254             catch (ArgumentException)
5255             {
5256                 MessageBox.Show(Properties.Resources.DoTabSearchText1, Properties.Resources.DoTabSearchText3, MessageBoxButtons.OK, MessageBoxIcon.Error);
5257                 return;
5258             }
5259
5260             var reverse = searchType == SEARCHTYPE.PrevSearch;
5261             var foundIndex = tab.SearchPostsAll(stringComparer, startIndex, reverse)
5262                 .DefaultIfEmpty(-1).First();
5263
5264             if (foundIndex == -1)
5265             {
5266                 MessageBox.Show(Properties.Resources.DoTabSearchText2, Properties.Resources.DoTabSearchText3, MessageBoxButtons.OK, MessageBoxIcon.Information);
5267                 return;
5268             }
5269
5270             this.SelectListItem(this._curList, foundIndex);
5271             this._curList.EnsureVisible(foundIndex);
5272         }
5273
5274         private void MenuItemSubSearch_Click(object sender, EventArgs e)
5275             => this.ShowSearchDialog(); // 検索メニュー
5276
5277         private void MenuItemSearchNext_Click(object sender, EventArgs e)
5278         {
5279             var previousSearch = this.SearchDialog.ResultOptions;
5280             if (previousSearch == null || previousSearch.Type != SearchWordDialog.SearchType.Timeline)
5281             {
5282                 this.SearchDialog.Reset();
5283                 this.ShowSearchDialog();
5284                 return;
5285             }
5286
5287             // 次を検索
5288             this.DoTabSearch(
5289                 previousSearch.Query,
5290                 previousSearch.CaseSensitive,
5291                 previousSearch.UseRegex,
5292                 SEARCHTYPE.NextSearch);
5293         }
5294
5295         private void MenuItemSearchPrev_Click(object sender, EventArgs e)
5296         {
5297             var previousSearch = this.SearchDialog.ResultOptions;
5298             if (previousSearch == null || previousSearch.Type != SearchWordDialog.SearchType.Timeline)
5299             {
5300                 this.SearchDialog.Reset();
5301                 this.ShowSearchDialog();
5302                 return;
5303             }
5304
5305             // 前を検索
5306             this.DoTabSearch(
5307                 previousSearch.Query,
5308                 previousSearch.CaseSensitive,
5309                 previousSearch.UseRegex,
5310                 SEARCHTYPE.PrevSearch);
5311         }
5312
5313         /// <summary>
5314         /// 検索ダイアログを表示し、検索を実行します
5315         /// </summary>
5316         private void ShowSearchDialog()
5317         {
5318             if (this.SearchDialog.ShowDialog(this) != DialogResult.OK)
5319             {
5320                 this.TopMost = SettingManager.Common.AlwaysTop;
5321                 return;
5322             }
5323             this.TopMost = SettingManager.Common.AlwaysTop;
5324
5325             var searchOptions = this.SearchDialog.ResultOptions;
5326             if (searchOptions.Type == SearchWordDialog.SearchType.Timeline)
5327             {
5328                 if (searchOptions.NewTab)
5329                 {
5330                     var tabName = Properties.Resources.SearchResults_TabName;
5331
5332                     try
5333                     {
5334                         tabName = this._statuses.MakeTabName(tabName);
5335                     }
5336                     catch (TabException ex)
5337                     {
5338                         MessageBox.Show(this, ex.Message, ApplicationSettings.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
5339                     }
5340
5341                     var resultTab = new LocalSearchTabModel(tabName);
5342                     this.AddNewTab(resultTab, startup: false);
5343                     this._statuses.AddTab(resultTab);
5344
5345                     var targetTab = this._statuses.Tabs[this._curTab.Text];
5346
5347                     Func<string, bool> stringComparer;
5348                     try
5349                     {
5350                         stringComparer = this.CreateSearchComparer(searchOptions.Query, searchOptions.UseRegex, searchOptions.CaseSensitive);
5351                     }
5352                     catch (ArgumentException)
5353                     {
5354                         MessageBox.Show(Properties.Resources.DoTabSearchText1, Properties.Resources.DoTabSearchText3, MessageBoxButtons.OK, MessageBoxIcon.Error);
5355                         return;
5356                     }
5357
5358                     var foundIndices = targetTab.SearchPostsAll(stringComparer).ToArray();
5359                     if (foundIndices.Length == 0)
5360                     {
5361                         MessageBox.Show(Properties.Resources.DoTabSearchText2, Properties.Resources.DoTabSearchText3, MessageBoxButtons.OK, MessageBoxIcon.Information);
5362                         return;
5363                     }
5364
5365                     var foundPosts = foundIndices.Select(x => targetTab[x]);
5366                     foreach (var post in foundPosts)
5367                     {
5368                         resultTab.AddPostQueue(post);
5369                     }
5370
5371                     this._statuses.DistributePosts();
5372                     this.RefreshTimeline();
5373
5374                     var tabPage = this.ListTab.TabPages.Cast<TabPage>()
5375                         .First(x => x.Text == tabName);
5376
5377                     this.ListTab.SelectedTab = tabPage;
5378                 }
5379                 else
5380                 {
5381                     this.DoTabSearch(
5382                         searchOptions.Query,
5383                         searchOptions.CaseSensitive,
5384                         searchOptions.UseRegex,
5385                         SEARCHTYPE.DialogSearch);
5386                 }
5387             }
5388             else if (searchOptions.Type == SearchWordDialog.SearchType.Public)
5389             {
5390                 this.AddNewTabForSearch(searchOptions.Query);
5391             }
5392         }
5393
5394         /// <summary>発言検索に使用するメソッドを生成します</summary>
5395         /// <exception cref="ArgumentException">
5396         /// <paramref name="useRegex"/> が true かつ、<paramref name="query"/> が不正な正規表現な場合
5397         /// </exception>
5398         private Func<string, bool> CreateSearchComparer(string query, bool useRegex, bool caseSensitive)
5399         {
5400             if (useRegex)
5401             {
5402                 var regexOption = caseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase;
5403                 var regex = new Regex(query, regexOption);
5404
5405                 return x => regex.IsMatch(x);
5406             }
5407             else
5408             {
5409                 var comparisonType = caseSensitive ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;
5410
5411                 return x => x.IndexOf(query, comparisonType) != -1;
5412             }
5413         }
5414
5415         private void AboutMenuItem_Click(object sender, EventArgs e)
5416         {
5417             using (TweenAboutBox about = new TweenAboutBox())
5418             {
5419                 about.ShowDialog(this);
5420             }
5421             this.TopMost = SettingManager.Common.AlwaysTop;
5422         }
5423
5424         private void JumpUnreadMenuItem_Click(object sender, EventArgs e)
5425         {
5426             int bgnIdx = ListTab.TabPages.IndexOf(_curTab);
5427
5428             if (ImageSelector.Enabled)
5429                 return;
5430
5431             TabModel foundTab = null;
5432             int foundIndex = 0;
5433
5434             DetailsListView lst = null;
5435
5436             //現在タブから最終タブまで探索
5437             for (int i = bgnIdx; i < ListTab.TabPages.Count; i++)
5438             {
5439                 var tabPage = this.ListTab.TabPages[i];
5440                 var tab = this._statuses.Tabs[tabPage.Text];
5441                 var unreadIndex = tab.NextUnreadIndex;
5442
5443                 if (unreadIndex != -1)
5444                 {
5445                     ListTab.SelectedIndex = i;
5446                     foundTab = tab;
5447                     foundIndex = unreadIndex;
5448                     lst = (DetailsListView)tabPage.Tag;
5449                     break;
5450                 }
5451             }
5452
5453             //未読みつからず&現在タブが先頭ではなかったら、先頭タブから現在タブの手前まで探索
5454             if (foundTab == null && bgnIdx > 0)
5455             {
5456                 for (int i = 0; i < bgnIdx; i++)
5457                 {
5458                     var tabPage = this.ListTab.TabPages[i];
5459                     var tab = this._statuses.Tabs[tabPage.Text];
5460                     var unreadIndex = tab.NextUnreadIndex;
5461
5462                     if (unreadIndex != -1)
5463                     {
5464                         ListTab.SelectedIndex = i;
5465                         foundTab = tab;
5466                         foundIndex = unreadIndex;
5467                         lst = (DetailsListView)tabPage.Tag;
5468                         break;
5469                     }
5470                 }
5471             }
5472
5473             if (foundTab == null)
5474             {
5475                 //全部調べたが未読見つからず→先頭タブの最新発言へ
5476                 ListTab.SelectedIndex = 0;
5477                 var tabPage = this.ListTab.TabPages[0];
5478                 var tab = this._statuses.Tabs[tabPage.Text];
5479
5480                 if (tab.AllCount == 0)
5481                     return;
5482
5483                 if (_statuses.SortOrder == SortOrder.Ascending)
5484                     foundIndex = tab.AllCount - 1;
5485                 else
5486                     foundIndex = 0;
5487
5488                 lst = (DetailsListView)tabPage.Tag;
5489             }
5490
5491             SelectListItem(lst, foundIndex);
5492
5493             if (_statuses.SortMode == ComparerMode.Id)
5494             {
5495                 if (_statuses.SortOrder == SortOrder.Ascending && lst.Items[foundIndex].Position.Y > lst.ClientSize.Height - _iconSz - 10 ||
5496                     _statuses.SortOrder == SortOrder.Descending && lst.Items[foundIndex].Position.Y < _iconSz + 10)
5497                 {
5498                     MoveTop();
5499                 }
5500                 else
5501                 {
5502                     lst.EnsureVisible(foundIndex);
5503                 }
5504             }
5505             else
5506             {
5507                 lst.EnsureVisible(foundIndex);
5508             }
5509
5510             lst.Focus();
5511         }
5512
5513         private async void StatusOpenMenuItem_Click(object sender, EventArgs e)
5514         {
5515             if (_curList.SelectedIndices.Count > 0 && _statuses.Tabs[_curTab.Text].TabType != MyCommon.TabUsageType.DirectMessage)
5516             {
5517                 var post = _statuses.Tabs[_curTab.Text][_curList.SelectedIndices[0]];
5518                 await this.OpenUriInBrowserAsync(MyCommon.GetStatusUrl(post));
5519             }
5520         }
5521
5522         private async void VerUpMenuItem_Click(object sender, EventArgs e)
5523             => await this.CheckNewVersion(false);
5524
5525         private void RunTweenUp()
5526         {
5527             var pinfo = new ProcessStartInfo
5528             {
5529                 UseShellExecute = true,
5530                 WorkingDirectory = MyCommon.settingPath,
5531                 FileName = Path.Combine(MyCommon.settingPath, "TweenUp3.exe"),
5532                 Arguments = "\"" + Application.StartupPath + "\"",
5533             };
5534
5535             try
5536             {
5537                 Process.Start(pinfo);
5538             }
5539             catch (Exception)
5540             {
5541                 MessageBox.Show("Failed to execute TweenUp3.exe.");
5542             }
5543         }
5544
5545         public class VersionInfo
5546         {
5547             public Version Version { get; set; }
5548             public Uri DownloadUri { get; set; }
5549             public string ReleaseNote { get; set; }
5550         }
5551
5552         /// <summary>
5553         /// OpenTween の最新バージョンの情報を取得します
5554         /// </summary>
5555         public async Task<VersionInfo> GetVersionInfoAsync()
5556         {
5557             var versionInfoUrl = new Uri(ApplicationSettings.VersionInfoUrl + "?" +
5558                 DateTimeUtc.Now.ToString("yyMMddHHmmss") + Environment.TickCount);
5559
5560             var responseText = await Networking.Http.GetStringAsync(versionInfoUrl)
5561                 .ConfigureAwait(false);
5562
5563             // 改行2つで前後パートを分割(前半がバージョン番号など、後半が詳細テキスト)
5564             var msgPart = responseText.Split(new[] { "\n\n", "\r\n\r\n" }, 2, StringSplitOptions.None);
5565
5566             var msgHeader = msgPart[0].Split(new[] { "\n", "\r\n" }, StringSplitOptions.None);
5567             var msgBody = msgPart.Length == 2 ? msgPart[1] : "";
5568
5569             msgBody = Regex.Replace(msgBody, "(?<!\r)\n", "\r\n"); // LF -> CRLF
5570
5571             return new VersionInfo
5572             {
5573                 Version = Version.Parse(msgHeader[0]),
5574                 DownloadUri = new Uri(msgHeader[1]),
5575                 ReleaseNote = msgBody,
5576             };
5577         }
5578
5579         private async Task CheckNewVersion(bool startup = false)
5580         {
5581             if (ApplicationSettings.VersionInfoUrl == null)
5582                 return; // 更新チェック無効化
5583
5584             try
5585             {
5586                 var versionInfo = await this.GetVersionInfoAsync();
5587
5588                 if (versionInfo.Version <= Version.Parse(MyCommon.FileVersion))
5589                 {
5590                     // 更新不要
5591                     if (!startup)
5592                     {
5593                         var msgtext = string.Format(Properties.Resources.CheckNewVersionText7,
5594                             MyCommon.GetReadableVersion(), MyCommon.GetReadableVersion(versionInfo.Version));
5595                         msgtext = MyCommon.ReplaceAppName(msgtext);
5596
5597                         MessageBox.Show(msgtext,
5598                             MyCommon.ReplaceAppName(Properties.Resources.CheckNewVersionText2),
5599                             MessageBoxButtons.OK, MessageBoxIcon.Information);
5600                     }
5601                     return;
5602                 }
5603
5604                 if (startup && versionInfo.Version <= SettingManager.Common.SkipUpdateVersion)
5605                     return;
5606
5607                 using (var dialog = new UpdateDialog())
5608                 {
5609                     dialog.SummaryText = string.Format(Properties.Resources.CheckNewVersionText3,
5610                         MyCommon.GetReadableVersion(versionInfo.Version));
5611                     dialog.DetailsText = versionInfo.ReleaseNote;
5612
5613                     if (dialog.ShowDialog(this) == DialogResult.Yes)
5614                     {
5615                         await this.OpenUriInBrowserAsync(versionInfo.DownloadUri.OriginalString);
5616                     }
5617                     else if (dialog.SkipButtonPressed)
5618                     {
5619                         SettingManager.Common.SkipUpdateVersion = versionInfo.Version;
5620                         this.ModifySettingCommon = true;
5621                     }
5622                 }
5623             }
5624             catch (Exception)
5625             {
5626                 this.StatusLabel.Text = Properties.Resources.CheckNewVersionText9;
5627                 if (!startup)
5628                 {
5629                     MessageBox.Show(Properties.Resources.CheckNewVersionText10,
5630                         MyCommon.ReplaceAppName(Properties.Resources.CheckNewVersionText2),
5631                         MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2);
5632                 }
5633             }
5634         }
5635
5636         private async Task Colorize()
5637         {
5638             _colorize = false;
5639             await this.DispSelectedPost();
5640             //件数関連の場合、タイトル即時書き換え
5641             if (SettingManager.Common.DispLatestPost != MyCommon.DispTitleEnum.None &&
5642                SettingManager.Common.DispLatestPost != MyCommon.DispTitleEnum.Post &&
5643                SettingManager.Common.DispLatestPost != MyCommon.DispTitleEnum.Ver &&
5644                SettingManager.Common.DispLatestPost != MyCommon.DispTitleEnum.OwnStatus)
5645             {
5646                 SetMainWindowTitle();
5647             }
5648             if (!StatusLabelUrl.Text.StartsWith("http", StringComparison.OrdinalIgnoreCase))
5649                 SetStatusLabelUrl();
5650             foreach (TabPage tb in ListTab.TabPages)
5651             {
5652                 if (_statuses.Tabs[tb.Text].UnreadCount == 0)
5653                 {
5654                     if (SettingManager.Common.TabIconDisp)
5655                     {
5656                         if (tb.ImageIndex == 0) tb.ImageIndex = -1;
5657                     }
5658                 }
5659             }
5660             if (!SettingManager.Common.TabIconDisp) ListTab.Refresh();
5661         }
5662
5663         public string createDetailHtml(string orgdata)
5664             => detailHtmlFormatHeader + orgdata + detailHtmlFormatFooter;
5665
5666         private Task DispSelectedPost()
5667             => this.DispSelectedPost(false);
5668
5669         private PostClass displayPost = new PostClass();
5670
5671         /// <summary>
5672         /// サムネイル表示に使用する CancellationToken の生成元
5673         /// </summary>
5674         private CancellationTokenSource thumbnailTokenSource = null;
5675
5676         private async Task DispSelectedPost(bool forceupdate)
5677         {
5678             if (_curList.SelectedIndices.Count == 0 || _curPost == null)
5679                 return;
5680
5681             var oldDisplayPost = this.displayPost;
5682             this.displayPost = this._curPost;
5683
5684             if (!forceupdate && this._curPost.Equals(oldDisplayPost))
5685                 return;
5686
5687             var loadTasks = new List<Task>
5688             {
5689                 this.tweetDetailsView.ShowPostDetails(this._curPost),
5690             };
5691
5692             this.SplitContainer3.Panel2Collapsed = true;
5693
5694             if (SettingManager.Common.PreviewEnable)
5695             {
5696                 var oldTokenSource = Interlocked.Exchange(ref this.thumbnailTokenSource, new CancellationTokenSource());
5697                 oldTokenSource?.Cancel();
5698
5699                 var token = this.thumbnailTokenSource.Token;
5700                 loadTasks.Add(this.tweetThumbnail1.ShowThumbnailAsync(_curPost, token));
5701             }
5702
5703             try
5704             {
5705                 await Task.WhenAll(loadTasks);
5706             }
5707             catch (OperationCanceledException) { }
5708         }
5709
5710         private async void MatomeMenuItem_Click(object sender, EventArgs e)
5711             => await this.OpenApplicationWebsite();
5712
5713         private async Task OpenApplicationWebsite()
5714             => await this.OpenUriInBrowserAsync(ApplicationSettings.WebsiteUrl);
5715
5716         private async void ShortcutKeyListMenuItem_Click(object sender, EventArgs e)
5717             => await this.OpenUriInBrowserAsync(ApplicationSettings.ShortcutKeyUrl);
5718
5719         private async void ListTab_KeyDown(object sender, KeyEventArgs e)
5720         {
5721             if (ListTab.SelectedTab != null)
5722             {
5723                 if (_statuses.Tabs[ListTab.SelectedTab.Text].TabType == MyCommon.TabUsageType.PublicSearch)
5724                 {
5725                     Control pnl = ListTab.SelectedTab.Controls["panelSearch"];
5726                     if (pnl.Controls["comboSearch"].Focused ||
5727                         pnl.Controls["comboLang"].Focused ||
5728                         pnl.Controls["buttonSearch"].Focused) return;
5729                 }
5730
5731                 if (e.Control || e.Shift || e.Alt)
5732                     this._anchorFlag = false;
5733
5734                 if (CommonKeyDown(e.KeyData, FocusedControl.ListTab, out var asyncTask))
5735                 {
5736                     e.Handled = true;
5737                     e.SuppressKeyPress = true;
5738                 }
5739
5740                 if (asyncTask != null)
5741                     await asyncTask;
5742             }
5743         }
5744
5745         private ShortcutCommand[] shortcutCommands = Array.Empty<ShortcutCommand>();
5746
5747         private void InitializeShortcuts()
5748         {
5749             this.shortcutCommands = new[]
5750             {
5751                 // リストのカーソル移動関係(上下キー、PageUp/Downに該当)
5752                 ShortcutCommand.Create(Keys.J, Keys.Control | Keys.J, Keys.Shift | Keys.J, Keys.Control | Keys.Shift | Keys.J)
5753                     .FocusedOn(FocusedControl.ListTab)
5754                     .Do(() => SendKeys.Send("{DOWN}")),
5755
5756                 ShortcutCommand.Create(Keys.K, Keys.Control | Keys.K, Keys.Shift | Keys.K, Keys.Control | Keys.Shift | Keys.K)
5757                     .FocusedOn(FocusedControl.ListTab)
5758                     .Do(() => SendKeys.Send("{UP}")),
5759
5760                 ShortcutCommand.Create(Keys.F, Keys.Shift | Keys.F)
5761                     .FocusedOn(FocusedControl.ListTab)
5762                     .Do(() => SendKeys.Send("{PGDN}")),
5763
5764                 ShortcutCommand.Create(Keys.B, Keys.Shift | Keys.B)
5765                     .FocusedOn(FocusedControl.ListTab)
5766                     .Do(() => SendKeys.Send("{PGUP}")),
5767
5768                 ShortcutCommand.Create(Keys.F1)
5769                     .Do(() => this.OpenApplicationWebsite()),
5770
5771                 ShortcutCommand.Create(Keys.F3)
5772                     .Do(() => this.MenuItemSearchNext_Click(null, null)),
5773
5774                 ShortcutCommand.Create(Keys.F5)
5775                     .Do(() => this.DoRefresh()),
5776
5777                 ShortcutCommand.Create(Keys.F6)
5778                     .Do(() => this.RefreshTabAsync<MentionsTabModel>()),
5779
5780                 ShortcutCommand.Create(Keys.F7)
5781                     .Do(() => this.RefreshTabAsync<DirectMessagesTabModel>()),
5782
5783                 ShortcutCommand.Create(Keys.Space, Keys.ProcessKey)
5784                     .NotFocusedOn(FocusedControl.StatusText)
5785                     .Do(() => { this._anchorFlag = false; this.JumpUnreadMenuItem_Click(null, null); }),
5786
5787                 ShortcutCommand.Create(Keys.G)
5788                     .NotFocusedOn(FocusedControl.StatusText)
5789                     .Do(() => { this._anchorFlag = false; this.ShowRelatedStatusesMenuItem_Click(null, null); }),
5790
5791                 ShortcutCommand.Create(Keys.Right, Keys.N)
5792                     .FocusedOn(FocusedControl.ListTab)
5793                     .Do(() => this.GoRelPost(forward: true)),
5794
5795                 ShortcutCommand.Create(Keys.Left, Keys.P)
5796                     .FocusedOn(FocusedControl.ListTab)
5797                     .Do(() => this.GoRelPost(forward: false)),
5798
5799                 ShortcutCommand.Create(Keys.OemPeriod)
5800                     .FocusedOn(FocusedControl.ListTab)
5801                     .Do(() => this.GoAnchor()),
5802
5803                 ShortcutCommand.Create(Keys.I)
5804                     .FocusedOn(FocusedControl.ListTab)
5805                     .OnlyWhen(() => this.StatusText.Enabled)
5806                     .Do(() => this.StatusText.Focus()),
5807
5808                 ShortcutCommand.Create(Keys.Enter)
5809                     .FocusedOn(FocusedControl.ListTab)
5810                     .Do(() => this.MakeReplyOrDirectStatus()),
5811
5812                 ShortcutCommand.Create(Keys.R)
5813                     .FocusedOn(FocusedControl.ListTab)
5814                     .Do(() => this.DoRefresh()),
5815
5816                 ShortcutCommand.Create(Keys.L)
5817                     .FocusedOn(FocusedControl.ListTab)
5818                     .Do(() => { this._anchorFlag = false; this.GoPost(forward: true); }),
5819
5820                 ShortcutCommand.Create(Keys.H)
5821                     .FocusedOn(FocusedControl.ListTab)
5822                     .Do(() => { this._anchorFlag = false; this.GoPost(forward: false); }),
5823
5824                 ShortcutCommand.Create(Keys.Z, Keys.Oemcomma)
5825                     .FocusedOn(FocusedControl.ListTab)
5826                     .Do(() => { this._anchorFlag = false; this.MoveTop(); }),
5827
5828                 ShortcutCommand.Create(Keys.S)
5829                     .FocusedOn(FocusedControl.ListTab)
5830                     .Do(() => { this._anchorFlag = false; this.GoNextTab(forward: true); }),
5831
5832                 ShortcutCommand.Create(Keys.A)
5833                     .FocusedOn(FocusedControl.ListTab)
5834                     .Do(() => { this._anchorFlag = false; this.GoNextTab(forward: false); }),
5835
5836                 // ] in_reply_to参照元へ戻る
5837                 ShortcutCommand.Create(Keys.Oem4)
5838                     .FocusedOn(FocusedControl.ListTab)
5839                     .Do(() => { this._anchorFlag = false; return this.GoInReplyToPostTree(); }),
5840
5841                 // [ in_reply_toへジャンプ
5842                 ShortcutCommand.Create(Keys.Oem6)
5843                     .FocusedOn(FocusedControl.ListTab)
5844                     .Do(() => { this._anchorFlag = false; this.GoBackInReplyToPostTree(); }),
5845
5846                 ShortcutCommand.Create(Keys.Escape)
5847                     .FocusedOn(FocusedControl.ListTab)
5848                     .Do(() => {
5849                         this._anchorFlag = false;
5850                         if (ListTab.SelectedTab != null)
5851                         {
5852                             var tabtype = _statuses.Tabs[ListTab.SelectedTab.Text].TabType;
5853                             if (tabtype == MyCommon.TabUsageType.Related || tabtype == MyCommon.TabUsageType.UserTimeline || tabtype == MyCommon.TabUsageType.PublicSearch || tabtype == MyCommon.TabUsageType.SearchResults)
5854                             {
5855                                 var relTp = ListTab.SelectedTab;
5856                                 RemoveSpecifiedTab(relTp.Text, false);
5857                                 SaveConfigsTabs();
5858                             }
5859                         }
5860                     }),
5861
5862                 // 上下キー, PageUp/Downキー, Home/Endキー は既定の動作を残しつつアンカー初期化
5863                 ShortcutCommand.Create(Keys.Up, Keys.Down, Keys.PageUp, Keys.PageDown, Keys.Home, Keys.End)
5864                     .FocusedOn(FocusedControl.ListTab)
5865                     .Do(() => this._anchorFlag = false, preventDefault: false),
5866
5867                 // PreviewKeyDownEventArgs.IsInputKey を true にしてスクロールを発生させる
5868                 ShortcutCommand.Create(Keys.Up, Keys.Down)
5869                     .FocusedOn(FocusedControl.PostBrowser)
5870                     .Do(() => { }),
5871
5872                 ShortcutCommand.Create(Keys.Control | Keys.R)
5873                     .Do(() => this.MakeReplyOrDirectStatus(isAuto: false, isReply: true)),
5874
5875                 ShortcutCommand.Create(Keys.Control | Keys.D)
5876                     .Do(() => this.doStatusDelete()),
5877
5878                 ShortcutCommand.Create(Keys.Control | Keys.M)
5879                     .Do(() => this.MakeReplyOrDirectStatus(isAuto: false, isReply: false)),
5880
5881                 ShortcutCommand.Create(Keys.Control | Keys.S)
5882                     .Do(() => this.FavoriteChange(FavAdd: true)),
5883
5884                 ShortcutCommand.Create(Keys.Control | Keys.I)
5885                     .Do(() => this.doRepliedStatusOpen()),
5886
5887                 ShortcutCommand.Create(Keys.Control | Keys.Q)
5888                     .Do(() => this.doQuoteOfficial()),
5889
5890                 ShortcutCommand.Create(Keys.Control | Keys.B)
5891                     .Do(() => this.ReadedStripMenuItem_Click(null, null)),
5892
5893                 ShortcutCommand.Create(Keys.Control | Keys.T)
5894                     .Do(() => this.HashManageMenuItem_Click(null, null)),
5895
5896                 ShortcutCommand.Create(Keys.Control | Keys.L)
5897                     .Do(() => this.UrlConvertAutoToolStripMenuItem_Click(null, null)),
5898
5899                 ShortcutCommand.Create(Keys.Control | Keys.Y)
5900                     .NotFocusedOn(FocusedControl.PostBrowser)
5901                     .Do(() => this.MultiLineMenuItem_Click(null, null)),
5902
5903                 ShortcutCommand.Create(Keys.Control | Keys.F)
5904                     .Do(() => this.MenuItemSubSearch_Click(null, null)),
5905
5906                 ShortcutCommand.Create(Keys.Control | Keys.U)
5907                     .Do(() => this.ShowUserTimeline()),
5908
5909                 ShortcutCommand.Create(Keys.Control | Keys.H)
5910                     .Do(() => this.MoveToHomeToolStripMenuItem_Click(null, null)),
5911
5912                 ShortcutCommand.Create(Keys.Control | Keys.G)
5913                     .Do(() => this.MoveToFavToolStripMenuItem_Click(null, null)),
5914
5915                 ShortcutCommand.Create(Keys.Control | Keys.O)
5916                     .Do(() => this.StatusOpenMenuItem_Click(null, null)),
5917
5918                 ShortcutCommand.Create(Keys.Control | Keys.E)
5919                     .Do(() => this.OpenURLMenuItem_Click(null, null)),
5920
5921                 ShortcutCommand.Create(Keys.Control | Keys.Home, Keys.Control | Keys.End)
5922                     .FocusedOn(FocusedControl.ListTab)
5923                     .Do(() => this._colorize = true, preventDefault: false),
5924
5925                 ShortcutCommand.Create(Keys.Control | Keys.N)
5926                     .FocusedOn(FocusedControl.ListTab)
5927                     .Do(() => this.GoNextTab(forward: true)),
5928
5929                 ShortcutCommand.Create(Keys.Control | Keys.P)
5930                     .FocusedOn(FocusedControl.ListTab)
5931                     .Do(() => this.GoNextTab(forward: false)),
5932
5933                 ShortcutCommand.Create(Keys.Control | Keys.C, Keys.Control | Keys.Insert)
5934                     .FocusedOn(FocusedControl.ListTab)
5935                     .Do(() => this.CopyStot()),
5936
5937                 // タブダイレクト選択(Ctrl+1~8,Ctrl+9)
5938                 ShortcutCommand.Create(Keys.Control | Keys.D1)
5939                     .FocusedOn(FocusedControl.ListTab)
5940                     .OnlyWhen(() => this.ListTab.TabPages.Count >= 1)
5941                     .Do(() => this.ListTab.SelectedIndex = 0),
5942
5943                 ShortcutCommand.Create(Keys.Control | Keys.D2)
5944                     .FocusedOn(FocusedControl.ListTab)
5945                     .OnlyWhen(() => this.ListTab.TabPages.Count >= 2)
5946                     .Do(() => this.ListTab.SelectedIndex = 1),
5947
5948                 ShortcutCommand.Create(Keys.Control | Keys.D3)
5949                     .FocusedOn(FocusedControl.ListTab)
5950                     .OnlyWhen(() => this.ListTab.TabPages.Count >= 3)
5951                     .Do(() => this.ListTab.SelectedIndex = 2),
5952
5953                 ShortcutCommand.Create(Keys.Control | Keys.D4)
5954                     .FocusedOn(FocusedControl.ListTab)
5955                     .OnlyWhen(() => this.ListTab.TabPages.Count >= 4)
5956                     .Do(() => this.ListTab.SelectedIndex = 3),
5957
5958                 ShortcutCommand.Create(Keys.Control | Keys.D5)
5959                     .FocusedOn(FocusedControl.ListTab)
5960                     .OnlyWhen(() => this.ListTab.TabPages.Count >= 5)
5961                     .Do(() => this.ListTab.SelectedIndex = 4),
5962
5963                 ShortcutCommand.Create(Keys.Control | Keys.D6)
5964                     .FocusedOn(FocusedControl.ListTab)
5965                     .OnlyWhen(() => this.ListTab.TabPages.Count >= 6)
5966                     .Do(() => this.ListTab.SelectedIndex = 5),
5967
5968                 ShortcutCommand.Create(Keys.Control | Keys.D7)
5969                     .FocusedOn(FocusedControl.ListTab)
5970                     .OnlyWhen(() => this.ListTab.TabPages.Count >= 7)
5971                     .Do(() => this.ListTab.SelectedIndex = 6),
5972
5973                 ShortcutCommand.Create(Keys.Control | Keys.D8)
5974                     .FocusedOn(FocusedControl.ListTab)
5975                     .OnlyWhen(() => this.ListTab.TabPages.Count >= 8)
5976                     .Do(() => this.ListTab.SelectedIndex = 7),
5977
5978                 ShortcutCommand.Create(Keys.Control | Keys.D9)
5979                     .FocusedOn(FocusedControl.ListTab)
5980                     .Do(() => this.ListTab.SelectedIndex = this.ListTab.TabPages.Count - 1),
5981
5982                 ShortcutCommand.Create(Keys.Control | Keys.A)
5983                     .FocusedOn(FocusedControl.StatusText)
5984                     .Do(() => this.StatusText.SelectAll()),
5985
5986                 ShortcutCommand.Create(Keys.Control | Keys.V)
5987                     .FocusedOn(FocusedControl.StatusText)
5988                     .Do(() => this.ProcClipboardFromStatusTextWhenCtrlPlusV()),
5989
5990                 ShortcutCommand.Create(Keys.Control | Keys.Up)
5991                     .FocusedOn(FocusedControl.StatusText)
5992                     .Do(() => this.StatusTextHistoryBack()),
5993
5994                 ShortcutCommand.Create(Keys.Control | Keys.Down)
5995                     .FocusedOn(FocusedControl.StatusText)
5996                     .Do(() => this.StatusTextHistoryForward()),
5997
5998                 ShortcutCommand.Create(Keys.Control | Keys.PageUp, Keys.Control | Keys.P)
5999                     .FocusedOn(FocusedControl.StatusText)
6000                     .Do(() => {
6001                         if (ListTab.SelectedIndex == 0)
6002                         {
6003                             ListTab.SelectedIndex = ListTab.TabCount - 1;
6004                         }
6005                         else
6006                         {
6007                             ListTab.SelectedIndex -= 1;
6008                         }
6009                         StatusText.Focus();
6010                     }),
6011
6012                 ShortcutCommand.Create(Keys.Control | Keys.PageDown, Keys.Control | Keys.N)
6013                     .FocusedOn(FocusedControl.StatusText)
6014                     .Do(() => {
6015                         if (ListTab.SelectedIndex == ListTab.TabCount - 1)
6016                         {
6017                             ListTab.SelectedIndex = 0;
6018                         }
6019                         else
6020                         {
6021                             ListTab.SelectedIndex += 1;
6022                         }
6023                         StatusText.Focus();
6024                     }),
6025
6026                 ShortcutCommand.Create(Keys.Control | Keys.Y)
6027                     .FocusedOn(FocusedControl.PostBrowser)
6028                     .Do(() => {
6029                         var multiline = !SettingManager.Local.StatusMultiline;
6030                         SettingManager.Local.StatusMultiline = multiline;
6031                         MultiLineMenuItem.Checked = multiline;
6032                         MultiLineMenuItem_Click(this.MultiLineMenuItem, EventArgs.Empty);
6033                     }),
6034
6035                 ShortcutCommand.Create(Keys.Shift | Keys.F3)
6036                     .Do(() => this.MenuItemSearchPrev_Click(null, null)),
6037
6038                 ShortcutCommand.Create(Keys.Shift | Keys.F5)
6039                     .Do(() => this.DoRefreshMore()),
6040
6041                 ShortcutCommand.Create(Keys.Shift | Keys.F6)
6042                     .Do(() => this.RefreshTabAsync<MentionsTabModel>(backward: true)),
6043
6044                 ShortcutCommand.Create(Keys.Shift | Keys.F7)
6045                     .Do(() => this.RefreshTabAsync<DirectMessagesTabModel>(backward: true)),
6046
6047                 ShortcutCommand.Create(Keys.Shift | Keys.R)
6048                     .NotFocusedOn(FocusedControl.StatusText)
6049                     .Do(() => this.DoRefreshMore()),
6050
6051                 ShortcutCommand.Create(Keys.Shift | Keys.H)
6052                     .FocusedOn(FocusedControl.ListTab)
6053                     .Do(() => this.GoTopEnd(GoTop: true)),
6054
6055                 ShortcutCommand.Create(Keys.Shift | Keys.L)
6056                     .FocusedOn(FocusedControl.ListTab)
6057                     .Do(() => this.GoTopEnd(GoTop: false)),
6058
6059                 ShortcutCommand.Create(Keys.Shift | Keys.M)
6060                     .FocusedOn(FocusedControl.ListTab)
6061                     .Do(() => this.GoMiddle()),
6062
6063                 ShortcutCommand.Create(Keys.Shift | Keys.G)
6064                     .FocusedOn(FocusedControl.ListTab)
6065                     .Do(() => this.GoLast()),
6066
6067                 ShortcutCommand.Create(Keys.Shift | Keys.Z)
6068                     .FocusedOn(FocusedControl.ListTab)
6069                     .Do(() => this.MoveMiddle()),
6070
6071                 ShortcutCommand.Create(Keys.Shift | Keys.Oem4)
6072                     .FocusedOn(FocusedControl.ListTab)
6073                     .Do(() => this.GoBackInReplyToPostTree(parallel: true, isForward: false)),
6074
6075                 ShortcutCommand.Create(Keys.Shift | Keys.Oem6)
6076                     .FocusedOn(FocusedControl.ListTab)
6077                     .Do(() => this.GoBackInReplyToPostTree(parallel: true, isForward: true)),
6078
6079                 // お気に入り前後ジャンプ(SHIFT+N←/P→)
6080                 ShortcutCommand.Create(Keys.Shift | Keys.Right, Keys.Shift | Keys.N)
6081                     .FocusedOn(FocusedControl.ListTab)
6082                     .Do(() => this.GoFav(forward: true)),
6083
6084                 // お気に入り前後ジャンプ(SHIFT+N←/P→)
6085                 ShortcutCommand.Create(Keys.Shift | Keys.Left, Keys.Shift | Keys.P)
6086                     .FocusedOn(FocusedControl.ListTab)
6087                     .Do(() => this.GoFav(forward: false)),
6088
6089                 ShortcutCommand.Create(Keys.Shift | Keys.Space)
6090                     .FocusedOn(FocusedControl.ListTab)
6091                     .Do(() => this.GoBackSelectPostChain()),
6092
6093                 ShortcutCommand.Create(Keys.Alt | Keys.R)
6094                     .Do(() => this.doReTweetOfficial(isConfirm: true)),
6095
6096                 ShortcutCommand.Create(Keys.Alt | Keys.P)
6097                     .OnlyWhen(() => this._curPost != null)
6098                     .Do(() => this.doShowUserStatus(_curPost.ScreenName, ShowInputDialog: false)),
6099
6100                 ShortcutCommand.Create(Keys.Alt | Keys.Up)
6101                     .Do(() => this.tweetDetailsView.ScrollDownPostBrowser(forward: false)),
6102
6103                 ShortcutCommand.Create(Keys.Alt | Keys.Down)
6104                     .Do(() => this.tweetDetailsView.ScrollDownPostBrowser(forward: true)),
6105
6106                 ShortcutCommand.Create(Keys.Alt | Keys.PageUp)
6107                     .Do(() => this.tweetDetailsView.PageDownPostBrowser(forward: false)),
6108
6109                 ShortcutCommand.Create(Keys.Alt | Keys.PageDown)
6110                     .Do(() => this.tweetDetailsView.PageDownPostBrowser(forward: true)),
6111
6112                 // 別タブの同じ書き込みへ(ALT+←/→)
6113                 ShortcutCommand.Create(Keys.Alt | Keys.Right)
6114                     .FocusedOn(FocusedControl.ListTab)
6115                     .Do(() => this.GoSamePostToAnotherTab(left: false)),
6116
6117                 ShortcutCommand.Create(Keys.Alt | Keys.Left)
6118                     .FocusedOn(FocusedControl.ListTab)
6119                     .Do(() => this.GoSamePostToAnotherTab(left: true)),
6120
6121                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.R)
6122                     .Do(() => this.MakeReplyOrDirectStatus(isAuto: false, isReply: true, isAll: true)),
6123
6124                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.C, Keys.Control | Keys.Shift | Keys.Insert)
6125                     .Do(() => this.CopyIdUri()),
6126
6127                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.F)
6128                     .OnlyWhen(() => this.ListTab.SelectedTab != null &&
6129                         this._statuses.Tabs[this.ListTab.SelectedTab.Text].TabType == MyCommon.TabUsageType.PublicSearch)
6130                     .Do(() => this.ListTab.SelectedTab.Controls["panelSearch"].Controls["comboSearch"].Focus()),
6131
6132                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.S)
6133                     .Do(() => this.FavoriteChange(FavAdd: false)),
6134
6135                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.B)
6136                     .Do(() => this.UnreadStripMenuItem_Click(null, null)),
6137
6138                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.T)
6139                     .Do(() => this.HashToggleMenuItem_Click(null, null)),
6140
6141                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.P)
6142                     .Do(() => this.ImageSelectMenuItem_Click(null, null)),
6143
6144                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.H)
6145                     .Do(() => this.doMoveToRTHome()),
6146
6147                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.Up)
6148                     .FocusedOn(FocusedControl.StatusText)
6149                     .Do(() => {
6150                         if (_curList != null && _curList.VirtualListSize != 0 &&
6151                                     _curList.SelectedIndices.Count > 0 && _curList.SelectedIndices[0] > 0)
6152                         {
6153                             var idx = _curList.SelectedIndices[0] - 1;
6154                             SelectListItem(_curList, idx);
6155                             _curList.EnsureVisible(idx);
6156                         }
6157                     }),
6158
6159                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.Down)
6160                     .FocusedOn(FocusedControl.StatusText)
6161                     .Do(() => {
6162                         if (_curList != null && _curList.VirtualListSize != 0 && _curList.SelectedIndices.Count > 0
6163                                     && _curList.SelectedIndices[0] < _curList.VirtualListSize - 1)
6164                         {
6165                             var idx = _curList.SelectedIndices[0] + 1;
6166                             SelectListItem(_curList, idx);
6167                             _curList.EnsureVisible(idx);
6168                         }
6169                     }),
6170
6171                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.Space)
6172                     .FocusedOn(FocusedControl.StatusText)
6173                     .Do(() => {
6174                         if (StatusText.SelectionStart > 0)
6175                         {
6176                             int endidx = StatusText.SelectionStart - 1;
6177                             string startstr = "";
6178                             for (int i = StatusText.SelectionStart - 1; i >= 0; i--)
6179                             {
6180                                 char c = StatusText.Text[i];
6181                                 if (Char.IsLetterOrDigit(c) || c == '_')
6182                                 {
6183                                     continue;
6184                                 }
6185                                 if (c == '@')
6186                                 {
6187                                     startstr = StatusText.Text.Substring(i + 1, endidx - i);
6188                                     int cnt = AtIdSupl.ItemCount;
6189                                     ShowSuplDialog(StatusText, AtIdSupl, startstr.Length + 1, startstr);
6190                                     if (AtIdSupl.ItemCount != cnt) ModifySettingAtId = true;
6191                                 }
6192                                 else if (c == '#')
6193                                 {
6194                                     startstr = StatusText.Text.Substring(i + 1, endidx - i);
6195                                     ShowSuplDialog(StatusText, HashSupl, startstr.Length + 1, startstr);
6196                                 }
6197                                 else
6198                                 {
6199                                     break;
6200                                 }
6201                             }
6202                         }
6203                     }),
6204
6205                 // ソートダイレクト選択(Ctrl+Shift+1~8,Ctrl+Shift+9)
6206                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.D1)
6207                     .FocusedOn(FocusedControl.ListTab)
6208                     .Do(() => this.SetSortColumnByDisplayIndex(0)),
6209
6210                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.D2)
6211                     .FocusedOn(FocusedControl.ListTab)
6212                     .Do(() => this.SetSortColumnByDisplayIndex(1)),
6213
6214                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.D3)
6215                     .FocusedOn(FocusedControl.ListTab)
6216                     .Do(() => this.SetSortColumnByDisplayIndex(2)),
6217
6218                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.D4)
6219                     .FocusedOn(FocusedControl.ListTab)
6220                     .Do(() => this.SetSortColumnByDisplayIndex(3)),
6221
6222                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.D5)
6223                     .FocusedOn(FocusedControl.ListTab)
6224                     .Do(() => this.SetSortColumnByDisplayIndex(4)),
6225
6226                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.D6)
6227                     .FocusedOn(FocusedControl.ListTab)
6228                     .Do(() => this.SetSortColumnByDisplayIndex(5)),
6229
6230                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.D7)
6231                     .FocusedOn(FocusedControl.ListTab)
6232                     .Do(() => this.SetSortColumnByDisplayIndex(6)),
6233
6234                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.D8)
6235                     .FocusedOn(FocusedControl.ListTab)
6236                     .Do(() => this.SetSortColumnByDisplayIndex(7)),
6237
6238                 ShortcutCommand.Create(Keys.Control | Keys.Shift | Keys.D9)
6239                     .FocusedOn(FocusedControl.ListTab)
6240                     .Do(() => this.SetSortLastColumn()),
6241
6242                 ShortcutCommand.Create(Keys.Control | Keys.Alt | Keys.S)
6243                     .FocusedOn(FocusedControl.ListTab)
6244                     .Do(() => this.FavoritesRetweetOfficial()),
6245
6246                 ShortcutCommand.Create(Keys.Control | Keys.Alt | Keys.R)
6247                     .FocusedOn(FocusedControl.ListTab)
6248                     .Do(() => this.FavoritesRetweetUnofficial()),
6249
6250                 ShortcutCommand.Create(Keys.Control | Keys.Alt | Keys.H)
6251                     .FocusedOn(FocusedControl.ListTab)
6252                     .Do(() => this.OpenUserAppointUrl()),
6253
6254                 ShortcutCommand.Create(Keys.Alt | Keys.Shift | Keys.R)
6255                     .FocusedOn(FocusedControl.PostBrowser)
6256                     .Do(() => this.doReTweetUnofficial()),
6257
6258                 ShortcutCommand.Create(Keys.Alt | Keys.Shift | Keys.T)
6259                     .OnlyWhen(() => this.ExistCurrentPost)
6260                     .Do(() => this.tweetDetailsView.DoTranslation()),
6261
6262                 ShortcutCommand.Create(Keys.Alt | Keys.Shift | Keys.R)
6263                     .Do(() => this.doReTweetUnofficial()),
6264
6265                 ShortcutCommand.Create(Keys.Alt | Keys.Shift | Keys.C, Keys.Alt | Keys.Shift | Keys.Insert)
6266                     .Do(() => this.CopyUserId()),
6267
6268                 ShortcutCommand.Create(Keys.Alt | Keys.Shift | Keys.Up)
6269                     .Do(() => this.tweetThumbnail1.ScrollUp()),
6270
6271                 ShortcutCommand.Create(Keys.Alt | Keys.Shift | Keys.Down)
6272                     .Do(() => this.tweetThumbnail1.ScrollDown()),
6273
6274                 ShortcutCommand.Create(Keys.Alt | Keys.Shift | Keys.Enter)
6275                     .FocusedOn(FocusedControl.ListTab)
6276                     .OnlyWhen(() => !this.SplitContainer3.Panel2Collapsed)
6277                     .Do(() => this.OpenThumbnailPicture(this.tweetThumbnail1.Thumbnail)),
6278             };
6279         }
6280
6281         internal bool CommonKeyDown(Keys keyData, FocusedControl focusedOn, out Task asyncTask)
6282         {
6283             // Task を返す非同期処理があれば asyncTask に代入する
6284             asyncTask = null;
6285
6286             // ShortcutCommand に対応しているコマンドはここで処理される
6287             foreach (var command in this.shortcutCommands)
6288             {
6289                 if (command.IsMatch(keyData, focusedOn))
6290                 {
6291                     asyncTask = command.RunCommand();
6292                     return command.PreventDefault;
6293                 }
6294             }
6295
6296             return false;
6297         }
6298
6299         private void GoNextTab(bool forward)
6300         {
6301             int idx = ListTab.SelectedIndex;
6302             if (forward)
6303             {
6304                 idx += 1;
6305                 if (idx > ListTab.TabPages.Count - 1) idx = 0;
6306             }
6307             else
6308             {
6309                 idx -= 1;
6310                 if (idx < 0) idx = ListTab.TabPages.Count - 1;
6311             }
6312             ListTab.SelectedIndex = idx;
6313         }
6314
6315         private void CopyStot()
6316         {
6317             string clstr = "";
6318             StringBuilder sb = new StringBuilder();
6319             bool IsProtected = false;
6320             bool isDm = false;
6321             if (this._curTab != null && this._statuses.GetTabByName(this._curTab.Text) != null) isDm = this._statuses.GetTabByName(this._curTab.Text).TabType == MyCommon.TabUsageType.DirectMessage;
6322             foreach (int idx in _curList.SelectedIndices)
6323             {
6324                 PostClass post = _statuses.Tabs[_curTab.Text][idx];
6325                 if (post.IsDeleted) continue;
6326                 if (!isDm)
6327                 {
6328                     if (post.RetweetedId != null)
6329                         sb.AppendFormat("{0}:{1} [https://twitter.com/{0}/status/{2}]{3}", post.ScreenName, post.TextSingleLine, post.RetweetedId, Environment.NewLine);
6330                     else
6331                         sb.AppendFormat("{0}:{1} [https://twitter.com/{0}/status/{2}]{3}", post.ScreenName, post.TextSingleLine, post.StatusId, Environment.NewLine);
6332                 }
6333                 else
6334                 {
6335                     sb.AppendFormat("{0}:{1} [{2}]{3}", post.ScreenName, post.TextSingleLine, post.StatusId, Environment.NewLine);
6336                 }
6337             }
6338             if (IsProtected)
6339             {
6340                 MessageBox.Show(Properties.Resources.CopyStotText1);
6341             }
6342             if (sb.Length > 0)
6343             {
6344                 clstr = sb.ToString();
6345                 try
6346                 {
6347                     Clipboard.SetDataObject(clstr, false, 5, 100);
6348                 }
6349                 catch (Exception ex)
6350                 {
6351                     MessageBox.Show(ex.Message);
6352                 }
6353             }
6354         }
6355
6356         private void CopyIdUri()
6357         {
6358             if (this._curTab == null)
6359                 return;
6360
6361             var tab = this._statuses.GetTabByName(this._curTab.Text);
6362             if (tab == null || tab is DirectMessagesTabModel)
6363                 return;
6364
6365             var copyUrls = new List<string>();
6366             foreach (int idx in _curList.SelectedIndices)
6367             {
6368                 var post = tab[idx];
6369                 copyUrls.Add(MyCommon.GetStatusUrl(post));
6370             }
6371
6372             if (copyUrls.Count == 0)
6373                 return;
6374
6375             try
6376             {
6377                 Clipboard.SetDataObject(string.Join(Environment.NewLine, copyUrls), false, 5, 100);
6378             }
6379             catch (ExternalException ex)
6380             {
6381                 MessageBox.Show(ex.Message);
6382             }
6383         }
6384
6385         private void GoFav(bool forward)
6386         {
6387             if (_curList.VirtualListSize == 0) return;
6388             int fIdx = 0;
6389             int toIdx = 0;
6390             int stp = 1;
6391
6392             if (forward)
6393             {
6394                 if (_curList.SelectedIndices.Count == 0)
6395                 {
6396                     fIdx = 0;
6397                 }
6398                 else
6399                 {
6400                     fIdx = _curList.SelectedIndices[0] + 1;
6401                     if (fIdx > _curList.VirtualListSize - 1) return;
6402                 }
6403                 toIdx = _curList.VirtualListSize;
6404                 stp = 1;
6405             }
6406             else
6407             {
6408                 if (_curList.SelectedIndices.Count == 0)
6409                 {
6410                     fIdx = _curList.VirtualListSize - 1;
6411                 }
6412                 else
6413                 {
6414                     fIdx = _curList.SelectedIndices[0] - 1;
6415                     if (fIdx < 0) return;
6416                 }
6417                 toIdx = -1;
6418                 stp = -1;
6419             }
6420
6421             for (int idx = fIdx; idx != toIdx; idx += stp)
6422             {
6423                 if (_statuses.Tabs[_curTab.Text][idx].IsFav)
6424                 {
6425                     SelectListItem(_curList, idx);
6426                     _curList.EnsureVisible(idx);
6427                     break;
6428                 }
6429             }
6430         }
6431
6432         private void GoSamePostToAnotherTab(bool left)
6433         {
6434             if (this._curList.SelectedIndices.Count == 0)
6435                 return;
6436
6437             var tab = this._statuses.Tabs[this._curTab.Text];
6438
6439             // Directタブは対象外(見つかるはずがない)
6440             if (tab.TabType == MyCommon.TabUsageType.DirectMessage)
6441                 return;
6442
6443             var selectedIndex = this._curList.SelectedIndices[0];
6444             var selectedStatusId = tab.GetStatusIdAt(selectedIndex);
6445
6446             int fIdx, toIdx, stp;
6447
6448             if (left)
6449             {
6450                 // 左のタブへ
6451                 if (ListTab.SelectedIndex == 0)
6452                 {
6453                     return;
6454                 }
6455                 else
6456                 {
6457                     fIdx = ListTab.SelectedIndex - 1;
6458                 }
6459                 toIdx = -1;
6460                 stp = -1;
6461             }
6462             else
6463             {
6464                 // 右のタブへ
6465                 if (ListTab.SelectedIndex == ListTab.TabCount - 1)
6466                 {
6467                     return;
6468                 }
6469                 else
6470                 {
6471                     fIdx = ListTab.SelectedIndex + 1;
6472                 }
6473                 toIdx = ListTab.TabCount;
6474                 stp = 1;
6475             }
6476
6477             for (int tabidx = fIdx; tabidx != toIdx; tabidx += stp)
6478             {
6479                 var targetTab = this._statuses.Tabs[this.ListTab.TabPages[tabidx].Text];
6480
6481                 // Directタブは対象外
6482                 if (targetTab.TabType == MyCommon.TabUsageType.DirectMessage)
6483                     continue;
6484
6485                 var foundIndex = targetTab.IndexOf(selectedStatusId);
6486                 if (foundIndex != -1)
6487                 {
6488                     ListTab.SelectedIndex = tabidx;
6489                     SelectListItem(_curList, foundIndex);
6490                     _curList.EnsureVisible(foundIndex);
6491                     return;
6492                 }
6493             }
6494         }
6495
6496         private void GoPost(bool forward)
6497         {
6498             if (_curList.SelectedIndices.Count == 0 || _curPost == null)
6499                 return;
6500
6501             var tab = this._statuses.Tabs[this._curTab.Text];
6502             var selectedIndex = this._curList.SelectedIndices[0];
6503
6504             int fIdx, toIdx, stp;
6505
6506             if (forward)
6507             {
6508                 fIdx = selectedIndex + 1;
6509                 if (fIdx > tab.AllCount - 1) return;
6510                 toIdx = tab.AllCount;
6511                 stp = 1;
6512             }
6513             else
6514             {
6515                 fIdx = selectedIndex - 1;
6516                 if (fIdx < 0) return;
6517                 toIdx = -1;
6518                 stp = -1;
6519             }
6520
6521             string name = "";
6522             if (_curPost.RetweetedId == null)
6523             {
6524                 name = _curPost.ScreenName;
6525             }
6526             else
6527             {
6528                 name = _curPost.RetweetedBy;
6529             }
6530             for (int idx = fIdx; idx != toIdx; idx += stp)
6531             {
6532                 var post = tab[idx];
6533                 if (post.RetweetedId == null)
6534                 {
6535                     if (post.ScreenName == name)
6536                     {
6537                         SelectListItem(_curList, idx);
6538                         _curList.EnsureVisible(idx);
6539                         break;
6540                     }
6541                 }
6542                 else
6543                 {
6544                     if (post.RetweetedBy == name)
6545                     {
6546                         SelectListItem(_curList, idx);
6547                         _curList.EnsureVisible(idx);
6548                         break;
6549                     }
6550                 }
6551             }
6552         }
6553
6554         private void GoRelPost(bool forward)
6555         {
6556             if (this._curList.SelectedIndices.Count == 0)
6557                 return;
6558
6559             var tab = this._statuses.Tabs[this._curTab.Text];
6560             var selectedIndex = this._curList.SelectedIndices[0];
6561
6562             int fIdx, toIdx, stp;
6563
6564             if (forward)
6565             {
6566                 fIdx = selectedIndex + 1;
6567                 if (fIdx > tab.AllCount - 1) return;
6568                 toIdx = tab.AllCount;
6569                 stp = 1;
6570             }
6571             else
6572             {
6573                 fIdx = selectedIndex - 1;
6574                 if (fIdx < 0) return;
6575                 toIdx = -1;
6576                 stp = -1;
6577             }
6578
6579             if (!_anchorFlag)
6580             {
6581                 if (_curPost == null) return;
6582                 _anchorPost = _curPost;
6583                 _anchorFlag = true;
6584             }
6585             else
6586             {
6587                 if (_anchorPost == null) return;
6588             }
6589
6590             for (int idx = fIdx; idx != toIdx; idx += stp)
6591             {
6592                 var post = tab[idx];
6593                 if (post.ScreenName == _anchorPost.ScreenName ||
6594                     post.RetweetedBy == _anchorPost.ScreenName ||
6595                     post.ScreenName == _anchorPost.RetweetedBy ||
6596                     (!string.IsNullOrEmpty(post.RetweetedBy) && post.RetweetedBy == _anchorPost.RetweetedBy) ||
6597                     _anchorPost.ReplyToList.Any(x => x.UserId == post.UserId) ||
6598                     _anchorPost.ReplyToList.Any(x => x.UserId == post.RetweetedByUserId) ||
6599                     post.ReplyToList.Any(x => x.UserId == _anchorPost.UserId) ||
6600                     post.ReplyToList.Any(x => x.UserId == _anchorPost.RetweetedByUserId))
6601                 {
6602                     SelectListItem(_curList, idx);
6603                     _curList.EnsureVisible(idx);
6604                     break;
6605                 }
6606             }
6607         }
6608
6609         private void GoAnchor()
6610         {
6611             if (_anchorPost == null) return;
6612             int idx = _statuses.Tabs[_curTab.Text].IndexOf(_anchorPost.StatusId);
6613             if (idx == -1) return;
6614
6615             SelectListItem(_curList, idx);
6616             _curList.EnsureVisible(idx);
6617         }
6618
6619         private void GoTopEnd(bool GoTop)
6620         {
6621             if (_curList.VirtualListSize == 0)
6622                 return;
6623
6624             ListViewItem _item;
6625             int idx;
6626
6627             if (GoTop)
6628             {
6629                 _item = _curList.GetItemAt(0, 25);
6630                 if (_item == null)
6631                     idx = 0;
6632                 else
6633                     idx = _item.Index;
6634             }
6635             else
6636             {
6637                 _item = _curList.GetItemAt(0, _curList.ClientSize.Height - 1);
6638                 if (_item == null)
6639                     idx = _curList.VirtualListSize - 1;
6640                 else
6641                     idx = _item.Index;
6642             }
6643             SelectListItem(_curList, idx);
6644         }
6645
6646         private void GoMiddle()
6647         {
6648             if (_curList.VirtualListSize == 0)
6649                 return;
6650
6651             ListViewItem _item;
6652             int idx1;
6653             int idx2;
6654             int idx3;
6655
6656             _item = _curList.GetItemAt(0, 0);
6657             if (_item == null)
6658             {
6659                 idx1 = 0;
6660             }
6661             else
6662             {
6663                 idx1 = _item.Index;
6664             }
6665
6666             _item = _curList.GetItemAt(0, _curList.ClientSize.Height - 1);
6667             if (_item == null)
6668             {
6669                 idx2 = _curList.VirtualListSize - 1;
6670             }
6671             else
6672             {
6673                 idx2 = _item.Index;
6674             }
6675             idx3 = (idx1 + idx2) / 2;
6676
6677             SelectListItem(_curList, idx3);
6678         }
6679
6680         private void GoLast()
6681         {
6682             if (_curList.VirtualListSize == 0) return;
6683
6684             if (_statuses.SortOrder == SortOrder.Ascending)
6685             {
6686                 SelectListItem(_curList, _curList.VirtualListSize - 1);
6687                 _curList.EnsureVisible(_curList.VirtualListSize - 1);
6688             }
6689             else
6690             {
6691                 SelectListItem(_curList, 0);
6692                 _curList.EnsureVisible(0);
6693             }
6694         }
6695
6696         private void MoveTop()
6697         {
6698             if (_curList.SelectedIndices.Count == 0) return;
6699             int idx = _curList.SelectedIndices[0];
6700             if (_statuses.SortOrder == SortOrder.Ascending)
6701             {
6702                 _curList.EnsureVisible(_curList.VirtualListSize - 1);
6703             }
6704             else
6705             {
6706                 _curList.EnsureVisible(0);
6707             }
6708             _curList.EnsureVisible(idx);
6709         }
6710
6711         private async Task GoInReplyToPostTree()
6712         {
6713             if (_curPost == null) return;
6714
6715             TabModel curTabClass = _statuses.Tabs[_curTab.Text];
6716
6717             if (curTabClass.TabType == MyCommon.TabUsageType.PublicSearch && _curPost.InReplyToStatusId == null && _curPost.TextFromApi.Contains("@"))
6718             {
6719                 try
6720                 {
6721                     var post = await tw.GetStatusApi(false, _curPost.StatusId);
6722
6723                     _curPost.InReplyToStatusId = post.InReplyToStatusId;
6724                     _curPost.InReplyToUser = post.InReplyToUser;
6725                     _curPost.IsReply = post.IsReply;
6726                     this.PurgeListViewItemCache();
6727                     _curList.RedrawItems(_curItemIndex, _curItemIndex, false);
6728                 }
6729                 catch (WebApiException ex)
6730                 {
6731                     this.StatusLabel.Text = $"Err:{ex.Message}(GetStatus)";
6732                 }
6733             }
6734
6735             if (!(this.ExistCurrentPost && _curPost.InReplyToUser != null && _curPost.InReplyToStatusId != null)) return;
6736
6737             if (replyChains == null || (replyChains.Count > 0 && replyChains.Peek().InReplyToId != _curPost.StatusId))
6738             {
6739                 replyChains = new Stack<ReplyChain>();
6740             }
6741             replyChains.Push(new ReplyChain(_curPost.StatusId, _curPost.InReplyToStatusId.Value, _curTab));
6742
6743             int inReplyToIndex;
6744             string inReplyToTabName;
6745             long inReplyToId = _curPost.InReplyToStatusId.Value;
6746             string inReplyToUser = _curPost.InReplyToUser;
6747             //Dictionary<long, PostClass> curTabPosts = curTabClass.Posts;
6748
6749             var inReplyToPosts = from tab in _statuses.Tabs.Values
6750                                  orderby tab != curTabClass
6751                                  from post in tab.Posts.Values
6752                                  where post.StatusId == inReplyToId
6753                                  let index = tab.IndexOf(post.StatusId)
6754                                  where index != -1
6755                                  select new {Tab = tab, Index = index};
6756
6757             var inReplyPost = inReplyToPosts.FirstOrDefault();
6758             if (inReplyPost == null)
6759             {
6760                 try
6761                 {
6762                     await Task.Run(async () =>
6763                     {
6764                         var post = await tw.GetStatusApi(false, _curPost.InReplyToStatusId.Value)
6765                             .ConfigureAwait(false);
6766                         post.IsRead = true;
6767
6768                         _statuses.AddPost(post);
6769                         _statuses.DistributePosts();
6770                     });
6771                 }
6772                 catch (WebApiException ex)
6773                 {
6774                     this.StatusLabel.Text = $"Err:{ex.Message}(GetStatus)";
6775                     await this.OpenUriInBrowserAsync(MyCommon.GetStatusUrl(inReplyToUser, inReplyToId));
6776                     return;
6777                 }
6778
6779                 this.RefreshTimeline();
6780
6781                 inReplyPost = inReplyToPosts.FirstOrDefault();
6782                 if (inReplyPost == null)
6783                 {
6784                     await this.OpenUriInBrowserAsync(MyCommon.GetStatusUrl(inReplyToUser, inReplyToId));
6785                     return;
6786                 }
6787             }
6788             inReplyToTabName = inReplyPost.Tab.TabName;
6789             inReplyToIndex = inReplyPost.Index;
6790
6791             TabPage tabPage = this.ListTab.TabPages.Cast<TabPage>().First((tp) => { return tp.Text == inReplyToTabName; });
6792             DetailsListView listView = (DetailsListView)tabPage.Tag;
6793
6794             if (_curTab != tabPage)
6795             {
6796                 this.ListTab.SelectTab(tabPage);
6797             }
6798
6799             this.SelectListItem(listView, inReplyToIndex);
6800             listView.EnsureVisible(inReplyToIndex);
6801         }
6802
6803         private void GoBackInReplyToPostTree(bool parallel = false, bool isForward = true)
6804         {
6805             if (_curPost == null) return;
6806
6807             TabModel curTabClass = _statuses.Tabs[_curTab.Text];
6808             //Dictionary<long, PostClass> curTabPosts = curTabClass.Posts;
6809
6810             if (parallel)
6811             {
6812                 if (_curPost.InReplyToStatusId != null)
6813                 {
6814                     var posts = from t in _statuses.Tabs
6815                                 from p in t.Value.Posts
6816                                 where p.Value.StatusId != _curPost.StatusId && p.Value.InReplyToStatusId == _curPost.InReplyToStatusId
6817                                 let indexOf = t.Value.IndexOf(p.Value.StatusId)
6818                                 where indexOf > -1
6819                                 orderby isForward ? indexOf : indexOf * -1
6820                                 orderby t.Value != curTabClass
6821                                 select new {Tab = t.Value, Post = p.Value, Index = indexOf};
6822                     try
6823                     {
6824                         var postList = posts.ToList();
6825                         for (int i = postList.Count - 1; i >= 0; i--)
6826                         {
6827                             int index = i;
6828                             if (postList.FindIndex((pst) => { return pst.Post.StatusId == postList[index].Post.StatusId; }) != index)
6829                             {
6830                                 postList.RemoveAt(index);
6831                             }
6832                         }
6833                         var post = postList.FirstOrDefault((pst) => { return pst.Tab == curTabClass && isForward ? pst.Index > _curItemIndex : pst.Index < _curItemIndex; });
6834                         if (post == null) post = postList.FirstOrDefault((pst) => { return pst.Tab != curTabClass; });
6835                         if (post == null) post = postList.First();
6836                         this.ListTab.SelectTab(this.ListTab.TabPages.Cast<TabPage>().First((tp) => { return tp.Text == post.Tab.TabName; }));
6837                         DetailsListView listView = (DetailsListView)this.ListTab.SelectedTab.Tag;
6838                         SelectListItem(listView, post.Index);
6839                         listView.EnsureVisible(post.Index);
6840                     }
6841                     catch (InvalidOperationException)
6842                     {
6843                         return;
6844                     }
6845                 }
6846             }
6847             else
6848             {
6849                 if (replyChains == null || replyChains.Count < 1)
6850                 {
6851                     var posts = from t in _statuses.Tabs
6852                                 from p in t.Value.Posts
6853                                 where p.Value.InReplyToStatusId == _curPost.StatusId
6854                                 let indexOf = t.Value.IndexOf(p.Value.StatusId)
6855                                 where indexOf > -1
6856                                 orderby indexOf
6857                                 orderby t.Value != curTabClass
6858                                 select new {Tab = t.Value, Index = indexOf};
6859                     try
6860                     {
6861                         var post = posts.First();
6862                         this.ListTab.SelectTab(this.ListTab.TabPages.Cast<TabPage>().First((tp) => { return tp.Text == post.Tab.TabName; }));
6863                         DetailsListView listView = (DetailsListView)this.ListTab.SelectedTab.Tag;
6864                         SelectListItem(listView, post.Index);
6865                         listView.EnsureVisible(post.Index);
6866                     }
6867                     catch (InvalidOperationException)
6868                     {
6869                         return;
6870                     }
6871                 }
6872                 else
6873                 {
6874                     ReplyChain chainHead = replyChains.Pop();
6875                     if (chainHead.InReplyToId == _curPost.StatusId)
6876                     {
6877                         int idx = _statuses.Tabs[chainHead.OriginalTab.Text].IndexOf(chainHead.OriginalId);
6878                         if (idx == -1)
6879                         {
6880                             replyChains = null;
6881                         }
6882                         else
6883                         {
6884                             try
6885                             {
6886                                 ListTab.SelectTab(chainHead.OriginalTab);
6887                             }
6888                             catch (Exception)
6889                             {
6890                                 replyChains = null;
6891                             }
6892                             SelectListItem(_curList, idx);
6893                             _curList.EnsureVisible(idx);
6894                         }
6895                     }
6896                     else
6897                     {
6898                         replyChains = null;
6899                         this.GoBackInReplyToPostTree(parallel);
6900                     }
6901                 }
6902             }
6903         }
6904
6905         private void GoBackSelectPostChain()
6906         {
6907             if (this.selectPostChains.Count > 1)
6908             {
6909                 var idx = -1;
6910                 TabPage tp = null;
6911
6912                 do
6913                 {
6914                     try
6915                     {
6916                         this.selectPostChains.Pop();
6917                         var (tabPage, post) = this.selectPostChains.Peek();
6918
6919                         if (!this.ListTab.TabPages.Contains(tabPage)) continue;  //該当タブが存在しないので無視
6920
6921                         if (post != null)
6922                         {
6923                             idx = this._statuses.Tabs[tabPage.Text].IndexOf(post.StatusId);
6924                             if (idx == -1) continue;  //該当ポストが存在しないので無視
6925                         }
6926
6927                         tp = tabPage;
6928
6929                         this.selectPostChains.Pop();
6930                     }
6931                     catch (InvalidOperationException)
6932                     {
6933                     }
6934
6935                     break;
6936                 }
6937                 while (this.selectPostChains.Count > 1);
6938
6939                 if (tp == null)
6940                 {
6941                     //状態がおかしいので処理を中断
6942                     //履歴が残り1つであればクリアしておく
6943                     if (this.selectPostChains.Count == 1)
6944                         this.selectPostChains.Clear();
6945                     return;
6946                 }
6947
6948                 DetailsListView lst = (DetailsListView)tp.Tag;
6949                 this.ListTab.SelectedTab = tp;
6950                 if (idx > -1)
6951                 {
6952                     SelectListItem(lst, idx);
6953                     lst.EnsureVisible(idx);
6954                 }
6955                 lst.Focus();
6956             }
6957         }
6958
6959         private void PushSelectPostChain()
6960         {
6961             int count = this.selectPostChains.Count;
6962             if (count > 0)
6963             {
6964                 var (tabPage, post) = this.selectPostChains.Peek();
6965                 if (tabPage == this._curTab)
6966                 {
6967                     if (post == this._curPost) return;  //最新の履歴と同一
6968                     if (post == null) this.selectPostChains.Pop();  //置き換えるため削除
6969                 }
6970             }
6971             if (count >= 2500) TrimPostChain();
6972             this.selectPostChains.Push((this._curTab, this._curPost));
6973         }
6974
6975         private void TrimPostChain()
6976         {
6977             if (this.selectPostChains.Count <= 2000) return;
6978             var p = new Stack<(TabPage, PostClass)>(2000);
6979             for (int i = 0; i < 2000; i++)
6980             {
6981                 p.Push(this.selectPostChains.Pop());
6982             }
6983             this.selectPostChains.Clear();
6984             for (int i = 0; i < 2000; i++)
6985             {
6986                 this.selectPostChains.Push(p.Pop());
6987             }
6988         }
6989
6990         private bool GoStatus(long statusId)
6991         {
6992             if (statusId == 0) return false;
6993             for (int tabidx = 0; tabidx < ListTab.TabCount; tabidx++)
6994             {
6995                 if (_statuses.Tabs[ListTab.TabPages[tabidx].Text].TabType != MyCommon.TabUsageType.DirectMessage && _statuses.Tabs[ListTab.TabPages[tabidx].Text].Contains(statusId))
6996                 {
6997                     int idx = _statuses.Tabs[ListTab.TabPages[tabidx].Text].IndexOf(statusId);
6998                     ListTab.SelectedIndex = tabidx;
6999                     SelectListItem(_curList, idx);
7000                     _curList.EnsureVisible(idx);
7001                     return true;
7002                 }
7003             }
7004             return false;
7005         }
7006
7007         private bool GoDirectMessage(long statusId)
7008         {
7009             if (statusId == 0) return false;
7010             for (int tabidx = 0; tabidx < ListTab.TabCount; tabidx++)
7011             {
7012                 if (_statuses.Tabs[ListTab.TabPages[tabidx].Text].TabType == MyCommon.TabUsageType.DirectMessage && _statuses.Tabs[ListTab.TabPages[tabidx].Text].Contains(statusId))
7013                 {
7014                     int idx = _statuses.Tabs[ListTab.TabPages[tabidx].Text].IndexOf(statusId);
7015                     ListTab.SelectedIndex = tabidx;
7016                     SelectListItem(_curList, idx);
7017                     _curList.EnsureVisible(idx);
7018                     return true;
7019                 }
7020             }
7021             return false;
7022         }
7023
7024         private void MyList_MouseClick(object sender, MouseEventArgs e)
7025             => this._anchorFlag = false;
7026
7027         private void StatusText_Enter(object sender, EventArgs e)
7028         {
7029             // フォーカスの戻り先を StatusText に設定
7030             this.Tag = StatusText;
7031             StatusText.BackColor = _clInputBackcolor;
7032         }
7033
7034         public Color InputBackColor
7035         {
7036             get => _clInputBackcolor;
7037             set => _clInputBackcolor = value;
7038         }
7039
7040         private void StatusText_Leave(object sender, EventArgs e)
7041         {
7042             // フォーカスがメニューに遷移しないならばフォーカスはタブに移ることを期待
7043             if (ListTab.SelectedTab != null && MenuStrip1.Tag == null) this.Tag = ListTab.SelectedTab.Tag;
7044             StatusText.BackColor = Color.FromKnownColor(KnownColor.Window);
7045         }
7046
7047         private async void StatusText_KeyDown(object sender, KeyEventArgs e)
7048         {
7049             if (CommonKeyDown(e.KeyData, FocusedControl.StatusText, out var asyncTask))
7050             {
7051                 e.Handled = true;
7052                 e.SuppressKeyPress = true;
7053             }
7054
7055             this.StatusText_TextChanged(null, null);
7056
7057             if (asyncTask != null)
7058                 await asyncTask;
7059         }
7060
7061         private void SaveConfigsAll(bool ifModified)
7062         {
7063             if (!ifModified)
7064             {
7065                 SaveConfigsCommon();
7066                 SaveConfigsLocal();
7067                 SaveConfigsTabs();
7068                 SaveConfigsAtId();
7069             }
7070             else
7071             {
7072                 if (ModifySettingCommon) SaveConfigsCommon();
7073                 if (ModifySettingLocal) SaveConfigsLocal();
7074                 if (ModifySettingAtId) SaveConfigsAtId();
7075             }
7076         }
7077
7078         private void SaveConfigsAtId()
7079         {
7080             if (_ignoreConfigSave || !SettingManager.Common.UseAtIdSupplement && AtIdSupl == null) return;
7081
7082             ModifySettingAtId = false;
7083             SettingManager.AtIdList.AtIdList = this.AtIdSupl.GetItemList();
7084             SettingManager.SaveAtIdList();
7085         }
7086
7087         private void SaveConfigsCommon()
7088         {
7089             if (_ignoreConfigSave) return;
7090
7091             ModifySettingCommon = false;
7092             lock (_syncObject)
7093             {
7094                 SettingManager.Common.UserName = tw.Username;
7095                 SettingManager.Common.UserId = tw.UserId;
7096                 SettingManager.Common.Token = tw.AccessToken;
7097                 SettingManager.Common.TokenSecret = tw.AccessTokenSecret;
7098                 SettingManager.Common.SortOrder = (int)_statuses.SortOrder;
7099                 switch (_statuses.SortMode)
7100                 {
7101                     case ComparerMode.Nickname:  //ニックネーム
7102                         SettingManager.Common.SortColumn = 1;
7103                         break;
7104                     case ComparerMode.Data:  //本文
7105                         SettingManager.Common.SortColumn = 2;
7106                         break;
7107                     case ComparerMode.Id:  //時刻=発言Id
7108                         SettingManager.Common.SortColumn = 3;
7109                         break;
7110                     case ComparerMode.Name:  //名前
7111                         SettingManager.Common.SortColumn = 4;
7112                         break;
7113                     case ComparerMode.Source:  //Source
7114                         SettingManager.Common.SortColumn = 7;
7115                         break;
7116                 }
7117
7118                 SettingManager.Common.HashTags = HashMgr.HashHistories;
7119                 if (HashMgr.IsPermanent)
7120                 {
7121                     SettingManager.Common.HashSelected = HashMgr.UseHash;
7122                 }
7123                 else
7124                 {
7125                     SettingManager.Common.HashSelected = "";
7126                 }
7127                 SettingManager.Common.HashIsHead = HashMgr.IsHead;
7128                 SettingManager.Common.HashIsPermanent = HashMgr.IsPermanent;
7129                 SettingManager.Common.HashIsNotAddToAtReply = HashMgr.IsNotAddToAtReply;
7130                 SettingManager.Common.TrackWord = tw.TrackWord;
7131                 SettingManager.Common.AllAtReply = tw.AllAtReply;
7132                 SettingManager.Common.UseImageService = ImageSelector.ServiceIndex;
7133                 SettingManager.Common.UseImageServiceName = ImageSelector.ServiceName;
7134
7135                 SettingManager.SaveCommon();
7136             }
7137         }
7138
7139         private void SaveConfigsLocal()
7140         {
7141             if (_ignoreConfigSave) return;
7142             lock (_syncObject)
7143             {
7144                 ModifySettingLocal = false;
7145                 SettingManager.Local.ScaleDimension = this.CurrentAutoScaleDimensions;
7146                 SettingManager.Local.FormSize = _mySize;
7147                 SettingManager.Local.FormLocation = _myLoc;
7148                 SettingManager.Local.SplitterDistance = _mySpDis;
7149                 SettingManager.Local.PreviewDistance = _mySpDis3;
7150                 SettingManager.Local.StatusMultiline = StatusText.Multiline;
7151                 SettingManager.Local.StatusTextHeight = _mySpDis2;
7152
7153                 SettingManager.Local.FontUnread = _fntUnread;
7154                 SettingManager.Local.ColorUnread = _clUnread;
7155                 SettingManager.Local.FontRead = _fntReaded;
7156                 SettingManager.Local.ColorRead = _clReaded;
7157                 SettingManager.Local.FontDetail = _fntDetail;
7158                 SettingManager.Local.ColorDetail = _clDetail;
7159                 SettingManager.Local.ColorDetailBackcolor = _clDetailBackcolor;
7160                 SettingManager.Local.ColorDetailLink = _clDetailLink;
7161                 SettingManager.Local.ColorFav = _clFav;
7162                 SettingManager.Local.ColorOWL = _clOWL;
7163                 SettingManager.Local.ColorRetweet = _clRetweet;
7164                 SettingManager.Local.ColorSelf = _clSelf;
7165                 SettingManager.Local.ColorAtSelf = _clAtSelf;
7166                 SettingManager.Local.ColorTarget = _clTarget;
7167                 SettingManager.Local.ColorAtTarget = _clAtTarget;
7168                 SettingManager.Local.ColorAtFromTarget = _clAtFromTarget;
7169                 SettingManager.Local.ColorAtTo = _clAtTo;
7170                 SettingManager.Local.ColorListBackcolor = _clListBackcolor;
7171                 SettingManager.Local.ColorInputBackcolor = _clInputBackcolor;
7172                 SettingManager.Local.ColorInputFont = _clInputFont;
7173                 SettingManager.Local.FontInputFont = _fntInputFont;
7174
7175                 if (_ignoreConfigSave) return;
7176                 SettingManager.SaveLocal();
7177             }
7178         }
7179
7180         private void SaveConfigsTabs()
7181         {
7182             var tabSettingList = new List<SettingTabs.SettingTabItem>();
7183
7184             var tabs = this.ListTab.TabPages.Cast<TabPage>()
7185                 .Select(x => this._statuses.Tabs[x.Text])
7186                 .Append(this._statuses.GetTabByType(MyCommon.TabUsageType.Mute));
7187
7188             foreach (var tab in tabs)
7189             {
7190                 if (!tab.IsPermanentTabType)
7191                     continue;
7192
7193                 var tabSetting = new SettingTabs.SettingTabItem
7194                 {
7195                     TabName = tab.TabName,
7196                     TabType = tab.TabType,
7197                     UnreadManage = tab.UnreadManage,
7198                     Protected = tab.Protected,
7199                     Notify = tab.Notify,
7200                     SoundFile = tab.SoundFile,
7201                 };
7202
7203                 switch (tab)
7204                 {
7205                     case FilterTabModel filterTab:
7206                         tabSetting.FilterArray = filterTab.FilterArray;
7207                         break;
7208                     case UserTimelineTabModel userTab:
7209                         tabSetting.User = userTab.ScreenName;
7210                         break;
7211                     case PublicSearchTabModel searchTab:
7212                         tabSetting.SearchWords = searchTab.SearchWords;
7213                         tabSetting.SearchLang = searchTab.SearchLang;
7214                         break;
7215                     case ListTimelineTabModel listTab:
7216                         tabSetting.ListInfo = listTab.ListInfo;
7217                         break;
7218                 }
7219
7220                 tabSettingList.Add(tabSetting);
7221             }
7222
7223             SettingManager.Tabs.Tabs = tabSettingList;
7224             SettingManager.SaveTabs();
7225         }
7226
7227         private async void OpenURLFileMenuItem_Click(object sender, EventArgs e)
7228         {
7229             var ret = InputDialog.Show(this, Properties.Resources.OpenURL_InputText, Properties.Resources.OpenURL_Caption, out var inputText);
7230             if (ret != DialogResult.OK)
7231                 return;
7232
7233             var match = Twitter.StatusUrlRegex.Match(inputText);
7234             if (!match.Success)
7235             {
7236                 MessageBox.Show(this, Properties.Resources.OpenURL_InvalidFormat,
7237                     Properties.Resources.OpenURL_Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
7238                 return;
7239             }
7240
7241             try
7242             {
7243                 var statusId = long.Parse(match.Groups["StatusId"].Value);
7244                 await this.OpenRelatedTab(statusId);
7245             }
7246             catch (TabException ex)
7247             {
7248                 MessageBox.Show(this, ex.Message, ApplicationSettings.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
7249             }
7250         }
7251
7252         private void SaveLogMenuItem_Click(object sender, EventArgs e)
7253         {
7254             DialogResult rslt = MessageBox.Show(string.Format(Properties.Resources.SaveLogMenuItem_ClickText1, Environment.NewLine),
7255                     Properties.Resources.SaveLogMenuItem_ClickText2,
7256                     MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
7257             if (rslt == DialogResult.Cancel) return;
7258
7259             SaveFileDialog1.FileName = $"{ApplicationSettings.AssemblyName}Posts{DateTimeUtc.Now.ToLocalTime():yyMMdd-HHmmss}.tsv";
7260             SaveFileDialog1.InitialDirectory = Application.ExecutablePath;
7261             SaveFileDialog1.Filter = Properties.Resources.SaveLogMenuItem_ClickText3;
7262             SaveFileDialog1.FilterIndex = 0;
7263             SaveFileDialog1.Title = Properties.Resources.SaveLogMenuItem_ClickText4;
7264             SaveFileDialog1.RestoreDirectory = true;
7265
7266             if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
7267             {
7268                 if (!SaveFileDialog1.ValidateNames) return;
7269                 using (StreamWriter sw = new StreamWriter(SaveFileDialog1.FileName, false, Encoding.UTF8))
7270                 {
7271                     if (rslt == DialogResult.Yes)
7272                     {
7273                         //All
7274                         for (int idx = 0; idx < _curList.VirtualListSize; idx++)
7275                         {
7276                             PostClass post = _statuses.Tabs[_curTab.Text][idx];
7277                             string protect = "";
7278                             if (post.IsProtect) protect = "Protect";
7279                             sw.WriteLine(post.Nickname + "\t" +
7280                                      "\"" + post.TextFromApi.Replace("\n", "").Replace("\"", "\"\"") + "\"" + "\t" +
7281                                      post.CreatedAt.ToLocalTimeString() + "\t" +
7282                                      post.ScreenName + "\t" +
7283                                      post.StatusId + "\t" +
7284                                      post.ImageUrl + "\t" +
7285                                      "\"" + post.Text.Replace("\n", "").Replace("\"", "\"\"") + "\"" + "\t" +
7286                                      protect);
7287                         }
7288                     }
7289                     else
7290                     {
7291                         foreach (int idx in _curList.SelectedIndices)
7292                         {
7293                             PostClass post = _statuses.Tabs[_curTab.Text][idx];
7294                             string protect = "";
7295                             if (post.IsProtect) protect = "Protect";
7296                             sw.WriteLine(post.Nickname + "\t" +
7297                                      "\"" + post.TextFromApi.Replace("\n", "").Replace("\"", "\"\"") + "\"" + "\t" +
7298                                      post.CreatedAt.ToLocalTimeString() + "\t" +
7299                                      post.ScreenName + "\t" +
7300                                      post.StatusId + "\t" +
7301                                      post.ImageUrl + "\t" +
7302                                      "\"" + post.Text.Replace("\n", "").Replace("\"", "\"\"") + "\"" + "\t" +
7303                                      protect);
7304                         }
7305                     }
7306                 }
7307             }
7308             this.TopMost = SettingManager.Common.AlwaysTop;
7309         }
7310
7311         public bool TabRename(string origTabName, out string newTabName)
7312         {
7313             //タブ名変更
7314             newTabName = null;
7315             using (InputTabName inputName = new InputTabName())
7316             {
7317                 inputName.TabName = origTabName;
7318                 inputName.ShowDialog();
7319                 if (inputName.DialogResult == DialogResult.Cancel) return false;
7320                 newTabName = inputName.TabName;
7321             }
7322             this.TopMost = SettingManager.Common.AlwaysTop;
7323             if (!string.IsNullOrEmpty(newTabName))
7324             {
7325                 //新タブ名存在チェック
7326                 for (int i = 0; i < ListTab.TabCount; i++)
7327                 {
7328                     if (ListTab.TabPages[i].Text == newTabName)
7329                     {
7330                         string tmp = string.Format(Properties.Resources.Tabs_DoubleClickText1, newTabName);
7331                         MessageBox.Show(tmp, Properties.Resources.Tabs_DoubleClickText2, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
7332                         return false;
7333                     }
7334                 }
7335
7336                 var tabPage = this.ListTab.TabPages.Cast<TabPage>()
7337                     .FirstOrDefault(x => x.Text == origTabName);
7338
7339                 // タブ名を変更
7340                 if (tabPage != null)
7341                     tabPage.Text = newTabName;
7342
7343                 _statuses.RenameTab(origTabName, newTabName);
7344
7345                 SaveConfigsCommon();
7346                 SaveConfigsTabs();
7347                 _rclickTabName = newTabName;
7348                 return true;
7349             }
7350             else
7351             {
7352                 return false;
7353             }
7354         }
7355
7356         private void ListTab_MouseClick(object sender, MouseEventArgs e)
7357         {
7358             if (e.Button == MouseButtons.Middle)
7359             {
7360                 for (int i = 0; i < this.ListTab.TabPages.Count; i++)
7361                 {
7362                     if (this.ListTab.GetTabRect(i).Contains(e.Location))
7363                     {
7364                         this.RemoveSpecifiedTab(this.ListTab.TabPages[i].Text, true);
7365                         this.SaveConfigsTabs();
7366                         break;
7367                     }
7368                 }
7369             }
7370         }
7371
7372         private void ListTab_DoubleClick(object sender, MouseEventArgs e)
7373             => this.TabRename(this.ListTab.SelectedTab.Text, out var _);
7374
7375         private void ListTab_MouseDown(object sender, MouseEventArgs e)
7376         {
7377             if (SettingManager.Common.TabMouseLock) return;
7378             if (e.Button == MouseButtons.Left)
7379             {
7380                 for (int i = 0; i < ListTab.TabPages.Count; i++)
7381                 {
7382                     if (this.ListTab.GetTabRect(i).Contains(e.Location))
7383                     {
7384                         _tabDrag = true;
7385                         _tabMouseDownPoint = e.Location;
7386                         break;
7387                     }
7388                 }
7389             }
7390             else
7391             {
7392                 _tabDrag = false;
7393             }
7394         }
7395
7396         private void ListTab_DragEnter(object sender, DragEventArgs e)
7397         {
7398             if (e.Data.GetDataPresent(typeof(TabPage)))
7399                 e.Effect = DragDropEffects.Move;
7400             else
7401                 e.Effect = DragDropEffects.None;
7402         }
7403
7404         private void ListTab_DragDrop(object sender, DragEventArgs e)
7405         {
7406             if (!e.Data.GetDataPresent(typeof(TabPage))) return;
7407
7408             _tabDrag = false;
7409             string tn = "";
7410             bool bef = false;
7411             Point cpos = new Point(e.X, e.Y);
7412             Point spos = ListTab.PointToClient(cpos);
7413             int i;
7414             for (i = 0; i < ListTab.TabPages.Count; i++)
7415             {
7416                 Rectangle rect = ListTab.GetTabRect(i);
7417                 if (rect.Left <= spos.X && spos.X <= rect.Right &&
7418                     rect.Top <= spos.Y && spos.Y <= rect.Bottom)
7419                 {
7420                     tn = ListTab.TabPages[i].Text;
7421                     if (spos.X <= (rect.Left + rect.Right) / 2)
7422                         bef = true;
7423                     else
7424                         bef = false;
7425
7426                     break;
7427                 }
7428             }
7429
7430             //タブのないところにドロップ->最後尾へ移動
7431             if (string.IsNullOrEmpty(tn))
7432             {
7433                 tn = ListTab.TabPages[ListTab.TabPages.Count - 1].Text;
7434                 bef = false;
7435                 i = ListTab.TabPages.Count - 1;
7436             }
7437
7438             TabPage tp = (TabPage)e.Data.GetData(typeof(TabPage));
7439             if (tp.Text == tn) return;
7440
7441             ReOrderTab(tp.Text, tn, bef);
7442         }
7443
7444         public void ReOrderTab(string targetTabText, string baseTabText, bool isBeforeBaseTab)
7445         {
7446             var baseIndex = this.GetTabPageIndex(baseTabText);
7447             if (baseIndex == -1)
7448                 return;
7449
7450             var targetIndex = this.GetTabPageIndex(targetTabText);
7451             if (targetIndex == -1)
7452                 return;
7453
7454             using (ControlTransaction.Layout(this.ListTab))
7455             {
7456                 var mTp = this.ListTab.TabPages[targetIndex];
7457                 this.ListTab.TabPages.Remove(mTp);
7458
7459                 if (targetIndex < baseIndex)
7460                     baseIndex--;
7461
7462                 if (isBeforeBaseTab)
7463                     ListTab.TabPages.Insert(baseIndex, mTp);
7464                 else
7465                     ListTab.TabPages.Insert(baseIndex + 1, mTp);
7466             }
7467
7468             SaveConfigsTabs();
7469         }
7470
7471         private void MakeReplyOrDirectStatus(bool isAuto = true, bool isReply = true, bool isAll = false)
7472         {
7473             //isAuto:true=先頭に挿入、false=カーソル位置に挿入
7474             //isReply:true=@,false=DM
7475             if (!StatusText.Enabled) return;
7476             if (_curList == null) return;
7477             if (_curTab == null) return;
7478             if (!this.ExistCurrentPost) return;
7479
7480             // 複数あてリプライはReplyではなく通常ポスト
7481             //↑仕様変更で全部リプライ扱いでOK(先頭ドット付加しない)
7482             //090403暫定でドットを付加しないようにだけ修正。単独と複数の処理は統合できると思われる。
7483             //090513 all @ replies 廃止の仕様変更によりドット付加に戻し(syo68k)
7484
7485             if (_curList.SelectedIndices.Count > 0)
7486             {
7487                 // アイテムが1件以上選択されている
7488                 if (_curList.SelectedIndices.Count == 1 && !isAll && this.ExistCurrentPost)
7489                 {
7490                     // 単独ユーザー宛リプライまたはDM
7491                     if ((_statuses.Tabs[ListTab.SelectedTab.Text].TabType == MyCommon.TabUsageType.DirectMessage && isAuto) || (!isAuto && !isReply))
7492                     {
7493                         // ダイレクトメッセージ
7494                         this.inReplyTo = null;
7495                         StatusText.Text = "D " + _curPost.ScreenName + " " + StatusText.Text;
7496                         StatusText.SelectionStart = StatusText.Text.Length;
7497                         StatusText.Focus();
7498                         return;
7499                     }
7500                     if (string.IsNullOrEmpty(StatusText.Text))
7501                     {
7502                         //空の場合
7503                         var inReplyToStatusId = this._curPost.RetweetedId ?? this._curPost.StatusId;
7504                         var inReplyToScreenName = this._curPost.ScreenName;
7505                         this.inReplyTo = (inReplyToStatusId, inReplyToScreenName);
7506
7507                         // ステータステキストが入力されていない場合先頭に@ユーザー名を追加する
7508                         StatusText.Text = "@" + _curPost.ScreenName + " ";
7509                     }
7510                     else
7511                     {
7512                         //何か入力済の場合
7513
7514                         if (isAuto)
7515                         {
7516                             //1件選んでEnter or DoubleClick
7517                             if (StatusText.Text.Contains("@" + _curPost.ScreenName + " "))
7518                             {
7519                                 if (this.inReplyTo?.ScreenName == _curPost.ScreenName)
7520                                 {
7521                                     //返信先書き換え
7522                                     var inReplyToStatusId = this._curPost.RetweetedId ?? this._curPost.StatusId;
7523                                     var inReplyToScreenName = this._curPost.ScreenName;
7524                                     this.inReplyTo = (inReplyToStatusId, inReplyToScreenName);
7525                                 }
7526                                 return;
7527                             }
7528                             if (!StatusText.Text.StartsWith("@", StringComparison.Ordinal))
7529                             {
7530                                 //文頭@以外
7531                                 if (StatusText.Text.StartsWith(". ", StringComparison.Ordinal))
7532                                 {
7533                                     // 複数リプライ
7534                                     this.inReplyTo = null;
7535                                     StatusText.Text = StatusText.Text.Insert(2, "@" + _curPost.ScreenName + " ");
7536                                 }
7537                                 else
7538                                 {
7539                                     // 単独リプライ
7540                                     var inReplyToStatusId = this._curPost.RetweetedId ?? this._curPost.StatusId;
7541                                     var inReplyToScreenName = this._curPost.ScreenName;
7542                                     this.inReplyTo = (inReplyToStatusId, inReplyToScreenName);
7543                                     StatusText.Text = "@" + _curPost.ScreenName + " " + StatusText.Text;
7544                                 }
7545                             }
7546                             else
7547                             {
7548                                 //文頭@
7549                                 // 複数リプライ
7550                                 this.inReplyTo = null;
7551                                 StatusText.Text = ". @" + _curPost.ScreenName + " " + StatusText.Text;
7552                                 //StatusText.Text = "@" + _curPost.ScreenName + " " + StatusText.Text;
7553                             }
7554                         }
7555                         else
7556                         {
7557                             //1件選んでCtrl-Rの場合(返信先操作せず)
7558                             int sidx = StatusText.SelectionStart;
7559                             string id = "@" + _curPost.ScreenName + " ";
7560                             if (sidx > 0)
7561                             {
7562                                 if (StatusText.Text.Substring(sidx - 1, 1) != " ")
7563                                 {
7564                                     id = " " + id;
7565                                 }
7566                             }
7567                             StatusText.Text = StatusText.Text.Insert(sidx, id);
7568                             sidx += id.Length;
7569                             //if (StatusText.Text.StartsWith("@"))
7570                             //{
7571                             //    //複数リプライ
7572                             //    StatusText.Text = ". " + StatusText.Text.Insert(sidx, " @" + _curPost.ScreenName + " ");
7573                             //    sidx += 5 + _curPost.ScreenName.Length;
7574                             //}
7575                             //else
7576                             //{
7577                             //    // 複数リプライ
7578                             //    StatusText.Text = StatusText.Text.Insert(sidx, " @" + _curPost.ScreenName + " ");
7579                             //    sidx += 3 + _curPost.ScreenName.Length;
7580                             //}
7581                             StatusText.SelectionStart = sidx;
7582                             StatusText.Focus();
7583                             //_reply_to_id = 0;
7584                             //_reply_to_name = null;
7585                             return;
7586                         }
7587                     }
7588                 }
7589                 else
7590                 {
7591                     // 複数リプライ
7592                     if (!isAuto && !isReply) return;
7593
7594                     //C-S-rか、複数の宛先を選択中にEnter/DoubleClick/C-r/C-S-r
7595
7596                     if (isAuto)
7597                     {
7598                         //Enter or DoubleClick
7599
7600                         string sTxt = StatusText.Text;
7601                         if (!sTxt.StartsWith(". ", StringComparison.Ordinal))
7602                         {
7603                             sTxt = ". " + sTxt;
7604                             this.inReplyTo = null;
7605                         }
7606                         for (int cnt = 0; cnt < _curList.SelectedIndices.Count; cnt++)
7607                         {
7608                             PostClass post = _statuses.Tabs[_curTab.Text][_curList.SelectedIndices[cnt]];
7609                             if (!sTxt.Contains("@" + post.ScreenName + " "))
7610                             {
7611                                 sTxt = sTxt.Insert(2, "@" + post.ScreenName + " ");
7612                                 //sTxt = "@" + post.ScreenName + " " + sTxt;
7613                             }
7614                         }
7615                         StatusText.Text = sTxt;
7616                     }
7617                     else
7618                     {
7619                         //C-S-r or C-r
7620                         if (_curList.SelectedIndices.Count > 1)
7621                         {
7622                             //複数ポスト選択
7623
7624                             string ids = "";
7625                             int sidx = StatusText.SelectionStart;
7626                             for (int cnt = 0; cnt < _curList.SelectedIndices.Count; cnt++)
7627                             {
7628                                 PostClass post = _statuses.Tabs[_curTab.Text][_curList.SelectedIndices[cnt]];
7629                                 if (!ids.Contains("@" + post.ScreenName + " ") && post.UserId != tw.UserId)
7630                                 {
7631                                     ids += "@" + post.ScreenName + " ";
7632                                 }
7633                                 if (isAll)
7634                                 {
7635                                     foreach (var (_, screenName) in post.ReplyToList)
7636                                     {
7637                                         if (!ids.Contains("@" + screenName + " ") &&
7638                                             !screenName.Equals(tw.Username, StringComparison.CurrentCultureIgnoreCase))
7639                                         {
7640                                             Match m = Regex.Match(post.TextFromApi, "[@@](?<id>" + screenName + ")([^a-zA-Z0-9]|$)", RegexOptions.IgnoreCase);
7641                                             if (m.Success)
7642                                                 ids += "@" + m.Result("${id}") + " ";
7643                                             else
7644                                                 ids += "@" + screenName + " ";
7645                                         }
7646                                     }
7647                                 }
7648                             }
7649                             if (ids.Length == 0) return;
7650                             if (!StatusText.Text.StartsWith(". ", StringComparison.Ordinal))
7651                             {
7652                                 this.inReplyTo = null;
7653                                 StatusText.Text = ". " + StatusText.Text;
7654                                 sidx += 2;
7655                             }
7656                             if (sidx > 0)
7657                             {
7658                                 if (StatusText.Text.Substring(sidx - 1, 1) != " ")
7659                                 {
7660                                     ids = " " + ids;
7661                                 }
7662                             }
7663                             StatusText.Text = StatusText.Text.Insert(sidx, ids);
7664                             sidx += ids.Length;
7665                             //if (StatusText.Text.StartsWith("@"))
7666                             //{
7667                             //    StatusText.Text = ". " + StatusText.Text.Insert(sidx, ids);
7668                             //    sidx += 2 + ids.Length;
7669                             //}
7670                             //else
7671                             //{
7672                             //    StatusText.Text = StatusText.Text.Insert(sidx, ids);
7673                             //    sidx += 1 + ids.Length;
7674                             //}
7675                             StatusText.SelectionStart = sidx;
7676                             StatusText.Focus();
7677                             return;
7678                         }
7679                         else
7680                         {
7681                             //1件のみ選択のC-S-r(返信元付加する可能性あり)
7682
7683                             string ids = "";
7684                             int sidx = StatusText.SelectionStart;
7685                             PostClass post = _curPost;
7686                             if (!ids.Contains("@" + post.ScreenName + " ") && post.UserId != tw.UserId)
7687                             {
7688                                 ids += "@" + post.ScreenName + " ";
7689                             }
7690                             foreach (var (_, screenName) in post.ReplyToList)
7691                             {
7692                                 if (!ids.Contains("@" + screenName + " ") &&
7693                                     !screenName.Equals(tw.Username, StringComparison.CurrentCultureIgnoreCase))
7694                                 {
7695                                     Match m = Regex.Match(post.TextFromApi, "[@@](?<id>" + screenName + ")([^a-zA-Z0-9]|$)", RegexOptions.IgnoreCase);
7696                                     if (m.Success)
7697                                         ids += "@" + m.Result("${id}") + " ";
7698                                     else
7699                                         ids += "@" + screenName + " ";
7700                                 }
7701                             }
7702                             if (!string.IsNullOrEmpty(post.RetweetedBy))
7703                             {
7704                                 if (!ids.Contains("@" + post.RetweetedBy + " ") && post.RetweetedByUserId != tw.UserId)
7705                                 {
7706                                     ids += "@" + post.RetweetedBy + " ";
7707                                 }
7708                             }
7709                             if (ids.Length == 0) return;
7710                             if (string.IsNullOrEmpty(StatusText.Text))
7711                             {
7712                                 //未入力の場合のみ返信先付加
7713                                 var inReplyToStatusId = this._curPost.RetweetedId ?? this._curPost.StatusId;
7714                                 var inReplyToScreenName = this._curPost.ScreenName;
7715                                 this.inReplyTo = (inReplyToStatusId, inReplyToScreenName);
7716
7717                                 StatusText.Text = ids;
7718                                 StatusText.SelectionStart = ids.Length;
7719                                 StatusText.Focus();
7720                                 return;
7721                             }
7722
7723                             if (sidx > 0)
7724                             {
7725                                 if (StatusText.Text.Substring(sidx - 1, 1) != " ")
7726                                 {
7727                                     ids = " " + ids;
7728                                 }
7729                             }
7730                             StatusText.Text = StatusText.Text.Insert(sidx, ids);
7731                             sidx += ids.Length;
7732                             StatusText.SelectionStart = sidx;
7733                             StatusText.Focus();
7734                             return;
7735                         }
7736                     }
7737                 }
7738                 StatusText.SelectionStart = StatusText.Text.Length;
7739                 StatusText.Focus();
7740             }
7741         }
7742
7743         private void ListTab_MouseUp(object sender, MouseEventArgs e)
7744             => this._tabDrag = false;
7745
7746         private static int iconCnt = 0;
7747         private static int blinkCnt = 0;
7748         private static bool blink = false;
7749         private static bool idle = false;
7750
7751         private async Task RefreshTasktrayIcon()
7752         {
7753             if (_colorize)
7754                 await this.Colorize();
7755
7756             if (!TimerRefreshIcon.Enabled) return;
7757             //Static usCheckCnt As int = 0
7758
7759             //Static iconDlListTopItem As ListViewItem = null
7760
7761             //if (((ListView)ListTab.SelectedTab.Tag).TopItem == iconDlListTopItem)
7762             //    ((ImageDictionary)this.TIconDic).PauseGetImage = false;
7763             //else
7764             //    ((ImageDictionary)this.TIconDic).PauseGetImage = true;
7765             //
7766             //iconDlListTopItem = ((ListView)ListTab.SelectedTab.Tag).TopItem;
7767
7768             iconCnt += 1;
7769             blinkCnt += 1;
7770             //usCheckCnt += 1;
7771
7772             //if (usCheckCnt > 300)    //1min
7773             //{
7774             //    usCheckCnt = 0;
7775             //    if (!this.IsReceivedUserStream)
7776             //    {
7777             //        TraceOut("ReconnectUserStream");
7778             //        tw.ReconnectUserStream();
7779             //    }
7780             //}
7781
7782             var busy = this.workerSemaphore.CurrentCount != MAX_WORKER_THREADS;
7783
7784             if (iconCnt >= this.NIconRefresh.Length)
7785             {
7786                 iconCnt = 0;
7787             }
7788             if (blinkCnt > 10)
7789             {
7790                 blinkCnt = 0;
7791                 //未保存の変更を保存
7792                 SaveConfigsAll(true);
7793             }
7794
7795             if (busy)
7796             {
7797                 NotifyIcon1.Icon = NIconRefresh[iconCnt];
7798                 idle = false;
7799                 _myStatusError = false;
7800                 return;
7801             }
7802
7803             TabModel tb = _statuses.GetTabByType(MyCommon.TabUsageType.Mentions);
7804             if (SettingManager.Common.ReplyIconState != MyCommon.REPLY_ICONSTATE.None && tb != null && tb.UnreadCount > 0)
7805             {
7806                 if (blinkCnt > 0) return;
7807                 blink = !blink;
7808                 if (blink || SettingManager.Common.ReplyIconState == MyCommon.REPLY_ICONSTATE.StaticIcon)
7809                 {
7810                     NotifyIcon1.Icon = ReplyIcon;
7811                 }
7812                 else
7813                 {
7814                     NotifyIcon1.Icon = ReplyIconBlink;
7815                 }
7816                 idle = false;
7817                 return;
7818             }
7819
7820             if (idle) return;
7821             idle = true;
7822             //優先度:エラー→オフライン→アイドル
7823             //エラーは更新アイコンでクリアされる
7824             if (_myStatusError)
7825             {
7826                 NotifyIcon1.Icon = NIconAtRed;
7827                 return;
7828             }
7829             if (_myStatusOnline)
7830             {
7831                 NotifyIcon1.Icon = NIconAt;
7832             }
7833             else
7834             {
7835                 NotifyIcon1.Icon = NIconAtSmoke;
7836             }
7837         }
7838
7839         private async void TimerRefreshIcon_Tick(object sender, EventArgs e)
7840             => await this.RefreshTasktrayIcon(); // 200ms
7841
7842         private void ContextMenuTabProperty_Opening(object sender, CancelEventArgs e)
7843         {
7844             //右クリックの場合はタブ名が設定済。アプリケーションキーの場合は現在のタブを対象とする
7845             if (string.IsNullOrEmpty(_rclickTabName) || sender != ContextMenuTabProperty)
7846             {
7847                 if (ListTab != null && ListTab.SelectedTab != null)
7848                     _rclickTabName = ListTab.SelectedTab.Text;
7849                 else
7850                     return;
7851             }
7852
7853             if (_statuses == null) return;
7854             if (_statuses.Tabs == null) return;
7855
7856             if (!this._statuses.Tabs.TryGetValue(this._rclickTabName, out var tb))
7857                 return;
7858
7859             NotifyDispMenuItem.Checked = tb.Notify;
7860             this.NotifyTbMenuItem.Checked = tb.Notify;
7861
7862             soundfileListup = true;
7863             SoundFileComboBox.Items.Clear();
7864             this.SoundFileTbComboBox.Items.Clear();
7865             SoundFileComboBox.Items.Add("");
7866             this.SoundFileTbComboBox.Items.Add("");
7867             DirectoryInfo oDir = new DirectoryInfo(Application.StartupPath + Path.DirectorySeparatorChar);
7868             if (Directory.Exists(Path.Combine(Application.StartupPath, "Sounds")))
7869             {
7870                 oDir = oDir.GetDirectories("Sounds")[0];
7871             }
7872             foreach (FileInfo oFile in oDir.GetFiles("*.wav"))
7873             {
7874                 SoundFileComboBox.Items.Add(oFile.Name);
7875                 this.SoundFileTbComboBox.Items.Add(oFile.Name);
7876             }
7877             int idx = SoundFileComboBox.Items.IndexOf(tb.SoundFile);
7878             if (idx == -1) idx = 0;
7879             SoundFileComboBox.SelectedIndex = idx;
7880             this.SoundFileTbComboBox.SelectedIndex = idx;
7881             soundfileListup = false;
7882             UreadManageMenuItem.Checked = tb.UnreadManage;
7883             this.UnreadMngTbMenuItem.Checked = tb.UnreadManage;
7884
7885             TabMenuControl(_rclickTabName);
7886         }
7887
7888         private void TabMenuControl(string tabName)
7889         {
7890             var tabInfo = _statuses.GetTabByName(tabName);
7891
7892             this.FilterEditMenuItem.Enabled = true;
7893             this.EditRuleTbMenuItem.Enabled = true;
7894
7895             if (tabInfo.IsDefaultTabType)
7896             {
7897                 this.ProtectTabMenuItem.Enabled = false;
7898                 this.ProtectTbMenuItem.Enabled = false;
7899             }
7900             else
7901             {
7902                 this.ProtectTabMenuItem.Enabled = true;
7903                 this.ProtectTbMenuItem.Enabled = true;
7904             }
7905
7906             if (tabInfo.IsDefaultTabType || tabInfo.Protected)
7907             {
7908                 this.ProtectTabMenuItem.Checked = true;
7909                 this.ProtectTbMenuItem.Checked = true;
7910                 this.DeleteTabMenuItem.Enabled = false;
7911                 this.DeleteTbMenuItem.Enabled = false;
7912             }
7913             else
7914             {
7915                 this.ProtectTabMenuItem.Checked = false;
7916                 this.ProtectTbMenuItem.Checked = false;
7917                 this.DeleteTabMenuItem.Enabled = true;
7918                 this.DeleteTbMenuItem.Enabled = true;
7919             }
7920         }
7921
7922         private void ProtectTabMenuItem_Click(object sender, EventArgs e)
7923         {
7924             var checkState = ((ToolStripMenuItem)sender).Checked;
7925
7926             // チェック状態を同期
7927             this.ProtectTbMenuItem.Checked = checkState;
7928             this.ProtectTabMenuItem.Checked = checkState;
7929
7930             // ロック中はタブの削除を無効化
7931             this.DeleteTabMenuItem.Enabled = !checkState;
7932             this.DeleteTbMenuItem.Enabled = !checkState;
7933
7934             if (string.IsNullOrEmpty(_rclickTabName)) return;
7935             _statuses.Tabs[_rclickTabName].Protected = checkState;
7936
7937             SaveConfigsTabs();
7938         }
7939
7940         private void UreadManageMenuItem_Click(object sender, EventArgs e)
7941         {
7942             UreadManageMenuItem.Checked = ((ToolStripMenuItem)sender).Checked;
7943             this.UnreadMngTbMenuItem.Checked = UreadManageMenuItem.Checked;
7944
7945             if (string.IsNullOrEmpty(_rclickTabName)) return;
7946             ChangeTabUnreadManage(_rclickTabName, UreadManageMenuItem.Checked);
7947
7948             SaveConfigsTabs();
7949         }
7950
7951         public void ChangeTabUnreadManage(string tabName, bool isManage)
7952         {
7953             var idx = this.GetTabPageIndex(tabName);
7954             if (idx == -1)
7955                 return;
7956
7957             _statuses.Tabs[tabName].UnreadManage = isManage;
7958             if (SettingManager.Common.TabIconDisp)
7959             {
7960                 if (_statuses.Tabs[tabName].UnreadCount > 0)
7961                     ListTab.TabPages[idx].ImageIndex = 0;
7962                 else
7963                     ListTab.TabPages[idx].ImageIndex = -1;
7964             }
7965
7966             if (_curTab.Text == tabName)
7967             {
7968                 this.PurgeListViewItemCache();
7969                 _curList.Refresh();
7970             }
7971
7972             SetMainWindowTitle();
7973             SetStatusLabelUrl();
7974             if (!SettingManager.Common.TabIconDisp) ListTab.Refresh();
7975         }
7976
7977         private void NotifyDispMenuItem_Click(object sender, EventArgs e)
7978         {
7979             NotifyDispMenuItem.Checked = ((ToolStripMenuItem)sender).Checked;
7980             this.NotifyTbMenuItem.Checked = NotifyDispMenuItem.Checked;
7981
7982             if (string.IsNullOrEmpty(_rclickTabName)) return;
7983
7984             _statuses.Tabs[_rclickTabName].Notify = NotifyDispMenuItem.Checked;
7985
7986             SaveConfigsTabs();
7987         }
7988
7989         private void SoundFileComboBox_SelectedIndexChanged(object sender, EventArgs e)
7990         {
7991             if (soundfileListup || string.IsNullOrEmpty(_rclickTabName)) return;
7992
7993             _statuses.Tabs[_rclickTabName].SoundFile = (string)((ToolStripComboBox)sender).SelectedItem;
7994
7995             SaveConfigsTabs();
7996         }
7997
7998         private void DeleteTabMenuItem_Click(object sender, EventArgs e)
7999         {
8000             if (string.IsNullOrEmpty(_rclickTabName) || sender == this.DeleteTbMenuItem) _rclickTabName = ListTab.SelectedTab.Text;
8001
8002             RemoveSpecifiedTab(_rclickTabName, true);
8003             SaveConfigsTabs();
8004         }
8005
8006         private void FilterEditMenuItem_Click(object sender, EventArgs e)
8007         {
8008             if (string.IsNullOrEmpty(_rclickTabName)) _rclickTabName = _statuses.GetTabByType(MyCommon.TabUsageType.Home).TabName;
8009
8010             using (var fltDialog = new FilterDialog())
8011             {
8012                 fltDialog.Owner = this;
8013                 fltDialog.SetCurrent(_rclickTabName);
8014                 fltDialog.ShowDialog(this);
8015             }
8016             this.TopMost = SettingManager.Common.AlwaysTop;
8017
8018             this.ApplyPostFilters();
8019             SaveConfigsTabs();
8020         }
8021
8022         private async void AddTabMenuItem_Click(object sender, EventArgs e)
8023         {
8024             string tabName = null;
8025             MyCommon.TabUsageType tabUsage;
8026             using (InputTabName inputName = new InputTabName())
8027             {
8028                 inputName.TabName = _statuses.MakeTabName("MyTab");
8029                 inputName.IsShowUsage = true;
8030                 inputName.ShowDialog();
8031                 if (inputName.DialogResult == DialogResult.Cancel) return;
8032                 tabName = inputName.TabName;
8033                 tabUsage = inputName.Usage;
8034             }
8035             this.TopMost = SettingManager.Common.AlwaysTop;
8036             if (!string.IsNullOrEmpty(tabName))
8037             {
8038                 //List対応
8039                 ListElement list = null;
8040                 if (tabUsage == MyCommon.TabUsageType.Lists)
8041                 {
8042                     using (ListAvailable listAvail = new ListAvailable())
8043                     {
8044                         if (listAvail.ShowDialog(this) == DialogResult.Cancel) return;
8045                         if (listAvail.SelectedList == null) return;
8046                         list = listAvail.SelectedList;
8047                     }
8048                 }
8049
8050                 TabModel tab;
8051                 switch (tabUsage)
8052                 {
8053                     case MyCommon.TabUsageType.UserDefined:
8054                         tab = new FilterTabModel(tabName);
8055                         break;
8056                     case MyCommon.TabUsageType.PublicSearch:
8057                         tab = new PublicSearchTabModel(tabName);
8058                         break;
8059                     case MyCommon.TabUsageType.Lists:
8060                         tab = new ListTimelineTabModel(tabName, list);
8061                         break;
8062                     default:
8063                         return;
8064                 }
8065
8066                 if (!_statuses.AddTab(tab) || !AddNewTab(tab, startup: false))
8067                 {
8068                     string tmp = string.Format(Properties.Resources.AddTabMenuItem_ClickText1, tabName);
8069                     MessageBox.Show(tmp, Properties.Resources.AddTabMenuItem_ClickText2, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
8070                 }
8071                 else
8072                 {
8073                     //成功
8074                     SaveConfigsTabs();
8075                     if (tabUsage == MyCommon.TabUsageType.PublicSearch)
8076                     {
8077                         ListTab.SelectedIndex = ListTab.TabPages.Count - 1;
8078                         ListTab.SelectedTab.Controls["panelSearch"].Controls["comboSearch"].Focus();
8079                     }
8080                     if (tabUsage == MyCommon.TabUsageType.Lists)
8081                     {
8082                         ListTab.SelectedIndex = ListTab.TabPages.Count - 1;
8083                         var listTab = this._statuses.Tabs[this._curTab.Text];
8084                         await this.RefreshTabAsync(listTab);
8085                     }
8086                 }
8087             }
8088         }
8089
8090         private void TabMenuItem_Click(object sender, EventArgs e)
8091         {
8092             using (var fltDialog = new FilterDialog())
8093             {
8094                 fltDialog.Owner = this;
8095
8096                 //選択発言を元にフィルタ追加
8097                 foreach (int idx in _curList.SelectedIndices)
8098                 {
8099                     //タブ選択(or追加)
8100                     if (!SelectTab(out var tabName)) return;
8101
8102                     fltDialog.SetCurrent(tabName);
8103                     if (_statuses.Tabs[_curTab.Text][idx].RetweetedId == null)
8104                     {
8105                         fltDialog.AddNewFilter(_statuses.Tabs[_curTab.Text][idx].ScreenName, _statuses.Tabs[_curTab.Text][idx].TextFromApi);
8106                     }
8107                     else
8108                     {
8109                         fltDialog.AddNewFilter(_statuses.Tabs[_curTab.Text][idx].RetweetedBy, _statuses.Tabs[_curTab.Text][idx].TextFromApi);
8110                     }
8111                     fltDialog.ShowDialog(this);
8112                     this.TopMost = SettingManager.Common.AlwaysTop;
8113                 }
8114             }
8115
8116             this.ApplyPostFilters();
8117             SaveConfigsTabs();
8118             if (this.ListTab.SelectedTab != null &&
8119                 ((DetailsListView)this.ListTab.SelectedTab.Tag).SelectedIndices.Count > 0)
8120             {
8121                 _curPost = _statuses.Tabs[this.ListTab.SelectedTab.Text][((DetailsListView)this.ListTab.SelectedTab.Tag).SelectedIndices[0]];
8122             }
8123         }
8124
8125         protected override bool ProcessDialogKey(Keys keyData)
8126         {
8127             //TextBox1でEnterを押してもビープ音が鳴らないようにする
8128             if ((keyData & Keys.KeyCode) == Keys.Enter)
8129             {
8130                 if (StatusText.Focused)
8131                 {
8132                     bool _NewLine = false;
8133                     bool _Post = false;
8134
8135                     if (SettingManager.Common.PostCtrlEnter) //Ctrl+Enter投稿時
8136                     {
8137                         if (StatusText.Multiline)
8138                         {
8139                             if ((keyData & Keys.Shift) == Keys.Shift && (keyData & Keys.Control) != Keys.Control) _NewLine = true;
8140
8141                             if ((keyData & Keys.Control) == Keys.Control) _Post = true;
8142                         }
8143                         else
8144                         {
8145                             if (((keyData & Keys.Control) == Keys.Control)) _Post = true;
8146                         }
8147
8148                     }
8149                     else if (SettingManager.Common.PostShiftEnter) //SHift+Enter投稿時
8150                     {
8151                         if (StatusText.Multiline)
8152                         {
8153                             if ((keyData & Keys.Control) == Keys.Control && (keyData & Keys.Shift) != Keys.Shift) _NewLine = true;
8154
8155                             if ((keyData & Keys.Shift) == Keys.Shift) _Post = true;
8156                         }
8157                         else
8158                         {
8159                             if (((keyData & Keys.Shift) == Keys.Shift)) _Post = true;
8160                         }
8161
8162                     }
8163                     else //Enter投稿時
8164                     {
8165                         if (StatusText.Multiline)
8166                         {
8167                             if ((keyData & Keys.Shift) == Keys.Shift && (keyData & Keys.Control) != Keys.Control) _NewLine = true;
8168
8169                             if (((keyData & Keys.Control) != Keys.Control && (keyData & Keys.Shift) != Keys.Shift) ||
8170                                 ((keyData & Keys.Control) == Keys.Control && (keyData & Keys.Shift) == Keys.Shift)) _Post = true;
8171                         }
8172                         else
8173                         {
8174                             if (((keyData & Keys.Shift) == Keys.Shift) ||
8175                                 (((keyData & Keys.Control) != Keys.Control) &&
8176                                 ((keyData & Keys.Shift) != Keys.Shift))) _Post = true;
8177                         }
8178                     }
8179
8180                     if (_NewLine)
8181                     {
8182                         int pos1 = StatusText.SelectionStart;
8183                         if (StatusText.SelectionLength > 0)
8184                         {
8185                             StatusText.Text = StatusText.Text.Remove(pos1, StatusText.SelectionLength);  //選択状態文字列削除
8186                         }
8187                         StatusText.Text = StatusText.Text.Insert(pos1, Environment.NewLine);  //改行挿入
8188                         StatusText.SelectionStart = pos1 + Environment.NewLine.Length;    //カーソルを改行の次の文字へ移動
8189                         return true;
8190                     }
8191                     else if (_Post)
8192                     {
8193                         PostButton_Click(null, null);
8194                         return true;
8195                     }
8196                 }
8197                 else if (_statuses.Tabs[ListTab.SelectedTab.Text].TabType == MyCommon.TabUsageType.PublicSearch &&
8198                          (ListTab.SelectedTab.Controls["panelSearch"].Controls["comboSearch"].Focused ||
8199                          ListTab.SelectedTab.Controls["panelSearch"].Controls["comboLang"].Focused))
8200                 {
8201                     this.SearchButton_Click(ListTab.SelectedTab.Controls["panelSearch"].Controls["comboSearch"], null);
8202                     return true;
8203                 }
8204             }
8205
8206             return base.ProcessDialogKey(keyData);
8207         }
8208
8209         private void ReplyAllStripMenuItem_Click(object sender, EventArgs e)
8210             => this.MakeReplyOrDirectStatus(false, true, true);
8211
8212         private void IDRuleMenuItem_Click(object sender, EventArgs e)
8213         {
8214             //未選択なら処理終了
8215             if (_curList.SelectedIndices.Count == 0) return;
8216
8217             var tab = this._statuses.Tabs[this._curTab.Text];
8218             var screenNameArray = this._curList.SelectedIndices.Cast<int>()
8219                 .Select(x => tab[x])
8220                 .Select(x => x.RetweetedId != null ? x.RetweetedBy : x.ScreenName)
8221                 .ToArray();
8222
8223             this.AddFilterRuleByScreenName(screenNameArray);
8224
8225             if (screenNameArray.Length != 0)
8226             {
8227                 List<string> atids = new List<string>();
8228                 foreach (var screenName in screenNameArray)
8229                 {
8230                     atids.Add("@" + screenName);
8231                 }
8232                 int cnt = AtIdSupl.ItemCount;
8233                 AtIdSupl.AddRangeItem(atids.ToArray());
8234                 if (AtIdSupl.ItemCount != cnt) ModifySettingAtId = true;
8235             }
8236         }
8237
8238         private void SourceRuleMenuItem_Click(object sender, EventArgs e)
8239         {
8240             if (this._curList.SelectedIndices.Count == 0)
8241                 return;
8242
8243             var tab = this._statuses.Tabs[this._curTab.Text];
8244             var sourceArray = this._curList.SelectedIndices.Cast<int>()
8245                 .Select(x => tab[x].Source).ToArray();
8246
8247             this.AddFilterRuleBySource(sourceArray);
8248         }
8249
8250         public void AddFilterRuleByScreenName(params string[] screenNameArray)
8251         {
8252             //タブ選択(or追加)
8253             if (!SelectTab(out var tabName)) return;
8254
8255             var tab = (FilterTabModel)this._statuses.Tabs[tabName];
8256
8257             bool mv;
8258             bool mk;
8259             if (tab.TabType != MyCommon.TabUsageType.Mute)
8260             {
8261                 this.MoveOrCopy(out mv, out mk);
8262             }
8263             else
8264             {
8265                 // ミュートタブでは常に MoveMatches を true にする
8266                 mv = true;
8267                 mk = false;
8268             }
8269
8270             foreach (var screenName in screenNameArray)
8271             {
8272                 tab.AddFilter(new PostFilterRule
8273                 {
8274                     FilterName = screenName,
8275                     UseNameField = true,
8276                     MoveMatches = mv,
8277                     MarkMatches = mk,
8278                     UseRegex = false,
8279                     FilterByUrl = false,
8280                 });
8281             }
8282
8283             this.ApplyPostFilters();
8284             SaveConfigsTabs();
8285         }
8286
8287         public void AddFilterRuleBySource(params string[] sourceArray)
8288         {
8289             // タブ選択ダイアログを表示(or追加)
8290             if (!this.SelectTab(out var tabName))
8291                 return;
8292
8293             var filterTab = (FilterTabModel)this._statuses.Tabs[tabName];
8294
8295             bool mv;
8296             bool mk;
8297             if (filterTab.TabType != MyCommon.TabUsageType.Mute)
8298             {
8299                 // フィルタ動作選択ダイアログを表示(移動/コピー, マーク有無)
8300                 this.MoveOrCopy(out mv, out mk);
8301             }
8302             else
8303             {
8304                 // ミュートタブでは常に MoveMatches を true にする
8305                 mv = true;
8306                 mk = false;
8307             }
8308
8309             // 振り分けルールに追加するSource
8310             foreach (var source in sourceArray)
8311             {
8312                 filterTab.AddFilter(new PostFilterRule
8313                 {
8314                     FilterSource = source,
8315                     MoveMatches = mv,
8316                     MarkMatches = mk,
8317                     UseRegex = false,
8318                     FilterByUrl = false,
8319                 });
8320             }
8321
8322             this.ApplyPostFilters();
8323             this.SaveConfigsTabs();
8324         }
8325
8326         private bool SelectTab(out string tabName)
8327         {
8328             do
8329             {
8330                 tabName = null;
8331
8332                 //振り分け先タブ選択
8333                 using (var dialog = new TabsDialog(_statuses))
8334                 {
8335                     if (dialog.ShowDialog(this) == DialogResult.Cancel) return false;
8336
8337                     tabName = dialog.SelectedTab?.TabName;
8338                 }
8339
8340                 ListTab.SelectedTab.Focus();
8341                 //新規タブを選択→タブ作成
8342                 if (tabName == null)
8343                 {
8344                     using (InputTabName inputName = new InputTabName())
8345                     {
8346                         inputName.TabName = _statuses.MakeTabName("MyTab");
8347                         inputName.ShowDialog();
8348                         if (inputName.DialogResult == DialogResult.Cancel) return false;
8349                         tabName = inputName.TabName;
8350                     }
8351                     this.TopMost = SettingManager.Common.AlwaysTop;
8352                     if (!string.IsNullOrEmpty(tabName))
8353                     {
8354                         var tab = new FilterTabModel(tabName);
8355                         if (!_statuses.AddTab(tab) || !AddNewTab(tab, startup: false))
8356                         {
8357                             string tmp = string.Format(Properties.Resources.IDRuleMenuItem_ClickText2, tabName);
8358                             MessageBox.Show(tmp, Properties.Resources.IDRuleMenuItem_ClickText3, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
8359                             //もう一度タブ名入力
8360                         }
8361                         else
8362                         {
8363                             return true;
8364                         }
8365                     }
8366                 }
8367                 else
8368                 {
8369                     //既存タブを選択
8370                     return true;
8371                 }
8372             }
8373             while (true);
8374         }
8375
8376         private void MoveOrCopy(out bool move, out bool mark)
8377         {
8378             {
8379                 //移動するか?
8380                 string _tmp = string.Format(Properties.Resources.IDRuleMenuItem_ClickText4, Environment.NewLine);
8381                 if (MessageBox.Show(_tmp, Properties.Resources.IDRuleMenuItem_ClickText5, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
8382                     move = false;
8383                 else
8384                     move = true;
8385             }
8386             if (!move)
8387             {
8388                 //マークするか?
8389                 string _tmp = string.Format(Properties.Resources.IDRuleMenuItem_ClickText6, Environment.NewLine);
8390                 if (MessageBox.Show(_tmp, Properties.Resources.IDRuleMenuItem_ClickText7, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
8391                     mark = true;
8392                 else
8393                     mark = false;
8394             }
8395             else
8396             {
8397                 mark = false;
8398             }
8399         }
8400
8401         private void CopySTOTMenuItem_Click(object sender, EventArgs e)
8402             => this.CopyStot();
8403
8404         private void CopyURLMenuItem_Click(object sender, EventArgs e)
8405             => this.CopyIdUri();
8406
8407         private void SelectAllMenuItem_Click(object sender, EventArgs e)
8408         {
8409             if (StatusText.Focused)
8410             {
8411                 // 発言欄でのCtrl+A
8412                 StatusText.SelectAll();
8413             }
8414             else
8415             {
8416                 // ListView上でのCtrl+A
8417                 NativeMethods.SelectAllItems(this._curList);
8418             }
8419         }
8420
8421         private void MoveMiddle()
8422         {
8423             ListViewItem _item;
8424             int idx1;
8425             int idx2;
8426
8427             if (_curList.SelectedIndices.Count == 0) return;
8428
8429             int idx = _curList.SelectedIndices[0];
8430
8431             _item = _curList.GetItemAt(0, 25);
8432             if (_item == null)
8433                 idx1 = 0;
8434             else
8435                 idx1 = _item.Index;
8436
8437             _item = _curList.GetItemAt(0, _curList.ClientSize.Height - 1);
8438             if (_item == null)
8439                 idx2 = _curList.VirtualListSize - 1;
8440             else
8441                 idx2 = _item.Index;
8442
8443             idx -= Math.Abs(idx1 - idx2) / 2;
8444             if (idx < 0) idx = 0;
8445
8446             _curList.EnsureVisible(_curList.VirtualListSize - 1);
8447             _curList.EnsureVisible(idx);
8448         }
8449
8450         private async void OpenURLMenuItem_Click(object sender, EventArgs e)
8451         {
8452             var linkElements = this.tweetDetailsView.GetLinkElements();
8453
8454             if (linkElements.Length > 0)
8455             {
8456                 UrlDialog.ClearUrl();
8457
8458                 string openUrlStr = "";
8459
8460                 if (linkElements.Length == 1)
8461                 {
8462                     // ツイートに含まれる URL が 1 つのみの場合
8463                     //   => OpenURL ダイアログを表示せずにリンクを開く
8464
8465                     string urlStr = "";
8466                     try
8467                     {
8468                         urlStr = MyCommon.IDNEncode(linkElements[0].GetAttribute("href"));
8469                     }
8470                     catch (ArgumentException)
8471                     {
8472                         //変なHTML?
8473                         return;
8474                     }
8475                     catch (Exception)
8476                     {
8477                         return;
8478                     }
8479                     if (string.IsNullOrEmpty(urlStr)) return;
8480                     openUrlStr = MyCommon.urlEncodeMultibyteChar(urlStr);
8481
8482                     // Ctrl+E で呼ばれた場合を考慮し isReverseSettings の判定を行わない
8483                     await this.OpenUriAsync(new Uri(openUrlStr));
8484                 }
8485                 else
8486                 {
8487                     // ツイートに含まれる URL が複数ある場合
8488                     //   => OpenURL を表示しユーザーが選択したリンクを開く
8489
8490                     foreach (var linkElm in linkElements)
8491                     {
8492                         string urlStr = "";
8493                         string linkText = "";
8494                         string href = "";
8495                         try
8496                         {
8497                             urlStr = linkElm.GetAttribute("title");
8498                             href = MyCommon.IDNEncode(linkElm.GetAttribute("href"));
8499                             if (string.IsNullOrEmpty(urlStr)) urlStr = href;
8500                             linkText = linkElm.InnerText;
8501                         }
8502                         catch (ArgumentException)
8503                         {
8504                             //変なHTML?
8505                             return;
8506                         }
8507                         catch (Exception)
8508                         {
8509                             return;
8510                         }
8511                         if (string.IsNullOrEmpty(urlStr)) continue;
8512                         UrlDialog.AddUrl(new OpenUrlItem(linkText, MyCommon.urlEncodeMultibyteChar(urlStr), href));
8513                     }
8514                     try
8515                     {
8516                         if (UrlDialog.ShowDialog() == DialogResult.OK)
8517                         {
8518                             openUrlStr = UrlDialog.SelectedUrl;
8519
8520                             // Ctrlを押しながらリンクを開いた場合は、設定と逆の動作をするフラグを true としておく
8521                             await this.OpenUriAsync(new Uri(openUrlStr), MyCommon.IsKeyDown(Keys.Control));
8522                         }
8523                     }
8524                     catch (Exception)
8525                     {
8526                         return;
8527                     }
8528                     this.TopMost = SettingManager.Common.AlwaysTop;
8529                 }
8530             }
8531         }
8532
8533         private void ClearTabMenuItem_Click(object sender, EventArgs e)
8534         {
8535             if (string.IsNullOrEmpty(_rclickTabName)) return;
8536             ClearTab(_rclickTabName, true);
8537         }
8538
8539         private void ClearTab(string tabName, bool showWarning)
8540         {
8541             if (showWarning)
8542             {
8543                 string tmp = string.Format(Properties.Resources.ClearTabMenuItem_ClickText1, Environment.NewLine);
8544                 if (MessageBox.Show(tmp, tabName + " " + Properties.Resources.ClearTabMenuItem_ClickText2, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
8545                 {
8546                     return;
8547                 }
8548             }
8549
8550             _statuses.ClearTabIds(tabName);
8551             if (ListTab.SelectedTab.Text == tabName)
8552             {
8553                 _anchorPost = null;
8554                 _anchorFlag = false;
8555                 this.PurgeListViewItemCache();
8556                 _curItemIndex = -1;
8557                 _curPost = null;
8558             }
8559             foreach (TabPage tb in ListTab.TabPages)
8560             {
8561                 if (tb.Text == tabName)
8562                 {
8563                     ((DetailsListView)tb.Tag).VirtualListSize = 0;
8564                     tb.ImageIndex = -1;
8565                     break;
8566                 }
8567             }
8568             if (!SettingManager.Common.TabIconDisp) ListTab.Refresh();
8569
8570             SetMainWindowTitle();
8571             SetStatusLabelUrl();
8572         }
8573
8574         private static long followers = 0;
8575
8576         private void SetMainWindowTitle()
8577         {
8578             //メインウインドウタイトルの書き換え
8579             StringBuilder ttl = new StringBuilder(256);
8580             int ur = 0;
8581             int al = 0;
8582             if (SettingManager.Common.DispLatestPost != MyCommon.DispTitleEnum.None &&
8583                 SettingManager.Common.DispLatestPost != MyCommon.DispTitleEnum.Post &&
8584                 SettingManager.Common.DispLatestPost != MyCommon.DispTitleEnum.Ver &&
8585                 SettingManager.Common.DispLatestPost != MyCommon.DispTitleEnum.OwnStatus)
8586             {
8587                 foreach (var tab in _statuses.Tabs.Values)
8588                 {
8589                     ur += tab.UnreadCount;
8590                     al += tab.AllCount;
8591                 }
8592             }
8593
8594             if (SettingManager.Common.DispUsername) ttl.Append(tw.Username).Append(" - ");
8595             ttl.Append(ApplicationSettings.ApplicationName);
8596             ttl.Append("  ");
8597             switch (SettingManager.Common.DispLatestPost)
8598             {
8599                 case MyCommon.DispTitleEnum.Ver:
8600                     ttl.Append("Ver:").Append(MyCommon.GetReadableVersion());
8601                     break;
8602                 case MyCommon.DispTitleEnum.Post:
8603                     if (_history != null && _history.Count > 1)
8604                         ttl.Append(_history[_history.Count - 2].status.Replace("\r\n", " "));
8605                     break;
8606                 case MyCommon.DispTitleEnum.UnreadRepCount:
8607                     ttl.AppendFormat(Properties.Resources.SetMainWindowTitleText1, _statuses.GetTabByType(MyCommon.TabUsageType.Mentions).UnreadCount + _statuses.GetTabByType(MyCommon.TabUsageType.DirectMessage).UnreadCount);
8608                     break;
8609                 case MyCommon.DispTitleEnum.UnreadAllCount:
8610                     ttl.AppendFormat(Properties.Resources.SetMainWindowTitleText2, ur);
8611                     break;
8612                 case MyCommon.DispTitleEnum.UnreadAllRepCount:
8613                     ttl.AppendFormat(Properties.Resources.SetMainWindowTitleText3, ur, _statuses.GetTabByType(MyCommon.TabUsageType.Mentions).UnreadCount + _statuses.GetTabByType(MyCommon.TabUsageType.DirectMessage).UnreadCount);
8614                     break;
8615                 case MyCommon.DispTitleEnum.UnreadCountAllCount:
8616                     ttl.AppendFormat(Properties.Resources.SetMainWindowTitleText4, ur, al);
8617                     break;
8618                 case MyCommon.DispTitleEnum.OwnStatus:
8619                     if (followers == 0 && tw.FollowersCount > 0) followers = tw.FollowersCount;
8620                     ttl.AppendFormat(Properties.Resources.OwnStatusTitle, tw.StatusesCount, tw.FriendsCount, tw.FollowersCount, tw.FollowersCount - followers);
8621                     break;
8622             }
8623
8624             try
8625             {
8626                 this.Text = ttl.ToString();
8627             }
8628             catch (AccessViolationException)
8629             {
8630                 //原因不明。ポスト内容に依存か?たまーに発生するが再現せず。
8631             }
8632         }
8633
8634         private string GetStatusLabelText()
8635         {
8636             //ステータス欄にカウント表示
8637             //タブ未読数/タブ発言数 全未読数/総発言数 (未読@+未読DM数)
8638             if (_statuses == null) return "";
8639             TabModel tbRep = _statuses.GetTabByType(MyCommon.TabUsageType.Mentions);
8640             TabModel tbDm = _statuses.GetTabByType(MyCommon.TabUsageType.DirectMessage);
8641             if (tbRep == null || tbDm == null) return "";
8642             int urat = tbRep.UnreadCount + tbDm.UnreadCount;
8643             int ur = 0;
8644             int al = 0;
8645             int tur = 0;
8646             int tal = 0;
8647             StringBuilder slbl = new StringBuilder(256);
8648             try
8649             {
8650                 foreach (var tab in _statuses.Tabs.Values)
8651                 {
8652                     ur += tab.UnreadCount;
8653                     al += tab.AllCount;
8654                     if (_curTab != null && tab.TabName.Equals(_curTab.Text))
8655                     {
8656                         tur = tab.UnreadCount;
8657                         tal = tab.AllCount;
8658                     }
8659                 }
8660             }
8661             catch (Exception)
8662             {
8663                 return "";
8664             }
8665
8666             UnreadCounter = ur;
8667             UnreadAtCounter = urat;
8668
8669             var homeTab = this._statuses.GetTabByType<HomeTabModel>();
8670
8671             slbl.AppendFormat(Properties.Resources.SetStatusLabelText1, tur, tal, ur, al, urat, _postTimestamps.Count, _favTimestamps.Count, homeTab.TweetsPerHour);
8672             if (SettingManager.Common.TimelinePeriod == 0)
8673             {
8674                 slbl.Append(Properties.Resources.SetStatusLabelText2);
8675             }
8676             else
8677             {
8678                 slbl.Append(SettingManager.Common.TimelinePeriod + Properties.Resources.SetStatusLabelText3);
8679             }
8680             return slbl.ToString();
8681         }
8682
8683         private async void TwitterApiStatus_AccessLimitUpdated(object sender, EventArgs e)
8684         {
8685             try
8686             {
8687                 if (this.InvokeRequired && !this.IsDisposed)
8688                 {
8689                     await this.InvokeAsync(() => this.TwitterApiStatus_AccessLimitUpdated(sender, e));
8690                 }
8691                 else
8692                 {
8693                     var endpointName = (e as TwitterApiStatus.AccessLimitUpdatedEventArgs).EndpointName;
8694                     SetApiStatusLabel(endpointName);
8695                 }
8696             }
8697             catch (ObjectDisposedException)
8698             {
8699                 return;
8700             }
8701             catch (InvalidOperationException)
8702             {
8703                 return;
8704             }
8705         }
8706
8707         private void SetApiStatusLabel(string endpointName = null)
8708         {
8709             if (_curTab == null)
8710             {
8711                 this.toolStripApiGauge.ApiEndpoint = null;
8712             }
8713             else
8714             {
8715                 var tabType = _statuses.Tabs[_curTab.Text].TabType;
8716
8717                 if (endpointName == null)
8718                 {
8719                     // 表示中のタブに応じて更新
8720                     switch (tabType)
8721                     {
8722                         case MyCommon.TabUsageType.Home:
8723                         case MyCommon.TabUsageType.UserDefined:
8724                             endpointName = "/statuses/home_timeline";
8725                             break;
8726
8727                         case MyCommon.TabUsageType.Mentions:
8728                             endpointName = "/statuses/mentions_timeline";
8729                             break;
8730
8731                         case MyCommon.TabUsageType.Favorites:
8732                             endpointName = "/favorites/list";
8733                             break;
8734
8735                         case MyCommon.TabUsageType.DirectMessage:
8736                             endpointName = "/direct_messages/events/list";
8737                             break;
8738
8739                         case MyCommon.TabUsageType.UserTimeline:
8740                             endpointName = "/statuses/user_timeline";
8741                             break;
8742
8743                         case MyCommon.TabUsageType.Lists:
8744                             endpointName = "/lists/statuses";
8745                             break;
8746
8747                         case MyCommon.TabUsageType.PublicSearch:
8748                             endpointName = "/search/tweets";
8749                             break;
8750
8751                         case MyCommon.TabUsageType.Related:
8752                             endpointName = "/statuses/show/:id";
8753                             break;
8754
8755                         default:
8756                             break;
8757                     }
8758
8759                     this.toolStripApiGauge.ApiEndpoint = endpointName;
8760                 }
8761                 else
8762                 {
8763                     // 表示中のタブに関連する endpoint であれば更新
8764                     var update = false;
8765
8766                     switch (endpointName)
8767                     {
8768                         case "/statuses/home_timeline":
8769                             update = tabType == MyCommon.TabUsageType.Home ||
8770                                      tabType == MyCommon.TabUsageType.UserDefined;
8771                             break;
8772
8773                         case "/statuses/mentions_timeline":
8774                             update = tabType == MyCommon.TabUsageType.Mentions;
8775                             break;
8776
8777                         case "/favorites/list":
8778                             update = tabType == MyCommon.TabUsageType.Favorites;
8779                             break;
8780
8781                         case "/direct_messages/events/list":
8782                             update = tabType == MyCommon.TabUsageType.DirectMessage;
8783                             break;
8784
8785                         case "/statuses/user_timeline":
8786                             update = tabType == MyCommon.TabUsageType.UserTimeline;
8787                             break;
8788
8789                         case "/lists/statuses":
8790                             update = tabType == MyCommon.TabUsageType.Lists;
8791                             break;
8792
8793                         case "/search/tweets":
8794                             update = tabType == MyCommon.TabUsageType.PublicSearch;
8795                             break;
8796
8797                         case "/statuses/show/:id":
8798                             update = tabType == MyCommon.TabUsageType.Related;
8799                             break;
8800
8801                         default:
8802                             break;
8803                     }
8804
8805                     if (update)
8806                     {
8807                         this.toolStripApiGauge.ApiEndpoint = endpointName;
8808                     }
8809                 }
8810             }
8811         }
8812
8813         private void SetStatusLabelUrl()
8814             => this.StatusLabelUrl.Text = this.GetStatusLabelText();
8815
8816         public void SetStatusLabel(string text)
8817             => this.StatusLabel.Text = text;
8818
8819         private void SetNotifyIconText()
8820         {
8821             var ur = new StringBuilder(64);
8822
8823             // タスクトレイアイコンのツールチップテキスト書き換え
8824             // Tween [未読/@]
8825             ur.Remove(0, ur.Length);
8826             if (SettingManager.Common.DispUsername)
8827             {
8828                 ur.Append(tw.Username);
8829                 ur.Append(" - ");
8830             }
8831             ur.Append(ApplicationSettings.ApplicationName);
8832 #if DEBUG
8833             ur.Append("(Debug Build)");
8834 #endif
8835             if (UnreadCounter != -1 && UnreadAtCounter != -1)
8836             {
8837                 ur.Append(" [");
8838                 ur.Append(UnreadCounter);
8839                 ur.Append("/@");
8840                 ur.Append(UnreadAtCounter);
8841                 ur.Append("]");
8842             }
8843             NotifyIcon1.Text = ur.ToString();
8844         }
8845
8846         internal void CheckReplyTo(string StatusText)
8847         {
8848             MatchCollection m;
8849             //ハッシュタグの保存
8850             m = Regex.Matches(StatusText, Twitter.HASHTAG, RegexOptions.IgnoreCase);
8851             string hstr = "";
8852             foreach (Match hm in m)
8853             {
8854                 if (!hstr.Contains("#" + hm.Result("$3") + " "))
8855                 {
8856                     hstr += "#" + hm.Result("$3") + " ";
8857                     HashSupl.AddItem("#" + hm.Result("$3"));
8858                 }
8859             }
8860             if (!string.IsNullOrEmpty(HashMgr.UseHash) && !hstr.Contains(HashMgr.UseHash + " "))
8861             {
8862                 hstr += HashMgr.UseHash;
8863             }
8864             if (!string.IsNullOrEmpty(hstr)) HashMgr.AddHashToHistory(hstr.Trim(), false);
8865
8866             // 本当にリプライ先指定すべきかどうかの判定
8867             m = Regex.Matches(StatusText, "(^|[ -/:-@[-^`{-~])(?<id>@[a-zA-Z0-9_]+)");
8868
8869             if (SettingManager.Common.UseAtIdSupplement)
8870             {
8871                 int bCnt = AtIdSupl.ItemCount;
8872                 foreach (Match mid in m)
8873                 {
8874                     AtIdSupl.AddItem(mid.Result("${id}"));
8875                 }
8876                 if (bCnt != AtIdSupl.ItemCount) ModifySettingAtId = true;
8877             }
8878
8879             // リプライ先ステータスIDの指定がない場合は指定しない
8880             if (this.inReplyTo == null)
8881                 return;
8882
8883             // 通常Reply
8884             // 次の条件を満たす場合に in_reply_to_status_id 指定
8885             // 1. Twitterによりリンクと判定される @idが文中に1つ含まれる (2009/5/28 リンク化される@IDのみカウントするように修正)
8886             // 2. リプライ先ステータスIDが設定されている(リストをダブルクリックで返信している)
8887             // 3. 文中に含まれた@idがリプライ先のポスト者のIDと一致する
8888
8889             if (m != null)
8890             {
8891                 var inReplyToScreenName = this.inReplyTo.Value.ScreenName;
8892                 if (StatusText.StartsWith("@", StringComparison.Ordinal))
8893                 {
8894                     if (StatusText.StartsWith("@" + inReplyToScreenName, StringComparison.Ordinal)) return;
8895                 }
8896                 else
8897                 {
8898                     foreach (Match mid in m)
8899                     {
8900                         if (StatusText.Contains("RT " + mid.Result("${id}") + ":") && mid.Result("${id}") == "@" + inReplyToScreenName) return;
8901                     }
8902                 }
8903             }
8904
8905             this.inReplyTo = null;
8906         }
8907
8908         private void TweenMain_Resize(object sender, EventArgs e)
8909         {
8910             if (!_initialLayout && SettingManager.Common.MinimizeToTray && WindowState == FormWindowState.Minimized)
8911             {
8912                 this.Visible = false;
8913             }
8914             if (_initialLayout && SettingManager.Local != null && this.WindowState == FormWindowState.Normal && this.Visible)
8915             {
8916                 // 現在の DPI と設定保存時の DPI との比を取得する
8917                 var configScaleFactor = SettingManager.Local.GetConfigScaleFactor(this.CurrentAutoScaleDimensions);
8918
8919                 this.ClientSize = ScaleBy(configScaleFactor, SettingManager.Local.FormSize);
8920
8921                 // Splitterの位置設定
8922                 var splitterDistance = ScaleBy(configScaleFactor.Height, SettingManager.Local.SplitterDistance);
8923                 if (splitterDistance > this.SplitContainer1.Panel1MinSize &&
8924                     splitterDistance < this.SplitContainer1.Height - this.SplitContainer1.Panel2MinSize - this.SplitContainer1.SplitterWidth)
8925                 {
8926                     this.SplitContainer1.SplitterDistance = splitterDistance;
8927                 }
8928
8929                 //発言欄複数行
8930                 StatusText.Multiline = SettingManager.Local.StatusMultiline;
8931                 if (StatusText.Multiline)
8932                 {
8933                     var statusTextHeight = ScaleBy(configScaleFactor.Height, SettingManager.Local.StatusTextHeight);
8934                     int dis = SplitContainer2.Height - statusTextHeight - SplitContainer2.SplitterWidth;
8935                     if (dis > SplitContainer2.Panel1MinSize && dis < SplitContainer2.Height - SplitContainer2.Panel2MinSize - SplitContainer2.SplitterWidth)
8936                     {
8937                         SplitContainer2.SplitterDistance = SplitContainer2.Height - statusTextHeight - SplitContainer2.SplitterWidth;
8938                     }
8939                     StatusText.Height = statusTextHeight;
8940                 }
8941                 else
8942                 {
8943                     if (SplitContainer2.Height - SplitContainer2.Panel2MinSize - SplitContainer2.SplitterWidth > 0)
8944                     {
8945                         SplitContainer2.SplitterDistance = SplitContainer2.Height - SplitContainer2.Panel2MinSize - SplitContainer2.SplitterWidth;
8946                     }
8947                 }
8948
8949                 var previewDistance = ScaleBy(configScaleFactor.Width, SettingManager.Local.PreviewDistance);
8950                 if (previewDistance > this.SplitContainer3.Panel1MinSize && previewDistance < this.SplitContainer3.Width - this.SplitContainer3.Panel2MinSize - this.SplitContainer3.SplitterWidth)
8951                 {
8952                     this.SplitContainer3.SplitterDistance = previewDistance;
8953                 }
8954
8955                 // Panel2Collapsed は SplitterDistance の設定を終えるまで true にしない
8956                 this.SplitContainer3.Panel2Collapsed = true;
8957
8958                 _initialLayout = false;
8959             }
8960             if (this.WindowState != FormWindowState.Minimized)
8961             {
8962                 _formWindowState = this.WindowState;
8963             }
8964         }
8965
8966         private void PlaySoundMenuItem_CheckedChanged(object sender, EventArgs e)
8967         {
8968             PlaySoundMenuItem.Checked = ((ToolStripMenuItem)sender).Checked;
8969             this.PlaySoundFileMenuItem.Checked = PlaySoundMenuItem.Checked;
8970             if (PlaySoundMenuItem.Checked)
8971             {
8972                 SettingManager.Common.PlaySound = true;
8973             }
8974             else
8975             {
8976                 SettingManager.Common.PlaySound = false;
8977             }
8978             ModifySettingCommon = true;
8979         }
8980
8981         private void SplitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
8982         {
8983             if (this._initialLayout)
8984                 return;
8985
8986             int splitterDistance;
8987             switch (this.WindowState)
8988             {
8989                 case FormWindowState.Normal:
8990                     splitterDistance = this.SplitContainer1.SplitterDistance;
8991                     break;
8992                 case FormWindowState.Maximized:
8993                     // 最大化時は、通常時のウィンドウサイズに換算した SplitterDistance を算出する
8994                     var normalContainerHeight = this._mySize.Height - this.ToolStripContainer1.TopToolStripPanel.Height - this.ToolStripContainer1.BottomToolStripPanel.Height;
8995                     splitterDistance = this.SplitContainer1.SplitterDistance - (this.SplitContainer1.Height - normalContainerHeight);
8996                     splitterDistance = Math.Min(splitterDistance, normalContainerHeight - this.SplitContainer1.SplitterWidth - this.SplitContainer1.Panel2MinSize);
8997                     break;
8998                 default:
8999                     return;
9000             }
9001
9002             this._mySpDis = splitterDistance;
9003             this.ModifySettingLocal = true;
9004         }
9005
9006         private async Task doRepliedStatusOpen()
9007         {
9008             if (this.ExistCurrentPost && _curPost.InReplyToUser != null && _curPost.InReplyToStatusId != null)
9009             {
9010                 if (MyCommon.IsKeyDown(Keys.Shift))
9011                 {
9012                     await this.OpenUriInBrowserAsync(MyCommon.GetStatusUrl(_curPost.InReplyToUser, _curPost.InReplyToStatusId.Value));
9013                     return;
9014                 }
9015                 if (_statuses.ContainsKey(_curPost.InReplyToStatusId.Value))
9016                 {
9017                     PostClass repPost = _statuses[_curPost.InReplyToStatusId.Value];
9018                     MessageBox.Show($"{repPost.ScreenName} / {repPost.Nickname}   ({repPost.CreatedAt.ToLocalTimeString()})" + Environment.NewLine + repPost.TextFromApi);
9019                 }
9020                 else
9021                 {
9022                     foreach (TabModel tb in _statuses.GetTabsByType(MyCommon.TabUsageType.Lists | MyCommon.TabUsageType.PublicSearch))
9023                     {
9024                         if (tb == null || !tb.Contains(_curPost.InReplyToStatusId.Value)) break;
9025                         PostClass repPost = _statuses[_curPost.InReplyToStatusId.Value];
9026                         MessageBox.Show($"{repPost.ScreenName} / {repPost.Nickname}   ({repPost.CreatedAt.ToLocalTimeString()})" + Environment.NewLine + repPost.TextFromApi);
9027                         return;
9028                     }
9029                     await this.OpenUriInBrowserAsync(MyCommon.GetStatusUrl(_curPost.InReplyToUser, _curPost.InReplyToStatusId.Value));
9030                 }
9031             }
9032         }
9033
9034         private async void RepliedStatusOpenMenuItem_Click(object sender, EventArgs e)
9035             => await this.doRepliedStatusOpen();
9036
9037         private void SplitContainer2_Panel2_Resize(object sender, EventArgs e)
9038         {
9039             if (this._initialLayout)
9040                 return; // SettingLocal の反映が完了するまで multiline の判定を行わない
9041
9042             var multiline = this.SplitContainer2.Panel2.Height > this.SplitContainer2.Panel2MinSize + 2;
9043             if (multiline != this.StatusText.Multiline)
9044             {
9045                 this.StatusText.Multiline = multiline;
9046                 SettingManager.Local.StatusMultiline = multiline;
9047                 ModifySettingLocal = true;
9048             }
9049         }
9050
9051         private void StatusText_MultilineChanged(object sender, EventArgs e)
9052         {
9053             if (this.StatusText.Multiline)
9054                 this.StatusText.ScrollBars = ScrollBars.Vertical;
9055             else
9056                 this.StatusText.ScrollBars = ScrollBars.None;
9057
9058             ModifySettingLocal = true;
9059         }
9060
9061         private void MultiLineMenuItem_Click(object sender, EventArgs e)
9062         {
9063             //発言欄複数行
9064             var menuItemChecked = ((ToolStripMenuItem)sender).Checked;
9065             StatusText.Multiline = menuItemChecked;
9066             SettingManager.Local.StatusMultiline = menuItemChecked;
9067             if (menuItemChecked)
9068             {
9069                 if (SplitContainer2.Height - _mySpDis2 - SplitContainer2.SplitterWidth < 0)
9070                     SplitContainer2.SplitterDistance = 0;
9071                 else
9072                     SplitContainer2.SplitterDistance = SplitContainer2.Height - _mySpDis2 - SplitContainer2.SplitterWidth;
9073             }
9074             else
9075             {
9076                 SplitContainer2.SplitterDistance = SplitContainer2.Height - SplitContainer2.Panel2MinSize - SplitContainer2.SplitterWidth;
9077             }
9078             ModifySettingLocal = true;
9079         }
9080
9081         private async Task<bool> UrlConvertAsync(MyCommon.UrlConverter Converter_Type)
9082         {
9083             if (Converter_Type == MyCommon.UrlConverter.Bitly || Converter_Type == MyCommon.UrlConverter.Jmp)
9084             {
9085                 // OAuth2 アクセストークンまたは API キー (旧方式) のいずれも設定されていなければ短縮しない
9086                 if (string.IsNullOrEmpty(SettingManager.Common.BitlyAccessToken) &&
9087                     (string.IsNullOrEmpty(SettingManager.Common.BilyUser) || string.IsNullOrEmpty(SettingManager.Common.BitlyPwd)))
9088                 {
9089                     MessageBox.Show(this, Properties.Resources.UrlConvert_BitlyAuthRequired, ApplicationSettings.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
9090                     return false;
9091                 }
9092             }
9093
9094             //t.coで投稿時自動短縮する場合は、外部サービスでの短縮禁止
9095             //if (SettingDialog.UrlConvertAuto && SettingDialog.ShortenTco) return;
9096
9097             //Converter_Type=Nicomsの場合は、nicovideoのみ短縮する
9098             //参考資料 RFC3986 Uniform Resource Identifier (URI): Generic Syntax
9099             //Appendix A.  Collected ABNF for URI
9100             //http://www.ietf.org/rfc/rfc3986.txt
9101
9102             string result = "";
9103
9104             const string nico = @"^https?://[a-z]+\.(nicovideo|niconicommons|nicolive)\.jp/[a-z]+/[a-z0-9]+$";
9105
9106             if (StatusText.SelectionLength > 0)
9107             {
9108                 string tmp = StatusText.SelectedText;
9109                 // httpから始まらない場合、ExcludeStringで指定された文字列で始まる場合は対象としない
9110                 if (tmp.StartsWith("http", StringComparison.OrdinalIgnoreCase))
9111                 {
9112                     // 文字列が選択されている場合はその文字列について処理
9113
9114                     //nico.ms使用、nicovideoにマッチしたら変換
9115                     if (SettingManager.Common.Nicoms && Regex.IsMatch(tmp, nico))
9116                     {
9117                         result = nicoms.Shorten(tmp);
9118                     }
9119                     else if (Converter_Type != MyCommon.UrlConverter.Nicoms)
9120                     {
9121                         //短縮URL変換 日本語を含むかもしれないのでURLエンコードする
9122                         try
9123                         {
9124                             var srcUri = new Uri(MyCommon.urlEncodeMultibyteChar(tmp));
9125                             var resultUri = await ShortUrl.Instance.ShortenUrlAsync(Converter_Type, srcUri);
9126                             result = resultUri.AbsoluteUri;
9127                         }
9128                         catch (WebApiException e)
9129                         {
9130                             this.StatusLabel.Text = Converter_Type + ":" + e.Message;
9131                             return false;
9132                         }
9133                         catch (UriFormatException e)
9134                         {
9135                             this.StatusLabel.Text = Converter_Type + ":" + e.Message;
9136                             return false;
9137                         }
9138                     }
9139                     else
9140                     {
9141                         return true;
9142                     }
9143
9144                     if (!string.IsNullOrEmpty(result))
9145                     {
9146                         urlUndo undotmp = new urlUndo();
9147
9148                         // 短縮 URL が生成されるまでの間に投稿欄から元の URL が削除されていたら中断する
9149                         var origUrlIndex = this.StatusText.Text.IndexOf(tmp, StringComparison.Ordinal);
9150                         if (origUrlIndex == -1)
9151                             return false;
9152
9153                         StatusText.Select(origUrlIndex, tmp.Length);
9154                         StatusText.SelectedText = result;
9155
9156                         //undoバッファにセット
9157                         undotmp.Before = tmp;
9158                         undotmp.After = result;
9159
9160                         if (urlUndoBuffer == null)
9161                         {
9162                             urlUndoBuffer = new List<urlUndo>();
9163                             UrlUndoToolStripMenuItem.Enabled = true;
9164                         }
9165
9166                         urlUndoBuffer.Add(undotmp);
9167                     }
9168                 }
9169             }
9170             else
9171             {
9172                 const string url = @"(?<before>(?:[^\""':!=]|^|\:))" +
9173                                    @"(?<url>(?<protocol>https?://)" +
9174                                    @"(?<domain>(?:[\.-]|[^\p{P}\s])+\.[a-z]{2,}(?::[0-9]+)?)" +
9175                                    @"(?<path>/[a-z0-9!*//();:&=+$/%#\-_.,~@]*[a-z0-9)=#/]?)?" +
9176                                    @"(?<query>\?[a-z0-9!*//();:&=+$/%#\-_.,~@?]*[a-z0-9_&=#/])?)";
9177                 // 正規表現にマッチしたURL文字列をtinyurl化
9178                 foreach (Match mt in Regex.Matches(StatusText.Text, url, RegexOptions.IgnoreCase))
9179                 {
9180                     if (StatusText.Text.IndexOf(mt.Result("${url}"), StringComparison.Ordinal) == -1) continue;
9181                     string tmp = mt.Result("${url}");
9182                     if (tmp.StartsWith("w", StringComparison.OrdinalIgnoreCase)) tmp = "http://" + tmp;
9183                     urlUndo undotmp = new urlUndo();
9184
9185                     //選んだURLを選択(?)
9186                     StatusText.Select(StatusText.Text.IndexOf(mt.Result("${url}"), StringComparison.Ordinal), mt.Result("${url}").Length);
9187
9188                     //nico.ms使用、nicovideoにマッチしたら変換
9189                     if (SettingManager.Common.Nicoms && Regex.IsMatch(tmp, nico))
9190                     {
9191                         result = nicoms.Shorten(tmp);
9192                     }
9193                     else if (Converter_Type != MyCommon.UrlConverter.Nicoms)
9194                     {
9195                         //短縮URL変換 日本語を含むかもしれないのでURLエンコードする
9196                         try
9197                         {
9198                             var srcUri = new Uri(MyCommon.urlEncodeMultibyteChar(tmp));
9199                             var resultUri = await ShortUrl.Instance.ShortenUrlAsync(Converter_Type, srcUri);
9200                             result = resultUri.AbsoluteUri;
9201                         }
9202                         catch (HttpRequestException e)
9203                         {
9204                             // 例外のメッセージが「Response status code does not indicate success: 500 (Internal Server Error).」
9205                             // のように長いので「:」が含まれていればそれ以降のみを抽出する
9206                             var message = e.Message.Split(new[] { ':' }, count: 2).Last();
9207
9208                             this.StatusLabel.Text = Converter_Type + ":" + message;
9209                             continue;
9210                         }
9211                         catch (WebApiException e)
9212                         {
9213                             this.StatusLabel.Text = Converter_Type + ":" + e.Message;
9214                             continue;
9215                         }
9216                         catch (UriFormatException e)
9217                         {
9218                             this.StatusLabel.Text = Converter_Type + ":" + e.Message;
9219                             continue;
9220                         }
9221                     }
9222                     else
9223                     {
9224                         continue;
9225                     }
9226
9227                     if (!string.IsNullOrEmpty(result))
9228                     {
9229                         // 短縮 URL が生成されるまでの間に投稿欄から元の URL が削除されていたら中断する
9230                         var origUrlIndex = this.StatusText.Text.IndexOf(mt.Result("${url}"), StringComparison.Ordinal);
9231                         if (origUrlIndex == -1)
9232                             return false;
9233
9234                         StatusText.Select(origUrlIndex, mt.Result("${url}").Length);
9235                         StatusText.SelectedText = result;
9236                         //undoバッファにセット
9237                         undotmp.Before = mt.Result("${url}");
9238                         undotmp.After = result;
9239
9240                         if (urlUndoBuffer == null)
9241                         {
9242                             urlUndoBuffer = new List<urlUndo>();
9243                             UrlUndoToolStripMenuItem.Enabled = true;
9244                         }
9245
9246                         urlUndoBuffer.Add(undotmp);
9247                     }
9248                 }
9249             }
9250
9251             return true;
9252         }
9253
9254         private void doUrlUndo()
9255         {
9256             if (urlUndoBuffer != null)
9257             {
9258                 string tmp = StatusText.Text;
9259                 foreach (urlUndo data in urlUndoBuffer)
9260                 {
9261                     tmp = tmp.Replace(data.After, data.Before);
9262                 }
9263                 StatusText.Text = tmp;
9264                 urlUndoBuffer = null;
9265                 UrlUndoToolStripMenuItem.Enabled = false;
9266                 StatusText.SelectionStart = 0;
9267                 StatusText.SelectionLength = 0;
9268             }
9269         }
9270
9271         private async void TinyURLToolStripMenuItem_Click(object sender, EventArgs e)
9272             => await this.UrlConvertAsync(MyCommon.UrlConverter.TinyUrl);
9273
9274         private async void IsgdToolStripMenuItem_Click(object sender, EventArgs e)
9275             => await this.UrlConvertAsync(MyCommon.UrlConverter.Isgd);
9276
9277         private async void UxnuMenuItem_Click(object sender, EventArgs e)
9278             => await this.UrlConvertAsync(MyCommon.UrlConverter.Uxnu);
9279
9280         private async void UrlConvertAutoToolStripMenuItem_Click(object sender, EventArgs e)
9281         {
9282             if (!await UrlConvertAsync(SettingManager.Common.AutoShortUrlFirst))
9283             {
9284                 MyCommon.UrlConverter svc = SettingManager.Common.AutoShortUrlFirst;
9285                 Random rnd = new Random();
9286                 // 前回使用した短縮URLサービス以外を選択する
9287                 do
9288                 {
9289                     svc = (MyCommon.UrlConverter)rnd.Next(System.Enum.GetNames(typeof(MyCommon.UrlConverter)).Length);
9290                 }
9291                 while (svc == SettingManager.Common.AutoShortUrlFirst || svc == MyCommon.UrlConverter.Nicoms || svc == MyCommon.UrlConverter.Unu);
9292                 await UrlConvertAsync(svc);
9293             }
9294         }
9295
9296         private void UrlUndoToolStripMenuItem_Click(object sender, EventArgs e)
9297             => this.doUrlUndo();
9298
9299         private void NewPostPopMenuItem_CheckStateChanged(object sender, EventArgs e)
9300         {
9301             this.NotifyFileMenuItem.Checked = ((ToolStripMenuItem)sender).Checked;
9302             this.NewPostPopMenuItem.Checked = this.NotifyFileMenuItem.Checked;
9303             SettingManager.Common.NewAllPop = NewPostPopMenuItem.Checked;
9304             ModifySettingCommon = true;
9305         }
9306
9307         private void ListLockMenuItem_CheckStateChanged(object sender, EventArgs e)
9308         {
9309             ListLockMenuItem.Checked = ((ToolStripMenuItem)sender).Checked;
9310             this.LockListFileMenuItem.Checked = ListLockMenuItem.Checked;
9311             SettingManager.Common.ListLock = ListLockMenuItem.Checked;
9312             ModifySettingCommon = true;
9313         }
9314
9315         private void MenuStrip1_MenuActivate(object sender, EventArgs e)
9316         {
9317             // フォーカスがメニューに移る (MenuStrip1.Tag フラグを立てる)
9318             MenuStrip1.Tag = new Object();
9319             MenuStrip1.Select(); // StatusText がフォーカスを持っている場合 Leave が発生
9320         }
9321
9322         private void MenuStrip1_MenuDeactivate(object sender, EventArgs e)
9323         {
9324             if (this.Tag != null) // 設定された戻り先へ遷移
9325             {
9326                 if (this.Tag == this.ListTab.SelectedTab)
9327                     ((Control)this.ListTab.SelectedTab.Tag).Select();
9328                 else
9329                     ((Control)this.Tag).Select();
9330             }
9331             else // 戻り先が指定されていない (初期状態) 場合はタブに遷移
9332             {
9333                 if (ListTab.SelectedIndex > -1 && ListTab.SelectedTab.HasChildren)
9334                 {
9335                     this.Tag = ListTab.SelectedTab.Tag;
9336                     ((Control)this.Tag).Select();
9337                 }
9338             }
9339             // フォーカスがメニューに遷移したかどうかを表すフラグを降ろす
9340             MenuStrip1.Tag = null;
9341         }
9342
9343         private void MyList_ColumnReordered(object sender, ColumnReorderedEventArgs e)
9344         {
9345             DetailsListView lst = (DetailsListView)sender;
9346             if (SettingManager.Local == null) return;
9347
9348             if (_iconCol)
9349             {
9350                 SettingManager.Local.Width1 = lst.Columns[0].Width;
9351                 SettingManager.Local.Width3 = lst.Columns[1].Width;
9352             }
9353             else
9354             {
9355                 int[] darr = new int[lst.Columns.Count];
9356                 for (int i = 0; i < lst.Columns.Count; i++)
9357                 {
9358                     darr[lst.Columns[i].DisplayIndex] = i;
9359                 }
9360                 MyCommon.MoveArrayItem(darr, e.OldDisplayIndex, e.NewDisplayIndex);
9361
9362                 for (int i = 0; i < lst.Columns.Count; i++)
9363                 {
9364                     switch (darr[i])
9365                     {
9366                         case 0:
9367                             SettingManager.Local.DisplayIndex1 = i;
9368                             break;
9369                         case 1:
9370                             SettingManager.Local.DisplayIndex2 = i;
9371                             break;
9372                         case 2:
9373                             SettingManager.Local.DisplayIndex3 = i;
9374                             break;
9375                         case 3:
9376                             SettingManager.Local.DisplayIndex4 = i;
9377                             break;
9378                         case 4:
9379                             SettingManager.Local.DisplayIndex5 = i;
9380                             break;
9381                         case 5:
9382                             SettingManager.Local.DisplayIndex6 = i;
9383                             break;
9384                         case 6:
9385                             SettingManager.Local.DisplayIndex7 = i;
9386                             break;
9387                         case 7:
9388                             SettingManager.Local.DisplayIndex8 = i;
9389                             break;
9390                     }
9391                 }
9392                 SettingManager.Local.Width1 = lst.Columns[0].Width;
9393                 SettingManager.Local.Width2 = lst.Columns[1].Width;
9394                 SettingManager.Local.Width3 = lst.Columns[2].Width;
9395                 SettingManager.Local.Width4 = lst.Columns[3].Width;
9396                 SettingManager.Local.Width5 = lst.Columns[4].Width;
9397                 SettingManager.Local.Width6 = lst.Columns[5].Width;
9398                 SettingManager.Local.Width7 = lst.Columns[6].Width;
9399                 SettingManager.Local.Width8 = lst.Columns[7].Width;
9400             }
9401             ModifySettingLocal = true;
9402             _isColumnChanged = true;
9403         }
9404
9405         private void MyList_ColumnWidthChanged(object sender, ColumnWidthChangedEventArgs e)
9406         {
9407             DetailsListView lst = (DetailsListView)sender;
9408             if (SettingManager.Local == null) return;
9409             if (_iconCol)
9410             {
9411                 if (SettingManager.Local.Width1 != lst.Columns[0].Width)
9412                 {
9413                     SettingManager.Local.Width1 = lst.Columns[0].Width;
9414                     ModifySettingLocal = true;
9415                     _isColumnChanged = true;
9416                 }
9417                 if (SettingManager.Local.Width3 != lst.Columns[1].Width)
9418                 {
9419                     SettingManager.Local.Width3 = lst.Columns[1].Width;
9420                     ModifySettingLocal = true;
9421                     _isColumnChanged = true;
9422                 }
9423             }
9424             else
9425             {
9426                 if (SettingManager.Local.Width1 != lst.Columns[0].Width)
9427                 {
9428                     SettingManager.Local.Width1 = lst.Columns[0].Width;
9429                     ModifySettingLocal = true;
9430                     _isColumnChanged = true;
9431                 }
9432                 if (SettingManager.Local.Width2 != lst.Columns[1].Width)
9433                 {
9434                     SettingManager.Local.Width2 = lst.Columns[1].Width;
9435                     ModifySettingLocal = true;
9436                     _isColumnChanged = true;
9437                 }
9438                 if (SettingManager.Local.Width3 != lst.Columns[2].Width)
9439                 {
9440                     SettingManager.Local.Width3 = lst.Columns[2].Width;
9441                     ModifySettingLocal = true;
9442                     _isColumnChanged = true;
9443                 }
9444                 if (SettingManager.Local.Width4 != lst.Columns[3].Width)
9445                 {
9446                     SettingManager.Local.Width4 = lst.Columns[3].Width;
9447                     ModifySettingLocal = true;
9448                     _isColumnChanged = true;
9449                 }
9450                 if (SettingManager.Local.Width5 != lst.Columns[4].Width)
9451                 {
9452                     SettingManager.Local.Width5 = lst.Columns[4].Width;
9453                     ModifySettingLocal = true;
9454                     _isColumnChanged = true;
9455                 }
9456                 if (SettingManager.Local.Width6 != lst.Columns[5].Width)
9457                 {
9458                     SettingManager.Local.Width6 = lst.Columns[5].Width;
9459                     ModifySettingLocal = true;
9460                     _isColumnChanged = true;
9461                 }
9462                 if (SettingManager.Local.Width7 != lst.Columns[6].Width)
9463                 {
9464                     SettingManager.Local.Width7 = lst.Columns[6].Width;
9465                     ModifySettingLocal = true;
9466                     _isColumnChanged = true;
9467                 }
9468                 if (SettingManager.Local.Width8 != lst.Columns[7].Width)
9469                 {
9470                     SettingManager.Local.Width8 = lst.Columns[7].Width;
9471                     ModifySettingLocal = true;
9472                     _isColumnChanged = true;
9473                 }
9474             }
9475             // 非表示の時にColumnChangedが呼ばれた場合はForm初期化処理中なので保存しない
9476             //if (changed)
9477             //{
9478             //    SaveConfigsLocal();
9479             //}
9480         }
9481
9482         private void SplitContainer2_SplitterMoved(object sender, SplitterEventArgs e)
9483         {
9484             if (StatusText.Multiline) _mySpDis2 = StatusText.Height;
9485             ModifySettingLocal = true;
9486         }
9487
9488         private void TweenMain_DragDrop(object sender, DragEventArgs e)
9489         {
9490             if (e.Data.GetDataPresent(DataFormats.FileDrop))
9491             {
9492                 if (!e.Data.GetDataPresent(DataFormats.Html, false))  // WebBrowserコントロールからの絵文字画像Drag&Dropは弾く
9493                 {
9494                     SelectMedia_DragDrop(e);
9495                 }
9496             }
9497             else if (e.Data.GetDataPresent("UniformResourceLocatorW"))
9498             {
9499                 var (url, title) = GetUrlFromDataObject(e.Data);
9500
9501                 string appendText;
9502                 if (title == null)
9503                     appendText = url;
9504                 else
9505                     appendText = title + " " + url;
9506
9507                 if (this.StatusText.TextLength == 0)
9508                     this.StatusText.Text = appendText;
9509                 else
9510                     this.StatusText.Text += " " + appendText;
9511             }
9512             else if (e.Data.GetDataPresent(DataFormats.UnicodeText))
9513             {
9514                 var text = (string)e.Data.GetData(DataFormats.UnicodeText);
9515                 if (text != null)
9516                     this.StatusText.Text += text;
9517             }
9518             else if (e.Data.GetDataPresent(DataFormats.StringFormat))
9519             {
9520                 string data = (string)e.Data.GetData(DataFormats.StringFormat, true);
9521                 if (data != null) StatusText.Text += data;
9522             }
9523         }
9524
9525         /// <summary>
9526         /// IDataObject から URL とタイトルの対を取得します
9527         /// </summary>
9528         /// <remarks>
9529         /// タイトルのみ取得できなかった場合は Value2 が null のタプルを返すことがあります。
9530         /// </remarks>
9531         /// <exception cref="ArgumentException">不正なフォーマットが入力された場合</exception>
9532         /// <exception cref="NotSupportedException">サポートされていないデータが入力された場合</exception>
9533         internal static (string Url, string Title) GetUrlFromDataObject(IDataObject data)
9534         {
9535             if (data.GetDataPresent("text/x-moz-url"))
9536             {
9537                 // Firefox, Google Chrome で利用可能
9538                 // 参照: https://developer.mozilla.org/ja/docs/DragDrop/Recommended_Drag_Types
9539
9540                 using (var stream = (MemoryStream)data.GetData("text/x-moz-url"))
9541                 {
9542                     var lines = Encoding.Unicode.GetString(stream.ToArray()).TrimEnd('\0').Split('\n');
9543                     if (lines.Length < 2)
9544                         throw new ArgumentException("不正な text/x-moz-url フォーマットです", nameof(data));
9545
9546                     return (lines[0], lines[1]);
9547                 }
9548             }
9549             else if (data.GetDataPresent("IESiteModeToUrl"))
9550             {
9551                 // Internet Exproler 用
9552                 // 保護モードが有効なデフォルトの IE では DragDrop イベントが発火しないため使えない
9553
9554                 using (var stream = (MemoryStream)data.GetData("IESiteModeToUrl"))
9555                 {
9556                     var lines = Encoding.Unicode.GetString(stream.ToArray()).TrimEnd('\0').Split('\0');
9557                     if (lines.Length < 2)
9558                         throw new ArgumentException("不正な IESiteModeToUrl フォーマットです", nameof(data));
9559
9560                     return (lines[0], lines[1]);
9561                 }
9562             }
9563             else if (data.GetDataPresent("UniformResourceLocatorW"))
9564             {
9565                 // それ以外のブラウザ向け
9566
9567                 using (var stream = (MemoryStream)data.GetData("UniformResourceLocatorW"))
9568                 {
9569                     var url = Encoding.Unicode.GetString(stream.ToArray()).TrimEnd('\0');
9570                     return (url, null);
9571                 }
9572             }
9573
9574             throw new NotSupportedException("サポートされていないデータ形式です: " + data.GetFormats()[0]);
9575         }
9576
9577         private void TweenMain_DragEnter(object sender, DragEventArgs e)
9578         {
9579             if (e.Data.GetDataPresent(DataFormats.FileDrop))
9580             {
9581                 if (!e.Data.GetDataPresent(DataFormats.Html, false))  // WebBrowserコントロールからの絵文字画像Drag&Dropは弾く
9582                 {
9583                     SelectMedia_DragEnter(e);
9584                     return;
9585                 }
9586             }
9587             else if (e.Data.GetDataPresent("UniformResourceLocatorW"))
9588             {
9589                 e.Effect = DragDropEffects.Copy;
9590                 return;
9591             }
9592             else if (e.Data.GetDataPresent(DataFormats.UnicodeText))
9593             {
9594                 e.Effect = DragDropEffects.Copy;
9595                 return;
9596             }
9597             else if (e.Data.GetDataPresent(DataFormats.StringFormat))
9598             {
9599                 e.Effect = DragDropEffects.Copy;
9600                 return;
9601             }
9602
9603             e.Effect = DragDropEffects.None;
9604         }
9605
9606         private void TweenMain_DragOver(object sender, DragEventArgs e)
9607         {
9608         }
9609
9610         public bool IsNetworkAvailable()
9611         {
9612             bool nw = true;
9613             nw = MyCommon.IsNetworkAvailable();
9614             _myStatusOnline = nw;
9615             return nw;
9616         }
9617
9618         public async Task OpenUriAsync(Uri uri, bool isReverseSettings = false)
9619         {
9620             var uriStr = uri.AbsoluteUri;
9621
9622             // OpenTween 内部で使用する URL
9623             if (uri.Authority == "opentween")
9624             {
9625                 await this.OpenInternalUriAsync(uri);
9626                 return;
9627             }
9628
9629             // ハッシュタグを含む Twitter 検索
9630             if (uri.Host == "twitter.com" && uri.AbsolutePath == "/search" && uri.Query.Contains("q=%23"))
9631             {
9632                 // ハッシュタグの場合は、タブで開く
9633                 var unescapedQuery = Uri.UnescapeDataString(uri.Query);
9634                 var pos = unescapedQuery.IndexOf('#');
9635                 if (pos == -1) return;
9636
9637                 var hash = unescapedQuery.Substring(pos);
9638                 this.HashSupl.AddItem(hash);
9639                 this.HashMgr.AddHashToHistory(hash.Trim(), false);
9640                 this.AddNewTabForSearch(hash);
9641                 return;
9642             }
9643
9644             // ユーザープロフィールURL
9645             // フラグが立っている場合は設定と逆の動作をする
9646             if( SettingManager.Common.OpenUserTimeline && !isReverseSettings ||
9647                 !SettingManager.Common.OpenUserTimeline && isReverseSettings )
9648             {
9649                 var userUriMatch = Regex.Match(uriStr, "^https?://twitter.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)$");
9650                 if (userUriMatch.Success)
9651                 {
9652                     var screenName = userUriMatch.Groups["ScreenName"].Value;
9653                     if (this.IsTwitterId(screenName))
9654                     {
9655                         await this.AddNewTabForUserTimeline(screenName);
9656                         return;
9657                     }
9658                 }
9659             }
9660
9661             // どのパターンにも該当しないURL
9662             await this.OpenUriInBrowserAsync(uriStr);
9663         }
9664
9665         /// <summary>
9666         /// OpenTween 内部の機能を呼び出すための URL を開きます
9667         /// </summary>
9668         private async Task OpenInternalUriAsync(Uri uri)
9669         {
9670             // ツイートを開く (//opentween/status/:status_id)
9671             var match = Regex.Match(uri.AbsolutePath, @"^/status/(\d+)$");
9672             if (match.Success)
9673             {
9674                 var statusId = long.Parse(match.Groups[1].Value);
9675                 await this.OpenRelatedTab(statusId);
9676                 return;
9677             }
9678         }
9679
9680         public Task OpenUriInBrowserAsync(string UriString)
9681         {
9682             return Task.Run(() =>
9683             {
9684                 string myPath = UriString;
9685
9686                 try
9687                 {
9688                     var configBrowserPath = SettingManager.Local.BrowserPath;
9689                     if (!string.IsNullOrEmpty(configBrowserPath))
9690                     {
9691                         if (configBrowserPath.StartsWith("\"", StringComparison.Ordinal) && configBrowserPath.Length > 2 && configBrowserPath.IndexOf("\"", 2, StringComparison.Ordinal) > -1)
9692                         {
9693                             int sep = configBrowserPath.IndexOf("\"", 2, StringComparison.Ordinal);
9694                             string browserPath = configBrowserPath.Substring(1, sep - 1);
9695                             string arg = "";
9696                             if (sep < configBrowserPath.Length - 1)
9697                             {
9698                                 arg = configBrowserPath.Substring(sep + 1);
9699                             }
9700                             myPath = arg + " " + myPath;
9701                             System.Diagnostics.Process.Start(browserPath, myPath);
9702                         }
9703                         else
9704                         {
9705                             System.Diagnostics.Process.Start(configBrowserPath, myPath);
9706                         }
9707                     }
9708                     else
9709                     {
9710                         System.Diagnostics.Process.Start(myPath);
9711                     }
9712                 }
9713                 catch (Exception)
9714                 {
9715                     //MessageBox.Show("ブラウザの起動に失敗、またはタイムアウトしました。" + ex.ToString());
9716                 }
9717             });
9718         }
9719
9720         private void ListTabSelect(TabPage _tab)
9721         {
9722             SetListProperty();
9723
9724             this.PurgeListViewItemCache();
9725
9726             _curTab = _tab;
9727             _curList = (DetailsListView)_tab.Tag;
9728
9729             if (_curList.SelectedIndices.Count > 0)
9730             {
9731                 _curItemIndex = _curList.SelectedIndices[0];
9732                 _curPost = GetCurTabPost(_curItemIndex);
9733             }
9734             else
9735             {
9736                 _curItemIndex = -1;
9737                 _curPost = null;
9738             }
9739
9740             _anchorPost = null;
9741             _anchorFlag = false;
9742
9743             if (_iconCol)
9744             {
9745                 ((DetailsListView)_tab.Tag).Columns[1].Text = ColumnText[2];
9746             }
9747             else
9748             {
9749                 for (int i = 0; i < _curList.Columns.Count; i++)
9750                 {
9751                     ((DetailsListView)_tab.Tag).Columns[i].Text = ColumnText[i];
9752                 }
9753             }
9754         }
9755
9756         private void ListTab_Selecting(object sender, TabControlCancelEventArgs e)
9757             => this.ListTabSelect(e.TabPage);
9758
9759         private void SelectListItem(DetailsListView LView, int Index)
9760         {
9761             //単一
9762             Rectangle bnd = new Rectangle();
9763             bool flg = false;
9764             var item = LView.FocusedItem;
9765             if (item != null)
9766             {
9767                 bnd = item.Bounds;
9768                 flg = true;
9769             }
9770
9771             do
9772             {
9773                 LView.SelectedIndices.Clear();
9774             }
9775             while (LView.SelectedIndices.Count > 0);
9776             item = LView.Items[Index];
9777             item.Selected = true;
9778             item.Focused = true;
9779
9780             if (flg) LView.Invalidate(bnd);
9781         }
9782
9783         private void SelectListItem(DetailsListView LView , int[] Index, int focusedIndex, int selectionMarkIndex)
9784         {
9785             //複数
9786             Rectangle bnd = new Rectangle();
9787             bool flg = false;
9788             var item = LView.FocusedItem;
9789             if (item != null)
9790             {
9791                 bnd = item.Bounds;
9792                 flg = true;
9793             }
9794
9795             if (Index != null)
9796             {
9797                 do
9798                 {
9799                     LView.SelectedIndices.Clear();
9800                 }
9801                 while (LView.SelectedIndices.Count > 0);
9802                 LView.SelectItems(Index);
9803             }
9804             if (selectionMarkIndex > -1 && LView.VirtualListSize > selectionMarkIndex)
9805             {
9806                 LView.SelectionMark = selectionMarkIndex;
9807             }
9808             if (focusedIndex > -1 && LView.VirtualListSize > focusedIndex)
9809             {
9810                 LView.Items[focusedIndex].Focused = true;
9811             }
9812             else if (Index != null && Index.Length != 0)
9813             {
9814                 LView.Items[Index.Last()].Focused = true;
9815             }
9816
9817             if (flg) LView.Invalidate(bnd);
9818         }
9819
9820         private void StartUserStream()
9821         {
9822             tw.NewPostFromStream += tw_NewPostFromStream;
9823             tw.UserStreamStarted += tw_UserStreamStarted;
9824             tw.UserStreamStopped += tw_UserStreamStopped;
9825             tw.PostDeleted += tw_PostDeleted;
9826             tw.UserStreamEventReceived += tw_UserStreamEventArrived;
9827
9828             this.RefreshUserStreamsMenu();
9829
9830             if (SettingManager.Common.UserstreamStartup)
9831                 tw.StartUserStream();
9832         }
9833
9834         private async void TweenMain_Shown(object sender, EventArgs e)
9835         {
9836             NotifyIcon1.Visible = true;
9837
9838             if (this.IsNetworkAvailable())
9839             {
9840                 StartUserStream();
9841
9842                 var loadTasks = new List<Task>
9843                 {
9844                     this.RefreshMuteUserIdsAsync(),
9845                     this.RefreshBlockIdsAsync(),
9846                     this.RefreshNoRetweetIdsAsync(),
9847                     this.RefreshTwitterConfigurationAsync(),
9848                     this.RefreshTabAsync<HomeTabModel>(),
9849                     this.RefreshTabAsync<MentionsTabModel>(),
9850                     this.RefreshTabAsync<DirectMessagesTabModel>(),
9851                     this.RefreshTabAsync<PublicSearchTabModel>(),
9852                     this.RefreshTabAsync<UserTimelineTabModel>(),
9853                     this.RefreshTabAsync<ListTimelineTabModel>(),
9854                 };
9855
9856                 if (SettingManager.Common.StartupFollowers)
9857                     loadTasks.Add(this.RefreshFollowerIdsAsync());
9858
9859                 if (SettingManager.Common.GetFav)
9860                     loadTasks.Add(this.RefreshTabAsync<FavoritesTabModel>());
9861
9862                 var allTasks = Task.WhenAll(loadTasks);
9863
9864                 var i = 0;
9865                 while (true)
9866                 {
9867                     var timeout = Task.Delay(5000);
9868                     if (await Task.WhenAny(allTasks, timeout) != timeout)
9869                         break;
9870
9871                     i += 1;
9872                     if (i > 24) break; // 120秒間初期処理が終了しなかったら強制的に打ち切る
9873
9874                     if (MyCommon._endingFlag)
9875                         return;
9876                 }
9877
9878                 if (MyCommon._endingFlag) return;
9879
9880                 if (ApplicationSettings.VersionInfoUrl != null)
9881                 {
9882                     //バージョンチェック(引数:起動時チェックの場合はtrue・・・チェック結果のメッセージを表示しない)
9883                     if (SettingManager.Common.StartupVersion)
9884                         await this.CheckNewVersion(true);
9885                 }
9886                 else
9887                 {
9888                     // ApplicationSetting.cs の設定により更新チェックが無効化されている場合
9889                     this.VerUpMenuItem.Enabled = false;
9890                     this.VerUpMenuItem.Available = false;
9891                     this.ToolStripSeparator16.Available = false; // VerUpMenuItem の一つ上にあるセパレータ
9892                 }
9893
9894                 // 権限チェック read/write権限(xAuthで取得したトークン)の場合は再認証を促す
9895                 if (MyCommon.TwitterApiInfo.AccessLevel == TwitterApiAccessLevel.ReadWrite)
9896                 {
9897                     MessageBox.Show(Properties.Resources.ReAuthorizeText);
9898                     SettingStripMenuItem_Click(null, null);
9899                 }
9900
9901                 // 取得失敗の場合は再試行する
9902                 var reloadTasks = new List<Task>();
9903
9904                 if (!tw.GetFollowersSuccess && SettingManager.Common.StartupFollowers)
9905                     reloadTasks.Add(this.RefreshFollowerIdsAsync());
9906
9907                 if (!tw.GetNoRetweetSuccess)
9908                     reloadTasks.Add(this.RefreshNoRetweetIdsAsync());
9909
9910                 if (this.tw.Configuration.PhotoSizeLimit == 0)
9911                     reloadTasks.Add(this.RefreshTwitterConfigurationAsync());
9912
9913                 await Task.WhenAll(reloadTasks);
9914             }
9915
9916             _initial = false;
9917
9918             TimerTimeline.Enabled = true;
9919         }
9920
9921         private async Task doGetFollowersMenu()
9922         {
9923             await this.RefreshFollowerIdsAsync();
9924             await this.DispSelectedPost(true);
9925         }
9926
9927         private async void GetFollowersAllToolStripMenuItem_Click(object sender, EventArgs e)
9928             => await this.doGetFollowersMenu();
9929
9930         private void ReTweetUnofficialStripMenuItem_Click(object sender, EventArgs e)
9931             => this.doReTweetUnofficial();
9932
9933         private async Task doReTweetOfficial(bool isConfirm)
9934         {
9935             //公式RT
9936             if (this.ExistCurrentPost)
9937             {
9938                 if (!_curPost.CanRetweetBy(this.twitterApi.CurrentUserId))
9939                 {
9940                     if (this._curPost.IsProtect)
9941                         MessageBox.Show("Protected.");
9942
9943                     _DoFavRetweetFlags = false;
9944                     return;
9945                 }
9946                 if (_curList.SelectedIndices.Count > 15)
9947                 {
9948                     MessageBox.Show(Properties.Resources.RetweetLimitText);
9949                     _DoFavRetweetFlags = false;
9950                     return;
9951                 }
9952                 else if (_curList.SelectedIndices.Count > 1)
9953                 {
9954                     string QuestionText = Properties.Resources.RetweetQuestion2;
9955                     if (_DoFavRetweetFlags) QuestionText = Properties.Resources.FavoriteRetweetQuestionText1;
9956                     switch (MessageBox.Show(QuestionText, "Retweet", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
9957                     {
9958                         case DialogResult.Cancel:
9959                         case DialogResult.No:
9960                             _DoFavRetweetFlags = false;
9961                             return;
9962                     }
9963                 }
9964                 else
9965                 {
9966                     if (!SettingManager.Common.RetweetNoConfirm)
9967                     {
9968                         string Questiontext = Properties.Resources.RetweetQuestion1;
9969                         if (_DoFavRetweetFlags) Questiontext = Properties.Resources.FavoritesRetweetQuestionText2;
9970                         if (isConfirm && MessageBox.Show(Questiontext, "Retweet", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
9971                         {
9972                             _DoFavRetweetFlags = false;
9973                             return;
9974                         }
9975                     }
9976                 }
9977
9978                 var statusIds = new List<long>();
9979                 foreach (int idx in _curList.SelectedIndices)
9980                 {
9981                     PostClass post = GetCurTabPost(idx);
9982                     if (post.CanRetweetBy(this.twitterApi.CurrentUserId))
9983                         statusIds.Add(post.StatusId);
9984                 }
9985
9986                 await this.RetweetAsync(statusIds);
9987             }
9988         }
9989
9990         private async void ReTweetStripMenuItem_Click(object sender, EventArgs e)
9991             => await this.doReTweetOfficial(true);
9992
9993         private async Task FavoritesRetweetOfficial()
9994         {
9995             if (!this.ExistCurrentPost) return;
9996             _DoFavRetweetFlags = true;
9997             var retweetTask = this.doReTweetOfficial(true);
9998             if (_DoFavRetweetFlags)
9999             {
10000                 _DoFavRetweetFlags = false;
10001                 var favoriteTask = this.FavoriteChange(true, false);
10002
10003                 await Task.WhenAll(retweetTask, favoriteTask);
10004             }
10005             else
10006             {
10007                 await retweetTask;
10008             }
10009         }
10010
10011         private async Task FavoritesRetweetUnofficial()
10012         {
10013             if (this.ExistCurrentPost && !_curPost.IsDm)
10014             {
10015                 _DoFavRetweetFlags = true;
10016                 var favoriteTask = this.FavoriteChange(true);
10017                 if (!_curPost.IsProtect && _DoFavRetweetFlags)
10018                 {
10019                     _DoFavRetweetFlags = false;
10020                     doReTweetUnofficial();
10021                 }
10022
10023                 await favoriteTask;
10024             }
10025         }
10026
10027         /// <summary>
10028         /// TweetFormatterクラスによって整形された状態のHTMLを、非公式RT用に元のツイートに復元します
10029         /// </summary>
10030         /// <param name="statusHtml">TweetFormatterによって整形された状態のHTML</param>
10031         /// <param name="multiline">trueであればBRタグを改行に、falseであればスペースに変換します</param>
10032         /// <returns>復元されたツイート本文</returns>
10033         internal static string CreateRetweetUnofficial(string statusHtml, bool multiline)
10034         {
10035             // TweetFormatterクラスによって整形された状態のHTMLを元のツイートに復元します
10036
10037             // 通常の URL
10038             statusHtml = Regex.Replace(statusHtml, "<a href=\"(?<href>.+?)\" title=\"(?<title>.+?)\">(?<text>.+?)</a>", "${title}");
10039             // メンション
10040             statusHtml = Regex.Replace(statusHtml, "<a class=\"mention\" href=\"(?<href>.+?)\">(?<text>.+?)</a>", "${text}");
10041             // ハッシュタグ
10042             statusHtml = Regex.Replace(statusHtml, "<a class=\"hashtag\" href=\"(?<href>.+?)\">(?<text>.+?)</a>", "${text}");
10043             // 絵文字
10044             statusHtml = Regex.Replace(statusHtml, "<img class=\"emoji\" src=\".+?\" alt=\"(?<text>.+?)\" />", "${text}");
10045
10046             // <br> 除去
10047             if (multiline)
10048                 statusHtml = statusHtml.Replace("<br>", Environment.NewLine);
10049             else
10050                 statusHtml = statusHtml.Replace("<br>", " ");
10051
10052             // &nbsp; は本来であれば U+00A0 (NON-BREAK SPACE) に置換すべきですが、
10053             // 現状では半角スペースの代用として &nbsp; を使用しているため U+0020 に置換します
10054             statusHtml = statusHtml.Replace("&nbsp;", " ");
10055
10056             return WebUtility.HtmlDecode(statusHtml);
10057         }
10058
10059         private async void DumpPostClassToolStripMenuItem_Click(object sender, EventArgs e)
10060         {
10061             this.tweetDetailsView.DumpPostClass = this.DumpPostClassToolStripMenuItem.Checked;
10062
10063             if (_curPost != null)
10064                 await this.DispSelectedPost(true);
10065         }
10066
10067         private void MenuItemHelp_DropDownOpening(object sender, EventArgs e)
10068         {
10069             if (MyCommon.DebugBuild || MyCommon.IsKeyDown(Keys.CapsLock, Keys.Control, Keys.Shift))
10070                 DebugModeToolStripMenuItem.Visible = true;
10071             else
10072                 DebugModeToolStripMenuItem.Visible = false;
10073         }
10074
10075         private void UrlMultibyteSplitMenuItem_CheckedChanged(object sender, EventArgs e)
10076             => this.urlMultibyteSplit = ((ToolStripMenuItem)sender).Checked;
10077
10078         private void PreventSmsCommandMenuItem_CheckedChanged(object sender, EventArgs e)
10079             => this.preventSmsCommand = ((ToolStripMenuItem)sender).Checked;
10080
10081         private void UrlAutoShortenMenuItem_CheckedChanged(object sender, EventArgs e)
10082             => SettingManager.Common.UrlConvertAuto = ((ToolStripMenuItem)sender).Checked;
10083
10084         private void IdeographicSpaceToSpaceMenuItem_Click(object sender, EventArgs e)
10085         {
10086             SettingManager.Common.WideSpaceConvert = ((ToolStripMenuItem)sender).Checked;
10087             ModifySettingCommon = true;
10088         }
10089
10090         private void FocusLockMenuItem_CheckedChanged(object sender, EventArgs e)
10091         {
10092             SettingManager.Common.FocusLockToStatusText = ((ToolStripMenuItem)sender).Checked;
10093             ModifySettingCommon = true;
10094         }
10095
10096         private void PostModeMenuItem_DropDownOpening(object sender, EventArgs e)
10097         {
10098             UrlMultibyteSplitMenuItem.Checked = this.urlMultibyteSplit;
10099             PreventSmsCommandMenuItem.Checked = this.preventSmsCommand;
10100             UrlAutoShortenMenuItem.Checked = SettingManager.Common.UrlConvertAuto;
10101             IdeographicSpaceToSpaceMenuItem.Checked = SettingManager.Common.WideSpaceConvert;
10102             MultiLineMenuItem.Checked = SettingManager.Local.StatusMultiline;
10103             FocusLockMenuItem.Checked = SettingManager.Common.FocusLockToStatusText;
10104         }
10105
10106         private void ContextMenuPostMode_Opening(object sender, CancelEventArgs e)
10107         {
10108             UrlMultibyteSplitPullDownMenuItem.Checked = this.urlMultibyteSplit;
10109             PreventSmsCommandPullDownMenuItem.Checked = this.preventSmsCommand;
10110             UrlAutoShortenPullDownMenuItem.Checked = SettingManager.Common.UrlConvertAuto;
10111             IdeographicSpaceToSpacePullDownMenuItem.Checked = SettingManager.Common.WideSpaceConvert;
10112             MultiLinePullDownMenuItem.Checked = SettingManager.Local.StatusMultiline;
10113             FocusLockPullDownMenuItem.Checked = SettingManager.Common.FocusLockToStatusText;
10114         }
10115
10116         private void TraceOutToolStripMenuItem_Click(object sender, EventArgs e)
10117         {
10118             if (TraceOutToolStripMenuItem.Checked)
10119                 MyCommon.TraceFlag = true;
10120             else
10121                 MyCommon.TraceFlag = false;
10122         }
10123
10124         private void TweenMain_Deactivate(object sender, EventArgs e)
10125             => this.StatusText_Leave(StatusText, EventArgs.Empty); // 画面が非アクティブになったら、発言欄の背景色をデフォルトへ
10126
10127         private void TabRenameMenuItem_Click(object sender, EventArgs e)
10128         {
10129             if (string.IsNullOrEmpty(_rclickTabName)) return;
10130
10131             TabRename(_rclickTabName, out var _);
10132         }
10133
10134         private async void BitlyToolStripMenuItem_Click(object sender, EventArgs e)
10135             => await this.UrlConvertAsync(MyCommon.UrlConverter.Bitly);
10136
10137         private async void JmpToolStripMenuItem_Click(object sender, EventArgs e)
10138             => await this.UrlConvertAsync(MyCommon.UrlConverter.Jmp);
10139
10140         private async void ApiUsageInfoMenuItem_Click(object sender, EventArgs e)
10141         {
10142             TwitterApiStatus apiStatus;
10143
10144             using (var dialog = new WaitingDialog(Properties.Resources.ApiInfo6))
10145             {
10146                 var cancellationToken = dialog.EnableCancellation();
10147
10148                 try
10149                 {
10150                     var task = this.tw.GetInfoApi();
10151                     apiStatus = await dialog.WaitForAsync(this, task);
10152                 }
10153                 catch (WebApiException)
10154                 {
10155                     apiStatus = null;
10156                 }
10157
10158                 if (cancellationToken.IsCancellationRequested)
10159                     return;
10160
10161                 if (apiStatus == null)
10162                 {
10163                     MessageBox.Show(Properties.Resources.ApiInfo5, Properties.Resources.ApiInfo4, MessageBoxButtons.OK, MessageBoxIcon.Information);
10164                     return;
10165                 }
10166             }
10167
10168             using (var apiDlg = new ApiInfoDialog())
10169             {
10170                 apiDlg.ShowDialog(this);
10171             }
10172         }
10173
10174         private async void FollowCommandMenuItem_Click(object sender, EventArgs e)
10175         {
10176             var id = _curPost?.ScreenName ?? "";
10177
10178             await this.FollowCommand(id);
10179         }
10180
10181         internal async Task FollowCommand(string id)
10182         {
10183             using (var inputName = new InputTabName())
10184             {
10185                 inputName.FormTitle = "Follow";
10186                 inputName.FormDescription = Properties.Resources.FRMessage1;
10187                 inputName.TabName = id;
10188
10189                 if (inputName.ShowDialog(this) != DialogResult.OK)
10190                     return;
10191                 if (string.IsNullOrWhiteSpace(inputName.TabName))
10192                     return;
10193
10194                 id = inputName.TabName.Trim();
10195             }
10196
10197             using (var dialog = new WaitingDialog(Properties.Resources.FollowCommandText1))
10198             {
10199                 try
10200                 {
10201                     var task = this.twitterApi.FriendshipsCreate(id).IgnoreResponse();
10202                     await dialog.WaitForAsync(this, task);
10203                 }
10204                 catch (WebApiException ex)
10205                 {
10206                     MessageBox.Show(Properties.Resources.FRMessage2 + ex.Message);
10207                     return;
10208                 }
10209             }
10210
10211             MessageBox.Show(Properties.Resources.FRMessage3);
10212         }
10213
10214         private async void RemoveCommandMenuItem_Click(object sender, EventArgs e)
10215         {
10216             var id = _curPost?.ScreenName ?? "";
10217
10218             await this.RemoveCommand(id, false);
10219         }
10220
10221         internal async Task RemoveCommand(string id, bool skipInput)
10222         {
10223             if (!skipInput)
10224             {
10225                 using (var inputName = new InputTabName())
10226                 {
10227                     inputName.FormTitle = "Unfollow";
10228                     inputName.FormDescription = Properties.Resources.FRMessage1;
10229                     inputName.TabName = id;
10230
10231                     if (inputName.ShowDialog(this) != DialogResult.OK)
10232                         return;
10233                     if (string.IsNullOrWhiteSpace(inputName.TabName))
10234                         return;
10235
10236                     id = inputName.TabName.Trim();
10237                 }
10238             }
10239
10240             using (var dialog = new WaitingDialog(Properties.Resources.RemoveCommandText1))
10241             {
10242                 try
10243                 {
10244                     var task = this.twitterApi.FriendshipsDestroy(id).IgnoreResponse();
10245                     await dialog.WaitForAsync(this, task);
10246                 }
10247                 catch (WebApiException ex)
10248                 {
10249                     MessageBox.Show(Properties.Resources.FRMessage2 + ex.Message);
10250                     return;
10251                 }
10252             }
10253
10254             MessageBox.Show(Properties.Resources.FRMessage3);
10255         }
10256
10257         private async void FriendshipMenuItem_Click(object sender, EventArgs e)
10258         {
10259             var id = _curPost?.ScreenName ?? "";
10260
10261             await this.ShowFriendship(id);
10262         }
10263
10264         internal async Task ShowFriendship(string id)
10265         {
10266             using (var inputName = new InputTabName())
10267             {
10268                 inputName.FormTitle = "Show Friendships";
10269                 inputName.FormDescription = Properties.Resources.FRMessage1;
10270                 inputName.TabName = id;
10271
10272                 if (inputName.ShowDialog(this) != DialogResult.OK)
10273                     return;
10274                 if (string.IsNullOrWhiteSpace(inputName.TabName))
10275                     return;
10276
10277                 id = inputName.TabName.Trim();
10278             }
10279
10280             bool isFollowing, isFollowed;
10281
10282             using (var dialog = new WaitingDialog(Properties.Resources.ShowFriendshipText1))
10283             {
10284                 var cancellationToken = dialog.EnableCancellation();
10285
10286                 try
10287                 {
10288                     var task = this.twitterApi.FriendshipsShow(this.twitterApi.CurrentScreenName, id);
10289                     var friendship = await dialog.WaitForAsync(this, task);
10290
10291                     isFollowing = friendship.Relationship.Source.Following;
10292                     isFollowed = friendship.Relationship.Source.FollowedBy;
10293                 }
10294                 catch (WebApiException ex)
10295                 {
10296                     if (!cancellationToken.IsCancellationRequested)
10297                         MessageBox.Show($"Err:{ex.Message}(FriendshipsShow)");
10298                     return;
10299                 }
10300
10301                 if (cancellationToken.IsCancellationRequested)
10302                     return;
10303             }
10304
10305             string result = "";
10306             if (isFollowing)
10307             {
10308                 result = Properties.Resources.GetFriendshipInfo1 + System.Environment.NewLine;
10309             }
10310             else
10311             {
10312                 result = Properties.Resources.GetFriendshipInfo2 + System.Environment.NewLine;
10313             }
10314             if (isFollowed)
10315             {
10316                 result += Properties.Resources.GetFriendshipInfo3;
10317             }
10318             else
10319             {
10320                 result += Properties.Resources.GetFriendshipInfo4;
10321             }
10322             result = id + Properties.Resources.GetFriendshipInfo5 + System.Environment.NewLine + result;
10323             MessageBox.Show(result);
10324         }
10325
10326         internal async Task ShowFriendship(string[] ids)
10327         {
10328             foreach (string id in ids)
10329             {
10330                 bool isFollowing, isFollowed;
10331
10332                 using (var dialog = new WaitingDialog(Properties.Resources.ShowFriendshipText1))
10333                 {
10334                     var cancellationToken = dialog.EnableCancellation();
10335
10336                     try
10337                     {
10338                         var task = this.twitterApi.FriendshipsShow(this.twitterApi.CurrentScreenName, id);
10339                         var friendship = await dialog.WaitForAsync(this, task);
10340
10341                         isFollowing = friendship.Relationship.Source.Following;
10342                         isFollowed = friendship.Relationship.Source.FollowedBy;
10343                     }
10344                     catch (WebApiException ex)
10345                     {
10346                         if (!cancellationToken.IsCancellationRequested)
10347                             MessageBox.Show($"Err:{ex.Message}(FriendshipsShow)");
10348                         return;
10349                     }
10350
10351                     if (cancellationToken.IsCancellationRequested)
10352                         return;
10353                 }
10354
10355                 string result = "";
10356                 string ff = "";
10357
10358                 ff = "  ";
10359                 if (isFollowing)
10360                 {
10361                     ff += Properties.Resources.GetFriendshipInfo1;
10362                 }
10363                 else
10364                 {
10365                     ff += Properties.Resources.GetFriendshipInfo2;
10366                 }
10367
10368                 ff += System.Environment.NewLine + "  ";
10369                 if (isFollowed)
10370                 {
10371                     ff += Properties.Resources.GetFriendshipInfo3;
10372                 }
10373                 else
10374                 {
10375                     ff += Properties.Resources.GetFriendshipInfo4;
10376                 }
10377                 result += id + Properties.Resources.GetFriendshipInfo5 + System.Environment.NewLine + ff;
10378                 if (isFollowing)
10379                 {
10380                     if (MessageBox.Show(
10381                         Properties.Resources.GetFriendshipInfo7 + System.Environment.NewLine + result, Properties.Resources.GetFriendshipInfo8,
10382                         MessageBoxButtons.YesNo,
10383                         MessageBoxIcon.Question,
10384                         MessageBoxDefaultButton.Button2) == DialogResult.Yes)
10385                     {
10386                         await this.RemoveCommand(id, true);
10387                     }
10388                 }
10389                 else
10390                 {
10391                     MessageBox.Show(result);
10392                 }
10393             }
10394         }
10395
10396         private async void OwnStatusMenuItem_Click(object sender, EventArgs e)
10397             => await this.doShowUserStatus(tw.Username, false);
10398
10399         // TwitterIDでない固定文字列を調べる(文字列検証のみ 実際に取得はしない)
10400         // URLから切り出した文字列を渡す
10401
10402         public bool IsTwitterId(string name)
10403         {
10404             if (this.tw.Configuration.NonUsernamePaths == null || this.tw.Configuration.NonUsernamePaths.Length == 0)
10405                 return !Regex.Match(name, @"^(about|jobs|tos|privacy|who_to_follow|download|messages)$", RegexOptions.IgnoreCase).Success;
10406             else
10407                 return !this.tw.Configuration.NonUsernamePaths.Contains(name, StringComparer.InvariantCultureIgnoreCase);
10408         }
10409
10410         private void doQuoteOfficial()
10411         {
10412             if (this.ExistCurrentPost)
10413             {
10414                 if (_curPost.IsDm ||
10415                     !StatusText.Enabled) return;
10416
10417                 if (_curPost.IsProtect)
10418                 {
10419                     MessageBox.Show("Protected.");
10420                     return;
10421                 }
10422
10423                 var selection = (this.StatusText.SelectionStart, this.StatusText.SelectionLength);
10424
10425                 this.inReplyTo = null;
10426
10427                 StatusText.Text += " " + MyCommon.GetStatusUrl(_curPost);
10428
10429                 (this.StatusText.SelectionStart, this.StatusText.SelectionLength) = selection;
10430                 StatusText.Focus();
10431             }
10432         }
10433
10434         private void doReTweetUnofficial()
10435         {
10436             //RT @id:内容
10437             if (this.ExistCurrentPost)
10438             {
10439                 if (_curPost.IsDm || !StatusText.Enabled)
10440                     return;
10441
10442                 if (_curPost.IsProtect)
10443                 {
10444                     MessageBox.Show("Protected.");
10445                     return;
10446                 }
10447                 string rtdata = _curPost.Text;
10448                 rtdata = CreateRetweetUnofficial(rtdata, this.StatusText.Multiline);
10449
10450                 var selection = (this.StatusText.SelectionStart, this.StatusText.SelectionLength);
10451
10452                 // 投稿時に in_reply_to_status_id を付加する
10453                 var inReplyToStatusId = this._curPost.RetweetedId ?? this._curPost.StatusId;
10454                 var inReplyToScreenName = this._curPost.ScreenName;
10455                 this.inReplyTo = (inReplyToStatusId, inReplyToScreenName);
10456
10457                 StatusText.Text += " RT @" + _curPost.ScreenName + ": " + rtdata;
10458
10459                 (this.StatusText.SelectionStart, this.StatusText.SelectionLength) = selection;
10460                 StatusText.Focus();
10461             }
10462         }
10463
10464         private void QuoteStripMenuItem_Click(object sender, EventArgs e)
10465             => this.doQuoteOfficial();
10466
10467         private async void SearchButton_Click(object sender, EventArgs e)
10468         {
10469             //公式検索
10470             Control pnl = ((Control)sender).Parent;
10471             if (pnl == null) return;
10472             string tbName = pnl.Parent.Text;
10473             var tb = (PublicSearchTabModel)_statuses.Tabs[tbName];
10474             ComboBox cmb = (ComboBox)pnl.Controls["comboSearch"];
10475             ComboBox cmbLang = (ComboBox)pnl.Controls["comboLang"];
10476             cmb.Text = cmb.Text.Trim();
10477             // 検索式演算子 OR についてのみ大文字しか認識しないので強制的に大文字とする
10478             bool Quote = false;
10479             StringBuilder buf = new StringBuilder();
10480             char[] c = cmb.Text.ToCharArray();
10481             for (int cnt = 0; cnt < cmb.Text.Length; cnt++)
10482             {
10483                 if (cnt > cmb.Text.Length - 4)
10484                 {
10485                     buf.Append(cmb.Text.Substring(cnt));
10486                     break;
10487                 }
10488                 if (c[cnt] == '"')
10489                 {
10490                     Quote = !Quote;
10491                 }
10492                 else
10493                 {
10494                     if (!Quote && cmb.Text.Substring(cnt, 4).Equals(" or ", StringComparison.OrdinalIgnoreCase))
10495                     {
10496                         buf.Append(" OR ");
10497                         cnt += 3;
10498                         continue;
10499                     }
10500                 }
10501                 buf.Append(c[cnt]);
10502             }
10503             cmb.Text = buf.ToString();
10504
10505             var listView = (DetailsListView)pnl.Parent.Tag;
10506
10507             var queryChanged = tb.SearchWords != cmb.Text || tb.SearchLang != cmbLang.Text;
10508
10509             tb.SearchWords = cmb.Text;
10510             tb.SearchLang = cmbLang.Text;
10511             if (string.IsNullOrEmpty(cmb.Text))
10512             {
10513                 listView.Focus();
10514                 SaveConfigsTabs();
10515                 return;
10516             }
10517             if (queryChanged)
10518             {
10519                 int idx = cmb.Items.IndexOf(tb.SearchWords);
10520                 if (idx > -1) cmb.Items.RemoveAt(idx);
10521                 cmb.Items.Insert(0, tb.SearchWords);
10522                 cmb.Text = tb.SearchWords;
10523                 cmb.SelectAll();
10524                 this.PurgeListViewItemCache();
10525                 listView.VirtualListSize = 0;
10526                 _statuses.ClearTabIds(tbName);
10527                 SaveConfigsTabs();   //検索条件の保存
10528             }
10529
10530             listView.Focus();
10531             await this.RefreshTabAsync(tb);
10532         }
10533
10534         private async void RefreshMoreStripMenuItem_Click(object sender, EventArgs e)
10535             => await this.DoRefreshMore(); // もっと前を取得
10536
10537         /// <summary>
10538         /// 指定されたタブのListTabにおける位置を返します
10539         /// </summary>
10540         /// <remarks>
10541         /// 非表示のタブについて -1 が返ることを常に考慮して下さい
10542         /// </remarks>
10543         public int GetTabPageIndex(string tabName)
10544         {
10545             var index = 0;
10546             foreach (var tabPage in this.ListTab.TabPages.Cast<TabPage>())
10547             {
10548                 if (tabPage.Text == tabName)
10549                     return index;
10550
10551                 index++;
10552             }
10553
10554             return -1;
10555         }
10556
10557         private void UndoRemoveTabMenuItem_Click(object sender, EventArgs e)
10558         {
10559             if (_statuses.RemovedTab.Count == 0)
10560             {
10561                 MessageBox.Show("There isn't removed tab.", "Undo", MessageBoxButtons.OK, MessageBoxIcon.Information);
10562                 return;
10563             }
10564             else
10565             {
10566                 DetailsListView listView = null;
10567
10568                 TabModel tb = _statuses.RemovedTab.Pop();
10569                 if (tb.TabType == MyCommon.TabUsageType.Related)
10570                 {
10571                     var relatedTab = _statuses.GetTabByType(MyCommon.TabUsageType.Related);
10572                     if (relatedTab != null)
10573                     {
10574                         // 関連発言なら既存のタブを置き換える
10575                         tb.TabName = relatedTab.TabName;
10576                         this.ClearTab(tb.TabName, false);
10577                         _statuses.Tabs[tb.TabName] = tb;
10578
10579                         for (int i = 0; i < ListTab.TabPages.Count; i++)
10580                         {
10581                             var tabPage = ListTab.TabPages[i];
10582                             if (tb.TabName == tabPage.Text)
10583                             {
10584                                 listView = (DetailsListView)tabPage.Tag;
10585                                 ListTab.SelectedIndex = i;
10586                                 break;
10587                             }
10588                         }
10589                     }
10590                     else
10591                     {
10592                         const string TabName = "Related Tweets";
10593                         string renamed = TabName;
10594                         for (int i = 2; i <= 100; i++)
10595                         {
10596                             if (!_statuses.ContainsTab(renamed)) break;
10597                             renamed = TabName + i;
10598                         }
10599                         tb.TabName = renamed;
10600
10601                         _statuses.AddTab(tb);
10602                         AddNewTab(tb, startup: false);
10603
10604                         var tabPage = ListTab.TabPages[ListTab.TabPages.Count - 1];
10605                         listView = (DetailsListView)tabPage.Tag;
10606                         ListTab.SelectedIndex = ListTab.TabPages.Count - 1;
10607                     }
10608                 }
10609                 else
10610                 {
10611                     string renamed = tb.TabName;
10612                     for (int i = 1; i < int.MaxValue; i++)
10613                     {
10614                         if (!_statuses.ContainsTab(renamed)) break;
10615                         renamed = tb.TabName + "(" + i + ")";
10616                     }
10617                     tb.TabName = renamed;
10618
10619                     _statuses.AddTab(tb);
10620                     AddNewTab(tb, startup: false);
10621
10622                     var tabPage = ListTab.TabPages[ListTab.TabPages.Count - 1];
10623                     listView = (DetailsListView)tabPage.Tag;
10624                     ListTab.SelectedIndex = ListTab.TabPages.Count - 1;
10625                 }
10626                 SaveConfigsTabs();
10627
10628                 if (listView != null)
10629                 {
10630                     using (ControlTransaction.Update(listView))
10631                     {
10632                         listView.VirtualListSize = tb.AllCount;
10633                     }
10634                 }
10635             }
10636         }
10637
10638         private async Task doMoveToRTHome()
10639         {
10640             if (_curList.SelectedIndices.Count > 0)
10641             {
10642                 PostClass post = GetCurTabPost(_curList.SelectedIndices[0]);
10643                 if (post.RetweetedId != null)
10644                 {
10645                     await this.OpenUriInBrowserAsync("https://twitter.com/" + GetCurTabPost(_curList.SelectedIndices[0]).RetweetedBy);
10646                 }
10647             }
10648         }
10649
10650         private async void MoveToRTHomeMenuItem_Click(object sender, EventArgs e)
10651             => await this.doMoveToRTHome();
10652
10653         private void ListManageUserContextToolStripMenuItem_Click(object sender, EventArgs e)
10654         {
10655             var screenName = this._curPost?.ScreenName;
10656             if (screenName != null)
10657                 this.ListManageUserContext(screenName);
10658         }
10659
10660         public void ListManageUserContext(string screenName)
10661         {
10662             using (var listSelectForm = new MyLists(screenName, this.twitterApi))
10663             {
10664                 listSelectForm.ShowDialog(this);
10665             }
10666         }
10667
10668         private void SearchControls_Enter(object sender, EventArgs e)
10669         {
10670             Control pnl = (Control)sender;
10671             foreach (Control ctl in pnl.Controls)
10672             {
10673                 ctl.TabStop = true;
10674             }
10675         }
10676
10677         private void SearchControls_Leave(object sender, EventArgs e)
10678         {
10679             Control pnl = (Control)sender;
10680             foreach (Control ctl in pnl.Controls)
10681             {
10682                 ctl.TabStop = false;
10683             }
10684         }
10685
10686         private void PublicSearchQueryMenuItem_Click(object sender, EventArgs e)
10687         {
10688             if (ListTab.SelectedTab != null)
10689             {
10690                 if (_statuses.Tabs[ListTab.SelectedTab.Text].TabType != MyCommon.TabUsageType.PublicSearch) return;
10691                 ListTab.SelectedTab.Controls["panelSearch"].Controls["comboSearch"].Focus();
10692             }
10693         }
10694
10695         private void StatusLabel_DoubleClick(object sender, EventArgs e)
10696             => MessageBox.Show(StatusLabel.TextHistory, "Logs", MessageBoxButtons.OK, MessageBoxIcon.None);
10697
10698         private void HashManageMenuItem_Click(object sender, EventArgs e)
10699         {
10700             DialogResult rslt = DialogResult.Cancel;
10701             try
10702             {
10703                 rslt = HashMgr.ShowDialog();
10704             }
10705             catch (Exception)
10706             {
10707                 return;
10708             }
10709             this.TopMost = SettingManager.Common.AlwaysTop;
10710             if (rslt == DialogResult.Cancel) return;
10711             if (!string.IsNullOrEmpty(HashMgr.UseHash))
10712             {
10713                 HashStripSplitButton.Text = HashMgr.UseHash;
10714                 HashTogglePullDownMenuItem.Checked = true;
10715                 HashToggleMenuItem.Checked = true;
10716             }
10717             else
10718             {
10719                 HashStripSplitButton.Text = "#[-]";
10720                 HashTogglePullDownMenuItem.Checked = false;
10721                 HashToggleMenuItem.Checked = false;
10722             }
10723             //if (HashMgr.IsInsert && HashMgr.UseHash != "")
10724             //{
10725             //    int sidx = StatusText.SelectionStart;
10726             //    string hash = HashMgr.UseHash + " ";
10727             //    if (sidx > 0)
10728             //    {
10729             //        if (StatusText.Text.Substring(sidx - 1, 1) != " ")
10730             //            hash = " " + hash;
10731             //    }
10732             //    StatusText.Text = StatusText.Text.Insert(sidx, hash);
10733             //    sidx += hash.Length;
10734             //    StatusText.SelectionStart = sidx;
10735             //    StatusText.Focus();
10736             //}
10737             ModifySettingCommon = true;
10738             this.StatusText_TextChanged(null, null);
10739         }
10740
10741         private void HashToggleMenuItem_Click(object sender, EventArgs e)
10742         {
10743             HashMgr.ToggleHash();
10744             if (!string.IsNullOrEmpty(HashMgr.UseHash))
10745             {
10746                 HashStripSplitButton.Text = HashMgr.UseHash;
10747                 HashToggleMenuItem.Checked = true;
10748                 HashTogglePullDownMenuItem.Checked = true;
10749             }
10750             else
10751             {
10752                 HashStripSplitButton.Text = "#[-]";
10753                 HashToggleMenuItem.Checked = false;
10754                 HashTogglePullDownMenuItem.Checked = false;
10755             }
10756             ModifySettingCommon = true;
10757             this.StatusText_TextChanged(null, null);
10758         }
10759
10760         private void HashStripSplitButton_ButtonClick(object sender, EventArgs e)
10761             => this.HashToggleMenuItem_Click(null, null);
10762
10763         public void SetPermanentHashtag(string hashtag)
10764         {
10765             HashMgr.SetPermanentHash("#" + hashtag);
10766             HashStripSplitButton.Text = HashMgr.UseHash;
10767             HashTogglePullDownMenuItem.Checked = true;
10768             HashToggleMenuItem.Checked = true;
10769             //使用ハッシュタグとして設定
10770             ModifySettingCommon = true;
10771         }
10772
10773         private void MenuItemOperate_DropDownOpening(object sender, EventArgs e)
10774         {
10775             if (ListTab.SelectedTab == null) return;
10776             if (_statuses == null || _statuses.Tabs == null || !_statuses.Tabs.ContainsKey(ListTab.SelectedTab.Text)) return;
10777             if (!this.ExistCurrentPost)
10778             {
10779                 this.ReplyOpMenuItem.Enabled = false;
10780                 this.ReplyAllOpMenuItem.Enabled = false;
10781                 this.DmOpMenuItem.Enabled = false;
10782                 this.ShowProfMenuItem.Enabled = false;
10783                 this.ShowUserTimelineToolStripMenuItem.Enabled = false;
10784                 this.ListManageMenuItem.Enabled = false;
10785                 this.OpenFavOpMenuItem.Enabled = false;
10786                 this.CreateTabRuleOpMenuItem.Enabled = false;
10787                 this.CreateIdRuleOpMenuItem.Enabled = false;
10788                 this.CreateSourceRuleOpMenuItem.Enabled = false;
10789                 this.ReadOpMenuItem.Enabled = false;
10790                 this.UnreadOpMenuItem.Enabled = false;
10791             }
10792             else
10793             {
10794                 this.ReplyOpMenuItem.Enabled = true;
10795                 this.ReplyAllOpMenuItem.Enabled = true;
10796                 this.DmOpMenuItem.Enabled = true;
10797                 this.ShowProfMenuItem.Enabled = true;
10798                 this.ShowUserTimelineToolStripMenuItem.Enabled = true;
10799                 this.ListManageMenuItem.Enabled = true;
10800                 this.OpenFavOpMenuItem.Enabled = true;
10801                 this.CreateTabRuleOpMenuItem.Enabled = true;
10802                 this.CreateIdRuleOpMenuItem.Enabled = true;
10803                 this.CreateSourceRuleOpMenuItem.Enabled = true;
10804                 this.ReadOpMenuItem.Enabled = true;
10805                 this.UnreadOpMenuItem.Enabled = true;
10806             }
10807
10808             if (_statuses.Tabs[ListTab.SelectedTab.Text].TabType == MyCommon.TabUsageType.DirectMessage || !this.ExistCurrentPost || _curPost.IsDm)
10809             {
10810                 this.FavOpMenuItem.Enabled = false;
10811                 this.UnFavOpMenuItem.Enabled = false;
10812                 this.OpenStatusOpMenuItem.Enabled = false;
10813                 this.ShowRelatedStatusesMenuItem2.Enabled = false;
10814                 this.RtOpMenuItem.Enabled = false;
10815                 this.RtUnOpMenuItem.Enabled = false;
10816                 this.QtOpMenuItem.Enabled = false;
10817                 this.FavoriteRetweetMenuItem.Enabled = false;
10818                 this.FavoriteRetweetUnofficialMenuItem.Enabled = false;
10819             }
10820             else
10821             {
10822                 this.FavOpMenuItem.Enabled = true;
10823                 this.UnFavOpMenuItem.Enabled = true;
10824                 this.OpenStatusOpMenuItem.Enabled = true;
10825                 this.ShowRelatedStatusesMenuItem2.Enabled = true;  //PublicSearchの時問題出るかも
10826
10827                 if (!_curPost.CanRetweetBy(this.twitterApi.CurrentUserId))
10828                 {
10829                     this.RtOpMenuItem.Enabled = false;
10830                     this.RtUnOpMenuItem.Enabled = false;
10831                     this.QtOpMenuItem.Enabled = false;
10832                     this.FavoriteRetweetMenuItem.Enabled = false;
10833                     this.FavoriteRetweetUnofficialMenuItem.Enabled = false;
10834                 }
10835                 else
10836                 {
10837                     this.RtOpMenuItem.Enabled = true;
10838                     this.RtUnOpMenuItem.Enabled = true;
10839                     this.QtOpMenuItem.Enabled = true;
10840                     this.FavoriteRetweetMenuItem.Enabled = true;
10841                     this.FavoriteRetweetUnofficialMenuItem.Enabled = true;
10842                 }
10843             }
10844
10845             if (_statuses.Tabs[ListTab.SelectedTab.Text].TabType != MyCommon.TabUsageType.Favorites)
10846             {
10847                 this.RefreshPrevOpMenuItem.Enabled = true;
10848             }
10849             else
10850             {
10851                 this.RefreshPrevOpMenuItem.Enabled = false;
10852             }
10853             if (!this.ExistCurrentPost
10854                 || _curPost.InReplyToStatusId == null)
10855             {
10856                 OpenRepSourceOpMenuItem.Enabled = false;
10857             }
10858             else
10859             {
10860                 OpenRepSourceOpMenuItem.Enabled = true;
10861             }
10862             if (!this.ExistCurrentPost || string.IsNullOrEmpty(_curPost.RetweetedBy))
10863             {
10864                 OpenRterHomeMenuItem.Enabled = false;
10865             }
10866             else
10867             {
10868                 OpenRterHomeMenuItem.Enabled = true;
10869             }
10870
10871             if (this.ExistCurrentPost)
10872             {
10873                 this.DelOpMenuItem.Enabled = this._curPost.CanDeleteBy(this.tw.UserId);
10874             }
10875         }
10876
10877         private void MenuItemTab_DropDownOpening(object sender, EventArgs e)
10878             => this.ContextMenuTabProperty_Opening(sender, null);
10879
10880         public Twitter TwitterInstance
10881             => this.tw;
10882
10883         private void SplitContainer3_SplitterMoved(object sender, SplitterEventArgs e)
10884         {
10885             if (this._initialLayout)
10886                 return;
10887
10888             int splitterDistance;
10889             switch (this.WindowState)
10890             {
10891                 case FormWindowState.Normal:
10892                     splitterDistance = this.SplitContainer3.SplitterDistance;
10893                     break;
10894                 case FormWindowState.Maximized:
10895                     // 最大化時は、通常時のウィンドウサイズに換算した SplitterDistance を算出する
10896                     var normalContainerWidth = this._mySize.Width - SystemInformation.Border3DSize.Width * 2;
10897                     splitterDistance = this.SplitContainer3.SplitterDistance - (this.SplitContainer3.Width - normalContainerWidth);
10898                     splitterDistance = Math.Min(splitterDistance, normalContainerWidth - this.SplitContainer3.SplitterWidth - this.SplitContainer3.Panel2MinSize);
10899                     break;
10900                 default:
10901                     return;
10902             }
10903
10904             this._mySpDis3 = splitterDistance;
10905             this.ModifySettingLocal = true;
10906         }
10907
10908         private void MenuItemEdit_DropDownOpening(object sender, EventArgs e)
10909         {
10910             if (_statuses.RemovedTab.Count == 0)
10911             {
10912                 UndoRemoveTabMenuItem.Enabled = false;
10913             }
10914             else
10915             {
10916                 UndoRemoveTabMenuItem.Enabled = true;
10917             }
10918             if (ListTab.SelectedTab != null)
10919             {
10920                 if (_statuses.Tabs[ListTab.SelectedTab.Text].TabType == MyCommon.TabUsageType.PublicSearch)
10921                     PublicSearchQueryMenuItem.Enabled = true;
10922                 else
10923                     PublicSearchQueryMenuItem.Enabled = false;
10924             }
10925             else
10926             {
10927                 PublicSearchQueryMenuItem.Enabled = false;
10928             }
10929             if (!this.ExistCurrentPost)
10930             {
10931                 this.CopySTOTMenuItem.Enabled = false;
10932                 this.CopyURLMenuItem.Enabled = false;
10933                 this.CopyUserIdStripMenuItem.Enabled = false;
10934             }
10935             else
10936             {
10937                 this.CopySTOTMenuItem.Enabled = true;
10938                 this.CopyURLMenuItem.Enabled = true;
10939                 this.CopyUserIdStripMenuItem.Enabled = true;
10940                 if (_curPost.IsDm) this.CopyURLMenuItem.Enabled = false;
10941                 if (_curPost.IsProtect) this.CopySTOTMenuItem.Enabled = false;
10942             }
10943         }
10944
10945         private void NotifyIcon1_MouseMove(object sender, MouseEventArgs e)
10946             => this.SetNotifyIconText();
10947
10948         private async void UserStatusToolStripMenuItem_Click(object sender, EventArgs e)
10949             => await this.ShowUserStatus(this._curPost?.ScreenName ?? "");
10950
10951         private async Task doShowUserStatus(string id, bool ShowInputDialog)
10952         {
10953             TwitterUser user = null;
10954
10955             if (ShowInputDialog)
10956             {
10957                 using (var inputName = new InputTabName())
10958                 {
10959                     inputName.FormTitle = "Show UserStatus";
10960                     inputName.FormDescription = Properties.Resources.FRMessage1;
10961                     inputName.TabName = id;
10962
10963                     if (inputName.ShowDialog(this) != DialogResult.OK)
10964                         return;
10965                     if (string.IsNullOrWhiteSpace(inputName.TabName))
10966                         return;
10967
10968                     id = inputName.TabName.Trim();
10969                 }
10970             }
10971
10972             using (var dialog = new WaitingDialog(Properties.Resources.doShowUserStatusText1))
10973             {
10974                 var cancellationToken = dialog.EnableCancellation();
10975
10976                 try
10977                 {
10978                     var task = this.twitterApi.UsersShow(id);
10979                     user = await dialog.WaitForAsync(this, task);
10980                 }
10981                 catch (WebApiException ex)
10982                 {
10983                     if (!cancellationToken.IsCancellationRequested)
10984                         MessageBox.Show($"Err:{ex.Message}(UsersShow)");
10985                     return;
10986                 }
10987
10988                 if (cancellationToken.IsCancellationRequested)
10989                     return;
10990             }
10991
10992             await this.doShowUserStatus(user);
10993         }
10994
10995         private async Task doShowUserStatus(TwitterUser user)
10996         {
10997             using (var userDialog = new UserInfoDialog(this, this.twitterApi))
10998             {
10999                 var showUserTask = userDialog.ShowUserAsync(user);
11000                 userDialog.ShowDialog(this);
11001
11002                 this.Activate();
11003                 this.BringToFront();
11004
11005                 // ユーザー情報の表示が完了するまで userDialog を破棄しない
11006                 await showUserTask;
11007             }
11008         }
11009
11010         internal Task ShowUserStatus(string id, bool ShowInputDialog)
11011             => this.doShowUserStatus(id, ShowInputDialog);
11012
11013         internal Task ShowUserStatus(string id)
11014             => this.doShowUserStatus(id, true);
11015
11016         private async void ShowProfileMenuItem_Click(object sender, EventArgs e)
11017         {
11018             if (_curPost != null)
11019             {
11020                 await this.ShowUserStatus(_curPost.ScreenName, false);
11021             }
11022         }
11023
11024         private async void RtCountMenuItem_Click(object sender, EventArgs e)
11025         {
11026             if (!this.ExistCurrentPost)
11027                 return;
11028
11029             var statusId = this._curPost.RetweetedId ?? this._curPost.StatusId;
11030             TwitterStatus status;
11031
11032             using (var dialog = new WaitingDialog(Properties.Resources.RtCountMenuItem_ClickText1))
11033             {
11034                 var cancellationToken = dialog.EnableCancellation();
11035
11036                 try
11037                 {
11038                     var task = this.twitterApi.StatusesShow(statusId);
11039                     status = await dialog.WaitForAsync(this, task);
11040                 }
11041                 catch (WebApiException ex)
11042                 {
11043                     if (!cancellationToken.IsCancellationRequested)
11044                         MessageBox.Show(Properties.Resources.RtCountText2 + Environment.NewLine + "Err:" + ex.Message);
11045                     return;
11046                 }
11047
11048                 if (cancellationToken.IsCancellationRequested)
11049                     return;
11050             }
11051
11052             MessageBox.Show(status.RetweetCount + Properties.Resources.RtCountText1);
11053         }
11054
11055         private HookGlobalHotkey _hookGlobalHotkey;
11056         public TweenMain()
11057         {
11058             _hookGlobalHotkey = new HookGlobalHotkey(this);
11059
11060             // この呼び出しは、Windows フォーム デザイナで必要です。
11061             InitializeComponent();
11062
11063             // InitializeComponent() 呼び出しの後で初期化を追加します。
11064
11065             if (!this.DesignMode)
11066             {
11067                 // デザイナでの編集時にレイアウトが縦方向に数pxずれる問題の対策
11068                 this.StatusText.Dock = DockStyle.Fill;
11069             }
11070
11071             this.tweetDetailsView.Owner = this;
11072
11073             this.TimerTimeline.Elapsed += this.TimerTimeline_Elapsed;
11074             this._hookGlobalHotkey.HotkeyPressed += _hookGlobalHotkey_HotkeyPressed;
11075             this.gh.NotifyClicked += GrowlHelper_Callback;
11076
11077             // メイリオフォント指定時にタブの最小幅が広くなる問題の対策
11078             this.ListTab.HandleCreated += (s, e) => NativeMethods.SetMinTabWidth((TabControl)s, 40);
11079
11080             this.ImageSelector.Visible = false;
11081             this.ImageSelector.Enabled = false;
11082             this.ImageSelector.FilePickDialog = OpenFileDialog1;
11083
11084             this.workerProgress = new Progress<string>(x => this.StatusLabel.Text = x);
11085
11086             this.ReplaceAppName();
11087             this.InitializeShortcuts();
11088         }
11089
11090         private void _hookGlobalHotkey_HotkeyPressed(object sender, KeyEventArgs e)
11091         {
11092             if ((this.WindowState == FormWindowState.Normal || this.WindowState == FormWindowState.Maximized) && this.Visible && Form.ActiveForm == this)
11093             {
11094                 //アイコン化
11095                 this.Visible = false;
11096             }
11097             else if (Form.ActiveForm == null)
11098             {
11099                 this.Visible = true;
11100                 if (this.WindowState == FormWindowState.Minimized) this.WindowState = FormWindowState.Normal;
11101                 this.Activate();
11102                 this.BringToFront();
11103                 this.StatusText.Focus();
11104             }
11105         }
11106
11107         private void SplitContainer2_MouseDoubleClick(object sender, MouseEventArgs e)
11108             => this.MultiLinePullDownMenuItem.PerformClick();
11109
11110         public PostClass CurPost
11111             => this._curPost;
11112
11113 #region "画像投稿"
11114         private void ImageSelectMenuItem_Click(object sender, EventArgs e)
11115         {
11116             if (ImageSelector.Visible)
11117                 ImageSelector.EndSelection();
11118             else
11119                 ImageSelector.BeginSelection();
11120         }
11121
11122         private void SelectMedia_DragEnter(DragEventArgs e)
11123         {
11124             if (ImageSelector.HasUploadableService(((string[])e.Data.GetData(DataFormats.FileDrop, false))[0], true))
11125             {
11126                 e.Effect = DragDropEffects.Copy;
11127                 return;
11128             }
11129             e.Effect = DragDropEffects.None;
11130         }
11131
11132         private void SelectMedia_DragDrop(DragEventArgs e)
11133         {
11134             this.Activate();
11135             this.BringToFront();
11136             ImageSelector.BeginSelection((string[])e.Data.GetData(DataFormats.FileDrop, false));
11137             StatusText.Focus();
11138         }
11139
11140         private void ImageSelector_BeginSelecting(object sender, EventArgs e)
11141         {
11142             TimelinePanel.Visible = false;
11143             TimelinePanel.Enabled = false;
11144         }
11145
11146         private void ImageSelector_EndSelecting(object sender, EventArgs e)
11147         {
11148             TimelinePanel.Visible = true;
11149             TimelinePanel.Enabled = true;
11150             ((DetailsListView)ListTab.SelectedTab.Tag).Focus();
11151         }
11152
11153         private void ImageSelector_FilePickDialogOpening(object sender, EventArgs e)
11154             => this.AllowDrop = false;
11155
11156         private void ImageSelector_FilePickDialogClosed(object sender, EventArgs e)
11157             => this.AllowDrop = true;
11158
11159         private void ImageSelector_SelectedServiceChanged(object sender, EventArgs e)
11160         {
11161             if (ImageSelector.Visible)
11162             {
11163                 ModifySettingCommon = true;
11164                 SaveConfigsAll(true);
11165
11166                 this.StatusText_TextChanged(null, null);
11167             }
11168         }
11169
11170         private void ImageSelector_VisibleChanged(object sender, EventArgs e)
11171             => this.StatusText_TextChanged(null, null);
11172
11173         /// <summary>
11174         /// StatusTextでCtrl+Vが押下された時の処理
11175         /// </summary>
11176         private void ProcClipboardFromStatusTextWhenCtrlPlusV()
11177         {
11178             try
11179             {
11180                 if (Clipboard.ContainsText())
11181                 {
11182                     // clipboardにテキストがある場合は貼り付け処理
11183                     this.StatusText.Paste(Clipboard.GetText());
11184                 }
11185                 else if (Clipboard.ContainsImage())
11186                 {
11187                     // 画像があるので投稿処理を行う
11188                     if (MessageBox.Show(Properties.Resources.PostPictureConfirm3,
11189                                        Properties.Resources.PostPictureWarn4,
11190                                        MessageBoxButtons.OKCancel,
11191                                        MessageBoxIcon.Question,
11192                                        MessageBoxDefaultButton.Button2)
11193                                    == DialogResult.OK)
11194                     {
11195                         // clipboardから画像を取得
11196                         using (var image = Clipboard.GetImage())
11197                         {
11198                             this.ImageSelector.BeginSelection(image);
11199                         }
11200                     }
11201                 }
11202             }
11203             catch (ExternalException ex)
11204             {
11205                 MessageBox.Show(ex.Message);
11206             }
11207         }
11208 #endregion
11209
11210         private void ListManageToolStripMenuItem_Click(object sender, EventArgs e)
11211         {
11212             using (ListManage form = new ListManage(tw))
11213             {
11214                 form.ShowDialog(this);
11215             }
11216         }
11217
11218         public bool ModifySettingCommon { get; set; }
11219         public bool ModifySettingLocal { get; set; }
11220         public bool ModifySettingAtId { get; set; }
11221
11222         private void MenuItemCommand_DropDownOpening(object sender, EventArgs e)
11223         {
11224             if (this.ExistCurrentPost && !_curPost.IsDm)
11225                 RtCountMenuItem.Enabled = true;
11226             else
11227                 RtCountMenuItem.Enabled = false;
11228
11229             //if (SettingDialog.UrlConvertAuto && SettingDialog.ShortenTco)
11230             //    TinyUrlConvertToolStripMenuItem.Enabled = false;
11231             //else
11232             //    TinyUrlConvertToolStripMenuItem.Enabled = true;
11233         }
11234
11235         private void CopyUserIdStripMenuItem_Click(object sender, EventArgs e)
11236             => this.CopyUserId();
11237
11238         private void CopyUserId()
11239         {
11240             if (_curPost == null) return;
11241             string clstr = _curPost.ScreenName;
11242             try
11243             {
11244                 Clipboard.SetDataObject(clstr, false, 5, 100);
11245             }
11246             catch (Exception ex)
11247             {
11248                 MessageBox.Show(ex.Message);
11249             }
11250         }
11251
11252         private async void ShowRelatedStatusesMenuItem_Click(object sender, EventArgs e)
11253         {
11254             if (this.ExistCurrentPost && !_curPost.IsDm)
11255             {
11256                 try
11257                 {
11258                     await this.OpenRelatedTab(this._curPost);
11259                 }
11260                 catch (TabException ex)
11261                 {
11262                     MessageBox.Show(this, ex.Message, ApplicationSettings.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
11263                 }
11264             }
11265         }
11266
11267         /// <summary>
11268         /// 指定されたツイートに対する関連発言タブを開きます
11269         /// </summary>
11270         /// <param name="statusId">表示するツイートのID</param>
11271         /// <exception cref="TabException">名前の重複が多すぎてタブを作成できない場合</exception>
11272         public async Task OpenRelatedTab(long statusId)
11273         {
11274             var post = this._statuses[statusId];
11275             if (post == null)
11276             {
11277                 try
11278                 {
11279                     post = await this.tw.GetStatusApi(false, statusId);
11280                 }
11281                 catch (WebApiException ex)
11282                 {
11283                     this.StatusLabel.Text = $"Err:{ex.Message}(GetStatus)";
11284                     return;
11285                 }
11286             }
11287
11288             await this.OpenRelatedTab(post);
11289         }
11290
11291         /// <summary>
11292         /// 指定されたツイートに対する関連発言タブを開きます
11293         /// </summary>
11294         /// <param name="post">表示する対象となるツイート</param>
11295         /// <exception cref="TabException">名前の重複が多すぎてタブを作成できない場合</exception>
11296         private async Task OpenRelatedTab(PostClass post)
11297         {
11298             var tabRelated = this._statuses.GetTabByType<RelatedPostsTabModel>();
11299             if (tabRelated != null)
11300             {
11301                 this.RemoveSpecifiedTab(tabRelated.TabName, confirm: false);
11302             }
11303
11304             var tabName = this._statuses.MakeTabName("Related Tweets");
11305
11306             tabRelated = new RelatedPostsTabModel(tabName, post)
11307             {
11308                 UnreadManage = false,
11309                 Notify = false,
11310             };
11311
11312             this._statuses.AddTab(tabRelated);
11313             this.AddNewTab(tabRelated, startup: false);
11314
11315             TabPage tabPage;
11316             for (int i = 0; i < this.ListTab.TabPages.Count; i++)
11317             {
11318                 tabPage = this.ListTab.TabPages[i];
11319                 if (tabName == tabPage.Text)
11320                 {
11321                     this.ListTab.SelectedIndex = i;
11322                     break;
11323                 }
11324             }
11325
11326             await this.RefreshTabAsync(tabRelated);
11327
11328             tabPage = this.ListTab.TabPages.Cast<TabPage>()
11329                 .FirstOrDefault(x => x.Text == tabRelated.TabName);
11330
11331             if (tabPage != null)
11332             {
11333                 // TODO: 非同期更新中にタブが閉じられている場合を厳密に考慮したい
11334
11335                 var listView = (DetailsListView)tabPage.Tag;
11336                 var targetPost = tabRelated.TargetPost;
11337                 var index = tabRelated.IndexOf(targetPost.RetweetedId ?? targetPost.StatusId);
11338
11339                 if (index != -1 && index < listView.Items.Count)
11340                 {
11341                     listView.SelectedIndices.Add(index);
11342                     listView.Items[index].Focused = true;
11343                 }
11344             }
11345         }
11346
11347         private void CacheInfoMenuItem_Click(object sender, EventArgs e)
11348         {
11349             StringBuilder buf = new StringBuilder();
11350             //buf.AppendFormat("キャッシュメモリ容量         : {0}bytes({1}MB)" + Environment.NewLine, IconCache.CacheMemoryLimit, ((ImageDictionary)IconCache).CacheMemoryLimit / 1048576);
11351             //buf.AppendFormat("物理メモリ使用割合           : {0}%" + Environment.NewLine, IconCache.PhysicalMemoryLimit);
11352             buf.AppendFormat("キャッシュエントリ保持数     : {0}" + Environment.NewLine, IconCache.CacheCount);
11353             buf.AppendFormat("キャッシュエントリ破棄数     : {0}" + Environment.NewLine, IconCache.CacheRemoveCount);
11354             MessageBox.Show(buf.ToString(), "アイコンキャッシュ使用状況");
11355         }
11356
11357         private void tw_UserIdChanged()
11358             => this.ModifySettingCommon = true;
11359
11360 #region "Userstream"
11361         private async void tw_PostDeleted(object sender, PostDeletedEventArgs e)
11362         {
11363             try
11364             {
11365                 if (InvokeRequired && !IsDisposed)
11366                 {
11367                     await this.InvokeAsync(async () =>
11368                     {
11369                         this._statuses.RemovePostFromAllTabs(e.StatusId, setIsDeleted: true);
11370                         if (_curTab != null && _statuses.Tabs[_curTab.Text].Contains(e.StatusId))
11371                         {
11372                             this.PurgeListViewItemCache();
11373                             ((DetailsListView)_curTab.Tag).Update();
11374                             if (_curPost != null && _curPost.StatusId == e.StatusId)
11375                                 await this.DispSelectedPost(true);
11376                         }
11377                     });
11378                     return;
11379                 }
11380             }
11381             catch (ObjectDisposedException)
11382             {
11383                 return;
11384             }
11385             catch (InvalidOperationException)
11386             {
11387                 return;
11388             }
11389         }
11390
11391         private void tw_NewPostFromStream(object sender, EventArgs e)
11392         {
11393             if (SettingManager.Common.ReadOldPosts)
11394             {
11395                 _statuses.SetReadHomeTab(); //新着時未読クリア
11396             }
11397
11398             this._statuses.DistributePosts();
11399
11400             this.RefreshThrottlingTimer.Invoke();
11401         }
11402
11403         private async void tw_UserStreamStarted(object sender, EventArgs e)
11404         {
11405             try
11406             {
11407                 if (InvokeRequired && !IsDisposed)
11408                 {
11409                     await this.InvokeAsync(() => this.tw_UserStreamStarted(sender, e));
11410                     return;
11411                 }
11412             }
11413             catch (ObjectDisposedException)
11414             {
11415                 return;
11416             }
11417             catch (InvalidOperationException)
11418             {
11419                 return;
11420             }
11421
11422             this.RefreshUserStreamsMenu();
11423             this.MenuItemUserStream.Enabled = true;
11424
11425             StatusLabel.Text = "UserStream Started.";
11426         }
11427
11428         private async void tw_UserStreamStopped(object sender, EventArgs e)
11429         {
11430             try
11431             {
11432                 if (InvokeRequired && !IsDisposed)
11433                 {
11434                     await this.InvokeAsync(() => this.tw_UserStreamStopped(sender, e));
11435                     return;
11436                 }
11437             }
11438             catch (ObjectDisposedException)
11439             {
11440                 return;
11441             }
11442             catch (InvalidOperationException)
11443             {
11444                 return;
11445             }
11446
11447             this.RefreshUserStreamsMenu();
11448             this.MenuItemUserStream.Enabled = true;
11449
11450             StatusLabel.Text = "UserStream Stopped.";
11451         }
11452
11453         private void RefreshUserStreamsMenu()
11454         {
11455             if (this.tw.UserStreamActive)
11456             {
11457                 this.MenuItemUserStream.Text = "&UserStream ▶";
11458                 this.StopToolStripMenuItem.Text = "&Stop";
11459             }
11460             else
11461             {
11462                 this.MenuItemUserStream.Text = "&UserStream ■";
11463                 this.StopToolStripMenuItem.Text = "&Start";
11464             }
11465         }
11466
11467         private async void tw_UserStreamEventArrived(object sender, UserStreamEventReceivedEventArgs e)
11468         {
11469             try
11470             {
11471                 if (InvokeRequired && !IsDisposed)
11472                 {
11473                     await this.InvokeAsync(() => this.tw_UserStreamEventArrived(sender, e));
11474                     return;
11475                 }
11476             }
11477             catch (ObjectDisposedException)
11478             {
11479                 return;
11480             }
11481             catch (InvalidOperationException)
11482             {
11483                 return;
11484             }
11485             var ev = e.EventData;
11486             StatusLabel.Text = "Event: " + ev.Event;
11487             //if (ev.Event == "favorite")
11488             //{
11489             //    NotifyFavorite(ev);
11490             //}
11491             NotifyEvent(ev);
11492             if (ev.Event == "favorite" || ev.Event == "unfavorite")
11493             {
11494                 if (_curTab != null && _statuses.Tabs[_curTab.Text].Contains(ev.Id))
11495                 {
11496                     this.PurgeListViewItemCache();
11497                     ((DetailsListView)_curTab.Tag).Update();
11498                 }
11499                 if (ev.Event == "unfavorite" && ev.Username.Equals(tw.Username, StringComparison.InvariantCultureIgnoreCase))
11500                 {
11501                     var favTab = this._statuses.GetTabByType(MyCommon.TabUsageType.Favorites);
11502                     favTab.EnqueueRemovePost(ev.Id, setIsDeleted: false);
11503                 }
11504             }
11505         }
11506
11507         private void NotifyEvent(Twitter.FormattedEvent ev)
11508         {
11509             //新着通知 
11510             if (BalloonRequired(ev))
11511             {
11512                 NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
11513                 //if (SettingDialog.DispUsername) NotifyIcon1.BalloonTipTitle = tw.Username + " - "; else NotifyIcon1.BalloonTipTitle = "";
11514                 //NotifyIcon1.BalloonTipTitle += Application.ProductName + " [" + ev.Event.ToUpper() + "] by " + ((string)(!string.IsNullOrEmpty(ev.Username) ? ev.Username : ""), string);
11515                 StringBuilder title = new StringBuilder();
11516                 if (SettingManager.Common.DispUsername)
11517                 {
11518                     title.Append(tw.Username);
11519                     title.Append(" - ");
11520                 }
11521                 else
11522                 {
11523                     //title.Clear();
11524                 }
11525                 title.Append(ApplicationSettings.ApplicationName);
11526                 title.Append(" [");
11527                 title.Append(ev.Event.ToUpper(CultureInfo.CurrentCulture));
11528                 title.Append("] by ");
11529                 if (!string.IsNullOrEmpty(ev.Username))
11530                 {
11531                     title.Append(ev.Username);
11532                 }
11533                 else
11534                 {
11535                     //title.Append("");
11536                 }
11537                 string text;
11538                 if (!string.IsNullOrEmpty(ev.Target))
11539                 {
11540                     //NotifyIcon1.BalloonTipText = ev.Target;
11541                     text = ev.Target;
11542                 }
11543                 else
11544                 {
11545                     //NotifyIcon1.BalloonTipText = " ";
11546                     text = " ";
11547                 }
11548                 //NotifyIcon1.ShowBalloonTip(500);
11549                 if (SettingManager.Common.IsUseNotifyGrowl)
11550                 {
11551                     gh.Notify(GrowlHelper.NotifyType.UserStreamEvent,
11552                               ev.Id.ToString(), title.ToString(), text);
11553                 }
11554                 else
11555                 {
11556                     NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
11557                     NotifyIcon1.BalloonTipTitle = title.ToString();
11558                     NotifyIcon1.BalloonTipText = text;
11559                     NotifyIcon1.ShowBalloonTip(500);
11560                 }
11561             }
11562
11563             //サウンド再生
11564             string snd = SettingManager.Common.EventSoundFile;
11565             if (!_initial && SettingManager.Common.PlaySound && !string.IsNullOrEmpty(snd))
11566             {
11567                 if ((ev.Eventtype & SettingManager.Common.EventNotifyFlag) != 0 && IsMyEventNotityAsEventType(ev))
11568                 {
11569                     try
11570                     {
11571                         string dir = Application.StartupPath;
11572                         if (Directory.Exists(Path.Combine(dir, "Sounds")))
11573                         {
11574                             dir = Path.Combine(dir, "Sounds");
11575                         }
11576                         using (SoundPlayer player = new SoundPlayer(Path.Combine(dir, snd)))
11577                         {
11578                             player.Play();
11579                         }
11580                     }
11581                     catch (Exception)
11582                     {
11583                     }
11584                 }
11585             }
11586         }
11587
11588         private void StopToolStripMenuItem_Click(object sender, EventArgs e)
11589         {
11590             MenuItemUserStream.Enabled = false;
11591             if (StopRefreshAllMenuItem.Checked)
11592             {
11593                 StopRefreshAllMenuItem.Checked = false;
11594                 return;
11595             }
11596             if (this.tw.UserStreamActive)
11597             {
11598                 tw.StopUserStream();
11599             }
11600             else
11601             {
11602                 tw.StartUserStream();
11603             }
11604         }
11605
11606         private static string inputTrack = "";
11607
11608         private void TrackToolStripMenuItem_Click(object sender, EventArgs e)
11609         {
11610             if (TrackToolStripMenuItem.Checked)
11611             {
11612                 using (InputTabName inputForm = new InputTabName())
11613                 {
11614                     inputForm.TabName = inputTrack;
11615                     inputForm.FormTitle = "Input track word";
11616                     inputForm.FormDescription = "Track word";
11617                     if (inputForm.ShowDialog() != DialogResult.OK)
11618                     {
11619                         TrackToolStripMenuItem.Checked = false;
11620                         return;
11621                     }
11622                     inputTrack = inputForm.TabName.Trim();
11623                 }
11624                 if (!inputTrack.Equals(tw.TrackWord))
11625                 {
11626                     tw.TrackWord = inputTrack;
11627                     this.ModifySettingCommon = true;
11628                     TrackToolStripMenuItem.Checked = !string.IsNullOrEmpty(inputTrack);
11629                     tw.ReconnectUserStream();
11630                 }
11631             }
11632             else
11633             {
11634                 tw.TrackWord = "";
11635                 tw.ReconnectUserStream();
11636             }
11637             this.ModifySettingCommon = true;
11638         }
11639
11640         private void AllrepliesToolStripMenuItem_Click(object sender, EventArgs e)
11641         {
11642             tw.AllAtReply = AllrepliesToolStripMenuItem.Checked;
11643             this.ModifySettingCommon = true;
11644             tw.ReconnectUserStream();
11645         }
11646
11647         private void EventViewerMenuItem_Click(object sender, EventArgs e)
11648         {
11649             if (evtDialog == null || evtDialog.IsDisposed)
11650             {
11651                 this.evtDialog = new EventViewerDialog
11652                 {
11653                     Owner = this,
11654                 };
11655
11656                 //親の中央に表示
11657                 this.evtDialog.Location = new Point
11658                 {
11659                     X = Convert.ToInt32(this.Location.X + this.Size.Width / 2 - evtDialog.Size.Width / 2),
11660                     Y = Convert.ToInt32(this.Location.Y + this.Size.Height / 2 - evtDialog.Size.Height / 2),
11661                 };
11662             }
11663             evtDialog.EventSource = tw.StoredEvent;
11664             if (!evtDialog.Visible)
11665             {
11666                 evtDialog.Show(this);
11667             }
11668             else
11669             {
11670                 evtDialog.Activate();
11671             }
11672             this.TopMost = SettingManager.Common.AlwaysTop;
11673         }
11674 #endregion
11675
11676         private void TweenRestartMenuItem_Click(object sender, EventArgs e)
11677         {
11678             MyCommon._endingFlag = true;
11679             try
11680             {
11681                 this.Close();
11682                 Application.Restart();
11683             }
11684             catch (Exception)
11685             {
11686                 MessageBox.Show("Failed to restart. Please run " + ApplicationSettings.ApplicationName + " manually.");
11687             }
11688         }
11689
11690         private async void OpenOwnHomeMenuItem_Click(object sender, EventArgs e)
11691             => await this.OpenUriInBrowserAsync(MyCommon.TwitterUrl + tw.Username);
11692
11693         private bool ExistCurrentPost
11694         {
11695             get
11696             {
11697                 if (_curPost == null) return false;
11698                 if (_curPost.IsDeleted) return false;
11699                 return true;
11700             }
11701         }
11702
11703         private async void ShowUserTimelineToolStripMenuItem_Click(object sender, EventArgs e)
11704             => await this.ShowUserTimeline();
11705
11706         private string GetUserIdFromCurPostOrInput(string caption)
11707         {
11708             var id = _curPost?.ScreenName ?? "";
11709
11710             using (InputTabName inputName = new InputTabName())
11711             {
11712                 inputName.FormTitle = caption;
11713                 inputName.FormDescription = Properties.Resources.FRMessage1;
11714                 inputName.TabName = id;
11715                 if (inputName.ShowDialog() == DialogResult.OK &&
11716                     !string.IsNullOrEmpty(inputName.TabName.Trim()))
11717                 {
11718                     id = inputName.TabName.Trim();
11719                 }
11720                 else
11721                 {
11722                     id = "";
11723                 }
11724             }
11725             return id;
11726         }
11727
11728         private async void UserTimelineToolStripMenuItem_Click(object sender, EventArgs e)
11729         {
11730             string id = GetUserIdFromCurPostOrInput("Show UserTimeline");
11731             if (!string.IsNullOrEmpty(id))
11732             {
11733                 await this.AddNewTabForUserTimeline(id);
11734             }
11735         }
11736
11737         private void SystemEvents_PowerModeChanged(object sender, Microsoft.Win32.PowerModeChangedEventArgs e)
11738         {
11739             if (e.Mode == Microsoft.Win32.PowerModes.Resume) osResumed = true;
11740         }
11741
11742         private async void SystemEvents_TimeChanged(object sender, EventArgs e)
11743         {
11744             var prevTimeOffset = TimeZoneInfo.Local.BaseUtcOffset;
11745
11746             TimeZoneInfo.ClearCachedData();
11747
11748             var curTimeOffset = TimeZoneInfo.Local.BaseUtcOffset;
11749
11750             if (curTimeOffset != prevTimeOffset)
11751             {
11752                 // タイムゾーンの変更を反映
11753                 this.PurgeListViewItemCache();
11754                 this._curList.Refresh();
11755
11756                 await this.DispSelectedPost(forceupdate: true);
11757             }
11758         }
11759
11760         private void TimelineRefreshEnableChange(bool isEnable)
11761         {
11762             if (isEnable)
11763             {
11764                 tw.StartUserStream();
11765             }
11766             else
11767             {
11768                 tw.StopUserStream();
11769             }
11770             TimerTimeline.Enabled = isEnable;
11771         }
11772
11773         private void StopRefreshAllMenuItem_CheckedChanged(object sender, EventArgs e)
11774             => this.TimelineRefreshEnableChange(!StopRefreshAllMenuItem.Checked);
11775
11776         private async Task OpenUserAppointUrl()
11777         {
11778             if (SettingManager.Common.UserAppointUrl != null)
11779             {
11780                 if (SettingManager.Common.UserAppointUrl.Contains("{ID}") || SettingManager.Common.UserAppointUrl.Contains("{STATUS}"))
11781                 {
11782                     if (_curPost != null)
11783                     {
11784                         string xUrl = SettingManager.Common.UserAppointUrl;
11785                         xUrl = xUrl.Replace("{ID}", _curPost.ScreenName);
11786
11787                         var statusId = _curPost.RetweetedId ?? _curPost.StatusId;
11788                         xUrl = xUrl.Replace("{STATUS}", statusId.ToString());
11789
11790                         await this.OpenUriInBrowserAsync(xUrl);
11791                     }
11792                 }
11793                 else
11794                 {
11795                     await this.OpenUriInBrowserAsync(SettingManager.Common.UserAppointUrl);
11796                 }
11797             }
11798         }
11799
11800         private async void OpenUserSpecifiedUrlMenuItem_Click(object sender, EventArgs e)
11801             => await this.OpenUserAppointUrl();
11802
11803         private async void GrowlHelper_Callback(object sender, GrowlHelper.NotifyCallbackEventArgs e)
11804         {
11805             if (Form.ActiveForm == null)
11806             {
11807                 await this.InvokeAsync(() =>
11808                 {
11809                     this.Visible = true;
11810                     if (this.WindowState == FormWindowState.Minimized) this.WindowState = FormWindowState.Normal;
11811                     this.Activate();
11812                     this.BringToFront();
11813                     if (e.NotifyType == GrowlHelper.NotifyType.DirectMessage)
11814                     {
11815                         if (!this.GoDirectMessage(e.StatusId)) this.StatusText.Focus();
11816                     }
11817                     else
11818                     {
11819                         if (!this.GoStatus(e.StatusId)) this.StatusText.Focus();
11820                     }
11821                 });
11822             }
11823         }
11824
11825         private void ReplaceAppName()
11826         {
11827             MatomeMenuItem.Text = MyCommon.ReplaceAppName(MatomeMenuItem.Text);
11828             AboutMenuItem.Text = MyCommon.ReplaceAppName(AboutMenuItem.Text);
11829         }
11830
11831         private void tweetThumbnail1_ThumbnailLoading(object sender, EventArgs e)
11832             => this.SplitContainer3.Panel2Collapsed = false;
11833
11834         private async void tweetThumbnail1_ThumbnailDoubleClick(object sender, ThumbnailDoubleClickEventArgs e)
11835             => await this.OpenThumbnailPicture(e.Thumbnail);
11836
11837         private async void tweetThumbnail1_ThumbnailImageSearchClick(object sender, ThumbnailImageSearchEventArgs e)
11838             => await this.OpenUriInBrowserAsync(e.ImageUrl);
11839
11840         private async Task OpenThumbnailPicture(ThumbnailInfo thumbnail)
11841         {
11842             var url = thumbnail.FullSizeImageUrl ?? thumbnail.MediaPageUrl;
11843
11844             await this.OpenUriInBrowserAsync(url);
11845         }
11846
11847         private async void TwitterApiStatusToolStripMenuItem_Click(object sender, EventArgs e)
11848             => await this.OpenUriInBrowserAsync(Twitter.ServiceAvailabilityStatusUrl);
11849
11850         private void PostButton_KeyDown(object sender, KeyEventArgs e)
11851         {
11852             if (e.KeyCode == Keys.Space)
11853             {
11854                 this.JumpUnreadMenuItem_Click(null, null);
11855
11856                 e.SuppressKeyPress = true;
11857             }
11858         }
11859
11860         private void ContextMenuColumnHeader_Opening(object sender, CancelEventArgs e)
11861         {
11862             this.IconSizeNoneToolStripMenuItem.Checked = SettingManager.Common.IconSize == MyCommon.IconSizes.IconNone;
11863             this.IconSize16ToolStripMenuItem.Checked = SettingManager.Common.IconSize == MyCommon.IconSizes.Icon16;
11864             this.IconSize24ToolStripMenuItem.Checked = SettingManager.Common.IconSize == MyCommon.IconSizes.Icon24;
11865             this.IconSize48ToolStripMenuItem.Checked = SettingManager.Common.IconSize == MyCommon.IconSizes.Icon48;
11866             this.IconSize48_2ToolStripMenuItem.Checked = SettingManager.Common.IconSize == MyCommon.IconSizes.Icon48_2;
11867
11868             this.LockListSortOrderToolStripMenuItem.Checked = SettingManager.Common.SortOrderLock;
11869         }
11870
11871         private void IconSizeNoneToolStripMenuItem_Click(object sender, EventArgs e)
11872             => this.ChangeListViewIconSize(MyCommon.IconSizes.IconNone);
11873
11874         private void IconSize16ToolStripMenuItem_Click(object sender, EventArgs e)
11875             => this.ChangeListViewIconSize(MyCommon.IconSizes.Icon16);
11876
11877         private void IconSize24ToolStripMenuItem_Click(object sender, EventArgs e)
11878             => this.ChangeListViewIconSize(MyCommon.IconSizes.Icon24);
11879
11880         private void IconSize48ToolStripMenuItem_Click(object sender, EventArgs e)
11881             => this.ChangeListViewIconSize(MyCommon.IconSizes.Icon48);
11882
11883         private void IconSize48_2ToolStripMenuItem_Click(object sender, EventArgs e)
11884             => this.ChangeListViewIconSize(MyCommon.IconSizes.Icon48_2);
11885
11886         private void ChangeListViewIconSize(MyCommon.IconSizes iconSize)
11887         {
11888             if (SettingManager.Common.IconSize == iconSize) return;
11889
11890             var oldIconCol = _iconCol;
11891
11892             SettingManager.Common.IconSize = iconSize;
11893             ApplyListViewIconSize(iconSize);
11894
11895             if (_iconCol != oldIconCol)
11896             {
11897                 foreach (TabPage tp in ListTab.TabPages)
11898                 {
11899                     ResetColumns((DetailsListView)tp.Tag);
11900                 }
11901             }
11902
11903             _curList?.Refresh();
11904
11905             ModifySettingCommon = true;
11906         }
11907
11908         private void LockListSortToolStripMenuItem_Click(object sender, EventArgs e)
11909         {
11910             var state = this.LockListSortOrderToolStripMenuItem.Checked;
11911             if (SettingManager.Common.SortOrderLock == state) return;
11912
11913             SettingManager.Common.SortOrderLock = state;
11914
11915             ModifySettingCommon = true;
11916         }
11917
11918         private void tweetDetailsView_StatusChanged(object sender, TweetDetailsViewStatusChengedEventArgs e)
11919         {
11920             if (!string.IsNullOrEmpty(e.StatusText))
11921             {
11922                 this.StatusLabelUrl.Text = e.StatusText;
11923             }
11924             else
11925             {
11926                 this.SetStatusLabelUrl();
11927             }
11928         }
11929     }
11930 }