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.
141 dateButton->Disconnect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(XCCalendarManipulator::DateTextClick), NULL, this);
142 nextButton->Disconnect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(XCCalendarManipulator::NextMonth), NULL, this);
143 calendarsButton->Disconnect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(XCCalendarManipulator::ShowCalendarsList), NULL, this);
144 previousButton->Disconnect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(XCCalendarManipulator::PreviousMonth), NULL, this);
147 dateButton->Disconnect(wxEVT_ENTER_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseover), NULL, this);
148 dateButton->Disconnect(wxEVT_LEAVE_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseout), NULL, this);
149 nextButton->Disconnect(wxEVT_ENTER_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseover), NULL, this);
150 nextButton->Disconnect(wxEVT_LEAVE_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseout), NULL, this);
151 calendarsButton->Disconnect(wxEVT_ENTER_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseover), NULL, this);
152 calendarsButton->Disconnect(wxEVT_LEAVE_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseout), NULL, this);
153 previousButton->Disconnect(wxEVT_ENTER_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseover), NULL, this);
154 previousButton->Disconnect(wxEVT_LEAVE_WINDOW, wxMouseEventHandler(XCCalendarManipulator::ButtonMouseout), NULL, this);
157 Unbind(XCCALENDARMANIPULATOR_CHANGEGRID, &XCCalendarManipulator::ChangeGrid, this, ID_CHANGEGRID);
159 // Delete the buttons in pnlMain.
161 szrNavigation->Clear();
163 delete previousButton;
164 previousButton = nullptr;
167 nextButton = nullptr;
169 delete calendarsButton;
170 calendarsButton = nullptr;
173 dateButton = nullptr;
175 // Delete szrNavigation.
178 pnlMain->SetSizer(nullptr, true);
180 // Delete pnlMain itself;
189 calendarList = nullptr;
191 // Delete szrMain itself;
193 this->SetSizer(nullptr, true);
195 dataStorage = nullptr;
199 void XCCalendarManipulator::DateTextClick(wxCommandEvent &event){
201 // Bring up a popup control to select the month and year.
203 // Do something different for Win32 and macOS.
205 moo->SetPosition(wxPoint(dateButton->GetScreenRect().GetLeft(), dateButton->GetScreenRect().GetBottom()));
206 moo->UpdateDate(month, year);
207 #if defined(WIN32) || defined(__APPLE__)
215 void XCCalendarManipulator::ChangeGrid(wxCommandEvent &event){
217 if (month == moo->GetMonth() && year == moo->GetYear()){
221 month = moo->GetMonth();
222 year = moo->GetYear();
224 // Post an event to the parent control to update the grid.
226 wxCommandEvent changeGrid(XCCALENDARCTRL_CHANGEGRID);
227 changeGrid.SetId(ID_CHANGEGRID);
228 wxPostEvent(this->GetParent(), changeGrid);
230 UpdateDateButtonText();
234 void XCCalendarManipulator::NextMonth(wxCommandEvent &event){
239 // Get the current month and year.
241 newMonth = moo->GetMonth();
242 newYear = moo->GetYear();
254 moo->UpdateDate(month, year);
256 // Post an event to the parent control to update the grid.
258 wxCommandEvent changeGrid(XCCALENDARCTRL_CHANGEGRID);
259 changeGrid.SetId(ID_CHANGEGRID);
260 wxPostEvent(this->GetParent(), changeGrid);
262 UpdateDateButtonText();
266 void XCCalendarManipulator::PreviousMonth(wxCommandEvent &event){
271 // Get the current month and year.
273 newMonth = moo->GetMonth();
274 newYear = moo->GetYear();
286 moo->UpdateDate(month, year);
288 // Post an event to the parent control to update the grid.
290 wxCommandEvent changeGrid(XCCALENDARCTRL_CHANGEGRID);
291 changeGrid.SetId(ID_CHANGEGRID);
292 wxPostEvent(this->GetParent(), changeGrid);
294 UpdateDateButtonText();
298 void XCCalendarManipulator::ShowCalendarsList(wxCommandEvent &event){
300 // Update the list of calendars before showing.
302 calendarList->SetPosition(wxPoint(calendarsButton->GetScreenRect().GetLeft(), calendarsButton->GetScreenRect().GetBottom()));
303 calendarList->UpdateCalendarList(dataStorage);
304 #if defined(__APPLE__)
306 if (calendarListShow == false)
308 calendarList->Show();
309 calendarListShow = true;
313 calendarList->Hide();
314 calendarListShow = false;
317 calendarList->Popup();
322 void XCCalendarManipulator::UpdateDateButtonText(){
324 // Update the date text.
326 string newDateText = "";
328 switch (moo->GetMonth()){
330 newDateText = _("January");
333 newDateText = _("February");
336 newDateText = _("March");
339 newDateText = _("April");
342 newDateText = _("May");
345 newDateText = _("June");
348 newDateText = _("July");
351 newDateText = _("August");
354 newDateText = _("September");
357 newDateText = _("October");
360 newDateText = _("November");
363 newDateText = _("December");
369 newDateText += to_string(year);
371 dateButton->SetLabel(newDateText);
376 int XCCalendarManipulator::GetMonth(){
382 int XCCalendarManipulator::GetYear(){
388 vector<int> XCCalendarManipulator::GetHiddenAccountsList(){
390 return calendarList->GetHiddenAccountsList();
394 vector<int> XCCalendarManipulator::GetHiddenCalendarsList(){
396 return calendarList->GetHiddenCalendarsList();
400 void XCCalendarManipulator::ButtonMouseover(wxMouseEvent &event)
402 wxWindow *eventObject = (wxWindow*)event.GetEventObject();
403 eventObject->SetWindowStyle(0 | wxSIMPLE_BORDER);
406 void XCCalendarManipulator::ButtonMouseout(wxMouseEvent &event)
408 wxWindow *eventObject = (wxWindow*)event.GetEventObject();
409 eventObject->SetWindowStyle(0 | wxNO_BORDER);