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
41 #include <Application.h>
46 #include <MessageRunner.h>
47 #include <PopUpMenu.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
),
74 fShowDayOfWeek(false),
77 fCurrentTime
= fLastTime
= time(NULL
);
78 fSeconds
= fMinute
= fHour
= 0;
79 fCurrentTimeStr
[0] = 0;
80 fCurrentDateStr
[0] = 0;
84 fLocale
= *BLocale::Default();
89 TTimeView::TTimeView(BMessage
* data
)
92 fCurrentTime
= fLastTime
= time(NULL
);
93 data
->FindBool("seconds", &fShowSeconds
);
95 fLocale
= *BLocale::Default();
100 TTimeView::~TTimeView()
107 TTimeView::Instantiate(BMessage
* data
)
109 if (!validate_instantiation(data
, "TTimeView"))
112 return new TTimeView(data
);
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
);
133 TTimeView::AttachedToWindow()
135 fCurrentTime
= time(NULL
);
137 SetFont(be_plain_font
);
140 SetViewColor(Parent()->ViewColor());
142 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR
));
144 CalculateTextPlacement();
150 TTimeView::Draw(BRect
/*updateRect*/)
154 SetHighColor(ViewColor());
155 SetLowColor(ViewColor());
157 SetHighColor(ui_color(B_MENU_ITEM_TEXT_COLOR
));
159 DrawString(fCurrentTimeStr
, fTimeLocation
);
166 TTimeView::FrameMoved(BPoint
)
173 TTimeView::GetPreferredSize(float* width
, float* height
)
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
182 *width
= fOrientation
?
183 min_c(fMaxWidth
- kHMargin
, kHMargin
+ StringWidth(fCurrentTimeStr
))
184 : kHMargin
+ StringWidth(fCurrentTimeStr
);
189 TTimeView::MessageReceived(BMessage
* message
)
191 switch (message
->what
) {
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
);
205 be_app
->MessageReceived(message
);
211 BRect
bounds(Bounds());
212 BPoint
center(bounds
.LeftTop());
213 center
+= BPoint(bounds
.Width() / 2, bounds
.Height() / 2);
214 ShowCalendar(center
);
219 BView::MessageReceived(message
);
226 TTimeView::MouseDown(BPoint point
)
230 Window()->CurrentMessage()->FindInt32("buttons", (int32
*)&buttons
);
231 if (buttons
== B_SECONDARY_MOUSE_BUTTON
) {
232 ShowTimeOptions(ConvertToScreen(point
));
234 } else if (buttons
== B_PRIMARY_MOUSE_BUTTON
)
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';
248 time_t curTime
= time(NULL
);
249 tm
* ct
= localtime(&curTime
);
253 fCurrentTime
= curTime
;
257 if (strcmp(fCurrentTimeStr
, fLastTimeStr
) != 0) {
258 // Update bounds when the size of the strings has changed
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
);
272 fSeconds
= ct
->tm_sec
;
273 fMinute
= ct
->tm_min
;
277 fNeedToUpdate
= false;
283 TTimeView::ResizeToPreferred()
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
301 TTimeView::SetOrientation(bool orientation
)
303 fOrientation
= orientation
;
304 CalculateTextPlacement();
310 TTimeView::ShowSeconds() const
317 TTimeView::SetShowSeconds(bool show
)
325 TTimeView::ShowDayOfWeek() const
327 return fShowDayOfWeek
;
332 TTimeView::SetShowDayOfWeek(bool show
)
334 fShowDayOfWeek
= show
;
340 TTimeView::ShowTimeZone() const
342 return fShowTimeZone
;
347 TTimeView::SetShowTimeZone(bool show
)
349 fShowTimeZone
= show
;
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
)
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
);
380 // # pragma mark - Private methods
384 TTimeView::GetCurrentTime()
386 int32 fields
= B_DATE_ELEMENT_HOUR
| B_DATE_ELEMENT_MINUTE
;
388 fields
|= B_DATE_ELEMENT_SECOND
;
390 fields
|= B_DATE_ELEMENT_WEEKDAY
;
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
);
403 TTimeView::GetCurrentDate()
405 char tmp
[sizeof(fCurrentDateStr
)];
407 BDateFormat
format(&fLocale
);
408 format
.Format(tmp
, sizeof(fCurrentDateStr
), fCurrentTime
,
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
;
417 strlcpy(fCurrentDateStr
, str
, sizeof(fCurrentDateStr
));
422 TTimeView::CalculateTextPlacement()
424 BRect
bounds(Bounds());
426 fDateLocation
.x
= 0.0;
427 fTimeLocation
.x
= 0.0;
432 const char* stringArray
[1];
433 stringArray
[0] = fCurrentTimeStr
;
435 escapement_delta delta
= { 0.0, 0.0 };
436 font
.GetBoundingBoxesForStrings(stringArray
, 1, B_SCREEN_METRIC
, &delta
,
439 fTimeLocation
.y
= fDateLocation
.y
= ceilf((bounds
.Height()
440 - rectArray
[0].Height() + 1.0) / 2.0 - rectArray
[0].top
);
445 TTimeView::ShowTimeOptions(BPoint point
)
447 BPopUpMenu
* menu
= new BPopUpMenu("", false, false);
448 menu
->SetFont(be_plain_font
);
451 item
= new BMenuItem(B_TRANSLATE("Time preferences" B_UTF8_ELLIPSIS
),
452 new BMessage(kChangeTime
));
455 item
= new BMenuItem(B_TRANSLATE("Hide clock"),
456 new BMessage(kShowHideTime
));
459 item
= new BMenuItem(B_TRANSLATE("Show calendar" B_UTF8_ELLIPSIS
),
460 new BMessage(kShowCalendar
));
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);
474 fLocale
= *BLocale::Default();
478 SetToolTip(fCurrentDateStr
);
480 CalculateTextPlacement();
484 fParent
->Invalidate();