-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPathLinker.cpp
More file actions
363 lines (299 loc) · 9.18 KB
/
PathLinker.cpp
File metadata and controls
363 lines (299 loc) · 9.18 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Implementation of a path linker that allows to directly go to any parent directory
// Initially proposed and coded by Julian Mitchell <jupeos@gmail.com>
#include <sstream>
#include <fx.h>
#include "xfedefs.h"
#include "xfeutils.h"
#include "XFileExplorer.h"
#include "PathLinker.h"
#define REFRESH_INTERVAL 1000
FXDEFMAP(PathLinker) PathLinkerMap[] =
{
FXMAPFUNC(SEL_FOCUSIN, PathLinker::ID_FOCUS_BUTTON, PathLinker::onCmdFocusButton),
FXMAPFUNCS(SEL_LEFTBUTTONPRESS, PathLinker::ID_START_LINK, PathLinker::ID_END_LINK, PathLinker::pathButtonPressed),
FXMAPFUNC(SEL_UPDATE, 0, PathLinker::onUpdPath),
};
FXIMPLEMENT(PathLinker, FXHorizontalFrame, PathLinkerMap, ARRAYNUMBER(PathLinkerMap))
// Construct object
PathLinker::PathLinker(FXComposite* a, FileList* flist, DirList* dlist, FXuint opts) : FXHorizontalFrame(a, opts, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2)
{
filelist = flist;
dirlist = dlist;
// Add some path links
int id = ID_START_LINK;
for (int i = 0; i < MAX_LINKS; i++)
{
std::stringstream ss;
ss << i;
linkButtons.push_back(new FXButton(this, (ss.str() + PATHSEPSTRING).c_str(), NULL, this, id, BUTTON_NORMAL, 0, 0, 0, 0, 5, 5, 0, 0));
id++;
linkButtons[i]->hide();
linkButtons[i]->setDefaultCursor(getApp()->getDefaultCursor(DEF_HAND_CURSOR));
}
// Initializations
visitedPath = PATHSEPSTRING;
nbActiveButtons = 0;
currentButton = 0;
// Right most button is a TextLabel and is only used for focus
focusButton = new TextLabel(this, 0, this, ID_FOCUS_BUTTON, LAYOUT_FILL_X|LAYOUT_FILL_Y);
// Create highlight font (bold if normal font is normal, and normal if normal font is bold)
FXFontDesc fontdesc;
normalFont = getApp()->getNormalFont();
normalFont->getFontDesc(fontdesc);
if (fontdesc.weight == FXFont::Normal)
{
fontdesc.weight = FXFont::Bold;
}
else
{
fontdesc.weight = FXFont::Normal;
}
highlightFont = new FXFont(getApp(), fontdesc);
highlightFont->create();
// Set the focus button initial color
focusButton->setBackColor(getApp()->getBaseColor());
}
// Create the path linker
void PathLinker::create()
{
FXHorizontalFrame::create();
}
// Destruct object
PathLinker::~PathLinker()
{
delete highlightFont;
}
// Change current path
void PathLinker::setPath(FXString text)
{
int nextPos = 0;
int previousPos = 0;
// Remove trailing /
FXString path = ::cleanPath(text);
// Indicates if actual path is included in the visited path
int visited;
if (path == PATHSEPSTRING)
{
visited = visitedPath.find(PATHSEPSTRING);
}
else
{
visited = visitedPath.find(path+PATHSEPSTRING);
}
// If actual path is included in the visited path
FXuint index = 0;
if (visited == 0)
{
nextPos = path.find(PATHSEPSTRING, 0);
while (nextPos >= 0)
{
previousPos = nextPos + 1;
nextPos = path.find(PATHSEPSTRING, previousPos);
index++;
}
if (path.length() == 1)
{
index = 0;
}
path = visitedPath;
}
// Hide all of the link buttons
for (int i = 0; i < MAX_LINKS; i++)
{
linkButtons[i]->hide();
linkButtons[i]->setFont(normalFont);
linkButtons[i]->setState(STATE_UP);
}
visitedPath = path;
FXString displayText = visitedPath;
FXuint ind = 0;
nextPos = displayText.find(PATHSEPSTRING, 0);
previousPos = 0;
while (nextPos >= 0)
{
// Root path
if (previousPos == 0)
{
setText(ind, displayText.mid(previousPos, nextPos - previousPos + 1));
}
// Other path
else
{
setText(ind, displayText.mid(previousPos, nextPos - previousPos));
}
ind++;
previousPos = nextPos + 1;
nextPos = displayText.find(PATHSEPSTRING, previousPos);
}
nbActiveButtons = ind+1;
setText(ind, displayText.mid(previousPos, displayText.length()));
if (ind < MAX_LINKS) // Avoid crashing when the number of path links is too high
{
// If actual path is included in the visited path
if (visited >= 0)
{
linkButtons[index]->setFont(highlightFont);
linkButtons[index]->setState(STATE_DOWN);
currentButton = index;
}
else
{
linkButtons[ind]->setFont(highlightFont);
linkButtons[ind]->setState(STATE_DOWN);
currentButton = ind;
}
}
}
// Update current path according to the clicked button
void PathLinker::updatePath(FXString text, FXuint index)
{
// Remove trailing /
FXString path = ::cleanPath(text);
// Hide all of the link buttons
for (int i = 0; i < MAX_LINKS; i++)
{
linkButtons[i]->hide();
linkButtons[i]->setFont(normalFont);
linkButtons[i]->setState(STATE_UP);
}
visitedPath = path;
FXString displayText = visitedPath;
int nextPos = 0;
int previousPos = 0;
FXuint ind = 0;
nextPos = displayText.find(PATHSEPSTRING, 0);
while (nextPos >= 0)
{
// Root path
if (previousPos == 0)
{
setText(ind, displayText.mid(previousPos, nextPos - previousPos + 1));
}
// Other path
else
{
setText(ind, displayText.mid(previousPos, nextPos - previousPos));
}
ind++;
previousPos = nextPos + 1;
nextPos = displayText.find(PATHSEPSTRING, previousPos);
}
nbActiveButtons = ind+1;
setText(ind, displayText.mid(previousPos, displayText.length()));
linkButtons[index]->setFont(highlightFont);
linkButtons[index]->setState(STATE_DOWN);
currentButton = index;
}
void PathLinker::setText(FXuint index, FXString displayText)
{
if (index < MAX_LINKS)
{
// Avoid interpretation of the & character
if (displayText.contains('&'))
{
displayText.substitute("&", 1, "&&", 2);
}
linkButtons[index]->setText(displayText);
if (displayText.length())
{
linkButtons[index]->show();
}
}
}
// Button was pressed
long PathLinker::pathButtonPressed(FXObject* obj, FXSelector sel, void* ptr)
{
// Set the focus on the file list
filelist->setFocus();
FXString filePath("");
int endId = FXSELID(sel);
if (endId == ID_START_LINK)
{
// Selecting root dir
filePath = PATHSEPSTRING;
}
else
{
int rpos = 0;
rpos = visitedPath.rfind((char)PATHSEPSTRING[0], 0x7FFFFFFF, nbActiveButtons - (endId - ID_START_LINK + 1));
filePath = visitedPath.left(rpos+1);
}
// Update the path text
updatePath(visitedPath, endId - ID_START_LINK);
// Update the FileList and DirList directory
filelist->setDirectory(filePath);
if (dirlist)
{
dirlist->setDirectory(filePath, true);
}
return(1);
}
// Gives the focus to the file list when clicking on the focus button
long PathLinker::onCmdFocusButton(FXObject* obj, FXSelector sel, void* ptr)
{
// Set the focus on the file list
filelist->setFocus();
return(1);
}
// Update visited path to delete directories that don't exist anymore
// Also update in the case where the actual link differs from the actual path
long PathLinker::onUpdPath(FXObject* obj, FXSelector sel, void* ptr)
{
// It is not necessary to update when the path linker is not visible
if (shown())
{
// Current path of the file list (the real path)
FXString currentpath = ::cleanPath(filelist->getDirectory());
// Current path link (the one corresponding to the down button)
FXString currentlink;
if (currentButton == 0)
{
currentlink = PATHSEPSTRING;
}
else
{
currentlink = visitedPath.before((char)PATHSEPSTRING[0], currentButton+1);
}
// Test each link for existence and update to the current path if necessary
FXString path = visitedPath;
FXuint n = 1;
while (path != "")
{
if (!::exists(path))
{
visitedPath = filelist->getDirectory();
setPath(visitedPath);
break;
}
// Next path to test
path = visitedPath.rbefore((char)PATHSEPSTRING[0], n);
n++;
}
// If current link and current path differ, update to the current path
if (currentlink != currentpath)
{
setPath(currentpath);
}
}
return(0);
}
void PathLinker::unfocus(void)
{
this->setBackColor(FXRGB(128, 128, 128));
for (int i = 0; i < MAX_LINKS; i++)
{
linkButtons[i]->setBackColor(FXRGB(128, 128, 128));
linkButtons[i]->setTextColor(FXRGB(255, 255, 255));
}
focusButton->setBackColor(FXRGB(128, 128, 128));
}
void PathLinker::focus(void)
{
this->setBackColor(getApp()->getBaseColor());
for (int i = 0; i < MAX_LINKS; i++)
{
linkButtons[i]->setBackColor(getApp()->getBaseColor());
linkButtons[i]->setTextColor(getApp()->getForeColor());
}
focusButton->setBackColor(getApp()->getBaseColor());
}