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