2009-01-02 Ivan N. Zlatev <contact@i-nz.net>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / PageSetupDialog.cs
index 4d41f5266a760fa651a8b3d163933a528031a527..8fbb5693a4ce455f4428472e396cb9417fcd198e 100644 (file)
@@ -34,13 +34,11 @@ using System.Drawing.Printing;
 using System.Globalization;
 using System.Reflection;
 
-namespace System.Windows.Forms {
-
+namespace System.Windows.Forms
+{
        [DefaultProperty("Document")]
-       public sealed class PageSetupDialog : CommonDialog {
-               const int yard_pound_default = 100;
-               static readonly int meter_default = (int) Math.Round (10 * 3.937 * 100);
-
+       public sealed class PageSetupDialog : CommonDialog
+       {
                #region Local variables
                private PrintDocument document;
                private PageSettings page_settings;
@@ -52,7 +50,9 @@ namespace System.Windows.Forms {
                private bool allow_printer;
                private bool show_help;
                private bool show_network;
-
+#if NET_2_0
+               private bool enable_metric;
+#endif
                private GroupBox groupbox_paper;
                private Label label_source;
                private Label label_size;
@@ -74,20 +74,20 @@ namespace System.Windows.Forms {
                private NumericTextBox textbox_bottom;
                private ComboBox combobox_source;
                private ComboBox combobox_size;
-               private PrinterForm printer_helper_form;
+               private PagePreview pagePreview;
                #endregion // Local variables
 
                #region Public Constructors
-               public PageSetupDialog () 
+               public PageSetupDialog ()
                {
+                       form = new DialogForm (this);
                        InitializeComponent();
                        Reset ();
                }
                #endregion // Public Constructors
 
-
                #region Public Instance Methods
-               public override void Reset () 
+               public override void Reset ()
                {
                        AllowMargins = true;
                        AllowOrientation = true;
@@ -139,6 +139,17 @@ namespace System.Windows.Forms {
                        }
                }
 
+#if NET_2_0
+               [Browsable (true)]
+               [DefaultValue (false)]
+               [MonoTODO ("Stubbed, not implemented")]
+               [EditorBrowsableAttribute (EditorBrowsableState.Always)]
+               public bool EnableMetric {
+                       get { return enable_metric; }
+                       set { enable_metric = value; }
+               }
+#endif
+
                public Margins MinMargins {
                        get { return min_margins; }
                        set { min_margins = value; }
@@ -149,7 +160,7 @@ namespace System.Windows.Forms {
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                public PageSettings PageSettings {
                        get { return page_settings; }
-                       set { 
+                       set {
                                page_settings = value;
                                document = null;
                        }
@@ -160,7 +171,7 @@ namespace System.Windows.Forms {
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                public PrinterSettings PrinterSettings {
                        get { return printer_settings; }
-                       set { 
+                       set {
                                printer_settings = value;
                                document = null;
                        }
@@ -169,7 +180,7 @@ namespace System.Windows.Forms {
                [DefaultValue(false)]
                public bool ShowHelp {
                        get { return show_help; }
-                       set { 
+                       set {
                                if (value != show_help) {
                                        show_help = value;
                                        ShowHelpButton ();
@@ -186,13 +197,12 @@ namespace System.Windows.Forms {
                #endregion // Public Instance Properties
 
                #region Protected Instance Methods
-               protected override bool RunDialog (IntPtr hwnd
+               protected override bool RunDialog (IntPtr hwndOwner)
                {
                        try {
                                SetPrinterDetails ();
                                return true;
-                       }
-                       catch (Exception e) {
+                       } catch (Exception e) {
                                MessageBox.Show (e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return false;
                        }
@@ -222,6 +232,7 @@ namespace System.Windows.Forms {
                        this.textbox_top = new System.Windows.Forms.NumericTextBox();
                        this.textbox_right = new System.Windows.Forms.NumericTextBox();
                        this.textbox_bottom = new System.Windows.Forms.NumericTextBox();
+                       this.pagePreview = new PagePreview ();
                        this.groupbox_paper.SuspendLayout();
                        this.groupbox_orientation.SuspendLayout();
                        this.groupbox_margin.SuspendLayout();
@@ -254,6 +265,7 @@ namespace System.Windows.Forms {
                        this.combobox_size.Name = "combobox_size";
                        this.combobox_size.Size = new System.Drawing.Size(240, 21);
                        this.combobox_size.TabIndex = 2;
+                       this.combobox_size.SelectedIndexChanged += new EventHandler(this.OnPaperSizeChange);
                        // 
                        // label_source
                        // 
@@ -289,6 +301,7 @@ namespace System.Windows.Forms {
                        this.radio_landscape.Size = new System.Drawing.Size(80, 24);
                        this.radio_landscape.TabIndex = 7;
                        this.radio_landscape.Text = "L&andscape";
+                       this.radio_landscape.CheckedChanged += new EventHandler(this.OnLandscapeChange);
                        // 
                        // radio_portrait
                        // 
@@ -380,6 +393,7 @@ namespace System.Windows.Forms {
                        this.textbox_left.Name = "textbox_left";
                        this.textbox_left.Size = new System.Drawing.Size(48, 20);
                        this.textbox_left.TabIndex = 4;
+                       this.textbox_left.TextChanged +=new EventHandler(OnMarginChange);
                        // 
                        // textbox_top
                        //
@@ -387,6 +401,7 @@ namespace System.Windows.Forms {
                        this.textbox_top.Name = "textbox_top";
                        this.textbox_top.Size = new System.Drawing.Size(48, 20);
                        this.textbox_top.TabIndex = 5;
+                       this.textbox_top.TextChanged +=new EventHandler(OnMarginChange);
                        // 
                        // textbox_right
                        // 
@@ -394,6 +409,7 @@ namespace System.Windows.Forms {
                        this.textbox_right.Name = "textbox_right";
                        this.textbox_right.Size = new System.Drawing.Size(48, 20);
                        this.textbox_right.TabIndex = 6;
+                       this.textbox_right.TextChanged +=new EventHandler(OnMarginChange);
                        // 
                        // textbox_bottom
                        // 
@@ -401,6 +417,14 @@ namespace System.Windows.Forms {
                        this.textbox_bottom.Name = "textbox_bottom";
                        this.textbox_bottom.Size = new System.Drawing.Size(48, 20);
                        this.textbox_bottom.TabIndex = 7;
+                       this.textbox_bottom.TextChanged +=new EventHandler(OnMarginChange);
+                       // 
+                       // pagePreview
+                       // 
+                       this.pagePreview.Location = new System.Drawing.Point (130, 10);
+                       this.pagePreview.Name = "pagePreview";
+                       this.pagePreview.Size = new System.Drawing.Size (150, 150);
+                       this.pagePreview.TabIndex = 6;
                        // 
                        // Form3
                        // 
@@ -408,7 +432,8 @@ namespace System.Windows.Forms {
                        form.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                        form.CancelButton = this.button_cancel;
                        form.ClientSize = new System.Drawing.Size(360, 390);
-                       form.Controls.Add(this.button_printer);
+                       form.Controls.Add (this.pagePreview);
+                       form.Controls.Add (this.button_printer);
                        form.Controls.Add(this.button_cancel);
                        form.Controls.Add(this.button_ok);
                        form.Controls.Add(this.groupbox_margin);
@@ -429,15 +454,7 @@ namespace System.Windows.Forms {
                }
 
                static bool UseYardPound {
-                       get {
-                               switch (CultureInfo.CurrentUICulture.Name) {
-                               case "en-US":
-                               case "en-GB":
-                                       return true;
-                               default:
-                                       return false;
-                               }
-                       }
+                       get { return !RegionInfo.CurrentRegion.IsMetric; }
                }
 
                // .Net uses PrinterSettings property if it is not null.
@@ -453,15 +470,15 @@ namespace System.Windows.Forms {
                private double ToLocalizedLength (int marginsUnit)
                {
                        return UseYardPound ?
-                               marginsUnit / 100.0 :
-                               marginsUnit / 10.0;
+                               PrinterUnitConvert.Convert (marginsUnit, PrinterUnit.ThousandthsOfAnInch, PrinterUnit.Display) :
+                               PrinterUnitConvert.Convert (marginsUnit, PrinterUnit.ThousandthsOfAnInch, PrinterUnit.TenthsOfAMillimeter);
                }
 
                private int FromLocalizedLength (double marginsUnit)
                {
-                       return UseYardPound ?
-                               (int) (marginsUnit * 100.0) :
-                               (int) (marginsUnit * 3.937);
+                       return  (int)(UseYardPound ?
+                               PrinterUnitConvert.Convert (marginsUnit, PrinterUnit.Display, PrinterUnit.ThousandthsOfAnInch) :
+                               PrinterUnitConvert.Convert (marginsUnit, PrinterUnit.TenthsOfAMillimeter, PrinterUnit.ThousandthsOfAnInch));
                }
 
                private string LocalizedLengthUnit ()
@@ -509,10 +526,12 @@ namespace System.Windows.Forms {
                        groupbox_orientation.Enabled = AllowOrientation;
                        groupbox_paper.Enabled = AllowPaper;
                        groupbox_margin.Enabled = AllowMargins;
+
+                       pagePreview.Setup (PageSettings);
                }
 
                private void OnClickOkButton (object sender, EventArgs e)
-               {                       
+               {
                        if (combobox_size.SelectedItem != null) {
                                foreach (PaperSize paper_size in InternalPrinterSettings.PaperSizes) {
                                        if (paper_size.PaperName == (string) combobox_size.SelectedItem) {
@@ -558,8 +577,7 @@ namespace System.Windows.Forms {
 
                void OnClickPrinterButton (object sender, EventArgs args)
                {
-                       if (printer_helper_form == null)
-                               printer_helper_form = new PrinterForm (this);
+                       PrinterForm printer_helper_form = new PrinterForm (this);
 
                        printer_helper_form.UpdateValues ();
 
@@ -568,7 +586,37 @@ namespace System.Windows.Forms {
                                if (printer_helper_form.SelectedPrinter != PrinterSettings.PrinterName)
                                        PrinterSettings.PrinterName = printer_helper_form.SelectedPrinter;
 
+                       PageSettings = PrinterSettings.DefaultPageSettings;
+                       SetPrinterDetails ();
                        button_ok.Select ();
+                       printer_helper_form.Dispose ();
+               }
+
+               void OnPaperSizeChange (object sender, EventArgs e)
+               {
+                       if (combobox_size.SelectedItem != null) {
+                               foreach (PaperSize paper_size in InternalPrinterSettings.PaperSizes) {
+                                       if (paper_size.PaperName == (string) combobox_size.SelectedItem) {
+                                               pagePreview.SetSize (paper_size.Width, paper_size.Height);
+                                               break;
+                                       }
+                               }
+                       }
+               }
+
+               void OnMarginChange (object sender, EventArgs e)
+               {
+                       pagePreview.SetMargins (
+                               FromLocalizedLength (textbox_left.Value),
+                               FromLocalizedLength (textbox_right.Value),
+                               FromLocalizedLength (textbox_top.Value),
+                               FromLocalizedLength (textbox_bottom.Value)
+                       );
+               }
+
+               void OnLandscapeChange (object sender, EventArgs e)
+               {
+                       pagePreview.Landscape = radio_landscape.Checked;
                }
                #endregion // Private Helper
 
@@ -795,5 +843,137 @@ namespace System.Windows.Forms {
                        }
                }
 
+               private class PagePreview : UserControl
+               {
+                       int width;
+                       int height;
+
+                       int marginBottom;
+                       int marginTop;
+                       int marginLeft;
+                       int marginRight;
+                       bool landscape;
+
+                       bool loaded = false;
+
+                       System.Text.StringBuilder sb;
+                       float displayHeight;
+                       new Font font;
+
+                       public bool Landscape {
+                               get { return landscape; }
+                               set {
+                                       if (landscape != value) {
+                                               landscape = value;
+                                               Invalidate ();
+                                       }
+                               }
+                       }
+
+                       public new float Height {
+                               get { return displayHeight; }
+                               set {
+                                       if (displayHeight != value) {
+                                               displayHeight = value; 
+                                               Invalidate ();
+                                       }
+                               }
+                       }
+
+                       public PagePreview ()
+                       {
+                               sb = new System.Text.StringBuilder ();
+                               for (int i = 0; i < 4; i++) {
+                                       sb.Append ("blabla piu piublapiu haha lai dlais dhlçai shd ");
+                                       sb.Append ("çoasd çlaj sdç\r\n lajsd lçaisdj lçillaisd lahs dli");
+                                       sb.Append ("laksjd liasjdliasdj blabla piu piublapiu haha ");
+                                       sb.Append ("lai dlais dhlçai shd çoasd çlaj sdç lajsd lçaisdj");
+                                       sb.Append (" lçillaisd lahs dli laksjd liasjdliasdj\r\n\r\n");
+                               }
+                               
+                               font = new Font (FontFamily.GenericSansSerif, 4);
+                               this.displayHeight = 130;
+                       }
+
+                       public void SetSize (int width, int height)
+                       {
+                               this.width = width;
+                               this.height = height;
+                               this.Invalidate ();
+                       }
+
+                       public void SetMargins (int left, int right, int top, int bottom)
+                       {
+                               this.marginBottom = bottom;
+                               this.marginTop = top;
+                               this.marginLeft = left;
+                               this.marginRight = right;
+                               this.Invalidate ();
+                       }
+
+
+                       public void Setup (PageSettings pageSettings)
+                       {
+                               this.width = pageSettings.PaperSize.Width;
+                               this.height = pageSettings.PaperSize.Height;
+
+                               Margins margins = pageSettings.Margins;
+                               this.marginBottom = margins.Bottom;
+                               this.marginTop = margins.Top;
+                               this.marginLeft = margins.Left;
+                               this.marginRight = margins.Right;
+                               this.landscape = pageSettings.Landscape;
+                               this.loaded = true;
+                       }
+
+                       protected override void OnPaint (PaintEventArgs e)
+                       {
+                               if (!loaded) {
+                                       base.OnPaint (e);
+                                       return;
+                               }
+
+                               Graphics g = e.Graphics;
+
+                               float h = displayHeight;
+                               float w = (width * displayHeight) / height;
+                               float top = (marginTop * displayHeight) / height;
+                               float left = (marginLeft * displayHeight) / height;
+                               float bottom = (marginBottom * displayHeight) / height;
+                               float right = (marginRight * displayHeight) / height;
+
+                               if (landscape) {
+                                       float a = w;
+                                       w = h;
+                                       h = a;
+                                       a = right;
+                                       right = top;
+                                       top = left;
+                                       left = bottom;
+                                       bottom = a;
+                               }
+                               
+                               g.FillRectangle (SystemBrushes.ControlDark, 4, 4, w + 4, h + 4);
+                               g.FillRectangle (Brushes.White, 0, 0, w, h);
+
+                               RectangleF outerrect = new RectangleF (0, 0, w, h);
+                               RectangleF innerrect = new RectangleF (left, top,
+                                                                                                               w - left - right,
+                                                                                                               h - top - bottom);
+
+                               ControlPaint.DrawBorder (g, outerrect, Color.Black, ButtonBorderStyle.Solid);
+                               ControlPaint.DrawBorder (g, innerrect, SystemColors.ControlDark, ButtonBorderStyle.Dashed);
+
+                               g.DrawString (sb.ToString (), font, Brushes.Black,
+                                                               new RectangleF (innerrect.X + 2,
+                                                                                       innerrect.Y + 2,
+                                                                                       innerrect.Width - 4,
+                                                                                       innerrect.Height - 4));
+
+
+                               base.OnPaint (e);
+                       }
+               }
        }
+
 }