2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
2002-11-14 13:46:35 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-11-14 13:46:35 +00:00
|
|
|
*
|
2006-02-11 10:08:56 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2002-11-14 13:46:35 +00:00
|
|
|
*/
|
|
|
|
|
2003-11-02 02:18:16 +00:00
|
|
|
#include "gui/browser.h"
|
|
|
|
#include "gui/newgui.h"
|
|
|
|
#include "gui/ListWidget.h"
|
2002-11-14 13:46:35 +00:00
|
|
|
|
2006-04-28 23:15:43 +00:00
|
|
|
#include "common/config-manager.h"
|
2005-01-10 22:06:49 +00:00
|
|
|
#include "common/system.h"
|
2007-05-23 12:02:31 +00:00
|
|
|
#include "common/algorithm.h"
|
2005-01-10 22:06:49 +00:00
|
|
|
|
2003-11-10 23:40:48 +00:00
|
|
|
namespace GUI {
|
|
|
|
|
2005-04-10 14:49:57 +00:00
|
|
|
enum {
|
|
|
|
kChooseCmd = 'Chos',
|
|
|
|
kGoUpCmd = 'GoUp'
|
|
|
|
};
|
|
|
|
|
2004-02-01 02:03:01 +00:00
|
|
|
#ifdef MACOSX
|
|
|
|
/* On Mac OS X, use the native file selector dialog. We could do the same for
|
|
|
|
* other operating systems.
|
|
|
|
*/
|
|
|
|
|
2005-04-16 17:55:09 +00:00
|
|
|
BrowserDialog::BrowserDialog(const char *title, bool dirBrowser)
|
2006-03-09 05:18:00 +00:00
|
|
|
: Dialog("browser") {
|
2004-02-01 02:03:01 +00:00
|
|
|
_titleRef = CFStringCreateWithCString(0, title, CFStringGetSystemEncoding());
|
2005-04-16 17:55:09 +00:00
|
|
|
_isDirBrowser = dirBrowser;
|
2004-02-01 02:03:01 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 17:55:09 +00:00
|
|
|
BrowserDialog::~BrowserDialog() {
|
2004-02-01 02:03:01 +00:00
|
|
|
CFRelease(_titleRef);
|
|
|
|
}
|
|
|
|
|
2005-04-16 17:55:09 +00:00
|
|
|
int BrowserDialog::runModal() {
|
2004-02-01 02:03:01 +00:00
|
|
|
NavDialogRef dialogRef;
|
2004-02-11 13:31:16 +00:00
|
|
|
WindowRef windowRef = 0;
|
2004-02-01 02:03:01 +00:00
|
|
|
NavDialogCreationOptions options;
|
|
|
|
NavUserAction result;
|
|
|
|
NavReplyRecord reply;
|
|
|
|
OSStatus err;
|
2004-11-20 21:35:49 +00:00
|
|
|
bool choiceMade = false;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-03-17 00:18:07 +00:00
|
|
|
// If in fullscreen mode, switch to windowed mode
|
|
|
|
bool wasFullscreen = g_system->getFeatureState(OSystem::kFeatureFullscreenMode);
|
|
|
|
if (wasFullscreen)
|
|
|
|
g_system->setFeatureState(OSystem::kFeatureFullscreenMode, false);
|
2004-02-01 02:03:01 +00:00
|
|
|
|
|
|
|
// Temporarily show the real mouse
|
2007-01-06 19:22:05 +00:00
|
|
|
CGDisplayShowCursor(kCGDirectMainDisplay);
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-02-01 02:03:01 +00:00
|
|
|
err = NavGetDefaultDialogCreationOptions(&options);
|
|
|
|
assert(err == noErr);
|
|
|
|
options.windowTitle = _titleRef;
|
2004-02-11 13:31:16 +00:00
|
|
|
// options.message = CFSTR("Select your game directory");
|
2004-02-01 02:03:01 +00:00
|
|
|
options.modality = kWindowModalityAppModal;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-04-16 17:55:09 +00:00
|
|
|
if (_isDirBrowser)
|
|
|
|
err = NavCreateChooseFolderDialog(&options, 0, 0, 0, &dialogRef);
|
|
|
|
else
|
|
|
|
err = NavCreateChooseFileDialog(&options, 0, 0, 0, 0, 0, &dialogRef);
|
2004-02-01 02:03:01 +00:00
|
|
|
assert(err == noErr);
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-02-11 13:31:16 +00:00
|
|
|
windowRef = NavDialogGetWindow(dialogRef);
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-02-01 02:03:01 +00:00
|
|
|
err = NavDialogRun(dialogRef);
|
|
|
|
assert(err == noErr);
|
|
|
|
|
2007-01-06 19:22:05 +00:00
|
|
|
CGDisplayHideCursor(kCGDirectMainDisplay);
|
2004-02-01 02:03:01 +00:00
|
|
|
|
|
|
|
result = NavDialogGetUserAction(dialogRef);
|
|
|
|
|
|
|
|
if (result == kNavUserActionChoose) {
|
|
|
|
err = NavDialogGetReply(dialogRef, &reply);
|
|
|
|
assert(err == noErr);
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-02-01 02:03:01 +00:00
|
|
|
if (reply.validRecord && err == noErr) {
|
|
|
|
SInt32 theCount;
|
|
|
|
AECountItems(&reply.selection, &theCount);
|
|
|
|
assert(theCount == 1);
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-02-01 02:03:01 +00:00
|
|
|
AEKeyword keyword;
|
|
|
|
FSRef ref;
|
|
|
|
char buf[4096];
|
|
|
|
err = AEGetNthPtr(&reply.selection, 1, typeFSRef, &keyword, NULL, &ref, sizeof(ref), NULL);
|
|
|
|
assert(err == noErr);
|
|
|
|
err = FSRefMakePath(&ref, (UInt8*)buf, sizeof(buf)-1);
|
|
|
|
assert(err == noErr);
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2008-10-02 16:58:59 +00:00
|
|
|
_choice = Common::FSNode(buf);
|
2004-11-20 21:35:49 +00:00
|
|
|
choiceMade = true;
|
2004-02-01 02:03:01 +00:00
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-02-01 02:03:01 +00:00
|
|
|
err = NavDisposeReply(&reply);
|
|
|
|
assert(err == noErr);
|
|
|
|
}
|
|
|
|
|
|
|
|
NavDialogDispose(dialogRef);
|
|
|
|
|
2004-03-17 00:18:07 +00:00
|
|
|
// If we were in fullscreen mode, switch back
|
|
|
|
if (wasFullscreen)
|
|
|
|
g_system->setFeatureState(OSystem::kFeatureFullscreenMode, true);
|
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
return choiceMade;
|
2004-02-01 02:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2002-11-14 13:46:35 +00:00
|
|
|
/* We want to use this as a general directory selector at some point... possible uses
|
|
|
|
* - to select the data dir for a game
|
|
|
|
* - to select the place where save games are stored
|
|
|
|
* - others???
|
|
|
|
*/
|
|
|
|
|
2005-04-16 17:55:09 +00:00
|
|
|
BrowserDialog::BrowserDialog(const char *title, bool dirBrowser)
|
2008-08-08 15:06:28 +00:00
|
|
|
: Dialog("Browser") {
|
2005-05-16 09:24:28 +00:00
|
|
|
|
2005-04-16 17:55:09 +00:00
|
|
|
_isDirBrowser = dirBrowser;
|
2003-04-30 13:57:57 +00:00
|
|
|
_fileList = NULL;
|
|
|
|
_currentPath = NULL;
|
|
|
|
|
2002-11-14 13:46:35 +00:00
|
|
|
// Headline - TODO: should be customizable during creation time
|
2008-08-08 15:06:28 +00:00
|
|
|
new StaticTextWidget(this, "Browser.Headline", title);
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2002-11-14 14:42:39 +00:00
|
|
|
// Current path - TODO: handle long paths ?
|
2008-08-08 15:06:28 +00:00
|
|
|
_currentPath = new StaticTextWidget(this, "Browser.Path", "DUMMY");
|
2002-11-14 13:46:35 +00:00
|
|
|
|
|
|
|
// Add file list
|
2008-08-08 15:06:28 +00:00
|
|
|
_fileList = new ListWidget(this, "Browser.List");
|
2002-11-14 14:42:39 +00:00
|
|
|
_fileList->setNumberingMode(kListNumberingOff);
|
2004-11-21 13:39:54 +00:00
|
|
|
_fileList->setEditable(false);
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2008-08-13 18:48:14 +00:00
|
|
|
_backgroundType = GUI::Theme::kDialogBackgroundPlain;
|
2006-05-27 05:46:04 +00:00
|
|
|
|
2002-11-14 13:46:35 +00:00
|
|
|
// Buttons
|
2008-08-08 15:06:28 +00:00
|
|
|
new ButtonWidget(this, "Browser.Up", "Go up", kGoUpCmd, 0);
|
|
|
|
new ButtonWidget(this, "Browser.Cancel", "Cancel", kCloseCmd, 0);
|
|
|
|
new ButtonWidget(this, "Browser.Choose", "Choose", kChooseCmd, 0);
|
2002-11-14 14:42:39 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 17:55:09 +00:00
|
|
|
void BrowserDialog::open() {
|
2006-04-28 23:15:43 +00:00
|
|
|
if (ConfMan.hasKey("browser_lastpath"))
|
2008-10-02 16:58:59 +00:00
|
|
|
_node = Common::FSNode(ConfMan.get("browser_lastpath"));
|
2006-05-04 03:44:50 +00:00
|
|
|
if (!_node.isDirectory())
|
2008-10-02 16:58:59 +00:00
|
|
|
_node = Common::FSNode(".");
|
2002-11-14 14:42:39 +00:00
|
|
|
|
|
|
|
// Alway refresh file list
|
|
|
|
updateListing();
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2002-11-14 14:42:39 +00:00
|
|
|
// Call super implementation
|
|
|
|
Dialog::open();
|
|
|
|
}
|
|
|
|
|
2005-04-16 17:55:09 +00:00
|
|
|
void BrowserDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 data) {
|
2002-11-14 14:42:39 +00:00
|
|
|
switch (cmd) {
|
2005-04-16 17:55:09 +00:00
|
|
|
case kChooseCmd:
|
|
|
|
if (_isDirBrowser) {
|
2002-11-19 01:36:47 +00:00
|
|
|
// If nothing is selected in the list widget, choose the current dir.
|
|
|
|
// Else, choose the dir that is selected.
|
|
|
|
int selection = _fileList->getSelected();
|
|
|
|
if (selection >= 0) {
|
2004-11-20 21:35:49 +00:00
|
|
|
_choice = _nodeContent[selection];
|
2002-11-19 01:36:47 +00:00
|
|
|
} else {
|
2004-11-20 21:35:49 +00:00
|
|
|
_choice = _node;
|
2002-11-19 01:36:47 +00:00
|
|
|
}
|
|
|
|
setResult(1);
|
|
|
|
close();
|
2005-04-16 17:55:09 +00:00
|
|
|
} else {
|
2005-04-10 14:33:44 +00:00
|
|
|
int selection = _fileList->getSelected();
|
|
|
|
if (selection < 0)
|
|
|
|
break;
|
|
|
|
if (_nodeContent[selection].isDirectory()) {
|
|
|
|
_node = _nodeContent[selection];
|
|
|
|
updateListing();
|
|
|
|
} else {
|
|
|
|
_choice = _nodeContent[selection];
|
|
|
|
setResult(1);
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kGoUpCmd:
|
|
|
|
_node = _node.getParent();
|
|
|
|
updateListing();
|
|
|
|
break;
|
|
|
|
case kListItemActivatedCmd:
|
|
|
|
case kListItemDoubleClickedCmd:
|
|
|
|
if (_nodeContent[data].isDirectory()) {
|
|
|
|
_node = _nodeContent[data];
|
|
|
|
updateListing();
|
|
|
|
} else {
|
|
|
|
_choice = _nodeContent[data];
|
|
|
|
setResult(1);
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Dialog::handleCommand(sender, cmd, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-16 17:55:09 +00:00
|
|
|
void BrowserDialog::updateListing() {
|
2005-04-10 14:33:44 +00:00
|
|
|
// Update the path display
|
2007-06-05 21:02:35 +00:00
|
|
|
_currentPath->setLabel(_node.getPath());
|
2006-07-10 11:12:11 +00:00
|
|
|
|
2006-04-28 23:15:43 +00:00
|
|
|
// We memorize the last visited path.
|
2007-06-05 21:02:35 +00:00
|
|
|
ConfMan.set("browser_lastpath", _node.getPath());
|
2005-04-10 14:33:44 +00:00
|
|
|
|
|
|
|
// Read in the data from the file system
|
2008-10-02 16:58:59 +00:00
|
|
|
Common::FSNode::ListMode listMode =
|
|
|
|
_isDirBrowser ? Common::FSNode::kListDirectoriesOnly
|
|
|
|
: Common::FSNode::kListAll;
|
2007-06-05 21:02:35 +00:00
|
|
|
if (!_node.getChildren(_nodeContent, listMode)) {
|
2006-07-10 11:12:11 +00:00
|
|
|
_nodeContent.clear();
|
|
|
|
} else {
|
|
|
|
Common::sort(_nodeContent.begin(), _nodeContent.end());
|
|
|
|
}
|
2005-04-10 14:33:44 +00:00
|
|
|
|
|
|
|
// Populate the ListWidget
|
|
|
|
Common::StringList list;
|
2008-09-03 11:22:51 +00:00
|
|
|
for (Common::FSList::iterator i = _nodeContent.begin(); i != _nodeContent.end(); ++i) {
|
2006-04-04 21:16:53 +00:00
|
|
|
if (!_isDirBrowser && i->isDirectory())
|
2007-06-05 21:02:35 +00:00
|
|
|
list.push_back(i->getDisplayName() + "/");
|
2005-04-10 14:33:44 +00:00
|
|
|
else
|
2007-06-05 21:02:35 +00:00
|
|
|
list.push_back(i->getDisplayName());
|
2005-04-10 14:33:44 +00:00
|
|
|
}
|
|
|
|
_fileList->setList(list);
|
|
|
|
_fileList->scrollTo(0);
|
|
|
|
|
|
|
|
// Finally, redraw
|
|
|
|
draw();
|
|
|
|
}
|
|
|
|
|
2005-04-16 17:55:09 +00:00
|
|
|
#endif // MACOSX
|
|
|
|
|
2003-11-10 23:40:48 +00:00
|
|
|
} // End of namespace GUI
|