Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / workbench / libs / muimaster / classes / palette.c
blob42f2e4eb012c33ec1b4b74fab187f2cc1d59d9be
1 /*
2 Copyright © 2002-2006, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <graphics/gfx.h>
7 #include <graphics/view.h>
8 #include <clib/alib_protos.h>
9 #include <proto/exec.h>
10 #include <proto/graphics.h>
11 #include <proto/utility.h>
12 #include <proto/intuition.h>
13 #include <proto/muimaster.h>
14 #include <proto/colorwheel.h>
15 #include <utility/hooks.h>
16 #include "gadgets/colorwheel.h"
17 #include <gadgets/gradientslider.h>
18 #include "intuition/icclass.h"
19 #include "intuition/gadgetclass.h"
20 #include <string.h>
21 #include <stdio.h>
23 #include "debug.h"
25 #include "mui.h"
26 #include "imspec.h"
27 #include "muimaster_intern.h"
28 #include "support.h"
29 #include "prefs.h"
30 #include "palette.h"
31 #include "palette_private.h"
33 #define ColorWheelBase data->colorwheelbase
35 extern struct Library *MUIMasterBase;
37 static LONG display_func(struct Hook *hook, char **array, struct MUI_Palette_Entry *entry)
39 struct MUI_PaletteData *data = hook->h_Data;
41 if (data->names) { /* does any strings exist */
42 *array = (char *)data->names[(int) array[-1]]; /*then display user names */
43 array++;
44 } else {
45 sprintf(data->buf,"Color %ld",(long)(array[-1]+1)); /* if nos show default color names */
46 *array++ = data->buf;
48 return 0;
51 static void NotifyGun(Object *obj, struct MUI_PaletteData *data, LONG gun)
53 static Tag guntotag[3] =
55 MUIA_Coloradjust_Red,
56 MUIA_Coloradjust_Green,
57 MUIA_Coloradjust_Blue
60 struct TagItem tags[] =
62 {0 , 0},
63 {MUIA_Coloradjust_RGB, 0},
64 {TAG_DONE }
67 tags[0].ti_Tag = guntotag[gun];
68 tags[0].ti_Data = data->rgb[gun];
69 tags[1].ti_Data = (IPTR)data->rgb;
71 CoerceMethod(data->notifyclass, obj, OM_SET, (IPTR)tags, NULL);
74 static LONG setcolor_func(struct Hook *hook, APTR *obj, STACKULONG *notify)
76 ULONG val;
77 ULONG mode = *notify++;
78 ULONG gun = *notify++;
79 struct MUI_PaletteData *data = (struct MUI_PaletteData *) *notify++;
81 LONG entrie = XGET(data->list, MUIA_List_Active);
82 if ((entrie < 0) || (entrie >= data->numentries)) return 0;
83 if (mode == 1) {
84 if (data->numentries > 0) {
85 val = XGET(data->list, MUIA_List_Active);
86 ULONG r = data->entries[entrie].mpe_Red;
87 ULONG g = data->entries[entrie].mpe_Green;
88 ULONG b = data->entries[entrie].mpe_Blue;
89 nnset(data->coloradjust, MUIA_Coloradjust_Red, r);
90 nnset(data->coloradjust, MUIA_Coloradjust_Green, g);
91 nnset(data->coloradjust, MUIA_Coloradjust_Blue, b);
92 data->rgb[0] = r;
93 data->rgb[1] = g;
94 data->rgb[2] = b;
95 NotifyGun((Object*)obj, data, gun);
97 } else if (mode == 2) {
98 data->entries[entrie].mpe_Red = XGET(data->coloradjust, MUIA_Coloradjust_Red);
99 data->entries[entrie].mpe_Green = XGET(data->coloradjust, MUIA_Coloradjust_Green);
100 data->entries[entrie].mpe_Blue = XGET(data->coloradjust, MUIA_Coloradjust_Blue);
102 return 0;
105 /**************************************************************************
106 OM_NEW
107 **************************************************************************/
108 IPTR Palette__OM_NEW(struct IClass *cl, Object *obj, struct opSet *msg)
110 struct MUI_PaletteData *data;
111 //struct TagItem *tag, *tags;
112 struct MUI_Palette_Entry *e;
113 Object *list, *coloradjust;
114 int i;
116 obj = (Object *) DoSuperNewTags(cl, obj, NULL,
117 GroupFrame,
118 MUIA_Background, MUII_ButtonBack,
119 MUIA_Group_Horiz, TRUE,
120 Child, list = ListviewObject,
121 MUIA_Listview_List, ListObject,
122 End,
123 End,
124 Child, coloradjust = ColoradjustObject,
125 End,
126 TAG_MORE, (IPTR) msg->ops_AttrList);
128 if (obj == NULL) return (IPTR)NULL;
130 data = INST_DATA(cl, obj);
132 data->list = list;
133 data->coloradjust = coloradjust;
135 data->display_hook.h_Entry = HookEntry;
136 data->display_hook.h_SubEntry = (HOOKFUNC)display_func;
137 data->display_hook.h_Data = data;
138 data->setcolor_hook.h_Entry = HookEntry;
139 data->setcolor_hook.h_SubEntry = (HOOKFUNC)setcolor_func;
140 data->setcolor_hook.h_Data = data;
142 nnset(list, MUIA_List_DisplayHook, (IPTR)&data->display_hook);
144 data->entries = (struct MUI_Palette_Entry*)GetTagData(MUIA_Palette_Entries, 0, msg->ops_AttrList);
145 data->names = (const char**)GetTagData(MUIA_Palette_Names, 0, msg->ops_AttrList);
146 data->group = GetTagData(MUIA_Palette_Groupable, 0, msg->ops_AttrList);
148 data->numentries = 0;
149 e = data->entries;
150 if (e) {
151 data->numentries = 0;
152 while(e->mpe_ID != MUIV_Palette_Entry_End) {
153 data->numentries++;
154 e++;
158 data->notifyclass = cl->cl_Super->cl_Super;
160 if (data->numentries > 0) {
161 for (i = 0; i < data->numentries; i++) DoMethod(data->list,MUIM_List_InsertSingle,&data->entries[i],MUIV_List_Insert_Bottom);
163 nnset(data->coloradjust, MUIA_Coloradjust_Red, data->entries[0].mpe_Red);
164 nnset(data->coloradjust, MUIA_Coloradjust_Green, data->entries[0].mpe_Green);
165 nnset(data->coloradjust, MUIA_Coloradjust_Blue, data->entries[0].mpe_Blue);
166 nnset(data->list, MUIA_List_Active, 0);
168 data->rgb[0] = data->entries[0].mpe_Red;
169 data->rgb[1] = data->entries[0].mpe_Green;
170 data->rgb[2] = data->entries[0].mpe_Blue;
172 DoMethod(data->list, MUIM_Notify, MUIA_List_Active, MUIV_EveryTime , obj, 5, MUIM_CallHook, &data->setcolor_hook, 1, 0, data);
173 DoMethod(data->coloradjust, MUIM_Notify, MUIA_Coloradjust_Red, MUIV_EveryTime, obj, 5, MUIM_CallHook, &data->setcolor_hook, 2, 0, data);
174 DoMethod(data->coloradjust, MUIM_Notify, MUIA_Coloradjust_Green, MUIV_EveryTime, obj, 5, MUIM_CallHook, &data->setcolor_hook, 2, 1, data);
175 DoMethod(data->coloradjust, MUIM_Notify, MUIA_Coloradjust_Blue, MUIV_EveryTime, obj, 5, MUIM_CallHook, &data->setcolor_hook, 2, 2, data);
176 return (IPTR)obj;
179 /**************************************************************************
180 OM_SET
181 **************************************************************************/
182 IPTR Palette__OM_SET(struct IClass *cl, Object *obj, struct opSet *msg)
184 struct MUI_PaletteData *data;
185 struct TagItem *tag, *tags;
187 data = INST_DATA(cl, obj);
189 for (tags = msg->ops_AttrList; (tag = NextTagItem((const struct TagItem**)&tags)); )
191 switch (tag->ti_Tag)
193 case MUIA_Palette_Entries:
194 data->entries = (struct MUI_Palette_Entry *) tag->ti_Data;
195 break;
197 case MUIA_Palette_Names:
198 data->names = (const char **) tag->ti_Data;
199 break;
201 case MUIA_Palette_Groupable:
202 data->group = (ULONG) tag->ti_Data;
203 break;
206 return DoSuperMethodA(cl,obj,(Msg)msg);
209 /**************************************************************************
210 OM_GET
211 **************************************************************************/
212 IPTR Palette__OM_GET(struct IClass *cl, Object *obj, struct opGet *msg)
214 struct MUI_PaletteData *data = INST_DATA(cl, obj);
216 IPTR *store = msg->opg_Storage;
218 switch(msg->opg_AttrID)
220 case MUIA_Palette_Entries:
221 *store = (IPTR)&data->entries;
222 return TRUE;
223 case MUIA_Palette_Names:
224 *store = (IPTR)&data->names;
225 return TRUE;
227 case MUIA_Coloradjust_Red:
228 *store = data->rgb[0];
229 return TRUE;
231 case MUIA_Coloradjust_Green:
232 *store = data->rgb[1];
233 return TRUE;
235 case MUIA_Coloradjust_Blue:
236 *store = data->rgb[2];
237 return TRUE;
239 case MUIA_Coloradjust_RGB:
240 *store = (IPTR)data->rgb;
241 return TRUE;
243 return DoSuperMethodA(cl,obj,(Msg)msg);
245 /**************************************************************************
246 OM_GET
247 **************************************************************************/
249 IPTR Palette__OM_DISPOSE(struct IClass *cl, Object *obj, Msg msg)
251 struct MUI_PaletteData *data;
253 data = INST_DATA(cl, obj);
255 return DoSuperMethodA(cl, obj, msg);
258 #if ZUNE_BUILTIN_PALETTE
260 BOOPSI_DISPATCHER(IPTR, Palette_Dispatcher, cl, obj, msg)
262 switch (msg->MethodID)
264 case OM_NEW: return Palette__OM_NEW(cl, obj, (struct opSet *)msg);
265 case OM_SET: return Palette__OM_SET(cl, obj, (struct opSet *)msg);
266 case OM_GET: return Palette__OM_GET(cl, obj, (struct opGet *)msg);
267 case OM_DISPOSE: return Palette__OM_DISPOSE(cl, obj, (struct opGet *)msg);
268 default: return DoSuperMethodA(cl, obj, msg);
271 BOOPSI_DISPATCHER_END
274 * Class descriptor.
276 const struct __MUIBuiltinClass _MUI_Palette_desc = {
277 MUIC_Palette,
278 MUIC_Group,
279 sizeof(struct MUI_PaletteData),
280 (void*)Palette_Dispatcher
282 #endif