]> git.saurik.com Git - wxWidgets.git/blobdiff - src/cocoa/radiobox.mm
ignore WinCE projects and build directories
[wxWidgets.git] / src / cocoa / radiobox.mm
index 2eacb188f99482a17e67a5f9db3ddf52df16fa5c..6ce3aa2dd366e8a427808cef97ed03aa7d7c65e6 100644 (file)
@@ -24,6 +24,7 @@
 #include "wx/cocoa/string.h"
 #include "wx/cocoa/autorelease.h"
 
+#import <Foundation/NSArray.h>
 #include "wx/cocoa/objc/NSView.h"
 #import <AppKit/NSButton.h>
 #import <AppKit/NSBox.h>
 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,
@@ -79,7 +96,7 @@ bool wxRadioBox::Create(wxWindow *parent, wxWindowID winid,
     NSMutableArray *allCells = [NSMutableArray arrayWithCapacity:n];
     for(int i=0; i<n; ++i)
     {
-        [currCell setTitle: wxNSStringWithWxString(wxStripMenuCodes(choices[i], wxStrip_Mnemonics))];
+        CocoaSetLabelForObject(choices[i], currCell);
         [allCells addObject: currCell];
         [currCell release];
         // NOTE: We can still safely message currCell as the array has retained it.
@@ -134,19 +151,27 @@ bool wxRadioBox::Create(wxWindow *parent, wxWindowID winid,
             withObject:[allCells subarrayWithRange:NSMakeRange(i*majorDim, majorDim)]];
     }
 
-    //make and set up an NSBox (TODO: Just derive from wxStaticBox)
-    SetNSView([[NSBox alloc] initWithFrame:MakeDefaultNSRect(size)]);
-    [m_cocoaNSView release];
+    NSBox *theBox = [[NSBox alloc] initWithFrame:MakeDefaultNSRect(size)];
 
     // Replace the box's content view with the NSMatrix we just created
-    [GetNSBox() setContentView:radioBox];
+    // IMPORTANT: This must be done before calling SetNSBox.
+    [theBox setContentView:radioBox];
     [radioBox release]; // The NSBox retains it for us.
 
-    [GetNSBox() setTitle:wxNSStringWithWxString(wxStripMenuCodes(title, wxStrip_Mnemonics))];
+    SetNSBox(theBox);
+    [theBox release];
+
+
+    CocoaSetLabelForObject(title, GetNSBox());
 //    [GetNSBox() setBorderType:NSLineBorder]; // why??
 
     SetMajorDim(majorDim, style);
 
+    // Set the selection to the first item if we have any items.
+    // This is for parity with other wx ports which do the same thing.
+    if(n > 0)
+        SetSelection(0);
+
     if(m_parent)
         m_parent->CocoaAddChild(this);
 
@@ -159,6 +184,7 @@ bool wxRadioBox::Create(wxWindow *parent, wxWindowID winid,
 
 wxRadioBox::~wxRadioBox()
 {
+    DisassociateNSBox(GetNSBox());
 }
 
 WX_NSMatrix wxRadioBox::GetNSMatrix() const
@@ -208,7 +234,7 @@ void wxRadioBox::SetString(unsigned int n, const wxString& label)
 {
     int r = GetRowForIndex(n);
     int c = GetColumnForIndex(n);
-    [[GetNSMatrix() cellAtRow:r column:c] setTitle:wxNSStringWithWxString(wxStripMenuCodes(label, wxStrip_Mnemonics))];
+    CocoaSetLabelForObject(label, [GetNSMatrix() cellAtRow:r column:c]);
 }
 
     // change the individual radio button state
@@ -240,4 +266,12 @@ wxSize wxRadioBox::DoGetBestSize() const
     return wxControl::DoGetBestSize();
 }
 
+void wxRadioBox::CocoaTarget_action(void)
+{
+    wxCommandEvent event(wxEVT_COMMAND_RADIOBOX_SELECTED, GetId());
+    InitCommandEvent(event);
+    event.SetInt(GetSelection()); // i.e. SetSelection.
+    Command(event);
+}
+
 #endif