OSDN Git Service

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