Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / workbench / libs / muimaster / classes / radio.c
blobc50a6c818cdc9103f60a1e590ab8ebd3c78e5335
1 /*
2 Copyright © 2002-2003, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <clib/alib_protos.h>
7 #include <proto/exec.h>
8 #include <proto/utility.h>
9 #include <proto/intuition.h>
10 #include <proto/muimaster.h>
12 #include <string.h>
14 /* #define MYDEBUG 1 */
15 #include "debug.h"
17 #include "mui.h"
18 #include "muimaster_intern.h"
19 #include "prefs.h"
20 #include "support.h"
21 #include "support_classes.h"
22 #include "radio_private.h"
24 extern struct Library *MUIMasterBase;
26 IPTR Radio__OM_NEW(struct IClass *cl, Object *obj, struct opSet *msg)
28 struct Radio_DATA *data;
29 const struct TagItem *tags;
30 struct TagItem *tag;
31 int i;
32 const char **entries = NULL;
33 int entries_active = 0;
34 int entries_num;
35 struct TagItem *grouptags;
36 Object **buttons;
37 int state;
39 /* parse initial taglist */
41 for (tags = msg->ops_AttrList; (tag = NextTagItem(&tags)); )
43 switch (tag->ti_Tag)
45 case MUIA_Radio_Entries:
46 entries = (const char**)tag->ti_Data;
47 break;
48 case MUIA_Radio_Active:
49 entries_active = tag->ti_Data;
50 break;
54 if (!entries)
56 D(bug("Radio_New: No Entries specified!\n"));
57 CoerceMethod(cl,obj,OM_DISPOSE);
58 return 0;
61 /* Count the number of entries */
62 for (i=0;entries[i];i++);
64 entries_num = i;
66 if ((entries_active < 0) || (entries_active > entries_num - 1))
67 entries_active = 0;
69 grouptags = AllocateTagItems(entries_num + 1);
70 if (!grouptags)
71 return FALSE;
72 buttons = AllocVec(i * sizeof(Object *), MEMF_PUBLIC);
73 if (!buttons)
75 FreeVec(grouptags);
76 return FALSE;
78 for (i = 0; i < entries_num; i++)
80 state = (entries_active == i) ? TRUE : FALSE;
82 buttons[i] = HGroup,
83 Child, (IPTR)ImageObject,
84 MUIA_Image_FontMatch, TRUE,
85 MUIA_InputMode, MUIV_InputMode_Immediate,
86 MUIA_Selected, state,
87 MUIA_ShowSelState, FALSE,
88 MUIA_Image_Spec, MUII_RadioButton,
89 MUIA_Frame, MUIV_Frame_None,
90 End,
91 Child, (IPTR)TextObject,
92 MUIA_InputMode, MUIV_InputMode_Immediate,
93 MUIA_ShowSelState, FALSE,
94 MUIA_Selected, state,
95 MUIA_Text_Contents, entries[i],
96 MUIA_Frame, MUIV_Frame_None,
97 MUIA_Text_PreParse, (IPTR)"\33l",
98 End,
99 End;
101 grouptags[i].ti_Tag = MUIA_Group_Child;
102 grouptags[i].ti_Data = (IPTR)buttons[i];
105 grouptags[i].ti_Tag = TAG_MORE;
106 grouptags[i].ti_Data = (IPTR)msg->ops_AttrList;
108 obj = (Object *)DoSuperNewTags(cl, obj, NULL,
109 TAG_MORE, (IPTR)grouptags);
110 FreeTagItems(grouptags);
111 if (!obj)
113 FreeVec(buttons);
114 return FALSE;
116 data = INST_DATA(cl, obj);
117 data->entries_active = entries_active;
118 data->entries_num = entries_num;
119 data->buttons = buttons;
121 for (i = 0; i < entries_num; i++)
123 DoMethod(buttons[i], MUIM_Notify, MUIA_Selected, MUIV_EveryTime,
124 (IPTR)obj, 3, MUIM_Set, MUIA_Radio_Active, i);
127 return (IPTR)obj;
130 IPTR Radio__OM_DISPOSE(struct IClass *cl, Object *obj, Msg msg)
132 struct Radio_DATA *data = INST_DATA(cl,obj);
133 if (data->buttons) FreeVec(data->buttons);
134 return DoSuperMethodA(cl,obj,msg);
137 IPTR Radio__OM_SET(struct IClass *cl, Object *obj, struct opSet *msg)
139 struct Radio_DATA *data;
140 const struct TagItem *tags;
141 struct TagItem *tag;
143 data = INST_DATA(cl, obj);
145 for (tags = msg->ops_AttrList; (tag = NextTagItem(&tags)); )
147 switch (tag->ti_Tag)
149 case MUIA_Radio_Active:
150 D(bug("Radio_Set(%p) MUIA_Radio_Active %ld\n",
151 obj, tag->ti_Data));
152 if (tag->ti_Data >= 0 && tag->ti_Data < data->entries_num &&
153 tag->ti_Data != data->entries_active)
155 DoMethod(data->buttons[data->entries_active],
156 MUIM_NoNotifySet, MUIA_Selected, FALSE);
157 DoMethod(data->buttons[tag->ti_Data],
158 MUIM_NoNotifySet, MUIA_Selected, TRUE);
159 data->entries_active = tag->ti_Data;
161 break;
164 return DoSuperMethodA(cl,obj,(Msg)msg);
167 IPTR Radio__OM_GET(struct IClass *cl, Object *obj, struct opGet *msg)
169 struct Radio_DATA *data = INST_DATA(cl, obj);
170 #define STORE *(msg->opg_Storage)
172 switch(msg->opg_AttrID)
174 case MUIA_Radio_Active:
175 STORE = data->entries_active;
176 return 1;
179 return DoSuperMethodA(cl,obj,(Msg)msg);
182 IPTR Radio__MUIM_Setup(struct IClass *cl, Object *obj, Msg msg)
184 if (!DoSuperMethodA(cl, obj, msg))
185 return FALSE;
187 set(obj, MUIA_Group_HorizSpacing, muiGlobalInfo(obj)->mgi_Prefs->radiobutton_hspacing);
188 set(obj, MUIA_Group_VertSpacing, muiGlobalInfo(obj)->mgi_Prefs->radiobutton_vspacing);
190 return TRUE;
193 /**************************************************************************
194 MUIM_Export - to export an objects "contents" to a dataspace object.
195 **************************************************************************/
196 IPTR Radio__MUIM_Export(struct IClass *cl, Object *obj, struct MUIP_Export *msg)
198 struct Radio_DATA *data = INST_DATA(cl, obj);
199 ULONG id;
201 if ((id = muiNotifyData(obj)->mnd_ObjectID))
203 LONG value = data->entries_active;
204 DoMethod(msg->dataspace, MUIM_Dataspace_Add,
205 (IPTR) &value,
206 sizeof(value),
207 (IPTR) id);
209 return 0;
212 /**************************************************************************
213 MUIM_Import - to import an objects "contents" from a dataspace object.
214 **************************************************************************/
215 IPTR Radio__MUIM_Import(struct IClass *cl, Object *obj, struct MUIP_Import *msg)
217 ULONG id;
218 LONG *s;
220 if ((id = muiNotifyData(obj)->mnd_ObjectID))
222 if ((s = (LONG*) DoMethod(msg->dataspace, MUIM_Dataspace_Find, (IPTR) id)))
224 set(obj, MUIA_Radio_Active, *s);
227 return 0;
230 #if ZUNE_BUILTIN_RADIO
231 BOOPSI_DISPATCHER(IPTR, Radio_Dispatcher, cl, obj, msg)
233 switch (msg->MethodID)
235 case OM_NEW: return Radio__OM_NEW(cl, obj, (struct opSet *) msg);
236 case OM_DISPOSE: return Radio__OM_DISPOSE(cl, obj, (Msg) msg);
237 case OM_SET: return Radio__OM_SET(cl, obj, (struct opSet *) msg);
238 case OM_GET: return Radio__OM_GET(cl, obj, (struct opGet *) msg);
239 case MUIM_Setup: return Radio__MUIM_Setup(cl, obj, msg);
240 case MUIM_Export: return Radio__MUIM_Export(cl, obj, (Msg) msg);
241 case MUIM_Import: return Radio__MUIM_Import(cl, obj, (Msg) msg);
242 default: return DoSuperMethodA(cl, obj, msg);
245 BOOPSI_DISPATCHER_END
247 const struct __MUIBuiltinClass _MUI_Radio_desc = {
248 MUIC_Radio,
249 MUIC_Group,
250 sizeof(struct Radio_DATA),
251 (void*)Radio_Dispatcher
253 #endif /* ZUNE_BUILTIN_RADIO */