xref: /haiku/headers/private/shared/CalendarView.h (revision bf57c148f7787f0df15980976997c6dfb70ee067)
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 
20 class BMessage;
21 
22 
23 namespace BPrivate {
24 
25 
26 class BCalendarView : public BView, public BInvoker {
27 public:
28 								BCalendarView(BRect frame, const char* name,
29 									uint32 resizeMask = B_FOLLOW_LEFT
30 										| B_FOLLOW_TOP,
31 									uint32 flags = B_WILL_DRAW | B_FRAME_EVENTS
32 										| B_NAVIGABLE);
33 
34 								BCalendarView(const char* name,
35 									uint32 flags = B_WILL_DRAW | B_FRAME_EVENTS
36 										| B_NAVIGABLE);
37 
38 	virtual						~BCalendarView();
39 
40 								BCalendarView(BMessage* archive);
41 	static 	BArchivable*		Instantiate(BMessage* archive);
42 	virtual status_t			Archive(BMessage* archive,
43 									bool deep = true) const;
44 
45 	virtual void				AttachedToWindow();
46 
47 	virtual void				FrameResized(float width, float height);
48 
49 	virtual void				Draw(BRect updateRect);
50 
51 	virtual void				DrawDay(BView* owner, BRect frame,
52 									const char* text, bool isSelected = false,
53 									bool isEnabled = true, bool focus = 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				ResizeToPreferred();
75 	virtual void				GetPreferredSize(float* width, float* height);
76 
77 	virtual	BSize				MaxSize();
78 	virtual	BSize				MinSize();
79 	virtual	BSize				PreferredSize();
80 
81 			int32				Day() const;
82 			int32				Month() const;
83 			int32				Year() const;
84 
85 			bool				SetDay(int32 day);
86 			bool				SetMonth(int32 month);
87 			bool				SetYear(int32 year);
88 
89 			BDate				Date() const;
90 			bool				SetDate(const BDate& date);
91 			bool				SetDate(int32 year, int32 month, int32 day);
92 
93 			BWeekday			StartOfWeek() const;
94 			void				SetStartOfWeek(BWeekday startOfWeek);
95 
96 			bool				IsDayNameHeaderVisible() const;
97 			void				SetDayNameHeaderVisible(bool visible);
98 
99 			bool				IsWeekNumberHeaderVisible() const;
100 			void				SetWeekNumberHeaderVisible(bool visible);
101 
102 private:
103 			struct 				Selection {
104 									Selection()
105 										: row(0), column(0)
106 									{
107 									}
108 
109 									void
110 									SetTo(int32 _row, int32 _column)
111 									{
112 										row = _row;
113 										column = _column;
114 									}
115 
116 									int32 row;
117 									int32 column;
118 
119 									Selection& operator=(const Selection& s)
120 									{
121 										row = s.row;
122 										column = s.column;
123 										return *this;
124 									}
125 
126 									bool operator==(const Selection& s) const
127 									{
128 										return row == s.row
129 											&& column == s.column;
130 									}
131 
132 									bool operator!=(const Selection& s) const
133 									{
134 										return row != s.row
135 											|| column != s.column;
136 									}
137 								};
138 
139 			void				_InitObject();
140 
141 			void				_SetToDay();
142 			void				_GetYearMonthForSelection(
143 									const Selection& selection, int32* year,
144 									int32* month) const;
145 			void				_GetPreferredSize(float* width, float* height);
146 
147 			void				_SetupDayNames();
148 			void				_SetupDayNumbers();
149 			void				_SetupWeekNumbers();
150 
151 			void				_DrawDays();
152 			void				_DrawFocusRect();
153 			void				_DrawDayHeader();
154 			void				_DrawWeekHeader();
155 			void				_DrawDay(int32 curRow, int32 curColumn,
156 									int32 row, int32 column, int32 counter,
157 									BRect frame, const char* text,
158 									bool focus = false);
159 			void				_DrawItem(BView* owner, BRect frame,
160 									const char* text, bool isSelected = false,
161 									bool isEnabled = true, bool focus = false);
162 
163 			void				_UpdateSelection();
164 			BRect				_FirstCalendarItemFrame() const;
165 			BRect				_SetNewSelectedDay(const BPoint& where);
166 
167 			BRect				_RectOfDay(const Selection& selection) const;
168 
169 private:
170 			BMessage*			fSelectionMessage;
171 
172 			BDate				fDate;
173 
174 			Selection			fFocusedDay;
175 			Selection			fNewFocusedDay;
176 			bool				fFocusChanged;
177 
178 			Selection			fSelectedDay;
179 			Selection			fNewSelectedDay;
180 			bool				fSelectionChanged;
181 
182 			int32				fStartOfWeek;
183 			bool				fDayNameHeaderVisible;
184 			bool				fWeekNumberHeaderVisible;
185 
186 			BString				fDayNames[7];
187 			BString				fWeekNumbers[6];
188 			BString				fDayNumbers[6][7];
189 
190 			// hide copy constructor & assignment
191 								BCalendarView(const BCalendarView& view);
192 			BCalendarView&		operator=(const BCalendarView& view);
193 };
194 
195 
196 }	// namespace BPrivate
197 
198 
199 #endif	// _CALENDAR_VIEW_H_
200