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

filebrowser-dialog.cpp

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 #include "gui/filebrowser-dialog.h"
00024 
00025 #include "common/system.h"
00026 #include "common/algorithm.h"
00027 #include "common/savefile.h"
00028 #include "common/str-array.h"
00029 
00030 #include "common/translation.h"
00031 
00032 #include "gui/widgets/list.h"
00033 #include "gui/gui-manager.h"
00034 #include "gui/message.h"
00035 
00036 namespace GUI {
00037 
00038 enum {
00039     kChooseCmd = 'Chos'
00040 };
00041 
00042 FileBrowserDialog::FileBrowserDialog(const char *title, const char *fileExtension, int mode)
00043     : Dialog("FileBrowser"), _mode(mode), _fileExt(fileExtension) {
00044 
00045     _fileMask = "*.";
00046     _fileMask += fileExtension;
00047     _fileList = NULL;
00048 
00049     new StaticTextWidget(this, "FileBrowser.Headline", title ? title :
00050                     mode == kFBModeLoad ? _("Choose file for loading") : _("Enter filename for saving"));
00051 
00052     _fileName = new EditTextWidget(this, "FileBrowser.Filename", "");
00053 
00054     if (mode == kFBModeLoad)
00055         _fileName->setEnabled(false);
00056 
00057     // Add file list
00058     _fileList = new ListWidget(this, "FileBrowser.List");
00059     _fileList->setNumberingMode(kListNumberingOff);
00060     _fileList->setEditable(false);
00061 
00062     _backgroundType = GUI::ThemeEngine::kDialogBackgroundPlain;
00063 
00064     // Buttons
00065     new ButtonWidget(this, "FileBrowser.Cancel", _("Cancel"), 0, kCloseCmd);
00066     new ButtonWidget(this, "FileBrowser.Choose", _("Choose"), 0, kChooseCmd);
00067 }
00068 
00069 void FileBrowserDialog::open() {
00070     // Call super implementation
00071     Dialog::open();
00072 
00073     updateListing();
00074 }
00075 
00076 void FileBrowserDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 data) {
00077     switch (cmd) {
00078     case kChooseCmd:
00079         if (_fileName->getEditString().empty())
00080             break;
00081 
00082         normalieFileName();
00083 
00084         if (!isProceedSave())
00085             break;
00086 
00087         setResult(1);
00088         close();
00089         break;
00090     case kListSelectionChangedCmd:
00091         _fileName->setEditString(_fileList->getList().operator[](_fileList->getSelected()).c_str());
00092         _fileName->markAsDirty();
00093         break;
00094     case kListItemActivatedCmd:
00095     case kListItemDoubleClickedCmd:
00096         normalieFileName();
00097 
00098         if (!isProceedSave())
00099             break;
00100 
00101         setResult(1);
00102         close();
00103         break;
00104     default:
00105         Dialog::handleCommand(sender, cmd, data);
00106     }
00107 }
00108 
00109 void FileBrowserDialog::normalieFileName() {
00110     Common::String filename = _fileName->getEditString();
00111 
00112     if (filename.matchString(_fileMask))
00113         return;
00114 
00115     _fileName->setEditString(filename + "." + _fileExt);
00116 }
00117 
00118 
00119 bool FileBrowserDialog::isProceedSave() {
00120     bool matched = false;
00121 
00122     if (_mode == kFBModeLoad)
00123         return true;
00124 
00125     for (ListWidget::StringArray::const_iterator file = _fileList->getList().begin(); file != _fileList->getList().end(); ++file) {
00126         if (*file == _fileName->getEditString()) {
00127             matched = true;
00128             break;
00129         }
00130     }
00131 
00132     if (matched) {
00133         GUI::MessageDialog alert(_("Do you really want to overwrite the file?"), _("Yes"), _("No"));
00134 
00135         if (alert.runModal() != GUI::kMessageOK)
00136             return false;
00137     }
00138 
00139     return true;
00140 }
00141 
00142 void FileBrowserDialog::updateListing() {
00143     Common::SaveFileManager *saveFileMan = g_system->getSavefileManager();
00144 
00145     ListWidget::StringArray list;
00146 
00147     Common::StringArray filenames = saveFileMan->listSavefiles(_fileMask);
00148     Common::sort(filenames.begin(), filenames.end());
00149 
00150     for (Common::StringArray::const_iterator file = filenames.begin(); file != filenames.end(); ++file) {
00151         list.push_back(file->c_str());
00152     }
00153 
00154     _fileList->setList(list);
00155     _fileList->scrollTo(0);
00156 
00157     // Finally, redraw
00158     g_gui.scheduleTopDialogRedraw();
00159 }
00160 
00161 } // End of namespace GUI


Generated on Sat Nov 9 2019 05:00:33 for ResidualVM by doxygen 1.7.1
curved edge   curved edge