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