2 * Copyright 2008 Karsten Heimrich, host.haiku@gmx.de. All rights reserved.
3 * Distributed under the terms of the MIT License.
7 #include "CalendarMenuWindow.h"
10 #include <CalendarView.h>
11 #include <GridLayoutBuilder.h>
12 #include <GroupLayout.h>
13 #include <GroupLayoutBuilder.h>
14 #include <GroupView.h>
17 #include <SpaceLayoutItem.h>
19 #include <StringView.h>
22 using BPrivate::BCalendarView
;
33 // #pragma mark - FlatButton
36 class FlatButton
: public BButton
{
38 FlatButton(const BString
& label
, uint32 what
)
39 : BButton(label
.String(), new BMessage(what
)) {}
40 virtual ~FlatButton() {}
42 virtual void Draw(BRect updateRect
);
47 FlatButton::Draw(BRect updateRect
)
49 updateRect
= Bounds();
50 rgb_color highColor
= HighColor();
52 SetHighColor(ui_color(B_PANEL_BACKGROUND_COLOR
));
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
)
65 SetHighColor(highColor
);
66 DrawString(label
, BPoint(x
, labelY
));
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
),
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
);
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())
120 .SetInsets(5.0, 5.0, 5.0, 5.0)
122 groupView
->AddChild(view
);
126 _UpdateDate(BDate::CurrentDate(B_LOCAL_TIME
));
130 CalendarMenuWindow::~CalendarMenuWindow()
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);
160 fCalendarView
->MakeFocus(true);
167 CalendarMenuWindow::WindowActivated(bool active
)
172 if (mouse_mode() != B_FOCUS_FOLLOWS_MOUSE
) {
174 PostMessage(B_QUIT_REQUESTED
);
176 if (fSuppressFirstClose
&& !active
) {
177 fSuppressFirstClose
= false;
181 if (!fSuppressFirstClose
&& !active
)
182 PostMessage(B_QUIT_REQUESTED
);
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
));
202 case kMonthDownMessage
:
203 case kMonthUpMessage
:
205 BDate date
= fCalendarView
->Date();
206 date
.AddMonths(kMonthDownMessage
== message
->what
? -1 : 1);
211 case kYearDownMessage
:
214 BDate date
= fCalendarView
->Date();
215 date
.AddYears(kYearDownMessage
== message
->what
? -1 : 1);
221 BWindow::MessageReceived(message
);
228 CalendarMenuWindow::_UpdateDate(const BDate
& date
)
233 fCalendarView
->SetDate(date
);
237 fYearLabel
->SetText(text
.String());
239 fMonthLabel
->SetText(date
.LongMonthName(date
.Month()).String());
244 CalendarMenuWindow::_SetupButton(const char* label
, uint32 what
, float height
)
246 FlatButton
* button
= new FlatButton(label
, what
);
247 button
->SetExplicitMinSize(BSize(height
, height
));