OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4FileSelDlg.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2008-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 // file selection dialogs
17 
18 #ifndef INC_C4FileSelDlg
19 #define INC_C4FileSelDlg
20 
21 #include "gui/C4Gui.h"
22 #include "c4group/C4Components.h"
23 
24 // callback handler for file selection dialog
26 {
27 public:
29  virtual ~C4FileSel_BaseCB() {}
30 
31 public:
32  virtual void OnFileSelected(const char *szFilename) = 0;
33 };
34 
35 template <class CB> class C4FileSel_CBEx : public C4FileSel_BaseCB
36 {
37 public:
38  typedef void (CB::*FileSelFunc)(const char *szFilename, int32_t idToken);
39 
40 private:
41  CB *pCBClass;
42  FileSelFunc SelFunc;
43  int32_t idToken;
44 public:
45  virtual void OnFileSelected(const char *szFilename)
46  { if (pCBClass && SelFunc) (pCBClass->*SelFunc)(szFilename, idToken); }
47 
48 public:
49  C4FileSel_CBEx(CB * pCBClass, FileSelFunc SelFunc, int32_t idToken) : pCBClass(pCBClass), SelFunc(SelFunc), idToken(idToken) { }
50 };
51 
52 // dialog to select one or more files
54 {
55 public:
56  class ListItem : public C4GUI::Control
57  {
58  protected:
59  StdStrBuf sFilename; // full path to file
60 
61  virtual bool IsFocusOnClick() { return false; } // do not focus; keep focus on listbox
62 
63  public:
64  ListItem(const char *szFilename);
65  virtual ~ListItem();
66 
67  const char *GetFilename() { return sFilename.getData(); }
68 
69  // multisel-checkbox-options
70  virtual bool IsChecked() const { return false; }
71  virtual void SetChecked(bool fChecked) {}
72  virtual bool IsGrayed() const { return false; }
73  virtual bool UserToggleCheck() { return false; }
74  };
75 
76  class DefaultListItem : public ListItem
77  {
78  private:
79  typedef ListItem BaseClass;
80  class C4GUI::Icon *pIco; class C4GUI::Label *pLbl;
81  class C4GUI::CheckBox *pCheck;
82  class C4KeyBinding *pKeyCheck; // space activates/deactivates selected file
83  bool fGrayed;
84 
85  protected:
86  void UpdateOwnPos();
87 
88  public:
89  DefaultListItem(const char *szFilename, bool fTruncateExtension, bool fCheckbox, bool fGrayed, C4GUI::Icons eIcon);
90  virtual ~DefaultListItem();
91 
92  virtual bool IsChecked() const;
93  virtual void SetChecked(bool fChecked);
94  virtual bool IsGrayed() const { return fGrayed; }
95  virtual bool UserToggleCheck();
96  };
97 
98 private:
99  typedef C4GUI::Dialog BaseClass;
100 
101  C4KeyBinding *pKeyRefresh, *pKeyEnterOverride;
102 
103  C4GUI::ComboBox *pLocationComboBox;
104  C4GUI::ListBox *pFileListBox;
105  C4GUI::TextWindow *pSelectionInfoBox;
106  C4GUI::Button *btnOK;
107 
108  StdCopyStrBuf sTitle; // dlg title
109 
110  StdCopyStrBuf sPath; // current path
111  struct Location
112  {
113  StdCopyStrBuf sName;
114  StdCopyStrBuf sPath;
115  };
116  Location *pLocations;
117  int32_t iLocationCount;
118  int32_t iCurrentLocationIndex;
119 
120 
121  ListItem *pSelection;
122  C4FileSel_BaseCB *pSelCallback;
123 
124  void UpdateFileList(); // rebuild file listbox from sPath
125 
126 protected:
127  virtual void OnShown();
128  virtual void UserClose(bool fOK); // allow OK only if something is sth is selected
129  virtual void OnClosed(bool fOK); // callback when dlg got closed
130 
131  virtual const char *GetFileMask() const { return nullptr; }
132  virtual bool IsMultiSelection() const { return false; } // if true, files are checked/unchecked using checkboxes
133  virtual bool IsItemGrayed(const char *szFilename) const { return false; }
134  virtual void UpdateSelection();
135  virtual bool HasNoneItem() const { return false; } // if true, an empty item can be selected
136  virtual bool HasPreviewArea() const { return true; }
137  virtual bool HasExtraOptions() const { return false; }
138  virtual void AddExtraOptions(const C4Rect &rcOptionsRect) {}
139  virtual C4GUI::Icons GetFileItemIcon() const { return C4GUI::Ico_None; }
140  virtual int32_t GetFileSelColWidth() const { return 0; } // width of each file selection element; 0 for default all listbox
141 
143  void OnSelDblClick(class C4GUI::Element *pEl);
144  bool KeyRefresh() { UpdateFileList(); return true; }
146  bool OnLocationComboSelChange(C4GUI::ComboBox *pForCombo, int32_t idNewSelection);
147 
148  void AddLocation(const char *szName, const char *szPath); // add location to be shown in dropdown list
149  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
150  int32_t GetCurrentLocationIndex() const;
151  void SetCurrentLocation(int32_t idx, bool fRefresh);
152 
153  virtual ListItem *CreateListItem(const char *szFilename);
154  virtual void BeginFileListUpdate() {}
155  virtual void EndFileListUpdate() {}
156 
157 public:
158  C4FileSelDlg(const char *szRootPath, const char *szTitle, C4FileSel_BaseCB *pSelCallback, bool fInitElements=true);
159  virtual ~C4FileSelDlg();
160  void InitElements();
161 
162  void SetPath(const char *szNewPath, bool fRefresh=true);
163  void SetSelection(const char *szNewSelection, bool fFilenameOnly);
164  StdStrBuf GetSelection(const char *szFixedSelection, bool fFilenameOnly) const; // get single selected file for single selection dlg ';'-separated list for multi selection dlg
165 };
166 
167 // dialog to select a player file
169 {
170 protected:
171  virtual const char *GetFileMask() const { return C4CFN_PlayerFiles; }
172  virtual C4GUI::Icons GetFileItemIcon() const { return C4GUI::Ico_Player; }
173 
174 public:
175  C4PlayerSelDlg(C4FileSel_BaseCB *pSelCallback);
176 };
177 
178 // dialog to select definition files
180 {
181 private:
182  StdStrBuf sFixedSelection; // initial selection which cannot be deselected
183 
184 protected:
185  virtual void OnShown();
186  virtual const char *GetFileMask() const { return C4CFN_DefFiles; }
187  virtual bool IsMultiSelection() const { return true; }
188  virtual bool IsItemGrayed(const char *szFilename) const;
190 
191 public:
192  C4DefinitionSelDlg(C4FileSel_BaseCB *pSelCallback, const char *szFixedSelection);
193 
194  static bool SelectDefinitions(C4GUI::Screen *pOnScreen, StdStrBuf *pSelection);
195 };
196 
197 // dialog to select portrait files
199 {
200 public:
201  enum { ImagePreviewSize = 100 };
202 
203 private:
204  class ListItem : public C4FileSelDlg::ListItem
205  {
206  private:
207  bool fError; // loading error
208  bool fLoaded; // image loaded but not yet scaled
209  C4FacetSurface fctImage; // portrait, if loaded
210  C4FacetSurface fctLoadedImage; // image as loaded by background thread. Must be scaled by main thread
211  StdCopyStrBuf sFilenameLabelText;
212 
213  protected:
214  void DrawElement(C4TargetFacet &cgo);
215 
216  public:
217  ListItem(const char *szFilename);
218 
219  void Load();
220  };
221 
222  // portrait loader thread
223  class LoaderThread : public StdThread
224  {
225  private:
226  std::list<ListItem *> LoadItems; // items to be loaded by this thread
227 
228  public:
229  LoaderThread() {}
230  virtual ~LoaderThread() { Stop(); }
231 
232  void ClearLoadItems(); // stops thread
233  void AddLoadItem(ListItem *pItem); // not to be called when thread is running!
234 
235  public:
236  virtual void Execute();
237  };
238 
239 private:
240  LoaderThread ImageLoader;
241 
242 protected:
243  void OnClosed(bool fOK);
244  virtual const char *GetFileMask() const { return C4CFN_ImageFiles; }
245  virtual bool HasNoneItem() const { return true; } // if true, a special <none> item can be selected
246  virtual bool HasPreviewArea() const { return false; } // no preview area. Preview images directly
247  virtual int32_t GetFileSelColWidth() const { return ImagePreviewSize; } // width of each file selection element
248 
249  virtual C4FileSelDlg::ListItem *CreateListItem(const char *szFilename);
250  virtual void BeginFileListUpdate();
251  virtual void EndFileListUpdate();
252 
253  virtual void OnIdle();
254 
255 public:
256  C4PortraitSelDlg(C4FileSel_BaseCB *pSelCallback);
257 
258  static bool SelectPortrait(C4GUI::Screen *pOnScreen, StdStrBuf *pSelection);
259 };
260 
261 #endif // INC_C4FileSelDlg
const char * getData() const
Definition: StdBuf.h:450
virtual bool HasNoneItem() const
Definition: C4FileSelDlg.h:135
virtual ListItem * CreateListItem(const char *szFilename)
StdStrBuf GetSelection(const char *szFixedSelection, bool fFilenameOnly) const
void OnSelDblClick(class C4GUI::Element *pEl)
virtual C4GUI::Icons GetFileItemIcon() const
Definition: C4FileSelDlg.h:139
virtual bool IsChecked() const
Definition: C4FileSelDlg.h:70
bool OnLocationComboSelChange(C4GUI::ComboBox *pForCombo, int32_t idNewSelection)
virtual void UpdateSelection()
virtual bool UserToggleCheck()
Definition: C4FileSelDlg.h:73
C4PlayerSelDlg(C4FileSel_BaseCB *pSelCallback)
virtual bool IsChecked() const
static bool SelectDefinitions(C4GUI::Screen *pOnScreen, StdStrBuf *pSelection)
void InitElements()
int32_t GetCurrentLocationIndex() const
void OnLocationComboFill(C4GUI::ComboBox_FillCB *pFiller)
void(CB::* FileSelFunc)(const char *szFilename, int32_t idToken)
Definition: C4FileSelDlg.h:38
Definition: C4Rect.h:29
virtual void SetChecked(bool fChecked)
virtual void SetChecked(bool fChecked)
Definition: C4FileSelDlg.h:71
virtual int32_t GetFileSelColWidth() const
Definition: C4FileSelDlg.h:140
virtual bool IsMultiSelection() const
Definition: C4FileSelDlg.h:187
virtual bool IsItemGrayed(const char *szFilename) const
#define C4CFN_ImageFiles
Definition: C4Components.h:180
virtual int32_t GetFileSelColWidth() const
Definition: C4FileSelDlg.h:247
void OnClosed(bool fOK)
virtual bool IsMultiSelection() const
Definition: C4FileSelDlg.h:132
void SetCurrentLocation(int32_t idx, bool fRefresh)
bool fOK
Definition: C4Gui.h:2082
C4FileSelDlg(const char *szRootPath, const char *szTitle, C4FileSel_BaseCB *pSelCallback, bool fInitElements=true)
#define C4CFN_PlayerFiles
Definition: C4Components.h:167
virtual void OnFileSelected(const char *szFilename)
Definition: C4FileSelDlg.h:45
void AddLocation(const char *szName, const char *szPath)
virtual const char * GetFileMask() const
Definition: C4FileSelDlg.h:131
virtual C4GUI::Icons GetFileItemIcon() const
Definition: C4FileSelDlg.h:189
virtual bool HasExtraOptions() const
Definition: C4FileSelDlg.h:137
virtual void BeginFileListUpdate()
virtual bool IsItemGrayed(const char *szFilename) const
Definition: C4FileSelDlg.h:133
void AddCheckedLocation(const char *szName, const char *szPath)
virtual void OnIdle()
virtual const char * GetFileMask() const
Definition: C4FileSelDlg.h:244
virtual bool HasPreviewArea() const
Definition: C4FileSelDlg.h:246
virtual void BeginFileListUpdate()
Definition: C4FileSelDlg.h:154
void SetSelection(const char *szNewSelection, bool fFilenameOnly)
void SetPath(const char *szNewPath, bool fRefresh=true)
virtual void OnFileSelected(const char *szFilename)=0
Icons
Definition: C4Gui.h:637
void OnSelChange(class C4GUI::Element *pEl)
Definition: C4FileSelDlg.h:142
C4PortraitSelDlg(C4FileSel_BaseCB *pSelCallback)
virtual void EndFileListUpdate()
Definition: C4FileSelDlg.h:155
C4FileSel_CBEx(CB *pCBClass, FileSelFunc SelFunc, int32_t idToken)
Definition: C4FileSelDlg.h:49
static bool SelectPortrait(C4GUI::Screen *pOnScreen, StdStrBuf *pSelection)
virtual bool HasPreviewArea() const
Definition: C4FileSelDlg.h:136
virtual bool IsGrayed() const
Definition: C4FileSelDlg.h:94
virtual void OnShown()
virtual void OnClosed(bool fOK)
virtual bool IsGrayed() const
Definition: C4FileSelDlg.h:72
virtual C4GUI::Icons GetFileItemIcon() const
Definition: C4FileSelDlg.h:172
virtual ~C4FileSelDlg()
#define C4CFN_DefFiles
Definition: C4Components.h:166
virtual void EndFileListUpdate()
virtual bool IsFocusOnClick()
Definition: C4FileSelDlg.h:61
virtual ~C4FileSel_BaseCB()
Definition: C4FileSelDlg.h:29
virtual void UserClose(bool fOK)
virtual const char * GetFileMask() const
Definition: C4FileSelDlg.h:171
DefaultListItem(const char *szFilename, bool fTruncateExtension, bool fCheckbox, bool fGrayed, C4GUI::Icons eIcon)
bool KeyRefresh()
Definition: C4FileSelDlg.h:144
C4DefinitionSelDlg(C4FileSel_BaseCB *pSelCallback, const char *szFixedSelection)
ListItem(const char *szFilename)
virtual const char * GetFileMask() const
Definition: C4FileSelDlg.h:186
virtual void DrawElement(C4TargetFacet &cgo)
const char * GetFilename()
Definition: C4FileSelDlg.h:67
virtual C4FileSelDlg::ListItem * CreateListItem(const char *szFilename)
virtual void AddExtraOptions(const C4Rect &rcOptionsRect)
Definition: C4FileSelDlg.h:138
virtual void OnShown()
virtual bool HasNoneItem() const
Definition: C4FileSelDlg.h:245