summaryrefslogtreecommitdiff
path: root/fluid/panels/codeview_panel.fl
blob: 54f0d790fd6120e9654d1fede1f5d2be05a60dff (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
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
# data file for the Fltk User Interface Designer (fluid)
version 1.0500
header_name {.h}
code_name {.cxx}
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 "app/fluid.h"} {private local
}

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

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

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

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

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

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

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

decl {int cv_code_choice;} {public local
}

decl {extern void select_only(Fl_Type *o);} {private global
}

decl {extern void reveal_in_browser(Fl_Type *t);} {private global
}

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

The Code 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 (!codeview_panel || !codeview_panel->visible())
    return;
  if (cv_autoposition->value()==0)
    return;
  if (codeview_panel && codeview_panel->visible() && Fl_Type::current) {
    int pos0 = 0, pos1 = 0;
    if (cv_source->visible_r()) {
      switch (cv_code_choice) {
        case 0: // prolog: not yet (include statements)
          pos0 = Fl_Type::current->code1_start;
          pos1 = Fl_Type::current->code2_end;
          break;
        case 1: // static: callbacks, menu declarations
          pos0 = Fl_Type::current->code_static_start;
          pos1 = Fl_Type::current->code_static_end;
          break;
        case 2: // code: entire implementation block including children
          pos0 = Fl_Type::current->code1_start;
          pos1 = Fl_Type::current->code2_end;
          break;
        case 3: // code1: all implementation code before the children
          pos0 = Fl_Type::current->code1_start;
          pos1 = Fl_Type::current->code1_end;
          break;
        case 4: // code1: all implementation code before the children
          pos0 = Fl_Type::current->code2_start;
          pos1 = Fl_Type::current->code2_end;
          break;
      }
      if (pos0>=0) {
        if (pos1<pos0)
          pos1 = cv_source->buffer()->line_end(pos0);
        cv_source->buffer()->highlight(pos0, pos1);
        int line = cv_source->buffer()->count_lines(0, pos0);
        cv_source->scroll(line, 0);
      }
    }
    if (cv_header->visible_r()) {
      switch (cv_code_choice) {
        case 0: // prolog: not yet (include statements)
        case 1: // static: callbacks, menu declarations
          pos0 = Fl_Type::current->header_static_start;
          pos1 = Fl_Type::current->header_static_end;
          break;
        case 2: // code: entire implementation block including children
          pos0 = Fl_Type::current->header1_start;
          pos1 = Fl_Type::current->header2_end;
          break;
        case 3: // code1: all implementation code before the children
          pos0 = Fl_Type::current->header1_start;
          pos1 = Fl_Type::current->header1_end;
          break;
        case 4: // code1: all implementation code before the children
          pos0 = Fl_Type::current->header2_start;
          pos1 = Fl_Type::current->header2_end;
          break;
      }
      if (pos0>=0) {
        if (pos1<pos0)
          pos1 = cv_header->buffer()->line_end(pos0);
        cv_header->buffer()->highlight(pos0, pos1);
        int line = cv_header->buffer()->count_lines(0, pos0);
        cv_header->scroll(line, 0);
      }
    }
    if (cv_project->visible_r()) {
      switch (cv_code_choice) {
        case 0: // prolog: not yet (include statements)
        case 1: // static: callbacks, menu declarations
        case 2: // code: entire implementation block including children
          pos0 = Fl_Type::current->proj1_start;
          pos1 = Fl_Type::current->proj2_end;
          break;
        case 3: // code1: all implementation code before the children
          pos0 = Fl_Type::current->proj1_start;
          pos1 = Fl_Type::current->proj1_end;
          break;
        case 4: // code1: all implementation code before the children
          pos0 = Fl_Type::current->proj2_start;
          pos1 = Fl_Type::current->proj2_end;
          break;
      }
      if (pos0>=0) {
        if (pos1<pos0)
          pos1 = cv_project->buffer()->line_end(pos0);
        cv_project->buffer()->highlight(pos0, pos1);
        int line = cv_project->buffer()->count_lines(0, pos0);
        cv_project->scroll(line, 0);
      }
    }
  }} {}
}

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

