boo/lib/mac/ApplicationCocoa.mm

210 lines
6.8 KiB
Plaintext
Raw Normal View History

2015-05-06 00:50:57 +00:00
#include <AppKit/AppKit.h>
2015-09-02 19:09:13 +00:00
#include "boo/IApplication.hpp"
2015-05-06 00:50:57 +00:00
@interface AppDelegate : NSObject <NSApplicationDelegate>
{
boo::IApplicationCallback* callback;
2015-05-06 01:03:49 +00:00
@public
2015-05-06 00:50:57 +00:00
NSPanel* aboutPanel;
}
- (id)initWithCallback:(boo::IApplicationCallback*)cb;
@end
@implementation AppDelegate
- (id)initWithCallback:(boo::IApplicationCallback*)cb
{
self = [super init];
callback = cb;
return self;
}
- (void)applicationDidFinishLaunching:(NSNotification*)notification
{
(void)notification;
2015-09-02 19:09:13 +00:00
callback->appMain(boo::APP);
2015-05-06 00:50:57 +00:00
}
- (void)applicationWillTerminate:(NSNotification*)notification
{
(void)notification;
2015-09-02 19:09:13 +00:00
callback->appQuitting(boo::APP);
2015-05-06 00:50:57 +00:00
}
- (BOOL)application:(NSApplication*)sender openFile:(NSString*)filename
{
2015-09-02 19:09:13 +00:00
std::vector<boo::SystemString> strVec;
strVec.push_back(boo::SystemString([filename UTF8String]));
callback->appFilesOpen(boo::APP, strVec);
return true;
}
- (void)application:(NSApplication*)sender openFiles:(NSArray*)filenames
{
std::vector<boo::SystemString> strVec;
strVec.reserve([filenames count]);
for (NSString* str in filenames)
strVec.push_back(boo::SystemString([str UTF8String]));
callback->appFilesOpen(boo::APP, strVec);
2015-05-06 00:50:57 +00:00
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)sender {
(void)sender;
return YES;
}
- (IBAction)aboutApp:(id)sender
{
(void)sender;
NSRect screenFrame = [[aboutPanel screen] frame];
CGFloat xPos = NSWidth(screenFrame)/2 - 300/2;
CGFloat yPos = NSHeight(screenFrame)/2 - 220/2;
NSRect aboutCr = NSMakeRect(xPos, yPos, 300, 220);
[aboutPanel setFrame:aboutCr display:NO];
[aboutPanel makeKeyAndOrderFront:self];
}
- (IBAction)toggleFs:(id)sender
{
(void)sender;
[[NSApp keyWindow] toggleFullScreen:nil];
}
- (IBAction)quitApp:(id)sender
{
(void)sender;
[NSApp terminate:nil];
}
@end
namespace boo
{
2015-09-02 19:09:13 +00:00
IWindow* _WindowCocoaNew(const SystemString& title);
2015-05-06 00:50:57 +00:00
2015-09-02 19:09:13 +00:00
class ApplicationCocoa : public IApplication
2015-05-06 00:50:57 +00:00
{
IApplicationCallback& m_callback;
2015-09-02 19:09:13 +00:00
const SystemString m_uniqueName;
const SystemString m_friendlyName;
const SystemString m_pname;
const std::vector<SystemString> m_args;
2015-05-06 00:50:57 +00:00
NSPanel* aboutPanel;
void _deletedWindow(IWindow* window)
{
(void)window;
}
public:
2015-09-02 19:09:13 +00:00
ApplicationCocoa(IApplicationCallback& callback,
const SystemString& uniqueName,
const SystemString& friendlyName,
const SystemString& pname,
const std::vector<SystemString>& args)
2015-05-06 00:50:57 +00:00
: m_callback(callback),
2015-09-02 19:09:13 +00:00
m_uniqueName(uniqueName),
2015-05-06 00:50:57 +00:00
m_friendlyName(friendlyName),
m_pname(pname),
m_args(args)
{
@autoreleasepool
{
NSApplication* app = [NSApplication sharedApplication];
[app setActivationPolicy:NSApplicationActivationPolicyRegular];
/* Delegate (OS X callbacks) */
AppDelegate* appDelegate = [[AppDelegate alloc] initWithCallback:&m_callback];
[app setDelegate:appDelegate];
/* App menu */
NSMenu* appMenu = [[NSMenu alloc] initWithTitle:@"main"];
NSMenu* rwkMenu = [[NSMenu alloc] initWithTitle:[[NSString stringWithUTF8String:m_friendlyName.c_str()] autorelease]];
[rwkMenu addItemWithTitle:[[NSString stringWithFormat:@"About %s", m_friendlyName.c_str()] autorelease]
action:@selector(aboutApp:)
keyEquivalent:@""];
NSMenuItem* fsItem = [rwkMenu addItemWithTitle:@"Toggle Full Screen"
action:@selector(toggleFs:)
keyEquivalent:@"f"];
[fsItem setKeyEquivalentModifierMask:NSCommandKeyMask];
[rwkMenu addItem:[NSMenuItem separatorItem]];
NSMenuItem* quit_item = [rwkMenu addItemWithTitle:[[NSString stringWithFormat:@"Quit %s", m_friendlyName.c_str()] autorelease]
action:@selector(quitApp:)
keyEquivalent:@"q"];
[quit_item setKeyEquivalentModifierMask:NSCommandKeyMask];
[[appMenu addItemWithTitle:[[NSString stringWithUTF8String:m_friendlyName.c_str()] autorelease]
action:nil keyEquivalent:@""] setSubmenu:rwkMenu];
[[NSApplication sharedApplication] setMainMenu:appMenu];
/* About panel */
NSRect aboutCr = NSMakeRect(0, 0, 300, 220);
aboutPanel = [[NSPanel alloc] initWithContentRect:aboutCr
styleMask:NSUtilityWindowMask|NSTitledWindowMask|NSClosableWindowMask
backing:NSBackingStoreBuffered defer:YES];
[aboutPanel setTitle:[[NSString stringWithFormat:@"About %s", m_friendlyName.c_str()] autorelease]];
NSText* aboutText = [[NSText alloc] initWithFrame:aboutCr];
[aboutText setEditable:NO];
[aboutText setAlignment:NSCenterTextAlignment];
[aboutText setString:@"\nRWK Authors\n\nJackoalan\nAntidote\n"];
[aboutPanel setContentView:aboutText];
2015-05-06 01:03:49 +00:00
appDelegate->aboutPanel = aboutPanel;
2015-05-06 00:50:57 +00:00
[app run];
}
}
2015-09-02 19:09:13 +00:00
EPlatformType getPlatformType() const
{
return PLAT_COCOA;
}
void pump()
{
}
2015-05-06 00:50:57 +00:00
void quit()
{
[NSApp terminate:nil];
}
2015-09-02 19:09:13 +00:00
const SystemString& getUniqueName() const
{
return m_uniqueName;
}
const SystemString& getFriendlyName() const
{
return m_friendlyName;
}
const SystemString& getProcessName() const
2015-05-06 00:50:57 +00:00
{
return m_pname;
}
2015-09-02 19:09:13 +00:00
const std::vector<SystemString>& getArgs() const
2015-05-06 00:50:57 +00:00
{
return m_args;
}
IWindow* newWindow(const std::string& title)
{
2015-09-02 19:09:13 +00:00
return _WindowCocoaNew(title);
2015-05-06 00:50:57 +00:00
}
};
IApplication* APP = NULL;
2015-09-02 19:09:13 +00:00
std::unique_ptr<IApplication> ApplicationBootstrap(IApplication::EPlatformType platform,
IApplicationCallback& cb,
const SystemString& uniqueName,
const SystemString& friendlyName,
const SystemString& pname,
const std::vector<SystemString>& args,
bool singleInstance)
2015-05-06 00:50:57 +00:00
{
if (!APP)
{
if (platform != IApplication::PLAT_COCOA &&
platform != IApplication::PLAT_AUTO)
return NULL;
2015-09-02 19:09:13 +00:00
APP = new ApplicationCocoa(cb, uniqueName, friendlyName, pname, args);
2015-05-06 00:50:57 +00:00
}
2015-09-02 19:09:13 +00:00
return std::unique_ptr<IApplication>(APP);
2015-05-06 00:50:57 +00:00
}
}