filename | src/cocoaui/cocoa_win.c |
changeset | 738:0cbff49086b8 |
prev | 736:a02d1475ccfd |
next | 757:2780bc393e7c |
author | nkeynes |
date | Mon Jul 21 00:08:34 2008 +0000 (15 years ago) |
permissions | -rw-r--r-- |
last change | Add gettext.h and build sanely without libintl if it's not available Remove x86dasm's config.h & opintl.h (no longer needed and actually wrong) |
view | annotate | diff | log | raw |
1 /**
2 * $Id$
3 *
4 * Construct and maintain the main window under cocoa.
5 *
6 * Copyright (c) 2008 Nathan Keynes.
7 *
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.
12 *
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.
17 */
19 #include "cocoaui/cocoaui.h"
20 #include "lxdream.h"
21 #include <ApplicationServices/ApplicationServices.h>
23 #define STATUSBAR_HEIGHT 25
24 #define STATUS_TEXT_HEIGHT 22
26 @interface LxdreamToolbarDelegate : NSObject {
27 NSArray *identifiers;
28 NSArray *defaults;
29 NSDictionary *items;
30 }
31 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label
32 tooltip: (NSString *)tooltip
33 icon: (NSString *)icon action: (SEL) action;
34 @end
36 @implementation LxdreamToolbarDelegate
37 - (id) init
38 {
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];
58 identifiers =
59 [NSArray arrayWithObjects: @"GdromMount", @"Reset", @"Pause", @"Run", @"LoadState", @"SaveState", nil ];
60 defaults =
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];
65 return self;
66 }
68 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label
69 tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action
70 {
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];
79 return item;
80 }
82 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar
83 {
84 return identifiers;
85 }
87 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
88 {
89 return defaults;
90 }
92 - (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
93 {
94 return [NSArray arrayWithObjects: @"Pause", @"Run", nil];
95 }
97 - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier
98 willBeInsertedIntoToolbar:(BOOL)flag
99 {
100 return [items objectForKey: itemIdentifier];
101 }
102 @end
104 @implementation LxdreamMainWindow
105 - (id)initWithContentRect:(NSRect)videoRect
106 {
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 ) {
114 return nil;
115 } else {
116 isGrabbed = NO;
117 video = video_osx_create_drawable();
118 [video setFrameOrigin: NSMakePoint(0.0,STATUSBAR_HEIGHT)];
120 status =
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];
147 return self;
148 }
149 }
151 - (void)setStatusText: (const gchar *)text
152 {
153 if( isGrabbed ) {
154 gchar buf[128];
155 snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
156 NSString *s = [NSString stringWithUTF8String: buf];
157 [status setStringValue: s];
158 } else {
159 NSString *s = [NSString stringWithUTF8String: text];
160 [status setStringValue: s];
161 }
162 }
163 - (void)setRunning:(BOOL)isRunning
164 {
165 if( isRunning ) {
166 [[self toolbar] setSelectedItemIdentifier: @"Run"];
167 [self setStatusText: _("Running")];
168 } else {
169 [[self toolbar] setSelectedItemIdentifier: @"Pause"];
170 [self setStatusText: _("Stopped")];
171 }
172 }
173 - (BOOL)isGrabbed
174 {
175 return isGrabbed;
176 }
177 - (void)setIsGrabbed:(BOOL)grab
178 {
179 if( grab != isGrabbed ) {
180 isGrabbed = grab;
181 [self setRunning: dreamcast_is_running() ? YES : NO];
182 }
183 }
185 @end
187 NSWindow *cocoa_gui_create_main_window()
188 {
189 NSRect contentRect = {{0,0},{640,480}};
190 NSWindow *main_win = [[LxdreamMainWindow alloc] initWithContentRect: contentRect];
191 return main_win;
192 }
.