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 964:f2f3c7612d06
next1028:f99eeaf084c2
author nkeynes
date Tue Mar 24 11:15:57 2009 +0000 (15 years ago)
permissions -rw-r--r--
last change Add preliminary implementation of the GDB remote debugging server - attaches to
either or both the SH4 and ARM
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>
    25 @interface NSWindow (OSX10_5_and_later)
    26 #ifndef CGFLOAT_DEFINED
    27 # ifdef __LP64__
    28     typedef double CGFloat;
    29 # else
    30     typedef float CGFloat;
    31 # endif
    32 #endif
    33 - (void)setAutorecalculatesContentBorderThickness:(BOOL)b forEdge:(NSRectEdge)e;
    34 - (void)setContentBorderThickness:(CGFloat)b forEdge:(NSRectEdge)e;
    35 @end
    38 #if NSAppKitVersionNumber > NSAppKitVersionNumber10_4 
    40 #endif
    42 #define STATUSBAR_HEIGHT 25
    43 #define STATUS_TEXT_HEIGHT 22
    45 @interface LxdreamToolbarDelegate : NSObject {
    46     NSArray *identifiers;
    47     NSArray *defaults;
    48     NSDictionary *items;
    49 }
    50 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label 
    51                               tooltip: (NSString *)tooltip 
    52                                  icon: (NSString *)icon action: (SEL) action; 
    53 @end
    55 @implementation LxdreamToolbarDelegate
    56 - (id) init
    57 {
    58     NSToolbarItem *mount = [self createToolbarItem: @"GdromMount" label: @"Open Image" 
    59                             tooltip: @"Mount a cdrom disc" icon: @"tb-cdrom" 
    60                             action: @selector(mount_action:)];
    61     NSToolbarItem *reset = [self createToolbarItem: @"Reset" label: @"Reset"
    62                             tooltip: @"Reset dreamcast" icon: @"tb-reset"
    63                             action: @selector(reset_action:)];
    64     NSToolbarItem *pause = [self createToolbarItem: @"Pause" label: @"Pause"
    65                             tooltip: @"Pause dreamcast" icon: @"tb-pause"
    66                             action: @selector(pause_action:)];
    67     NSToolbarItem *run = [self createToolbarItem: @"Run" label: @"Resume"
    68                           tooltip: @"Resume" icon: @"tb-run"
    69                           action: @selector(run_action:)];
    70     NSToolbarItem *load = [self createToolbarItem: @"LoadState" label: @"Load State..."
    71                            tooltip: @"Load an lxdream save state" icon: @"tb-load"
    72                            action: @selector(load_action:)];
    73     NSToolbarItem *save = [self createToolbarItem: @"SaveState" label: @"Save State..."
    74                            tooltip: @"Create an lxdream save state" icon: @"tb-save"
    75                            action: @selector(save_action:)];
    76     NSToolbarItem *prefs = [self createToolbarItem: @"Preferences" label: @"Preferences..."
    77                            tooltip: @"Edit preferences" icon: @"tb-preferences"
    78                            action: @selector(preferences_action:)];
    79     [pause setEnabled: NO];
    80     identifiers = 
    81         [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run", @"LoadState", @"SaveState", @"Preferences", nil ];
    82     defaults = 
    83         [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run", 
    84          NSToolbarSeparatorItemIdentifier, @"LoadState", @"SaveState", 
    85          NSToolbarFlexibleSpaceItemIdentifier, @"Preferences", nil ];
    86     NSArray *values = [NSArray arrayWithObjects: mount, reset, pause, run, load, save, prefs, nil ];
    87     items = [NSDictionary dictionaryWithObjects: values forKeys: identifiers];
    88     return self;
    89 }
    91 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label 
    92 tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action 
    93 {
    94     NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier: id];
    95     [item setLabel: label];
    96     [item setToolTip: tooltip];
    97     [item setTarget: [NSApp delegate]];
    98     NSString *iconFile = [[NSBundle mainBundle] pathForResource:icon ofType:@"png"];
    99     NSImage *image = [[NSImage alloc] initWithContentsOfFile: iconFile];
   100     [item setImage: image];
   101     [item setAction: action];
   102     return item;
   103 }
   105 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar 
   106 {
   107     return identifiers;
   108 }
   110 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
   111 {
   112     return defaults;
   113 }
   115 - (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
   116 {
   117     return [NSArray arrayWithObjects: @"Pause", @"Run", nil];
   118 }
   120 - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier
   121 willBeInsertedIntoToolbar:(BOOL)flag 
   122 {
   123     return [items objectForKey: itemIdentifier];
   124 }
   125 @end
   127 @implementation LxdreamMainWindow
   128 - (id)initWithContentRect:(NSRect)videoRect 
   129 {
   130     NSRect contentRect = NSMakeRect(videoRect.origin.x,videoRect.origin.y,
   131             videoRect.size.width,videoRect.size.height+STATUSBAR_HEIGHT);
   132     if( [super initWithContentRect: contentRect
   133          styleMask: ( NSTitledWindowMask | NSClosableWindowMask | 
   134                  NSMiniaturizableWindowMask | NSResizableWindowMask |
   135                  NSUnifiedTitleAndToolbarWindowMask )
   136                  backing: NSBackingStoreBuffered defer: NO ] == nil ) {
   137         return nil;
   138     } else {
   139         useGrab = NO;
   140         isGrabbed = NO;
   141         video = (LxdreamVideoView *)video_osx_create_drawable();
   142         [video setFrameOrigin: NSMakePoint(0.0,STATUSBAR_HEIGHT)];
   143         [video setDelegate: self];
   145         status = 
   146             [[NSTextField alloc] initWithFrame: NSMakeRect(0.0,0.0,videoRect.size.width,STATUS_TEXT_HEIGHT)];
   147         [status setStringValue: @"Idle"];
   148         [status setEditable: NO];
   149         [status setDrawsBackground: NO];
   150         [status setBordered: NO];
   151         [[self contentView] addSubview: video];
   152         [[self contentView] addSubview: status];
   153         [self makeFirstResponder: video];
   155         if( [self respondsToSelector:@selector(setAutorecalculatesContentBorderThickness:forEdge:)] )
   156             [self setAutorecalculatesContentBorderThickness: NO forEdge: NSMinYEdge ];
   157         if( [self respondsToSelector:@selector(setContentBorderThickness:forEdge:)] ) 
   158             [self setContentBorderThickness: STATUSBAR_HEIGHT forEdge: NSMinYEdge];
   160         // Share the app delegate for the purposes of keeping it in one place
   161         [self setDelegate: [NSApp delegate]];
   162         [self setContentMinSize: contentRect.size];
   163         [self setAcceptsMouseMovedEvents: YES];
   164         [self updateTitle];
   166         NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier: @"LxdreamToolbar"];
   167         [toolbar setDelegate: [[LxdreamToolbarDelegate alloc] init]];
   168         [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
   169         [toolbar setSizeMode: NSToolbarSizeModeSmall];
   170         [toolbar setSelectedItemIdentifier: @"Pause"];
   171         [self setToolbar: toolbar];
   172         return self;
   173     }
   174 }
   176 - (void)updateTitle
   177 {
   178     NSString *title;
   179     const char *disc_title = gdrom_get_current_disc_title();
   180     if( disc_title == NULL ) {
   181         title = [NSString stringWithCString: lxdream_package_name];
   182     } else {
   183         title = [NSString stringWithFormat: @"%s - %s", lxdream_package_name, disc_title];
   184     }
   185     [self setTitle: title];
   186 }    
   188 - (void)setStatusText: (const gchar *)text
   189 {
   190     if( isGrabbed ) {
   191         gchar buf[128];
   192         snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
   193         NSString *s = [NSString stringWithUTF8String: buf]; 
   194         [status setStringValue: s];
   195     } else {
   196         NSString *s = [NSString stringWithUTF8String: text];
   197         [status setStringValue: s];
   198     }   
   199 }
   200 - (void)setRunning:(BOOL)isRunning
   201 {
   202     if( isRunning ) {
   203         [[self toolbar] setSelectedItemIdentifier: @"Run"];
   204         [self setStatusText: _("Running")];
   205     } else {
   206         [[self toolbar] setSelectedItemIdentifier: @"Pause"];
   207         [self setStatusText: _("Stopped")];
   208     }            
   209 }
   210 - (BOOL)isGrabbed
   211 {
   212     return isGrabbed;
   213 }
   214 - (void)setIsGrabbed:(BOOL)grab
   215 {
   216     if( grab != isGrabbed ) {
   217         isGrabbed = grab;
   218         [self setRunning: dreamcast_is_running() ? YES : NO];
   220         if( isGrabbed ) {
   221             [NSCursor hide];
   222             CGAssociateMouseAndMouseCursorPosition(NO);
   223         } else {
   224             [NSCursor unhide];
   225             CGAssociateMouseAndMouseCursorPosition(YES);
   226         }
   227         [video setIsGrabbed: isGrabbed];
   228     }
   229 }
   230 - (void)setUseGrab:(BOOL)grab
   231 {
   232     if( grab != useGrab ) {
   233         if( !grab && isGrabbed ) {
   234             [self setIsGrabbed: NO];
   235         }
   236         useGrab = grab;
   237     }
   238 }
   240 - (id)viewRequestedGrab: (id)sender
   241 {
   242     if( useGrab ) {
   243         [self setIsGrabbed: YES];
   244     }
   245     return useGrab ? self : nil;
   246 }
   247 - (id)viewRequestedUngrab: (id)sender
   248 {
   249     [self setIsGrabbed: NO];
   250     return useGrab ? self : nil;
   251 }
   252 @end
   254 LxdreamMainWindow *cocoa_gui_create_main_window()
   255 {
   256     NSRect contentRect = {{0,0},{640,480}};
   257     return [[LxdreamMainWindow alloc] initWithContentRect: contentRect]; 
   258 }
.