summaryrefslogtreecommitdiff
path: root/fluid/sourceview_panel.fl
blob: 37c38d2fb51dd469ca3a215d5ed99efb77fc0a0d (plain)
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
# data file for the Fltk User Interface Designer (fluid)
version 1.0400
header_name {.h}
code_name {.cxx}
snap {
  ver 1
  current_suite FLTK
  current_preset 1
}
comment {//
// Code dialogs 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
//
} {in_source in_header
}

decl {\#include "fluid.h"} {private local
}

decl {\#include "file.h"} {private local
}

decl {\#include "../src/flstring.h"} {selected private local
}

decl {\#include <FL/Fl_Tabs.H>} {private local
}

decl {\#include <FL/Fl_Button.H>} {private local
}

decl {char *sv_source_filename = NULL;} {private local
}

decl {char *sv_header_filename = NULL;} {private local
}

decl {char *sv_design_filename = NULL;} {private local
}

Function {update_sourceview_position()} {
  comment {Update the header and source code highlighting depending on the
currently selected object

The Source View system offers an immediate preview of the code
files that will be generated by FLUID. It also marks the code
generated for the last selected item in the header and the source
file.} open return_type void
} {
  code {if (!sourceview_panel || !sourceview_panel->visible())
    return;
  if (sv_autoposition->value()==0)
    return;
  if (sourceview_panel && sourceview_panel->visible() && Fl_Type::current) {
    int pos0, pos1;
    if (sv_source->visible_r()) {
      pos0 = Fl_Type::current->code_position;
      pos1 = Fl_Type::current->code_position_end;
      if (pos0>=0) {
        if (pos1<pos0)
          pos1 = pos0;
        sv_source->buffer()->highlight(pos0, pos1);
        int line = sv_source->buffer()->count_lines(0, pos0);
        sv_source->scroll(line, 0);
      }
    }
    if (sv_header->visible_r()) {
      pos0 = Fl_Type::current->header_position;
      pos1 = Fl_Type::current->header_position_end;
      if (pos0>=0) {
        if (pos1<pos0)
          pos1 = pos0;
        sv_header->buffer()->highlight(pos0, pos1);
        int line = sv_header->buffer()->count_lines(0, pos0);
        sv_header->scroll(line, 0);
      }
    }
  }} {}
}

Function {update_sourceview_position_cb(class Fl_Tabs*, void*)} {
  comment {Callback to update the sourceview position.} open return_type void
} {
  code {// make sure that the selected tab shows the current view
  update_sourceview_cb(0,0);
  // highlight the selected widget in the selected tab
  update_sourceview_position();} {}
}

Function {update_sourceview_cb(class Fl_Button*, void*)} {
  comment {Generate a header, source, strings, or design file in a temporary directory
and load those into the Code Viewer widgets.} open return_type void
} {
  code {if (!sourceview_panel || !sourceview_panel->visible())
    return;

  if (!sv_source_filename) {
    sv_source_filename = (char*)malloc(FL_PATH_MAX);
    fluid_prefs.getUserdataPath(sv_source_filename, FL_PATH_MAX);
    fl_strlcat(sv_source_filename, "source_view_tmp.cxx", FL_PATH_MAX);
  }
  if (!sv_header_filename) {
    sv_header_filename = (char*)malloc(FL_PATH_MAX);
    fluid_prefs.getUserdataPath(sv_header_filename, FL_PATH_MAX);
    fl_strlcat(sv_header_filename, "source_view_tmp.h", FL_PATH_MAX);
  }
  if (!sv_design_filename) {
    sv_design_filename = (char*)malloc(FL_PATH_MAX);
    fluid_prefs.getUserdataPath(sv_design_filename, FL_PATH_MAX);
    fl_strlcat(sv_design_filename, "source_view_tmp.fl", FL_PATH_MAX);
  }

  if (sv_project->visible_r()) {
    write_file(sv_design_filename);
    int top = sv_project->top_line();
    sv_project->buffer()->loadfile(sv_design_filename);
    sv_project->scroll(top, 0);
  } else if (sv_strings->visible_r()) {
    static const char *exts[] = { ".txt", ".po", ".msg" };
    char fn[FL_PATH_MAX];
    fluid_prefs.getUserdataPath(fn, FL_PATH_MAX);
    fl_filename_setext(fn, FL_PATH_MAX, exts[g_project.i18n_type]);
    write_strings(fn);
    int top = sv_strings->top_line();
    sv_strings->buffer()->loadfile(fn);
    sv_strings->scroll(top, 0);
  } else if (sv_source->visible_r() || sv_header->visible_r()) {
    g_project.basename = fl_filename_name(sv_source_filename);
    g_project.basename = fl_filename_setext(g_project.basename, "");
    Fl_String code_file_name_bak = g_project.code_file_name;
    g_project.code_file_name = sv_source_filename;
    Fl_String header_file_name_bak = g_project.header_file_name;
    g_project.header_file_name = sv_header_filename;

    // generate the code and load the files
    Fd_Code_Writer f;
    // generate files
    if (f.write_code(sv_source_filename, sv_header_filename, true))
    {
      // load file into source editor
      int pos = sv_source->top_line();
      sv_source->buffer()->loadfile(sv_source_filename);
      sv_source->scroll(pos, 0);
      // load file into header editor
      pos = sv_header->top_line();
      sv_header->buffer()->loadfile(sv_header_filename);
      sv_header->scroll(pos, 0);
      // update the source code highlighting
      update_sourceview_position();
    }

    g_project.code_file_name = code_file_name_bak;
    g_project.header_file_name = header_file_name_bak;
  }} {}
}

