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
|
//
// Fluid Project File History code for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2025 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
// https://www.fltk.org/COPYING.php
//
// Please see the following page on how to report bugs and issues:
//
// https://www.fltk.org/bugs.php
//
#include "app/history.h"
#include "Fluid.h"
#include "tools/filename.h"
#include "../src/flstring.h"
fld::app::History::History() {
int i, j;
for (i = 0; i < 10; i++) {
for (j = 0; j < FL_PATH_MAX; j++) {
abspath[i][j] = '\0';
relpath[i][j] = '\0';
}
}
}
/**
Load file history from preferences.
This loads the absolute filepaths of the last 10 used design files.
It also computes and stores the relative filepaths for display in
the main menu.
*/
void fld::app::History::load() {
int i;
int max_files;
Fluid.preferences.get("recent_files", max_files, 5);
if (max_files > 10) max_files = 10;
for (i = 0; i < max_files; i++) {
Fluid.preferences.get(Fl_Preferences::Name("file%d", i), abspath[i], "", sizeof(abspath[i]));
if (abspath[i][0]) {
fl_filename_shortened(relpath[i], sizeof(relpath[i]), abspath[i], 48);
if (i == 9) Fluid.history_item[i].flags = FL_MENU_DIVIDER;
else Fluid.history_item[i].flags = 0;
} else break;
}
for (; i < 10; i++) {
if (i) Fluid.history_item[i-1].flags |= FL_MENU_DIVIDER;
Fluid.history_item[i].hide();
}
}
/**
Update file history from preferences.
Add this new filepath to the history and update the main menu.
Writes the new file history to the app preferences.
\param[in] project_file path and filename of .fl project file, will be
converted into an absolute file path based on the current working directory.
*/
void fld::app::History::update(const char *project_file) {
int i;
int max_files;
if (!project_file || !project_file[0]) return;
Fluid.preferences.get("recent_files", max_files, 5);
if (max_files > 10) max_files = 10;
char absolute[FL_PATH_MAX];
fl_filename_absolute(absolute, FL_PATH_MAX, project_file);
fld_fix_separators(absolute);
for (i = 0; i < max_files; i++)
#if defined(_WIN32) || defined(__APPLE__)
if (!strcasecmp(absolute, abspath[i])) break;
#else
if (!strcmp(absolute, abspath[i])) break;
#endif // _WIN32 || __APPLE__
// Does the first entry match?
if (i == 0)
return;
// Was there no match?
if (i >= max_files)
i = max_files - 1;
// Move the other filenames down in the list...
memmove(abspath + 1, abspath, i * sizeof(abspath[0]));
memmove(relpath + 1, relpath, i * sizeof(relpath[0]));
// Put the new file at the top...
strlcpy(abspath[0], absolute, sizeof(abspath[0]));
fl_filename_shortened(relpath[0], sizeof(relpath[0]), absolute, 48);
// Update the menu items as needed...
for (i = 0; i < max_files; i++) {
Fluid.preferences.set(Fl_Preferences::Name("file%d", i), abspath[i]);
if (abspath[i][0]) {
if (i == 9) Fluid.history_item[i].flags = FL_MENU_DIVIDER;
else Fluid.history_item[i].flags = 0;
} else break;
}
for (; i < 10; i++) {
Fluid.preferences.set(Fl_Preferences::Name("file%d", i), "");
if (i) Fluid.history_item[i-1].flags |= FL_MENU_DIVIDER;
Fluid.history_item[i].hide();
}
Fluid.preferences.flush();
}
|