Put wxAutoNSAutoreleasePool in methods that may be used outside the run loop.
[wxWidgets.git] / src / cocoa / menuitem.mm
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        menuitem.cpp
3 // Purpose:     wxMenuItem implementation
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2002/12/15
7 // RCS-ID:      $Id: 
8 // Copyright:   2002 David Elliott
9 // Licence:     wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21 #ifndef WX_PRECOMP
22     #include "wx/menu.h"
23     #include "wx/menuitem.h"
24     #include "wx/utils.h"
25     #include "wx/frame.h"
26 #endif
27
28 #include "wx/cocoa/ObjcPose.h"
29 #include "wx/cocoa/autorelease.h"
30
31 #import <AppKit/NSMenuItem.h>
32 #import <AppKit/NSMenu.h>
33 #import <Foundation/NSString.h>
34
35 #if wxUSE_MENUS
36
37 // ----------------------------------------------------------------------------
38 // globals
39 // ----------------------------------------------------------------------------
40 wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash;
41
42 // ----------------------------------------------------------------------------
43 // functions prototypes
44 // ----------------------------------------------------------------------------
45
46 // ============================================================================
47 // @class wxPoserNSMenuItem
48 // ============================================================================
49 @interface wxPoserNSMenuItem : NSMenuItem
50 {
51 }
52
53 @end // wxPoserNSMenuItem
54
55 WX_IMPLEMENT_POSER(wxPoserNSMenuItem);
56 @implementation wxPoserNSMenuItem : NSMenuItem
57
58 @end // wxPoseRNSMenuItem
59
60 // ============================================================================
61 // wxMenuItemCocoa implementation
62 // ============================================================================
63 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
64
65 // ----------------------------------------------------------------------------
66 // wxMenuItemBase
67 // ----------------------------------------------------------------------------
68
69 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
70                                 int itemid,
71                                 const wxString& name,
72                                 const wxString& help,
73                                 wxItemKind kind,
74                                 wxMenu *subMenu)
75 {
76     return new wxMenuItem(parentMenu, itemid, name, help, kind, subMenu);
77 }
78
79 /* static */
80 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
81 {
82     return wxStripMenuCodes(text);
83 }
84
85 // ----------------------------------------------------------------------------
86 // ctor & dtor
87 // ----------------------------------------------------------------------------
88 wxMenuItemCocoa::wxMenuItemCocoa(wxMenu *pParentMenu,
89                        int itemid,
90                        const wxString& strName,
91                        const wxString& strHelp,
92                        wxItemKind kind,
93                        wxMenu *pSubMenu)
94           : wxMenuItemBase(pParentMenu, itemid, strName, strHelp, kind, pSubMenu)
95 {
96     wxAutoNSAutoreleasePool pool;
97     NSString *menuTitle = [[NSString alloc] initWithCString: wxStripMenuCodes(strName).c_str()];
98     m_cocoaNSMenuItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:@selector(wxMenuItemAction:) keyEquivalent:@""];
99     sm_cocoaHash.insert(wxMenuItemCocoaHash::value_type(m_cocoaNSMenuItem,this));
100     if(pSubMenu)
101     {
102         wxASSERT(pSubMenu->GetNSMenu());
103         [pSubMenu->GetNSMenu() setTitle:menuTitle];
104         [m_cocoaNSMenuItem setSubmenu:pSubMenu->GetNSMenu()];
105     }
106     [menuTitle release];
107 }
108
109 wxMenuItem::~wxMenuItem()
110 {
111     sm_cocoaHash.erase(m_cocoaNSMenuItem);
112     [m_cocoaNSMenuItem release];
113 }
114
115 // ----------------------------------------------------------------------------
116 // misc
117 // ----------------------------------------------------------------------------
118
119 // change item state
120 // -----------------
121
122 void wxMenuItem::Enable(bool bDoEnable)
123 {
124     wxMenuItemBase::Enable(bDoEnable);
125 }
126
127 void wxMenuItem::Check(bool bDoCheck)
128 {
129     wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
130     wxMenuItemBase::Check(bDoCheck);
131 }
132
133 void wxMenuItem::SetText(const wxString& label)
134 {
135     wxMenuItemBase::SetText(label);
136 }
137
138 void wxMenuItem::SetCheckable(bool checkable)
139 {
140     wxMenuItemBase::SetCheckable(checkable);
141 }
142
143 #endif // wxUSE_MENUS