2 Copyright © 1995-2003, 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 GetArguments(void)
210 if (!(myargs
= ReadArgs(ARG_TEMPLATE
, args
, NULL
)))
212 Fault(IoErr(), 0, s
, 256);
216 // if (!args[ARG_FROM]) args[ARG_FROM] = (IPTR)CONFIGNAME_ENV;
219 /*********************************************************************************************/
221 static void FreeArguments(void)
223 if (myargs
) FreeArgs(myargs
);
226 /*********************************************************************************************/
228 static void GetVisual(void)
232 /*********************************************************************************************/
234 static void FreeVisual(void)
238 /*********************************************************************************************/
240 #if DO_SPECIAL_BUTTON_LAYOUT
242 /*********************************************************************************************/
244 static ULONG
ButtonLayoutFunc(struct Hook
*hook
, Object
*obj
, struct MUI_LayoutMsg
*msg
)
246 IPTR retval
= MUILM_UNKNOWN
;
252 Object
*cstate
= (Object
*)msg
->lm_Children
->mlh_Head
;
255 WORD maxminwidth
= 0;
256 WORD maxminheight
= 0;
259 while((child
= NextObject(&cstate
)))
261 if (_minwidth(child
) > maxminwidth
) maxminwidth
= _minwidth(child
);
262 if (_minheight(child
) > maxminheight
) maxminheight
= _minheight(child
);
266 msg
->lm_MinMax
.MinWidth
=
267 msg
->lm_MinMax
.DefWidth
= numchilds
* maxminwidth
+ SPACING_BUTTONGROUP
* (numchilds
- 1);
268 msg
->lm_MinMax
.MaxWidth
= MUI_MAXMAX
;
270 msg
->lm_MinMax
.MinHeight
=
271 msg
->lm_MinMax
.DefHeight
=
272 msg
->lm_MinMax
.MaxHeight
= maxminheight
;
279 Object
*cstate
= (Object
*)msg
->lm_Children
->mlh_Head
;
282 WORD maxminwidth
= 0;
283 WORD maxminheight
= 0;
287 while((child
= NextObject(&cstate
)))
289 if (_minwidth(child
) > maxminwidth
) maxminwidth
= _minwidth(child
);
290 if (_minheight(child
) > maxminheight
) maxminheight
= _minheight(child
);
294 cstate
= (Object
*)msg
->lm_Children
->mlh_Head
;
295 while((child
= NextObject(&cstate
)))
297 if (i
== numchilds
-1)
299 x
= msg
->lm_Layout
.Width
- maxminwidth
;
303 x
= maxminwidth
+ (msg
->lm_Layout
.Width
- numchilds
* maxminwidth
) / (numchilds
- 1);
306 MUI_Layout(child
, x
, 0, maxminwidth
, maxminheight
, 0);
315 } /* switch(msg->lm_Type) */
320 /*********************************************************************************************/
322 #endif /* DO_SPECIAL_BUTTON_LAYOUT */
324 /*********************************************************************************************/
326 static void YearFunc(struct Hook
*hook
, Object
*obj
, IPTR
*param
)
330 get(obj
, MUIA_String_Integer
, &year
);
332 if ((LONG
)*param
== -1)
334 else if ((LONG
)*param
== 1)
340 nnset(obj
, MUIA_String_Integer
, year
);
342 else if (year
> 2099)
345 nnset(obj
, MUIA_String_Integer
, year
);
349 nnset(obj
, MUIA_String_Integer
, year
);
352 nnset(calobj
, MUIA_Calendar_Year
, year
);
356 /*********************************************************************************************/
358 static void ActiveFunc(struct Hook
*hook
, Object
*obj
, IPTR
*param
)
363 active
= *(Object
**)param
;
365 if (active
== hourobj
)
367 hand
= MUIV_Clock_EditHand_Hour
;
369 else if (active
== minobj
)
371 hand
= MUIV_Clock_EditHand_Minute
;
373 else if (active
== secobj
)
375 hand
= MUIV_Clock_EditHand_Second
;
380 activetimestrobj
= active
;
381 set(clockobj
, MUIA_Clock_EditHand
, hand
);
386 /*********************************************************************************************/
388 static void ClockFunc(struct Hook
*hook
, Object
*obj
, IPTR
*param
)
390 struct ClockData
*cd
;
393 get(obj
, MUIA_Clock_Time
, &cd
);
397 sprintf(s
, "%02d", cd
->hour
);
398 nnset(hourobj
, MUIA_String_Contents
, s
);
400 sprintf(s
, "%02d", cd
->min
);
401 nnset(minobj
, MUIA_String_Contents
, s
);
403 sprintf(s
, "%02d", cd
->sec
);
404 nnset(secobj
, MUIA_String_Contents
, s
);
408 struct ClockData cd2
;
409 LONG diff
= (LONG
)*param
;
411 UWORD
*cd2_member
= NULL
;
413 if (diff
== 100) diff
= 0; /* 100 means string gadget acknowledge */
415 if (activetimestrobj
== hourobj
)
418 cd2_member
= &cd2
.hour
;
420 else if (activetimestrobj
== minobj
)
423 cd2_member
= &cd2
.min
;
425 else if (activetimestrobj
== secobj
)
428 cd2_member
= &cd2
.sec
;
435 set(obj
, MUIA_Clock_Frozen
, TRUE
);
436 get(obj
, MUIA_Clock_Time
, &cd
);
440 get(activetimestrobj
, MUIA_String_Integer
, &number
);
444 if ((LONG
)number
< 0)
448 else if ((LONG
)number
> max
)
452 *cd2_member
= number
;
454 sprintf(s
, "%02ld", number
);
456 nnset(activetimestrobj
, MUIA_String_Contents
, s
);
458 set(obj
, MUIA_Clock_Time
, (IPTR
)&cd2
);
464 /*********************************************************************************************/
466 static void RestoreFunc(struct Hook
*hook
, Object
*obj
, APTR msg
)
470 set(calobj
, MUIA_Calendar_Date
, (IPTR
)&clockdata
);
471 set(monthobj
, MUIA_Cycle_Active
, clockdata
.month
- 1);
472 set(yearobj
, MUIA_String_Integer
, clockdata
.year
);
473 set(clockobj
, MUIA_Clock_Frozen
, FALSE
);
477 /*********************************************************************************************/
479 static void MakeGUI(void)
481 extern struct NewMenu nm
;
483 Object
*menu
, *yearaddobj
, *yearsubobj
, *timeaddobj
, *timesubobj
;
484 Object
*saveobj
, *useobj
, *cancelobj
;
486 #if DO_SPECIAL_BUTTON_LAYOUT
487 buttonlayouthook
.h_Entry
= HookEntry
;
488 buttonlayouthook
.h_SubEntry
= (HOOKFUNC
)ButtonLayoutFunc
;
490 yearhook
.h_Entry
= HookEntry
;
491 yearhook
.h_SubEntry
= (HOOKFUNC
)YearFunc
;
493 clockhook
.h_Entry
= HookEntry
;
494 clockhook
.h_SubEntry
= (HOOKFUNC
)ClockFunc
;
496 activehook
.h_Entry
= HookEntry
;
497 activehook
.h_SubEntry
= (HOOKFUNC
)ActiveFunc
;
499 restorehook
.h_Entry
= HookEntry
;
500 restorehook
.h_SubEntry
= (HOOKFUNC
)RestoreFunc
;
504 struct Locale
*locale
= OpenLocale(NULL
);
510 for(i
= 0; i
< 12; i
++)
512 monthlabels
[i
] = GetLocaleStr(locale
, MON_1
+ i
);
520 menu
= MUI_MakeObject(MUIO_MenustripNM
, &nm
, 0);
522 app
= ApplicationObject
,
523 MUIA_Application_Title
, (IPTR
)"Time",
524 MUIA_Application_Version
, (IPTR
)VERSIONSTR
,
525 MUIA_Application_Copyright
, (IPTR
)"Copyright © 1995-2002, The AROS Development Team",
526 MUIA_Application_Author
, (IPTR
)"The AROS Development Team",
527 MUIA_Application_Description
, (IPTR
)MSG(MSG_WINTITLE
),
528 MUIA_Application_Base
, (IPTR
)"Time",
529 menu
? MUIA_Application_Menustrip
: TAG_IGNORE
, menu
,
530 SubWindow
, wnd
= WindowObject
,
531 MUIA_Window_Title
, (IPTR
)MSG(MSG_WINTITLE
),
532 MUIA_Window_ID
, MAKE_ID('T','W','I','N'),
533 WindowContents
, VGroup
,
534 Child
, HGroup
, /* Group containing calendar box and clock box */
535 MUIA_Group_SameWidth
, TRUE
,
536 Child
, VGroup
, /* Calendar box */
538 MUIA_Background
, MUII_GroupBack
,
539 Child
, HGroup
, /* Month/year row */
540 Child
, monthobj
= MUI_MakeObject(MUIO_Cycle
, NULL
, monthlabels
),
542 Child
, yearsubobj
= TextObject
, /* year [-] gadget */
544 MUIA_Background
, MUII_ButtonBack
,
545 MUIA_CycleChain
, TRUE
,
546 MUIA_Font
, MUIV_Font_Button
,
547 MUIA_InputMode
, MUIV_InputMode_RelVerify
,
548 MUIA_Text_Contents
, "\033c-",
549 MUIA_FixWidthTxt
, (IPTR
)"+",
551 Child
, yearobj
= StringObject
, /* year gadget */
553 MUIA_CycleChain
, TRUE
,
554 MUIA_String_Accept
, (IPTR
)"0123456789",
555 MUIA_FixWidthTxt
, (IPTR
)"55555",
557 Child
, yearaddobj
= TextObject
, /* year [-] gadget */
559 MUIA_Background
, MUII_ButtonBack
,
560 MUIA_CycleChain
, TRUE
,
561 MUIA_Font
, MUIV_Font_Button
,
562 MUIA_InputMode
, MUIV_InputMode_RelVerify
,
563 MUIA_Text_Contents
, "\033c+",
564 MUIA_FixWidthTxt
, (IPTR
)"-",
567 Child
, calobj
= CalendarObject
,
568 MUIA_CycleChain
, TRUE
,
571 Child
, VGroup
, /* Clock box */
573 MUIA_Background
, MUII_GroupBack
,
574 Child
, clockobj
= ClockObject
,
581 MUIA_Group_Spacing
, 0,
582 Child
, TextObject
, /* phantom time [-] gadget */
584 MUIA_Background
, MUII_ButtonBack
,
585 MUIA_Font
, MUIV_Font_Button
,
586 MUIA_InputMode
, MUIV_InputMode_RelVerify
,
587 MUIA_Text_Contents
, "\033c-",
588 MUIA_FixWidthTxt
, (IPTR
)"+",
590 Child
, TextObject
, /* phantom time [+] gadget */
592 MUIA_Background
, MUII_ButtonBack
,
593 MUIA_Font
, MUIV_Font_Button
,
594 MUIA_InputMode
, MUIV_InputMode_RelVerify
,
595 MUIA_Text_Contents
, "\033c+",
596 MUIA_FixWidthTxt
, (IPTR
)"-",
600 Child
, hourobj
= StringObject
, /* hour gadget */
602 MUIA_CycleChain
, TRUE
,
603 MUIA_String_Accept
, (IPTR
)"0123456789",
604 MUIA_FixWidthTxt
, (IPTR
)"555",
607 Child
, minobj
= StringObject
, /* min gadget */
609 MUIA_CycleChain
, TRUE
,
610 MUIA_String_Accept
, (IPTR
)"0123456789",
611 MUIA_FixWidthTxt
, (IPTR
)"555",
614 Child
, secobj
= StringObject
, /* sec gadget */
616 MUIA_CycleChain
, TRUE
,
617 MUIA_String_Accept
, (IPTR
)"0123456789",
618 MUIA_FixWidthTxt
, (IPTR
)"555",
621 MUIA_Group_Spacing
, 0,
622 Child
, timesubobj
= TextObject
, /* time [-] gadget */
624 MUIA_Background
, MUII_ButtonBack
,
625 MUIA_CycleChain
, TRUE
,
626 MUIA_Font
, MUIV_Font_Button
,
627 MUIA_InputMode
, MUIV_InputMode_RelVerify
,
628 MUIA_Text_Contents
, "\033c-",
629 MUIA_FixWidthTxt
, (IPTR
)"+",
631 Child
, timeaddobj
= TextObject
, /* time [+] gadget */
633 MUIA_Background
, MUII_ButtonBack
,
634 MUIA_CycleChain
, TRUE
,
635 MUIA_Font
, MUIV_Font_Button
,
636 MUIA_InputMode
, MUIV_InputMode_RelVerify
,
637 MUIA_Text_Contents
, "\033c+",
638 MUIA_FixWidthTxt
, (IPTR
)"-",
645 Child
, HGroup
, /* save/use/cancel button row */
646 #if DO_SPECIAL_BUTTON_LAYOUT
647 MUIA_Group_LayoutHook
, (IPTR
)&buttonlayouthook
,
650 MUIA_Group_SameWidth
, TRUE
,
652 Child
, saveobj
= CoolImageIDButton(MSG(MSG_GAD_SAVE
), COOL_SAVEIMAGE_ID
),
653 Child
, useobj
= CoolImageIDButton(MSG(MSG_GAD_USE
), COOL_DOTIMAGE_ID
),
654 Child
, cancelobj
= CoolImageIDButton(MSG(MSG_GAD_CANCEL
), COOL_CANCELIMAGE_ID
),
660 if (!app
) Cleanup(MSG(MSG_CANT_CREATE_APP
));
662 DoMethod(wnd
, MUIM_Notify
, MUIA_Window_CloseRequest
, TRUE
, (IPTR
) app
, 2, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
);
664 DoMethod(cancelobj
, MUIM_Notify
, MUIA_Pressed
, FALSE
, (IPTR
) app
, 2, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
);
665 DoMethod(saveobj
, MUIM_Notify
, MUIA_Pressed
, FALSE
, (IPTR
) app
, 2, MUIM_Application_ReturnID
, RETURNID_SAVE
);
666 DoMethod(useobj
, MUIM_Notify
, MUIA_Pressed
, FALSE
, (IPTR
) app
, 2, MUIM_Application_ReturnID
, RETURNID_USE
);
668 DoMethod(wnd
, MUIM_Notify
, MUIA_Window_MenuAction
, MSG_MEN_PROJECT_QUIT
, (IPTR
) app
, 2, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
);
669 DoMethod(wnd
, MUIM_Notify
, MUIA_Window_MenuAction
, MSG_MEN_EDIT_RESTORE
, (IPTR
) app
, 2, MUIM_CallHook
, (IPTR
)&restorehook
);
671 DoMethod(wnd
, MUIM_Notify
, MUIA_Window_ActiveObject
, MUIV_EveryTime
, (IPTR
) app
, 3, MUIM_CallHook
, (IPTR
)&activehook
, MUIV_TriggerValue
);
672 DoMethod(monthobj
, MUIM_Notify
, MUIA_Cycle_Active
, MUIV_EveryTime
, (IPTR
) calobj
, 3, MUIM_NoNotifySet
, MUIA_Calendar_Month0
, MUIV_TriggerValue
);
673 DoMethod(yearobj
, MUIM_Notify
, MUIA_String_Acknowledge
, MUIV_EveryTime
, (IPTR
) yearobj
, 3, MUIM_CallHook
, (IPTR
)&yearhook
, 0);
674 DoMethod(yearaddobj
, MUIM_Notify
, MUIA_Timer
, MUIV_EveryTime
, (IPTR
) yearobj
, 3, MUIM_CallHook
, (IPTR
)&yearhook
, 1);
675 DoMethod(yearsubobj
, MUIM_Notify
, MUIA_Timer
, MUIV_EveryTime
, (IPTR
) yearobj
, 3, MUIM_CallHook
, (IPTR
)&yearhook
, -1);
676 DoMethod(timeaddobj
, MUIM_Notify
, MUIA_Timer
, MUIV_EveryTime
, (IPTR
) clockobj
, 3, MUIM_CallHook
, (IPTR
)&clockhook
, 1);
677 DoMethod(timesubobj
, MUIM_Notify
, MUIA_Timer
, MUIV_EveryTime
, (IPTR
) clockobj
, 3, MUIM_CallHook
, (IPTR
)&clockhook
, -1);
678 DoMethod(clockobj
, MUIM_Notify
, MUIA_Clock_Ticked
, TRUE
, (IPTR
) clockobj
, 3, MUIM_CallHook
, (IPTR
)&clockhook
, 0);
679 DoMethod(hourobj
, MUIM_Notify
, MUIA_String_Acknowledge
, MUIV_EveryTime
, (IPTR
) clockobj
, 3, MUIM_CallHook
, (IPTR
)&clockhook
, 100);
680 DoMethod(minobj
, MUIM_Notify
, MUIA_String_Acknowledge
, MUIV_EveryTime
, (IPTR
) clockobj
, 3, MUIM_CallHook
, (IPTR
)&clockhook
, 100);
681 DoMethod(secobj
, MUIM_Notify
, MUIA_String_Acknowledge
, MUIV_EveryTime
, (IPTR
) clockobj
, 3, MUIM_CallHook
, (IPTR
)&clockhook
, 100);
683 set(calobj
, MUIA_Calendar_Date
, &clockdata
);
684 set(monthobj
, MUIA_Cycle_Active
, clockdata
.month
- 1);
685 set(yearobj
, MUIA_String_Integer
, clockdata
.year
);
687 CallHook(&clockhook
, clockobj
, 0);
690 /*********************************************************************************************/
692 static void KillGUI(void)
697 /*********************************************************************************************/
699 static void HandleAll(void)
704 set (wnd
, MUIA_Window_Open
, TRUE
);
708 returnid
= (LONG
) DoMethod(app
, MUIM_Application_NewInput
, (IPTR
) &sigs
);
710 if ((returnid
== MUIV_Application_ReturnID_Quit
) ||
711 (returnid
== RETURNID_SAVE
) || (returnid
== RETURNID_USE
)) break;
715 sigs
= Wait(sigs
| SIGBREAKF_CTRL_C
);
716 if (sigs
& SIGBREAKF_CTRL_C
) break;
725 struct ClockData cal_date
, clock_time
, *dateptr
;
728 get(calobj
, MUIA_Calendar_Date
, &dateptr
);
731 get(clockobj
, MUIA_Clock_Frozen
, &frozen
);
734 get(clockobj
, MUIA_Clock_Time
, &dateptr
);
735 clock_time
= *dateptr
;
742 Amiga2Date(tv
.tv_secs
, &clock_time
);
745 clockdata
.sec
= clock_time
.sec
;
746 clockdata
.min
= clock_time
.min
;
747 clockdata
.hour
= clock_time
.hour
;
748 clockdata
.mday
= cal_date
.mday
;
749 clockdata
.month
= cal_date
.month
;
750 clockdata
.year
= cal_date
.year
;
751 clockdata
.wday
= cal_date
.wday
;
761 /*********************************************************************************************/
765 InitLocale("System/Prefs/Time.catalog", 1);
770 InitPrefs((args
[ARG_USE
] ? TRUE
: FALSE
), (args
[ARG_SAVE
] ? TRUE
: FALSE
));
779 /*********************************************************************************************/