]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/services/gfx/mw/v2_0/src/demos/nxscribble/nxscribble.c
Initial revision
[karo-tx-redboot.git] / packages / services / gfx / mw / v2_0 / src / demos / nxscribble / nxscribble.c
1 /*
2  * Copyright (c) 2000 Greg Haerr <greg@censoft.com>
3  * Copyright (c) 2000 Century Software <embedded.centurysoftware.com>
4  *
5  * Scribble Handwriting Recognition for Nano-X!
6  */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #define MWINCLUDECOLORS
10 #include "nano-X.h"
11 #include "scrib.h"
12
13 #define TEXTWIN_WIDTH   200             /* text window width/height*/
14 #define TEXTWIN_HEIGHT  150
15
16 static ScribbleWidget   w;
17
18 static GR_BOOL          bTextwin = GR_FALSE;
19 static GR_WINDOW_ID     wt = 0;
20 static GR_GC_ID         gct = 0;
21 static GR_GC_ID         gctb = 0;
22 static GR_COORD         xpos = 0;
23 static GR_COORD         ypos = 0;
24 static GR_SIZE          width;          /* width of character */
25 static GR_SIZE          height;         /* height of character */
26 static GR_SIZE          base;           /* height of baseline */
27 static void char_out(GR_CHAR ch);
28 static void char_del(GR_COORD x, GR_COORD y);
29
30 void do_buttondown(GR_EVENT_BUTTON      *bp);
31 void do_buttonup(GR_EVENT_BUTTON        *bp);
32 void do_motion(GR_EVENT_MOUSE           *mp);
33 void do_focusin(GR_EVENT_GENERAL        *gp);
34 void do_keystroke(GR_EVENT_KEYSTROKE    *kp);
35 void do_exposure(GR_EVENT_EXPOSURE      *ep);
36
37 int
38 main(int argc, char **argv)
39 {
40         int             t = 1;
41         GR_EVENT        event;          /* current event */
42
43         while (t < argc) {
44                 if (!strcmp("-t", argv[t])) {
45                         bTextwin = GR_TRUE;
46                         ++t;
47                         continue;
48                 }
49         }
50
51         if (GrOpen() < 0) {
52                 fprintf(stderr, "cannot open graphics\n");
53                 exit(1);
54         }
55
56         if (bTextwin) {
57                 /* create text output window for debugging*/
58                 wt = GrNewWindow(GR_ROOT_WINDOW_ID, 50, 20,
59                                 TEXTWIN_WIDTH, TEXTWIN_HEIGHT, 5, BLACK, GREEN);
60                 GrSelectEvents(wt, 
61                         GR_EVENT_MASK_CLOSE_REQ | GR_EVENT_MASK_KEY_DOWN
62                         | GR_EVENT_MASK_EXPOSURE);
63                 GrMapWindow(wt);
64                 gct = GrNewGC();
65                 GrSetGCForeground(gct, GREEN);
66                 GrGetGCTextSize(gct, "A",1, GR_TFASCII, &width, &height, &base);
67                 GrSetGCFont(gct, GrCreateFont(GR_FONT_OEM_FIXED, 0, NULL));
68                 gctb = GrNewGC();
69                 GrSetGCForeground(gctb, BLACK);
70         }
71
72         /* create scribble input window*/
73         w = create_scribble();
74
75         while (1) {
76                 GrGetNextEvent(&event);
77
78                 switch (event.type) {
79                         case GR_EVENT_TYPE_BUTTON_DOWN:
80                                 do_buttondown(&event.button);
81                                 break;
82
83                         case GR_EVENT_TYPE_BUTTON_UP:
84                                 do_buttonup(&event.button);
85                                 break;
86
87                         case GR_EVENT_TYPE_MOUSE_POSITION:
88                         case GR_EVENT_TYPE_MOUSE_MOTION:
89                                 do_motion(&event.mouse);
90                                 break;
91
92                         case GR_EVENT_TYPE_FOCUS_IN:
93                                 do_focusin(&event.general);
94                                 break;
95
96                         case GR_EVENT_TYPE_KEY_DOWN:
97                                 do_keystroke(&event.keystroke);
98                                 break;
99
100                         case GR_EVENT_TYPE_EXPOSURE:
101                                 do_exposure(&event.exposure);
102                                 break;
103
104                         case GR_EVENT_TYPE_CLOSE_REQ:
105                                 GrClose();
106                                 exit(0);
107                 }
108         }
109 }
110
111
112 /*
113  * Here when a button is pressed.
114  */
115 void
116 do_buttondown(GR_EVENT_BUTTON   *bp)
117 {
118         ActionStart(w, bp->x, bp->y);
119 }
120
121
122 /*
123  * Here when a button is released.
124  */
125 void
126 do_buttonup(GR_EVENT_BUTTON     *bp)
127 {
128         ActionEnd(w, bp->x, bp->y);
129 }
130
131
132 /*
133  * Here when the mouse has a motion event.
134  */
135 void
136 do_motion(GR_EVENT_MOUSE        *mp)
137 {
138         ActionMove(w, mp->x, mp->y);
139 }
140
141
142 /*
143  * Here when our window gets focus
144  */
145 void
146 do_focusin(GR_EVENT_GENERAL     *gp)
147 {
148 #if 0
149         /* if the window receiving focus is scribble, remember last window*/
150         if (gp->wid == w->win && gp->wid != 1)
151                 w->lastfocusid = gp->otherid;
152 #endif
153 }
154
155
156 /*
157  * Here when an exposure event occurs.
158  */
159 void
160 do_exposure(GR_EVENT_EXPOSURE   *ep)
161 {
162         if (ep->wid == w->win)
163                 Redisplay(w);
164 }
165
166
167 /*
168  * Here when a keyboard press or injection occurs.
169  */
170 void
171 do_keystroke(GR_EVENT_KEYSTROKE *kp)
172 {
173         if (bTextwin)
174                 char_out(kp->ch);
175 }
176
177 static void
178 char_del(GR_COORD x, GR_COORD y)
179 {
180         xpos -= width;
181         GrFillRect(wt, gctb, x+1, y /*- height*/ /*+ base*/, width, height);
182 }
183
184 static void
185 char_out(GR_CHAR ch)
186 {
187         switch(ch) {
188         case '\r':
189         case '\n':
190                 xpos = 0;
191                 ypos += height;
192                 if(ypos >= TEXTWIN_HEIGHT - height) {
193                         ypos -= height;
194
195                         /* FIXME: changing FALSE to TRUE crashes nano-X*/
196                         /* clear screen, no scroll*/
197                         ypos = 0;
198                         GrClearWindow(wt, GR_FALSE);
199                 }
200                 return;
201         case '\007':                    /* bel*/
202                 return;
203         case '\t':
204                 xpos += width;
205                 while((xpos/width) & 7)
206                         char_out(' ');
207                 return;
208         case '\b':                      /* assumes fixed width font!!*/
209                 if (xpos <= 0)
210                         return;
211                 char_del(xpos, ypos);
212                 char_out(' ');
213                 char_del(xpos, ypos);
214                 return;
215         }
216         GrText(wt, gct, xpos+1, ypos, &ch, 1, GR_TFTOP);
217         xpos += width;
218
219         if (xpos >= TEXTWIN_WIDTH-width)
220                 char_out('\n');
221 }