📄 mydocument.mm
字号:
// This file is part of Ambulant Player, www.ambulantplayer.org.//// Copyright (C) 2003-2007 Stichting CWI, // Kruislaan 413, 1098 SJ Amsterdam, The Netherlands.//// Ambulant Player is free software; you can redistribute it and/or modify// it under the terms of the GNU Lesser General Public License as published by// the Free Software Foundation; either version 2.1 of the License, or// (at your option) any later version.//// Ambulant Player is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public License// along with Ambulant Player; if not, write to the Free Software// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA//// MyDocument.m// cocoambulant//// Created by Jack Jansen on Thu Sep 04 2003.// Copyright (c) 2003 __MyCompanyName__. All rights reserved.//#import "MyDocument.h"#import "MyAmbulantView.h"#import "ambulant/common/preferences.h"#ifndef AM_DBG#define AM_DBG if(0)#endif// Help class for fullscreen windows: normally, windows// with style NSBorderlessWindowMask don't get any keyboard input.// By overriding canBecomeKeyWindow we fix that.@interface FullScreenWindow : NSWindow{}- (BOOL)canBecomeKeyWindow;@end@implementation FullScreenWindow- (BOOL)canBecomeKeyWindow{ return YES;}@endvoiddocument_embedder::show_file(const ambulant::net::url& href){ CFStringRef cfhref = CFStringCreateWithCString(NULL, href.get_url().c_str(), kCFStringEncodingUTF8); CFURLRef url = CFURLCreateWithString(NULL, cfhref, NULL); OSErr status; if ((status=LSOpenCFURLRef(url, NULL)) != 0) { ambulant::lib::logger::get_logger()->trace("Opening URL <%s>: LSOpenCFURLRef error %d", href.get_url().c_str(), status); ambulant::lib::logger::get_logger()->error(gettext("Cannot open: %s"), href.get_url().c_str()); }}voiddocument_embedder::close(ambulant::common::player *p){ [m_mydocument performSelectorOnMainThread: @selector(close:) withObject: nil waitUntilDone: NO];}voiddocument_embedder::open(ambulant::net::url newdoc, bool start, ambulant::common::player *old){ if (old) { AM_DBG NSLog(@"performSelectorOnMainThread: close: on 0x%x", (void*)m_mydocument); [m_mydocument performSelectorOnMainThread: @selector(close:) withObject: nil waitUntilDone: NO]; } NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString *str_url = [NSString stringWithCString: newdoc.get_url().c_str()]; NSURL *url = [NSURL URLWithString: str_url]; NSDocumentController *docController = [NSDocumentController sharedDocumentController]; NSDocument *doc = [docController openDocumentWithContentsOfURL:url display:YES]; if (!doc) { ambulant::lib::logger::get_logger()->error(gettext("Cannot open: %s"), newdoc.get_url().c_str()); } [pool release]; // [doc retain] ?? }#ifdef WITH_AUX_DOCUMENTbooldocument_embedder::aux_open(const ambulant::net::url& auxdoc){ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; if (auxdoc.get_url() == "") { NSLog(@"aux_open: closing"); [m_mydocument closeAuxDocument]; return true; } NSString *str_url = [NSString stringWithCString: auxdoc.get_url().c_str()]; NSURL *url = [NSURL URLWithString: str_url]; NSLog(@"aux_open: open %@", url); BOOL rv = [m_mydocument openAuxDocument: url]; [pool release]; return (bool)rv;}#endif // WITH_AUX_DOCUMENT@implementation MyDocument- (id)init{ self = [super init]; if (self) { // Add your subclass-specific initialization here. // If an error occurs here, send a [self release] message and return nil. } saved_window = nil; return self;}- (NSString *)windowNibName{ // Override returning the nib file name of the document // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead. return @"MyDocument";}- (void)windowControllerDidLoadNib:(NSWindowController *) aController{ [super windowControllerDidLoadNib:aController]; // Add any code here that needs to be executed once the windowController has loaded the document's window. [[view window] makeFirstResponder: view]; [[view window] setAcceptsMouseMovedEvents: YES]; BOOL compat103 = ![self respondsToSelector: @selector(fileURL)]; if (compat103) { if ([self fileName] == nil) { [self askForURL: self]; } else { [self openTheDocument]; } } else { if ([self fileURL] == nil) { [self askForURL: self]; } else { [self openTheDocument]; } } [self validateButtons: self]; // Go fullscreen if either the -fullScreen argument was given on the command // line or the user defaults were edited manually. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; if ( [defaults boolForKey: @"fullScreen"] ) [self goFullScreen: self];}- (void)askForURL: (id)sender{ AM_DBG NSLog(@"Show sheet to ask for URL"); [self showWindows]; [NSApp beginSheet: ask_url_panel modalForWindow:[self windowForSheet] modalDelegate:self didEndSelector:@selector(askForURLDidEnd:returnCode:contextInfo:) contextInfo:nil];}- (IBAction)closeURLPanel:(id)sender{ [ask_url_panel orderOut:self]; [NSApp endSheet:ask_url_panel returnCode:([sender tag] == 1) ? NSOKButton : NSCancelButton];}- (void)askForURLDidEnd:(NSAlert *)alert returnCode:(int)returnCode contextInfo:(void *)contextInfo{ if (returnCode == NSOKButton && [[url_field stringValue] length] > 0) { AM_DBG NSLog(@"ask_for_url: User said OK: %@", [url_field stringValue]); BOOL compat103 = ![self respondsToSelector: @selector(fileURL)]; if (compat103) { [self setFileName: [url_field stringValue]]; } else { [self setFileURL: [NSURL URLWithString: [url_field stringValue]]]; } [self openTheDocument]; } else { AM_DBG NSLog(@"ask_for_url: User said cancel"); [self close]; }}- (void)openTheDocument{ NSString *url; BOOL compat103 = ![self respondsToSelector: @selector(fileURL)]; if (compat103) { url = [self fileName]; } else { url = [[self fileURL] absoluteString]; }#if 0 // XXX This is incorrect if ( [[self fileURL] isFileURL] && ![[self fileURL] fragment]) { NSString *escapedurl = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)url, NULL, NULL, kCFStringEncodingUTF8); //[url release]; url = escapedurl; }#endif bool use_mms = ([[url pathExtension] compare: @".mms"] == 0); embedder = new document_embedder(self); myMainloop = new mainloop([url UTF8String], view, use_mms, embedder); [self play: self];}- (NSData *)dataRepresentationOfType:(NSString *)aType{ // Insert code here to write your document from the given data. You can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead. return nil;}- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType{ // Insert code here to read your document from the given data. You can also choose to override -loadFileWrapperRepresentation:ofType: or -readFromFile:ofType: instead. return YES;}- (BOOL) validateUIItem:(id)UIItem{ AM_DBG NSLog(@"Validating %@", UIItem); SEL theAction = [UIItem action]; if (!myMainloop) { // No document: no checkmarks and grayed for all items [UIItem setState: NSOffState]; return NO; } if (theAction == @selector(play:)) { if (myMainloop->is_play_active()) { AM_DBG NSLog(@"play - On"); [UIItem setState: NSOnState]; } else { AM_DBG NSLog(@"play - Off"); [UIItem setState: NSOffState]; } return myMainloop->is_play_enabled(); } else if (theAction == @selector(stop:)) { if (myMainloop->is_stop_active()) { AM_DBG NSLog(@"stop - On"); [UIItem setState: NSOnState]; } else { AM_DBG NSLog(@"stop - Off"); [UIItem setState: NSOffState]; } return myMainloop->is_stop_enabled(); } else if (theAction == @selector(pause:)) { if (myMainloop->is_pause_active()) { AM_DBG NSLog(@"pause - On"); [UIItem setState: NSOnState]; } else { AM_DBG NSLog(@"pause - On"); [UIItem setState: NSOffState]; } return myMainloop->is_pause_enabled(); } else if (theAction == @selector(toggleFullScreen:)) { if (saved_window) { [UIItem setState: NSOnState]; } else { [UIItem setState: NSOffState]; } return YES; } return NO;}- (BOOL) validateMenuItem:(id)menuItem{ return [self validateUIItem: menuItem];}- (void) validateButtons: (id)dummy{ if (!play_button || !stop_button || !pause_button) return; BOOL enabled; enabled = [self validateUIItem: play_button]; [play_button setEnabled: enabled]; enabled = [self validateUIItem: stop_button]; [stop_button setEnabled: enabled]; enabled = [self validateUIItem: pause_button]; [pause_button setEnabled: enabled];}- (IBAction)pause:(id)sender{ if (myMainloop) myMainloop->pause(); [self validateButtons: nil];}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -