1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/datetimectrl.mm
3 // Purpose: Implementation of wxDateTimePickerCtrl for Cocoa.
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #if wxUSE_DATEPICKCTRL
28 #include "wx/datetimectrl.h"
29 #include "wx/datectrl.h"
31 #include "wx/osx/core/private/datetimectrl.h"
32 #include "wx/osx/cocoa/private/date.h"
34 using namespace wxOSXImpl;
36 // ============================================================================
38 // ============================================================================
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 @interface wxNSDatePicker : NSDatePicker
50 @implementation wxNSDatePicker
54 static BOOL initialized = NO;
58 wxOSXCocoaClassAddWXMethods( self );
64 // ----------------------------------------------------------------------------
65 // Peer-specific subclass
66 // ----------------------------------------------------------------------------
71 class wxDateTimeWidgetCocoaImpl : public wxDateTimeWidgetImpl
74 wxDateTimeWidgetCocoaImpl(wxDateTimePickerCtrl* peer, wxNSDatePicker* w)
75 : wxDateTimeWidgetImpl(peer, w)
79 virtual void SetDateTime(const wxDateTime& dt)
81 wxDateTime dtFrom, dtTo;
83 if ( GetDateRange(&dtFrom,&dtTo) == false ||
84 ( (!dtFrom.IsValid() || dtFrom <= dt) &&
85 (!dtTo.IsValid() || dt <= dtTo ) ) )
86 [View() setDateValue: NSDateFromWX(dt)];
89 virtual wxDateTime GetDateTime() const
91 return NSDateToWX([View() dateValue]);
94 virtual void SetDateRange(const wxDateTime& dt1, const wxDateTime& dt2)
96 // Note that passing nil is ok here so we don't need to test for the
98 [View() setMinDate: NSDateFromWX(dt1)];
99 [View() setMaxDate: NSDateFromWX(dt2)];
102 virtual bool GetDateRange(wxDateTime* dt1, wxDateTime* dt2)
104 bool hasLimits = false;
107 *dt1 = NSDateToWX([View() minDate]);
113 *dt2 = NSDateToWX([View() maxDate]);
120 virtual void controlAction(WXWidget WXUNUSED(slf),
122 void* WXUNUSED(sender))
124 wxWindow* const wxpeer = GetWXPeer();
127 static_cast<wxDateTimePickerCtrl*>(wxpeer)->
128 OSXGenerateEvent(GetDateTime());
133 wxNSDatePicker* View() const
135 return static_cast<wxNSDatePicker *>(m_osxView);
139 } // anonymous namespace
141 // ----------------------------------------------------------------------------
142 // CreateDateTimePicker() implementation
143 // ----------------------------------------------------------------------------
146 wxDateTimeWidgetImpl*
147 wxDateTimeWidgetImpl::CreateDateTimePicker(wxDateTimePickerCtrl* wxpeer,
148 const wxDateTime& dt,
152 wxDateTimeWidgetKind kind)
154 NSRect r = wxOSXGetFrameForControl(wxpeer, pos, size);
155 wxNSDatePicker* v = [[wxNSDatePicker alloc] initWithFrame:r];
157 NSDatePickerElementFlags elements = 0;
160 case wxDateTimeWidget_YearMonthDay:
161 elements = NSYearMonthDayDatePickerElementFlag;
164 case wxDateTimeWidget_HourMinuteSecond:
165 elements = NSHourMinuteSecondDatePickerElementFlag;
169 wxASSERT_MSG( elements, "Unknown date time widget kind" );
170 [v setDatePickerElements: elements];
172 [v setDatePickerStyle: NSTextFieldAndStepperDatePickerStyle];
176 [v setDateValue: NSDateFromWX(dt)];
179 wxDateTimeWidgetImpl* c = new wxDateTimeWidgetCocoaImpl(wxpeer, v);
180 #if !wxOSX_USE_NATIVE_FLIPPED
181 c->SetFlipped(false);
186 #endif // wxUSE_DATEPICKCTRL