forgotten commit. disabled until egl is adapted.
[AROS-Contrib.git] / aminet / GadgetTest / GadgetTest.c
blobbfa3ee9c8d07f9902e3e4ba1177fa1c14052eff1
1 /*
2 * Source generated with GadToolsBox V1.4
3 * which is (c) Copyright 1991,92 Jaba Development
4 */
6 /* This is an example source which show you the initialization and handling of
7 almost every kind of the new gadget types under OS 2.xx.
9 Thanks to Jaba development for the GadToolsBox V 1.4 which is the GUI first
10 program if designing a new interface.
12 All gadgets and menuhandling will be performed by using the gadtools.library.
14 No lowlevel function calls to intuition.library will be needed.
16 This code is freely distributable and released as public domain.
18 You use this at yourown risk. No guaranty is made for the function and anything
19 else.
21 But I think, it should properly run. I took care about all enforcer hits and
22 had the commodores reference manual on my desk while I wrote this.
26 // INCLUDES
27 #include <exec/types.h>
28 #include <intuition/intuition.h>
29 #include <intuition/classes.h>
30 #include <intuition/classusr.h>
31 #include <intuition/imageclass.h>
32 #include <intuition/gadgetclass.h>
33 #include <libraries/gadtools.h>
34 #include <graphics/displayinfo.h>
35 #include <graphics/gfxbase.h>
36 #include <clib/exec_protos.h>
37 #include <clib/intuition_protos.h>
38 #include <clib/gadtools_protos.h>
39 #include <clib/graphics_protos.h>
40 #include <clib/utility_protos.h>
41 #include <string.h>
43 #include <aros/oldprograms.h>
44 #include <proto/gadtools.h>
45 #include <clib/alib_protos.h>
46 #include <exec/memory.h>
47 #include <stdio.h>
48 #include <stdlib.h>
50 // DEFINITIONS
52 #define VERSION "$VER: GadgetTest 0.01 just a simple example"
54 #define GD_Button_Gadget 0
55 #define GD_CheckBox_Gadget 1
56 #define GD_Integer_Gadget 2
57 #define GD_ListView_Gadget 3
58 #define GD_MX_Gadget 4
59 #define GD_Cycle_Gadget 5
60 #define GD_Palette_Gadget 6
61 #define GD_Scroller_Gadget 7
62 #define GD_Slider_Gadget 8
63 #define GD_String_Gadget 9
64 #define GD_Text_Gadget 10
66 #define Test__CNT 11
68 #define IB IntuitionBase
69 #define GTB Library
70 #define GFX GfxBase
71 // These defines saves a lot of typing time
73 // PROTOTYPES
75 static UWORD ComputeX(UWORD value);
77 // this function computes the width of gadgets and offsets dependent to the
78 // currently used wbench font.
80 static UWORD ComputeY(UWORD value);
82 // this function computes the height of gadgets and offsets dependent to the
83 // currently used wbench font.
85 static VOID ComputeFont(UWORD width,UWORD height);
87 // this function initializes some needed variables and structures for the
88 // font independentness
90 LONG SetupScreen(VOID);
92 // This function locks the wbench screen and initializes a visualinfo
93 // structure which is needed by creating gadgets, bevelboxes and menus
95 VOID CloseDownScreen(VOID);
97 // This function frees all allocated resources, closes all opened
98 // libraries.
100 VOID Test_Render(VOID);
102 // This function draws the bevelbox and call the refreshing routins for
103 // the window. This function will be called each time we got a refresh
104 // window idcmp message.
106 LONG OpenTest_Window(VOID);
108 // This function creates all gadgets and menus and opens the window.
109 // This functions must be called after the SetupScreen() function.
111 VOID CloseTest_Window(VOID);
113 // This function closes our window and frees all gadget and menu
114 // structures that had been allocated from the gadtools.library
116 int main(int argc,char **argv);
118 // The main function starts all actions. The program starts and
119 // exits from this function.
121 VOID HandleIDCMP(VOID);
123 // This function receives the idcmp messages for our window and
124 // does all other actions.
127 // STRUCTS & VARS
128 struct IB *IB = NULL;
129 struct GTB *GadToolsBase = NULL;
130 struct GFX *GFX = NULL;
131 struct Screen *Scr = NULL;
132 APTR VisualInfo = NULL;
133 struct Window *Test_Wnd = NULL;
134 struct Gadget *Test_GList = NULL;
135 struct Menu *Test_Menus = NULL;
136 struct Gadget *Test_Gadgets[11];
137 struct TextAttr *Font, Attr;
138 struct List LV_List;
139 struct Remember *RememberKey = NULL;
141 UWORD Test_Left = 135;
142 UWORD Test_Top = 11;
143 UWORD Test_Width = 569;
144 UWORD Test_Height = 159;
146 UBYTE *Test_Wdt = (UBYTE *)"Work Window";
148 UWORD FontX, FontY;
149 UWORD OffX, OffY;
152 UBYTE *MX_Gadget0Labels[] =
153 { // These are the labels for the MX gadget
154 (UBYTE *)"MX....: 1",
155 (UBYTE *)"2",
156 (UBYTE *)"3",
157 NULL
160 UBYTE *Cycle_Gadget0Labels[] =
161 { // These are the labels for the Cycle gadget
162 (UBYTE *)"FIRST",
163 (UBYTE *)"SECOND",
164 (UBYTE *)"THIRD",
165 NULL
168 UBYTE *ListView_Gadget0Labels[]=
169 { // these are the the node names for the Listview gadget
170 "First Node" ,
171 "Second Node" ,
172 "Third Node"
175 struct NewMenu Test_NewMenu[] =
176 { // this is the menuitem
177 { NM_TITLE, (STRPTR)"Project", NULL, 0, 0L, NULL },
178 { NM_ITEM, (STRPTR)"Quit", (STRPTR)"Q", 0, 0L, NULL },
179 { NM_END, NULL, NULL, 0, 0L, NULL }
182 UWORD Test_GTypes[] =
183 { // gadget types
184 BUTTON_KIND,
185 CHECKBOX_KIND,
186 INTEGER_KIND,
187 LISTVIEW_KIND,
188 MX_KIND,
189 CYCLE_KIND,
190 PALETTE_KIND,
191 SCROLLER_KIND,
192 SLIDER_KIND,
193 STRING_KIND,
194 TEXT_KIND
197 struct NewGadget Test_NGad[] =
198 { // structures for allocating gadgets
199 { 91, 73, 202, 13, (UBYTE *)"BUTTON", NULL, GD_Button_Gadget, PLACETEXT_IN ,NULL, NULL },
200 { 91, 108, 26, 11, (UBYTE *)"CHECKBOX:", NULL, GD_CheckBox_Gadget, PLACETEXT_LEFT ,NULL, NULL },
201 { 379, 125, 177, 13, (UBYTE *)"INTEGER.:", NULL, GD_Integer_Gadget, PLACETEXT_LEFT ,NULL, NULL },
202 { 91, 4, 202, 72, (UBYTE *)"LISTVIEW:", NULL, GD_ListView_Gadget, PLACETEXT_LEFT ,NULL, NULL },
203 { 273, 111, 17, 9, NULL, NULL, GD_MX_Gadget, PLACETEXT_LEFT ,NULL, NULL },
204 { 91, 91, 202, 13, (UBYTE *)"CYCLE...:", NULL, GD_Cycle_Gadget, PLACETEXT_LEFT ,NULL, NULL },
205 { 379, 4, 180, 65, (UBYTE *)"PALETTE.:", NULL, GD_Palette_Gadget, PLACETEXT_LEFT ,NULL, NULL },
206 { 379, 73, 178, 13, (UBYTE *)"SCROLLER:", NULL, GD_Scroller_Gadget, PLACETEXT_LEFT ,NULL, NULL },
207 { 379, 91, 177, 13, (UBYTE *)"SLIDER..:", NULL, GD_Slider_Gadget, PLACETEXT_LEFT ,NULL, NULL },
208 { 379, 108, 177, 13, (UBYTE *)"STRING..:", NULL, GD_String_Gadget, PLACETEXT_LEFT ,NULL, NULL },
209 { 93, 141, 459, 13, (UBYTE *)"TEXT....:", NULL, GD_Text_Gadget, PLACETEXT_LEFT ,NULL, NULL }
212 ULONG *Test_GTags[] =
213 { // the tags for the gadget creation
214 (ULONG *)(TAG_DONE),
216 (ULONG *)(GTCB_Checked),
217 (ULONG *)TRUE,
218 (ULONG *)(TAG_DONE),
220 (ULONG *)(GTIN_Number),
221 (ULONG *) 0,
222 (ULONG *)(GTIN_MaxChars),
223 (ULONG *)6,
224 (ULONG *)(TAG_DONE),
226 (ULONG *)(GTLV_ShowSelected),
227 (ULONG *)NULL,
228 (ULONG *)(GTLV_ScrollWidth),
229 (ULONG *)20,
230 (ULONG *)(LAYOUTA_Spacing),
231 (ULONG *)2,
232 (ULONG *)(TAG_DONE),
234 (ULONG *)(GTMX_Labels),
235 (ULONG *)&MX_Gadget0Labels[ 0 ],
236 (ULONG *)(TAG_DONE),
238 (ULONG *)(GTCY_Labels),
239 (ULONG *)&Cycle_Gadget0Labels[ 0 ],
240 (ULONG *)(TAG_DONE),
242 (ULONG *)(GTPA_Depth),
243 (ULONG *)2,
244 (ULONG *)(GTPA_IndicatorWidth),
245 (ULONG *)50,
246 (ULONG *)(TAG_DONE),
248 (ULONG *)(GTSC_Total),
249 (ULONG *)10,
250 (ULONG *)(GTSC_Arrows),
251 (ULONG *)20,
252 (ULONG *)(PGA_Freedom),
253 (ULONG *)LORIENT_HORIZ,
254 (ULONG *)(GA_RelVerify),
255 (ULONG *)TRUE,
256 (ULONG *)(TAG_DONE),
258 (ULONG *)(PGA_Freedom),
259 (ULONG *)LORIENT_HORIZ,
260 (ULONG *)(GA_RelVerify),
261 (ULONG *)TRUE,
262 (ULONG *)(TAG_DONE),
264 (ULONG *)(GTST_MaxChars),
265 (ULONG *)256,
266 (ULONG *)(TAG_DONE),
268 (ULONG *)(GTTX_Text),
269 (ULONG *)"Click a Gadget or use menu Quit to exit...",
270 (ULONG *)(GTTX_Border),
271 (ULONG *)TRUE,
272 (ULONG *)(TAG_DONE)
275 // FUNCTIONS
277 static UWORD ComputeX( UWORD value )
278 { // this function computes the x value dependent to your system font
279 return(( UWORD )(( FontX * value ) / 8 ));
282 static UWORD ComputeY( UWORD value )
283 { // this function computes the y value dependent to your system font
284 return(( UWORD )(( FontY * value ) / 8 ));
287 static void ComputeFont( UWORD width, UWORD height )
288 { // this function computes the width and height values dependent to your
289 // system font and initializes the Font and Attr vars.
291 Font = &Attr;
292 Font->ta_Name = (STRPTR)GfxBase->DefaultFont->tf_Message.mn_Node.ln_Name;
293 Font->ta_YSize = FontY = GfxBase->DefaultFont->tf_YSize;
294 FontX = GfxBase->DefaultFont->tf_XSize;
296 OffX = Scr->WBorLeft;
297 OffY = Scr->RastPort.TxHeight + Scr->WBorTop + 1;
299 if ( width && height )
301 if (( ComputeX( width ) + OffX + Scr->WBorRight ) > Scr->Width )
302 goto UseTopaz;
303 if (( ComputeY( height ) + OffY + Scr->WBorBottom ) > Scr->Height )
304 goto UseTopaz;
306 return;
308 UseTopaz:
310 Font->ta_Name = (STRPTR)"topaz.font";
311 FontX = FontY = Font->ta_YSize = 8;
314 LONG SetupScreen( VOID )
315 { // this alloctes a lock for the current default public screen
316 if ( ! ( Scr = LockPubScreen( NULL ))) return( 1L );
317 ComputeFont( 0, 0 );
318 if ( ! ( VisualInfo = GetVisualInfo( Scr, TAG_DONE ))) return( 2L );
319 return( 0L );
322 VOID CloseDownScreen( VOID )
323 { // this frees all allocated resources
324 if ( VisualInfo )
326 FreeVisualInfo( VisualInfo );
327 VisualInfo = NULL;
330 if (IB) CloseLibrary((struct Library *)IB);
331 if (GadToolsBase) CloseLibrary((struct Library *)GadToolsBase);
332 if (GFX) CloseLibrary((struct Library *)GFX);
335 void Test_Render( void )
336 { // this refreshes the window and the gadgets
337 ComputeFont( Test_Width, Test_Height );
339 DrawBevelBox( Test_Wnd->RPort, OffX + ComputeX( 3 ),
340 OffY + ComputeY( 2 ),
341 ComputeX( 563 ),
342 ComputeY( 155 ),
343 GT_VisualInfo, VisualInfo, TAG_DONE );
346 LONG OpenTest_Window( void )
347 { // this creates the gadgets and opens the window
348 struct NewGadget ng;
349 struct Gadget *g;
350 struct Node *nd;
351 UWORD lc, tc;
352 UWORD wleft = Test_Left, wtop = Test_Top, ww, wh;
354 ComputeFont( Test_Width, Test_Height );
356 ww = ComputeX( Test_Width );
357 wh = ComputeY( Test_Height );
359 if (( wleft + ww + OffX + Scr->WBorRight ) > Scr->Width ) wleft = Scr->Width - ww;
360 if (( wtop + wh + OffY + Scr->WBorBottom ) > Scr->Height ) wtop = Scr->Height - wh;
362 NewList(&LV_List); // ask Exec to clear out our list
364 if (!(nd=(struct Node *)AllocRemember(&RememberKey,sizeof(struct Node),MEMF_CLEAR|MEMF_PUBLIC)))
365 return(100);
367 nd->ln_Name = ListView_Gadget0Labels[0];
368 nd->ln_Type = NT_UNKNOWN;
370 AddTail(&LV_List,nd);
372 if (!(nd=(struct Node *)AllocRemember(&RememberKey,sizeof(struct Node),MEMF_CLEAR|MEMF_PUBLIC)))
373 return(101);
375 nd->ln_Name = ListView_Gadget0Labels[1];
376 nd->ln_Type = NT_UNKNOWN;
378 AddTail(&LV_List,nd);
380 if (!(nd=(struct Node *)AllocRemember(&RememberKey,sizeof(struct Node),MEMF_CLEAR|MEMF_PUBLIC)))
381 return(102);
383 nd->ln_Name = ListView_Gadget0Labels[2];
384 nd->ln_Type = NT_UNKNOWN;
386 AddTail(&LV_List,nd);
389 if ( ! ( g = CreateContext( &Test_GList ))) return( 20L );
392 for( lc = 0, tc = 0; lc < Test__CNT; lc++ )
395 CopyMem((char * )&Test_NGad[ lc ], (char * )&ng, (long)sizeof( struct NewGadget ));
397 ng.ng_VisualInfo = VisualInfo;
398 ng.ng_TextAttr = Font;
399 ng.ng_LeftEdge = OffX + ComputeX( ng.ng_LeftEdge );
400 ng.ng_TopEdge = OffY + ComputeY( ng.ng_TopEdge );
401 ng.ng_Width = ComputeX( ng.ng_Width );
402 ng.ng_Height = ComputeY( ng.ng_Height);
404 printf("%s - %d\n",__FUNCTION__,__LINE__);
406 Test_Gadgets[ lc ] = g = CreateGadgetA((ULONG)Test_GTypes[ lc ], g, &ng, ( struct TagItem * )&Test_GTags[ tc ] );
408 while( Test_GTags[ tc ] ) tc += 2;
409 tc++;
411 if ( NOT g ) return( Test_Gadgets[tc]->GadgetID + 1 );
413 printf("%s - %d\n",__FUNCTION__,__LINE__);
415 if ( ! ( Test_Menus = CreateMenus( Test_NewMenu, GTMN_FrontPen, 0L, TAG_DONE )))
416 return( 30L );
418 LayoutMenus( Test_Menus, VisualInfo, TAG_DONE );
420 if ( ! ( Test_Wnd = OpenWindowTags( NULL,
421 WA_Left, wleft,
422 WA_Top, wtop,
423 WA_Width, ww + OffX + Scr->WBorRight,
424 WA_Height, wh + OffY + Scr->WBorBottom,
426 WA_IDCMP,
427 BUTTONIDCMP| CHECKBOXIDCMP| INTEGERIDCMP|
428 LISTVIEWIDCMP| MXIDCMP| CYCLEIDCMP|
429 PALETTEIDCMP| SCROLLERIDCMP| ARROWIDCMP|
430 SLIDERIDCMP| STRINGIDCMP| IDCMP_MENUPICK|
431 IDCMP_CLOSEWINDOW| IDCMP_REFRESHWINDOW,
433 WA_Flags,
434 WFLG_DRAGBAR| WFLG_DEPTHGADGET| WFLG_CLOSEGADGET|
435 WFLG_SMART_REFRESH,
437 WA_Gadgets, Test_GList,
438 WA_Title, Test_Wdt,
439 WA_ScreenTitle, "GadToolsBox v1.4 © 1991,92 ",
440 WA_PubScreen, Scr,
441 TAG_DONE )))
442 return( 40L );
444 SetMenuStrip( Test_Wnd, Test_Menus );
446 GT_SetGadgetAttrs(Test_Gadgets[GD_ListView_Gadget],Test_Wnd,NULL,
447 GTLV_Labels,&LV_List,
448 TAG_DONE);
450 GT_RefreshWindow( Test_Wnd, NULL );
451 Test_Render();
453 UnlockPubScreen(NULL,Scr);
454 // The window is opened so we doesn`t need the lock anymore
456 return( 0L );
459 void CloseTest_Window( void )
460 { // this closes the test window and frees some structures
461 struct Node *nd;
463 if ( Test_Menus )
465 ClearMenuStrip( Test_Wnd );
466 FreeMenus( Test_Menus );
467 Test_Menus = NULL;
470 if ( Test_Wnd )
472 CloseWindow( Test_Wnd );
473 Test_Wnd = NULL;
476 if ( Test_GList )
478 FreeGadgets( Test_GList );
479 Test_GList = NULL;
482 Forbid();
483 while ((nd=RemHead(&LV_List)));
484 Permit();
485 FreeRemember(&RememberKey,TRUE);
488 int main(int argc,char **argv)
491 LONG Error=0L; // Error indicator
492 __unused BPTR wbout=0L;
494 if (!(IB=(struct IB *)OpenLibrary("intuition.library",37L)))
495 { // first checks if we `re running on an OS 2.xx machine
497 Write(Output(),"Oops you need OS 2.xx to run this program\n",42);
498 exit(0);
502 if (argc > 1)
503 { // we got any more argument
504 PutStr(VERSION);
505 PutStr("\nThis is just a simple demo program which should show you\n"
506 "how you can create and handle the OS 2.xx gadgets and takes\n"
507 "other arguments than its name\n");
508 CloseDownScreen();
509 exit(0);
512 // now open all other libraries and check the success
514 if (!(GadToolsBase=(struct GTB *)OpenLibrary("gadtools.library",37L)))
516 PutStr ("Opening gadtools.library failed\n");
517 CloseDownScreen();
518 exit(20);
521 if (!(GFX=(struct GFX *)OpenLibrary("graphics.library",37L)))
523 PutStr ("Opening graphics.library failed\n");
524 CloseDownScreen();
525 exit(15);
528 // initialize out screen lock and visual info
529 Error = SetupScreen();
531 switch (Error)
533 case 1:
534 // we got no lock for the wbench screen
535 PutStr("No lock !\n");
536 if (Scr) UnlockPubScreen( NULL, Scr );
537 CloseDownScreen();
538 break;
539 case 2:
540 // we got no visual info structure
541 PutStr("No VisualInfo !\n");
542 if (Scr) UnlockPubScreen( NULL, Scr );
543 CloseDownScreen();
544 break;
545 default:
546 // no error !
547 break;
550 Error = OpenTest_Window();
552 switch (Error)
554 case 0:
555 // no error !
556 break;
557 case 20:
558 // CreateContext() failed
559 PutStr("Could not create context !\n");
560 CloseTest_Window();
561 if (Scr) UnlockPubScreen( NULL, Scr );
562 CloseDownScreen();
563 exit(5);
564 break;
565 case 30:
566 // no menu
567 PutStr("Could not create menu !\n");
568 CloseTest_Window();
569 if (Scr) UnlockPubScreen( NULL, Scr );
570 CloseDownScreen();
571 exit(5);
572 break;
573 case 40:
574 // no window
575 PutStr("Could not open window !\n");
576 CloseTest_Window();
577 if (Scr) UnlockPubScreen( NULL, Scr );
578 CloseDownScreen();
579 exit(5);
580 break;
581 default:
582 // we got any gadget id
583 Printf("Error: %ld gadget could not be created\n",Error);
584 CloseTest_Window();
585 if (Scr) UnlockPubScreen( NULL, Scr );
586 CloseDownScreen();
587 exit(5);
588 break;
591 HandleIDCMP();
592 CloseTest_Window();
593 CloseDownScreen();
594 exit(0);
597 VOID HandleIDCMP(VOID)
598 { // this handles all inputs
599 struct IntuiMessage *Msg; // the received Message
600 LONG Class,Code,SelGadgetID = 0; // vars to copy Message`s contents to
601 struct Gadget *SelGadget = 0; // the selected gadget
602 BOOL ENDTEST=FALSE; // end indicator
603 BOOL CHECKGADGET=TRUE; // whether the checkbox is checked or not
604 char *res=NULL;
606 // main loop
608 while (!(ENDTEST))
610 WaitPort(Test_Wnd->UserPort);
611 while ((Msg=GT_GetIMsg(Test_Wnd->UserPort)))
612 { // as long as we got any message
613 // copy all informations and then reply the message
615 Class = Msg->Class;
616 Code = Msg->Code;
617 if ((Class == IDCMP_GADGETUP) || (Class == IDCMP_GADGETDOWN))
618 { // enforcer is watching you !
620 if ((SelGadget = (struct Gadget *)Msg->IAddress))
621 SelGadgetID = SelGadget->GadgetID;
623 GT_ReplyIMsg(Msg);
625 // reaction
627 switch (Class)
629 case IDCMP_MENUPICK:
630 ENDTEST=TRUE;
631 break;
632 case IDCMP_GADGETUP:
633 case IDCMP_GADGETDOWN:
634 res=NULL;
635 switch (SelGadgetID)
638 case GD_Button_Gadget :
639 PutStr("You clicked the button gadget\n");
640 break;
641 case GD_CheckBox_Gadget:
642 if (CHECKGADGET) CHECKGADGET=FALSE;
643 else CHECKGADGET=TRUE;
644 Printf("You clicked the CheckBox gadget state: %s\n",
645 CHECKGADGET ? "Checked" : "NOT CHECKED");
646 break;
647 case GD_Integer_Gadget :
648 res=((struct StringInfo *)SelGadget->SpecialInfo)->Buffer;
649 if (res) Printf("You insert >%s< in the Integer gadget\n",res);
650 break;
651 case GD_ListView_Gadget:
652 Printf("You clicked the ListView gadget entry: %s\n",ListView_Gadget0Labels[Code]);
653 break;
654 case GD_MX_Gadget :
655 Printf("You clicked the MX gadget active: %s\n",MX_Gadget0Labels[Code]);
656 break;
657 case GD_Cycle_Gadget :
658 Printf("You clicked the Cycle gadget selected: %s\n",Cycle_Gadget0Labels[Code]);
659 break;
660 case GD_Palette_Gadget :
661 Printf("You clicked the Palette gadget colour: %ld\n",Code);
662 break;
663 case GD_Scroller_Gadget:
664 Printf("You clicked the Scroller gadget level: %ld\n",Code);
665 break;
666 case GD_Slider_Gadget :
667 Printf("You clicked the Slider gadget state: %ld\n",Code);
668 break;
669 case GD_String_Gadget :
670 res=((struct StringInfo *)SelGadget->SpecialInfo)->Buffer;
671 if (res) Printf("You insert >%s< in the String gadget\n",res);
672 break;
673 default:
674 // never see this
675 break;
677 // a gadget message
678 break;
679 case IDCMP_CLOSEWINDOW:
680 // user ask us to quit
681 ENDTEST=TRUE;
682 break;
683 case IDCMP_REFRESHWINDOW:
684 // our window needs a refresh
685 Test_Render();
686 break;
687 default:
688 // none of them about
689 break;
694 Forbid();
696 // first clear out all messages
697 while ((Msg=GT_GetIMsg(Test_Wnd->UserPort))) GT_ReplyIMsg(Msg);
699 // no more messages will be received
700 ModifyIDCMP(Test_Wnd,0L);
702 Permit();