2 * Copyright 2004-2011, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * John Scipione <jscipione@gmail.com>
10 #include "ClockView.h"
12 #include <Alignment.h>
16 #include <ControlLook.h>
17 #include <LayoutBuilder.h>
20 #include <Messenger.h>
21 #include <RadioButton.h>
22 #include <SpaceLayoutItem.h>
25 #include "TimeMessages.h"
28 static const char* kDeskbarSignature
= "application/x-vnd.Be-TSKB";
31 #undef B_TRANSLATION_CONTEXT
32 #define B_TRANSLATION_CONTEXT "Time"
35 ClockView::ClockView(const char* name
)
38 fCachedShowClock(B_CONTROL_ON
),
39 fCachedShowSeconds(B_CONTROL_OFF
),
40 fCachedShowDayOfWeek(B_CONTROL_OFF
),
41 fCachedShowTimeZone(B_CONTROL_OFF
)
43 fShowClock
= new BCheckBox(B_TRANSLATE("Show clock in Deskbar"),
44 new BMessage(kShowHideTime
));
45 fShowSeconds
= new BCheckBox(B_TRANSLATE("Display time with seconds"),
46 new BMessage(kShowSeconds
));
47 fShowDayOfWeek
= new BCheckBox(B_TRANSLATE("Show day of week"),
48 new BMessage(kShowDayOfWeek
));
49 fShowTimeZone
= new BCheckBox(B_TRANSLATE("Show time zone"),
50 new BMessage(kShowTimeZone
));
52 BView
* view
= BLayoutBuilder::Group
<>(B_VERTICAL
, 0)
53 .SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED
, B_SIZE_UNSET
))
58 .SetInsets(B_USE_DEFAULT_SPACING
)
61 BBox
* showClockBox
= new BBox("show clock box");
62 showClockBox
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
, B_ALIGN_TOP
));
63 showClockBox
->SetLabel(fShowClock
);
64 showClockBox
->AddChild(view
);
66 BLayoutBuilder::Group
<>(this)
67 .AddGroup(B_VERTICAL
, 0)
70 .SetInsets(B_USE_DEFAULT_SPACING
);
74 ClockView::~ClockView()
80 ClockView::AttachedToWindow()
83 SetViewColor(Parent()->ViewColor());
85 fShowClock
->SetTarget(this);
86 fShowSeconds
->SetTarget(this);
87 fShowDayOfWeek
->SetTarget(this);
88 fShowTimeZone
->SetTarget(this);
90 // Disable these controls initially, they'll be enabled
91 // when we get a response from Deskbar.
92 fShowClock
->SetEnabled(false);
93 fShowSeconds
->SetEnabled(false);
94 fShowDayOfWeek
->SetEnabled(false);
95 fShowTimeZone
->SetEnabled(false);
97 // Ask Deskbar for current clock settings, it will reply
98 // asynchronously in MesssageReceived() below.
99 BMessenger
* messenger
= new BMessenger(kDeskbarSignature
);
100 BMessenger
replyMessenger(this);
101 BMessage
* message
= new BMessage(kGetClockSettings
);
102 messenger
->SendMessage(message
, replyMessenger
);
107 ClockView::MessageReceived(BMessage
* message
)
109 switch (message
->what
) {
110 case kGetClockSettings
:
112 // Get current clock settings from Deskbar
118 if (message
->FindBool("showSeconds", &showSeconds
) == B_OK
) {
119 fCachedShowSeconds
= showSeconds
120 ? B_CONTROL_ON
: B_CONTROL_OFF
;
121 fShowSeconds
->SetValue(fCachedShowSeconds
);
122 fShowSeconds
->SetEnabled(true);
125 if (message
->FindBool("showDayOfWeek", &showDayOfWeek
) == B_OK
) {
126 fCachedShowDayOfWeek
= showDayOfWeek
127 ? B_CONTROL_ON
: B_CONTROL_OFF
;
128 fShowDayOfWeek
->SetValue(fCachedShowDayOfWeek
);
129 fShowDayOfWeek
->SetEnabled(true);
132 if (message
->FindBool("showTimeZone", &showTimeZone
) == B_OK
) {
133 fCachedShowTimeZone
= showTimeZone
134 ? B_CONTROL_ON
: B_CONTROL_OFF
;
135 fShowTimeZone
->SetValue(fCachedShowTimeZone
);
136 fShowTimeZone
->SetEnabled(true);
139 // do this one last because it might disable the others
140 if (message
->FindBool("showClock", &showClock
) == B_OK
) {
141 fCachedShowClock
= showClock
? B_CONTROL_ON
: B_CONTROL_OFF
;
142 fShowClock
->SetValue(fCachedShowClock
);
143 fShowClock
->SetEnabled(true);
144 fShowSeconds
->SetEnabled(showClock
);
145 fShowDayOfWeek
->SetEnabled(showClock
);
146 fShowTimeZone
->SetEnabled(showClock
);
154 if (message
->FindBool("showClock", &showClock
) == B_OK
) {
155 // message originated from Deskbar, handle special
156 fShowClock
->SetValue(showClock
? B_CONTROL_ON
: B_CONTROL_OFF
);
157 fShowSeconds
->SetEnabled(showClock
);
158 fShowDayOfWeek
->SetEnabled(showClock
);
159 fShowTimeZone
->SetEnabled(showClock
);
161 Window()->PostMessage(kMsgChange
);
163 // don't fall through
165 showClock
= fShowClock
->Value() == B_CONTROL_ON
;
166 fShowSeconds
->SetEnabled(showClock
);
167 fShowDayOfWeek
->SetEnabled(showClock
);
168 fShowTimeZone
->SetEnabled(showClock
);
175 BMessenger
* messenger
= new BMessenger(kDeskbarSignature
);
176 messenger
->SendMessage(message
);
178 Window()->PostMessage(kMsgChange
);
188 BView::MessageReceived(message
);
195 ClockView::CheckCanRevert()
197 return fShowClock
->Value() != fCachedShowClock
198 || fShowSeconds
->Value() != fCachedShowSeconds
199 || fShowDayOfWeek
->Value() != fCachedShowDayOfWeek
200 || fShowTimeZone
->Value() != fCachedShowTimeZone
;
207 if (fShowClock
->Value() != fCachedShowClock
) {
208 fShowClock
->SetValue(fCachedShowClock
);
209 fShowClock
->Invoke();
212 if (fShowSeconds
->Value() != fCachedShowSeconds
) {
213 fShowSeconds
->SetValue(fCachedShowSeconds
);
214 fShowSeconds
->Invoke();
217 if (fShowDayOfWeek
->Value() != fCachedShowDayOfWeek
) {
218 fShowDayOfWeek
->SetValue(fCachedShowDayOfWeek
);
219 fShowDayOfWeek
->Invoke();
222 if (fShowTimeZone
->Value() != fCachedShowTimeZone
) {
223 fShowTimeZone
->SetValue(fCachedShowTimeZone
);
224 fShowTimeZone
->Invoke();