Added DefaultWindowShapeFunc. Calls WDM_WINDOWSHAPE.
[tangerine.git] / workbench / demos / gtdemo.c
blob0c0301116149d62d0f117d131273a2aee12a2018
1 /*
2 (C) 1997-98 AROS - The Amiga Research OS
3 $Id$
5 Desc: Demo for gadtools.library
6 Lang: english
7 */
9 #include <aros/config.h>
11 #if !(AROS_FLAVOUR & AROS_FLAVOUR_NATIVE)
12 # define ENABLE_RT 1
13 #endif
15 #include <stdio.h>
16 #include <proto/exec.h>
17 #include <exec/libraries.h>
18 #include <exec/types.h>
19 #include <proto/dos.h>
20 #include <dos/dos.h>
21 #include <proto/intuition.h>
22 #include <intuition/classusr.h>
23 #include <intuition/gadgetclass.h>
24 #include <intuition/intuition.h>
25 #include <intuition/intuitionbase.h>
26 #include <intuition/screens.h>
27 #include <utility/tagitem.h>
28 #include <proto/gadtools.h>
29 #include <libraries/gadtools.h>
31 #include <proto/alib.h>
33 #include <aros/rt.h>
35 #define SDEBUG 1
36 #define DEBUG 1
37 #include <aros/debug.h>
38 struct IntuitionBase *IntuitionBase;
39 struct Library *GadToolsBase;
41 APTR vi;
42 struct Screen *scr;
43 struct Window *win;
44 struct Gadget *glist = NULL;
46 struct Gadget *button, *strgad, *integergadget;
47 WORD topoffset;
49 #define ID_BUTTON 1
50 #define ID_CHECKBOX 2
51 #define ID_MX 3
52 #define ID_CYCLE 4
53 #define ID_PALETTE 5
54 #define ID_SLIDER 6
55 #define ID_SCROLLER 7
56 #define ID_STRING 8
57 #define ID_INTEGER 9
58 #define ID_LISTVIEW 10
60 struct NewGadget buttongad =
62 210, 10, 100, 20,
63 "Exit (1)", NULL,
64 ID_BUTTON, PLACETEXT_IN, NULL, NULL
67 struct NewGadget checkbox =
69 320, 10, 20, 20,
70 "Disable (2)", NULL,
71 ID_CHECKBOX, PLACETEXT_RIGHT, NULL, NULL
74 struct NewGadget cyclegad =
76 210, 140, 100, 20,
77 "cyclegad (4)", NULL,
78 ID_CYCLE, PLACETEXT_ABOVE, NULL, NULL
81 struct NewGadget mxgad =
83 210, 60, MX_WIDTH, 20,
84 "Mutual Exclude (3)", NULL,
85 ID_MX, PLACETEXT_RIGHT, NULL, NULL
88 struct NewGadget palettegad =
90 210, 180, 120, 100,
91 "Palette (5)", NULL,
92 ID_PALETTE, PLACETEXT_ABOVE, NULL, NULL
95 struct NewGadget textgad =
97 380, 40, 120, 30,
98 NULL, NULL,
99 0, 0, NULL, NULL
102 struct NewGadget numbergad =
104 380, 80, 70, 20,
105 NULL, NULL,
106 0, 0, NULL, NULL
109 struct NewGadget slidergad =
111 380, 130, 120, 20,
112 "Slider (6)", NULL,
113 ID_SLIDER, PLACETEXT_ABOVE, NULL, NULL
116 struct NewGadget scrollergad =
118 380, 160, 20, 100,
119 NULL, NULL,
120 ID_SCROLLER, 0, NULL, NULL
123 struct NewGadget stringgad =
125 420, 180, 100, 20,
126 "String (8)", NULL,
127 ID_STRING, PLACETEXT_ABOVE, NULL, NULL
130 struct NewGadget integergad =
132 420, 240, 60, 20,
133 "Integer (9)", NULL,
134 ID_INTEGER, PLACETEXT_ABOVE, NULL, NULL
137 struct NewGadget listviewgad =
139 530, 30, 120, 100,
140 "Listview (10)", NULL,
141 ID_LISTVIEW, PLACETEXT_ABOVE, NULL, NULL
144 #define NUMLVNODES 20
145 struct Node lv_nodes[NUMLVNODES];
146 struct List lv_list;
147 STRPTR lv_texts[] = {"This", "is", "a", "demo", "of", "the", "GadTools", "listview.",
148 "Try", "scrolling", "and", "selecting", "entries",
149 "One", "Two", "Three", "Four", "Five", "Six", "Seven"};
152 STRPTR mxlabels[] =
154 "Label 1",
155 "Label 2",
156 "Label 3",
157 NULL
160 STRPTR cyclelabels[] =
162 "Label 1",
163 "Label 2",
164 "Label 3",
165 NULL
168 VOID initlvnodes(struct List *list, struct Node *nodes, STRPTR *texts, WORD numnodes)
170 WORD i;
171 NewList(list);
173 for (i = 0; i < numnodes; i ++)
175 AddTail(list, &(nodes[i]));
176 nodes[i].ln_Name = texts[i];
179 return;
182 BOOL openlibs()
184 IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37);
185 GadToolsBase = OpenLibrary("gadtools.library", 0);
186 if (!IntuitionBase) {
187 printf("GTDemo: Error opening intuition.library\n");
188 return FALSE;
190 if (!GadToolsBase) {
191 printf("GTDemo: Error opening gadtools.library\n");
192 return FALSE;
194 return TRUE;
197 void closelibs()
199 D(bug("Closelibs: closing gadtools\n"));
200 CloseLibrary(GadToolsBase);
201 D(bug("Closelibs: closing intuition\n"));
202 CloseLibrary((struct Library *) IntuitionBase);
203 D(bug("Closelibs: libs closed\n"));
207 struct Gadget *gt_init()
209 struct Gadget *gad = NULL;
210 struct DrawInfo *dri;
212 scr = LockPubScreen(NULL);
213 vi = GetVisualInfoA(scr, NULL);
214 if (vi != NULL)
215 gad = CreateContext(&glist);
217 if ((dri = GetScreenDrawInfo(scr)))
219 topoffset = dri->dri_Font->tf_YSize + scr->WBorTop - 10 + 5;
221 buttongad.ng_TopEdge += topoffset;
222 checkbox.ng_TopEdge += topoffset;
223 cyclegad.ng_TopEdge += topoffset;
224 mxgad.ng_TopEdge += topoffset;
225 palettegad.ng_TopEdge += topoffset;
226 textgad.ng_TopEdge += topoffset;
227 numbergad.ng_TopEdge += topoffset;
228 slidergad.ng_TopEdge += topoffset;
229 scrollergad.ng_TopEdge += topoffset;
230 stringgad.ng_TopEdge += topoffset;
231 integergad.ng_TopEdge += topoffset;
232 listviewgad.ng_TopEdge += topoffset;
234 FreeScreenDrawInfo(scr,dri);
237 return gad;
240 void gt_end()
242 D(bug("gtend: Freeing gadgets\n"));
243 FreeGadgets(glist);
244 D(bug("gtend: Freeing visualnfo\n"));
245 FreeVisualInfo(vi);
246 D(bug("gtend: Unlocking screen\n"));
247 UnlockPubScreen(NULL, scr);
251 BOOL openwin()
253 win = OpenWindowTags(NULL,
254 WA_PubScreen, (IPTR)scr,
255 WA_Left, 0,
256 WA_Top, 0,
257 WA_Width, 700,
258 WA_Height, 300 + topoffset,
259 WA_Title, (IPTR)"GTDemo",
260 WA_IDCMP,
261 BUTTONIDCMP |
262 CHECKBOXIDCMP |
263 CYCLEIDCMP |
264 MXIDCMP |
265 PALETTEIDCMP |
266 SLIDERIDCMP |
267 SCROLLERIDCMP |
268 ARROWIDCMP |
269 IDCMP_GADGETUP |
270 IDCMP_VANILLAKEY |
271 IDCMP_CLOSEWINDOW |
272 IDCMP_REFRESHWINDOW,
273 // WA_SimpleRefresh, TRUE,
274 WA_Gadgets, (IPTR)glist,
275 WA_DragBar, TRUE,
276 WA_CloseGadget, TRUE,
277 WA_DepthGadget, TRUE,
278 TAG_DONE);
279 if (!win) {
280 printf("GTDemo: Error opening window\n");
281 return FALSE;
283 return TRUE;
287 struct Gadget *makegadgets(struct Gadget *gad)
289 buttongad.ng_VisualInfo = vi;
290 checkbox.ng_VisualInfo = vi;
291 cyclegad.ng_VisualInfo = vi;
292 mxgad.ng_VisualInfo = vi;
293 palettegad.ng_VisualInfo = vi;
294 textgad.ng_VisualInfo = vi;
295 numbergad.ng_VisualInfo = vi;
296 slidergad.ng_VisualInfo = vi;
297 scrollergad.ng_VisualInfo = vi;
298 stringgad.ng_VisualInfo = vi;
299 integergad.ng_VisualInfo = vi;
300 listviewgad.ng_VisualInfo = vi;
302 gad = CreateGadget(BUTTON_KIND, gad, &buttongad,
303 GA_Immediate, TRUE,
304 TAG_DONE);
305 D(bug("Created button gadget: %p\n", gad));
306 button = gad;
307 gad = CreateGadget(CHECKBOX_KIND, gad, &checkbox,
308 GTCB_Checked, FALSE,
309 GTCB_Scaled, TRUE,
310 TAG_DONE);
311 D(bug("Created checkbox gadget: %p\n", gad));
312 gad = CreateGadget(CYCLE_KIND, gad, &cyclegad,
313 GTCY_Labels, (IPTR)&cyclelabels,
314 TAG_DONE);
315 D(bug("Created cycle gadget: %p\n", gad));
316 gad = CreateGadget(MX_KIND, gad, &mxgad,
317 GTMX_Labels, (IPTR)&mxlabels,
318 GTMX_Scaled, TRUE,
319 GTMX_TitlePlace, PLACETEXT_ABOVE,
320 TAG_DONE);
322 D(bug("Created mx gadget: %p\n", gad));
323 gad = CreateGadget(PALETTE_KIND, gad, &palettegad,
324 GTPA_NumColors, 6,
325 GTPA_IndicatorHeight, 30,
326 GTPA_Color, 0,
327 TAG_DONE);
329 D(bug("Created palette gadget: %p\n", gad));
330 gad = CreateGadget(TEXT_KIND, gad, &textgad,
331 GTTX_Text, (IPTR)"Text display",
332 GTTX_CopyText, TRUE,
333 GTTX_Border, TRUE,
334 GTTX_Justification, GTJ_CENTER,
335 TAG_DONE);
337 D(bug("Created text gadget: %p\n", gad));
338 gad = CreateGadget(NUMBER_KIND, gad, &numbergad,
339 GTNM_Number, 10,
340 GTNM_Border, TRUE,
341 GTNM_Justification, GTJ_CENTER,
342 TAG_DONE);
344 D(bug("Created number gadget: %p\n", gad));
345 gad = CreateGadget(SLIDER_KIND, gad, &slidergad,
346 GTSL_Min, 10,
347 GTSL_Max, 20,
348 GTSL_Level, 12,
349 GTSL_MaxLevelLen, 3,
350 GTSL_LevelFormat, (IPTR)"%2ld",
351 GTSL_LevelPlace, PLACETEXT_RIGHT,
352 GTSL_Justification, GTJ_RIGHT,
353 PGA_Freedom, LORIENT_HORIZ,
354 TAG_DONE);
357 D(bug("Created slider gadget: %p\n", gad));
358 gad = CreateGadget(SCROLLER_KIND, gad, &scrollergad,
359 GTSC_Top, 2,
360 GTSC_Total, 10,
361 GTSC_Visible, 2,
362 GTSC_Arrows, 10,
363 GA_RelVerify, TRUE,
364 PGA_Freedom, LORIENT_VERT,
365 TAG_DONE);
367 D(bug("Created scroller gadget: %p\n", gad));
368 gad = strgad = CreateGadget(STRING_KIND, gad, &stringgad,
369 GTST_String, (IPTR)"Blahblahblah",
370 GTST_MaxChars, 80,
371 GTSC_Visible, 2,
372 GA_Immediate, TRUE,
373 TAG_DONE);
375 D(bug("Created string gadget: %p\n", gad));
377 gad = integergadget = CreateGadget(INTEGER_KIND, gad, &integergad,
378 GTIN_Number, 100,
379 GTIN_MaxChars, 5,
380 STRINGA_Justification, GACT_STRINGCENTER,
381 GA_Immediate, TRUE,
382 TAG_DONE);
384 D(bug("Created integer gadget: %p\n", gad));
387 initlvnodes(&lv_list, lv_nodes, lv_texts, NUMLVNODES);
388 D(bug("Inited lv nodes\n"));
389 gad = CreateGadget(LISTVIEW_KIND, gad, &listviewgad,
390 GTLV_Labels, (IPTR)&lv_list,
391 GTLV_ReadOnly, FALSE,
392 TAG_DONE);
394 D(bug("Created listview gadget: %p\n", gad));
396 if (!gad) {
397 FreeGadgets(glist);
398 printf("GTDemo: Error creating gadgets\n");
400 return gad;
403 void draw_bevels(struct Window *win, APTR vi)
405 DrawBevelBox(win->RPort, 10, 10 + topoffset, 80, 80,
406 GT_VisualInfo, (IPTR) vi, TAG_DONE);
407 DrawBevelBox(win->RPort, 110, 10 + topoffset, 80, 80,
408 GTBB_Recessed, TRUE,
409 GT_VisualInfo, (IPTR) vi, TAG_DONE);
410 DrawBevelBox(win->RPort, 10, 110 + topoffset, 80, 80,
411 GTBB_FrameType, BBFT_RIDGE,
412 GT_VisualInfo, (IPTR) vi, TAG_DONE);
413 DrawBevelBox(win->RPort, 110, 110 + topoffset, 80, 80,
414 GTBB_FrameType, BBFT_RIDGE, GTBB_Recessed, TRUE,
415 GT_VisualInfo, (IPTR) vi, TAG_DONE);
416 DrawBevelBox(win->RPort, 10, 210 + topoffset, 80, 80,
417 GTBB_FrameType, BBFT_ICONDROPBOX,
418 GT_VisualInfo, (IPTR) vi, TAG_DONE);
419 DrawBevelBox(win->RPort, 110, 210 + topoffset, 80, 80,
420 GTBB_FrameType, BBFT_ICONDROPBOX, GTBB_Recessed, TRUE,
421 GT_VisualInfo, (IPTR) vi, TAG_DONE);
424 void handlewin()
426 BOOL ready = FALSE;
427 struct IntuiMessage *msg;
429 GT_SetGadgetAttrs(strgad,win,NULL,GTST_String, (IPTR)"Hello",
430 TAG_DONE);
432 GT_SetGadgetAttrs(integergadget,win,NULL,GTIN_Number,1000,TAG_DONE);
434 while (ready == FALSE) {
435 WaitPort(win->UserPort);
436 msg = GT_GetIMsg(win->UserPort);
437 if (msg != NULL) {
438 switch (msg->Class) {
439 case IDCMP_REFRESHWINDOW:
440 GT_BeginRefresh(win);
441 draw_bevels(win, vi);
442 GT_EndRefresh(win, TRUE);
443 D(bug("Got IDCMP_REFRESHWINDOW msg\n"));
444 break;
445 case IDCMP_VANILLAKEY:
446 if (msg->Code != 0x1B) /* if escape, quit */
447 break;
448 case IDCMP_CLOSEWINDOW:
449 ready = TRUE;
450 break;
451 case IDCMP_GADGETDOWN:
452 printf("Gadget %d pressed",
453 ((struct Gadget *) msg->IAddress)->GadgetID);
454 switch (((struct Gadget *) msg->IAddress)->GadgetID) {
455 case ID_MX:
456 printf(" (active: %d)", msg->Code);
457 break;
459 printf("\n");
460 break;
461 case IDCMP_MOUSEMOVE:
462 if (msg->IAddress)
464 switch (((struct Gadget *) msg->IAddress)->GadgetID) {
465 case ID_SLIDER:
466 printf("Slider moved to value %d\n", msg->Code);
467 break;
469 case ID_SCROLLER:
470 printf("Scroller moved to value %d\n", msg->Code);
471 break;
476 break;
478 case IDCMP_GADGETUP:
479 printf("Gadget %d released",
480 ((struct Gadget *) msg->IAddress)->GadgetID);
481 switch (((struct Gadget *) msg->IAddress)->GadgetID) {
482 case ID_BUTTON:
483 ready = TRUE;
484 break;
486 case ID_PALETTE:
487 printf(" (color: %d)", msg->Code);
488 break;
490 case ID_LISTVIEW:
491 printf(" (lv item: %d)", msg->Code);
492 break;
494 case ID_CYCLE:
495 printf(" (cycle item: %d)", msg->Code);
496 break;
498 case ID_CHECKBOX:{
499 BOOL checked;
501 checked = msg->Code;
502 if (checked)
503 printf(" (checked)");
504 else
505 printf(" (not checked)");
506 GT_SetGadgetAttrs(button, win, NULL,
507 GA_Disabled, (IPTR)checked, TAG_DONE);
508 break;
511 printf("\n");
512 break;
514 GT_ReplyIMsg(msg);
520 int main()
522 int error = RETURN_OK;
526 #if SDEBUG /* Debugging hack */
527 struct Task *idtask;
528 SDInit();
529 if ((idtask = FindTask("input.device")))
530 idtask->tc_UserData = NULL;
531 #endif
533 RT_Init();
535 if (openlibs() != FALSE) {
536 struct Gadget *gad;
538 gad = gt_init();
539 gad = makegadgets(gad);
540 if (gad != NULL) {
541 if (openwin() != FALSE) {
542 draw_bevels(win, vi);
543 handlewin();
544 CloseWindow(win);
545 } else
546 error = RETURN_FAIL;
547 } else
548 error = RETURN_FAIL;
550 D(bug("Doing gt_end()\n"));
551 gt_end();
552 } else
553 error = RETURN_FAIL;
555 D(bug("closing libs\n"));
556 closelibs();
558 RT_Exit();
560 return (error);