-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfluid.cxx
2290 lines (1965 loc) · 69.8 KB
/
fluid.cxx
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// FLUID main entry for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2023 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 "fluid.h"
#include "Fl_Type.h"
#include "Fl_Function_Type.h"
#include "Fl_Group_Type.h"
#include "Fl_Window_Type.h"
#include "widget_browser.h"
#include "shell_command.h"
#include "factory.h"
#include "pixmaps.h"
#include "undo.h"
#include "file.h"
#include "code.h"
#include "mergeback.h"
#include "alignment_panel.h"
#include "function_panel.h"
#include "sourceview_panel.h"
#include "template_panel.h"
#include "about_panel.h"
#include <FL/Fl.H>
#ifdef __APPLE__
#include <FL/platform.H> // for fl_open_callback
#endif
#include <FL/Fl_Help_Dialog.H>
#include <FL/Fl_Menu_Bar.H>
#include <FL/Fl_PNG_Image.H>
#include <FL/Fl_Native_File_Chooser.H>
#include <FL/Fl_Printer.H>
#include <FL/fl_string_functions.h>
#include <locale.h> // setlocale()..
#include "../src/flstring.h"
extern "C"
{
#if defined(HAVE_LIBPNG) && defined(HAVE_LIBZ)
# include <zlib.h>
# ifdef HAVE_PNG_H
# include <png.h>
# else
# include <libpng/png.h>
# endif // HAVE_PNG_H
#endif // HAVE_LIBPNG && HAVE_LIBZ
}
/// \defgroup globals Fluid Global Variables, Functions and Callbacks
/// \{
//
// Globals..
//
/// Fluid-wide help dialog.
static Fl_Help_Dialog *help_dialog = 0;
Fl_Menu_Bar *main_menubar = NULL;
Fl_Window *main_window;
/// Fluid application preferences, always accessible, will be flushed when app closes.
Fl_Preferences fluid_prefs(Fl_Preferences::USER_L, "fltk.org", "fluid");
/// Show guides in the design window when positioning widgets, saved in app preferences.
int show_guides = 1;
/// Show areas of restricted use in overlay plane.
/// Restricted areas are widget that overlap each other, widgets that are outside
/// of their parent's bounds (except children of Scroll groups), and areas
/// within an Fl_Tile that are not covered by children.
int show_restricted = 1;
/// Show a ghosted outline for groups that have very little contrast.
/// This makes groups with NO_BOX or FLAT_BOX better editable.
int show_ghosted_outline = 1;
/// Show widget comments in the browser, saved in app preferences.
int show_comments = 1;
/// Use external editor for editing Fl_Code_Type, saved in app preferences.
int G_use_external_editor = 0;
/// Debugging help for external Fl_Code_Type editor.
int G_debug = 0;
/// Run this command to load an Fl_Code_Type into an external editor, save in app preferences.
char G_external_editor_command[512];
/// If set, if the `current` node is a group, and a new group is added, it will
/// be added as sibling to the first group instead of inside the group.
/// \todo Needs to be verified.
int force_parent = 0;
/// This is set to create different labels when creating new widgets.
/// \todo Details unclear.
int reading_file = 0;
// File history info...
/// Stores the absolute filename of the last 10 design files, saved in app preferences.
char absolute_history[10][FL_PATH_MAX];
/// This list of filenames is computed from \c absolute_history and displayed in the main menu.
char relative_history[10][FL_PATH_MAX];
/// Menuitem to save a .fl design file, will be deactivated if the design is unchanged.
Fl_Menu_Item *save_item = NULL;
/// First Menuitem that shows the .fl design file history.
Fl_Menu_Item *history_item = NULL;
/// Menuitem to show or hide the widget bin, label will change if bin is visible.
Fl_Menu_Item *widgetbin_item = NULL;
/// Menuitem to show or hide the source view, label will change if view is visible.
Fl_Menu_Item *sourceview_item = NULL;
/// Menuitem to show or hide the editing overlay, label will change if overlay visibility changes.
Fl_Menu_Item *overlay_item = NULL;
/// Menuitem to show or hide the editing guides, label will change if overlay visibility changes.
Fl_Menu_Item *guides_item = NULL;
/// Menuitem to show or hide the restricted area overlys, label will change if overlay visibility changes.
Fl_Menu_Item *restricted_item = NULL;
////////////////////////////////////////////////////////////////
/// Filename of the current .fl design file
static const char *filename = NULL;
/// Set if the current design has been modified compared to the associated .fl design file.
int modflag = 0;
/// Set if the code files are older than the current design.
int modflag_c = 0;
/// Application work directory, stored here when temporarily changing to the source code directory.
/// \see goto_source_dir()
static Fl_String app_work_dir;
/// Used as a counter to set the .fl project dir as the current directory.
/// \see enter_project_dir(), leave_project_dir()
static char in_project_dir = 0;
/// Set, if Fluid was started with the command line argument -u
int update_file = 0; // fluid -u
/// Set, if Fluid was started with the command line argument -c
int compile_file = 0; // fluid -c
/// Set, if Fluid was started with the command line argument -cs
int compile_strings = 0; // fluid -cs
/// Set, if Fluid runs in batch mode, and no user interface is activated.
int batch_mode = 0; // if set (-c, -u) don't open display
/// command line arguments override settings in the project file
Fl_String g_code_filename_arg;
Fl_String g_header_filename_arg;
Fl_String g_launch_path;
Fl_String tmpdir_path;
bool tmpdir_create_called = false;
/** \var int Fluid_Project::header_file_set
If set, command line overrides header file name in .fl file.
*/
/** \var int Fluid_Project::code_file_set
If set, command line overrides source code file name in .fl file.
*/
/** \var int Fluid_Project::header_file_name
Hold the default extension for header files, or the entire filename if set via command line.
*/
/** \var int Fluid_Project::code_file_name
Hold the default extension for source code files, or the entire filename if set via command line.
*/
/** \var int Fluid_Project::i18n_type
Saved in the .fl design file.
\todo document me
*/
/** \var int Fluid_Project::i18n_gnu_include
Include file for GNU i18n, writes an #include statement into the
source file.
This is usually `<libintl.h>` or `"gettext.h"` for GNU gettext.
Fluid accepts filenames in quotes or in \< and \>. If neither is found,
double quotes are added.
If this value is empty, no include statement will be generated.
Saved in the .fl design file if GNU i18n is selected.
*/
/** \var int Fluid_Project::i18n_pos_include
Include file for Posix i18n, write a #include statement into the
source file.
This is usually `<nl_types.h>` for Posix catgets.
Fluid accepts filenames in quotes or in \< and \>. If neither is found,
double quotes are added.
If this value is empty, no include statement will be generated.
Saved in the .fl design file.
*/
/** \var int Fluid_Project::i18n_gnu_conditional
Saved in the .fl design file.
\todo document me
*/
/** \var int Fluid_Project::i18n_pos_conditional
Saved in the .fl design file.
\todo document me
*/
/** \var int Fluid_Project::i18n_gnu_function
For the gettext/intl.h options, this is the function that translates text
at runtime.
This is usually "gettext" or "_".
This should not be empty.
Saved in the .fl design file.
*/
/** \var int Fluid_Project::i18n_gnu_static_function
For the gettext/intl.h options, this is the function that marks the
translation of text at initialisation time.
This is usually "gettext_noop" or "N_".
This should not be empty.
Fluid will translate static text (usually in menu items) later when used
for the first time.
Saved in the .fl design file.
*/
/** \var int Fluid_Project::i18n_pos_file
Saved in the .fl design file.
\todo document me
*/
/** \var int Fluid_Project::i18n_pos_set
Saved in the .fl design file.
\todo document me
*/
/** \var int Fluid_Project::basename
\todo document me
*/
/// Offset in pixels when adding widgets from an .fl file.
int pasteoffset = 0;
/// Paste offset incrementing at every paste command.
static int ipasteoffset = 0;
// ---- project settings
Fluid_Project g_project;
Fluid_Project::Fluid_Project() :
i18n_type(0),
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),
header_file_name(".h"),
code_file_name(".cxx")
{ }
Fluid_Project::~Fluid_Project() {
}
void Fluid_Project::reset() {
::delete_all();
i18n_type = 0;
i18n_gnu_include = "<libintl.h>";
i18n_gnu_conditional = "";
i18n_gnu_function = "gettext";
i18n_gnu_static_function = "gettext_noop";
i18n_pos_include = "<nl_types.h>";
i18n_pos_conditional = "";
i18n_pos_file = "";
i18n_pos_set = "1";
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;
header_file_name = ".h";
code_file_name = ".cxx";
write_mergeback_data = 0;
}
void Fluid_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);
}
}
// make sure that a path name ends with a forward slash
static Fl_String end_with_slash(const Fl_String &str) {
char last = str[str.size()-1];
if (last !='/' && last != '\\')
return str + "/";
else
return str;
}
/** Generate a path to a directory for temporary data storage.
The path is stored in g_tmpdir.
*/
static void create_tmpdir() {
if (tmpdir_create_called)
return;
tmpdir_create_called = true;
char buf[128];
#if _WIN32
// The usual temp file locations on Windows are
// %system%\Windows\Temp
// %userprofiles%\AppData\Local
// usually resolving into
// C:/Windows/Temp/
// C:\Users\<username>\AppData\Local\Temp
fl_snprintf(buf, sizeof(buf)-1, "fluid-%d/", (long)GetCurrentProcessId());
Fl_String name = buf;
wchar_t tempdirW[FL_PATH_MAX+1];
char tempdir[FL_PATH_MAX+1];
unsigned len = GetTempPathW(FL_PATH_MAX, tempdirW);
if (len == 0) {
strcpy(tempdir, "c:/windows/temp/");
} else {
unsigned wn = fl_utf8fromwc(tempdir, FL_PATH_MAX, tempdirW, len);
tempdir[wn] = 0;
}
Fl_String path = tempdir;
end_with_slash(path);
path += name;
fl_make_path(path.c_str());
if (fl_access(path.c_str(), 6) == 0) tmpdir_path = path;
#else
fl_snprintf(buf, sizeof(buf)-1, "fluid-%d/", getpid());
Fl_String name = buf;
Fl_String path = fl_getenv("TMPDIR");
if (!path.empty()) {
end_with_slash(path);
path += name;
fl_make_path(path.c_str());
if (fl_access(path.c_str(), 6) == 0) tmpdir_path = path;
}
if (tmpdir_path.empty()) {
path = Fl_String("/tmp/") + name;
fl_make_path(path.c_str());
if (fl_access(path.c_str(), 6) == 0) tmpdir_path = path;
}
#endif
if (tmpdir_path.empty()) {
char pbuf[FL_PATH_MAX+1];
fluid_prefs.get_userdata_path(pbuf, FL_PATH_MAX);
path = Fl_String(pbuf);
end_with_slash(path);
path += name;
fl_make_path(path.c_str());
if (fl_access(path.c_str(), 6) == 0) tmpdir_path = path;
}
if (tmpdir_path.empty())
fl_alert("Can't create directory for temporary data storage.");
}
/** Delete the temporary directory that was created in set_tmpdir. */
static void delete_tmpdir() {
// was a temporary directory created
if (!tmpdir_create_called)
return;
if (tmpdir_path.empty())
return;
// first delete all files that may still be left in the temp directory
struct dirent **de;
int n_de = fl_filename_list(tmpdir_path.c_str(), &de);
if (n_de >= 0) {
for (int i=0; i<n_de; i++) {
Fl_String path = tmpdir_path + de[i]->d_name;
fl_unlink(path.c_str());
}
fl_filename_free_list(&de, n_de);
}
// then delete the directory itself
if (fl_rmdir(tmpdir_path.c_str()) < 0) {
fl_alert("WARNING: Can't delete tmpdir '%s': %s", tmpdir_path.c_str(), strerror(errno));
}
}
/**
Return the path to a temporary directory for this instance of FLUID.
Fluid will do its best to clear and delete this directory when exiting.
\return the path to the temporary directory, ending in a '/', or and empty
string is no directory could be created.
*/
const Fl_String &get_tmpdir() {
if (!tmpdir_create_called)
create_tmpdir();
return tmpdir_path;
}
/**
Give the user the opportunity to save a project before clearing it.
If the project has unsaved changes, this function pops up a dialog, that
allows the user to save the project, continue without saving the project,
or to cancel the operation.
If the user chooses to save, and no filename was set, a file dialog allows
the user to pick a name and location, or to cancel the operation.
\return false if the user aborted the operation and the calling function
should abort as well
*/
bool confirm_project_clear() {
if (modflag == 0) return true;
switch (fl_choice("This project has unsaved changes. Do you want to save\n"
"the project file before proceeding?",
"Cancel", "Save", "Don't Save"))
{
case 0 : /* Cancel */
return false;
case 1 : /* Save */
save_cb(NULL, NULL);
if (modflag) return false; // user canceled the "Save As" dialog
}
return true;
}
// ----
extern Fl_Window *the_panel;
// make sure that a currently changed text widgets propagates its contents
void flush_text_widgets() {
if (Fl::focus() && (Fl::focus()->top_window() == the_panel)) {
Fl_Widget *old_focus = Fl::focus();
Fl::focus(NULL); // trigger callback of the widget that is losing focus
Fl::focus(old_focus);
}
}
// ----
/**
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 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 (!filename || !*filename) {
fprintf(stderr, "** Fluid internal error: enter_project_dir() no filename set\n");
return;
}
// store the current working directory for later
app_work_dir = fl_getcwd();
// set the current directory to the path of our .fl file
Fl_String project_path = fl_filename_path(fl_filename_absolute(filename));
if (fl_chdir(project_path.c_str()) == -1) {
fprintf(stderr, "** Fluid internal error: enter_project_dir() can't chdir to %s: %s\n",
project_path.c_str(), strerror(errno));
return;
}
//fprintf(stderr, "chdir from %s to %s\n", app_work_dir.c_str(), fl_getcwd().c_str());
}
/**
Change the current working directory to the previous directory.
\see enter_project_dir(), pwd, in_project_dir
*/
void 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.c_str()) < 0) {
fprintf(stderr, "** Fluid internal error: leave_project_dir() can't chdir back to %s : %s\n",
app_work_dir.c_str(), strerror(errno));
}
}
/**
Position the given window window based on entries in the app preferences.
Customisable by user; feature can be switched off.
The window is not shown or hidden by this function, but a value is returned
to indicate the state to the caller.
\param[in] w position this window
\param[in] prefsName name of the preferences item that stores the window settings
\param[in] Visible default value if window is hidden or shown
\param[in] X, Y, W, H default size and position if nothing is specified in the preferences
\return 1 if the caller should make the window visible, 0 if hidden.
*/
char position_window(Fl_Window *w, const char *prefsName, int Visible, int X, int Y, int W, int H) {
Fl_Preferences pos(fluid_prefs, prefsName);
if (prevpos_button->value()) {
pos.get("x", X, X);
pos.get("y", Y, Y);
if ( W!=0 ) {
pos.get("w", W, W);
pos.get("h", H, H);
w->resize( X, Y, W, H );
}
else
w->position( X, Y );
}
pos.get("visible", Visible, Visible);
return Visible;
}
/**
Save the position and visibility state of a window to the app preferences.
\param[in] w save this window data
\param[in] prefsName name of the preferences item that stores the window settings
*/
void save_position(Fl_Window *w, const char *prefsName) {
Fl_Preferences pos(fluid_prefs, prefsName);
pos.set("x", w->x());
pos.set("y", w->y());
pos.set("w", w->w());
pos.set("h", w->h());
pos.set("visible", (int)(w->shown() && w->visible()));
}
/**
Return the path and filename of a temporary file for cut or duplicated data.
\param[in] which 0 gets the cut/copy/paste buffer, 1 gets the duplication buffer
\return a pointer to a string in a static buffer
*/
static char* cutfname(int which = 0) {
static char name[2][FL_PATH_MAX];
static char beenhere = 0;
if (!beenhere) {
beenhere = 1;
fluid_prefs.getUserdataPath(name[0], sizeof(name[0]));
strlcat(name[0], "cut_buffer", sizeof(name[0]));
fluid_prefs.getUserdataPath(name[1], sizeof(name[1]));
strlcat(name[1], "dup_buffer", sizeof(name[1]));
}
return name[which];
}
/**
Timer to watch for external editor modifications.
If one or more external editors open, check if their files were modified.
If so: reload to ram, update size/mtime records, and change fluid's
'modified' state.
*/
static void external_editor_timer(void*) {
int editors_open = ExternalCodeEditor::editors_open();
if ( G_debug ) printf("--- TIMER --- External editors open=%d\n", editors_open);
if ( editors_open > 0 ) {
// Walk tree looking for files modified by external editors.
int modified = 0;
for (Fl_Type *p = Fl_Type::first; p; p = p->next) {
if ( p->is_a(ID_Code) ) {
Fl_Code_Type *code = (Fl_Code_Type*)p;
// Code changed by external editor?
if ( code->handle_editor_changes() ) { // updates ram, file size/mtime
modified++;
}
if ( code->is_editing() ) { // editor open?
code->reap_editor(); // Try to reap; maybe it recently closed
}
}
}
if ( modified ) set_modflag(1);
}
// Repeat timeout if editors still open
// The ExternalCodeEditor class handles start/stopping timer, we just
// repeat_timeout() if it's already on. NOTE: above code may have reaped
// only open editor, which would disable further timeouts. So *recheck*
// if editors still open, to ensure we don't accidentally re-enable them.
//
if ( ExternalCodeEditor::editors_open() ) {
Fl::repeat_timeout(2.0, external_editor_timer);
}
}
/**
Save the current design to the file given by \c filename.
If automatic, this overwrites an existing file. If interactive, if will
verify with the user.
\param[in] v if v is not NULL, or no filename is set, open a filechooser.
*/
void save_cb(Fl_Widget *, void *v) {
flush_text_widgets();
Fl_Native_File_Chooser fnfc;
const char *c = filename;
if (v || !c || !*c) {
fnfc.title("Save To:");
fnfc.type(Fl_Native_File_Chooser::BROWSE_SAVE_FILE);
fnfc.filter("FLUID Files\t*.f[ld]");
if (fnfc.show() != 0) return;
c = fnfc.filename();
if (!fl_access(c, 0)) {
Fl_String basename = fl_filename_name(Fl_String(c));
if (fl_choice("The file \"%s\" already exists.\n"
"Do you want to replace it?", "Cancel",
"Replace", NULL, basename.c_str()) == 0) return;
}
if (v != (void *)2) set_filename(c);
}
if (!write_file(c)) {
fl_alert("Error writing %s: %s", c, strerror(errno));
return;
}
if (v != (void *)2) {
set_modflag(0, 1);
undo_save = undo_current;
}
}
/**
Save a design template.
\todo We should document the concept of templates.
*/
void save_template_cb(Fl_Widget *, void *) {
// Setup the template panel...
if (!template_panel) make_template_panel();
template_clear();
template_browser->add("New Template");
template_load();
template_name->show();
template_name->value("");
template_instance->hide();
template_delete->show();
template_delete->deactivate();
template_submit->label("Save");
template_submit->deactivate();
template_panel->label("Save Template");
// Show the panel and wait for the user to do something...
template_panel->show();
while (template_panel->shown()) Fl::wait();
// Get the template name, return if it is empty...
const char *c = template_name->value();
if (!c || !*c) return;
// Convert template name to filename_with_underscores
char savename[FL_PATH_MAX], *saveptr;
strlcpy(savename, c, sizeof(savename));
for (saveptr = savename; *saveptr; saveptr ++) {
if (isspace(*saveptr)) *saveptr = '_';
}
// Find the templates directory...
char filename[FL_PATH_MAX];
fluid_prefs.getUserdataPath(filename, sizeof(filename));
strlcat(filename, "templates", sizeof(filename));
if (fl_access(filename, 0)) fl_make_path(filename);
strlcat(filename, "/", sizeof(filename));
strlcat(filename, savename, sizeof(filename));
char *ext = filename + strlen(filename);
if (ext >= (filename + sizeof(filename) - 5)) {
fl_alert("The template name \"%s\" is too long!", c);
return;
}
// Save the .fl file...
strcpy(ext, ".fl");
if (!fl_access(filename, 0)) {
if (fl_choice("The template \"%s\" already exists.\n"
"Do you want to replace it?", "Cancel",
"Replace", NULL, c) == 0) return;
}
if (!write_file(filename)) {
fl_alert("Error writing %s: %s", filename, strerror(errno));
return;
}
#if defined(HAVE_LIBPNG) && defined(HAVE_LIBZ)
// Get the screenshot, if any...
Fl_Type *t;
for (t = Fl_Type::first; t; t = t->next) {
// Find the first window...
if (t->is_a(ID_Window)) break;
}
if (!t) return;
// Grab a screenshot...
Fl_Window_Type *wt = (Fl_Window_Type *)t;
uchar *pixels;
int w, h;
if ((pixels = wt->read_image(w, h)) == NULL) return;
// Save to a PNG file...
strcpy(ext, ".png");
errno = 0;
if (fl_write_png(filename, pixels, w, h, 3) != 0) {
delete[] pixels;
fl_alert("Error writing %s: %s", filename, strerror(errno));
return;
}
# if 0 // The original PPM output code...
strcpy(ext, ".ppm");
fp = fl_fopen(filename, "wb");
fprintf(fp, "P6\n%d %d 255\n", w, h);
fwrite(pixels, w * h, 3, fp);
fclose(fp);
# endif // 0
delete[] pixels;
#endif // HAVE_LIBPNG && HAVE_LIBZ
}
/**
Reload the file set by \c filename, replacing the current design.
If the design was modified, a dialog will ask for confirmation.
*/
void revert_cb(Fl_Widget *,void *) {
if (modflag) {
if (!fl_choice("This user interface has been changed. Really revert?",
"Cancel", "Revert", NULL)) return;
}
undo_suspend();
if (!read_file(filename, 0)) {
undo_resume();
widget_browser->rebuild();
g_project.update_settings_dialog();
fl_message("Can't read %s: %s", filename, strerror(errno));
return;
}
widget_browser->rebuild();
undo_resume();
set_modflag(0, 0);
undo_clear();
g_project.update_settings_dialog();
}
/**
Exit Fluid; we hope you had a nice experience.
If the design was modified, a dialog will ask for confirmation.
*/
void exit_cb(Fl_Widget *,void *) {
if (shell_command_running()) {
fl_alert("Previous shell command still running!");
return;
}
flush_text_widgets();
// verify user intention
if (confirm_project_clear() == false)
return;
// Stop any external editor update timers
ExternalCodeEditor::stop_update_timer();
save_position(main_window,"main_window_pos");
if (widgetbin_panel) {
save_position(widgetbin_panel,"widgetbin_pos");
delete widgetbin_panel;
}
if (sourceview_panel) {
Fl_Preferences svp(fluid_prefs, "sourceview");
svp.set("autorefresh", sv_autorefresh->value());
svp.set("autoposition", sv_autoposition->value());
svp.set("tab", sv_tab->find(sv_tab->value()));
svp.set("code_choice", sv_code_choice);
save_position(sourceview_panel,"sourceview_pos");
delete sourceview_panel;
sourceview_panel = 0;
}
if (shell_run_window) {
save_position(shell_run_window,"shell_run_Window_pos");
}
if (about_panel)
delete about_panel;
if (help_dialog)
delete help_dialog;
if (g_shell_config)
g_shell_config->write(fluid_prefs, FD_STORE_USER);
g_layout_list.write(fluid_prefs, FD_STORE_USER);
undo_clear();
// Destroy tree
// Doing so causes dtors to automatically close all external editors
// and cleans up editor tmp files. Then remove fluid tmpdir /last/.
g_project.reset();
ExternalCodeEditor::tmpdir_clear();
delete_tmpdir();
exit(0);
}
/**
Clear the current project and create a new, empty one.
If the current project was modified, FLUID will give the user the opportunity
to save the old project first.
\return false if the operation was canceled
*/
bool new_project(bool user_must_confirm = true) {
// verify user intention
if ((user_must_confirm) && (confirm_project_clear() == false))
return false;
// clear the current project
g_project.reset();
set_filename(NULL);
set_modflag(0, 0);
widget_browser->rebuild();
g_project.update_settings_dialog();
// all is clear to continue
return true;
}
/**
Open the template browser and load a new file from templates.
If the current project was modified, FLUID will give the user the opportunity
to save the old project first.
\return false if the operation was canceled or failed otherwise
*/
bool new_project_from_template() {
// clear the current project first
if (new_project() == false)
return false;
// Setup the template panel...
if (!template_panel) make_template_panel();
template_clear();
template_browser->add("Blank");
template_load();
template_name->hide();
template_name->value("");
template_instance->show();
template_instance->deactivate();
template_instance->value("");
template_delete->show();
template_submit->label("New");
template_submit->deactivate();
template_panel->label("New");
//if ( template_browser->size() == 1 ) { // only one item?
template_browser->value(1); // select it
template_browser->do_callback();
//}
// Show the panel and wait for the user to do something...
template_panel->show();
while (template_panel->shown()) Fl::wait();
// See if the user chose anything...
int item = template_browser->value();
if (item < 1) return false;
// Load the template, if any...
const char *tname = (const char *)template_browser->data(item);
if (tname) {
// Grab the instance name...
const char *iname = template_instance->value();
if (iname && *iname) {
// Copy the template to a temp file, then read it in...
char line[1024], *ptr, *next;
FILE *infile, *outfile;
if ((infile = fl_fopen(tname, "rb")) == NULL) {
fl_alert("Error reading template file \"%s\":\n%s", tname,
strerror(errno));
set_modflag(0);
undo_clear();
return false;
}
if ((outfile = fl_fopen(cutfname(1), "wb")) == NULL) {
fl_alert("Error writing buffer file \"%s\":\n%s", cutfname(1),
strerror(errno));
fclose(infile);
set_modflag(0);
undo_clear();
return false;
}
while (fgets(line, sizeof(line), infile)) {
// Replace @INSTANCE@ with the instance name...
for (ptr = line; (next = strstr(ptr, "@INSTANCE@")) != NULL; ptr = next + 10) {
fwrite(ptr, next - ptr, 1, outfile);
fputs(iname, outfile);
}
fputs(ptr, outfile);
}
fclose(infile);
fclose(outfile);
undo_suspend();
read_file(cutfname(1), 0);
fl_unlink(cutfname(1));
undo_resume();
} else {
// No instance name, so read the template without replacements...
undo_suspend();
read_file(tname, 0);
undo_resume();
}
}
widget_browser->rebuild();
g_project.update_settings_dialog();
set_modflag(0);
undo_clear();