]> granicus.if.org Git - graphviz/commitdiff
basic viewer app for Windows
authorglenlow <devnull@localhost>
Wed, 19 Mar 2008 04:15:06 +0000 (04:15 +0000)
committerglenlow <devnull@localhost>
Wed, 19 Mar 2008 04:15:06 +0000 (04:15 +0000)
windows/ScrollableImageControl.cs [new file with mode: 0755]

diff --git a/windows/ScrollableImageControl.cs b/windows/ScrollableImageControl.cs
new file mode 100755 (executable)
index 0000000..484322e
--- /dev/null
@@ -0,0 +1,133 @@
+/* $Id$ $Revision$ */\r
+/* vim:set shiftwidth=4 ts=8: */\r
+\r
+/**********************************************************\r
+*      This software is part of the graphviz package      *\r
+*                http://www.graphviz.org/                 *\r
+*                                                         *\r
+*            Copyright (c) 1994-2008 AT&T Corp.           *\r
+*                and is licensed under the                *\r
+*            Common Public License, Version 1.0           *\r
+*                      by AT&T Corp.                      *\r
+*                                                         *\r
+*        Information and Software Systems Research        *\r
+*              AT&T Research, Florham Park NJ             *\r
+**********************************************************/\r
+\r
+using System;\r
+using System.Drawing;\r
+using System.Drawing.Drawing2D;\r
+using System.Windows.Forms;\r
+\r
+namespace Graphviz\r
+{\r
+       public partial class ScrollableImageControl : ScrollableControl\r
+       {\r
+               public Image Image\r
+               {\r
+                       get\r
+                       {\r
+                               return _image;\r
+                       }\r
+                       \r
+                       set\r
+                       {\r
+                               _image = value;\r
+                               UpdateAutoScroll();\r
+                       }\r
+               }\r
+               \r
+               public float Zoom\r
+               {\r
+                       get\r
+                       {\r
+                               return _zoom;\r
+                       }\r
+                       \r
+                       set\r
+                       {\r
+                               _zoom = value;\r
+                               UpdateAutoScroll();\r
+                       }\r
+               }\r
+               \r
+               public ScrollableImageControl()\r
+               {\r
+                       InitializeComponent();\r
+\r
+                       SetStyle(ControlStyles.AllPaintingInWmPaint |\r
+                               ControlStyles.DoubleBuffer |\r
+                               ControlStyles.ResizeRedraw |\r
+                               ControlStyles.UserMouse |\r
+                               ControlStyles.UserPaint,\r
+                               true);\r
+                       _image = null;\r
+                       _zoom = 1.0f;\r
+                       _lastScroll = new Point(0, 0);\r
+               }\r
+\r
+               protected override void OnMouseDown(MouseEventArgs e)\r
+               {\r
+                       /* if potentially panning with the mouse, record last scroll position */\r
+                       /* NOTE: Autoscroll should be set to +ve values but always returns -ve values */\r
+                       if (e.Button == MouseButtons.Left)\r
+                               _lastScroll = new Point(\r
+                                       e.X - AutoScrollPosition.X,\r
+                                       e.Y - AutoScrollPosition.Y);\r
+                       base.OnMouseDown(e);\r
+               }\r
+\r
+               protected override void OnMouseMove(MouseEventArgs e)\r
+               {\r
+                       /* if panning with the mouse, scroll to position */\r
+                       /* NOTE: Autoscroll should be set to +ve values but always returns -ve values */\r
+                       if (e.Button == MouseButtons.Left)\r
+                               AutoScrollPosition = new Point(\r
+                                       _lastScroll.X - e.X,\r
+                                       _lastScroll.Y - e.Y);\r
+                       base.OnMouseMove(e);\r
+               }\r
+\r
+               protected override void OnPaintBackground(PaintEventArgs e)\r
+               {\r
+                       /* fill clip rectangle with background color */\r
+                       e.Graphics.FillRectangle(new SolidBrush(BackColor), e.ClipRectangle);           \r
+               }\r
+               \r
+               protected override void OnPaint(PaintEventArgs pe)\r
+               {\r
+                       if (_image != null) {\r
+                               Matrix oldMatrix = pe.Graphics.Transform;\r
+                               \r
+                               /* if the zoomed image size is smaller than the control size, center the display */\r
+                               /* adjust by the scroll position, then scale by the zoom factor */\r
+                               pe.Graphics.TranslateTransform(\r
+                                       Math.Max((Width - _image.Width * _zoom) / 2.0f, 0.0f) + AutoScrollPosition.X,\r
+                                       Math.Max((Height - _image.Height * _zoom) / 2.0f, 0.0f) + AutoScrollPosition.Y);\r
+                               pe.Graphics.ScaleTransform(_zoom, _zoom);\r
+                       \r
+                               /* blit the image onto the graphics */\r
+                               pe.Graphics.DrawImage(_image, 0.0f, 0.0f);\r
+                               \r
+                               pe.Graphics.Transform = oldMatrix;\r
+                       }\r
+                       base.OnPaint(pe);\r
+                       \r
+               }\r
+               \r
+               private void UpdateAutoScroll()\r
+               {\r
+                       if (_image == null)\r
+                               AutoScrollMinSize = Size;\r
+                       else\r
+                               AutoScrollMinSize = new Size(\r
+                                       (int)(_image.Width * _zoom + 0.5f),\r
+                                       (int)(_image.Height * _zoom + 0.5f));\r
+                       Invalidate();\r
+               }\r
+               \r
+               private Image _image;\r
+               private float _zoom;\r
+               private Point _lastScroll;\r
+       }\r
+}\r