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 757:2780bc393e7c
prev738:0cbff49086b8
next780:4e4ea322cb84
author nkeynes
date Mon Jul 28 03:41:25 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Implement key-binding configuration pane for Cocoa UI
Minor tweaks for consistency and static-correctness
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 = video_osx_create_drawable();
   135         [video setFrameOrigin: NSMakePoint(0.0,STATUSBAR_HEIGHT)];
   137         status = 
   138             [[NSTextField alloc] initWithFrame: NSMakeRect(0.0,0.0,videoRect.size.width,STATUS_TEXT_HEIGHT)];
   139         [status setStringValue: @"Idle"];
   140         [status setEditable: NO];
   141         [status setDrawsBackground: NO];
   142         [status setBordered: NO];
   143         [[self contentView] addSubview: video];
   144         [[self contentView] addSubview: status];
   145         [self makeFirstResponder: video];
   147         if( [self respondsToSelector:@selector(setAutorecalculatesContentBorderThickness:forEdge:)] )
   148             [self setAutorecalculatesContentBorderThickness: NO forEdge: NSMinYEdge ];
   149         if( [self respondsToSelector:@selector(setContentBorderThickness:forEdge:)] ) 
   150             [self setContentBorderThickness: STATUSBAR_HEIGHT forEdge: NSMinYEdge];
   152         // Share the app delegate for the purposes of keeping it in one place
   153         [self setDelegate: [NSApp delegate]];
   154         [self setContentMinSize: contentRect.size];
   155         [self setAcceptsMouseMovedEvents: YES];
   157         NSString *title = [[NSString alloc] initWithCString: lxdream_package_name encoding: NSASCIIStringEncoding]; 
   158         [self setTitle: title];
   160         NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier: @"LxdreamToolbar"];
   161         [toolbar setDelegate: [[LxdreamToolbarDelegate alloc] init]];
   162         [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
   163         [toolbar setSizeMode: NSToolbarSizeModeSmall];
   164         [toolbar setSelectedItemIdentifier: @"Pause"];
   165         [self setToolbar: toolbar];
   166         return self;
   167     }
   168 }
   170 - (void)setStatusText: (const gchar *)text
   171 {
   172     if( isGrabbed ) {
   173         gchar buf[128];
   174         snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
   175         NSString *s = [NSString stringWithUTF8String: buf]; 
   176         [status setStringValue: s];
   177     } else {
   178         NSString *s = [NSString stringWithUTF8String: text];
   179         [status setStringValue: s];
   180     }   
   181 }
   182 - (void)setRunning:(BOOL)isRunning
   183 {
   184     if( isRunning ) {
   185         [[self toolbar] setSelectedItemIdentifier: @"Run"];
   186         [self setStatusText: _("Running")];
   187     } else {
   188         [[self toolbar] setSelectedItemIdentifier: @"Pause"];
   189         [self setStatusText: _("Stopped")];
   190     }            
   191 }
   192 - (BOOL)isGrabbed
   193 {
   194     return isGrabbed;
   195 }
   196 - (void)setIsGrabbed:(BOOL)grab
   197 {
   198     if( grab != isGrabbed ) {
   199         isGrabbed = grab;
   200         [self setRunning: dreamcast_is_running() ? YES : NO];
   201     }
   202 }
   204 @end
   206 NSWindow *cocoa_gui_create_main_window()
   207 {
   208     NSRect contentRect = {{0,0},{640,480}};
   209     NSWindow *main_win = [[LxdreamMainWindow alloc] initWithContentRect: contentRect]; 
   210     return main_win;
   211 }
.