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