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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
//
// Fluid Project 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 <errno.h> // strerror(errno)
#include <stdlib.h> // free, malloc
#include <string.h> // strdup, strlen, strcmp
#include "Project.h"
#include "Fluid.h"
#include "io/String_Writer.h"
#include "nodes/Node.h"
#include "panels/settings_panel.h"
#include "panels/codeview_panel.h"
#include "tools/filename.h"
#include <FL/fl_ask.H>
#include <FL/fl_utf8.h>
// ---- project settings
/**
Initialize a new project.
*/
Project::Project()
: undo(*this),
tree(*this),
i18n(*this),
include_H_from_C(1),
use_FL_COMMAND(0),
utf8_in_src(0),
avoid_early_includes(0),
header_file_set(0),
code_file_set(0),
write_mergeback_data(0),
proj_filename(0),
header_file_name_(0),
code_file_name_(0),
include_guard_(0),
in_project_dir(0),
modflag(0),
modflag_c(0),
layout(default_layout_preset)
{
app_work_dir_[0] = '\0';
set_header_file_name(".h");
set_code_file_name(".cxx");
set_include_guard("");
}
/**
Clear all project resources.
*/
Project::~Project() {
if (header_file_name_) free(header_file_name_);
if (code_file_name_) free(code_file_name_);
if (include_guard_) free(include_guard_);
if (proj_filename) free((void*)proj_filename);
}
/**
Reset all project setting to create a new empty project.
*/
void Project::reset() {
::delete_all();
i18n.reset();
include_H_from_C = 1;
use_FL_COMMAND = 0;
utf8_in_src = 0;
avoid_early_includes = 0;
header_file_set = 0;
code_file_set = 0;
set_header_file_name(".h");
set_code_file_name(".cxx");
set_include_guard("");
write_mergeback_data = 0;
}
/**
Tell the project and i18n tab of the settings dialog to refresh themselves.
*/
void Project::update_settings_dialog() {
if (settings_window) {
w_settings_project_tab->do_callback(w_settings_project_tab, LOAD);
w_settings_i18n_tab->do_callback(w_settings_i18n_tab, LOAD);
}
}
// Setters for string members
void Project::set_header_file_name(const char *name) {
if (header_file_name_) free(header_file_name_);
header_file_name_ = name ? strdup(name) : 0;
}
void Project::set_code_file_name(const char *name) {
if (code_file_name_) free(code_file_name_);
code_file_name_ = name ? strdup(name) : 0;
}
void Project::set_include_guard(const char *guard) {
if (include_guard_) free(include_guard_);
include_guard_ = guard ? strdup(guard) : 0;
}
/**
Get the absolute path of the project file, for example `/Users/matt/dev/`.
\param[out] buf buffer to store result (ends with '/')
\param[in] bufsize size of buffer
*/
void Project::projectfile_path(char *buf, int bufsize) const {
buf[0] = '\0';
if (!proj_filename) return;
char path[FL_PATH_MAX];
fl_filename_path(path, FL_PATH_MAX, proj_filename);
char abs_path[FL_PATH_MAX];
fl_filename_absolute(abs_path, FL_PATH_MAX, path, Fluid.launch_path());
fld_end_with_slash(buf, bufsize, abs_path);
}
/**
Get the project file name including extension, for example `test.fl`.
\return the file name without path
*/
const char *Project::projectfile_name() const {
if (!proj_filename) return 0;
return fl_filename_name(proj_filename);
}
/**
Get the absolute path of the generated C++ code file, for example `/Users/matt/dev/src/`.
\param[out] buf buffer to store result (ends with '/')
\param[in] bufsize size of buffer
*/
void Project::codefile_path(char *buf, int bufsize) const {
buf[0] = '\0';
char path[FL_PATH_MAX];
fl_filename_path(path, FL_PATH_MAX, code_file_name_);
char abs_path[FL_PATH_MAX];
if (Fluid.batch_mode) {
fl_filename_absolute(abs_path, FL_PATH_MAX, path, Fluid.launch_path());
} else {
char proj_path[FL_PATH_MAX];
projectfile_path(proj_path, FL_PATH_MAX);
fl_filename_absolute(abs_path, FL_PATH_MAX, path, proj_path);
}
fld_end_with_slash(buf, bufsize, abs_path);
}
/**
Get the generated C++ code file name including extension, for example `test.cxx`.
\param[out] buf buffer to store result
\param[in] bufsize size of buffer
*/
void Project::codefile_name(char *buf, int bufsize) const {
buf[0] = '\0';
const char *name = fl_filename_name(code_file_name_);
if (!name || !*name) {
// No name specified, use project filename with .cxx extension
if (!proj_filename) return;
strlcpy(buf, fl_filename_name(proj_filename), bufsize);
fl_filename_setext(buf, bufsize, ".cxx");
} else if (name[0] == '.') {
// Only extension specified, use project filename with this extension
if (!proj_filename) return;
strlcpy(buf, fl_filename_name(proj_filename), bufsize);
fl_filename_setext(buf, bufsize, code_file_name_);
} else {
strlcpy(buf, name, bufsize);
}
}
/**
Get the absolute path of the generated C++ header file, for example `/Users/matt/dev/src/`.
\param[out] buf buffer to store result (ends with '/')
\param[in] bufsize size of buffer
*/
void Project::headerfile_path(char *buf, int bufsize) const {
buf[0] = '\0';
char path[FL_PATH_MAX];
fl_filename_path(path, FL_PATH_MAX, header_file_name_);
char abs_path[FL_PATH_MAX];
if (Fluid.batch_mode) {
fl_filename_absolute(abs_path, FL_PATH_MAX, path, Fluid.launch_path());
} else {
char proj_path[FL_PATH_MAX];
projectfile_path(proj_path, FL_PATH_MAX);
fl_filename_absolute(abs_path, FL_PATH_MAX, path, proj_path);
}
fld_end_with_slash(buf, bufsize, abs_path);
}
/**
Get the generated C++ header file name including extension, for example `test.h`.
\param[out] buf buffer to store result
\param[in] bufsize size of buffer
*/
void Project::headerfile_name(char *buf, int bufsize) const {
buf[0] = '\0';
const char *name = fl_filename_name(header_file_name_);
if (!name || !*name) {
// No name specified, use project filename with .h extension
if (!proj_filename) return;
strlcpy(buf, fl_filename_name(proj_filename), bufsize);
fl_filename_setext(buf, bufsize, ".h");
} else if (name[0] == '.') {
// Only extension specified, use project filename with this extension
if (!proj_filename) return;
strlcpy(buf, fl_filename_name(proj_filename), bufsize);
fl_filename_setext(buf, bufsize, header_file_name_);
} else {
strlcpy(buf, name, bufsize);
}
}
/**
Get the absolute path of the generated i18n strings file, for example `/Users/matt/dev/`.
\param[out] buf buffer to store result (ends with '/')
\param[in] bufsize size of buffer
*/
void Project::stringsfile_path(char *buf, int bufsize) const {
if (Fluid.batch_mode) {
strlcpy(buf, Fluid.launch_path(), bufsize);
} else {
projectfile_path(buf, bufsize);
}
}
/**
Get the generated i18n text file name including extension, for example `test.po`.
\param[out] buf buffer to store result
\param[in] bufsize size of buffer
*/
void Project::stringsfile_name(char *buf, int bufsize) const {
buf[0] = '\0';
if (!proj_filename) return;
strlcpy(buf, fl_filename_name(proj_filename), bufsize);
switch (i18n.type) {
default:
fl_filename_setext(buf, bufsize, ".txt");
break;
case FLD_I18N_TYPE_GNU:
fl_filename_setext(buf, bufsize, ".po");
break;
case FLD_I18N_TYPE_POSIX:
fl_filename_setext(buf, bufsize, ".msg");
break;
}
}
/**
Get the name of the project file without the filename extension.
\param[out] buf buffer to store result
\param[in] bufsize size of buffer
*/
void Project::basename(char *buf, int bufsize) const {
buf[0] = '\0';
if (!proj_filename) return;
strlcpy(buf, fl_filename_name(proj_filename), bufsize);
fl_filename_setext(buf, bufsize, "");
}
/**
Change the current working directory to the .fl project directory.
Every call to enter_project_dir() must have a corresponding leave_project_dir()
call. Enter and leave calls can be nested.
The first call to enter_project_dir() remembers the original directory, usually
the launch directory of the application. Nested calls will increment a nesting
counter. When the nesting counter is back to 0, leave_project_dir() will return
to the original directory.
The global variable 'filename' must be set to the current project file with
absolute or relative path information.
\see leave_project_dir(), pwd, in_project_dir
*/
void Project::enter_project_dir() {
if (in_project_dir < 0) {
fprintf(stderr, "** Fluid internal error: enter_project_dir() calls unmatched\n");
return;
}
in_project_dir++;
// check if we are already in the project dir and do nothing if so
if (in_project_dir > 1) return;
// check if there is an active project, and do nothing if there is none
if (!proj_filename || !*proj_filename) {
fprintf(stderr, "** Fluid internal error: enter_project_dir() no filename set\n");
return;
}
// store the current working directory for later
fl_getcwd(app_work_dir_, FL_PATH_MAX);
// set the current directory to the path of our .fl file
char abs_filename[FL_PATH_MAX];
fl_filename_absolute(abs_filename, FL_PATH_MAX, proj_filename);
char project_path[FL_PATH_MAX];
fl_filename_path(project_path, FL_PATH_MAX, abs_filename);
if (fl_chdir(project_path) == -1) {
fprintf(stderr, "** Fluid internal error: enter_project_dir() can't chdir to %s: %s\n",
project_path, strerror(errno));
return;
}
}
/**
Change the current working directory to the previous directory.
\see enter_project_dir(), pwd, in_project_dir
*/
void Project::leave_project_dir() {
if (in_project_dir == 0) {
fprintf(stderr, "** Fluid internal error: leave_project_dir() calls unmatched\n");
return;
}
in_project_dir--;
// still nested, stay in the project directory
if (in_project_dir > 0) return;
// no longer nested, return to the original, usually the application working directory
if (fl_chdir(app_work_dir_) < 0) {
fprintf(stderr, "** Fluid internal error: leave_project_dir() can't chdir back to %s : %s\n",
app_work_dir_, strerror(errno));
}
}
/**
Clear the project filename.
*/
void Project::clear_filename() {
set_filename(0);
}
/**
Set the filename of the current .fl design.
\param[in] c the new absolute filename and path (may be NULL to clear)
*/
void Project::set_filename(const char *c) {
if (proj_filename) free((void *)proj_filename);
proj_filename = (c && *c) ? strdup(c) : 0;
if (proj_filename && !Fluid.batch_mode)
Fluid.history.update(proj_filename);
set_modflag(modflag);
}
/**
Write the strings that are used in i18n.
*/
void Project::write_strings() {
Fluid.flush_text_widgets();
if (!proj_filename) {
Fluid.save_project_file(0);
if (!proj_filename) return;
}
char path[FL_PATH_MAX];
char name[FL_PATH_MAX];
stringsfile_path(path, FL_PATH_MAX);
stringsfile_name(name, FL_PATH_MAX);
char filename[FL_PATH_MAX];
snprintf(filename, FL_PATH_MAX, "%s%s", path, name);
int x = ::write_strings(*this, filename);
if (Fluid.batch_mode) {
if (x) {
fprintf(stderr, "%s : %s\n", filename, strerror(errno));
exit(1);
}
} else {
if (x) {
fl_message("Can't write %s: %s", filename, strerror(errno));
} else if (completion_button->value()) {
fl_message("Wrote %s", name);
}
}
}
/**
Set the "modified" flag and update the title of the main window.
The first argument sets the modification state of the current design against
the corresponding .fl design file. Any change to the widget tree will mark
the design 'modified'. Saving the design will mark it clean.
The second argument is optional and set the modification state of the current
design against the source code and header file. Any change to the tree,
including saving the tree, will mark the code 'outdated'. Generating source
code and header files will clear this flag until the next modification.
\param[in] mf 0 to clear the modflag, 1 to mark the design "modified", -1 to
ignore this parameter
\param[in] mfc default -1 to let \c mf control \c modflag_c, 0 to mark the
code files current, 1 to mark it out of date. -2 to ignore changes to mf.
*/
void Project::set_modflag(int mf, int mfc) {
const char *code_ext = 0;
char new_title[FL_PATH_MAX];
// Update the modflag_c to the worst possible condition. We could be a bit
// more graceful and compare modification times of the files, but C++ has
// no API for that until C++17.
if (mf != -1) {
modflag = mf;
if (mfc == -1 && mf == 1)
mfc = mf;
}
if (mfc >= 0) {
modflag_c = mfc;
}
if (Fluid.main_window) {
const char *base;
if (!proj_filename) {
base = "Untitled.fl";
} else {
base = fl_filename_name(proj_filename);
}
code_ext = fl_filename_ext(code_file_name_);
char mod_star = modflag ? '*' : ' ';
char mod_c_star = modflag_c ? '*' : ' ';
snprintf(new_title, sizeof(new_title), "%s%c %s%c",
base, mod_star, code_ext, mod_c_star);
const char *old_title = Fluid.main_window->label();
// only update the title if it actually changed
if (!old_title || strcmp(old_title, new_title))
Fluid.main_window->copy_label(new_title);
}
// if the UI was modified in any way, update the Code View panel
if (codeview_panel && codeview_panel->visible() && cv_autorefresh->value())
codeview_defer_update();
}
|