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 .
2014-02-18 02:34:20 +01:00
*
2002-11-14 13:46:35 +00:00
*/
2003-11-02 02:18:16 +00:00
# include "gui/browser.h"
2018-01-06 16:13:29 +01:00
# include "gui/gui-manager.h"
2018-08-18 09:37:59 +03:00
# include "gui/widgets/edittext.h"
2010-11-16 10:11:57 +00:00
# include "gui/widgets/list.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"
2018-11-20 02:11:59 +00:00
# if defined(USE_SYSDIALOGS)
# include "common/dialogs.h"
# endif
2005-01-10 22:06:49 +00:00
2010-06-15 10:44:51 +00:00
# include "common/translation.h"
2003-11-10 23:40:48 +00:00
namespace GUI {
2005-04-10 14:49:57 +00:00
enum {
kChooseCmd = ' Chos ' ,
2013-01-27 19:11:20 +01:00
kGoUpCmd = ' GoUp ' ,
2018-08-27 06:39:40 +02:00
kHiddenCmd = ' Hidd ' ,
kPathEditedCmd = ' Path '
2005-04-10 14:49:57 +00:00
} ;
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
2018-11-20 02:11:59 +00:00
_title = title ;
2005-04-16 17:55:09 +00:00
_isDirBrowser = dirBrowser ;
2020-01-05 17:48:04 +01:00
_fileList = nullptr ;
_currentPath = nullptr ;
2016-08-29 08:03:40 +02:00
_showHidden = false ;
2003-04-30 13:57:57 +00:00
2002-11-14 13:46:35 +00:00
// Headline - TODO: should be customizable during creation time
2020-06-10 21:37:51 +05:30
new StaticTextWidget ( this , " Browser.Headline " , Common : : convertToU32String ( title ) ) ;
2003-03-06 19:52:54 +00:00
2002-11-14 14:42:39 +00:00
// Current path - TODO: handle long paths ?
2018-08-27 06:39:40 +02:00
_currentPath = new EditTextWidget ( this , " Browser.Path " , " " , nullptr , 0 , kPathEditedCmd ) ;
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-11-10 11:24:55 +00:00
_backgroundType = GUI : : ThemeEngine : : kDialogBackgroundPlain ;
2006-05-27 05:46:04 +00:00
2013-01-27 19:11:20 +01:00
// Checkbox for the "show hidden files" state.
2020-06-10 21:37:51 +05:30
_showHiddenWidget = new CheckboxWidget ( this , " Browser.Hidden " , Common : : convertToU32String ( _ ( " Show hidden files " ) ) , _ ( " Show files marked with the hidden attribute " ) , kHiddenCmd ) ;
2013-01-27 19:11:20 +01:00
2002-11-14 13:46:35 +00:00
// Buttons
2010-09-12 11:43:51 +00:00
if ( g_system - > getOverlayWidth ( ) > 320 )
2020-06-10 21:37:51 +05:30
new ButtonWidget ( this , " Browser.Up " , Common : : convertToU32String ( _ ( " Go up " ) ) , _ ( " Go to previous directory level " ) , kGoUpCmd ) ;
2010-09-12 11:43:51 +00:00
else
2020-06-10 21:37:51 +05:30
new ButtonWidget ( this , " Browser.Up " , Common : : convertToU32String ( _c ( " Go up " , " lowres " ) ) , _ ( " Go to previous directory level " ) , kGoUpCmd ) ;
new ButtonWidget ( this , " Browser.Cancel " , Common : : convertToU32String ( _ ( " Cancel " ) ) , nullptr , kCloseCmd ) ;
new ButtonWidget ( this , " Browser.Choose " , Common : : convertToU32String ( _ ( " Choose " ) ) , nullptr , kChooseCmd ) ;
2002-11-14 14:42:39 +00:00
}
2018-11-20 02:11:59 +00:00
int BrowserDialog : : runModal ( ) {
# if defined(USE_SYSDIALOGS)
// Try to use the backend browser
Common : : DialogManager * dialogManager = g_system - > getDialogManager ( ) ;
if ( dialogManager ) {
2018-11-22 16:26:04 +00:00
if ( ConfMan . getBool ( " gui_browser_native " , Common : : ConfigManager : : kApplicationDomain ) ) {
2018-12-01 00:12:39 +00:00
Common : : DialogManager : : DialogResult result = dialogManager - > showFileBrowser ( _title . c_str ( ) , _choice , _isDirBrowser ) ;
2018-11-22 16:26:04 +00:00
if ( result ! = Common : : DialogManager : : kDialogError ) {
return result ;
}
2018-11-20 02:11:59 +00:00
}
}
# endif
// If all else fails, use the GUI browser
return Dialog : : runModal ( ) ;
}
2005-04-16 17:55:09 +00:00
void BrowserDialog : : open ( ) {
2009-01-10 22:31:15 +00:00
// Call super implementation
Dialog : : 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
2016-08-29 08:03:40 +02:00
_showHidden = ConfMan . getBool ( " gui_browser_show_hidden " , Common : : ConfigManager : : kApplicationDomain ) ;
_showHiddenWidget - > setState ( _showHidden ) ;
// At this point the file list has already been refreshed by the kHiddenCmd handler
2002-11-14 14:42:39 +00:00
}
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 ) {
2018-08-18 09:37:59 +03:00
//Search for typed-in directory
2018-08-27 06:39:40 +02:00
case kPathEditedCmd :
2018-08-18 09:37:59 +03:00
_node = Common : : FSNode ( _currentPath - > getEditString ( ) ) ;
updateListing ( ) ;
break ;
//Search by text input
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 ( ) ;
2009-12-09 16:48:33 +00:00
if ( selection > = 0 )
2004-11-20 21:35:49 +00:00
_choice = _nodeContent [ selection ] ;
2009-12-09 16:48:33 +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 ( ) ;
2009-12-09 16:48:33 +00:00
} else if ( ! _isDirBrowser ) {
2005-04-10 14:33:44 +00:00
_choice = _nodeContent [ data ] ;
setResult ( 1 ) ;
close ( ) ;
}
break ;
2009-12-09 16:48:33 +00:00
case kListSelectionChangedCmd :
// We do not allow selecting directories in directory
// browser mode, thus we will invalidate the selection
// when the user selects an directory over here.
if ( data ! = ( uint32 ) - 1 & & _isDirBrowser & & ! _nodeContent [ data ] . isDirectory ( ) )
_fileList - > setSelected ( - 1 ) ;
break ;
2013-01-27 19:11:20 +01:00
case kHiddenCmd :
// Update whether the user wants hidden files to be shown
_showHidden = _showHiddenWidget - > getState ( ) ;
// We save the state in the application domain to avoid cluttering and
// to prevent odd behavior.
ConfMan . setBool ( " gui_browser_show_hidden " , _showHidden , Common : : ConfigManager : : kApplicationDomain ) ;
// Update the file listing
updateListing ( ) ;
break ;
2005-04-10 14:33:44 +00:00
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
2018-08-18 09:37:59 +03:00
_currentPath - > setEditString ( _node . getPath ( ) ) ;
2006-07-10 11:12:11 +00:00
2006-04-28 23:15:43 +00:00
// We memorize the last visited path.
2020-02-11 00:54:56 +02:00
// Don't memorize a path that is not a directory
if ( _node . isDirectory ( ) ) {
ConfMan . set ( " browser_lastpath " , _node . getPath ( ) ) ;
}
2005-04-10 14:33:44 +00:00
// Read in the data from the file system
2013-01-27 19:11:20 +01:00
if ( ! _node . getChildren ( _nodeContent , Common : : FSNode : : kListAll , _showHidden ) )
2006-07-10 11:12:11 +00:00
_nodeContent . clear ( ) ;
2009-12-09 16:48:33 +00:00
else
2006-07-10 11:12:11 +00:00
Common : : sort ( _nodeContent . begin ( ) , _nodeContent . end ( ) ) ;
2005-04-10 14:33:44 +00:00
// Populate the ListWidget
2010-03-18 15:09:24 +00:00
ListWidget : : StringArray list ;
2009-12-09 16:48:33 +00:00
ListWidget : : ColorList colors ;
2008-09-03 11:22:51 +00:00
for ( Common : : FSList : : iterator i = _nodeContent . begin ( ) ; i ! = _nodeContent . end ( ) ; + + i ) {
2009-12-09 16:48:33 +00:00
if ( 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 ( ) ) ;
2009-12-09 16:48:33 +00:00
if ( _isDirBrowser ) {
if ( i - > isDirectory ( ) )
colors . push_back ( ThemeEngine : : kFontColorNormal ) ;
else
colors . push_back ( ThemeEngine : : kFontColorAlternate ) ;
}
2005-04-10 14:33:44 +00:00
}
2009-12-09 16:48:33 +00:00
if ( _isDirBrowser )
_fileList - > setList ( list , & colors ) ;
else
_fileList - > setList ( list ) ;
2005-04-10 14:33:44 +00:00
_fileList - > scrollTo ( 0 ) ;
// Finally, redraw
2018-01-06 16:13:29 +01:00
g_gui . scheduleTopDialogRedraw ( ) ;
2005-04-10 14:33:44 +00:00
}
2003-11-10 23:40:48 +00:00
} // End of namespace GUI