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