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