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_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 101 bool IsWeekNumberHeaderVisible() const; 102 void SetWeekNumberHeaderVisible(bool visible); 103 104 private: 105 struct Selection { 106 Selection() 107 : row(0), column(0) 108 { 109 } 110 111 void 112 SetTo(int32 _row, int32 _column) 113 { 114 row = _row; 115 column = _column; 116 } 117 118 int32 row; 119 int32 column; 120 121 Selection& operator=(const Selection& s) 122 { 123 row = s.row; 124 column = s.column; 125 return *this; 126 } 127 128 bool operator==(const Selection& s) const 129 { 130 return row == s.row 131 && column == s.column; 132 } 133 134 bool operator!=(const Selection& s) const 135 { 136 return row != s.row 137 || column != s.column; 138 } 139 }; 140 141 void _InitObject(); 142 143 void _SetToDay(); 144 void _SetToCurrentDay(); 145 void _GetYearMonthForSelection( 146 const Selection& selection, int32* year, 147 int32* month) const; 148 void _GetPreferredSize(float* width, float* height); 149 150 void _SetupDayNames(); 151 void _SetupDayNumbers(); 152 void _SetupWeekNumbers(); 153 154 void _DrawDays(); 155 void _DrawFocusRect(); 156 void _DrawDayHeader(); 157 void _DrawWeekHeader(); 158 void _DrawDay(int32 curRow, int32 curColumn, 159 int32 row, int32 column, int32 counter, 160 BRect frame, const char* text, 161 bool focus = false, bool highlight = false); 162 void _DrawItem(BView* owner, BRect frame, 163 const char* text, bool isSelected = false, 164 bool isEnabled = true, bool focus = false, 165 bool highlight = false); 166 167 void _UpdateSelection(); 168 void _UpdateCurrentDay(); 169 void _UpdateCurrentDate(); 170 171 BRect _FirstCalendarItemFrame() const; 172 BRect _SetNewSelectedDay(const BPoint& where); 173 174 BRect _RectOfDay(const Selection& selection) const; 175 176 private: 177 BMessage* fSelectionMessage; 178 179 BDate fDate; 180 BDate fCurrentDate; 181 182 Selection fFocusedDay; 183 Selection fNewFocusedDay; 184 bool fFocusChanged; 185 186 Selection fSelectedDay; 187 Selection fNewSelectedDay; 188 bool fSelectionChanged; 189 190 Selection fCurrentDay; 191 Selection fNewCurrentDay; 192 bool fCurrentDayChanged; 193 194 int32 fStartOfWeek; 195 bool fDayNameHeaderVisible; 196 bool fWeekNumberHeaderVisible; 197 198 BString fDayNames[7]; 199 BString fWeekNumbers[6]; 200 BString fDayNumbers[6][7]; 201 202 // hide copy constructor & assignment 203 BCalendarView(const BCalendarView& view); 204 BCalendarView& operator=(const BCalendarView& view); 205 }; 206 207 208 } // namespace BPrivate 209 210 211 #endif // _CALENDAR_VIEW_H_ 212