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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2006 Novell, Inc.
23 // Jonathan Chambers (jonathan.chambers@ansys.com)
27 using System.ComponentModel;
28 using System.ComponentModel.Design;
29 using System.ComponentModel.Design.Serialization;
30 using System.Collections;
31 using System.Diagnostics;
33 using System.Drawing.Printing;
34 using System.Reflection;
35 using System.Runtime.InteropServices;
37 namespace System.Windows.Forms {
38 [DefaultPropertyAttribute("Document")]
40 [ClassInterface (ClassInterfaceType.AutoDispatch)]
43 public class PrintPreviewControl : Control {
44 #region Local variables
50 int padding = ThemeEngine.Current.PrintPreviewControlPadding;
51 PrintDocument document;
52 internal PreviewPrintController controller;
53 internal PreviewPageInfo[] page_infos;
54 private VScrollBar vbar;
55 private HScrollBar hbar;
57 internal Rectangle ViewPort;
58 internal Image[] image_cache;
61 #endregion // Local variables
63 #region Public Constructors
64 public PrintPreviewControl() {
70 this.BackColor = SystemColors.AppWorkspace;
72 controller = new PreviewPrintController ();
74 vbar = new ImplicitVScrollBar ();
75 hbar = new ImplicitHScrollBar ();
79 vbar.ValueChanged += new EventHandler (VScrollBarValueChanged);
80 hbar.ValueChanged += new EventHandler (HScrollBarValueChanged);
83 Controls.AddImplicit (vbar);
84 Controls.AddImplicit (hbar);
87 #endregion // Public Constructors
90 #region Public Instance Properties
92 public bool AutoZoom {
93 get { return autozoom; }
101 get { return columns; }
108 public PrintDocument Document {
109 get { return document; }
117 [AmbientValue (RightToLeft.Inherit)]
118 public override RightToLeft RightToLeft {
119 get { return base.RightToLeft; }
120 set { base.RightToLeft = value; }
133 public int StartPage {
134 get { return startPage; }
138 if (document != null && value + (Rows + 1) * Columns > page_infos.Length + 1) {
139 value = page_infos.Length + 1 - (Rows + 1) * Columns;
144 int start = StartPage;
146 if (start != startPage) {
148 OnStartPageChanged (EventArgs.Empty);
155 [EditorBrowsable(EditorBrowsableState.Never)]
156 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
157 public override string Text {
158 get { return base.Text; }
159 set { base.Text = value; }
162 [DefaultValue(false)]
163 public bool UseAntiAlias {
164 get { return controller.UseAntiAlias; }
165 set { controller.UseAntiAlias = value; }
175 throw new ArgumentException ("zoom");
181 #endregion // Public Instance Properties
184 #region Public Instance Methods
185 internal void GeneratePreview ()
187 if (document == null)
191 if (page_infos == null) {
192 if (document.PrintController == null || !(document.PrintController is PrintControllerWithStatusDialog)) {
193 document.PrintController = new PrintControllerWithStatusDialog (controller);
196 page_infos = controller.GetPreviewPageInfo ();
199 if (image_cache == null) {
200 image_cache = new Image[page_infos.Length];
202 if (page_infos.Length > 0) {
203 image_size = ThemeEngine.Current.PrintPreviewControlGetPageSize (this);
204 if (image_size.Width >= 0 && image_size.Width < page_infos[0].Image.Width
205 && image_size.Height >= 0 && image_size.Height < page_infos[0].Image.Height) {
207 for (int i = 0; i < page_infos.Length; i ++) {
208 image_cache[i] = new Bitmap (image_size.Width, image_size.Height);
209 Graphics g = Graphics.FromImage (image_cache[i]);
210 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);
218 catch (Exception e) {
219 page_infos = new PreviewPageInfo[0];
220 image_cache = new Image[0];
221 MessageBox.Show (e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
225 public void InvalidatePreview()
227 if (page_infos != null) {
228 for (int i = 0; i < page_infos.Length; i++) {
229 if (page_infos[i].Image != null) {
230 page_infos[i].Image.Dispose();
238 [EditorBrowsable(EditorBrowsableState.Never)]
239 public override void ResetBackColor()
241 base.ResetBackColor();
244 [EditorBrowsable(EditorBrowsableState.Never)]
245 public override void ResetForeColor()
247 base.ResetForeColor ();
249 #endregion // Public Instance Methods
251 #region Protected Instance Properties
252 protected override CreateParams CreateParams {
254 return base.CreateParams;
258 #endregion // Protected Instance Methods
260 #region Protected Instance Properties
262 protected override void OnPaint(PaintEventArgs pevent)
264 if (page_infos == null || image_cache == null)
266 ThemeEngine.Current.PrintPreviewControlPaint (pevent, this, image_size);
269 protected override void OnResize(EventArgs eventargs)
272 base.OnResize (eventargs);
275 protected virtual void OnStartPageChanged(EventArgs e)
277 EventHandler eh = (EventHandler)(Events [StartPageChangedEvent]);
282 protected override void WndProc(ref Message m)
284 base.WndProc (ref m);
287 #endregion // Protected Instance Methods
289 static object StartPageChangedEvent = new object ();
291 public event EventHandler StartPageChanged {
292 add { Events.AddHandler (StartPageChangedEvent, value); }
293 remove { Events.RemoveHandler (StartPageChangedEvent, value); }
297 [EditorBrowsable(EditorBrowsableState.Never)]
298 public new event EventHandler TextChanged {
299 add { base.TextChanged += value; }
300 remove { base.TextChanged -= value; }
303 internal int vbar_value;
304 internal int hbar_value;
306 #region UIA Framework Property
308 internal ScrollBar UIAVScrollBar {
312 internal ScrollBar UIAHScrollBar {
318 private void VScrollBarValueChanged (object sender, EventArgs e)
322 if (vbar.Value > vbar_value)
323 pixels = -1 * (vbar.Value - vbar_value);
325 pixels = vbar_value - vbar.Value;
327 vbar_value = vbar.Value;
328 XplatUI.ScrollWindow (Handle, ViewPort, 0, pixels, false);
332 private void HScrollBarValueChanged (object sender, EventArgs e)
336 if (hbar.Value > hbar_value)
337 pixels = -1 * (hbar.Value - hbar_value);
339 pixels = hbar_value - hbar.Value;
341 hbar_value = hbar.Value;
342 XplatUI.ScrollWindow (Handle, ViewPort, pixels, 0, false);
345 private void UpdateScrollBars ()
347 ViewPort = ClientRectangle;
351 int total_width, total_height;
353 total_width = image_size.Width * Columns + (Columns + 1) * padding;
354 total_height = image_size.Height * (Rows + 1) + (Rows + 2) * padding;
359 if (total_width > ClientRectangle.Width) {
360 /* we need the hbar */
362 ViewPort.Height -= hbar.Height;
364 if (total_height > ViewPort.Height) {
365 /* we need the vbar */
367 ViewPort.Width -= vbar.Width;
369 if (!horz && total_width > ViewPort.Width) {
371 ViewPort.Height -= hbar.Height;
377 vbar.SetValues (total_height, ViewPort.Height);
379 vbar.Bounds = new Rectangle (ClientRectangle.Width - vbar.Width, 0, vbar.Width,
380 ClientRectangle.Height -
381 (horz ? SystemInformation.VerticalScrollBarWidth : 0));
383 vbar_value = vbar.Value;
386 vbar.Visible = false;
390 hbar.SetValues (total_width, ViewPort.Width);
392 hbar.Bounds = new Rectangle (0, ClientRectangle.Height - hbar.Height,
393 ClientRectangle.Width - (vert ?
394 SystemInformation.HorizontalScrollBarHeight : 0),
397 hbar_value = hbar.Value;
400 hbar.Visible = false;
403 ResumeLayout (false);
406 private void InvalidateLayout() {
407 if (image_cache != null) {
408 for (int i = 0; i < image_cache.Length; i++) {
409 if (image_cache[i] !=null)
410 image_cache[i].Dispose();