1/*
2 * Copyright 2007-2011, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _CALENDAR_VIEW_H_
6#define _CALENDAR_VIEW_H_
7
8
9#include "DateTime.h"
10
11
12#include <DateFormat.h>
13#include <Invoker.h>
14#include <List.h>
15#include <Locale.h>
16#include <String.h>
17#include <View.h>
18
19
20class BMessage;
21
22
23namespace BPrivate {
24
25
26class BCalendarView : public BView, public BInvoker {
27public:
28								BCalendarView(BRect frame, const char* name,
29									uint32 resizeMask = B_FOLLOW_LEFT_TOP,
30									uint32 flags = B_WILL_DRAW | B_FRAME_EVENTS
31										| B_NAVIGABLE | B_PULSE_NEEDED);
32
33								BCalendarView(const char* name,
34									uint32 flags = B_WILL_DRAW | B_FRAME_EVENTS
35										| B_NAVIGABLE | B_PULSE_NEEDED);
36
37	virtual						~BCalendarView();
38
39								BCalendarView(BMessage* archive);
40	static 	BArchivable*		Instantiate(BMessage* archive);
41	virtual status_t			Archive(BMessage* archive,
42									bool deep = true) const;
43
44	virtual void				AttachedToWindow();
45
46	virtual void				FrameResized(float width, float height);
47
48	virtual void				Draw(BRect updateRect);
49
50	virtual void				DrawDay(BView* owner, BRect frame,
51									const char* text, bool isSelected = false,
52									bool isEnabled = true, bool focus = false,
53									bool highlight = false);
54	virtual void				DrawDayName(BView* owner, BRect frame,
55									const char* text);
56	virtual void				DrawWeekNumber(BView* owner, BRect frame,
57									const char* text);
58
59			uint32				SelectionCommand() const;
60			BMessage*			SelectionMessage() const;
61	virtual void				SetSelectionMessage(BMessage* message);
62
63			uint32				InvocationCommand() const;
64			BMessage*			InvocationMessage() const;
65	virtual void				SetInvocationMessage(BMessage* message);
66
67	virtual void				MakeFocus(bool state = true);
68	virtual status_t			Invoke(BMessage* message = NULL);
69
70	virtual void				MouseDown(BPoint where);
71
72	virtual void				KeyDown(const char* bytes, int32 numBytes);
73
74	virtual void				Pulse();
75
76	virtual void				ResizeToPreferred();
77	virtual void				GetPreferredSize(float* width, float* height);
78
79	virtual	BSize				MaxSize();
80	virtual	BSize				MinSize();
81	virtual	BSize				PreferredSize();
82
83			int32				Day() const;
84			int32				Month() const;
85			int32				Year() const;
86
87			bool				SetDay(int32 day);
88			bool				SetMonth(int32 month);
89			bool				SetYear(int32 year);
90
91			BDate				Date() const;
92			bool				SetDate(const BDate& date);
93			bool				SetDate(int32 year, int32 month, int32 day);
94
95			BWeekday			StartOfWeek() const;
96			void				SetStartOfWeek(BWeekday startOfWeek);
97
98			bool				IsDayNameHeaderVisible() const;
99			void				SetDayNameHeaderVisible(bool visible);
100			void				UpdateDayNameHeader();
101
102			bool				IsWeekNumberHeaderVisible() const;
103			void				SetWeekNumberHeaderVisible(bool visible);
104
105private:
106			struct 				Selection {
107									Selection()
108										: row(0), column(0)
109									{
110									}
111
112									void
113									SetTo(int32 _row, int32 _column)
114									{
115										row = _row;
116										column = _column;
117									}
118
119									int32 row;
120									int32 column;
121
122									Selection& operator=(const Selection& s)
123									{
124										row = s.row;
125										column = s.column;
126										return *this;
127									}
128
129									bool operator==(const Selection& s) const
130									{
131										return row == s.row
132											&& column == s.column;
133									}
134
135									bool operator!=(const Selection& s) const
136									{
137										return row != s.row
138											|| column != s.column;
139									}
140								};
141
142			void				_InitObject();
143
144			void				_SetToDay();
145			void				_SetToCurrentDay();
146			void				_GetYearMonthForSelection(
147									const Selection& selection, int32* year,
148									int32* month) const;
149			void				_GetPreferredSize(float* width, float* height);
150
151			void				_SetupDayNames();
152			void				_SetupDayNumbers();
153			void				_SetupWeekNumbers();
154
155			void				_PopulateDayNames(BDateFormatStyle style);
156
157			void				_DrawDays();
158			void				_DrawFocusRect();
159			void				_DrawDayHeader();
160			void				_DrawWeekHeader();
161			void				_DrawDay(int32 curRow, int32 curColumn,
162									int32 row, int32 column, int32 counter,
163									BRect frame, const char* text,
164									bool focus = false, bool highlight = false);
165			void				_DrawItem(BView* owner, BRect frame,
166									const char* text, bool isSelected = false,
167									bool isEnabled = true, bool focus = false,
168									bool highlight = false);
169
170			void				_UpdateSelection();
171			void				_UpdateCurrentDay();
172			void				_UpdateCurrentDate();
173
174			BRect				_FirstCalendarItemFrame() const;
175			BRect				_SetNewSelectedDay(const BPoint& where);
176
177			BRect				_RectOfDay(const Selection& selection) const;
178
179private:
180			BMessage*			fSelectionMessage;
181
182			BDate				fDate;
183			BDate				fCurrentDate;
184
185			Selection			fFocusedDay;
186			Selection			fNewFocusedDay;
187			bool				fFocusChanged;
188
189			Selection			fSelectedDay;
190			Selection			fNewSelectedDay;
191			bool				fSelectionChanged;
192
193			Selection			fCurrentDay;
194			Selection			fNewCurrentDay;
195			bool				fCurrentDayChanged;
196
197			int32				fStartOfWeek;
198			bool				fDayNameHeaderVisible;
199			bool				fWeekNumberHeaderVisible;
200
201			BString				fDayNames[7];
202			BString				fWeekNumbers[6];
203			BString				fDayNumbers[6][7];
204
205			// hide copy constructor & assignment
206								BCalendarView(const BCalendarView& view);
207			BCalendarView&		operator=(const BCalendarView& view);
208};
209
210
211}	// namespace BPrivate
212
213
214#endif	// _CALENDAR_VIEW_H_
215