Rune - Fix array passing, optimize constant array indices
[rune.git] / tests / gfx2.d
1 #!/usr/local/bin/rune -x
2 #
3 #       Graphical test
4
5 import "sys";
6 import <stdio>;
7 import <gfx>;
8
9 alias stdio.File *stdout = stdio.stdout;
10
11 typedef gfx.Event       Event;
12 typedef gfx.Frame       Frame;
13
14 subclass Frame as MyFrame {
15         int holding;
16         gfx.Point savePos;
17
18         refine public method
19         bool
20         refreshFrame(int x, int y, int w, int h)
21         {
22                 int i;
23                 int j;
24                 for (i = 0; i < 512; i += 32) {
25                         for (j = 0; j < 512; j += 32) {
26                                 this.drawLine(0, i, j, 0);
27                         }
28                 }
29         }
30
31         refine public method 
32         int
33         mouseMoved(Event *ev) 
34         {
35                 if (this.holding == Event.XK_Pointer_Button1)
36                         this.drawLineTo(ev->pos.x, ev->pos.y);
37                 if (this.holding == Event.XK_Pointer_Button2)
38                         this.drawPoint(ev->pos.x, ev->pos.y);
39         }
40
41         refine public method 
42         int
43         buttonPressed(Event *ev) 
44         {
45                 switch(ev->keySym) {
46                 case Event.XK_Pointer_Button1:
47                         this.savePos = ev->pos;
48                         /* fall through */
49                 case Event.XK_Pointer_Button2:
50                         this.drawPoint(ev->pos.x, ev->pos.y);
51                         this.holding = ev->keySym;
52                         stdout->show("position", ev->pos.x, ev->pos.y);
53                         break;
54                 case -Event.XK_Pointer_Button1:
55                 case -Event.XK_Pointer_Button2:
56                         if (this.holding == Event.XK_Pointer_Button1) {
57                                 this.drawLineTo(ev->pos.x, ev->pos.y);
58                         }
59                         this.holding = 0;
60                         break;
61                 case Event.XK_Pointer_Button3:
62                         this.pen->setFGColor(0xFFFF, 0, 0);
63                         this.savePos = ev->pos;
64                         break;
65                 case -Event.XK_Pointer_Button3:
66                         this.fillRect(this.savePos.x, this.savePos.y,
67                                       ev->pos.x, ev->pos.y);
68                         break;
69                 default:
70                         if (ev->keySym > 0 && ev->keyChar > 0UB) {
71                                 this.drawText(this.savePos.x, this.savePos.y,
72                                               &ev->keyChar, 1);
73                                 this.savePos.x += this.textWidth(&ev->keyChar,
74                                                                  1);
75                         }
76                 }
77         }
78 }
79
80 int waiting;
81
82 int
83 main(int ac, char **av)
84 {
85         MyFrame *frame;
86         int i;
87         int j;
88
89         frame.createFrame("BODY", &gfx.root, 512, 512, BODY);
90         frame->recalc();
91         frame->flush();
92
93         # If we unlink and NULL out the frame, there will be no refs
94         # and Rune should destroy it.
95         #
96         # If we do not unlink the frame the frame thread will prevent
97         # us from dying.
98         #
99         #stdout->show("should exit after 2.5 seconds");
100         delayedwake();
101         Thread.tsleep(&waiting, 5000);
102         stdout->show("main woke up");
103         frame->unlinkFrame();
104         # frame->destroyFrame(); - NULLing it out should be good enough
105         frame = NULL;
106         stdout->show("waiting");
107         Thread.waitThreads();           # wait until all threads have exited
108         stdout->show("returning");
109
110         return(0);
111 }
112
113 thread
114 void
115 delayedwake()
116 {
117         result;
118         Thread.mssleep(2500);
119         Thread.wakeup(&waiting);
120 }