]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/datetimectrl.mm
263a8aa6e8cf78231c73a242079e6f061aac210b
[wxWidgets.git] / src / osx / cocoa / datetimectrl.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/datetimectrl.mm
3 // Purpose: Implementation of wxDateTimePickerCtrl for Cocoa.
4 // Author: Vadim Zeitlin
5 // Created: 2011-12-18
6 // Version: $Id$
7 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_DATEPICKCTRL
27
28 #include "wx/datetimectrl.h"
29 #include "wx/datectrl.h"
30
31 #include "wx/osx/core/private/datetimectrl.h"
32 #include "wx/osx/cocoa/private/date.h"
33
34 using namespace wxOSXImpl;
35
36 // ============================================================================
37 // implementation
38 // ============================================================================
39
40 // ----------------------------------------------------------------------------
41 // Cocoa wrappers
42 // ----------------------------------------------------------------------------
43
44 @interface wxNSDatePicker : NSDatePicker
45 {
46 }
47
48 @end
49
50 @implementation wxNSDatePicker
51
52 + (void)initialize
53 {
54 static BOOL initialized = NO;
55 if (!initialized)
56 {
57 initialized = YES;
58 wxOSXCocoaClassAddWXMethods( self );
59 }
60 }
61
62 @end
63
64 // ----------------------------------------------------------------------------
65 // Peer-specific subclass
66 // ----------------------------------------------------------------------------
67
68 namespace
69 {
70
71 class wxDateTimeWidgetCocoaImpl : public wxDateTimeWidgetImpl
72 {
73 public:
74 wxDateTimeWidgetCocoaImpl(wxDateTimePickerCtrl* peer, wxNSDatePicker* w)
75 : wxDateTimeWidgetImpl(peer, w)
76 {
77 }
78
79 virtual void SetDateTime(const wxDateTime& dt)
80 {
81 wxDateTime dtFrom, dtTo;
82
83 if ( GetDateRange(&dtFrom,&dtTo) == false ||
84 ( (!dtFrom.IsValid() || dtFrom <= dt) &&
85 (!dtTo.IsValid() || dt <= dtTo ) ) )
86 [View() setDateValue: NSDateFromWX(dt)];
87 }
88
89 virtual wxDateTime GetDateTime() const
90 {
91 return NSDateToWX([View() dateValue]);
92 }
93
94 virtual void SetDateRange(const wxDateTime& dt1, const wxDateTime& dt2)
95 {
96 // Note that passing nil is ok here so we don't need to test for the
97 // dates validity.
98 [View() setMinDate: NSDateFromWX(dt1)];
99 [View() setMaxDate: NSDateFromWX(dt2)];
100 }
101
102 virtual bool GetDateRange(wxDateTime* dt1, wxDateTime* dt2)
103 {
104 bool hasLimits = false;
105 if ( dt1 )
106 {
107 *dt1 = NSDateToWX([View() minDate]);
108 hasLimits = true;
109 }
110
111 if ( dt2 )
112 {
113 *dt2 = NSDateToWX([View() maxDate]);
114 hasLimits = true;
115 }
116
117 return hasLimits;
118 }
119
120 virtual void controlAction(WXWidget WXUNUSED(slf),
121 void* WXUNUSED(cmd),
122 void* WXUNUSED(sender))
123 {
124 wxWindow* const wxpeer = GetWXPeer();
125 if ( wxpeer )
126 {
127 static_cast<wxDateTimePickerCtrl*>(wxpeer)->
128 OSXGenerateEvent(GetDateTime());
129 }
130 }
131
132 private:
133 wxNSDatePicker* View() const
134 {
135 return static_cast<wxNSDatePicker *>(m_osxView);
136 }
137 };
138
139 } // anonymous namespace
140
141 // ----------------------------------------------------------------------------
142 // CreateDateTimePicker() implementation
143 // ----------------------------------------------------------------------------
144
145 /* static */
146 wxDateTimeWidgetImpl*
147 wxDateTimeWidgetImpl::CreateDateTimePicker(wxDateTimePickerCtrl* wxpeer,
148 const wxDateTime& dt,
149 const wxPoint& pos,
150 const wxSize& size,
151 long style,
152 wxDateTimeWidgetKind kind)
153 {
154 NSRect r = wxOSXGetFrameForControl(wxpeer, pos, size);
155 wxNSDatePicker* v = [[wxNSDatePicker alloc] initWithFrame:r];
156
157 NSDatePickerElementFlags elements = 0;
158 switch ( kind )
159 {
160 case wxDateTimeWidget_YearMonthDay:
161 elements = NSYearMonthDayDatePickerElementFlag;
162 break;
163
164 case wxDateTimeWidget_HourMinuteSecond:
165 elements = NSHourMinuteSecondDatePickerElementFlag;
166 break;
167 }
168
169 wxASSERT_MSG( elements, "Unknown date time widget kind" );
170 [v setDatePickerElements: elements];
171
172 [v setDatePickerStyle: NSTextFieldAndStepperDatePickerStyle];
173
174 if ( dt.IsValid() )
175 {
176 [v setDateValue: NSDateFromWX(dt)];
177 }
178
179 wxDateTimeWidgetImpl* c = new wxDateTimeWidgetCocoaImpl(wxpeer, v);
180 #if !wxOSX_USE_NATIVE_FLIPPED
181 c->SetFlipped(false);
182 #endif
183 return c;
184 }
185
186 #endif // wxUSE_DATEPICKCTRL