]>
Commit | Line | Data |
---|---|---|
fb896a32 DE |
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" | |
2fc2d511 | 26 | #include "wx/log.h" |
fb896a32 DE |
27 | #endif |
28 | ||
29 | #include "wx/cocoa/ObjcPose.h" | |
7fc77f30 | 30 | #include "wx/cocoa/autorelease.h" |
fb896a32 DE |
31 | |
32 | #import <AppKit/NSMenuItem.h> | |
33 | #import <AppKit/NSMenu.h> | |
34 | #import <Foundation/NSString.h> | |
35 | ||
36 | #if wxUSE_MENUS | |
37 | ||
fb896a32 DE |
38 | // ---------------------------------------------------------------------------- |
39 | // functions prototypes | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
2fc2d511 DE |
42 | // ============================================================================ |
43 | // @class wxNSMenuItemTarget | |
44 | // ============================================================================ | |
45 | @interface wxNSMenuItemTarget : NSObject | |
46 | { | |
47 | } | |
48 | ||
49 | - (void)wxMenuItemAction: (id)sender; | |
42036ca8 | 50 | - (BOOL)validateMenuItem: (id)menuItem; |
2fc2d511 DE |
51 | @end //interface wxNSMenuItemTarget |
52 | ||
53 | @implementation wxNSMenuItemTarget : NSObject | |
54 | ||
55 | - (void)wxMenuItemAction: (id)sender | |
56 | { | |
57 | wxLogDebug("wxMenuItemAction"); | |
58 | wxMenuItem *item = wxMenuItem::GetFromCocoa(sender); | |
59 | wxCHECK_RET(item,"wxMenuItemAction received but no wxMenuItem exists!"); | |
60 | ||
61 | wxMenu *menu = item->GetMenu(); | |
62 | wxCHECK_RET(menu,"wxMenuItemAction received but wxMenuItem is not in a wxMenu"); | |
63 | wxMenuBar *menubar = menu->GetMenuBar(); | |
64 | if(menubar) | |
65 | { | |
66 | wxFrame *frame = menubar->GetFrame(); | |
67 | wxCHECK_RET(frame, "wxMenuBar MUST be attached to a wxFrame!"); | |
68 | frame->Command(item->GetId()); | |
69 | } | |
70 | } | |
71 | ||
42036ca8 DE |
72 | - (BOOL)validateMenuItem: (id)menuItem |
73 | { | |
74 | // TODO: Do wxWindows validation here and avoid sending during idle time | |
75 | wxLogDebug("wxMenuItemAction"); | |
76 | wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem); | |
77 | wxCHECK_MSG(item,NO,"validateMenuItem received but no wxMenuItem exists!"); | |
78 | return item->IsEnabled(); | |
79 | } | |
80 | ||
2fc2d511 DE |
81 | @end //implementation wxNSMenuItemTarget |
82 | ||
fb896a32 DE |
83 | // ============================================================================ |
84 | // @class wxPoserNSMenuItem | |
85 | // ============================================================================ | |
86 | @interface wxPoserNSMenuItem : NSMenuItem | |
87 | { | |
88 | } | |
89 | ||
90 | @end // wxPoserNSMenuItem | |
91 | ||
92 | WX_IMPLEMENT_POSER(wxPoserNSMenuItem); | |
93 | @implementation wxPoserNSMenuItem : NSMenuItem | |
94 | ||
95 | @end // wxPoseRNSMenuItem | |
96 | ||
97 | // ============================================================================ | |
98 | // wxMenuItemCocoa implementation | |
99 | // ============================================================================ | |
100 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject) | |
2fc2d511 DE |
101 | wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash; |
102 | ||
103 | struct objc_object *wxMenuItemCocoa::sm_cocoaTarget = [[wxNSMenuItemTarget alloc] init]; | |
fb896a32 DE |
104 | |
105 | // ---------------------------------------------------------------------------- | |
106 | // wxMenuItemBase | |
107 | // ---------------------------------------------------------------------------- | |
108 | ||
109 | wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu, | |
110 | int itemid, | |
111 | const wxString& name, | |
112 | const wxString& help, | |
113 | wxItemKind kind, | |
114 | wxMenu *subMenu) | |
115 | { | |
116 | return new wxMenuItem(parentMenu, itemid, name, help, kind, subMenu); | |
117 | } | |
118 | ||
119 | /* static */ | |
120 | wxString wxMenuItemBase::GetLabelFromText(const wxString& text) | |
121 | { | |
122 | return wxStripMenuCodes(text); | |
123 | } | |
124 | ||
125 | // ---------------------------------------------------------------------------- | |
126 | // ctor & dtor | |
127 | // ---------------------------------------------------------------------------- | |
128 | wxMenuItemCocoa::wxMenuItemCocoa(wxMenu *pParentMenu, | |
129 | int itemid, | |
130 | const wxString& strName, | |
131 | const wxString& strHelp, | |
132 | wxItemKind kind, | |
133 | wxMenu *pSubMenu) | |
134 | : wxMenuItemBase(pParentMenu, itemid, strName, strHelp, kind, pSubMenu) | |
135 | { | |
7fc77f30 | 136 | wxAutoNSAutoreleasePool pool; |
fb896a32 DE |
137 | NSString *menuTitle = [[NSString alloc] initWithCString: wxStripMenuCodes(strName).c_str()]; |
138 | m_cocoaNSMenuItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:@selector(wxMenuItemAction:) keyEquivalent:@""]; | |
139 | sm_cocoaHash.insert(wxMenuItemCocoaHash::value_type(m_cocoaNSMenuItem,this)); | |
2fc2d511 | 140 | [m_cocoaNSMenuItem setTarget:sm_cocoaTarget]; |
fb896a32 DE |
141 | if(pSubMenu) |
142 | { | |
143 | wxASSERT(pSubMenu->GetNSMenu()); | |
144 | [pSubMenu->GetNSMenu() setTitle:menuTitle]; | |
145 | [m_cocoaNSMenuItem setSubmenu:pSubMenu->GetNSMenu()]; | |
146 | } | |
147 | [menuTitle release]; | |
148 | } | |
149 | ||
150 | wxMenuItem::~wxMenuItem() | |
151 | { | |
152 | sm_cocoaHash.erase(m_cocoaNSMenuItem); | |
153 | [m_cocoaNSMenuItem release]; | |
154 | } | |
155 | ||
156 | // ---------------------------------------------------------------------------- | |
157 | // misc | |
158 | // ---------------------------------------------------------------------------- | |
159 | ||
160 | // change item state | |
161 | // ----------------- | |
162 | ||
163 | void wxMenuItem::Enable(bool bDoEnable) | |
164 | { | |
165 | wxMenuItemBase::Enable(bDoEnable); | |
166 | } | |
167 | ||
168 | void wxMenuItem::Check(bool bDoCheck) | |
169 | { | |
170 | wxCHECK_RET( IsCheckable(), "only checkable items may be checked" ); | |
171 | wxMenuItemBase::Check(bDoCheck); | |
172 | } | |
173 | ||
174 | void wxMenuItem::SetText(const wxString& label) | |
175 | { | |
176 | wxMenuItemBase::SetText(label); | |
177 | } | |
178 | ||
179 | void wxMenuItem::SetCheckable(bool checkable) | |
180 | { | |
181 | wxMenuItemBase::SetCheckable(checkable); | |
182 | } | |
183 | ||
184 | #endif // wxUSE_MENUS |