OSDN Git Service

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