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>
25 @interface NSWindow (OSX10_5_and_later)
26 #ifndef CGFLOAT_DEFINED
28 typedef double CGFloat;
30 typedef float CGFloat;
33 - (void)setAutorecalculatesContentBorderThickness:(BOOL)b forEdge:(NSRectEdge)e;
34 - (void)setContentBorderThickness:(CGFloat)b forEdge:(NSRectEdge)e;
38 #if NSAppKitVersionNumber > NSAppKitVersionNumber10_4
42 #define STATUSBAR_HEIGHT 25
43 #define STATUS_TEXT_HEIGHT 22
45 @interface LxdreamToolbarDelegate : NSObject {
50 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label
51 tooltip: (NSString *)tooltip
52 icon: (NSString *)icon action: (SEL) action;
55 @implementation LxdreamToolbarDelegate
58 NSToolbarItem *mount = [self createToolbarItem: @"GdromMount" label: @"Open Image"
59 tooltip: @"Mount a cdrom disc" icon: @"tb-cdrom"
60 action: @selector(mount_action:)];
61 NSToolbarItem *reset = [self createToolbarItem: @"Reset" label: @"Reset"
62 tooltip: @"Reset dreamcast" icon: @"tb-reset"
63 action: @selector(reset_action:)];
64 NSToolbarItem *pause = [self createToolbarItem: @"Pause" label: @"Pause"
65 tooltip: @"Pause dreamcast" icon: @"tb-pause"
66 action: @selector(pause_action:)];
67 NSToolbarItem *run = [self createToolbarItem: @"Run" label: @"Resume"
68 tooltip: @"Resume" icon: @"tb-run"
69 action: @selector(run_action:)];
70 NSToolbarItem *load = [self createToolbarItem: @"LoadState" label: @"Load State..."
71 tooltip: @"Load an lxdream save state" icon: @"tb-load"
72 action: @selector(load_action:)];
73 NSToolbarItem *save = [self createToolbarItem: @"SaveState" label: @"Save State..."
74 tooltip: @"Create an lxdream save state" icon: @"tb-save"
75 action: @selector(save_action:)];
76 NSToolbarItem *prefs = [self createToolbarItem: @"Preferences" label: @"Preferences..."
77 tooltip: @"Edit preferences" icon: @"tb-preferences"
78 action: @selector(preferences_action:)];
79 [pause setEnabled: NO];
81 [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run", @"LoadState", @"SaveState", @"Preferences", nil ];
83 [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run",
84 NSToolbarSeparatorItemIdentifier, @"LoadState", @"SaveState",
85 NSToolbarFlexibleSpaceItemIdentifier, @"Preferences", nil ];
86 NSArray *values = [NSArray arrayWithObjects: mount, reset, pause, run, load, save, prefs, nil ];
87 items = [NSDictionary dictionaryWithObjects: values forKeys: identifiers];
91 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label
92 tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action
94 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier: id];
95 [item setLabel: label];
96 [item setToolTip: tooltip];
97 [item setTarget: [NSApp delegate]];
98 NSString *iconFile = [[NSBundle mainBundle] pathForResource:icon ofType:@"png"];
99 NSImage *image = [[NSImage alloc] initWithContentsOfFile: iconFile];
100 [item setImage: image];
101 [item setAction: action];
105 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar
110 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
115 - (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
117 return [NSArray arrayWithObjects: @"Pause", @"Run", nil];
120 - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier
121 willBeInsertedIntoToolbar:(BOOL)flag
123 return [items objectForKey: itemIdentifier];
127 @implementation LxdreamMainWindow
128 - (id)initWithContentRect:(NSRect)videoRect
130 NSRect contentRect = NSMakeRect(videoRect.origin.x,videoRect.origin.y,
131 videoRect.size.width,videoRect.size.height+STATUSBAR_HEIGHT);
132 if( [super initWithContentRect: contentRect
133 styleMask: ( NSTitledWindowMask | NSClosableWindowMask |
134 NSMiniaturizableWindowMask | NSResizableWindowMask |
135 NSUnifiedTitleAndToolbarWindowMask )
136 backing: NSBackingStoreBuffered defer: NO ] == nil ) {
141 video = (LxdreamVideoView *)video_osx_create_drawable();
142 [video setFrameOrigin: NSMakePoint(0.0,STATUSBAR_HEIGHT)];
143 [video setDelegate: self];
146 [[NSTextField alloc] initWithFrame: NSMakeRect(0.0,0.0,videoRect.size.width,STATUS_TEXT_HEIGHT)];
147 [status setStringValue: @"Idle"];
148 [status setEditable: NO];
149 [status setDrawsBackground: NO];
150 [status setBordered: NO];
151 [[self contentView] addSubview: video];
152 [[self contentView] addSubview: status];
153 [self makeFirstResponder: video];
155 if( [self respondsToSelector:@selector(setAutorecalculatesContentBorderThickness:forEdge:)] )
156 [self setAutorecalculatesContentBorderThickness: NO forEdge: NSMinYEdge ];
157 if( [self respondsToSelector:@selector(setContentBorderThickness:forEdge:)] )
158 [self setContentBorderThickness: STATUSBAR_HEIGHT forEdge: NSMinYEdge];
160 // Share the app delegate for the purposes of keeping it in one place
161 [self setDelegate: [NSApp delegate]];
162 [self setContentMinSize: contentRect.size];
163 [self setAcceptsMouseMovedEvents: YES];
166 NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier: @"LxdreamToolbar"];
167 [toolbar setDelegate: [[LxdreamToolbarDelegate alloc] init]];
168 [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
169 [toolbar setSizeMode: NSToolbarSizeModeSmall];
170 [toolbar setSelectedItemIdentifier: @"Pause"];
171 [self setToolbar: toolbar];
179 const char *disc_title = gdrom_get_current_disc_title();
180 if( disc_title == NULL ) {
181 title = [NSString stringWithCString: lxdream_package_name];
183 title = [NSString stringWithFormat: @"%s - %s", lxdream_package_name, disc_title];
185 [self setTitle: title];
188 - (void)setStatusText: (const gchar *)text
192 snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
193 NSString *s = [NSString stringWithUTF8String: buf];
194 [status setStringValue: s];
196 NSString *s = [NSString stringWithUTF8String: text];
197 [status setStringValue: s];
200 - (void)setRunning:(BOOL)isRunning
203 [[self toolbar] setSelectedItemIdentifier: @"Run"];
204 [self setStatusText: _("Running")];
206 [[self toolbar] setSelectedItemIdentifier: @"Pause"];
207 [self setStatusText: _("Stopped")];
214 - (void)setIsGrabbed:(BOOL)grab
216 if( grab != isGrabbed ) {
218 [self setRunning: dreamcast_is_running() ? YES : NO];
222 CGAssociateMouseAndMouseCursorPosition(NO);
225 CGAssociateMouseAndMouseCursorPosition(YES);
227 [video setIsGrabbed: isGrabbed];
230 - (void)setUseGrab:(BOOL)grab
232 if( grab != useGrab ) {
233 if( !grab && isGrabbed ) {
234 [self setIsGrabbed: NO];
240 - (id)viewRequestedGrab: (id)sender
243 [self setIsGrabbed: YES];
245 return useGrab ? self : nil;
247 - (id)viewRequestedUngrab: (id)sender
249 [self setIsGrabbed: NO];
250 return useGrab ? self : nil;
254 LxdreamMainWindow *cocoa_gui_create_main_window()
256 NSRect contentRect = {{0,0},{640,480}};
257 return [[LxdreamMainWindow alloc] initWithContentRect: contentRect];
.