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 <ApplicationServices/ApplicationServices.h>
23 @interface NSWindow (OSX10_5_and_later)
24 #ifndef CGFLOAT_DEFINED
26 typedef double CGFloat;
28 typedef float CGFloat;
31 - (void)setAutorecalculatesContentBorderThickness:(BOOL)b forEdge:(NSRectEdge)e;
32 - (void)setContentBorderThickness:(CGFloat)b forEdge:(NSRectEdge)e;
36 #if NSAppKitVersionNumber > NSAppKitVersionNumber10_4
40 #define STATUSBAR_HEIGHT 25
41 #define STATUS_TEXT_HEIGHT 22
43 @interface LxdreamToolbarDelegate : NSObject {
48 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label
49 tooltip: (NSString *)tooltip
50 icon: (NSString *)icon action: (SEL) action;
53 @implementation LxdreamToolbarDelegate
56 NSToolbarItem *mount = [self createToolbarItem: @"GdromMount" label: @"Open Image"
57 tooltip: @"Mount a cdrom disc" icon: @"tb-cdrom"
58 action: @selector(mount_action:)];
59 NSToolbarItem *reset = [self createToolbarItem: @"Reset" label: @"Reset"
60 tooltip: @"Reset dreamcast" icon: @"tb-reset"
61 action: @selector(reset_action:)];
62 NSToolbarItem *pause = [self createToolbarItem: @"Pause" label: @"Pause"
63 tooltip: @"Pause dreamcast" icon: @"tb-pause"
64 action: @selector(pause_action:)];
65 NSToolbarItem *run = [self createToolbarItem: @"Run" label: @"Resume"
66 tooltip: @"Resume" icon: @"tb-run"
67 action: @selector(run_action:)];
68 NSToolbarItem *load = [self createToolbarItem: @"LoadState" label: @"Load State..."
69 tooltip: @"Load an lxdream save state" icon: @"tb-load"
70 action: @selector(load_action:)];
71 NSToolbarItem *save = [self createToolbarItem: @"SaveState" label: @"Save State..."
72 tooltip: @"Create an lxdream save state" icon: @"tb-save"
73 action: @selector(save_action:)];
74 [pause setEnabled: NO];
76 [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run", @"LoadState", @"SaveState", nil ];
78 [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run",
79 NSToolbarSeparatorItemIdentifier, @"LoadState", @"SaveState", nil ];
80 NSArray *values = [NSArray arrayWithObjects: mount, reset, pause, run, load, save, nil ];
81 items = [NSDictionary dictionaryWithObjects: values forKeys: identifiers];
85 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label
86 tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action
88 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier: id];
89 [item setLabel: label];
90 [item setToolTip: tooltip];
91 [item setTarget: [NSApp delegate]];
92 NSString *iconFile = [[NSBundle mainBundle] pathForResource:icon ofType:@"png"];
93 NSImage *image = [[NSImage alloc] initWithContentsOfFile: iconFile];
94 [item setImage: image];
95 [item setAction: action];
99 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar
104 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
109 - (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
111 return [NSArray arrayWithObjects: @"Pause", @"Run", nil];
114 - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier
115 willBeInsertedIntoToolbar:(BOOL)flag
117 return [items objectForKey: itemIdentifier];
121 @implementation LxdreamMainWindow
122 - (id)initWithContentRect:(NSRect)videoRect
124 NSRect contentRect = NSMakeRect(videoRect.origin.x,videoRect.origin.y,
125 videoRect.size.width,videoRect.size.height+STATUSBAR_HEIGHT);
126 if( [super initWithContentRect: contentRect
127 styleMask: ( NSTitledWindowMask | NSClosableWindowMask |
128 NSMiniaturizableWindowMask | NSResizableWindowMask |
129 NSUnifiedTitleAndToolbarWindowMask )
130 backing: NSBackingStoreBuffered defer: NO ] == nil ) {
134 video = video_osx_create_drawable();
135 [video setFrameOrigin: NSMakePoint(0.0,STATUSBAR_HEIGHT)];
138 [[NSTextField alloc] initWithFrame: NSMakeRect(0.0,0.0,videoRect.size.width,STATUS_TEXT_HEIGHT)];
139 [status setStringValue: @"Idle"];
140 [status setEditable: NO];
141 [status setDrawsBackground: NO];
142 [status setBordered: NO];
143 [[self contentView] addSubview: video];
144 [[self contentView] addSubview: status];
145 [self makeFirstResponder: video];
147 if( [self respondsToSelector:@selector(setAutorecalculatesContentBorderThickness:forEdge:)] )
148 [self setAutorecalculatesContentBorderThickness: NO forEdge: NSMinYEdge ];
149 if( [self respondsToSelector:@selector(setContentBorderThickness:forEdge:)] )
150 [self setContentBorderThickness: STATUSBAR_HEIGHT forEdge: NSMinYEdge];
152 // Share the app delegate for the purposes of keeping it in one place
153 [self setDelegate: [NSApp delegate]];
154 [self setContentMinSize: contentRect.size];
155 [self setAcceptsMouseMovedEvents: YES];
157 NSString *title = [[NSString alloc] initWithCString: lxdream_package_name encoding: NSASCIIStringEncoding];
158 [self setTitle: title];
160 NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier: @"LxdreamToolbar"];
161 [toolbar setDelegate: [[LxdreamToolbarDelegate alloc] init]];
162 [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
163 [toolbar setSizeMode: NSToolbarSizeModeSmall];
164 [toolbar setSelectedItemIdentifier: @"Pause"];
165 [self setToolbar: toolbar];
170 - (void)setStatusText: (const gchar *)text
174 snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
175 NSString *s = [NSString stringWithUTF8String: buf];
176 [status setStringValue: s];
178 NSString *s = [NSString stringWithUTF8String: text];
179 [status setStringValue: s];
182 - (void)setRunning:(BOOL)isRunning
185 [[self toolbar] setSelectedItemIdentifier: @"Run"];
186 [self setStatusText: _("Running")];
188 [[self toolbar] setSelectedItemIdentifier: @"Pause"];
189 [self setStatusText: _("Stopped")];
196 - (void)setIsGrabbed:(BOOL)grab
198 if( grab != isGrabbed ) {
200 [self setRunning: dreamcast_is_running() ? YES : NO];
206 NSWindow *cocoa_gui_create_main_window()
208 NSRect contentRect = {{0,0},{640,480}};
209 NSWindow *main_win = [[LxdreamMainWindow alloc] initWithContentRect: contentRect];
.