TaskDialog.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. using PaintDotNet.SystemLayer;
  2. using System;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. namespace PaintDotNet
  6. {
  7. public static class TaskDialog
  8. {
  9. public static int DefaultPixelWidth96Dpi = 300;
  10. public static TaskButton Show(
  11. IWin32Window owner,
  12. Icon formIcon,
  13. string formTitle,
  14. Image taskImage,
  15. string introText,
  16. TaskButton[] taskButtons,
  17. TaskButton acceptTaskButton,
  18. TaskButton cancelTaskButton)
  19. {
  20. return Show(owner, formIcon, formTitle, taskImage, true, introText,
  21. taskButtons, acceptTaskButton, cancelTaskButton, false, 0);
  22. }
  23. public static TaskButton Show(
  24. IWin32Window owner,
  25. Icon formIcon,
  26. string formTitle,
  27. Image taskImage,
  28. bool scaleTaskImageWithDpi,
  29. string introText,
  30. TaskButton[] taskButtons,
  31. TaskButton acceptTaskButton,
  32. TaskButton cancelTaskButton,
  33. bool showCheckbox,
  34. int conflictNum)
  35. {
  36. bool useChecked;
  37. return Show(owner, formIcon, formTitle, taskImage, scaleTaskImageWithDpi, introText,
  38. taskButtons, acceptTaskButton, cancelTaskButton, DefaultPixelWidth96Dpi, showCheckbox, conflictNum, out useChecked);
  39. }
  40. public static TaskButton Show(
  41. IWin32Window owner,
  42. Icon formIcon,
  43. string formTitle,
  44. Image taskImage,
  45. bool scaleTaskImageWithDpi,
  46. string introText,
  47. TaskButton[] taskButtons,
  48. TaskButton acceptTaskButton,
  49. TaskButton cancelTaskButton,
  50. int pixelWidth96Dpi,
  51. bool showCheckbox,
  52. int conflictNum,
  53. out bool useChecked)
  54. {
  55. return Show(owner, formIcon, formTitle, taskImage, scaleTaskImageWithDpi, introText,
  56. taskButtons, acceptTaskButton, cancelTaskButton, pixelWidth96Dpi, null, null, showCheckbox, conflictNum, out useChecked);
  57. }
  58. public static TaskButton Show(
  59. IWin32Window owner,
  60. Icon formIcon,
  61. string formTitle,
  62. Image taskImage,
  63. bool scaleTaskImageWithDpi,
  64. string introText,
  65. TaskButton[] taskButtons,
  66. TaskButton acceptTaskButton,
  67. TaskButton cancelTaskButton,
  68. int pixelWidth96Dpi,
  69. string auxButtonText,
  70. EventHandler auxButtonClickHandler,
  71. bool showCheckbox,
  72. int conflictNum,
  73. out bool useChecked)
  74. {
  75. using (TaskDialogForm form = new TaskDialogForm(showCheckbox, conflictNum))
  76. {
  77. form.Icon = formIcon;
  78. form.IntroText = introText;
  79. form.Text = formTitle;
  80. form.TaskImage = taskImage;
  81. form.ScaleTaskImageWithDpi = scaleTaskImageWithDpi;
  82. form.TaskButtons = taskButtons;
  83. form.AcceptTaskButton = acceptTaskButton;
  84. form.CancelTaskButton = cancelTaskButton;
  85. if (auxButtonText != null)
  86. {
  87. form.AuxButtonText = auxButtonText;
  88. }
  89. if (auxButtonClickHandler != null)
  90. {
  91. form.AuxButtonClick += auxButtonClickHandler;
  92. }
  93. int pixelWidth = UI.ScaleWidth(pixelWidth96Dpi);
  94. form.ClientSize = new Size(pixelWidth, form.ClientSize.Height);
  95. DialogResult dr = form.ShowDialog(owner);
  96. TaskButton result = form.DialogResult;
  97. useChecked = form.checkBox.Checked;
  98. return result;
  99. }
  100. }
  101. private sealed class TaskDialogForm : PdnBaseForm
  102. {
  103. public bool showCheckbox = true;
  104. private PictureBox taskImagePB;
  105. private bool scaleTaskImageWithDpi;
  106. private Label introTextLabel;
  107. private TaskButton[] taskButtons;
  108. private CommandButton[] commandButtons;
  109. private HeaderLabel separator;
  110. private TaskButton acceptTaskButton;
  111. private TaskButton cancelTaskButton;
  112. private TaskButton dialogResult;
  113. public CheckBox checkBox;
  114. private Button auxButton;
  115. public string AuxButtonText
  116. {
  117. get
  118. {
  119. return this.auxButton.Text;
  120. }
  121. set
  122. {
  123. this.auxButton.Text = value;
  124. PerformLayout();
  125. }
  126. }
  127. public event EventHandler AuxButtonClick;
  128. private void OnAuxButtonClick()
  129. {
  130. if (AuxButtonClick != null)
  131. {
  132. AuxButtonClick(this, EventArgs.Empty);
  133. }
  134. }
  135. public new TaskButton DialogResult
  136. {
  137. get
  138. {
  139. return this.dialogResult;
  140. }
  141. }
  142. public Image TaskImage
  143. {
  144. get
  145. {
  146. return this.taskImagePB.Image;
  147. }
  148. set
  149. {
  150. this.taskImagePB.Image = value;
  151. PerformLayout();
  152. Invalidate(true);
  153. }
  154. }
  155. public bool ScaleTaskImageWithDpi
  156. {
  157. get
  158. {
  159. return this.scaleTaskImageWithDpi;
  160. }
  161. set
  162. {
  163. this.scaleTaskImageWithDpi = value;
  164. PerformLayout();
  165. Invalidate(true);
  166. }
  167. }
  168. public string IntroText
  169. {
  170. get
  171. {
  172. return this.introTextLabel.Text;
  173. }
  174. set
  175. {
  176. this.introTextLabel.Text = value;
  177. PerformLayout();
  178. Invalidate(true);
  179. }
  180. }
  181. public TaskButton[] TaskButtons
  182. {
  183. get
  184. {
  185. return (TaskButton[])this.taskButtons.Clone();
  186. }
  187. set
  188. {
  189. this.taskButtons = (TaskButton[])value.Clone();
  190. InitCommandButtons();
  191. PerformLayout();
  192. Invalidate(true);
  193. }
  194. }
  195. public TaskButton AcceptTaskButton
  196. {
  197. get
  198. {
  199. return this.acceptTaskButton;
  200. }
  201. set
  202. {
  203. this.acceptTaskButton = value;
  204. IButtonControl newAcceptButton = null;
  205. for (int i = 0; i < this.commandButtons.Length; ++i)
  206. {
  207. TaskButton asTaskButton = this.commandButtons[i].Tag as TaskButton;
  208. if (this.acceptTaskButton == asTaskButton)
  209. {
  210. newAcceptButton = this.commandButtons[i];
  211. }
  212. }
  213. AcceptButton = newAcceptButton;
  214. }
  215. }
  216. public TaskButton CancelTaskButton
  217. {
  218. get
  219. {
  220. return this.cancelTaskButton;
  221. }
  222. set
  223. {
  224. this.cancelTaskButton = value;
  225. IButtonControl newCancelButton = null;
  226. for (int i = 0; i < this.commandButtons.Length; ++i)
  227. {
  228. TaskButton asTaskButton = this.commandButtons[i].Tag as TaskButton;
  229. if (this.cancelTaskButton == asTaskButton)
  230. {
  231. newCancelButton = this.commandButtons[i];
  232. }
  233. }
  234. CancelButton = newCancelButton;
  235. }
  236. }
  237. /// <summary>
  238. ///
  239. /// </summary>
  240. /// <param name="showCheckbox">ÊÇ·ñÏÔʾcheckbox</param>
  241. /// <param name="num">³åÍ»ÊýÁ¿</param>
  242. public TaskDialogForm(bool showCheckbox, int num)
  243. {
  244. this.showCheckbox = showCheckbox;
  245. InitializeComponent();
  246. if (!this.showCheckbox)
  247. {
  248. this.checkBox.Visible = false;
  249. }
  250. else
  251. {
  252. if (num > 0)
  253. {
  254. this.checkBox.Text = PdnResources.GetString("Menu.Forafter.text") + num + PdnResources.GetString("Menu.Fiveoperation.text");
  255. this.checkBox.Visible = true;
  256. }else
  257. {
  258. this.checkBox.Visible = false;
  259. }
  260. }
  261. }
  262. private void InitializeComponent()
  263. {
  264. SuspendLayout();
  265. this.introTextLabel = new Label();
  266. this.auxButton = new Button();
  267. this.taskImagePB = new PictureBox();
  268. this.separator = new HeaderLabel();
  269. this.checkBox = new CheckBox();
  270. //
  271. // checkBox
  272. //
  273. this.checkBox.Dock = DockStyle.Bottom;
  274. this.checkBox.Padding = new Padding(10, 0, 0, 0);
  275. //
  276. // introTextLabel
  277. //
  278. this.introTextLabel.Name = "introTextLabel";
  279. //
  280. // taskImagePB
  281. //
  282. this.taskImagePB.Name = "taskImagePB";
  283. this.taskImagePB.SizeMode = PictureBoxSizeMode.StretchImage;
  284. //
  285. // auxButton
  286. //
  287. this.auxButton.Name = "auxButton";
  288. this.auxButton.AutoSize = true;
  289. this.auxButton.FlatStyle = FlatStyle.System;
  290. this.auxButton.Visible = false;
  291. this.auxButton.Click +=
  292. delegate (object sender, EventArgs e)
  293. {
  294. OnAuxButtonClick();
  295. };
  296. //
  297. // separator
  298. //
  299. this.separator.Name = "separator";
  300. this.separator.RightMargin = 0;
  301. //
  302. // TaskDialogForm
  303. //
  304. this.Name = "TaskDialogForm";
  305. this.ClientSize = new Size(300, 100);
  306. this.FormBorderStyle = FormBorderStyle.FixedDialog;
  307. this.MinimizeBox = false;
  308. this.MaximizeBox = false;
  309. this.ShowInTaskbar = false;
  310. this.StartPosition = FormStartPosition.CenterParent;
  311. this.Controls.Add(this.introTextLabel);
  312. this.Controls.Add(this.taskImagePB);
  313. this.Controls.Add(this.auxButton);
  314. this.Controls.Add(this.separator);
  315. this.Controls.Add(this.checkBox);
  316. ResumeLayout();
  317. }
  318. protected override void OnLayout(LayoutEventArgs levent)
  319. {
  320. int leftMargin = UI.ScaleWidth(8);
  321. int rightMargin = UI.ScaleWidth(8);
  322. int topMargin = UI.ScaleHeight(8);
  323. int bottomMargin = UI.ScaleHeight(8);
  324. int imageToIntroHMargin = UI.ScaleWidth(8);
  325. int topSectionToLinksVMargin = UI.ScaleHeight(8);
  326. int commandButtonVMargin = UI.ScaleHeight(0);
  327. int afterCommandButtonsVMargin = UI.ScaleHeight(8);
  328. int insetWidth = ClientSize.Width - leftMargin - rightMargin;
  329. if (this.taskImagePB.Image == null)
  330. {
  331. this.taskImagePB.Location = new Point(0, topMargin);
  332. this.taskImagePB.Size = new Size(0, 0);
  333. this.taskImagePB.Visible = false;
  334. }
  335. else
  336. {
  337. this.taskImagePB.Location = new Point(leftMargin, topMargin);
  338. if (this.scaleTaskImageWithDpi)
  339. {
  340. this.taskImagePB.Size = UI.ScaleSize(this.taskImagePB.Image.Size);
  341. }
  342. else
  343. {
  344. this.taskImagePB.Size = this.taskImagePB.Image.Size;
  345. }
  346. this.taskImagePB.Visible = true;
  347. }
  348. this.introTextLabel.Location = new Point(this.taskImagePB.Right + imageToIntroHMargin, this.taskImagePB.Top);
  349. this.introTextLabel.Width = ClientSize.Width - this.introTextLabel.Left - rightMargin;
  350. this.introTextLabel.Height = this.introTextLabel.GetPreferredSize(new Size(this.introTextLabel.Width, 1)).Height;
  351. int y = Math.Max(this.taskImagePB.Bottom, this.introTextLabel.Bottom);
  352. y += topSectionToLinksVMargin;
  353. if (!string.IsNullOrEmpty(this.auxButton.Text))
  354. {
  355. this.auxButton.Visible = true;
  356. this.auxButton.Location = new Point(leftMargin, y);
  357. this.auxButton.PerformLayout();
  358. y += this.auxButton.Height;
  359. y += topSectionToLinksVMargin;
  360. }
  361. else
  362. {
  363. this.auxButton.Visible = false;
  364. }
  365. if (this.commandButtons != null)
  366. {
  367. this.separator.Location = new Point(leftMargin, y);
  368. this.separator.Width = insetWidth;
  369. y += this.separator.Height;
  370. for (int i = 0; i < this.commandButtons.Length; ++i)
  371. {
  372. this.commandButtons[i].Location = new Point(leftMargin, y);
  373. this.commandButtons[i].Width = insetWidth;
  374. this.commandButtons[i].PerformLayout();
  375. y += this.commandButtons[i].Height + commandButtonVMargin;
  376. }
  377. y += afterCommandButtonsVMargin;
  378. }
  379. this.ClientSize = new Size(ClientSize.Width, (this.showCheckbox ? (y + 20) : y));
  380. base.OnLayout(levent);
  381. }
  382. private void InitCommandButtons()
  383. {
  384. SuspendLayout();
  385. if (this.commandButtons != null)
  386. {
  387. foreach (CommandButton commandButton in this.commandButtons)
  388. {
  389. Controls.Remove(commandButton);
  390. commandButton.Tag = null;
  391. commandButton.Click -= CommandButton_Click;
  392. commandButton.Dispose();
  393. }
  394. this.commandButtons = null;
  395. }
  396. this.commandButtons = new CommandButton[this.taskButtons.Length];
  397. IButtonControl newAcceptButton = null;
  398. IButtonControl newCancelButton = null;
  399. for (int i = 0; i < this.commandButtons.Length; ++i)
  400. {
  401. TaskButton taskButton = this.taskButtons[i];
  402. CommandButton commandButton = new CommandButton();
  403. commandButton.ActionText = taskButton.ActionText;
  404. commandButton.ActionImage = taskButton.Image;
  405. commandButton.AutoSize = true;
  406. commandButton.ExplanationText = taskButton.ExplanationText;
  407. commandButton.Tag = taskButton;
  408. commandButton.Click += CommandButton_Click;
  409. this.commandButtons[i] = commandButton;
  410. Controls.Add(commandButton);
  411. if (this.acceptTaskButton == taskButton)
  412. {
  413. newAcceptButton = commandButton;
  414. }
  415. if (this.cancelTaskButton == taskButton)
  416. {
  417. newCancelButton = commandButton;
  418. }
  419. }
  420. AcceptButton = newAcceptButton;
  421. CancelButton = newCancelButton;
  422. if (newAcceptButton != null && newAcceptButton is Control)
  423. {
  424. ((Control)newAcceptButton).Select();
  425. }
  426. ResumeLayout();
  427. }
  428. private void CommandButton_Click(object sender, EventArgs e)
  429. {
  430. CommandButton commandButton = (CommandButton)sender;
  431. if (commandButton.keyCode == 0)
  432. {
  433. for (int i = 0; i < this.taskButtons.Length; i++)
  434. {
  435. if (this.taskButtons[i].ActionText == PdnResources.GetString("CloseWorkspaceAction.SaveButton.ActionText") + "( Y )")
  436. {
  437. this.dialogResult = this.taskButtons[i];
  438. commandButton.keyCode = -1;
  439. break;
  440. }
  441. }
  442. }
  443. else if (commandButton.keyCode == 1)
  444. {
  445. for (int i = 0; i < this.taskButtons.Length; i++)
  446. {
  447. if (this.taskButtons[i].ActionText == PdnResources.GetString("CloseWorkspaceAction.DontSaveButton.ActionText") + "( N )")
  448. {
  449. this.dialogResult = this.taskButtons[i];
  450. commandButton.keyCode = -1;
  451. break;
  452. }
  453. }
  454. }
  455. else
  456. this.dialogResult = (TaskButton)commandButton.Tag;
  457. Close();
  458. }
  459. }
  460. }
  461. }