DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
src/gui/menu_osx.mm
00001 /* Mac OS X portion of menu.cpp */
00002 
00003 #include "config.h"
00004 #include "menu.h"
00005 
00006 #if DOSBOXMENU_TYPE == DOSBOXMENU_NSMENU /* Mac OS X NSMenu / NSMenuItem handle */
00007 # include <MacTypes.h>
00008 # include <Cocoa/Cocoa.h>
00009 # include <Foundation/NSString.h>
00010 
00011 @interface NSApplication (DOSBoxX)
00012 @end
00013 
00014 extern "C" void* sdl1_hax_stock_osx_menu(void);
00015 
00016 void *sdl_hax_nsMenuItemFromTag(void *nsMenu, unsigned int tag) {
00017         NSMenuItem *ns_item = [((NSMenu*)nsMenu) itemWithTag: tag];
00018         return (ns_item != nil) ? ns_item : NULL;
00019 }
00020 
00021 void sdl_hax_nsMenuItemUpdateFromItem(void *nsMenuItem, DOSBoxMenu::item &item) {
00022         if (item.has_changed()) {
00023                 NSMenuItem *ns_item = (NSMenuItem*)nsMenuItem;
00024 
00025                 [ns_item setEnabled:(item.is_enabled() ? YES : NO)];
00026                 [ns_item setState:(item.is_checked() ? NSOnState : NSOffState)];
00027 
00028                 const std::string &it = item.get_text();
00029                 const std::string &st = item.get_shortcut_text();
00030                 std::string ft = it;
00031 
00032                 /* TODO: Figure out how to put the shortcut text right-aligned while leaving the main text left-aligned */
00033                 if (!st.empty()) {
00034                         ft += " [";
00035                         ft += st;
00036                         ft += "]";
00037                 }
00038 
00039                 {
00040                         NSString *title = [[NSString alloc] initWithUTF8String:ft.c_str()];
00041                         [ns_item setTitle:title];
00042                         [title release];
00043                 }
00044 
00045                 item.clear_changed();
00046         }
00047 }
00048 
00049 void* sdl_hax_nsMenuAlloc(const char *initWithText) {
00050         NSString *title = [[NSString alloc] initWithUTF8String:initWithText];
00051         NSMenu *menu = [[NSMenu alloc] initWithTitle: title];
00052         [title release];
00053         [menu setAutoenablesItems:NO];
00054         return (void*)menu;
00055 }
00056 
00057 void sdl_hax_nsMenuRelease(void *nsMenu) {
00058         [((NSMenu*)nsMenu) release];
00059 }
00060 
00061 void sdl_hax_macosx_setmenu(void *nsMenu) {
00062         if (nsMenu != NULL) {
00063         /* switch to the menu object given */
00064                 [NSApp setMainMenu:((NSMenu*)nsMenu)];
00065         }
00066         else {
00067                 /* switch back to the menu SDL 1.x made */
00068                 [NSApp setMainMenu:((NSMenu*)sdl1_hax_stock_osx_menu())];
00069         }
00070 }
00071 
00072 void sdl_hax_nsMenuItemSetTag(void *nsMenuItem, unsigned int new_id) {
00073         [((NSMenuItem*)nsMenuItem) setTag:new_id];
00074 }
00075 
00076 void sdl_hax_nsMenuItemSetSubmenu(void *nsMenuItem,void *nsMenu) {
00077         [((NSMenuItem*)nsMenuItem) setSubmenu:((NSMenu*)nsMenu)];
00078 }
00079 
00080 void* sdl_hax_nsMenuItemAlloc(const char *initWithText) {
00081         NSString *title = [[NSString alloc] initWithUTF8String:initWithText];
00082         NSMenuItem *item = [[NSMenuItem alloc] initWithTitle: title action:@selector(DOSBoxXMenuAction:) keyEquivalent:@""];
00083         [title release];
00084         return (void*)item;
00085 }
00086 
00087 void sdl_hax_nsMenuAddItem(void *nsMenu,void *nsMenuItem) {
00088         [((NSMenu*)nsMenu) addItem:((NSMenuItem*)nsMenuItem)];
00089 }
00090 
00091 void* sdl_hax_nsMenuAllocSeparator(void) {
00092         return (void*)([NSMenuItem separatorItem]);
00093 }
00094 
00095 void sdl_hax_nsMenuItemRelease(void *nsMenuItem) {
00096         [((NSMenuItem*)nsMenuItem) release];
00097 }
00098 
00099 void sdl_hax_nsMenuAddApplicationMenu(void *nsMenu) {
00100         /* make up an Application menu and stick it in first.
00101            the caller should have passed us an empty menu */
00102         NSMenu *appMenu;
00103         NSMenuItem *appMenuItem;
00104 
00105         appMenu = [[NSMenu alloc] initWithTitle:@""];
00106         [appMenu addItemWithTitle:@"About DOSBox-X" action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
00107 
00108         appMenuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
00109         [appMenuItem setSubmenu:appMenu];
00110         [((NSMenu*)nsMenu) addItem:appMenuItem];
00111         [appMenuItem release];
00112         [appMenu release];
00113 }
00114 
00115 @implementation NSApplication (DOSBoxX)
00116 - (void)DOSBoxXMenuAction:(id)sender
00117 {
00118         /* sorry! */
00119         mainMenu.mainMenuAction([sender tag]);
00120 }
00121 @end
00122 #endif
00123