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 20
25 @interface LxdreamToolbarDelegate : NSObject {
30 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label
31 tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action;
34 @implementation LxdreamToolbarDelegate
37 NSToolbarItem *mount = [self createToolbarItem: @"GdromMount" label: @"Open Image"
38 tooltip: @"Mount a cdrom disc" icon: @"tb-cdrom"
39 action: @selector(mount_action:)];
40 NSToolbarItem *reset = [self createToolbarItem: @"Reset" label: @"Reset"
41 tooltip: @"Reset dreamcast" icon: @"tb-reset"
42 action: @selector(reset_action:)];
43 NSToolbarItem *pause = [self createToolbarItem: @"Pause" label: @"Pause"
44 tooltip: @"Pause dreamcast" icon: @"tb-pause"
45 action: @selector(pause_action:)];
46 NSToolbarItem *run = [self createToolbarItem: @"Run" label: @"Resume"
47 tooltip: @"Resume" icon: @"tb-run"
48 action: @selector(run_action:)];
49 NSToolbarItem *load = [self createToolbarItem: @"LoadState" label: @"Load State..."
50 tooltip: @"Load an lxdream save state" icon: @"tb-load"
51 action: @selector(load_action:)];
52 NSToolbarItem *save = [self createToolbarItem: @"SaveState" label: @"Save State..."
53 tooltip: @"Create an lxdream save state" icon: @"tb-save"
54 action: @selector(save_action:)];
55 [pause setEnabled: NO];
57 [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run", @"LoadState", @"SaveState", nil ];
59 [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run",
60 NSToolbarSeparatorItemIdentifier, @"LoadState", @"SaveState", nil ];
61 NSArray *values = [NSArray arrayWithObjects: mount, reset, pause, run, load, save, nil ];
62 items = [NSDictionary dictionaryWithObjects: values forKeys: identifiers];
66 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label
67 tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action
69 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier: id];
70 [item setLabel: label];
71 [item setToolTip: tooltip];
72 [item setTarget: [NSApp delegate]];
73 NSString *iconFile = [[NSBundle mainBundle] pathForResource:icon ofType:@"png"];
74 NSImage *image = [[NSImage alloc] initWithContentsOfFile: iconFile];
75 [item setImage: image];
76 [item setAction: action];
80 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar
85 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
90 - (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
92 return [NSArray arrayWithObjects: @"Pause", @"Run", nil];
95 - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier
96 willBeInsertedIntoToolbar:(BOOL)flag
98 return [items objectForKey: itemIdentifier];
102 @implementation LxdreamMainWindow
103 - (id)initWithContentRect:(NSRect)videoRect
105 NSRect contentRect = NSMakeRect(videoRect.origin.x,videoRect.origin.y,
106 videoRect.size.width,videoRect.size.height+STATUSBAR_HEIGHT);
107 if( [super initWithContentRect: contentRect
108 styleMask: ( NSTitledWindowMask | NSClosableWindowMask |
109 NSMiniaturizableWindowMask | NSResizableWindowMask |
110 NSTexturedBackgroundWindowMask | NSUnifiedTitleAndToolbarWindowMask )
111 backing: NSBackingStoreBuffered defer: NO ] == nil ) {
115 video = video_osx_create_drawable();
116 [video setFrameOrigin: NSMakePoint(0.0,STATUSBAR_HEIGHT)];
119 [[NSTextField alloc] initWithFrame: NSMakeRect(0.0,0.0,videoRect.size.width,STATUSBAR_HEIGHT)];
120 [status setStringValue: @"Idle"];
121 [status setEditable: NO];
122 [status setDrawsBackground: NO];
123 [status setBordered: NO];
124 [[self contentView] addSubview: video];
125 [[self contentView] addSubview: status];
126 [self makeFirstResponder: video];
128 [self setAutorecalculatesContentBorderThickness: YES forEdge: NSMinYEdge ];
129 [self setContentBorderThickness: 15.0 forEdge: NSMinYEdge];
131 // Share the app delegate for the purposes of keeping it in one place
132 [self setDelegate: [NSApp delegate]];
133 [self setContentMinSize: contentRect.size];
134 [self setAcceptsMouseMovedEvents: YES];
136 NSString *title = [[NSString alloc] initWithCString: (APP_NAME " " APP_VERSION) encoding: NSASCIIStringEncoding];
137 [self setTitle: title];
139 NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier: @"LxdreamToolbar"];
140 [toolbar setDelegate: [[LxdreamToolbarDelegate alloc] init]];
141 [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
142 [toolbar setSizeMode: NSToolbarSizeModeSmall];
143 [toolbar setSelectedItemIdentifier: @"Pause"];
144 [self setToolbar: toolbar];
149 - (void)setStatusText: (const gchar *)text
153 snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
154 NSString *s = [NSString stringWithUTF8String: buf];
155 [status setStringValue: s];
157 NSString *s = [NSString stringWithUTF8String: text];
158 [status setStringValue: s];
161 - (void)setRunning:(BOOL)isRunning
164 [[self toolbar] setSelectedItemIdentifier: @"Run"];
165 [self setStatusText: _("Running")];
167 [[self toolbar] setSelectedItemIdentifier: @"Pause"];
168 [self setStatusText: _("Stopped")];
175 - (void)setIsGrabbed:(BOOL)grab
177 if( grab != isGrabbed ) {
179 [self setRunning: dreamcast_is_running() ? YES : NO];
185 NSWindow *cocoa_gui_create_main_window()
187 NSRect contentRect = {{0,0},{640,480}};
188 NSWindow *main_win = [[LxdreamMainWindow alloc] initWithContentRect: contentRect];
.