2 * This file is part of gtkD.
4 * gtkD is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
9 * gtkD is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with gtkD; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 module gtkD
.TestDrawingArea
;
23 private import gtk
.VBox
;
24 private import gdk
.Font
;
25 private import pango
.PgContext
;
26 private import pango
.PgLayout
;
27 private import gdk
.ImageGdk
;
29 version(Tango
) private import tango
.io
.Stdout
;
30 version(Tango
) private import tango
.stdc
.stdio
;
31 else private import std
.stdio
;
33 version(Tango
) private import tango
.math
.Math
;
34 else private import std
.math
;
36 private import gtk
.Widget
;
37 private import gtk
.MenuItem
;
38 private import gtk
.ComboBox
;
39 private import gtk
.Menu
;
40 private import gtk
.Adjustment
;
41 private import gtk
.HBox
;
42 private import gdk
.Pixbuf
;
43 //private import gdk.Point;
44 private import gdk
.Color
;
45 private import gdk
.Drawable
;
46 private import gdk
.GC
;
47 private import pango
.PgFontDescription
;
49 private import gtk
.DrawingArea
;
50 private import gtk
.Image
;
51 private import gtk
.SpinButton
;
53 private import gtkc
.gtktypes
;
54 private import gdk
.Event
;
56 private import gdk
.Color
;
58 //private import event.Event;
62 * This tests the gtkD drawing area widget
64 class TestDrawingArea
: VBox
71 TestDrawing drawingArea
= new TestDrawing();
73 ComboBox gcOptions
= new ComboBox();
74 gcOptions
.appendText("GC COPY");
75 gcOptions
.appendText("GC INVERT");
76 gcOptions
.appendText("GC XOR");
77 gcOptions
.appendText("GC CLEAR");
78 gcOptions
.appendText("GC AND");
79 gcOptions
.appendText("GC AND_REVERSE");
80 gcOptions
.appendText("GC AND_INVERT");
81 gcOptions
.appendText("GC NOOP");
82 gcOptions
.appendText("GC OR");
83 gcOptions
.appendText("GC EQUIV");
84 gcOptions
.appendText("GC OR_REVERSE");
85 gcOptions
.appendText("GC COPY_INVERT");
86 gcOptions
.appendText("GC OR_INVERT");
87 gcOptions
.appendText("GC NAND");
88 gcOptions
.appendText("GC NOR");
89 gcOptions
.appendText("GC SET");
90 gcOptions
.setActive(1);
91 gcOptions
.addOnChanged(&drawingArea
.onCGOptionsChanged
);
93 ComboBox primOption
= new ComboBox();
94 primOption
.appendText("Filled Arc");
95 primOption
.appendText("Arc");
96 primOption
.appendText("Line");
97 primOption
.appendText("Point");
98 primOption
.appendText("Rectangle");
99 primOption
.appendText("Filled Rectangle");
100 primOption
.appendText("Text");
101 primOption
.appendText("Pango text");
102 primOption
.appendText("Image");
103 primOption
.appendText("Polygon");
104 primOption
.setActive(0);
105 primOption
.addOnChanged(&drawingArea
.onPrimOptionChanged
);
107 packStart(drawingArea
,true,true,0);
109 HBox hbox
= new HBox(false,4);
110 hbox
.packStart(gcOptions
,false,false,2);
111 hbox
.packStart(primOption
,false,false,2);
112 hbox
.packStart(drawingArea
.spin
,false,false,2);
113 hbox
.packStart(drawingArea
.backSpin
,false,false,2);
116 packStart(hbox
, false, false, 0);
120 class TestDrawing
: DrawingArea
124 GdkFunction gcFunction
= GdkFunction
.INVERT
;
133 char[] primitiveType
;
134 PgFontDescription font
;
140 static GdkPoint
[] polygonStar
= [
153 setSizeRequest(333,333);
155 height
= getHeight();
157 primitiveType
= "Filled Arc";
158 font
= new PgFontDescription(PgFontDescription
.fromString("Courier 48"));
159 image
= new Image("images/gtkDlogo_a_small.png");
160 scaledPixbuf
= image
.getPixbuf();
161 paintColor
= new Color(cast(ubyte)0,cast(ubyte)0,cast(ubyte)0);
162 black
= new Color(cast(ubyte)0,cast(ubyte)0,cast(ubyte)0);
164 spin
= new SpinButton(new Adjustment(30, 1, 400, 1, 10, 10),1,0);
165 sizeSpinChanged(spin
);
166 spin
.addOnValueChanged(&sizeSpinChanged
);
167 backSpin
= new SpinButton(new Adjustment(5, 4, 100, 1, 10, 10),1,0);
168 backSpin
.addOnValueChanged(&backSpinChanged
);
172 addOnExpose(&exposeCallback
);
173 // addOnMap(&mapCallback);
174 addOnMotionNotify(&onMotionNotify
);
175 addOnSizeAllocate(&onSizeAllocate
);
176 addOnButtonPress(&onButtonPress
);
177 addOnButtonRelease(&onButtonRelease
);
180 void onSizeAllocate(GtkAllocation
* allocation
, Widget widget
)
182 width
= allocation
.width
;
183 height
= allocation
.height
;
187 public int onButtonPress(GdkEventButton
* event
, Widget widget
)
189 debug(trace
) version(Tango
) Stdout("button DOWN").newline
;
190 else writefln("button DOWN");
191 if ( event
.button
== 1 )
193 debug(trace
) version(Tango
) Stdout("Button 1 down").newline
;
194 else writefln("Button 1 down");
196 Drawable d
= getDrawable();
198 gc
.setForeground(new Color(cast(ubyte)0,cast(ubyte)0,cast(ubyte)0));
199 gc
.setFunction(gcFunction
);
201 drawPrimitive(gc
, d
, cast(int)event
.x
, cast(int)event
.y
); //event.getX(), event.getY());
203 gc
.setForeground(black
);
204 gc
.setFunction(GdkFunction
.COPY
);
209 public int onButtonRelease(GdkEventButton
* event
, Widget widget
)
211 debug(trace
) version(Tango
) Stdout("button UP").newline
;
212 else writefln("button UP");
213 if ( event
.button
== 1 )
215 debug(trace
) version(Tango
) Stdout("button 1 UP").newline
;
216 else writefln("Button 1 UP");
217 buttonIsDown
= false;
223 public bool mapCallback(Widget d
, Event event
)
225 //printf("MAP CALLBACK\n");
230 * This will be called from the expose event call back.
231 * \bug this is called on get or loose focus - review
233 public int exposeCallback(GdkEventExpose
* event
, Widget widget
)
235 //printf("testWindow.exposed ----------------------------- \n");
236 drawPoints(getDrawable());
239 public bool noExposeCallback(Widget widget
)
241 //printf("testWindow.noExposed ----------------------------- \n");
246 public int onMotionNotify(GdkEventMotion
* event
, Widget widget
)
248 //printf("testWindow.mouseMoved ----------------------------- \n");
251 Drawable d
= getDrawable();
253 gc
.setForeground(paintColor
);
254 gc
.setFunction(gcFunction
);
256 drawPrimitive(gc
, d
, cast(int)event
.x
, cast(int)event
.y
);
257 //d.drawPoint(event.getX(),event.getY());
258 //d.drawArc(true,event.getX()-2,event.getY()-2,4,4,0,64*360);
259 //d.drawRectangle(true,event.getX()-2,event.getY()-2,4,4);
261 gc
.setForeground(black
);
262 gc
.setFunction(GdkFunction
.COPY
);
267 public bool mouseButtonReleaseCallback(Widget widget
, GdkEventButton event
)
269 //printf("testWindow.buttonReleased ----------------------------- \n");
273 static int backSpinCount
= 0;
275 public void backSpinChanged(SpinButton spinButton
)
278 debug(trace
) version(Tango
) Stdout
.format("backSpinChanged - entry {}", ++backSpinCount
).newline
;
279 else writefln("backSpinChanged - entry %s", ++backSpinCount
);
280 drawPoints(getDrawable());
281 GC gc
= new GC(getDrawable());
282 debug(trace
) version(Tango
) Stdout("backSpinChanged - exit").newline
;
283 else writefln("backSpinChanged - exit");
286 public void sizeSpinChanged(SpinButton spinButton
)
288 int width
= spinButton
.getValueAsInt();
289 scaledPixbuf
= image
.getPixbuf();
291 float ww
= width
* scaledPixbuf
.getWidth() / 30;
292 float hh
= width
* scaledPixbuf
.getHeight() / 30;
294 scaledPixbuf
= scaledPixbuf
.scaleSimple(cast(int)ww
, cast(int)hh
, GdkInterpType
.HYPER
);
299 public void drawPrimitive(GC gc
, Drawable d
, int x
, int y
)
301 int width
= spin
.getValueAsInt();
302 int height
= width
* 3 / 4;
303 debug(trace
) version(Tango
) Stdout
.format("primitiveType = {}", primitiveType
).newline
;
304 else writefln("primitiveType = %s", primitiveType
);
305 switch ( primitiveType
)
308 d
.drawArc(gc
, false,x
-width
/2,y
-width
/2,width
,width
,0,64*360);
312 d
.drawArc(gc
, true,x
-width
/4,y
-width
/4,width
/2,width
/2,0,64*360);
316 d
.drawLine(gc
, x
, y
, x
+width
, y
);
320 d
.drawPoint(gc
, x
, y
);
324 d
.drawRectangle(gc
, false, x
-width
/2, y
-width
/4, width
, height
);
327 case "Filled Rectangle":
328 d
.drawRectangle(gc
, true, x
-width
/2, y
-width
/4, width
, height
);
332 Font font
= new Font("FreeMono 12");
333 debug(trace
) version(Tango
) Stdout
.format("Text font = {}", font
).newline
;
334 else writefln("Text font = %s", font
);
335 d
.drawString( font
, gc
,x
, y
, "gtkD toolkit");
339 //PgContext pc = getPangoContext();
340 //PgLayout l = new PgLayout(pc);
341 PgLayout l
= createPangoLayout("GtkD with D");
342 //l.setText("GtkD with D");
343 //PgFontDescription fd = l.getFontDescription();
344 PgFontDescription fd
= new PgFontDescription("Sans", width
);
345 l
.setFontDescription(fd
);
347 d
.drawLayout(gc
, x
,y
,l
);
351 scaledPixbuf
.renderToDrawable(d
.getDrawableStruct(), gc
.getGCStruct(),
353 scaledPixbuf
.getWidth(), scaledPixbuf
.getHeight(),
354 GdkRgbDither
.NONE
, 0, 0
360 pol1
.length
= polygonStar
.length
;
361 for ( int scale
= 10 ; scale
<= 300; scale
+=15)
363 foreach(int i
, inout GdkPoint p
; polygonStar
)
365 pol1
[i
].x
= p
.x
*scale
/2+x
;
366 pol1
[i
].y
= p
.y
*scale
/2+y
;
368 d
.drawPolygon(gc
, true , pol1
);
373 d
.drawArc(gc
, true,x
-2,y
-2,4,4,0,64*360);
378 private void drawPoints(Drawable d
)
380 int square
= backSpin
.getValueAsInt();
383 Color color
= new Color();
385 int width
= this.width
;
386 int height
= this.height
;
390 debug(trace
) version(Tango
) Stdout
.format("w,h = {} {}",width
,height
).newline
;
391 else writefln("w,h = %s %s",width
,height
);
393 debug(trace
) version(Tango
) Stdout
.format("w,h = {} {}",width
,height
).newline
;
394 else writefln("w,h = %s %s",width
,height
);
396 float dx
= 256.0 / width
;
397 float dy
= 256.0 / height
;
400 while ( x
< width || y
<height
)
404 color
.set8( cast(byte)xx
,
406 cast(byte)(sqrt((xx
*xx
)+(yy
*yy
))));
407 gc
.setForeground(color
);
410 //d.drawPoint(gc, x,y);
411 d
.drawRectangle(gc
, true,x
,y
,square
,square
);
413 //d.drawArc(gc, 1, 10, 10, 100, 100, 45, 60);
417 d
.drawPoint(gc
, x
,y
);
434 gc
.setForeground(black
);
438 void onCGOptionsChanged(ComboBox comboBox
)
440 debug(trace
) version(Tango
) Stdout
.format("gcOptions = {}", comboBox
.getActiveText()).newline
;
441 else writefln("gcOptions = %s", comboBox
.getActiveText());
442 switch ( comboBox
.getActiveText() )
444 case "GC COPY": gcFunction
= GdkFunction
.COPY
; break;
445 case "GC INVERT": gcFunction
= GdkFunction
.INVERT
; break;
446 case "GC XOR": gcFunction
= GdkFunction
.XOR
; break;
447 case "GC CLEAR": gcFunction
= GdkFunction
.CLEAR
; break;
448 case "GC AND": gcFunction
= GdkFunction
.AND
; break;
449 case "GC AND_REVERSE": gcFunction
= GdkFunction
.AND_REVERSE
; break;
450 case "GC AND_INVERT": gcFunction
= GdkFunction
.AND_INVERT
; break;
451 case "GC NOOP": gcFunction
= GdkFunction
.NOOP
; break;
452 case "GC OR": gcFunction
= GdkFunction
.OR
; break;
453 case "GC EQUIV": gcFunction
= GdkFunction
.EQUIV
; break;
454 case "GC OR_REVERSE": gcFunction
= GdkFunction
.OR_REVERSE
; break;
455 case "GC COPY_INVERT": gcFunction
= GdkFunction
.COPY_INVERT
; break;
456 case "GC OR_INVERT": gcFunction
= GdkFunction
.OR_INVERT
; break;
457 case "GC NAND": gcFunction
= GdkFunction
.NAND
; break;
458 case "GC NOR": gcFunction
= GdkFunction
.NOR
; break;
459 case "GC SET": gcFunction
= GdkFunction
.SET
; break;
460 default: gcFunction
= GdkFunction
.INVERT
; break;
464 void onPrimOptionChanged(ComboBox comboBox
)
466 primitiveType
= comboBox
.getActiveText();
470 // void primitive(MenuItem item)
472 // printf("TestDrawing.primitive item command = %.*s\n", item.getActionCommand().toString());
473 // primitiveType.set(item.getActionCommand().dup);