2009-05-08 13:28:41 +00:00
/*
* OpenClonk , http : //www.openclonk.org
*
2013-12-17 20:01:09 +00:00
* Copyright ( c ) 2008 - 2009 , RedWolf Design GmbH , http : //www.clonk.de/
2016-04-03 18:18:29 +00:00
* Copyright ( c ) 2009 - 2016 , The OpenClonk Team and contributors
2009-05-08 13:28:41 +00:00
*
2013-12-17 20:01:09 +00:00
* Distributed under the terms of the ISC license ; see accompanying file
* " COPYING " for details .
2009-05-08 13:28:41 +00:00
*
2013-12-17 20:01:09 +00:00
* " Clonk " is a registered trademark of Matthes Bender , used with permission .
* See accompanying file " TRADEMARK " for details .
2009-05-08 13:28:41 +00:00
*
2013-12-17 20:01:09 +00:00
* To redistribute this file separately , substitute the full license texts
* for the above references .
2009-05-08 13:28:41 +00:00
*/
// file selection dialogs
# ifndef INC_C4FileSelDlg
# define INC_C4FileSelDlg
2016-04-03 18:07:56 +00:00
# include "gui/C4Gui.h"
# include "c4group/C4Components.h"
2009-05-08 13:28:41 +00:00
// callback handler for file selection dialog
class C4FileSel_BaseCB
2010-03-28 17:58:21 +00:00
{
public :
2017-05-07 11:50:00 +00:00
C4FileSel_BaseCB ( ) = default ;
virtual ~ C4FileSel_BaseCB ( ) = default ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
public :
virtual void OnFileSelected ( const char * szFilename ) = 0 ;
} ;
2009-05-08 13:28:41 +00:00
template < class CB > class C4FileSel_CBEx : public C4FileSel_BaseCB
2010-03-28 17:58:21 +00:00
{
public :
typedef void ( CB : : * FileSelFunc ) ( const char * szFilename , int32_t idToken ) ;
private :
CB * pCBClass ;
FileSelFunc SelFunc ;
int32_t idToken ;
public :
2017-05-07 11:50:00 +00:00
void OnFileSelected ( const char * szFilename ) override
2010-03-28 17:58:21 +00:00
{ if ( pCBClass & & SelFunc ) ( pCBClass - > * SelFunc ) ( szFilename , idToken ) ; }
public :
C4FileSel_CBEx ( CB * pCBClass , FileSelFunc SelFunc , int32_t idToken ) : pCBClass ( pCBClass ) , SelFunc ( SelFunc ) , idToken ( idToken ) { }
} ;
2009-05-08 13:28:41 +00:00
// dialog to select one or more files
class C4FileSelDlg : public C4GUI : : Dialog
2010-03-28 17:58:21 +00:00
{
public :
class ListItem : public C4GUI : : Control
2009-05-08 13:28:41 +00:00
{
2010-03-28 17:58:21 +00:00
protected :
StdStrBuf sFilename ; // full path to file
2017-05-07 11:50:00 +00:00
bool IsFocusOnClick ( ) override { return false ; } // do not focus; keep focus on listbox
2010-03-28 17:58:21 +00:00
2009-05-08 13:28:41 +00:00
public :
2010-03-28 17:58:21 +00:00
ListItem ( const char * szFilename ) ;
2017-05-07 11:50:00 +00:00
~ ListItem ( ) override ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
const char * GetFilename ( ) { return sFilename . getData ( ) ; }
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
// multisel-checkbox-options
virtual bool IsChecked ( ) const { return false ; }
virtual void SetChecked ( bool fChecked ) { }
virtual bool IsGrayed ( ) const { return false ; }
virtual bool UserToggleCheck ( ) { return false ; }
} ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
class DefaultListItem : public ListItem
{
private :
typedef ListItem BaseClass ;
class C4GUI : : Icon * pIco ; class C4GUI : : Label * pLbl ;
class C4GUI : : CheckBox * pCheck ;
class C4KeyBinding * pKeyCheck ; // space activates/deactivates selected file
bool fGrayed ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
protected :
2017-05-07 11:50:00 +00:00
void UpdateOwnPos ( ) override ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
public :
DefaultListItem ( const char * szFilename , bool fTruncateExtension , bool fCheckbox , bool fGrayed , C4GUI : : Icons eIcon ) ;
2017-05-07 11:50:00 +00:00
~ DefaultListItem ( ) override ;
2009-05-08 13:28:41 +00:00
2017-05-07 11:50:00 +00:00
bool IsChecked ( ) const override ;
void SetChecked ( bool fChecked ) override ;
bool IsGrayed ( ) const override { return fGrayed ; }
bool UserToggleCheck ( ) override ;
2010-03-28 17:58:21 +00:00
} ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
private :
typedef C4GUI : : Dialog BaseClass ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
C4KeyBinding * pKeyRefresh , * pKeyEnterOverride ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
C4GUI : : ComboBox * pLocationComboBox ;
C4GUI : : ListBox * pFileListBox ;
C4GUI : : TextWindow * pSelectionInfoBox ;
C4GUI : : Button * btnOK ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
StdCopyStrBuf sTitle ; // dlg title
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
StdCopyStrBuf sPath ; // current path
struct Location
{
StdCopyStrBuf sName ;
StdCopyStrBuf sPath ;
2009-05-08 13:28:41 +00:00
} ;
2010-03-28 17:58:21 +00:00
Location * pLocations ;
int32_t iLocationCount ;
int32_t iCurrentLocationIndex ;
ListItem * pSelection ;
C4FileSel_BaseCB * pSelCallback ;
void UpdateFileList ( ) ; // rebuild file listbox from sPath
protected :
2017-05-07 11:50:00 +00:00
void OnShown ( ) override ;
void UserClose ( bool fOK ) override ; // allow OK only if something is sth is selected
void OnClosed ( bool fOK ) override ; // callback when dlg got closed
2010-03-28 17:58:21 +00:00
2016-11-02 23:58:02 +00:00
virtual const char * GetFileMask ( ) const { return nullptr ; }
2010-03-28 17:58:21 +00:00
virtual bool IsMultiSelection ( ) const { return false ; } // if true, files are checked/unchecked using checkboxes
virtual bool IsItemGrayed ( const char * szFilename ) const { return false ; }
virtual void UpdateSelection ( ) ;
virtual bool HasNoneItem ( ) const { return false ; } // if true, an empty item can be selected
virtual bool HasPreviewArea ( ) const { return true ; }
virtual bool HasExtraOptions ( ) const { return false ; }
virtual void AddExtraOptions ( const C4Rect & rcOptionsRect ) { }
virtual C4GUI : : Icons GetFileItemIcon ( ) const { return C4GUI : : Ico_None ; }
virtual int32_t GetFileSelColWidth ( ) const { return 0 ; } // width of each file selection element; 0 for default all listbox
void OnSelChange ( class C4GUI : : Element * pEl ) { UpdateSelection ( ) ; }
void OnSelDblClick ( class C4GUI : : Element * pEl ) ;
bool KeyRefresh ( ) { UpdateFileList ( ) ; return true ; }
void OnLocationComboFill ( C4GUI : : ComboBox_FillCB * pFiller ) ;
bool OnLocationComboSelChange ( C4GUI : : ComboBox * pForCombo , int32_t idNewSelection ) ;
void AddLocation ( const char * szName , const char * szPath ) ; // add location to be shown in dropdown list
void AddCheckedLocation ( const char * szName , const char * szPath ) ; // add location to be shown in dropdown list, only if path exists and isn't added yet
int32_t GetCurrentLocationIndex ( ) const ;
void SetCurrentLocation ( int32_t idx , bool fRefresh ) ;
virtual ListItem * CreateListItem ( const char * szFilename ) ;
virtual void BeginFileListUpdate ( ) { }
virtual void EndFileListUpdate ( ) { }
public :
C4FileSelDlg ( const char * szRootPath , const char * szTitle , C4FileSel_BaseCB * pSelCallback , bool fInitElements = true ) ;
2017-05-07 11:50:00 +00:00
~ C4FileSelDlg ( ) override ;
2010-03-28 17:58:21 +00:00
void InitElements ( ) ;
void SetPath ( const char * szNewPath , bool fRefresh = true ) ;
void SetSelection ( const char * szNewSelection , bool fFilenameOnly ) ;
2010-04-01 21:08:06 +00:00
StdStrBuf GetSelection ( const char * szFixedSelection , bool fFilenameOnly ) const ; // get single selected file for single selection dlg ';'-separated list for multi selection dlg
2010-03-28 17:58:21 +00:00
} ;
2009-05-08 13:28:41 +00:00
// dialog to select a player file
class C4PlayerSelDlg : public C4FileSelDlg
2010-03-28 17:58:21 +00:00
{
protected :
2017-05-07 11:50:00 +00:00
const char * GetFileMask ( ) const override { return C4CFN_PlayerFiles ; }
C4GUI : : Icons GetFileItemIcon ( ) const override { return C4GUI : : Ico_Player ; }
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
public :
C4PlayerSelDlg ( C4FileSel_BaseCB * pSelCallback ) ;
} ;
2009-05-08 13:28:41 +00:00
// dialog to select definition files
class C4DefinitionSelDlg : public C4FileSelDlg
2010-03-28 17:58:21 +00:00
{
private :
StdStrBuf sFixedSelection ; // initial selection which cannot be deselected
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
protected :
2017-05-07 11:50:00 +00:00
void OnShown ( ) override ;
const char * GetFileMask ( ) const override { return C4CFN_DefFiles ; }
bool IsMultiSelection ( ) const override { return true ; }
bool IsItemGrayed ( const char * szFilename ) const override ;
C4GUI : : Icons GetFileItemIcon ( ) const override { return C4GUI : : Ico_Definition ; }
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
public :
C4DefinitionSelDlg ( C4FileSel_BaseCB * pSelCallback , const char * szFixedSelection ) ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
static bool SelectDefinitions ( C4GUI : : Screen * pOnScreen , StdStrBuf * pSelection ) ;
} ;
2009-05-08 13:28:41 +00:00
// dialog to select portrait files
class C4PortraitSelDlg : public C4FileSelDlg
2010-03-28 17:58:21 +00:00
{
public :
enum { ImagePreviewSize = 100 } ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
private :
class ListItem : public C4FileSelDlg : : ListItem
{
2009-05-08 13:28:41 +00:00
private :
2010-03-28 17:58:21 +00:00
bool fError ; // loading error
bool fLoaded ; // image loaded but not yet scaled
C4FacetSurface fctImage ; // portrait, if loaded
C4FacetSurface fctLoadedImage ; // image as loaded by background thread. Must be scaled by main thread
StdCopyStrBuf sFilenameLabelText ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
protected :
2017-05-07 11:50:00 +00:00
void DrawElement ( C4TargetFacet & cgo ) override ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
public :
ListItem ( const char * szFilename ) ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
void Load ( ) ;
} ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
// portrait loader thread
class LoaderThread : public StdThread
{
private :
std : : list < ListItem * > LoadItems ; // items to be loaded by this thread
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
public :
2017-05-07 11:50:00 +00:00
LoaderThread ( ) = default ;
~ LoaderThread ( ) override { Stop ( ) ; }
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
void ClearLoadItems ( ) ; // stops thread
void AddLoadItem ( ListItem * pItem ) ; // not to be called when thread is running!
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
public :
2017-05-07 11:50:00 +00:00
void Execute ( ) override ;
2010-03-28 17:58:21 +00:00
} ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
private :
LoaderThread ImageLoader ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
protected :
2017-05-07 11:50:00 +00:00
void OnClosed ( bool fOK ) override ;
const char * GetFileMask ( ) const override { return C4CFN_ImageFiles ; }
bool HasNoneItem ( ) const override { return true ; } // if true, a special <none> item can be selected
bool HasPreviewArea ( ) const override { return false ; } // no preview area. Preview images directly
int32_t GetFileSelColWidth ( ) const override { return ImagePreviewSize ; } // width of each file selection element
2009-05-08 13:28:41 +00:00
2017-05-07 11:50:00 +00:00
C4FileSelDlg : : ListItem * CreateListItem ( const char * szFilename ) override ;
void BeginFileListUpdate ( ) override ;
void EndFileListUpdate ( ) override ;
2009-05-08 13:28:41 +00:00
2017-05-07 11:50:00 +00:00
void OnIdle ( ) override ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
public :
2011-08-05 12:58:16 +00:00
C4PortraitSelDlg ( C4FileSel_BaseCB * pSelCallback ) ;
2009-05-08 13:28:41 +00:00
2011-08-05 12:58:16 +00:00
static bool SelectPortrait ( C4GUI : : Screen * pOnScreen , StdStrBuf * pSelection ) ;
2010-03-28 17:58:21 +00:00
} ;
2009-05-08 13:28:41 +00:00
# endif // INC_C4FileSelDlg