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 #define STATUSBAR_HEIGHT 25
24 #define STATUS_TEXT_HEIGHT 22
26 @interface LxdreamToolbarDelegate : NSObject {
31 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label
32 tooltip: (NSString *)tooltip
33 icon: (NSString *)icon action: (SEL) action;
36 @implementation LxdreamToolbarDelegate
39 NSToolbarItem *mount = [self createToolbarItem: @"GdromMount" label: @"Open Image"
40 tooltip: @"Mount a cdrom disc" icon: @"tb-cdrom"
41 action: @selector(mount_action:)];
42 NSToolbarItem *reset = [self createToolbarItem: @"Reset" label: @"Reset"
43 tooltip: @"Reset dreamcast" icon: @"tb-reset"
44 action: @selector(reset_action:)];
45 NSToolbarItem *pause = [self createToolbarItem: @"Pause" label: @"Pause"
46 tooltip: @"Pause dreamcast" icon: @"tb-pause"
47 action: @selector(pause_action:)];
48 NSToolbarItem *run = [self createToolbarItem: @"Run" label: @"Resume"
49 tooltip: @"Resume" icon: @"tb-run"
50 action: @selector(run_action:)];
51 NSToolbarItem *load = [self createToolbarItem: @"LoadState" label: @"Load State..."
52 tooltip: @"Load an lxdream save state" icon: @"tb-load"
53 action: @selector(load_action:)];
54 NSToolbarItem *save = [self createToolbarItem: @"SaveState" label: @"Save State..."
55 tooltip: @"Create an lxdream save state" icon: @"tb-save"
56 action: @selector(save_action:)];
57 [pause setEnabled: NO];
59 [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run", @"LoadState", @"SaveState", nil ];
61 [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run",
62 NSToolbarSeparatorItemIdentifier, @"LoadState", @"SaveState", nil ];
63 NSArray *values = [NSArray arrayWithObjects: mount, reset, pause, run, load, save, nil ];
64 items = [NSDictionary dictionaryWithObjects: values forKeys: identifiers];
68 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label
69 tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action
71 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier: id];
72 [item setLabel: label];
73 [item setToolTip: tooltip];
74 [item setTarget: [NSApp delegate]];
75 NSString *iconFile = [[NSBundle mainBundle] pathForResource:icon ofType:@"png"];
76 NSImage *image = [[NSImage alloc] initWithContentsOfFile: iconFile];
77 [item setImage: image];
78 [item setAction: action];
82 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar
87 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
92 - (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
94 return [NSArray arrayWithObjects: @"Pause", @"Run", nil];
97 - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier
98 willBeInsertedIntoToolbar:(BOOL)flag
100 return [items objectForKey: itemIdentifier];
104 @implementation LxdreamMainWindow
105 - (id)initWithContentRect:(NSRect)videoRect
107 NSRect contentRect = NSMakeRect(videoRect.origin.x,videoRect.origin.y,
108 videoRect.size.width,videoRect.size.height+STATUSBAR_HEIGHT);
109 if( [super initWithContentRect: contentRect
110 styleMask: ( NSTitledWindowMask | NSClosableWindowMask |
111 NSMiniaturizableWindowMask | NSResizableWindowMask |
112 NSUnifiedTitleAndToolbarWindowMask )
113 backing: NSBackingStoreBuffered defer: NO ] == nil ) {
117 video = video_osx_create_drawable();
118 [video setFrameOrigin: NSMakePoint(0.0,STATUSBAR_HEIGHT)];
121 [[NSTextField alloc] initWithFrame: NSMakeRect(0.0,0.0,videoRect.size.width,STATUS_TEXT_HEIGHT)];
122 [status setStringValue: @"Idle"];
123 [status setEditable: NO];
124 [status setDrawsBackground: NO];
125 [status setBordered: NO];
126 [[self contentView] addSubview: video];
127 [[self contentView] addSubview: status];
128 [self makeFirstResponder: video];
130 [self setAutorecalculatesContentBorderThickness: NO forEdge: NSMinYEdge ];
131 [self setContentBorderThickness: STATUSBAR_HEIGHT forEdge: NSMinYEdge];
133 // Share the app delegate for the purposes of keeping it in one place
134 [self setDelegate: [NSApp delegate]];
135 [self setContentMinSize: contentRect.size];
136 [self setAcceptsMouseMovedEvents: YES];
138 NSString *title = [[NSString alloc] initWithCString: lxdream_package_name encoding: NSASCIIStringEncoding];
139 [self setTitle: title];
141 NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier: @"LxdreamToolbar"];
142 [toolbar setDelegate: [[LxdreamToolbarDelegate alloc] init]];
143 [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
144 [toolbar setSizeMode: NSToolbarSizeModeSmall];
145 [toolbar setSelectedItemIdentifier: @"Pause"];
146 [self setToolbar: toolbar];
151 - (void)setStatusText: (const gchar *)text
155 snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
156 NSString *s = [NSString stringWithUTF8String: buf];
157 [status setStringValue: s];
159 NSString *s = [NSString stringWithUTF8String: text];
160 [status setStringValue: s];
163 - (void)setRunning:(BOOL)isRunning
166 [[self toolbar] setSelectedItemIdentifier: @"Run"];
167 [self setStatusText: _("Running")];
169 [[self toolbar] setSelectedItemIdentifier: @"Pause"];
170 [self setStatusText: _("Stopped")];
177 - (void)setIsGrabbed:(BOOL)grab
179 if( grab != isGrabbed ) {
181 [self setRunning: dreamcast_is_running() ? YES : NO];
187 NSWindow *cocoa_gui_create_main_window()
189 NSRect contentRect = {{0,0},{640,480}};
190 NSWindow *main_win = [[LxdreamMainWindow alloc] initWithContentRect: contentRect];
.