]> git.saurik.com Git - wxWidgets.git/blob - src/generic/calctrl.cpp
colourSize was probably supposed to be used and not just defined and then forgot...
[wxWidgets.git] / src / generic / calctrl.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: generic/calctrl.cpp
3 // Purpose: implementation fo the generic wxCalendarCtrl
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 29.12.99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "calctrl.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/dcclient.h"
33 #include "wx/settings.h"
34 #include "wx/brush.h"
35 #include "wx/combobox.h"
36 #include "wx/listbox.h"
37 #include "wx/stattext.h"
38 #include "wx/textctrl.h"
39 #endif //WX_PRECOMP
40
41 #if wxUSE_CALENDARCTRL
42
43 #include "wx/spinctrl.h"
44
45 // if wxDatePickerCtrl code doesn't define the date event, do it here as we
46 // need it as well
47 #if !wxUSE_DATEPICKCTRL
48 #define _WX_DEFINE_DATE_EVENTS_
49 #endif
50
51 #include "wx/calctrl.h"
52
53 #define DEBUG_PAINT 0
54
55 // ----------------------------------------------------------------------------
56 // wxWin macros
57 // ----------------------------------------------------------------------------
58
59 BEGIN_EVENT_TABLE(wxCalendarCtrl, wxControl)
60 EVT_PAINT(wxCalendarCtrl::OnPaint)
61
62 EVT_CHAR(wxCalendarCtrl::OnChar)
63
64 EVT_LEFT_DOWN(wxCalendarCtrl::OnClick)
65 EVT_LEFT_DCLICK(wxCalendarCtrl::OnDClick)
66 END_EVENT_TABLE()
67
68 #if wxUSE_EXTENDED_RTTI
69 WX_DEFINE_FLAGS( wxCalendarCtrlStyle )
70
71 wxBEGIN_FLAGS( wxCalendarCtrlStyle )
72 // new style border flags, we put them first to
73 // use them for streaming out
74 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
75 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
76 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
77 wxFLAGS_MEMBER(wxBORDER_RAISED)
78 wxFLAGS_MEMBER(wxBORDER_STATIC)
79 wxFLAGS_MEMBER(wxBORDER_NONE)
80
81 // old style border flags
82 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
83 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
84 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
85 wxFLAGS_MEMBER(wxRAISED_BORDER)
86 wxFLAGS_MEMBER(wxSTATIC_BORDER)
87 wxFLAGS_MEMBER(wxBORDER)
88
89 // standard window styles
90 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
91 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
92 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
93 wxFLAGS_MEMBER(wxWANTS_CHARS)
94 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
95 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
96 wxFLAGS_MEMBER(wxVSCROLL)
97 wxFLAGS_MEMBER(wxHSCROLL)
98
99 wxFLAGS_MEMBER(wxCAL_SUNDAY_FIRST)
100 wxFLAGS_MEMBER(wxCAL_MONDAY_FIRST)
101 wxFLAGS_MEMBER(wxCAL_SHOW_HOLIDAYS)
102 wxFLAGS_MEMBER(wxCAL_NO_YEAR_CHANGE)
103 wxFLAGS_MEMBER(wxCAL_NO_MONTH_CHANGE)
104 wxFLAGS_MEMBER(wxCAL_SEQUENTIAL_MONTH_SELECTION)
105 wxFLAGS_MEMBER(wxCAL_SHOW_SURROUNDING_WEEKS)
106
107 wxEND_FLAGS( wxCalendarCtrlStyle )
108
109 IMPLEMENT_DYNAMIC_CLASS_XTI(wxCalendarCtrl, wxControl,"wx/calctrl.h")
110
111 wxBEGIN_PROPERTIES_TABLE(wxCalendarCtrl)
112 wxEVENT_RANGE_PROPERTY( Updated , wxEVT_CALENDAR_SEL_CHANGED , wxEVT_CALENDAR_WEEKDAY_CLICKED , wxCalendarEvent )
113 wxHIDE_PROPERTY( Children )
114 wxPROPERTY( Date,wxDateTime, SetDate , GetDate, , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
115 wxPROPERTY_FLAGS( WindowStyle , wxCalendarCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
116 wxEND_PROPERTIES_TABLE()
117
118 wxBEGIN_HANDLERS_TABLE(wxCalendarCtrl)
119 wxEND_HANDLERS_TABLE()
120
121 wxCONSTRUCTOR_6( wxCalendarCtrl , wxWindow* , Parent , wxWindowID , Id , wxDateTime , Date , wxPoint , Position , wxSize , Size , long , WindowStyle )
122 #else
123 IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl, wxControl)
124 #endif
125 IMPLEMENT_DYNAMIC_CLASS(wxCalendarEvent, wxDateEvent)
126
127 // ----------------------------------------------------------------------------
128 // events
129 // ----------------------------------------------------------------------------
130
131 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_SEL_CHANGED)
132 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DAY_CHANGED)
133 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_MONTH_CHANGED)
134 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_YEAR_CHANGED)
135 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DOUBLECLICKED)
136 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_WEEKDAY_CLICKED)
137
138 // ============================================================================
139 // implementation
140 // ============================================================================
141
142 // ----------------------------------------------------------------------------
143 // wxCalendarCtrl
144 // ----------------------------------------------------------------------------
145
146 wxCalendarCtrl::wxCalendarCtrl(wxWindow *parent,
147 wxWindowID id,
148 const wxDateTime& date,
149 const wxPoint& pos,
150 const wxSize& size,
151 long style,
152 const wxString& name)
153 {
154 Init();
155
156 (void)Create(parent, id, date, pos, size, style, name);
157 }
158
159 void wxCalendarCtrl::Init()
160 {
161 m_comboMonth = NULL;
162 m_spinYear = NULL;
163 m_staticYear = NULL;
164 m_staticMonth = NULL;
165
166 m_userChangedYear = false;
167
168 m_widthCol =
169 m_heightRow = 0;
170
171 wxDateTime::WeekDay wd;
172 for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) )
173 {
174 m_weekdays[wd] = wxDateTime::GetWeekDayName(wd, wxDateTime::Name_Abbr);
175 }
176
177 for ( size_t n = 0; n < WXSIZEOF(m_attrs); n++ )
178 {
179 m_attrs[n] = NULL;
180 }
181
182 m_colHighlightFg = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
183 m_colHighlightBg = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
184
185 m_colHolidayFg = *wxRED;
186 // don't set m_colHolidayBg - by default, same as our bg colour
187
188 m_colHeaderFg = *wxBLUE;
189 m_colHeaderBg = *wxLIGHT_GREY;
190 }
191
192 bool wxCalendarCtrl::Create(wxWindow *parent,
193 wxWindowID id,
194 const wxDateTime& date,
195 const wxPoint& pos,
196 const wxSize& size,
197 long style,
198 const wxString& name)
199 {
200 if ( !wxControl::Create(parent, id, pos, size,
201 style | wxCLIP_CHILDREN | wxWANTS_CHARS,
202 wxDefaultValidator, name) )
203 {
204 return false;
205 }
206
207 // needed to get the arrow keys normally used for the dialog navigation
208 SetWindowStyle(style | wxWANTS_CHARS);
209
210 m_date = date.IsValid() ? date : wxDateTime::Today();
211
212 m_lowdate = wxDefaultDateTime;
213 m_highdate = wxDefaultDateTime;
214
215 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
216 {
217 CreateYearSpinCtrl();
218 m_staticYear = new wxStaticText(GetParent(), wxID_ANY, m_date.Format(_T("%Y")),
219 wxDefaultPosition, wxDefaultSize,
220 wxALIGN_CENTRE);
221
222 CreateMonthComboBox();
223 m_staticMonth = new wxStaticText(GetParent(), wxID_ANY, m_date.Format(_T("%B")),
224 wxDefaultPosition, wxDefaultSize,
225 wxALIGN_CENTRE);
226 }
227
228 ShowCurrentControls();
229
230 // we need to set the position as well because the main control position
231 // is not the same as the one specified in pos if we have the controls
232 // above it
233 SetBestSize(size);
234 SetPosition(pos);
235
236 // Since we don't paint the whole background make sure that the platform
237 // will use the right one.
238 SetBackgroundColour(GetBackgroundColour());
239
240 SetHolidayAttrs();
241
242 return true;
243 }
244
245 wxCalendarCtrl::~wxCalendarCtrl()
246 {
247 for ( size_t n = 0; n < WXSIZEOF(m_attrs); n++ )
248 {
249 delete m_attrs[n];
250 }
251 }
252
253 // ----------------------------------------------------------------------------
254 // Create the wxComboBox and wxSpinCtrl
255 // ----------------------------------------------------------------------------
256
257 void wxCalendarCtrl::CreateMonthComboBox()
258 {
259 m_comboMonth = new wxComboBox(GetParent(), wxID_ANY,
260 wxEmptyString,
261 wxDefaultPosition,
262 wxDefaultSize,
263 0, NULL,
264 wxCB_READONLY | wxCLIP_SIBLINGS);
265
266 wxDateTime::Month m;
267 for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
268 {
269 m_comboMonth->Append(wxDateTime::GetMonthName(m));
270 }
271
272 m_comboMonth->SetSelection(GetDate().GetMonth());
273 m_comboMonth->SetSize(wxDefaultCoord,
274 wxDefaultCoord,
275 wxDefaultCoord,
276 wxDefaultCoord,
277 wxSIZE_AUTO_WIDTH|wxSIZE_AUTO_HEIGHT);
278
279 m_comboMonth->Connect(wxEVT_COMMAND_COMBOBOX_SELECTED,
280 wxCommandEventHandler(wxCalendarCtrl::OnMonthChange),
281 NULL, this);
282 }
283
284 void wxCalendarCtrl::CreateYearSpinCtrl()
285 {
286 m_spinYear = new wxSpinCtrl(GetParent(), wxID_ANY,
287 GetDate().Format(_T("%Y")),
288 wxDefaultPosition,
289 wxDefaultSize,
290 wxSP_ARROW_KEYS | wxCLIP_SIBLINGS,
291 -4300, 10000, GetDate().GetYear());
292
293 m_spinYear->Connect(wxEVT_COMMAND_TEXT_UPDATED,
294 wxCommandEventHandler(wxCalendarCtrl::OnYearTextChange),
295 NULL, this);
296
297 m_spinYear->Connect(wxEVT_COMMAND_SPINCTRL_UPDATED,
298 wxCommandEventHandler(wxCalendarCtrl::OnYearChange),
299 NULL, this);
300 }
301
302 // ----------------------------------------------------------------------------
303 // forward wxWin functions to subcontrols
304 // ----------------------------------------------------------------------------
305
306 bool wxCalendarCtrl::Destroy()
307 {
308 if ( m_staticYear )
309 m_staticYear->Destroy();
310 if ( m_spinYear )
311 m_spinYear->Destroy();
312 if ( m_comboMonth )
313 m_comboMonth->Destroy();
314 if ( m_staticMonth )
315 m_staticMonth->Destroy();
316
317 m_staticYear = NULL;
318 m_spinYear = NULL;
319 m_comboMonth = NULL;
320 m_staticMonth = NULL;
321
322 return wxControl::Destroy();
323 }
324
325 bool wxCalendarCtrl::Show(bool show)
326 {
327 if ( !wxControl::Show(show) )
328 {
329 return false;
330 }
331
332 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) )
333 {
334 if ( GetMonthControl() )
335 {
336 GetMonthControl()->Show(show);
337 GetYearControl()->Show(show);
338 }
339 }
340
341 return true;
342 }
343
344 bool wxCalendarCtrl::Enable(bool enable)
345 {
346 if ( !wxControl::Enable(enable) )
347 {
348 return false;
349 }
350
351 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) )
352 {
353 GetMonthControl()->Enable(enable);
354 GetYearControl()->Enable(enable);
355 }
356
357 return true;
358 }
359
360 // ----------------------------------------------------------------------------
361 // enable/disable month/year controls
362 // ----------------------------------------------------------------------------
363
364 void wxCalendarCtrl::ShowCurrentControls()
365 {
366 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
367 {
368 if ( AllowMonthChange() )
369 {
370 m_comboMonth->Show();
371 m_staticMonth->Hide();
372
373 if ( AllowYearChange() )
374 {
375 m_spinYear->Show();
376 m_staticYear->Hide();
377
378 // skip the rest
379 return;
380 }
381 }
382 else
383 {
384 m_comboMonth->Hide();
385 m_staticMonth->Show();
386 }
387
388 // year change not allowed here
389 m_spinYear->Hide();
390 m_staticYear->Show();
391 }
392 }
393
394 wxControl *wxCalendarCtrl::GetMonthControl() const
395 {
396 return AllowMonthChange() ? (wxControl *)m_comboMonth : (wxControl *)m_staticMonth;
397 }
398
399 wxControl *wxCalendarCtrl::GetYearControl() const
400 {
401 return AllowYearChange() ? (wxControl *)m_spinYear : (wxControl *)m_staticYear;
402 }
403
404 void wxCalendarCtrl::EnableYearChange(bool enable)
405 {
406 if ( enable != AllowYearChange() )
407 {
408 long style = GetWindowStyle();
409 if ( enable )
410 style &= ~wxCAL_NO_YEAR_CHANGE;
411 else
412 style |= wxCAL_NO_YEAR_CHANGE;
413 SetWindowStyle(style);
414
415 ShowCurrentControls();
416 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION )
417 {
418 Refresh();
419 }
420 }
421 }
422
423 void wxCalendarCtrl::EnableMonthChange(bool enable)
424 {
425 if ( enable != AllowMonthChange() )
426 {
427 long style = GetWindowStyle();
428 if ( enable )
429 style &= ~wxCAL_NO_MONTH_CHANGE;
430 else
431 style |= wxCAL_NO_MONTH_CHANGE;
432 SetWindowStyle(style);
433
434 ShowCurrentControls();
435 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION )
436 {
437 Refresh();
438 }
439 }
440 }
441
442 // ----------------------------------------------------------------------------
443 // changing date
444 // ----------------------------------------------------------------------------
445
446 bool wxCalendarCtrl::SetDate(const wxDateTime& date)
447 {
448 bool retval = true;
449
450 bool sameMonth = m_date.GetMonth() == date.GetMonth(),
451 sameYear = m_date.GetYear() == date.GetYear();
452
453 if ( IsDateInRange(date) )
454 {
455 if ( sameMonth && sameYear )
456 {
457 // just change the day
458 ChangeDay(date);
459 }
460 else
461 {
462 if ( AllowMonthChange() && (AllowYearChange() || sameYear) )
463 {
464 // change everything
465 m_date = date;
466
467 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) )
468 {
469 // update the controls
470 m_comboMonth->SetSelection(m_date.GetMonth());
471
472 if ( AllowYearChange() )
473 {
474 if ( !m_userChangedYear )
475 m_spinYear->SetValue(m_date.Format(_T("%Y")));
476 }
477 }
478
479 // as the month changed, holidays did too
480 SetHolidayAttrs();
481
482 // update the calendar
483 Refresh();
484 }
485 else
486 {
487 // forbidden
488 retval = false;
489 }
490 }
491 }
492
493 m_userChangedYear = false;
494
495 return retval;
496 }
497
498 void wxCalendarCtrl::ChangeDay(const wxDateTime& date)
499 {
500 if ( m_date != date )
501 {
502 // we need to refresh the row containing the old date and the one
503 // containing the new one
504 wxDateTime dateOld = m_date;
505 m_date = date;
506
507 RefreshDate(dateOld);
508
509 // if the date is in the same row, it was already drawn correctly
510 if ( GetWeek(m_date) != GetWeek(dateOld) )
511 {
512 RefreshDate(m_date);
513 }
514 }
515 }
516
517 void wxCalendarCtrl::SetDateAndNotify(const wxDateTime& date)
518 {
519 wxDateTime::Tm tm1 = m_date.GetTm(),
520 tm2 = date.GetTm();
521
522 wxEventType type;
523 if ( tm1.year != tm2.year )
524 type = wxEVT_CALENDAR_YEAR_CHANGED;
525 else if ( tm1.mon != tm2.mon )
526 type = wxEVT_CALENDAR_MONTH_CHANGED;
527 else if ( tm1.mday != tm2.mday )
528 type = wxEVT_CALENDAR_DAY_CHANGED;
529 else
530 return;
531
532 if ( SetDate(date) )
533 {
534 GenerateEvents(type, wxEVT_CALENDAR_SEL_CHANGED);
535 }
536 }
537
538 // ----------------------------------------------------------------------------
539 // date range
540 // ----------------------------------------------------------------------------
541
542 bool wxCalendarCtrl::SetLowerDateLimit(const wxDateTime& date /* = wxDefaultDateTime */)
543 {
544 bool retval = true;
545
546 if ( !(date.IsValid()) || ( ( m_highdate.IsValid() ) ? ( date <= m_highdate ) : true ) )
547 {
548 m_lowdate = date;
549 }
550 else
551 {
552 retval = false;
553 }
554
555 return retval;
556 }
557
558 bool wxCalendarCtrl::SetUpperDateLimit(const wxDateTime& date /* = wxDefaultDateTime */)
559 {
560 bool retval = true;
561
562 if ( !(date.IsValid()) || ( ( m_lowdate.IsValid() ) ? ( date >= m_lowdate ) : true ) )
563 {
564 m_highdate = date;
565 }
566 else
567 {
568 retval = false;
569 }
570
571 return retval;
572 }
573
574 bool wxCalendarCtrl::SetDateRange(const wxDateTime& lowerdate /* = wxDefaultDateTime */, const wxDateTime& upperdate /* = wxDefaultDateTime */)
575 {
576 bool retval = true;
577
578 if (
579 ( !( lowerdate.IsValid() ) || ( ( upperdate.IsValid() ) ? ( lowerdate <= upperdate ) : true ) ) &&
580 ( !( upperdate.IsValid() ) || ( ( lowerdate.IsValid() ) ? ( upperdate >= lowerdate ) : true ) ) )
581 {
582 m_lowdate = lowerdate;
583 m_highdate = upperdate;
584 }
585 else
586 {
587 retval = false;
588 }
589
590 return retval;
591 }
592
593 // ----------------------------------------------------------------------------
594 // date helpers
595 // ----------------------------------------------------------------------------
596
597 wxDateTime wxCalendarCtrl::GetStartDate() const
598 {
599 wxDateTime::Tm tm = m_date.GetTm();
600
601 wxDateTime date = wxDateTime(1, tm.mon, tm.year);
602
603 // rewind back
604 date.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
605 ? wxDateTime::Mon : wxDateTime::Sun);
606
607 if ( GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS )
608 {
609 // We want to offset the calendar if we start on the first..
610 if ( date.GetDay() == 1 )
611 {
612 date -= wxDateSpan::Week();
613 }
614 }
615
616 return date;
617 }
618
619 bool wxCalendarCtrl::IsDateShown(const wxDateTime& date) const
620 {
621 if ( !(GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS) )
622 {
623 return date.GetMonth() == m_date.GetMonth();
624 }
625 else
626 {
627 return true;
628 }
629 }
630
631 bool wxCalendarCtrl::IsDateInRange(const wxDateTime& date) const
632 {
633 // Check if the given date is in the range specified
634 return ( ( ( m_lowdate.IsValid() ) ? ( date >= m_lowdate ) : true )
635 && ( ( m_highdate.IsValid() ) ? ( date <= m_highdate ) : true ) );
636 }
637
638 bool wxCalendarCtrl::ChangeYear(wxDateTime* target) const
639 {
640 bool retval = false;
641
642 if ( !(IsDateInRange(*target)) )
643 {
644 if ( target->GetYear() < m_date.GetYear() )
645 {
646 if ( target->GetYear() >= GetLowerDateLimit().GetYear() )
647 {
648 *target = GetLowerDateLimit();
649 retval = true;
650 }
651 else
652 {
653 *target = m_date;
654 }
655 }
656 else
657 {
658 if ( target->GetYear() <= GetUpperDateLimit().GetYear() )
659 {
660 *target = GetUpperDateLimit();
661 retval = true;
662 }
663 else
664 {
665 *target = m_date;
666 }
667 }
668 }
669 else
670 {
671 retval = true;
672 }
673
674 return retval;
675 }
676
677 bool wxCalendarCtrl::ChangeMonth(wxDateTime* target) const
678 {
679 bool retval = true;
680
681 if ( !(IsDateInRange(*target)) )
682 {
683 retval = false;
684
685 if ( target->GetMonth() < m_date.GetMonth() )
686 {
687 *target = GetLowerDateLimit();
688 }
689 else
690 {
691 *target = GetUpperDateLimit();
692 }
693 }
694
695 return retval;
696 }
697
698 size_t wxCalendarCtrl::GetWeek(const wxDateTime& date) const
699 {
700 size_t retval = date.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST
701 ? wxDateTime::Monday_First
702 : wxDateTime::Sunday_First);
703
704 if ( (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS) )
705 {
706 // we need to offset an extra week if we "start" on the 1st of the month
707 wxDateTime::Tm tm = date.GetTm();
708
709 wxDateTime datetest = wxDateTime(1, tm.mon, tm.year);
710
711 // rewind back
712 datetest.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
713 ? wxDateTime::Mon : wxDateTime::Sun);
714
715 if ( datetest.GetDay() == 1 )
716 {
717 retval += 1;
718 }
719 }
720
721 return retval;
722 }
723
724 // ----------------------------------------------------------------------------
725 // size management
726 // ----------------------------------------------------------------------------
727
728 // this is a composite control and it must arrange its parts each time its
729 // size or position changes: the combobox and spinctrl are along the top of
730 // the available area and the calendar takes up therest of the space
731
732 // the static controls are supposed to be always smaller than combo/spin so we
733 // always use the latter for size calculations and position the static to take
734 // the same space
735
736 // the constants used for the layout
737 #define VERT_MARGIN 5 // distance between combo and calendar
738 #ifdef __WXMAC__
739 #define HORZ_MARGIN 5 // spin
740 #else
741 #define HORZ_MARGIN 15 // spin
742 #endif
743 wxSize wxCalendarCtrl::DoGetBestSize() const
744 {
745 // calc the size of the calendar
746 ((wxCalendarCtrl *)this)->RecalcGeometry(); // const_cast
747
748 wxCoord width = 7*m_widthCol,
749 height = 7*m_heightRow + m_rowOffset + VERT_MARGIN;
750
751 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
752 {
753 // the combobox doesn't report its height correctly (it returns the
754 // height including the drop down list) so don't use it
755 height += m_spinYear->GetBestSize().y;
756
757
758 wxCoord w2 = m_comboMonth->GetBestSize().x + HORZ_MARGIN + GetCharWidth()*6;
759 if (width < w2)
760 width = w2;
761 }
762
763 if ( !HasFlag(wxBORDER_NONE) )
764 {
765 // the border would clip the last line otherwise
766 height += 6;
767 width += 4;
768 }
769
770 wxSize best(width, height);
771 CacheBestSize(best);
772 return best;
773 }
774
775 void wxCalendarCtrl::DoSetSize(int x, int y,
776 int width, int height,
777 int sizeFlags)
778 {
779 wxControl::DoSetSize(x, y, width, height, sizeFlags);
780 }
781
782 void wxCalendarCtrl::DoMoveWindow(int x, int y, int width, int height)
783 {
784 int yDiff;
785
786 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
787 {
788 wxSize sizeCombo = m_comboMonth->GetSize();
789 wxSize sizeStatic = m_staticMonth->GetSize();
790 wxSize sizeSpin = m_spinYear->GetSize();
791 int dy = (sizeCombo.y - sizeStatic.y) / 2;
792 m_comboMonth->Move(x, y);
793 m_staticMonth->SetSize(x, y + dy, sizeCombo.x, sizeStatic.y);
794
795 int xDiff = sizeCombo.x + HORZ_MARGIN;
796
797 m_spinYear->SetSize(x + xDiff, y, width - xDiff, sizeCombo.y);
798 m_staticYear->SetSize(x + xDiff, y + dy, width - xDiff, sizeStatic.y);
799
800 yDiff = wxMax(sizeSpin.y, sizeCombo.y) + VERT_MARGIN;
801 }
802 else // no controls on the top
803 {
804 yDiff = 0;
805 }
806
807 wxControl::DoMoveWindow(x, y + yDiff, width, height - yDiff);
808 }
809
810 void wxCalendarCtrl::DoGetPosition(int *x, int *y) const
811 {
812 wxControl::DoGetPosition(x, y);
813
814 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) )
815 {
816 // our real top corner is not in this position
817 if ( y )
818 {
819 *y -= GetMonthControl()->GetSize().y + VERT_MARGIN;
820 }
821 }
822 }
823
824 void wxCalendarCtrl::DoGetSize(int *width, int *height) const
825 {
826 wxControl::DoGetSize(width, height);
827
828 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) )
829 {
830 // our real height is bigger
831 if ( height && GetMonthControl())
832 {
833 *height += GetMonthControl()->GetSize().y + VERT_MARGIN;
834 }
835 }
836 }
837
838 void wxCalendarCtrl::RecalcGeometry()
839 {
840 wxClientDC dc(this);
841
842 dc.SetFont(GetFont());
843
844 // determine the column width (weekday names are not necessarily wider
845 // than the numbers (in some languages), so let's not assume that they are)
846 m_widthCol = 0;
847 for ( int day = 10; day <= 31; day++)
848 {
849 wxCoord width;
850 dc.GetTextExtent(wxString::Format(wxT("%d"), day), &width, &m_heightRow);
851 if ( width > m_widthCol )
852 {
853 // 1.5 times the width gives nice margins even if the weekday
854 // names are short
855 m_widthCol = width+width/2;
856 }
857 }
858 wxDateTime::WeekDay wd;
859 for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) )
860 {
861 wxCoord width;
862 dc.GetTextExtent(m_weekdays[wd], &width, &m_heightRow);
863 if ( width > m_widthCol )
864 {
865 m_widthCol = width;
866 }
867 }
868
869 // leave some margins
870 m_widthCol += 2;
871 m_heightRow += 2;
872
873 m_rowOffset = (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) ? m_heightRow : 0; // conditional in relation to style
874 }
875
876 // ----------------------------------------------------------------------------
877 // drawing
878 // ----------------------------------------------------------------------------
879
880 void wxCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
881 {
882 wxPaintDC dc(this);
883
884 dc.SetFont(GetFont());
885
886 RecalcGeometry();
887
888 #if DEBUG_PAINT
889 wxLogDebug("--- starting to paint, selection: %s, week %u\n",
890 m_date.Format("%a %d-%m-%Y %H:%M:%S").c_str(),
891 GetWeek(m_date));
892 #endif
893
894 wxCoord y = 0;
895 wxCoord x0 = (GetSize().x - m_widthCol*7) /2;
896 if (x0 < 0) x0 = 0;
897
898 if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
899 {
900 // draw the sequential month-selector
901
902 dc.SetBackgroundMode(wxTRANSPARENT);
903 dc.SetTextForeground(*wxBLACK);
904 dc.SetBrush(wxBrush(m_colHeaderBg, wxSOLID));
905 dc.SetPen(wxPen(m_colHeaderBg, 1, wxSOLID));
906 dc.DrawRectangle(0, y, GetClientSize().x, m_heightRow);
907
908 // Get extent of month-name + year
909 wxCoord monthw, monthh;
910 wxString headertext = m_date.Format(wxT("%B %Y"));
911 dc.GetTextExtent(headertext, &monthw, &monthh);
912
913 // draw month-name centered above weekdays
914 wxCoord monthx = ((m_widthCol * 7) - monthw) / 2 + x0;
915 wxCoord monthy = ((m_heightRow - monthh) / 2) + y;
916 dc.DrawText(headertext, monthx, monthy);
917
918 // calculate the "month-arrows"
919 wxPoint leftarrow[3];
920 wxPoint rightarrow[3];
921
922 int arrowheight = monthh / 2;
923
924 leftarrow[0] = wxPoint(0, arrowheight / 2);
925 leftarrow[1] = wxPoint(arrowheight / 2, 0);
926 leftarrow[2] = wxPoint(arrowheight / 2, arrowheight - 1);
927
928 rightarrow[0] = wxPoint(0,0);
929 rightarrow[1] = wxPoint(arrowheight / 2, arrowheight / 2);
930 rightarrow[2] = wxPoint(0, arrowheight - 1);
931
932 // draw the "month-arrows"
933
934 wxCoord arrowy = (m_heightRow - arrowheight) / 2;
935 wxCoord larrowx = (m_widthCol - (arrowheight / 2)) / 2 + x0;
936 wxCoord rarrowx = ((m_widthCol - (arrowheight / 2)) / 2) + m_widthCol*6 + x0;
937 m_leftArrowRect = m_rightArrowRect = wxRect(0,0,0,0);
938
939 if ( AllowMonthChange() )
940 {
941 wxDateTime ldpm = wxDateTime(1,m_date.GetMonth(), m_date.GetYear()) - wxDateSpan::Day(); // last day prev month
942 // Check if range permits change
943 if ( IsDateInRange(ldpm) && ( ( ldpm.GetYear() == m_date.GetYear() ) ? true : AllowYearChange() ) )
944 {
945 m_leftArrowRect = wxRect(larrowx - 3, arrowy - 3, (arrowheight / 2) + 8, (arrowheight + 6));
946 dc.SetBrush(wxBrush(*wxBLACK, wxSOLID));
947 dc.SetPen(wxPen(*wxBLACK, 1, wxSOLID));
948 dc.DrawPolygon(3, leftarrow, larrowx , arrowy, wxWINDING_RULE);
949 dc.SetBrush(*wxTRANSPARENT_BRUSH);
950 dc.DrawRectangle(m_leftArrowRect);
951 }
952 wxDateTime fdnm = wxDateTime(1,m_date.GetMonth(), m_date.GetYear()) + wxDateSpan::Month(); // first day next month
953 if ( IsDateInRange(fdnm) && ( ( fdnm.GetYear() == m_date.GetYear() ) ? true : AllowYearChange() ) )
954 {
955 m_rightArrowRect = wxRect(rarrowx - 4, arrowy - 3, (arrowheight / 2) + 8, (arrowheight + 6));
956 dc.SetBrush(wxBrush(*wxBLACK, wxSOLID));
957 dc.SetPen(wxPen(*wxBLACK, 1, wxSOLID));
958 dc.DrawPolygon(3, rightarrow, rarrowx , arrowy, wxWINDING_RULE);
959 dc.SetBrush(*wxTRANSPARENT_BRUSH);
960 dc.DrawRectangle(m_rightArrowRect);
961 }
962 }
963
964 y += m_heightRow;
965 }
966
967 // first draw the week days
968 if ( IsExposed(x0, y, 7*m_widthCol, m_heightRow) )
969 {
970 #if DEBUG_PAINT
971 wxLogDebug("painting the header");
972 #endif
973
974 dc.SetBackgroundMode(wxTRANSPARENT);
975 dc.SetTextForeground(m_colHeaderFg);
976 dc.SetBrush(wxBrush(m_colHeaderBg, wxSOLID));
977 dc.SetPen(wxPen(m_colHeaderBg, 1, wxSOLID));
978 dc.DrawRectangle(0, y, GetClientSize().x, m_heightRow);
979
980 bool startOnMonday = (GetWindowStyle() & wxCAL_MONDAY_FIRST) != 0;
981 for ( size_t wd = 0; wd < 7; wd++ )
982 {
983 size_t n;
984 if ( startOnMonday )
985 n = wd == 6 ? 0 : wd + 1;
986 else
987 n = wd;
988 wxCoord dayw, dayh;
989 dc.GetTextExtent(m_weekdays[n], &dayw, &dayh);
990 dc.DrawText(m_weekdays[n], x0 + (wd*m_widthCol) + ((m_widthCol- dayw) / 2), y); // center the day-name
991 }
992 }
993
994 // then the calendar itself
995 dc.SetTextForeground(*wxBLACK);
996 //dc.SetFont(*wxNORMAL_FONT);
997
998 y += m_heightRow;
999 wxDateTime date = GetStartDate();
1000
1001 #if DEBUG_PAINT
1002 wxLogDebug("starting calendar from %s\n",
1003 date.Format("%a %d-%m-%Y %H:%M:%S").c_str());
1004 #endif
1005
1006 dc.SetBackgroundMode(wxSOLID);
1007 for ( size_t nWeek = 1; nWeek <= 6; nWeek++, y += m_heightRow )
1008 {
1009 // if the update region doesn't intersect this row, don't paint it
1010 if ( !IsExposed(0, y, 7*m_widthCol, m_heightRow - 1) )
1011 {
1012 date += wxDateSpan::Week();
1013
1014 continue;
1015 }
1016
1017 #if DEBUG_PAINT
1018 wxLogDebug("painting week %d at y = %d\n", nWeek, y);
1019 #endif
1020
1021 for ( size_t wd = 0; wd < 7; wd++ )
1022 {
1023 if ( IsDateShown(date) )
1024 {
1025 // don't use wxDate::Format() which prepends 0s
1026 unsigned int day = date.GetDay();
1027 wxString dayStr = wxString::Format(_T("%u"), day);
1028 wxCoord width;
1029 dc.GetTextExtent(dayStr, &width, (wxCoord *)NULL);
1030
1031 bool changedColours = false,
1032 changedFont = false;
1033
1034 bool isSel = false;
1035 wxCalendarDateAttr *attr = NULL;
1036
1037 if ( date.GetMonth() != m_date.GetMonth() || !IsDateInRange(date) )
1038 {
1039 // surrounding week or out-of-range
1040 // draw "disabled"
1041 dc.SetTextForeground(*wxLIGHT_GREY);
1042 changedColours = true;
1043 }
1044 else
1045 {
1046 isSel = date.IsSameDate(m_date);
1047 attr = m_attrs[day - 1];
1048
1049 if ( isSel )
1050 {
1051 dc.SetTextForeground(m_colHighlightFg);
1052 dc.SetTextBackground(m_colHighlightBg);
1053
1054 changedColours = true;
1055 }
1056 else if ( attr )
1057 {
1058 wxColour colFg, colBg;
1059
1060 if ( attr->IsHoliday() )
1061 {
1062 colFg = m_colHolidayFg;
1063 colBg = m_colHolidayBg;
1064 }
1065 else
1066 {
1067 colFg = attr->GetTextColour();
1068 colBg = attr->GetBackgroundColour();
1069 }
1070
1071 if ( colFg.Ok() )
1072 {
1073 dc.SetTextForeground(colFg);
1074 changedColours = true;
1075 }
1076
1077 if ( colBg.Ok() )
1078 {
1079 dc.SetTextBackground(colBg);
1080 changedColours = true;
1081 }
1082
1083 if ( attr->HasFont() )
1084 {
1085 dc.SetFont(attr->GetFont());
1086 changedFont = true;
1087 }
1088 }
1089 }
1090
1091 wxCoord x = wd*m_widthCol + (m_widthCol - width) / 2 + x0;
1092 dc.DrawText(dayStr, x, y + 1);
1093
1094 if ( !isSel && attr && attr->HasBorder() )
1095 {
1096 wxColour colBorder;
1097 if ( attr->HasBorderColour() )
1098 {
1099 colBorder = attr->GetBorderColour();
1100 }
1101 else
1102 {
1103 colBorder = GetForegroundColour();
1104 }
1105
1106 wxPen pen(colBorder, 1, wxSOLID);
1107 dc.SetPen(pen);
1108 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1109
1110 switch ( attr->GetBorder() )
1111 {
1112 case wxCAL_BORDER_SQUARE:
1113 dc.DrawRectangle(x - 2, y,
1114 width + 4, m_heightRow);
1115 break;
1116
1117 case wxCAL_BORDER_ROUND:
1118 dc.DrawEllipse(x - 2, y,
1119 width + 4, m_heightRow);
1120 break;
1121
1122 default:
1123 wxFAIL_MSG(_T("unknown border type"));
1124 }
1125 }
1126
1127 if ( changedColours )
1128 {
1129 dc.SetTextForeground(GetForegroundColour());
1130 dc.SetTextBackground(GetBackgroundColour());
1131 }
1132
1133 if ( changedFont )
1134 {
1135 dc.SetFont(GetFont());
1136 }
1137 }
1138 //else: just don't draw it
1139
1140 date += wxDateSpan::Day();
1141 }
1142 }
1143
1144 // Greying out out-of-range background
1145 bool showSurrounding = (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS) != 0;
1146
1147 date = ( showSurrounding ) ? GetStartDate() : wxDateTime(1, m_date.GetMonth(), m_date.GetYear());
1148 if ( !IsDateInRange(date) )
1149 {
1150 wxDateTime firstOOR = GetLowerDateLimit() - wxDateSpan::Day(); // first out-of-range
1151
1152 wxBrush oorbrush = *wxLIGHT_GREY_BRUSH;
1153 oorbrush.SetStyle(wxFDIAGONAL_HATCH);
1154
1155 HighlightRange(&dc, date, firstOOR, wxTRANSPARENT_PEN, &oorbrush);
1156 }
1157
1158 date = ( showSurrounding ) ? GetStartDate() + wxDateSpan::Weeks(6) - wxDateSpan::Day() : wxDateTime().SetToLastMonthDay(m_date.GetMonth(), m_date.GetYear());
1159 if ( !IsDateInRange(date) )
1160 {
1161 wxDateTime firstOOR = GetUpperDateLimit() + wxDateSpan::Day(); // first out-of-range
1162
1163 wxBrush oorbrush = *wxLIGHT_GREY_BRUSH;
1164 oorbrush.SetStyle(wxFDIAGONAL_HATCH);
1165
1166 HighlightRange(&dc, firstOOR, date, wxTRANSPARENT_PEN, &oorbrush);
1167 }
1168
1169 #if DEBUG_PAINT
1170 wxLogDebug("+++ finished painting");
1171 #endif
1172 }
1173
1174 void wxCalendarCtrl::RefreshDate(const wxDateTime& date)
1175 {
1176 RecalcGeometry();
1177
1178 wxRect rect;
1179
1180 // always refresh the whole row at once because our OnPaint() will draw
1181 // the whole row anyhow - and this allows the small optimisation in
1182 // OnClick() below to work
1183 rect.x = 0;
1184
1185 rect.y = (m_heightRow * GetWeek(date)) + m_rowOffset;
1186
1187 rect.width = 7*m_widthCol;
1188 rect.height = m_heightRow;
1189
1190 #ifdef __WXMSW__
1191 // VZ: for some reason, the selected date seems to occupy more space under
1192 // MSW - this is probably some bug in the font size calculations, but I
1193 // don't know where exactly. This fix is ugly and leads to more
1194 // refreshes than really needed, but without it the selected days
1195 // leaves even more ugly underscores on screen.
1196 rect.Inflate(0, 1);
1197 #endif // MSW
1198
1199 #if DEBUG_PAINT
1200 wxLogDebug("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
1201 GetWeek(date),
1202 rect.x, rect.y,
1203 rect.x + rect.width, rect.y + rect.height);
1204 #endif
1205
1206 Refresh(true, &rect);
1207 }
1208
1209 void wxCalendarCtrl::HighlightRange(wxPaintDC* pDC, const wxDateTime& fromdate, const wxDateTime& todate, wxPen* pPen, wxBrush* pBrush)
1210 {
1211 // Highlights the given range using pen and brush
1212 // Does nothing if todate < fromdate
1213
1214
1215 #if DEBUG_PAINT
1216 wxLogDebug("+++ HighlightRange: (%s) - (%s) +++", fromdate.Format("%d %m %Y"), todate.Format("%d %m %Y"));
1217 #endif
1218
1219 if ( todate >= fromdate )
1220 {
1221 // do stuff
1222 // date-coordinates
1223 int fd, fw;
1224 int td, tw;
1225
1226 // implicit: both dates must be currently shown - checked by GetDateCoord
1227 if ( GetDateCoord(fromdate, &fd, &fw) && GetDateCoord(todate, &td, &tw) )
1228 {
1229 #if DEBUG_PAINT
1230 wxLogDebug("Highlight range: (%i, %i) - (%i, %i)", fd, fw, td, tw);
1231 #endif
1232 if ( ( (tw - fw) == 1 ) && ( td < fd ) )
1233 {
1234 // special case: interval 7 days or less not in same week
1235 // split in two separate intervals
1236 wxDateTime tfd = fromdate + wxDateSpan::Days(7-fd);
1237 wxDateTime ftd = tfd + wxDateSpan::Day();
1238 #if DEBUG_PAINT
1239 wxLogDebug("Highlight: Separate segments");
1240 #endif
1241 // draw separately
1242 HighlightRange(pDC, fromdate, tfd, pPen, pBrush);
1243 HighlightRange(pDC, ftd, todate, pPen, pBrush);
1244 }
1245 else
1246 {
1247 int numpoints;
1248 wxPoint corners[8]; // potentially 8 corners in polygon
1249
1250 if ( fw == tw )
1251 {
1252 // simple case: same week
1253 numpoints = 4;
1254 corners[0] = wxPoint((fd - 1) * m_widthCol, (fw * m_heightRow) + m_rowOffset);
1255 corners[1] = wxPoint((fd - 1) * m_widthCol, ((fw + 1 ) * m_heightRow) + m_rowOffset);
1256 corners[2] = wxPoint(td * m_widthCol, ((tw + 1) * m_heightRow) + m_rowOffset);
1257 corners[3] = wxPoint(td * m_widthCol, (tw * m_heightRow) + m_rowOffset);
1258 }
1259 else
1260 {
1261 int cidx = 0;
1262 // "complex" polygon
1263 corners[cidx] = wxPoint((fd - 1) * m_widthCol, (fw * m_heightRow) + m_rowOffset); cidx++;
1264
1265 if ( fd > 1 )
1266 {
1267 corners[cidx] = wxPoint((fd - 1) * m_widthCol, ((fw + 1) * m_heightRow) + m_rowOffset); cidx++;
1268 corners[cidx] = wxPoint(0, ((fw + 1) * m_heightRow) + m_rowOffset); cidx++;
1269 }
1270
1271 corners[cidx] = wxPoint(0, ((tw + 1) * m_heightRow) + m_rowOffset); cidx++;
1272 corners[cidx] = wxPoint(td * m_widthCol, ((tw + 1) * m_heightRow) + m_rowOffset); cidx++;
1273
1274 if ( td < 7 )
1275 {
1276 corners[cidx] = wxPoint(td * m_widthCol, (tw * m_heightRow) + m_rowOffset); cidx++;
1277 corners[cidx] = wxPoint(7 * m_widthCol, (tw * m_heightRow) + m_rowOffset); cidx++;
1278 }
1279
1280 corners[cidx] = wxPoint(7 * m_widthCol, (fw * m_heightRow) + m_rowOffset); cidx++;
1281
1282 numpoints = cidx;
1283 }
1284
1285 // draw the polygon
1286 pDC->SetBrush(*pBrush);
1287 pDC->SetPen(*pPen);
1288 pDC->DrawPolygon(numpoints, corners);
1289 }
1290 }
1291 }
1292 // else do nothing
1293 #if DEBUG_PAINT
1294 wxLogDebug("--- HighlightRange ---");
1295 #endif
1296 }
1297
1298 bool wxCalendarCtrl::GetDateCoord(const wxDateTime& date, int *day, int *week) const
1299 {
1300 bool retval = true;
1301
1302 #if DEBUG_PAINT
1303 wxLogDebug("+++ GetDateCoord: (%s) +++", date.Format("%d %m %Y"));
1304 #endif
1305
1306 if ( IsDateShown(date) )
1307 {
1308 bool startOnMonday = ( GetWindowStyle() & wxCAL_MONDAY_FIRST ) != 0;
1309
1310 // Find day
1311 *day = date.GetWeekDay();
1312
1313 if ( *day == 0 ) // sunday
1314 {
1315 *day = ( startOnMonday ) ? 7 : 1;
1316 }
1317 else
1318 {
1319 *day += ( startOnMonday ) ? 0 : 1;
1320 }
1321
1322 int targetmonth = date.GetMonth() + (12 * date.GetYear());
1323 int thismonth = m_date.GetMonth() + (12 * m_date.GetYear());
1324
1325 // Find week
1326 if ( targetmonth == thismonth )
1327 {
1328 *week = GetWeek(date);
1329 }
1330 else
1331 {
1332 if ( targetmonth < thismonth )
1333 {
1334 *week = 1; // trivial
1335 }
1336 else // targetmonth > thismonth
1337 {
1338 wxDateTime ldcm;
1339 int lastweek;
1340 int lastday;
1341
1342 // get the datecoord of the last day in the month currently shown
1343 #if DEBUG_PAINT
1344 wxLogDebug(" +++ LDOM +++");
1345 #endif
1346 GetDateCoord(ldcm.SetToLastMonthDay(m_date.GetMonth(), m_date.GetYear()), &lastday, &lastweek);
1347 #if DEBUG_PAINT
1348 wxLogDebug(" --- LDOM ---");
1349 #endif
1350
1351 wxTimeSpan span = date - ldcm;
1352
1353 int daysfromlast = span.GetDays();
1354 #if DEBUG_PAINT
1355 wxLogDebug("daysfromlast: %i", daysfromlast);
1356 #endif
1357 if ( daysfromlast + lastday > 7 ) // past week boundary
1358 {
1359 int wholeweeks = (daysfromlast / 7);
1360 *week = wholeweeks + lastweek;
1361 if ( (daysfromlast - (7 * wholeweeks) + lastday) > 7 )
1362 {
1363 *week += 1;
1364 }
1365 }
1366 else
1367 {
1368 *week = lastweek;
1369 }
1370 }
1371 }
1372 }
1373 else
1374 {
1375 *day = -1;
1376 *week = -1;
1377 retval = false;
1378 }
1379
1380 #if DEBUG_PAINT
1381 wxLogDebug("--- GetDateCoord: (%s) = (%i, %i) ---", date.Format("%d %m %Y"), *day, *week);
1382 #endif
1383
1384 return retval;
1385 }
1386
1387 // ----------------------------------------------------------------------------
1388 // mouse handling
1389 // ----------------------------------------------------------------------------
1390
1391 void wxCalendarCtrl::OnDClick(wxMouseEvent& event)
1392 {
1393 if ( HitTest(event.GetPosition()) != wxCAL_HITTEST_DAY )
1394 {
1395 event.Skip();
1396 }
1397 else
1398 {
1399 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED);
1400 }
1401 }
1402
1403 void wxCalendarCtrl::OnClick(wxMouseEvent& event)
1404 {
1405 wxDateTime date;
1406 wxDateTime::WeekDay wday;
1407 switch ( HitTest(event.GetPosition(), &date, &wday) )
1408 {
1409 case wxCAL_HITTEST_DAY:
1410 if ( IsDateInRange(date) )
1411 {
1412 ChangeDay(date);
1413
1414 GenerateEvents(wxEVT_CALENDAR_DAY_CHANGED,
1415 wxEVT_CALENDAR_SEL_CHANGED);
1416 }
1417 break;
1418
1419 case wxCAL_HITTEST_HEADER:
1420 {
1421 wxCalendarEvent event(this, wxEVT_CALENDAR_WEEKDAY_CLICKED);
1422 event.m_wday = wday;
1423 (void)GetEventHandler()->ProcessEvent(event);
1424 }
1425 break;
1426
1427 case wxCAL_HITTEST_DECMONTH:
1428 case wxCAL_HITTEST_INCMONTH:
1429 case wxCAL_HITTEST_SURROUNDING_WEEK:
1430 SetDateAndNotify(date); // we probably only want to refresh the control. No notification.. (maybe as an option?)
1431 break;
1432
1433 default:
1434 wxFAIL_MSG(_T("unknown hittest code"));
1435 // fall through
1436
1437 case wxCAL_HITTEST_NOWHERE:
1438 event.Skip();
1439 break;
1440 }
1441 }
1442
1443 wxCalendarHitTestResult wxCalendarCtrl::HitTest(const wxPoint& pos,
1444 wxDateTime *date,
1445 wxDateTime::WeekDay *wd)
1446 {
1447 RecalcGeometry();
1448
1449 wxCoord y = pos.y;
1450
1451 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1452 if ( (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) )
1453 {
1454 // Header: month
1455
1456 // we need to find out if the hit is on left arrow, on month or on right arrow
1457 // left arrow?
1458 if ( wxRegion(m_leftArrowRect).Contains(pos) == wxInRegion )
1459 {
1460 if ( date )
1461 {
1462 if ( IsDateInRange(m_date - wxDateSpan::Month()) )
1463 {
1464 *date = m_date - wxDateSpan::Month();
1465 }
1466 else
1467 {
1468 *date = GetLowerDateLimit();
1469 }
1470 }
1471
1472 return wxCAL_HITTEST_DECMONTH;
1473 }
1474
1475 if ( wxRegion(m_rightArrowRect).Contains(pos) == wxInRegion )
1476 {
1477 if ( date )
1478 {
1479 if ( IsDateInRange(m_date + wxDateSpan::Month()) )
1480 {
1481 *date = m_date + wxDateSpan::Month();
1482 }
1483 else
1484 {
1485 *date = GetUpperDateLimit();
1486 }
1487 }
1488
1489 return wxCAL_HITTEST_INCMONTH;
1490 }
1491
1492 }
1493
1494 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1495 // Header: Days
1496 int wday = pos.x / m_widthCol;
1497 // if ( y < m_heightRow )
1498 if ( y < (m_heightRow + m_rowOffset) )
1499 {
1500 if ( y > m_rowOffset )
1501 {
1502 if ( wd )
1503 {
1504 if ( GetWindowStyle() & wxCAL_MONDAY_FIRST )
1505 {
1506 wday = wday == 6 ? 0 : wday + 1;
1507 }
1508
1509 *wd = (wxDateTime::WeekDay)wday;
1510 }
1511
1512 return wxCAL_HITTEST_HEADER;
1513 }
1514 else
1515 {
1516 return wxCAL_HITTEST_NOWHERE;
1517 }
1518 }
1519
1520 // int week = (y - m_heightRow) / m_heightRow;
1521 int week = (y - (m_heightRow + m_rowOffset)) / m_heightRow;
1522 if ( week >= 6 || wday >= 7 )
1523 {
1524 return wxCAL_HITTEST_NOWHERE;
1525 }
1526
1527 wxDateTime dt = GetStartDate() + wxDateSpan::Days(7*week + wday);
1528
1529 if ( IsDateShown(dt) )
1530 {
1531 if ( date )
1532 *date = dt;
1533
1534 if ( dt.GetMonth() == m_date.GetMonth() )
1535 {
1536
1537 return wxCAL_HITTEST_DAY;
1538 }
1539 else
1540 {
1541 return wxCAL_HITTEST_SURROUNDING_WEEK;
1542 }
1543 }
1544 else
1545 {
1546 return wxCAL_HITTEST_NOWHERE;
1547 }
1548 }
1549
1550 // ----------------------------------------------------------------------------
1551 // subcontrols events handling
1552 // ----------------------------------------------------------------------------
1553
1554 void wxCalendarCtrl::OnMonthChange(wxCommandEvent& event)
1555 {
1556 wxDateTime::Tm tm = m_date.GetTm();
1557
1558 wxDateTime::Month mon = (wxDateTime::Month)event.GetInt();
1559 if ( tm.mday > wxDateTime::GetNumberOfDays(mon, tm.year) )
1560 {
1561 tm.mday = wxDateTime::GetNumberOfDays(mon, tm.year);
1562 }
1563
1564 wxDateTime target = wxDateTime(tm.mday, mon, tm.year);
1565
1566 ChangeMonth(&target);
1567 SetDateAndNotify(target);
1568 }
1569
1570 void wxCalendarCtrl::OnYearChange(wxCommandEvent& event)
1571 {
1572 int year = (int)event.GetInt();
1573 if ( year == INT_MIN )
1574 {
1575 // invalid year in the spin control, ignore it
1576 return;
1577 }
1578
1579 wxDateTime::Tm tm = m_date.GetTm();
1580
1581 if ( tm.mday > wxDateTime::GetNumberOfDays(tm.mon, year) )
1582 {
1583 tm.mday = wxDateTime::GetNumberOfDays(tm.mon, year);
1584 }
1585
1586 wxDateTime target = wxDateTime(tm.mday, tm.mon, year);
1587
1588 if ( ChangeYear(&target) )
1589 {
1590 SetDateAndNotify(target);
1591 }
1592 else
1593 {
1594 // In this case we don't want to change the date. That would put us
1595 // inside the same year but a strange number of months forward/back..
1596 m_spinYear->SetValue(target.GetYear());
1597 }
1598 }
1599
1600 void wxCalendarCtrl::OnYearTextChange(wxCommandEvent& event)
1601 {
1602 SetUserChangedYear();
1603 OnYearChange(event);
1604 }
1605
1606 // ----------------------------------------------------------------------------
1607 // keyboard interface
1608 // ----------------------------------------------------------------------------
1609
1610 void wxCalendarCtrl::OnChar(wxKeyEvent& event)
1611 {
1612 wxDateTime target;
1613 switch ( event.GetKeyCode() )
1614 {
1615 case _T('+'):
1616 case WXK_ADD:
1617 target = m_date + wxDateSpan::Year();
1618 if ( ChangeYear(&target) )
1619 {
1620 SetDateAndNotify(target);
1621 }
1622 break;
1623
1624 case _T('-'):
1625 case WXK_SUBTRACT:
1626 target = m_date - wxDateSpan::Year();
1627 if ( ChangeYear(&target) )
1628 {
1629 SetDateAndNotify(target);
1630 }
1631 break;
1632
1633 case WXK_PRIOR:
1634 target = m_date - wxDateSpan::Month();
1635 ChangeMonth(&target);
1636 SetDateAndNotify(target); // always
1637 break;
1638
1639 case WXK_NEXT:
1640 target = m_date + wxDateSpan::Month();
1641 ChangeMonth(&target);
1642 SetDateAndNotify(target); // always
1643 break;
1644
1645 case WXK_RIGHT:
1646 if ( event.ControlDown() )
1647 {
1648 target = wxDateTime(m_date).SetToNextWeekDay(
1649 GetWindowStyle() & wxCAL_MONDAY_FIRST
1650 ? wxDateTime::Sun : wxDateTime::Sat);
1651 if ( !IsDateInRange(target) )
1652 {
1653 target = GetUpperDateLimit();
1654 }
1655 SetDateAndNotify(target);
1656 }
1657 else
1658 SetDateAndNotify(m_date + wxDateSpan::Day());
1659 break;
1660
1661 case WXK_LEFT:
1662 if ( event.ControlDown() )
1663 {
1664 target = wxDateTime(m_date).SetToPrevWeekDay(
1665 GetWindowStyle() & wxCAL_MONDAY_FIRST
1666 ? wxDateTime::Mon : wxDateTime::Sun);
1667 if ( !IsDateInRange(target) )
1668 {
1669 target = GetLowerDateLimit();
1670 }
1671 SetDateAndNotify(target);
1672 }
1673 else
1674 SetDateAndNotify(m_date - wxDateSpan::Day());
1675 break;
1676
1677 case WXK_UP:
1678 SetDateAndNotify(m_date - wxDateSpan::Week());
1679 break;
1680
1681 case WXK_DOWN:
1682 SetDateAndNotify(m_date + wxDateSpan::Week());
1683 break;
1684
1685 case WXK_HOME:
1686 if ( event.ControlDown() )
1687 SetDateAndNotify(wxDateTime::Today());
1688 else
1689 SetDateAndNotify(wxDateTime(1, m_date.GetMonth(), m_date.GetYear()));
1690 break;
1691
1692 case WXK_END:
1693 SetDateAndNotify(wxDateTime(m_date).SetToLastMonthDay());
1694 break;
1695
1696 case WXK_RETURN:
1697 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED);
1698 break;
1699
1700 default:
1701 event.Skip();
1702 }
1703 }
1704
1705 // ----------------------------------------------------------------------------
1706 // holidays handling
1707 // ----------------------------------------------------------------------------
1708
1709 void wxCalendarCtrl::EnableHolidayDisplay(bool display)
1710 {
1711 long style = GetWindowStyle();
1712 if ( display )
1713 style |= wxCAL_SHOW_HOLIDAYS;
1714 else
1715 style &= ~wxCAL_SHOW_HOLIDAYS;
1716
1717 SetWindowStyle(style);
1718
1719 if ( display )
1720 SetHolidayAttrs();
1721 else
1722 ResetHolidayAttrs();
1723
1724 Refresh();
1725 }
1726
1727 void wxCalendarCtrl::SetHolidayAttrs()
1728 {
1729 if ( GetWindowStyle() & wxCAL_SHOW_HOLIDAYS )
1730 {
1731 ResetHolidayAttrs();
1732
1733 wxDateTime::Tm tm = m_date.GetTm();
1734 wxDateTime dtStart(1, tm.mon, tm.year),
1735 dtEnd = dtStart.GetLastMonthDay();
1736
1737 wxDateTimeArray hol;
1738 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart, dtEnd, hol);
1739
1740 size_t count = hol.GetCount();
1741 for ( size_t n = 0; n < count; n++ )
1742 {
1743 SetHoliday(hol[n].GetDay());
1744 }
1745 }
1746 }
1747
1748 void wxCalendarCtrl::SetHoliday(size_t day)
1749 {
1750 wxCHECK_RET( day > 0 && day < 32, _T("invalid day in SetHoliday") );
1751
1752 wxCalendarDateAttr *attr = GetAttr(day);
1753 if ( !attr )
1754 {
1755 attr = new wxCalendarDateAttr;
1756 }
1757
1758 attr->SetHoliday(true);
1759
1760 // can't use SetAttr() because it would delete this pointer
1761 m_attrs[day - 1] = attr;
1762 }
1763
1764 void wxCalendarCtrl::ResetHolidayAttrs()
1765 {
1766 for ( size_t day = 0; day < 31; day++ )
1767 {
1768 if ( m_attrs[day] )
1769 {
1770 m_attrs[day]->SetHoliday(false);
1771 }
1772 }
1773 }
1774
1775
1776 //static
1777 wxVisualAttributes
1778 wxCalendarCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
1779 {
1780 // Use the same color scheme as wxListBox
1781 return wxListBox::GetClassDefaultAttributes(variant);
1782 }
1783
1784 #endif // wxUSE_CALENDARCTRL