// 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"
BEGIN_EVENT_TABLE(XCCalendarManipulator, wxPanel)
END_EVENT_TABLE()
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);
pnlMain->SetBackgroundColour(wxColour(40,40,40));
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);
// 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));
// 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);
Connect(ID_CHANGEGRID, XCCALENDARMANIPULATOR_CHANGEGRID, wxCommandEventHandler(XCCalendarManipulator::ChangeGrid));
// 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();
Moo = new XCCalendarMonthSelect(this);
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;
}
XCCalendarManipulator::~XCCalendarManipulator(){
// Destory the controls from the widget.
}
void XCCalendarManipulator::DateTextClick(wxCommandEvent &event){
// Bring up a popup control to select the month and year.
Moo->SetPosition(wxPoint(DateButton->GetScreenRect().GetLeft(), DateButton->GetScreenRect().GetBottom()));
Moo->UpdateDate(Month, Year);
Moo->Popup();
}
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);
calendarList->Popup();
}
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();
}