PageSetupDialog.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2006 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jonathan Chambers ([email protected])
  24. //
  25. using System;
  26. using System.ComponentModel;
  27. using System.ComponentModel.Design;
  28. using System.ComponentModel.Design.Serialization;
  29. using System.Collections;
  30. using System.Diagnostics;
  31. using System.Drawing;
  32. using System.Drawing.Printing;
  33. using System.Globalization;
  34. using System.Reflection;
  35. namespace System.Windows.Forms {
  36. [DefaultProperty("Document")]
  37. public sealed class PageSetupDialog : CommonDialog {
  38. #region Local variables
  39. private PrintDocument document;
  40. private PageSettings page_settings;
  41. private PrinterSettings printer_settings;
  42. private Margins min_margins;
  43. private bool allow_margins;
  44. private bool allow_orientation;
  45. private bool allow_paper;
  46. private bool allow_printer;
  47. private bool show_help;
  48. private bool show_network;
  49. private GroupBox groupbox_paper;
  50. private Label label_source;
  51. private Label label_size;
  52. private GroupBox groupbox_orientation;
  53. private RadioButton radio_landscape;
  54. private RadioButton radio_portrait;
  55. private GroupBox groupbox_margin;
  56. private Label label_left;
  57. private Button button_help;
  58. private Button button_ok;
  59. private Button button_cancel;
  60. private Button button_printer;
  61. private Label label_top;
  62. private Label label_right;
  63. private Label label_bottom;
  64. private NumericTextBox textbox_left;
  65. private NumericTextBox textbox_top;
  66. private NumericTextBox textbox_right;
  67. private NumericTextBox textbox_bottom;
  68. private ComboBox combobox_source;
  69. private ComboBox combobox_size;
  70. private PagePreview pagePreview;
  71. #endregion // Local variables
  72. #region Public Constructors
  73. public PageSetupDialog ()
  74. {
  75. InitializeComponent();
  76. Reset ();
  77. }
  78. #endregion // Public Constructors
  79. #region Public Instance Methods
  80. public override void Reset ()
  81. {
  82. AllowMargins = true;
  83. AllowOrientation = true;
  84. AllowPaper = true;
  85. AllowPrinter = true;
  86. ShowHelp = false;
  87. ShowNetwork = true;
  88. MinMargins = new Margins (0, 0, 0, 0);
  89. PrinterSettings = null;
  90. PageSettings = null;
  91. Document = null;
  92. }
  93. #endregion // Public Instance Methods
  94. #region Public Instance Properties
  95. [DefaultValue(true)]
  96. public bool AllowMargins {
  97. get { return allow_margins; }
  98. set { allow_margins = value; }
  99. }
  100. [DefaultValue(true)]
  101. public bool AllowOrientation {
  102. get { return allow_orientation; }
  103. set { allow_orientation = value; }
  104. }
  105. [DefaultValue(true)]
  106. public bool AllowPaper {
  107. get { return allow_paper; }
  108. set { allow_paper = value; }
  109. }
  110. [DefaultValue(true)]
  111. public bool AllowPrinter {
  112. get { return allow_printer; }
  113. set { allow_printer = value; }
  114. }
  115. [DefaultValue(null)]
  116. public PrintDocument Document {
  117. get { return document; }
  118. set {
  119. document = value;
  120. if (document != null) {
  121. printer_settings = document.PrinterSettings;
  122. page_settings = document.DefaultPageSettings;
  123. }
  124. }
  125. }
  126. public Margins MinMargins {
  127. get { return min_margins; }
  128. set { min_margins = value; }
  129. }
  130. [Browsable(false)]
  131. [DefaultValue(null)]
  132. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  133. public PageSettings PageSettings {
  134. get { return page_settings; }
  135. set {
  136. page_settings = value;
  137. document = null;
  138. }
  139. }
  140. [Browsable(false)]
  141. [DefaultValue(null)]
  142. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  143. public PrinterSettings PrinterSettings {
  144. get { return printer_settings; }
  145. set {
  146. printer_settings = value;
  147. document = null;
  148. }
  149. }
  150. [DefaultValue(false)]
  151. public bool ShowHelp {
  152. get { return show_help; }
  153. set {
  154. if (value != show_help) {
  155. show_help = value;
  156. ShowHelpButton ();
  157. }
  158. }
  159. }
  160. [DefaultValue(true)]
  161. public bool ShowNetwork {
  162. get { return show_network; }
  163. set { show_network = value; }
  164. }
  165. #endregion // Public Instance Properties
  166. #region Protected Instance Methods
  167. protected override bool RunDialog (IntPtr hwnd)
  168. {
  169. try {
  170. SetPrinterDetails ();
  171. return true;
  172. }
  173. catch (Exception e) {
  174. MessageBox.Show (e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  175. return false;
  176. }
  177. }
  178. #endregion // Protected Instance Methods
  179. #region Private Helper
  180. private void InitializeComponent()
  181. {
  182. this.groupbox_paper = new System.Windows.Forms.GroupBox();
  183. this.combobox_source = new System.Windows.Forms.ComboBox();
  184. this.combobox_size = new System.Windows.Forms.ComboBox();
  185. this.label_source = new System.Windows.Forms.Label();
  186. this.label_size = new System.Windows.Forms.Label();
  187. this.groupbox_orientation = new System.Windows.Forms.GroupBox();
  188. this.radio_landscape = new System.Windows.Forms.RadioButton();
  189. this.radio_portrait = new System.Windows.Forms.RadioButton();
  190. this.groupbox_margin = new System.Windows.Forms.GroupBox();
  191. this.label_left = new System.Windows.Forms.Label();
  192. this.button_ok = new System.Windows.Forms.Button();
  193. this.button_cancel = new System.Windows.Forms.Button();
  194. this.button_printer = new System.Windows.Forms.Button();
  195. this.label_top = new System.Windows.Forms.Label();
  196. this.label_right = new System.Windows.Forms.Label();
  197. this.label_bottom = new System.Windows.Forms.Label();
  198. this.textbox_left = new System.Windows.Forms.NumericTextBox();
  199. this.textbox_top = new System.Windows.Forms.NumericTextBox();
  200. this.textbox_right = new System.Windows.Forms.NumericTextBox();
  201. this.textbox_bottom = new System.Windows.Forms.NumericTextBox();
  202. this.pagePreview = new PagePreview ();
  203. this.groupbox_paper.SuspendLayout();
  204. this.groupbox_orientation.SuspendLayout();
  205. this.groupbox_margin.SuspendLayout();
  206. form.SuspendLayout();
  207. //
  208. // groupbox_paper
  209. //
  210. this.groupbox_paper.Controls.Add(this.combobox_source);
  211. this.groupbox_paper.Controls.Add(this.combobox_size);
  212. this.groupbox_paper.Controls.Add(this.label_source);
  213. this.groupbox_paper.Controls.Add(this.label_size);
  214. this.groupbox_paper.Location = new System.Drawing.Point(12, 157);
  215. this.groupbox_paper.Name = "groupbox_paper";
  216. this.groupbox_paper.Size = new System.Drawing.Size(336, 90);
  217. this.groupbox_paper.TabIndex = 0;
  218. this.groupbox_paper.TabStop = false;
  219. this.groupbox_paper.Text = "Paper";
  220. //
  221. // combobox_source
  222. //
  223. this.combobox_source.Location = new System.Drawing.Point(84, 54);
  224. this.combobox_source.Name = "combobox_source";
  225. this.combobox_source.Size = new System.Drawing.Size(240, 21);
  226. this.combobox_source.TabIndex = 3;
  227. //
  228. // combobox_size
  229. //
  230. this.combobox_size.ItemHeight = 13;
  231. this.combobox_size.Location = new System.Drawing.Point(84, 22);
  232. this.combobox_size.Name = "combobox_size";
  233. this.combobox_size.Size = new System.Drawing.Size(240, 21);
  234. this.combobox_size.TabIndex = 2;
  235. this.combobox_size.SelectedIndexChanged += new EventHandler(this.OnPaperSizeChange);
  236. //
  237. // label_source
  238. //
  239. this.label_source.Location = new System.Drawing.Point(13, 58);
  240. this.label_source.Name = "label_source";
  241. this.label_source.Size = new System.Drawing.Size(48, 16);
  242. this.label_source.TabIndex = 1;
  243. this.label_source.Text = "&Source:";
  244. //
  245. // label_size
  246. //
  247. this.label_size.Location = new System.Drawing.Point(13, 25);
  248. this.label_size.Name = "label_size";
  249. this.label_size.Size = new System.Drawing.Size(52, 16);
  250. this.label_size.TabIndex = 0;
  251. this.label_size.Text = "Si&ze:";
  252. //
  253. // groupbox_orientation
  254. //
  255. this.groupbox_orientation.Controls.Add(this.radio_landscape);
  256. this.groupbox_orientation.Controls.Add(this.radio_portrait);
  257. this.groupbox_orientation.Location = new System.Drawing.Point(12, 255);
  258. this.groupbox_orientation.Name = "groupbox_orientation";
  259. this.groupbox_orientation.Size = new System.Drawing.Size(96, 90);
  260. this.groupbox_orientation.TabIndex = 1;
  261. this.groupbox_orientation.TabStop = false;
  262. this.groupbox_orientation.Text = "Orientation";
  263. //
  264. // radio_landscape
  265. //
  266. this.radio_landscape.Location = new System.Drawing.Point(13, 52);
  267. this.radio_landscape.Name = "radio_landscape";
  268. this.radio_landscape.Size = new System.Drawing.Size(80, 24);
  269. this.radio_landscape.TabIndex = 7;
  270. this.radio_landscape.Text = "L&andscape";
  271. this.radio_landscape.CheckedChanged += new EventHandler(this.OnLandscapeChange);
  272. //
  273. // radio_portrait
  274. //
  275. this.radio_portrait.Location = new System.Drawing.Point(13, 19);
  276. this.radio_portrait.Name = "radio_portrait";
  277. this.radio_portrait.Size = new System.Drawing.Size(72, 24);
  278. this.radio_portrait.TabIndex = 6;
  279. this.radio_portrait.Text = "P&ortrait";
  280. //
  281. // groupbox_margin
  282. //
  283. this.groupbox_margin.Controls.Add(this.textbox_bottom);
  284. this.groupbox_margin.Controls.Add(this.textbox_right);
  285. this.groupbox_margin.Controls.Add(this.textbox_top);
  286. this.groupbox_margin.Controls.Add(this.textbox_left);
  287. this.groupbox_margin.Controls.Add(this.label_bottom);
  288. this.groupbox_margin.Controls.Add(this.label_right);
  289. this.groupbox_margin.Controls.Add(this.label_top);
  290. this.groupbox_margin.Controls.Add(this.label_left);
  291. this.groupbox_margin.Location = new System.Drawing.Point(120, 255);
  292. this.groupbox_margin.Name = "groupbox_margin";
  293. this.groupbox_margin.Size = new System.Drawing.Size(228, 90);
  294. this.groupbox_margin.TabIndex = 2;
  295. this.groupbox_margin.TabStop = false;
  296. this.groupbox_margin.Text = LocalizedLengthUnit ();
  297. //
  298. // label_left
  299. //
  300. this.label_left.Location = new System.Drawing.Point(11, 25);
  301. this.label_left.Name = "label_left";
  302. this.label_left.Size = new System.Drawing.Size(40, 23);
  303. this.label_left.TabIndex = 0;
  304. this.label_left.Text = "&Left:";
  305. //
  306. // button_ok
  307. //
  308. this.button_ok.Location = new System.Drawing.Point(120, 358);
  309. this.button_ok.Name = "button_ok";
  310. this.button_ok.Size = new System.Drawing.Size(72, 23);
  311. this.button_ok.TabIndex = 3;
  312. this.button_ok.Text = "OK";
  313. this.button_ok.Click += new EventHandler (OnClickOkButton);
  314. //
  315. // button_cancel
  316. //
  317. this.button_cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  318. this.button_cancel.Location = new System.Drawing.Point(198, 358);
  319. this.button_cancel.Name = "button_cancel";
  320. this.button_cancel.Size = new System.Drawing.Size(72, 23);
  321. this.button_cancel.TabIndex = 4;
  322. this.button_cancel.Text = "Cancel";
  323. //
  324. // button_printer
  325. //
  326. this.button_printer.Location = new System.Drawing.Point(276, 358);
  327. this.button_printer.Name = "button_printer";
  328. this.button_printer.Size = new System.Drawing.Size(72, 23);
  329. this.button_printer.TabIndex = 5;
  330. this.button_printer.Text = "&Printer...";
  331. this.button_printer.Click += new EventHandler (OnClickPrinterButton);
  332. //
  333. // label_top
  334. //
  335. this.label_top.Location = new System.Drawing.Point(11, 57);
  336. this.label_top.Name = "label_top";
  337. this.label_top.Size = new System.Drawing.Size(40, 23);
  338. this.label_top.TabIndex = 1;
  339. this.label_top.Text = "&Top:";
  340. //
  341. // label_right
  342. //
  343. this.label_right.Location = new System.Drawing.Point(124, 25);
  344. this.label_right.Name = "label_right";
  345. this.label_right.Size = new System.Drawing.Size(40, 23);
  346. this.label_right.TabIndex = 2;
  347. this.label_right.Text = "&Right:";
  348. //
  349. // label_bottom
  350. //
  351. this.label_bottom.Location = new System.Drawing.Point(124, 57);
  352. this.label_bottom.Name = "label_bottom";
  353. this.label_bottom.Size = new System.Drawing.Size(40, 23);
  354. this.label_bottom.TabIndex = 3;
  355. this.label_bottom.Text = "&Bottom:";
  356. //
  357. // textbox_left
  358. //
  359. this.textbox_left.Location = new System.Drawing.Point(57, 21);
  360. this.textbox_left.Name = "textbox_left";
  361. this.textbox_left.Size = new System.Drawing.Size(48, 20);
  362. this.textbox_left.TabIndex = 4;
  363. this.textbox_left.TextChanged +=new EventHandler(OnMarginChange);
  364. //
  365. // textbox_top
  366. //
  367. this.textbox_top.Location = new System.Drawing.Point(57, 54);
  368. this.textbox_top.Name = "textbox_top";
  369. this.textbox_top.Size = new System.Drawing.Size(48, 20);
  370. this.textbox_top.TabIndex = 5;
  371. this.textbox_top.TextChanged +=new EventHandler(OnMarginChange);
  372. //
  373. // textbox_right
  374. //
  375. this.textbox_right.Location = new System.Drawing.Point(171, 21);
  376. this.textbox_right.Name = "textbox_right";
  377. this.textbox_right.Size = new System.Drawing.Size(48, 20);
  378. this.textbox_right.TabIndex = 6;
  379. this.textbox_right.TextChanged +=new EventHandler(OnMarginChange);
  380. //
  381. // textbox_bottom
  382. //
  383. this.textbox_bottom.Location = new System.Drawing.Point(171, 54);
  384. this.textbox_bottom.Name = "textbox_bottom";
  385. this.textbox_bottom.Size = new System.Drawing.Size(48, 20);
  386. this.textbox_bottom.TabIndex = 7;
  387. this.textbox_bottom.TextChanged +=new EventHandler(OnMarginChange);
  388. //
  389. // pagePreview
  390. //
  391. this.pagePreview.Location = new System.Drawing.Point (130, 10);
  392. this.pagePreview.Name = "pagePreview";
  393. this.pagePreview.Size = new System.Drawing.Size (150, 150);
  394. this.pagePreview.TabIndex = 6;
  395. //
  396. // Form3
  397. //
  398. form.AcceptButton = this.button_ok;
  399. form.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  400. form.CancelButton = this.button_cancel;
  401. form.ClientSize = new System.Drawing.Size(360, 390);
  402. form.Controls.Add (this.pagePreview);
  403. form.Controls.Add (this.button_printer);
  404. form.Controls.Add(this.button_cancel);
  405. form.Controls.Add(this.button_ok);
  406. form.Controls.Add(this.groupbox_margin);
  407. form.Controls.Add(this.groupbox_orientation);
  408. form.Controls.Add(this.groupbox_paper);
  409. form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  410. form.HelpButton = true;
  411. form.MaximizeBox = false;
  412. form.MinimizeBox = false;
  413. form.Name = "Form3";
  414. form.ShowInTaskbar = false;
  415. form.Text = "Page Setup";
  416. this.groupbox_paper.ResumeLayout(false);
  417. this.groupbox_orientation.ResumeLayout(false);
  418. this.groupbox_margin.ResumeLayout(false);
  419. form.ResumeLayout(false);
  420. }
  421. static bool UseYardPound {
  422. get { return !RegionInfo.CurrentRegion.IsMetric; } }
  423. // .Net uses PrinterSettings property if it is not null.
  424. // Otherwise, it uses PageSettings.PrinterSettings to set values.
  425. // We use this property internally to automatically select the available one.
  426. PrinterSettings InternalPrinterSettings {
  427. get {
  428. return (printer_settings == null ? page_settings.PrinterSettings :
  429. printer_settings);
  430. }
  431. }
  432. private double ToLocalizedLength (int marginsUnit)
  433. {
  434. return UseYardPound ?
  435. PrinterUnitConvert.Convert (marginsUnit, PrinterUnit.ThousandthsOfAnInch, PrinterUnit.Display) :
  436. PrinterUnitConvert.Convert (marginsUnit, PrinterUnit.ThousandthsOfAnInch, PrinterUnit.TenthsOfAMillimeter);
  437. }
  438. private int FromLocalizedLength (double marginsUnit)
  439. {
  440. return (int)(UseYardPound ?
  441. PrinterUnitConvert.Convert (marginsUnit, PrinterUnit.Display, PrinterUnit.ThousandthsOfAnInch) :
  442. PrinterUnitConvert.Convert (marginsUnit, PrinterUnit.TenthsOfAMillimeter, PrinterUnit.ThousandthsOfAnInch));
  443. }
  444. private string LocalizedLengthUnit ()
  445. {
  446. return UseYardPound ? "Margins (inches)" : "Margins (millimeters)";
  447. }
  448. private void SetPrinterDetails ()
  449. {
  450. if (PageSettings == null)
  451. throw new ArgumentException ("PageSettings");
  452. combobox_size.Items.Clear ();
  453. foreach (PaperSize paper_size in InternalPrinterSettings.PaperSizes)
  454. combobox_size.Items.Add (paper_size.PaperName);
  455. combobox_size.SelectedItem = page_settings.PaperSize.PaperName;
  456. combobox_source.Items.Clear ();
  457. foreach (PaperSource paper_source in InternalPrinterSettings.PaperSources)
  458. combobox_source.Items.Add (paper_source.SourceName);
  459. combobox_source.SelectedItem = page_settings.PaperSource.SourceName;
  460. if (PageSettings.Landscape)
  461. radio_landscape.Checked = true;
  462. else
  463. radio_portrait.Checked = true;
  464. if (ShowHelp)
  465. ShowHelpButton ();
  466. Margins page_margins = PageSettings.Margins;
  467. Margins min_margins = MinMargins;
  468. // Update margin data
  469. textbox_top.Text = ToLocalizedLength (page_margins.Top).ToString ();
  470. textbox_bottom.Text = ToLocalizedLength (page_margins.Bottom).ToString ();
  471. textbox_left.Text = ToLocalizedLength (page_margins.Left).ToString ();
  472. textbox_right.Text = ToLocalizedLength (page_margins.Right).ToString ();
  473. textbox_top.Min = ToLocalizedLength (min_margins.Top);
  474. textbox_bottom.Min = ToLocalizedLength (min_margins.Bottom);
  475. textbox_left.Min = ToLocalizedLength (min_margins.Left);
  476. textbox_right.Min = ToLocalizedLength (min_margins.Right);
  477. button_printer.Enabled = AllowPrinter && PrinterSettings != null;
  478. groupbox_orientation.Enabled = AllowOrientation;
  479. groupbox_paper.Enabled = AllowPaper;
  480. groupbox_margin.Enabled = AllowMargins;
  481. pagePreview.Setup (PageSettings);
  482. }
  483. private void OnClickOkButton (object sender, EventArgs e)
  484. {
  485. if (combobox_size.SelectedItem != null) {
  486. foreach (PaperSize paper_size in InternalPrinterSettings.PaperSizes) {
  487. if (paper_size.PaperName == (string) combobox_size.SelectedItem) {
  488. PageSettings.PaperSize = paper_size;
  489. break;
  490. }
  491. }
  492. }
  493. if (combobox_source.SelectedItem != null) {
  494. foreach (PaperSource paper_source in InternalPrinterSettings.PaperSources) {
  495. if (paper_source.SourceName == (string) combobox_source.SelectedItem) {
  496. PageSettings.PaperSource = paper_source;
  497. break;
  498. }
  499. }
  500. }
  501. Margins margins = new Margins ();
  502. margins.Top = FromLocalizedLength (textbox_top.Value);
  503. margins.Bottom = FromLocalizedLength (textbox_bottom.Value);
  504. margins.Left = FromLocalizedLength (textbox_left.Value);
  505. margins.Right = FromLocalizedLength (textbox_right.Value);
  506. PageSettings.Margins = margins;
  507. PageSettings.Landscape = radio_landscape.Checked;
  508. form.DialogResult = DialogResult.OK;
  509. }
  510. void ShowHelpButton ()
  511. {
  512. if (button_help == null) {
  513. button_help = new Button ();
  514. button_help.Location = new System.Drawing.Point (12, 358);
  515. button_help.Name = "button_help";
  516. button_help.Size = new System.Drawing.Size (72, 23);
  517. button_help.Text = "&Help";
  518. form.Controls.Add (button_help);
  519. }
  520. button_help.Visible = show_help;
  521. }
  522. void OnClickPrinterButton (object sender, EventArgs args)
  523. {
  524. PrinterForm printer_helper_form = new PrinterForm (this);
  525. printer_helper_form.UpdateValues ();
  526. // Here update values for PrinterSettings
  527. if (printer_helper_form.ShowDialog () == DialogResult.OK)
  528. if (printer_helper_form.SelectedPrinter != PrinterSettings.PrinterName)
  529. PrinterSettings.PrinterName = printer_helper_form.SelectedPrinter;
  530. PageSettings = PrinterSettings.DefaultPageSettings;
  531. SetPrinterDetails ();
  532. button_ok.Select ();
  533. printer_helper_form.Dispose ();
  534. }
  535. void OnPaperSizeChange (object sender, EventArgs e)
  536. {
  537. if (combobox_size.SelectedItem != null) {
  538. foreach (PaperSize paper_size in InternalPrinterSettings.PaperSizes) {
  539. if (paper_size.PaperName == (string) combobox_size.SelectedItem) {
  540. pagePreview.SetSize (paper_size.Width, paper_size.Height);
  541. break;
  542. }
  543. }
  544. }
  545. }
  546. void OnMarginChange (object sender, EventArgs e)
  547. {
  548. pagePreview.SetMargins (
  549. FromLocalizedLength (textbox_left.Value),
  550. FromLocalizedLength (textbox_right.Value),
  551. FromLocalizedLength (textbox_top.Value),
  552. FromLocalizedLength (textbox_bottom.Value)
  553. );
  554. }
  555. void OnLandscapeChange (object sender, EventArgs e)
  556. {
  557. pagePreview.Landscape = radio_landscape.Checked;
  558. }
  559. #endregion // Private Helper
  560. class PrinterForm : Form
  561. {
  562. private System.Windows.Forms.GroupBox groupbox_printer;
  563. private System.Windows.Forms.ComboBox combobox_printers;
  564. private System.Windows.Forms.Label label_name;
  565. private System.Windows.Forms.Label label_status;
  566. private System.Windows.Forms.Button button_properties;
  567. private System.Windows.Forms.Button button_network;
  568. private System.Windows.Forms.Button button_cancel;
  569. private System.Windows.Forms.Button button_ok;
  570. private System.Windows.Forms.Label label_status_text;
  571. private System.Windows.Forms.Label label_type;
  572. private System.Windows.Forms.Label label_where;
  573. private System.Windows.Forms.Label label_where_text;
  574. private System.Windows.Forms.Label label_type_text;
  575. private System.Windows.Forms.Label label_comment;
  576. private System.Windows.Forms.Label label_comment_text;
  577. PageSetupDialog page_setup_dialog;
  578. public PrinterForm (PageSetupDialog page_setup_dialog)
  579. {
  580. InitializeComponent();
  581. this.page_setup_dialog = page_setup_dialog;
  582. }
  583. public string SelectedPrinter {
  584. get {
  585. return (string) combobox_printers.SelectedItem;
  586. }
  587. set {
  588. combobox_printers.SelectedItem = value;
  589. label_type_text.Text = value;
  590. }
  591. }
  592. public void UpdateValues ()
  593. {
  594. combobox_printers.Items.Clear ();
  595. foreach (string printer_name in PrinterSettings.InstalledPrinters)
  596. combobox_printers.Items.Add (printer_name);
  597. // Select the printer indicated by PageSetupDialog.PrinterSettings
  598. SelectedPrinter = page_setup_dialog.PrinterSettings.PrinterName;
  599. button_network.Enabled = page_setup_dialog.ShowNetwork;
  600. }
  601. #region Windows Form Designer generated code
  602. private void InitializeComponent()
  603. {
  604. this.groupbox_printer = new System.Windows.Forms.GroupBox();
  605. this.combobox_printers = new System.Windows.Forms.ComboBox();
  606. this.button_network = new System.Windows.Forms.Button();
  607. this.button_cancel = new System.Windows.Forms.Button();
  608. this.button_ok = new System.Windows.Forms.Button();
  609. this.label_name = new System.Windows.Forms.Label();
  610. this.label_status = new System.Windows.Forms.Label();
  611. this.label_status_text = new System.Windows.Forms.Label();
  612. this.label_type = new System.Windows.Forms.Label();
  613. this.label_type_text = new System.Windows.Forms.Label();
  614. this.label_where = new System.Windows.Forms.Label();
  615. this.label_comment = new System.Windows.Forms.Label();
  616. this.label_where_text = new System.Windows.Forms.Label();
  617. this.label_comment_text = new System.Windows.Forms.Label();
  618. this.button_properties = new System.Windows.Forms.Button();
  619. this.groupbox_printer.SuspendLayout();
  620. this.SuspendLayout();
  621. //
  622. // groupbox_printer
  623. //
  624. this.groupbox_printer.Controls.AddRange(new System.Windows.Forms.Control[] {
  625. this.button_properties,
  626. this.label_comment_text,
  627. this.label_where_text,
  628. this.label_comment,
  629. this.label_where,
  630. this.label_type_text,
  631. this.label_type,
  632. this.label_status_text,
  633. this.label_status,
  634. this.label_name,
  635. this.combobox_printers});
  636. this.groupbox_printer.Location = new System.Drawing.Point(12, 8);
  637. this.groupbox_printer.Name = "groupbox_printer";
  638. this.groupbox_printer.Size = new System.Drawing.Size(438, 136);
  639. this.groupbox_printer.Text = "Printer";
  640. //
  641. // combobox_printers
  642. //
  643. this.combobox_printers.Location = new System.Drawing.Point(64, 24);
  644. this.combobox_printers.Name = "combobox_printers";
  645. this.combobox_printers.SelectedValueChanged += new EventHandler (OnSelectedValueChangedPrinters);
  646. this.combobox_printers.Size = new System.Drawing.Size(232, 21);
  647. this.combobox_printers.TabIndex = 1;
  648. //
  649. // button_network
  650. //
  651. this.button_network.Location = new System.Drawing.Point(16, 160);
  652. this.button_network.Name = "button_network";
  653. this.button_network.Size = new System.Drawing.Size(68, 22);
  654. this.button_network.TabIndex = 5;
  655. this.button_network.Text = "Network...";
  656. //
  657. // button_cancel
  658. //
  659. this.button_cancel.DialogResult = DialogResult.Cancel;
  660. this.button_cancel.Location = new System.Drawing.Point(376, 160);
  661. this.button_cancel.Name = "button_cancel";
  662. this.button_cancel.Size = new System.Drawing.Size(68, 22);
  663. this.button_cancel.TabIndex = 4;
  664. this.button_cancel.Text = "Cancel";
  665. //
  666. // button_ok
  667. //
  668. this.button_ok.DialogResult = DialogResult.OK;
  669. this.button_ok.Location = new System.Drawing.Point(300, 160);
  670. this.button_ok.Name = "button_ok";
  671. this.button_ok.Size = new System.Drawing.Size(68, 22);
  672. this.button_ok.TabIndex = 3;
  673. this.button_ok.Text = "OK";
  674. //
  675. // label_name
  676. //
  677. this.label_name.Location = new System.Drawing.Point(12, 28);
  678. this.label_name.Name = "label_name";
  679. this.label_name.Size = new System.Drawing.Size(48, 20);
  680. this.label_name.Text = "Name:";
  681. //
  682. // label_status
  683. //
  684. this.label_status.Location = new System.Drawing.Point(6, 52);
  685. this.label_status.Name = "label_status";
  686. this.label_status.Size = new System.Drawing.Size(58, 14);
  687. this.label_status.Text = "Status:";
  688. //
  689. // label_status_text
  690. //
  691. this.label_status_text.Location = new System.Drawing.Point(64, 52);
  692. this.label_status_text.Name = "label_status_text";
  693. this.label_status_text.Size = new System.Drawing.Size(64, 14);
  694. this.label_status_text.Text = String.Empty;
  695. //
  696. // label_type
  697. //
  698. this.label_type.Location = new System.Drawing.Point(6, 72);
  699. this.label_type.Name = "label_type";
  700. this.label_type.Size = new System.Drawing.Size(58, 14);
  701. this.label_type.Text = "Type:";
  702. //
  703. // label_type_text
  704. //
  705. this.label_type_text.Location = new System.Drawing.Point(64, 72);
  706. this.label_type_text.Name = "label_type_text";
  707. this.label_type_text.Size = new System.Drawing.Size(232, 14);
  708. this.label_type_text.TabIndex = 5;
  709. this.label_type_text.Text = String.Empty;
  710. //
  711. // label_where
  712. //
  713. this.label_where.Location = new System.Drawing.Point(6, 92);
  714. this.label_where.Name = "label_where";
  715. this.label_where.Size = new System.Drawing.Size(58, 16);
  716. this.label_where.TabIndex = 6;
  717. this.label_where.Text = "Where:";
  718. //
  719. // label_comment
  720. //
  721. this.label_comment.Location = new System.Drawing.Point(6, 112);
  722. this.label_comment.Name = "label_comment";
  723. this.label_comment.Size = new System.Drawing.Size(56, 16);
  724. this.label_comment.Text = "Comment:";
  725. //
  726. // label_where_text
  727. //
  728. this.label_where_text.Location = new System.Drawing.Point(64, 92);
  729. this.label_where_text.Name = "label_where_text";
  730. this.label_where_text.Size = new System.Drawing.Size(232, 16);
  731. this.label_where_text.Text = String.Empty;
  732. //
  733. // label_comment_text
  734. //
  735. this.label_comment_text.Location = new System.Drawing.Point(64, 112);
  736. this.label_comment_text.Name = "label_comment_text";
  737. this.label_comment_text.Size = new System.Drawing.Size(232, 16);
  738. this.label_comment_text.Text = String.Empty;
  739. //
  740. // button_properties
  741. //
  742. this.button_properties.Location = new System.Drawing.Point(308, 22);
  743. this.button_properties.Name = "button_properties";
  744. this.button_properties.Size = new System.Drawing.Size(92, 22);
  745. this.button_properties.TabIndex = 2;
  746. this.button_properties.Text = "Properties...";
  747. //
  748. // PrinterForm
  749. //
  750. this.AllowDrop = true;
  751. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  752. this.AcceptButton = button_ok;
  753. this.CancelButton = button_cancel;
  754. this.ClientSize = new System.Drawing.Size(456, 194);
  755. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  756. this.button_ok,
  757. this.button_cancel,
  758. this.button_network,
  759. this.groupbox_printer});
  760. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  761. this.HelpButton = true;
  762. this.MaximizeBox = false;
  763. this.MinimizeBox = false;
  764. this.Name = "PrinterForm";
  765. this.ShowInTaskbar = false;
  766. this.Text = "Configure page";
  767. this.groupbox_printer.ResumeLayout(false);
  768. this.ResumeLayout(false);
  769. }
  770. #endregion
  771. void OnSelectedValueChangedPrinters (object sender, EventArgs args)
  772. {
  773. SelectedPrinter = (string) combobox_printers.SelectedItem;
  774. }
  775. }
  776. private class PagePreview : UserControl
  777. {
  778. int width;
  779. int height;
  780. int marginBottom;
  781. int marginTop;
  782. int marginLeft;
  783. int marginRight;
  784. bool landscape;
  785. bool loaded = false;
  786. System.Text.StringBuilder sb;
  787. float displayHeight;
  788. new Font font;
  789. public bool Landscape
  790. {
  791. get { return landscape; }
  792. set
  793. {
  794. if (landscape != value) {
  795. landscape = value;
  796. Invalidate ();
  797. }
  798. }
  799. }
  800. public new float Height {
  801. get { return displayHeight; }
  802. set {
  803. if (displayHeight != value) {
  804. displayHeight = value;
  805. Invalidate ();
  806. }
  807. }
  808. }
  809. public PagePreview ()
  810. {
  811. sb = new System.Text.StringBuilder ();
  812. for (int i = 0; i < 4; i++) {
  813. sb.Append ("blabla piu piublapiu haha lai dlais dhlçai shd ");
  814. sb.Append ("çoasd çlaj sdç\r\n lajsd lçaisdj lçillaisd lahs dli");
  815. sb.Append ("laksjd liasjdliasdj blabla piu piublapiu haha ");
  816. sb.Append ("lai dlais dhlçai shd çoasd çlaj sdç lajsd lçaisdj");
  817. sb.Append (" lçillaisd lahs dli laksjd liasjdliasdj\r\n\r\n");
  818. }
  819. font = new Font (FontFamily.GenericSansSerif, 4);
  820. this.displayHeight = 130;
  821. }
  822. public void SetSize (int width, int height)
  823. {
  824. this.width = width;
  825. this.height = height;
  826. this.Invalidate ();
  827. }
  828. public void SetMargins (int left, int right, int top, int bottom)
  829. {
  830. this.marginBottom = bottom;
  831. this.marginTop = top;
  832. this.marginLeft = left;
  833. this.marginRight = right;
  834. this.Invalidate ();
  835. }
  836. public void Setup (PageSettings pageSettings)
  837. {
  838. this.width = pageSettings.PaperSize.Width;
  839. this.height = pageSettings.PaperSize.Height;
  840. Margins margins = pageSettings.Margins;
  841. this.marginBottom = margins.Bottom;
  842. this.marginTop = margins.Top;
  843. this.marginLeft = margins.Left;
  844. this.marginRight = margins.Right;
  845. this.landscape = pageSettings.Landscape;
  846. this.loaded = true;
  847. }
  848. protected override void OnPaint (PaintEventArgs e)
  849. {
  850. if (!loaded) {
  851. base.OnPaint (e);
  852. return;
  853. }
  854. Graphics g = e.Graphics;
  855. float h = displayHeight;
  856. float w = (width * displayHeight) / height;
  857. float top = (marginTop * displayHeight) / height;
  858. float left = (marginLeft * displayHeight) / height;
  859. float bottom = (marginBottom * displayHeight) / height;
  860. float right = (marginRight * displayHeight) / height;
  861. if (landscape) {
  862. float a = w;
  863. w = h;
  864. h = a;
  865. a = right;
  866. right = top;
  867. top = left;
  868. left = bottom;
  869. bottom = a;
  870. }
  871. g.FillRectangle (SystemBrushes.ControlDark, 4, 4, w + 4, h + 4);
  872. g.FillRectangle (Brushes.White, 0, 0, w, h);
  873. RectangleF outerrect = new RectangleF (0, 0, w, h);
  874. RectangleF innerrect = new RectangleF (left, top,
  875. w - left - right,
  876. h - top - bottom);
  877. ControlPaint.DrawBorder (g, outerrect, Color.Black, ButtonBorderStyle.Solid);
  878. ControlPaint.DrawBorder (g, innerrect, SystemColors.ControlDark, ButtonBorderStyle.Dashed);
  879. g.DrawString (sb.ToString (), font, Brushes.Black,
  880. new RectangleF (innerrect.X + 2,
  881. innerrect.Y + 2,
  882. innerrect.Width - 4,
  883. innerrect.Height - 4));
  884. base.OnPaint (e);
  885. }
  886. }
  887. }
  888. }