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