2 Copyright © 1995-2016, The AROS Development Team. All rights reserved.
6 /*********************************************************************************************/
13 #include <aros/debug.h>
15 #include <zune/clock.h>
16 #include <zune/calendar.h>
18 /*********************************************************************************************/
20 #define ARG_TEMPLATE "FROM,EDIT/S,USE/S,SAVE/S,PUBSCREEN/K"
26 #define ARG_PUBSCREEN 4
30 #define DO_SPECIAL_BUTTON_LAYOUT 1 /* all button get same width, but as small as possible,
31 and spread along whole parent group */
33 #define SPACING_BUTTONGROUP 4
35 #define RETURNID_USE 1
36 #define RETURNID_SAVE 2
38 /*********************************************************************************************/
40 static struct Hook yearhook
, clockhook
, activehook
, restorehook
;
41 #if DO_SPECIAL_BUTTON_LAYOUT
42 static struct Hook buttonlayouthook
;
44 static struct RDArgs
*myargs
;
45 static Object
*activetimestrobj
= NULL
;
46 static IPTR args
[NUM_ARGS
];
48 static CONST_STRPTR monthlabels
[] =
65 /*********************************************************************************************/
67 static void CloseTimerDev(void);
68 static void FreeArguments(void);
69 static void KillGUI(void);
71 /*********************************************************************************************/
73 WORD
ShowMessage(STRPTR title
, STRPTR text
, STRPTR gadtext
)
77 es
.es_StructSize
= sizeof(es
);
80 es
.es_TextFormat
= text
;
81 es
.es_GadgetFormat
= gadtext
;
83 return EasyRequestArgs(NULL
, &es
, NULL
, NULL
);
86 /*********************************************************************************************/
88 void Cleanup(STRPTR msg
)
92 if (IntuitionBase
&& !((struct Process
*)FindTask(NULL
))->pr_CLI
)
94 ShowMessage((STRPTR
)"Time", msg
, MSG(MSG_OK
));
98 Printf((STRPTR
)"Time: %s\n", msg
);
110 /*********************************************************************************************/
112 static void OpenTimerDev(void)
114 if ((TimerMP
= CreateMsgPort()))
116 if ((TimerIO
= (struct timerequest
*)CreateIORequest(TimerMP
, sizeof(struct timerequest
))))
118 if (!OpenDevice("timer.device", UNIT_VBLANK
, (struct IORequest
*)TimerIO
, 0))
120 TimerBase
= (struct Device
*)TimerIO
->tr_node
.io_Device
;
127 my_sprintf(s
,(UBYTE
*)MSG(MSG_CANT_OPEN_LIB
),"timer.device", 0);
132 /*********************************************************************************************/
134 static void CloseTimerDev(void)
138 CloseDevice((struct IORequest
*)TimerIO
);
139 DeleteIORequest((struct IORequest
*)TimerIO
);
144 DeleteMsgPort(TimerMP
);
148 /*********************************************************************************************/
150 static void OpenBattClockRes(void)
152 BattClockBase
= OpenResource("battclock.resource");
156 my_sprintf(s
,(UBYTE
*)MSG(MSG_CANT_OPEN_LIB
),"battclock.resource", 0);
161 /*********************************************************************************************/
163 static void GetArguments(int argc
, char **argv
)
166 if (argc
) // started from CLI
168 if (!(myargs
= ReadArgs(ARG_TEMPLATE
, args
, NULL
)))
170 Fault(IoErr(), 0, s
, 256);
175 // if (!args[ARG_FROM]) args[ARG_FROM] = (IPTR)CONFIGNAME_ENV;
178 /*********************************************************************************************/
180 static void FreeArguments(void)
182 if (myargs
) FreeArgs(myargs
);
185 /*********************************************************************************************/
187 #if DO_SPECIAL_BUTTON_LAYOUT
189 /*********************************************************************************************/
191 static ULONG
ButtonLayoutFunc(struct Hook
*hook
, Object
*obj
, struct MUI_LayoutMsg
*msg
)
193 IPTR retval
= MUILM_UNKNOWN
;
199 Object
*cstate
= (Object
*)msg
->lm_Children
->mlh_Head
;
202 WORD maxminwidth
= 0;
203 WORD maxminheight
= 0;
206 while((child
= NextObject(&cstate
)))
208 if (_minwidth(child
) > maxminwidth
) maxminwidth
= _minwidth(child
);
209 if (_minheight(child
) > maxminheight
) maxminheight
= _minheight(child
);
213 msg
->lm_MinMax
.MinWidth
=
214 msg
->lm_MinMax
.DefWidth
= numchilds
* maxminwidth
+ SPACING_BUTTONGROUP
* (numchilds
- 1);
215 msg
->lm_MinMax
.MaxWidth
= MUI_MAXMAX
;
217 msg
->lm_MinMax
.MinHeight
=
218 msg
->lm_MinMax
.DefHeight
=
219 msg
->lm_MinMax
.MaxHeight
= maxminheight
;
226 Object
*cstate
= (Object
*)msg
->lm_Children
->mlh_Head
;
229 WORD maxminwidth
= 0;
230 WORD maxminheight
= 0;
234 while((child
= NextObject(&cstate
)))
236 if (_minwidth(child
) > maxminwidth
) maxminwidth
= _minwidth(child
);
237 if (_minheight(child
) > maxminheight
) maxminheight
= _minheight(child
);
241 cstate
= (Object
*)msg
->lm_Children
->mlh_Head
;
242 while((child
= NextObject(&cstate
)))
244 if (i
== numchilds
-1)
246 x
= msg
->lm_Layout
.Width
- maxminwidth
;
250 x
= maxminwidth
+ (msg
->lm_Layout
.Width
- numchilds
* maxminwidth
) / (numchilds
- 1);
253 MUI_Layout(child
, x
, 0, maxminwidth
, maxminheight
, 0);
261 } /* switch(msg->lm_Type) */
266 /*********************************************************************************************/
268 #endif /* DO_SPECIAL_BUTTON_LAYOUT */
270 /*********************************************************************************************/
272 static void YearFunc(struct Hook
*hook
, Object
*obj
, IPTR
*param
)
276 get(obj
, MUIA_String_Integer
, &year
);
278 if ((LONG
)*param
== -1)
282 else if ((LONG
)*param
== 1)
290 nnset(obj
, MUIA_String_Integer
, year
);
292 else if (year
> 2099)
295 nnset(obj
, MUIA_String_Integer
, year
);
299 nnset(obj
, MUIA_String_Integer
, year
);
302 nnset(calobj
, MUIA_Calendar_Year
, year
);
306 /*********************************************************************************************/
308 static void ActiveFunc(struct Hook
*hook
, Object
*obj
, IPTR
*param
)
313 active
= *(Object
**)param
;
315 if (active
== hourobj
)
317 hand
= MUIV_Clock_EditHand_Hour
;
319 else if (active
== minobj
)
321 hand
= MUIV_Clock_EditHand_Minute
;
323 else if (active
== secobj
)
325 hand
= MUIV_Clock_EditHand_Second
;
330 activetimestrobj
= active
;
331 set(clockobj
, MUIA_Clock_EditHand
, hand
);
335 /*********************************************************************************************/
337 static void ClockFunc(struct Hook
*hook
, Object
*obj
, IPTR
*param
)
339 struct ClockData
*cd
=NULL
;
342 get(obj
, MUIA_Clock_Time
, &cd
);
346 if (activetimestrobj
== NULL
)
348 my_sprintf(s
,(UBYTE
*)"%02d",cd
->hour
);
349 nnset(hourobj
, MUIA_String_Contents
, s
);
351 my_sprintf(s
,(UBYTE
*)"%02d",cd
->min
);
352 nnset(minobj
, MUIA_String_Contents
, s
);
354 my_sprintf(s
,(UBYTE
*)"%02d",cd
->sec
);
355 nnset(secobj
, MUIA_String_Contents
, s
);
360 struct ClockData cd2
;
361 LONG diff
= (LONG
)*param
;
363 UWORD
*cd2_member
= NULL
;
365 if (diff
== 100) diff
= 0; /* 100 means string gadget acknowledge */
367 if (activetimestrobj
== hourobj
)
370 cd2_member
= &cd2
.hour
;
372 else if (activetimestrobj
== minobj
)
375 cd2_member
= &cd2
.min
;
378 else if (activetimestrobj
== secobj
)
381 cd2_member
= &cd2
.sec
;
388 set(obj
, MUIA_Clock_Frozen
, TRUE
);
389 get(obj
, MUIA_Clock_Time
, &cd
);
393 get(activetimestrobj
, MUIA_String_Integer
, &number
);
397 if ((LONG
)number
< 0)
401 else if ((LONG
)number
> max
)
405 *cd2_member
= number
;
407 my_sprintf(s
,(UBYTE
*)"%02ld",number
);
409 nnset(activetimestrobj
, MUIA_String_Contents
, s
);
411 set(obj
, MUIA_Clock_Time
, (IPTR
)&cd2
);
417 /*********************************************************************************************/
419 static void RestoreFunc(struct Hook
*hook
, Object
*obj
, APTR msg
)
423 set(calobj
, MUIA_Calendar_Date
, (IPTR
)&clockdata
);
424 set(monthobj
, MUIA_Cycle_Active
, clockdata
.month
- 1);
425 set(yearobj
, MUIA_String_Integer
, clockdata
.year
);
426 set(clockobj
, MUIA_Clock_Frozen
, FALSE
);
429 /*********************************************************************************************/
431 static void MakeGUI(void)
433 extern struct NewMenu nm
;
435 Object
*menu
, *yearaddobj
, *yearsubobj
, *timeaddobj
, *timesubobj
;
436 Object
*saveobj
, *useobj
, *cancelobj
;
438 #if DO_SPECIAL_BUTTON_LAYOUT
439 buttonlayouthook
.h_Entry
= HookEntry
;
440 buttonlayouthook
.h_SubEntry
= (HOOKFUNC
)ButtonLayoutFunc
;
442 yearhook
.h_Entry
= HookEntry
;
443 yearhook
.h_SubEntry
= (HOOKFUNC
)YearFunc
;
445 clockhook
.h_Entry
= HookEntry
;
446 clockhook
.h_SubEntry
= (HOOKFUNC
)ClockFunc
;
448 activehook
.h_Entry
= HookEntry
;
449 activehook
.h_SubEntry
= (HOOKFUNC
)ActiveFunc
;
451 restorehook
.h_Entry
= HookEntry
;
452 restorehook
.h_SubEntry
= (HOOKFUNC
)RestoreFunc
;
456 struct Locale
*locale
= OpenLocale(NULL
);
462 for(i
= 0; i
< 12; i
++)
464 monthlabels
[i
] = GetLocaleStr(locale
, MON_1
+ i
);
472 menu
= MUI_MakeObject(MUIO_MenustripNM
, &nm
, 0);
474 app
= ApplicationObject
,
475 MUIA_Application_Title
, (IPTR
)"Time",
476 MUIA_Application_Version
, (IPTR
)VERSIONSTR
,
477 MUIA_Application_Copyright
, (IPTR
)"Copyright © 1995-2011, The AROS Development Team",
478 MUIA_Application_Author
, (IPTR
)"The AROS Development Team",
479 MUIA_Application_Description
, (IPTR
)MSG(MSG_WINTITLE
),
480 MUIA_Application_SingleTask
, TRUE
,
481 MUIA_Application_Base
, (IPTR
)"Time",
482 menu
? MUIA_Application_Menustrip
: TAG_IGNORE
, menu
,
484 SubWindow
, wnd
= WindowObject
,
485 MUIA_Window_Title
, (IPTR
)MSG(MSG_WINTITLE
),
486 MUIA_Window_ID
, MAKE_ID('T','W','I','N'),
487 MUIA_Window_CloseGadget
, FALSE
,
489 WindowContents
, VGroup
,
490 Child
, HGroup
, /* Group containing calendar box and clock box */
491 MUIA_Group_SameWidth
, TRUE
,
493 Child
, VGroup
, /* Calendar box */
495 MUIA_Background
, MUII_GroupBack
,
496 Child
, HGroup
, /* Month/year row */
497 Child
, monthobj
= MUI_MakeObject(MUIO_Cycle
, NULL
, monthlabels
),
499 Child
, yearsubobj
= TextObject
, /* year [-] gadget */
501 MUIA_Background
, MUII_ButtonBack
,
502 MUIA_CycleChain
, TRUE
,
503 MUIA_Font
, MUIV_Font_Button
,
504 MUIA_InputMode
, MUIV_InputMode_RelVerify
,
505 MUIA_Text_Contents
, "\033c-",
506 MUIA_FixWidthTxt
, (IPTR
)"+",
508 Child
, yearobj
= StringObject
, /* year gadget */
510 MUIA_CycleChain
, TRUE
,
511 MUIA_String_Accept
, (IPTR
)"0123456789",
512 MUIA_FixWidthTxt
, (IPTR
)"55555",
514 Child
, yearaddobj
= TextObject
, /* year [+] gadget */
516 MUIA_Background
, MUII_ButtonBack
,
517 MUIA_CycleChain
, TRUE
,
518 MUIA_Font
, MUIV_Font_Button
,
519 MUIA_InputMode
, MUIV_InputMode_RelVerify
,
520 MUIA_Text_Contents
, "\033c+",
521 MUIA_FixWidthTxt
, (IPTR
)"-",
524 Child
, calobj
= CalendarObject
,
525 MUIA_CycleChain
, TRUE
,
529 Child
, VGroup
, /* Clock box */
531 MUIA_Background
, MUII_GroupBack
,
533 Child
, clockobj
= ClockObject
,
541 MUIA_Group_Spacing
, 0,
542 Child
, TextObject
, /* phantom time [-] gadget */
544 MUIA_Background
, MUII_ButtonBack
,
545 MUIA_Font
, MUIV_Font_Button
,
546 MUIA_InputMode
, MUIV_InputMode_RelVerify
,
547 MUIA_Text_Contents
, "\033c-",
548 MUIA_FixWidthTxt
, (IPTR
)"+",
550 Child
, TextObject
, /* phantom time [+] gadget */
552 MUIA_Background
, MUII_ButtonBack
,
553 MUIA_Font
, MUIV_Font_Button
,
554 MUIA_InputMode
, MUIV_InputMode_RelVerify
,
555 MUIA_Text_Contents
, "\033c+",
556 MUIA_FixWidthTxt
, (IPTR
)"-",
561 Child
, hourobj
= StringObject
, /* hour gadget */
563 MUIA_CycleChain
, TRUE
,
564 MUIA_String_Accept
, (IPTR
)"0123456789",
565 MUIA_FixWidthTxt
, (IPTR
)"555",
568 Child
, minobj
= StringObject
, /* min gadget */
570 MUIA_CycleChain
, TRUE
,
571 MUIA_String_Accept
, (IPTR
)"0123456789",
572 MUIA_FixWidthTxt
, (IPTR
)"555",
575 Child
, secobj
= StringObject
, /* sec gadget */
577 MUIA_CycleChain
, TRUE
,
578 MUIA_String_Accept
, (IPTR
)"0123456789",
579 MUIA_FixWidthTxt
, (IPTR
)"555",
583 MUIA_Group_Spacing
, 0,
584 Child
, timesubobj
= TextObject
, /* time [-] gadget */
586 MUIA_Background
, MUII_ButtonBack
,
587 MUIA_CycleChain
, TRUE
,
588 MUIA_Font
, MUIV_Font_Button
,
589 MUIA_InputMode
, MUIV_InputMode_RelVerify
,
590 MUIA_Text_Contents
, "\033c-",
591 MUIA_FixWidthTxt
, (IPTR
)"+",
593 Child
, timeaddobj
= TextObject
, /* time [+] gadget */
595 MUIA_Background
, MUII_ButtonBack
,
596 MUIA_CycleChain
, TRUE
,
597 MUIA_Font
, MUIV_Font_Button
,
598 MUIA_InputMode
, MUIV_InputMode_RelVerify
,
599 MUIA_Text_Contents
, "\033c+",
600 MUIA_FixWidthTxt
, (IPTR
)"-",
608 Child
, HGroup
, /* save/use/cancel button row */
609 #if DO_SPECIAL_BUTTON_LAYOUT
610 MUIA_Group_LayoutHook
, (IPTR
)&buttonlayouthook
,
613 MUIA_Group_SameWidth
, TRUE
,
615 Child
, saveobj
= ImageButton(MSG(MSG_GAD_SAVE
), "THEME:Images/Gadgets/Save"),
616 Child
, useobj
= ImageButton(MSG(MSG_GAD_USE
), "THEME:Images/Gadgets/Use"),
617 Child
, cancelobj
= ImageButton(MSG(MSG_GAD_CANCEL
), "THEME:Images/Gadgets/Cancel"),
623 if (!app
) Cleanup(MSG(MSG_CANT_CREATE_APP
));
625 DoMethod(cancelobj
, MUIM_Notify
, MUIA_Pressed
, FALSE
, (IPTR
) app
, 2, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
);
626 DoMethod(saveobj
, MUIM_Notify
, MUIA_Pressed
, FALSE
, (IPTR
) app
, 2, MUIM_Application_ReturnID
, RETURNID_SAVE
);
627 DoMethod(useobj
, MUIM_Notify
, MUIA_Pressed
, FALSE
, (IPTR
) app
, 2, MUIM_Application_ReturnID
, RETURNID_USE
);
629 DoMethod(wnd
, MUIM_Notify
, MUIA_Window_MenuAction
, MSG_MEN_PROJECT_QUIT
, (IPTR
) app
, 2, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
);
630 DoMethod(wnd
, MUIM_Notify
, MUIA_Window_MenuAction
, MSG_MEN_EDIT_RESTORE
, (IPTR
) app
, 2, MUIM_CallHook
, (IPTR
)&restorehook
);
632 DoMethod(wnd
, MUIM_Notify
, MUIA_Window_ActiveObject
, MUIV_EveryTime
, (IPTR
) app
, 3, MUIM_CallHook
, (IPTR
)&activehook
, MUIV_TriggerValue
);
633 DoMethod(monthobj
, MUIM_Notify
, MUIA_Cycle_Active
, MUIV_EveryTime
, (IPTR
) calobj
, 3, MUIM_NoNotifySet
, MUIA_Calendar_Month0
, MUIV_TriggerValue
);
634 DoMethod(yearobj
, MUIM_Notify
, MUIA_String_Acknowledge
, MUIV_EveryTime
, (IPTR
) yearobj
, 3, MUIM_CallHook
, (IPTR
)&yearhook
, 0);
635 DoMethod(yearaddobj
, MUIM_Notify
, MUIA_Timer
, MUIV_EveryTime
, (IPTR
) yearobj
, 3, MUIM_CallHook
, (IPTR
)&yearhook
, 1);
636 DoMethod(yearsubobj
, MUIM_Notify
, MUIA_Timer
, MUIV_EveryTime
, (IPTR
) yearobj
, 3, MUIM_CallHook
, (IPTR
)&yearhook
, -1);
637 DoMethod(timeaddobj
, MUIM_Notify
, MUIA_Timer
, MUIV_EveryTime
, (IPTR
) clockobj
, 3, MUIM_CallHook
, (IPTR
)&clockhook
, 1);
638 DoMethod(timesubobj
, MUIM_Notify
, MUIA_Timer
, MUIV_EveryTime
, (IPTR
) clockobj
, 3, MUIM_CallHook
, (IPTR
)&clockhook
, -1);
639 DoMethod(clockobj
, MUIM_Notify
, MUIA_Clock_Ticked
, TRUE
, (IPTR
) clockobj
, 3, MUIM_CallHook
, (IPTR
)&clockhook
, 0);
640 DoMethod(hourobj
, MUIM_Notify
, MUIA_String_Acknowledge
, MUIV_EveryTime
, (IPTR
) clockobj
, 3, MUIM_CallHook
, (IPTR
)&clockhook
, 100);
641 DoMethod(minobj
, MUIM_Notify
, MUIA_String_Acknowledge
, MUIV_EveryTime
, (IPTR
) clockobj
, 3, MUIM_CallHook
, (IPTR
)&clockhook
, 100);
642 DoMethod(secobj
, MUIM_Notify
, MUIA_String_Acknowledge
, MUIV_EveryTime
, (IPTR
) clockobj
, 3, MUIM_CallHook
, (IPTR
)&clockhook
, 100);
644 set(calobj
, MUIA_Calendar_Date
, &clockdata
);
645 set(monthobj
, MUIA_Cycle_Active
, clockdata
.month
- 1);
646 set(yearobj
, MUIA_String_Integer
, clockdata
.year
);
648 CallHook(&clockhook
, clockobj
, 0);
651 /*********************************************************************************************/
653 static void KillGUI(void)
658 /*********************************************************************************************/
660 static void HandleAll(void)
665 set (wnd
, MUIA_Window_Open
, TRUE
);
669 returnid
= (LONG
) DoMethod(app
, MUIM_Application_NewInput
, (IPTR
) &sigs
);
671 if ((returnid
== MUIV_Application_ReturnID_Quit
) ||
672 (returnid
== RETURNID_SAVE
) || (returnid
== RETURNID_USE
)) break;
676 sigs
= Wait(sigs
| SIGBREAKF_CTRL_C
);
677 if (sigs
& SIGBREAKF_CTRL_C
) break;
686 struct ClockData cal_date
, clock_time
, *dateptr
=NULL
;
689 get(calobj
, MUIA_Calendar_Date
, &dateptr
);
692 get(clockobj
, MUIA_Clock_Frozen
, &frozen
);
695 get(clockobj
, MUIA_Clock_Time
, &dateptr
);
696 clock_time
= *dateptr
;
703 Amiga2Date(tv
.tv_secs
, &clock_time
);
706 clockdata
.sec
= clock_time
.sec
;
707 clockdata
.min
= clock_time
.min
;
708 clockdata
.hour
= clock_time
.hour
;
709 clockdata
.mday
= cal_date
.mday
;
710 clockdata
.month
= cal_date
.month
;
711 clockdata
.year
= cal_date
.year
;
712 clockdata
.wday
= cal_date
.wday
;
714 if (returnid
== RETURNID_SAVE
)
728 /*********************************************************************************************/
730 int main(int argc
, char **argv
)
732 InitLocale("System/Prefs/Time.catalog", 1);
736 GetArguments(argc
, argv
);
737 InitPrefs((args
[ARG_USE
] ? TRUE
: FALSE
), (args
[ARG_SAVE
] ? TRUE
: FALSE
));
745 /*********************************************************************************************/