PrintPreviewControl.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 ([email protected])
  24. //
  25. using System;
  26. using System.ComponentModel;
  27. using System.ComponentModel.Design;
  28. using System.ComponentModel.Design.Serialization;
  29. using System.Collections;
  30. using System.Diagnostics;
  31. using System.Drawing;
  32. using System.Drawing.Printing;
  33. using System.Reflection;
  34. namespace System.Windows.Forms {
  35. [DefaultPropertyAttribute("Document")]
  36. public class PrintPreviewControl : Control {
  37. #region Local variables
  38. bool autozoom;
  39. int columns;
  40. int rows;
  41. int startPage;
  42. double zoom;
  43. int padding = ThemeEngine.Current.PrintPreviewControlPadding;
  44. PrintDocument document;
  45. internal PreviewPrintController controller;
  46. internal PreviewPageInfo[] page_infos;
  47. private VScrollBar vbar;
  48. private HScrollBar hbar;
  49. internal Rectangle ViewPort;
  50. internal Image[] image_cache;
  51. Size image_size;
  52. #endregion // Local variables
  53. #region Public Constructors
  54. public PrintPreviewControl() {
  55. autozoom = true;
  56. columns = 1;
  57. rows = 0;
  58. startPage = 1;
  59. this.BackColor = SystemColors.AppWorkspace;
  60. controller = new PreviewPrintController ();
  61. vbar = new ImplicitVScrollBar ();
  62. hbar = new ImplicitHScrollBar ();
  63. vbar.Visible = false;
  64. hbar.Visible = false;
  65. vbar.ValueChanged += new EventHandler (VScrollBarValueChanged);
  66. hbar.ValueChanged += new EventHandler (HScrollBarValueChanged);
  67. SuspendLayout ();
  68. Controls.AddImplicit (vbar);
  69. Controls.AddImplicit (hbar);
  70. ResumeLayout ();
  71. }
  72. #endregion // Public Constructors
  73. #region Public Instance Properties
  74. [DefaultValue(true)]
  75. public bool AutoZoom {
  76. get { return autozoom; }
  77. set {
  78. if (autozoom != value) {
  79. InvalidatePreview ();
  80. Invalidate ();
  81. }
  82. autozoom = value;
  83. }
  84. }
  85. [DefaultValue(1)]
  86. public int Columns {
  87. get { return columns; }
  88. set {
  89. if (columns != value) {
  90. columns = value;
  91. if (AutoZoom)
  92. InvalidatePreview ();
  93. Invalidate ();
  94. }
  95. }
  96. }
  97. [DefaultValue(null)]
  98. public PrintDocument Document {
  99. get { return document; }
  100. set {
  101. document = value;
  102. }
  103. }
  104. [DefaultValue(1)]
  105. public int Rows {
  106. get { return rows; }
  107. set {
  108. if (rows != value) {
  109. rows = value;
  110. if (AutoZoom)
  111. InvalidatePreview ();
  112. Invalidate ();
  113. }
  114. }
  115. }
  116. [DefaultValue(0)]
  117. public int StartPage {
  118. get { return startPage; }
  119. set {
  120. if (value < 1)
  121. return;
  122. if (document != null && value + (Rows + 1) * Columns > page_infos.Length + 1) {
  123. value = page_infos.Length + 1 - (Rows + 1) * Columns;
  124. if (value < 1)
  125. value = 1;
  126. }
  127. if (startPage != value)
  128. Invalidate ();
  129. startPage = value;
  130. OnStartPageChanged (EventArgs.Empty);
  131. }
  132. }
  133. [Bindable(false)]
  134. [Browsable(false)]
  135. [EditorBrowsable(EditorBrowsableState.Never)]
  136. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  137. public override string Text {
  138. get { return base.Text; }
  139. set { base.Text = value; }
  140. }
  141. [DefaultValue(false)]
  142. public bool UseAntiAlias {
  143. get { return controller.UseAntiAlias; }
  144. set { controller.UseAntiAlias = value; }
  145. }
  146. public double Zoom {
  147. get { return zoom; }
  148. set {
  149. if (zoom < 0.0)
  150. throw new ArgumentException ("zoom");
  151. if (zoom != value) {
  152. InvalidatePreview ();
  153. Invalidate ();
  154. }
  155. zoom = value;
  156. }
  157. }
  158. #endregion // Public Instance Properties
  159. #region Public Instance Methods
  160. internal void GeneratePreview ()
  161. {
  162. try {
  163. if (document == null)
  164. return;
  165. if (page_infos == null) {
  166. document.PrintController = new PrintControllerWithStatusDialog (controller);
  167. document.Print ();
  168. page_infos = controller.GetPreviewPageInfo ();
  169. }
  170. if (image_cache == null) {
  171. image_cache = new Image[page_infos.Length];
  172. if (page_infos.Length > 0) {
  173. image_size = ThemeEngine.Current.PrintPreviewControlGetPageSize (this);
  174. if (image_size.Width >= 0 && image_size.Width < page_infos[0].Image.Width
  175. && image_size.Height >= 0 && image_size.Height < page_infos[0].Image.Height) {
  176. for (int i = 0; i < page_infos.Length; i ++) {
  177. image_cache[i] = new Bitmap (image_size.Width, image_size.Height);
  178. Graphics g = Graphics.FromImage (image_cache[i]);
  179. g.DrawImage (page_infos[i].Image, new Rectangle (new Point (0, 0), image_size), 0, 0, page_infos[i].Image.Width, page_infos[i].Image.Height, GraphicsUnit.Pixel);
  180. g.Dispose ();
  181. }
  182. }
  183. }
  184. }
  185. UpdateScrollBars();
  186. }
  187. catch (Exception e) {
  188. page_infos = new PreviewPageInfo[0];
  189. image_cache = new Image[0];
  190. MessageBox.Show (e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  191. }
  192. }
  193. public void InvalidatePreview()
  194. {
  195. page_infos = null;
  196. image_cache = null;
  197. }
  198. [EditorBrowsable(EditorBrowsableState.Never)]
  199. public override void ResetBackColor()
  200. {
  201. base.ResetBackColor();
  202. }
  203. [EditorBrowsable(EditorBrowsableState.Never)]
  204. public override void ResetForeColor()
  205. {
  206. base.ResetForeColor ();
  207. }
  208. #endregion // Public Instance Methods
  209. #region Protected Instance Properties
  210. protected override CreateParams CreateParams {
  211. get {
  212. return base.CreateParams;
  213. }
  214. }
  215. #endregion // Protected Instance Methods
  216. #region Protected Instance Properties
  217. protected override void OnPaint(PaintEventArgs e)
  218. {
  219. if (page_infos == null || image_cache == null)
  220. GeneratePreview ();
  221. ThemeEngine.Current.PrintPreviewControlPaint (e, this, image_size);
  222. }
  223. protected override void OnResize(EventArgs e)
  224. {
  225. base.OnResize (e);
  226. if (AutoZoom) {
  227. if (page_infos != null && page_infos.Length > 0) {
  228. Size new_size = ThemeEngine.Current.PrintPreviewControlGetPageSize (this);
  229. if (new_size.Width != image_size.Width && new_size.Height != image_size.Height)
  230. image_cache = null;
  231. }
  232. }
  233. UpdateScrollBars ();
  234. Invalidate ();
  235. }
  236. protected virtual void OnStartPageChanged(EventArgs e)
  237. {
  238. if (StartPageChanged != null)
  239. StartPageChanged(this, e);
  240. }
  241. protected override void WndProc(ref Message m)
  242. {
  243. base.WndProc (ref m);
  244. }
  245. #endregion // Protected Instance Methods
  246. public event EventHandler StartPageChanged;
  247. [Browsable(false)]
  248. [EditorBrowsable(EditorBrowsableState.Never)]
  249. public new event EventHandler TextChanged {
  250. add { base.TextChanged += value; }
  251. remove { base.TextChanged -= value; }
  252. }
  253. internal int vbar_value;
  254. internal int hbar_value;
  255. private void VScrollBarValueChanged (object sender, EventArgs e)
  256. {
  257. int pixels;
  258. if (vbar.Value > vbar_value)
  259. pixels = -1 * (vbar.Value - vbar_value);
  260. else
  261. pixels = vbar_value - vbar.Value;
  262. vbar_value = vbar.Value;
  263. XplatUI.ScrollWindow (Handle, ViewPort, 0, pixels, false);
  264. }
  265. private void HScrollBarValueChanged (object sender, EventArgs e)
  266. {
  267. int pixels;
  268. if (hbar.Value > hbar_value)
  269. pixels = -1 * (hbar.Value - hbar_value);
  270. else
  271. pixels = hbar_value - hbar.Value;
  272. hbar_value = hbar.Value;
  273. XplatUI.ScrollWindow (Handle, ViewPort, pixels, 0, false);
  274. }
  275. private void UpdateScrollBars ()
  276. {
  277. ViewPort = ClientRectangle;
  278. if (AutoZoom)
  279. return;
  280. int total_width, total_height;
  281. total_width = image_size.Width * Columns + (Columns + 1) * padding;
  282. total_height = image_size.Height * (Rows + 1) + (Rows + 2) * padding;
  283. bool vert = false;
  284. bool horz = false;
  285. if (total_width > ClientRectangle.Width) {
  286. /* we need the hbar */
  287. horz = true;
  288. ViewPort.Height -= hbar.Height;
  289. }
  290. if (total_height > ViewPort.Height) {
  291. /* we need the vbar */
  292. vert = true;
  293. ViewPort.Width -= vbar.Width;
  294. }
  295. if (!horz && total_width > ViewPort.Width) {
  296. horz = true;
  297. ViewPort.Height -= hbar.Height;
  298. }
  299. SuspendLayout ();
  300. if (vert) {
  301. vbar.SetValues (total_height, ViewPort.Height);
  302. vbar.Bounds = new Rectangle (ClientRectangle.Width - vbar.Width, 0, vbar.Width,
  303. ClientRectangle.Height -
  304. (horz ? SystemInformation.VerticalScrollBarWidth : 0));
  305. vbar.Visible = true;
  306. vbar_value = vbar.Value;
  307. }
  308. else {
  309. vbar.Visible = false;
  310. }
  311. if (horz) {
  312. hbar.SetValues (total_width, ViewPort.Width);
  313. hbar.Bounds = new Rectangle (0, ClientRectangle.Height - hbar.Height,
  314. ClientRectangle.Width - (vert ?
  315. SystemInformation.HorizontalScrollBarHeight : 0),
  316. hbar.Height);
  317. hbar.Visible = true;
  318. hbar_value = hbar.Value;
  319. }
  320. else {
  321. hbar.Visible = false;
  322. }
  323. ResumeLayout (false);
  324. }
  325. }
  326. }