Search
lxdream.org :: lxdream/src/cocoaui/cocoa_win.m
lxdream 0.9.1
released Jun 29
Download Now
filename src/cocoaui/cocoa_win.m
changeset 1098:4f2750753a6c
prev1028:f99eeaf084c2
next1298:d0eb2307b847
author nkeynes
date Sat Mar 03 16:06:58 2012 +1000 (12 years ago)
permissions -rw-r--r--
last change Fully clean the android part before building it - dependencies don't seem to
work properly.
Add install-adb rule for convenience
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Construct and maintain the main window under cocoa.
     5  *
     6  * Copyright (c) 2008 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #include "cocoaui/cocoaui.h"
    20 #include "lxdream.h"
    21 #include "dreamcast.h"
    22 #include "gdrom/gdrom.h"
    23 #include <ApplicationServices/ApplicationServices.h>
    24 #include <Carbon/Carbon.h>
    26 @interface NSWindow (OSX10_5_and_later)
    27 #ifndef CGFLOAT_DEFINED
    28 # ifdef __LP64__
    29     typedef double CGFloat;
    30 # else
    31     typedef float CGFloat;
    32 # endif
    33 #endif
    34 - (void)setAutorecalculatesContentBorderThickness:(BOOL)b forEdge:(NSRectEdge)e;
    35 - (void)setContentBorderThickness:(CGFloat)b forEdge:(NSRectEdge)e;
    36 @end
    39 #if NSAppKitVersionNumber > NSAppKitVersionNumber10_4 
    41 #endif
    43 #define STATUSBAR_HEIGHT 25
    44 #define STATUS_TEXT_HEIGHT 22
    46 @interface LxdreamToolbarDelegate : NSObject {
    47     NSArray *identifiers;
    48     NSArray *defaults;
    49     NSDictionary *items;
    50 }
    51 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label 
    52                               tooltip: (NSString *)tooltip 
    53                                  icon: (NSString *)icon action: (SEL) action; 
    54 @end
    56 @implementation LxdreamToolbarDelegate
    57 - (id) init
    58 {
    59     NSToolbarItem *mount = [self createToolbarItem: @"GdromMount" label: @"Open Image" 
    60                             tooltip: @"Mount a cdrom disc" icon: @"tb-cdrom" 
    61                             action: @selector(mount_action:)];
    62     NSToolbarItem *reset = [self createToolbarItem: @"Reset" label: @"Reset"
    63                             tooltip: @"Reset dreamcast" icon: @"tb-reset"
    64                             action: @selector(reset_action:)];
    65     NSToolbarItem *pause = [self createToolbarItem: @"Pause" label: @"Pause"
    66                             tooltip: @"Pause dreamcast" icon: @"tb-pause"
    67                             action: @selector(pause_action:)];
    68     NSToolbarItem *run = [self createToolbarItem: @"Run" label: @"Resume"
    69                           tooltip: @"Resume" icon: @"tb-run"
    70                           action: @selector(run_action:)];
    71     NSToolbarItem *load = [self createToolbarItem: @"LoadState" label: @"Load State..."
    72                            tooltip: @"Load an lxdream save state" icon: @"tb-load"
    73                            action: @selector(load_action:)];
    74     NSToolbarItem *save = [self createToolbarItem: @"SaveState" label: @"Save State..."
    75                            tooltip: @"Create an lxdream save state" icon: @"tb-save"
    76                            action: @selector(save_action:)];
    77     NSToolbarItem *prefs = [self createToolbarItem: @"Preferences" label: @"Preferences..."
    78                            tooltip: @"Edit preferences" icon: @"tb-preferences"
    79                            action: @selector(preferences_action:)];
    80     [pause setEnabled: NO];
    81     identifiers = 
    82         [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run", @"LoadState", @"SaveState", @"Preferences", nil ];
    83     defaults = 
    84         [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run", 
    85          NSToolbarSeparatorItemIdentifier, @"LoadState", @"SaveState", 
    86          NSToolbarFlexibleSpaceItemIdentifier, @"Preferences", nil ];
    87     NSArray *values = [NSArray arrayWithObjects: mount, reset, pause, run, load, save, prefs, nil ];
    88     items = [NSDictionary dictionaryWithObjects: values forKeys: identifiers];
    89     return self;
    90 }
    92 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label 
    93 tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action 
    94 {
    95     NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier: id];
    96     [item setLabel: label];
    97     [item setToolTip: tooltip];
    98     [item setTarget: [NSApp delegate]];
    99     NSString *iconFile = [[NSBundle mainBundle] pathForResource:icon ofType:@"png"];
   100     NSImage *image = [[NSImage alloc] initWithContentsOfFile: iconFile];
   101     [item setImage: image];
   102     [item setAction: action];
   103     return item;
   104 }
   106 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar 
   107 {
   108     return identifiers;
   109 }
   111 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
   112 {
   113     return defaults;
   114 }
   116 - (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
   117 {
   118     return [NSArray arrayWithObjects: @"Pause", @"Run", nil];
   119 }
   121 - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier
   122 willBeInsertedIntoToolbar:(BOOL)flag 
   123 {
   124     return [items objectForKey: itemIdentifier];
   125 }
   126 @end
   128 @implementation LxdreamMainWindow
   129 - (id)initWithContentRect:(NSRect)videoRect 
   130 {
   131     NSRect contentRect = NSMakeRect(videoRect.origin.x,videoRect.origin.y,
   132             videoRect.size.width,videoRect.size.height+STATUSBAR_HEIGHT);
   133     if( [super initWithContentRect: contentRect
   134          styleMask: ( NSTitledWindowMask | NSClosableWindowMask | 
   135                  NSMiniaturizableWindowMask | NSResizableWindowMask |
   136                  NSUnifiedTitleAndToolbarWindowMask )
   137                  backing: NSBackingStoreBuffered defer: NO ] == nil ) {
   138         return nil;
   139     } else {
   140         useGrab = NO;
   141         isGrabbed = NO;
   142         video = (LxdreamVideoView *)video_osx_create_drawable();
   143         [video setFrameOrigin: NSMakePoint(0.0,STATUSBAR_HEIGHT)];
   144         [video setDelegate: self];
   146         status = 
   147             [[NSTextField alloc] initWithFrame: NSMakeRect(0.0,0.0,videoRect.size.width,STATUS_TEXT_HEIGHT)];
   148         [status setStringValue: @"Idle"];
   149         [status setEditable: NO];
   150         [status setDrawsBackground: NO];
   151         [status setBordered: NO];
   152         [[self contentView] addSubview: video];
   153         [[self contentView] addSubview: status];
   154         [self makeFirstResponder: video];
   156         if( [self respondsToSelector:@selector(setAutorecalculatesContentBorderThickness:forEdge:)] )
   157             [self setAutorecalculatesContentBorderThickness: NO forEdge: NSMinYEdge ];
   158         if( [self respondsToSelector:@selector(setContentBorderThickness:forEdge:)] ) 
   159             [self setContentBorderThickness: STATUSBAR_HEIGHT forEdge: NSMinYEdge];
   161         // Share the app delegate for the purposes of keeping it in one place
   162         [self setDelegate: [NSApp delegate]];
   163         [self setContentMinSize: contentRect.size];
   164         [self setAcceptsMouseMovedEvents: YES];
   165         [self updateTitle];
   167         NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier: @"LxdreamToolbar"];
   168         [toolbar setDelegate: [[LxdreamToolbarDelegate alloc] init]];
   169         [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
   170         [toolbar setSizeMode: NSToolbarSizeModeSmall];
   171         [toolbar setSelectedItemIdentifier: @"Pause"];
   172         [self setToolbar: toolbar];
   173         return self;
   174     }
   175 }
   177 - (void)updateTitle
   178 {
   179     NSString *title;
   180     const char *disc_title = gdrom_get_current_disc_title();
   181     if( disc_title == NULL ) {
   182         title = [NSString stringWithFormat: @"%s - <no disc>", lxdream_package_name];
   183     } else {
   184         title = [NSString stringWithFormat: @"%s - %s", lxdream_package_name, disc_title];
   185     }
   186     [self setTitle: title];
   187 }    
   189 - (void)setStatusText: (const gchar *)text
   190 {
   191     if( isGrabbed ) {
   192         gchar buf[128];
   193         snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
   194         NSString *s = [NSString stringWithUTF8String: buf]; 
   195         [status setStringValue: s];
   196     } else {
   197         NSString *s = [NSString stringWithUTF8String: text];
   198         [status setStringValue: s];
   199     }   
   200 }
   201 - (void)setRunning:(BOOL)isRunning
   202 {
   203     if( isRunning ) {
   204         [[self toolbar] setSelectedItemIdentifier: @"Run"];
   205         [self setStatusText: _("Running")];
   206     } else {
   207         [[self toolbar] setSelectedItemIdentifier: @"Pause"];
   208         [self setStatusText: _("Stopped")];
   209     }            
   210 }
   211 - (BOOL)isFullscreen
   212 {
   213     return isFullscreen;
   214 }
   215 - (void)setFullscreen:(BOOL)full
   216 {
   217     if( full != isFullscreen ) {
   218         isFullscreen = full;
   220         if( full ) {
   221             savedFrame = [self frame];
   222             SetSystemUIMode( kUIModeAllHidden, 0 );
   223             NSRect screenRect = [[NSScreen mainScreen] frame];
   224             screenRect.size.height += STATUSBAR_HEIGHT;
   225             screenRect.origin.y -= STATUSBAR_HEIGHT;
   226             NSRect targetRect = [self frameRectForContentRect: screenRect];
   227             [self setFrame: targetRect display: YES];
   228         } else {
   229             SetSystemUIMode( kUIModeNormal, 0 );
   230             [self setFrame: savedFrame display: YES];
   231         }
   232     }
   233 }
   234 - (BOOL)isGrabbed
   235 {
   236     return isGrabbed;
   237 }
   238 - (void)setIsGrabbed:(BOOL)grab
   239 {
   240     if( grab != isGrabbed ) {
   241         isGrabbed = grab;
   242         [self setRunning: dreamcast_is_running() ? YES : NO];
   244         if( isGrabbed ) {
   245             [NSCursor hide];
   246             CGAssociateMouseAndMouseCursorPosition(NO);
   247         } else {
   248             [NSCursor unhide];
   249             CGAssociateMouseAndMouseCursorPosition(YES);
   250         }
   251         [video setIsGrabbed: isGrabbed];
   252     }
   253 }
   254 - (void)setUseGrab:(BOOL)grab
   255 {
   256     if( grab != useGrab ) {
   257         if( !grab && isGrabbed ) {
   258             [self setIsGrabbed: NO];
   259         }
   260         useGrab = grab;
   261     }
   262 }
   264 - (id)viewRequestedGrab: (id)sender
   265 {
   266     if( useGrab ) {
   267         [self setIsGrabbed: YES];
   268     }
   269     return useGrab ? self : nil;
   270 }
   271 - (id)viewRequestedUngrab: (id)sender
   272 {
   273     [self setIsGrabbed: NO];
   274     return useGrab ? self : nil;
   275 }
   276 - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)aScreen
   277 {
   278     return frameRect;
   279 }
   280 @end
   282 LxdreamMainWindow *cocoa_gui_create_main_window()
   283 {
   284     NSRect contentRect = {{0,0},{640,480}};
   285     return [[LxdreamMainWindow alloc] initWithContentRect: contentRect]; 
   286 }
.