disable debug
[AROS.git] / workbench / demos / register.c
blobb0bf1da8a52b5c8852ad0dc654cea8c9ee267edf
1 #include <intuition/intuition.h>
2 #include <graphics/gfx.h>
3 #include <graphics/text.h>
4 #include <proto/exec.h>
5 #include <proto/intuition.h>
6 #include <proto/graphics.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
12 /****************************************************************************************/
14 #define REGISTER_EXTRA_HEIGHT 4
15 #define REGISTERITEM_EXTRA_WIDTH 8
16 #define REGISTER_SPACE_LEFT 8
17 #define REGISTER_SPACE_RIGHT 8
19 struct Register
21 struct RegisterItem *items;
22 struct DrawInfo *dri;
23 WORD numitems;
24 WORD active;
25 WORD left;
26 WORD top;
27 WORD width;
28 WORD height;
29 WORD fontw;
30 WORD fonth;
31 WORD fontb;
32 WORD slopew;
35 struct RegisterItem
37 STRPTR text;
38 WORD textlen;
39 WORD x1, y1, x2, y2, w, h;
40 WORD tx, ty;
43 /****************************************************************************************/
45 struct IntuitionBase *IntuitionBase;
46 struct GfxBase *GfxBase;
48 struct Screen *scr;
49 struct DrawInfo *dri;
50 struct Window *win;
52 /****************************************************************************************/
54 struct RegisterItem myregisteritems[] =
56 {"Timezone"},
57 {"Country"},
58 {"Language"},
59 {"Something"},
60 {"AROS"},
61 {NULL}
63 struct Register myregister;
65 /****************************************************************************************/
67 static void cleanup(char *msg)
69 if (msg) printf("register: %s\n", msg);
71 if (win) CloseWindow(win);
73 if (dri) FreeScreenDrawInfo(scr, dri);
74 if (scr) UnlockPubScreen(NULL, scr);
76 if (GfxBase) CloseLibrary((struct Library *)GfxBase);
77 if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
79 exit (0);
82 /****************************************************************************************/
84 static void openlibs(void)
86 if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 39)))
88 cleanup("Can't open intuition.library V39!");
91 if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 39)))
93 cleanup("Can't open graphics.library V39!");
97 /****************************************************************************************/
99 static void getvisual(void)
101 scr = LockPubScreen(NULL);
102 if (!scr) cleanup("Can't lock pub screen!");
104 dri = GetScreenDrawInfo(scr);
105 if (!dri) cleanup("Can't get drawinfo!");
109 /****************************************************************************************/
111 static void initregister(struct Register *reg, struct RegisterItem *items)
113 reg->items = items;
114 reg->numitems = 0;
116 while(items->text)
118 reg->numitems++;
119 items++;
123 /****************************************************************************************/
125 static void layoutregister(struct Register *reg, struct Screen *scr,
126 struct DrawInfo *dri, BOOL samewidth)
128 struct RastPort temprp;
129 WORD i, x, biggest_w = 0;
131 InitRastPort(&temprp);
132 SetFont(&temprp, dri->dri_Font);
133 reg->dri = dri;
134 reg->fonth = dri->dri_Font->tf_YSize;
135 reg->fontb = dri->dri_Font->tf_Baseline;
137 reg->height = ((reg->fonth + REGISTER_EXTRA_HEIGHT) + 3) & ~3; /* Multiple of 4 */
138 reg->height += 4;
140 reg->slopew = (reg->height - 4) / 2;
142 for(i = 0; i < reg->numitems; i++)
144 reg->items[i].textlen = strlen(reg->items[i].text);
145 reg->items[i].w = TextLength(&temprp, reg->items[i].text, reg->items[i].textlen);
146 reg->items[i].w += REGISTERITEM_EXTRA_WIDTH + reg->slopew * 2;
148 if (reg->items[i].w > biggest_w) biggest_w = reg->items[i].w;
151 if (samewidth)
153 for(i = 0; i < reg->numitems; i++)
155 reg->items[i].w = biggest_w;
159 x = REGISTER_SPACE_LEFT;
160 for(i = 0; i < reg->numitems; i++)
162 reg->items[i].x1 = x;
163 reg->items[i].y1 = 0;
164 reg->items[i].x2 = x + reg->items[i].w - 1;
165 reg->items[i].y2 = reg->height - 1;
166 reg->items[i].h = reg->items[i].y2 - reg->items[i].y1 + 1;
167 reg->items[i].tx = reg->items[i].w / 2 - TextLength(&temprp, reg->items[i].text, reg->items[i].textlen) / 2;
168 reg->items[i].ty = reg->fontb + reg->items[i].h / 2 - reg->fonth / 2;
170 x += reg->items[i].w - reg->slopew;
173 reg->width = x + reg->slopew + REGISTER_SPACE_RIGHT;
176 /****************************************************************************************/
178 static void setregisterpos(struct Register *reg, WORD left, WORD top)
180 reg->left = left;
181 reg->top = top;
184 /****************************************************************************************/
186 static void renderregisteritem(struct RastPort *rp, struct Register *reg, WORD item)
188 struct RegisterItem *ri = &reg->items[item];
189 WORD x, y;
191 SetDrMd(rp, JAM1);
192 SetFont(rp, reg->dri->dri_Font);
194 x = reg->left + ri->x1;
195 y = reg->top + ri->y1;
198 SetAPen(rp, reg->dri->dri_Pens[(reg->active == item) ? TEXTPEN : BACKGROUNDPEN]);
199 Move(rp, x + ri->tx + 1, y + ri->ty);
200 Text(rp, ri->text, ri->textlen);
201 SetAPen(rp, reg->dri->dri_Pens[TEXTPEN]);
202 Move(rp, x + ri->tx, y + ri->ty);
203 Text(rp, ri->text, ri->textlen);
205 /* upper / at left side */
207 SetAPen(rp, reg->dri->dri_Pens[SHINEPEN]);
208 WritePixel(rp, x + reg->slopew, y + 2);
209 Move(rp, x + reg->slopew / 2, y + 3 + reg->slopew - 1);
210 Draw(rp, x + reg->slopew - 1, y + 3);
212 /* --- at top side */
214 RectFill(rp, x + reg->slopew + 1, y + 1, x + reg->slopew + 2, y + 1);
215 RectFill(rp, x + reg->slopew + 3, y, x + ri->w - 1 - reg->slopew - 3, y);
217 SetAPen(rp, reg->dri->dri_Pens[SHADOWPEN]);
218 RectFill(rp, x + ri->w - 1 - reg->slopew - 2, y + 1, x + ri->w - 1 - reg->slopew - 1, y + 1);
220 /* upper \ at right side */
222 WritePixel(rp, x + ri->w - 1 - reg->slopew, y + 2);
223 Move(rp, x + ri->w - 1 - reg->slopew + 1, y + 3);
224 Draw(rp, x + ri->w - 1 - reg->slopew / 2, y + 3 + reg->slopew - 1);
226 /* lower / at left side. */
228 if ((item == 0) || (reg->active == item))
230 SetAPen(rp, reg->dri->dri_Pens[SHINEPEN]);
232 else
234 SetAPen(rp, reg->dri->dri_Pens[BACKGROUNDPEN]);
236 Move(rp, x, y + ri->h - 2);
237 Draw(rp, x + reg->slopew / 2 - 1, y + ri->h - 2 - reg->slopew + 1);
239 /* lower \ at the lefst side from the previous item */
241 if (item > 0)
243 if (reg->active == item)
245 SetAPen(rp, reg->dri->dri_Pens[BACKGROUNDPEN]);
247 else
249 SetAPen(rp, reg->dri->dri_Pens[SHADOWPEN]);
251 Move(rp, x + reg->slopew / 2, y + ri->h - 2 - reg->slopew + 1);
252 Draw(rp, x + reg->slopew - 1, y + ri->h - 2);
255 /* lower \ at right side. */
257 if (reg->active == item + 1)
259 SetAPen(rp, reg->dri->dri_Pens[BACKGROUNDPEN]);
261 else
263 SetAPen(rp, reg->dri->dri_Pens[SHADOWPEN]);
265 Move(rp, x + ri->w - 1 - reg->slopew / 2 + 1, y + ri->h - 2 - reg->slopew + 1);
266 Draw(rp, x + ri->w - 1, y + ri->h - 2);
268 if (reg->active == item)
270 SetAPen(rp, reg->dri->dri_Pens[BACKGROUNDPEN]);
271 RectFill(rp, x, y + ri->h - 1, x + ri->w - 1, y + ri->h - 1);
273 else
275 WORD x1, x2;
277 x1 = x;
278 x2 = x + ri->w - 1;
280 if (reg->active == item - 1)
281 x1 += reg->slopew;
282 else if (reg->active == item + 1)
283 x2 -= reg->slopew;
285 SetAPen(rp, reg->dri->dri_Pens[SHINEPEN]);
286 RectFill(rp, x1, y + ri->h - 1, x2, y + ri->h - 1);
291 #if 0
292 /* \ at left side from previous item */
294 if (item != 0)
296 if (reg->active == item)
298 SetAPen(rp, reg->dri->dri_Pens[BACKGROUNDPEN]);
300 else
302 SetAPen(rp, reg->dri->dri_Pens[SHADOWPEN]);
305 Move(rp, x + reg->slopew / 2, y + ri->h - 2 - reg->slopew + 1);
306 Draw(rp, x + reg->slopew - 1, y + ri->h - 2);
308 #endif
312 /****************************************************************************************/
314 static void renderregister(struct RastPort *rp, struct Register *reg)
316 WORD i;
318 SetDrMd(rp, JAM1);
320 SetAPen(rp, reg->dri->dri_Pens[SHINEPEN]);
322 RectFill(rp, reg->left,
323 reg->top + reg->height - 1,
324 reg->left + REGISTER_SPACE_LEFT - 1,
325 reg->top + reg->height - 1);
327 RectFill(rp, reg->left + reg->width - REGISTER_SPACE_RIGHT,
328 reg->top + reg->height - 1,
329 reg->left + reg->width - 1,
330 reg->top + reg->height - 1);
332 for(i = 0; i < reg->numitems; i++)
334 renderregisteritem(rp, reg, i);
340 /****************************************************************************************/
342 static BOOL handleregisterinput(struct Register *reg, struct IntuiMessage *msg)
344 struct Window *win = msg->IDCMPWindow;
345 BOOL retval = FALSE;
347 if ((msg->Class == IDCMP_MOUSEBUTTONS) &&
348 (msg->Code == SELECTDOWN))
350 WORD i;
351 WORD x = win->MouseX - reg->left;
352 WORD y = win->MouseY - reg->top;
354 for(i = 0; i < reg->numitems; i++)
356 if ((x >= reg->items[i].x1) &&
357 (y >= reg->items[i].y1) &&
358 (x <= reg->items[i].x2) &&
359 (y <= reg->items[i].y2))
361 if (reg->active != i)
363 WORD oldactive = reg->active;
365 reg->active = i;
366 renderregisteritem(win->RPort, reg, oldactive);
367 renderregisteritem(win->RPort, reg, i);
369 break;
374 return retval;
379 /****************************************************************************************/
381 static void makewin(void)
383 win = OpenWindowTags(0, WA_PubScreen, (IPTR)scr,
384 WA_Title, (IPTR) "Register Tabs",
385 WA_InnerWidth, myregister.width + 8,
386 WA_InnerHeight, myregister.height + 8,
387 WA_CloseGadget, TRUE,
388 WA_DepthGadget, TRUE,
389 WA_Activate, TRUE,
390 WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_MOUSEBUTTONS,
391 TAG_DONE);
393 if (!win) cleanup("Can't open window!");
396 /****************************************************************************************/
398 static void handleall(void)
400 struct IntuiMessage *msg;
402 BOOL quitme = FALSE;
404 while(!quitme)
406 WaitPort(win->UserPort);
408 while((msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
410 if (!handleregisterinput(&myregister, msg))
412 switch(msg->Class)
414 case IDCMP_CLOSEWINDOW:
415 quitme = TRUE;
416 break;
419 ReplyMsg((struct Message *)msg);
421 } /* while((msg = (struct IntuiMessage *)GetMsg(win->UserPort))) */
423 } /* while(!quitme) */
426 /****************************************************************************************/
428 int main(void)
430 openlibs();
431 getvisual();
432 initregister(&myregister, myregisteritems);
433 layoutregister(&myregister, scr, dri, TRUE);
434 makewin();
435 setregisterpos(&myregister, win->BorderLeft + 4, win->BorderTop + 4);
436 renderregister(win->RPort, &myregister);
437 handleall();
438 cleanup(0);
439 return 0;
442 /****************************************************************************************/
443 /****************************************************************************************/
444 /****************************************************************************************/
445 /****************************************************************************************/
446 /****************************************************************************************/
447 /****************************************************************************************/