]>
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 | |
8228b893 | 7 | // RCS-ID: $Id$ |
da0634c1 | 8 | // Copyright: (c) 2003 David Elliott |
9ed97552 | 9 | // (c) 2007 Software 2000 Ltd. |
1a87edf2 | 10 | // Licence: wxWidgets licence |
da0634c1 DE |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
449c5673 | 13 | #include "wx/wxprec.h" |
16c81607 RN |
14 | |
15 | #if wxUSE_RADIOBOX | |
16 | ||
cc11cc69 WS |
17 | #include "wx/radiobox.h" |
18 | ||
449c5673 DE |
19 | #ifndef WX_PRECOMP |
20 | #include "wx/app.h" | |
584ad2a3 | 21 | #include "wx/arrstr.h" |
449c5673 | 22 | #endif //WX_PRECOMP |
da0634c1 | 23 | |
9ed97552 DE |
24 | #include "wx/cocoa/string.h" |
25 | #include "wx/cocoa/autorelease.h" | |
26 | ||
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() | |
35 | // WX_IMPLEMENT_COCOA_OWNER(wxRadioBox,NSTextField,NSControl,NSView) | |
36 | ||
584ad2a3 MB |
37 | bool wxRadioBox::Create(wxWindow *parent, wxWindowID winid, |
38 | const wxString& title, | |
39 | const wxPoint& pos, | |
40 | const wxSize& size, | |
41 | const wxArrayString& choices, | |
42 | int majorDim, | |
43 | long style, const wxValidator& validator, | |
44 | const wxString& name) | |
45 | { | |
46 | wxCArrayString chs(choices); | |
47 | ||
48 | return Create(parent, winid, title, pos, size, chs.GetCount(), | |
49 | chs.GetStrings(), majorDim, style, validator, name); | |
50 | } | |
51 | ||
da0634c1 DE |
52 | bool wxRadioBox::Create(wxWindow *parent, wxWindowID winid, |
53 | const wxString& title, | |
54 | const wxPoint& pos, | |
55 | const wxSize& size, | |
56 | int n, const wxString choices[], | |
57 | int majorDim, | |
58 | long style, const wxValidator& validator, | |
59 | const wxString& name) | |
60 | { | |
9ed97552 DE |
61 | // We autorelease heavily so we want our own pool |
62 | wxAutoNSAutoreleasePool pool; | |
63 | ||
da0634c1 DE |
64 | if(!CreateControl(parent,winid,pos,size,style,validator,name)) |
65 | return false; | |
9ed97552 DE |
66 | |
67 | majorDim = majorDim == 0 ? n : majorDim; | |
68 | // TODO: Don't forget to call SetMajorDim | |
69 | // We can't yet as we can't implement GetCount() until after | |
70 | // we make the NSMatrix. | |
71 | int minorDim = (n + majorDim - 1) / majorDim; | |
72 | ||
73 | ||
74 | // Create a prototype cell for use with the NSMatrix build | |
75 | NSCell *currCell = [[NSButtonCell alloc] initTextCell:@""]; | |
76 | [(NSButtonCell*)currCell setButtonType:NSRadioButton]; | |
77 | ||
78 | // Build up an array of all cells plus any extra empty cells | |
79 | NSMutableArray *allCells = [NSMutableArray arrayWithCapacity:n]; | |
80 | for(int i=0; i<n; ++i) | |
81 | { | |
82 | [currCell setTitle: wxNSStringWithWxString(choices[i])]; | |
83 | [allCells addObject: currCell]; | |
84 | [currCell release]; | |
85 | // NOTE: We can still safely message currCell as the array has retained it. | |
86 | currCell = [currCell copy]; | |
87 | } | |
88 | [currCell release]; | |
89 | ||
90 | // NOTE: Although an image cell with no image is documented to return NSZeroSize from | |
91 | // the cellSize method, the documentation is WRONG. It will actually return a huge size | |
92 | // (thousands) which makes every cell in the matrix that big. Not good. | |
93 | // Be safe and initialize a text cell with an empty string. That always works. | |
94 | currCell = [[NSCell alloc] initTextCell:@""]; | |
95 | for(int i=n; i < majorDim * minorDim; ++i) | |
96 | { | |
97 | [allCells addObject: currCell]; | |
98 | // NOTE: Use the same instance.. this should work and save some heap allocations. | |
99 | // It will, however, make the selection rather indeterminate if the user clicks | |
100 | // on the empty space. | |
101 | #if 0 | |
102 | [currCell release]; | |
103 | currCell = [currCell copy]; | |
104 | #endif | |
105 | } | |
106 | [currCell release]; | |
107 | currCell = NULL; | |
108 | ||
109 | // Although the documentation on addColumnWithCells:/addRowWithCells: explicitly | |
110 | // states that it will determine the initial dimension upon the first call if | |
111 | // the initial size is 0x0 it LIES. It will fail an assertion in the code | |
112 | // if you use the simpler initWithFrame: initializer. | |
113 | // Therefore, we specify the major dimension and leave the minor dimension as 0 | |
114 | // so that we can add the rows/columns without failing the assertion. | |
115 | NSMatrix* radioBox = [[NSMatrix alloc] | |
116 | initWithFrame:NSZeroRect | |
117 | mode:NSRadioModeMatrix | |
118 | cellClass:nil | |
119 | numberOfRows:style&wxRA_SPECIFY_COLS?0:majorDim | |
120 | numberOfColumns:style&wxRA_SPECIFY_COLS?majorDim:0 | |
121 | ]; | |
122 | ||
123 | SEL addMajorWithCellsSelector; | |
124 | // If column count is the major dimension then we add by row | |
125 | if( style & wxRA_SPECIFY_COLS ) | |
126 | addMajorWithCellsSelector = @selector(addRowWithCells:); | |
127 | // If row count is the major dimension then we add by column | |
128 | else | |
129 | addMajorWithCellsSelector = @selector(addColumnWithCells:); | |
130 | ||
131 | for(int i=0; i<minorDim; ++i) | |
132 | { | |
133 | [radioBox | |
134 | performSelector:addMajorWithCellsSelector | |
135 | withObject:[allCells subarrayWithRange:NSMakeRange(i*majorDim, majorDim)]]; | |
136 | } | |
137 | ||
138 | //make and set up an NSBox (TODO: Just derive from wxStaticBox) | |
139 | SetNSView([[NSBox alloc] initWithFrame:MakeDefaultNSRect(size)]); | |
548dd93d | 140 | [m_cocoaNSView release]; |
9ed97552 DE |
141 | |
142 | // Replace the box's content view with the NSMatrix we just created | |
143 | [GetNSBox() setContentView:radioBox]; | |
144 | [radioBox release]; // The NSBox retains it for us. | |
145 | ||
146 | [GetNSBox() setTitle:wxNSStringWithWxString(title)]; | |
147 | // [GetNSBox() setBorderType:NSLineBorder]; // why?? | |
148 | ||
149 | SetMajorDim(majorDim, style); | |
150 | ||
da0634c1 DE |
151 | if(m_parent) |
152 | m_parent->CocoaAddChild(this); | |
9ed97552 DE |
153 | |
154 | // Do the sizer dance | |
155 | [GetNSBox() sizeToFit]; | |
156 | SetInitialFrameRect(pos, size); | |
8d656ea9 | 157 | |
da0634c1 DE |
158 | return true; |
159 | } | |
160 | ||
161 | wxRadioBox::~wxRadioBox() | |
162 | { | |
da0634c1 DE |
163 | } |
164 | ||
9ed97552 DE |
165 | WX_NSMatrix wxRadioBox::GetNSMatrix() const |
166 | { | |
167 | return (NSMatrix*)[(NSBox*)m_cocoaNSView contentView]; | |
168 | } | |
169 | ||
da0634c1 DE |
170 | // selection |
171 | void wxRadioBox::SetSelection(int n) | |
172 | { | |
173 | } | |
174 | ||
175 | int wxRadioBox::GetSelection() const | |
176 | { | |
177 | return 0; | |
178 | } | |
179 | ||
180 | // string access | |
aa61d352 | 181 | unsigned int wxRadioBox::GetCount() const |
da0634c1 | 182 | { |
9ed97552 DE |
183 | NSMatrix *radioBox = GetNSMatrix(); |
184 | NSInteger rowCount, columnCount; | |
185 | [radioBox getNumberOfRows:&rowCount columns:&columnCount]; | |
186 | ||
187 | // FIXME: This is wrong if padding cells were made | |
188 | return rowCount * columnCount; | |
da0634c1 DE |
189 | } |
190 | ||
aa61d352 | 191 | wxString wxRadioBox::GetString(unsigned int n) const |
da0634c1 DE |
192 | { |
193 | return wxEmptyString; | |
194 | } | |
195 | ||
aa61d352 | 196 | void wxRadioBox::SetString(unsigned int n, const wxString& label) |
da0634c1 DE |
197 | { |
198 | } | |
199 | ||
200 | // change the individual radio button state | |
aa61d352 | 201 | bool wxRadioBox::Enable(unsigned int n, bool enable) |
da0634c1 | 202 | { |
1a87edf2 WS |
203 | // TODO |
204 | return false; | |
da0634c1 DE |
205 | } |
206 | ||
aa61d352 | 207 | bool wxRadioBox::Show(unsigned int n, bool show) |
da0634c1 | 208 | { |
789f6795 WS |
209 | // TODO |
210 | return false; | |
da0634c1 DE |
211 | } |
212 | ||
9a165f54 DE |
213 | wxSize wxRadioBox::DoGetBestSize() const |
214 | { | |
9ed97552 DE |
215 | // The NSBox responds to sizeToFit by sending sizeToFit to its contentView |
216 | // which is the NSMatrix and does the right thing. | |
217 | return wxControl::DoGetBestSize(); | |
9a165f54 DE |
218 | } |
219 | ||
16c81607 | 220 | #endif |