4 * Construct and maintain the main window under cocoa.
6 * Copyright (c) 2008 Nathan Keynes.
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.
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.
19 #include "cocoaui/cocoaui.h"
21 #include "dreamcast.h"
22 #include <ApplicationServices/ApplicationServices.h>
24 @interface NSWindow (OSX10_5_and_later)
25 #ifndef CGFLOAT_DEFINED
27 typedef double CGFloat;
29 typedef float CGFloat;
32 - (void)setAutorecalculatesContentBorderThickness:(BOOL)b forEdge:(NSRectEdge)e;
33 - (void)setContentBorderThickness:(CGFloat)b forEdge:(NSRectEdge)e;
37 #if NSAppKitVersionNumber > NSAppKitVersionNumber10_4
41 #define STATUSBAR_HEIGHT 25
42 #define STATUS_TEXT_HEIGHT 22
44 @interface LxdreamToolbarDelegate : NSObject {
49 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label
50 tooltip: (NSString *)tooltip
51 icon: (NSString *)icon action: (SEL) action;
54 @implementation LxdreamToolbarDelegate
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];
77 [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run", @"LoadState", @"SaveState", nil ];
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];
86 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label
87 tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action
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];
100 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar
105 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
110 - (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
112 return [NSArray arrayWithObjects: @"Pause", @"Run", nil];
115 - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier
116 willBeInsertedIntoToolbar:(BOOL)flag
118 return [items objectForKey: itemIdentifier];
122 @implementation LxdreamMainWindow
123 - (id)initWithContentRect:(NSRect)videoRect
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 ) {
135 video = (LxdreamVideoView *)video_osx_create_drawable();
136 [video setFrameOrigin: NSMakePoint(0.0,STATUSBAR_HEIGHT)];
137 [video setDelegate: self];
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];
172 - (void)setStatusText: (const gchar *)text
176 snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
177 NSString *s = [NSString stringWithUTF8String: buf];
178 [status setStringValue: s];
180 NSString *s = [NSString stringWithUTF8String: text];
181 [status setStringValue: s];
184 - (void)setRunning:(BOOL)isRunning
187 [[self toolbar] setSelectedItemIdentifier: @"Run"];
188 [self setStatusText: _("Running")];
190 [[self toolbar] setSelectedItemIdentifier: @"Pause"];
191 [self setStatusText: _("Stopped")];
198 - (void)setIsGrabbed:(BOOL)grab
200 if( grab != isGrabbed ) {
202 [self setRunning: dreamcast_is_running() ? YES : NO];
206 CGAssociateMouseAndMouseCursorPosition(NO);
209 CGAssociateMouseAndMouseCursorPosition(YES);
211 [video setIsGrabbed: isGrabbed];
214 - (void)viewRequestedGrab: (id)sender
216 [self setIsGrabbed: YES];
218 - (void)viewRequestedUngrab: (id)sender
220 [self setIsGrabbed: NO];
224 NSWindow *cocoa_gui_create_main_window()
226 NSRect contentRect = {{0,0},{640,480}};
227 NSWindow *main_win = [[LxdreamMainWindow alloc] initWithContentRect: contentRect];
.