ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

browser_osx.mm

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 // Disable symbol overrides so that we can use system headers
00024 #define FORBIDDEN_SYMBOL_ALLOW_ALL
00025 
00026 #include "gui/browser.h"
00027 
00028 #include "common/config-manager.h"
00029 #include "common/system.h"
00030 #include "common/events.h"
00031 #include "common/algorithm.h"
00032 #include "common/translation.h"
00033 
00034 #include <AppKit/NSNibDeclarations.h>
00035 #include <AppKit/NSOpenPanel.h>
00036 #include <AppKit/NSApplication.h>
00037 #include <AppKit/NSButton.h>
00038 #include <Foundation/NSString.h>
00039 #include <Foundation/NSURL.h>
00040 #include <Foundation/NSAutoreleasePool.h>
00041 
00042 
00043 @interface BrowserDialogPresenter : NSObject {
00044 @public
00045     NSURL *_url;
00046 @private
00047     NSOpenPanel *_panel;
00048 }
00049 - (id) init;
00050 - (void) dealloc;
00051 - (void) showOpenPanel: (NSOpenPanel*) panel;
00052 - (IBAction) showHiddenFiles : (id) sender;
00053 @end
00054 
00055 @implementation BrowserDialogPresenter
00056 
00057 - (id) init {
00058     self = [super init];
00059     _url = 0;
00060     _panel = 0;
00061     return self;
00062 }
00063 
00064 - (void) dealloc {
00065     [_url release];
00066     [super dealloc];
00067 }
00068 
00069 - (void) showOpenPanel: (NSOpenPanel*) panel {
00070     _panel = panel;
00071 
00072     NSButton *showHiddenFilesButton = 0;
00073     if ([panel respondsToSelector:@selector(setShowsHiddenFiles:)]) {
00074         showHiddenFilesButton = [[NSButton alloc] init];
00075         [showHiddenFilesButton setButtonType:NSSwitchButton];
00076 
00077 #ifdef USE_TRANSLATION
00078         CFStringRef encStr = CFStringCreateWithCString(NULL, TransMan.getCurrentCharset().c_str(), kCFStringEncodingASCII);
00079         CFStringEncoding stringEncoding = CFStringConvertIANACharSetNameToEncoding(encStr);
00080         CFRelease(encStr);
00081 #else
00082         CFStringEncoding stringEncoding = kCFStringEncodingASCII;
00083 #endif
00084         CFStringRef hiddenFilesString = CFStringCreateWithCString(0, _("Show hidden files"), stringEncoding);
00085         [showHiddenFilesButton setTitle:(NSString*)hiddenFilesString];
00086         CFRelease(hiddenFilesString);
00087 
00088         [showHiddenFilesButton sizeToFit];
00089         if (ConfMan.getBool("gui_browser_show_hidden", Common::ConfigManager::kApplicationDomain)) {
00090             [showHiddenFilesButton setState:NSOnState];
00091             [panel setShowsHiddenFiles: YES];
00092         } else {
00093             [showHiddenFilesButton setState:NSOffState];
00094             [panel setShowsHiddenFiles: NO];
00095         }
00096         [panel setAccessoryView:showHiddenFilesButton];
00097 
00098         [showHiddenFilesButton setTarget:self];
00099         [showHiddenFilesButton setAction:@selector(showHiddenFiles:)];
00100     }
00101 
00102 #if MAC_OS_X_VERSION_MAX_ALLOWED <= 1090
00103     if ([panel runModal] == NSOKButton) {
00104 #else
00105     if ([panel runModal] == NSModalResponseOK) {
00106 #endif
00107         NSURL *url = [panel URL];
00108         if ([url isFileURL]) {
00109             _url = url;
00110             [_url retain];
00111         }
00112     }
00113 
00114     [showHiddenFilesButton release];
00115     _panel = 0;
00116 }
00117 
00118 - (IBAction) showHiddenFiles : (id) sender {
00119     if ([sender state] == NSOnState) {
00120         [_panel setShowsHiddenFiles: YES];
00121         ConfMan.setBool("gui_browser_show_hidden", true, Common::ConfigManager::kApplicationDomain);
00122     } else {
00123         [_panel setShowsHiddenFiles: NO];
00124         ConfMan.setBool("gui_browser_show_hidden", false, Common::ConfigManager::kApplicationDomain);
00125     }
00126 }
00127 
00128 @end
00129 
00130 namespace GUI {
00131 
00132 BrowserDialog::BrowserDialog(const char *title, bool dirBrowser)
00133     : Dialog("Browser") {
00134 
00135     // remember whether this is a file browser or a directory browser.
00136     _isDirBrowser = dirBrowser;
00137 
00138     // Get current encoding
00139 #ifdef USE_TRANSLATION
00140     CFStringRef encStr = CFStringCreateWithCString(NULL, TransMan.getCurrentCharset().c_str(), kCFStringEncodingASCII);
00141     CFStringEncoding stringEncoding = CFStringConvertIANACharSetNameToEncoding(encStr);
00142     CFRelease(encStr);
00143 #else
00144     CFStringEncoding stringEncoding = kCFStringEncodingASCII;
00145 #endif
00146 
00147     // Convert title to NSString
00148     _titleRef = CFStringCreateWithCString(0, title, stringEncoding);
00149 
00150     // Convert button text to NSString
00151     _chooseRef = CFStringCreateWithCString(0, _("Choose"), stringEncoding);
00152 }
00153 
00154 BrowserDialog::~BrowserDialog() {
00155     CFRelease(_titleRef);
00156     CFRelease(_chooseRef);
00157 }
00158 
00159 int BrowserDialog::runModal() {
00160     bool choiceMade = false;
00161 
00162     // If in fullscreen mode, switch to windowed mode
00163     bool wasFullscreen = g_system->getFeatureState(OSystem::kFeatureFullscreenMode);
00164     if (wasFullscreen) {
00165         g_system->beginGFXTransaction();
00166         g_system->setFeatureState(OSystem::kFeatureFullscreenMode, false);
00167         g_system->endGFXTransaction();
00168     }
00169 
00170     // Temporarily show the real mouse
00171     CGDisplayShowCursor(kCGDirectMainDisplay);
00172 
00173     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
00174     NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
00175 
00176 
00177     NSOpenPanel *panel = [NSOpenPanel openPanel];
00178     [panel setCanChooseFiles:!_isDirBrowser];
00179     [panel setCanChooseDirectories:_isDirBrowser];
00180     if (_isDirBrowser)
00181         [panel setTreatsFilePackagesAsDirectories:true];
00182     [panel setTitle:(NSString *)_titleRef];
00183     [panel setPrompt:(NSString *)_chooseRef];
00184 
00185     BrowserDialogPresenter* presenter = [[BrowserDialogPresenter alloc] init];
00186     [presenter performSelectorOnMainThread:@selector(showOpenPanel:) withObject:panel waitUntilDone:YES];
00187     if (presenter->_url) {
00188         Common::String filename = [[presenter->_url path] UTF8String];
00189         _choice = Common::FSNode(filename);
00190         choiceMade = true;
00191     }
00192     [presenter release];
00193 
00194     [pool release];
00195     [keyWindow makeKeyAndOrderFront:nil];
00196 
00197     // While the native macOS file browser is open, any input events (e.g. keypresses) are
00198     // still received by the NSApplication. With SDL backend for example this results in the
00199     // events beeing queued and processed after we return, thus dispatching events that were
00200     // intended for the native file browser. For example: pressing Esc to cancel the native
00201     // macOS file browser would cause the application to quit in addition to closing the
00202     // file browser. To avoid this happening clear all pending events.
00203     g_system->getEventManager()->getEventDispatcher()->clearEvents();
00204 
00205     // If we were in fullscreen mode, switch back
00206     if (wasFullscreen) {
00207         g_system->beginGFXTransaction();
00208         g_system->setFeatureState(OSystem::kFeatureFullscreenMode, true);
00209         g_system->endGFXTransaction();
00210     }
00211 
00212     return choiceMade;
00213 }
00214 
00215 } // End of namespace GUI


Generated on Sat Feb 16 2019 05:00:46 for ResidualVM by doxygen 1.7.1
curved edge   curved edge