]> git.saurik.com Git - wxWidgets.git/blob - src/generic/calctrl.cpp
Modified <shift>+arrow key behaviour so that the starting cell is
[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 // Can only use wxSpinEvent if this is enabled
40 #if wxUSE_SPINBTN
41
42 #include "wx/calctrl.h"
43
44 #define DEBUG_PAINT 0
45
46 // ----------------------------------------------------------------------------
47 // private classes
48 // ----------------------------------------------------------------------------
49
50 class wxMonthComboBox : public wxComboBox
51 {
52 public:
53 wxMonthComboBox(wxCalendarCtrl *cal);
54
55 void OnMonthChange(wxCommandEvent& event) { m_cal->OnMonthChange(event); }
56
57 private:
58 wxCalendarCtrl *m_cal;
59
60 DECLARE_EVENT_TABLE()
61 };
62
63 class wxYearSpinCtrl : public wxSpinCtrl
64 {
65 public:
66 wxYearSpinCtrl(wxCalendarCtrl *cal);
67
68 void OnYearChange(wxSpinEvent& event) { m_cal->OnYearChange(event); }
69
70 private:
71 wxCalendarCtrl *m_cal;
72
73 DECLARE_EVENT_TABLE()
74 };
75
76 // ----------------------------------------------------------------------------
77 // wxWin macros
78 // ----------------------------------------------------------------------------
79
80 BEGIN_EVENT_TABLE(wxCalendarCtrl, wxControl)
81 EVT_PAINT(wxCalendarCtrl::OnPaint)
82
83 EVT_CHAR(wxCalendarCtrl::OnChar)
84
85 EVT_LEFT_DOWN(wxCalendarCtrl::OnClick)
86 EVT_LEFT_DCLICK(wxCalendarCtrl::OnDClick)
87 END_EVENT_TABLE()
88
89 BEGIN_EVENT_TABLE(wxMonthComboBox, wxComboBox)
90 EVT_COMBOBOX(-1, wxMonthComboBox::OnMonthChange)
91 END_EVENT_TABLE()
92
93 BEGIN_EVENT_TABLE(wxYearSpinCtrl, wxSpinCtrl)
94 EVT_SPINCTRL(-1, wxYearSpinCtrl::OnYearChange)
95 END_EVENT_TABLE()
96
97 IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl, wxControl)
98
99 // ============================================================================
100 // implementation
101 // ============================================================================
102
103 // ----------------------------------------------------------------------------
104 // wxMonthComboBox and wxYearSpinCtrl
105 // ----------------------------------------------------------------------------
106
107 wxMonthComboBox::wxMonthComboBox(wxCalendarCtrl *cal)
108 : wxComboBox(cal->GetParent(), -1,
109 wxEmptyString,
110 wxDefaultPosition,
111 wxDefaultSize,
112 0, NULL,
113 wxCB_READONLY)
114 {
115 m_cal = cal;
116
117 wxDateTime::Month m;
118 for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
119 {
120 Append(wxDateTime::GetMonthName(m));
121 }
122
123 SetSelection(m_cal->GetDate().GetMonth());
124 }
125
126 wxYearSpinCtrl::wxYearSpinCtrl(wxCalendarCtrl *cal)
127 : wxSpinCtrl(cal->GetParent(), -1,
128 cal->GetDate().Format(_T("%Y")),
129 wxDefaultPosition,
130 wxDefaultSize,
131 wxSP_ARROW_KEYS,
132 -4300, 10000, cal->GetDate().GetYear())
133 {
134 m_cal = cal;
135 }
136
137 // ----------------------------------------------------------------------------
138 // wxCalendarCtrl
139 // ----------------------------------------------------------------------------
140
141 void wxCalendarCtrl::Init()
142 {
143 m_comboMonth = NULL;
144 m_spinYear = NULL;
145
146 m_widthCol =
147 m_heightRow = 0;
148
149 wxDateTime::WeekDay wd;
150 for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) )
151 {
152 m_weekdays[wd] = wxDateTime::GetWeekDayName(wd, wxDateTime::Name_Abbr);
153 }
154 m_weekdaysLen = m_weekdays[0].Length(); // mj10777 : length of Day name
155 for ( size_t n = 0; n < WXSIZEOF(m_attrs); n++ )
156 {
157 m_attrs[n] = NULL;
158 }
159
160 wxSystemSettings ss;
161 m_colHighlightFg = ss.GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
162 m_colHighlightBg = ss.GetSystemColour(wxSYS_COLOUR_HIGHLIGHT);
163
164 m_colHolidayFg = *wxRED;
165 // don't set m_colHolidayBg - by default, same as our bg colour
166
167 m_colHeaderFg = *wxBLUE;
168 m_colHeaderBg = *wxLIGHT_GREY;
169 }
170
171 bool wxCalendarCtrl::Create(wxWindow * WXUNUSED(parent),
172 wxWindowID WXUNUSED(id),
173 const wxDateTime& date,
174 const wxPoint& WXUNUSED(pos),
175 const wxSize& size,
176 long style,
177 const wxString& WXUNUSED(name))
178 {
179 // needed to get the arrow keys normally used for the dialog navigation
180 SetWindowStyle(style | wxWANTS_CHARS);
181
182 m_date = date.IsValid() ? date : wxDateTime::Today();
183
184 m_spinYear = new wxYearSpinCtrl(this);
185 m_staticYear = new wxStaticText(GetParent(), -1, m_date.Format(_T("%Y")),
186 wxDefaultPosition, wxDefaultSize,
187 wxALIGN_CENTRE);
188
189 m_comboMonth = new wxMonthComboBox(this);
190 m_staticMonth = new wxStaticText(GetParent(), -1, m_date.Format(_T("%B")),
191 wxDefaultPosition, wxDefaultSize,
192 wxALIGN_CENTRE);
193
194 ShowCurrentControls();
195
196 wxSize sizeReal;
197 if ( size.x == -1 || size.y == -1 )
198 {
199 sizeReal = DoGetBestSize();
200 if ( size.x != -1 )
201 sizeReal.x = size.x;
202 if ( size.y != -1 )
203 sizeReal.y = size.y;
204 }
205 else
206 {
207 sizeReal = size;
208 }
209
210 SetSize(sizeReal);
211
212 SetBackgroundColour(*wxWHITE);
213 SetFont(*wxSWISS_FONT);
214
215 SetHolidayAttrs();
216
217 return TRUE;
218 }
219
220 wxCalendarCtrl::~wxCalendarCtrl()
221 {
222 for ( size_t n = 0; n < WXSIZEOF(m_attrs); n++ )
223 {
224 delete m_attrs[n];
225 }
226 }
227
228 // ----------------------------------------------------------------------------
229 // forward wxWin functions to subcontrols
230 // ----------------------------------------------------------------------------
231
232 bool wxCalendarCtrl::Show(bool show)
233 {
234 if ( !wxControl::Show(show) )
235 {
236 return FALSE;
237 }
238
239 GetMonthControl()->Show(show);
240 GetYearControl()->Show(show);
241
242 return TRUE;
243 }
244
245 bool wxCalendarCtrl::Enable(bool enable)
246 {
247 if ( !wxControl::Enable(enable) )
248 {
249 return FALSE;
250 }
251
252 GetMonthControl()->Enable(enable);
253 GetYearControl()->Enable(enable);
254
255 return TRUE;
256 }
257
258 // ----------------------------------------------------------------------------
259 // enable/disable month/year controls
260 // ----------------------------------------------------------------------------
261
262 void wxCalendarCtrl::ShowCurrentControls()
263 {
264 if ( AllowMonthChange() )
265 {
266 m_comboMonth->Show();
267 m_staticMonth->Hide();
268
269 if ( AllowYearChange() )
270 {
271 m_spinYear->Show();
272 m_staticYear->Hide();
273
274 // skip the rest
275 return;
276 }
277 }
278 else
279 {
280 m_comboMonth->Hide();
281 m_staticMonth->Show();
282 }
283
284 // year change not allowed here
285 m_spinYear->Hide();
286 m_staticYear->Show();
287 }
288
289 wxControl *wxCalendarCtrl::GetMonthControl() const
290 {
291 return AllowMonthChange() ? (wxControl *)m_comboMonth : (wxControl *)m_staticMonth;
292 }
293
294 wxControl *wxCalendarCtrl::GetYearControl() const
295 {
296 return AllowYearChange() ? (wxControl *)m_spinYear : (wxControl *)m_staticYear;
297 }
298
299 void wxCalendarCtrl::EnableYearChange(bool enable)
300 {
301 if ( enable != AllowYearChange() )
302 {
303 long style = GetWindowStyle();
304 if ( enable )
305 style &= ~wxCAL_NO_YEAR_CHANGE;
306 else
307 style |= wxCAL_NO_YEAR_CHANGE;
308 SetWindowStyle(style);
309
310 ShowCurrentControls();
311 }
312 }
313
314 void wxCalendarCtrl::EnableMonthChange(bool enable)
315 {
316 if ( enable != AllowMonthChange() )
317 {
318 long style = GetWindowStyle();
319 if ( enable )
320 style &= ~wxCAL_NO_MONTH_CHANGE;
321 else
322 style |= wxCAL_NO_MONTH_CHANGE;
323 SetWindowStyle(style);
324
325 ShowCurrentControls();
326 }
327 }
328
329 // ----------------------------------------------------------------------------
330 // changing date
331 // ----------------------------------------------------------------------------
332
333 void wxCalendarCtrl::SetDate(const wxDateTime& date)
334 {
335 bool sameMonth = m_date.GetMonth() == date.GetMonth(),
336 sameYear = m_date.GetYear() == date.GetYear();
337
338 if ( sameMonth && sameYear )
339 {
340 // just change the day
341 ChangeDay(date);
342 }
343 else
344 {
345 if ( !AllowMonthChange() || (!AllowYearChange() && !sameYear) )
346 {
347 // forbidden
348 return;
349 }
350
351 // change everything
352 m_date = date;
353
354 // update the controls
355 m_comboMonth->SetSelection(m_date.GetMonth());
356
357 if ( AllowYearChange() )
358 {
359 m_spinYear->SetValue(m_date.Format(_T("%Y")));
360 }
361
362 // as the month changed, holidays did too
363 SetHolidayAttrs();
364
365 // update the calendar
366 Refresh();
367 }
368 }
369
370 void wxCalendarCtrl::ChangeDay(const wxDateTime& date)
371 {
372 if ( m_date != date )
373 {
374 // we need to refresh the row containing the old date and the one
375 // containing the new one
376 wxDateTime dateOld = m_date;
377 m_date = date;
378
379 RefreshDate(dateOld);
380
381 // if the date is in the same row, it was already drawn correctly
382 if ( GetWeek(m_date) != GetWeek(dateOld) )
383 {
384 RefreshDate(m_date);
385 }
386 }
387 }
388
389 void wxCalendarCtrl::SetDateAndNotify(const wxDateTime& date)
390 {
391 wxDateTime::Tm tm1 = m_date.GetTm(),
392 tm2 = date.GetTm();
393
394 wxEventType type;
395 if ( tm1.year != tm2.year )
396 type = wxEVT_CALENDAR_YEAR_CHANGED;
397 else if ( tm1.mon != tm2.mon )
398 type = wxEVT_CALENDAR_MONTH_CHANGED;
399 else if ( tm1.mday != tm2.mday )
400 type = wxEVT_CALENDAR_DAY_CHANGED;
401 else
402 return;
403
404 SetDate(date);
405
406 GenerateEvents(type, wxEVT_CALENDAR_SEL_CHANGED);
407 }
408
409 // ----------------------------------------------------------------------------
410 // date helpers
411 // ----------------------------------------------------------------------------
412
413 wxDateTime wxCalendarCtrl::GetStartDate() const
414 {
415 wxDateTime::Tm tm = m_date.GetTm();
416
417 wxDateTime date = wxDateTime(1, tm.mon, tm.year);
418
419 // rewind back
420 date.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
421 ? wxDateTime::Mon : wxDateTime::Sun);
422
423 return date;
424 }
425
426 bool wxCalendarCtrl::IsDateShown(const wxDateTime& date) const
427 {
428 return date.GetMonth() == m_date.GetMonth();
429 }
430
431 size_t wxCalendarCtrl::GetWeek(const wxDateTime& date) const
432 {
433 return date.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST
434 ? wxDateTime::Monday_First
435 : wxDateTime::Sunday_First);
436 }
437
438 // ----------------------------------------------------------------------------
439 // size management
440 // ----------------------------------------------------------------------------
441
442 // this is a composite control and it must arrange its parts each time its
443 // size or position changes: the combobox and spinctrl are along the top of
444 // the available area and the calendar takes up therest of the space
445
446 // the static controls are supposed to be always smaller than combo/spin so we
447 // always use the latter for size calculations and position the static to take
448 // the same space
449
450 // the constants used for the layout
451 #define VERT_MARGIN 5 // distance between combo and calendar
452 #define HORZ_MARGIN 15 // spin
453
454 wxSize wxCalendarCtrl::DoGetBestSize() const
455 {
456 // calc the size of the calendar
457 ((wxCalendarCtrl *)this)->RecalcGeometry(); // const_cast
458
459 wxCoord width = 7*m_widthCol,
460 height = 7*m_heightRow;
461
462 wxSize sizeCombo = m_comboMonth->GetBestSize(),
463 sizeSpin = m_spinYear->GetBestSize();
464
465 height += VERT_MARGIN + wxMax(sizeCombo.y, sizeSpin.y);
466
467 if ( GetWindowStyle() & (wxRAISED_BORDER | wxSUNKEN_BORDER) )
468 {
469 // the border would clip the last line otherwise
470 height += 4;
471 }
472
473 return wxSize(width, height);
474 }
475
476 void wxCalendarCtrl::DoSetSize(int x, int y,
477 int width, int height,
478 int sizeFlags)
479 {
480 wxControl::DoSetSize(x, y, width, height, sizeFlags);
481 }
482
483 void wxCalendarCtrl::DoMoveWindow(int x, int y, int width, int height)
484 {
485 wxSize sizeCombo = m_comboMonth->GetSize();
486 wxSize sizeStatic = m_staticMonth->GetSize();
487
488 int dy = (sizeCombo.y - sizeStatic.y) / 2;
489 m_comboMonth->Move(x, y);
490 m_staticMonth->SetSize(x, y + dy, sizeCombo.x, sizeStatic.y);
491
492 int xDiff = sizeCombo.x + HORZ_MARGIN;
493 m_spinYear->SetSize(x + xDiff, y, width - xDiff, sizeCombo.y);
494 m_staticYear->SetSize(x + xDiff, y + dy, width - xDiff, sizeStatic.y);
495
496 wxSize sizeSpin = m_spinYear->GetSize();
497 int yDiff = wxMax(sizeSpin.y, sizeCombo.y) + VERT_MARGIN;
498
499 wxControl::DoMoveWindow(x, y + yDiff, width, height - yDiff);
500 }
501
502 void wxCalendarCtrl::DoGetPosition(int *x, int *y) const
503 {
504 wxControl::DoGetPosition(x, y);
505
506 // our real top corner is not in this position
507 if ( y )
508 {
509 *y -= GetMonthControl()->GetSize().y + VERT_MARGIN;
510 }
511 }
512
513 void wxCalendarCtrl::DoGetSize(int *width, int *height) const
514 {
515 wxControl::DoGetSize(width, height);
516
517 // our real height is bigger
518 if ( height )
519 {
520 *height += GetMonthControl()->GetSize().y + VERT_MARGIN;
521 }
522 }
523
524 void wxCalendarCtrl::RecalcGeometry()
525 {
526 if ( m_widthCol != 0 )
527 return;
528
529 wxClientDC dc(this);
530 dc.SetFont(m_font);
531 // determine the column width (we assume that the weekday names are always
532 // wider (in any language) than the numbers)
533 m_widthCol = 0;
534 wxDateTime::WeekDay wd;
535 for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) )
536 {
537 wxCoord width;
538 dc.GetTextExtent(m_weekdays[wd], &width, &m_heightRow);
539 if ( width > m_widthCol )
540 {
541 m_widthCol = width;
542 }
543 }
544 // leave some margins
545 m_widthCol += 2;
546 m_heightRow += 2;
547 }
548
549 // ----------------------------------------------------------------------------
550 // drawing
551 // ----------------------------------------------------------------------------
552
553 void wxCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
554 {
555 wxCoord width; // mj10777 : moved to top of function
556 RecalcGeometry(); // mj10777 : needed for wxDefaultSize
557 wxSize size; // mj10777 : size of Ctrl
558
559 wxPaintDC dc(this);
560 size = GetSize();
561 m_font.SetPointSize(size.y/14); // Font in proportion to height
562 dc.SetFont(m_font);
563 if ((m_widthCol != size.x/7) || (m_heightRow != size.y/9))
564 { // mj10777 : only if size has changed
565 // ShowCurrentControls(); // Turn off ?
566 m_widthCol = size.x/7;
567 m_heightRow = size.y/9;
568 SetSize(m_widthCol*7,m_heightRow*9); // mj10777 : Set to fit full number
569 m_weekdaysLen = m_weekdays[0].Length();
570 dc.GetTextExtent(m_weekdays[0], &width, (wxCoord *)NULL);
571 while (width >= m_widthCol)
572 { // mj10777 : how many letters fit in the Column ?
573 #if DEBUG_PAINT
574 printf("%d) : m_widthCol(%d) ; width(%d)\n",m_weekdaysLen,m_widthCol,width);
575 #endif
576 m_weekdaysLen--;
577 dc.GetTextExtent(m_weekdays[0].Mid(0, m_weekdaysLen), &width, (wxCoord *)NULL);
578 }
579 // ShowCurrentControls(); // Turn on ?
580 wxSize combosize = m_comboMonth->GetSize();
581 combosize.x = size.x / 2; // When small, the Year cannot not be seen if the month is to big
582 m_comboMonth->SetSize(combosize);
583 } // mj10777 : only if size has changed
584
585 if (m_weekdaysLen < 1)
586 m_weekdaysLen = 1; // mj10777 : must never be less that 1
587
588 #if DEBUG_PAINT
589 printf("--- starting to paint, selection: %s, week %u\n",
590 m_date.Format("%a %d-%m-%Y %H:%M:%S").c_str(),
591 GetWeek(m_date));
592 #endif
593
594 // first draw the week days
595 if ( IsExposed(0, 0, 7*m_widthCol, m_heightRow) )
596 {
597 #if DEBUG_PAINT
598 puts("painting the header");
599 #endif
600
601 dc.SetBackgroundMode(wxTRANSPARENT);
602 dc.SetTextForeground(m_colHeaderFg);
603 dc.SetBrush(wxBrush(m_colHeaderBg, wxSOLID));
604 dc.SetPen(wxPen(m_colHeaderBg, 1, wxSOLID));
605 dc.DrawRectangle(0, 0, 7*m_widthCol, m_heightRow);
606
607 bool startOnMonday = (GetWindowStyle() & wxCAL_MONDAY_FIRST) != 0;
608 for ( size_t wd = 0; wd < 7; wd++ )
609 {
610 size_t n;
611 if ( startOnMonday )
612 n = wd == 6 ? 0 : wd + 1;
613 else
614 n = wd;
615
616 dc.GetTextExtent(m_weekdays[n].Mid(0,m_weekdaysLen), &width, (wxCoord *)NULL); // mj10777
617 width = wd*m_widthCol + (m_widthCol - width) / 2; // mj10777
618 dc.DrawText(m_weekdays[n].Mid(0,m_weekdaysLen), width, 0); // mj10777
619 }
620 }
621
622 // then the calendar itself
623 dc.SetTextForeground(*wxBLACK);
624 //dc.SetFont(*wxNORMAL_FONT);
625
626 wxCoord y = m_heightRow;
627
628 wxDateTime date = GetStartDate();
629 #if DEBUG_PAINT
630 printf("starting calendar from %s\n",
631 date.Format("%a %d-%m-%Y %H:%M:%S").c_str());
632 #endif
633
634 dc.SetBackgroundMode(wxSOLID);
635 for ( size_t nWeek = 1; nWeek <= 6; nWeek++, y += m_heightRow )
636 {
637 // if the update region doesn't intersect this row, don't paint it
638 if ( !IsExposed(0, y, 7*m_widthCol, m_heightRow - 1) )
639 {
640 date += wxDateSpan::Week();
641
642 continue;
643 }
644
645 #if DEBUG_PAINT
646 printf("painting week %d at y = %d\n", nWeek, y);
647 #endif
648
649 for ( size_t wd = 0; wd < 7; wd++ )
650 {
651 if ( IsDateShown(date) )
652 {
653 // don't use wxDate::Format() which prepends 0s
654 unsigned int day = date.GetDay();
655 wxString dayStr = wxString::Format(_T("%u"), day);
656 dc.GetTextExtent(dayStr, &width, (wxCoord *)NULL);
657
658 bool changedColours = FALSE,
659 changedFont = FALSE;
660
661 wxCalendarDateAttr *attr = m_attrs[day - 1];
662
663 bool isSel = m_date == date;
664 if ( isSel )
665 {
666 dc.SetTextForeground(m_colHighlightFg);
667 dc.SetTextBackground(m_colHighlightBg);
668
669 changedColours = TRUE;
670 }
671 else if ( attr )
672 {
673 wxColour colFg, colBg;
674
675 if ( attr->IsHoliday() )
676 {
677 colFg = m_colHolidayFg;
678 colBg = m_colHolidayBg;
679 }
680 else
681 {
682 colFg = attr->GetTextColour();
683 colBg = attr->GetBackgroundColour();
684 }
685
686 if ( colFg.Ok() )
687 {
688 dc.SetTextForeground(colFg);
689 changedColours = TRUE;
690 }
691
692 if ( colBg.Ok() )
693 {
694 dc.SetTextBackground(colBg);
695 changedColours = TRUE;
696 }
697
698 if ( attr->HasFont() )
699 {
700 dc.SetFont(attr->GetFont());
701 changedFont = TRUE;
702 }
703 }
704
705 wxCoord x = wd*m_widthCol + (m_widthCol - width) / 2;
706 dc.DrawText(dayStr, x, y + 1);
707
708 if ( !isSel && attr && attr->HasBorder() )
709 {
710 wxColour colBorder;
711 if ( attr->HasBorderColour() )
712 {
713 colBorder = attr->GetBorderColour();
714 }
715 else
716 {
717 colBorder = m_foregroundColour;
718 }
719
720 wxPen pen(colBorder, 1, wxSOLID);
721 dc.SetPen(pen);
722 dc.SetBrush(*wxTRANSPARENT_BRUSH);
723
724 switch ( attr->GetBorder() )
725 {
726 case wxCAL_BORDER_SQUARE:
727 dc.DrawRectangle(x - 2, y,
728 width + 4, m_heightRow);
729 break;
730
731 case wxCAL_BORDER_ROUND:
732 dc.DrawEllipse(x - 2, y,
733 width + 4, m_heightRow);
734 break;
735
736 default:
737 wxFAIL_MSG(_T("unknown border type"));
738 }
739 }
740
741 if ( changedColours )
742 {
743 dc.SetTextForeground(m_foregroundColour);
744 dc.SetTextBackground(m_backgroundColour);
745 }
746
747 if ( changedFont )
748 {
749 dc.SetFont(m_font);
750 }
751 }
752 //else: just don't draw it
753
754 date += wxDateSpan::Day();
755 }
756 }
757 #if DEBUG_PAINT
758 puts("+++ finished painting");
759 #endif
760 }
761
762 void wxCalendarCtrl::RefreshDate(const wxDateTime& date)
763 {
764 RecalcGeometry();
765
766 wxRect rect;
767
768 // always refresh the whole row at once because our OnPaint() will draw
769 // the whole row anyhow - and this allows the small optimisation in
770 // OnClick() below to work
771 rect.x = 0;
772 rect.y = m_heightRow * GetWeek(date);
773 rect.width = 7*m_widthCol;
774 rect.height = m_heightRow;
775
776 #if DEBUG_PAINT
777 printf("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
778 GetWeek(date),
779 rect.x, rect.y,
780 rect.x + rect.width, rect.y + rect.height);
781 #endif
782
783 Refresh(TRUE, &rect);
784 }
785
786 // ----------------------------------------------------------------------------
787 // mouse handling
788 // ----------------------------------------------------------------------------
789
790 void wxCalendarCtrl::OnDClick(wxMouseEvent& event)
791 {
792 if ( HitTest(event.GetPosition()) != wxCAL_HITTEST_DAY )
793 {
794 event.Skip();
795 }
796 else
797 {
798 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED);
799 }
800 }
801
802 void wxCalendarCtrl::OnClick(wxMouseEvent& event)
803 {
804 wxDateTime date;
805 wxDateTime::WeekDay wday;
806 switch ( HitTest(event.GetPosition(), &date, &wday) )
807 {
808 case wxCAL_HITTEST_DAY:
809 ChangeDay(date);
810
811 GenerateEvents(wxEVT_CALENDAR_DAY_CHANGED,
812 wxEVT_CALENDAR_SEL_CHANGED);
813 break;
814
815 case wxCAL_HITTEST_HEADER:
816 {
817 wxCalendarEvent event(this, wxEVT_CALENDAR_WEEKDAY_CLICKED);
818 event.m_wday = wday;
819 (void)GetEventHandler()->ProcessEvent(event);
820 }
821 break;
822
823 default:
824 wxFAIL_MSG(_T("unknown hittest code"));
825 // fall through
826
827 case wxCAL_HITTEST_NOWHERE:
828 event.Skip();
829 break;
830 }
831 }
832
833 wxCalendarHitTestResult wxCalendarCtrl::HitTest(const wxPoint& pos,
834 wxDateTime *date,
835 wxDateTime::WeekDay *wd)
836 {
837 RecalcGeometry();
838
839 int wday = pos.x / m_widthCol;
840
841 wxCoord y = pos.y;
842 if ( y < m_heightRow )
843 {
844 if ( wd )
845 {
846 if ( GetWindowStyle() & wxCAL_MONDAY_FIRST )
847 {
848 wday = wday == 6 ? 0 : wday + 1;
849 }
850
851 *wd = (wxDateTime::WeekDay)wday;
852 }
853
854 return wxCAL_HITTEST_HEADER;
855 }
856
857 int week = (y - m_heightRow) / m_heightRow;
858 if ( week >= 6 || wday >= 7 )
859 {
860 return wxCAL_HITTEST_NOWHERE;
861 }
862
863 wxDateTime dt = GetStartDate() + wxDateSpan::Days(7*week + wday);
864
865 if ( IsDateShown(dt) )
866 {
867 if ( date )
868 *date = dt;
869
870 return wxCAL_HITTEST_DAY;
871 }
872 else
873 {
874 return wxCAL_HITTEST_NOWHERE;
875 }
876 }
877
878 // ----------------------------------------------------------------------------
879 // subcontrols events handling
880 // ----------------------------------------------------------------------------
881
882 void wxCalendarCtrl::OnMonthChange(wxCommandEvent& event)
883 {
884 wxDateTime::Tm tm = m_date.GetTm();
885
886 wxDateTime::Month mon = (wxDateTime::Month)event.GetInt();
887 if ( tm.mday > wxDateTime::GetNumberOfDays(mon, tm.year) )
888 {
889 tm.mday = wxDateTime::GetNumberOfDays(mon, tm.year);
890 }
891
892 SetDateAndNotify(wxDateTime(tm.mday, mon, tm.year));
893 }
894
895 void wxCalendarCtrl::OnYearChange(wxSpinEvent& event)
896 {
897 wxDateTime::Tm tm = m_date.GetTm();
898
899 int year = (int)event.GetInt();
900 if ( tm.mday > wxDateTime::GetNumberOfDays(tm.mon, year) )
901 {
902 tm.mday = wxDateTime::GetNumberOfDays(tm.mon, year);
903 }
904
905 SetDateAndNotify(wxDateTime(tm.mday, tm.mon, year));
906 }
907
908 // ----------------------------------------------------------------------------
909 // keyboard interface
910 // ----------------------------------------------------------------------------
911
912 void wxCalendarCtrl::OnChar(wxKeyEvent& event)
913 {
914 switch ( event.KeyCode() )
915 {
916 case _T('+'):
917 case WXK_ADD:
918 SetDateAndNotify(m_date + wxDateSpan::Year());
919 break;
920
921 case _T('-'):
922 case WXK_SUBTRACT:
923 SetDateAndNotify(m_date - wxDateSpan::Year());
924 break;
925
926 case WXK_PRIOR:
927 SetDateAndNotify(m_date - wxDateSpan::Month());
928 break;
929
930 case WXK_NEXT:
931 SetDateAndNotify(m_date + wxDateSpan::Month());
932 break;
933
934 case WXK_RIGHT:
935 if ( event.ControlDown() )
936 SetDateAndNotify(wxDateTime(m_date).SetToNextWeekDay(
937 GetWindowStyle() & wxCAL_MONDAY_FIRST
938 ? wxDateTime::Sun : wxDateTime::Sat));
939 else
940 SetDateAndNotify(m_date + wxDateSpan::Day());
941 break;
942
943 case WXK_LEFT:
944 if ( event.ControlDown() )
945 SetDateAndNotify(wxDateTime(m_date).SetToPrevWeekDay(
946 GetWindowStyle() & wxCAL_MONDAY_FIRST
947 ? wxDateTime::Mon : wxDateTime::Sun));
948 else
949 SetDateAndNotify(m_date - wxDateSpan::Day());
950 break;
951
952 case WXK_UP:
953 SetDateAndNotify(m_date - wxDateSpan::Week());
954 break;
955
956 case WXK_DOWN:
957 SetDateAndNotify(m_date + wxDateSpan::Week());
958 break;
959
960 case WXK_HOME:
961 if ( event.ControlDown() )
962 SetDateAndNotify(wxDateTime::Today());
963 else
964 SetDateAndNotify(wxDateTime(1, m_date.GetMonth(), m_date.GetYear()));
965 break;
966
967 case WXK_END:
968 SetDateAndNotify(wxDateTime(m_date).SetToLastMonthDay());
969 break;
970
971 case WXK_RETURN:
972 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED);
973 break;
974
975 default:
976 event.Skip();
977 }
978 }
979
980 // ----------------------------------------------------------------------------
981 // holidays handling
982 // ----------------------------------------------------------------------------
983
984 void wxCalendarCtrl::EnableHolidayDisplay(bool display)
985 {
986 long style = GetWindowStyle();
987 if ( display )
988 style |= wxCAL_SHOW_HOLIDAYS;
989 else
990 style &= ~wxCAL_SHOW_HOLIDAYS;
991
992 SetWindowStyle(style);
993
994 if ( display )
995 SetHolidayAttrs();
996 else
997 ResetHolidayAttrs();
998
999 Refresh();
1000 }
1001
1002 void wxCalendarCtrl::SetHolidayAttrs()
1003 {
1004 if ( GetWindowStyle() & wxCAL_SHOW_HOLIDAYS )
1005 {
1006 ResetHolidayAttrs();
1007
1008 wxDateTime::Tm tm = m_date.GetTm();
1009 wxDateTime dtStart(1, tm.mon, tm.year),
1010 dtEnd = dtStart.GetLastMonthDay();
1011
1012 wxDateTimeArray hol;
1013 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart, dtEnd, hol);
1014
1015 size_t count = hol.GetCount();
1016 for ( size_t n = 0; n < count; n++ )
1017 {
1018 SetHoliday(hol[n].GetDay());
1019 }
1020 }
1021 }
1022
1023 void wxCalendarCtrl::SetHoliday(size_t day)
1024 {
1025 wxCHECK_RET( day > 0 && day < 32, _T("invalid day in SetHoliday") );
1026
1027 wxCalendarDateAttr *attr = GetAttr(day);
1028 if ( !attr )
1029 {
1030 attr = new wxCalendarDateAttr;
1031 }
1032
1033 attr->SetHoliday(TRUE);
1034
1035 // can't use SetAttr() because it would delete this pointer
1036 m_attrs[day - 1] = attr;
1037 }
1038
1039 void wxCalendarCtrl::ResetHolidayAttrs()
1040 {
1041 for ( size_t day = 0; day < 31; day++ )
1042 {
1043 if ( m_attrs[day] )
1044 {
1045 m_attrs[day]->SetHoliday(FALSE);
1046 }
1047 }
1048 }
1049
1050 // ----------------------------------------------------------------------------
1051 // wxCalendarEvent
1052 // ----------------------------------------------------------------------------
1053
1054 void wxCalendarEvent::Init()
1055 {
1056 m_wday = wxDateTime::Inv_WeekDay;
1057 }
1058
1059 wxCalendarEvent::wxCalendarEvent(wxCalendarCtrl *cal, wxEventType type)
1060 : wxCommandEvent(type, cal->GetId())
1061 {
1062 m_date = cal->GetDate();
1063 }
1064
1065 #endif // wxUSE_SPINBTN
1066