1 #include <intuition/intuition.h>
2 #include <intuition/gadgetclass.h>
3 #include <proto/exec.h>
4 #include <proto/intuition.h>
5 #include <proto/graphics.h>
6 #include <proto/cybergraphics.h>
8 /* Look in this file (compiler/coolimages/include/coolimages.h) to
9 see which images are available in coolimages lib. */
11 #include <linklibs/coolimages.h>
17 struct IntuitionBase
*IntuitionBase
;
18 struct GfxBase
*GfxBase
;
19 struct UtilityBase
*UtilityBase
;
20 struct Library
*CyberGfxBase
;
22 static struct Window
*win
;
23 static struct Screen
*scr
;
24 static struct DrawInfo
*dri
;
25 static struct Gadget
*gad
;
26 static WORD winwidth
, winheight
;
28 static void cleanup(char *msg
)
30 if (msg
) printf("coolbutton: %s\n", msg
);
34 /* It's always better to remove gadgets before closing window.
35 You *must* do this, if you want to kill (DisposeObject) a
36 gadget, when the window is supposed to stay open, or when
37 the window is killed after the gadget is killed. */
39 if (gad
) RemoveGadget(win
, gad
);
43 if (gad
) DisposeObject((Object
*)gad
);
45 CleanupCoolButtonClass();
47 if (dri
) FreeScreenDrawInfo(scr
, dri
);
48 if (scr
) UnlockPubScreen(NULL
, scr
);
50 if (CyberGfxBase
) CloseLibrary(CyberGfxBase
);
51 if (UtilityBase
) CloseLibrary((struct Library
*)UtilityBase
);
52 if (GfxBase
) CloseLibrary((struct Library
*)GfxBase
);
53 if (IntuitionBase
) CloseLibrary((struct Library
*)IntuitionBase
);
58 static void openlibs(void)
60 /* coolimages.lib needs this 3 libraries to be open, otherwise
61 InitCoolButtonClass() will fail and return FALSE */
63 IntuitionBase
= (struct IntuitionBase
*)OpenLibrary("intuition.library", 39);
64 GfxBase
= (struct GfxBase
*)OpenLibrary("graphics.library", 40);
65 UtilityBase
= (struct UtilityBase
*)OpenLibrary("utility.library", 39);
67 if (!IntuitionBase
|| !GfxBase
|| !UtilityBase
) cleanup("Error opening library!");
69 /* cybergraphics.library is optional, but at the moment coolimages.lib has not yet support
70 for rendering on indexed screens. So rendering will for now only work on hicolor
71 or truecolor screens. And this requires cybergraphics.library */
73 CyberGfxBase
= OpenLibrary("cybergraphics.library", 39);
76 static void getvisual(void)
78 scr
= LockPubScreen(NULL
);
79 if (!scr
) cleanup("Can't lock pub screen!");
81 dri
= GetScreenDrawInfo(scr
);
82 if (!dri
) cleanup("Can't get screen drawinfo!");
85 static void makegadget(void)
87 struct RastPort temprp
;
88 const struct CoolImage
*image
= &cool_switchimage
;
89 char *buttontext
= "Hello world";
92 /* Calc. the size of the button. The coolbuttonclass for now always
93 uses the screen font */
95 InitRastPort(&temprp
);
96 SetFont(&temprp
, dri
->dri_Font
); /* dri_Font is screen font */
100 w
= TextLength(&temprp
, buttontext
, strlen(buttontext
));
102 /* add some extra width */
107 h
= dri
->dri_Font
->tf_YSize
;
112 /* Font is higher than image */
118 /* Image is higher than font. Add some smaller extra height */
122 DeinitRastPort(&temprp
);
124 /* Calc. inner window size */
129 /* Calc. gadget pos */
131 x
= scr
->WBorLeft
+ 4;
132 y
= scr
->WBorTop
+ scr
->Font
->ta_YSize
+ 1 + 4; /* scr->Font is the TextAttr of Screen font. I could also use dri->dri_Font->tf_YSize */
136 GA_Immediate -> want IDCMP_GADGETDOWN msgs.
137 GA_RelVerify -> want IDCMP_GADGETUP msgs
140 gad
= (struct Gadget
*)NewObject(cool_buttonclass
, NULL
, GA_Left
, x
,
144 GA_Text
, (IPTR
)buttontext
,
145 GA_Immediate
, TRUE
,
146 GA_RelVerify
, TRUE
,
147 COOLBT_CoolImage
, (IPTR
)image
,
150 if (!gad
) cleanup("Can't create gadget!");
153 static void makewin(void)
155 /* Make window. Gadget is directly specified with WA_Gadgets. If
156 we wanted to add the gadget after the window is created:
158 AddGadget(win, gad, -1);
159 RefreshGList(gad, win, NULL, 1);
162 win
= OpenWindowTags(NULL
, WA_PubScreen
, (IPTR
)scr
,
163 WA_Title
, (IPTR
)"Cool Button" ,
164 WA_InnerWidth
, winwidth
,
165 WA_InnerHeight
, winheight
,
166 WA_CloseGadget
, TRUE
,
168 WA_DepthGadget
, TRUE
,
170 WA_IDCMP
, IDCMP_CLOSEWINDOW
|
173 WA_Gadgets
, (IPTR
)gad
,
176 if (!win
) cleanup("Error creating window!");
180 static void handleall(void)
182 struct IntuiMessage
*msg
;
187 WaitPort(win
->UserPort
);
189 /* If you have also gadtools gadgets in your window, you would
190 use GT_GetIMsg/GT_ReplyIMsg */
192 while ((msg
= (struct IntuiMessage
*)GetMsg(win
->UserPort
)))
196 case IDCMP_CLOSEWINDOW
:
200 case IDCMP_GADGETDOWN
:
201 printf("Button clicked\n");
205 printf("Button released\n");
209 ReplyMsg((struct Message
*)msg
);
219 if (!InitCoolButtonClass(CyberGfxBase
))
221 cleanup("Can't init cool button class!");