BPicture: Fix archive constructor.
[haiku.git] / src / apps / deskbar / TimeView.cpp
blob5a96ed8d6400477267c766bdb8fdfa2c143140ae
1 /*
2 Open Tracker License
4 Terms and Conditions
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered
30 trademarks of Be Incorporated in the United States and other countries. Other
31 brand product names are registered trademarks or trademarks of their respective
32 holders.
33 All rights reserved.
37 #include "TimeView.h"
39 #include <string.h>
41 #include <Application.h>
42 #include <Catalog.h>
43 #include <Debug.h>
44 #include <Locale.h>
45 #include <MenuItem.h>
46 #include <MessageRunner.h>
47 #include <PopUpMenu.h>
48 #include <Roster.h>
49 #include <Screen.h>
50 #include <Window.h>
52 #include "CalendarMenuWindow.h"
55 static const char* const kMinString = "99:99 AM";
56 static const float kHMargin = 2.0;
59 #undef B_TRANSLATION_CONTEXT
60 #define B_TRANSLATION_CONTEXT "TimeView"
63 TTimeView::TTimeView(float maxWidth, float height)
65 BView(BRect(-100, -100, -90, -90), "_deskbar_tv_",
66 B_FOLLOW_RIGHT | B_FOLLOW_TOP,
67 B_WILL_DRAW | B_PULSE_NEEDED | B_FRAME_EVENTS),
68 fParent(NULL),
69 fMaxWidth(maxWidth),
70 fHeight(height),
71 fOrientation(true),
72 fShowLevel(0),
73 fShowSeconds(false),
74 fShowDayOfWeek(false),
75 fShowTimeZone(false)
77 fCurrentTime = fLastTime = time(NULL);
78 fSeconds = fMinute = fHour = 0;
79 fCurrentTimeStr[0] = 0;
80 fCurrentDateStr[0] = 0;
81 fLastTimeStr[0] = 0;
82 fLastDateStr[0] = 0;
83 fNeedToUpdate = true;
84 fLocale = *BLocale::Default();
88 #ifdef AS_REPLICANT
89 TTimeView::TTimeView(BMessage* data)
90 : BView(data)
92 fCurrentTime = fLastTime = time(NULL);
93 data->FindBool("seconds", &fShowSeconds);
95 fLocale = *BLocale::Default();
97 #endif
100 TTimeView::~TTimeView()
105 #ifdef AS_REPLICANT
106 BArchivable*
107 TTimeView::Instantiate(BMessage* data)
109 if (!validate_instantiation(data, "TTimeView"))
110 return NULL;
112 return new TTimeView(data);
116 status_t
117 TTimeView::Archive(BMessage* data, bool deep) const
119 BView::Archive(data, deep);
120 data->AddBool("orientation", fOrientation);
121 data->AddInt16("showLevel", fShowLevel);
122 data->AddBool("showSeconds", fShowSeconds);
123 data->AddBool("showDayOfWeek", fShowDayOfWeek);
124 data->AddBool("showTimeZone", fShowTimeZone);
125 data->AddInt32("deskbar:private_align", B_ALIGN_RIGHT);
127 return B_OK;
129 #endif
132 void
133 TTimeView::AttachedToWindow()
135 fCurrentTime = time(NULL);
137 SetFont(be_plain_font);
138 if (Parent()) {
139 fParent = Parent();
140 SetViewColor(Parent()->ViewColor());
141 } else
142 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
144 CalculateTextPlacement();
145 ResizeToPreferred();
149 void
150 TTimeView::Draw(BRect /*updateRect*/)
152 PushState();
154 SetHighColor(ViewColor());
155 SetLowColor(ViewColor());
156 FillRect(Bounds());
157 SetHighColor(ui_color(B_MENU_ITEM_TEXT_COLOR));
159 DrawString(fCurrentTimeStr, fTimeLocation);
161 PopState();
165 void
166 TTimeView::FrameMoved(BPoint)
168 Update();
172 void
173 TTimeView::GetPreferredSize(float* width, float* height)
175 *height = fHeight;
177 GetCurrentTime();
179 // TODO: SetOrientation never gets called, fix that when in vertical mode,
180 // we want to limit the width so that it can't overlap the bevels in the
181 // parent view.
182 *width = fOrientation ?
183 min_c(fMaxWidth - kHMargin, kHMargin + StringWidth(fCurrentTimeStr))
184 : kHMargin + StringWidth(fCurrentTimeStr);
188 void
189 TTimeView::MessageReceived(BMessage* message)
191 switch (message->what) {
192 case kChangeTime:
194 // launch the time prefs app
195 be_roster->Launch("application/x-vnd.Haiku-Time");
196 // tell Time preflet to switch to the clock tab
197 BMessenger messenger("application/x-vnd.Haiku-Time");
198 BMessage switchToClock('SlCk');
199 messenger.SendMessage(&switchToClock);
200 break;
203 case kShowHideTime:
205 be_app->MessageReceived(message);
206 break;
209 case kShowCalendar:
211 BRect bounds(Bounds());
212 BPoint center(bounds.LeftTop());
213 center += BPoint(bounds.Width() / 2, bounds.Height() / 2);
214 ShowCalendar(center);
215 break;
218 default:
219 BView::MessageReceived(message);
220 break;
225 void
226 TTimeView::MouseDown(BPoint point)
228 uint32 buttons;
230 Window()->CurrentMessage()->FindInt32("buttons", (int32*)&buttons);
231 if (buttons == B_SECONDARY_MOUSE_BUTTON) {
232 ShowTimeOptions(ConvertToScreen(point));
233 return;
234 } else if (buttons == B_PRIMARY_MOUSE_BUTTON)
235 ShowCalendar(point);
237 // invalidate last time/date strings and call the pulse
238 // method directly to change the display instantly
239 fLastDateStr[0] = '\0';
240 fLastTimeStr[0] = '\0';
241 Pulse();
245 void
246 TTimeView::Pulse()
248 time_t curTime = time(NULL);
249 tm* ct = localtime(&curTime);
250 if (ct == NULL)
251 return;
253 fCurrentTime = curTime;
255 GetCurrentTime();
256 GetCurrentDate();
257 if (strcmp(fCurrentTimeStr, fLastTimeStr) != 0) {
258 // Update bounds when the size of the strings has changed
259 Update();
261 strlcpy(fLastTimeStr, fCurrentTimeStr, sizeof(fLastTimeStr));
262 fNeedToUpdate = true;
265 // Update the tooltip if the date has changed
266 if (strcmp(fCurrentDateStr, fLastDateStr) != 0) {
267 strlcpy(fLastDateStr, fCurrentDateStr, sizeof(fLastDateStr));
268 SetToolTip(fCurrentDateStr);
271 if (fNeedToUpdate) {
272 fSeconds = ct->tm_sec;
273 fMinute = ct->tm_min;
274 fHour = ct->tm_hour;
276 Draw(Bounds());
277 fNeedToUpdate = false;
282 void
283 TTimeView::ResizeToPreferred()
285 float width, height;
286 float oldWidth = Bounds().Width(), oldHeight = Bounds().Height();
288 GetPreferredSize(&width, &height);
289 if (height != oldHeight || width != oldWidth) {
290 ResizeTo(width, height);
291 MoveBy(oldWidth - width, 0);
292 fNeedToUpdate = true;
297 // # pragma mark - Public methods
300 void
301 TTimeView::SetOrientation(bool orientation)
303 fOrientation = orientation;
304 CalculateTextPlacement();
305 Invalidate();
309 bool
310 TTimeView::ShowSeconds() const
312 return fShowSeconds;
316 void
317 TTimeView::SetShowSeconds(bool show)
319 fShowSeconds = show;
320 Update();
324 bool
325 TTimeView::ShowDayOfWeek() const
327 return fShowDayOfWeek;
331 void
332 TTimeView::SetShowDayOfWeek(bool show)
334 fShowDayOfWeek = show;
335 Update();
339 bool
340 TTimeView::ShowTimeZone() const
342 return fShowTimeZone;
346 void
347 TTimeView::SetShowTimeZone(bool show)
349 fShowTimeZone = show;
350 Update();
354 void
355 TTimeView::ShowCalendar(BPoint where)
357 if (fCalendarWindow.IsValid()) {
358 // If the calendar is already shown, just activate it
359 BMessage activate(B_SET_PROPERTY);
360 activate.AddSpecifier("Active");
361 activate.AddBool("data", true);
363 if (fCalendarWindow.SendMessage(&activate) == B_OK)
364 return;
367 where.y = Bounds().bottom + 4.0;
368 ConvertToScreen(&where);
370 if (where.y >= BScreen().Frame().bottom)
371 where.y -= (Bounds().Height() + 4.0);
373 CalendarMenuWindow* window = new CalendarMenuWindow(where);
374 fCalendarWindow = BMessenger(window);
376 window->Show();
380 // # pragma mark - Private methods
383 void
384 TTimeView::GetCurrentTime()
386 int32 fields = B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE;
387 if (fShowSeconds)
388 fields |= B_DATE_ELEMENT_SECOND;
389 if (fShowDayOfWeek)
390 fields |= B_DATE_ELEMENT_WEEKDAY;
391 if (fShowTimeZone)
392 fields |= B_DATE_ELEMENT_TIMEZONE;
394 BDateTimeFormat format(&fLocale);
395 format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, fields);
397 format.Format(fCurrentTimeStr, sizeof(fCurrentTimeStr), fCurrentTime,
398 B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT);
402 void
403 TTimeView::GetCurrentDate()
405 char tmp[sizeof(fCurrentDateStr)];
407 BDateFormat format(&fLocale);
408 format.Format(tmp, sizeof(fCurrentDateStr), fCurrentTime,
409 B_FULL_DATE_FORMAT);
411 // remove leading 0 from date when month is less than 10 (MM/DD/YY)
412 // or remove leading 0 from date when day is less than 10 (DD/MM/YY)
413 const char* str = tmp;
414 if (str[0] == '0')
415 str++;
417 strlcpy(fCurrentDateStr, str, sizeof(fCurrentDateStr));
421 void
422 TTimeView::CalculateTextPlacement()
424 BRect bounds(Bounds());
426 fDateLocation.x = 0.0;
427 fTimeLocation.x = 0.0;
429 BFont font;
430 GetFont(&font);
432 const char* stringArray[1];
433 stringArray[0] = fCurrentTimeStr;
434 BRect rectArray[1];
435 escapement_delta delta = { 0.0, 0.0 };
436 font.GetBoundingBoxesForStrings(stringArray, 1, B_SCREEN_METRIC, &delta,
437 rectArray);
439 fTimeLocation.y = fDateLocation.y = ceilf((bounds.Height()
440 - rectArray[0].Height() + 1.0) / 2.0 - rectArray[0].top);
444 void
445 TTimeView::ShowTimeOptions(BPoint point)
447 BPopUpMenu* menu = new BPopUpMenu("", false, false);
448 menu->SetFont(be_plain_font);
449 BMenuItem* item;
451 item = new BMenuItem(B_TRANSLATE("Time preferences" B_UTF8_ELLIPSIS),
452 new BMessage(kChangeTime));
453 menu->AddItem(item);
455 item = new BMenuItem(B_TRANSLATE("Hide clock"),
456 new BMessage(kShowHideTime));
457 menu->AddItem(item);
459 item = new BMenuItem(B_TRANSLATE("Show calendar" B_UTF8_ELLIPSIS),
460 new BMessage(kShowCalendar));
461 menu->AddItem(item);
463 menu->SetTargetForItems(this);
464 // Changed to accept screen coord system point;
465 // not constrained to this view now
466 menu->Go(point, true, true, BRect(point.x - 4, point.y - 4,
467 point.x + 4, point.y +4), true);
471 void
472 TTimeView::Update()
474 fLocale = *BLocale::Default();
476 GetCurrentTime();
477 GetCurrentDate();
478 SetToolTip(fCurrentDateStr);
480 CalculateTextPlacement();
481 ResizeToPreferred();
483 if (fParent != NULL)
484 fParent->Invalidate();