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

macosx-dialogs.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 #include "common/scummsys.h"
00026 
00027 #if defined(MACOSX) && defined(USE_SYSDIALOGS)
00028 
00029 #include "backends/dialogs/macosx/macosx-dialogs.h"
00030 
00031 #include "common/config-manager.h"
00032 #include "common/system.h"
00033 #include "common/events.h"
00034 #include "common/algorithm.h"
00035 #include "common/translation.h"
00036 
00037 #include <AppKit/NSNibDeclarations.h>
00038 #include <AppKit/NSOpenPanel.h>
00039 #include <AppKit/NSApplication.h>
00040 #include <AppKit/NSButton.h>
00041 #include <Foundation/NSString.h>
00042 #include <Foundation/NSURL.h>
00043 #include <Foundation/NSAutoreleasePool.h>
00044 
00045 
00046 @interface BrowserDialogPresenter : NSObject {
00047 @public
00048     NSURL *_url;
00049 @private
00050     NSOpenPanel *_panel;
00051 }
00052 - (id) init;
00053 - (void) dealloc;
00054 - (void) showOpenPanel: (NSOpenPanel*) panel;
00055 - (IBAction) showHiddenFiles : (id) sender;
00056 @end
00057 
00058 @implementation BrowserDialogPresenter
00059 
00060 - (id) init {
00061     self = [super init];
00062     _url = 0;
00063     _panel = 0;
00064     return self;
00065 }
00066 
00067 - (void) dealloc {
00068     [_url release];
00069     [super dealloc];
00070 }
00071 
00072 - (void) showOpenPanel: (NSOpenPanel*) panel {
00073     _panel = panel;
00074 
00075     NSButton *showHiddenFilesButton = 0;
00076     if ([panel respondsToSelector:@selector(setShowsHiddenFiles:)]) {
00077         showHiddenFilesButton = [[NSButton alloc] init];
00078         [showHiddenFilesButton setButtonType:NSSwitchButton];
00079 
00080 #ifdef USE_TRANSLATION
00081         CFStringRef encStr = CFStringCreateWithCString(NULL, TransMan.getCurrentCharset().c_str(), kCFStringEncodingASCII);
00082         CFStringEncoding stringEncoding = CFStringConvertIANACharSetNameToEncoding(encStr);
00083         CFRelease(encStr);
00084 #else
00085         CFStringEncoding stringEncoding = kCFStringEncodingASCII;
00086 #endif
00087         CFStringRef hiddenFilesString = CFStringCreateWithCString(0, _("Show hidden files"), stringEncoding);
00088         [showHiddenFilesButton setTitle:(NSString*)hiddenFilesString];
00089         CFRelease(hiddenFilesString);
00090 
00091         [showHiddenFilesButton sizeToFit];
00092         if (ConfMan.getBool("gui_browser_show_hidden", Common::ConfigManager::kApplicationDomain)) {
00093             [showHiddenFilesButton setState:NSOnState];
00094             [panel setShowsHiddenFiles: YES];
00095         } else {
00096             [showHiddenFilesButton setState:NSOffState];
00097             [panel setShowsHiddenFiles: NO];
00098         }
00099         [panel setAccessoryView:showHiddenFilesButton];
00100 
00101         [showHiddenFilesButton setTarget:self];
00102         [showHiddenFilesButton setAction:@selector(showHiddenFiles:)];
00103     }
00104 
00105 #if MAC_OS_X_VERSION_MAX_ALLOWED <= 1090
00106     if ([panel runModal] == NSOKButton) {
00107 #else
00108     if ([panel runModal] == NSModalResponseOK) {
00109 #endif
00110         NSURL *url = [panel URL];
00111         if ([url isFileURL]) {
00112             _url = url;
00113             [_url retain];
00114         }
00115     }
00116 
00117     [showHiddenFilesButton release];
00118     _panel = 0;
00119 }
00120 
00121 - (IBAction) showHiddenFiles : (id) sender {
00122     if ([sender state] == NSOnState) {
00123         [_panel setShowsHiddenFiles: YES];
00124         ConfMan.setBool("gui_browser_show_hidden", true, Common::ConfigManager::kApplicationDomain);
00125     } else {
00126         [_panel setShowsHiddenFiles: NO];
00127         ConfMan.setBool("gui_browser_show_hidden", false, Common::ConfigManager::kApplicationDomain);
00128     }
00129 }
00130 
00131 @end
00132 
00133 Common::DialogManager::DialogResult MacOSXDialogManager::showFileBrowser(const char *title, Common::FSNode &choice, bool isDirBrowser) {
00134 
00135     DialogResult result = kDialogCancel;
00136 
00137     // Get current encoding
00138 #ifdef USE_TRANSLATION
00139     CFStringRef encStr = CFStringCreateWithCString(NULL, TransMan.getCurrentCharset().c_str(), kCFStringEncodingASCII);
00140     CFStringEncoding stringEncoding = CFStringConvertIANACharSetNameToEncoding(encStr);
00141     CFRelease(encStr);
00142 #else
00143     CFStringEncoding stringEncoding = kCFStringEncodingASCII;
00144 #endif
00145 
00146     // Convert labels to NSString
00147     CFStringRef titleRef = CFStringCreateWithCString(0, title, stringEncoding);
00148     CFStringRef chooseRef = CFStringCreateWithCString(0, _("Choose"), stringEncoding);
00149 
00150     // If in fullscreen mode, switch to windowed mode
00151     bool wasFullscreen = g_system->getFeatureState(OSystem::kFeatureFullscreenMode);
00152     if (wasFullscreen) {
00153         g_system->beginGFXTransaction();
00154         g_system->setFeatureState(OSystem::kFeatureFullscreenMode, false);
00155         g_system->endGFXTransaction();
00156     }
00157 
00158     // Temporarily show the real mouse
00159     CGDisplayShowCursor(kCGDirectMainDisplay);
00160 
00161     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
00162     NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
00163 
00164 
00165     NSOpenPanel *panel = [NSOpenPanel openPanel];
00166     [panel setCanChooseFiles:!isDirBrowser];
00167     [panel setCanChooseDirectories:isDirBrowser];
00168     if (isDirBrowser)
00169         [panel setTreatsFilePackagesAsDirectories:true];
00170     [panel setTitle:(NSString *)titleRef];
00171     [panel setPrompt:(NSString *)chooseRef];
00172 
00173     BrowserDialogPresenter* presenter = [[BrowserDialogPresenter alloc] init];
00174     [presenter performSelectorOnMainThread:@selector(showOpenPanel:) withObject:panel waitUntilDone:YES];
00175     if (presenter->_url) {
00176         Common::String filename = [[presenter->_url path] UTF8String];
00177         choice = Common::FSNode(filename);
00178         result = kDialogOk;
00179     }
00180     [presenter release];
00181 
00182     [pool release];
00183     [keyWindow makeKeyAndOrderFront:nil];
00184 
00185     CFRelease(titleRef);
00186     CFRelease(chooseRef);
00187 
00188     // While the native macOS file browser is open, any input events (e.g. keypresses) are
00189     // still received by the NSApplication. With SDL backend for example this results in the
00190     // events beeing queued and processed after we return, thus dispatching events that were
00191     // intended for the native file browser. For example: pressing Esc to cancel the native
00192     // macOS file browser would cause the application to quit in addition to closing the
00193     // file browser. To avoid this happening clear all pending events.
00194     g_system->getEventManager()->getEventDispatcher()->clearEvents();
00195 
00196     // If we were in fullscreen mode, switch back
00197     if (wasFullscreen) {
00198         g_system->beginGFXTransaction();
00199         g_system->setFeatureState(OSystem::kFeatureFullscreenMode, true);
00200         g_system->endGFXTransaction();
00201     }
00202 
00203     return result;
00204 }
00205 
00206 #endif


Generated on Sat Mar 23 2019 05:01:49 for ResidualVM by doxygen 1.7.1
curved edge   curved edge