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 780:4e4ea322cb84
prev757:2780bc393e7c
next786:8f6ece92500e
author nkeynes
date Mon Jul 28 10:13:13 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Move grab management to window
Clear grab if the window loses key focus (probably a good idea in general)
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 <ApplicationServices/ApplicationServices.h>
    23 @interface NSWindow (OSX10_5_and_later)
    24 #ifndef CGFLOAT_DEFINED
    25 # ifdef __LP64__
    26     typedef double CGFloat;
    27 # else
    28     typedef float CGFloat;
    29 # endif
    30 #endif
    31 - (void)setAutorecalculatesContentBorderThickness:(BOOL)b forEdge:(NSRectEdge)e;
    32 - (void)setContentBorderThickness:(CGFloat)b forEdge:(NSRectEdge)e;
    33 @end
    36 #if NSAppKitVersionNumber > NSAppKitVersionNumber10_4 
    38 #endif
    40 #define STATUSBAR_HEIGHT 25
    41 #define STATUS_TEXT_HEIGHT 22
    43 @interface LxdreamToolbarDelegate : NSObject {
    44     NSArray *identifiers;
    45     NSArray *defaults;
    46     NSDictionary *items;
    47 }
    48 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label 
    49                               tooltip: (NSString *)tooltip 
    50                                  icon: (NSString *)icon action: (SEL) action; 
    51 @end
    53 @implementation LxdreamToolbarDelegate
    54 - (id) init
    55 {
    56     NSToolbarItem *mount = [self createToolbarItem: @"GdromMount" label: @"Open Image" 
    57                             tooltip: @"Mount a cdrom disc" icon: @"tb-cdrom" 
    58                             action: @selector(mount_action:)];
    59     NSToolbarItem *reset = [self createToolbarItem: @"Reset" label: @"Reset"
    60                             tooltip: @"Reset dreamcast" icon: @"tb-reset"
    61                             action: @selector(reset_action:)];
    62     NSToolbarItem *pause = [self createToolbarItem: @"Pause" label: @"Pause"
    63                             tooltip: @"Pause dreamcast" icon: @"tb-pause"
    64                             action: @selector(pause_action:)];
    65     NSToolbarItem *run = [self createToolbarItem: @"Run" label: @"Resume"
    66                           tooltip: @"Resume" icon: @"tb-run"
    67                           action: @selector(run_action:)];
    68     NSToolbarItem *load = [self createToolbarItem: @"LoadState" label: @"Load State..."
    69                            tooltip: @"Load an lxdream save state" icon: @"tb-load"
    70                            action: @selector(load_action:)];
    71     NSToolbarItem *save = [self createToolbarItem: @"SaveState" label: @"Save State..."
    72                            tooltip: @"Create an lxdream save state" icon: @"tb-save"
    73                            action: @selector(save_action:)];
    74     [pause setEnabled: NO];
    75     identifiers = 
    76         [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run", @"LoadState", @"SaveState", nil ];
    77     defaults = 
    78         [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run", 
    79          NSToolbarSeparatorItemIdentifier, @"LoadState", @"SaveState", nil ];
    80     NSArray *values = [NSArray arrayWithObjects: mount, reset, pause, run, load, save, nil ];
    81     items = [NSDictionary dictionaryWithObjects: values forKeys: identifiers];
    82     return self;
    83 }
    85 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label 
    86 tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action 
    87 {
    88     NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier: id];
    89     [item setLabel: label];
    90     [item setToolTip: tooltip];
    91     [item setTarget: [NSApp delegate]];
    92     NSString *iconFile = [[NSBundle mainBundle] pathForResource:icon ofType:@"png"];
    93     NSImage *image = [[NSImage alloc] initWithContentsOfFile: iconFile];
    94     [item setImage: image];
    95     [item setAction: action];
    96     return item;
    97 }
    99 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar 
   100 {
   101     return identifiers;
   102 }
   104 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
   105 {
   106     return defaults;
   107 }
   109 - (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
   110 {
   111     return [NSArray arrayWithObjects: @"Pause", @"Run", nil];
   112 }
   114 - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier
   115 willBeInsertedIntoToolbar:(BOOL)flag 
   116 {
   117     return [items objectForKey: itemIdentifier];
   118 }
   119 @end
   121 @implementation LxdreamMainWindow
   122 - (id)initWithContentRect:(NSRect)videoRect 
   123 {
   124     NSRect contentRect = NSMakeRect(videoRect.origin.x,videoRect.origin.y,
   125             videoRect.size.width,videoRect.size.height+STATUSBAR_HEIGHT);
   126     if( [super initWithContentRect: contentRect
   127          styleMask: ( NSTitledWindowMask | NSClosableWindowMask | 
   128                  NSMiniaturizableWindowMask | NSResizableWindowMask |
   129                  NSUnifiedTitleAndToolbarWindowMask )
   130                  backing: NSBackingStoreBuffered defer: NO ] == nil ) {
   131         return nil;
   132     } else {
   133         isGrabbed = NO;
   134         video = (LxdreamVideoView *)video_osx_create_drawable();
   135         [video setFrameOrigin: NSMakePoint(0.0,STATUSBAR_HEIGHT)];
   136         [video setDelegate: self];
   138         status = 
   139             [[NSTextField alloc] initWithFrame: NSMakeRect(0.0,0.0,videoRect.size.width,STATUS_TEXT_HEIGHT)];
   140         [status setStringValue: @"Idle"];
   141         [status setEditable: NO];
   142         [status setDrawsBackground: NO];
   143         [status setBordered: NO];
   144         [[self contentView] addSubview: video];
   145         [[self contentView] addSubview: status];
   146         [self makeFirstResponder: video];
   148         if( [self respondsToSelector:@selector(setAutorecalculatesContentBorderThickness:forEdge:)] )
   149             [self setAutorecalculatesContentBorderThickness: NO forEdge: NSMinYEdge ];
   150         if( [self respondsToSelector:@selector(setContentBorderThickness:forEdge:)] ) 
   151             [self setContentBorderThickness: STATUSBAR_HEIGHT forEdge: NSMinYEdge];
   153         // Share the app delegate for the purposes of keeping it in one place
   154         [self setDelegate: [NSApp delegate]];
   155         [self setContentMinSize: contentRect.size];
   156         [self setAcceptsMouseMovedEvents: YES];
   158         NSString *title = [[NSString alloc] initWithCString: lxdream_package_name encoding: NSASCIIStringEncoding]; 
   159         [self setTitle: title];
   161         NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier: @"LxdreamToolbar"];
   162         [toolbar setDelegate: [[LxdreamToolbarDelegate alloc] init]];
   163         [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
   164         [toolbar setSizeMode: NSToolbarSizeModeSmall];
   165         [toolbar setSelectedItemIdentifier: @"Pause"];
   166         [self setToolbar: toolbar];
   167         return self;
   168     }
   169 }
   171 - (void)setStatusText: (const gchar *)text
   172 {
   173     if( isGrabbed ) {
   174         gchar buf[128];
   175         snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
   176         NSString *s = [NSString stringWithUTF8String: buf]; 
   177         [status setStringValue: s];
   178     } else {
   179         NSString *s = [NSString stringWithUTF8String: text];
   180         [status setStringValue: s];
   181     }   
   182 }
   183 - (void)setRunning:(BOOL)isRunning
   184 {
   185     if( isRunning ) {
   186         [[self toolbar] setSelectedItemIdentifier: @"Run"];
   187         [self setStatusText: _("Running")];
   188     } else {
   189         [[self toolbar] setSelectedItemIdentifier: @"Pause"];
   190         [self setStatusText: _("Stopped")];
   191     }            
   192 }
   193 - (BOOL)isGrabbed
   194 {
   195     return isGrabbed;
   196 }
   197 - (void)setIsGrabbed:(BOOL)grab
   198 {
   199     if( grab != isGrabbed ) {
   200         isGrabbed = grab;
   201         [self setRunning: dreamcast_is_running() ? YES : NO];
   203         if( isGrabbed ) {
   204             [NSCursor hide];
   205             CGAssociateMouseAndMouseCursorPosition(NO);
   206         } else {
   207             [NSCursor unhide];
   208             CGAssociateMouseAndMouseCursorPosition(YES);
   209         }
   210         [video setIsGrabbed: isGrabbed];
   211     }
   212 }
   213 - (void)viewRequestedGrab: (id)sender
   214 {
   215     [self setIsGrabbed: YES];
   216 }
   217 - (void)viewRequestedUngrab: (id)sender
   218 {
   219     [self setIsGrabbed: NO];
   220 }
   221 @end
   223 NSWindow *cocoa_gui_create_main_window()
   224 {
   225     NSRect contentRect = {{0,0},{640,480}};
   226     NSWindow *main_win = [[LxdreamMainWindow alloc] initWithContentRect: contentRect]; 
   227     return main_win;
   228 }
.