]>
Commit | Line | Data |
---|---|---|
fb896a32 DE |
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 | ||
7fc77f30 DE |
26 | #include "wx/cocoa/autorelease.h" |
27 | ||
fb896a32 DE |
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 | { | |
605c7e7e DE |
45 | wxAutoNSAutoreleasePool pool; |
46 | m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: [NSString stringWithCString: title.c_str()]]; | |
47 | return true; | |
fb896a32 DE |
48 | } |
49 | ||
50 | wxMenu::~wxMenu() | |
51 | { | |
605c7e7e | 52 | [m_cocoaNSMenu release]; |
fb896a32 DE |
53 | } |
54 | ||
55 | bool wxMenu::DoAppend(wxMenuItem *item) | |
56 | { | |
7fc77f30 | 57 | wxAutoNSAutoreleasePool pool; |
fb896a32 DE |
58 | if(!wxMenuBase::DoAppend(item)) |
59 | return false; | |
60 | [m_cocoaNSMenu addItem: item->GetNSMenuItem()]; | |
61 | return true; | |
62 | } | |
63 | ||
64 | bool wxMenu::DoInsert(unsigned long pos, wxMenuItem *item) | |
65 | { | |
7fc77f30 | 66 | wxAutoNSAutoreleasePool pool; |
fb896a32 DE |
67 | if(!wxMenuBase::DoInsert(pos,item)) |
68 | return false; | |
69 | [m_cocoaNSMenu insertItem:item->GetNSMenuItem() atIndex:pos]; | |
70 | return true; | |
71 | } | |
72 | ||
73 | wxMenuItem* wxMenu::DoRemove(wxMenuItem *item) | |
74 | { | |
7fc77f30 | 75 | wxAutoNSAutoreleasePool pool; |
fb896a32 DE |
76 | wxMenuItem *retitem = wxMenuBase::DoRemove(item); |
77 | wxASSERT(retitem->GetNSMenuItem()); | |
78 | [m_cocoaNSMenu removeItem:retitem->GetNSMenuItem()]; | |
79 | return retitem; | |
80 | } | |
81 | ||
82 | // ============================================================================ | |
83 | // wxMenuBar implementation | |
84 | // ============================================================================ | |
85 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow) | |
86 | ||
87 | bool wxMenuBar::Create(long style) | |
88 | { | |
605c7e7e DE |
89 | wxAutoNSAutoreleasePool pool; |
90 | m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: @"wxMenuBar"]; | |
91 | ||
97351e17 DE |
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]; | |
fb896a32 DE |
97 | return true; |
98 | } | |
99 | ||
100 | wxMenuBar::~wxMenuBar() | |
101 | { | |
605c7e7e | 102 | [m_cocoaNSMenu release]; |
fb896a32 DE |
103 | } |
104 | ||
105 | bool wxMenuBar::Append( wxMenu *menu, const wxString &title ) | |
106 | { | |
7fc77f30 | 107 | wxAutoNSAutoreleasePool pool; |
2b030203 | 108 | wxLogDebug(wxT("append menu=%p, title=%s"),menu,title.c_str()); |
fb896a32 DE |
109 | if(!wxMenuBarBase::Append(menu,title)) |
110 | return false; | |
111 | wxASSERT(menu); | |
112 | wxASSERT(menu->GetNSMenu()); | |
113 | NSString *menuTitle = [[NSString alloc] initWithCString: wxStripMenuCodes(title).c_str()]; | |
114 | NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""]; | |
115 | [menu->GetNSMenu() setTitle:menuTitle]; | |
116 | [newItem setSubmenu:menu->GetNSMenu()]; | |
117 | ||
118 | [m_cocoaNSMenu addItem:newItem]; | |
119 | ||
120 | [menuTitle release]; | |
121 | [newItem release]; | |
122 | return true; | |
123 | } | |
124 | ||
125 | bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) | |
126 | { | |
7fc77f30 | 127 | wxAutoNSAutoreleasePool pool; |
2b030203 | 128 | wxLogDebug(wxT("insert pos=%lu, menu=%p, title=%s"),pos,menu,title.c_str()); |
97351e17 DE |
129 | // Get the current menu at this position |
130 | wxMenu *nextmenu = GetMenu(pos); | |
fb896a32 DE |
131 | if(!wxMenuBarBase::Insert(pos,menu,title)) |
132 | return false; | |
133 | wxASSERT(menu); | |
134 | wxASSERT(menu->GetNSMenu()); | |
135 | NSString *menuTitle = [[NSString alloc] initWithCString: title.c_str()]; | |
136 | NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""]; | |
137 | [menu->GetNSMenu() setTitle:menuTitle]; | |
138 | [newItem setSubmenu:menu->GetNSMenu()]; | |
139 | ||
97351e17 DE |
140 | int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:nextmenu->GetNSMenu()]; |
141 | wxASSERT(itemindex>=0); | |
142 | [m_cocoaNSMenu insertItem:newItem atIndex:itemindex]; | |
fb896a32 DE |
143 | |
144 | [menuTitle release]; | |
145 | [newItem release]; | |
146 | return true; | |
147 | } | |
148 | ||
149 | wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) | |
150 | { | |
151 | return NULL; | |
152 | } | |
153 | ||
154 | wxMenu *wxMenuBar::Remove(size_t pos) | |
155 | { | |
97351e17 DE |
156 | wxMenu *menu = wxMenuBarBase::Remove(pos); |
157 | wxASSERT(menu); | |
158 | int itemindex = [GetNSMenu() indexOfItemWithSubmenu:menu->GetNSMenu()]; | |
159 | wxASSERT(itemindex>=0); | |
160 | [m_cocoaNSMenu removeItemAtIndex:itemindex]; | |
161 | return menu; | |
fb896a32 DE |
162 | } |
163 | ||
164 | ||
165 | void wxMenuBar::EnableTop(size_t pos, bool enable) | |
166 | { | |
167 | } | |
168 | ||
169 | bool wxMenuBar::IsEnabledTop(size_t pos) const | |
170 | { | |
171 | return false; | |
172 | } | |
173 | ||
174 | void wxMenuBar::SetLabelTop(size_t pos, const wxString& label) | |
175 | { | |
176 | } | |
177 | ||
178 | wxString wxMenuBar::GetLabelTop(size_t pos) const | |
179 | { | |
97351e17 DE |
180 | wxMenu *menu = GetMenu(pos); |
181 | int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:menu->GetNSMenu()]; | |
182 | wxASSERT(itemindex>=0); | |
183 | return wxString([[[m_cocoaNSMenu itemAtIndex:itemindex] title] lossyCString]); | |
fb896a32 DE |
184 | } |
185 | ||
186 | void wxMenuBar::Attach(wxFrame *frame) | |
187 | { | |
2fc2d511 | 188 | wxMenuBarBase::Attach(frame); |
fb896a32 DE |
189 | } |
190 | ||
191 | void wxMenuBar::Detach() | |
192 | { | |
2fc2d511 | 193 | wxMenuBarBase::Detach(); |
fb896a32 DE |
194 | } |
195 | ||
196 | wxSize wxMenuBar::DoGetBestClientSize() const | |
197 | { | |
198 | return wxDefaultSize; | |
199 | } | |
200 | ||
201 | #endif // wxUSE_MENUS |