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