2 * Source generated with GadToolsBox V1.4
3 * which is (c) Copyright 1991,92 Jaba Development
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
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.
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>
43 #include <aros/oldprograms.h>
44 #include <proto/gadtools.h>
45 #include <clib/alib_protos.h>
46 #include <exec/memory.h>
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
68 #define IB IntuitionBase
71 // These defines saves a lot of typing time
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
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.
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
;
139 struct Remember
*RememberKey
= NULL
;
141 UWORD Test_Left
= 135;
143 UWORD Test_Width
= 569;
144 UWORD Test_Height
= 159;
146 UBYTE
*Test_Wdt
= (UBYTE
*)"Work Window";
152 UBYTE
*MX_Gadget0Labels
[] =
153 { // These are the labels for the MX gadget
154 (UBYTE
*)"MX....: 1",
160 UBYTE
*Cycle_Gadget0Labels
[] =
161 { // These are the labels for the Cycle gadget
168 UBYTE
*ListView_Gadget0Labels
[]=
169 { // these are the the node names for the Listview gadget
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
[] =
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
216 (ULONG
*)(GTCB_Checked
),
220 (ULONG
*)(GTIN_Number
),
222 (ULONG
*)(GTIN_MaxChars
),
226 (ULONG
*)(GTLV_ShowSelected
),
228 (ULONG
*)(GTLV_ScrollWidth
),
230 (ULONG
*)(LAYOUTA_Spacing
),
234 (ULONG
*)(GTMX_Labels
),
235 (ULONG
*)&MX_Gadget0Labels
[ 0 ],
238 (ULONG
*)(GTCY_Labels
),
239 (ULONG
*)&Cycle_Gadget0Labels
[ 0 ],
242 (ULONG
*)(GTPA_Depth
),
244 (ULONG
*)(GTPA_IndicatorWidth
),
248 (ULONG
*)(GTSC_Total
),
250 (ULONG
*)(GTSC_Arrows
),
252 (ULONG
*)(PGA_Freedom
),
253 (ULONG
*)LORIENT_HORIZ
,
254 (ULONG
*)(GA_RelVerify
),
258 (ULONG
*)(PGA_Freedom
),
259 (ULONG
*)LORIENT_HORIZ
,
260 (ULONG
*)(GA_RelVerify
),
264 (ULONG
*)(GTST_MaxChars
),
268 (ULONG
*)(GTTX_Text
),
269 (ULONG
*)"Click a Gadget or use menu Quit to exit...",
270 (ULONG
*)(GTTX_Border
),
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.
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
)
303 if (( ComputeY( height
) + OffY
+ Scr
->WBorBottom
) > Scr
->Height
)
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 );
318 if ( ! ( VisualInfo
= GetVisualInfo( Scr
, TAG_DONE
))) return( 2L );
322 VOID
CloseDownScreen( VOID
)
323 { // this frees all allocated resources
326 FreeVisualInfo( VisualInfo
);
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 ),
343 GT_VisualInfo
, VisualInfo
, TAG_DONE
);
346 LONG
OpenTest_Window( void )
347 { // this creates the gadgets and opens the window
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
)))
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
)))
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
)))
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;
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
)))
418 LayoutMenus( Test_Menus
, VisualInfo
, TAG_DONE
);
420 if ( ! ( Test_Wnd
= OpenWindowTags( NULL
,
423 WA_Width
, ww
+ OffX
+ Scr
->WBorRight
,
424 WA_Height
, wh
+ OffY
+ Scr
->WBorBottom
,
427 BUTTONIDCMP
| CHECKBOXIDCMP
| INTEGERIDCMP
|
428 LISTVIEWIDCMP
| MXIDCMP
| CYCLEIDCMP
|
429 PALETTEIDCMP
| SCROLLERIDCMP
| ARROWIDCMP
|
430 SLIDERIDCMP
| STRINGIDCMP
| IDCMP_MENUPICK
|
431 IDCMP_CLOSEWINDOW
| IDCMP_REFRESHWINDOW
,
434 WFLG_DRAGBAR
| WFLG_DEPTHGADGET
| WFLG_CLOSEGADGET
|
437 WA_Gadgets
, Test_GList
,
439 WA_ScreenTitle
, "GadToolsBox v1.4 © 1991,92 ",
444 SetMenuStrip( Test_Wnd
, Test_Menus
);
446 GT_SetGadgetAttrs(Test_Gadgets
[GD_ListView_Gadget
],Test_Wnd
,NULL
,
447 GTLV_Labels
,&LV_List
,
450 GT_RefreshWindow( Test_Wnd
, NULL
);
453 UnlockPubScreen(NULL
,Scr
);
454 // The window is opened so we doesn`t need the lock anymore
459 void CloseTest_Window( void )
460 { // this closes the test window and frees some structures
465 ClearMenuStrip( Test_Wnd
);
466 FreeMenus( Test_Menus
);
472 CloseWindow( Test_Wnd
);
478 FreeGadgets( Test_GList
);
483 while ((nd
=RemHead(&LV_List
)));
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);
503 { // we got any more argument
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");
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");
521 if (!(GFX
=(struct GFX
*)OpenLibrary("graphics.library",37L)))
523 PutStr ("Opening graphics.library failed\n");
528 // initialize out screen lock and visual info
529 Error
= SetupScreen();
534 // we got no lock for the wbench screen
535 PutStr("No lock !\n");
536 if (Scr
) UnlockPubScreen( NULL
, Scr
);
540 // we got no visual info structure
541 PutStr("No VisualInfo !\n");
542 if (Scr
) UnlockPubScreen( NULL
, Scr
);
550 Error
= OpenTest_Window();
558 // CreateContext() failed
559 PutStr("Could not create context !\n");
561 if (Scr
) UnlockPubScreen( NULL
, Scr
);
567 PutStr("Could not create menu !\n");
569 if (Scr
) UnlockPubScreen( NULL
, Scr
);
575 PutStr("Could not open window !\n");
577 if (Scr
) UnlockPubScreen( NULL
, Scr
);
582 // we got any gadget id
583 Printf("Error: %ld gadget could not be created\n",Error
);
585 if (Scr
) UnlockPubScreen( NULL
, Scr
);
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
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
617 if ((Class
== IDCMP_GADGETUP
) || (Class
== IDCMP_GADGETDOWN
))
618 { // enforcer is watching you !
620 if ((SelGadget
= (struct Gadget
*)Msg
->IAddress
))
621 SelGadgetID
= SelGadget
->GadgetID
;
633 case IDCMP_GADGETDOWN
:
638 case GD_Button_Gadget
:
639 PutStr("You clicked the button gadget\n");
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");
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
);
651 case GD_ListView_Gadget
:
652 Printf("You clicked the ListView gadget entry: %s\n",ListView_Gadget0Labels
[Code
]);
655 Printf("You clicked the MX gadget active: %s\n",MX_Gadget0Labels
[Code
]);
657 case GD_Cycle_Gadget
:
658 Printf("You clicked the Cycle gadget selected: %s\n",Cycle_Gadget0Labels
[Code
]);
660 case GD_Palette_Gadget
:
661 Printf("You clicked the Palette gadget colour: %ld\n",Code
);
663 case GD_Scroller_Gadget
:
664 Printf("You clicked the Scroller gadget level: %ld\n",Code
);
666 case GD_Slider_Gadget
:
667 Printf("You clicked the Slider gadget state: %ld\n",Code
);
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
);
679 case IDCMP_CLOSEWINDOW
:
680 // user ask us to quit
683 case IDCMP_REFRESHWINDOW
:
684 // our window needs a refresh
688 // none of them about
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);