Ticket #13592: 0001-Highlight-current-system-date-in-BCalendarView.patch
File 0001-Highlight-current-system-date-in-BCalendarView.patch, 13.1 KB (added by , 7 years ago) |
---|
-
headers/private/shared/CalendarView.h
From 7a24e5420e494dd609888b225f1ed7a8c9c852f5 Mon Sep 17 00:00:00 2001 From: Akshay Agarwal <agarwal.akshay.akshay8@gmail.com> Date: Mon, 10 Jul 2017 05:48:36 +0530 Subject: [PATCH] Highlight current system date in BCalendarView. * Issue: In BCalendarView presently, there is no notion of a current date and the current date is not highlighted. So in the deskbar tray calendar which uses BCalendarView, we cannot know the current date once we change the selected day. * Fix: Make BCalendarView accept pulse messages, check for system date with every pulse message and update the current date accordingly. Highlight the current date by rendering its day number text in a different color. --- headers/private/shared/CalendarView.h | 23 +++-- src/kits/shared/CalendarView.cpp | 159 +++++++++++++++++++++++++++++++--- 2 files changed, 167 insertions(+), 15 deletions(-) diff --git a/headers/private/shared/CalendarView.h b/headers/private/shared/CalendarView.h index d48c991..c644dfb 100644
a b public: 28 28 BCalendarView(BRect frame, const char* name, 29 29 uint32 resizeMask = B_FOLLOW_LEFT_TOP, 30 30 uint32 flags = B_WILL_DRAW | B_FRAME_EVENTS 31 | B_NAVIGABLE );31 | B_NAVIGABLE | B_PULSE_NEEDED); 32 32 33 33 BCalendarView(const char* name, 34 34 uint32 flags = B_WILL_DRAW | B_FRAME_EVENTS 35 | B_NAVIGABLE );35 | B_NAVIGABLE | B_PULSE_NEEDED); 36 36 37 37 virtual ~BCalendarView(); 38 38 … … public: 49 49 50 50 virtual void DrawDay(BView* owner, BRect frame, 51 51 const char* text, bool isSelected = false, 52 bool isEnabled = true, bool focus = false); 52 bool isEnabled = true, bool focus = false, 53 bool highlight = false); 53 54 virtual void DrawDayName(BView* owner, BRect frame, 54 55 const char* text); 55 56 virtual void DrawWeekNumber(BView* owner, BRect frame, … … public: 70 71 71 72 virtual void KeyDown(const char* bytes, int32 numBytes); 72 73 74 virtual void Pulse(); 75 73 76 virtual void ResizeToPreferred(); 74 77 virtual void GetPreferredSize(float* width, float* height); 75 78 … … private: 138 141 void _InitObject(); 139 142 140 143 void _SetToDay(); 144 void _SetToCurrentDay(); 141 145 void _GetYearMonthForSelection( 142 146 const Selection& selection, int32* year, 143 147 int32* month) const; … … private: 154 158 void _DrawDay(int32 curRow, int32 curColumn, 155 159 int32 row, int32 column, int32 counter, 156 160 BRect frame, const char* text, 157 bool focus = false );161 bool focus = false, bool highlight = false); 158 162 void _DrawItem(BView* owner, BRect frame, 159 163 const char* text, bool isSelected = false, 160 bool isEnabled = true, bool focus = false); 164 bool isEnabled = true, bool focus = false, 165 bool highlight = false); 161 166 162 167 void _UpdateSelection(); 168 void _UpdateCurrentDay(); 169 void _UpdateCurrentDate(); 170 163 171 BRect _FirstCalendarItemFrame() const; 164 172 BRect _SetNewSelectedDay(const BPoint& where); 165 173 … … private: 169 177 BMessage* fSelectionMessage; 170 178 171 179 BDate fDate; 180 BDate fCurrentDate; 172 181 173 182 Selection fFocusedDay; 174 183 Selection fNewFocusedDay; … … private: 178 187 Selection fNewSelectedDay; 179 188 bool fSelectionChanged; 180 189 190 Selection fCurrentDay; 191 Selection fNewCurrentDay; 192 bool fCurrentDayChanged; 193 181 194 int32 fStartOfWeek; 182 195 bool fDayNameHeaderVisible; 183 196 bool fWeekNumberHeaderVisible; -
src/kits/shared/CalendarView.cpp
diff --git a/src/kits/shared/CalendarView.cpp b/src/kits/shared/CalendarView.cpp index 58c7fe2..a60aa6b 100644
a b BCalendarView::BCalendarView(BRect frame, const char* name, uint32 resizeMask, 42 42 BInvoker(), 43 43 fSelectionMessage(NULL), 44 44 fDate(), 45 fCurrentDate(BDate::CurrentDate(B_LOCAL_TIME)), 45 46 fFocusChanged(false), 46 47 fSelectionChanged(false), 48 fCurrentDayChanged(false), 47 49 fStartOfWeek((int32)B_WEEKDAY_MONDAY), 48 50 fDayNameHeaderVisible(true), 49 51 fWeekNumberHeaderVisible(true) … … BCalendarView::BCalendarView(const char* name, uint32 flags) 58 60 BInvoker(), 59 61 fSelectionMessage(NULL), 60 62 fDate(), 63 fCurrentDate(BDate::CurrentDate(B_LOCAL_TIME)), 61 64 fFocusChanged(false), 62 65 fSelectionChanged(false), 66 fCurrentDayChanged(false), 63 67 fStartOfWeek((int32)B_WEEKDAY_MONDAY), 64 68 fDayNameHeaderVisible(true), 65 69 fWeekNumberHeaderVisible(true) … … BCalendarView::BCalendarView(BMessage* archive) 80 84 BInvoker(), 81 85 fSelectionMessage(NULL), 82 86 fDate(archive), 87 fCurrentDate(BDate::CurrentDate(B_LOCAL_TIME)), 83 88 fFocusChanged(false), 84 89 fSelectionChanged(false), 90 fCurrentDayChanged(false), 85 91 fStartOfWeek((int32)B_WEEKDAY_MONDAY), 86 92 fDayNameHeaderVisible(true), 87 93 fWeekNumberHeaderVisible(true) … … BCalendarView::Draw(BRect updateRect) 185 191 return; 186 192 } 187 193 194 if (fCurrentDayChanged) { 195 _UpdateCurrentDay(); 196 UnlockLooper(); 197 return; 198 } 199 188 200 _DrawDays(); 189 201 _DrawDayHeader(); 190 202 _DrawWeekHeader(); … … BCalendarView::Draw(BRect updateRect) 200 212 201 213 void 202 214 BCalendarView::DrawDay(BView* owner, BRect frame, const char* text, 203 bool isSelected, bool isEnabled, bool focus )215 bool isSelected, bool isEnabled, bool focus, bool highlight) 204 216 { 205 _DrawItem(owner, frame, text, isSelected, isEnabled, focus );217 _DrawItem(owner, frame, text, isSelected, isEnabled, focus, highlight); 206 218 } 207 219 208 220 … … BCalendarView::KeyDown(const char* bytes, int32 numBytes) 464 476 465 477 466 478 void 479 BCalendarView::Pulse() 480 { 481 _UpdateCurrentDate(); 482 } 483 484 485 void 467 486 BCalendarView::ResizeToPreferred() 468 487 { 469 488 float width; … … BCalendarView::_SetToDay() 742 761 743 762 744 763 void 764 BCalendarView::_SetToCurrentDay() 765 { 766 BDate date(fDate.Year(), fDate.Month(), 1); 767 if (!date.IsValid()) 768 return; 769 770 const int32 firstDayOffset = (7 + date.DayOfWeek() - fStartOfWeek) % 7; 771 772 int32 day = 1 - firstDayOffset; 773 for (int32 row = 0; row < 6; ++row) { 774 for (int32 column = 0; column < 7; ++column) { 775 if (day == fCurrentDate.Day()) { 776 fNewCurrentDay.SetTo(row, column); 777 return; 778 } 779 day++; 780 } 781 } 782 783 fNewCurrentDay.SetTo(0, 0); 784 } 785 786 787 void 745 788 BCalendarView::_GetYearMonthForSelection(const Selection& selection, 746 789 int32* year, int32* month) const 747 790 { … … BCalendarView::_SetupDayNumbers() 813 856 fFocusedDay.SetTo(0, 0); 814 857 fSelectedDay.SetTo(0, 0); 815 858 fNewFocusedDay.SetTo(0, 0); 859 fCurrentDay.SetTo(-1, -1); 816 860 817 861 const int32 daysInMonth = startOfMonth.DaysInMonth(); 818 862 const int32 firstDayOffset … … BCalendarView::_SetupDayNumbers() 837 881 fSelectedDay.SetTo(row, column); 838 882 fNewFocusedDay.SetTo(row, column); 839 883 } 884 if (day == fCurrentDate.Day() 885 && fDate.Month() == fCurrentDate.Month()) 886 fCurrentDay.SetTo(row, column); 840 887 counter++; 841 888 fDayNumbers[row][column].Truncate(0); 842 889 fDayNumbers[row][column] << day; … … BCalendarView::_SetupWeekNumbers() 862 909 863 910 void 864 911 BCalendarView::_DrawDay(int32 currRow, int32 currColumn, int32 row, 865 int32 column, int32 counter, BRect frame, const char* text, bool focus) 912 int32 column, int32 counter, BRect frame, const char* text, 913 bool focus, bool highlight) 866 914 { 867 915 BDate startOfMonth(fDate.Year(), fDate.Month(), 1); 868 916 const int32 firstDayOffset … … BCalendarView::_DrawDay(int32 currRow, int32 currColumn, int32 row, 883 931 enabled = false; // days of month before or after 884 932 } 885 933 886 DrawDay(this, frame, text, selected, enabled, focus );934 DrawDay(this, frame, text, selected, enabled, focus, highlight); 887 935 } 888 936 889 937 … … BCalendarView::_DrawDays() 899 947 const int32 focusRow = fFocusedDay.row; 900 948 const int32 focusColumn = fFocusedDay.column; 901 949 950 const int32 highlightRow = fCurrentDay.row; 951 const int32 highlightColumn = fCurrentDay.column; 952 902 953 int32 counter = 0; 903 954 for (int32 row = 0; row < 6; ++row) { 904 955 BRect tmp = frame; … … BCalendarView::_DrawDays() 906 957 counter++; 907 958 const char* day = fDayNumbers[row][column].String(); 908 959 bool focus = isFocus && focusRow == row && focusColumn == column; 960 bool highlight = highlightRow == row && highlightColumn == column; 909 961 _DrawDay(currRow, currColumn, row, column, counter, tmp, day, 910 focus );962 focus, highlight); 911 963 912 964 tmp.OffsetBy(tmp.Width(), 0.0); 913 965 } … … BCalendarView::_DrawFocusRect() 927 979 const int32 focusRow = fFocusedDay.row; 928 980 const int32 focusColumn = fFocusedDay.column; 929 981 982 const int32 highlightRow = fCurrentDay.row; 983 const int32 highlightColumn = fCurrentDay.column; 984 930 985 int32 counter = 0; 931 986 for (int32 row = 0; row < 6; ++row) { 932 987 BRect tmp = frame; … … BCalendarView::_DrawFocusRect() 936 991 fFocusedDay.SetTo(row, column); 937 992 938 993 bool focus = IsFocus() && true; 994 bool highlight = highlightRow == row && highlightColumn == column; 939 995 const char* day = fDayNumbers[row][column].String(); 940 996 _DrawDay(currRow, currColumn, row, column, counter, tmp, day, 941 focus );997 focus, highlight); 942 998 } else if (focusRow == row && focusColumn == column) { 943 999 const char* day = fDayNumbers[row][column].String(); 1000 bool highlight = highlightRow == row && highlightColumn == column; 944 1001 _DrawDay(currRow, currColumn, row, column, counter, tmp, day, 945 false );1002 false, highlight); 946 1003 } 947 1004 tmp.OffsetBy(tmp.Width(), 0.0); 948 1005 } … … BCalendarView::_DrawWeekHeader() 1010 1067 1011 1068 void 1012 1069 BCalendarView::_DrawItem(BView* owner, BRect frame, const char* text, 1013 bool isSelected, bool isEnabled, bool focus )1070 bool isSelected, bool isEnabled, bool focus, bool isHighlight) 1014 1071 { 1015 1072 rgb_color lColor = LowColor(); 1016 1073 rgb_color highColor = HighColor(); … … BCalendarView::_DrawItem(BView* owner, BRect frame, const char* text, 1027 1084 } else 1028 1085 SetHighColor(ui_color(B_LIST_BACKGROUND_COLOR)); 1029 1086 1087 if (isHighlight) { 1088 textColor = keyboard_navigation_color(); 1089 } 1090 1030 1091 SetLowColor(HighColor()); 1031 1092 1032 1093 FillRect(frame.InsetByCopy(1.0, 1.0)); … … BCalendarView::_UpdateSelection() 1062 1123 const int32 focusRow = fFocusedDay.row; 1063 1124 const int32 focusColumn = fFocusedDay.column; 1064 1125 1126 const int32 highlightRow = fCurrentDay.row; 1127 const int32 highlightColumn = fCurrentDay.column; 1128 1065 1129 int32 counter = 0; 1066 1130 for (int32 row = 0; row < 6; ++row) { 1067 1131 BRect tmp = frame; … … BCalendarView::_UpdateSelection() 1074 1138 const char* day = fDayNumbers[row][column].String(); 1075 1139 bool focus = IsFocus() && focusRow == row 1076 1140 && focusColumn == column; 1077 _DrawDay(row, column, row, column, counter, tmp, day, focus); 1141 bool highlight = highlightRow == row && highlightColumn == column; 1142 _DrawDay(row, column, row, column, counter, tmp, day, focus, highlight); 1078 1143 } else if (currRow == row && currColumn == column) { 1079 1144 const char* day = fDayNumbers[row][column].String(); 1080 1145 bool focus = IsFocus() && focusRow == row 1081 1146 && focusColumn == column; 1082 _DrawDay(currRow, currColumn, -1, -1, counter, tmp, day, focus); 1147 bool highlight = highlightRow == row && highlightColumn == column; 1148 _DrawDay(currRow, currColumn, -1, -1, counter, tmp, day, focus, highlight); 1083 1149 } 1084 1150 tmp.OffsetBy(tmp.Width(), 0.0); 1085 1151 } … … BCalendarView::_UpdateSelection() 1088 1154 } 1089 1155 1090 1156 1157 void 1158 BCalendarView::_UpdateCurrentDay() 1159 { 1160 BRect frame = _FirstCalendarItemFrame(); 1161 1162 const int32 selectRow = fSelectedDay.row; 1163 const int32 selectColumn = fSelectedDay.column; 1164 1165 const int32 focusRow = fFocusedDay.row; 1166 const int32 focusColumn = fFocusedDay.column; 1167 1168 const int32 currRow = fCurrentDay.row; 1169 const int32 currColumn = fCurrentDay.column; 1170 1171 int32 counter = 0; 1172 for (int32 row = 0; row < 6; ++row) { 1173 BRect tmp = frame; 1174 for (int32 column = 0; column < 7; ++column) { 1175 counter++; 1176 if (fNewCurrentDay.row == row 1177 && fNewCurrentDay.column == column) { 1178 fCurrentDay.SetTo(row, column); 1179 1180 const char* day = fDayNumbers[row][column].String(); 1181 bool focus = IsFocus() && focusRow == row 1182 && focusColumn == column; 1183 bool isSelected = selectRow == row && selectColumn == column; 1184 if (isSelected) 1185 _DrawDay(row, column, row, column, counter, tmp, day, focus, true); 1186 else 1187 _DrawDay(row, column, -1, -1, counter, tmp, day, focus, true); 1188 1189 } else if (currRow == row && currColumn == column) { 1190 const char* day = fDayNumbers[row][column].String(); 1191 bool focus = IsFocus() && focusRow == row 1192 && focusColumn == column; 1193 bool isSelected = selectRow == row && selectColumn == column; 1194 if(isSelected) 1195 _DrawDay(currRow, currColumn, row, column, counter, tmp, day, focus, false); 1196 else 1197 _DrawDay(currRow, currColumn, -1, -1, counter, tmp, day, focus, false); 1198 } 1199 tmp.OffsetBy(tmp.Width(), 0.0); 1200 } 1201 frame.OffsetBy(0.0, frame.Height()); 1202 } 1203 } 1204 1205 1206 void 1207 BCalendarView::_UpdateCurrentDate() 1208 { 1209 BDate date = BDate::CurrentDate(B_LOCAL_TIME); 1210 1211 if (!date.IsValid()) 1212 return; 1213 if (date == fCurrentDate) 1214 return; 1215 1216 fCurrentDate = date; 1217 1218 if (fDate.Year() == date.Year() && fDate.Month() == date.Month()) { 1219 _SetToCurrentDay(); 1220 fCurrentDayChanged = true; 1221 Draw(_RectOfDay(fCurrentDay)); 1222 Draw(_RectOfDay(fNewCurrentDay)); 1223 fCurrentDayChanged = false; 1224 } 1225 1226 return; 1227 } 1228 1229 1091 1230 BRect 1092 1231 BCalendarView::_FirstCalendarItemFrame() const 1093 1232 {