-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBrowseInputDialog.cpp
More file actions
233 lines (180 loc) · 6.09 KB
/
BrowseInputDialog.cpp
File metadata and controls
233 lines (180 loc) · 6.09 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Input dialog with file browse icon
#include "config.h"
#include "i18n.h"
#include <fx.h>
#include <fxkeys.h>
#include "xfedefs.h"
#include "icons.h"
#include "xfeutils.h"
#include "FileDialog.h"
#include "TextLabel.h"
#include "BrowseInputDialog.h"
extern FXString homedir;
FXDEFMAP(BrowseInputDialog) BrowseInputDialogMap[] =
{
FXMAPFUNC(SEL_KEYPRESS, 0, BrowseInputDialog::onCmdKeyPress),
FXMAPFUNC(SEL_COMMAND, BrowseInputDialog::ID_BROWSE_PATH, BrowseInputDialog::onCmdBrowsePath),
};
// Object implementation
FXIMPLEMENT(BrowseInputDialog, DialogBox, BrowseInputDialogMap, ARRAYNUMBER(BrowseInputDialogMap))
// Construct a dialog box
BrowseInputDialog::BrowseInputDialog(FXWindow* win, FXString inp, FXString message, FXString title, FXString label, FXIcon* ic, FXuint browse, FXbool option, FXString optiontext) :
DialogBox(win, title, DECOR_TITLE|DECOR_BORDER|DECOR_STRETCHABLE|DECOR_MAXIMIZE|DECOR_CLOSE)
{
// Browse type flag
browsetype = browse;
// Buttons
FXHorizontalFrame* buttons = new FXHorizontalFrame(this, PACK_UNIFORM_WIDTH|LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X, 0, 0, 0, 0, 10, 10, 5, 5);
// Accept
new FXButton(buttons, _("&Accept"), NULL, this, ID_ACCEPT, FRAME_RAISED|FRAME_THICK|LAYOUT_RIGHT, 0, 0, 0, 0, 20, 20);
// Cancel
new FXButton(buttons, _("&Cancel"), NULL, this, ID_CANCEL, FRAME_RAISED|FRAME_THICK|LAYOUT_RIGHT, 0, 0, 0, 0, 20, 20);
// Optional check box
checkbutton = new FXHorizontalFrame(this, JUSTIFY_RIGHT|LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X, 0, 0, 0, 0, 10, 10, 0, 0);
if (option)
{
new FXCheckButton(checkbutton, optiontext, this, ID_TOGGLE_OPTION);
}
// Vertical frame
FXVerticalFrame* contents = new FXVerticalFrame(this, LAYOUT_SIDE_TOP|FRAME_NONE|LAYOUT_FILL_X|LAYOUT_FILL_Y);
// Icon and text label
// Note : we display the message in a TextLabel. This allows to copy/paste the file name to the input text field
FXMatrix* matrix1 = new FXMatrix(contents, 2, MATRIX_BY_COLUMNS|LAYOUT_SIDE_TOP|LAYOUT_FILL_X|LAYOUT_FILL_Y);
iconlabel = new FXLabel(matrix1, "", ic, LAYOUT_LEFT|LAYOUT_CENTER_Y|LAYOUT_FILL_ROW);
msg = new TextLabel(matrix1, 30, 0, 0, LAYOUT_LEFT|LAYOUT_CENTER_Y|LAYOUT_FILL_ROW|FRAME_NONE);
msg->setText(message);
msg->setBackColor(getApp()->getBaseColor());
// Label and input field
FXMatrix* matrix2 = new FXMatrix(contents, 3, MATRIX_BY_COLUMNS|LAYOUT_SIDE_TOP|LAYOUT_FILL_X|LAYOUT_FILL_Y);
new FXLabel(matrix2, label, NULL, LAYOUT_LEFT|LAYOUT_CENTER_Y|LAYOUT_FILL_ROW);
input = new FXTextField(matrix2, 40, 0, 0, LAYOUT_CENTER_Y|LAYOUT_CENTER_X|FRAME_SUNKEN|FRAME_THICK|LAYOUT_FILL_COLUMN|LAYOUT_FILL_ROW|LAYOUT_FILL_X);
input->setText(inp);
new FXButton(matrix2, _("\tSelect destination..."), filedialogicon, this, ID_BROWSE_PATH, FRAME_RAISED|FRAME_THICK|LAYOUT_RIGHT|LAYOUT_CENTER_Y, 0, 0, 0, 0, 20, 20);
if (!isUtf8(message.text(), message.length()))
{
new FXLabel(contents, _("=> Warning: file name is not UTF-8 encoded!"), NULL, LAYOUT_LEFT|LAYOUT_CENTER_Y|LAYOUT_FILL_ROW);
}
// Initial directory for browsing
initialdir = homedir;
}
void BrowseInputDialog::create()
{
DialogBox::create();
input->setFocus();
}
BrowseInputDialog::~BrowseInputDialog()
{
delete input;
delete msg;
delete iconlabel;
}
long BrowseInputDialog::onCmdKeyPress(FXObject* sender, FXSelector sel, void* ptr)
{
FXEvent* event = (FXEvent*)ptr;
switch (event->code)
{
case KEY_Escape:
handle(this, FXSEL(SEL_COMMAND, ID_CANCEL), NULL);
return(1);
case KEY_KP_Enter:
case KEY_Return:
handle(this, FXSEL(SEL_COMMAND, ID_ACCEPT), NULL);
return(1);
default:
FXTopWindow::onKeyPress(sender, sel, ptr);
return(1);
}
return(0);
}
long BrowseInputDialog::onCmdBrowsePath(FXObject* o, FXSelector s, void* p)
{
FXString title;
if (browsetype == BROWSE_INPUT_FOLDER)
{
title = _("Select a destination folder");
}
else if (browsetype == BROWSE_INPUT_FILE)
{
title = _("Select a file");
}
else
{
title = _("Select a file or a destination folder");
}
// File dialog
FileDialog browse(this, title);
const char* patterns[] =
{
_("All Files"), "*", NULL
};
browse.setDirectory(initialdir);
browse.setPatternList(patterns);
// Browse files in directory or mixed mode depending on the flag
if (browsetype == BROWSE_INPUT_FOLDER)
{
browse.setSelectMode(SELECT_FILE_DIRECTORY);
}
else if (browsetype == BROWSE_INPUT_FILE)
{
browse.setSelectMode(SELECT_FILE_EXISTING);
}
else
{
browse.setSelectMode(SELECT_FILE_MIXED);
}
if (browse.execute())
{
FXString path = browse.getFilename();
input->setText(path);
}
return(1);
}
// Adjust message size
void BrowseInputDialog::setMessage(FXString message)
{
// Compute the equivalent size in number of columns of '8' of the message string,
// taking into account the real size of the font characters
FXFont* font = getApp()->getNormalFont();
int nbcols = (int)ceil((double)font->getTextWidth(message) / (double)font->getCharWidth('8'));
// Tricks to adjust the dialog width to the real text size
this->setWidth(1);
if (message.length() > MAX_MESSAGE_LENGTH)
{
msg->setNumColumns(MAX_MESSAGE_LENGTH);
}
else
{
msg->setNumColumns(nbcols);
}
msg->setText(message);
}
// Set initial directory
void BrowseInputDialog::setDirectory(const FXString& path)
{
initialdir = path;
}
FXString BrowseInputDialog::getText()
{
return(input->getText());
}
void BrowseInputDialog::setText(const FXString& text)
{
input->setText(text);
}
// Change dialog icon
void BrowseInputDialog::setIcon(FXIcon* icon)
{
iconlabel->setIcon(icon);
}
void BrowseInputDialog::selectAll()
{
input->setSelection(0, (input->getText()).length());
}
void BrowseInputDialog::CursorEnd()
{
input->onCmdCursorEnd(0, 0, 0);
}
void BrowseInputDialog::setSelection(int pos, int len)
{
input->setSelection(pos, len);
}