Mercurial > projects > doodle
annotate gtk/canvas.d @ 27:f3d91579bb28
Checkpoint
author | David Bryant <daveb@acres.com.au> |
---|---|
date | Wed, 29 Jul 2009 14:11:35 +0930 |
parents | 06c30d250c0a |
children |
rev | line source |
---|---|
22 | 1 module gtk.canvas; |
0 | 2 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
3 public { |
22 | 4 import dia.icanvas; |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
5 import tk.events; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
6 } |
0 | 7 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
8 private { |
24 | 9 import gtk.conversions; |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
10 import tk.misc; |
24 | 11 import cairo.routines; |
0 | 12 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
13 import cairo.Surface; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
14 |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
15 import std.math; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
16 import std.stdio; |
0 | 17 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
18 import gtk.Widget; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
19 import gtk.Toolbar; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
20 import gtk.Table; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
21 import gtk.HRuler; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
22 import gtk.VRuler; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
23 import gtk.Range; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
24 import gtk.HScrollbar; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
25 import gtk.VScrollbar; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
26 import gtk.DrawingArea; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
27 import gtk.Adjustment; |
0 | 28 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
29 import gdk.Drawable; |
0 | 30 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
31 import gtkc.gtk; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
32 } |
0 | 33 |
34 // x and y run right and up respectively | |
35 | |
10 | 36 class Canvas : Table, Viewport { |
11 | 37 this(in Layer[] layers, EventHandler event_handler, in double ppi) { |
0 | 38 super(3, 3, 0); |
39 | |
17 | 40 mDamage = Rectangle.DEFAULT; |
41 | |
11 | 42 mLayers = layers.dup; |
0 | 43 mEventHandler = event_handler; |
13 | 44 mPPI = ppi; |
0 | 45 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
46 /* |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
47 writefln("Layer bounds: %s", layer_bounds); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
48 writefln("Canvas bounds: %s", mCanvasBounds); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
49 writefln("View centre: %s", mViewCentre); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
50 */ |
0 | 51 |
13 | 52 // Create our child widgets and register callbacks |
53 | |
54 mHRuler = new HRuler; | |
0 | 55 attach(mHRuler, |
56 1, 2, | |
57 0, 1, | |
58 AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.SHRINK, | |
59 0, 0); | |
20 | 60 mHRuler.setMetric(MetricType.PIXELS); |
0 | 61 |
13 | 62 mVRuler = new VRuler; |
0 | 63 attach(mVRuler, |
64 0, 1, | |
65 1, 2, | |
66 AttachOptions.SHRINK, AttachOptions.FILL | AttachOptions.EXPAND, | |
67 0, 0); | |
20 | 68 mVRuler.setMetric(MetricType.PIXELS); |
0 | 69 |
13 | 70 mDrawingArea = new DrawingArea; |
27 | 71 mDrawingArea.addOnRealize(&on_realize); |
72 mDrawingArea.addOnConfigure(&on_configure); | |
73 mDrawingArea.addOnExpose(&on_expose); | |
74 mDrawingArea.addOnButtonPress(&on_button_press); | |
75 mDrawingArea.addOnButtonRelease(&on_button_release); | |
76 mDrawingArea.addOnKeyPress(&on_key_event); | |
77 mDrawingArea.addOnKeyRelease(&on_key_event); | |
78 mDrawingArea.addOnMotionNotify(&on_motion_notify); | |
79 mDrawingArea.addOnScroll(&on_scroll); | |
80 mDrawingArea.addOnEnterNotify(&on_enter_notify); | |
81 mDrawingArea.addOnLeaveNotify(&on_leave_notify); | |
20 | 82 mDrawingArea.setEvents(EventMask.EXPOSURE_MASK | |
83 EventMask.POINTER_MOTION_MASK | | |
84 EventMask.POINTER_MOTION_HINT_MASK | | |
85 EventMask.BUTTON_MOTION_MASK | | |
86 EventMask.BUTTON_PRESS_MASK | | |
87 EventMask.BUTTON_RELEASE_MASK | | |
88 EventMask.KEY_PRESS_MASK | | |
89 EventMask.KEY_RELEASE_MASK | | |
90 EventMask.ENTER_NOTIFY_MASK | | |
91 EventMask.LEAVE_NOTIFY_MASK | | |
92 EventMask.FOCUS_CHANGE_MASK | | |
93 EventMask.SCROLL_MASK); | |
94 | |
0 | 95 attach(mDrawingArea, |
96 1, 2, | |
97 1, 2, | |
98 AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL | AttachOptions.EXPAND, | |
99 0, 0); | |
100 | |
101 // value, lower, upper, step-inc, page-inc, page-size | |
102 // Give the adjustments dummy values until we receive a configure | |
12 | 103 mHAdjustment = new Adjustment(0.0, 0.0, 1.0, 0.2, 0.5, 0.5); |
0 | 104 mHAdjustment.addOnValueChanged(&onValueChanged); |
105 mHScrollbar = new HScrollbar(mHAdjustment); | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
106 mHScrollbar.setInverted(false); |
0 | 107 attach(mHScrollbar, |
108 1, 2, | |
109 2, 3, | |
110 AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.SHRINK, | |
111 0, 0); | |
112 | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
113 mVAdjustment = new Adjustment(0.0, 0.0, 1.0, 0.2, 0.5, 0.5); |
0 | 114 mVAdjustment.addOnValueChanged(&onValueChanged); |
115 mVScrollbar = new VScrollbar(mVAdjustment); | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
116 mVScrollbar.setInverted(true); |
0 | 117 attach(mVScrollbar, |
118 2, 3, | |
119 1, 2, | |
120 AttachOptions.SHRINK, | |
121 AttachOptions.FILL | AttachOptions.EXPAND, | |
122 0, 0); | |
123 } | |
124 | |
26 | 125 override void zoom_relative(in Point pixel_datum, in double factor) { |
0 | 126 // Work out pixel distance from current centre to datum, |
127 // Do the zoom, then work out the new centre that keeps the | |
128 // pixel distance the same | |
129 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
130 Point old_model_datum = pixel_to_model(pixel_datum); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
131 Vector pixel_distance = model_to_pixel(old_model_datum - mViewCentre); |
0 | 132 mZoom = clamp_zoom(factor * mZoom); |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
133 mViewCentre = old_model_datum - pixel_to_model(pixel_distance); |
0 | 134 |
13 | 135 update_adjustments; |
136 update_rulers; | |
137 queueDraw; | |
0 | 138 } |
139 | |
26 | 140 override void pan_relative(in Vector pixel_displacement) { |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
141 mViewCentre = mViewCentre + pixel_to_model(pixel_displacement); |
0 | 142 |
13 | 143 update_adjustments; |
144 update_rulers; | |
145 queueDraw; | |
146 } | |
147 | |
27 | 148 override void set_cursor(in Cursor cursor) { |
149 CursorType cursor_type; | |
150 | |
151 switch (cursor) { | |
152 case Cursor.DEFAULT: | |
153 cursor_type = CursorType.ARROW; | |
154 break; | |
155 case Cursor.HAND: | |
156 cursor_type = CursorType.HAND1; | |
157 break; | |
158 case Cursor.CROSSHAIR: | |
159 cursor_type = CursorType.CROSSHAIR; | |
160 break; | |
161 } | |
162 | |
163 mDrawingArea.setCursor(new gdk.Cursor.Cursor(cursor_type)); | |
164 } | |
165 | |
26 | 166 override void damage_model(in Rectangle area) { |
17 | 167 mDamage = mDamage | model_to_pixel(area); |
168 } | |
169 | |
26 | 170 override void damage_pixel(in Rectangle area) { |
18 | 171 mDamage = mDamage | area; |
172 } | |
173 | |
0 | 174 private { |
175 | |
27 | 176 bool on_configure(GdkEventConfigure * event, Widget widget) { |
0 | 177 assert(widget is mDrawingArea); |
178 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
179 if (!mHadConfigure) { |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
180 const double MM_PER_INCH = 25.4; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
181 mZoom = 0.25 * mPPI / MM_PER_INCH; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
182 |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
183 // Take the union of the bounds of each layer to |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
184 // determine the canvas size |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
185 |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
186 Rectangle layer_bounds = Rectangle.DEFAULT; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
187 |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
188 foreach (ref layer; mLayers) { |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
189 layer_bounds = layer_bounds | layer.bounds; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
190 } |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
191 |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
192 assert(layer_bounds.valid); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
193 |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
194 mCanvasBounds = layer_bounds.moved(-layer_bounds.size).expanded(2.0 * layer_bounds.size); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
195 mViewCentre = mCanvasBounds.centre; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
196 |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
197 mHadConfigure = true; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
198 } |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
199 |
3
7d57cae10805
Renamed geometry2 to geometry
David Bryant <daveb@acres.com.au>
parents:
2
diff
changeset
|
200 mViewSize = Vector(cast(double)event.width, cast(double)event.height); |
13 | 201 update_adjustments; |
202 update_rulers; | |
0 | 203 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
204 //writefln("Canvas bounds: %s", mCanvasBounds); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
205 //writefln("View centre: %s", mViewCentre); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
206 |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
207 return true; |
0 | 208 } |
209 | |
27 | 210 bool on_expose(GdkEventExpose * event, Widget widget) { |
0 | 211 assert(widget is mDrawingArea); |
212 | |
13 | 213 Drawable dr = mDrawingArea.getWindow; |
0 | 214 |
215 int width, height; | |
216 dr.getSize(width, height); | |
217 //writefln("Got expose %dx%d\n", width, height); | |
218 | |
12 | 219 scope model_cr = new Context(dr); |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
220 scope pixel_cr = new Context(dr); |
12 | 221 |
19 | 222 Rectangle pixel_damage = |
11 | 223 event is null ? |
19 | 224 Rectangle(Point(0.0, 0.0), mViewSize) : |
225 Rectangle(Point(cast(double)event.area.x, mViewSize.y - cast(double)(event.area.y + event.area.height)), | |
11 | 226 Vector(cast(double)event.area.width, cast(double)event.area.height)); |
0 | 227 |
19 | 228 Rectangle model_damage = pixel_to_model(pixel_damage); |
229 | |
230 //writefln("Pixel damage: %s, model damage: %s", pixel_damage, model_damage); | |
231 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
232 model_cr.save; pixel_cr.save; { |
19 | 233 // Setup model context and clip |
12 | 234 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
235 GtkAdjustment * h_gtkAdjustment = mHAdjustment.getAdjustmentStruct; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
236 GtkAdjustment * v_gtkAdjustment = mVAdjustment.getAdjustmentStruct; |
12 | 237 |
13 | 238 model_cr.scale(mZoom, -mZoom); |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
239 model_cr.translate(-gtk_adjustment_get_value(h_gtkAdjustment), |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
240 -gtk_adjustment_get_value(v_gtkAdjustment) - gtk_adjustment_get_page_size(v_gtkAdjustment)); |
0 | 241 |
19 | 242 rectangle(model_cr, model_damage); |
243 model_cr.clip; | |
244 | |
245 // Setup pixel context and clip | |
12 | 246 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
247 pixel_cr.translate(0.0, mViewSize.y); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
248 pixel_cr.scale(1.0, -1.0); |
19 | 249 |
250 rectangle(pixel_cr, pixel_damage); | |
251 pixel_cr.clip; | |
12 | 252 |
253 // Fill the background | |
254 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
255 pixel_cr.save; { |
11 | 256 // Make the window light grey |
18 | 257 pixel_cr.setSourceRgba(0.6, 0.6, 0.6, 1.0); |
19 | 258 rectangle(pixel_cr, pixel_damage); |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
259 pixel_cr.fill; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
260 } pixel_cr.restore; |
12 | 261 |
262 // Draw each layer | |
0 | 263 |
11 | 264 foreach(ref layer; mLayers) { |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
265 model_cr.save; pixel_cr.save; { |
19 | 266 layer.draw(this, pixel_damage, pixel_cr, model_damage, model_cr); |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
267 } pixel_cr.restore; model_cr.restore; |
11 | 268 } |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
269 } pixel_cr.restore; model_cr.restore; |
0 | 270 |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
271 return true; |
0 | 272 } |
273 | |
27 | 274 bool on_button_press(GdkEventButton * event, Widget widget) { |
0 | 275 assert(widget is mDrawingArea); |
276 //writefln("Got button event\n"); | |
277 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
278 Point pixel_point = Point(event.x + 0.5, mViewSize.y - (event.y + 0.5)); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
279 Point model_point = pixel_to_model(pixel_point); |
0 | 280 |
7 | 281 auto button_event = new ButtonEvent(gtk2tk_button_action(event.type), |
282 gtk2tk_button_name(event.button), | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
283 pixel_point, |
0 | 284 model_point, |
285 gtk2tk_mask(event.state)); | |
286 | |
5 | 287 mEventHandler.handle_button_press(this, button_event); |
288 | |
17 | 289 process_damage; |
290 | |
5 | 291 return true; |
292 } | |
293 | |
27 | 294 bool on_button_release(GdkEventButton * event, Widget widget) { |
5 | 295 assert(widget is mDrawingArea); |
296 //writefln("Got button event\n"); | |
297 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
298 Point pixel_point = Point(event.x + 0.5, mViewSize.y - (event.y + 0.5)); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
299 Point model_point = pixel_to_model(pixel_point); |
5 | 300 |
7 | 301 auto button_event = new ButtonEvent(gtk2tk_button_action(event.type), |
302 gtk2tk_button_name(event.button), | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
303 pixel_point, |
5 | 304 model_point, |
305 gtk2tk_mask(event.state)); | |
306 | |
307 mEventHandler.handle_button_release(this, button_event); | |
0 | 308 |
17 | 309 process_damage; |
310 | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
311 return true; |
0 | 312 } |
313 | |
27 | 314 bool on_key_event(GdkEventKey * event, Widget widget) { |
0 | 315 assert(widget is mDrawingArea); |
316 //writefln("Got key event\n"); | |
317 | |
318 //auto key_event = new KeyEvent("", | |
319 // mEventHandle.handle_key(key_event); | |
320 | |
17 | 321 process_damage; |
322 | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
323 return true; |
0 | 324 } |
325 | |
27 | 326 bool on_motion_notify(GdkEventMotion * event, Widget widget) { |
0 | 327 assert(widget is mDrawingArea); |
328 //writefln("Got motion notify\n"); | |
329 gtk_widget_event(mHRuler.getWidgetStruct(), cast(GdkEvent *)event); | |
330 gtk_widget_event(mVRuler.getWidgetStruct(), cast(GdkEvent *)event); | |
331 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
332 Point pixel_point = Point(event.x + 0.5, mViewSize.y - (event.y + 0.5)); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
333 Point model_point = pixel_to_model(pixel_point); |
0 | 334 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
335 auto motion_event = new MotionEvent(pixel_point, |
0 | 336 model_point, |
337 gtk2tk_mask(event.state)); | |
338 | |
339 mEventHandler.handle_motion(this, motion_event); | |
340 | |
17 | 341 process_damage; |
342 | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
343 return true; |
0 | 344 } |
345 | |
27 | 346 bool on_scroll(GdkEventScroll * event, Widget widget) { |
0 | 347 assert(widget is mDrawingArea); |
348 //writefln("Got scroll\n"); | |
349 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
350 Point pixel_point = Point(event.x + 0.5, mViewSize.y - (event.y + 0.5)); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
351 Point model_point = pixel_to_model(pixel_point); |
0 | 352 |
353 auto scroll_event = new ScrollEvent(gtk2tk_direction(event.direction), | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
354 pixel_point, |
0 | 355 model_point, |
356 gtk2tk_mask(event.state)); | |
357 | |
358 mEventHandler.handle_scroll(this, scroll_event); | |
359 | |
17 | 360 process_damage; |
361 | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
362 return true; |
0 | 363 } |
364 | |
20 | 365 /* |
366 public enum GdkCrossingMode { | |
367 NORMAL, | |
368 GRAB, | |
369 UNGRAB, | |
370 GTK_GRAB, | |
371 GTK_UNGRAB, | |
372 STATE_CHANGED | |
373 } | |
374 | |
375 public struct GdkEventCrossing { | |
376 GdkEventType type; | |
377 GdkWindow *window; | |
378 byte sendEvent; | |
379 GdkWindow *subwindow; | |
380 uint time; | |
381 double x; | |
382 double y; | |
383 double xRoot; | |
384 double yRoot; | |
385 GdkCrossingMode mode; | |
386 GdkNotifyType detail; | |
387 int focus; | |
388 uint state; | |
389 } | |
390 */ | |
391 | |
27 | 392 bool on_enter_notify(GdkEventCrossing * event, Widget widget) { |
20 | 393 assert(widget is mDrawingArea); |
27 | 394 //writefln("Enter %d %d %d", cast(int)event.mode, event.focus, event.state); |
395 // TODO | |
20 | 396 return true; |
397 } | |
398 | |
27 | 399 bool on_leave_notify(GdkEventCrossing * event, Widget widget) { |
20 | 400 assert(widget is mDrawingArea); |
27 | 401 //writefln("Leave %d %d %d", cast(int)event.mode, event.focus, event.state); |
402 // TODO | |
20 | 403 return true; |
404 } | |
405 | |
0 | 406 void onValueChanged(Adjustment adjustment) { |
13 | 407 GtkAdjustment * h_gtkAdjustment = mHAdjustment.getAdjustmentStruct; |
408 GtkAdjustment * v_gtkAdjustment = mVAdjustment.getAdjustmentStruct; | |
0 | 409 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
410 Point view_left_top = Point(gtk_adjustment_get_value(h_gtkAdjustment), |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
411 gtk_adjustment_get_value(v_gtkAdjustment)); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
412 |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
413 Vector model_size = pixel_to_model(mViewSize); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
414 |
9 | 415 //writefln("%s", view_left_bottom); |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
416 mViewCentre = view_left_top + model_size / 2.0; |
19 | 417 //writefln("onValueChanged mViewCentre: %s", mViewCentre); |
0 | 418 |
13 | 419 update_rulers; |
0 | 420 |
13 | 421 queueDraw; |
0 | 422 } |
423 | |
424 void update_rulers() { | |
26 | 425 invariant Vector model_size = pixel_to_model(mViewSize); |
0 | 426 |
26 | 427 invariant Point view_left_bottom = mViewCentre - model_size / 2.0; |
428 invariant Point view_right_top = mViewCentre + model_size / 2.0; | |
0 | 429 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
430 // Define these just to obtain the position |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
431 // below and we can preserve it |
12 | 432 double lower, upper, position, max_size; |
433 | |
434 mHRuler.getRange(lower, upper, position, max_size); | |
0 | 435 mHRuler.setRange(view_left_bottom.x, |
436 view_right_top.x, | |
12 | 437 position, |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
438 mZoom * 50.0); |
12 | 439 |
440 mVRuler.getRange(lower, upper, position, max_size); | |
0 | 441 mVRuler.setRange(view_right_top.y, |
442 view_left_bottom.y, | |
14 | 443 position, |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
444 mZoom * 50.0); |
0 | 445 } |
446 | |
447 void update_adjustments() { | |
26 | 448 invariant Vector model_size = pixel_to_model(mViewSize); |
0 | 449 |
26 | 450 invariant Point view_left_bottom = mViewCentre - model_size / 2.0; |
451 invariant Point view_right_top = mViewCentre + model_size / 2.0; | |
0 | 452 |
453 // Adjust the canvas size if necessary | |
15 | 454 mCanvasBounds = Rectangle(min_extents(mCanvasBounds.min_corner, view_left_bottom), |
455 max_extents(mCanvasBounds.max_corner, view_right_top)); | |
0 | 456 |
457 // Update the adjustments | |
458 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
459 GtkAdjustment * h_gtkAdjustment = mHAdjustment.getAdjustmentStruct; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
460 GtkAdjustment * v_gtkAdjustment = mVAdjustment.getAdjustmentStruct; |
0 | 461 |
15 | 462 gtk_adjustment_set_lower(h_gtkAdjustment, mCanvasBounds.min_corner.x); |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
463 gtk_adjustment_set_upper(h_gtkAdjustment, mCanvasBounds.max_corner.x); |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
464 gtk_adjustment_set_value(h_gtkAdjustment, view_left_bottom.x); |
15 | 465 gtk_adjustment_set_step_increment(h_gtkAdjustment, mCanvasBounds.size.x / 16.0); |
466 gtk_adjustment_set_page_increment(h_gtkAdjustment, mCanvasBounds.size.x / 4.0); | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
467 gtk_adjustment_set_page_size(h_gtkAdjustment, model_size.x); |
0 | 468 |
15 | 469 gtk_adjustment_set_lower(v_gtkAdjustment, mCanvasBounds.min_corner.y); |
470 gtk_adjustment_set_upper(v_gtkAdjustment, mCanvasBounds.max_corner.y); | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
471 gtk_adjustment_set_value(v_gtkAdjustment, view_left_bottom.y); |
15 | 472 gtk_adjustment_set_step_increment(v_gtkAdjustment, mCanvasBounds.size.y / 16.0); |
473 gtk_adjustment_set_page_increment(v_gtkAdjustment, mCanvasBounds.size.y / 4.0); | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
474 gtk_adjustment_set_page_size(v_gtkAdjustment, model_size.y); |
0 | 475 |
13 | 476 mHAdjustment.changed; |
477 mHAdjustment.valueChanged; | |
478 mVAdjustment.changed; | |
479 mVAdjustment.valueChanged; | |
0 | 480 } |
481 | |
17 | 482 void process_damage() { |
483 if (mDamage.valid) { | |
18 | 484 //writefln("Damage: %s", mDamage); |
17 | 485 int x, y, w, h; |
486 mDamage.get_quantised(x, y, w, h); | |
27 | 487 mDrawingArea.queueDrawArea(x, cast(int)mViewSize.y - (y + h), w, h); |
17 | 488 mDamage = Rectangle.DEFAULT; |
489 } | |
490 else { | |
18 | 491 //writefln("No damage"); |
17 | 492 } |
493 } | |
494 | |
26 | 495 double clamp_zoom(in double zoom) { return clamp(zoom, 0.2, 10.0); } |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
496 |
27 | 497 Point model_to_pixel(in Point model) const { |
498 return Point.DEFAULT + mViewSize / 2.0 + mZoom * (model - mViewCentre); | |
499 } | |
500 | |
501 Point pixel_to_model(in Point pixel) const { | |
502 return mViewCentre + (pixel - mViewSize / 2.0 - Point.DEFAULT) / mZoom; | |
503 } | |
504 | |
505 Vector model_to_pixel(in Vector model) const { | |
506 return mZoom * model; | |
507 } | |
508 | |
509 Vector pixel_to_model(in Vector pixel) const { | |
510 return pixel / mZoom; | |
511 } | |
512 | |
513 Rectangle model_to_pixel(in Rectangle model) const { | |
514 return Rectangle(model_to_pixel(model.position), model_to_pixel(model.size)); | |
515 } | |
516 | |
517 Rectangle pixel_to_model(in Rectangle model) const { | |
518 return Rectangle(pixel_to_model(model.position), pixel_to_model(model.size)); | |
519 } | |
520 | |
521 void on_realize(Widget widget) { | |
522 assert(widget is mDrawingArea); | |
523 //writefln("Got realize\n"); | |
524 } | |
525 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
526 bool mHadConfigure; |
17 | 527 Rectangle mDamage; // pixels |
0 | 528 |
529 // Model units are in millimetres | |
530 // Screen units are in pixels | |
11 | 531 double mZoom; // pixels-per-model-unit (mm) |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
532 Vector mViewSize; // pixel: size of view window in pixels |
3
7d57cae10805
Renamed geometry2 to geometry
David Bryant <daveb@acres.com.au>
parents:
2
diff
changeset
|
533 Point mViewCentre; // model: where in the model is the centre of our view |
15 | 534 Rectangle mCanvasBounds; // model: |
0 | 535 |
10 | 536 // Child widgets: |
0 | 537 HRuler mHRuler; |
538 VRuler mVRuler; | |
539 DrawingArea mDrawingArea; | |
540 Adjustment mHAdjustment; | |
541 HScrollbar mHScrollbar; | |
542 Adjustment mVAdjustment; | |
543 VScrollbar mVScrollbar; | |
11 | 544 |
545 // Layers: | |
546 Layer[] mLayers; | |
13 | 547 EventHandler mEventHandler; |
548 double mPPI; | |
0 | 549 } |
550 } |