OSDN Git Service

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