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