Search
lxdream.org :: lxdream/src/cocoaui/cocoa_win.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/cocoaui/cocoa_win.c
changeset 839:51f1c4195790
prev837:4eae2ddccf9c
next874:e3007b8799f3
author nkeynes
date Sat Sep 06 05:21:57 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change Disable the run actions when the system can't run (missing from cocoa ui code)
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     [pause setEnabled: NO];
    77     identifiers = 
    78         [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run", @"LoadState", @"SaveState", nil ];
    79     defaults = 
    80         [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run", 
    81          NSToolbarSeparatorItemIdentifier, @"LoadState", @"SaveState", nil ];
    82     NSArray *values = [NSArray arrayWithObjects: mount, reset, pause, run, load, save, nil ];
    83     items = [NSDictionary dictionaryWithObjects: values forKeys: identifiers];
    84     return self;
    85 }
    87 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label 
    88 tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action 
    89 {
    90     NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier: id];
    91     [item setLabel: label];
    92     [item setToolTip: tooltip];
    93     [item setTarget: [NSApp delegate]];
    94     NSString *iconFile = [[NSBundle mainBundle] pathForResource:icon ofType:@"png"];
    95     NSImage *image = [[NSImage alloc] initWithContentsOfFile: iconFile];
    96     [item setImage: image];
    97     [item setAction: action];
    98     return item;
    99 }
   101 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar 
   102 {
   103     return identifiers;
   104 }
   106 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
   107 {
   108     return defaults;
   109 }
   111 - (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
   112 {
   113     return [NSArray arrayWithObjects: @"Pause", @"Run", nil];
   114 }
   116 - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier
   117 willBeInsertedIntoToolbar:(BOOL)flag 
   118 {
   119     return [items objectForKey: itemIdentifier];
   120 }
   121 @end
   123 @implementation LxdreamMainWindow
   124 - (id)initWithContentRect:(NSRect)videoRect 
   125 {
   126     NSRect contentRect = NSMakeRect(videoRect.origin.x,videoRect.origin.y,
   127             videoRect.size.width,videoRect.size.height+STATUSBAR_HEIGHT);
   128     if( [super initWithContentRect: contentRect
   129          styleMask: ( NSTitledWindowMask | NSClosableWindowMask | 
   130                  NSMiniaturizableWindowMask | NSResizableWindowMask |
   131                  NSUnifiedTitleAndToolbarWindowMask )
   132                  backing: NSBackingStoreBuffered defer: NO ] == nil ) {
   133         return nil;
   134     } else {
   135         useGrab = NO;
   136         isGrabbed = NO;
   137         video = (LxdreamVideoView *)video_osx_create_drawable();
   138         [video setFrameOrigin: NSMakePoint(0.0,STATUSBAR_HEIGHT)];
   139         [video setDelegate: self];
   141         status = 
   142             [[NSTextField alloc] initWithFrame: NSMakeRect(0.0,0.0,videoRect.size.width,STATUS_TEXT_HEIGHT)];
   143         [status setStringValue: @"Idle"];
   144         [status setEditable: NO];
   145         [status setDrawsBackground: NO];
   146         [status setBordered: NO];
   147         [[self contentView] addSubview: video];
   148         [[self contentView] addSubview: status];
   149         [self makeFirstResponder: video];
   151         if( [self respondsToSelector:@selector(setAutorecalculatesContentBorderThickness:forEdge:)] )
   152             [self setAutorecalculatesContentBorderThickness: NO forEdge: NSMinYEdge ];
   153         if( [self respondsToSelector:@selector(setContentBorderThickness:forEdge:)] ) 
   154             [self setContentBorderThickness: STATUSBAR_HEIGHT forEdge: NSMinYEdge];
   156         // Share the app delegate for the purposes of keeping it in one place
   157         [self setDelegate: [NSApp delegate]];
   158         [self setContentMinSize: contentRect.size];
   159         [self setAcceptsMouseMovedEvents: YES];
   160         [self updateTitle];
   162         NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier: @"LxdreamToolbar"];
   163         [toolbar setDelegate: [[LxdreamToolbarDelegate alloc] init]];
   164         [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
   165         [toolbar setSizeMode: NSToolbarSizeModeSmall];
   166         [toolbar setSelectedItemIdentifier: @"Pause"];
   167         [self setToolbar: toolbar];
   168         return self;
   169     }
   170 }
   172 - (void)updateTitle
   173 {
   174     NSString *title;
   175     const char *disc_title = gdrom_get_current_disc_title();
   176     if( disc_title == NULL ) {
   177         title = [NSString stringWithCString: lxdream_package_name];
   178     } else {
   179         title = [NSString stringWithFormat: @"%s - %s", lxdream_package_name, disc_title];
   180     }
   181     [self setTitle: title];
   182 }    
   184 - (void)setStatusText: (const gchar *)text
   185 {
   186     if( isGrabbed ) {
   187         gchar buf[128];
   188         snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
   189         NSString *s = [NSString stringWithUTF8String: buf]; 
   190         [status setStringValue: s];
   191     } else {
   192         NSString *s = [NSString stringWithUTF8String: text];
   193         [status setStringValue: s];
   194     }   
   195 }
   196 - (void)setRunning:(BOOL)isRunning
   197 {
   198     if( isRunning ) {
   199         [[self toolbar] setSelectedItemIdentifier: @"Run"];
   200         [self setStatusText: _("Running")];
   201     } else {
   202         [[self toolbar] setSelectedItemIdentifier: @"Pause"];
   203         [self setStatusText: _("Stopped")];
   204     }            
   205 }
   206 - (BOOL)isGrabbed
   207 {
   208     return isGrabbed;
   209 }
   210 - (void)setIsGrabbed:(BOOL)grab
   211 {
   212     if( grab != isGrabbed ) {
   213         isGrabbed = grab;
   214         [self setRunning: dreamcast_is_running() ? YES : NO];
   216         if( isGrabbed ) {
   217             [NSCursor hide];
   218             CGAssociateMouseAndMouseCursorPosition(NO);
   219         } else {
   220             [NSCursor unhide];
   221             CGAssociateMouseAndMouseCursorPosition(YES);
   222         }
   223         [video setIsGrabbed: isGrabbed];
   224     }
   225 }
   226 - (void)setUseGrab:(BOOL)grab
   227 {
   228     if( grab != useGrab ) {
   229         if( !grab && isGrabbed ) {
   230             [self setIsGrabbed: NO];
   231         }
   232         useGrab = grab;
   233     }
   234 }
   236 - (id)viewRequestedGrab: (id)sender
   237 {
   238     if( useGrab ) {
   239         [self setIsGrabbed: YES];
   240     }
   241     return useGrab ? self : nil;
   242 }
   243 - (id)viewRequestedUngrab: (id)sender
   244 {
   245     [self setIsGrabbed: NO];
   246     return useGrab ? self : nil;
   247 }
   248 @end
   250 NSWindow *cocoa_gui_create_main_window()
   251 {
   252     NSRect contentRect = {{0,0},{640,480}};
   253     NSWindow *main_win = [[LxdreamMainWindow alloc] initWithContentRect: contentRect]; 
   254     return main_win;
   255 }
.