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

browser.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/browser.h"
00024 #include "gui/gui-manager.h"
00025 #include "gui/widgets/edittext.h"
00026 #include "gui/widgets/list.h"
00027 
00028 #include "common/config-manager.h"
00029 #include "common/system.h"
00030 #include "common/algorithm.h"
00031 #if defined(USE_SYSDIALOGS)
00032 #include "common/dialogs.h"
00033 #endif
00034 
00035 #include "common/translation.h"
00036 
00037 namespace GUI {
00038 
00039 enum {
00040     kChooseCmd = 'Chos',
00041     kGoUpCmd = 'GoUp',
00042     kHiddenCmd = 'Hidd',
00043     kPathEditedCmd = 'Path'
00044 };
00045 
00046 /* We want to use this as a general directory selector at some point... possible uses
00047  * - to select the data dir for a game
00048  * - to select the place where save games are stored
00049  * - others???
00050  */
00051 
00052 BrowserDialog::BrowserDialog(const char *title, bool dirBrowser)
00053     : Dialog("Browser") {
00054 
00055     _title = title;
00056     _isDirBrowser = dirBrowser;
00057     _fileList = NULL;
00058     _currentPath = NULL;
00059     _showHidden = false;
00060 
00061     // Headline - TODO: should be customizable during creation time
00062     new StaticTextWidget(this, "Browser.Headline", title);
00063 
00064     // Current path - TODO: handle long paths ?
00065     _currentPath = new EditTextWidget(this, "Browser.Path", "", nullptr, 0, kPathEditedCmd);
00066 
00067     // Add file list
00068     _fileList = new ListWidget(this, "Browser.List");
00069     _fileList->setNumberingMode(kListNumberingOff);
00070     _fileList->setEditable(false);
00071 
00072     _backgroundType = GUI::ThemeEngine::kDialogBackgroundPlain;
00073 
00074     // Checkbox for the "show hidden files" state.
00075     _showHiddenWidget = new CheckboxWidget(this, "Browser.Hidden", _("Show hidden files"), _("Show files marked with the hidden attribute"), kHiddenCmd);
00076 
00077     // Buttons
00078     if (g_system->getOverlayWidth() > 320)
00079         new ButtonWidget(this, "Browser.Up", _("Go up"), _("Go to previous directory level"), kGoUpCmd);
00080     else
00081         new ButtonWidget(this, "Browser.Up", _c("Go up", "lowres"), _("Go to previous directory level"), kGoUpCmd);
00082     new ButtonWidget(this, "Browser.Cancel", _("Cancel"), 0, kCloseCmd);
00083     new ButtonWidget(this, "Browser.Choose", _("Choose"), 0, kChooseCmd);
00084 }
00085 
00086 int BrowserDialog::runModal() {
00087 #if defined(USE_SYSDIALOGS)
00088     // Try to use the backend browser
00089     Common::DialogManager *dialogManager = g_system->getDialogManager();
00090     if (dialogManager) {
00091         if (ConfMan.getBool("gui_browser_native", Common::ConfigManager::kApplicationDomain)) {
00092             Common::DialogManager::DialogResult result = dialogManager->showFileBrowser(_title.c_str(), _choice, _isDirBrowser);
00093             if (result != Common::DialogManager::kDialogError) {
00094                 return result;
00095             }
00096         }
00097     }
00098 #endif
00099     // If all else fails, use the GUI browser
00100     return Dialog::runModal();
00101 }
00102 
00103 void BrowserDialog::open() {
00104     // Call super implementation
00105     Dialog::open();
00106 
00107     if (ConfMan.hasKey("browser_lastpath"))
00108         _node = Common::FSNode(ConfMan.get("browser_lastpath"));
00109     if (!_node.isDirectory())
00110         _node = Common::FSNode(".");
00111 
00112     _showHidden = ConfMan.getBool("gui_browser_show_hidden", Common::ConfigManager::kApplicationDomain);
00113     _showHiddenWidget->setState(_showHidden);
00114 
00115     // At this point the file list has already been refreshed by the kHiddenCmd handler
00116 }
00117 
00118 void BrowserDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 data) {
00119     switch (cmd) {
00120     //Search for typed-in directory
00121     case kPathEditedCmd:
00122         _node = Common::FSNode(_currentPath->getEditString());
00123         updateListing();
00124         break;
00125     //Search by text input
00126     case kChooseCmd:
00127         if (_isDirBrowser) {
00128             // If nothing is selected in the list widget, choose the current dir.
00129             // Else, choose the dir that is selected.
00130             int selection = _fileList->getSelected();
00131             if (selection >= 0)
00132                 _choice = _nodeContent[selection];
00133             else
00134                 _choice = _node;
00135             setResult(1);
00136             close();
00137         } else {
00138             int selection = _fileList->getSelected();
00139             if (selection < 0)
00140                 break;
00141             if (_nodeContent[selection].isDirectory()) {
00142                 _node = _nodeContent[selection];
00143                 updateListing();
00144             } else {
00145                 _choice = _nodeContent[selection];
00146                 setResult(1);
00147                 close();
00148             }
00149         }
00150         break;
00151     case kGoUpCmd:
00152         _node = _node.getParent();
00153         updateListing();
00154         break;
00155     case kListItemActivatedCmd:
00156     case kListItemDoubleClickedCmd:
00157         if (_nodeContent[data].isDirectory()) {
00158             _node = _nodeContent[data];
00159             updateListing();
00160         } else if (!_isDirBrowser) {
00161             _choice = _nodeContent[data];
00162             setResult(1);
00163             close();
00164         }
00165         break;
00166     case kListSelectionChangedCmd:
00167         // We do not allow selecting directories in directory
00168         // browser mode, thus we will invalidate the selection
00169         // when the user selects an directory over here.
00170         if (data != (uint32)-1 && _isDirBrowser && !_nodeContent[data].isDirectory())
00171             _fileList->setSelected(-1);
00172         break;
00173     case kHiddenCmd:
00174         // Update whether the user wants hidden files to be shown
00175         _showHidden = _showHiddenWidget->getState();
00176         // We save the state in the application domain to avoid cluttering and
00177         // to prevent odd behavior.
00178         ConfMan.setBool("gui_browser_show_hidden", _showHidden, Common::ConfigManager::kApplicationDomain);
00179         // Update the file listing
00180         updateListing();
00181         break;
00182     default:
00183         Dialog::handleCommand(sender, cmd, data);
00184     }
00185 }
00186 
00187 void BrowserDialog::updateListing() {
00188     // Update the path display
00189     _currentPath->setEditString(_node.getPath());
00190 
00191     // We memorize the last visited path.
00192     ConfMan.set("browser_lastpath", _node.getPath());
00193 
00194     // Read in the data from the file system
00195     if (!_node.getChildren(_nodeContent, Common::FSNode::kListAll, _showHidden))
00196         _nodeContent.clear();
00197     else
00198         Common::sort(_nodeContent.begin(), _nodeContent.end());
00199 
00200     // Populate the ListWidget
00201     ListWidget::StringArray list;
00202     ListWidget::ColorList colors;
00203     for (Common::FSList::iterator i = _nodeContent.begin(); i != _nodeContent.end(); ++i) {
00204         if (i->isDirectory())
00205             list.push_back(i->getDisplayName() + "/");
00206         else
00207             list.push_back(i->getDisplayName());
00208 
00209         if (_isDirBrowser) {
00210             if (i->isDirectory())
00211                 colors.push_back(ThemeEngine::kFontColorNormal);
00212             else
00213                 colors.push_back(ThemeEngine::kFontColorAlternate);
00214         }
00215     }
00216 
00217     if (_isDirBrowser)
00218         _fileList->setList(list, &colors);
00219     else
00220         _fileList->setList(list);
00221     _fileList->scrollTo(0);
00222 
00223     // Finally, redraw
00224     g_gui.scheduleTopDialogRedraw();
00225 }
00226 
00227 } // End of namespace GUI


Generated on Sat May 25 2019 05:00:39 for ResidualVM by doxygen 1.7.1
curved edge   curved edge