Function {update_codeview_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 (!codeview_panel || !codeview_panel->visible())
    return;

  if (!cv_source_filename) {
    cv_source_filename = (char*)malloc(FL_PATH_MAX);
    fl_strlcpy(cv_source_filename, get_tmpdir().c_str(), FL_PATH_MAX);
    fl_strlcat(cv_source_filename, "codeview_tmp.cxx", FL_PATH_MAX);
  }
  if (!cv_header_filename) {
    cv_header_filename = (char*)malloc(FL_PATH_MAX);
    fl_strlcpy(cv_header_filename, get_tmpdir().c_str(), FL_PATH_MAX);
    fl_strlcat(cv_header_filename, "codeview_tmp.h", FL_PATH_MAX);
  }
  if (!cv_design_filename) {
    cv_design_filename = (char*)malloc(FL_PATH_MAX);
    fl_strlcpy(cv_design_filename, get_tmpdir().c_str(), FL_PATH_MAX);
    fl_strlcat(cv_design_filename, "codeview_tmp.fl", FL_PATH_MAX);
  }

  if (cv_project->visible_r()) {
    write_file(cv_design_filename, false, true);
    int top = cv_project->top_line();
    cv_project->buffer()->loadfile(cv_design_filename);
    cv_project->scroll(top, 0);
  } else if (cv_strings->visible_r()) {
    static const char *exts[] = { ".txt", ".po", ".msg" };
    char fn[FL_PATH_MAX+1];
    fl_strlcpy(fn, get_tmpdir().c_str(), FL_PATH_MAX);
    fl_strlcat(fn, "strings", FL_PATH_MAX);
    fl_filename_setext(fn, FL_PATH_MAX, exts[g_project.i18n_type]);
    write_strings(fn);
    int top = cv_strings->top_line();
    cv_strings->buffer()->loadfile(fn);
    cv_strings->scroll(top, 0);
  } else if (cv_source->visible_r() || cv_header->visible_r()) {
    std::string code_file_name_bak = g_project.code_file_name;
    g_project.code_file_name = cv_source_filename;
    std::string header_file_name_bak = g_project.header_file_name;
    g_project.header_file_name = cv_header_filename;

    // generate the code and load the files
    Fd_Code_Writer f;
    // generate files
    if (f.write_code(cv_source_filename, cv_header_filename, true))
    {
      // load file into source editor
      int pos = cv_source->top_line();
      cv_source->buffer()->loadfile(cv_source_filename);
      cv_source->scroll(pos, 0);
      // load file into header editor
      pos = cv_header->top_line();
      cv_header->buffer()->loadfile(cv_header_filename);
      cv_header->scroll(pos, 0);
      // update the source code highlighting
      update_codeview_position();
    }

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

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

Function {codeview_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_codeview_timer, 0);
    Fl::add_timeout(0.5, update_codeview_timer, 0);} {}
}

Function {codeview_toggle_visibility()} {
  comment {Show or hide the source code preview.
The state is stored in the app preferences.
} open return_type void
} {
  code {if (!codeview_panel) {
    make_codeview();
    codeview_panel->callback((Fl_Callback*)toggle_codeview_cb);
    Fl_Preferences svp(fluid_prefs, "codeview");
    int autorefresh;
    svp.get("autorefresh", autorefresh, 1);
    cv_autorefresh->value(autorefresh);
    int autoposition;
    svp.get("autoposition", autoposition, 1);
    cv_autoposition->value(autoposition);
    int tab;
    svp.get("tab", tab, 0);
    if (tab>=0 && tab<cv_tab->children()) cv_tab->value(cv_tab->child(tab));
    svp.get("code_choice", cv_code_choice, 2);
    cv_code_choice_w->value(cv_code_choice_w->find_item_with_argument(cv_code_choice));
    if (!position_window(codeview_panel,"codeview_pos", 0, 320, 120, 550, 500)) return;
  }

  if (codeview_panel->visible()) {
    codeview_panel->hide();
    codeview_item->label("Show Code View");
  } else {
    codeview_panel->show();
    codeview_item->label("Hide Code View");
    update_codeview_cb(0,0);
  }} {}
}

