1 // XCCalendarManipulator.cpp - Xestia Calendar XCCalendarManipulator 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 "XCCalendarManipulator.h"
21 wxDEFINE_EVENT(XCCALENDARMANIPULATOR_CHANGEGRID, wxCommandEvent);
25 XCCalendarManipulator::XCCalendarManipulator(wxWindow* parent, const wxString& title,
26 const wxPoint& pos, const wxSize& size, CalendarDataStorage *dataStorage)
27 : wxPanel(parent, wxID_ANY, pos, size, wxTAB_TRAVERSAL, title){
28 szrMain = new wxBoxSizer( wxVERTICAL );
29 pnlMain = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(500, 50), wxTAB_TRAVERSAL);
30 #if defined(__APPLE__)
31 pnlMain->SetBackgroundColour(wxColour(120,120,120));
33 pnlMain->SetBackgroundColour(wxColour(40,40,40));
35 this->SetSizer(szrMain);
36 szrMain->Add(pnlMain, 0, wxEXPAND, 0);
38 // Setup the navigation section.
40 szrNavigation = new wxBoxSizer( wxHORIZONTAL );
41 pnlMain->SetSizer(szrNavigation);
43 // Add next month and previous month buttons.
45 previousButton = new wxButton(pnlMain, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(32,32), 0|wxNO_BORDER);
46 nextButton = new wxButton(pnlMain, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(32,32), 0|wxNO_BORDER);
47 calendarsButton = new wxButton(pnlMain, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(32,32), 0|wxNO_BORDER);
49 wxMemoryInputStream previousIcon(icons_previous_png, sizeof(icons_previous_png));
50 wxMemoryInputStream nextIcon(icons_next_png, sizeof(icons_next_png));
51 wxMemoryInputStream calendarsIcon(icons_calendars_png, sizeof(icons_calendars_png));
53 wxImage icons_previous_png(previousIcon, wxBITMAP_TYPE_PNG);
54 previousIconBitmap = wxBitmap(icons_previous_png, -1);
56 wxImage icons_next_png(nextIcon, wxBITMAP_TYPE_PNG);
57 nextIconBitmap = wxBitmap(icons_next_png, -1);
59 wxImage icons_calendars_png(calendarsIcon, wxBITMAP_TYPE_PNG);
60 calendarsIconBitmap = wxBitmap(icons_calendars_png, -1);
62 previousButton->SetBitmap(previousIconBitmap);
63 nextButton->SetBitmap(nextIconBitmap);
64 calendarsButton->SetBitmap(calendarsIconBitmap);
65 previousButton->SetBackgroundColour(pnlMain->GetBackgroundColour());
66 nextButton->SetBackgroundColour(pnlMain->GetBackgroundColour());
67 calendarsButton->SetBackgroundColour(pnlMain->GetBackgroundColour());
69 // Setup the static text.
71 dateButton = new wxButton(pnlMain, wxID_ANY, wxT(""), wxDefaultPosition, wxDefaultSize, 0|wxNO_BORDER);
75 test.SetWeight(wxFONTWEIGHT_BOLD);
76 test.SetPointSize(18);
78 dateButton->SetFont(test);
79 dateButton->SetForegroundColour(wxColour(255,255,255));
80 dateButton->SetBackgroundColour(pnlMain->GetBackgroundColour());
82 // Setup the manipulator control.
84 szrNavigation->Add(previousButton, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5);
85 szrNavigation->Add(calendarsButton, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5);
86 szrNavigation->Add( 0, 0, 1, wxEXPAND, 5 );
87 szrNavigation->Add(dateButton, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
88 szrNavigation->Add( 0, 0, 1, wxEXPAND, 5 );
89 szrNavigation->Add(nextButton, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5);
90 szrNavigation->Layout();
92 // Setup the month selection control.
94 wxDateTime dtNow = wxDateTime::Now();
96 #if defined(WIN32) || defined(__APPLE__)
97 moo = new frmCalendarSelectMonth(this);
99 moo = new XCCalendarMonthSelect(this);
101 month = ((int)dtNow.GetMonth() + 1);
102 year = dtNow.GetYear();
103 moo->UpdateDate(month, year);
104 UpdateDateButtonText();
106 // Setup the calendars list.
108 calendarList = new XCCalendarList(this);
110 // Setup the calendar data storage pointer.
112 this->dataStorage = dataStorage;
114 // Setup the event controls.
116 dateButton->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(XCCalendarManipulator::DateTextClick), NULL, this);
117 nextButton->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(XCCalendarManipulator::NextMonth), NULL, this);
118 calendarsButton->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(XCCalendarManipulator::ShowCalendarsList), NULL, this);
119 previousButton->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(XCCalendarManipulator::PreviousMonth), NULL, this);
122 dateButton->Connect(wxEVT_ENTER_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseover), NULL, this);
123 dateButton->Connect(wxEVT_LEAVE_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseout), NULL, this);
124 nextButton->Connect(wxEVT_ENTER_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseover), NULL, this);
125 nextButton->Connect(wxEVT_LEAVE_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseout), NULL, this);
126 calendarsButton->Connect(wxEVT_ENTER_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseover), NULL, this);
127 calendarsButton->Connect(wxEVT_LEAVE_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseout), NULL, this);
128 previousButton->Connect(wxEVT_ENTER_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseover), NULL, this);
129 previousButton->Connect(wxEVT_LEAVE_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseout), NULL, this);
132 Bind(XCCALENDARMANIPULATOR_CHANGEGRID, &XCCalendarManipulator::ChangeGrid, this, ID_CHANGEGRID);
137 XCCalendarManipulator::~XCCalendarManipulator(){
139 // Destory the controls from the widget.
143 void XCCalendarManipulator::DateTextClick(wxCommandEvent &event){
145 // Bring up a popup control to select the month and year.
147 // Do something different for Win32 and macOS.
149 moo->SetPosition(wxPoint(dateButton->GetScreenRect().GetLeft(), dateButton->GetScreenRect().GetBottom()));
150 moo->UpdateDate(month, year);
151 #if defined(WIN32) || defined(__APPLE__)
159 void XCCalendarManipulator::ChangeGrid(wxCommandEvent &event){
161 if (month == moo->GetMonth() && year == moo->GetYear()){
165 month = moo->GetMonth();
166 year = moo->GetYear();
168 // Post an event to the parent control to update the grid.
170 wxCommandEvent changeGrid(XCCALENDARCTRL_CHANGEGRID);
171 changeGrid.SetId(ID_CHANGEGRID);
172 wxPostEvent(this->GetParent(), changeGrid);
174 UpdateDateButtonText();
178 void XCCalendarManipulator::NextMonth(wxCommandEvent &event){
183 // Get the current month and year.
185 newMonth = moo->GetMonth();
186 newYear = moo->GetYear();
198 moo->UpdateDate(month, year);
200 // Post an event to the parent control to update the grid.
202 wxCommandEvent changeGrid(XCCALENDARCTRL_CHANGEGRID);
203 changeGrid.SetId(ID_CHANGEGRID);
204 wxPostEvent(this->GetParent(), changeGrid);
206 UpdateDateButtonText();
210 void XCCalendarManipulator::PreviousMonth(wxCommandEvent &event){
215 // Get the current month and year.
217 newMonth = moo->GetMonth();
218 newYear = moo->GetYear();
230 moo->UpdateDate(month, year);
232 // Post an event to the parent control to update the grid.
234 wxCommandEvent changeGrid(XCCALENDARCTRL_CHANGEGRID);
235 changeGrid.SetId(ID_CHANGEGRID);
236 wxPostEvent(this->GetParent(), changeGrid);
238 UpdateDateButtonText();
242 void XCCalendarManipulator::ShowCalendarsList(wxCommandEvent &event){
244 // Update the list of calendars before showing.
246 calendarList->SetPosition(wxPoint(calendarsButton->GetScreenRect().GetLeft(), calendarsButton->GetScreenRect().GetBottom()));
247 calendarList->UpdateCalendarList(dataStorage);
248 #if defined(__APPLE__)
250 if (calendarListShow == false)
252 calendarList->Show();
253 calendarListShow = true;
257 calendarList->Hide();
258 calendarListShow = false;
261 calendarList->Popup();
266 void XCCalendarManipulator::UpdateDateButtonText(){
268 // Update the date text.
270 string newDateText = "";
272 switch (moo->GetMonth()){
274 newDateText = _("January");
277 newDateText = _("February");
280 newDateText = _("March");
283 newDateText = _("April");
286 newDateText = _("May");
289 newDateText = _("June");
292 newDateText = _("July");
295 newDateText = _("August");
298 newDateText = _("September");
301 newDateText = _("October");
304 newDateText = _("November");
307 newDateText = _("December");
313 newDateText += to_string(year);
315 dateButton->SetLabel(newDateText);
320 int XCCalendarManipulator::GetMonth(){
326 int XCCalendarManipulator::GetYear(){
332 vector<int> XCCalendarManipulator::GetHiddenAccountsList(){
334 return calendarList->GetHiddenAccountsList();
338 vector<int> XCCalendarManipulator::GetHiddenCalendarsList(){
340 return calendarList->GetHiddenCalendarsList();
344 void XCCalendarManipulator::ButtonMouseover(wxMouseEvent &event)
346 wxWindow *eventObject = (wxWindow*)event.GetEventObject();
347 eventObject->SetWindowStyle(0 | wxSIMPLE_BORDER);
350 void XCCalendarManipulator::ButtonMouseout(wxMouseEvent &event)
352 wxWindow *eventObject = (wxWindow*)event.GetEventObject();
353 eventObject->SetWindowStyle(0 | wxNO_BORDER);