// XCCalendarMonthView.cpp - Xestia Calendar XCCalendarMonthView widget class.
//
// (c) 2016 Xestia Software Development.
//
// This file is part of Xestia Calendar.
//
// Xestia Address Book is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by the
// Free Software Foundation, version 3 of the license.
//
// Xestia Address Book is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with Xestia Calendar. If not, see
#include "XCCalendarMonthView.h"
using namespace std;
wxDEFINE_EVENT(XCCALENDARMONTH_DESELECTOTHERENTRIES, wxCommandEvent);
wxDEFINE_EVENT(XCCALENDARMONTH_HIDEACCOUNTENTRIES, wxCommandEvent);
wxDEFINE_EVENT(XCCALENDARMONTH_SHOWACCOUNTENTRIES, wxCommandEvent);
wxDEFINE_EVENT(XCCALENDARMONTH_HIDECALENDARENTRIES, wxCommandEvent);
wxDEFINE_EVENT(XCCALENDARMONTH_SHOWCALENDARENTRIES, wxCommandEvent);
wxDEFINE_EVENT(XCCALENDARMONTH_DELETECALENDARENTRIES, wxCommandEvent);
wxDEFINE_EVENT(XCCALENDARMONTH_DISPLAYEVENTMENU, wxCommandEvent);
wxDEFINE_EVENT(XCCALENDARMONTH_DELETEENTRY, wxCommandEvent);
wxDEFINE_EVENT(XCCALENDARMONTH_ADDENTRY, wxCommandEvent);
wxDEFINE_EVENT(XCCALENDARMONTH_UPDATEENTRY, wxCommandEvent);
wxDEFINE_EVENT(XCCALENDARMONTH_UPDATECALENDARCOLOUR, wxCommandEvent);
BEGIN_EVENT_TABLE(XCCalendarMonthView, wxPanel)
END_EVENT_TABLE()
XCCalendarMonthView::XCCalendarMonthView(wxWindow *parent, const wxString& title, const wxPoint& pos, const wxSize& size, XCCalendarMonthViewGrid *grid, CalendarDataStorage *dataStorage, std::vector *hideAccounts, std::vector *hideCalendars)
: wxPanel(parent, wxID_ANY, pos, size, wxTAB_TRAVERSAL){
calendarStorage = dataStorage;
mondayText = new XCCalendarMonthViewDayTitle(this, _("Monday"), wxDefaultPosition, wxDefaultSize);
tuesdayText = new XCCalendarMonthViewDayTitle(this, _("Tuesday"), wxDefaultPosition, wxDefaultSize);
wednesdayText = new XCCalendarMonthViewDayTitle(this, _("Wednesday"), wxDefaultPosition, wxDefaultSize);
thursdayText = new XCCalendarMonthViewDayTitle(this, _("Thursday"), wxDefaultPosition, wxDefaultSize);
fridayText = new XCCalendarMonthViewDayTitle(this, _("Friday"), wxDefaultPosition, wxDefaultSize);
saturdayText = new XCCalendarMonthViewDayTitle(this, _("Saturday"), wxDefaultPosition, wxDefaultSize);
sundayText = new XCCalendarMonthViewDayTitle(this, _("Sunday"), wxDefaultPosition, wxDefaultSize);
eventMenu = new XCEventMenu;
windowSizer->Add(mondayText, 1, wxEXPAND|wxALL, 0);
windowSizer->Add(tuesdayText, 1, wxEXPAND|wxALL, 0);
windowSizer->Add(wednesdayText, 1, wxEXPAND|wxALL, 0);
windowSizer->Add(thursdayText, 1, wxEXPAND|wxALL, 0);
windowSizer->Add(fridayText, 1, wxEXPAND|wxALL, 0);
windowSizer->Add(saturdayText, 1, wxEXPAND|wxALL, 0);
windowSizer->Add(sundayText, 1, wxEXPAND|wxALL, 0);
ProcessGrid(grid, hideAccounts, hideCalendars);
// Setup the days.
// Setup the growable rows and columns.
windowSizer->AddGrowableCol(0);
windowSizer->AddGrowableCol(1);
windowSizer->AddGrowableCol(2);
windowSizer->AddGrowableCol(3);
windowSizer->AddGrowableCol(4);
windowSizer->AddGrowableCol(5);
windowSizer->AddGrowableCol(6);
windowSizer->SetFlexibleDirection( wxBOTH );
windowSizer->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
this->SetSizer(windowSizer);
windowSizer->Fit(this);
this->Layout();
this->SetBackgroundColour(wxColour(0,0,0));
// Bind events to the control.
Bind(XCCALENDARMONTH_DESELECTOTHERENTRIES, &XCCalendarMonthView::DeselectOthersEvent, this, ID_MONTHVIEWCLEARSELECTION);
Bind(XCCALENDARMONTH_HIDEACCOUNTENTRIES, &XCCalendarMonthView::HideAccountEntries, this, ID_HIDEENTRIES);
Bind(XCCALENDARMONTH_SHOWACCOUNTENTRIES, &XCCalendarMonthView::ShowAccountEntries, this, ID_SHOWENTRIES);
Bind(XCCALENDARMONTH_HIDECALENDARENTRIES, &XCCalendarMonthView::HideCalendarEntries, this, ID_HIDECALENDARENTRIES);
Bind(XCCALENDARMONTH_SHOWCALENDARENTRIES, &XCCalendarMonthView::ShowCalendarEntries, this, ID_SHOWCALENDARENTRIES);
Bind(XCCALENDARMONTH_DELETECALENDARENTRIES, &XCCalendarMonthView::DeleteCalendarEntries, this, ID_DELETECALENDARENTRIES);
Bind(XCCALENDARMONTH_DISPLAYEVENTMENU, &XCCalendarMonthView::ShowEventMenu, this, ID_DISPLAYEVENTMENU);
Bind(XCCALENDARMONTH_DELETEENTRY, &XCCalendarMonthView::DeleteCalendarEntry, this, ID_DELETEENTRY);
Bind(XCCALENDARMONTH_ADDENTRY, &XCCalendarMonthView::AddCalendarEntry, this, ID_ADDENTRY);
Bind(XCCALENDARMONTH_UPDATEENTRY, &XCCalendarMonthView::UpdateCalendarEntry, this, ID_UPDATEENTRY);
Bind(XCCALENDARMONTH_UPDATECALENDARCOLOUR, &XCCalendarMonthView::UpdateCalendarColour, this, ID_UPDATECOLOUR);
}
XCCalendarMonthView::~XCCalendarMonthView(){
delete mondayText;
delete tuesdayText;
delete wednesdayText;
delete thursdayText;
delete fridayText;
delete saturdayText;
delete sundayText;
for (vector::iterator dayIter = calendarDayList.begin();
dayIter != calendarDayList.end(); dayIter++){
delete ((*dayIter));
}
delete eventMenu;
this->SetSizer(nullptr, true);
// Unbind events from control
Unbind(XCCALENDARMONTH_DESELECTOTHERENTRIES, &XCCalendarMonthView::DeselectOthersEvent, this);
Unbind(XCCALENDARMONTH_HIDEACCOUNTENTRIES, &XCCalendarMonthView::HideAccountEntries, this);
Unbind(XCCALENDARMONTH_SHOWACCOUNTENTRIES, &XCCalendarMonthView::ShowAccountEntries, this);
Unbind(XCCALENDARMONTH_HIDECALENDARENTRIES, &XCCalendarMonthView::HideCalendarEntries, this);
Unbind(XCCALENDARMONTH_SHOWCALENDARENTRIES, &XCCalendarMonthView::ShowCalendarEntries, this);
Unbind(XCCALENDARMONTH_DELETECALENDARENTRIES, &XCCalendarMonthView::DeleteCalendarEntries, this);
Unbind(XCCALENDARMONTH_DISPLAYEVENTMENU, &XCCalendarMonthView::ShowEventMenu, this);
Unbind(XCCALENDARMONTH_DELETEENTRY, &XCCalendarMonthView::DeleteCalendarEntry, this);
Unbind(XCCALENDARMONTH_ADDENTRY, &XCCalendarMonthView::AddCalendarEntry, this);
Unbind(XCCALENDARMONTH_UPDATEENTRY, &XCCalendarMonthView::UpdateCalendarEntry, this);
Unbind(XCCALENDARMONTH_UPDATECALENDARCOLOUR, &XCCalendarMonthView::UpdateCalendarColour, this);
// Don't delete CalendarDataStorage object (used elsewhere).
calendarStorage = nullptr;
}
void XCCalendarMonthView::ProcessGrid(XCCalendarMonthViewGrid *grid, std::vector *hideAccounts, std::vector *hideCalendars){
// TODO: Delete the old calendar entries.
// Add the new calendar entries.
int week = 0;
for (vector::iterator weekIter = grid->weekList.begin();
weekIter != grid->weekList.end(); weekIter++){
week++;
for (vector::iterator dayIter = (*weekIter).dayList.begin();
dayIter != (*weekIter).dayList.end(); dayIter++){
// Add Calendar entries.
XCCalendarDay *calendarDayItem = new XCCalendarDay(this, "XCCalendarMonthView Test", wxPoint(50, 50), wxSize(100,100));
// Setup the control.
calendarDayItem->SetupControl((*dayIter).day, (*dayIter).month, (*dayIter).year, (*dayIter).isInMonth, this, calendarStorage, hideAccounts, hideCalendars);
calendarDayList.push_back(calendarDayItem);
windowSizer->Add(calendarDayItem, 1, wxEXPAND, 5);
}
}
// Add the Growable Rows.
for (int weekSeek = 0; weekSeek < week; weekSeek++){
windowSizer->AddGrowableRow((weekSeek + 1));
}
}
void XCCalendarMonthView::ResizeFrameEvent(wxSizeEvent &SizeEvent){
// TODO: Check if window size is less than 120 pixels and if it is,
// switch to the small block mode.
// Refresh the window.
this->Refresh();
}
void XCCalendarMonthView::DeselectOthersEvent(wxCommandEvent &deselectEvent){
for (vector::iterator dayIter = calendarDayList.begin();
dayIter != calendarDayList.end(); dayIter++){
if (deselectEvent.GetClientData() != (void*)(*dayIter)){
wxCommandEvent deselectEvent(XCCALENDARDAY_DESELECTALLENTRIES);
deselectEvent.SetId(ID_DESELECTALLITEMS);
wxPostEvent((*dayIter), deselectEvent);
}
}
}
void XCCalendarMonthView::HideAccountEntries(wxCommandEvent &accountData){
for (vector::iterator dayIter = calendarDayList.begin();
dayIter != calendarDayList.end(); dayIter++){
wxCommandEvent hideEvent(XCCALENDARDAY_HIDEACCOUNTENTRIES);
hideEvent.SetInt(accountData.GetInt());
hideEvent.SetId(ID_HIDEENTRIES);
wxPostEvent((*dayIter), hideEvent);
}
}
void XCCalendarMonthView::ShowAccountEntries(wxCommandEvent &accountData){
for (vector::iterator dayIter = calendarDayList.begin();
dayIter != calendarDayList.end(); dayIter++){
wxCommandEvent showEvent(XCCALENDARDAY_SHOWACCOUNTENTRIES);
showEvent.SetInt(accountData.GetInt());
showEvent.SetId(ID_SHOWENTRIES);
wxPostEvent((*dayIter), showEvent);
}
}
void XCCalendarMonthView::HideCalendarEntries(wxCommandEvent &accountData){
for (vector::iterator dayIter = calendarDayList.begin();
dayIter != calendarDayList.end(); dayIter++){
wxCommandEvent hideEvent(XCCALENDARDAY_HIDECALENDARENTRIES);
hideEvent.SetInt(accountData.GetInt());
hideEvent.SetId(ID_HIDECALENDARENTRIES);
wxPostEvent((*dayIter), hideEvent);
}
}
void XCCalendarMonthView::ShowCalendarEntries(wxCommandEvent &accountData){
for (vector::iterator dayIter = calendarDayList.begin();
dayIter != calendarDayList.end(); dayIter++){
wxCommandEvent hideEvent(XCCALENDARDAY_SHOWCALENDARENTRIES);
hideEvent.SetInt(accountData.GetInt());
hideEvent.SetId(ID_SHOWCALENDARENTRIES);
wxPostEvent((*dayIter), hideEvent);
}
}
void XCCalendarMonthView::DeleteCalendarEntries(wxCommandEvent &calendarData){
for (vector::iterator dayIter = calendarDayList.begin();
dayIter != calendarDayList.end(); dayIter++){
wxCommandEvent deleteEvent(XCCALENDARDAY_DELETECALENDARENTRIES);
deleteEvent.SetInt(calendarData.GetInt());
deleteEvent.SetId(ID_DELETECALENDARENTRIES);
wxPostEvent((*dayIter), deleteEvent);
}
}
void XCCalendarMonthView::DeleteCalendarEntry(wxCommandEvent &eventData){
for (vector::iterator dayIter = calendarDayList.begin();
dayIter != calendarDayList.end(); dayIter++){
wxCommandEvent deleteEvent(XCCALENDARDAY_DELETEENTRY);
deleteEvent.SetInt(eventData.GetInt());
deleteEvent.SetId(ID_DELETEENTRY);
wxPostEvent((*dayIter), deleteEvent);
}
}
void XCCalendarMonthView::AddCalendarEntry(wxCommandEvent &eventData){
EventProperties *eventInfo = (EventProperties*)eventData.GetClientData();
bool foundDate = false;
for (vector::iterator dayIter = calendarDayList.begin();
dayIter != calendarDayList.end(); dayIter++){
// Check if the date matches with the date in the day
// widget. If it does, add the widget.
if ((*dayIter)->GetCalendarYear() == eventInfo->eventYear &&
(*dayIter)->GetCalendarMonth() == (eventInfo->eventMonth + 1) &&
(*dayIter)->GetCalendarDay() == eventInfo->eventDay){
wxCommandEvent addEvent(XCCALENDARDAY_ADDENTRY);
addEvent.SetId(ID_ADDENTRY);
addEvent.SetClientData(eventInfo);
wxPostEvent((*dayIter), addEvent);
foundDate = true;
break;
}
}
if (foundDate == false){
delete eventInfo;
eventInfo = nullptr;
}
}
void XCCalendarMonthView::UpdateCalendarEntry(wxCommandEvent &eventData){
EventProperties *eventInfo = (EventProperties*)eventData.GetClientData();
bool foundDate = false;
for (vector::iterator dayIter = calendarDayList.begin();
dayIter != calendarDayList.end(); dayIter++){
wxCommandEvent addEvent(XCCALENDARDAY_DELETEENTRY);
addEvent.SetId(ID_DELETEENTRY);
addEvent.SetInt(eventInfo->eventID);
wxPostEvent((*dayIter), addEvent);
}
for (vector::iterator dayIter = calendarDayList.begin();
dayIter != calendarDayList.end(); dayIter++){
// Check if the date matches with the date in the day
// widget. If it does, update the widget.
if ((*dayIter)->GetCalendarYear() == eventInfo->eventYear &&
(*dayIter)->GetCalendarMonth() == (eventInfo->eventMonth + 1) &&
(*dayIter)->GetCalendarDay() == eventInfo->eventDay){
wxCommandEvent addEvent(XCCALENDARDAY_ADDENTRY);
addEvent.SetId(ID_ADDENTRY);
addEvent.SetClientData(eventInfo);
wxPostEvent((*dayIter), addEvent);
foundDate = true;
break;
}
}
if (foundDate == false){
delete eventInfo;
eventInfo = nullptr;
}
}
void XCCalendarMonthView::UpdateCalendarColour(wxCommandEvent &colourData){
ColourUpdateProperties *colourDataInfo = (ColourUpdateProperties*)colourData.GetClientData();
for (vector::iterator dayIter = calendarDayList.begin();
dayIter != calendarDayList.end(); dayIter++){
// Check if the date matches with the date in the day
// widget. If it does, update the widget.
ColourUpdateProperties *colourDataPassInfo = colourDataInfo;
wxCommandEvent updateColour(XCCALENDARDAY_UPDATECALENDARCOLOUR);
updateColour.SetId(ID_UPDATECOLOUR);
updateColour.SetClientData(colourDataPassInfo);
wxPostEvent((*dayIter), updateColour);
}
}
void XCCalendarMonthView::ShowEventMenu(wxCommandEvent &eventData){
EventProperties *eventInfo = (EventProperties*)eventData.GetClientData();
eventMenu->SetEventID(eventInfo->eventID);
eventMenu->SetCalendarID(eventInfo->calendarID);
eventMenu->SetWindowPointer(this->GetParent()->GetParent());
this->PopupMenu(eventMenu, wxDefaultPosition);
delete eventInfo;
eventInfo = nullptr;
}