/Skywave Class Library/Skywave Windows Forms/Windows/Forms/AdvancedPageSetupDialog/AdvancedPageSetupDialogForm.cs

# · C# · 286 lines · 256 code · 23 blank · 7 comment · 43 complexity · b415f1ca469ecaa57bcd51e6587f8594 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Runtime.Serialization.Formatters.Binary;
  9. using System.Runtime.Serialization;
  10. using System.Drawing.Printing;
  11. using System.IO;
  12. namespace Skywave.Windows.Forms
  13. {
  14. public partial class AdvancedPageSetupDialogForm : Form
  15. {
  16. public AdvancedPageSetupDialogForm()
  17. {
  18. InitializeComponent();
  19. }
  20. private IFormatter formatter = new BinaryFormatter();
  21. private bool PC = false;
  22. private PrintDocument mPrintDocument;
  23. public PrintDocument Document
  24. {
  25. get { return mPrintDocument; }
  26. set
  27. {
  28. mPrintDocument = value;
  29. if ((value == null)) return;
  30. RefreshByPrintDocument();
  31. }
  32. }
  33. private Skywave.ComponentModel.ProfilesHolder mProfilesHolder;
  34. [Browsable(false)]
  35. public Skywave.ComponentModel.ProfilesHolder ProfilesHolder
  36. {
  37. get { return mProfilesHolder; }
  38. set
  39. {
  40. mProfilesHolder = value;
  41. pmPageSettings.InitializeMe(mProfilesHolder);
  42. }
  43. }
  44. //[DefaultValue("")]
  45. //public string ProfilesPath
  46. //{
  47. // get { return pmPageSettings.ProfilesPath; }
  48. // set { pmPageSettings.ProfilesPath = value; }
  49. //}
  50. private void RefreshByPrintDocument()
  51. {
  52. try
  53. {
  54. PaperSize pkSize;
  55. PaperSource ps;
  56. int i;
  57. int int1;
  58. string str1;
  59. PrinterSettings.PaperSizeCollection xPaperSizes;
  60. PrinterSettings.PaperSourceCollection xPaperSources;
  61. PageSettings xDefaultPageSettings;
  62. if (mPrintDocument == null)
  63. {
  64. this.Enabled = false;
  65. return;
  66. }
  67. this.Enabled = true;
  68. if (PrinterSettings.InstalledPrinters.Count != 0)
  69. {
  70. //Refresh Printers combobox
  71. cmbPrinters.BeginUpdate();
  72. cmbPrinters.Items.Clear();
  73. int1 = -1;
  74. for (i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
  75. {
  76. str1 = PrinterSettings.InstalledPrinters[i];
  77. cmbPrinters.Items.Add(str1);
  78. if (mPrintDocument.DefaultPageSettings.PrinterSettings.PrinterName == str1) int1 = i;
  79. }
  80. PC = true;
  81. if (int1 >= 0) cmbPrinters.SelectedIndex = int1;
  82. PC = false;
  83. cmbPrinters.EndUpdate();
  84. xDefaultPageSettings = mPrintDocument.DefaultPageSettings;
  85. tpPage_cmbPageSize.DisplayMember = "PaperName";
  86. tpPage_cmbPageSize.BeginUpdate();
  87. tpPage_cmbPageSize.Items.Clear();
  88. int1 = -1;
  89. xPaperSizes = xDefaultPageSettings.PrinterSettings.PaperSizes;
  90. str1 = xDefaultPageSettings.PaperSize.PaperName;
  91. for (i = 0; i < xPaperSizes.Count; i++)
  92. {
  93. pkSize = xPaperSizes[i];
  94. if (pkSize.PaperName == str1) int1 = i;
  95. tpPage_cmbPageSize.Items.Add(pkSize);
  96. }
  97. PaperSize pkCustomSize1 = new PaperSize("Custom Paper Size", 100, 200);
  98. tpPage_cmbPageSize.Items.Add(pkCustomSize1);
  99. tpPage_cmbPageSize.EndUpdate();
  100. if (int1 < 0)
  101. {
  102. pkCustomSize1.Width = xDefaultPageSettings.PaperSize.Width;
  103. pkCustomSize1.Height = xDefaultPageSettings.PaperSize.Height;
  104. tpPage_cmbPageSize.SelectedIndex = tpPage_cmbPageSize.Items.Count - 1;
  105. }
  106. else
  107. {
  108. tpPage_cmbPageSize.SelectedIndex = int1;
  109. }
  110. tpPage_lstPaperSource.DisplayMember = "SourceName";
  111. tpPage_lstPaperSource.BeginUpdate();
  112. tpPage_lstPaperSource.Items.Clear();
  113. int1 = -1;
  114. xPaperSources = xDefaultPageSettings.PrinterSettings.PaperSources;
  115. str1 = xDefaultPageSettings.PaperSource.SourceName;
  116. for (i = 0; i < xPaperSources.Count; i++)
  117. {
  118. ps = xPaperSources[i];
  119. if (ps.SourceName == str1) int1 = i;
  120. tpPage_lstPaperSource.Items.Add(ps);
  121. }
  122. tpPage_lstPaperSource.EndUpdate();
  123. tpPage_lstPaperSource.SelectedIndex = int1;
  124. PC = true;
  125. tpMargins_txtLeft.Text = xDefaultPageSettings.Margins.Left.ToString();
  126. tpMargins_txtRight.Text = xDefaultPageSettings.Margins.Right.ToString();
  127. tpMargins_txtTop.Text = xDefaultPageSettings.Margins.Top.ToString();
  128. tpMargins_txtBottom.Text = xDefaultPageSettings.Margins.Bottom.ToString();
  129. if (xDefaultPageSettings.Landscape)
  130. {
  131. tpMargins_rbHorizontal.Checked = true;
  132. }
  133. else
  134. {
  135. tpMargins_rbVertical.Checked = true;
  136. }
  137. PC = false;
  138. }
  139. }
  140. catch (Exception)
  141. {
  142. }
  143. }
  144. private void cmdOK_Click(object sender, EventArgs e)
  145. {
  146. this.DialogResult = DialogResult.OK;
  147. Close();
  148. }
  149. private void cmdCancel_Click(object sender, EventArgs e)
  150. {
  151. this.DialogResult = DialogResult.Cancel;
  152. Close();
  153. }
  154. private void tpPage_cmbPageSize_SelectedIndexChanged(object sender, EventArgs e)
  155. {
  156. PaperSize xPaperSize = (PaperSize)tpPage_cmbPageSize.SelectedItem;
  157. mPrintDocument.DefaultPageSettings.PaperSize = xPaperSize;
  158. PC = true;
  159. tpPage_txtPageWidth.Text = xPaperSize.Width.ToString();
  160. tpPage_txtPageHeight.Text = xPaperSize.Height.ToString();
  161. PC = false;
  162. }
  163. private int MaInt(string str)
  164. {
  165. return (str == "") ? 0 : int.Parse(str);
  166. }
  167. private void tpPage_txtPageHeightOrWidth_TextChanged(object sender, EventArgs e)
  168. {
  169. if (PC) return;
  170. if (tpPage_cmbPageSize.Items.Count < 1) return;
  171. PaperSize xPaperSize = (PaperSize)tpPage_cmbPageSize.Items[tpPage_cmbPageSize.Items.Count - 1];
  172. xPaperSize.Height = MaInt(tpPage_txtPageHeight.Text);
  173. xPaperSize.Width = MaInt(tpPage_txtPageWidth.Text);
  174. tpPage_cmbPageSize.SelectedItem = xPaperSize;
  175. }
  176. private void tpPage_lstPaperSource_SelectedIndexChanged(object sender, EventArgs e)
  177. {
  178. if (tpPage_lstPaperSource.SelectedIndex < 0) return;
  179. mPrintDocument.DefaultPageSettings.PaperSource = (PaperSource)tpPage_lstPaperSource.SelectedItem;
  180. }
  181. private void tpMargins_rbVertical_CheckedChanged(object sender, EventArgs e)
  182. {
  183. if (mPrintDocument == null || PC) return;
  184. mPrintDocument.DefaultPageSettings.Landscape = !tpMargins_rbVertical.Checked;
  185. }
  186. private void cmbPrinters_SelectedIndexChanged(object sender, EventArgs e)
  187. {
  188. if (PC) return;
  189. if (cmbPrinters.SelectedIndex < 0) return;
  190. mPrintDocument.DefaultPageSettings.PrinterSettings.PrinterName = cmbPrinters.SelectedItem.ToString();
  191. RefreshByPrintDocument();
  192. }
  193. private void tpMargins_txtTop_TextChanged(object sender, EventArgs e)
  194. {
  195. if (mPrintDocument == null || PC) return;
  196. mPrintDocument.DefaultPageSettings.Margins.Top = MaInt(tpMargins_txtTop.Text);
  197. }
  198. private void tpMargins_txtBottom_TextChanged(object sender, EventArgs e)
  199. {
  200. if (mPrintDocument == null || PC) return;
  201. mPrintDocument.DefaultPageSettings.Margins.Bottom = MaInt(tpMargins_txtBottom.Text);
  202. }
  203. private void tpMargins_txtLeft_TextChanged(object sender, EventArgs e)
  204. {
  205. if (mPrintDocument == null || PC) return;
  206. mPrintDocument.DefaultPageSettings.Margins.Left = MaInt(tpMargins_txtLeft.Text);
  207. }
  208. private void tpMargins_txtRight_TextChanged(object sender, EventArgs e)
  209. {
  210. if (mPrintDocument == null || PC) return;
  211. mPrintDocument.DefaultPageSettings.Margins.Right = MaInt(tpMargins_txtRight.Text);
  212. }
  213. private void pmPageSettings_Serialize(object sender, Skywave.Windows.Forms.ProfilesManagerProfileEventArgs e)
  214. {
  215. Stream fs = e.Content;
  216. PageSettings xDefaultPageSettings = mPrintDocument.DefaultPageSettings;
  217. formatter.Serialize(fs, xDefaultPageSettings.PrinterSettings.PrinterName);
  218. formatter.Serialize(fs, xDefaultPageSettings.PaperSize.Width);
  219. formatter.Serialize(fs, xDefaultPageSettings.PaperSize.Height);
  220. formatter.Serialize(fs, xDefaultPageSettings.PaperSize.PaperName);
  221. formatter.Serialize(fs, xDefaultPageSettings.PaperSource.SourceName);
  222. formatter.Serialize(fs, xDefaultPageSettings.Margins.Left);
  223. formatter.Serialize(fs, xDefaultPageSettings.Margins.Right);
  224. formatter.Serialize(fs, xDefaultPageSettings.Margins.Top);
  225. formatter.Serialize(fs, xDefaultPageSettings.Margins.Bottom);
  226. formatter.Serialize(fs, xDefaultPageSettings.Landscape);
  227. }
  228. private void pmPageSettings_Deserialize(object sender, Skywave.Windows.Forms.ProfilesManagerProfileEventArgs e)
  229. {
  230. Stream fs = e.Content;
  231. string xPrinterName = (string)formatter.Deserialize(fs);
  232. PageSettings xDefaultPageSettings = mPrintDocument.DefaultPageSettings;
  233. foreach (string xStr in PrinterSettings.InstalledPrinters)
  234. {
  235. if (xPrinterName == xStr)
  236. {
  237. xDefaultPageSettings.PrinterSettings.PrinterName = xPrinterName;
  238. int xWidth = (int)formatter.Deserialize(fs);
  239. int xHeight = (int)formatter.Deserialize(fs);
  240. string xPaperName = (string)formatter.Deserialize(fs);
  241. PaperSize xPaperSize = new PaperSize(xPaperName, xWidth, xHeight);
  242. xDefaultPageSettings.PaperSize = xPaperSize;
  243. string xSourceName = (string)formatter.Deserialize(fs);
  244. foreach (PaperSource xPaperSource in xDefaultPageSettings.PrinterSettings.PaperSources)
  245. {
  246. if (xPaperSource.SourceName == xSourceName)
  247. {
  248. xDefaultPageSettings.PaperSource = xPaperSource;
  249. break;
  250. }
  251. }
  252. xDefaultPageSettings.Margins.Left = (int)formatter.Deserialize(fs);
  253. xDefaultPageSettings.Margins.Right = (int)formatter.Deserialize(fs);
  254. xDefaultPageSettings.Margins.Top = (int)formatter.Deserialize(fs);
  255. xDefaultPageSettings.Margins.Bottom = (int)formatter.Deserialize(fs);
  256. xDefaultPageSettings.Landscape = (bool)formatter.Deserialize(fs);
  257. break;
  258. }
  259. }
  260. RefreshByPrintDocument();
  261. }
  262. }
  263. }