This should solve an error which made the nightly build fail with:
[AROS-Contrib.git] / bgui / misc.c
blob75c5f7bbdbd350a25f62068cfa5706c7b27be4d9
1 /*
2 * @(#) $Header$
4 * BGUI library
5 * misc.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.4 2004/06/16 20:16:48 verhaegs
15 * Use METHODPROTO, METHOD_END and REGFUNCPROTOn where needed.
17 * Revision 42.3 2003/01/18 19:09:59 chodorowski
18 * Instead of using the _AROS or __AROS preprocessor symbols, use __AROS__.
20 * Revision 42.2 2000/05/29 00:40:24 bergers
21 * Update to compile with AROS now. Should also still compile with SASC etc since I only made changes that test the define __AROS__. The compilation is still very noisy but it does the trick for the main directory. Maybe members of the BGUI team should also have a look at the compiler warnings because some could also cause problems on other systems... (Comparison always TRUE due to datatype (or something like that)). And please compile it on an Amiga to see whether it still works... Thanks.
23 * Revision 42.1 2000/05/14 23:32:48 stegerg
24 * changed over 200 function headers which all use register
25 * parameters (oh boy ...), because the simple REG() macro
26 * doesn't work with AROS. And there are still hundreds
27 * of headers left to be fixed :(
29 * Many of these functions would also work with stack
30 * params, but since i have fixed every single one
31 * I encountered up to now, I guess will have to do
32 * the same for the rest.
34 * Revision 42.0 2000/05/09 22:09:42 mlemos
35 * Bumped to revision 42.0 before handing BGUI to AROS team
37 * Revision 41.11 2000/05/09 19:54:46 mlemos
38 * Merged with the branch Manuel_Lemos_fixes.
40 * Revision 41.10.2.4 1998/12/07 03:07:08 mlemos
41 * Replaced OpenFont and CloseFont calls by the respective BGUI debug macros.
43 * Revision 41.10.2.3 1998/11/23 14:59:16 mlemos
44 * Fixed mistaken initialization of allocated buffer.
46 * Revision 41.10.2.2 1998/11/16 20:11:04 mlemos
47 * Added missing brackets in the previous changes.
49 * Revision 41.10.2.1 1998/11/16 19:59:24 mlemos
50 * Replaced AllocVec and FreeVec calls by BGUI_AllocPoolMem and
51 * BGUI_FreePoolMem calls respectively.
53 * Revision 41.10 1998/02/25 21:12:40 mlemos
54 * Bumping to 41.10
56 * Revision 1.1 1998/02/25 17:09:07 mlemos
57 * Ian sources
62 #ifdef __AROS__
63 #include "include/bgui_macros.h"
64 #endif
66 #include "include/classdefs.h"
68 makeproto struct TextAttr Topaz80 =
70 "topaz.font",
71 TOPAZ_EIGHTY,
72 FS_NORMAL,
73 FPF_ROMFONT
76 //makeproto ASM LONG max(REG(d0) LONG a, REG(d1) LONG b)
77 makeproto ASM REGFUNC2(LONG, max,
78 REGPARAM(D0, LONG, a),
79 REGPARAM(D1, LONG, b))
80 { return (a > b) ? a : b; }
81 REGFUNC_END
83 //makeproto ASM LONG min(REG(d0) LONG a, REG(d1) LONG b)
84 makeproto ASM REGFUNC2(LONG, min,
85 REGPARAM(D0, LONG, a),
86 REGPARAM(D1, LONG, b))
87 { return (a < b) ? a : b; }
88 REGFUNC_END
90 #ifdef __AROS__
91 #warning No abs function here, since it is 'built in'.
92 #else
93 //makeproto ASM LONG abs(REG(d0) LONG a)
94 makeproto ASM REGFUNC1(LONG, abs,
95 REGPARAM(D0, LONG, a))
96 { return (a > 0) ? a :-a; }
97 REGFUNC_END
98 #endif
100 //makeproto ASM LONG range(REG(d0) LONG c, REG(d1) LONG a, REG(d2) LONG b)
101 makeproto ASM REGFUNC3(LONG, range,
102 REGPARAM(D0, LONG, c),
103 REGPARAM(D1, LONG, a),
104 REGPARAM(D2, LONG, b))
106 if (c < a) return a;
107 if (c > b) return b;
108 return c;
110 REGFUNC_END
113 * Count the number of labels in an array.
115 //makeproto ASM ULONG CountLabels(REG(a0) UBYTE **labels)
116 makeproto ASM REGFUNC1(ULONG, CountLabels,
117 REGPARAM(A0, UBYTE **, labels))
119 int n = 0;
121 while (labels[n]) n++;
123 return (ULONG)(n - 1);
125 REGFUNC_END
128 * See if a point is in a box.
130 //makeproto ASM BOOL PointInBox(REG(a0) struct IBox *box, REG(d0) WORD x, REG(d1) WORD y)
131 makeproto ASM REGFUNC3(BOOL, PointInBox,
132 REGPARAM(A0, struct IBox *, box),
133 REGPARAM(D0, WORD, x),
134 REGPARAM(D1, WORD, y))
136 x -= box->Left;
137 y -= box->Top;
139 return (BOOL)((x >= 0) && (y >= 0) && (x < box->Width) && (y < box->Height));
141 REGFUNC_END
144 * Scale a width and height.
146 makeproto VOID Scale(struct RastPort *rp, UWORD *width, UWORD *height, UWORD dwidth, UWORD dheight, struct TextAttr *font)
148 struct TextFont *tf;
149 int open = FALSE;
150 ULONG fw, fh;
153 * Setup the font.
155 if (font)
157 if (tf = BGUI_OpenFont(font))
158 open = TRUE;
160 else
162 tf = rp->Font;
166 * Get font size.
168 if (tf->tf_Flags & FPF_PROPORTIONAL)
170 fw = (tf->tf_Modulo << 3) / (tf->tf_HiChar - tf->tf_LoChar);
172 else
174 fw = tf->tf_XSize;
176 fh = tf->tf_YSize;
179 * Close font.
181 if (open) BGUI_CloseFont(tf);
184 * Scale size;
186 *height = fh + (dheight - 8);
187 *width = fw + (dwidth - 8);
191 * Map a RAW key to an ansi character.
193 makeproto WORD MapKey(UWORD code, UWORD qualifier, APTR *iaddress)
195 struct InputEvent ie = { NULL };
196 UBYTE chr;
198 ie.ie_Class = IECLASS_RAWKEY;
199 ie.ie_Code = code;
200 ie.ie_Qualifier = qualifier;
201 ie.ie_EventAddress = *iaddress;
203 if ( MapRawKey( &ie, &chr, 1, NULL ) != 1 )
204 return( ~0 );
206 return( chr );
210 * Show online-help requester.
212 //makeproto ASM VOID ShowHelpReq( REG(a0) struct Window *win, REG(a1) UBYTE *text )
213 makeproto ASM REGFUNC2(VOID, ShowHelpReq,
214 REGPARAM(A0, struct Window *, win),
215 REGPARAM(A1, UBYTE *, text))
217 struct bguiRequest req = { NULL };
219 InitLocale();
221 req.br_Title = LOCSTR( MSG_BGUI_HELP_TITLE );
222 req.br_GadgetFormat = LOCSTR( MSG_BGUI_HELP_CONTINUE );
223 req.br_TextFormat = text;
224 req.br_Flags = BREQF_CENTERWINDOW | BREQF_LOCKWINDOW | BREQF_AUTO_ASPECT;
226 BGUI_RequestA( win, &req, NULL );
228 REGFUNC_END
232 * Re-allocate and format a buffer.
234 //makeproto ASM UBYTE *DoBuffer(REG(a0) UBYTE *text, REG(a1) UBYTE **buf_ptr, REG(a2) ULONG *buf_len, REG(a3) ULONG *args)
235 makeproto ASM REGFUNC4(UBYTE *, DoBuffer,
236 REGPARAM(A0, UBYTE *, text),
237 REGPARAM(A1, UBYTE **, buf_ptr),
238 REGPARAM(A2, ULONG *, buf_len),
239 REGPARAM(A3, ULONG *, args))
241 ULONG len;
244 * Find out the formatted string length.
246 len = CompStrlenF( text, args );
249 * Longer than the current buffer?
251 if ( len > *buf_len ) {
253 * Deallocate old buffer.
255 if ( *buf_ptr )
256 BGUI_FreePoolMem( *buf_ptr );
258 * And allocate a new one.
260 if ( *buf_ptr = ( UBYTE * )BGUI_AllocPoolMem( len )) {
262 * Setup new buffer length.
264 **buf_ptr = '\0';
265 *buf_len = len;
267 else {
269 * Screw up.
271 *buf_len = 0;
272 return( text );
276 * Format the string.
278 DoSPrintF( *buf_ptr, text, args );
279 return( *buf_ptr );
281 REGFUNC_END
284 * Set on multiple object.
286 makeproto VOID DoMultiSet(Tag tag, ULONG data, ULONG count, Object *obj1, ...)
288 Object **array = &obj1;
289 ULONG i;
291 for (i = 0; i < count; i++)
293 if (array[i]) DoSetMethodNG(array[i], tag, data, TAG_END);
298 * Set a gadget's bounds.
300 //makeproto ASM VOID SetGadgetBounds(REG(a0) Object *obj, REG(a1) struct IBox *bounds)
301 makeproto ASM REGFUNC2(VOID, SetGadgetBounds,
302 REGPARAM(A0, Object *, obj),
303 REGPARAM(A1, struct IBox *, bounds))
305 DoSetMethodNG(obj, GA_Left, bounds->Left, GA_Top, bounds->Top,
306 GA_Width, bounds->Width, GA_Height, bounds->Height, TAG_DONE);
308 REGFUNC_END
311 * Set an image's bounds.
313 //makeproto ASM VOID SetImageBounds(REG(a0) Object *obj, REG(a1) struct IBox *bounds)
314 makeproto ASM REGFUNC2(VOID, SetImageBounds,
315 REGPARAM(A0, Object *, obj),
316 REGPARAM(A1, struct IBox *, bounds))
318 DoSetMethodNG(obj, IA_Left, bounds->Left, IA_Top, bounds->Top,
319 IA_Width, bounds->Width, IA_Height, bounds->Height, TAG_DONE);
321 REGFUNC_END
325 * Un-map a mapped tag-list.
327 //makeproto ASM VOID UnmapTags(REG(a0) struct TagItem *tags, REG(a1) struct TagItem *map)
328 makeproto ASM REGFUNC2(VOID, UnmapTags,
329 REGPARAM(A0, struct TagItem *, tags),
330 REGPARAM(A1, struct TagItem *, map))
332 struct TagItem *tag, *tag1, *at = map;
335 * Go through the mapped tag-list.
337 while (tag = NextTagItem(&at))
340 * Is this tag in the unmapped list?
342 if (tag1 = FindTagItem(tag->ti_Data, tags))
344 * Yes. Unmap this tag.
346 tag1->ti_Tag = tag->ti_Tag;
349 REGFUNC_END
352 * Create a vector image.
354 //makeproto ASM Object *CreateVector(REG(a0) struct TagItem *attr)
355 makeproto ASM REGFUNC1(Object *, CreateVector,
356 REGPARAM(A0, struct TagItem *, attr))
358 struct TagItem *tag, *tstate = attr;
360 while (tag = NextTagItem(&tstate))
362 if (VIT_TAG(tag->ti_Tag))
363 return BGUI_NewObjectA(BGUI_VECTOR_IMAGE, tag);
365 return NULL;
367 REGFUNC_END
370 * Fix the tag pointer.
372 //makeproto ASM struct TagItem *BGUI_NextTagItem(REG(a0) struct TagItem **t)
373 makeproto ASM REGFUNC1(struct TagItem *, BGUI_NextTagItem,
374 REGPARAM(A0, struct TagItem **, t))
376 struct TagItem *rt, *tag, *tag2, *tstate;
378 rt = NextTagItem(t);
380 if (rt)
382 switch (rt->ti_Tag)
384 case GROUP_Member:
385 tag2 = rt;
386 tstate = rt + 1;
387 while (tag = NextTagItem(&tstate))
388 tag2 = tag;
389 *t = tag2 + 2;
390 break;
393 return rt;
395 REGFUNC_END