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;
private PrinterSettings printer_settings;
private Margins min_margins;
- private Margins default_margins = UseYardPound ?
- new Margins (yard_pound_default, yard_pound_default, yard_pound_default, yard_pound_default) :
- new Margins (meter_default, meter_default, meter_default, meter_default);
private bool allow_margins;
private bool allow_orientation;
private bool allow_paper;
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;
private Label label_top;
private Label label_right;
private Label label_bottom;
- private TextBox textbox_left;
- private TextBox textbox_top;
- private TextBox textbox_right;
- private TextBox textbox_bottom;
+ private NumericTextBox textbox_left;
+ private NumericTextBox textbox_top;
+ private NumericTextBox textbox_right;
+ 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;
}
}
+#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; }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public PageSettings PageSettings {
get { return page_settings; }
- set {
+ set {
page_settings = value;
document = null;
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public PrinterSettings PrinterSettings {
get { return printer_settings; }
- set {
+ set {
printer_settings = value;
document = null;
}
[DefaultValue(false)]
public bool ShowHelp {
get { return show_help; }
- set {
+ set {
if (value != show_help) {
show_help = value;
ShowHelpButton ();
#endregion // Public Instance Properties
#region Protected Instance Methods
- protected override bool RunDialog (IntPtr hwnd)
+ protected override bool RunDialog (IntPtr hwndOwner)
{
- SetPrinterDetails ();
- return true;
+ try {
+ SetPrinterDetails ();
+ return true;
+ } catch (Exception e) {
+ MessageBox.Show (e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ return false;
+ }
}
#endregion // Protected Instance Methods
this.label_top = new System.Windows.Forms.Label();
this.label_right = new System.Windows.Forms.Label();
this.label_bottom = new System.Windows.Forms.Label();
- this.textbox_left = new System.Windows.Forms.TextBox();
- this.textbox_top = new System.Windows.Forms.TextBox();
- this.textbox_right = new System.Windows.Forms.TextBox();
- this.textbox_bottom = new System.Windows.Forms.TextBox();
+ this.textbox_left = new System.Windows.Forms.NumericTextBox();
+ 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();
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
//
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
//
this.textbox_left.Name = "textbox_left";
this.textbox_left.Size = new System.Drawing.Size(48, 20);
this.textbox_left.TabIndex = 4;
- this.textbox_left.Text = ToLocalizedLength (default_margins.Left);
+ this.textbox_left.TextChanged +=new EventHandler(OnMarginChange);
//
// textbox_top
- //
+ //
this.textbox_top.Location = new System.Drawing.Point(57, 54);
this.textbox_top.Name = "textbox_top";
this.textbox_top.Size = new System.Drawing.Size(48, 20);
this.textbox_top.TabIndex = 5;
- this.textbox_top.Text = ToLocalizedLength (default_margins.Top);
+ this.textbox_top.TextChanged +=new EventHandler(OnMarginChange);
//
// textbox_right
//
this.textbox_right.Name = "textbox_right";
this.textbox_right.Size = new System.Drawing.Size(48, 20);
this.textbox_right.TabIndex = 6;
- this.textbox_right.Text = ToLocalizedLength (default_margins.Right);
+ this.textbox_right.TextChanged +=new EventHandler(OnMarginChange);
//
// textbox_bottom
//
this.textbox_bottom.Name = "textbox_bottom";
this.textbox_bottom.Size = new System.Drawing.Size(48, 20);
this.textbox_bottom.TabIndex = 7;
- this.textbox_bottom.Text = ToLocalizedLength (default_margins.Bottom);
+ 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
//
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);
}
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.
}
}
- private string ToLocalizedLength (int marginsUnit)
+ private double ToLocalizedLength (int marginsUnit)
{
- return (UseYardPound ?
- marginsUnit / 100 :
- marginsUnit / 3.937 / 100).ToString ();
+ return UseYardPound ?
+ PrinterUnitConvert.Convert (marginsUnit, PrinterUnit.ThousandthsOfAnInch, PrinterUnit.Display) :
+ PrinterUnitConvert.Convert (marginsUnit, PrinterUnit.ThousandthsOfAnInch, PrinterUnit.TenthsOfAMillimeter);
+ }
+
+ private int FromLocalizedLength (double marginsUnit)
+ {
+ return (int)(UseYardPound ?
+ PrinterUnitConvert.Convert (marginsUnit, PrinterUnit.Display, PrinterUnit.ThousandthsOfAnInch) :
+ PrinterUnitConvert.Convert (marginsUnit, PrinterUnit.TenthsOfAMillimeter, PrinterUnit.ThousandthsOfAnInch));
}
private string LocalizedLengthUnit ()
if (ShowHelp)
ShowHelpButton ();
+ Margins page_margins = PageSettings.Margins;
+ Margins min_margins = MinMargins;
+
+ // Update margin data
+ textbox_top.Text = ToLocalizedLength (page_margins.Top).ToString ();
+ textbox_bottom.Text = ToLocalizedLength (page_margins.Bottom).ToString ();
+ textbox_left.Text = ToLocalizedLength (page_margins.Left).ToString ();
+ textbox_right.Text = ToLocalizedLength (page_margins.Right).ToString ();
+ textbox_top.Min = ToLocalizedLength (min_margins.Top);
+ textbox_bottom.Min = ToLocalizedLength (min_margins.Bottom);
+ textbox_left.Min = ToLocalizedLength (min_margins.Left);
+ textbox_right.Min = ToLocalizedLength (min_margins.Right);
+
button_printer.Enabled = AllowPrinter && PrinterSettings != null;
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) {
}
}
+ Margins margins = new Margins ();
+ margins.Top = FromLocalizedLength (textbox_top.Value);
+ margins.Bottom = FromLocalizedLength (textbox_bottom.Value);
+ margins.Left = FromLocalizedLength (textbox_left.Value);
+ margins.Right = FromLocalizedLength (textbox_right.Value);
+ PageSettings.Margins = margins;
+
PageSettings.Landscape = radio_landscape.Checked;
form.DialogResult = DialogResult.OK;
}
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 ();
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
}
}
+ 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);
+ }
+ }
}
+
}