1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/radiobox.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // (c) 2007 Software 2000 Ltd.
10 // Licence: wxWidgets licence
11 /////////////////////////////////////////////////////////////////////////////
13 #include "wx/wxprec.h"
17 #include "wx/radiobox.h"
21 #include "wx/arrstr.h"
24 #include "wx/cocoa/string.h"
25 #include "wx/cocoa/autorelease.h"
27 #include "wx/cocoa/objc/NSView.h"
28 #import <AppKit/NSButton.h>
29 #import <AppKit/NSBox.h>
30 #import <AppKit/NSMatrix.h>
32 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
33 BEGIN_EVENT_TABLE(wxRadioBox, wxControl)
35 // WX_IMPLEMENT_COCOA_OWNER(wxRadioBox,NSTextField,NSControl,NSView)
37 bool wxRadioBox::Create(wxWindow *parent, wxWindowID winid,
38 const wxString& title,
41 const wxArrayString& choices,
43 long style, const wxValidator& validator,
46 wxCArrayString chs(choices);
48 return Create(parent, winid, title, pos, size, chs.GetCount(),
49 chs.GetStrings(), majorDim, style, validator, name);
52 bool wxRadioBox::Create(wxWindow *parent, wxWindowID winid,
53 const wxString& title,
56 int n, const wxString choices[],
58 long style, const wxValidator& validator,
61 // We autorelease heavily so we want our own pool
62 wxAutoNSAutoreleasePool pool;
64 if(!CreateControl(parent,winid,pos,size,style,validator,name))
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;
74 // Create a prototype cell for use with the NSMatrix build
75 NSCell *currCell = [[NSButtonCell alloc] initTextCell:@""];
76 [(NSButtonCell*)currCell setButtonType:NSRadioButton];
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)
82 [currCell setTitle: wxNSStringWithWxString(choices[i])];
83 [allCells addObject: currCell];
85 // NOTE: We can still safely message currCell as the array has retained it.
86 currCell = [currCell copy];
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)
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.
103 currCell = [currCell copy];
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
119 numberOfRows:style&wxRA_SPECIFY_COLS?0:majorDim
120 numberOfColumns:style&wxRA_SPECIFY_COLS?majorDim:0
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
129 addMajorWithCellsSelector = @selector(addColumnWithCells:);
131 for(int i=0; i<minorDim; ++i)
134 performSelector:addMajorWithCellsSelector
135 withObject:[allCells subarrayWithRange:NSMakeRange(i*majorDim, majorDim)]];
138 //make and set up an NSBox (TODO: Just derive from wxStaticBox)
139 SetNSView([[NSBox alloc] initWithFrame:MakeDefaultNSRect(size)]);
140 [m_cocoaNSView release];
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.
146 [GetNSBox() setTitle:wxNSStringWithWxString(title)];
147 // [GetNSBox() setBorderType:NSLineBorder]; // why??
149 SetMajorDim(majorDim, style);
152 m_parent->CocoaAddChild(this);
154 // Do the sizer dance
155 [GetNSBox() sizeToFit];
156 SetInitialFrameRect(pos, size);
161 wxRadioBox::~wxRadioBox()
165 WX_NSMatrix wxRadioBox::GetNSMatrix() const
167 return (NSMatrix*)[(NSBox*)m_cocoaNSView contentView];
171 void wxRadioBox::SetSelection(int n)
175 int wxRadioBox::GetSelection() const
181 unsigned int wxRadioBox::GetCount() const
183 NSMatrix *radioBox = GetNSMatrix();
184 NSInteger rowCount, columnCount;
185 [radioBox getNumberOfRows:&rowCount columns:&columnCount];
187 // FIXME: This is wrong if padding cells were made
188 return rowCount * columnCount;
191 wxString wxRadioBox::GetString(unsigned int n) const
193 return wxEmptyString;
196 void wxRadioBox::SetString(unsigned int n, const wxString& label)
200 // change the individual radio button state
201 bool wxRadioBox::Enable(unsigned int n, bool enable)
207 bool wxRadioBox::Show(unsigned int n, bool show)
213 wxSize wxRadioBox::DoGetBestSize() const
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();