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