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
file annotate diff log raw
nkeynes@681
     1
/**
nkeynes@681
     2
 * $Id$
nkeynes@681
     3
 *
nkeynes@681
     4
 * Construct and maintain the main window under cocoa.
nkeynes@681
     5
 *
nkeynes@681
     6
 * Copyright (c) 2008 Nathan Keynes.
nkeynes@681
     7
 *
nkeynes@681
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@681
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@681
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@681
    11
 * (at your option) any later version.
nkeynes@681
    12
 *
nkeynes@681
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@681
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@681
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@681
    16
 * GNU General Public License for more details.
nkeynes@681
    17
 */
nkeynes@681
    18
nkeynes@681
    19
#include "cocoaui/cocoaui.h"
nkeynes@681
    20
#include "lxdream.h"
nkeynes@786
    21
#include "dreamcast.h"
nkeynes@681
    22
#include <ApplicationServices/ApplicationServices.h>
nkeynes@681
    23
nkeynes@757
    24
@interface NSWindow (OSX10_5_and_later)
nkeynes@757
    25
#ifndef CGFLOAT_DEFINED
nkeynes@757
    26
# ifdef __LP64__
nkeynes@757
    27
    typedef double CGFloat;
nkeynes@757
    28
# else
nkeynes@757
    29
    typedef float CGFloat;
nkeynes@757
    30
# endif
nkeynes@757
    31
#endif
nkeynes@757
    32
- (void)setAutorecalculatesContentBorderThickness:(BOOL)b forEdge:(NSRectEdge)e;
nkeynes@757
    33
- (void)setContentBorderThickness:(CGFloat)b forEdge:(NSRectEdge)e;
nkeynes@757
    34
@end
nkeynes@757
    35
nkeynes@757
    36
nkeynes@757
    37
#if NSAppKitVersionNumber > NSAppKitVersionNumber10_4 
nkeynes@757
    38
nkeynes@757
    39
#endif
nkeynes@757
    40
nkeynes@705
    41
#define STATUSBAR_HEIGHT 25
nkeynes@705
    42
#define STATUS_TEXT_HEIGHT 22
nkeynes@681
    43
nkeynes@681
    44
@interface LxdreamToolbarDelegate : NSObject {
nkeynes@681
    45
    NSArray *identifiers;
nkeynes@681
    46
    NSArray *defaults;
nkeynes@681
    47
    NSDictionary *items;
nkeynes@681
    48
}
nkeynes@681
    49
