2 Copyright © 1995-2009, The AROS Development Team. All rights reserved.
6 /*********************************************************************************************/
11 #include <libraries/coolimages.h>
12 #include <aros/debug.h>
14 #include <zune/clock.h>
15 #include <zune/calendar.h>
17 #include <stdlib.h> /* for exit() */
21 /*********************************************************************************************/
23 #define ARG_TEMPLATE "FROM,EDIT/S,USE/S,SAVE/S,PUBSCREEN/K"
29 #define ARG_PUBSCREEN 4
33 #define DO_SPECIAL_BUTTON_LAYOUT 1 /* all button get same width, but as small as possible,
34 and spread along whole parent group */
36 #define SPACING_BUTTONGROUP 4
38 #define RETURNID_USE 1
39 #define RETURNID_SAVE 2
41 /*********************************************************************************************/
52 {&IntuitionBase
, "intuition.library" , 39, TRUE
},
53 {&GfxBase
, "graphics.library" , 40, TRUE
}, /* 40, because of WriteChunkyPixels */
54 {&UtilityBase
, "utility.library" , 39, TRUE
},
55 {&MUIMasterBase
, "muimaster.library" , 0 , TRUE
},
59 /*********************************************************************************************/
61 static struct Hook yearhook
, clockhook
, activehook
, restorehook
;
62 #if DO_SPECIAL_BUTTON_LAYOUT
63 static struct Hook buttonlayouthook
;
65 static struct RDArgs
*myargs
;
66 static Object
*activetimestrobj
;
67 static IPTR args
[NUM_ARGS
];
69 static STRPTR monthlabels
[] =
86 /*********************************************************************************************/
88 static void CloseLibs(void);
89 static void CloseTimerDev(void);
90 static void FreeArguments(void);
91 static void FreeVisual(void);
92 static void KillGUI(void);
94 /*********************************************************************************************/
96 WORD
ShowMessage(STRPTR title
, STRPTR text
, STRPTR gadtext
)
100 es
.es_StructSize
= sizeof(es
);
103 es
.es_TextFormat
= text
;
104 es
.es_GadgetFormat
= gadtext
;
106 return EasyRequestArgs(NULL
, &es
, NULL
, NULL
);
109 /*********************************************************************************************/
111 void Cleanup(STRPTR msg
)
115 if (IntuitionBase
&& !((struct Process
*)FindTask(NULL
))->pr_CLI
)
117 ShowMessage("Time", msg
, MSG(MSG_OK
));
121 printf("Time: %s\n", msg
);
136 /*********************************************************************************************/
138 static void OpenLibs(void)
142 for(li
= libtable
; li
->var
; li
++)
144 if (!((*(struct Library
**)li
->var
) = OpenLibrary(li
->name
, li
->version
)))
148 sprintf(s
, MSG(MSG_CANT_OPEN_LIB
), li
->name
, li
->version
);
156 /*********************************************************************************************/
158 static void CloseLibs(void)
162 for(li
= libtable
; li
->var
; li
++)
164 if (*(struct Library
**)li
->var
) CloseLibrary((*(struct Library
**)li
->var
));
168 /*********************************************************************************************/
170 static void OpenTimerDev(void)
172 if ((TimerMP
= CreateMsgPort()))
174 if ((TimerIO
= (struct timerequest
*)CreateIORequest(TimerMP
, sizeof(struct timerequest
))))
176 if (!OpenDevice("timer.device", UNIT_VBLANK
, (struct IORequest
*)TimerIO
, 0))
178 TimerBase
= (struct Device
*)TimerIO
->tr_node
.io_Device
;
185 sprintf(s
, MSG(MSG_CANT_OPEN_LIB
), "timer.device", 0);
190 /*********************************************************************************************/
192 static void CloseTimerDev(void)
196 CloseDevice((struct IORequest
*)TimerIO
);
197 DeleteIORequest((struct IORequest
*)TimerIO
);
202 DeleteMsgPort(TimerMP
);
206 /*********************************************************************************************/
208 static void OpenBattClockRes(void)
210 BattClockBase
= OpenResource("battclock.resource");
214 sprintf(s
, MSG(MSG_CANT_OPEN_LIB
), "battclock.resource", 0);
219 /*********************************************************************************************/
221 static void GetArguments(void)
223 if (!(myargs
= ReadArgs(ARG_TEMPLATE
, args
, NULL
)))
225 Fault(IoErr(), 0, s
, 256);
229 // if (!args[ARG_FROM]) args[ARG_FROM] = (IPTR)CONFIGNAME_ENV;
232 /*********************************************************************************************/
234 static void FreeArguments(void)
236 if (myargs
) FreeArgs(myargs
);
239 /*********************************************************************************************/
241 static void GetVisual(void)
245 /*********************************************************************************************/
247 static void FreeVisual(void)
251 /*********************************************************************************************/
253 #if DO_SPECIAL_BUTTON_LAYOUT
255 /*********************************************************************************************/
257 static ULONG
ButtonLayoutFunc(struct Hook
*hook
, Object
*obj
, struct MUI_LayoutMsg
*msg
)
259 IPTR retval
= MUILM_UNKNOWN
;
265 Object
*cstate
= (Object
*)msg
->lm_Children
->mlh_Head
;
268 WORD maxminwidth
= 0;
269 WORD maxminheight
= 0;
272 while((child
= NextObject(&cstate
)))
274 if (_minwidth(child
) > maxminwidth
) maxminwidth
= _minwidth(child
);
275 if (_minheight(child
) > maxminheight
) maxminheight
= _minheight(child
);
279 msg
->lm_MinMax
.MinWidth
=
280 msg
->lm_MinMax
.DefWidth
= numchilds
* maxminwidth
+ SPACING_BUTTONGROUP
* (numchilds
- 1);
281 msg
->lm_MinMax
.MaxWidth
= MUI_MAXMAX
;
283 msg
->lm_MinMax
.MinHeight
=
284 msg
->lm_MinMax
.DefHeight
=
285 msg
->lm_MinMax
.MaxHeight
= maxminheight
;
292 Object
*cstate
= (Object
*)msg
->lm_Children
->mlh_Head
;
295 WORD maxminwidth
= 0;
296 WORD maxminheight
= 0;
300 while((child
= NextObject(&cstate
)))
302 if (_minwidth(child
) > maxminwidth
) maxminwidth
= _minwidth(child
);
303 if (_minheight(child
) > maxminheight
) maxminheight
= _minheight(child
);
307 cstate
= (Object
*)msg
->lm_Children
->mlh_Head
;
308 while((child
= NextObject(&cstate
)))
310 if (i
== numchilds
-1)
312 x
= msg
->lm_Layout
.Width
- maxminwidth
;
316 x
= maxminwidth
+ (msg
->lm_Layout
.Width
- numchilds
* maxminwidth
) / (numchilds
- 1);
319 MUI_Layout(child
, x
, 0, maxminwidth
, maxminheight
, 0);
328 } /* switch(msg->lm_Type) */
333 /*********************************************************************************************/
335 #endif /* DO_SPECIAL_BUTTON_LAYOUT */
337 /*********************************************************************************************/
339 static void YearFunc(struct Hook
*hook
, Object
*obj
, IPTR
*param
)
343 get(obj
, MUIA_String_Integer
, &year
);
345 if ((LONG
)*param
== -1)
347 else if ((LONG
)*param
== 1)
353 nnset(obj
, MUIA_String_Integer
, year
);
355 else if (year
> 2099)
358 nnset(obj
, MUIA_String_Integer
, year
);
362 nnset(obj
, MUIA_String_Integer
, year
);
365 nnset(calobj
, MUIA_Calendar_Year
, year
);
369 /*********************************************************************************************/
371 static void ActiveFunc(struct Hook
*hook
, Object
*obj
, IPTR
*param
)
376 active
= *(Object
**)param
;
378 if (active
== hourobj
)
380 hand
= MUIV_Clock_EditHand_Hour
;
382 else if (active
== minobj
)
384 hand
= MUIV_Clock_EditHand_Minute
;
386 else if (active
== secobj
)
388 hand
= MUIV_Clock_EditHand_Second
;
393 activetimestrobj
= active
;
394 set(clockobj
, MUIA_Clock_EditHand
, hand
);
399 /*********************************************************************************************/
401 static void ClockFunc(struct Hook
*hook
, Object
*obj
, IPTR
*param
)
403 struct ClockData
*cd
;
406 get(obj
, MUIA_Clock_Time
, &cd
);
410 sprintf(s
, "%02d", cd
->hour
);
411 nnset(hourobj
, MUIA_String_Contents
, s
);
413 sprintf(s
, "%02d", cd
->min
);
414 nnset(minobj
, MUIA_String_Contents
, s
);
416 sprintf(s
, "%02d", cd
->sec
);
417 nnset(secobj
, MUIA_String_Contents
, s
);
421 struct ClockData cd2
;
422 LONG diff
= (LONG
)*param
;
424 UWORD
*cd2_member
= NULL
;
426 if (diff
== 100) diff
= 0; /* 100 means string gadget acknowledge */
428 if (activetimestrobj
== hourobj
)
431 cd2_member
= &cd2
.hour
;
433 else if (activetimestrobj
== minobj
)
436 cd2_member
= &cd2
.min
;
438 else if (activetimestrobj
== secobj
)
441 cd2_member
= &cd2
.sec
;
448 set(obj
, MUIA_Clock_Frozen
, TRUE
);
449 get(obj
, MUIA_Clock_Time
, &cd
);
453 get(activetimestrobj
, MUIA_String_Integer
, &number
);
457 if ((LONG
)number
< 0)
461 else if ((LONG
)number
> max
)
465 *cd2_member
= number
;
467 sprintf(s
, "%02ld", number
);
469 nnset(activetimestrobj
, MUIA_String_Contents
, s
);
471 set(obj
, MUIA_Clock_Time
, (IPTR
)&cd2
);
477 /*********************************************************************************************/
479 static void RestoreFunc(struct Hook
*hook
, Object
*obj
, APTR msg
)
483 set(calobj
, MUIA_Calendar_Date
, (IPTR
)&clockdata
);
484 set(monthobj
, MUIA_Cycle_Active
, clockdata
.month
- 1);
485 set(yearobj
, MUIA_String_Integer
, clockdata
.year
);
486 set(clockobj
, MUIA_Clock_Frozen
, FALSE
);
490 /*********************************************************************************************/
492 static void MakeGUI(void)
494 extern struct NewMenu nm
;
496 Object
*menu
, *yearaddobj
, *yearsubobj
, *timeaddobj
, *timesubobj
;
497 Object
*saveobj
, *useobj
, *cancelobj
;
499 #if DO_SPECIAL_BUTTON_LAYOUT
500 buttonlayouthook
.h_Entry
= HookEntry
;
501 buttonlayouthook
.h_SubEntry
= (HOOKFUNC
)ButtonLayoutFunc
;
503 yearhook
.h_Entry
= HookEntry
;
504 yearhook
.h_SubEntry
= (HOOKFUNC
)YearFunc
;
506 clockhook
.h_Entry
= HookEntry
;
507 clockhook
.h_SubEntry
= (HOOKFUNC
)ClockFunc
;
509 activehook
.h_Entry
= HookEntry
;
510 activehook
.h_SubEntry
= (HOOKFUNC
)ActiveFunc
;
512 restorehook
.h_Entry
= HookEntry
;
513 restorehook
.h_SubEntry
= (HOOKFUNC
)RestoreFunc
;
517 struct Locale
*locale
= OpenLocale(NULL
);
523 for(i
= 0; i
< 12; i
++)
525 monthlabels
[i
] = GetLocaleStr(locale
, MON_1
+ i
);
533 menu
= MUI_MakeObject(MUIO_MenustripNM
, &nm
, 0);
535 app
= ApplicationObject
,
536 MUIA_Application_Title
, (IPTR
)"Time",
537 MUIA_Application_Version
, (IPTR
)VERSIONSTR
,
538 MUIA_Application_Copyright
, (IPTR
)"Copyright © 1995-2002, The AROS Development Team",
539 MUIA_Application_Author
, (IPTR
)"The AROS Development Team",
540 MUIA_Application_Description
, (IPTR
)MSG(MSG_WINTITLE
),
541 MUIA_Application_Base
, (IPTR
)"Time",
542 menu
? MUIA_Application_Menustrip
: TAG_IGNORE
, menu
,
543 SubWindow
, wnd
= WindowObject
,
544 MUIA_Window_Title
, (IPTR
)MSG(MSG_WINTITLE
),
545 MUIA_Window_ID
, MAKE_ID('T','W','I','N'),
546 WindowContents
, VGroup
,
547 Child
, HGroup
, /* Group containing calendar box and clock box */
548 MUIA_Group_SameWidth
, TRUE
,
549 Child
, VGroup
, /* Calendar box */
551 MUIA_Background
, MUII_GroupBack
,
552 Child
, HGroup
, /* Month/year row */
553 Child
, monthobj
= MUI_MakeObject(MUIO_Cycle
, NULL
, monthlabels
),
555 Child
, yearsubobj
= TextObject
, /* year [-] gadget */
557 MUIA_Background
, MUII_ButtonBack
,
558 MUIA_CycleChain
, TRUE
,
559 MUIA_Font
, MUIV_Font_Button
,
560 MUIA_InputMode
, MUIV_InputMode_RelVerify
,
561 MUIA_Text_Contents
, "\033c-",
562 MUIA_FixWidthTxt
, (IPTR
)"+",
564 Child
, yearobj
= StringObject
, /* year gadget */
566 MUIA_CycleChain
, TRUE
,
567 MUIA_String_Accept
, (IPTR
)"0123456789",
568 MUIA_FixWidthTxt
, (IPTR
)"55555",
570 Child
, yearaddobj
= TextObject
, /* year [-] gadget */
572 MUIA_Background
, MUII_ButtonBack
,
573 MUIA_CycleChain
, TRUE
,
574 MUIA_Font
, MUIV_Font_Button
,
575 MUIA_InputMode
, MUIV_InputMode_RelVerify
,
576 MUIA_Text_Contents
, "\033c+",
577 MUIA_FixWidthTxt
, (IPTR
)"-",
580 Child
, calobj
= CalendarObject
,
581 MUIA_CycleChain
, TRUE
,
584 Child
, VGroup
, /* Clock box */
586 MUIA_Background
, MUII_GroupBack
,
587 Child
, clockobj
= ClockObject
,
594 MUIA_Group_Spacing
, 0,
595 Child
, TextObject
, /* phantom time [-] gadget */
597 MUIA_Background
, MUII_ButtonBack
,
598 MUIA_Font
, MUIV_Font_Button
,
599 MUIA_InputMode
, MUIV_InputMode_RelVerify
,
600 MUIA_Text_Contents
, "\033c-",
601 MUIA_FixWidthTxt
, (IPTR
)"+",
603 Child
, TextObject
, /* phantom time [+] gadget */
605 MUIA_Background
, MUII_ButtonBack
,
606 MUIA_Font
, MUIV_Font_Button
,
607 MUIA_InputMode
, MUIV_InputMode_RelVerify
,
608 MUIA_Text_Contents
, "\033c+",
609 MUIA_FixWidthTxt
, (IPTR
)"-",
613 Child
, hourobj
= StringObject
, /* hour gadget */
615 MUIA_CycleChain
, TRUE
,
616 MUIA_String_Accept
, (IPTR
)"0123456789",
617 MUIA_FixWidthTxt
, (IPTR
)"555",
620 Child
, minobj
= StringObject
, /* min gadget */
622 MUIA_CycleChain
, TRUE
,
623 MUIA_String_Accept
, (IPTR
)"0123456789",
624 MUIA_FixWidthTxt
, (IPTR
)"555",
627 Child
, secobj
= StringObject
, /* sec gadget */
629 MUIA_CycleChain
, TRUE
,
630 MUIA_String_Accept
, (IPTR
)"0123456789",
631 MUIA_FixWidthTxt
, (IPTR
)"555",
634 MUIA_Group_Spacing
, 0,
635 Child
, timesubobj
= TextObject
, /* time [-] gadget */
637 MUIA_Background
, MUII_ButtonBack
,
638 MUIA_CycleChain
, TRUE
,
639 MUIA_Font
, MUIV_Font_Button
,
640 MUIA_InputMode
, MUIV_InputMode_RelVerify
,
641 MUIA_Text_Contents
, "\033c-",
642 MUIA_FixWidthTxt
, (IPTR
)"+",
644 Child
, timeaddobj
= TextObject
, /* time [+] gadget */
646 MUIA_Background
, MUII_ButtonBack
,
647 MUIA_CycleChain
, TRUE
,
648 MUIA_Font
, MUIV_Font_Button
,
649 MUIA_InputMode
, MUIV_InputMode_RelVerify
,
650 MUIA_Text_Contents
, "\033c+",
651 MUIA_FixWidthTxt
, (IPTR
)"-",
658 Child
, HGroup
, /* save/use/cancel button row */
659 #if DO_SPECIAL_BUTTON_LAYOUT
660 MUIA_Group_LayoutHook
, (IPTR
)&buttonlayouthook
,
663 MUIA_Group_SameWidth
, TRUE
,
665 Child
, saveobj
= CoolImageIDButton(MSG(MSG_GAD_SAVE
), COOL_SAVEIMAGE_ID
),
666 Child
, useobj
= CoolImageIDButton(MSG(MSG_GAD_USE
), COOL_DOTIMAGE_ID
),
667 Child
, cancelobj
= CoolImageIDButton(MSG(MSG_GAD_CANCEL
), COOL_CANCELIMAGE_ID
),
673 if (!app
) Cleanup(MSG(MSG_CANT_CREATE_APP
));
675 DoMethod(wnd
, MUIM_Notify
, MUIA_Window_CloseRequest
, TRUE
, (IPTR
) app
, 2, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
);
677 DoMethod(cancelobj
, MUIM_Notify
, MUIA_Pressed
, FALSE
, (IPTR
) app
, 2, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
);
678 DoMethod(saveobj
, MUIM_Notify
, MUIA_Pressed
, FALSE
, (IPTR
) app
, 2, MUIM_Application_ReturnID
, RETURNID_SAVE
);
679 DoMethod(useobj
, MUIM_Notify
, MUIA_Pressed
, FALSE
, (IPTR
) app
, 2, MUIM_Application_ReturnID
, RETURNID_USE
);
681 DoMethod(wnd
, MUIM_Notify
, MUIA_Window_MenuAction
, MSG_MEN_PROJECT_QUIT
, (IPTR
) app
, 2, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
);
682 DoMethod(wnd
, MUIM_Notify
, MUIA_Window_MenuAction
, MSG_MEN_EDIT_RESTORE
, (IPTR
) app
, 2, MUIM_CallHook
, (IPTR
)&restorehook
);
684 DoMethod(wnd
, MUIM_Notify
, MUIA_Window_ActiveObject
, MUIV_EveryTime
, (IPTR
) app
, 3, MUIM_CallHook
, (IPTR
)&activehook
, MUIV_TriggerValue
);
685 DoMethod(monthobj
, MUIM_Notify
, MUIA_Cycle_Active
, MUIV_EveryTime
, (IPTR
) calobj
, 3, MUIM_NoNotifySet
, MUIA_Calendar_Month0
, MUIV_TriggerValue
);
686 DoMethod(yearobj
, MUIM_Notify
, MUIA_String_Acknowledge
, MUIV_EveryTime
, (IPTR
) yearobj
, 3, MUIM_CallHook
, (IPTR
)&yearhook
, 0);
687 DoMethod(yearaddobj
, MUIM_Notify
, MUIA_Timer
, MUIV_EveryTime
, (IPTR
) yearobj
, 3, MUIM_CallHook
, (IPTR
)&yearhook
, 1);
688 DoMethod(yearsubobj
, MUIM_Notify
, MUIA_Timer
, MUIV_EveryTime
, (IPTR
) yearobj
, 3, MUIM_CallHook
, (IPTR
)&yearhook
, -1);
689 DoMethod(timeaddobj
, MUIM_Notify
, MUIA_Timer
, MUIV_EveryTime
, (IPTR
) clockobj
, 3, MUIM_CallHook
, (IPTR
)&clockhook
, 1);
690 DoMethod(timesubobj
, MUIM_Notify
, MUIA_Timer
, MUIV_EveryTime
, (IPTR
) clockobj
, 3, MUIM_CallHook
, (IPTR
)&clockhook
, -1);
691 DoMethod(clockobj
, MUIM_Notify
, MUIA_Clock_Ticked
, TRUE
, (IPTR
) clockobj
, 3, MUIM_CallHook
, (IPTR
)&clockhook
, 0);
692 DoMethod(hourobj
, MUIM_Notify
, MUIA_String_Acknowledge
, MUIV_EveryTime
, (IPTR
) clockobj
, 3, MUIM_CallHook
, (IPTR
)&clockhook
, 100);
693 DoMethod(minobj
, MUIM_Notify
, MUIA_String_Acknowledge
, MUIV_EveryTime
, (IPTR
) clockobj
, 3, MUIM_CallHook
, (IPTR
)&clockhook
, 100);
694 DoMethod(secobj
, MUIM_Notify
, MUIA_String_Acknowledge
, MUIV_EveryTime
, (IPTR
) clockobj
, 3, MUIM_CallHook
, (IPTR
)&clockhook
, 100);
696 set(calobj
, MUIA_Calendar_Date
, &clockdata
);
697 set(monthobj
, MUIA_Cycle_Active
, clockdata
.month
- 1);
698 set(yearobj
, MUIA_String_Integer
, clockdata
.year
);
700 CallHook(&clockhook
, clockobj
, 0);
703 /*********************************************************************************************/
705 static void KillGUI(void)
710 /*********************************************************************************************/
712 static void HandleAll(void)
717 set (wnd
, MUIA_Window_Open
, TRUE
);
721 returnid
= (LONG
) DoMethod(app
, MUIM_Application_NewInput
, (IPTR
) &sigs
);
723 if ((returnid
== MUIV_Application_ReturnID_Quit
) ||
724 (returnid
== RETURNID_SAVE
) || (returnid
== RETURNID_USE
)) break;
728 sigs
= Wait(sigs
| SIGBREAKF_CTRL_C
);
729 if (sigs
& SIGBREAKF_CTRL_C
) break;
738 struct ClockData cal_date
, clock_time
, *dateptr
;
741 get(calobj
, MUIA_Calendar_Date
, &dateptr
);
744 get(clockobj
, MUIA_Clock_Frozen
, &frozen
);
747 get(clockobj
, MUIA_Clock_Time
, &dateptr
);
748 clock_time
= *dateptr
;
755 Amiga2Date(tv
.tv_secs
, &clock_time
);
758 clockdata
.sec
= clock_time
.sec
;
759 clockdata
.min
= clock_time
.min
;
760 clockdata
.hour
= clock_time
.hour
;
761 clockdata
.mday
= cal_date
.mday
;
762 clockdata
.month
= cal_date
.month
;
763 clockdata
.year
= cal_date
.year
;
764 clockdata
.wday
= cal_date
.wday
;
766 if (returnid
== RETURNID_SAVE
)
776 /*********************************************************************************************/
780 InitLocale("System/Prefs/Time.catalog", 1);
786 InitPrefs((args
[ARG_USE
] ? TRUE
: FALSE
), (args
[ARG_SAVE
] ? TRUE
: FALSE
));
795 /*********************************************************************************************/