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
43 #include <Application.h>
48 #include <MessageRunner.h>
49 #include <PopUpMenu.h>
54 #include "CalendarMenuWindow.h"
57 static const char* const kMinString
= "99:99 AM";
58 static const float kHMargin
= 2.0;
61 #undef B_TRANSLATION_CONTEXT
62 #define B_TRANSLATION_CONTEXT "TimeView"
65 TTimeView::TTimeView(float maxWidth
, float height
)
67 BView(BRect(-100, -100, -90, -90), "_deskbar_tv_",
68 B_FOLLOW_RIGHT
| B_FOLLOW_TOP
,
69 B_WILL_DRAW
| B_PULSE_NEEDED
| B_FRAME_EVENTS
),
76 fShowDayOfWeek(false),
81 fCurrentTime
= fLastTime
= time(NULL
);
82 fSeconds
= fMinute
= fHour
= 0;
83 fCurrentTimeStr
[0] = 0;
84 fCurrentDateStr
[0] = 0;
93 TTimeView::TTimeView(BMessage
* data
)
98 fCurrentTime
= fLastTime
= time(NULL
);
99 data
->FindBool("seconds", &fShowSeconds
);
106 TTimeView::~TTimeView()
115 TTimeView::Instantiate(BMessage
* data
)
117 if (!validate_instantiation(data
, "TTimeView"))
120 return new TTimeView(data
);
125 TTimeView::Archive(BMessage
* data
, bool deep
) const
127 BView::Archive(data
, deep
);
128 data
->AddBool("orientation", fOrientation
);
129 data
->AddInt16("showLevel", fShowLevel
);
130 data
->AddBool("showSeconds", fShowSeconds
);
131 data
->AddBool("showDayOfWeek", fShowDayOfWeek
);
132 data
->AddBool("showTimeZone", fShowTimeZone
);
133 data
->AddInt32("deskbar:private_align", B_ALIGN_RIGHT
);
141 TTimeView::AttachedToWindow()
143 fCurrentTime
= time(NULL
);
145 SetFont(be_plain_font
);
150 SetViewUIColor(B_PANEL_BACKGROUND_COLOR
);
152 CalculateTextPlacement();
158 TTimeView::Draw(BRect
/*updateRect*/)
162 SetHighColor(ViewColor());
163 SetLowColor(ViewColor());
165 SetHighUIColor(B_MENU_ITEM_TEXT_COLOR
);
167 DrawString(fCurrentTimeStr
, fTimeLocation
);
174 TTimeView::FrameMoved(BPoint
)
181 TTimeView::GetPreferredSize(float* width
, float* height
)
187 float timeWidth
= StringWidth(fCurrentTimeStr
);
189 // TODO: SetOrientation never gets called, fix that when in vertical mode,
190 // we want to limit the width so that it can't overlap the bevels in the
192 *width
= fOrientation
? std::min(fMaxWidth
- kHMargin
, timeWidth
)
198 TTimeView::MessageReceived(BMessage
* message
)
200 switch (message
->what
) {
203 // launch the time prefs app
204 be_roster
->Launch("application/x-vnd.Haiku-Time");
205 // tell Time preflet to switch to the clock tab
206 BMessenger
messenger("application/x-vnd.Haiku-Time");
207 BMessage
switchToClock('SlCk');
208 messenger
.SendMessage(&switchToClock
);
214 be_app
->MessageReceived(message
);
220 BRect
bounds(Bounds());
221 BPoint
center(bounds
.LeftTop());
222 center
+= BPoint(bounds
.Width() / 2, bounds
.Height() / 2);
223 ShowCalendar(center
);
228 BView::MessageReceived(message
);
235 TTimeView::MouseDown(BPoint point
)
239 Window()->CurrentMessage()->FindInt32("buttons", (int32
*)&buttons
);
240 if (buttons
== B_SECONDARY_MOUSE_BUTTON
) {
241 ShowTimeOptions(ConvertToScreen(point
));
243 } else if (buttons
== B_PRIMARY_MOUSE_BUTTON
)
246 // invalidate last time/date strings and call the pulse
247 // method directly to change the display instantly
248 fLastDateStr
[0] = '\0';
249 fLastTimeStr
[0] = '\0';
257 time_t curTime
= time(NULL
);
258 tm
* ct
= localtime(&curTime
);
262 fCurrentTime
= curTime
;
266 if (strcmp(fCurrentTimeStr
, fLastTimeStr
) != 0) {
267 // Update bounds when the size of the strings has changed
270 strlcpy(fLastTimeStr
, fCurrentTimeStr
, sizeof(fLastTimeStr
));
271 fNeedToUpdate
= true;
274 // Update the tooltip if the date has changed
275 if (strcmp(fCurrentDateStr
, fLastDateStr
) != 0) {
276 strlcpy(fLastDateStr
, fCurrentDateStr
, sizeof(fLastDateStr
));
277 SetToolTip(fCurrentDateStr
);
281 fSeconds
= ct
->tm_sec
;
282 fMinute
= ct
->tm_min
;
286 fNeedToUpdate
= false;
292 TTimeView::ResizeToPreferred()
296 float oldWidth
= Bounds().Width();
297 float oldHeight
= Bounds().Height();
299 GetPreferredSize(&width
, &height
);
300 if (height
!= oldHeight
|| width
!= oldWidth
) {
301 ResizeTo(width
, height
);
302 MoveBy(oldWidth
- width
, 0);
303 fNeedToUpdate
= true;
308 // # pragma mark - Public methods
312 TTimeView::SetOrientation(bool orientation
)
314 fOrientation
= orientation
;
315 CalculateTextPlacement();
321 TTimeView::ShowSeconds() const
328 TTimeView::SetShowSeconds(bool show
)
337 TTimeView::ShowDayOfWeek() const
339 return fShowDayOfWeek
;
344 TTimeView::SetShowDayOfWeek(bool show
)
346 fShowDayOfWeek
= show
;
353 TTimeView::ShowTimeZone() const
355 return fShowTimeZone
;
360 TTimeView::SetShowTimeZone(bool show
)
362 fShowTimeZone
= show
;
369 TTimeView::ShowCalendar(BPoint where
)
371 if (fCalendarWindow
.IsValid()) {
372 // If the calendar is already shown, just activate it
373 BMessage
activate(B_SET_PROPERTY
);
374 activate
.AddSpecifier("Active");
375 activate
.AddBool("data", true);
377 if (fCalendarWindow
.SendMessage(&activate
) == B_OK
)
381 where
.y
= Bounds().bottom
+ 4.0;
382 ConvertToScreen(&where
);
384 if (where
.y
>= BScreen().Frame().bottom
)
385 where
.y
-= (Bounds().Height() + 4.0);
387 CalendarMenuWindow
* window
= new CalendarMenuWindow(where
);
388 fCalendarWindow
= BMessenger(window
);
394 // # pragma mark - Private methods
398 TTimeView::UpdateTimeFormat()
400 int32 fields
= B_DATE_ELEMENT_HOUR
| B_DATE_ELEMENT_MINUTE
;
402 fields
|= B_DATE_ELEMENT_SECOND
;
404 fields
|= B_DATE_ELEMENT_WEEKDAY
;
406 fields
|= B_DATE_ELEMENT_TIMEZONE
;
409 fTimeFormat
= new BDateTimeFormat(BLocale::Default());
410 fTimeFormat
->SetDateTimeFormat(B_SHORT_DATE_FORMAT
, B_SHORT_TIME_FORMAT
, fields
);
413 fDateFormat
= new BDateFormat(BLocale::Default());
417 TTimeView::GetCurrentTime()
419 fTimeFormat
->Format(fCurrentTimeStr
, sizeof(fCurrentTimeStr
), fCurrentTime
,
420 B_SHORT_DATE_FORMAT
, B_SHORT_TIME_FORMAT
);
425 TTimeView::GetCurrentDate()
427 char tmp
[sizeof(fCurrentDateStr
)];
429 fDateFormat
->Format(tmp
, sizeof(fCurrentDateStr
), fCurrentTime
,
432 // remove leading 0 from date when month is less than 10 (MM/DD/YY)
433 // or remove leading 0 from date when day is less than 10 (DD/MM/YY)
434 const char* str
= tmp
;
438 strlcpy(fCurrentDateStr
, str
, sizeof(fCurrentDateStr
));
443 TTimeView::CalculateTextPlacement()
445 BRect
bounds(Bounds());
447 fDateLocation
.x
= 0.0;
448 fTimeLocation
.x
= 0.0;
453 const char* stringArray
[1];
454 stringArray
[0] = fCurrentTimeStr
;
456 escapement_delta delta
= { 0.0, 0.0 };
457 font
.GetBoundingBoxesForStrings(stringArray
, 1, B_SCREEN_METRIC
, &delta
,
460 fTimeLocation
.y
= fDateLocation
.y
= ceilf((bounds
.Height()
461 - rectArray
[0].Height() + 1.0) / 2.0 - rectArray
[0].top
);
466 TTimeView::ShowTimeOptions(BPoint point
)
468 BPopUpMenu
* menu
= new BPopUpMenu("", false, false);
469 menu
->SetFont(be_plain_font
);
472 item
= new BMenuItem(B_TRANSLATE("Time preferences" B_UTF8_ELLIPSIS
),
473 new BMessage(kChangeTime
));
476 item
= new BMenuItem(B_TRANSLATE("Hide clock"),
477 new BMessage(kShowHideTime
));
480 item
= new BMenuItem(B_TRANSLATE("Show calendar" B_UTF8_ELLIPSIS
),
481 new BMessage(kShowCalendar
));
484 menu
->SetTargetForItems(this);
485 // Changed to accept screen coord system point;
486 // not constrained to this view now
487 menu
->Go(point
, true, true, BRect(point
.x
- 4, point
.y
- 4,
488 point
.x
+ 4, point
.y
+4), true);
497 SetToolTip(fCurrentDateStr
);
499 CalculateTextPlacement();
503 fParent
->Invalidate();