1 // XCCalendarMonthView.cpp - Xestia Calendar XCCalendarMonthView widget class.
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 "XCCalendarMonthView.h"
23 wxDEFINE_EVENT(XCCALENDARMONTH_DESELECTOTHERENTRIES, wxCommandEvent);
24 wxDEFINE_EVENT(XCCALENDARMONTH_HIDEACCOUNTENTRIES, wxCommandEvent);
25 wxDEFINE_EVENT(XCCALENDARMONTH_SHOWACCOUNTENTRIES, wxCommandEvent);
26 wxDEFINE_EVENT(XCCALENDARMONTH_HIDECALENDARENTRIES, wxCommandEvent);
27 wxDEFINE_EVENT(XCCALENDARMONTH_SHOWCALENDARENTRIES, wxCommandEvent);
28 wxDEFINE_EVENT(XCCALENDARMONTH_DELETECALENDARENTRIES, wxCommandEvent);
29 wxDEFINE_EVENT(XCCALENDARMONTH_DISPLAYEVENTMENU, wxCommandEvent);
30 wxDEFINE_EVENT(XCCALENDARMONTH_DELETEENTRY, wxCommandEvent);
31 wxDEFINE_EVENT(XCCALENDARMONTH_ADDENTRY, wxCommandEvent);
32 wxDEFINE_EVENT(XCCALENDARMONTH_UPDATEENTRY, wxCommandEvent);
33 wxDEFINE_EVENT(XCCALENDARMONTH_UPDATECALENDARCOLOUR, wxCommandEvent);
35 BEGIN_EVENT_TABLE(XCCalendarMonthView, wxPanel)
38 XCCalendarMonthView::XCCalendarMonthView(wxWindow *parent, const wxString& title, const wxPoint& pos, const wxSize& size, XCCalendarMonthViewGrid *grid, CalendarDataStorage *dataStorage, std::vector<int> *hideAccounts, std::vector<int> *hideCalendars)
39 : wxPanel(parent, wxID_ANY, pos, size, wxTAB_TRAVERSAL){
40 calendarStorage = dataStorage;
42 mondayText = new XCCalendarMonthViewDayTitle(this, _("Monday"), wxDefaultPosition, wxDefaultSize);
43 tuesdayText = new XCCalendarMonthViewDayTitle(this, _("Tuesday"), wxDefaultPosition, wxDefaultSize);
44 wednesdayText = new XCCalendarMonthViewDayTitle(this, _("Wednesday"), wxDefaultPosition, wxDefaultSize);
45 thursdayText = new XCCalendarMonthViewDayTitle(this, _("Thursday"), wxDefaultPosition, wxDefaultSize);
46 fridayText = new XCCalendarMonthViewDayTitle(this, _("Friday"), wxDefaultPosition, wxDefaultSize);
47 saturdayText = new XCCalendarMonthViewDayTitle(this, _("Saturday"), wxDefaultPosition, wxDefaultSize);
48 sundayText = new XCCalendarMonthViewDayTitle(this, _("Sunday"), wxDefaultPosition, wxDefaultSize);
50 eventMenu = new XCEventMenu;
52 windowSizer->Add(mondayText, 1, wxEXPAND|wxALL, 0);
53 windowSizer->Add(tuesdayText, 1, wxEXPAND|wxALL, 0);
54 windowSizer->Add(wednesdayText, 1, wxEXPAND|wxALL, 0);
55 windowSizer->Add(thursdayText, 1, wxEXPAND|wxALL, 0);
56 windowSizer->Add(fridayText, 1, wxEXPAND|wxALL, 0);
57 windowSizer->Add(saturdayText, 1, wxEXPAND|wxALL, 0);
58 windowSizer->Add(sundayText, 1, wxEXPAND|wxALL, 0);
60 ProcessGrid(grid, hideAccounts, hideCalendars);
64 // Setup the growable rows and columns.
66 windowSizer->AddGrowableCol(0);
67 windowSizer->AddGrowableCol(1);
68 windowSizer->AddGrowableCol(2);
69 windowSizer->AddGrowableCol(3);
70 windowSizer->AddGrowableCol(4);
71 windowSizer->AddGrowableCol(5);
72 windowSizer->AddGrowableCol(6);
73 windowSizer->SetFlexibleDirection( wxBOTH );
74 windowSizer->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
76 this->SetSizer(windowSizer);
77 windowSizer->Fit(this);
79 this->SetBackgroundColour(wxColour(0,0,0));
81 // Bind events to the control.
83 Bind(XCCALENDARMONTH_DESELECTOTHERENTRIES, &XCCalendarMonthView::DeselectOthersEvent, this, ID_MONTHVIEWCLEARSELECTION);
84 Bind(XCCALENDARMONTH_HIDEACCOUNTENTRIES, &XCCalendarMonthView::HideAccountEntries, this, ID_HIDEENTRIES);
85 Bind(XCCALENDARMONTH_SHOWACCOUNTENTRIES, &XCCalendarMonthView::ShowAccountEntries, this, ID_SHOWENTRIES);
86 Bind(XCCALENDARMONTH_HIDECALENDARENTRIES, &XCCalendarMonthView::HideCalendarEntries, this, ID_HIDECALENDARENTRIES);
87 Bind(XCCALENDARMONTH_SHOWCALENDARENTRIES, &XCCalendarMonthView::ShowCalendarEntries, this, ID_SHOWCALENDARENTRIES);
88 Bind(XCCALENDARMONTH_DELETECALENDARENTRIES, &XCCalendarMonthView::DeleteCalendarEntries, this, ID_DELETECALENDARENTRIES);
89 Bind(XCCALENDARMONTH_DISPLAYEVENTMENU, &XCCalendarMonthView::ShowEventMenu, this, ID_DISPLAYEVENTMENU);
90 Bind(XCCALENDARMONTH_DELETEENTRY, &XCCalendarMonthView::DeleteCalendarEntry, this, ID_DELETEENTRY);
91 Bind(XCCALENDARMONTH_ADDENTRY, &XCCalendarMonthView::AddCalendarEntry, this, ID_ADDENTRY);
92 Bind(XCCALENDARMONTH_UPDATEENTRY, &XCCalendarMonthView::UpdateCalendarEntry, this, ID_UPDATEENTRY);
93 Bind(XCCALENDARMONTH_UPDATECALENDARCOLOUR, &XCCalendarMonthView::UpdateCalendarColour, this, ID_UPDATECOLOUR);
97 XCCalendarMonthView::~XCCalendarMonthView(){
101 delete wednesdayText;
107 for (vector<XCCalendarDay*>::iterator dayIter = calendarDayList.begin();
108 dayIter != calendarDayList.end(); dayIter++){
115 this->SetSizer(nullptr, true);
117 // Unbind events from control
119 Unbind(XCCALENDARMONTH_DESELECTOTHERENTRIES, &XCCalendarMonthView::DeselectOthersEvent, this);
120 Unbind(XCCALENDARMONTH_HIDEACCOUNTENTRIES, &XCCalendarMonthView::HideAccountEntries, this);
121 Unbind(XCCALENDARMONTH_SHOWACCOUNTENTRIES, &XCCalendarMonthView::ShowAccountEntries, this);
122 Unbind(XCCALENDARMONTH_HIDECALENDARENTRIES, &XCCalendarMonthView::HideCalendarEntries, this);
123 Unbind(XCCALENDARMONTH_SHOWCALENDARENTRIES, &XCCalendarMonthView::ShowCalendarEntries, this);
124 Unbind(XCCALENDARMONTH_DELETECALENDARENTRIES, &XCCalendarMonthView::DeleteCalendarEntries, this);
125 Unbind(XCCALENDARMONTH_DISPLAYEVENTMENU, &XCCalendarMonthView::ShowEventMenu, this);
126 Unbind(XCCALENDARMONTH_DELETEENTRY, &XCCalendarMonthView::DeleteCalendarEntry, this);
127 Unbind(XCCALENDARMONTH_ADDENTRY, &XCCalendarMonthView::AddCalendarEntry, this);
128 Unbind(XCCALENDARMONTH_UPDATEENTRY, &XCCalendarMonthView::UpdateCalendarEntry, this);
129 Unbind(XCCALENDARMONTH_UPDATECALENDARCOLOUR, &XCCalendarMonthView::UpdateCalendarColour, this);
131 // Don't delete CalendarDataStorage object (used elsewhere).
133 calendarStorage = nullptr;
137 void XCCalendarMonthView::ProcessGrid(XCCalendarMonthViewGrid *grid, std::vector<int> *hideAccounts, std::vector<int> *hideCalendars){
139 // TODO: Delete the old calendar entries.
141 // Add the new calendar entries.
145 for (vector<XCCalendarMonthViewGridDayWeek>::iterator weekIter = grid->weekList.begin();
146 weekIter != grid->weekList.end(); weekIter++){
150 for (vector<XCCalendarMonthViewGridDayData>::iterator dayIter = (*weekIter).dayList.begin();
151 dayIter != (*weekIter).dayList.end(); dayIter++){
153 // Add Calendar entries.
155 XCCalendarDay *calendarDayItem = new XCCalendarDay(this, "XCCalendarMonthView Test", wxPoint(50, 50), wxSize(100,100));
157 // Setup the control.
159 calendarDayItem->SetupControl((*dayIter).day, (*dayIter).month, (*dayIter).year, (*dayIter).isInMonth, this, calendarStorage, hideAccounts, hideCalendars);
161 calendarDayList.push_back(calendarDayItem);
162 windowSizer->Add(calendarDayItem, 1, wxEXPAND, 5);
168 // Add the Growable Rows.
170 for (int weekSeek = 0; weekSeek < week; weekSeek++){
172 windowSizer->AddGrowableRow((weekSeek + 1));
178 void XCCalendarMonthView::ResizeFrameEvent(wxSizeEvent &SizeEvent){
180 // TODO: Check if window size is less than 120 pixels and if it is,
181 // switch to the small block mode.
183 // Refresh the window.
189 void XCCalendarMonthView::DeselectOthersEvent(wxCommandEvent &deselectEvent){
191 for (vector<XCCalendarDay*>::iterator dayIter = calendarDayList.begin();
192 dayIter != calendarDayList.end(); dayIter++){
194 if (deselectEvent.GetClientData() != (void*)(*dayIter)){
196 wxCommandEvent deselectEvent(XCCALENDARDAY_DESELECTALLENTRIES);
197 deselectEvent.SetId(ID_DESELECTALLITEMS);
198 wxPostEvent((*dayIter), deselectEvent);
206 void XCCalendarMonthView::HideAccountEntries(wxCommandEvent &accountData){
208 for (vector<XCCalendarDay*>::iterator dayIter = calendarDayList.begin();
209 dayIter != calendarDayList.end(); dayIter++){
211 wxCommandEvent hideEvent(XCCALENDARDAY_HIDEACCOUNTENTRIES);
212 hideEvent.SetInt(accountData.GetInt());
213 hideEvent.SetId(ID_HIDEENTRIES);
214 wxPostEvent((*dayIter), hideEvent);
220 void XCCalendarMonthView::ShowAccountEntries(wxCommandEvent &accountData){
222 for (vector<XCCalendarDay*>::iterator dayIter = calendarDayList.begin();
223 dayIter != calendarDayList.end(); dayIter++){
225 wxCommandEvent showEvent(XCCALENDARDAY_SHOWACCOUNTENTRIES);
226 showEvent.SetInt(accountData.GetInt());
227 showEvent.SetId(ID_SHOWENTRIES);
228 wxPostEvent((*dayIter), showEvent);
234 void XCCalendarMonthView::HideCalendarEntries(wxCommandEvent &accountData){
236 for (vector<XCCalendarDay*>::iterator dayIter = calendarDayList.begin();
237 dayIter != calendarDayList.end(); dayIter++){
239 wxCommandEvent hideEvent(XCCALENDARDAY_HIDECALENDARENTRIES);
240 hideEvent.SetInt(accountData.GetInt());
241 hideEvent.SetId(ID_HIDECALENDARENTRIES);
242 wxPostEvent((*dayIter), hideEvent);
248 void XCCalendarMonthView::ShowCalendarEntries(wxCommandEvent &accountData){
250 for (vector<XCCalendarDay*>::iterator dayIter = calendarDayList.begin();
251 dayIter != calendarDayList.end(); dayIter++){
253 wxCommandEvent hideEvent(XCCALENDARDAY_SHOWCALENDARENTRIES);
254 hideEvent.SetInt(accountData.GetInt());
255 hideEvent.SetId(ID_SHOWCALENDARENTRIES);
256 wxPostEvent((*dayIter), hideEvent);
262 void XCCalendarMonthView::DeleteCalendarEntries(wxCommandEvent &calendarData){
264 for (vector<XCCalendarDay*>::iterator dayIter = calendarDayList.begin();
265 dayIter != calendarDayList.end(); dayIter++){
267 wxCommandEvent deleteEvent(XCCALENDARDAY_DELETECALENDARENTRIES);
268 deleteEvent.SetInt(calendarData.GetInt());
269 deleteEvent.SetId(ID_DELETECALENDARENTRIES);
270 wxPostEvent((*dayIter), deleteEvent);
276 void XCCalendarMonthView::DeleteCalendarEntry(wxCommandEvent &eventData){
278 for (vector<XCCalendarDay*>::iterator dayIter = calendarDayList.begin();
279 dayIter != calendarDayList.end(); dayIter++){
281 wxCommandEvent deleteEvent(XCCALENDARDAY_DELETEENTRY);
282 deleteEvent.SetInt(eventData.GetInt());
283 deleteEvent.SetId(ID_DELETEENTRY);
284 wxPostEvent((*dayIter), deleteEvent);
290 void XCCalendarMonthView::AddCalendarEntry(wxCommandEvent &eventData){
292 EventProperties *eventInfo = (EventProperties*)eventData.GetClientData();
293 bool foundDate = false;
295 for (vector<XCCalendarDay*>::iterator dayIter = calendarDayList.begin();
296 dayIter != calendarDayList.end(); dayIter++){
298 // Check if the date matches with the date in the day
299 // widget. If it does, add the widget.
301 if ((*dayIter)->GetCalendarYear() == eventInfo->eventYear &&
302 (*dayIter)->GetCalendarMonth() == (eventInfo->eventMonth + 1) &&
303 (*dayIter)->GetCalendarDay() == eventInfo->eventDay){
305 wxCommandEvent addEvent(XCCALENDARDAY_ADDENTRY);
306 addEvent.SetId(ID_ADDENTRY);
307 addEvent.SetClientData(eventInfo);
308 wxPostEvent((*dayIter), addEvent);
316 if (foundDate == false){
325 void XCCalendarMonthView::UpdateCalendarEntry(wxCommandEvent &eventData){
327 EventProperties *eventInfo = (EventProperties*)eventData.GetClientData();
328 bool foundDate = false;
330 for (vector<XCCalendarDay*>::iterator dayIter = calendarDayList.begin();
331 dayIter != calendarDayList.end(); dayIter++){
333 wxCommandEvent addEvent(XCCALENDARDAY_DELETEENTRY);
334 addEvent.SetId(ID_DELETEENTRY);
335 addEvent.SetInt(eventInfo->eventID);
336 wxPostEvent((*dayIter), addEvent);
340 for (vector<XCCalendarDay*>::iterator dayIter = calendarDayList.begin();
341 dayIter != calendarDayList.end(); dayIter++){
343 // Check if the date matches with the date in the day
344 // widget. If it does, update the widget.
346 if ((*dayIter)->GetCalendarYear() == eventInfo->eventYear &&
347 (*dayIter)->GetCalendarMonth() == (eventInfo->eventMonth + 1) &&
348 (*dayIter)->GetCalendarDay() == eventInfo->eventDay){
350 wxCommandEvent addEvent(XCCALENDARDAY_ADDENTRY);
351 addEvent.SetId(ID_ADDENTRY);
352 addEvent.SetClientData(eventInfo);
353 wxPostEvent((*dayIter), addEvent);
361 if (foundDate == false){
370 void XCCalendarMonthView::UpdateCalendarColour(wxCommandEvent &colourData){
372 ColourUpdateProperties *colourDataInfo = (ColourUpdateProperties*)colourData.GetClientData();
374 for (vector<XCCalendarDay*>::iterator dayIter = calendarDayList.begin();
375 dayIter != calendarDayList.end(); dayIter++){
377 // Check if the date matches with the date in the day
378 // widget. If it does, update the widget.
380 ColourUpdateProperties *colourDataPassInfo = colourDataInfo;
382 wxCommandEvent updateColour(XCCALENDARDAY_UPDATECALENDARCOLOUR);
383 updateColour.SetId(ID_UPDATECOLOUR);
384 updateColour.SetClientData(colourDataPassInfo);
385 wxPostEvent((*dayIter), updateColour);
391 void XCCalendarMonthView::ShowEventMenu(wxCommandEvent &eventData){
393 EventProperties *eventInfo = (EventProperties*)eventData.GetClientData();
395 eventMenu->SetEventID(eventInfo->eventID);
396 eventMenu->SetCalendarID(eventInfo->calendarID);
397 eventMenu->SetWindowPointer(this->GetParent()->GetParent());
399 this->PopupMenu(eventMenu, wxDefaultPosition);