]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/datetimectrl.mm
Fit modeless preferences dialog to its contents too.
[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 [View() setDateValue: NSDateFromWX(dt)];
82 }
83
84 virtual wxDateTime GetDateTime() const
85 {
86 return NSDateToWX([View() dateValue]);
87 }
88
89 virtual void SetDateRange(const wxDateTime& dt1, const wxDateTime& dt2)
90 {
91 // Note that passing nil is ok here so we don't need to test for the
92 // dates validity.
93 [View() setMinDate: NSDateFromWX(dt1)];
94 [View() setMaxDate: NSDateFromWX(dt2)];
95 }
96
97 virtual bool GetDateRange(wxDateTime* dt1, wxDateTime* dt2)
98 {
99 bool hasLimits = false;
100 if ( dt1 )
101 {
102 *dt1 = NSDateToWX([View() minDate]);
103 hasLimits = true;
104 }
105
106 if ( dt2 )
107 {
108 *dt2 = NSDateToWX([View() maxDate]);
109 hasLimits = true;
110 }
111
112 return hasLimits;
113 }
114
115 virtual void controlAction(WXWidget WXUNUSED(slf),
116 void* WXUNUSED(cmd),
117 void* WXUNUSED(sender))
118 {
119 wxWindow* const wxpeer = GetWXPeer();
120 if ( wxpeer )
121 {
122 static_cast<wxDateTimePickerCtrl*>(wxpeer)->
123 OSXGenerateEvent(GetDateTime());
124 }
125 }
126
127 private:
128 wxNSDatePicker* View() const
129 {
130 return static_cast<wxNSDatePicker *>(m_osxView);
131 }
132 };
133
134 } // anonymous namespace
135
136 // ----------------------------------------------------------------------------
137 // CreateDateTimePicker() implementation
138 // ----------------------------------------------------------------------------
139
140 /* static */
141 wxDateTimeWidgetImpl*
142 wxDateTimeWidgetImpl::CreateDateTimePicker(wxDateTimePickerCtrl* wxpeer,
143 const wxDateTime& dt,
144 const wxPoint& pos,
145 const wxSize& size,
146 long style,
147 wxDateTimeWidgetKind kind)
148 {
149 NSRect r = wxOSXGetFrameForControl(wxpeer, pos, size);
150 wxNSDatePicker* v = [[wxNSDatePicker alloc] initWithFrame:r];
151
152 NSDatePickerElementFlags elements = 0;
153 switch ( kind )
154 {
155 case wxDateTimeWidget_YearMonthDay:
156 elements = NSYearMonthDayDatePickerElementFlag;
157 break;
158
159 case wxDateTimeWidget_HourMinuteSecond:
160 elements = NSHourMinuteSecondDatePickerElementFlag;
161 break;
162 }
163
164 wxASSERT_MSG( elements, "Unknown date time widget kind" );
165 [v setDatePickerElements: elements];
166
167 [v setDatePickerStyle: NSTextFieldAndStepperDatePickerStyle];
168
169 if ( dt.IsValid() )
170 {
171 [v setDateValue: NSDateFromWX(dt)];
172 }
173
174 wxDateTimeWidgetImpl* c = new wxDateTimeWidgetCocoaImpl(wxpeer, v);
175 #if !wxOSX_USE_NATIVE_FLIPPED
176 c->SetFlipped(false);
177 #endif
178 return c;
179 }
180
181 #endif // wxUSE_DATEPICKCTRL