Reapplied r44288:
[AROS.git] / workbench / demos / coolbutton.c
blobdb2bc51cb5d4c4c60242e79fc73ac331aad86e2c
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>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.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);
32 if (win)
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);
40 CloseWindow(win);
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);
55 exit(0);
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";
90 WORD x, y, w, h, ih;
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 */
98 /* gadget width */
100 w = TextLength(&temprp, buttontext, strlen(buttontext));
101 w += image->width;
102 /* add some extra width */
103 w += 20;
105 /* gadget height */
107 h = dri->dri_Font->tf_YSize;
108 ih = image->height;
110 if (h > ih)
112 /* Font is higher than image */
114 h += 6;
116 else
118 /* Image is higher than font. Add some smaller extra height */
119 h = ih + 4;
122 /* Calc. inner window size */
124 winwidth = w + 8;
125 winheight = h + 8;
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 */
132 /* Create gadget.
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 ,
139 GA_Top , y ,
140 GA_Width , w ,
141 GA_Height , h ,
142 GA_Text , (IPTR)buttontext ,
143 GA_Immediate , TRUE ,
144 GA_RelVerify , TRUE ,
145 COOLBT_CoolImage, (IPTR)image ,
146 TAG_DONE);
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 ,
165 WA_DragBar , TRUE ,
166 WA_DepthGadget , TRUE ,
167 WA_Activate , TRUE ,
168 WA_IDCMP , IDCMP_CLOSEWINDOW |
169 IDCMP_GADGETUP |
170 IDCMP_GADGETDOWN ,
171 WA_Gadgets , (IPTR)gad ,
172 TAG_DONE);
174 if (!win) cleanup("Error creating window!");
178 static void handleall(void)
180 struct IntuiMessage *msg;
181 BOOL quitme = FALSE;
183 while (!quitme)
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)))
192 switch (msg->Class)
194 case IDCMP_CLOSEWINDOW:
195 quitme = TRUE;
196 break;
198 case IDCMP_GADGETDOWN:
199 printf("Button clicked\n");
200 break;
202 case IDCMP_GADGETUP:
203 printf("Button released\n");
204 break;
207 ReplyMsg((struct Message *)msg);
212 int main(void)
214 openlibs();
215 getvisual();
217 if (!InitCoolButtonClass(CyberGfxBase))
219 cleanup("Can't init cool button class!");
222 makegadget();
223 makewin();
224 handleall();
225 cleanup(NULL);
227 return 0;