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 [View() setDateValue: NSDateFromWX(dt)];
84 virtual wxDateTime GetDateTime() const
86 return NSDateToWX([View() dateValue]);
89 virtual void SetDateRange(const wxDateTime& dt1, const wxDateTime& dt2)
91 // Note that passing nil is ok here so we don't need to test for the
93 [View() setMinDate: NSDateFromWX(dt1)];
94 [View() setMaxDate: NSDateFromWX(dt2)];
97 virtual bool GetDateRange(wxDateTime* dt1, wxDateTime* dt2)
99 bool hasLimits = false;
102 *dt1 = NSDateToWX([View() minDate]);
108 *dt2 = NSDateToWX([View() maxDate]);
115 virtual void controlAction(WXWidget WXUNUSED(slf),
117 void* WXUNUSED(sender))
119 wxWindow* const wxpeer = GetWXPeer();
122 static_cast<wxDateTimePickerCtrl*>(wxpeer)->
123 OSXGenerateEvent(GetDateTime());
128 wxNSDatePicker* View() const
130 return static_cast<wxNSDatePicker *>(m_osxView);
134 } // anonymous namespace
136 // ----------------------------------------------------------------------------
137 // CreateDateTimePicker() implementation
138 // ----------------------------------------------------------------------------
141 wxDateTimeWidgetImpl*
142 wxDateTimeWidgetImpl::CreateDateTimePicker(wxDateTimePickerCtrl* wxpeer,
143 const wxDateTime& dt,
147 wxDateTimeWidgetKind kind)
149 NSRect r = wxOSXGetFrameForControl(wxpeer, pos, size);
150 wxNSDatePicker* v = [[wxNSDatePicker alloc] initWithFrame:r];
152 NSDatePickerElementFlags elements = 0;
155 case wxDateTimeWidget_YearMonthDay:
156 elements = NSYearMonthDayDatePickerElementFlag;
159 case wxDateTimeWidget_HourMinuteSecond:
160 elements = NSHourMinuteSecondDatePickerElementFlag;
164 wxASSERT_MSG( elements, "Unknown date time widget kind" );
165 [v setDatePickerElements: elements];
167 [v setDatePickerStyle: NSTextFieldAndStepperDatePickerStyle];
171 [v setDateValue: NSDateFromWX(dt)];
174 wxDateTimeWidgetImpl* c = new wxDateTimeWidgetCocoaImpl(wxpeer, v);
175 #if !wxOSX_USE_NATIVE_FLIPPED
176 c->SetFlipped(false);
181 #endif // wxUSE_DATEPICKCTRL