// XCCalendarDay.cpp - Xestia Calendar XCCalendarDay 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 "XCCalendarDay.h"
using namespace std;
BEGIN_EVENT_TABLE(XCCalendarDay, wxPanel)
EVT_PAINT(XCCalendarDay::PaintFrameEvent)
EVT_SIZE(XCCalendarDay::ResizeFrameEvent)
END_EVENT_TABLE()
XCCalendarDay::XCCalendarDay(wxWindow *parent, const wxString& title, const wxPoint& pos, const wxSize& size)
: wxPanel(parent, wxID_ANY, pos, size, wxTAB_TRAVERSAL, title){
this->SetMinSize( wxSize( 100,100 ) );
// Setip the icons.
wxMemoryInputStream alerticon(icons_alert32_png, sizeof(icons_alert32_png));
wxMemoryInputStream priorityicon(icons_priority32_png, sizeof(icons_priority32_png));
wxImage icons_alert_png(alerticon, wxBITMAP_TYPE_PNG);
wxBitmap imgAlertIcon(icons_alert_png, -1);
wxImage icons_priority_png(priorityicon, wxBITMAP_TYPE_PNG);
wxBitmap imgPriorityIcon(icons_priority_png, -1);
alertIcon->SetBitmap(imgAlertIcon);
highPriorityIcon->SetBitmap(imgPriorityIcon);
windowSizer->AddGrowableCol(0);
windowSizer->AddGrowableRow(1);
windowSizer->SetFlexibleDirection( wxBOTH );
windowSizer->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
numberText = new wxStaticText(this, wxID_ANY, wxT("09"), wxDefaultPosition, wxDefaultSize, 0);
numberText->SetFont(wxFont(24, 70, 90, 92, false, wxEmptyString));
topSectionSizer->Add(highPriorityIcon, 0, wxALL|wxALIGN_CENTER_VERTICAL, 0);
topSectionSizer->Add(alertIcon, 0, wxALL|wxALIGN_CENTER_VERTICAL, 0);
topSectionSizer->Add(0, 0, 1, wxEXPAND, 5);
topSectionSizer->Add(numberText, 0, wxALL|wxRIGHT|wxALIGN_CENTER_VERTICAL, 5);
// Setup the scrollable section.
Colour eventColour;
eventColour.red = 40; eventColour.green = 80; eventColour.blue = 80;
eventListFrame->SetSizer(eventListFrameSizer);
eventListFrame->SetScrollRate(0,1);
//EventListFrameSizer->FitInside(EventListFrame);
eventListFrameSizer->Fit(EventListFrame);
eventListFrameSizer->Layout();
windowSizer->Fit(this);
// Setup the scroll window.
mainSectionSizer->Add(eventListFrame, 1, wxGROW | wxALL, 10);
windowSizer->Add(topSectionSizer, 1, wxEXPAND, 5);
windowSizer->Add(mainSectionSizer, 1, wxEXPAND, 5);
this->SetSizer(windowSizer);
this->SetSize(size);
this->Layout();
this->SetBackgroundStyle(wxBG_STYLE_PAINT);
this->Centre(wxBOTH);
UpdateTopIcons();
Connect(ID_DESELECTOTHERENTRIES, XCCALENDARDAY_DESELECTOTHERENTRIES, wxCommandEventHandler(XCCalendarDay::DeselectOthersEvent));
Connect(ID_DESELECTALLITEMS, XCCALENDARDAY_DESELECTALLENTRIES, wxCommandEventHandler(XCCalendarDay::DeselectAllEvent));
Connect(ID_HIDEENTRIES, XCCALENDARDAY_HIDEACCOUNTENTRIES, wxCommandEventHandler(XCCalendarDay::HideAccountEntries));
Connect(ID_SHOWENTRIES, XCCALENDARDAY_SHOWACCOUNTENTRIES, wxCommandEventHandler(XCCalendarDay::ShowAccountEntries));
Connect(ID_HIDECALENDARENTRIES, XCCALENDARDAY_HIDECALENDARENTRIES, wxCommandEventHandler(XCCalendarDay::HideCalendarEntries));
Connect(ID_SHOWCALENDARENTRIES, XCCALENDARDAY_SHOWCALENDARENTRIES, wxCommandEventHandler(XCCalendarDay::ShowCalendarEntries));
Connect(ID_DELETECALENDARENTRIES, XCCALENDARDAY_DELETECALENDARENTRIES, wxCommandEventHandler(XCCalendarDay::DeleteCalendarEntries));
Connect(ID_DELETEENTRY, XCCALENDARDAY_DELETEENTRY, wxCommandEventHandler(XCCalendarDay::DeleteCalendarEntry));
Connect(ID_ADDENTRY, XCCALENDARDAY_ADDENTRY, wxCommandEventHandler(XCCalendarDay::AddCalendarEntry));
Connect(ID_UPDATEENTRY, XCCALENDARDAY_UPDATEENTRY, wxCommandEventHandler(XCCalendarDay::UpdateCalendarEntry));
Connect(ID_UPDATECOLOUR, XCCALENDARDAY_UPDATECALENDARCOLOUR, wxCommandEventHandler(XCCalendarDay::UpdateCalendarColour));
}
XCCalendarDay::~XCCalendarDay(){
// Destory the controls from the widget.
for (vector::iterator calendarEntryIter = calendarEntryList.begin();
calendarEntryIter != calendarEntryList.end(); calendarEntryIter++){
delete((*calendarEntryIter));
}
calendarEntryList.clear();
this->Refresh();
}
void XCCalendarDay::Repaint(){
wxPaintDC dc(this);
wxPaintDC eventListFrameDC(eventListFrame);
// Get the wxSizerItem for the top date and the entries part.
wxSizerItem *topSectionSizerItem = windowSizer->GetItem((size_t)0);
wxSizerItem *mainSectionSizerItem = windowSizer->GetItem((size_t)1);
wxRect topSizer = wxRect(windowSizer->GetPosition(), windowSizer->GetSize());
if (isInMonth == true){
dc.SetPen(wxPen(wxColor(255,255,255), 0, wxPENSTYLE_TRANSPARENT));
dc.SetBrush(wxBrush(wxColor(255,255,255)));
dc.DrawRectangle(topSectionSizerItem->GetRect());
dc.SetBrush(wxBrush(wxColor(225,225,225)));
dc.DrawRectangle(mainSectionSizerItem->GetRect());
eventListFrameDC.SetPen(wxPen(wxColor(255,255,255), 0, wxPENSTYLE_TRANSPARENT));
eventListFrameDC.SetBrush(wxBrush(wxColor(225,225,225)));
eventListFrameDC.DrawRectangle(eventListFrame->GetClientRect());
} else {
dc.SetPen(wxPen(wxColor(185,185,185), 0, wxPENSTYLE_TRANSPARENT));
dc.SetBrush(wxBrush(wxColor(185,185,185)));
dc.DrawRectangle(topSectionSizerItem->GetRect());
dc.SetBrush(wxBrush(wxColor(155,155,155)));
dc.DrawRectangle(mainSectionSizerItem->GetRect());
eventListFrameDC.SetPen(wxPen(wxColor(255,255,255), 0, wxPENSTYLE_TRANSPARENT));
eventListFrameDC.SetBrush(wxBrush(wxColor(155,155,155)));
eventListFrameDC.DrawRectangle(eventListFrame->GetClientRect());
}
// Draw the border.
//dc.SetBrush(wxBrush(wxColor(0,0,0), wxBRUSHSTYLE_TRANSPARENT));
this->Layout();
}
void XCCalendarDay::UpdateTopIcons(){
bool alarmFound = false;
bool highPriorityFound = false;
for (vector::iterator entryIter = calendarEntryList.begin();
entryIter != calendarEntryList.end(); entryIter++){
if ((*entryIter)->GetDisplayAlarm() == true){
alarmFound = true;
}
if ((*entryIter)->GetDisplayHighPriority() == true){
highPriorityFound = true;
}
if (alarmFound == true && highPriorityFound == true){
break;
}
}
alertIcon->Show(alarmFound);
highPriorityIcon->Show(highPriorityFound);
}
void XCCalendarDay::PaintFrameEvent(wxPaintEvent &paintEvent)
{
Repaint();
}
void XCCalendarDay::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 XCCalendarDay::DeselectOthersEvent(wxCommandEvent &deselectEvent)
{
int selectedEntryID = deselectEvent.GetInt();
wxCommandEvent deselectEntryEvent(XCCALENDARDAYENTRY_DESELECT);
deselectEntryEvent.SetId(ID_ENTRYDESELECT);
for (vector::iterator entryIter = calendarEntryList.begin();
entryIter != calendarEntryList.end(); entryIter++){
if ((*entryIter)->GetID() != selectedEntryID){
wxPostEvent((*entryIter), deselectEntryEvent);
}
}
// Send event notification to deselect the other calendar entries.
if (this->monthViewPointer != nullptr){
wxCommandEvent deselectEvent(XCCALENDARMONTH_DESELECTOTHERENTRIES);
deselectEvent.SetClientData(this);
deselectEvent.SetId(ID_MONTHVIEWCLEARSELECTION);
wxPostEvent(this->monthViewPointer, deselectEvent);
}
}
void XCCalendarDay::DeselectAllEvent(wxCommandEvent &DeselectEvent)
{
int selectedEntryID = deselectEvent.GetInt();
wxCommandEvent deselectEntryEvent(XCCALENDARDAYENTRY_DESELECT);
deselectEntryEvent.SetId(ID_ENTRYDESELECT);
for (vector::iterator entryIter = calendarEntryList.begin();
entryIter != calendarEntryList.end(); EntryIter++){
wxPostEvent((*entryIter), deselectEntryEvent);
}
}
void XCCalendarDay::SetupControl(int setupDay, int setupMonth, int setupYear, bool setupIsInMonth, XCCalendarMonthView *monthViewPointer, CalendarDataStorage *dataStorage, vector *hideAccounts, vector *hideCalendars)
{
this->dataStorage = dataStorage;
// Set the day
numberText->SetLabel(wxString::Format("%02i", setupDay));
calendarDay = setupDay;
// Set the month
calendarMonth = setupMonth;
// Set the year.
calendarYear = setupYear;
// Setup the Is In Month value.
isInMonth = setupIsInMonth;
// Setup the month view pointer.
this->monthViewPointer = monthViewPointer;
// Setup the calendar items.
CDSEntryList calendarItems = dataStorage->GetEventListByDate(setupYear, setupMonth, setupDay);
for (int entrySeek = 0; entrySeek < calendarItems.entryList.size(); entrySeek++){
// Get the information about the calendar entry.
CDSGetCalendarEntryInfo newEntryInfo = dataStorage->GetEvent(calendarItems.entryList[entrySeek]);
CDSGetCalendarInfo newEntryCalendarInfo = dataStorage->GetCalendar(newEntryInfo.calendarID);
// Setup the calendar entry.
XCCalendarDayEntry *newEntry = new XCCalendarDayEntry(eventListFrame, newEntryInfo.entryName, wxDefaultPosition, wxDefaultSize, calendarItems.entryList[entrySeek]);
newEntry->SetColour(&newEntryCalendarInfo.calendarColour);
newEntry->SetTime(newEntryInfo.entryStartHour, newEntryInfo.entryStartMinute, newEntryInfo.entryStartSecond);
newEntry->SetEntryIDs(newEntryCalendarInfo.accountID, newEntryInfo.calendarID, newEntryInfo.calendarEntryID);
eventListFrameSizer->Add(newEntry, 0, wxEXPAND, 5);
wxSizerItem *afterSpacer = eventListFrameSizer->Add(0, 5, 0, 0, 5);
newEntry->SetAfterSpacer(afterSpacer);
// Go through the list of calendar entries to hide by account.
for (vector::iterator hideAccountsItem = hideAccounts->begin();
hideAccountsItem != hideAccounts->end(); hideAccountsItem++){
if (*hideAccountsItem == newEntryCalendarInfo.accountID){
newEntry->Show(false);
newEntry->GetAfterSpacer()->Show(false);
}
}
// Go through the list of calendar entries to hide by calendar.
for (vector::iterator hideCalendarsItem = hideCalendars->begin();
hideCalendarsItem != hideCalendars->end(); hideCalendarsItem++){
if (*hideCalendarsItem == newEntryInfo.calendarID){
newEntry->Show(false);
newEntry->GetAfterSpacer()->Show(false);
}
}
calendarEntryList.push_back(newEntry);
}
}
void XCCalendarDay::HideAccountEntries(wxCommandEvent &accountData){
// Go through each of the controls and hide the controls (and spacing) that
// have the matched account IDs.
int sizerPosition = 0;
for (vector::iterator calendarEntryIter = calendarEntryList.begin();
calendarEntryIter != calendarEntryList.end(); calendarEntryIter++){
(*calendarEntryIter)->SetShowAccountStatus(false);
if ((*calendarEntryIter)->GetAccountID() == accountData.GetInt()){
wxSizerItem *calendarItem = eventListFrameSizer->GetItem((*calendarEntryIter));
(*calendarEntryIter)->Show(false);
// Get the spacing and hide it as well.
wxSizerItem *afterSpacer = (*calendarEntryIter)->GetAfterSpacer();
afterSpacer->Show(false);
}
}
this->Refresh();
Repaint();
}
void XCCalendarDay::ShowAccountEntries(wxCommandEvent &accountData){
// Go through each of the controls and hide the controls (and spacing) that
// have the matched account IDs.
int sizerPosition = 0;
for (vector::iterator calendarEntryIter = calendarEntryList.begin();
calendarEntryIter != calendarEntryList.end(); calendarEntryIter++){
(*calendarEntryIter)->SetShowAccountStatus(true);
if ((*calendarEntryIter)->GetShowCalendarStatus() == false){
continue;
}
if ((*calendarEntryIter)->GetAccountID() == accountData.GetInt()){
wxSizerItem *calendarItem = eventListFrameSizer->GetItem((*calendarEntryIter));
(*calendarEntryIter)->Show(true);
// Get the spacing and hide it as well.
wxSizerItem *afterSpacer = (*calendarEntryIter)->GetAfterSpacer();
afterSpacer->Show(true);
}
}
this->Refresh();
Repaint();
}
void XCCalendarDay::HideCalendarEntries(wxCommandEvent &calendarData){
// Go through each of the controls and hide the controls (and spacing) that
// have the matched account IDs.
int sizerPosition = 0;
for (vector::iterator calendarEntryIter = calendarEntryList.begin();
calendarEntryIter != calendarEntryList.end(); calendarEntryIter++){
if ((*calendarEntryIter)->GetCalendarID() == calendarData.GetInt()){
(*calendarEntryIter)->SetShowCalendarStatus(false);
wxSizerItem *calendarItem = eventListFrameSizer->GetItem((*calendarEntryIter));
(*calendarEntryIter)->Show(false);
// Get the spacing and hide it as well.
wxSizerItem *afterSpacer = (*calendarEntryIter)->GetAfterSpacer();
afterSpacer->Show(false);
}
}
this->Refresh();
Repaint();
}
void XCCalendarDay::ShowCalendarEntries(wxCommandEvent &calendarData){
// Go through each of the controls and hide the controls (and spacing) that
// have the matched account IDs.
int sizerPosition = 0;
for (vector::iterator calendarEntryIter = calendarEntryList.begin();
calendarEntryIter != calendarEntryList.end(); calendarEntryIter++){
if ((*calendarEntryIter)->GetCalendarID() == calendarData.GetInt()){
(*calendarEntryIter)->SetShowCalendarStatus(true);
if ((*calendarEntryIter)->GetShowAccountStatus() == false){
// Don't show the calendar entry because the account status
// is set to hidden. Continue to the next one.
continue;
}
wxSizerItem *calendarItem = EventListFrameSizer->GetItem((*calendarEntryIter));
(*calendarEntryIter)->Show(true);
// Get the spacing and hide it as well.
wxSizerItem *afterSpacer = (*calendarEntryIter)->GetAfterSpacer();
afterSpacer->Show(true);
}
}
this->Refresh();
Repaint();
}
void XCCalendarDay::DeleteCalendarEntries(wxCommandEvent &calendarData){
vector::iterator> deleteEntriesList;
for (vector::iterator calendarEntryIter = calendarEntryList.begin();
calendarEntryIter != calendarEntryList.end(); calendarEntryIter++){
if ((*calendarEntryIter)->GetCalendarID() == calendarData.GetInt()){
wxSizerItem *calendarItem = eventListFrameSizer->GetItem((*calendarEntryIter));
(*calendarEntryIter)->Show(false);
// Get the spacing and hide it as well.
wxSizerItem *afterSpacer = (*calendarEntryIter)->GetAfterSpacer();
afterSpacer->Show(false);
afterSpacer->DetachSizer();
delete afterSpacer;
afterSpacer = nullptr;
delete (*calendarEntryIter);
deleteEntriesList.push_back(calendarEntryIter);
}
}
this->Refresh();
Repaint();
for (auto deleteIter : deleteEntriesList){
calendarEntryList.erase(deleteIter);
}
}
void XCCalendarDay::DeleteCalendarEntry(wxCommandEvent &eventData){
vector::iterator> deleteEntriesList;
for (vector::iterator calendarEntryIter = calendarEntryList.begin();
calendarEntryIter != calendarEntryList.end(); calendarEntryIter++){
if ((*calendarEntryIter)->GetEventID() == eventData.GetInt()){
wxSizerItem *calendarItem = EventListFrameSizer->GetItem((*calendarEntryIter));
(*calendarEntryIter)->Show(false);
// Get the spacing and hide it as well.
wxSizerItem *afterSpacer = (*calendarEntryIter)->GetAfterSpacer();
afterSpacer->Show(false);
afterSpacer->DetachSizer();
delete afterSpacer;
afterSpacer = nullptr;
delete (*calendarEntryIter);
deleteEntriesList.push_back(calendarEntryIter);
}
}
for (auto deleteIter : deleteEntriesList){
calendarEntryList.erase(deleteIter);
}
this->Refresh();
Repaint();
}
void XCCalendarDay::AddCalendarEntry(wxCommandEvent &eventData){
EventProperties *eventInfo = (EventProperties*)eventData.GetClientData();
XCCalendarDayEntry *newEntry = new XCCalendarDayEntry(eventListFrame, eventInfo->eventName, wxDefaultPosition, wxDefaultSize, eventInfo->eventID);
CDSGetCalendarInfo calendarInfo = dataStorage->GetCalendar(eventInfo->calendarID);
newEntry->SetColour(&calendarInfo.calendarColour);
newEntry->SetTime(eventInfo->eventHour, eventInfo->eventMinute, eventInfo->eventSecond);
newEntry->SetEntryIDs(calendarInfo.accountID, eventInfo->calendarID, eventInfo->eventID);
eventListFrameSizer->Add(newEntry, 0, wxEXPAND, 5);
wxSizerItem *afterSpacer = eventListFrameSizer->Add(0, 5, 0, 0, 5);
newEntry->SetAfterSpacer(afterSpacer);
// Go through the list of calendar entries to hide by account.
for (vector::iterator hideAccountsItem = eventInfo->hideAccountsList.begin();
hideAccountsItem != eventInfo->hideAccountsList.end(); hideAccountsItem++){
if (*hideAccountsItem = calendarInfo.accountID){
newEntry->Show(false);
newEntry->GetAfterSpacer()->Show(false);
}
}
// Go through the list of calendar entries to hide by calendar.
for (vector::iterator hideCalendarsItem = eventInfo->hideCalendarsList.begin();
hideCalendarsItem != eventInfo->hideCalendarsList.end(); hideCalendarsItem++){
if (*hideCalendarsItem == eventInfo->calendarID){
newEntry->Show(false);
newEntry->GetAfterSpacer()->Show(false);
}
}
calendarEntryList.push_back(newEntry);
this->Refresh();
Repaint();
}
void XCCalendarDay::UpdateCalendarEntry(wxCommandEvent &eventData){
// Go thorugh each of the entries in the day and
// then update it.
EventProperties *eventInfo = (EventProperties*)eventData.GetClientData();
for (vector::iterator calendarEntryIter = calendarEntryList.begin();
calendarEntryIter != calendarEntryList.end(); calendarEntryIter++){
if ((*calendarEntryIter)->GetEventID() == eventInfo->eventID){
(*calendarEntryIter)->SetEventName(eventInfo->eventName);
(*calendarEntryIter)->SetTime(eventInfo->eventHour, eventInfo->eventMinute, eventInfo->eventSecond);
}
}
delete eventInfo;
eventInfo = nullptr;
}
void XCCalendarDay::UpdateCalendarColour(wxCommandEvent &colourData){
// Go thorugh each of the entries in the day and
// then update it.
ColourUpdateProperties *colourInfo = (ColourUpdateProperties*)colourData.GetClientData();
for (vector::iterator calendarEntryIter = calendarEntryList.begin();
calendarEntryIter != calendarEntryList.end(); calendarEntryIter++){
if ((*calendarEntryIter)->GetCalendarID() == colourInfo->calendarID){
(*calendarEntryIter)->SetColour(&colourInfo->newColour);
}
}
}
int XCCalendarDay::GetCalendarDay(){
return calendarDay;
}
int XCCalendarDay::GetCalendarMonth(){
return calendarMonth;
}
int XCCalendarDay::GetCalendarYear(){
return calendarYear;
}