Function {make_codeview()} {open
} {
  Fl_Window codeview_panel {
    label {Code View}
    callback toggle_codeview_cb open
    xywh {389 507 520 515} type Double align 80 resizable size_range {384 120 0 0} visible
  } {
    Fl_Tabs cv_tab {
      callback update_codeview_position_cb open
      xywh {10 10 500 440} selection_color 4 labelcolor 7 resizable
    } {
      Fl_Group cv_source_tab {
        label Source open
        xywh {10 35 500 415} labelsize 13 resizable
      } {
        Fl_Text_Editor cv_source {
          xywh {10 40 500 410} textfont 4 textsize 11 resizable
          code0 {\#include "widgets/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 cv_header {
          xywh {10 40 500 410} textfont 4 textsize 11 resizable
          code0 {\#include "widgets/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 cv_strings {
          xywh {10 40 500 410} 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 cv_project {
          xywh {10 40 500 410} textfont 4 textsize 11 resizable
          code1 {o->linenumber_width(60);}
          code2 {o->linenumber_size(o->Fl_Text_Display::textsize());}
          class TextViewer
        }
      }
    }
    Fl_Group cv_find_row {open
      xywh {10 460 500 20}
    } {
      Fl_Button cv_find_text_case {
        label aA
        xywh {244 460 25 20} type Toggle labelsize 11
      }
      Fl_Input cv_find_text {
        label {Find:}
        callback {Fl_Text_Display *e = NULL;
if (cv_source->visible_r()) {
  e = cv_source;
} else if (cv_header->visible_r()) {
  e = cv_header;
} else if (cv_project->visible_r()) {
  e = cv_project;
}
if (e) {
  Fl_Text_Buffer *b = e->buffer();
  int pos = e->insert_position();
  int found = b->search_forward(pos, o->value(), &pos, cv_find_text_case->value());
  if (found) {
    b->select(pos, pos + (int)strlen(o->value()));
    e->insert_position(pos);
    e->show_insert_position();
  }
}}
        xywh {40 460 200 20} labelsize 11 when 15 textsize 11
      }
      Fl_Button {} {
        label {<<}
        callback {Fl_Text_Display *e = NULL;
if (cv_source->visible_r()) {
  e = cv_source;
} else if (cv_header->visible_r()) {
  e = cv_header;
} else if (cv_project->visible_r()) {
  e = cv_project;
}
if (e) {
  const char *needle = cv_find_text->value();
  Fl_Text_Buffer *b = e->buffer();
  int pos = e->insert_position()-1;
  if (pos < 0) pos = b->length()-1;
  int found = b->search_backward(pos, needle, &pos, cv_find_text_case->value());
  if (!found)
    found = b->search_backward(b->length()-1, needle, &pos, cv_find_text_case->value());
  if (found) {
    b->select(pos, pos + (int)strlen(needle));
    e->insert_position(pos);
    e->show_insert_position();
  }
}}
        xywh {273 460 25 20} labelsize 11
      }
      Fl_Button {} {
        label {>>}
        callback {Fl_Text_Display *e = NULL;
if (cv_source->visible_r()) {
  e = cv_source;
} else if (cv_header->visible_r()) {
  e = cv_header;
} else if (cv_project->visible_r()) {
  e = cv_project;
}
if (e) {
  const char *needle = cv_find_text->value();
  Fl_Text_Buffer *b = e->buffer();
  int pos = e->insert_position() + 1;
  if (pos+1 >= b->length()) pos = 0;
  int found = b->search_forward(pos, needle, &pos, cv_find_text_case->value());
  if (!found && (pos > 0))
    found = b->search_forward(0, needle, &pos, cv_find_text_case->value());
  if (found) {
    b->select(pos, pos + (int)strlen(needle));
    e->insert_position(pos);
    e->show_insert_position();
  }
}}
        xywh {298 460 25 20} labelsize 11
      }
      Fl_Button {} {
        label Reveal
        callback {if (codeview_panel && codeview_panel->visible()) {
  Fl_Type *node = NULL;
  if (cv_source->visible_r())
    node = Fl_Type::find_in_text(0, cv_source->insert_position());
  else if (cv_header->visible_r())
    node = Fl_Type::find_in_text(1, cv_header->insert_position());
  else if (cv_project->visible_r())
    node = Fl_Type::find_in_text(2, cv_project->insert_position());
  if (node) {
    select_only(node);
    reveal_in_browser(node);
    if (Fl::event_clicks()==1) // double click
      node->open();
  }
}}
        xywh {327 460 61 20} labelsize 11
      }
      Fl_Box {} {
        xywh {490 460 20 20} resizable
      }
    }
    Fl_Group cv_settings_row {open
      xywh {10 485 500 20}
    } {
      Fl_Button {} {
        label Refresh
        callback update_codeview_cb
        xywh {10 485 61 20} labelsize 11
      }
      Fl_Light_Button cv_autorefresh {
        label {Auto-Refresh}
        xywh {77 485 91 20} labelsize 11
        code0 {o->callback((Fl_Callback*)update_codeview_cb);}
      }
      Fl_Light_Button cv_autoposition {
        label {Auto-Position}
        xywh {172 485 89 20} labelsize 11
      }
      Fl_Choice cv_code_choice_w {
        callback {cv_code_choice = (int)o->mvalue()->argument();
update_codeview_position();} open
        xywh {265 485 70 20} down_box BORDER_BOX labelsize 11 textsize 11
      } {
        MenuItem {} {
          label prolog
          user_data 0 user_data_type long
          tooltip {Include statements in header or source code} xywh {0 0 100 20} labelsize 11 hide
        }
        MenuItem {} {
          label static
          user_data 1 user_data_type long
          tooltip {static declarations in source code} xywh {10 10 100 20} labelsize 11
        }
        MenuItem {} {
          label code
          user_data 2 user_data_type long
          tooltip {widget code block including children} xywh {20 20 100 20} labelsize 11
        }
        MenuItem {} {
          label {code 1}
          user_data 3 user_data_type long
          tooltip {widget code block before children} xywh {30 30 100 20} labelsize 11
        }
        MenuItem {} {
          label {code 2}
          user_data 4 user_data_type long
          tooltip {widget code block after children} xywh {40 40 100 20} labelsize 11
        }
      }
      Fl_Box {} {
        xywh {375 485 80 20} resizable
      }
      Fl_Button {} {
        label Close
        callback toggle_codeview_b_cb
        xywh {460 485 50 20} labelsize 11
      }
    }
  }
}

comment {
//} {in_source in_header
}