Updated to rev. 134 from its own Sourceforge project
[AROS-Contrib.git] / mui / classes / thebar / mcp / utils.c
bloba557271747335f4ee5328c8df5dde956d57437e8
1 /***************************************************************************
3 TheBar.mcc - Next Generation Toolbar MUI Custom Class
4 Copyright (C) 2003-2005 Alfonso Ranieri
5 Copyright (C) 2005-2007 by TheBar.mcc Open Source Team
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 TheBar class Support Site: http://www.sf.net/projects/thebar
19 $Id$
21 ***************************************************************************/
23 #include "class.h"
24 #include "private.h"
25 #include "locale.h"
27 #include <mui/muiundoc.h>
29 #include <ctype.h>
31 #include "SDI_stdarg.h"
32 #include "SDI_compiler.h"
34 /***********************************************************************/
36 #ifndef __MORPHOS__
37 // DoSuperNew()
38 // Calls parent NEW method within a subclass
39 Object * VARARGS68K DoSuperNew(struct IClass *cl, Object *obj, ...)
41 Object *rc;
42 VA_LIST args;
44 VA_START(args, obj);
45 rc = (Object *)DoSuperMethod(cl, obj, OM_NEW, VA_ARG(args, ULONG), NULL);
46 VA_END(args);
48 return rc;
50 #endif
52 /***********************************************************************/
54 #ifndef __MORPHOS__
56 #define HEX(c) ((c>'9')?c-'A'+10:c-'0')
58 ULONG stch_l(char *chr_ptr, ULONG *u_ptr)
60 *u_ptr=0;
61 while (isxdigit(*chr_ptr))
63 *u_ptr=*u_ptr*16+HEX(*chr_ptr);
64 chr_ptr++;
67 return *u_ptr;
70 #endif
72 /***********************************************************************/
74 Object *
75 obutton(const void *text, const void *help)
77 Object *obj;
79 if((obj = MUI_MakeObject(MUIO_Button,(ULONG)tr(text))))
80 SetAttrs(obj,MUIA_CycleChain,TRUE,MUIA_ShortHelp,(ULONG)tr(help),TAG_DONE);
82 return obj;
85 /***********************************************************************/
87 Object *
88 ocycle(const char **array, const void *key, const void *help)
90 Object *obj;
92 if((obj = MUI_MakeObject(MUIO_Cycle,(ULONG)tr(key),(ULONG)array)))
93 SetAttrs(obj,MUIA_CycleChain,TRUE,MUIA_ShortHelp,(ULONG)tr(help),TAG_DONE);
95 return obj;
98 /***********************************************************************/
100 Object *
101 ocheck(const void *key, const void *help)
103 Object *obj;
105 if((obj = MUI_MakeObject(MUIO_Checkmark,(ULONG)tr(key))))
106 SetAttrs(obj,MUIA_CycleChain,TRUE,MUIA_ShortHelp,(ULONG)tr(help),TAG_DONE);
108 return obj;
111 /***********************************************************************/
113 Object *
114 oslider(const void *key, const void *help, LONG min, LONG max)
116 Object *obj;
118 if((obj = MUI_MakeObject(MUIO_Slider,(ULONG)tr(key),min,max)))
119 SetAttrs(obj,MUIA_CycleChain,TRUE,MUIA_ShortHelp,(ULONG)tr(help),TAG_DONE);
121 return obj;
124 /***********************************************************************/
126 Object *
127 opop(ULONG type, const void *key)
129 Object *obj;
131 if (!(lib_flags & BASEFLG_MUI4) && (type==MUII_PopFont))
132 type = MUII_PopUp;
134 if((obj = MUI_MakeObject(MUIO_PopButton,type)))
136 set(obj,MUIA_CycleChain,TRUE);
138 if (key)
140 ULONG k;
142 if((k = GetKeyChar(tr(key))))
143 set(obj,MUIA_ControlChar,ToLower(k));
147 return obj;
150 /***********************************************************************/
152 Object *
153 opoppen(const void *key, const void *title, const void *help)
155 #if defined(__MORPHOS__) || defined(__amigaos4__)
156 return PoppenObject,
157 MUIA_Window_Title, (ULONG)tr(title),
158 MUIA_ControlChar, (ULONG)GetKeyChar(tr(key)),
159 MUIA_Draggable, TRUE,
160 MUIA_CycleChain, TRUE,
161 MUIA_ShortHelp, (ULONG)tr(help),
162 End;
163 #else
164 if (lib_flags & BASEFLG_MUI20)
166 return PoppenObject,
167 MUIA_Window_Title, (ULONG)tr(title),
168 MUIA_ControlChar, (ULONG)GetKeyChar(tr(key)),
169 MUIA_Draggable, TRUE,
170 MUIA_CycleChain, TRUE,
171 MUIA_ShortHelp, (ULONG)tr(help),
172 End;
174 else
176 return poppenObject,
177 MUIA_Window_Title, (ULONG)tr(title),
178 MUIA_ControlChar, (ULONG)GetKeyChar(tr(key)),
179 MUIA_Draggable, TRUE,
180 MUIA_CycleChain, TRUE,
181 MUIA_ShortHelp, (ULONG)tr(help),
182 End;
184 #endif
187 /***********************************************************************/
189 Object *
190 opopfri(const void *key, const void *title, const void *help)
192 return MUI_NewObject("Popfrimage.mui",
193 MUIA_Window_Title, (ULONG)tr(title),
194 MUIA_ControlChar, (ULONG)GetKeyChar(tr(key)),
195 MUIA_CycleChain, TRUE,
196 MUIA_ShortHelp, (ULONG)tr(help),
197 0x80421794, 0,
198 0x8042a547, 0,
199 0x80426a55, 1,
200 0x8042a92b, 0,
201 End;
204 /***********************************************************************/
206 Object *
207 opopback(UNUSED ULONG gradient, const void *key, const void *title, const void *help)
209 #if defined(__MORPHOS__) || defined(__amigaos4__)
210 return MUI_NewObject(MUIC_Popimage,
211 MUIA_Imageadjust_Type, MUIV_Imageadjust_Type_Background,
212 MUIA_Window_Title, (ULONG)tr(title),
213 MUIA_ControlChar, (ULONG)GetKeyChar(tr(key)),
214 MUIA_Draggable, TRUE,
215 MUIA_CycleChain, TRUE,
216 MUIA_ShortHelp, (ULONG)tr(help),
217 End;
218 #else
219 if (lib_flags & BASEFLG_MUI20)
221 return MUI_NewObject(MUIC_Popimage,
222 MUIA_Imageadjust_Type, MUIV_Imageadjust_Type_Background,
223 MUIA_Window_Title, (ULONG)tr(title),
224 MUIA_ControlChar, (ULONG)GetKeyChar(tr(key)),
225 MUIA_Draggable, TRUE,
226 MUIA_CycleChain, TRUE,
227 MUIA_ShortHelp, (ULONG)tr(help),
228 End;
230 else
232 return popbackObject,
233 MUIA_Window_Title, (ULONG)tr(title),
234 MUIA_ControlChar, (ULONG)GetKeyChar(tr(key)),
235 MUIA_Draggable, TRUE,
236 MUIA_CycleChain, TRUE,
237 MUIA_ShortHelp, (ULONG)tr(help),
238 MUIA_Popbackground_Gradient, gradient,
239 End;
241 #endif
244 /***********************************************************************/
246 #ifndef PopframeObject
247 #define PopframeObject MUI_NewObject(MUIC_Popframe
248 #endif
250 Object *
251 opopframe(const void *key, const void *title, const void *help)
253 return PopframeObject,
254 MUIA_Window_Title, (ULONG)tr(title),
255 MUIA_Draggable, TRUE,
256 MUIA_CycleChain, 1,
257 MUIA_ShortHelp, (ULONG)tr(help),
258 MUIA_ControlChar, (ULONG)GetKeyChar(tr(key)),
259 End;
262 /***********************************************************************/
264 #ifndef __MORPHOS__
265 void
266 drawGradient(Object *obj,struct MUIS_TheBar_Gradient *grad)
268 struct RastPort *rp;
269 ULONG rs, gs, bs, horiz, from, to;
270 int i, x, d, step, rdif, gdif, bdif, r1, g1, b1, r2, g2, b2, r, g, b, rd, gd, bd, ridx, gidx, bidx;
271 UWORD l = _mleft(obj), t = _mtop(obj), w = _mwidth(obj), h = _mheight(obj);
273 rd = rdif= ridx = gd = gidx = bd = bidx = 0;
275 rp = _rp(obj);
277 from = grad->from;
278 to = grad->to;
279 horiz = grad->flags & MUIV_TheBar_Gradient_Horiz;
281 /* Compute diffs */
282 r1 = (from & 0x00FF0000)>>16;
283 g1 = (from & 0x0000FF00)>>8;
284 b1 = (from & 0x000000FF);
286 r2 = (to & 0x00FF0000)>>16;
287 g2 = (to & 0x0000FF00)>>8;
288 b2 = (to & 0x000000FF);
290 if (r1>r2)
292 rdif = r1-r2;
293 rs = FALSE;
295 else
297 rdif = r2-r1;
298 rs = TRUE;
301 if (g1>g2)
303 gdif = g1-g2;
304 gs = FALSE;
306 else
308 gdif = g2-g1;
309 gs = TRUE;
312 if (b1>b2)
314 bdif = b1-b2;
315 bs = FALSE;
317 else
319 bdif = b2-b1;
320 bs = TRUE;
323 /* Find out max diff */
324 step = (rdif>=gdif) ? rdif : gdif;
325 if (bdif>step) step = bdif;
327 /* Find out best step to minimize rest */
328 if (horiz)
330 if (step>w) step = w;
332 if (step==0) step = 1;
333 else
335 x = w/step;
336 if (w!=x*step)
338 step = w/(x+1);
339 if (step>w) step = w;
343 else
345 if (step>h) step = h;
347 if (step==0) step = 1;
348 else
350 x = h/step;
351 if (h!=x*step)
353 step = h/(x+1);
354 if (step>h) step = h;
359 if (step>1)
361 /* Compute color components deltas */
362 rd = rdif/step;
363 if (!rd) rd = 1;
365 gd = gdif/step;
366 if (!gd) gd = 1;
368 bd = bdif/step;
369 if (!bd) bd = 1;
371 /* Compute rests */
372 ridx = (rdif>step) ? step/((rdif-step)/rd) : 0;
373 gidx = (gdif>step) ? step/((gdif-step)/gd) : 0;
374 bidx = (bdif>step) ? step/((bdif-step)/bd) : 0;
377 /* Here we go */
378 d = (horiz) ? w/step : h/step;
379 x = (horiz) ? l : t;
380 r = r1;
381 g = g1;
382 b = b1;
384 for (i = 0; i<step; i++)
386 ULONG col = (r<<16) | (g<<8) | b;
388 /* Fill; if last step fill all rest */
389 if (horiz)
390 if (i==step-1)
392 FillPixelArray(rp,x,t,w-x+l,h,col);
393 break;
395 else FillPixelArray(rp,x,t,d,h,col);
396 else
397 if (i==step-1)
399 FillPixelArray(rp,l,x,w,h-x+t,col);
400 break;
402 else FillPixelArray(rp,l,x,w,d,col);
404 x += d;
406 /* Add delta to each color component */
407 if (rs)
409 r += rd;
411 /* Time to add more one more delta? */
412 if (i && ridx && !(i%ridx)) r += rd;
413 if (r>r2) r = r2;
415 else
417 r -= rd;
419 if (i && ridx && !(i%ridx)) r -= rd;
420 if (r<r2) r = r2;
423 if (gs)
425 g += gd;
427 if (i && gidx && !(i%gidx)) g += gd;
428 if (g>g2) g = g2;
430 else
432 g -= gd;
434 if (i && gidx && !(i%gidx)) g -= gd;
435 if (g<g2) g = g2;
438 if (bs)
440 b += bd;
442 if (i && bidx && !(i%bidx)) b += bd;
443 if (b>b2) b = b2;
445 else
447 b -= bd;
449 if (i && bidx && !(i%bidx)) b -= bd;
450 if (b<b2) b = b2;
454 #endif
456 /***********************************************************************/