Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
widgets: Cleanup objects in destructors and minor code cleanup
[xestiacalendar/.git] / source / widgets / XCCalendarManipulator.cpp
1 // XCCalendarManipulator.cpp - Xestia Calendar XCCalendarManipulator widget class.
2 //
3 // (c) 2016 Xestia Software Development.
4 //
5 // This file is part of Xestia Calendar.
6 //
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.
10 //
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.
15 //
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);
23 using namespace std;
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));
32 #else
33         pnlMain->SetBackgroundColour(wxColour(40,40,40));
34 #endif
35         this->SetSizer(szrMain);
36         szrMain->Add(pnlMain, 0, wxEXPAND, 0);
37         
38         // Setup the navigation section.
39                 
40         szrNavigation = new wxBoxSizer( wxHORIZONTAL );
41         pnlMain->SetSizer(szrNavigation);
42                 
43         // Add next month and previous month buttons.
44                 
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);
48         
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));
52                 
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());
68                 
69         // Setup the static text.
70                 
71         dateButton = new wxButton(pnlMain, wxID_ANY, wxT(""), wxDefaultPosition, wxDefaultSize, 0|wxNO_BORDER);
72                 
73         wxFont test;
74         
75         test.SetWeight(wxFONTWEIGHT_BOLD);
76         test.SetPointSize(18);
77                 
78         dateButton->SetFont(test);
79         dateButton->SetForegroundColour(wxColour(255,255,255));
80         dateButton->SetBackgroundColour(pnlMain->GetBackgroundColour());
81         
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();
91                 
92         // Setup the month selection control.
93         
94         wxDateTime dtNow = wxDateTime::Now();
96 #if defined(WIN32) || defined(__APPLE__)
97         moo = new frmCalendarSelectMonth(this);
98 #else
99         moo = new XCCalendarMonthSelect(this);
100 #endif
101         month = ((int)dtNow.GetMonth() + 1);
102         year = dtNow.GetYear();
103         moo->UpdateDate(month, year);
104         UpdateDateButtonText();
105         
106         // Setup the calendars list.
107         
108         calendarList = new XCCalendarList(this);
109         
110         // Setup the calendar data storage pointer.
111         
112         this->dataStorage = dataStorage;
113                 
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);
120         
121 #if defined(WIN32)
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);
130 #endif
132         Bind(XCCALENDARMANIPULATOR_CHANGEGRID, &XCCalendarManipulator::ChangeGrid, this, ID_CHANGEGRID);
134         this->Refresh();
137 XCCalendarManipulator::~XCCalendarManipulator(){
138         
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);
145         
146 #if defined(WIN32)
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);
155 #endif
156         
157         Unbind(XCCALENDARMANIPULATOR_CHANGEGRID, &XCCalendarManipulator::ChangeGrid, this, ID_CHANGEGRID);
159         // Delete the buttons in pnlMain.
160         
161         szrNavigation->Clear();
162         
163         delete previousButton;
164         previousButton = nullptr;
165         
166         delete nextButton;
167         nextButton = nullptr;
168         
169         delete calendarsButton;
170         calendarsButton = nullptr;
171         
172         delete dateButton;
173         dateButton = nullptr;
174         
175         // Delete szrNavigation.
176         
177         szrMain->Clear();
178         pnlMain->SetSizer(nullptr, true);
179         
180         // Delete pnlMain itself;
181         
182         delete pnlMain;
183         pnlMain = nullptr;
185         delete moo;
186         moo = nullptr;
188         delete calendarList;
189         calendarList = nullptr;
190         
191         // Delete szrMain itself;
192         
193         this->SetSizer(nullptr, true);
195         dataStorage = nullptr;
196         
199 void XCCalendarManipulator::DateTextClick(wxCommandEvent &event){
200         
201         // Bring up a popup control to select the month and year.
202         
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__)
208         moo->ShowModal();
209 #else
210         moo->Popup();
211 #endif
215 void XCCalendarManipulator::ChangeGrid(wxCommandEvent &event){
216         
217         if (month == moo->GetMonth() && year == moo->GetYear()){
218                 return;
219         }
220         
221         month = moo->GetMonth();
222         year = moo->GetYear();
223         
224         // Post an event to the parent control to update the grid.
225         
226         wxCommandEvent changeGrid(XCCALENDARCTRL_CHANGEGRID);
227         changeGrid.SetId(ID_CHANGEGRID);
228         wxPostEvent(this->GetParent(), changeGrid);
229         
230         UpdateDateButtonText();
231         
234 void XCCalendarManipulator::NextMonth(wxCommandEvent &event){
235         
236         int newMonth = 1;
237         int newYear = 0;
238         
239         // Get the current month and year.
240         
241         newMonth = moo->GetMonth();
242         newYear = moo->GetYear();
244         if (newMonth == 12){
245                 newMonth = 1;
246                 newYear++;
247         } else {
248                 newMonth++;
249         }
250         
251         month = newMonth;
252         year = newYear;
254         moo->UpdateDate(month, year);
255         
256         // Post an event to the parent control to update the grid.
257         
258         wxCommandEvent changeGrid(XCCALENDARCTRL_CHANGEGRID);
259         changeGrid.SetId(ID_CHANGEGRID);
260         wxPostEvent(this->GetParent(), changeGrid);
262         UpdateDateButtonText();
263         
266 void XCCalendarManipulator::PreviousMonth(wxCommandEvent &event){
267         
268         int newMonth = 1;
269         int newYear = 0;
270         
271         // Get the current month and year.
272         
273         newMonth = moo->GetMonth();
274         newYear = moo->GetYear();
276         if (newMonth == 1){
277                 newMonth = 12;
278                 newYear--;
279         } else {
280                 newMonth--;
281         }
283         month = newMonth;
284         year = newYear;
285         
286         moo->UpdateDate(month, year);
287         
288         // Post an event to the parent control to update the grid.
289         
290         wxCommandEvent changeGrid(XCCALENDARCTRL_CHANGEGRID);
291         changeGrid.SetId(ID_CHANGEGRID);
292         wxPostEvent(this->GetParent(), changeGrid);
293         
294         UpdateDateButtonText();
295         
298 void XCCalendarManipulator::ShowCalendarsList(wxCommandEvent &event){
299         
300         // Update the list of calendars before showing.
301         
302         calendarList->SetPosition(wxPoint(calendarsButton->GetScreenRect().GetLeft(), calendarsButton->GetScreenRect().GetBottom()));
303         calendarList->UpdateCalendarList(dataStorage);
304 #if defined(__APPLE__)
306         if (calendarListShow == false)
307         {
308                 calendarList->Show();
309                 calendarListShow = true;
310         }
311         else
312         {
313                 calendarList->Hide();
314                 calendarListShow = false;
315         }
316 #else
317         calendarList->Popup();
318 #endif
319     
322 void XCCalendarManipulator::UpdateDateButtonText(){
323         
324         // Update the date text.
325         
326         string newDateText = "";
327         
328         switch (moo->GetMonth()){
329                 case 1:
330                         newDateText = _("January");
331                         break;
332                 case 2:
333                         newDateText = _("February");
334                         break;
335                 case 3:
336                         newDateText = _("March");
337                         break;
338                 case 4:
339                         newDateText = _("April");
340                         break;
341                 case 5:
342                         newDateText = _("May");
343                         break;
344                 case 6:
345                         newDateText = _("June");
346                         break;
347                 case 7:
348                         newDateText = _("July");
349                         break;
350                 case 8:
351                         newDateText = _("August");
352                         break;
353                 case 9:
354                         newDateText = _("September");
355                         break;
356                 case 10:
357                         newDateText = _("October");
358                         break;
359                 case 11:
360                         newDateText = _("November");
361                         break;
362                 case 12:
363                         newDateText = _("December");
364                         break;
365         }
366         
367         newDateText += " ";
368         
369         newDateText += to_string(year);
370         
371         dateButton->SetLabel(newDateText);
372         szrMain->Layout();
373         
376 int XCCalendarManipulator::GetMonth(){
377         
378         return month;
379         
382 int XCCalendarManipulator::GetYear(){
383         
384         return year;
385         
388 vector<int> XCCalendarManipulator::GetHiddenAccountsList(){
389         
390         return calendarList->GetHiddenAccountsList();
391         
394 vector<int> XCCalendarManipulator::GetHiddenCalendarsList(){
395         
396         return calendarList->GetHiddenCalendarsList();
397         
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);
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy