btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / apps / deskbar / CalendarMenuWindow.cpp
blob14a314c80376a07d01aca8838ad78274a916f2a2
1 /*
2 * Copyright 2008 Karsten Heimrich, host.haiku@gmx.de. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "CalendarMenuWindow.h"
9 #include <Button.h>
10 #include <CalendarView.h>
11 #include <GridLayoutBuilder.h>
12 #include <GroupLayout.h>
13 #include <GroupLayoutBuilder.h>
14 #include <GroupView.h>
15 #include <Locale.h>
16 #include <Screen.h>
17 #include <SpaceLayoutItem.h>
18 #include <String.h>
19 #include <StringView.h>
22 using BPrivate::BCalendarView;
24 enum {
25 kInvokationMessage,
26 kMonthDownMessage,
27 kMonthUpMessage,
28 kYearDownMessage,
29 kYearUpMessage
33 // #pragma mark - FlatButton
36 class FlatButton : public BButton {
37 public:
38 FlatButton(const BString& label, uint32 what)
39 : BButton(label.String(), new BMessage(what)) {}
40 virtual ~FlatButton() {}
42 virtual void Draw(BRect updateRect);
46 void
47 FlatButton::Draw(BRect updateRect)
49 updateRect = Bounds();
50 rgb_color highColor = HighColor();
52 SetHighColor(ui_color(B_PANEL_BACKGROUND_COLOR));
53 FillRect(updateRect);
55 font_height fh;
56 GetFontHeight(&fh);
58 const char* label = Label();
59 const float stringWidth = StringWidth(label);
60 const float x = (updateRect.right - stringWidth) / 2.0f;
61 const float labelY = updateRect.top
62 + ((updateRect.Height() - fh.ascent - fh.descent) / 2.0f)
63 + fh.ascent + 1.0f;
65 SetHighColor(highColor);
66 DrawString(label, BPoint(x, labelY));
68 if (IsFocus()) {
69 SetHighColor(ui_color(B_KEYBOARD_NAVIGATION_COLOR));
70 StrokeRect(updateRect);
75 // #pragma mark - CalendarMenuWindow
78 CalendarMenuWindow::CalendarMenuWindow(BPoint where)
80 BWindow(BRect(0.0, 0.0, 100.0, 130.0), "", B_BORDERED_WINDOW,
81 B_AUTO_UPDATE_SIZE_LIMITS | B_ASYNCHRONOUS_CONTROLS | B_CLOSE_ON_ESCAPE
82 | B_NOT_MINIMIZABLE | B_NOT_ZOOMABLE),
83 fYearLabel(NULL),
84 fMonthLabel(NULL),
85 fCalendarView(NULL),
86 fSuppressFirstClose(true)
88 SetFeel(B_FLOATING_ALL_WINDOW_FEEL);
90 RemoveShortcut('H', B_COMMAND_KEY | B_CONTROL_KEY);
91 AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED));
93 fYearLabel = new BStringView("year", "");
94 fMonthLabel = new BStringView("month", "");
96 fCalendarView = new BCalendarView(Bounds(), "calendar", B_FOLLOW_ALL);
97 fCalendarView->SetInvocationMessage(new BMessage(kInvokationMessage));
99 BGroupLayout* layout = new BGroupLayout(B_HORIZONTAL);
100 SetLayout(layout);
102 float width, height;
103 fMonthLabel->GetPreferredSize(&width, &height);
105 BGridLayout* gridLayout = BGridLayoutBuilder(5.0)
106 .Add(_SetupButton("-", kMonthDownMessage, height), 0, 0)
107 .Add(fMonthLabel, 1, 0)
108 .Add(_SetupButton("+", kMonthUpMessage, height), 2, 0)
109 .Add(BSpaceLayoutItem::CreateGlue(), 3, 0)
110 .Add(_SetupButton("-", kYearDownMessage, height), 4, 0)
111 .Add(fYearLabel, 5, 0)
112 .Add(_SetupButton("+", kYearUpMessage, height), 6, 0)
113 .SetInsets(5.0, 0.0, 5.0, 0.0);
114 gridLayout->SetMinColumnWidth(1, be_plain_font->StringWidth("September"));
116 BGroupView* groupView = new BGroupView(B_VERTICAL, 10.0);
117 BView* view = BGroupLayoutBuilder(B_VERTICAL, 5.0)
118 .Add(gridLayout->View())
119 .Add(fCalendarView)
120 .SetInsets(5.0, 5.0, 5.0, 5.0)
121 .TopView();
122 groupView->AddChild(view);
123 AddChild(groupView);
125 MoveTo(where);
126 _UpdateDate(BDate::CurrentDate(B_LOCAL_TIME));
130 CalendarMenuWindow::~CalendarMenuWindow()
135 void
136 CalendarMenuWindow::Show()
138 BRect screen(BScreen().Frame());
140 float right = screen.right;
141 float bottom = screen.bottom;
143 BRect rightTop(right / 2.0, screen.top, right, bottom / 2.0);
144 BRect rightBottom(right / 2.0, bottom / 2.0, right + 1.0, bottom + 1.0);
145 BRect leftBottom(screen.left, bottom / 2.0, right / 2.0, bottom + 1.0);
147 BPoint where = Frame().LeftTop();
148 BSize size = GetLayout()->PreferredSize();
150 if (rightTop.Contains(where)) {
151 where.x -= size.width;
152 } else if (leftBottom.Contains(where)) {
153 where.y -= (size.height + 4.0);
154 } else if (rightBottom.Contains(where)) {
155 where.x -= size.width;
156 where.y -= (size.height + 4.0);
159 MoveTo(where);
160 fCalendarView->MakeFocus(true);
162 BWindow::Show();
166 void
167 CalendarMenuWindow::WindowActivated(bool active)
169 if (active)
170 return;
172 if (mouse_mode() != B_FOCUS_FOLLOWS_MOUSE) {
173 if (!active)
174 PostMessage(B_QUIT_REQUESTED);
175 } else {
176 if (fSuppressFirstClose && !active) {
177 fSuppressFirstClose = false;
178 return;
181 if (!fSuppressFirstClose && !active)
182 PostMessage(B_QUIT_REQUESTED);
187 void
188 CalendarMenuWindow::MessageReceived(BMessage* message)
190 switch (message->what) {
191 case kInvokationMessage:
193 int32 day, month, year;
194 message->FindInt32("day", &day);
195 message->FindInt32("month", &month);
196 message->FindInt32("year", &year);
198 _UpdateDate(BDate(year, month, day));
199 break;
202 case kMonthDownMessage:
203 case kMonthUpMessage:
205 BDate date = fCalendarView->Date();
206 date.AddMonths(kMonthDownMessage == message->what ? -1 : 1);
207 _UpdateDate(date);
208 break;
211 case kYearDownMessage:
212 case kYearUpMessage:
214 BDate date = fCalendarView->Date();
215 date.AddYears(kYearDownMessage == message->what ? -1 : 1);
216 _UpdateDate(date);
217 break;
220 default:
221 BWindow::MessageReceived(message);
222 break;
227 void
228 CalendarMenuWindow::_UpdateDate(const BDate& date)
230 if (!date.IsValid())
231 return;
233 fCalendarView->SetDate(date);
235 BString text;
236 text << date.Year();
237 fYearLabel->SetText(text.String());
239 fMonthLabel->SetText(date.LongMonthName(date.Month()).String());
243 BButton*
244 CalendarMenuWindow::_SetupButton(const char* label, uint32 what, float height)
246 FlatButton* button = new FlatButton(label, what);
247 button->SetExplicitMinSize(BSize(height, height));
249 return button;