1 /********************************************************************************
2 *                                                                               *
3 *                  F i l e   S e l e c t i o n   W i d g e t                    *
4 *                                                                               *
5 *********************************************************************************
6 * Copyright (C) 1998,2021 by Jeroen van der Zijp.   All Rights Reserved.        *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify          *
9 * it under the terms of the GNU Lesser General Public License as published by   *
10 * the Free Software Foundation; either version 3 of the License, or             *
11 * (at your option) any later version.                                           *
12 *                                                                               *
13 * This library is distributed in the hope that it will be useful,               *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
16 * GNU Lesser General Public License for more details.                           *
17 *                                                                               *
18 * You should have received a copy of the GNU Lesser General Public License      *
19 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
20 ********************************************************************************/
21 #ifndef FXFILESELECTOR_H
22 #define FXFILESELECTOR_H
23 
24 #ifndef FXPACKER_H
25 #include "FXPacker.h"
26 #endif
27 
28 namespace FX {
29 
30 
31 class FXFileAssociations;
32 class FXFileList;
33 class FXTextField;
34 class FXComboBox;
35 class FXDirBox;
36 class FXButton;
37 class FXMenuButton;
38 class FXIcon;
39 class FXMenuPane;
40 class FXCheckButton;
41 class FXMatrix;
42 class FXIconSource;
43 class FXHorizontalFrame;
44 
45 
46 /// File selection modes
47 enum {
48   SELECTFILE_ANY,             /// A single file, existing or not (to save to)
49   SELECTFILE_EXISTING,        /// An existing file (to load)
50   SELECTFILE_MULTIPLE,        /// Multiple existing files
51   SELECTFILE_MULTIPLE_ALL,    /// Multiple existing files or directories, but not '.' and '..'
52   SELECTFILE_DIRECTORY        /// Existing directory, including '.' or '..'
53   };
54 
55 
56 /// File selection widget
57 class FXAPI FXFileSelector : public FXPacker {
58   FXDECLARE(FXFileSelector)
59 protected:
60   FXFileList        *filebox;           // File list widget
61   FXTextField       *filename;          // File name entry field
62   FXComboBox        *filefilter;        // Combobox for pattern list
63   FXMenuPane        *bookmarkmenu;      // Menu for bookmarks
64   FXHorizontalFrame *navbuttons;        // Navigation buttons
65   FXHorizontalFrame *fileboxframe;      // Frame around file list
66   FXMatrix          *entryblock;        // Entry block
67   FXCheckButton     *readonly;          // Open file as read only
68   FXDirBox          *dirbox;            // Directory hierarchy list
69   FXButton          *accept;            // Accept button
70   FXButton          *cancel;            // Cancel button
71   FXIcon            *updiricon;         // Up directory icon
72   FXIcon            *listicon;          // List mode icon
73   FXIcon            *detailicon;        // Detail mode icon
74   FXIcon            *iconsicon;         // Icon mode icon
75   FXIcon            *homeicon;          // Go home icon
76   FXIcon            *workicon;          // Go home icon
77   FXIcon            *shownicon;         // Files shown icon
78   FXIcon            *hiddenicon;        // Files hidden icon
79   FXIcon            *bookmarkicon;      // Book mark icon
80   FXIcon            *bookaddicon;       // Book add icon
81   FXIcon            *bookdelicon;       // Book delete icon
82   FXIcon            *bookclricon;       // Book clear icon
83   FXIcon            *sortingicon;       // Sorting icon
84   FXIcon            *newicon;           // New directory icon
85   FXIcon            *renameicon;        // Rename file icon
86   FXIcon            *copyicon;          // Copy file icon
87   FXIcon            *moveicon;          // Rename file icon
88   FXIcon            *linkicon;          // Link file icon
89   FXIcon            *deleteicon;        // Delete file icon
90   FXRecentFiles      bookmarks;         // Bookmarked places
91   FXuint             selectmode;        // Select mode
92   FXbool             navigable;         // May navigate
93 protected:
FXFileSelector()94   FXFileSelector(){}
95   static FXint countFilenames(const FXString& string);
96   static FXString decodeFilename(const FXString& string,FXint n=0);
97   static FXString encodeFilename(const FXString& string);
98 private:
99   FXFileSelector(const FXFileSelector&);
100   FXFileSelector &operator=(const FXFileSelector&);
101 public:
102   long onCmdAccept(FXObject*,FXSelector,void*);
103   long onCmdFilter(FXObject*,FXSelector,void*);
104   long onCmdItemDblClicked(FXObject*,FXSelector,void*);
105   long onCmdItemSelected(FXObject*,FXSelector,void*);
106   long onCmdItemDeselected(FXObject*,FXSelector,void*);
107   long onCmdDirectoryUp(FXObject*,FXSelector,void*);
108   long onUpdDirectoryUp(FXObject*,FXSelector,void*);
109   long onUpdDirTree(FXObject*,FXSelector,void*);
110   long onCmdDirTree(FXObject*,FXSelector,void*);
111   long onCmdHome(FXObject*,FXSelector,void*);
112   long onCmdWork(FXObject*,FXSelector,void*);
113   long onCmdBookmark(FXObject*,FXSelector,void*);
114   long onCmdUnBookmark(FXObject*,FXSelector,void*);
115   long onCmdVisit(FXObject*,FXSelector,void*);
116   long onCmdNew(FXObject*,FXSelector,void*);
117   long onUpdNew(FXObject*,FXSelector,void*);
118   long onCmdRename(FXObject*,FXSelector,void*);
119   long onCmdCopy(FXObject*,FXSelector,void*);
120   long onCmdMove(FXObject*,FXSelector,void*);
121   long onCmdLink(FXObject*,FXSelector,void*);
122   long onCmdRemove(FXObject*,FXSelector,void*);
123   long onUpdSelected(FXObject*,FXSelector,void*);
124   long onPopupMenu(FXObject*,FXSelector,void*);
125   long onCmdImageSize(FXObject*,FXSelector,void*);
126   long onUpdImageSize(FXObject*,FXSelector,void*);
127   long onUpdNavigable(FXObject*,FXSelector,void*);
128 public:
129   enum {
130     ID_FILEFILTER=FXPacker::ID_LAST,
131     ID_ACCEPT,
132     ID_FILELIST,
133     ID_DIRECTORY_UP,
134     ID_DIRTREE,
135     ID_MINI_SIZE,
136     ID_NORMAL_SIZE,
137     ID_MEDIUM_SIZE,
138     ID_GIANT_SIZE,
139     ID_HOME,
140     ID_WORK,
141     ID_BOOKMARK,
142     ID_UNBOOKMARK,
143     ID_BOOKMENU,
144     ID_VISIT,
145     ID_NEW,
146     ID_RENAME,
147     ID_COPY,
148     ID_MOVE,
149     ID_LINK,
150     ID_REMOVE,
151     ID_LAST
152     };
153 public:
154 
155   /// Constructor
156   FXFileSelector(FXComposite *p,FXObject* tgt=NULL,FXSelector sel=0,FXuint opts=0,FXint x=0,FXint y=0,FXint w=0,FXint h=0);
157 
158   /// Return a pointer to the "Accept" button
acceptButton()159   FXButton *acceptButton() const { return accept; }
160 
161   /// Return a pointer to the "Cancel" button
cancelButton()162   FXButton *cancelButton() const { return cancel; }
163 
164   /// Change file name
165   void setFilename(const FXString& path);
166 
167   /// Return file name, if any
168   FXString getFilename() const;
169 
170   /**
171   * Return array of strings containing the selected file names, terminated
172   * by an empty string.  This string array must be freed using delete [].
173   * If no files were selected, a NULL is returned.
174   */
175   FXString* getFilenames() const;
176 
177   /// Change directory
178   void setDirectory(const FXString& path);
179 
180   /// Return directory
181   FXString getDirectory() const;
182 
183   /// Change file selection mode; the default is SELECTFILE_ANY
184   void setSelectMode(FXuint mode);
185 
186   /// Return file selection mode
getSelectMode()187   FXuint getSelectMode() const { return selectmode; }
188 
189   /// Change file pattern
190   void setPattern(const FXString& ptrn);
191 
192   /// Return file pattern
193   FXString getPattern() const;
194 
195   /// Change wildcard matching mode (see FXPath)
196   void setMatchMode(FXuint mode);
197 
198   /// Return wildcard matching mode
199   FXuint getMatchMode() const;
200 
201   /**
202   * Change the list of file patterns shown in the file dialog.
203   * Each pattern comprises an optional name, followed by a pattern in
204   * parentheses.  The patterns are separated by newlines.
205   * For example,
206   *
207   *  "*\n*.cpp,*.cc\n*.hpp,*.hh,*.h"
208   *
209   * and
210   *
211   *  "All Files (*)\nC++ Sources (*.cpp,*.cc)\nC++ Headers (*.hpp,*.hh,*.h)"
212   *
213   * will set the same three patterns, but the former shows no pattern names.
214   */
215   void setPatternList(const FXString& patterns);
216 
217   /// Return list of patterns
218   FXString getPatternList() const;
219 
220   /**
221   * After setting the list of patterns, this call will
222   * initially select pattern n as the active one.
223   */
224   void setCurrentPattern(FXint patno);
225 
226   /// Return current pattern number
227   FXint getCurrentPattern() const;
228 
229   /// Change pattern text for pattern number
230   void setPatternText(FXint patno,const FXString& text);
231 
232   /// Get pattern text for given pattern number
233   FXString getPatternText(FXint patno) const;
234 
235   /// Return number of patterns
236   FXint getNumPatterns() const;
237 
238   /// Allow pattern entry
239   void allowPatternEntry(FXbool flag);
240 
241   /// Return true if pattern entry is allowed
242   FXbool allowPatternEntry() const;
243 
244   /// Set the inter-item spacing (in pixels)
245   void setItemSpace(FXint s);
246 
247   /// Return the inter-item spacing (in pixels)
248   FXint getItemSpace() const;
249 
250   /// Change file list style
251   void setFileBoxStyle(FXuint style);
252 
253   /// Return file list style
254   FXuint getFileBoxStyle() const;
255 
256   /// Return true if showing hidden files
257   FXbool showHiddenFiles() const;
258 
259   /// Show or hide hidden files
260   void showHiddenFiles(FXbool flag);
261 
262   /// Return true if image preview on
263   FXbool showImages() const;
264 
265   /// Show or hide preview images
266   void showImages(FXbool flag);
267 
268   /// Return images preview size
269   FXint getImageSize() const;
270 
271   /// Change images preview size
272   void setImageSize(FXint size);
273 
274   /// Show readonly button
275   void showReadOnly(FXbool flag);
276 
277   /// Return true if readonly is shown
278   FXbool shownReadOnly() const;
279 
280   /// Set initial state of readonly button
281   void setReadOnly(FXbool flag);
282 
283   /// Get readonly state
284   FXbool getReadOnly() const;
285 
286   /// Allow or disallow navigation
allowNavigation(FXbool flag)287   void allowNavigation(FXbool flag){ navigable=flag; }
288 
289   /// Is navigation allowed?
allowNavigation()290   FXbool allowNavigation() const { return navigable; }
291 
292   /// Set draggable files
293   void setDraggableFiles(FXbool flag);
294 
295   /// Are files draggable?
296   FXbool getDraggableFiles() const;
297 
298   /// Set file time format
299   void setTimeFormat(const FXString& fmt);
300 
301   /// Return file time format
302   FXString getTimeFormat() const;
303 
304   /// Change file associations; delete old ones if owned
305   void setAssociations(FXFileAssociations* assoc,FXbool owned=false);
306 
307   /// Return file associations
308   FXFileAssociations* getAssociations() const;
309 
310   /// Change icon loader
311   void setIconSource(FXIconSource* src);
312 
313   /// Return icon loader
314   FXIconSource* getIconSource() const;
315 
316   /**
317   * Given filename pattern of the form "GIF Format (*.gif)",
318   * returns the pattern only, i.e. "*.gif" in this case.
319   * If the parentheses are not found then returns the entire
320   * input pattern.
321   */
322   static FXString patternFromText(const FXString& pattern);
323 
324   /**
325   * Given a pattern of the form "*.gif,*.GIF", return
326   * the first extension of the pattern, i.e. "gif" in this
327   * example. Returns empty string if it doesn't work out.
328   */
329   static FXString extensionFromPattern(const FXString& pattern);
330 
331   /// Save object to a stream
332   virtual void save(FXStream& store) const;
333 
334   /// Load object from a stream
335   virtual void load(FXStream& store);
336 
337   /// Destructor
338   virtual ~FXFileSelector();
339   };
340 
341 }
342 
343 #endif
344