1 #define FILESELVERSION 2
2 
3 Image GetDriveImage(char drive_style);
4 Image GetFileIcon(const char *path, bool dir, bool force = false);
5 
6 void DrawFileName(Draw& w, int x, int y, int wcx, int cy, const WString& mname, bool isdir, Font font,
7                   Color ink, Color extink, const WString& desc = Null, Font descfont = Null,
8                   bool justname = false, Color underline = Null);
9 
10 Image NativePathIcon(const char *path, bool folder);
11 Image NativePathIcon(const char *path);
12 
13 Image NativePathLargeIcon(const char *path, bool folder);
14 Image NativePathLargeIcon(const char *path);
15 
16 class FileList : public ColumnList, private Display {
17 public:
18 	virtual void  LeftDown(Point p, dword);
19 	virtual bool  Key(dword key, int count);
20 
21 	virtual void  Paint(Draw& w, const Rect& r, const Value& q,
22 		                Color ink, Color paper, dword style) const;
23 	virtual Size  GetStdSize(const Value& q) const;
24 
25 public:
26 	struct File {
27 		bool   isdir;
28 		bool   unixexe;
29 		bool   hidden;
30 		Image  icon;
31 		String name;
32 		Font   font;
33 		Color  ink;
34 		int64  length;
35 		Time   time;
36 		Color  extink;
37 		String desc;
38 		Font   descfont;
39 		Value  data;
40 		Color  underline;
41 
42 		operator const String&() const { return name; }
43 	};
44 
45 	class Order {
46 	public:
47 		virtual bool operator()(const File& a, const File& b) const = 0;
~Order()48 		virtual ~Order() {}
49 	};
50 
51 private:
52 	int            iconwidth;
53 	EditField      edit;
54 
55 	bool           renaming;
56 	bool           justname;
57 	bool           accelkey;
58 	bool           selectdir;
59 
60 	void        UpdateSelect();
61 	void        Update();
62 	bool        FindChar(int from, int chr);
63 	void        KillEdit();
64 
65 	struct FileOrder;
66 
67 	using ColumnList::GetStdSize;
68 	using ColumnList::Paint;
69 
70 protected:
71 	enum {
72 		TIMEID_STARTEDIT = ColumnList::TIMEID_COUNT,
73 		TIMEID_COUNT
74 	};
75 
76 public:
77 	Event<const String&, const String&> WhenRename;
78 
79 	void        StartEdit();
80 	void        EndEdit();
IsEdit()81 	bool        IsEdit() const                       { return edit.IsVisible(); }
82 	void        OkEdit();
83 
84 	const File& Get(int i) const;
85 	const File& operator[](int i) const              { return Get(i); }
86 	void        Insert(int ii,
87 	                const String& name, const Image& icon = Null,
88 		            Font font = StdFont(), Color ink = SColorText(),
89 	                bool isdir = false, int64 length = 0, Time time = Null, Color extink = Null,
90 	                const String& desc = Null, Font descfont = Null, Value data = Null,
91 	                Color underline = Null, bool unixexe = false, bool hidden = false);
92 	void        Set(int ii,
93 	                const String& name, const Image& icon = Null,
94 		            Font font = StdFont(), Color ink = SColorText(),
95 	                bool isdir = false, int64 length = 0, Time time = Null, Color extink = Null,
96 	                const String& desc = Null, Font descfont = Null, Value data = Null,
97 	                Color underline = Null, bool unixexe = false, bool hidden = false);
98 	void        Set(int ii, const File& f);
99 	void        SetIcon(int ii, const Image& icon);
100 	void        Add(const File& f);
101 	void        Add(const String& name, const Image& icon = Null,
102 		            Font font = StdFont(), Color ink = SColorText(),
103 	                bool isdir = false, int64 length = 0, Time time = Null, Color extink = Null,
104 	                const String& desc = Null, Font descfont = Null, Value data = Null,
105 	                Color underline = Null, bool unixexe = false, bool hidden = false);
106 	String      GetCurrentName() const;
107 
108 	int         Find(const char *name);
109 	bool        FindSetCursor(const char *name);
110 
111 	void        Sort(const Order& order);
112 
IconWidth(int w)113 	FileList&   IconWidth(int w)                     { iconwidth = w; Refresh(); return *this; }
GetIconWidth()114 	int         GetIconWidth() const                 { return iconwidth; }
115 	FileList&   Renaming(bool b = true)              { renaming = b; return *this; }
116 	FileList&   JustName(bool b = true)              { justname = b; Refresh(); return *this; }
117 	FileList&   AccelKey(bool b = true)              { accelkey = b; return *this; }
118 	FileList&   SelectDir(bool b = true)             { selectdir = b; return *this; }
119 
120 	typedef FileList CLASSNAME;
121 
122 	FileList();
123 	virtual ~FileList();
124 };
125 
126 bool Load(FileList& list, const String& dir, const char *patterns, bool dirs = false,
127           Event<bool, const String&, Image&> WhenIcon = Null,
128           FileSystemInfo& filesystem = StdFileSystemInfo(), const String& search = String(),
129           bool hidden = true, bool hiddenfiles = true, bool lazyicons = false);
130 
131 
132 enum {
133 	FILELISTSORT_NAME,
134 	FILELISTSORT_EXT,
135 	FILELISTSORT_TIME,
136 	FILELISTSORT_SIZE,
137 	FILELISTSORT_DESCENDING = 0x8000,
138 };
139 
140 void SortBy(FileList& list, int kind);
141 
142 // deprecated, use Sort above
SortByName(FileList & list)143 inline void SortByName(FileList& list) { SortBy(list, FILELISTSORT_NAME); }
SortByExt(FileList & list)144 inline void SortByExt(FileList& list) { SortBy(list, FILELISTSORT_EXT); }
SortByTime(FileList & list)145 inline void SortByTime(FileList& list) { SortBy(list, FILELISTSORT_TIME); }
146 
147 #ifdef GUI_WIN
148 // Helper class for lazy (using aux thread) evaluation of .exe icons in Win32
149 class LazyExeFileIcons {
150 	TimeCallback tm;
151 	String       dir;
152 	FileList    *list;
153 	int          pos;
154 	Vector<int>  ndx;
155 	Event<bool, const String&, Image&> WhenIcon;
156 
157 	Mutex  mutex;
158 
159 	void   Do();
Restart(int delay)160 	void   Restart(int delay)                 { tm.KillSet(delay, callback(this, &LazyExeFileIcons::Do)); }
161 	WString Path();
162 	void   Done(Image img);
163 
164 public:
165 	void ReOrder();
166 	void Start(FileList& list_, const String& dir_, Event<bool, const String&, Image&> WhenIcon_);
167 };
168 #endif
169 
170 String DirectoryUp(String& dir, bool basedir = false);
171 
172 String FormatFileSize(int64 n);
173 
174 class FileSel : public WithFileSelectorLayout<TopWindow> {
175 public:
176 	virtual bool Key(dword key, int count);
177 	virtual void Activate();
178 
179 private:
180 	SizeGrip    sizegrip;
181 
182 	Button      dirup, mkdir, plus, minus, toggle;
183 
184 protected:
185 	Vector<String> mask;
186 	Vector<String> path;
187 	Vector<String> lru;
188 	int            lastsby;
189 	FileSystemInfo *filesystem;
190 
191 	String         olddir;
192 	String         basedir;
193 	String         activetype;
194 	String         defext;
195 	String         preselect;
196 	Vector<String> fn;
197 
198 #ifdef PLATFORM_WIN32
199 	Array<NetNode> netstack;
200 	Array<NetNode> netnode;
201 #endif
202 
203 #ifdef GUI_WIN
204 	LazyExeFileIcons  lazyicons;
205 #endif
206 
207 	DisplayCtrl    preview_display;
208 	Ctrl          *preview;
209 	FileList       list;
210 	ArrayCtrl      places;
211 
212 	enum {
213 		OPEN, SAVEAS, SELECTDIR
214 	};
215 
216 	int         mode;
217 	bool        asking;
218 	bool        multi;
219 	bool        rdonly;
220 	bool        bidname;
221 	bool        appmodal;
222 	bool        loaded;
223 
224 	Ctrl       *file_ctrl = NULL;
225 	int         file_ctrl_cx;
226 
227 	static StaticMutex li_mutex;
228 	static void      (*li_current)(const String& path, Image& result);
229 	static String      li_path;
230 	static Image       li_result;
231 	static bool        li_running;
232 	static int         li_pos;
233 	TimeCallback       li_tm;
234 
235 	static void LIThread();
236 	String      LIPath();
237 	void        StartLI();
238 	void        DoLI();
ScheduleLI()239 	void        ScheduleLI()                                 { li_tm.KillSet(0, THISBACK(DoLI)); }
240 
241 	void        LoadNet();
242 	void        SelectNet();
243 	bool        PatternMatch(const char *fn);
244 	bool        OpenItem();
OpenItem2()245 	void        OpenItem2()                                  { OpenItem(); }
246 	void        Open();
247 	void        DirUp();
248 	void        MkDir();
249 	void        Plus();
250 	void        Minus();
251 	void        Toggle();
252 	void        Reload();
253 	void        PlusMinus(const char *title, bool sel);
254 	void        Update();
255 	void        FileUpdate();
256 	void        Rename(const String& on, const String& nn);
257 	void        Choice();
258 	void        SearchLoad();
259 	void        Load();
260 	String      FilePath(const String& fn);
261 	void        SetDir(const String& dir);
262 	String      GetDir() const;
263 	void        AddName(Vector<String>& fn, String& o);
264 	String      ResolveLnk(const String& name) const;
265 	String      ResolveLnkDir(const String& name) const;
266 	String      ResolveLnkFile(const String& name) const;
267 	void        Finish();
268 	bool        Execute(int mode);
IsMulti()269 	bool        IsMulti()                                     { return multi && mode == OPEN; }
270 	void        SyncSplitter();
271 	void        InitSplitter();
272 	String      GetMask();
273 	void        GoToPlace();
274 	void        AddPlaceRaw(const String& path, const Image& m, const String& name, const char* group = NULL, int row = -1);
275 	void        AddSystemPlaces(int row = -1);
276 
277 	using       WithFileSelectorLayout<TopWindow>::Title;
278 
279 	typedef FileSel CLASSNAME;
280 
281 public:
282 	static bool IsLnkFile(const String& p);
283 
284 	Event<bool, const String&, Image&> WhenIcon;
285 	void (*WhenIconLazy)(const String& path, Image& result);
286 
287 	void        Serialize(Stream& s);
288 
289 	bool        ExecuteOpen(const char *title = NULL);
290 	bool        ExecuteSaveAs(const char *title = NULL);
291 
292 	bool        ExecuteSelectDir(const char *title = NULL);
293 
Get()294 	String Get() const                           { return GetFile(0); }
295 	void   Set(const String& s);
Set(const Vector<String> & s)296 	void   Set(const Vector<String>& s)          { fn <<= s; bidname = true; }
297 
String()298 	operator String() const                      { return Get(); }
299 	void operator=(const String& s)              { Set(s); }
300 
301 	String operator~() const                     { return Get(); }
302 	void operator<<=(const String& s)            { Set(s); }
303 
GetCount()304 	int    GetCount() const                      { return fn.GetCount(); }
305 	String GetFile(int i) const;
306 	String operator[](int i) const               { return GetFile(i); }
ClearFiles()307 	void   ClearFiles()                          { fn.Clear(); }
308 
GetReadOnly()309 	bool   GetReadOnly() const                   { return readonly; }
GetActiveDir()310 	String GetActiveDir() const                  { return dir.GetData(); }
GetActiveType()311 	int    GetActiveType() const                 { return type.GetIndex(); }
312 
Filesystem(FileSystemInfo & fsys)313 	void   Filesystem(FileSystemInfo& fsys)      { filesystem = &fsys;}
GetFilesystem()314 	FileSystemInfo& GetFilesystem() const        { return *filesystem; }
315 
316 	FileSel& Type(const char *name, const char *ext);
317 	FileSel& Types(const char *d);
318 	FileSel& AllFilesType();
319 	FileSel& ClearTypes();
ActiveDir(const String & d)320 	FileSel& ActiveDir(const String& d)          { dir <<= d; return *this; }
321 	FileSel& ActiveType(int i);
322 	FileSel& PreSelect(const String& path);
DefaultExt(const char * ext)323 	FileSel& DefaultExt(const char *ext)         { defext = ext; return *this; }
Sort(int kind)324 	FileSel& Sort(int kind)						 { sortby.SetData(kind); return *this; }
Columns(int n)325 	FileSel& Columns(int n)						 { list.Columns(n); return *this; }
326 	FileSel& Multi(bool b = true)                { multi = b; return *this; }
327 	FileSel& ReadOnlyOption(bool b = true)       { rdonly = b; return *this; }
328 	FileSel& MkDirOption(bool b = true)          { mkdir.Show(b); return *this; }
NoMkDirOption()329 	FileSel& NoMkDirOption()                     { return MkDirOption(false); }
BaseDir(const char * dir)330 	FileSel& BaseDir(const char *dir)            { basedir = dir; return *this; }
331 	FileSel& Asking(bool b = true)               { asking = b; return *this; }
NoAsking()332 	FileSel& NoAsking()                          { return Asking(false); }
EditFileName(bool b)333 	FileSel& EditFileName(bool b)                { file.SetEditable(b); return *this; }
NoEditFileName()334 	FileSel& NoEditFileName()                    { return EditFileName(false); }
335 	FileSel& AppModal(bool b = true)             { appmodal = b; return *this; }
NoAppModal()336 	FileSel& NoAppModal()                        { return AppModal(false); }
337 	FileSel& Preview(Ctrl& ctrl);
338 	FileSel& Preview(const Display& d);
339 	FileSel& ClearPlaces();
340 	FileSel& AddPlace(const String& path, const Image& m, const String& name, const char* group = NULL, int row = -1);
341 	FileSel& AddPlace(const String& path, const String& name, const char* group = NULL, int row = -1);
342 	FileSel& AddPlace(const String& path, const char* group = NULL, int row = -1);
343 	FileSel& AddPlaceSeparator();
344 	FileSel& AddStandardPlaces();
FileCtrl(Ctrl & ext,int cx)345 	FileSel& FileCtrl(Ctrl& ext, int cx)         { file_ctrl = &ext; file_ctrl_cx = cx; return *this; }
FileCtrl(Ctrl & ext)346 	FileSel& FileCtrl(Ctrl& ext)                 { return FileCtrl(ext, ext.GetMinSize().cx); }
347 
348 	FileSel();
349 	virtual ~FileSel();
350 };
351 
352 String SelectFileOpen(const char *types);
353 String SelectFileSaveAs(const char *types);
354 String SelectDirectory();
355 String SelectLoadFile(const char *types);
356 bool   SelectSaveFile(const char *types, const String& data);
357 
358 struct SelectFileIn : FileIn {
359 	SelectFileIn(const char *types);
360 };
361 
362 struct SelectFileOut : FileOut {
363 	SelectFileOut(const char *types);
364 };
365