X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/584ad2a32fec156c6049145d7ece9a33213aea28..b74077ace2413000c5556cbe0c41ab210ac12c54:/src/cocoa/radiobox.mm diff --git a/src/cocoa/radiobox.mm b/src/cocoa/radiobox.mm index 216038e63d..6ce3aa2dd3 100644 --- a/src/cocoa/radiobox.mm +++ b/src/cocoa/radiobox.mm @@ -1,27 +1,55 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: cocoa/radiobox.mm +// Name: src/cocoa/radiobox.mm // Purpose: wxRadioBox // Author: David Elliott // Modified by: // Created: 2003/02/15 -// RCS-ID: $Id: +// RCS-ID: $Id$ // Copyright: (c) 2003 David Elliott -// Licence: wxWindows license +// (c) 2007 Software 2000 Ltd. +// Licence: wxWidgets licence ///////////////////////////////////////////////////////////////////////////// #include "wx/wxprec.h" + +#if wxUSE_RADIOBOX + +#include "wx/radiobox.h" + #ifndef WX_PRECOMP #include "wx/app.h" - #include "wx/radiobox.h" #include "wx/arrstr.h" #endif //WX_PRECOMP -#import +#include "wx/cocoa/string.h" +#include "wx/cocoa/autorelease.h" + +#import +#include "wx/cocoa/objc/NSView.h" +#import +#import +#import IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl) BEGIN_EVENT_TABLE(wxRadioBox, wxControl) END_EVENT_TABLE() -// WX_IMPLEMENT_COCOA_OWNER(wxRadioBox,NSTextField,NSControl,NSView) + +void wxRadioBox::AssociateNSBox(WX_NSBox cocoaObjcClass) +{ + NSMatrix *radioBox = [(WX_NSBox)cocoaObjcClass contentView]; + // Associate the NSMatrix (the NSBox's contentView) with the wxCocoaNSControl MI base class. + AssociateNSControl(radioBox); + // Set the target/action.. we don't really need to unset these + [radioBox setTarget:wxCocoaNSControl::sm_cocoaTarget]; + [radioBox setAction:@selector(wxNSControlAction:)]; +} + +void wxRadioBox::DisassociateNSBox(WX_NSBox cocoaObjcClass) +{ + DisassociateNSControl([(WX_NSBox)cocoaObjcClass contentView]); +} + +WX_IMPLEMENT_COCOA_OWNER(wxRadioBox,NSBox,NSView,NSView) bool wxRadioBox::Create(wxWindow *parent, wxWindowID winid, const wxString& title, @@ -47,63 +75,203 @@ bool wxRadioBox::Create(wxWindow *parent, wxWindowID winid, long style, const wxValidator& validator, const wxString& name) { + // We autorelease heavily so we want our own pool + wxAutoNSAutoreleasePool pool; + if(!CreateControl(parent,winid,pos,size,style,validator,name)) return false; - SetNSView([[NSView alloc] initWithFrame: MakeDefaultNSRect(size)]); - [m_cocoaNSView release]; + + majorDim = majorDim == 0 ? n : majorDim; + // TODO: Don't forget to call SetMajorDim + // We can't yet as we can't implement GetCount() until after + // we make the NSMatrix. + int minorDim = (n + majorDim - 1) / majorDim; + + + // Create a prototype cell for use with the NSMatrix build + NSCell *currCell = [[NSButtonCell alloc] initTextCell:@""]; + [(NSButtonCell*)currCell setButtonType:NSRadioButton]; + + // Build up an array of all cells plus any extra empty cells + NSMutableArray *allCells = [NSMutableArray arrayWithCapacity:n]; + for(int i=0; i 0) + SetSelection(0); + if(m_parent) m_parent->CocoaAddChild(this); - SetInitialFrameRect(pos,size); + + // Do the sizer dance + [GetNSBox() sizeToFit]; + SetInitialFrameRect(pos, size); return true; } wxRadioBox::~wxRadioBox() { + DisassociateNSBox(GetNSBox()); +} + +WX_NSMatrix wxRadioBox::GetNSMatrix() const +{ + return (NSMatrix*)[(NSBox*)m_cocoaNSView contentView]; } // selection void wxRadioBox::SetSelection(int n) { + int r = GetRowForIndex(n); + int c = GetColumnForIndex(n); + [GetNSMatrix() selectCellAtRow:r column:c]; } int wxRadioBox::GetSelection() const { - return 0; + NSMatrix *radioBox = GetNSMatrix(); + NSInteger r = [radioBox selectedRow]; + NSInteger c = [radioBox selectedColumn]; + if(m_windowStyle & wxRA_SPECIFY_COLS) + return r * GetMajorDim() + c; + else + return c * GetMajorDim() + r; } // string access -int wxRadioBox::GetCount() const +unsigned int wxRadioBox::GetCount() const { - return 0; + NSMatrix *radioBox = GetNSMatrix(); + NSInteger rowCount, columnCount; + [radioBox getNumberOfRows:&rowCount columns:&columnCount]; + + // FIXME: This is wrong if padding cells were made + return rowCount * columnCount; } -wxString wxRadioBox::GetString(int n) const +wxString wxRadioBox::GetString(unsigned int n) const { - return wxEmptyString; + int r = GetRowForIndex(n); + int c = GetColumnForIndex(n); + // FIXME: Cocoa stores the mnemonic-stripped title. + return wxStringWithNSString([[GetNSMatrix() cellAtRow:r column:c] title]); } -void wxRadioBox::SetString(int n, const wxString& label) +void wxRadioBox::SetString(unsigned int n, const wxString& label) { + int r = GetRowForIndex(n); + int c = GetColumnForIndex(n); + CocoaSetLabelForObject(label, [GetNSMatrix() cellAtRow:r column:c]); } // change the individual radio button state -void wxRadioBox::Enable(int n, bool enable) +bool wxRadioBox::Enable(unsigned int n, bool enable) { + int r = GetRowForIndex(n); + int c = GetColumnForIndex(n); + NSCell *cell = [GetNSMatrix() cellAtRow:r column:c]; + if(cell == nil) + return false; + bool wasEnabled = [cell isEnabled]; + [cell setEnabled:enable]; + return (wasEnabled && !enable) || (!wasEnabled && enable); } -void wxRadioBox::Show(int n, bool show) +bool wxRadioBox::Show(unsigned int n, bool show) { + // TODO + // NOTE: Cocoa has no visible state for cells so we'd need to replace the + // cell with a dummy one to hide it or alternatively subclass NSButtonCell + // and add the behavior. + return false; } - // layout parameters -int wxRadioBox::GetColumnCount() const +wxSize wxRadioBox::DoGetBestSize() const { - return 0; + // The NSBox responds to sizeToFit by sending sizeToFit to its contentView + // which is the NSMatrix and does the right thing. + return wxControl::DoGetBestSize(); } -int wxRadioBox::GetRowCount() const +void wxRadioBox::CocoaTarget_action(void) { - return 0; + wxCommandEvent event(wxEVT_COMMAND_RADIOBOX_SELECTED, GetId()); + InitCommandEvent(event); + event.SetInt(GetSelection()); // i.e. SetSelection. + Command(event); } +#endif