PageRenderTime 1124ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/thirdparties/PSTaskDialog/frmTaskDialog.cs

http://lextudio.googlecode.com/
C# | 366 lines | 297 code | 40 blank | 29 comment | 38 complexity | 6139b4cc9aa52f609d9e9fceafda8136 MD5 | raw file
Possible License(s): CPL-1.0, Apache-2.0, GPL-2.0, LGPL-2.1
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace PSTaskDialog
  9. {
  10. public partial class frmTaskDialog : Form
  11. {
  12. //--------------------------------------------------------------------------------
  13. #region PRIVATE members
  14. //--------------------------------------------------------------------------------
  15. eSysIcons m_mainIcon = eSysIcons.Question;
  16. eSysIcons m_footerIcon = eSysIcons.Warning;
  17. List<RadioButton> m_radioButtonCtrls = new List<RadioButton>();
  18. string m_radioButtons = "";
  19. int m_initialRadioButtonIndex = 0;
  20. List<Button> m_cmdButtons = new List<Button>();
  21. string m_commandButtons = "";
  22. int m_commandButtonClicked = -1;
  23. eTaskDialogButtons m_Buttons = eTaskDialogButtons.YesNoCancel;
  24. bool m_Expanded = false;
  25. bool m_isVista = false;
  26. #endregion
  27. //--------------------------------------------------------------------------------
  28. #region PROPERTIES
  29. //--------------------------------------------------------------------------------
  30. public eSysIcons MainIcon { get { return m_mainIcon; } set { m_mainIcon = value; } }
  31. public eSysIcons FooterIcon { get { return m_footerIcon; } set { m_footerIcon = value; } }
  32. public string Title { get { return this.Text; } set { this.Text = value; } }
  33. public string MainInstruction { get { return lbMainInstruction.Text; } set { lbMainInstruction.Text = value; } }
  34. public string Content { get { return lbContent.Text; } set { lbContent.Text = value; } }
  35. public string ExpandedInfo { get { return lbExpandedInfo.Text; } set { lbExpandedInfo.Text = value; } }
  36. public string Footer { get { return lbFooter.Text; } set { lbFooter.Text = value; } }
  37. public string RadioButtons { get { return m_radioButtons; } set { m_radioButtons = value; } }
  38. public int InitialRadioButtonIndex { get { return m_initialRadioButtonIndex; } set { m_initialRadioButtonIndex = value; } }
  39. public int RadioButtonIndex
  40. {
  41. get
  42. {
  43. foreach (RadioButton rb in m_radioButtonCtrls)
  44. if (rb.Checked)
  45. return (int)rb.Tag;
  46. return -1;
  47. }
  48. }
  49. public string CommandButtons { get { return m_commandButtons; } set { m_commandButtons = value; } }
  50. public int CommandButtonClickedIndex { get { return m_commandButtonClicked; } }
  51. public eTaskDialogButtons Buttons { get { return m_Buttons; } set { m_Buttons = value; } }
  52. public string VerificationText { get { return cbVerify.Text; } set { cbVerify.Text = value; } }
  53. public bool VerificationCheckBoxChecked { get { return cbVerify.Checked; } set { cbVerify.Checked = value; } }
  54. public bool Expanded { get { return m_Expanded; } set { m_Expanded = value; } }
  55. #endregion
  56. //--------------------------------------------------------------------------------
  57. #region CONSTRUCTOR
  58. //--------------------------------------------------------------------------------
  59. public frmTaskDialog()
  60. {
  61. InitializeComponent();
  62. m_isVista = VistaTaskDialog.IsAvailableOnThisOS;
  63. if (m_isVista)
  64. {
  65. // We're emulating on Vista, so tweak the font's a little...
  66. lbMainInstruction.Font = new Font(lbMainInstruction.Font, FontStyle.Regular);
  67. }
  68. else
  69. {
  70. // not on Vista
  71. if (cTaskDialog.UseToolWindowOnXP) // <- shall we use the smaller toolbar?
  72. this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
  73. }
  74. MainInstruction = "Main Instruction";
  75. Content = "";
  76. ExpandedInfo = "";
  77. Footer = "";
  78. VerificationText = "";
  79. }
  80. #endregion
  81. //--------------------------------------------------------------------------------
  82. #region BuildForm
  83. // This is the main routine that should be called before .ShowDialog()
  84. //--------------------------------------------------------------------------------
  85. bool m_formBuilt = false;
  86. public void BuildForm()
  87. {
  88. int form_height = 0;
  89. // Setup Main Instruction
  90. switch (m_mainIcon)
  91. {
  92. case eSysIcons.Information: imgMain.Image = SystemIcons.Information.ToBitmap(); break;
  93. case eSysIcons.Question: imgMain.Image = SystemIcons.Question.ToBitmap(); break;
  94. case eSysIcons.Warning: imgMain.Image = SystemIcons.Warning.ToBitmap(); break;
  95. case eSysIcons.Error: imgMain.Image = SystemIcons.Error.ToBitmap(); break;
  96. }
  97. AdjustLabelHeight(lbMainInstruction);
  98. pnlMainInstruction.Height = Math.Max(41, lbMainInstruction.Height + 16);
  99. form_height += pnlMainInstruction.Height;
  100. // Setup Content
  101. pnlContent.Visible = (Content != "");
  102. if (Content != "")
  103. {
  104. AdjustLabelHeight(lbContent);
  105. pnlContent.Height = lbContent.Height + 4;
  106. form_height += pnlContent.Height;
  107. }
  108. bool show_verify_checkbox = (cbVerify.Text != "");
  109. cbVerify.Visible = show_verify_checkbox;
  110. // Setup Expanded Info and Buttons panels
  111. if (ExpandedInfo == "")
  112. {
  113. pnlExpandedInfo.Visible = false;
  114. lbShowHideDetails.Visible = false;
  115. cbVerify.Top = 12;
  116. pnlButtons.Height = 40;
  117. }
  118. else
  119. {
  120. AdjustLabelHeight(lbExpandedInfo);
  121. pnlExpandedInfo.Height = lbExpandedInfo.Height + 4;
  122. pnlExpandedInfo.Visible = m_Expanded;
  123. lbShowHideDetails.Text = (m_Expanded ? " Hide details" : " Show details");
  124. lbShowHideDetails.ImageIndex = (m_Expanded ? 0 : 3);
  125. if (!show_verify_checkbox)
  126. pnlButtons.Height = 40;
  127. if (m_Expanded)
  128. form_height += pnlExpandedInfo.Height;
  129. }
  130. // Setup RadioButtons
  131. pnlRadioButtons.Visible = (m_radioButtons != "");
  132. if (m_radioButtons != "")
  133. {
  134. string[] arr = m_radioButtons.Split(new char[] { '|' });
  135. int pnl_height = 12;
  136. for (int i = 0; i < arr.Length; i++)
  137. {
  138. RadioButton rb = new RadioButton();
  139. rb.Parent = pnlRadioButtons;
  140. rb.Location = new Point(60, 4 + (i * rb.Height));
  141. rb.Text = arr[i];
  142. rb.Tag = i;
  143. rb.Checked = (m_initialRadioButtonIndex == i);
  144. rb.Width = this.Width - rb.Left - 15;
  145. pnl_height += rb.Height;
  146. m_radioButtonCtrls.Add(rb);
  147. }
  148. pnlRadioButtons.Height = pnl_height;
  149. form_height += pnlRadioButtons.Height;
  150. }
  151. // Setup CommandButtons
  152. pnlCommandButtons.Visible = (m_commandButtons != "");
  153. if (m_commandButtons != "")
  154. {
  155. string[] arr = m_commandButtons.Split(new char[] { '|' });
  156. int t = 8;
  157. int pnl_height = 16;
  158. for (int i = 0; i < arr.Length; i++)
  159. {
  160. CommandButton btn = new CommandButton();
  161. btn.Parent = pnlCommandButtons;
  162. btn.Location = new Point(50, t);
  163. if (m_isVista) // <- tweak font if vista
  164. btn.Font = new Font(btn.Font, FontStyle.Regular);
  165. btn.Text = arr[i];
  166. btn.Size = new Size(this.Width - btn.Left - 15, btn.GetBestHeight());
  167. t += btn.Height;
  168. pnl_height += btn.Height;
  169. btn.Tag = i;
  170. btn.Click += new EventHandler(CommandButton_Click);
  171. }
  172. pnlCommandButtons.Height = pnl_height;
  173. form_height += pnlCommandButtons.Height;
  174. }
  175. // Setup Buttons
  176. switch (m_Buttons)
  177. {
  178. case eTaskDialogButtons.YesNo:
  179. bt1.Visible = false;
  180. bt2.Text = "&Yes";
  181. bt2.DialogResult = DialogResult.Yes;
  182. bt3.Text = "&No";
  183. bt3.DialogResult = DialogResult.No;
  184. this.AcceptButton = bt2;
  185. this.CancelButton = bt3;
  186. break;
  187. case eTaskDialogButtons.YesNoCancel:
  188. bt1.Text = "&Yes";
  189. bt1.DialogResult = DialogResult.Yes;
  190. bt2.Text = "&No";
  191. bt2.DialogResult = DialogResult.No;
  192. bt3.Text = "&Cancel";
  193. bt3.DialogResult = DialogResult.Cancel;
  194. this.AcceptButton = bt1;
  195. this.CancelButton = bt3;
  196. break;
  197. case eTaskDialogButtons.OKCancel:
  198. bt1.Visible = false;
  199. bt2.Text = "&OK";
  200. bt2.DialogResult = DialogResult.OK;
  201. bt3.Text = "&Cancel";
  202. bt3.DialogResult = DialogResult.Cancel;
  203. this.AcceptButton = bt2;
  204. this.CancelButton = bt3;
  205. break;
  206. case eTaskDialogButtons.OK:
  207. bt1.Visible = false;
  208. bt2.Visible = false;
  209. bt3.Text = "&OK";
  210. bt3.DialogResult = DialogResult.OK;
  211. this.AcceptButton = bt3;
  212. this.CancelButton = bt3;
  213. break;
  214. case eTaskDialogButtons.Close:
  215. bt1.Visible = false;
  216. bt2.Visible = false;
  217. bt3.Text = "&Close";
  218. bt3.DialogResult = DialogResult.Cancel;
  219. this.CancelButton = bt3;
  220. break;
  221. case eTaskDialogButtons.Cancel:
  222. bt1.Visible = false;
  223. bt2.Visible = false;
  224. bt3.Text = "&Cancel";
  225. bt3.DialogResult = DialogResult.Cancel;
  226. this.CancelButton = bt3;
  227. break;
  228. case eTaskDialogButtons.None:
  229. bt1.Visible = false;
  230. bt2.Visible = false;
  231. bt3.Visible = false;
  232. break;
  233. }
  234. this.ControlBox = (Buttons == eTaskDialogButtons.Cancel ||
  235. Buttons == eTaskDialogButtons.Close ||
  236. Buttons == eTaskDialogButtons.OKCancel ||
  237. Buttons == eTaskDialogButtons.YesNoCancel);
  238. if (!show_verify_checkbox && ExpandedInfo == "" && m_Buttons == eTaskDialogButtons.None)
  239. pnlButtons.Visible = false;
  240. else
  241. form_height += pnlButtons.Height;
  242. pnlFooter.Visible = (Footer != "");
  243. if (Footer != "")
  244. {
  245. AdjustLabelHeight(lbFooter);
  246. pnlFooter.Height = Math.Max(28, lbFooter.Height + 16);
  247. switch (m_footerIcon)
  248. {
  249. case eSysIcons.Information: imgFooter.Image = SystemIcons.Information.ToBitmap().GetThumbnailImage(16, 16, null, IntPtr.Zero); break;
  250. case eSysIcons.Question: imgFooter.Image = SystemIcons.Question.ToBitmap().GetThumbnailImage(16, 16, null, IntPtr.Zero); break;
  251. case eSysIcons.Warning: imgFooter.Image = SystemIcons.Warning.ToBitmap().GetThumbnailImage(16, 16, null, IntPtr.Zero); break;
  252. case eSysIcons.Error: imgFooter.Image = SystemIcons.Error.ToBitmap().GetThumbnailImage(16, 16, null, IntPtr.Zero); break;
  253. }
  254. form_height += pnlFooter.Height;
  255. }
  256. this.ClientSize = new Size(ClientSize.Width, form_height);
  257. m_formBuilt = true;
  258. }
  259. //--------------------------------------------------------------------------------
  260. // utility function for setting a Label's height
  261. void AdjustLabelHeight(Label lb)
  262. {
  263. string text = lb.Text;
  264. Font textFont = lb.Font;
  265. SizeF layoutSize = new SizeF(lb.ClientSize.Width, 5000.0F);
  266. Graphics g = Graphics.FromHwnd(lb.Handle);
  267. SizeF stringSize = g.MeasureString(text, textFont, layoutSize);
  268. lb.Height = (int)stringSize.Height + 4;
  269. g.Dispose();
  270. }
  271. #endregion
  272. //--------------------------------------------------------------------------------
  273. #region EVENTS
  274. //--------------------------------------------------------------------------------
  275. void CommandButton_Click(object sender, EventArgs e)
  276. {
  277. m_commandButtonClicked = (int)((CommandButton)sender).Tag;
  278. this.DialogResult = DialogResult.OK;
  279. }
  280. //--------------------------------------------------------------------------------
  281. protected override void OnLoad(EventArgs e)
  282. {
  283. base.OnLoad(e);
  284. }
  285. //--------------------------------------------------------------------------------
  286. protected override void OnShown(EventArgs e)
  287. {
  288. if (!m_formBuilt)
  289. throw new Exception("frmTaskDialog : Please call .BuildForm() before showing the TaskDialog");
  290. base.OnShown(e);
  291. }
  292. //--------------------------------------------------------------------------------
  293. private void lbDetails_MouseEnter(object sender, EventArgs e)
  294. {
  295. lbShowHideDetails.ImageIndex = (m_Expanded ? 1 : 4);
  296. }
  297. //--------------------------------------------------------------------------------
  298. private void lbDetails_MouseLeave(object sender, EventArgs e)
  299. {
  300. lbShowHideDetails.ImageIndex = (m_Expanded ? 0 : 3);
  301. }
  302. //--------------------------------------------------------------------------------
  303. private void lbDetails_MouseUp(object sender, MouseEventArgs e)
  304. {
  305. lbShowHideDetails.ImageIndex = (m_Expanded ? 1 : 4);
  306. }
  307. //--------------------------------------------------------------------------------
  308. private void lbDetails_MouseDown(object sender, MouseEventArgs e)
  309. {
  310. lbShowHideDetails.ImageIndex =(m_Expanded ? 2 : 5);
  311. }
  312. //--------------------------------------------------------------------------------
  313. private void lbDetails_Click(object sender, EventArgs e)
  314. {
  315. m_Expanded = !m_Expanded;
  316. pnlExpandedInfo.Visible = m_Expanded;
  317. lbShowHideDetails.Text = (m_Expanded ? " Hide details" : " Show details");
  318. if (m_Expanded)
  319. this.Height += pnlExpandedInfo.Height;
  320. else
  321. this.Height -= pnlExpandedInfo.Height;
  322. }
  323. #endregion
  324. //--------------------------------------------------------------------------------
  325. }
  326. }