// XCCalendarManipulator.cpp - Xestia Calendar XCCalendarManipulator 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 "XCCalendarManipulator.h"
wxDEFINE_EVENT(XCCALENDARMANIPULATOR_CHANGEGRID, wxCommandEvent);
using namespace std;
XCCalendarManipulator::XCCalendarManipulator(wxWindow* parent, const wxString& title,
const wxPoint& pos, const wxSize& size, CalendarDataStorage *dataStorage)
: wxPanel(parent, wxID_ANY, pos, size, wxTAB_TRAVERSAL, title){
szrMain = new wxBoxSizer( wxVERTICAL );
pnlMain = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(500, 50), wxTAB_TRAVERSAL);
#if defined(__APPLE__)
pnlMain->SetBackgroundColour(wxColour(120,120,120));
#else
pnlMain->SetBackgroundColour(wxColour(40,40,40));
#endif
this->SetSizer(szrMain);
szrMain->Add(pnlMain, 0, wxEXPAND, 0);
// Setup the navigation section.
szrNavigation = new wxBoxSizer( wxHORIZONTAL );
pnlMain->SetSizer(szrNavigation);
// Add next month and previous month buttons.
previousButton = new wxButton(pnlMain, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(32,32), 0|wxNO_BORDER);
nextButton = new wxButton(pnlMain, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(32,32), 0|wxNO_BORDER);
calendarsButton = new wxButton(pnlMain, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(32,32), 0|wxNO_BORDER);
wxMemoryInputStream previousIcon(icons_previous_png, sizeof(icons_previous_png));
wxMemoryInputStream nextIcon(icons_next_png, sizeof(icons_next_png));
wxMemoryInputStream calendarsIcon(icons_calendars_png, sizeof(icons_calendars_png));
wxImage icons_previous_png(previousIcon, wxBITMAP_TYPE_PNG);
previousIconBitmap = wxBitmap(icons_previous_png, -1);
wxImage icons_next_png(nextIcon, wxBITMAP_TYPE_PNG);
nextIconBitmap = wxBitmap(icons_next_png, -1);
wxImage icons_calendars_png(calendarsIcon, wxBITMAP_TYPE_PNG);
calendarsIconBitmap = wxBitmap(icons_calendars_png, -1);
previousButton->SetBitmap(previousIconBitmap);
nextButton->SetBitmap(nextIconBitmap);
calendarsButton->SetBitmap(calendarsIconBitmap);
previousButton->SetBackgroundColour(pnlMain->GetBackgroundColour());
nextButton->SetBackgroundColour(pnlMain->GetBackgroundColour());
calendarsButton->SetBackgroundColour(pnlMain->GetBackgroundColour());
// Setup the static text.
dateButton = new wxButton(pnlMain, wxID_ANY, wxT(""), wxDefaultPosition, wxDefaultSize, 0|wxNO_BORDER);
wxFont test;
test.SetWeight(wxFONTWEIGHT_BOLD);
test.SetPointSize(18);
dateButton->SetFont(test);
dateButton->SetForegroundColour(wxColour(255,255,255));
dateButton->SetBackgroundColour(pnlMain->GetBackgroundColour());
// Setup the manipulator control.
szrNavigation->Add(previousButton, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5);
szrNavigation->Add(calendarsButton, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5);
szrNavigation->Add( 0, 0, 1, wxEXPAND, 5 );
szrNavigation->Add(dateButton, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
szrNavigation->Add( 0, 0, 1, wxEXPAND, 5 );
szrNavigation->Add(nextButton, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5);
szrNavigation->Layout();
// Setup the month selection control.
wxDateTime dtNow = wxDateTime::Now();
#if defined(WIN32) || defined(__APPLE__)
moo = new frmCalendarSelectMonth(this);
#else
moo = new XCCalendarMonthSelect(this);
#endif
month = ((int)dtNow.GetMonth() + 1);
year = dtNow.GetYear();
moo->UpdateDate(month, year);
UpdateDateButtonText();
// Setup the calendars list.
calendarList = new XCCalendarList(this);
// Setup the calendar data storage pointer.
this->dataStorage = dataStorage;
// Setup the event controls.
dateButton->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(XCCalendarManipulator::DateTextClick), NULL, this);
nextButton->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(XCCalendarManipulator::NextMonth), NULL, this);
calendarsButton->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(XCCalendarManipulator::ShowCalendarsList), NULL, this);
previousButton->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(XCCalendarManipulator::PreviousMonth), NULL, this);
#if defined(WIN32)
dateButton->Connect(wxEVT_ENTER_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseover), NULL, this);
dateButton->Connect(wxEVT_LEAVE_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseout), NULL, this);
nextButton->Connect(wxEVT_ENTER_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseover), NULL, this);
nextButton->Connect(wxEVT_LEAVE_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseout), NULL, this);
calendarsButton->Connect(wxEVT_ENTER_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseover), NULL, this);
calendarsButton->Connect(wxEVT_LEAVE_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseout), NULL, this);
previousButton->Connect(wxEVT_ENTER_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseover), NULL, this);
previousButton->Connect(wxEVT_LEAVE_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseout), NULL, this);
#endif
Bind(XCCALENDARMANIPULATOR_CHANGEGRID, &XCCalendarManipulator::ChangeGrid, this, ID_CHANGEGRID);
this->Refresh();
}
XCCalendarManipulator::~XCCalendarManipulator(){
// Destory the controls from the widget.
dateButton->Disconnect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(XCCalendarManipulator::DateTextClick), NULL, this);
nextButton->Disconnect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(XCCalendarManipulator::NextMonth), NULL, this);
calendarsButton->Disconnect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(XCCalendarManipulator::ShowCalendarsList), NULL, this);
previousButton->Disconnect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(XCCalendarManipulator::PreviousMonth), NULL, this);
#if defined(WIN32)
dateButton->Disconnect(wxEVT_ENTER_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseover), NULL, this);
dateButton->Disconnect(wxEVT_LEAVE_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseout), NULL, this);
nextButton->Disconnect(wxEVT_ENTER_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseover), NULL, this);
nextButton->Disconnect(wxEVT_LEAVE_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseout), NULL, this);
calendarsButton->Disconnect(wxEVT_ENTER_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseover), NULL, this);
calendarsButton->Disconnect(wxEVT_LEAVE_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseout), NULL, this);
previousButton->Disconnect(wxEVT_ENTER_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseover), NULL, this);
previousButton->Disconnect(wxEVT_LEAVE_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseout), NULL, this);
#endif
Unbind(XCCALENDARMANIPULATOR_CHANGEGRID, &XCCalendarManipulator::ChangeGrid, this, ID_CHANGEGRID);
// Delete the buttons in pnlMain.
szrNavigation->Clear();
delete previousButton;
previousButton = nullptr;
delete nextButton;
nextButton = nullptr;
delete calendarsButton;
calendarsButton = nullptr;
delete dateButton;
dateButton = nullptr;
// Delete szrNavigation.
szrMain->Clear();
pnlMain->SetSizer(nullptr, true);
// Delete pnlMain itself;
delete pnlMain;
pnlMain = nullptr;
delete moo;
moo = nullptr;
delete calendarList;
calendarList = nullptr;
// Delete szrMain itself;
this->SetSizer(nullptr, true);
dataStorage = nullptr;
}
void XCCalendarManipulator::DateTextClick(wxCommandEvent &event){
// Bring up a popup control to select the month and year.
// Do something different for Win32 and macOS.
moo->SetPosition(wxPoint(dateButton->GetScreenRect().GetLeft(), dateButton->GetScreenRect().GetBottom()));
moo->UpdateDate(month, year);
#if defined(WIN32) || defined(__APPLE__)
moo->ShowModal();
#else
moo->Popup();
#endif
}
void XCCalendarManipulator::ChangeGrid(wxCommandEvent &event){
if (month == moo->GetMonth() && year == moo->GetYear()){
return;
}
month = moo->GetMonth();
year = moo->GetYear();
// Post an event to the parent control to update the grid.
wxCommandEvent changeGrid(XCCALENDARCTRL_CHANGEGRID);
changeGrid.SetId(ID_CHANGEGRID);
wxPostEvent(this->GetParent(), changeGrid);
UpdateDateButtonText();
}
void XCCalendarManipulator::NextMonth(wxCommandEvent &event){
int newMonth = 1;
int newYear = 0;
// Get the current month and year.
newMonth = moo->GetMonth();
newYear = moo->GetYear();
if (newMonth == 12){
newMonth = 1;
newYear++;
} else {
newMonth++;
}
month = newMonth;
year = newYear;
moo->UpdateDate(month, year);
// Post an event to the parent control to update the grid.
wxCommandEvent changeGrid(XCCALENDARCTRL_CHANGEGRID);
changeGrid.SetId(ID_CHANGEGRID);
wxPostEvent(this->GetParent(), changeGrid);
UpdateDateButtonText();
}
void XCCalendarManipulator::PreviousMonth(wxCommandEvent &event){
int newMonth = 1;
int newYear = 0;
// Get the current month and year.
newMonth = moo->GetMonth();
newYear = moo->GetYear();
if (newMonth == 1){
newMonth = 12;
newYear--;
} else {
newMonth--;
}
month = newMonth;
year = newYear;
moo->UpdateDate(month, year);
// Post an event to the parent control to update the grid.
wxCommandEvent changeGrid(XCCALENDARCTRL_CHANGEGRID);
changeGrid.SetId(ID_CHANGEGRID);
wxPostEvent(this->GetParent(), changeGrid);
UpdateDateButtonText();
}
void XCCalendarManipulator::ShowCalendarsList(wxCommandEvent &event){
// Update the list of calendars before showing.
calendarList->SetPosition(wxPoint(calendarsButton->GetScreenRect().GetLeft(), calendarsButton->GetScreenRect().GetBottom()));
calendarList->UpdateCalendarList(dataStorage);
#if defined(__APPLE__)
if (calendarListShow == false)
{
calendarList->Show();
calendarListShow = true;
}
else
{
calendarList->Hide();
calendarListShow = false;
}
#else
calendarList->Popup();
#endif
}
void XCCalendarManipulator::UpdateDateButtonText(){
// Update the date text.
string newDateText = "";
switch (moo->GetMonth()){
case 1:
newDateText = _("January");
break;
case 2:
newDateText = _("February");
break;
case 3:
newDateText = _("March");
break;
case 4:
newDateText = _("April");
break;
case 5:
newDateText = _("May");
break;
case 6:
newDateText = _("June");
break;
case 7:
newDateText = _("July");
break;
case 8:
newDateText = _("August");
break;
case 9:
newDateText = _("September");
break;
case 10:
newDateText = _("October");
break;
case 11:
newDateText = _("November");
break;
case 12:
newDateText = _("December");
break;
}
newDateText += " ";
newDateText += to_string(year);
dateButton->SetLabel(newDateText);
szrMain->Layout();
}
int XCCalendarManipulator::GetMonth(){
return month;
}
int XCCalendarManipulator::GetYear(){
return year;
}
vector XCCalendarManipulator::GetHiddenAccountsList(){
return calendarList->GetHiddenAccountsList();
}
vector XCCalendarManipulator::GetHiddenCalendarsList(){
return calendarList->GetHiddenCalendarsList();
}
void XCCalendarManipulator::ButtonMouseover(wxMouseEvent &event)
{
wxWindow *eventObject = (wxWindow*)event.GetEventObject();
eventObject->SetWindowStyle(0 | wxSIMPLE_BORDER);
}
void XCCalendarManipulator::ButtonMouseout(wxMouseEvent &event)
{
wxWindow *eventObject = (wxWindow*)event.GetEventObject();
eventObject->SetWindowStyle(0 | wxNO_BORDER);
}