Function {update_sourceview_timer(void*)} {
  comment {This is called by the timer itself
} open return_type void
} {
  code {update_sourceview_cb(0,0);} {}
}

Function {sourceview_defer_update()} {open return_type void
} {
  code {// we will only update earliest 0.5 seconds after the last change, and only
    // if no other change was made, so dragging a widget will not generate any
    // CPU load
    Fl::remove_timeout(update_sourceview_timer, 0);
    Fl::add_timeout(0.5, update_sourceview_timer, 0);} {}
}

Function {sourceview_toggle_visibility()} {
  comment {Show or hide the source code preview.
The state is stored in the app preferences.
} open return_type void
} {
  code {if (!sourceview_panel) {
    make_sourceview();
    sourceview_panel->callback((Fl_Callback*)toggle_sourceview_cb);
    Fl_Preferences svp(fluid_prefs, "sourceview");
    int autorefresh;
    svp.get("autorefresh", autorefresh, 1);
    sv_autorefresh->value(autorefresh);
    int autoposition;
    svp.get("autoposition", autoposition, 1);
    sv_autoposition->value(autoposition);
    int tab;
    svp.get("tab", tab, 0);
    if (tab>=0 && tab<sv_tab->children()) sv_tab->value(sv_tab->child(tab));
    if (!position_window(sourceview_panel,"sourceview_pos", 0, 320, 120, 550, 500)) return;
  }

  if (sourceview_panel->visible()) {
    sourceview_panel->hide();
    sourceview_item->label("Show Source Code...");
  } else {
    sourceview_panel->show();
    sourceview_item->label("Hide Source Code...");
    update_sourceview_cb(0,0);
  }} {}
}

Function {make_sourceview()} {open
} {
  Fl_Window sourceview_panel {
    label {Code View}
    callback toggle_sourceview_cb open
    xywh {400 569 520 490} type Double align 80 resizable size_range {384 120 0 0} visible
  } {
    Fl_Tabs sv_tab {
      callback update_sourceview_position_cb open
      xywh {10 10 500 440} selection_color 4 labelcolor 7 resizable
    } {
      Fl_Group {} {
        label Source open
        xywh {10 35 500 415} labelsize 13 resizable
      } {
        Fl_Text_Editor sv_source {
          xywh {20 50 480 390} textfont 4 textsize 11 resizable
          code0 {\#include "CodeEditor.h"}
          code1 {o->linenumber_width(60);}
          code2 {o->linenumber_size(o->Fl_Text_Display::textsize());}
          class CodeViewer
        }
      }
      Fl_Group {} {
        label Header open
        xywh {10 35 500 415} labelsize 13 hide
      } {
        Fl_Text_Editor sv_header {
          xywh {20 50 480 390} textfont 4 textsize 11 resizable
          code0 {\#include "CodeEditor.h"}
          code1 {o->linenumber_width(60);}
          code2 {o->linenumber_size(o->Fl_Text_Display::textsize());}
          class CodeViewer
        }
      }
      Fl_Group {} {
        label Strings open
        xywh {10 35 500 415} labelsize 13 hide
      } {
        Fl_Text_Display sv_strings {
          xywh {20 50 480 390} textfont 4 textsize 11 resizable
          code1 {o->linenumber_width(60);}
          code2 {o->linenumber_size(o->Fl_Text_Display::textsize());}
          class TextViewer
        }
      }
      Fl_Group {} {
        label Project open
        xywh {10 35 500 415} labelsize 13 hide
      } {
        Fl_Text_Display sv_project {
          xywh {20 50 480 390} textfont 4 textsize 11 resizable
          code1 {o->linenumber_width(60);}
          code2 {o->linenumber_size(o->Fl_Text_Display::textsize());}
          class TextViewer
        }
      }
    }
    Fl_Group {} {open
      xywh {10 460 500 20}
    } {
      Fl_Button {} {
        label Refresh
        callback update_sourceview_cb
        xywh {10 460 61 20} labelsize 11
      }
      Fl_Light_Button sv_autorefresh {
        label {Auto-Refresh}
        xywh {76 460 91 20} labelsize 11
        code0 {o->callback((Fl_Callback*)update_sourceview_cb);}
      }
      Fl_Light_Button sv_autoposition {
        label {Auto-Position}
        xywh {172 460 89 20} labelsize 11
      }
      Fl_Button {} {
        label Close
        callback toggle_sourceview_b_cb
        xywh {460 460 50 20} labelsize 11
      }
      Fl_Box {} {
        xywh {265 460 190 20} resizable
      }
    }
  }
}

comment {
//} {in_source in_header
}