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