- (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label 
nkeynes@736
    50
                              tooltip: (NSString *)tooltip 
nkeynes@736
    51
                                 icon: (NSString *)icon action: (SEL) action; 
nkeynes@681
    52
@end
nkeynes@681
    53
nkeynes@681
    54
@implementation LxdreamToolbarDelegate
nkeynes@681
    55
- (id) init
nkeynes@681
    56
{
nkeynes@681
    57
    NSToolbarItem *mount = [self createToolbarItem: @"GdromMount" label: @"Open Image" 
nkeynes@681
    58
                            tooltip: @"Mount a cdrom disc" icon: @"tb-cdrom" 
nkeynes@681
    59
                            action: @selector(mount_action:)];
nkeynes@681
    60
    NSToolbarItem *reset = [self createToolbarItem: @"Reset" label: @"Reset"
nkeynes@681
    61
                            tooltip: @"Reset dreamcast" icon: @"tb-reset"
nkeynes@681
    62
                            action: @selector(reset_action:)];
nkeynes@681
    63
    NSToolbarItem *pause = [self createToolbarItem: @"Pause" label: @"Pause"
nkeynes@681
    64
                            tooltip: @"Pause dreamcast" icon: @"tb-pause"
nkeynes@681
    65
                            action: @selector(pause_action:)];
nkeynes@681
    66
    NSToolbarItem *run = [self createToolbarItem: @"Run" label: @"Resume"
nkeynes@736
    67
                          tooltip: @"Resume" icon: @"tb-run"
nkeynes@736
    68
                          action: @selector(run_action:)];
nkeynes@681
    69
    NSToolbarItem *load = [self createToolbarItem: @"LoadState" label: @"Load State..."
nkeynes@736
    70
                           tooltip: @"Load an lxdream save state" icon: @"tb-load"
nkeynes@736
    71
                           action: @selector(load_action:)];
nkeynes@681
    72
    NSToolbarItem *save = [self createToolbarItem: @"SaveState" label: @"Save State..."
nkeynes@736
    73
                           tooltip: @"Create an lxdream save state" icon: @"tb-save"
nkeynes@736
    74
                           action: @selector(save_action:)];
nkeynes@681
    75
    [pause setEnabled: NO];
nkeynes@681
    76
    identifiers = 
nkeynes@681
    77
        [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run", @"LoadState", @"SaveState", nil ];
nkeynes@681
    78
    defaults = 
nkeynes@681
    79
        [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run", 
nkeynes@736
    80
         NSToolbarSeparatorItemIdentifier, @"LoadState", @"SaveState", nil ];
nkeynes@681
    81
    NSArray *values = [NSArray arrayWithObjects: mount, reset, pause, run, load, save, nil ];
nkeynes@681
    82
    items = [NSDictionary dictionaryWithObjects: values forKeys: identifiers];
nkeynes@681
    83
    return self;
nkeynes@681
    84
}
nkeynes@681
    85
nkeynes@681
    86
- (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label 
nkeynes@736
    87
tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action 
nkeynes@681
    88
{
nkeynes@681
    89
    NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier: id];
nkeynes@681
    90
    [item setLabel: label];
nkeynes@681
    91
    [item setToolTip: tooltip];
nkeynes@681
    92
    [item setTarget: [NSApp delegate]];
nkeynes@681
    93
    NSString *iconFile = [[NSBundle mainBundle] pathForResource:icon ofType:@"png"];
nkeynes@681
    94
    NSImage *image = [[NSImage alloc] initWithContentsOfFile: iconFile];
nkeynes@681
    95
    [item setImage: image];
nkeynes@681
    96
    [item setAction: action];
nkeynes@681
    97
    return item;
nkeynes@681
    98
}
nkeynes@681
    99
nkeynes@681
   100
- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar 
nkeynes@681
   101
{
nkeynes@681
   102
    return identifiers;
nkeynes@681
   103
}
nkeynes@681
   104
nkeynes@681
   105
- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
nkeynes@681
   106
{
nkeynes@681
   107
    return defaults;
nkeynes@681
   108
}
nkeynes@681
   109
nkeynes@681
   110
- (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
nkeynes@681
   111
{
nkeynes@681
   112
    return [NSArray arrayWithObjects: @"Pause", @"Run", nil];
nkeynes@681
   113
}
nkeynes@681
   114
nkeynes@681
   115
- (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier
nkeynes@736
   116
willBeInsertedIntoToolbar:(BOOL)flag 
nkeynes@681
   117
{
nkeynes@736
   118
    return [items objectForKey: itemIdentifier];
nkeynes@681
   119
}
nkeynes@681
   120
@end
nkeynes@681
   121
nkeynes@681
   122
@implementation LxdreamMainWindow
nkeynes@681
   123
- (id)initWithContentRect:(NSRect)videoRect 
nkeynes@681
   124
{
nkeynes@681
   125
    NSRect contentRect = NSMakeRect(videoRect.origin.x,videoRect.origin.y,
nkeynes@681
   126
            videoRect.size.width,videoRect.size.height+STATUSBAR_HEIGHT);
nkeynes@681
   127
    if( [super initWithContentRect: contentRect
nkeynes@736
   128
         styleMask: ( NSTitledWindowMask | NSClosableWindowMask | 
nkeynes@736
   129
                 NSMiniaturizableWindowMask | NSResizableWindowMask |
nkeynes@736
   130
                 NSUnifiedTitleAndToolbarWindowMask )
nkeynes@736
   131
                 backing: NSBackingStoreBuffered defer: NO ] == nil ) {
nkeynes@681
   132
        return nil;
nkeynes@681
   133
    } else {
nkeynes@681
   134
        isGrabbed = NO;
nkeynes@780
   135
        video = (LxdreamVideoView *)video_osx_create_drawable();
nkeynes@681
   136
        [video setFrameOrigin: NSMakePoint(0.0,STATUSBAR_HEIGHT)];
nkeynes@780
   137
        [video setDelegate: self];
nkeynes@736
   138
nkeynes@681
   139
        status = 
nkeynes@705
   140
            [[NSTextField alloc] initWithFrame: NSMakeRect(0.0,0.0,videoRect.size.width,STATUS_TEXT_HEIGHT)];
nkeynes@681
   141
        [status setStringValue: @"Idle"];
nkeynes@681
   142
        [status setEditable: NO];
nkeynes@681
   143
        [status setDrawsBackground: NO];
nkeynes@681
   144
        [status setBordered: NO];
nkeynes@681
   145
        [[self contentView] addSubview: video];
nkeynes@681
   146
        [[self contentView] addSubview: status];
nkeynes@681
   147
        [self makeFirstResponder: video];
nkeynes@681
   148
nkeynes@757
   149
        if( [self respondsToSelector:@selector(setAutorecalculatesContentBorderThickness:forEdge:)] )
nkeynes@757
   150
            [self setAutorecalculatesContentBorderThickness: NO forEdge: NSMinYEdge ];
nkeynes@757
   151
        if( [self respondsToSelector:@selector(setContentBorderThickness:forEdge:)] ) 
nkeynes@757
   152
            [self setContentBorderThickness: STATUSBAR_HEIGHT forEdge: NSMinYEdge];
nkeynes@681
   153
nkeynes@681
   154
        // Share the app delegate for the purposes of keeping it in one place
nkeynes@681
   155
        [self setDelegate: [NSApp delegate]];
nkeynes@681
   156
        [self setContentMinSize: contentRect.size];
nkeynes@681
   157
        [self setAcceptsMouseMovedEvents: YES];
nkeynes@681
   158
nkeynes@738
   159
        NSString *title = [[NSString alloc] initWithCString: lxdream_package_name encoding: NSASCIIStringEncoding]; 
nkeynes@681
   160
        [self setTitle: title];
nkeynes@681
   161
nkeynes@681
   162
        NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier: @"LxdreamToolbar"];
nkeynes@681
   163
        [toolbar setDelegate: [[LxdreamToolbarDelegate alloc] init]];
nkeynes@681
   164
        [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
nkeynes@681
   165
        [toolbar setSizeMode: NSToolbarSizeModeSmall];
nkeynes@681
   166
        [toolbar setSelectedItemIdentifier: @"Pause"];
nkeynes@681
   167
        [self setToolbar: toolbar];
nkeynes@681
   168
        return self;
nkeynes@681
   169
    }
nkeynes@681
   170
}
nkeynes@681
   171
nkeynes@681
   172
- (void)setStatusText: (const gchar *)text
nkeynes@681
   173
{
nkeynes@681
   174
    if( isGrabbed ) {
nkeynes@681
   175
        gchar buf[128];
nkeynes@681
   176
        snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
nkeynes@681
   177
        NSString *s = [NSString stringWithUTF8String: buf]; 
nkeynes@681
   178
        [status setStringValue: s];
nkeynes@681
   179
    } else {
nkeynes@681
   180
        NSString *s = [NSString stringWithUTF8String: text];
nkeynes@681
   181
        [status setStringValue: s];
nkeynes@681
   182
    }   
nkeynes@681
   183
}
nkeynes@681
   184
- (void)setRunning:(BOOL)isRunning
nkeynes@681
   185
{
nkeynes@681
   186
    if( isRunning ) {
nkeynes@681
   187
        [[self toolbar] setSelectedItemIdentifier: @"Run"];
nkeynes@681
   188
        [self setStatusText: _("Running")];
nkeynes@681
   189
    } else {
nkeynes@681
   190
        [[self toolbar] setSelectedItemIdentifier: @"Pause"];
nkeynes@681
   191
        [self setStatusText: _("Stopped")];
nkeynes@681
   192
    }            
nkeynes@681
   193
}
nkeynes@681
   194
- (BOOL)isGrabbed
nkeynes@681
   195
{
nkeynes@681
   196
    return isGrabbed;
nkeynes@681
   197
}
nkeynes@681
   198
- (void)setIsGrabbed:(BOOL)grab
nkeynes@681
   199
{
nkeynes@681
   200
    if( grab != isGrabbed ) {
nkeynes@681
   201
        isGrabbed = grab;
nkeynes@681
   202
        [self setRunning: dreamcast_is_running() ? YES : NO];
nkeynes@780
   203
        
nkeynes@780
   204
        if( isGrabbed ) {
nkeynes@780
   205
            [NSCursor hide];
nkeynes@780
   206
            CGAssociateMouseAndMouseCursorPosition(NO);
nkeynes@780
   207
        } else {
nkeynes@780
   208
            [NSCursor unhide];
nkeynes@780
   209
            CGAssociateMouseAndMouseCursorPosition(YES);
nkeynes@780
   210
        }
nkeynes@780
   211
        [video setIsGrabbed: isGrabbed];
nkeynes@681
   212
    }
nkeynes@681
   213
}
nkeynes@780
   214
- (void)viewRequestedGrab: (id)sender
nkeynes@780
   215
{
nkeynes@780
   216
    [self setIsGrabbed: YES];
nkeynes@780
   217
}
nkeynes@780
   218
- (void)viewRequestedUngrab: (id)sender
nkeynes@780
   219
{
nkeynes@780
   220
    [self setIsGrabbed: NO];
nkeynes@780
   221
}
nkeynes@681
   222
@end
nkeynes@681
   223
nkeynes@681
   224
NSWindow *cocoa_gui_create_main_window()
nkeynes@681
   225
{
nkeynes@736
   226
    NSRect contentRect = {{0,0},{640,480}};
nkeynes@736
   227
    NSWindow *main_win = [[LxdreamMainWindow alloc] initWithContentRect: contentRect]; 
nkeynes@736
   228
    return main_win;
nkeynes@681
   229
}
.