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")]
39 [ClassInterface (ClassInterfaceType.AutoDispatch)]
41 public class PrintPreviewControl : Control {
42 #region Local variables
48 int padding = ThemeEngine.Current.PrintPreviewControlPadding;
49 PrintDocument document;
50 internal PreviewPrintController controller;
51 internal PreviewPageInfo[] page_infos;
52 private VScrollBar vbar;
53 private HScrollBar hbar;
55 internal Rectangle ViewPort;
56 internal Image[] image_cache;
59 #endregion // Local variables
61 #region Public Constructors
62 public PrintPreviewControl() {
68 this.BackColor = SystemColors.AppWorkspace;
70 controller = new PreviewPrintController ();
72 vbar = new ImplicitVScrollBar ();
73 hbar = new ImplicitHScrollBar ();
77 vbar.ValueChanged += new EventHandler (VScrollBarValueChanged);
78 hbar.ValueChanged += new EventHandler (HScrollBarValueChanged);
81 Controls.AddImplicit (vbar);
82 Controls.AddImplicit (hbar);
85 #endregion // Public Constructors
88 #region Public Instance Properties
90 public bool AutoZoom {
91 get { return autozoom; }
99 get { return columns; }
106 public PrintDocument Document {
107 get { return document; }
114 [AmbientValue (RightToLeft.Inherit)]
115 public override RightToLeft RightToLeft {
116 get { return base.RightToLeft; }
117 set { base.RightToLeft = value; }
129 public int StartPage {
130 get { return startPage; }
134 if (document != null && value + (Rows + 1) * Columns > page_infos.Length + 1) {
135 value = page_infos.Length + 1 - (Rows + 1) * Columns;
140 int start = StartPage;
142 if (start != startPage) {
144 OnStartPageChanged (EventArgs.Empty);
151 [EditorBrowsable(EditorBrowsableState.Never)]
152 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
153 public override string Text {
154 get { return base.Text; }
155 set { base.Text = value; }
158 [DefaultValue(false)]
159 public bool UseAntiAlias {
160 get { return controller.UseAntiAlias; }
161 set { controller.UseAntiAlias = value; }
169 throw new ArgumentException ("zoom");
175 #endregion // Public Instance Properties
178 #region Public Instance Methods
179 internal void GeneratePreview ()
181 if (document == null)
185 if (page_infos == null) {
186 if (document.PrintController == null || !(document.PrintController is PrintControllerWithStatusDialog)) {
187 document.PrintController = new PrintControllerWithStatusDialog (controller);
190 page_infos = controller.GetPreviewPageInfo ();
193 if (image_cache == null) {
194 image_cache = new Image[page_infos.Length];
196 if (page_infos.Length > 0) {
197 image_size = ThemeEngine.Current.PrintPreviewControlGetPageSize (this);
198 if (image_size.Width >= 0 && image_size.Width < page_infos[0].Image.Width
199 && image_size.Height >= 0 && image_size.Height < page_infos[0].Image.Height) {
201 for (int i = 0; i < page_infos.Length; i ++) {
202 image_cache[i] = new Bitmap (image_size.Width, image_size.Height);
203 Graphics g = Graphics.FromImage (image_cache[i]);
204 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);
212 catch (Exception e) {
213 page_infos = new PreviewPageInfo[0];
214 image_cache = new Image[0];
215 MessageBox.Show (e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
219 public void InvalidatePreview()
221 if (page_infos != null) {
222 for (int i = 0; i < page_infos.Length; i++) {
223 if (page_infos[i].Image != null) {
224 page_infos[i].Image.Dispose();
232 [EditorBrowsable(EditorBrowsableState.Never)]
233 public override void ResetBackColor()
235 base.ResetBackColor();
238 [EditorBrowsable(EditorBrowsableState.Never)]
239 public override void ResetForeColor()
241 base.ResetForeColor ();
243 #endregion // Public Instance Methods
245 #region Protected Instance Properties
246 protected override CreateParams CreateParams {
248 return base.CreateParams;
252 #endregion // Protected Instance Methods
254 #region Protected Instance Properties
256 protected override void OnPaint(PaintEventArgs pevent)
258 if (page_infos == null || image_cache == null)
260 ThemeEngine.Current.PrintPreviewControlPaint (pevent, this, image_size);
263 protected override void OnResize(EventArgs eventargs)
266 base.OnResize (eventargs);
269 protected virtual void OnStartPageChanged(EventArgs e)
271 EventHandler eh = (EventHandler)(Events [StartPageChangedEvent]);
276 protected override void WndProc(ref Message m)
278 base.WndProc (ref m);
281 #endregion // Protected Instance Methods
283 static object StartPageChangedEvent = new object ();
285 public event EventHandler StartPageChanged {
286 add { Events.AddHandler (StartPageChangedEvent, value); }
287 remove { Events.RemoveHandler (StartPageChangedEvent, value); }
291 [EditorBrowsable(EditorBrowsableState.Never)]
292 public new event EventHandler TextChanged {
293 add { base.TextChanged += value; }
294 remove { base.TextChanged -= value; }
297 internal int vbar_value;
298 internal int hbar_value;
300 #region UIA Framework Property
301 internal ScrollBar UIAVScrollBar {
305 internal ScrollBar UIAHScrollBar {
310 private void VScrollBarValueChanged (object sender, EventArgs e)
314 if (vbar.Value > vbar_value)
315 pixels = -1 * (vbar.Value - vbar_value);
317 pixels = vbar_value - vbar.Value;
319 vbar_value = vbar.Value;
320 XplatUI.ScrollWindow (Handle, ViewPort, 0, pixels, false);
324 private void HScrollBarValueChanged (object sender, EventArgs e)
328 if (hbar.Value > hbar_value)
329 pixels = -1 * (hbar.Value - hbar_value);
331 pixels = hbar_value - hbar.Value;
333 hbar_value = hbar.Value;
334 XplatUI.ScrollWindow (Handle, ViewPort, pixels, 0, false);
337 private void UpdateScrollBars ()
339 ViewPort = ClientRectangle;
343 int total_width, total_height;
345 total_width = image_size.Width * Columns + (Columns + 1) * padding;
346 total_height = image_size.Height * (Rows + 1) + (Rows + 2) * padding;
351 if (total_width > ClientRectangle.Width) {
352 /* we need the hbar */
354 ViewPort.Height -= hbar.Height;
356 if (total_height > ViewPort.Height) {
357 /* we need the vbar */
359 ViewPort.Width -= vbar.Width;
361 if (!horz && total_width > ViewPort.Width) {
363 ViewPort.Height -= hbar.Height;
369 vbar.SetValues (total_height, ViewPort.Height);
371 vbar.Bounds = new Rectangle (ClientRectangle.Width - vbar.Width, 0, vbar.Width,
372 ClientRectangle.Height -
373 (horz ? SystemInformation.VerticalScrollBarWidth : 0));
375 vbar_value = vbar.Value;
378 vbar.Visible = false;
382 hbar.SetValues (total_width, ViewPort.Width);
384 hbar.Bounds = new Rectangle (0, ClientRectangle.Height - hbar.Height,
385 ClientRectangle.Width - (vert ?
386 SystemInformation.HorizontalScrollBarHeight : 0),
389 hbar_value = hbar.Value;
392 hbar.Visible = false;
395 ResumeLayout (false);
398 private void InvalidateLayout() {
399 if (image_cache != null) {
400 for (int i = 0; i < image_cache.Length; i++) {
401 if (image_cache[i] !=null)
402 image_cache[i].Dispose();