Skip to content

Commit

Permalink
FLUID: Introducing namespaces, reducing globals
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthiasWM committed Nov 12, 2024
1 parent a5c7ad7 commit 7419c98
Show file tree
Hide file tree
Showing 20 changed files with 372 additions and 329 deletions.
26 changes: 13 additions & 13 deletions fluid/ExternalCodeEditor_UNIX.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ ExternalCodeEditor::ExternalCodeEditor() {
This also closes the external editor.
*/
ExternalCodeEditor::~ExternalCodeEditor() {
if ( G_debug )
if ( Fluid.args.debug )
printf("ExternalCodeEditor() DTOR CALLED (this=%p, pid=%ld)\n",
(void*)this, (long)pid_);
close_editor(); // close editor, delete tmp file
Expand Down Expand Up @@ -104,7 +104,7 @@ int ExternalCodeEditor::is_editing() {
Wait for editor to close
*/
void ExternalCodeEditor::close_editor() {
if ( G_debug ) printf("close_editor() called: pid=%ld\n", long(pid_));
if ( Fluid.args.debug ) printf("close_editor() called: pid=%ld\n", long(pid_));
// Wait until editor is closed + reaped
while ( is_editing() ) {
switch ( reap_editor() ) {
Expand Down Expand Up @@ -140,7 +140,7 @@ void ExternalCodeEditor::close_editor() {
The dtor calls this to ensure no editors remain running when fluid exits.
*/
void ExternalCodeEditor::kill_editor() {
if ( G_debug ) printf("kill_editor() called: pid=%ld\n", (long)pid_);
if ( Fluid.args.debug ) printf("kill_editor() called: pid=%ld\n", (long)pid_);
if ( !is_editing() ) return; // editor not running? return..
kill(pid_, SIGTERM); // kill editor
int wcount = 0;
Expand All @@ -163,7 +163,7 @@ void ExternalCodeEditor::kill_editor() {
}
continue;
case 1: // process reaped (reap_editor() sets pid_ to -1)
if ( G_debug )
if ( Fluid.args.debug )
printf("*** REAPED KILLED EXTERNAL EDITOR: PID %ld\n", (long)pid_reaped);
break;
}
Expand Down Expand Up @@ -238,7 +238,7 @@ int ExternalCodeEditor::remove_tmpfile() {
if ( !tmpfile ) return 0;
// Filename set? remove (if exists) and zero filename/mtime/size
if ( is_file(tmpfile) ) {
if ( G_debug ) printf("Removing tmpfile '%s'\n", tmpfile);
if ( Fluid.args.debug ) printf("Removing tmpfile '%s'\n", tmpfile);
if ( remove(tmpfile) < 0 ) {
fl_alert("WARNING: Can't remove() '%s': %s", tmpfile, strerror(errno));
return -1;
Expand Down Expand Up @@ -267,7 +267,7 @@ const char* ExternalCodeEditor::tmpdir_name() {
void ExternalCodeEditor::tmpdir_clear() {
const char *tmpdir = tmpdir_name();
if ( is_dir(tmpdir) ) {
if ( G_debug ) printf("Removing tmpdir '%s'\n", tmpdir);
if ( Fluid.args.debug ) printf("Removing tmpdir '%s'\n", tmpdir);
if ( rmdir(tmpdir) < 0 ) {
fl_alert("WARNING: Can't rmdir() '%s': %s", tmpdir, strerror(errno));
}
Expand Down Expand Up @@ -382,7 +382,7 @@ void ExternalCodeEditor::open_alert_pipe() {
*/
int ExternalCodeEditor::start_editor(const char *editor_cmd,
const char *filename) {
if ( G_debug ) printf("start_editor() cmd='%s', filename='%s'\n",
if ( Fluid.args.debug ) printf("start_editor() cmd='%s', filename='%s'\n",
editor_cmd, filename);
char cmd[1024];
snprintf(cmd, sizeof(cmd), "%s %s", editor_cmd, filename);
Expand Down Expand Up @@ -415,7 +415,7 @@ int ExternalCodeEditor::start_editor(const char *editor_cmd,
default: // parent
if ( L_editors_open++ == 0 ) // first editor? start timers
{ start_update_timer(); }
if ( G_debug )
if ( Fluid.args.debug )
printf("--- EDITOR STARTED: pid_=%ld #open=%d\n", (long)pid_, L_editors_open);
break;
}
Expand Down Expand Up @@ -452,7 +452,7 @@ int ExternalCodeEditor::reap_editor(pid_t *pid_reaped) {
{ stop_update_timer(); }
break;
}
if ( G_debug )
if ( Fluid.args.debug )
printf("*** EDITOR REAPED: pid=%ld #open=%d\n", long(wpid), L_editors_open);
return 1;
}
Expand Down Expand Up @@ -491,7 +491,7 @@ int ExternalCodeEditor::open_editor(const char *editor_cmd,
filename(), (long)pid_);
return 0;
case 1: // process reaped, wpid is pid reaped
if ( G_debug )
if ( Fluid.args.debug )
printf("*** REAPED EXTERNAL EDITOR: PID %ld\n", (long)wpid);
break; // fall thru to open new editor instance
}
Expand All @@ -511,7 +511,7 @@ int ExternalCodeEditor::open_editor(const char *editor_cmd,
file_mtime_ = sbuf.st_mtime;
file_size_ = sbuf.st_size;
if ( start_editor(editor_cmd, filename()) < 0 ) { // open file in external editor
if ( G_debug ) printf("Editor failed to start\n");
if ( Fluid.args.debug ) printf("Editor failed to start\n");
return -1; // errors were shown in dialog
}
return 0;
Expand All @@ -522,7 +522,7 @@ int ExternalCodeEditor::open_editor(const char *editor_cmd,
*/
void ExternalCodeEditor::start_update_timer() {
if ( !L_update_timer_cb ) return;
if ( G_debug ) printf("--- TIMER: STARTING UPDATES\n");
if ( Fluid.args.debug ) printf("--- TIMER: STARTING UPDATES\n");
Fl::add_timeout(2.0, L_update_timer_cb);
}

Expand All @@ -531,7 +531,7 @@ void ExternalCodeEditor::start_update_timer() {
*/
void ExternalCodeEditor::stop_update_timer() {
if ( !L_update_timer_cb ) return;
if ( G_debug ) printf("--- TIMER: STOPPING UPDATES\n");
if ( Fluid.args.debug ) printf("--- TIMER: STOPPING UPDATES\n");
Fl::remove_timeout(L_update_timer_cb);
}

Expand Down
40 changes: 19 additions & 21 deletions fluid/ExternalCodeEditor_WIN32.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#include <stdio.h> // snprintf()
#include <stdlib.h>

extern int G_debug; // defined in fluid.cxx

// Static local data
static int L_editors_open = 0; // keep track of #editors open
static Fl_Timeout_Handler L_update_timer_cb = 0; // app's update timer callback
Expand Down Expand Up @@ -136,7 +134,7 @@ static BOOL CALLBACK terminate_app_enum(HWND hwnd, LPARAM lParam) {
GetWindowThreadProcessId(hwnd, &dwID);
if (dwID == (DWORD)lParam) {
PostMessage(hwnd, WM_CLOSE, 0, 0);
if ( G_debug )
if ( Fluid.args.debug )
printf("terminate_app_enum() sends WIN_CLOSE to hwnd=%p\n", (void*)hwnd);
}
return TRUE;
Expand All @@ -153,12 +151,12 @@ static int terminate_app(DWORD pid, DWORD msecTimeout) {
// Wait on handle. If it closes, great. If it times out, use TerminateProcess()
int ret = 0;
if ( WaitForSingleObject(hProc, msecTimeout) != WAIT_OBJECT_0 ) {
if ( G_debug ) {
if ( Fluid.args.debug ) {
printf("WARNING: sent WIN_CLOSE, but timeout after %ld msecs.."
"trying TerminateProcess\n", msecTimeout);
}
if ( TerminateProcess(hProc, 0) == 0 ) {
if ( G_debug ) {
if ( Fluid.args.debug ) {
printf("ERROR: TerminateProcess() for pid=%ld failed: %s\n",
long(pid), get_ms_errmsg());
}
Expand All @@ -175,7 +173,7 @@ static int terminate_app(DWORD pid, DWORD msecTimeout) {

// [Protected] Wait for editor to close
void ExternalCodeEditor::close_editor() {
if ( G_debug ) printf("close_editor() called: pid=%ld\n", long(pinfo_.dwProcessId));
if ( Fluid.args.debug ) printf("close_editor() called: pid=%ld\n", long(pinfo_.dwProcessId));
// Wait until editor is closed + reaped
while ( is_editing() ) {
switch ( reap_editor() ) {
Expand Down Expand Up @@ -209,7 +207,7 @@ void ExternalCodeEditor::close_editor() {
// The dtor calls this to ensure no editors remain running when fluid exits.
//
void ExternalCodeEditor::kill_editor() {
if ( G_debug )
if ( Fluid.args.debug )
printf("kill_editor() called: pid=%ld\n", (long)pinfo_.dwProcessId);
if ( !is_editing() ) return;
switch ( terminate_app(pinfo_.dwProcessId, 500) ) { // kill editor, wait up to 1/2 sec to die
Expand All @@ -221,7 +219,7 @@ void ExternalCodeEditor::kill_editor() {
case 0: { // success -- process reaped
DWORD pid = pinfo_.dwProcessId; // save pid
reap_cleanup(); // clears pinfo_
if ( G_debug )
if ( Fluid.args.debug )
printf("*** kill_editor() REAP pid=%ld #open=%ld\n",
long(pid), long(L_editors_open));
break;
Expand Down Expand Up @@ -314,18 +312,18 @@ int ExternalCodeEditor::handle_changes(const char **code, int force) {
//
int ExternalCodeEditor::remove_tmpfile() {
const char *tmpfile = filename();
if ( G_debug ) printf("remove_tmpfile() '%s'\n", tmpfile ? tmpfile : "(empty)");
if ( Fluid.args.debug ) printf("remove_tmpfile() '%s'\n", tmpfile ? tmpfile : "(empty)");
if ( !tmpfile ) return 0;
// Filename set? remove (if exists) and zero filename/mtime/size
if ( is_file(tmpfile) ) {
if ( G_debug ) printf("Removing tmpfile '%s'\n", tmpfile);
if ( Fluid.args.debug ) printf("Removing tmpfile '%s'\n", tmpfile);
utf8_to_wchar(tmpfile, wbuf);
if (DeleteFileW(wbuf) == 0) {
fl_alert("WARNING: Can't DeleteFile() '%s': %s", tmpfile, get_ms_errmsg());
return -1;
}
} else {
if ( G_debug ) printf("remove_tmpfile(): is_file(%s) failed\n", tmpfile);
if ( Fluid.args.debug ) printf("remove_tmpfile(): is_file(%s) failed\n", tmpfile);
}
set_filename(0);
memset(&file_mtime_, 0, sizeof(file_mtime_));
Expand All @@ -347,7 +345,7 @@ const char* ExternalCodeEditor::tmpdir_name() {
static char dirname[100];
_snprintf(dirname, sizeof(dirname), "%s.fluid-%ld",
tempdir, (long)GetCurrentProcessId());
if ( G_debug ) printf("tmpdir_name(): '%s'\n", dirname);
if ( Fluid.args.debug ) printf("tmpdir_name(): '%s'\n", dirname);
return dirname;
}

Expand All @@ -357,7 +355,7 @@ const char* ExternalCodeEditor::tmpdir_name() {
void ExternalCodeEditor::tmpdir_clear() {
const char *tmpdir = tmpdir_name();
if ( is_dir(tmpdir) ) {
if ( G_debug ) printf("Removing tmpdir '%s'\n", tmpdir);
if ( Fluid.args.debug ) printf("Removing tmpdir '%s'\n", tmpdir);
utf8_to_wchar(tmpdir, wbuf);
if ( RemoveDirectoryW(wbuf) == 0 ) {
fl_alert("WARNING: Can't RemoveDirectory() '%s': %s",
Expand Down Expand Up @@ -458,7 +456,7 @@ static int save_file(const char *filename,
//
int ExternalCodeEditor::start_editor(const char *editor_cmd,
const char *filename) {
if ( G_debug ) printf("start_editor() cmd='%s', filename='%s'\n",
if ( Fluid.args.debug ) printf("start_editor() cmd='%s', filename='%s'\n",
editor_cmd, filename);
// Startup info
STARTUPINFOW sinfo;
Expand Down Expand Up @@ -489,7 +487,7 @@ int ExternalCodeEditor::start_editor(const char *editor_cmd,
}
if ( L_editors_open++ == 0 ) // first editor? start timers
{ start_update_timer(); }
if ( G_debug )
if ( Fluid.args.debug )
printf("--- EDITOR STARTED: pid_=%ld #open=%d\n",
(long)pinfo_.dwProcessId, L_editors_open);
return 0;
Expand Down Expand Up @@ -533,7 +531,7 @@ int ExternalCodeEditor::reap_editor(DWORD *pid_reaped) {
DWORD wpid = pinfo_.dwProcessId; // save pid
reap_cleanup(); // clears pinfo_
if ( pid_reaped ) *pid_reaped = wpid; // return pid to caller
if ( G_debug ) printf("*** EDITOR REAPED: pid=%ld #open=%d\n",
if ( Fluid.args.debug ) printf("*** EDITOR REAPED: pid=%ld #open=%d\n",
long(wpid), L_editors_open);
return 1;
}
Expand Down Expand Up @@ -578,7 +576,7 @@ int ExternalCodeEditor::open_editor(const char *editor_cmd,
filename(), long(pinfo_.dwProcessId));
return 0;
case 1: // process reaped, wpid is pid reaped
if ( G_debug )
if ( Fluid.args.debug )
printf("*** REAPED EXTERNAL EDITOR: PID %ld\n", long(wpid));
break; // fall thru to open new editor instance
}
Expand All @@ -591,12 +589,12 @@ int ExternalCodeEditor::open_editor(const char *editor_cmd,
return -1; // errors were shown in dialog
}
if ( start_editor(editor_cmd, filename()) < 0 ) { // open file in external editor
if ( G_debug ) printf("Editor failed to start\n");
if ( Fluid.args.debug ) printf("Editor failed to start\n");
return -1; // errors were shown in dialog
}
// New editor opened -- start update timer (if not already)
if ( L_update_timer_cb && !Fl::has_timeout(L_update_timer_cb) ) {
if ( G_debug ) printf("--- Editor opened: STARTING UPDATE TIMER\n");
if ( Fluid.args.debug ) printf("--- Editor opened: STARTING UPDATE TIMER\n");
Fl::add_timeout(2.0, L_update_timer_cb);
}
return 0;
Expand All @@ -605,14 +603,14 @@ int ExternalCodeEditor::open_editor(const char *editor_cmd,
// [Public/Static] Start update timer
void ExternalCodeEditor::start_update_timer() {
if ( !L_update_timer_cb ) return;
if ( G_debug ) printf("--- TIMER: STARTING UPDATES\n");
if ( Fluid.args.debug ) printf("--- TIMER: STARTING UPDATES\n");
Fl::add_timeout(2.0, L_update_timer_cb);
}

// [Public/Static] Stop update timer
void ExternalCodeEditor::stop_update_timer() {
if ( !L_update_timer_cb ) return;
if ( G_debug ) printf("--- TIMER: STOPPING UPDATES\n");
if ( Fluid.args.debug ) printf("--- TIMER: STOPPING UPDATES\n");
Fl::remove_timeout(L_update_timer_cb);
}

Expand Down
4 changes: 2 additions & 2 deletions fluid/Fl_Function_Type.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1420,9 +1420,9 @@ void Fl_Data_Type::write_code1(Fd_Code_Writer& f) {
}
}
// if we are in interactive mode, we pop up a warning dialog
// giving the error: (batch_mode && !write_codeview) ???
// giving the error: (Fluid.batch_mode && !write_codeview) ???
if (message && !f.write_codeview) {
if (batch_mode)
if (Fluid.batch_mode)
fprintf(stderr, "FLUID ERROR: %s %s\n", message, fn);
else
fl_alert("%s\n%s\n", message, fn);
Expand Down
30 changes: 15 additions & 15 deletions fluid/Fl_Menu_Type.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -482,10 +482,10 @@ void Fl_Menu_Item_Type::write_item(Fd_Code_Writer& f) {
write_comment_inline_c(f, " ");
f.write_c(" {");
if (label() && label()[0])
switch (g_project.i18n_type) {
switch (f.project().i18n_type) {
case Fd_I18n_Type::GNU:
// we will call i18n when the menu is instantiated for the first time
f.write_c("%s(", g_project.i18n_gnu_static_function.c_str());
f.write_c("%s(", f.project().i18n_gnu_static_function.c_str());
f.write_cstring(label());
f.write_c(")");
break;
Expand All @@ -499,7 +499,7 @@ void Fl_Menu_Item_Type::write_item(Fd_Code_Writer& f) {
if (((Fl_Button*)o)->shortcut()) {
int s = ((Fl_Button*)o)->shortcut();
f.write_c(", ");
if (g_project.use_FL_COMMAND) {
if (f.project().use_FL_COMMAND) {
if (s & FL_CTRL) { f.write_c("FL_CONTROL|"); s &= ~FL_CTRL; }
if (s & FL_META) { f.write_c("FL_COMMAND|"); s &= ~FL_META; }
} else {
Expand Down Expand Up @@ -588,16 +588,16 @@ void Fl_Menu_Item_Type::write_code1(Fd_Code_Writer& f) {
f.write_c("%sml->labela = (char*)", f.indent());
image->write_inline(f);
f.write_c(";\n");
if (g_project.i18n_type==Fd_I18n_Type::NONE) {
if (f.project().i18n_type==Fd_I18n_Type::NONE) {
f.write_c("%sml->labelb = o->label();\n", f.indent());
} else if (g_project.i18n_type==Fd_I18n_Type::GNU) {
} else if (f.project().i18n_type==Fd_I18n_Type::GNU) {
f.write_c("%sml->labelb = %s(o->label());\n",
f.indent(), g_project.i18n_gnu_function.c_str());
} else if (g_project.i18n_type==Fd_I18n_Type::POSIX) {
f.indent(), f.project().i18n_gnu_function.c_str());
} else if (f.project().i18n_type==Fd_I18n_Type::POSIX) {
f.write_c("%sml->labelb = catgets(%s,%s,i+%d,o->label());\n",
f.indent(),
g_project.i18n_pos_file.empty() ? "_catalog" : g_project.i18n_pos_file.c_str(),
g_project.i18n_pos_set.c_str(), msgnum());
f.project().i18n_pos_file.empty() ? "_catalog" : f.project().i18n_pos_file.c_str(),
f.project().i18n_pos_set.c_str(), msgnum());
}
f.write_c("%sml->typea = FL_IMAGE_LABEL;\n", f.indent());
f.write_c("%sml->typeb = FL_NORMAL_LABEL;\n", f.indent());
Expand All @@ -606,21 +606,21 @@ void Fl_Menu_Item_Type::write_code1(Fd_Code_Writer& f) {
image->write_code(f, 0, "o");
}
}
if ( (g_project.i18n_type!=Fd_I18n_Type::NONE) && label() && label()[0]) {
if ( (f.project().i18n_type!=Fd_I18n_Type::NONE) && label() && label()[0]) {
Fl_Labeltype t = o->labeltype();
if (image) {
// label was already copied a few lines up
} else if ( t==FL_NORMAL_LABEL || t==FL_SHADOW_LABEL
|| t==FL_ENGRAVED_LABEL || t==FL_EMBOSSED_LABEL) {
start_menu_initialiser(f, menuItemInitialized, mname, i);
if (g_project.i18n_type==Fd_I18n_Type::GNU) {
if (f.project().i18n_type==Fd_I18n_Type::GNU) {
f.write_c("%so->label(%s(o->label()));\n",
f.indent(), g_project.i18n_gnu_function.c_str());
} else if (g_project.i18n_type==Fd_I18n_Type::POSIX) {
f.indent(), f.project().i18n_gnu_function.c_str());
} else if (f.project().i18n_type==Fd_I18n_Type::POSIX) {
f.write_c("%so->label(catgets(%s,%s,i+%d,o->label()));\n",
f.indent(),
g_project.i18n_pos_file.empty() ? "_catalog" : g_project.i18n_pos_file.c_str(),
g_project.i18n_pos_set.c_str(), msgnum());
f.project().i18n_pos_file.empty() ? "_catalog" : f.project().i18n_pos_file.c_str(),
f.project().i18n_pos_set.c_str(), msgnum());
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions fluid/Fl_Type.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ static void delete_children(Fl_Type *p) {

/** Delete all nodes in the Types tree and reset project settings, or delete selected nodes.
Also calls the browser to refresh.
\note Please refactor this into two separate methods of Fluid_Project.
\note Please refactor this into two separate methods of FLUID::Project.
\param[in] selected_only if set, delete only the selected widgets and
don't reset the project.
*/
Expand Down Expand Up @@ -908,7 +908,7 @@ void Fl_Type::write(Fd_Project_Writer &f) {

void Fl_Type::write_properties(Fd_Project_Writer &f) {
// repeat this for each attribute:
if (g_project.write_mergeback_data && uid_) {
if (f.project().write_mergeback_data && uid_) {
f.write_word("uid");
f.write_string("%04x", uid_);
}
Expand Down
Loading

0 comments on commit 7419c98

Please sign in to comment.