ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/MacOSX/Controller.mm
Revision: 1.6
Committed: 2003-01-10T23:01:48Z (21 years, 10 months ago) by nigel
Branch: MAIN
Changes since 1.5: +34 -1 lines
Log Message:
Extra methods for new Help menu items

File Contents

# User Rev Content
1 nigel 1.2 /*
2     * Controller.m - Simple application window management.
3     *
4 nigel 1.6 * $Id: Controller.mm,v 1.5 2002/12/18 11:50:12 nigel Exp $
5 nigel 1.2 *
6     * Basilisk II (C) 1997-2001 Christian Bauer
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     *
18     * You should have received a copy of the GNU General Public License
19     * along with this program; if not, write to the Free Software
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21     */
22    
23     #import "Controller.h"
24     #import "Emulator.h"
25    
26     @implementation Controller
27    
28     #import "sysdeps.h" // Types used in Basilisk C++ code
29    
30     #import <main.h>
31     #import <prefs.h>
32    
33     #define DEBUG 0
34     #import <debug.h>
35    
36 nigel 1.6 #import "misc_macosx.h"
37 nigel 1.2 #import "video_macosx.h"
38    
39     //
40     // Standard NSApplication methods that we override
41     //
42    
43     - (id) init
44     {
45     #ifdef ENABLE_MULTIPLE
46 nigel 1.5 emulators = [NSMutableArray new];
47 nigel 1.2 #endif
48     return [super init];
49     }
50    
51     - (void) dealloc
52     {
53     #ifdef ENABLE_MULTIPLE
54     [emulators dealloc];
55     #endif
56     [super dealloc];
57     }
58    
59     - (void) awakeFromNib
60     {
61     #ifdef ENABLE_MULTIPLE
62     [self NewEmulator: self]; // So the user gets something on screen
63     #endif
64     [[NSApplication sharedApplication]
65     setDelegate: self]; // Enable applicationShouldTerminate
66     }
67    
68     - (void) sendEvent: (NSEvent *)event;
69     {
70 nigel 1.4 if ( [self isAnyEmulatorDisplayingSheets] || ! [self isAnyEmulatorRunning] )
71     [super sendEvent: event];
72     else
73     {
74     NSEventType type = [event type];
75 nigel 1.2
76 nigel 1.4 if ( type == NSKeyUp || type == NSKeyDown || type == NSFlagsChanged )
77     [self dispatchKeyEvent: event
78     type: type];
79     else
80     [self dispatchEvent: event
81     type: type];
82     }
83 nigel 1.2 }
84    
85     // NSApplication methods which are invoked through delegation
86    
87     - (BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication *)app
88     { return YES; }
89    
90     - (NSApplicationTerminateReply) applicationShouldTerminate: (NSApplication *)app
91     {
92     short count;
93     char *stillRunningMessage;
94    
95     if ( [thePrefsEditor hasEdited] )
96     if ( ChoiceAlert("Preferences have been edited",
97     "Save changes", "Quit") )
98     SavePrefs();
99    
100     // if ( edited )
101     // {
102     // NSString *title = [NSString stringWithCString: getString(STR_WARNING_ALERT_TITLE)],
103     // *msg = @"Preferences have been edited",
104     // *def = @"Save changes",
105     // *alt = @"Quit Application",
106     // *other = @"Continue";
107     //
108     // switch ( NSRunAlertPanel(title, msg, def, alt, other, nil) )
109     // {
110     // case NSAlertDefault: savePrefs();
111     // case NSAlertAlternate: return NSTerminateNow;
112     // case NSAlertOther: return NSTerminateCancel;
113     // }
114     // }
115    
116    
117     if ( [[thePrefsEditor window] isVisible] )
118     [[thePrefsEditor window] performClose: self];
119    
120    
121     count = [self emulatorCreatedCount];
122    
123     if ( count > 0 )
124     {
125     if ( count > 1 )
126     stillRunningMessage = "Emulators are still running\nExiting Basilisk may lose data";
127     else
128     stillRunningMessage = "Emulator is still running\nExiting Basilisk may lose data";
129     if ( ! ChoiceAlert(stillRunningMessage, "Exit", "Continue") )
130     return NSTerminateCancel; // NSTerminateLater?
131     }
132    
133     return NSTerminateNow;
134     }
135    
136    
137     // Event dispatching, called by sendEvent
138    
139     - (void) dispatchKeyEvent: (NSEvent *)event
140     type: (NSEventType)type
141     {
142     EmulatorView *view;
143    
144     #ifdef ENABLE_MULTIPLE
145     // We need to work out what window's Emulator should receive these messages
146    
147    
148     int tmp;
149    
150     for ( tmp = 0; tmp < [emulators count], ++tmp )
151     {
152     theEmulator = [emulators objectAtIndex: tmp];
153 nigel 1.4 view = [theEmulator screen];
154    
155     if ( [ theEmulator isRunning ] &&
156     ( [[theEmulator window] isKeyWindow] || [view isFullScreen] ) )
157 nigel 1.2 break;
158     }
159    
160     if ( tmp < [emulators count] ) // i.e. if we exited the for loop
161     #else
162 nigel 1.4 view = [theEmulator screen];
163    
164     if ( [theEmulator isRunning] &&
165     ( [[theEmulator window] isKeyWindow] || [view isFullScreen] ) )
166 nigel 1.2 #endif
167     {
168 nigel 1.4 D(NSLog(@"Got a key event - %d\n", [event keyCode]));
169 nigel 1.2 switch ( type )
170     {
171     case NSKeyUp:
172     [view keyUp: event];
173     break;
174     case NSKeyDown:
175     D(NSLog(@"%s - NSKeyDown - %@", __PRETTY_FUNCTION__, event));
176     [view keyDown: event];
177     break;
178     case NSFlagsChanged:
179     [view flagsChanged: event];
180     break;
181     default:
182     NSLog(@"%s - Sent a non-key event (logic error)",
183     __PRETTY_FUNCTION__);
184     [super sendEvent: event];
185     }
186     }
187     else // No Basilisk window is key (maybe a panel or pane).
188     [super sendEvent: event]; // Call NSApplication default
189    
190     }
191    
192     - (void) dispatchEvent: (NSEvent *)event
193     type: (NSEventType)type
194     {
195 nigel 1.4 EmulatorView *view;
196    
197 nigel 1.2 #ifdef ENABLE_MULTIPLE
198     // We need to work out what window's Emulator should receive these messages
199    
200    
201     int tmp;
202    
203     for ( tmp = 0; tmp < [emulators count], ++tmp )
204     {
205     theEmulator = [emulators objectAtIndex: tmp];
206 nigel 1.4 view = [theEmulator screen];
207    
208     if ( [theEmulator isRunning] &&
209     ( [[theEmulator window] isMainWindow] || [view isFullScreen] ) )
210 nigel 1.2 break;
211     }
212    
213     if ( tmp < [emulators count] ) // i.e. if we exited the for loop
214     #else
215 nigel 1.4 view = [theEmulator screen];
216    
217     if ( [theEmulator isRunning] &&
218     ( [[theEmulator window] isMainWindow] || [view isFullScreen] ) )
219 nigel 1.2 #endif
220     {
221 nigel 1.4 if ( [view mouseInView: event] )
222 nigel 1.2 {
223     switch ( type )
224     {
225     case NSLeftMouseDown:
226     [view mouseDown: event];
227     break;
228     case NSLeftMouseUp:
229     [view mouseUp: event];
230     break;
231     case NSLeftMouseDragged:
232     case NSMouseMoved:
233     [view processMouseMove: event];
234     break;
235     default:
236     [super sendEvent: event]; // NSApplication default
237     }
238     return;
239     }
240     }
241    
242     // Either the pointer is not in the Emulator's screen, no Basilisk window is running,
243     // or no Basilisk window is main (e.g. there might be a panel or pane up).
244     //
245     // We should just be calling NSApp's default sendEvent, but then for some reason
246     // mouseMoved events are still passed to our EmulatorView, so we filter them out.
247    
248     if ( type != NSMouseMoved )
249     [super sendEvent: event];
250     }
251 nigel 1.6
252    
253     // Methods to display documentation:
254    
255     - (IBAction) HelpToDo: (id)sender
256     {
257     NSString *path = [[NSBundle mainBundle] pathForResource: @"ToDo"
258     ofType: @"html"];
259    
260     if ( ! path )
261     InfoSheet(@"Cannot find ToDo.html", [theEmulator window]);
262     else
263     if ( ! [[NSWorkspace sharedWorkspace] openFile: path
264     withApplication: @"TextEdit"] )
265     InfoSheet(@"Cannot open ToDo.html with TextEdit", [theEmulator window]);
266     }
267    
268     - (IBAction) HelpVersions: (id)sender
269     {
270     NSString *path = [[NSBundle mainBundle] pathForResource: @"Versions"
271     ofType: @"html"];
272    
273     if ( ! path )
274     InfoSheet(@"Cannot find Versions.html", [theEmulator window]);
275     else
276     if ( ! [[NSWorkspace sharedWorkspace] openFile: path
277     withApplication: @"TextEdit"] )
278     InfoSheet(@"Cannot open Versions.html with TextEdit", [theEmulator window]);
279     }
280    
281    
282     // Menu items which for managing more than one window
283 nigel 1.2
284     #ifdef ENABLE_MULTIPLE
285    
286     - (IBAction) NewEmulator: (id)sender
287     {
288     NSString *title;
289    
290     if ( ! [NSBundle loadNibNamed:@"Win512x342" owner:self] )
291     {
292     NSLog(@"%s - LoadNibNamed@Win512x342 failed", __PRETTY_FUNCTION__);
293     return;
294     }
295    
296     if ( theEmulator == nil)
297     {
298     NSLog(@"%s - Newly created emulator's NIB stuff not fully linked?", __PRETTY_FUNCTION__);
299     return;
300     }
301    
302     [emulators addObject: theEmulator];
303     title = [NSString localizedStringWithFormat:@"BasiliskII Emulator %d", [emulators count]];
304     [theEmulator -> win setTitle: title];
305     }
306    
307     - (IBAction) PauseAll: (id)sender
308     {
309     [emulators makeObjectsPerformSelector:@selector(Suspend:)
310     withObject:self];
311     }
312    
313     - (IBAction) RunAll: (id)sender
314     {
315     [emulators makeObjectsPerformSelector:@selector(Resume:)
316     withObject:self];
317     }
318    
319     - (IBAction) TerminateAll: (id)sender
320     {
321     [emulators makeObjectsPerformSelector:@selector(Terminate:)
322     withObject:self];
323     }
324    
325     #endif
326 nigel 1.4
327     - (BOOL) isAnyEmulatorDisplayingSheets
328     {
329     #ifdef ENABLE_MULTIPLE
330     int tmp;
331    
332     for ( tmp = 0; tmp < [emulators count], ++tmp )
333     if ( [[[emulators objectAtIndex: tmp] window] attachedSheet] )
334     break;
335    
336     if ( tmp < [emulators count] ) // i.e. if we exited the for loop
337     #else
338     if ( [[theEmulator window] attachedSheet] )
339     #endif
340     return TRUE;
341    
342     return FALSE;
343     }
344 nigel 1.2
345     - (BOOL) isAnyEmulatorRunning
346     {
347     #ifdef ENABLE_MULTIPLE
348     int tmp;
349    
350     for ( tmp = 0; tmp < [emulators count], ++tmp )
351     if ( [[emulators objectAtIndex: tmp] isRunning] )
352     break;
353    
354     if ( tmp < [emulators count] ) // i.e. if we exited the for loop
355     #else
356     if ( [theEmulator isRunning] )
357     #endif
358     return TRUE;
359    
360     return FALSE;
361     }
362    
363     - (short) emulatorCreatedCount
364     {
365     short count = 0;
366     #ifdef ENABLE_MULTIPLE
367     int tmp;
368    
369     for ( tmp = 0; tmp < [emulators count], ++tmp )
370     if ( [[emulators objectAtIndex: tmp] uaeCreated] )
371     ++count;
372     #else
373     if ( [theEmulator uaeCreated] )
374     ++count;
375     #endif
376    
377     return count;
378     }
379    
380     @end