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