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