File: FilePicker.h

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (140 lines) | stat: -rw-r--r-- 3,617 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#pragma once
#include "Core/Io/Url.h"
#include "Core/EnginePtr.h"
#include "Core/CloneEnv.h"
#include "Frame.h"

namespace gui {

	/**
	 * A collection of file types suitable to pass to the open and save dialogs.
	 *
	 * A file type here directly corresponds to a file extension.
	 */
	class FileTypes : public Object {
		STORM_CLASS;
	public:
		// Create. Specify a title for all file types in here. This will be shown as a "catch all"
		// option in the file picker.
		STORM_CTOR FileTypes(Str *title);

		// Deep copy.
		void STORM_FN deepCopy(CloneEnv *env);

		// Add a set of file types with a label. Note that `exts` should *not* contain "*.". That
		// is, simply write "foo" to match files that end in ".foo".
		void STORM_FN add(Str *title, Array<Str *> *exts);

		// Elements stored in here.
		class Elem {
			STORM_VALUE;
		public:
			STORM_CTOR Elem(Str *title, Array<Str *> *exts);

			void STORM_FN deepCopy(CloneEnv *env);

			Str *title;
			Array<Str *> *exts;

			// Get a title with all extensions in it.
			Str *extTitle() const;
		};

		// Title.
		Str *title;

		// File types.
		Array<Elem> *elements;

		// Allow any file types?
		Bool allowAny;

		// To string.
		void STORM_FN toS(StrBuf *to) const;
	};

	/**
	 * A file picker dialog window.
	 *
	 * This is a common base class for both "open" and "save" varieties of the dialog.
	 */
	class FilePicker : public ObjectOn<Ui> {
		STORM_CLASS;
	public:
		// Create an open dialog for files.
		static FilePicker *STORM_FN open(FileTypes *types) ON(Ui);

		// Create a save dialog for files. 'suggestion' is the name suggestion present initially.
		static FilePicker *STORM_FN save(FileTypes *types, MAYBE(Str *) suggestion) ON(Ui);

		// Create a file picker dialog that allows selecting directories.
		static FilePicker *STORM_FN folder(EnginePtr e) ON(Ui);

		// Set the default folder when the file picker is opened. If unset, a reasonable default
		// will be picked by the underlying implementation.
		FilePicker *STORM_ASSIGN defaultFolder(Url *folder);

		// Set the title of the dialog.
		FilePicker *STORM_ASSIGN title(Str *caption);

		// Set the label of the OK button. If not set, a reasonable default will be selected by the
		// system.
		FilePicker *STORM_ASSIGN okLabel(Str *label);

		// Set the label of the Cancel button. If not set, a reasonable default will be selected by
		// the system.
		FilePicker *STORM_ASSIGN cancelLabel(Str *label);

		// Allow multiple file selection.
		FilePicker *STORM_FN multiselect();
		FilePicker *STORM_ASSIGN multiselect(Bool allow);

		// Show the dialog. Returns 'false' if cancelled.
		Bool STORM_FN show(MAYBE(Frame *) parent);

		// Get the result, assuming it is a single file.
		MAYBE(Url *) STORM_FN result();

		// Get the entire array of results. Use this for multi-select.
		Array<Url *> *STORM_FN results();

	private:
		// Mode.
		enum {
			mOpen, mSave, mFolder,
			mMulti = 0x10
		};

		// Create. Called from the factory functions.
		FilePicker(Nat mode, FileTypes *types, Str *defName);

		// Mode.
		Nat mode;

		// File types to pick. NULL if we're picking directories.
		MAYBE(FileTypes *) types;

		// Suggested filename (intended for "save" dialogs).
		MAYBE(Str *) defName;

		// Initial folder.
		MAYBE(Url *) defFolder;

		// Title.
		MAYBE(Str *) caption;

		// OK caption.
		MAYBE(Str *) ok;

		// Cancel caption.
		MAYBE(Str *) cancel;

		// Picked files.
		Array<Url *> *res;

		// Clean a Url according to the "types" object (i.e. make sure it has a proper
		// extension). 'selected' is the selected filter.
		Url *cleanUrl(Nat selected, Url *url);
	};

}