Don't call ReadArgs() if started from WB.
[tangerine.git] / compiler / coolimages / buttonclass.c
blob6163fe1ab6a63f881d7fc3adbb1adcbeb025f0e6
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: english
7 */
9 /****************************************************************************************/
11 #define USE_BOOPSI_STUBS
13 #include <exec/execbase.h>
14 #include <exec/memory.h>
15 #include <intuition/intuition.h>
16 #include <intuition/classes.h>
17 #include <intuition/classusr.h>
18 #include <intuition/imageclass.h>
19 #include <intuition/gadgetclass.h>
20 #include <intuition/cghooks.h>
21 #include <graphics/gfx.h>
22 #include <cybergraphx/cybergraphics.h>
23 #include <proto/exec.h>
24 #include <proto/intuition.h>
25 #include <proto/graphics.h>
26 #include <proto/cybergraphics.h>
27 #include <proto/utility.h>
28 #include <aros/asmcall.h>
29 #include <clib/boopsistubs.h>
30 #include <string.h>
32 #include "coolimages.h"
34 /****************************************************************************************/
36 struct CoolButtonData
38 struct CoolImage *image;
39 ULONG *pal;
40 Object *frame;
41 ULONG bgcol;
42 ULONG selcol;
45 /****************************************************************************************/
47 extern struct IntuitionBase *IntuitionBase;
48 extern struct GfxBase *GfxBase;
49 extern struct UtilityBase *UtilityBase;
51 struct IClass *cool_buttonclass;
53 /****************************************************************************************/
55 #define CyberGfxBase cool_cybergfxbase
56 #define G(x) ((struct Gadget *)(x))
58 #define BORDERIMAGESPACINGX 4
60 /****************************************************************************************/
62 static struct Library *cool_cybergfxbase;
64 /****************************************************************************************/
66 static void getgadgetcoords(struct Gadget *gad, struct GadgetInfo *gi,
67 WORD *x, WORD *y, WORD *w, WORD *h)
69 *x = gad->LeftEdge + ((gad->Flags & GFLG_RELRIGHT) ? gi->gi_Domain.Width - 1 : 0);
70 *y = gad->TopEdge + ((gad->Flags & GFLG_RELBOTTOM) ? gi->gi_Domain.Height - 1 : 0);
71 *w = gad->Width + ((gad->Flags & GFLG_RELWIDTH) ? gi->gi_Domain.Width : 0);
72 *h = gad->Height + ((gad->Flags & GFLG_RELHEIGHT) ? gi->gi_Domain.Height : 0);
75 /****************************************************************************************/
77 static IPTR coolbutton_new(Class * cl, Object * o, struct opSet * msg)
79 AROS_GET_SYSBASE_OK
80 struct CoolButtonData *data;
81 struct TagItem fitags[] =
83 { IA_FrameType , FRAME_BUTTON },
84 { IA_EdgesOnly , FALSE },
85 { TAG_DONE }
88 o = (Object *)DoSuperMethodA(cl, o, (Msg)msg);
90 if (o)
92 data = INST_DATA(cl, o);
94 data->image = (struct CoolImage *)GetTagData(COOLBT_CoolImage, 0, msg->ops_AttrList);
95 data->frame = NewObjectA(NULL, FRAMEICLASS, fitags);
97 if (!data->frame)
99 CoerceMethod(cl, o, OM_DISPOSE);
100 o = NULL;
101 } else {
102 if (CyberGfxBase && data->image)
104 if ((data->pal = AllocVec(data->image->numcolors * sizeof(ULONG), MEMF_PUBLIC)))
106 ULONG *p = data->pal;
107 WORD i;
109 for(i = 0; i < data->image->numcolors; i++)
111 *p++ = (data->image->pal[i * 3] << 16) |
112 (data->image->pal[i * 3 + 1] << 8) |
113 (data->image->pal[i * 3 + 2]);
116 } else {
117 data->image = NULL;
119 } else {
120 data->image = NULL;
124 } /* if (o) */
126 return (IPTR)o;
129 /****************************************************************************************/
131 static IPTR coolbutton_dispose(Class * cl, Object * o, Msg msg)
133 AROS_GET_SYSBASE_OK
134 struct CoolButtonData *data;
136 data = INST_DATA(cl, o);
138 if (data->frame) DisposeObject(data->frame);
139 if (data->pal) FreeVec(data->pal);
141 return DoSuperMethodA(cl, o, msg);
144 /****************************************************************************************/
146 static IPTR coolbutton_hittest(Class *cl, Object *o, struct gpHitTest *msg)
148 WORD gadx, gady, gadw, gadh;
150 getgadgetcoords(G(o), msg->gpht_GInfo, &gadx, &gady, &gadw, &gadh);
152 return ((msg->gpht_Mouse.X >= 0) &&
153 (msg->gpht_Mouse.Y >= 0) &&
154 (msg->gpht_Mouse.X < gadw) &&
155 (msg->gpht_Mouse.Y < gadh)) ? GMR_GADGETHIT : 0;
158 /****************************************************************************************/
160 static IPTR coolbutton_render(Class *cl, Object *o, struct gpRender *msg)
162 struct CoolButtonData *data;
163 STRPTR text = (STRPTR)G(o)->GadgetText;
164 struct TagItem im_tags[] =
166 {IA_Width , 0 },
167 {IA_Height , 0 },
168 {TAG_DONE }
170 WORD x, y, w, h;
172 getgadgetcoords(G(o), msg->gpr_GInfo, &x, &y, &w, &h);
174 data = INST_DATA(cl, o);
176 im_tags[0].ti_Data = w;
177 im_tags[1].ti_Data = h;
179 SetAttrsA(data->frame, im_tags);
181 DrawImageState(msg->gpr_RPort,
182 (struct Image *)data->frame,
185 (G(o)->Flags & GFLG_SELECTED) ? IDS_SELECTED : IDS_NORMAL,
186 msg->gpr_GInfo->gi_DrInfo);
188 if (GetBitMapAttr(msg->gpr_RPort->BitMap, BMA_DEPTH) < 15)
190 data->image = NULL;
193 if (text)
195 WORD len = strlen(text);
196 WORD tx = x, ty = y;
198 SetFont(msg->gpr_RPort, msg->gpr_GInfo->gi_DrInfo->dri_Font);
200 if (data->image)
202 tx += BORDERIMAGESPACINGX + data->image->width + BORDERIMAGESPACINGX;
203 w -= (BORDERIMAGESPACINGX + data->image->width + BORDERIMAGESPACINGX + BORDERIMAGESPACINGX);
206 tx += (w - TextLength(msg->gpr_RPort, text, len)) / 2;
207 ty += (h - msg->gpr_RPort->TxHeight) / 2 + msg->gpr_RPort->TxBaseline;
209 SetABPenDrMd(msg->gpr_RPort,
210 msg->gpr_GInfo->gi_DrInfo->dri_Pens[(G(o)->Flags & GFLG_SELECTED) ? FILLTEXTPEN : TEXTPEN],
212 JAM1);
213 Move(msg->gpr_RPort, tx, ty);
214 Text(msg->gpr_RPort, text, len);
215 } else {
216 x += 3; w -= 6;
217 y += 3; h -= 6;
219 SetABPenDrMd(msg->gpr_RPort,
220 msg->gpr_GInfo->gi_DrInfo->dri_Pens[(G(o)->Flags & GFLG_SELECTED) ? FILLPEN : BACKGROUNDPEN],
222 JAM1);
224 RectFill(msg->gpr_RPort, x, y, x + w - 1, y + h - 1);
227 if (data->image)
229 if ((data->selcol == 0) && (data->bgcol == 0))
231 ULONG col[3];
233 GetRGB32(msg->gpr_GInfo->gi_Screen->ViewPort.ColorMap,
234 msg->gpr_GInfo->gi_DrInfo->dri_Pens[FILLPEN],
236 col);
238 data->selcol = ((col[0] & 0xFF000000) >> 8) +
239 ((col[1] & 0xFF000000) >> 16) +
240 ((col[2] & 0xFF000000) >> 24);
243 GetRGB32(msg->gpr_GInfo->gi_Screen->ViewPort.ColorMap,
244 msg->gpr_GInfo->gi_DrInfo->dri_Pens[BACKGROUNDPEN],
246 col);
248 data->bgcol = ((col[0] & 0xFF000000) >> 8) +
249 ((col[1] & 0xFF000000) >> 16) +
250 ((col[2] & 0xFF000000) >> 24);
255 data->pal[0] = (G(o)->Flags & GFLG_SELECTED) ? data->selcol : data->bgcol;
257 WriteLUTPixelArray((APTR)data->image->data,
260 data->image->width,
261 msg->gpr_RPort,
262 data->pal,
263 x + BORDERIMAGESPACINGX,
264 y + (h - data->image->height) / 2,
265 data->image->width,
266 data->image->height,
267 CTABFMT_XRGB8);
271 return 0;
274 /****************************************************************************************/
275 /****************************************************************************************/
276 /****************************************************************************************/
278 AROS_UFH3S(IPTR, cool_buttonclass_dispatcher,
279 AROS_UFHA(Class *, cl, A0),
280 AROS_UFHA(Object *, obj, A2),
281 AROS_UFHA(Msg, msg, A1))
283 AROS_USERFUNC_INIT
285 IPTR retval;
287 switch (msg->MethodID)
289 case OM_NEW:
290 retval = coolbutton_new(cl, obj, (struct opSet *)msg);
291 break;
293 case OM_DISPOSE:
294 retval = coolbutton_dispose(cl, obj, msg);
295 break;
297 case GM_HITTEST:
298 retval = coolbutton_hittest(cl, obj, (struct gpHitTest *)msg);
299 break;
301 case GM_RENDER:
302 retval = coolbutton_render(cl, obj, (struct gpRender *)msg);
303 break;
305 default:
306 retval = DoSuperMethodA(cl, obj, msg);
307 break;
309 } /* switch (msg->MethodID) */
311 return retval;
313 AROS_USERFUNC_EXIT
316 /****************************************************************************************/
318 #undef CyberGfxBase
320 /****************************************************************************************/
322 BOOL InitCoolButtonClass(struct Library *CyberGfxBase)
324 AROS_GET_SYSBASE_OK
325 BOOL retval = FALSE;
327 cool_cybergfxbase = CyberGfxBase;
329 if (IntuitionBase && GfxBase && UtilityBase && SysBase)
331 if (!cool_buttonclass)
333 cool_buttonclass = MakeClass(NULL, BUTTONGCLASS, NULL, sizeof(struct CoolButtonData), 0UL);
336 if (cool_buttonclass)
338 cool_buttonclass->cl_Dispatcher.h_Entry = (HOOKFUNC)cool_buttonclass_dispatcher;
339 retval = TRUE;
343 return retval;
346 /****************************************************************************************/
348 void CleanupCoolButtonClass(void)
350 if (cool_buttonclass)
352 FreeClass(cool_buttonclass);
353 cool_buttonclass = NULL;
357 /****************************************************************************************/