OSDN Git Service

プロフィール画面へのD&Dによるアイコン変更時に確認ダイアログを表示するように変更
[opentween/open-tween.git] / OpenTween / FormInfo.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 using System;
28 using System.Collections.Generic;
29 using System.ComponentModel;
30 using System.Data;
31 using System.Drawing;
32 using System.Linq;
33 using System.Text;
34 using System.Windows.Forms;
35 using System.Threading;
36
37 namespace OpenTween
38 {
39     ///<summary>
40     ///タスクサービス機能付きプログレスバー
41     ///</summary>
42     ///<remarks>
43     ///重要:BackGroundWorkerコンポーネントが実際のタスクサービスを行うため、DoWorkでコントロールを触ることはできない。
44     ///また、Twitterへの通信を必要とする場合は引数にTwitterInstanceを含めそれを使用すること。
45     /// 1.class生成 2.コンストラクタの引数としてサービス登録(Dowork RunWorkerCompletedも同様 使用しない場合null)
46     /// 3.Instance.Argumentへ,あるいはコンストラクタ引数へ引数セット 
47     /// 4.Instance.InfoMessage、またはコンストラクタ引数へ表示メッセージ設定 5.Instance.ShowDialog()により表示
48     /// 6. 必要な場合はInstance.Result(=Servicerのe.Result)を参照し戻り値を得る
49     /// 7.Dispose タスクサービスが正常終了した場合は自分自身をCloseするので最後にDisposeすること。
50     ///</remarks>
51     public partial class FormInfo : OTBaseForm
52     {
53         private class BackgroundWorkerServicer : BackgroundWorker
54         {
55             public object Result = null;
56
57             protected override void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
58             {
59                 this.Result = e.Result;
60                 base.OnRunWorkerCompleted(e);
61             }
62         }
63
64         private string _msg;
65         private object _arg = null;
66
67         private BackgroundWorkerServicer Servicer = new BackgroundWorkerServicer();
68
69         public FormInfo(Form owner,
70                         string Message,
71                         DoWorkEventHandler DoWork)
72         {
73             doInitialize(owner, Message, DoWork, null, null);
74         }
75
76         public FormInfo(Form owner,
77                         string Message,
78                         DoWorkEventHandler DoWork,
79                         RunWorkerCompletedEventHandler RunWorkerCompleted)
80         {
81             doInitialize(owner, Message, DoWork, RunWorkerCompleted, null);
82         }
83
84         public FormInfo(Form owner,
85                         string Message,
86                         DoWorkEventHandler DoWork,
87                         RunWorkerCompletedEventHandler RunWorkerCompleted,
88                         object Argument)
89         {
90             doInitialize(owner, Message, DoWork, RunWorkerCompleted, Argument);
91         }
92
93         private void doInitialize(Form owner,
94                                   string Message,
95                                   DoWorkEventHandler DoWork,
96                                   RunWorkerCompletedEventHandler RunWorkerCompleted,
97                                   object Argument)
98         {
99             // この呼び出しはデザイナーで必要です。
100             InitializeComponent();
101
102             // InitializeComponent() 呼び出しの後で初期化を追加します。
103
104             this.Owner = owner;
105             this.InfoMessage = Message;
106             this.Servicer.DoWork += DoWork;
107
108             if (RunWorkerCompleted != null)
109             {
110                 this.Servicer.RunWorkerCompleted += RunWorkerCompleted;
111             }
112
113             this.Argument = Argument;
114         }
115
116         private void LabelInformation_TextChanged(object sender, EventArgs e)
117         {
118             LabelInformation.Refresh();
119         }
120
121         /// <summary>
122         /// ダイアログに表示されるユーザー向けメッセージを設定あるいは取得する
123         /// </summary>
124         /// <returns>現在設定されているメッセージ</returns>
125         public string InfoMessage
126         {
127             get { return _msg; }
128             set
129             {
130                 _msg = value;
131                 LabelInformation.Text = _msg;
132             }
133         }
134
135         /// <summary>
136         /// Servicerへ渡すパラメータ
137         /// </summary>
138         /// <returns>現在設定されているServicerへ渡すパラメータ</returns>
139         public object Argument
140         {
141             get { return _arg; }
142             set { _arg = value; }
143         }
144
145         ///<summary>
146         ///Servicerのe.Result
147         ///</summary>
148         ///<returns>Servicerのe.Result</returns>
149         public object Result
150         {
151             get { return Servicer.Result; }
152         }
153
154         private void FormInfo_Shown(object sender, EventArgs e)
155         {
156             Servicer.RunWorkerAsync(_arg);
157             while (Servicer.IsBusy)
158             {
159                 Thread.Sleep(100);
160                 Application.DoEvents();
161             }
162             this.TopMost = false;          // MessageBoxが裏に隠れる問題に対応
163             this.Close();
164         }
165
166         // フォームを閉じたあとに親フォームが最前面にならない問題に対応
167
168         private void FormInfo_FormClosed(object sender, FormClosedEventArgs e)
169         {
170             if (Owner != null && Owner.Created)
171             {
172                 Owner.TopMost = !Owner.TopMost;
173                 Owner.TopMost = !Owner.TopMost;
174             }
175         }
176     }
177 }