add casts to zune macros to silence some warnings
[tangerine.git] / workbench / prefs / locale / main.c
blob7fd33718fc91eb87fc535fe285efd02a0c191229
1 /*
2 Copyright © 1995-2004, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 /*********************************************************************************************/
8 #include "global.h"
9 #include "registertab.h"
11 #if USE_SHARED_COOLIMAGES
12 #include <libraries/coolimages.h>
13 #include <proto/coolimages.h>
14 #else
15 #include <linklibs/coolimages.h>
16 #endif
18 #include <stdlib.h> /* for exit() */
19 #include <stdio.h>
20 #include <string.h>
22 /*********************************************************************************************/
24 #define ARG_TEMPLATE "FROM,EDIT/S,USE/S,SAVE/S,MAP/K,PUBSCREEN/K"
26 #define ARG_FROM 0
27 #define ARG_EDIT 1
28 #define ARG_USE 2
29 #define ARG_SAVE 3
30 #define ARG_MAP 4
31 #define ARG_PUBSCREEN 5
33 #define NUM_ARGS 6
35 /*********************************************************************************************/
37 static struct libinfo
39 APTR var;
40 STRPTR name;
41 WORD version;
42 BOOL required;
44 libtable[] =
46 {&IntuitionBase , "intuition.library" , 39, TRUE },
47 {&GfxBase , "graphics.library" , 40, TRUE }, /* 40, because of WriteChunkyPixels */
48 {&GadToolsBase , "gadtools.library" , 39, TRUE },
49 {&UtilityBase , "utility.library" , 39, TRUE },
50 {&IFFParseBase , "iffparse.library" , 39, TRUE },
51 {&CyberGfxBase , "cybergraphics.library", 39, FALSE },
52 {&DataTypesBase , "datatypes.library" , 39, FALSE },
53 #if USE_SHARED_COOLIMAGES
54 {&CoolImagesBase , "coolimages.library" , 1, TRUE },
55 #endif
56 {NULL }
59 /*********************************************************************************************/
61 #define NUM_PAGES 3
63 static struct page
65 LONG nameid;
66 LONG (*handler)(LONG, IPTR);
67 LONG minw;
68 LONG minh;
70 pagetable[NUM_PAGES] =
72 {MSG_GAD_TAB_LANGUAGE, page_language_handler},
73 {MSG_GAD_TAB_COUNTRY , page_country_handler },
74 {MSG_GAD_TAB_TIMEZONE, page_timezone_handler}
77 /*********************************************************************************************/
79 #define NUM_BUTTONS 3
81 static struct button
83 LONG nameid;
84 const struct CoolImage *image;
85 struct Gadget *gad;
87 buttontable[NUM_BUTTONS] =
89 #if USE_SHARED_COOLIMAGES
90 {MSG_GAD_SAVE , (const struct CoolImage *)COOL_SAVEIMAGE_ID },
91 {MSG_GAD_USE , (const struct CoolImage *)COOL_DOTIMAGE_ID },
92 {MSG_GAD_CANCEL, (const struct CoolImage *)COOL_CANCELIMAGE_ID}
93 #else
94 {MSG_GAD_SAVE , &cool_saveimage },
95 {MSG_GAD_USE , &cool_dotimage },
96 {MSG_GAD_CANCEL, &cool_cancelimage}
97 #endif
100 /*********************************************************************************************/
102 static struct RegisterTabItem regitems[NUM_PAGES + 1];
103 static struct RegisterTab reg;
104 static struct RDArgs *myargs;
105 static WORD activetab;
106 static IPTR args[NUM_ARGS];
108 /*********************************************************************************************/
110 static void CloseLibs(void);
111 static void FreeArguments(void);
112 static void FreeVisual(void);
113 static void KillPages(void);
114 static void KillWin(void);
115 static void KillGadgets(void);
117 /*********************************************************************************************/
119 WORD ShowMessage(CONST_STRPTR title, CONST_STRPTR text, CONST_STRPTR gadtext)
121 struct EasyStruct es;
123 es.es_StructSize = sizeof(es);
124 es.es_Flags = 0;
125 es.es_Title = title;
126 es.es_TextFormat = text;
127 es.es_GadgetFormat = gadtext;
129 return EasyRequestArgs(win, &es, NULL, NULL);
132 /*********************************************************************************************/
134 void Cleanup(CONST_STRPTR msg)
136 if (msg)
138 if (IntuitionBase && !((struct Process *)FindTask(NULL))->pr_CLI)
140 ShowMessage("Locale", msg, MSG(MSG_OK));
141 } else {
142 printf("Locale: %s\n", msg);
146 KillWin();
147 KillGadgets();
148 KillPages();
149 KillMenus();
150 FreeVisual();
151 CleanupPrefs();
152 FreeArguments();
153 CloseLibs();
154 CleanupLocale();
156 exit(prog_exitcode);
160 /*********************************************************************************************/
162 static void OpenLibs(void)
164 struct libinfo *li;
166 for(li = libtable; li->var; li++)
168 if (!((*(struct Library **)li->var) = OpenLibrary(li->name, li->version)))
170 if (li->required)
172 sprintf(s, MSG(MSG_CANT_OPEN_LIB), li->name, li->version);
173 Cleanup(s);
180 /*********************************************************************************************/
182 static void CloseLibs(void)
184 struct libinfo *li;
186 for(li = libtable; li->var; li++)
188 if (*(struct Library **)li->var) CloseLibrary((*(struct Library **)li->var));
192 /*********************************************************************************************/
194 static void GetArguments(void)
196 if (!(myargs = ReadArgs(ARG_TEMPLATE, args, NULL)))
198 Fault(IoErr(), 0, s, 256);
199 Cleanup(s);
202 if (!args[ARG_FROM]) args[ARG_FROM] = (IPTR)CONFIGNAME_ENV;
205 /*********************************************************************************************/
207 static void FreeArguments(void)
209 if (myargs) FreeArgs(myargs);
212 /*********************************************************************************************/
214 static void GetVisual(void)
216 scr = LockPubScreen((CONST_STRPTR)args[ARG_PUBSCREEN]);
217 if (!scr) Cleanup(MSG(MSG_CANT_LOCK_SCR));
219 dri = GetScreenDrawInfo(scr);
220 if (!dri) Cleanup(MSG(MSG_CANT_GET_DRI));
222 vi = GetVisualInfoA(scr, NULL);
223 if (!vi) Cleanup(MSG(MSG_CANT_GET_VI));
225 if (CyberGfxBase)
227 truecolor = GetBitMapAttr(scr->RastPort.BitMap, BMA_DEPTH) >= 15;
231 /*********************************************************************************************/
233 static void FreeVisual(void)
235 if (vi) FreeVisualInfo(vi);
236 if (dri) FreeScreenDrawInfo(scr, dri);
237 if (scr) UnlockPubScreen(NULL, scr);
240 /*********************************************************************************************/
242 static void MakePages(void)
244 static const struct CoolImage *tabimages[] =
246 #if USE_SHARED_COOLIMAGES
247 (const struct CoolImage *)COOL_HEADIMAGE_ID,
248 (const struct CoolImage *)COOL_FLAGIMAGE_ID,
249 (const struct CoolImage *)COOL_CLOCKIMAGE_ID
250 #else
251 &cool_headimage,
252 &cool_flagimage,
253 &cool_clockimage
254 #endif
256 ULONG bgcol = 0;
257 WORD i;
258 BOOL cool_imageclass_ok = FALSE;
260 if (truecolor)
262 #if USE_SHARED_COOLIMAGES
263 cool_imageclass_ok = (CoolImagesBase != NULL);
264 #else
265 cool_imageclass_ok = InitCoolImageClass(CyberGfxBase);
266 #endif
267 if (cool_imageclass_ok)
269 ULONG col[3];
271 GetRGB32(scr->ViewPort.ColorMap,
272 dri->dri_Pens[BACKGROUNDPEN],
274 col);
276 bgcol = ((col[0] & 0xFF000000) >> 8) +
277 ((col[1] & 0xFF000000) >> 16) +
278 ((col[2] & 0xFF000000) >> 24);
283 for(i = 0; i < NUM_PAGES; i++)
285 regitems[i].text = MSG(pagetable[i].nameid);
286 if (cool_imageclass_ok)
288 #if USE_SHARED_COOLIMAGES
289 tabimages[i] = (const struct CoolImage *)COOL_ObtainImageA((ULONG)tabimages[i], NULL);
291 regitems[i].image = NewObject(NULL, COOLIMAGECLASS, IA_Width , tabimages[i]->width ,
292 IA_Height , tabimages[i]->height,
293 COOLIM_CoolImage , (IPTR)tabimages[i] ,
294 COOLIM_BgColor , bgcol ,
295 TAG_DONE);
296 #else
297 regitems[i].image = NewObject(cool_imageclass, NULL, IA_Width , tabimages[i]->width ,
298 IA_Height , tabimages[i]->height,
299 COOLIM_CoolImage, (IPTR)tabimages[i] ,
300 COOLIM_BgColor , bgcol ,
301 TAG_DONE);
302 #endif
304 if (!(pagetable[i].handler(PAGECMD_INIT, 0)))
306 Cleanup(MSG(MSG_CANT_CREATE_GADGET));
310 InitRegisterTab(&reg, regitems);
314 /*********************************************************************************************/
316 static void KillPages(void)
318 WORD i;
320 for(i = 0; i < NUM_PAGES; i++)
322 pagetable[i].handler(PAGECMD_CLEANUP, 0);
323 if (regitems[i].image) DisposeObject(regitems[i].image);
326 #if !USE_SHARED_COOLIMAGES
327 CleanupCoolImageClass();
328 #endif
331 /*********************************************************************************************/
333 static void LayoutButtons(void)
335 struct RastPort temprp;
336 WORD i, w, maxtextlen = 0, maximheight = 0;
338 InitRastPort(&temprp);
339 SetFont(&temprp, dri->dri_Font);
341 for(i = 0; i < 3; i++)
343 #if USE_SHARED_COOLIMAGES
344 buttontable[i].image = (const struct CoolImage *)COOL_ObtainImageA((ULONG)buttontable[i].image, NULL);
346 #endif
348 w = TextLength(&temprp, MSG(buttontable[i].nameid), strlen(MSG(buttontable[i].nameid)));
349 if (truecolor)
351 if (buttontable[i].image->height > maximheight)
352 maximheight = buttontable[i].image->height;
354 w += IMBUTTON_EXTRAWIDTH + buttontable[i].image->width;
356 else
358 buttontable[i].image = NULL;
360 if (w > maxtextlen) maxtextlen = w;
364 buttonwidth = w + BUTTON_EXTRAWIDTH;
365 buttonheight = dri->dri_Font->tf_YSize + BUTTON_EXTRAHEIGHT;
367 if (truecolor)
369 maximheight += IMBUTTON_EXTRAHEIGHT;
370 if (maximheight > buttonheight) buttonheight = maximheight;
373 DeinitRastPort(&temprp);
376 /*********************************************************************************************/
378 static void LayoutGUI(void)
380 WORD w, max_pagewidth = 0;
381 WORD h, max_pageheight = 0;
382 WORD i;
384 LayoutButtons();
386 for(i = 0; i < NUM_PAGES; i++)
388 if (!(pagetable[i].handler(PAGECMD_LAYOUT, 0)))
389 Cleanup(MSG(MSG_CANT_CREATE_GADGET));
391 w = pagetable[i].handler(PAGECMD_GETMINWIDTH, 0);
392 h = pagetable[i].handler(PAGECMD_GETMINHEIGHT, 0);
394 if (w > max_pagewidth) max_pagewidth = w;
395 if (h > max_pageheight) max_pageheight = h;
398 LayoutRegisterTab(&reg, scr, dri, TRUE);
399 if (reg.width > max_pagewidth) max_pagewidth = reg.width;
401 i = buttonwidth * NUM_BUTTONS + SPACE_X * (NUM_BUTTONS - 1) - TABBORDER_X * 2;
402 if (i > max_pagewidth) max_pagewidth = i;
404 SetRegisterTabPos(&reg, scr->WBorLeft + BORDER_X, scr->WBorTop + scr->Font->ta_YSize + 1 + BORDER_Y);
406 pages_left = scr->WBorLeft + BORDER_X + TABBORDER_X ;
407 pages_top = scr->WBorTop + scr->Font->ta_YSize + 1 + BORDER_Y + reg.height + TABBORDER_Y;
408 pages_width = max_pagewidth;
409 pages_height = max_pageheight;
411 SetRegisterTabFrameSize(&reg, pages_width + TABBORDER_X * 2,
412 pages_height + TABBORDER_Y * 2);
414 for(i = 0; i < NUM_PAGES; i++)
416 pagetable[i].handler(PAGECMD_SETDOMLEFT , pages_left );
417 pagetable[i].handler(PAGECMD_SETDOMTOP , pages_top );
418 pagetable[i].handler(PAGECMD_SETDOMWIDTH , max_pagewidth );
419 pagetable[i].handler(PAGECMD_SETDOMHEIGHT, max_pageheight);
422 winwidth = pages_width + TABBORDER_X * 2 + BORDER_X * 2;
423 winheight = pages_height + buttonheight + SPACE_Y + reg.height + TABBORDER_Y * 2 + BORDER_Y * 2;
426 /*********************************************************************************************/
428 static void MakeGadgets(void)
430 WORD x = scr->WBorLeft + BORDER_X;
431 WORD y = scr->WBorTop + scr->Font->ta_YSize + 1 + winheight - BORDER_Y - buttonheight;
432 WORD spacex;
433 WORD i;
434 struct TagItem tags[] =
436 {GA_Left , 0 },
437 {GA_Top , y },
438 {GA_Width , buttonwidth },
439 {GA_Height , buttonheight },
440 {GA_Text , 0 },
441 {GA_ID , 0 },
442 {GA_Previous , 0 },
443 {COOLBT_CoolImage , 0 },
444 {GA_RelVerify , TRUE },
445 {TAG_DONE }
448 #if USE_SHARED_COOLIMAGES
449 if (!CoolImagesBase)
450 Cleanup(MSG(MSG_CANT_CREATE_GADGET));
451 #else
452 if (!InitCoolButtonClass(CyberGfxBase))
453 Cleanup(MSG(MSG_CANT_CREATE_GADGET));
454 #endif
456 spacex = (pages_width + TABBORDER_X * 2 - buttonwidth * NUM_BUTTONS) * 16 / (NUM_BUTTONS - 1);
458 for(i = 0; i < NUM_BUTTONS; i++)
460 if (i == NUM_BUTTONS - 1)
462 tags[0].ti_Data = x + pages_width + TABBORDER_X * 2 - buttonwidth;
464 else
466 tags[0].ti_Data = x + (buttonwidth * 16 + spacex) * i / 16;
469 tags[4].ti_Data = (IPTR)MSG(buttontable[i].nameid);
470 tags[5].ti_Data = buttontable[i].nameid;
471 tags[7].ti_Data = (IPTR)buttontable[i].image;
473 if (i > 0) tags[6].ti_Data = (IPTR)buttontable[i - 1].gad;
475 #if USE_SHARED_COOLIMAGES
476 buttontable[i].gad = NewObjectA(NULL, COOLBUTTONGCLASS, tags);
477 #else
478 buttontable[i].gad = NewObjectA(cool_buttonclass, NULL, tags);
479 #endif
481 if (!buttontable[i].gad) Cleanup(MSG(MSG_CANT_CREATE_GADGET));
485 for(i = 0; i < NUM_PAGES; i++)
487 if (!(pagetable[i].handler(PAGECMD_MAKEGADGETS, 0)))
488 Cleanup(MSG(MSG_CANT_CREATE_GADGET));
492 /*********************************************************************************************/
494 static void KillGadgets(void)
496 WORD i;
498 for(i = 0; i < 3; i++)
500 if (buttontable[i].gad) DisposeObject((Object *)buttontable[i].gad);
503 #if !USE_SHARED_COOLIMAGES
504 CleanupCoolButtonClass();
505 #endif
508 /*********************************************************************************************/
510 static void ActivatePage(WORD which)
512 if (which == activetab) return;
514 if (activetab >= 0)
516 pagetable[activetab].handler(PAGECMD_REMGADGETS, 0);
519 SetDrMd(win->RPort, JAM1),
520 SetAPen(win->RPort, dri->dri_Pens[BACKGROUNDPEN]);
521 RectFill(win->RPort, pages_left, pages_top, pages_left + pages_width - 1, pages_top + pages_height - 1);
523 activetab = which;
525 pagetable[activetab].handler(PAGECMD_ADDGADGETS, 0);
530 /*********************************************************************************************/
532 static void MakeWin(void)
534 static struct IBox zoom;
535 WORD w, t, h, wx, wy;
537 w = winwidth + scr->WBorLeft + scr->WBorRight;
538 t = scr->WBorTop + scr->Font->ta_YSize + 1;
539 h = winheight + t + scr->WBorBottom;
541 wx = (scr->Width - w) / 2;
542 wy = (scr->Height - h) / 2;
544 zoom.Left = -1;
545 zoom.Top = -1;
546 zoom.Width = w;
547 zoom.Height = t;
549 win = OpenWindowTags(0, WA_PubScreen , (IPTR)scr ,
550 WA_Left , wx ,
551 WA_Top , wy ,
552 WA_InnerWidth , winwidth ,
553 WA_InnerHeight , winheight ,
554 WA_Title , (IPTR)MSG(MSG_WINTITLE) ,
555 WA_CloseGadget , TRUE ,
556 WA_DragBar , TRUE ,
557 WA_DepthGadget , TRUE ,
558 WA_Activate , TRUE ,
559 WA_Gadgets , (IPTR)buttontable[0].gad ,
560 WA_NewLookMenus , TRUE ,
561 WA_Zoom , (IPTR)&zoom ,
562 WA_IDCMP , REGISTERTAB_IDCMP |
563 BUTTONIDCMP |
564 LISTVIEWIDCMP |
565 IDCMP_CLOSEWINDOW |
566 IDCMP_VANILLAKEY |
567 IDCMP_RAWKEY |
568 IDCMP_MENUPICK |
569 IDCMP_REFRESHWINDOW ,
570 TAG_DONE);
572 SetMenuStrip(win, menus);
574 RenderRegisterTab(win->RPort, &reg, TRUE);
576 activetab = -1;
577 ActivatePage(0);
580 /*********************************************************************************************/
582 static void KillWin(void)
584 pagetable[reg.active].handler(PAGECMD_REMGADGETS, 0);
586 if (win)
588 RemoveGList(win, buttontable[0].gad, NUM_BUTTONS);
589 ClearMenuStrip(win);
590 CloseWindow(win);
591 win = NULL;
595 /*********************************************************************************************/
597 void TellGUI(LONG cmd)
599 WORD i;
601 for(i = 0; i < NUM_PAGES; i++)
603 pagetable[i].handler(cmd, 0);
607 /*********************************************************************************************/
609 static void HandleAll(void)
611 struct IntuiMessage *msg;
612 struct MenuItem *item;
613 struct Gadget *gad;
614 UWORD men;
615 BOOL quitme = FALSE;
617 while (!quitme)
619 WaitPort(win->UserPort);
621 while((msg = GT_GetIMsg(win->UserPort)))
623 if (HandleRegisterTabInput(&reg, msg))
625 ActivatePage(reg.active);
627 else if (pagetable[activetab].handler(PAGECMD_HANDLEINPUT, (IPTR)msg))
630 else switch (msg->Class)
632 case IDCMP_CLOSEWINDOW:
633 quitme = TRUE;
634 break;
636 case IDCMP_REFRESHWINDOW:
637 GT_BeginRefresh(win);
639 GT_RefreshWindow(win, NULL);
640 RenderRegisterTab(win->RPort, &reg, TRUE);
641 pagetable[activetab].handler(PAGECMD_REFRESH, 0);
643 GT_EndRefresh(win, TRUE);
644 break;
646 case IDCMP_VANILLAKEY:
647 switch(msg->Code)
649 case 27: /* ESC */
650 quitme = TRUE;
651 break;
653 } /* switch(msg->Code) */
654 break;
656 case IDCMP_GADGETUP:
657 gad = (struct Gadget *)msg->IAddress;
658 switch(gad->GadgetID)
660 case MSG_GAD_SAVE:
661 if (!SavePrefs(CONFIGNAME_ENVARC)) break;
662 /* fall through */
664 case MSG_GAD_USE:
665 if (!SavePrefs(CONFIGNAME_ENV)) break;
666 /* fall through */
668 case MSG_GAD_CANCEL:
669 quitme = TRUE;
670 break;
673 break;
675 case IDCMP_MENUPICK:
676 men = msg->Code;
677 while(men != MENUNULL)
679 if ((item = ItemAddress(menus, men)))
681 STRPTR filename;
683 switch((ULONG)GTMENUITEM_USERDATA(item))
685 case MSG_MEN_PROJECT_OPEN:
686 if ((filename = GetFile(MSG(MSG_ASL_OPEN_TITLE), "SYS:Prefs/Presets", FALSE)))
688 LoadPrefs(filename);
690 break;
692 case MSG_MEN_PROJECT_SAVEAS:
693 if ((filename = GetFile(MSG(MSG_ASL_SAVE_TITLE), "SYS:Prefs/Presets", TRUE)))
695 SavePrefs(filename);
697 break;
699 case MSG_MEN_PROJECT_QUIT:
700 quitme = TRUE;
701 break;
703 case MSG_MEN_EDIT_DEFAULT:
704 DefaultPrefs();
705 break;
707 case MSG_MEN_EDIT_LASTSAVED:
708 LoadPrefs(CONFIGNAME_ENVARC);
709 break;
711 case MSG_MEN_EDIT_RESTORE:
712 RestorePrefs();
713 break;
715 case MSG_MEN_SETTINGS_CREATEICONS:
716 break;
718 } /* switch(GTMENUITEM_USERDATA(item)) */
720 men = item->NextSelect;
721 } else {
722 men = MENUNULL;
725 } /* while(men != MENUNULL) */
726 break;
728 } /* else switch (msg->Class) */
730 GT_ReplyIMsg(msg);
732 } /* while((msg = GT_GetIMsg(win->UserPort))) */
734 } /* while (!quitme) */
737 /*********************************************************************************************/
739 int main(void)
741 InitLocale("System/Prefs/Locale.catalog", 1);
742 InitMenus();
743 OpenLibs();
744 GetArguments();
745 InitPrefs((STRPTR)args[ARG_FROM], (args[ARG_USE] ? TRUE : FALSE), (args[ARG_SAVE] ? TRUE : FALSE));
746 GetVisual();
747 MakeMenus();
748 MakePages();
749 LayoutGUI();
750 MakeGadgets();
751 MakeWin();
752 HandleAll();
753 Cleanup(NULL);
755 return 0;
758 /*********************************************************************************************/