]>
Commit | Line | Data |
---|---|---|
da0634c1 | 1 | ///////////////////////////////////////////////////////////////////////////// |
8228b893 | 2 | // Name: src/cocoa/radiobox.mm |
da0634c1 DE |
3 | // Purpose: wxRadioBox |
4 | // Author: David Elliott | |
5 | // Modified by: | |
6 | // Created: 2003/02/15 | |
da0634c1 | 7 | // Copyright: (c) 2003 David Elliott |
9ed97552 | 8 | // (c) 2007 Software 2000 Ltd. |
526954c5 | 9 | // Licence: wxWindows licence |
da0634c1 DE |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
449c5673 | 12 | #include "wx/wxprec.h" |
16c81607 RN |
13 | |
14 | #if wxUSE_RADIOBOX | |
15 | ||
cc11cc69 WS |
16 | #include "wx/radiobox.h" |
17 | ||
449c5673 DE |
18 | #ifndef WX_PRECOMP |
19 | #include "wx/app.h" | |
584ad2a3 | 20 | #include "wx/arrstr.h" |
449c5673 | 21 | #endif //WX_PRECOMP |
da0634c1 | 22 | |
9ed97552 DE |
23 | #include "wx/cocoa/string.h" |
24 | #include "wx/cocoa/autorelease.h" | |
25 | ||
369559ca | 26 | #import <Foundation/NSArray.h> |
829a2e95 | 27 | #include "wx/cocoa/objc/NSView.h" |
9ed97552 DE |
28 | #import <AppKit/NSButton.h> |
29 | #import <AppKit/NSBox.h> | |
30 | #import <AppKit/NSMatrix.h> | |
548dd93d | 31 | |
da0634c1 DE |
32 | IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl) |
33 | BEGIN_EVENT_TABLE(wxRadioBox, wxControl) | |
34 | END_EVENT_TABLE() | |
259502c6 DE |
35 | |
36 | void wxRadioBox::AssociateNSBox(WX_NSBox cocoaObjcClass) | |
37 | { | |
38 | NSMatrix *radioBox = [(WX_NSBox)cocoaObjcClass contentView]; | |
39 | // Associate the NSMatrix (the NSBox's contentView) with the wxCocoaNSControl MI base class. | |
40 | AssociateNSControl(radioBox); | |
41 | // Set the target/action.. we don't really need to unset these | |
42 | [radioBox setTarget:wxCocoaNSControl::sm_cocoaTarget]; | |
43 | [radioBox setAction:@selector(wxNSControlAction:)]; | |
44 | } | |
45 | ||
46 | void wxRadioBox::DisassociateNSBox(WX_NSBox cocoaObjcClass) | |
47 | { | |
48 | DisassociateNSControl([(WX_NSBox)cocoaObjcClass contentView]); | |
49 | } | |
50 | ||
51 | WX_IMPLEMENT_COCOA_OWNER(wxRadioBox,NSBox,NSView,NSView) | |
da0634c1 | 52 | |
584ad2a3 MB |
53 | bool wxRadioBox::Create(wxWindow *parent, wxWindowID winid, |
54 | const wxString& title, | |
55 | const wxPoint& pos, | |
56 | const wxSize& size, | |
57 | const wxArrayString& choices, | |
58 | int majorDim, | |
59 | long style, const wxValidator& validator, | |
60 | const wxString& name) | |
61 | { | |
62 | wxCArrayString chs(choices); | |
63 | ||
64 | return Create(parent, winid, title, pos, size, chs.GetCount(), | |
65 | chs.GetStrings(), majorDim, style, validator, name); | |
66 | } | |
67 | ||
da0634c1 DE |
68 | bool wxRadioBox::Create(wxWindow *parent, wxWindowID winid, |
69 | const wxString& title, | |
70 | const wxPoint& pos, | |
71 | const wxSize& size, | |
72 | int n, const wxString choices[], | |
73 | int majorDim, | |
74 | long style, const wxValidator& validator, | |
75 | const wxString& name) | |
76 | { | |
9ed97552 DE |
77 | // We autorelease heavily so we want our own pool |
78 | wxAutoNSAutoreleasePool pool; | |
79 | ||
da0634c1 DE |
80 | if(!CreateControl(parent,winid,pos,size,style,validator,name)) |
81 | return false; | |
9ed97552 DE |
82 | |
83 | majorDim = majorDim == 0 ? n : majorDim; | |
84 | // TODO: Don't forget to call SetMajorDim | |
85 | // We can't yet as we can't implement GetCount() until after | |
86 | // we make the NSMatrix. | |
87 | int minorDim = (n + majorDim - 1) / majorDim; | |
88 | ||
89 | ||
90 | // Create a prototype cell for use with the NSMatrix build | |
91 | NSCell *currCell = [[NSButtonCell alloc] initTextCell:@""]; | |
92 | [(NSButtonCell*)currCell setButtonType:NSRadioButton]; | |
93 | ||
94 | // Build up an array of all cells plus any extra empty cells | |
95 | NSMutableArray *allCells = [NSMutableArray arrayWithCapacity:n]; | |
96 | for(int i=0; i<n; ++i) | |
97 | { | |
525007cf | 98 | CocoaSetLabelForObject(choices[i], currCell); |
9ed97552 DE |
99 | [allCells addObject: currCell]; |
100 | [currCell release]; | |
101 | // NOTE: We can still safely message currCell as the array has retained it. | |
102 | currCell = [currCell copy]; | |
103 | } | |
104 | [currCell release]; | |
105 | ||
106 | // NOTE: Although an image cell with no image is documented to return NSZeroSize from | |
107 | // the cellSize method, the documentation is WRONG. It will actually return a huge size | |
108 | // (thousands) which makes every cell in the matrix that big. Not good. | |
109 | // Be safe and initialize a text cell with an empty string. That always works. | |
110 | currCell = [[NSCell alloc] initTextCell:@""]; | |
4c1a4c41 | 111 | [currCell setEnabled:NO]; // Don't allow user to select this cell |
9ed97552 DE |
112 | for(int i=n; i < majorDim * minorDim; ++i) |
113 | { | |
114 | [allCells addObject: currCell]; | |
115 | // NOTE: Use the same instance.. this should work and save some heap allocations. | |
9ed97552 DE |
116 | #if 0 |
117 | [currCell release]; | |
118 | currCell = [currCell copy]; | |
119 | #endif | |
120 | } | |
121 | [currCell release]; | |
122 | currCell = NULL; | |
123 | ||
124 | // Although the documentation on addColumnWithCells:/addRowWithCells: explicitly | |
125 | // states that it will determine the initial dimension upon the first call if | |
126 | // the initial size is 0x0 it LIES. It will fail an assertion in the code | |
127 | // if you use the simpler initWithFrame: initializer. | |
128 | // Therefore, we specify the major dimension and leave the minor dimension as 0 | |
129 | // so that we can add the rows/columns without failing the assertion. | |
130 | NSMatrix* radioBox = [[NSMatrix alloc] | |
131 | initWithFrame:NSZeroRect | |
132 | mode:NSRadioModeMatrix | |
133 | cellClass:nil | |
134 | numberOfRows:style&wxRA_SPECIFY_COLS?0:majorDim | |
135 | numberOfColumns:style&wxRA_SPECIFY_COLS?majorDim:0 | |
136 | ]; | |
137 | ||
138 | SEL addMajorWithCellsSelector; | |
139 | // If column count is the major dimension then we add by row | |
140 | if( style & wxRA_SPECIFY_COLS ) | |
141 | addMajorWithCellsSelector = @selector(addRowWithCells:); | |
142 | // If row count is the major dimension then we add by column | |
143 | else | |
144 | addMajorWithCellsSelector = @selector(addColumnWithCells:); | |
145 | ||
146 | for(int i=0; i<minorDim; ++i) | |
147 | { | |
148 | [radioBox | |
149 | performSelector:addMajorWithCellsSelector | |
150 | withObject:[allCells subarrayWithRange:NSMakeRange(i*majorDim, majorDim)]]; | |
151 | } | |
152 | ||
259502c6 | 153 | NSBox *theBox = [[NSBox alloc] initWithFrame:MakeDefaultNSRect(size)]; |
9ed97552 DE |
154 | |
155 | // Replace the box's content view with the NSMatrix we just created | |
259502c6 DE |
156 | // IMPORTANT: This must be done before calling SetNSBox. |
157 | [theBox setContentView:radioBox]; | |
9ed97552 DE |
158 | [radioBox release]; // The NSBox retains it for us. |
159 | ||
259502c6 DE |
160 | SetNSBox(theBox); |
161 | [theBox release]; | |
162 | ||
163 | ||
525007cf | 164 | CocoaSetLabelForObject(title, GetNSBox()); |
9ed97552 DE |
165 | // [GetNSBox() setBorderType:NSLineBorder]; // why?? |
166 | ||
167 | SetMajorDim(majorDim, style); | |
168 | ||
afa292ff DE |
169 | // Set the selection to the first item if we have any items. |
170 | // This is for parity with other wx ports which do the same thing. | |
171 | if(n > 0) | |
172 | SetSelection(0); | |
173 | ||
da0634c1 DE |
174 | if(m_parent) |
175 | m_parent->CocoaAddChild(this); | |
9ed97552 DE |
176 | |
177 | // Do the sizer dance | |
178 | [GetNSBox() sizeToFit]; | |
179 | SetInitialFrameRect(pos, size); | |
8d656ea9 | 180 | |
da0634c1 DE |
181 | return true; |
182 | } | |
183 | ||
184 | wxRadioBox::~wxRadioBox() | |
185 | { | |
259502c6 | 186 | DisassociateNSBox(GetNSBox()); |
da0634c1 DE |
187 | } |
188 | ||
9ed97552 DE |
189 | WX_NSMatrix wxRadioBox::GetNSMatrix() const |
190 | { | |
191 | return (NSMatrix*)[(NSBox*)m_cocoaNSView contentView]; | |
192 | } | |
193 | ||
da0634c1 DE |
194 | // selection |
195 | void wxRadioBox::SetSelection(int n) | |
196 | { | |
4c1a4c41 DE |
197 | int r = GetRowForIndex(n); |
198 | int c = GetColumnForIndex(n); | |
199 | [GetNSMatrix() selectCellAtRow:r column:c]; | |
da0634c1 DE |
200 | } |
201 | ||
202 | int wxRadioBox::GetSelection() const | |
203 | { | |
4c1a4c41 DE |
204 | NSMatrix *radioBox = GetNSMatrix(); |
205 | NSInteger r = [radioBox selectedRow]; | |
206 | NSInteger c = [radioBox selectedColumn]; | |
207 | if(m_windowStyle & wxRA_SPECIFY_COLS) | |
208 | return r * GetMajorDim() + c; | |
209 | else | |
210 | return c * GetMajorDim() + r; | |
da0634c1 DE |
211 | } |
212 | ||
213 | // string access | |
aa61d352 | 214 | unsigned int wxRadioBox::GetCount() const |
da0634c1 | 215 | { |
9ed97552 DE |
216 | NSMatrix *radioBox = GetNSMatrix(); |
217 | NSInteger rowCount, columnCount; | |
218 | [radioBox getNumberOfRows:&rowCount columns:&columnCount]; | |
219 | ||
220 | // FIXME: This is wrong if padding cells were made | |
221 | return rowCount * columnCount; | |
da0634c1 DE |
222 | } |
223 | ||
aa61d352 | 224 | wxString wxRadioBox::GetString(unsigned int n) const |
da0634c1 | 225 | { |
4c1a4c41 DE |
226 | int r = GetRowForIndex(n); |
227 | int c = GetColumnForIndex(n); | |
228 | // FIXME: Cocoa stores the mnemonic-stripped title. | |
229 | return wxStringWithNSString([[GetNSMatrix() cellAtRow:r column:c] title]); | |
da0634c1 DE |
230 | } |
231 | ||
aa61d352 | 232 | void wxRadioBox::SetString(unsigned int n, const wxString& label) |
da0634c1 | 233 | { |
4c1a4c41 DE |
234 | int r = GetRowForIndex(n); |
235 | int c = GetColumnForIndex(n); | |
525007cf | 236 | CocoaSetLabelForObject(label, [GetNSMatrix() cellAtRow:r column:c]); |
da0634c1 DE |
237 | } |
238 | ||
239 | // change the individual radio button state | |
aa61d352 | 240 | bool wxRadioBox::Enable(unsigned int n, bool enable) |
da0634c1 | 241 | { |
4c1a4c41 DE |
242 | int r = GetRowForIndex(n); |
243 | int c = GetColumnForIndex(n); | |
244 | NSCell *cell = [GetNSMatrix() cellAtRow:r column:c]; | |
245 | if(cell == nil) | |
246 | return false; | |
247 | bool wasEnabled = [cell isEnabled]; | |
248 | [cell setEnabled:enable]; | |
249 | return (wasEnabled && !enable) || (!wasEnabled && enable); | |
da0634c1 DE |
250 | } |
251 | ||
aa61d352 | 252 | bool wxRadioBox::Show(unsigned int n, bool show) |
da0634c1 | 253 | { |
789f6795 | 254 | // TODO |
4c1a4c41 DE |
255 | // NOTE: Cocoa has no visible state for cells so we'd need to replace the |
256 | // cell with a dummy one to hide it or alternatively subclass NSButtonCell | |
4c51a665 | 257 | // and add the behaviour. |
789f6795 | 258 | return false; |
da0634c1 DE |
259 | } |
260 | ||
9a165f54 DE |
261 | wxSize wxRadioBox::DoGetBestSize() const |
262 | { | |
9ed97552 DE |
263 | // The NSBox responds to sizeToFit by sending sizeToFit to its contentView |
264 | // which is the NSMatrix and does the right thing. | |
265 | return wxControl::DoGetBestSize(); | |
9a165f54 DE |
266 | } |
267 | ||
259502c6 DE |
268 | void wxRadioBox::CocoaTarget_action(void) |
269 | { | |
ce7fe42e | 270 | wxCommandEvent event(wxEVT_RADIOBOX, GetId()); |
259502c6 DE |
271 | InitCommandEvent(event); |
272 | event.SetInt(GetSelection()); // i.e. SetSelection. | |
273 | Command(event); | |
274 | } | |
275 | ||
16c81607 | 276 | #endif |