xref: /haiku/headers/private/shared/CalendarView.h (revision 87663db420980bdfe50101b6d067dea5e4ded7b2)
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 <Invoker.h>
13 #include <List.h>
14 #include <String.h>
15 #include <View.h>
16 
17 
18 class BMessage;
19 
20 
21 namespace BPrivate {
22 
23 
24 enum week_start {
25 	B_WEEK_START_MONDAY,
26 	B_WEEK_START_SUNDAY
27 };
28 
29 
30 class BCalendarView : public BView, public BInvoker {
31 	public:
32 								BCalendarView(BRect frame, const char *name,
33 									uint32 resizeMask = B_FOLLOW_LEFT
34 										| B_FOLLOW_TOP,
35 									uint32 flags = B_WILL_DRAW | B_FRAME_EVENTS
36 										| B_NAVIGABLE);
37 
38 								BCalendarView(BRect frame, const char *name,
39 									week_start start,
40 									uint32 resizeMask = B_FOLLOW_LEFT
41 										| B_FOLLOW_TOP,
42 									uint32 flags = B_WILL_DRAW | B_FRAME_EVENTS
43 										| B_NAVIGABLE);
44 
45 								BCalendarView(const char* name,
46 									uint32 flags = B_WILL_DRAW | B_FRAME_EVENTS
47 										| B_NAVIGABLE);
48 
49 								BCalendarView(const char* name,
50 									week_start start, uint32 flags = B_WILL_DRAW
51 										| B_FRAME_EVENTS | B_NAVIGABLE);
52 
53 		virtual					~BCalendarView();
54 
55 								BCalendarView(BMessage *archive);
56 		static BArchivable*		Instantiate(BMessage *archive);
57 		virtual status_t		Archive(BMessage *archive,
58 									bool deep = true) const;
59 
60 		virtual void			AttachedToWindow();
61 		virtual	void			DetachedFromWindow();
62 
63 		virtual void			AllAttached();
64 		virtual void			AllDetached();
65 
66 		virtual void			FrameMoved(BPoint newPosition);
67 		virtual void			FrameResized(float width, float height);
68 
69 		virtual void			Draw(BRect updateRect);
70 
71 		virtual void			DrawDay(BView *owner, BRect frame,
72 									const char *text, bool isSelected = false,
73 									bool isEnabled = true, bool focus = false);
74 		virtual void			DrawDayName(BView *owner, BRect frame,
75 									const char *text);
76 		virtual void			DrawWeekNumber(BView *owner, BRect frame,
77 									const char *text);
78 
79 		virtual void			MessageReceived(BMessage *message);
80 
81 		uint32					SelectionCommand() const;
82 		BMessage*				SelectionMessage() const;
83 		virtual void			SetSelectionMessage(BMessage *message);
84 
85 		uint32					InvocationCommand() const;
86 		BMessage*				InvocationMessage() const;
87 		virtual void			SetInvocationMessage(BMessage *message);
88 
89 		virtual void			WindowActivated(bool state);
90 		virtual void			MakeFocus(bool state = true);
91 		virtual status_t		Invoke(BMessage* message = NULL);
92 
93 		virtual void			MouseUp(BPoint point);
94 		virtual void			MouseDown(BPoint where);
95 		virtual void			MouseMoved(BPoint point, uint32 code,
96 									const BMessage *dragMessage);
97 
98 		virtual void			KeyDown(const char *bytes, int32 numBytes);
99 
100 		virtual BHandler*		ResolveSpecifier(BMessage *message, int32 index,
101 									BMessage *specifier, int32 form,
102 									const char *property);
103 		virtual status_t		GetSupportedSuites(BMessage *data);
104 		virtual status_t		Perform(perform_code code, void* arg);
105 
106 		virtual void			ResizeToPreferred();
107 		virtual void			GetPreferredSize(float *width, float *height);
108 
109 		virtual	BSize			MaxSize();
110 		virtual	BSize			MinSize();
111 		virtual	BSize			PreferredSize();
112 
113 		int32					Day() const;
114 		int32					Year() const;
115 		int32					Month() const;
116 
117 		BDate					Date() const;
118 		bool					SetDate(const BDate &date);
119 		bool					SetDate(int32 year, int32 month, int32 day);
120 
121 		week_start				WeekStart() const;
122 		void					SetWeekStart(week_start start);
123 
124 		bool					IsDayNameHeaderVisible() const;
125 		void					SetDayNameHeaderVisible(bool visible);
126 
127 		bool					IsWeekNumberHeaderVisible() const;
128 		void					SetWeekNumberHeaderVisible(bool visible);
129 
130 	private:
131 		void					_InitObject();
132 
133 		void					_SetToDay();
134 		void					_GetYearMonth(int32 *year, int32 *month) const;
135 		void					_GetPreferredSize(float *width, float *height);
136 
137 		void					_SetupDayNames();
138 		void					_SetupDayNumbers();
139 		void					_SetupWeekNumbers();
140 
141 		void					_DrawDays();
142 		void					_DrawFocusRect();
143 		void					_DrawDayHeader();
144 		void					_DrawWeekHeader();
145 		void					_DrawDay(int32 curRow, int32 curColumn,
146 									int32 row, int32 column, int32 counter,
147 									BRect frame, const char *text,
148 									bool focus = false);
149 		void					_DrawItem(BView *owner, BRect frame,
150 									const char *text, bool isSelected = false,
151 									bool isEnabled = true, bool focus = false);
152 
153 		void					_UpdateSelection();
154 		BRect					_FirstCalendarItemFrame() const;
155 		BRect					_SetNewSelectedDay(const BPoint &where);
156 
157 								BCalendarView(const BCalendarView &view);
158 		BCalendarView&			operator=(const BCalendarView &view);
159 
160 	private:
161 		struct 					Selection {
162 									Selection()
163 										: row(0), column(0) { }
164 
165 									void SetTo(int32 _row, int32 _column)
166 									{ row = _row; column = _column; }
167 
168 									int32 row;
169 									int32 column;
170 
171 									Selection& operator=(const Selection &s)
172 									{
173 										row = s.row;
174 										column = s.column;
175 										return *this;
176 									}
177 
178 									bool operator==(const Selection &s) const
179 									{
180 										return row == s.row
181 											&& column == s.column;
182 									}
183 
184 									bool operator!=(const Selection &s) const
185 									{
186 										return row != s.row
187 											|| column != s.column;
188 									}
189 								};
190 		BRect					_RectOfDay(const Selection &selection) const;
191 
192 		BMessage				*fSelectionMessage;
193 
194 		int32					fDay;
195 		int32					fYear;
196 		int32					fMonth;
197 
198 		Selection				fFocusedDay;
199 		bool					fFocusChanged;
200 		Selection				fNewFocusedDay;
201 
202 		Selection				fSelectedDay;
203 		Selection				fNewSelectedDay;
204 		bool					fSelectionChanged;
205 
206 		week_start				fWeekStart;
207 		bool					fDayNameHeaderVisible;
208 		bool					fWeekNumberHeaderVisible;
209 
210 		BString					fDayNames[7];
211 		BString					fWeekNumbers[6];
212 		BString					fDayNumbers[6][7];
213 };
214 
215 
216 }	// namespace BPrivate
217 
218 
219 #endif	// _CALENDAR_VIEW_H_
220