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 681:1755a126b109
next705:03dd1ea35c69
author nkeynes
date Fri Jun 20 05:43:34 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Cocoa: Don't try to display the error dialog if the gui isn't running
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 #define STATUSBAR_HEIGHT 20
    25 @interface LxdreamToolbarDelegate : NSObject {
    26     NSArray *identifiers;
    27     NSArray *defaults;
    28     NSDictionary *items;
    29 }
    30 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label 
    31     tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action; 
    32 @end
    34 @implementation LxdreamToolbarDelegate
    35 - (id) init
    36 {
    37     NSToolbarItem *mount = [self createToolbarItem: @"GdromMount" label: @"Open Image" 
    38                             tooltip: @"Mount a cdrom disc" icon: @"tb-cdrom" 
    39                             action: @selector(mount_action:)];
    40     NSToolbarItem *reset = [self createToolbarItem: @"Reset" label: @"Reset"
    41                             tooltip: @"Reset dreamcast" icon: @"tb-reset"
    42                             action: @selector(reset_action:)];
    43     NSToolbarItem *pause = [self createToolbarItem: @"Pause" label: @"Pause"
    44                             tooltip: @"Pause dreamcast" icon: @"tb-pause"
    45                             action: @selector(pause_action:)];
    46     NSToolbarItem *run = [self createToolbarItem: @"Run" label: @"Resume"
    47                             tooltip: @"Resume" icon: @"tb-run"
    48                             action: @selector(run_action:)];
    49     NSToolbarItem *load = [self createToolbarItem: @"LoadState" label: @"Load State..."
    50                             tooltip: @"Load an lxdream save state" icon: @"tb-load"
    51                             action: @selector(load_action:)];
    52     NSToolbarItem *save = [self createToolbarItem: @"SaveState" label: @"Save State..."
    53                             tooltip: @"Create an lxdream save state" icon: @"tb-save"
    54                             action: @selector(save_action:)];
    55     [pause setEnabled: NO];
    56     identifiers = 
    57         [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run", @"LoadState", @"SaveState", nil ];
    58     defaults = 
    59         [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run", 
    60                      NSToolbarSeparatorItemIdentifier, @"LoadState", @"SaveState", nil ];
    61     NSArray *values = [NSArray arrayWithObjects: mount, reset, pause, run, load, save, nil ];
    62     items = [NSDictionary dictionaryWithObjects: values forKeys: identifiers];
    63     return self;
    64 }
    66 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label 
    67     tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action 
    68 {
    69     NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier: id];
    70     [item setLabel: label];
    71     [item setToolTip: tooltip];
    72     [item setTarget: [NSApp delegate]];
    73     NSString *iconFile = [[NSBundle mainBundle] pathForResource:icon ofType:@"png"];
    74     NSImage *image = [[NSImage alloc] initWithContentsOfFile: iconFile];
    75     [item setImage: image];
    76     [item setAction: action];
    77     return item;
    78 }
    80 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar 
    81 {
    82     return identifiers;
    83 }
    85 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
    86 {
    87     return defaults;
    88 }
    90 - (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
    91 {
    92     return [NSArray arrayWithObjects: @"Pause", @"Run", nil];
    93 }
    95 - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier
    96      willBeInsertedIntoToolbar:(BOOL)flag 
    97 {
    98      return [items objectForKey: itemIdentifier];
    99 }
   100 @end
   102 @implementation LxdreamMainWindow
   103 - (id)initWithContentRect:(NSRect)videoRect 
   104 {
   105     NSRect contentRect = NSMakeRect(videoRect.origin.x,videoRect.origin.y,
   106             videoRect.size.width,videoRect.size.height+STATUSBAR_HEIGHT);
   107     if( [super initWithContentRect: contentRect
   108            styleMask: ( NSTitledWindowMask | NSClosableWindowMask | 
   109                NSMiniaturizableWindowMask | NSResizableWindowMask |
   110                NSTexturedBackgroundWindowMask | NSUnifiedTitleAndToolbarWindowMask )
   111            backing: NSBackingStoreBuffered defer: NO ] == nil ) {
   112         return nil;
   113     } else {
   114         isGrabbed = NO;
   115         video = video_osx_create_drawable();
   116         [video setFrameOrigin: NSMakePoint(0.0,STATUSBAR_HEIGHT)];
   118         status = 
   119             [[NSTextField alloc] initWithFrame: NSMakeRect(0.0,0.0,videoRect.size.width,STATUSBAR_HEIGHT)];
   120         [status setStringValue: @"Idle"];
   121         [status setEditable: NO];
   122         [status setDrawsBackground: NO];
   123         [status setBordered: NO];
   124         [[self contentView] addSubview: video];
   125         [[self contentView] addSubview: status];
   126         [self makeFirstResponder: video];
   128         [self setAutorecalculatesContentBorderThickness: YES forEdge: NSMinYEdge ];
   129         [self setContentBorderThickness: 15.0 forEdge: NSMinYEdge];
   131         // Share the app delegate for the purposes of keeping it in one place
   132         [self setDelegate: [NSApp delegate]];
   133         [self setContentMinSize: contentRect.size];
   134         [self setAcceptsMouseMovedEvents: YES];
   136         NSString *title = [[NSString alloc] initWithCString: (APP_NAME " " APP_VERSION) encoding: NSASCIIStringEncoding]; 
   137         [self setTitle: title];
   139         NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier: @"LxdreamToolbar"];
   140         [toolbar setDelegate: [[LxdreamToolbarDelegate alloc] init]];
   141         [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
   142         [toolbar setSizeMode: NSToolbarSizeModeSmall];
   143         [toolbar setSelectedItemIdentifier: @"Pause"];
   144         [self setToolbar: toolbar];
   145         return self;
   146     }
   147 }
   149 - (void)setStatusText: (const gchar *)text
   150 {
   151     if( isGrabbed ) {
   152         gchar buf[128];
   153         snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
   154         NSString *s = [NSString stringWithUTF8String: buf]; 
   155         [status setStringValue: s];
   156     } else {
   157         NSString *s = [NSString stringWithUTF8String: text];
   158         [status setStringValue: s];
   159     }   
   160 }
   161 - (void)setRunning:(BOOL)isRunning
   162 {
   163     if( isRunning ) {
   164         [[self toolbar] setSelectedItemIdentifier: @"Run"];
   165         [self setStatusText: _("Running")];
   166     } else {
   167         [[self toolbar] setSelectedItemIdentifier: @"Pause"];
   168         [self setStatusText: _("Stopped")];
   169     }            
   170 }
   171 - (BOOL)isGrabbed
   172 {
   173     return isGrabbed;
   174 }
   175 - (void)setIsGrabbed:(BOOL)grab
   176 {
   177     if( grab != isGrabbed ) {
   178         isGrabbed = grab;
   179         [self setRunning: dreamcast_is_running() ? YES : NO];
   180     }
   181 }
   183 @end
   185 NSWindow *cocoa_gui_create_main_window()
   186 {
   187    NSRect contentRect = {{0,0},{640,480}};
   188    NSWindow *main_win = [[LxdreamMainWindow alloc] initWithContentRect: contentRect]; 
   189    return main_win;
   190 }
.