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