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