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 "gdrom/gdrom.h"
23 #include <ApplicationServices/ApplicationServices.h>
24 #include <Carbon/Carbon.h>
26 @interface NSWindow (OSX10_5_and_later)
27 #ifndef CGFLOAT_DEFINED
29 typedef double CGFloat;
31 typedef float CGFloat;
34 - (void)setAutorecalculatesContentBorderThickness:(BOOL)b forEdge:(NSRectEdge)e;
35 - (void)setContentBorderThickness:(CGFloat)b forEdge:(NSRectEdge)e;
39 #if NSAppKitVersionNumber > NSAppKitVersionNumber10_4
43 #define STATUSBAR_HEIGHT 25
44 #define STATUS_TEXT_HEIGHT 22
46 @interface LxdreamToolbarDelegate : NSObject {
51 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label
52 tooltip: (NSString *)tooltip
53 icon: (NSString *)icon action: (SEL) action;
56 @implementation LxdreamToolbarDelegate
59 NSToolbarItem *mount = [self createToolbarItem: @"GdromMount" label: @"Open Image"
60 tooltip: @"Mount a cdrom disc" icon: @"tb-cdrom"
61 action: @selector(mount_action:)];
62 NSToolbarItem *reset = [self createToolbarItem: @"Reset" label: @"Reset"
63 tooltip: @"Reset dreamcast" icon: @"tb-reset"
64 action: @selector(reset_action:)];
65 NSToolbarItem *pause = [self createToolbarItem: @"Pause" label: @"Pause"
66 tooltip: @"Pause dreamcast" icon: @"tb-pause"
67 action: @selector(pause_action:)];
68 NSToolbarItem *run = [self createToolbarItem: @"Run" label: @"Resume"
69 tooltip: @"Resume" icon: @"tb-run"
70 action: @selector(run_action:)];
71 NSToolbarItem *load = [self createToolbarItem: @"LoadState" label: @"Load State..."
72 tooltip: @"Load an lxdream save state" icon: @"tb-load"
73 action: @selector(load_action:)];
74 NSToolbarItem *save = [self createToolbarItem: @"SaveState" label: @"Save State..."
75 tooltip: @"Create an lxdream save state" icon: @"tb-save"
76 action: @selector(save_action:)];
77 NSToolbarItem *prefs = [self createToolbarItem: @"Preferences" label: @"Preferences..."
78 tooltip: @"Edit preferences" icon: @"tb-preferences"
79 action: @selector(preferences_action:)];
80 [pause setEnabled: NO];
82 [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run", @"LoadState", @"SaveState", @"Preferences", nil ];
84 [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run",
85 NSToolbarSeparatorItemIdentifier, @"LoadState", @"SaveState",
86 NSToolbarFlexibleSpaceItemIdentifier, @"Preferences", nil ];
87 NSArray *values = [NSArray arrayWithObjects: mount, reset, pause, run, load, save, prefs, nil ];
88 items = [NSDictionary dictionaryWithObjects: values forKeys: identifiers];
92 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label
93 tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action
95 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier: id];
96 [item setLabel: label];
97 [item setToolTip: tooltip];
98 [item setTarget: [NSApp delegate]];
99 NSString *iconFile = [[NSBundle mainBundle] pathForResource:icon ofType:@"png"];
100 NSImage *image = [[NSImage alloc] initWithContentsOfFile: iconFile];
101 [item setImage: image];
102 [item setAction: action];
106 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar
111 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
116 - (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
118 return [NSArray arrayWithObjects: @"Pause", @"Run", nil];
121 - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier
122 willBeInsertedIntoToolbar:(BOOL)flag
124 return [items objectForKey: itemIdentifier];
128 @implementation LxdreamMainWindow
129 - (id)initWithContentRect:(NSRect)videoRect
131 NSRect contentRect = NSMakeRect(videoRect.origin.x,videoRect.origin.y,
132 videoRect.size.width,videoRect.size.height+STATUSBAR_HEIGHT);
133 if( [super initWithContentRect: contentRect
134 styleMask: ( NSTitledWindowMask | NSClosableWindowMask |
135 NSMiniaturizableWindowMask | NSResizableWindowMask |
136 NSUnifiedTitleAndToolbarWindowMask )
137 backing: NSBackingStoreBuffered defer: NO ] == nil ) {
142 video = (LxdreamVideoView *)video_osx_create_drawable();
143 [video setFrameOrigin: NSMakePoint(0.0,STATUSBAR_HEIGHT)];
144 [video setDelegate: self];
147 [[NSTextField alloc] initWithFrame: NSMakeRect(0.0,0.0,videoRect.size.width,STATUS_TEXT_HEIGHT)];
148 [status setStringValue: @"Idle"];
149 [status setEditable: NO];
150 [status setDrawsBackground: NO];
151 [status setBordered: NO];
152 [[self contentView] addSubview: video];
153 [[self contentView] addSubview: status];
154 [self makeFirstResponder: video];
156 if( [self respondsToSelector:@selector(setAutorecalculatesContentBorderThickness:forEdge:)] )
157 [self setAutorecalculatesContentBorderThickness: NO forEdge: NSMinYEdge ];
158 if( [self respondsToSelector:@selector(setContentBorderThickness:forEdge:)] )
159 [self setContentBorderThickness: STATUSBAR_HEIGHT forEdge: NSMinYEdge];
161 // Share the app delegate for the purposes of keeping it in one place
162 [self setDelegate: [NSApp delegate]];
163 [self setContentMinSize: contentRect.size];
164 [self setAcceptsMouseMovedEvents: YES];
167 NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier: @"LxdreamToolbar"];
168 [toolbar setDelegate: [[LxdreamToolbarDelegate alloc] init]];
169 [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
170 [toolbar setSizeMode: NSToolbarSizeModeSmall];
171 [toolbar setSelectedItemIdentifier: @"Pause"];
172 [self setToolbar: toolbar];
180 const char *disc_title = gdrom_get_current_disc_title();
181 if( disc_title == NULL ) {
182 title = [NSString stringWithFormat: @"%s - <no disc>", lxdream_package_name];
184 title = [NSString stringWithFormat: @"%s - %s", lxdream_package_name, disc_title];
186 [self setTitle: title];
189 - (void)setStatusText: (const gchar *)text
193 snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
194 NSString *s = [NSString stringWithUTF8String: buf];
195 [status setStringValue: s];
197 NSString *s = [NSString stringWithUTF8String: text];
198 [status setStringValue: s];
201 - (void)setRunning:(BOOL)isRunning
204 [[self toolbar] setSelectedItemIdentifier: @"Run"];
205 [self setStatusText: _("Running")];
207 [[self toolbar] setSelectedItemIdentifier: @"Pause"];
208 [self setStatusText: _("Stopped")];
215 - (void)setFullscreen:(BOOL)full
217 if( full != isFullscreen ) {
221 savedFrame = [self frame];
222 SetSystemUIMode( kUIModeAllHidden, 0 );
223 NSRect screenRect = [[NSScreen mainScreen] frame];
224 screenRect.size.height += STATUSBAR_HEIGHT;
225 screenRect.origin.y -= STATUSBAR_HEIGHT;
226 NSRect targetRect = [self frameRectForContentRect: screenRect];
227 [self setFrame: targetRect display: YES];
229 SetSystemUIMode( kUIModeNormal, 0 );
230 [self setFrame: savedFrame display: YES];
238 - (void)setIsGrabbed:(BOOL)grab
240 if( grab != isGrabbed ) {
242 [self setRunning: dreamcast_is_running() ? YES : NO];
246 CGAssociateMouseAndMouseCursorPosition(NO);
249 CGAssociateMouseAndMouseCursorPosition(YES);
251 [video setIsGrabbed: isGrabbed];
254 - (void)setUseGrab:(BOOL)grab
256 if( grab != useGrab ) {
257 if( !grab && isGrabbed ) {
258 [self setIsGrabbed: NO];
264 - (id)viewRequestedGrab: (id)sender
267 [self setIsGrabbed: YES];
269 return useGrab ? self : nil;
271 - (id)viewRequestedUngrab: (id)sender
273 [self setIsGrabbed: NO];
274 return useGrab ? self : nil;
276 - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)aScreen
282 LxdreamMainWindow *cocoa_gui_create_main_window()
284 NSRect contentRect = {{0,0},{640,480}};
285 return [[LxdreamMainWindow alloc] initWithContentRect: contentRect];
.