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); 32 33 BCalendarView(const char* name, 34 uint32 flags = B_WILL_DRAW | B_FRAME_EVENTS 35 | B_NAVIGABLE); 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 virtual void DrawDayName(BView* owner, BRect frame, 54 const char* text); 55 virtual void DrawWeekNumber(BView* owner, BRect frame, 56 const char* text); 57 58 uint32 SelectionCommand() const; 59 BMessage* SelectionMessage() const; 60 virtual void SetSelectionMessage(BMessage* message); 61 62 uint32 InvocationCommand() const; 63 BMessage* InvocationMessage() const; 64 virtual void SetInvocationMessage(BMessage* message); 65 66 virtual void MakeFocus(bool state = true); 67 virtual status_t Invoke(BMessage* message = NULL); 68 69 virtual void MouseDown(BPoint where); 70 71 virtual void KeyDown(const char* bytes, int32 numBytes); 72 73 virtual void ResizeToPreferred(); 74 virtual void GetPreferredSize(float* width, float* height); 75 76 virtual BSize MaxSize(); 77 virtual BSize MinSize(); 78 virtual BSize PreferredSize(); 79 80 int32 Day() const; 81 int32 Month() const; 82 int32 Year() const; 83 84 bool SetDay(int32 day); 85 bool SetMonth(int32 month); 86 bool SetYear(int32 year); 87 88 BDate Date() const; 89 bool SetDate(const BDate& date); 90 bool SetDate(int32 year, int32 month, int32 day); 91 92 BWeekday StartOfWeek() const; 93 void SetStartOfWeek(BWeekday startOfWeek); 94 95 bool IsDayNameHeaderVisible() const; 96 void SetDayNameHeaderVisible(bool visible); 97 98 bool IsWeekNumberHeaderVisible() const; 99 void SetWeekNumberHeaderVisible(bool visible); 100 101 private: 102 struct Selection { 103 Selection() 104 : row(0), column(0) 105 { 106 } 107 108 void 109 SetTo(int32 _row, int32 _column) 110 { 111 row = _row; 112 column = _column; 113 } 114 115 int32 row; 116 int32 column; 117 118 Selection& operator=(const Selection& s) 119 { 120 row = s.row; 121 column = s.column; 122 return *this; 123 } 124 125 bool operator==(const Selection& s) const 126 { 127 return row == s.row 128 && column == s.column; 129 } 130 131 bool operator!=(const Selection& s) const 132 { 133 return row != s.row 134 || column != s.column; 135 } 136 }; 137 138 void _InitObject(); 139 140 void _SetToDay(); 141 void _GetYearMonthForSelection( 142 const Selection& selection, int32* year, 143 int32* month) const; 144 void _GetPreferredSize(float* width, float* height); 145 146 void _SetupDayNames(); 147 void _SetupDayNumbers(); 148 void _SetupWeekNumbers(); 149 150 void _DrawDays(); 151 void _DrawFocusRect(); 152 void _DrawDayHeader(); 153 void _DrawWeekHeader(); 154 void _DrawDay(int32 curRow, int32 curColumn, 155 int32 row, int32 column, int32 counter, 156 BRect frame, const char* text, 157 bool focus = false); 158 void _DrawItem(BView* owner, BRect frame, 159 const char* text, bool isSelected = false, 160 bool isEnabled = true, bool focus = false); 161 162 void _UpdateSelection(); 163 BRect _FirstCalendarItemFrame() const; 164 BRect _SetNewSelectedDay(const BPoint& where); 165 166 BRect _RectOfDay(const Selection& selection) const; 167 168 private: 169 BMessage* fSelectionMessage; 170 171 BDate fDate; 172 173 Selection fFocusedDay; 174 Selection fNewFocusedDay; 175 bool fFocusChanged; 176 177 Selection fSelectedDay; 178 Selection fNewSelectedDay; 179 bool fSelectionChanged; 180 181 int32 fStartOfWeek; 182 bool fDayNameHeaderVisible; 183 bool fWeekNumberHeaderVisible; 184 185 BString fDayNames[7]; 186 BString fWeekNumbers[6]; 187 BString fDayNumbers[6][7]; 188 189 // hide copy constructor & assignment 190 BCalendarView(const BCalendarView& view); 191 BCalendarView& operator=(const BCalendarView& view); 192 }; 193 194 195 } // namespace BPrivate 196 197 198 #endif // _CALENDAR_VIEW_H_ 199