1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/mbarman.cpp
3 // Purpose: wxMenuBarManager implementation
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #include "wx/toplevel.h"
21 #include "wx/cocoa/mbarman.h"
22 #include "wx/cocoa/autorelease.h"
24 #import <Foundation/NSString.h>
25 #import <AppKit/NSMenu.h>
26 #import <AppKit/NSApplication.h>
28 // ============================================================================
30 // ============================================================================
31 wxMenuBarManager *wxMenuBarManager::sm_mbarmanInstance = NULL;
33 wxMenuBarManager::wxMenuBarManager()
40 m_mainMenuBarInstalled = true;
45 NSApplication *theNSApplication = wxTheApp->GetNSApplication();
46 // Create the services menu.
47 m_menuServices = [[NSMenu alloc] initWithTitle: @"Services"];
48 [theNSApplication setServicesMenu:m_menuServices];
51 // Create the application (Apple) menu.
52 m_menuApp = [[NSMenu alloc] initWithTitle: @"Apple Menu"];
54 /**/[m_menuApp addItemWithTitle:@"Preferences..." action:nil keyEquivalent:@""];
55 /**/[m_menuApp addItem: [NSMenuItem separatorItem]];
56 /**/menuitem = [[NSMenuItem alloc] initWithTitle: @"Services" action:nil keyEquivalent:@""];
57 [menuitem setSubmenu:m_menuServices];
58 [m_menuApp addItem: menuitem];
60 /**/[m_menuApp addItem: [NSMenuItem separatorItem]];
61 /**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide" action:@selector(hide:) keyEquivalent:@""];
62 [menuitem setTarget: theNSApplication];
63 [m_menuApp addItem: menuitem];
65 /**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@""];
66 [menuitem setTarget: theNSApplication];
67 [m_menuApp addItem: menuitem];
69 /**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
70 [menuitem setTarget: theNSApplication];
71 [m_menuApp addItem: menuitem];
73 /**/[m_menuApp addItem: [NSMenuItem separatorItem]];
74 /**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"Q"];
75 [menuitem setTarget: theNSApplication];
76 [m_menuApp addItem: menuitem];
79 [theNSApplication setAppleMenu:m_menuApp];
81 // Create the Windows menu
82 m_menuWindows = [[NSMenu alloc] initWithTitle: @"Window"];
84 /**/[m_menuWindows addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@""];
85 /**/[m_menuWindows addItem: [NSMenuItem separatorItem]];
86 /**/[m_menuWindows addItemWithTitle:@"Bring All to Front" action:@selector(arrangeInFront:) keyEquivalent:@""];
88 [theNSApplication setWindowsMenu:m_menuWindows];
90 // Create the main menubar
91 m_menuMain = [[NSMenu alloc] initWithTitle: @"wxApp Menu"];
92 /**/NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@"App menu"
93 /* Note: title gets clobbered by app name anyway */
94 action:nil keyEquivalent:@""];
95 [dummyItem setSubmenu:m_menuApp];
96 [m_menuMain addItem:dummyItem];
98 /**/dummyItem = [[NSMenuItem alloc] initWithTitle:@"Window"
99 action:nil keyEquivalent:@""];
100 [dummyItem setSubmenu:m_menuWindows];
101 [m_menuMain addItem:dummyItem];
104 [theNSApplication setMainMenu: m_menuMain];
108 wxMenuBarManager::~wxMenuBarManager()
112 void wxMenuBarManager::CreateInstance()
114 sm_mbarmanInstance = new wxMenuBarManager;
117 void wxMenuBarManager::DestroyInstance()
119 delete sm_mbarmanInstance;
120 sm_mbarmanInstance = NULL;
123 void wxMenuBarManager::CocoaInternalIdle()
129 void wxMenuBarManager::SetMenuBar(wxMenuBar* menubar)
131 m_mainMenuBarInstalled = false;
132 m_needMenuBar = !menubar;
135 [[[wxTheApp->GetNSApplication() mainMenu] itemAtIndex:0] setSubmenu:nil];
136 [[menubar->GetNSMenu() itemAtIndex:0] setSubmenu:m_menuApp];
137 [wxTheApp->GetNSApplication() setMainMenu:menubar->GetNSMenu()];
141 void wxMenuBarManager::SetMainMenuBar(wxMenuBar* menubar)
143 m_mainMenuBar = menubar;
144 if(m_mainMenuBarInstalled)
148 void wxMenuBarManager::InstallMainMenu()
151 SetMenuBar(m_mainMenuBar);
154 m_needMenuBar = false;
155 m_mainMenuBarInstalled = true;
156 [[[wxTheApp->GetNSApplication() mainMenu] itemAtIndex:0] setSubmenu:nil];
157 [[m_menuMain itemAtIndex:0] setSubmenu:m_menuApp];
158 [wxTheApp->GetNSApplication() setMainMenu:m_menuMain];
162 void wxMenuBarManager::WindowDidBecomeKey(wxTopLevelWindowNative *win)
164 // wxASSERT(!m_windowKey);
166 InstallMenuBarForWindow(win);
169 void wxMenuBarManager::WindowDidResignKey(wxTopLevelWindowNative *win)
171 wxASSERT(m_windowKey==win);
176 void wxMenuBarManager::WindowDidBecomeMain(wxTopLevelWindowNative *win)
178 // wxASSERT(!m_windowMain);
182 void wxMenuBarManager::WindowDidResignMain(wxTopLevelWindowNative *win)
184 wxASSERT(m_windowMain==win);
188 void wxMenuBarManager::InstallMenuBarForWindow(wxTopLevelWindowNative *win)
190 wxMenuBar *menubar = NULL;
191 for(wxTopLevelWindowNative *destwin = win;
193 destwin = wxDynamicCast(destwin->GetParent(), wxTopLevelWindow))
195 menubar = destwin->GetAppMenuBar();
200 void wxMenuBarManager::UpdateWindowMenuBar(wxTopLevelWindowNative *win)
202 InstallMenuBarForWindow(m_windowKey);
205 #endif // wxUSE_MENUS