Partially implement wxRadioBox for wxCocoa.
[wxWidgets.git] / src / cocoa / radiobox.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/radiobox.mm
3 // Purpose:     wxRadioBox
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2003/02/15
7 // RCS-ID:      $Id$
8 // Copyright:   (c) 2003 David Elliott
9 //              (c) 2007 Software 2000 Ltd.
10 // Licence:     wxWidgets licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #include "wx/wxprec.h"
14
15 #if wxUSE_RADIOBOX
16
17 #include "wx/radiobox.h"
18
19 #ifndef WX_PRECOMP
20     #include "wx/app.h"
21     #include "wx/arrstr.h"
22 #endif //WX_PRECOMP
23
24 #include "wx/cocoa/string.h"
25 #include "wx/cocoa/autorelease.h"
26
27 #include "wx/cocoa/objc/NSView.h"
28 #import <AppKit/NSButton.h>
29 #import <AppKit/NSBox.h>
30 #import <AppKit/NSMatrix.h>
31
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
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
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 {
61     // We autorelease heavily so we want our own pool
62     wxAutoNSAutoreleasePool pool;
63
64     if(!CreateControl(parent,winid,pos,size,style,validator,name))
65         return false;
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)]);
140     [m_cocoaNSView release];
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
151     if(m_parent)
152         m_parent->CocoaAddChild(this);
153
154     // Do the sizer dance
155     [GetNSBox() sizeToFit];
156     SetInitialFrameRect(pos, size);
157
158     return true;
159 }
160
161 wxRadioBox::~wxRadioBox()
162 {
163 }
164
165 WX_NSMatrix wxRadioBox::GetNSMatrix() const
166 {
167     return (NSMatrix*)[(NSBox*)m_cocoaNSView contentView];
168 }
169
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
181 unsigned int wxRadioBox::GetCount() const
182 {
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;
189 }
190
191 wxString wxRadioBox::GetString(unsigned int n) const
192 {
193     return wxEmptyString;
194 }
195
196 void wxRadioBox::SetString(unsigned int n, const wxString& label)
197 {
198 }
199
200     // change the individual radio button state
201 bool wxRadioBox::Enable(unsigned int n, bool enable)
202 {
203     // TODO
204     return false;
205 }
206
207 bool wxRadioBox::Show(unsigned int n, bool show)
208 {
209     // TODO
210     return false;
211 }
212
213 wxSize wxRadioBox::DoGetBestSize() const
214 {
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();
218 }
219
220 #endif