Tabs to spaces, more consistent formatting.
[AROS.git] / workbench / libs / muimaster / classes / gauge.c
blob1f534ad77a35927863de0269f11662471ca26f74
1 /*
2 Copyright © 2002-2011, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #define MUIMASTER_YES_INLINE_STDARG
8 #include <stdio.h>
10 #include <graphics/gfx.h>
11 #include <graphics/view.h>
12 #include <clib/alib_protos.h>
13 #include <proto/exec.h>
14 #include <proto/graphics.h>
15 #include <proto/utility.h>
16 #include <proto/intuition.h>
17 #include <proto/muimaster.h>
19 #include <string.h>
21 #include "mui.h"
22 #include "muimaster_intern.h"
23 #include "textengine.h"
24 #include "support.h"
25 #include "support_classes.h"
26 #include "gauge_private.h"
28 /* #define MYDEBUG 1 */
29 #include "debug.h"
31 extern struct Library *MUIMasterBase;
34 IPTR Gauge__OM_NEW(struct IClass *cl, Object *obj, struct opSet *msg)
36 struct Gauge_DATA *data;
37 struct TagItem *tag;
38 struct TagItem *tags;
40 obj = (Object *) DoSuperMethodA(cl, obj, (Msg) msg);
41 if (!obj)
42 return FALSE;
44 data = INST_DATA(cl, obj);
46 data->divide = 1;
47 data->max = 100;
48 data->horiz = FALSE;
49 data->current = 0;
50 data->dupinfo = FALSE;
51 data->info = NULL;
53 /* parse initial taglist */
54 for (tags = msg->ops_AttrList; (tag = NextTagItem(&tags));)
56 switch (tag->ti_Tag)
58 case MUIA_Gauge_Current:
59 data->current = tag->ti_Data;
60 break;
61 case MUIA_Gauge_Divide:
62 data->divide = tag->ti_Data;
63 break;
64 case MUIA_Gauge_Horiz:
65 data->horiz = tag->ti_Data;
66 break;
67 case MUIA_Gauge_InfoText:
68 data->info = (STRPTR) tag->ti_Data;
69 break;
70 case MUIA_Gauge_Max:
71 data->max = tag->ti_Data;
72 break;
73 case MUIA_Gauge_DupInfoText:
74 data->dupinfo = tag->ti_Data;
75 break;
78 if (data->divide != 0)
79 data->current /= data->divide;
80 if (data->current > data->max)
81 data->current = data->max;
82 if (data->dupinfo)
83 data->info = StrDup(data->info);
85 if (data->info)
87 snprintf(data->buf, GAUGE_BUFSIZE, data->info, data->current);
89 else
90 data->buf[0] = 0;
92 /* D(bug("Gauge_New(0x%lx, %d)\n",obj,muiAreaData(obj)->mad_Frame)); */
94 return (IPTR) obj;
97 IPTR Gauge__OM_DISPOSE(struct IClass *cl, Object *obj, Msg msg)
99 struct Gauge_DATA *data = INST_DATA(cl, obj);
100 if (data->dupinfo && data->info)
101 FreeVec(data->info);
102 return DoSuperMethodA(cl, obj, msg);
105 IPTR Gauge__OM_SET(struct IClass *cl, Object *obj, struct opSet *msg)
107 struct Gauge_DATA *data;
108 struct TagItem *tag;
109 struct TagItem *tags;
110 int info_changed = 0;
111 int need_redraw = 0;
113 data = INST_DATA(cl, obj);
115 for (tags = msg->ops_AttrList; (tag = NextTagItem(&tags));)
117 switch (tag->ti_Tag)
119 case MUIA_Gauge_Current:
120 if (data->current != tag->ti_Data)
122 data->current = tag->ti_Data;
123 info_changed = 1;
124 need_redraw = 1;
126 else
128 tag->ti_Tag = TAG_IGNORE;
130 break;
132 case MUIA_Gauge_Divide:
133 if (data->divide != tag->ti_Data)
135 data->divide = tag->ti_Data;
136 info_changed = 1;
137 need_redraw = 1;
139 else
141 tag->ti_Tag = TAG_IGNORE;
143 break;
145 case MUIA_Gauge_InfoText:
146 if (!data->info || strcmp(data->info, (STRPTR) tag->ti_Data))
148 if (data->dupinfo)
150 if (data->info)
151 FreeVec(data->info);
152 data->info = StrDup((STRPTR) tag->ti_Data);
154 else
156 data->info = (STRPTR) tag->ti_Data;
158 need_redraw = info_changed = 1;
160 else
162 tag->ti_Tag = TAG_IGNORE;
164 break;
166 case MUIA_Gauge_Max:
167 if (data->max != tag->ti_Data)
169 data->max = tag->ti_Data;
170 need_redraw = 1;
172 else
174 tag->ti_Tag = TAG_IGNORE;
176 break;
180 if (data->divide != 0)
181 data->current /= data->divide;
182 if (data->current > data->max)
183 data->current = data->max;
185 if (info_changed)
187 if (data->info)
189 snprintf(data->buf, GAUGE_BUFSIZE, data->info, data->current);
191 else
192 data->buf[0] = 0;
195 if (need_redraw)
197 MUI_Redraw(obj, MADF_DRAWOBJECT);
199 return DoSuperMethodA(cl, obj, (Msg) msg);
202 IPTR Gauge__OM_GET(struct IClass *cl, Object *obj, struct opGet *msg)
204 struct Gauge_DATA *data = INST_DATA(cl, obj);
205 IPTR *store = msg->opg_Storage;
206 ULONG tag = msg->opg_AttrID;
208 switch (tag)
210 case MUIA_Gauge_Current:
211 *store = data->current;
212 break;
214 case MUIA_Gauge_Divide:
215 *store = data->divide;
216 break;
218 case MUIA_Gauge_InfoText:
219 *store = (IPTR) data->info;
220 break;
222 case MUIA_Gauge_Max:
223 *store = data->max;
224 break;
226 case MUIA_Gauge_DupInfoText:
227 *store = (IPTR) data->dupinfo;
228 break;
230 default:
231 return DoSuperMethodA(cl, obj, (Msg) msg);
234 return TRUE;
237 IPTR Gauge__MUIM_Setup(struct IClass *cl, Object *obj,
238 struct MUIP_Setup *msg)
240 struct Gauge_DATA *data = INST_DATA(cl, obj);
242 if (!(DoSuperMethodA(cl, obj, (Msg) msg)))
243 return 0;
245 if (data->info)
247 struct RastPort rp;
249 InitRastPort(&rp);
250 SetFont(&rp, _font(obj));
252 data->info_width = TextLength(&rp, data->buf, strlen(data->buf));
253 data->info_height = _font(obj)->tf_YSize;
255 else
257 if (data->horiz)
259 data->info_width = 0;
260 data->info_height = 0;
262 else
264 data->info_width = 0;
265 data->info_height = 0;
269 return 1;
272 IPTR Gauge__MUIM_AskMinMax(struct IClass *cl, Object *obj,
273 struct MUIP_AskMinMax *msg)
275 struct Gauge_DATA *data = INST_DATA(cl, obj);
276 DoSuperMethodA(cl, obj, (Msg) msg);
278 if (data->horiz)
280 msg->MinMaxInfo->MinWidth += data->info_width;
281 msg->MinMaxInfo->MinHeight += data->info_height + 2;
282 msg->MinMaxInfo->DefWidth += data->info_width + 10;
283 msg->MinMaxInfo->DefHeight += data->info_height + 2;
284 msg->MinMaxInfo->MaxWidth = MUI_MAXMAX;
285 if (data->info)
286 msg->MinMaxInfo->MaxHeight += data->info_height + 2;
287 else
288 msg->MinMaxInfo->MaxHeight = MUI_MAXMAX;
291 else
293 msg->MinMaxInfo->MinWidth += data->info_width + 2;
294 msg->MinMaxInfo->MinHeight += data->info_height;
295 msg->MinMaxInfo->DefWidth += data->info_width + 2;
296 msg->MinMaxInfo->DefHeight += data->info_height + 10;
297 if (data->info)
298 msg->MinMaxInfo->MaxWidth += data->info_width + 2;
299 else
300 msg->MinMaxInfo->MaxWidth = MUI_MAXMAX;
302 msg->MinMaxInfo->MaxHeight = MUI_MAXMAX;
304 return 0;
307 IPTR Gauge__MUIM_Draw(struct IClass *cl, Object *obj,
308 struct MUIP_Draw *msg)
310 struct Gauge_DATA *data = INST_DATA(cl, obj);
311 //ULONG val;
313 DoSuperMethodA(cl, obj, (Msg) msg);
315 /* D(bug("Gauge_Draw(0x%lx) %ldx%ldx%ldx%ld :: %ldx%ldx%ldx%ld\n",obj,_left(obj),_top(obj),_right(obj),_bottom(obj), _mleft(obj),_mtop(obj), _mright(obj), _mbottom(obj))); */
317 if (data->horiz)
319 ULONG w;
320 if (data->max != 0)
322 /* NOTE: should be 64 bit */
323 w = _mwidth(obj) * data->current / data->max;
325 else
327 w = 0;
329 SetABPenDrMd(_rp(obj), _pens(obj)[MPEN_FILL], 0, JAM1);
330 RectFill(_rp(obj), _mleft(obj), _mtop(obj), _mleft(obj) + w - 1,
331 _mbottom(obj));
333 if (data->info)
335 ZText *ztext =
336 zune_text_new("\33c\0338", data->buf, ZTEXT_ARG_NONE, 0);
337 if (ztext)
339 zune_text_get_bounds(ztext, obj);
340 SetAPen(_rp(obj), _pens(obj)[MPEN_SHINE]);
341 zune_text_draw(ztext, obj, _mleft(obj), _mright(obj),
342 _mtop(obj) + (_mheight(obj) - ztext->height) / 2);
343 zune_text_destroy(ztext);
347 else
349 ULONG h;
351 /* NOTE: should be 64 bit */
352 h = _mheight(obj) * data->current / data->max;
354 SetABPenDrMd(_rp(obj), _pens(obj)[MPEN_FILL], 0, JAM1);
355 RectFill(_rp(obj), _mleft(obj), _mbottom(obj) - h + 1, _mright(obj),
356 _mbottom(obj));
358 if (data->info)
360 ZText *ztext =
361 zune_text_new("\33c\0338", data->buf, ZTEXT_ARG_NONE, 0);
362 if (ztext)
364 zune_text_get_bounds(ztext, obj);
365 SetAPen(_rp(obj), _pens(obj)[MPEN_SHINE]);
366 zune_text_draw(ztext, obj, _mleft(obj), _mright(obj),
367 _mtop(obj) + (_mheight(obj) - ztext->height) / 2);
368 zune_text_destroy(ztext);
372 return 0;
376 #if ZUNE_BUILTIN_GAUGE
377 BOOPSI_DISPATCHER(IPTR, Gauge_Dispatcher, cl, obj, msg)
379 switch (msg->MethodID)
381 case OM_NEW:
382 return Gauge__OM_NEW(cl, obj, (struct opSet *)msg);
383 case OM_DISPOSE:
384 return Gauge__OM_DISPOSE(cl, obj, (Msg) msg);
385 case OM_SET:
386 return Gauge__OM_SET(cl, obj, (struct opSet *)msg);
387 case OM_GET:
388 return Gauge__OM_GET(cl, obj, (struct opGet *)msg);
389 case MUIM_Setup:
390 return Gauge__MUIM_Setup(cl, obj, (struct MUIP_Setup *)msg);
391 case MUIM_AskMinMax:
392 return Gauge__MUIM_AskMinMax(cl, obj, (struct MUIP_AskMinMax *)msg);
393 case MUIM_Draw:
394 return Gauge__MUIM_Draw(cl, obj, (struct MUIP_Draw *)msg);
395 default:
396 return DoSuperMethodA(cl, obj, msg);
399 BOOPSI_DISPATCHER_END
401 const struct __MUIBuiltinClass _MUI_Gauge_desc =
403 MUIC_Gauge,
404 MUIC_Area,
405 sizeof(struct Gauge_DATA),
406 (void *) Gauge_Dispatcher
408 #endif /* ZUNE_BUILTIN_GAUGE */