Added icon for MadMatrix.
[AROS-Contrib.git] / bgui / indicatorclass.c
blobc2d88cb3cd94e3e314a9864db394a3d59bd31f06
1 /*
2 * @(#) $Header$
4 * BGUI library
5 * indicatorclass.c
7 * (C) Copyright 1998 Manuel Lemos.
8 * (C) Copyright 1996-1997 Ian J. Einman.
9 * (C) Copyright 1993-1996 Jaba Development.
10 * (C) Copyright 1993-1996 Jan van den Baard.
11 * All Rights Reserved.
13 * $Log$
14 * Revision 42.2 2004/06/16 20:16:48 verhaegs
15 * Use METHODPROTO, METHOD_END and REGFUNCPROTOn where needed.
17 * Revision 42.1 2000/05/15 19:27:01 stegerg
18 * another hundreds of REG() macro replacements in func headers/protos.
20 * Revision 42.0 2000/05/09 22:09:12 mlemos
21 * Bumped to revision 42.0 before handing BGUI to AROS team
23 * Revision 41.11 2000/05/09 19:54:25 mlemos
24 * Merged with the branch Manuel_Lemos_fixes.
26 * Revision 41.10.2.1 1998/03/01 19:55:15 mlemos
27 * Fixed short allocation for indicator text string.
29 * Revision 41.10 1998/02/25 21:12:15 mlemos
30 * Bumping to 41.10
32 * Revision 1.1 1998/02/25 17:08:33 mlemos
33 * Ian sources
38 /// Class definitions.
39 #include "include/classdefs.h"
42 * Object instance data.
44 typedef struct id_ {
45 UWORD id_Flags; /* see below */
46 Object *id_Text; /* the text to display */
47 IPTR id_Args[1]; /* Args to the label */
48 LONG id_Min; /* minimum indication */
49 LONG id_Max; /* maximum indication */
50 LONG id_Level; /* Current level */
51 UWORD id_Justification; /* label justification */
52 struct TextFont *id_Font; /* use this font */
53 UWORD id_MinSize; /* pre-calculated min size */
54 } ID;
56 #define IDF_INVALID_MINSIZE (1<<0) /* re-calculate minsize */
58 #define ID_ENTRY(tag, offset, flags) PACK_ENTRY(INDIC_TAGSTART, tag, id_, offset, flags)
59 #define ID_FLAG(tag, flag) PACK_LONGBIT(INDIC_TAGSTART, tag, id_, bd_Flags, PKCTRL_BIT, flag)
61 static ULONG IndicatorPackTable[] =
63 PACK_STARTTABLE(INDIC_TAGSTART),
65 ID_ENTRY(INDIC_Justification, id_Justification, PKCTRL_UWORD),
66 ID_ENTRY(INDIC_Min, id_Min, PKCTRL_LONG),
67 ID_ENTRY(INDIC_Max, id_Max, PKCTRL_LONG),
68 ID_ENTRY(INDIC_Level, id_Level, PKCTRL_LONG),
70 PACK_ENDTABLE
73 static void SetLevel(ID *id, LONG level)
75 id->id_Level = level;
76 id->id_Args[0] = (IPTR)level;
79 ///
80 /// OM_NEW
82 * Create a shiny new object.
84 METHOD(IClassNew, struct opSet *, ops)
86 ID *id;
87 struct TagItem *tags;
88 IPTR rc;
90 tags = DefTagList(BGUI_INDICATOR_GADGET, ops->ops_AttrList);
93 * Let the superclass create us an object.
95 if ((rc = NewSuperObject(cl, obj, tags)))
97 struct TagItem *tstate;
98 struct TagItem *tag;
99 id = INST_DATA(cl, rc);
102 * Initialize the local instance
103 * data.
105 id->id_Justification = IDJ_LEFT;
106 id->id_Max = 100;
107 id->id_Text = BGUI_NewObject(BGUI_TEXT_GRAPHIC, TEXTA_Args, &id->id_Args[0], TAG_DONE);
109 BGUI_PackStructureTags((APTR)id, IndicatorPackTable, tags);
110 tstate = tags;
111 while ((tag = NextTagItem(&tstate))) {
112 if (tag->ti_Tag == INDIC_Level) {
113 SetLevel(id, (LONG)tag->ti_Data);
118 * are we setup OK?
120 if (!id->id_Text || (id->id_Min > id->id_Max))
122 AsmCoerceMethod(cl, (Object *)rc, OM_DISPOSE);
123 rc = 0;
125 DoSetMethodNG((Object *)rc, INDIC_FormatString, "%ld", TAG_DONE);
127 FreeTagItems(tags);
129 return rc;
131 METHOD_END
133 /// OM_DISPOSE
135 * Hmm, they want us out of here.
137 METHOD(IClassDispose, Msg, msg)
139 ID *id = INST_DATA(cl, obj);
142 * Dispose of the text graphic.
144 if (id->id_Text) DisposeObject(id->id_Text);
147 * And let the superclass take
148 * care of the rest.
150 return AsmDoSuperMethodA(cl, obj, msg);
152 METHOD_END
154 /// OM_SET, OM_UPDATE
156 * Set/Update object attributes.
158 METHOD(IClassSetUpdate, struct opUpdate *, opu)
160 ID *id = INST_DATA(cl, obj);
161 IPTR data;
162 struct TagItem *tstate = opu->opu_AttrList;
163 struct TagItem *tag;
164 BOOL update = FALSE;
165 LONG old_level = id->id_Level;
166 UBYTE *full_format;
169 * First we let the superclass have a go at it.
171 AsmDoSuperMethodA(cl, obj, (Msg)opu);
174 * Set attributes.
176 BGUI_PackStructureTags((APTR)id, IndicatorPackTable, opu->opu_AttrList);
178 while ((tag = NextTagItem(&tstate)))
180 data = tag->ti_Data;
182 switch (tag->ti_Tag)
185 * Intercept the font.
187 case BT_TextFont:
188 id->id_Font = (struct TextFont *)data;
189 id->id_Flags |= IDF_INVALID_MINSIZE;
190 break;
192 case INDIC_FormatString:
193 if ((full_format = BGUI_AllocPoolMem(3 + strlen((UBYTE *)data))))
195 sprintf(full_format, "\033l%s", (UBYTE *)data);
196 DoSetMethodNG(id->id_Text, TEXTA_Text, full_format, TEXTA_CopyText, TRUE, TAG_DONE);
197 BGUI_FreePoolMem(full_format);
199 case INDIC_Justification:
200 update = TRUE;
201 break;
202 case INDIC_Level:
203 SetLevel(id, (LONG)data);
204 update = TRUE;
205 break;
209 if (id->id_Level < id->id_Min) id->id_Level = id->id_Min;
210 if (id->id_Level > id->id_Max) id->id_Level = id->id_Max;
211 if (id->id_Level != old_level) update = TRUE;
214 * Refresh gadget?
216 if (update) DoRenderMethod(obj, opu->opu_GInfo, GREDRAW_REDRAW);
218 return 1;
220 METHOD_END
222 /// OM_GET
223 METHOD(IClassGet, struct opGet *, opg)
225 ID *id = INST_DATA(cl, obj);
226 ULONG rc = 1;
227 Tag attr = opg->opg_AttrID;
228 IPTR *store = opg->opg_Storage;
230 switch (attr)
232 case INDIC_FormatString:
233 Get_Attr(id->id_Text, TEXTA_Text, store);
234 *store += 2;
235 break;
237 default:
239 * First we see if the attribute they want is known to us. If not
240 * we pass it onto the superclass.
242 rc = BGUI_UnpackStructureTag((UBYTE *)id, IndicatorPackTable, attr, store);
243 if (!rc) rc = AsmDoSuperMethodA(cl, obj, (Msg)opg);
244 break;
246 return rc;
248 METHOD_END
250 /// BASE_RENDER
252 * Render the object.
254 METHOD(IClassRender, struct bmRender *, bmr)
256 ID *id = INST_DATA(cl, obj);
257 BC *bc = BASE_DATA(obj);
258 struct BaseInfo *bi = bmr->bmr_BInfo;
259 UBYTE *str = NULL;
262 * Render the baseclass.
264 AsmDoSuperMethodA(cl, obj, (Msg)bmr);
267 * Setup the font.
269 if (id->id_Font) BSetFont(bi, id->id_Font);
271 Get_Attr(id->id_Text, TEXTA_Text, (IPTR *)&str);
273 if (str)
275 switch (id->id_Justification)
277 case IDJ_LEFT:
278 str[1] = 'l';
279 break;
280 case IDJ_RIGHT:
281 str[1] = 'r';
282 break;
283 case IDJ_CENTER:
284 str[1] = 'c';
285 break;
287 BSetDPenA(bi, TEXTPEN);
288 DoMethod(id->id_Text, TEXTM_RENDER, bi, &bc->bc_InnerBox);
290 return 1;
292 METHOD_END
294 /// GM_HITTEST
296 * This object cannot be hit.
298 METHOD(IClassHitTest, Msg, msg)
300 return 0;
302 METHOD_END
304 /// BASE_DIMENSIONS
306 * Our parent group needs to know
307 * something about our dimensions.
309 METHOD(IClassDimensions, struct bmDimensions *, bmd)
311 ID *id = INST_DATA(cl, obj);
312 struct RastPort *rp = bmd->bmd_BInfo->bi_RPort;
313 UWORD mw, mh, w, h;
314 LONG old_level = id->id_Level;
316 mw = mh = w = h = 0;
319 * Compute size of minimum level.
321 SetLevel(id, id->id_Min);
323 DoMethod(id->id_Text, TEXTM_DIMENSIONS, rp, &mw, &mh);
326 * Compute size of maximum level.
328 SetLevel(id, id->id_Max);
330 DoMethod(id->id_Text, TEXTM_DIMENSIONS, rp, &w, &h);
332 if (mw < w) mw = w;
333 if (mh < h) mh = h;
335 SetLevel(id, old_level);
338 * Add the width of an average digit.
340 mw += TextWidthNum(rp, "0123456789", 10) / 10;
343 * Setup minimum dimensions.
345 return CalcDimensions(cl, obj, bmd, mw, mh);
347 METHOD_END
349 /// Class initialization.
351 * Function table.
353 STATIC DPFUNC ClassFunc[] =
355 { BASE_RENDER, IClassRender, },
356 { BASE_DIMENSIONS, IClassDimensions, },
358 { OM_NEW, IClassNew, },
359 { OM_SET, IClassSetUpdate, },
360 { OM_UPDATE, IClassSetUpdate, },
361 { OM_GET, IClassGet, },
362 { OM_DISPOSE, IClassDispose, },
363 { GM_HITTEST, IClassHitTest, },
364 { DF_END, NULL },
368 * Simple class initialization
370 makeproto Class *InitIndicatorClass(void)
372 return BGUI_MakeClass(CLASS_SuperClassBGUI, BGUI_BASE_GADGET,
373 CLASS_ObjectSize, sizeof(ID),
374 CLASS_DFTable, ClassFunc,
375 TAG_DONE);