1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/datetimectrl.mm
3 // Purpose: Implementation of wxDateTimePickerCtrl for Cocoa.
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
25 #if wxUSE_DATEPICKCTRL
27 #include "wx/datetimectrl.h"
28 #include "wx/datectrl.h"
30 #include "wx/osx/core/private/datetimectrl.h"
31 #include "wx/osx/cocoa/private/date.h"
33 using namespace wxOSXImpl;
35 // ============================================================================
37 // ============================================================================
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 @interface wxNSDatePicker : NSDatePicker
49 @implementation wxNSDatePicker
53 static BOOL initialized = NO;
57 wxOSXCocoaClassAddWXMethods( self );
63 // ----------------------------------------------------------------------------
64 // Peer-specific subclass
65 // ----------------------------------------------------------------------------
70 class wxDateTimeWidgetCocoaImpl : public wxDateTimeWidgetImpl
73 wxDateTimeWidgetCocoaImpl(wxDateTimePickerCtrl* peer, wxNSDatePicker* w)
74 : wxDateTimeWidgetImpl(peer, w)
78 virtual void SetDateTime(const wxDateTime& dt)
80 wxDateTime dtFrom, dtTo;
82 if ( GetDateRange(&dtFrom,&dtTo) == false ||
83 ( (!dtFrom.IsValid() || dtFrom <= dt) &&
84 (!dtTo.IsValid() || dt <= dtTo ) ) )
85 [View() setDateValue: NSDateFromWX(dt)];
88 virtual wxDateTime GetDateTime() const
90 return NSDateToWX([View() dateValue]);
93 virtual void SetDateRange(const wxDateTime& dt1, const wxDateTime& dt2)
95 // Note that passing nil is ok here so we don't need to test for the
97 [View() setMinDate: NSDateFromWX(dt1)];
98 [View() setMaxDate: NSDateFromWX(dt2)];
101 virtual bool GetDateRange(wxDateTime* dt1, wxDateTime* dt2)
103 bool hasLimits = false;
106 *dt1 = NSDateToWX([View() minDate]);
112 *dt2 = NSDateToWX([View() maxDate]);
119 virtual void controlAction(WXWidget WXUNUSED(slf),
121 void* WXUNUSED(sender))
123 wxWindow* const wxpeer = GetWXPeer();
126 static_cast<wxDateTimePickerCtrl*>(wxpeer)->
127 OSXGenerateEvent(GetDateTime());
132 wxNSDatePicker* View() const
134 return static_cast<wxNSDatePicker *>(m_osxView);
138 } // anonymous namespace
140 // ----------------------------------------------------------------------------
141 // CreateDateTimePicker() implementation
142 // ----------------------------------------------------------------------------
145 wxDateTimeWidgetImpl*
146 wxDateTimeWidgetImpl::CreateDateTimePicker(wxDateTimePickerCtrl* wxpeer,
147 const wxDateTime& dt,
151 wxDateTimeWidgetKind kind)
153 NSRect r = wxOSXGetFrameForControl(wxpeer, pos, size);
154 wxNSDatePicker* v = [[wxNSDatePicker alloc] initWithFrame:r];
156 NSDatePickerElementFlags elements = 0;
159 case wxDateTimeWidget_YearMonthDay:
160 elements = NSYearMonthDayDatePickerElementFlag;
163 case wxDateTimeWidget_HourMinuteSecond:
164 elements = NSHourMinuteSecondDatePickerElementFlag;
168 wxASSERT_MSG( elements, "Unknown date time widget kind" );
169 [v setDatePickerElements: elements];
171 [v setDatePickerStyle: NSTextFieldAndStepperDatePickerStyle];
175 [v setDateValue: NSDateFromWX(dt)];
178 wxDateTimeWidgetImpl* c = new wxDateTimeWidgetCocoaImpl(wxpeer, v);
179 #if !wxOSX_USE_NATIVE_FLIPPED
180 c->SetFlipped(false);
185 #endif // wxUSE_DATEPICKCTRL