Reworked the rule_copy_multi, rule_ref_multi, rule_compile_multi to avoid
[AROS.git] / workbench / demos / coolbutton.c
blobfd89cd0fbaabd170815a057bcdb2c4b6b3ac764f
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 DeinitRastPort(&temprp);
124 /* Calc. inner window size */
126 winwidth = w + 8;
127 winheight = h + 8;
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 */
134 /* Create gadget.
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 ,
141 GA_Top , y ,
142 GA_Width , w ,
143 GA_Height , h ,
144 GA_Text , (IPTR)buttontext ,
145 GA_Immediate , TRUE ,
146 GA_RelVerify , TRUE ,
147 COOLBT_CoolImage, (IPTR)image ,
148 TAG_DONE);
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 ,
167 WA_DragBar , TRUE ,
168 WA_DepthGadget , TRUE ,
169 WA_Activate , TRUE ,
170 WA_IDCMP , IDCMP_CLOSEWINDOW |
171 IDCMP_GADGETUP |
172 IDCMP_GADGETDOWN ,
173 WA_Gadgets , (IPTR)gad ,
174 TAG_DONE);
176 if (!win) cleanup("Error creating window!");
180 static void handleall(void)
182 struct IntuiMessage *msg;
183 BOOL quitme = FALSE;
185 while (!quitme)
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)))
194 switch (msg->Class)
196 case IDCMP_CLOSEWINDOW:
197 quitme = TRUE;
198 break;
200 case IDCMP_GADGETDOWN:
201 printf("Button clicked\n");
202 break;
204 case IDCMP_GADGETUP:
205 printf("Button released\n");
206 break;
209 ReplyMsg((struct Message *)msg);
214 int main(void)
216 openlibs();
217 getvisual();
219 if (!InitCoolButtonClass(CyberGfxBase))
221 cleanup("Can't init cool button class!");
224 makegadget();
225 makewin();
226 handleall();
227 cleanup(NULL);
229 return 0;