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