/
nubasicide.cc
2376 lines (1642 loc) · 66.8 KB
/
nubasicide.cc
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
//
// This file is part of nuBASIC
// Copyright (c) Antonino Calderone (antonino.calderone@gmail.com)
// All rights reserved.
// Licensed under the MIT License.
// See COPYING file in the project root for full license information.
//
/* -------------------------------------------------------------------------- */
#include "nu_msgbox.h"
#include "nu_menu.h"
#include "nu_menubar.h"
#include "nu_statusbar.h"
#include "nu_accelgroup.h"
#include "nu_window.h"
#include "nu_vbox.h"
#include "nu_toolbar.h"
#include "nu_widget.h"
#include "nu_editor.h"
#include "nu_dialog_openfile.h"
#include "nu_dialog_savefile.h"
#include "nu_dialog_font.h"
#include "nu_dialog_about.h"
#include "nu_dialog_inputbox.h"
#include "nu_dialog_search.h"
#include "nu_terminal_frame.h"
#include "nu_about.h"
#include "nu_builtin_help.h"
#include "nu_exception.h"
#include "nu_interpreter.h"
#include "nu_reserved_keywords.h"
#include "mip_json_parser.h"
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <pthread.h>
#include <math.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <unordered_map>
#include <thread>
#include <functional>
/* -------------------------------------------------------------------------- */
#define NU_XSTR(s) NU_STR(s)
#define NU_STR(s) #s
#define IDE_CFG_FILE "ide.cfg"
#define IDE_CFG_DIR ".nubasic"
#define IDE_DEF_FONT "Monospace"
#define IDE_DEF_FONT_SIZE 9
#define IDE_SETTINGS_DEF_FONTNAME "fontname"
#define IDE_SETTINGS_DEF_FONTSIZE "fontsize"
/* -------------------------------------------------------------------------- */
struct cfg_t {
using cfg_data_t = std::unordered_map<std::string, std::string>;
std::string homedir() {
struct passwd *pw = getpwuid(getuid());
return pw->pw_dir ? pw->pw_dir : "";
}
bool _mkdir(const char *path, mode_t mode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) {
struct stat st;
if (stat(path, &st) != 0) {
if (mkdir(path, mode) != 0 && errno != EEXIST) {
return false;
}
} else if (!S_ISDIR(st.st_mode)) {
return false;
}
return true;
}
cfg_t() = default;
bool save(
const char* cfgdir,
const char* filename,
const cfg_data_t & cfg_data)
{
if (cfg_data.empty()) {
return false;
}
auto handle = mip::json_obj_t::make_object();
assert(handle);
for (const auto & cfg_item : cfg_data) {
handle->add_member(cfg_item.first, cfg_item.second.c_str());
}
_mkdir(cfgdir);
std::ofstream os(filename, std::ofstream::out | std::ofstream::binary);
if (!os.is_open() || os.bad()) {
std::_cerr << "Cannot create '" << filename << "'" << std::endl;
return false;
}
os << *handle;
return true;
}
bool load(
const char* filename,
mip::json_obj_t::handle_t & handle)
{
std::stringstream ss;
mip::json_parser_t parser(& ss);
std::ifstream is(filename, std::ifstream::in | std::ifstream::binary);
if (!is.is_open() || is.bad()) {
std::_cerr << "Cannot open '" << filename << "'" << std::endl;
return false;
}
std::_cout << "Loading configuration from '" << filename << "'" << std::endl;
std::stringstream errlog;
auto res = parser.parse(is, errlog);
if (!res.first) {
std::_cerr << std::endl << "JSON text is Invalid: "
<< errlog.str() << std::endl;
return false;
}
handle = std::move(res.second);
return true;
}
};
/* -------------------------------------------------------------------------- */
struct app_t : public nu::dialog_search_t::observer_t {
enum {
ERR_MSG_BUF_SIZE = 2048,
INTERPRETER_YELDS_BOUND = 5
};
enum class marker_t {
BOOKMARK = 0,
BREAKPOINT = 1,
PROGCOUNTER = 2,
LINESELECTION = 4,
};
enum class dbg_flg_t {
NORMAL_EXECUTION,
CONTINUE_EXECUTION = 1,
SINGLE_STEP_EXECUTION = 2
};
static const int black = RGB(0, 0, 0);
static const int white = RGB(0xff, 0xff, 0xff);
static const int red = RGB(0xff, 0, 0);
static const int yellow = RGB(0xff, 0xff, 0xe0);
static const int offWhite = RGB(0xff, 0xfb, 0xF0);
static const int darkGreen = RGB(0, 0x80, 0);
static const int darkBlue = RGB(0, 0, 0x80);
/* -------------------------------------------------------------------------- */
void replace_all( nu::dialog_search_t & ctx ) noexcept {
set_search_flags( ctx );
_replace_str = ctx.get_replace_text();
_find_str = ctx.get_text();
auto n = _replace_all(
_find_str.c_str(), _replace_str.c_str(), false);
if (n>0) {
std::string msg = "Replaced " + std::to_string(n) + " occurrences of ";
msg += _find_str;
msg += " with ";
msg += _replace_str + "\n";
nu::msgbox(mainwin(), msg.c_str(), "Replacing text...");
std::cerr << msg << std::endl;
}
else {
std::string msg = "";
msg += _find_str;
msg += " not found\n";
nu::msgbox(mainwin(), msg.c_str(), "Replacing text...");
std::cerr << msg << std::endl;
}
}
/* -------------------------------------------------------------------------- */
void set_search_flags( nu::dialog_search_t & ctx ) {
auto dlg_flgs = ctx.get_flags();
int flgs = get_search_flags();
if (dlg_flgs & nu::dialog_search_t::MATCHCASE) {
flgs |= SCFIND_MATCHCASE;
}
else {
flgs &= ~SCFIND_MATCHCASE;
}
if (dlg_flgs & nu::dialog_search_t::WHOLEWORD) {
flgs |= SCFIND_WHOLEWORD;
}
else {
flgs &= ~SCFIND_WHOLEWORD;
}
_set_search_flags(flgs);
}
/* -------------------------------------------------------------------------- */
enum class find_op_t { FIND, REPLACE };
void find_or_replace( nu::dialog_search_t & ctx, find_op_t op ) noexcept {
_find_str = ctx.get_text();
if ( op == find_op_t::REPLACE ) {
_replace_str = ctx.get_replace_text();
replace_searching_text( _replace_str.c_str() );
}
set_search_flags(ctx);
bool search_result = ctx.is_forward() ?
search_forward(_find_str) :
search_backward(_find_str);
if (!search_result) {
std::string msg = "'";
msg += _find_str;
msg += "' not found\n";
nu::msgbox(mainwin(), msg.c_str(), "Searching text...");
}
}
/* -------------------------------------------------------------------------- */
int _replace_all( const char* szFind, const char* szReplace, bool bUseSelection) {
int nCount = 0;
auto & ed = editor();
// different branches for replace in selection or total file
if (bUseSelection) {
// Get starting selection range
long length = 0;
long begin = long(ed.get_selection_begin());
long end = long(ed.get_selection_end());
// Set target to selection
ed.cmd(SCI_SETTARGETSTART, begin);
ed.cmd(SCI_SETTARGETEND, end);
// Try to find text in target for the first time
long pos = long(
ed.cmd(SCI_SEARCHINTARGET, strlen(szFind), szFind));
// loop over selection until end of selection
// reached - moving the target start each time
while (pos < end && pos >= 0) {
const auto szlen = strlen(szReplace);
length = _search_flags & SCFIND_REGEXP
? (long)ed.cmd( SCI_REPLACETARGETRE, szlen, szReplace)
: (long)ed.cmd( SCI_REPLACETARGET, szlen, szReplace);
// the end of the selection was changed - recalc the end
end = long(ed.get_selection_end());
// move start of target behind last change and end of target to new
// end of selection
ed.cmd(SCI_SETTARGETSTART, pos + length);
ed.cmd(SCI_SETTARGETEND, end);
// find again - if nothing found loop exits
pos = long(ed.cmd( SCI_SEARCHINTARGET, strlen(szFind), szFind));
nCount++;
}
}
else {
// start with first and last char in buffer
long length = 0;
long begin = 0;
long end = long(ed.cmd(SCI_GETTEXTLENGTH, 0, 0));
// set target to selection
ed.cmd(SCI_SETTARGETSTART, begin, 0);
ed.cmd(SCI_SETTARGETEND, end, 0);
// try to find text in target for the first time
long pos = long(
ed.cmd(SCI_SEARCHINTARGET, strlen(szFind), szFind));
// loop over selection until end of selection reached - moving the
// target start each time
while (pos < end && pos >= 0) {
const auto szlen = strlen(szReplace);
// check for regular expression flag
length = _search_flags & SCFIND_REGEXP
? (long)ed.cmd( SCI_REPLACETARGETRE, szlen, szReplace)
: (long)ed.cmd( SCI_REPLACETARGET, szlen, szReplace);
// the end of the selection was changed - recalc the end
end = long(ed.cmd(SCI_GETTEXTLENGTH, 0, 0));
// move start of target behind last change and end of target to new
// end of buffer
ed.cmd(SCI_SETTARGETSTART, pos + length);
ed.cmd(SCI_SETTARGETEND, end);
// find again - if nothing found loop exits
pos = long(ed.cmd( SCI_SEARCHINTARGET, strlen(szFind), szFind));
nCount++;
}
}
return nCount;
}
/* -------------------------------------------------------------------------- */
void notify_find_result( nu::dialog_search_t & ctx ) noexcept override {
find_or_replace( ctx, find_op_t::FIND );
}
/* -------------------------------------------------------------------------- */
void notify_replace_result( nu::dialog_search_t & ctx ) noexcept override {
find_or_replace( ctx, find_op_t::REPLACE );
}
/* -------------------------------------------------------------------------- */
void notify_replace_all_result( nu::dialog_search_t & ctx ) noexcept override {
replace_all( ctx );
}
/* -------------------------------------------------------------------------- */
int get_search_flags() const {
return int(editor().cmd(SCI_GETSEARCHFLAGS, 0, 0));
}
/* -------------------------------------------------------------------------- */
void _set_search_flags(int flags) {
_search_flags = flags;
editor().cmd(SCI_SETSEARCHFLAGS, _search_flags, 0);
}
/* -------------------------------------------------------------------------- */
static void process_gui_events() {
while (gtk_events_pending ())
gtk_main_iteration ();
}
/* -------------------------------------------------------------------------- */
static void run_gui_task_after(
size_t secs,
std::function<void(void)> f )
{
std::thread t([secs, f]() {
sleep(secs);
gdk_threads_enter();
f();
gdk_threads_leave();
});
t.detach();
}
/* -------------------------------------------------------------------------- */
static int dialog_goto (nu::window_t & parent) {
int ret = -1;
nu::dialog_inputbox_t dialog(parent, "Go to line...","Line", "1");
if (dialog.run() == GTK_RESPONSE_OK) {
try {
ret = std::stoi(dialog.edit_text());
}
catch (...) {}
}
return ret;
}
/* -------------------------------------------------------------------------- */
static std::vector<std::string> split(const std::string& s, char delim = ' ') {
std::vector<std::string> elems;
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim))
elems.push_back(item);
return elems;
}
/* -------------------------------------------------------------------------- */
void set_default_icon() {
GdkPixbuf *pixbuf =
gdk_pixbuf_new_from_resource("/org/gnome/name/nuBasic.ico", nullptr);
if (pixbuf) {
gtk_window_set_icon(GTK_WINDOW(mainwin().get_internal_obj()),pixbuf);
}
}
/* -------------------------------------------------------------------------- */
void configure_editor() {
auto & ed = editor();
// clear all text styles
ed.cmd(SCI_CLEARDOCUMENTSTYLE, 0, 0);
// Set the number of styling bits to 7 (default is 5)
ed.cmd(SCI_SETSTYLEBITS, 7, 0);
// Set the display for indentation guides to on.
// This displays vertical lines from the beginning of
// a code block to the end of the block
ed.cmd(SCI_SETINDENTATIONGUIDES, 1, 0);
ed.cmd(SCI_SETTABWIDTH, nu::editor_t::DEF_TABWIDTH, 0);
ed.cmd(SCI_SETINDENT, nu::editor_t::DEF_INDENT, 0);
ed.cmd(SCI_SETCARETPERIOD, nu::editor_t::DEF_CARETPERIOD, 0);
ed.cmd(SCI_SETUSETABS, 0, 0L);
ed.cmd(SCI_SETLEXER, SCLEX_VB);
static std::string keywords;
for (const auto& keyword : nu::reserved_keywords_t::list) {
keywords += keyword;
keywords += " ";
}
ed.cmd(SCI_SETKEYWORDS, 0, keywords.c_str());
// Get font name and size
auto font = cfg(IDE_SETTINGS_DEF_FONTNAME);
auto fontsize = cfg(IDE_SETTINGS_DEF_FONTSIZE);
if (font.empty())
font = IDE_DEF_FONT;
int i_fontsize = IDE_DEF_FONT_SIZE;
try {
i_fontsize = fontsize.empty() ? IDE_DEF_FONT_SIZE : std::stoi(fontsize);
}
catch (...) {}
// Set up the global default style.
ed.set_item_style(STYLE_DEFAULT, black, white, i_fontsize, font.c_str());
// Copies global style to all others
ed.cmd(SCI_STYLECLEARALL);
ed.set_item_style(SCE_B_NUMBER, RGB(0x80, 0, 0x80));
ed.set_item_style(SCE_B_HEXNUMBER, RGB(0x80, 0, 0x80));
ed.set_item_style(SCE_B_COMMENT, RGB(0x80, 0x80, 0));
ed.set_item_style(SCE_B_COMMENTBLOCK, RGB(0x80, 0x80, 0));
ed.set_item_style(SCE_B_PREPROCESSOR, black, yellow);
ed.set_item_style(SCE_B_DOCLINE, white, darkBlue);
ed.set_item_style(SCE_B_LABEL, offWhite, RGB(0x40, 0x40, 0x40));
ed.set_item_style(SCE_B_KEYWORD, darkBlue);
ed.set_item_style(SCE_B_KEYWORD2, darkBlue);
ed.set_item_style(SCE_B_KEYWORD3, darkBlue);
ed.set_item_style(SCE_B_KEYWORD4, darkBlue);
ed.set_item_style(SCE_B_STRING, darkGreen);
ed.set_item_style(SCE_B_STRINGEOL, darkGreen);
ed.set_item_style(SCE_B_DEFAULT, RGB(0x80, 0x80, 0x80));
// source folding section
// Tell the lexer that we want folding information
// The lexer supplies "folding levels"
ed.cmd(SCI_SETPROPERTY, "fold", "1");
ed.cmd(SCI_SETPROPERTY, "fold.preprocessor", "1");
ed.cmd(SCI_SETPROPERTY, "fold.comment", "1");
ed.cmd(SCI_SETPROPERTY, "fold.at.else", "1");
ed.cmd(SCI_SETPROPERTY, "fold.flags", "1");
ed.cmd(SCI_SETPROPERTY, "styling.within.preprocessor", "1");
ed.cmd(SCI_SETPROPERTY, "basic.default.language", "1");
// Tell scintilla to draw folding lines UNDER the folded line
ed.cmd(SCI_SETFOLDFLAGS, 16, 0);
// Set margin 2 = folding margin to display folding symbols
ed.cmd(SCI_SETMARGINMASKN, 2, SC_MASK_FOLDERS);
// Allow notifications for folding actions
ed.cmd(SCI_SETMODEVENTMASK, SC_MOD_INSERTTEXT | SC_MOD_DELETETEXT, 0);
ed.cmd(SCI_SETMODEVENTMASK, SC_MOD_CHANGEFOLD | SC_MOD_INSERTTEXT | SC_MOD_DELETETEXT, 0);
// Make the folding margin sensitive to folding events
// If you click into the margin you get a notification event
ed.cmd(SCI_SETMARGINSENSITIVEN, 2, TRUE);
// define a set of markers to display folding symbols
ed.cmd(SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPEN, SC_MARK_MINUS);
ed.cmd(SCI_MARKERDEFINE, SC_MARKNUM_FOLDER, SC_MARK_PLUS);
ed.cmd(SCI_MARKERDEFINE, SC_MARKNUM_FOLDERSUB, SC_MARK_EMPTY);
ed.cmd(SCI_MARKERDEFINE, SC_MARKNUM_FOLDERTAIL, SC_MARK_EMPTY);
ed.cmd(SCI_MARKERDEFINE, SC_MARKNUM_FOLDEREND, SC_MARK_EMPTY);
ed.cmd(SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPENMID, SC_MARK_EMPTY);
ed.cmd(SCI_MARKERDEFINE, SC_MARKNUM_FOLDERMIDTAIL, SC_MARK_EMPTY);
// Set the foreground colour for some styles
ed.cmd(SCI_STYLESETFORE, 0, RGB(0, 0, 0));
ed.cmd(SCI_STYLESETFORE, 2, RGB(0, 64, 0));
ed.cmd(SCI_STYLESETFORE, 5, RGB(0, 0, 255));
ed.cmd(SCI_STYLESETFORE, 6, RGB(200, 20, 0));
ed.cmd(SCI_STYLESETFORE, 9, RGB(0, 0, 255));
ed.cmd(SCI_STYLESETFORE, 10, RGB(255, 0, 64));
ed.cmd(SCI_STYLESETFORE, 11, RGB(0, 0, 0));
// Set the backgroundcolor of brace highlights
ed.cmd(SCI_STYLESETBACK, STYLE_BRACELIGHT, RGB(0, 255, 0));
// Set end of line mode to CRLF
ed.cmd(SCI_CONVERTEOLS, 2, 0);
ed.cmd(SCI_SETEOLMODE, 2, 0);
// Set markersymbol for marker type 0 - bookmark
ed.cmd(SCI_MARKERDEFINE, 0, SC_MARK_CIRCLE);
// Display all margins
ed.set_numbers_margin(true);
ed.set_folding_margin(true);
ed.set_selection_margin(true);
ed.refresh();
}
/* ---------------------------------------------------------------------- */
void make_editor(const nu::vbox_t& vbox) {
static nu::editor_t editor;
editor.set_id(0);
vbox.container_add(editor);
_editor_ptr = &editor;
}
/* ---------------------------------------------------------------------- */
void open_document_file(const char* fileName) {
std::string old_file_name = _full_path_str;
_full_path_str = fileName;
FILE* fp = fopen(_full_path_str.c_str(), "rb");
if (fp) {
set_new_document(false);
editor().cmd(SCI_CANCEL);
editor().cmd(SCI_SETUNDOCOLLECTION, 0);
set_title();
enum { READ_BLOCKSIZE = 128 * 1024 };
char data[READ_BLOCKSIZE] = { 0 };
int lenFile = int(fread(data, 1, sizeof(data), fp));
while (lenFile > 0) {
editor().cmd(SCI_ADDTEXT, lenFile, data);
lenFile = int(fread(data, 1, sizeof(data), fp));
}
fclose(fp);
std::string msg = "Load '";
msg += _full_path_str;
msg += "'\n";
} else {
std::string msg = "Could not open file \"";
msg += _full_path_str + "\".";
nu::msgbox(mainwin(), msg.c_str(), "Error");
_full_path_str = old_file_name;
set_title();
}
editor().cmd(SCI_SETUNDOCOLLECTION, 1);
editor().cmd(SCI_EMPTYUNDOBUFFER);
editor().cmd(SCI_SETSAVEPOINT);
editor().cmd(SCI_GOTOPOS, 0);
}
/* -------------------------------------------------------------------------- */
void set_title() {
std::string s_title = "nuBASIC";
s_title += " - File name: '";
s_title += _full_path_str + "'";
mainwin().set_title(s_title.c_str());
std::stringstream os;
os << "Ln " << editor().get_current_line();
os << " Col " << editor().get_current_colum() << "\r\n";
if (is_dirty())
os << "<Mod>";
const auto txt = os.str();
statusbar().set_text(txt.c_str());
}
/* -------------------------------------------------------------------------- */
void save_document_as() {
nu::dialog_savefile_t dlg(mainwin(), "Save File");
auto response = dlg.run("", _full_path_str.c_str());
if (response == GTK_RESPONSE_ACCEPT) {
const char *filename = dlg.filename();
_full_path_str = filename;
set_title();
save_file(_full_path_str.c_str());
}
}
/* -------------------------------------------------------------------------- */
void save_file(const char* filename) {
FILE* fp = fopen(filename, "w");
if (fp) {
auto doc_size = editor().cmd(SCI_GETLENGTH);
std::vector<char> data(doc_size + 1);
editor().get_text_range(0, int(doc_size), data.data());
fwrite(data.data(), doc_size, 1, fp);
fclose(fp);
editor().cmd(SCI_SETSAVEPOINT);
} else {
std::string msg = "Could not save file \"";
msg += filename;
msg += "\".";
nu::msgbox(mainwin(), msg.c_str(), "Information", GTK_BUTTONS_OK);
}
}
/* -------------------------------------------------------------------------- */
void save_document() {
if (_full_path_str.empty())
_full_path_str = "noname.bas";
save_file(_full_path_str.c_str());
}
/* -------------------------------------------------------------------------- */
int save_if_unsure() {
if (_is_dirty) {
bool save_as_dialog = false;
if (_full_path_str.empty()) {
_full_path_str = "noname.bas";
save_as_dialog = true;
}
std::string msg = "Save changes to \"" + _full_path_str + "\"?";
int decision = nu::msgbox(
mainwin(), msg.c_str(), "Confirmation", GTK_BUTTONS_YES_NO);
if (decision == GTK_RESPONSE_YES) {
if (save_as_dialog)
save_document_as();
else
save_document();
}
return decision;
}
return 1;
}
/* -------------------------------------------------------------------------- */
void set_new_document(bool clear_title) {
auto & ed = editor();
ed.cmd(SCI_CLEARALL);
ed.cmd(SCI_EMPTYUNDOBUFFER);
if (clear_title) {
_full_path_str.clear();
set_title();
}
_is_dirty = false;
_need_build = true;
ed.cmd(SCI_SETSAVEPOINT);
ed.remove_all_bookmarks();
ed.remove_all_breakpoints();
errorbar().set_text("");
errorbar().hide();
}
/* -------------------------------------------------------------------------- */
static void toolbar_openfile(GtkToolItem *toolitem, GtkWindow *parentWindow) {
menu_file_open(nullptr, parentWindow);
}
/* -------------------------------------------------------------------------- */
static void make_toolbar(const nu::window_t& window, const nu::vbox_t& vbox) {
nu::toolbar_t toolbar;
toolbar.set_show_arrow();
toolbar.set_style(GTK_TOOLBAR_ICONS);
vbox.pack_start(toolbar);
auto id = 0;
toolbar.add_stock_item(GTK_STOCK_NEW, "New", window,
[] { get_instance().set_new_document(true); }, id++);
toolbar.add_stock_item(GTK_STOCK_OPEN, "Open", window, toolbar_openfile, id++);
toolbar.add_stock_item(GTK_STOCK_SAVE, "Save", window,
[] { get_instance().save_document(); }, id++);
toolbar.add_separator( id ++ );
toolbar.add_stock_item(GTK_STOCK_EXECUTE, "Build", window,
[] { get_instance().rebuild_code(true); } , id++);
toolbar.add_separator( id ++ );
toolbar.add_stock_item(GTK_STOCK_MEDIA_RECORD, "Breakpoint", window,
[] { get_instance().toggle_breakpoint(); }, id++);
toolbar.add_separator( id ++ );
toolbar.add_stock_item(GTK_STOCK_MEDIA_PLAY, "Run", window,
[] { get_instance().start_debugging(); }, id++);
toolbar.add_stock_item(GTK_STOCK_MEDIA_PAUSE, "Stop", window,
[] { get_instance().stop_debugging(); }, id++);
toolbar.add_stock_item(GTK_STOCK_GOTO_LAST, "Step", window,
[] { get_instance().singlestep_debugging(); }, id++);
toolbar.add_stock_item(GTK_STOCK_GO_FORWARD, "Continue", window,
[] { get_instance().continue_debugging(); }, id++);
toolbar.add_separator( id ++ );
toolbar.add_stock_item(GTK_STOCK_INDEX, "Evaluate", window,
[] { get_instance().eval_sel(); }, id++);
toolbar.add_separator( id ++ );
toolbar.add_stock_item(GTK_STOCK_FIND, "Find", window, run_find_dlg, id++);
toolbar.add_stock_item(GTK_STOCK_FIND_AND_REPLACE, "Replace", window, run_replace_dlg, id++);
toolbar.add_separator( id ++ );
toolbar.add_stock_item(GTK_STOCK_DIALOG_INFO, "Describe", window,
[] { get_instance().show_ctx_help(); }, id++);
toolbar.add_stock_item(GTK_STOCK_DIALOG_QUESTION, "Help-online", window,
[] { get_instance().show_online_help(); }, id++);
toolbar.add_separator( id ++ );
toolbar.add_stock_item(GTK_STOCK_ZOOM_IN, "Zoom in", window,
[]{get_instance().editor().cmd(SCI_ZOOMIN);}, id++);
toolbar.add_stock_item(GTK_STOCK_ZOOM_OUT, "Zoom out", window,
[]{get_instance().editor().cmd(SCI_ZOOMOUT);}, id++);
}
/* -------------------------------------------------------------------------- */
// on stop the GTK+ main loop function
static void destroy_window(GtkWidget *window, gpointer data) {
get_instance().quit();
//gtk_main_quit();
}
/* -------------------------------------------------------------------------- */
/*
Return FALSE to destroy the widget.
By returning TRUE, you can cancel a delete-event.
This can be used to confirm quitting the application.
*/
static gboolean delete_event_window(GtkWidget *window, GdkEvent *event, gpointer data) {
(void) window;
(void) event;
(void) data;
return (FALSE);
}
/* ---------------------------------------------------------------------- */
static void menu_dummy(GtkMenuItem *fileopen, GtkWindow *parentWindow) {
nu::msgbox(parentWindow, "Menu clicked.", "Menu");
}
/* ---------------------------------------------------------------------- */
static void menu_file_open(GtkMenuItem *fileopen, GtkWindow *window) {
(void) fileopen;
nu::window_t nuwin((GtkWidget *)window);
nu::dialog_openfile_t dlg(nuwin, "Open File");
auto response = dlg.run();
if (response == GTK_RESPONSE_ACCEPT) {
const char *filename = dlg.filename();
if (filename) {
get_instance().open_document_file(filename);
}
}
}
/* -------------------------------------------------------------------------- */
static void menu_file_saveas(GtkMenuItem *, GtkWindow *window) {
get_instance().save_document_as();
}
/* ---------------------------------------------------------------------- */
static void menu_settings_font(GtkMenuItem *, GtkWindow *window) {
nu::window_t nuwin((GtkWidget *)window);
auto & app = get_instance();
auto fontname = app.cfg(IDE_SETTINGS_DEF_FONTNAME);
auto fontsize = app.cfg(IDE_SETTINGS_DEF_FONTSIZE);
if (fontname.empty())
fontname = IDE_DEF_FONT;
int i_fontsize = IDE_DEF_FONT_SIZE;
try {
i_fontsize = std::stoi(fontsize);
}
catch (...) {}
nu::dialog_font_t dlg(nuwin, fontname.c_str(), i_fontsize);
auto response = dlg.run();
if (response == GTK_RESPONSE_OK) {
auto c_font = dlg.font();
i_fontsize = dlg.font_size();
std::string font = c_font ? c_font : IDE_DEF_FONT;
app.set_cfg(IDE_SETTINGS_DEF_FONTNAME, font.c_str());
app.set_cfg(IDE_SETTINGS_DEF_FONTSIZE, std::to_string(i_fontsize));
app.configure_editor();
}
}
/* ---------------------------------------------------------------------- */
static int exit_main_window(GtkWidget* w, GdkEventAny* e, gpointer p) {
(void) w;
(void) e;
(void) p;
get_instance().quit();
return 1;
}
/* ---------------------------------------------------------------------- */
static void on_scintilla_notification(
ScintillaObject * /*scintilla_obj*/,
gint /*dummy*/,
struct SCNotification *notification,
gpointer /*user_data*/)
{
auto & app = get_instance();
switch (notification->nmhdr.code) {
case SCN_AUTOCSELECTION:
break;
case SCN_SAVEPOINTREACHED:
app.set_dirty_flg(false);
app.set_needbuild_flg();
break;