]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/menu.mm
wxMenuBar:
[wxWidgets.git] / src / cocoa / menu.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/menu.cpp
3 // Purpose: wxMenu and wxMenuBar implementation
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2002/12/09
7 // RCS-ID: $Id:
8 // Copyright: (c) 2002 David Elliott
9 // Licence: wxWindows license
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/log.h"
24 #endif // WX_PRECOMP
25
26 #include "wx/cocoa/autorelease.h"
27
28 #import <Foundation/NSString.h>
29 #import <AppKit/NSMenu.h>
30
31 #if wxUSE_MENUS
32
33 // ----------------------------------------------------------------------------
34 // globals
35 // ----------------------------------------------------------------------------
36
37 // ============================================================================
38 // wxMenu implementation
39 // ============================================================================
40
41 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
42
43 bool wxMenu::Create(const wxString& title, long style)
44 {
45 #if 0
46 if(!title)
47 return CocoaCreate("wxMenu");
48 #endif
49 return CocoaCreate(title);
50 }
51
52 wxMenu::~wxMenu()
53 {
54 }
55
56 bool wxMenu::DoAppend(wxMenuItem *item)
57 {
58 wxAutoNSAutoreleasePool pool;
59 if(!wxMenuBase::DoAppend(item))
60 return false;
61 [m_cocoaNSMenu addItem: item->GetNSMenuItem()];
62 return true;
63 }
64
65 bool wxMenu::DoInsert(unsigned long pos, wxMenuItem *item)
66 {
67 wxAutoNSAutoreleasePool pool;
68 if(!wxMenuBase::DoInsert(pos,item))
69 return false;
70 [m_cocoaNSMenu insertItem:item->GetNSMenuItem() atIndex:pos];
71 return true;
72 }
73
74 wxMenuItem* wxMenu::DoRemove(wxMenuItem *item)
75 {
76 wxAutoNSAutoreleasePool pool;
77 wxMenuItem *retitem = wxMenuBase::DoRemove(item);
78 wxASSERT(retitem->GetNSMenuItem());
79 [m_cocoaNSMenu removeItem:retitem->GetNSMenuItem()];
80 return retitem;
81 }
82
83 // ============================================================================
84 // wxMenuBar implementation
85 // ============================================================================
86 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
87
88 bool wxMenuBar::Create(long style)
89 {
90 if(!CocoaCreate("wxMenuBar"))
91 return false;
92 NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@"App menu"
93 /* Note: title gets clobbered by app name anyway */
94 action:nil keyEquivalent:@""];
95 [m_cocoaNSMenu addItem:dummyItem];
96 [dummyItem release];
97 return true;
98 }
99
100 wxMenuBar::~wxMenuBar()
101 {
102 }
103
104 bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
105 {
106 wxAutoNSAutoreleasePool pool;
107 wxLogDebug("append menu=%p, title=%s",menu,title.c_str());
108 if(!wxMenuBarBase::Append(menu,title))
109 return false;
110 wxASSERT(menu);
111 wxASSERT(menu->GetNSMenu());
112 NSString *menuTitle = [[NSString alloc] initWithCString: wxStripMenuCodes(title).c_str()];
113 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
114 [menu->GetNSMenu() setTitle:menuTitle];
115 [newItem setSubmenu:menu->GetNSMenu()];
116
117 [m_cocoaNSMenu addItem:newItem];
118
119 [menuTitle release];
120 [newItem release];
121 return true;
122 }
123
124 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
125 {
126 wxAutoNSAutoreleasePool pool;
127 wxLogDebug("insert pos=%lu, menu=%p, title=%s",pos,menu,title.c_str());
128 // Get the current menu at this position
129 wxMenu *nextmenu = GetMenu(pos);
130 if(!wxMenuBarBase::Insert(pos,menu,title))
131 return false;
132 wxASSERT(menu);
133 wxASSERT(menu->GetNSMenu());
134 NSString *menuTitle = [[NSString alloc] initWithCString: title.c_str()];
135 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
136 [menu->GetNSMenu() setTitle:menuTitle];
137 [newItem setSubmenu:menu->GetNSMenu()];
138
139 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:nextmenu->GetNSMenu()];
140 wxASSERT(itemindex>=0);
141 [m_cocoaNSMenu insertItem:newItem atIndex:itemindex];
142
143 [menuTitle release];
144 [newItem release];
145 return true;
146 }
147
148 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
149 {
150 return NULL;
151 }
152
153 wxMenu *wxMenuBar::Remove(size_t pos)
154 {
155 wxMenu *menu = wxMenuBarBase::Remove(pos);
156 wxASSERT(menu);
157 int itemindex = [GetNSMenu() indexOfItemWithSubmenu:menu->GetNSMenu()];
158 wxASSERT(itemindex>=0);
159 [m_cocoaNSMenu removeItemAtIndex:itemindex];
160 return menu;
161 }
162
163
164 void wxMenuBar::EnableTop(size_t pos, bool enable)
165 {
166 }
167
168 bool wxMenuBar::IsEnabledTop(size_t pos) const
169 {
170 return false;
171 }
172
173 void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
174 {
175 }
176
177 wxString wxMenuBar::GetLabelTop(size_t pos) const
178 {
179 wxMenu *menu = GetMenu(pos);
180 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:menu->GetNSMenu()];
181 wxASSERT(itemindex>=0);
182 return wxString([[[m_cocoaNSMenu itemAtIndex:itemindex] title] lossyCString]);
183 }
184
185 void wxMenuBar::Attach(wxFrame *frame)
186 {
187 wxMenuBarBase::Attach(frame);
188 }
189
190 void wxMenuBar::Detach()
191 {
192 wxMenuBarBase::Detach();
193 }
194
195 wxSize wxMenuBar::DoGetBestClientSize() const
196 {
197 return wxDefaultSize;
198 }
199
200 #endif // wxUSE_MENUS