1 // monthviewgen.cpp - Xestia Calendar month view generation functions.
3 // (c) 2016 Xestia Software Development.
5 // This file is part of Xestia Calendar.
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
11 // Xestia Address Book is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Calendar. If not, see <http://www.gnu.org/licenses/>
19 #include "monthviewgen.h"
23 int PreviousMonthNumberofDays(int Month, int Year){
25 // Work out what the previous month is.
27 // Return 31 if month matches.
29 if (Month == 1 || Month == 2 || Month == 4 ||
30 Month == 6 || Month == 8 || Month == 9 || Month == 11){
36 // Return 30 if month matches.
38 if (Month == 5 || Month == 7 || Month == 10 || Month == 12){
44 // Work out if year is a leap year and return 28 or 29
73 XCCalendarMonthViewGrid GenerateMonthGrid(int Month, int Year){
75 XCCalendarMonthViewGrid MonthViewOutput;
77 // Work out which day of the week the first day of
82 int PreviousMonthNumDays = PreviousMonthNumberofDays(Month, Year);
83 int PreviousMonthDays = 0;
84 int PreviousMonth = 0;
85 int PreviousYear = Year;
87 bool ProcessGrid = true;
88 bool FirstWeekProcessing = true;
92 FirstDay = localtime(&EmptyTime);
93 FirstDay->tm_year = Year - 1900;
94 FirstDay->tm_mon = Month - 1;
95 FirstDay->tm_mday = 1;
99 if (FirstDay->tm_wday == 0){
100 PreviousMonthDays = 6;
102 PreviousMonthDays = FirstDay->tm_wday - 1;
112 PreviousMonth = Month - 1;
116 // Workout the days of the previous month for the
117 // first week to be displayed.
119 XCCalendarMonthViewGridDayWeek FirstWeek;
121 for (int PreviousMonthDaysProcessing = 0;
122 PreviousMonthDaysProcessing < PreviousMonthDays;
123 PreviousMonthDaysProcessing++){
125 XCCalendarMonthViewGridDayData DayItem;
127 DayItem.Day = PreviousMonthNumDays - PreviousMonthDaysProcessing;
128 DayItem.Month = PreviousMonth;
129 DayItem.Year = PreviousYear;
130 DayItem.IsInMonth = false;
132 FirstWeek.DayList.insert(FirstWeek.DayList.begin(), DayItem);
138 // Process the actual days of the month.
140 XCCalendarMonthViewGridDayWeek MonthWeek;
143 while (ProcessGrid == true){
145 if (DayWeekCount == 7){
147 // Add to the month view grid.
149 if (FirstWeekProcessing == true){
151 FirstWeekProcessing = false;
152 MonthViewOutput.WeekList.push_back(FirstWeek);
156 MonthViewOutput.WeekList.push_back(MonthWeek);
157 MonthWeek.DayList.clear();
166 XCCalendarMonthViewGridDayData DayItem;
168 DayItem.Day = ProcessDay;
169 DayItem.Month = Month;
171 DayItem.IsInMonth = true;
173 if (FirstWeekProcessing == true){
175 FirstWeek.DayList.push_back(DayItem);
179 MonthWeek.DayList.push_back(DayItem);
185 if ((Month == 1 || Month == 3 || Month == 5 ||
186 Month == 7 || Month == 8 || Month == 10 || Month == 12) &&
193 if ((Month == 4 || Month == 6 || Month == 9 || Month == 11) &&
202 if (Year % 100 == 0){
204 if (Year % 400 == 0){
206 if (Month == 2 && ProcessDay == 29){
214 if (Month == 2 && ProcessDay == 28){
224 if (Month == 2 && ProcessDay == 29){
230 } else if ((Month == 2 && ProcessDay == 28) && Year % 4 != 0){
241 // At the end of the month add any remaining days
244 int NextMonthYear = Year;
246 int NextMonthDay = 1;
255 NextMonth = Month + 1;
259 for (; DayWeekCount < 7; DayWeekCount++){
261 XCCalendarMonthViewGridDayData DayItem;
263 DayItem.Day = NextMonthDay;
264 DayItem.Month = NextMonth;
265 DayItem.Year = NextMonthYear;
266 DayItem.IsInMonth = false;
268 MonthWeek.DayList.push_back(DayItem);
273 if (DayWeekCount == 7){
275 MonthViewOutput.WeekList.push_back(MonthWeek);
279 return MonthViewOutput;