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 /* Calc. inner window size */
127 /* Calc. gadget pos */
129 x
= scr
->WBorLeft
+ 4;
130 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 */
134 GA_Immediate -> want IDCMP_GADGETDOWN msgs.
135 GA_RelVerify -> want IDCMP_GADGETUP msgs
138 gad
= (struct Gadget
*)NewObject(cool_buttonclass
, NULL
, GA_Left
, x
,
142 GA_Text
, (IPTR
)buttontext
,
143 GA_Immediate
, TRUE
,
144 GA_RelVerify
, TRUE
,
145 COOLBT_CoolImage
, (IPTR
)image
,
148 if (!gad
) cleanup("Can't create gadget!");
151 static void makewin(void)
153 /* Make window. Gadget is directly specified with WA_Gadgets. If
154 we wanted to add the gadget after the window is created:
156 AddGadget(win, gad, -1);
157 RefreshGList(gad, win, NULL, 1);
160 win
= OpenWindowTags(NULL
, WA_PubScreen
, (IPTR
)scr
,
161 WA_Title
, (IPTR
)"Cool Button" ,
162 WA_InnerWidth
, winwidth
,
163 WA_InnerHeight
, winheight
,
164 WA_CloseGadget
, TRUE
,
166 WA_DepthGadget
, TRUE
,
168 WA_IDCMP
, IDCMP_CLOSEWINDOW
|
171 WA_Gadgets
, (IPTR
)gad
,
174 if (!win
) cleanup("Error creating window!");
178 static void handleall(void)
180 struct IntuiMessage
*msg
;
185 WaitPort(win
->UserPort
);
187 /* If you have also gadtools gadgets in your window, you would
188 use GT_GetIMsg/GT_ReplyIMsg */
190 while ((msg
= (struct IntuiMessage
*)GetMsg(win
->UserPort
)))
194 case IDCMP_CLOSEWINDOW
:
198 case IDCMP_GADGETDOWN
:
199 printf("Button clicked\n");
203 printf("Button released\n");
207 ReplyMsg((struct Message
*)msg
);
217 if (!InitCoolButtonClass(CyberGfxBase
))
219 cleanup("Can't init cool button class!");