Search
lxdream.org :: lxdream/src/cocoaui/cocoa_win.m :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/cocoaui/cocoa_win.m
changeset 964:f2f3c7612d06
next1028:f99eeaf084c2
author nkeynes
date Thu Jan 15 04:15:11 2009 +0000 (13 years ago)
permissions -rw-r--r--
last change Add support for the Intel ICC compiler (C only, icc doesn't support Obj-C)
- Rename Obj-C source to .m
- Separate paths.c into paths_unix.c and paths_osx.m
- Add configuration detection of ICC, along with specific opt flags
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/cocoaui/cocoa_win.m Thu Jan 15 04:15:11 2009 +0000
1.3 @@ -0,0 +1,258 @@
1.4 +/**
1.5 + * $Id$
1.6 + *
1.7 + * Construct and maintain the main window under cocoa.
1.8 + *
1.9 + * Copyright (c) 2008 Nathan Keynes.
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.15 + *
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.20 + */
1.21 +
1.22 +#include "cocoaui/cocoaui.h"
1.23 +#include "lxdream.h"
1.24 +#include "dreamcast.h"
1.25 +#include "gdrom/gdrom.h"
1.26 +#include <ApplicationServices/ApplicationServices.h>
1.27 +
1.28 +@interface NSWindow (OSX10_5_and_later)
1.29 +#ifndef CGFLOAT_DEFINED
1.30 +# ifdef __LP64__
1.31 + typedef double CGFloat;
1.32 +# else
1.33 + typedef float CGFloat;
1.34 +# endif
1.35 +#endif
1.36 +- (void)setAutorecalculatesContentBorderThickness:(BOOL)b forEdge:(NSRectEdge)e;
1.37 +- (void)setContentBorderThickness:(CGFloat)b forEdge:(NSRectEdge)e;
1.38 +@end
1.39 +
1.40 +
1.41 +#if NSAppKitVersionNumber > NSAppKitVersionNumber10_4
1.42 +
1.43 +#endif
1.44 +
1.45 +#define STATUSBAR_HEIGHT 25
1.46 +#define STATUS_TEXT_HEIGHT 22
1.47 +
1.48 +@interface LxdreamToolbarDelegate : NSObject {
1.49 + NSArray *identifiers;
1.50 + NSArray *defaults;
1.51 + NSDictionary *items;
1.52 +}
1.53 +- (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label
1.54 + tooltip: (NSString *)tooltip
1.55 + icon: (NSString *)icon action: (SEL) action;
1.56 +@end
1.57 +
1.58 +@implementation LxdreamToolbarDelegate
1.59 +- (id) init
1.60 +{
1.61 + NSToolbarItem *mount = [self createToolbarItem: @"GdromMount" label: @"Open Image"
1.62 + tooltip: @"Mount a cdrom disc" icon: @"tb-cdrom"
1.63 + action: @selector(mount_action:)];
1.64 + NSToolbarItem *reset = [self createToolbarItem: @"Reset" label: @"Reset"
1.65 + tooltip: @"Reset dreamcast" icon: @"tb-reset"
1.66 + action: @selector(reset_action:)];
1.67 + NSToolbarItem *pause = [self createToolbarItem: @"Pause" label: @"Pause"
1.68 + tooltip: @"Pause dreamcast" icon: @"tb-pause"
1.69 + action: @selector(pause_action:)];
1.70 + NSToolbarItem *run = [self createToolbarItem: @"Run" label: @"Resume"
1.71 + tooltip: @"Resume" icon: @"tb-run"
1.72 + action: @selector(run_action:)];
1.73 + NSToolbarItem *load = [self createToolbarItem: @"LoadState" label: @"Load State..."
1.74 + tooltip: @"Load an lxdream save state" icon: @"tb-load"
1.75 + action: @selector(load_action:)];
1.76 + NSToolbarItem *save = [self createToolbarItem: @"SaveState" label: @"Save State..."
1.77 + tooltip: @"Create an lxdream save state" icon: @"tb-save"
1.78 + action: @selector(save_action:)];
1.79 + NSToolbarItem *prefs = [self createToolbarItem: @"Preferences" label: @"Preferences..."
1.80 + tooltip: @"Edit preferences" icon: @"tb-preferences"
1.81 + action: @selector(preferences_action:)];
1.82 + [pause setEnabled: NO];
1.83 + identifiers =
1.84 + [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run", @"LoadState", @"SaveState", @"Preferences", nil ];
1.85 + defaults =
1.86 + [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run",
1.87 + NSToolbarSeparatorItemIdentifier, @"LoadState", @"SaveState",
1.88 + NSToolbarFlexibleSpaceItemIdentifier, @"Preferences", nil ];
1.89 + NSArray *values = [NSArray arrayWithObjects: mount, reset, pause, run, load, save, prefs, nil ];
1.90 + items = [NSDictionary dictionaryWithObjects: values forKeys: identifiers];
1.91 + return self;
1.92 +}
1.93 +
1.94 +- (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label
1.95 +tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action
1.96 +{
1.97 + NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier: id];
1.98 + [item setLabel: label];
1.99 + [item setToolTip: tooltip];
1.100 + [item setTarget: [NSApp delegate]];
1.101 + NSString *iconFile = [[NSBundle mainBundle] pathForResource:icon ofType:@"png"];
1.102 + NSImage *image = [[NSImage alloc] initWithContentsOfFile: iconFile];
1.103 + [item setImage: image];
1.104 + [item setAction: action];
1.105 + return item;
1.106 +}
1.107 +
1.108 +- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar
1.109 +{
1.110 + return identifiers;
1.111 +}
1.112 +
1.113 +- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
1.114 +{
1.115 + return defaults;
1.116 +}
1.117 +
1.118 +- (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
1.119 +{
1.120 + return [NSArray arrayWithObjects: @"Pause", @"Run", nil];
1.121 +}
1.122 +
1.123 +- (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier
1.124 +willBeInsertedIntoToolbar:(BOOL)flag
1.125 +{
1.126 + return [items objectForKey: itemIdentifier];
1.127 +}
1.128 +@end
1.129 +
1.130 +@implementation LxdreamMainWindow
1.131 +- (id)initWithContentRect:(NSRect)videoRect
1.132 +{
1.133 + NSRect contentRect = NSMakeRect(videoRect.origin.x,videoRect.origin.y,
1.134 + videoRect.size.width,videoRect.size.height+STATUSBAR_HEIGHT);
1.135 + if( [super initWithContentRect: contentRect
1.136 + styleMask: ( NSTitledWindowMask | NSClosableWindowMask |
1.137 + NSMiniaturizableWindowMask | NSResizableWindowMask |
1.138 + NSUnifiedTitleAndToolbarWindowMask )
1.139 + backing: NSBackingStoreBuffered defer: NO ] == nil ) {
1.140 + return nil;
1.141 + } else {
1.142 + useGrab = NO;
1.143 + isGrabbed = NO;
1.144 + video = (LxdreamVideoView *)video_osx_create_drawable();
1.145 + [video setFrameOrigin: NSMakePoint(0.0,STATUSBAR_HEIGHT)];
1.146 + [video setDelegate: self];
1.147 +
1.148 + status =
1.149 + [[NSTextField alloc] initWithFrame: NSMakeRect(0.0,0.0,videoRect.size.width,STATUS_TEXT_HEIGHT)];
1.150 + [status setStringValue: @"Idle"];
1.151 + [status setEditable: NO];
1.152 + [status setDrawsBackground: NO];
1.153 + [status setBordered: NO];
1.154 + [[self contentView] addSubview: video];
1.155 + [[self contentView] addSubview: status];
1.156 + [self makeFirstResponder: video];
1.157 +
1.158 + if( [self respondsToSelector:@selector(setAutorecalculatesContentBorderThickness:forEdge:)] )
1.159 + [self setAutorecalculatesContentBorderThickness: NO forEdge: NSMinYEdge ];
1.160 + if( [self respondsToSelector:@selector(setContentBorderThickness:forEdge:)] )
1.161 + [self setContentBorderThickness: STATUSBAR_HEIGHT forEdge: NSMinYEdge];
1.162 +
1.163 + // Share the app delegate for the purposes of keeping it in one place
1.164 + [self setDelegate: [NSApp delegate]];
1.165 + [self setContentMinSize: contentRect.size];
1.166 + [self setAcceptsMouseMovedEvents: YES];
1.167 + [self updateTitle];
1.168 +
1.169 + NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier: @"LxdreamToolbar"];
1.170 + [toolbar setDelegate: [[LxdreamToolbarDelegate alloc] init]];
1.171 + [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
1.172 + [toolbar setSizeMode: NSToolbarSizeModeSmall];
1.173 + [toolbar setSelectedItemIdentifier: @"Pause"];
1.174 + [self setToolbar: toolbar];
1.175 + return self;
1.176 + }
1.177 +}
1.178 +
1.179 +- (void)updateTitle
1.180 +{
1.181 + NSString *title;
1.182 + const char *disc_title = gdrom_get_current_disc_title();
1.183 + if( disc_title == NULL ) {
1.184 + title = [NSString stringWithCString: lxdream_package_name];
1.185 + } else {
1.186 + title = [NSString stringWithFormat: @"%s - %s", lxdream_package_name, disc_title];
1.187 + }
1.188 + [self setTitle: title];
1.189 +}
1.190 +
1.191 +- (void)setStatusText: (const gchar *)text
1.192 +{
1.193 + if( isGrabbed ) {
1.194 + gchar buf[128];
1.195 + snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
1.196 + NSString *s = [NSString stringWithUTF8String: buf];
1.197 + [status setStringValue: s];
1.198 + } else {
1.199 + NSString *s = [NSString stringWithUTF8String: text];
1.200 + [status setStringValue: s];
1.201 + }
1.202 +}
1.203 +- (void)setRunning:(BOOL)isRunning
1.204 +{
1.205 + if( isRunning ) {
1.206 + [[self toolbar] setSelectedItemIdentifier: @"Run"];
1.207 + [self setStatusText: _("Running")];
1.208 + } else {
1.209 + [[self toolbar] setSelectedItemIdentifier: @"Pause"];
1.210 + [self setStatusText: _("Stopped")];
1.211 + }
1.212 +}
1.213 +- (BOOL)isGrabbed
1.214 +{
1.215 + return isGrabbed;
1.216 +}
1.217 +- (void)setIsGrabbed:(BOOL)grab
1.218 +{
1.219 + if( grab != isGrabbed ) {
1.220 + isGrabbed = grab;
1.221 + [self setRunning: dreamcast_is_running() ? YES : NO];
1.222 +
1.223 + if( isGrabbed ) {
1.224 + [NSCursor hide];
1.225 + CGAssociateMouseAndMouseCursorPosition(NO);
1.226 + } else {
1.227 + [NSCursor unhide];
1.228 + CGAssociateMouseAndMouseCursorPosition(YES);
1.229 + }
1.230 + [video setIsGrabbed: isGrabbed];
1.231 + }
1.232 +}
1.233 +- (void)setUseGrab:(BOOL)grab
1.234 +{
1.235 + if( grab != useGrab ) {
1.236 + if( !grab && isGrabbed ) {
1.237 + [self setIsGrabbed: NO];
1.238 + }
1.239 + useGrab = grab;
1.240 + }
1.241 +}
1.242 +
1.243 +- (id)viewRequestedGrab: (id)sender
1.244 +{
1.245 + if( useGrab ) {
1.246 + [self setIsGrabbed: YES];
1.247 + }
1.248 + return useGrab ? self : nil;
1.249 +}
1.250 +- (id)viewRequestedUngrab: (id)sender
1.251 +{
1.252 + [self setIsGrabbed: NO];
1.253 + return useGrab ? self : nil;
1.254 +}
1.255 +@end
1.256 +
1.257 +LxdreamMainWindow *cocoa_gui_create_main_window()
1.258 +{
1.259 + NSRect contentRect = {{0,0},{640,480}};
1.260 + return [[LxdreamMainWindow alloc] initWithContentRect: contentRect];
1.261 +}
.