Updated PCI IDs to latest snapshot.
[tangerine.git] / workbench / prefs / locale / page_country.c
blobee3aac91c801a63d18b11d3d244e5efeef0dd0f9
1 /*
2 Copyright 2003-2008, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 // #define MUIMASTER_YES_INLINE_STDARG
8 #include <exec/types.h>
9 #include <utility/tagitem.h>
10 #include <libraries/asl.h>
11 #include <libraries/mui.h>
12 #include <prefs/locale.h>
13 #include <prefs/prefhdr.h>
14 //#define DEBUG 1
15 #include <zune/customclasses.h>
16 #include <zune/prefseditor.h>
18 #include <proto/exec.h>
19 #include <proto/intuition.h>
20 #include <proto/utility.h>
21 #include <proto/muimaster.h>
22 #include <proto/dos.h>
23 #include <proto/iffparse.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <stdlib.h>
29 #include <aros/debug.h>
31 #include "global.h"
32 #include "page_country.h"
33 #include "registertab.h"
35 /*** String Data ************************************************************/
37 /*** Instance Data **********************************************************/
39 typedef struct MUI_CountryPic
41 char *strings_country;
42 Object *pic;
43 Object *list_pic;
44 } MUI_CountryPic;
46 struct MUI_CountryData
48 Object *me;
49 Object *child;
50 Object *prefs;
51 ULONG active;
52 unsigned int nr_countries;
53 unsigned int filled;
54 MUI_CountryPic **pic;
57 struct MUI_CustomClass *Country_CLASS;
59 /*** Helpers *****************************************************************/
61 #define MAX_COUNTRY_LEN 256
63 /*******************************
64 * prepare for fill_country_list
65 *******************************/
67 STATIC VOID init_country_list(struct MUI_CountryData *data) {
69 struct CountryEntry *entry;
70 BPTR lock;
71 int i;
72 char filename[MAX_COUNTRY_LEN];
74 if(data->filled)
76 return;
79 data->filled=1;
81 /* count countries */
82 data->nr_countries=0;
83 ForeachNode(&country_list, entry)
85 data->nr_countries++;
88 D(bug("[country class] nr of countries: %d\n",data->nr_countries));
90 data->pic=AllocVec(sizeof(struct MUI_CountryPic *) * (data->nr_countries+1),MEMF_CLEAR);
92 i=0;
93 ForeachNode(&country_list, entry)
95 data->pic[i]=AllocVec(sizeof(struct MUI_CountryPic),MEMF_CLEAR);
97 snprintf(filename,MAX_COUNTRY_LEN-1,
98 "LOCALE:Flags/Countries/%s",entry->lve.realname);
100 if ((lock = Lock(filename, ACCESS_READ)) != NULL) {
101 data->pic[i]->pic=(APTR) MUI_NewObject("Dtpic.mui",
102 MUIA_Dtpic_Name,(ULONG) filename,
103 TAG_DONE);
105 UnLock(lock);
106 if(!data->pic[i]->pic)
108 D(bug("[country class] Picture %s failed to load\n",filename));
110 else
112 D(bug("[country class] Picture %s loaded: %lx\n",filename,data->pic[i]->pic));
116 if(data->pic[i]->pic)
118 data->pic[i]->list_pic=(Object *) DoMethod(data->child,
119 MUIM_List_CreateImage,data->pic[i]->pic,
122 else
124 data->pic[i]->list_pic=NULL; /* should be ok */
125 D(bug("ERROR: [country class] data->pic[%d]->pic is NULL!\n",i));
127 data->pic[i]->strings_country=AllocVec(sizeof(char) * MAX_COUNTRY_LEN,MEMF_CLEAR);
128 snprintf(data->pic[i]->strings_country,
129 MAX_COUNTRY_LEN,"\33O[%08lx] %s",
130 (long unsigned int) data->pic[i]->list_pic,
131 entry->lve.name); /* 64-bit !? */
133 DoMethod(data->child,
134 MUIM_List_InsertSingle, data->pic[i]->strings_country,
135 MUIV_List_Insert_Bottom);
137 i++;
140 /* we did remember that */
141 nnset(data->child, MUIA_List_Active, data->active);
144 /*** Methods ****************************************************************
148 Object *Country_New(struct IClass *cl, Object *obj, struct opSet *msg)
150 struct MUI_CountryData *data;
151 struct TagItem *tstate, *tag;
153 D(bug("[country class] Country Class New\n"));
156 * country flags are at the moment 17 pixels high
157 * MUIA_List_MinLineHeight, 18 leaves at least one
158 * pixel space between the images
159 * If images ever get bigger, this should be
160 * no problem.
163 obj = (Object *) DoSuperNewTags(
164 cl, obj, NULL,
165 InputListFrame,
166 MUIA_List_MinLineHeight, 18,
167 TAG_DONE
170 if (obj == NULL)
172 D(bug("ERROR: [country class] DoSuperNewTags failed!\n"));
173 return NULL;
176 data = INST_DATA(cl, obj);
178 tstate=((struct opSet *)msg)->ops_AttrList;
179 while ((tag = (struct TagItem *) NextTagItem((APTR) &tstate)))
181 switch (tag->ti_Tag)
183 case MA_PrefsObject:
184 data->prefs = (Object *) tag->ti_Data;
185 break;
189 data->filled=0;
190 data->child=obj;
192 /* changed hook */
193 DoMethod(obj, MUIM_Notify, MUIA_List_Active, MUIV_EveryTime, (IPTR) data->prefs, 3, MUIM_Set, MUIA_PrefsEditor_Changed, TRUE);
195 data->me=obj;
196 return obj;
199 /**************************************************************
200 * seems like you can only call CreateImage after the list
201 * is setup, which is quite late. So we do it, after
202 * the list is shown. Nice? Not really. Maybe I did
203 * not understand, why this needs to be.
204 **************************************************************/
205 static IPTR Country_Fill(struct IClass *cl, Object *obj, struct MUIP_Show *msg)
207 struct MUI_CountryData *data = INST_DATA(cl, obj);
209 IPTR ret;
211 ret=DoSuperMethodA(cl, obj, (Msg)msg);
213 init_country_list(data);
215 return ret;
218 /******************************************
219 * According to the MUI docs, you should
220 * call DeleteImage and Dispose during
221 * Cleanup.
222 ******************************************/
223 static IPTR Country_Cleanup(struct IClass *cl, Object *obj, struct MUIP_Cleanup *msg)
225 struct MUI_CountryData *data = INST_DATA(cl, obj);
226 unsigned int i;
228 D(bug("[country class] Country_Cleanup\n"));
230 if(data->filled)
232 for(i=0; i<data->nr_countries; i++)
234 if(data->pic[i])
236 if(data->pic[i]->list_pic)
238 DoMethod(data->child,
239 MUIM_List_DeleteImage,data->pic[i]->list_pic);
240 data->pic[i]->list_pic=NULL;
242 if(data->pic[i]->pic)
244 DoMethod(data->pic[i]->pic,OM_DISPOSE);
245 data->pic[i]->pic=NULL;
250 else
252 D(bug("[country class] Country_Cleanup and !data->filled!?\n"));
255 return DoSuperMethodA(cl,obj,(Msg)msg);
258 static IPTR Country_Dispose(struct IClass *cl, Object *obj, Msg msg)
260 struct MUI_CountryData *data = INST_DATA(cl, obj);
261 unsigned int i;
263 D(bug("[country class] Country_Dispose\n"));
265 if(!data->pic)
266 return DoSuperMethodA(cl, obj, msg);
268 for(i=0;i<data->nr_countries;i++)
270 if(data->pic[i])
272 if(data->pic[i]->strings_country)
274 FreeVec(data->pic[i]->strings_country);
276 FreeVec(data->pic[i]);
279 FreeVec(data->pic);
280 return DoSuperMethodA(cl, obj, msg);
283 /*** Get ******************************************************************/
284 static IPTR Country_Get(struct IClass *cl, Object *obj, struct opGet *msg)
286 struct MUI_CountryData *data = INST_DATA(cl, obj);
287 struct CountryEntry *entry;
288 ULONG rc;
289 ULONG nr;
290 ULONG i;
293 switch (msg->opg_AttrID)
295 case MA_CountryName:
296 get(data->child, MUIA_List_Active, &nr);
297 rc = -1;
298 i = 0;
299 ForeachNode(&country_list, entry) {
300 if(i==nr)
302 rc = (ULONG)entry->lve.realname;
304 i++;
307 if(rc == -1)
309 *msg->opg_Storage = 0;
310 return FALSE;
312 break;
314 default:
315 return DoSuperMethodA(cl, obj, (Msg)msg);
318 *msg->opg_Storage = rc;
319 return TRUE;
322 /*** Set ******************************************************************/
323 static IPTR Country_Set(struct IClass *cl, Object *obj, struct opSet *msg)
325 struct MUI_CountryData *data = INST_DATA(cl, obj);
326 struct TagItem *tstate, *tag;
327 struct CountryEntry *entry;
328 ULONG update;
329 ULONG nr;
330 ULONG i;
332 tstate = msg->ops_AttrList;
333 update = FALSE;
335 while ((tag = (struct TagItem *) NextTagItem((APTR) &tstate)))
337 switch (tag->ti_Tag)
339 case MA_CountryName:
341 nr = -1;
342 i = 0;
343 ForeachNode(&country_list, entry) {
344 if(!stricmp(entry->lve.realname, (STRPTR)tag->ti_Data))
346 nr=i;
348 i++;
351 if(nr < 0)
353 D(bug("ERROR: [country class] could not find >%s< !?\n",tag->ti_Data));
355 else
357 if(data->filled)
359 nnset(data->child, MUIA_List_Active, nr);
360 update=TRUE;
362 else
364 /* remember */
365 data->active=nr;
368 break;
370 default:
371 return DoSuperMethodA(cl, obj, (Msg)msg);
375 if(update)
377 MUI_Redraw(obj, MADF_DRAWOBJECT);
380 return TRUE;
383 /*** Setup ******************************************************************/
385 BOOPSI_DISPATCHER(IPTR, Country_Dispatcher, cl, obj, msg)
387 switch (msg->MethodID)
389 case OM_NEW: return (IPTR) Country_New(cl, obj, (struct opSet *)msg);
390 case MUIM_Show: return Country_Fill(cl, obj, (struct MUIP_Show *)msg);
391 case OM_GET: return Country_Get(cl, obj, (APTR)msg);
392 case OM_SET: return Country_Set(cl, obj, (APTR)msg);
393 case MUIM_Cleanup: return Country_Cleanup(cl, obj, (struct MUIP_Cleanup *)msg);
394 case OM_DISPOSE: return Country_Dispose(cl, obj, msg);
397 return DoSuperMethodA(cl, obj, msg);
399 BOOPSI_DISPATCHER_END
402 * Class descriptor.
404 const struct __MUIBuiltinClass _MUIP_Country_desc =
406 "Country",
407 MUIC_List,
408 sizeof(struct MUI_CountryData),
409 (void*)Country_Dispatcher
412 void InitCountry()
414 Country_CLASS=MUI_CreateCustomClass(NULL,MUIC_List,NULL,sizeof(struct MUI_CountryData), &Country_Dispatcher);
417 void CleanCountry()
419 if(Country_CLASS)
421 MUI_DeleteCustomClass(Country_CLASS);
422 Country_CLASS=NULL;