forked from RefPerSys/RefPerSys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfltk_rps.cc
More file actions
1528 lines (1406 loc) · 54.4 KB
/
fltk_rps.cc
File metadata and controls
1528 lines (1406 loc) · 54.4 KB
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
/****************************************************************
* file fltk_rps.cc
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Description:
* This file is part of the Reflective Persistent System.
*
* It has the code for the FLTK 1.4 graphical interface. See
* also https://fltk.org - download FLTK source code and compile
* it with debugging enabled, see our README.md for more.
*
* Author(s):
* Basile Starynkevitch <basile@starynkevitch.net>
* Abhishek Chakravarti <abhishek@taranjali.org>
* Nimesh Neema <nimeshneema@gmail.com>
*
* © Copyright 2024 - 2025 The Reflective Persistent System Team
* team@refpersys.org & http://refpersys.org/
*
* License:
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
************************************************************************/
#include "refpersys.hh"
//@@PKGCONFIG glib-2.0
#include "glib.h"
/// explicitly include the generated/rpsdata.h
#include "generated/rpsdata.h"
#ifndef RPS_WITH_FLTK
#error RefPerSys without FLTK
#endif
#if !RPS_WITH_FLTK
#error RefPerSys without FLTK toolkit
#endif
///NOTICE: after commit 094f904dd02 of end Nov. 2024 we use our userpref_rps.cc functions
/// these are from generated/rpsdata.h
#if RPS_FLTK_ABI_VERSION < 10400
#error RefPerSys requires FLTK 1.4 or 1.5 ABI
#endif
#if RPS_FLTK_API_VERSION < 10400
#error RefPerSys requires FLTK 1.4 or 1.5 API
#endif
#warning perhaps we should use FOX-toolkit from "https://fox-toolkit.org"
#include <stdarg.h>
#include <FL/Fl.H>
// The below include defines FL_ABI_VERSION and FL_API_VERSION
#include <FL/Enumerations.H>
#if FL_API_VERSION < 10400
#error FLTK 1.4 is required
#endif
#include <FL/platform.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Tile.H>
#include <FL/Fl_Menu_Bar.H>
#include <FL/Fl_Menu_Button.H>
#include <FL/Fl_Choice.H>
#include <FL/Fl_Multi_Label.H>
#include <FL/Fl_Widget.H>
#include <FL/Fl_Text_Buffer.H>
#include <FL/Fl_Text_Editor.H>
#include <FL/Fl_Text_Display.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Pack.H>
#include <FL/Fl_Input.H>
/// Fl_Flex.h is only in FLTK 1.4 not FLTK 1.3
#include <FL/Fl_Flex.H>
/// conventional strings
extern "C" const char rps_fltk_gitid[];
const char rps_fltk_gitid[]= RPS_GITID;
extern "C" const char rps_fltk_date[];
const char rps_fltk_date[]= __DATE__;
extern "C" const char rps_fltk_shortgitid[];
const char rps_fltk_shortgitid[]= RPS_SHORTGITID;
extern "C" const int rps_fltk_api_version;
const int rps_fltk_api_version = FL_API_VERSION;
// we don't use (after commit 4b2d027185 end of Nov. 2024) the FLTK
// preference machinery, since we have userpref_rps.cc....
////////////////////////////////////////////////////////////////////////
////// ******** DECLARATIONS ********
/// Fl_Color is an unsigned int (32 bits)
extern "C" Fl_Color rps_fltk_color_of_name(const char*colorname);
class Rps_PayloadFltkThing;
class Rps_PayloadFltkWidget;
class Rps_PayloadFltkRefWidget;
class Rps_PayloadFltkWindow;
class Rps_FltkMainWindow;
class Rps_FltkDebugWindow;
class Rps_FltkInputTextEditor;
extern "C" Rps_FltkMainWindow* rps_fltk_mainwin;
Rps_FltkMainWindow* rps_fltk_mainwin;
extern "C" Rps_FltkDebugWindow* rps_fltk_debugwin;
Rps_FltkDebugWindow* rps_fltk_debugwin;
extern "C" bool rps_fltk_is_initialized;
bool rps_fltk_is_initialized;
// Convert a string naming a color...
// if the color is not found, return black; on success set *ok to true
// accept numerical colors like #102030 for red=0x10 green=0x20 blue=0x30
extern "C" Fl_Color rps_fltk_color_by_name (const char*name, bool*ok=nullptr);
extern "C" void rps_fltk_input_fd_handler(FL_SOCKET fd, void *data);
extern "C" void rps_fltk_output_fd_handler(FL_SOCKET fd, void *data);
extern "C"
/// temporary payload for any FLTK object
class Rps_PayloadFltkThing : public Rps_Payload
{
friend Rps_PayloadFltkThing*
Rps_QuasiZone::rps_allocate1<Rps_PayloadFltkThing,Rps_ObjectZone*>(Rps_ObjectZone*);
protected:
union
{
void*fltk_ptr;
// see https://github.com/fltk/fltk/issues/975
Fl_Widget*fltk_widget;
Fl_Window*fltk_window;
Fl_Text_Buffer*fltk_text_buffer;
};
virtual void gc_mark(Rps_GarbageCollector&gc) const;
virtual void dump_scan(Rps_Dumper*du) const;
virtual void dump_json_content(Rps_Dumper*, Json::Value&) const;
inline Rps_PayloadFltkThing(Rps_ObjectZone*owner);
Rps_PayloadFltkThing(Rps_ObjectRef obr) :
Rps_Payload(Rps_Type::PaylFltkThing,obr?obr.optr():nullptr), fltk_ptr(nullptr) {};
Rps_PayloadFltkThing(Rps_Type rty, Rps_ObjectRef obr) :
Rps_Payload(rty,obr?obr.optr():nullptr), fltk_ptr(nullptr)
{
RPS_ASSERT(rty== Rps_Type::PaylFltkWidget
|| rty==Rps_Type::PaylFltkWindow
|| rty==Rps_Type::PaylFltkThing);
};
Rps_PayloadFltkThing(Rps_Type rty, Rps_ObjectRef obr, Fl_Widget*wid) :
Rps_Payload(rty,obr?obr.optr():nullptr), fltk_widget(wid)
{
RPS_ASSERT(rty== Rps_Type::PaylFltkWidget);
};
virtual const std::string payload_type_name(void) const
{
return "FltkThing";
};
virtual uint32_t wordsize(void) const
{
return sizeof(*this)/sizeof(void*);
};
virtual bool is_erasable(void) const
{
return false;
};
public:
virtual ~Rps_PayloadFltkThing() =0;
}; // end class Rps_PayloadFltkThing
////////////////////////////////////////////////////////////////
/// temporary payload for any FLTK widget
class Rps_PayloadFltkWidget : public Rps_PayloadFltkThing
{
friend Rps_PayloadFltkWidget*
Rps_QuasiZone::rps_allocate1<Rps_PayloadFltkWidget,Rps_ObjectZone*>(Rps_ObjectZone*);
friend Rps_PayloadFltkWidget*
Rps_QuasiZone::rps_allocate2<Rps_PayloadFltkWidget,Rps_ObjectZone*,Fl_Widget*>(Rps_ObjectZone*,Fl_Widget*);
inline Rps_PayloadFltkWidget(Rps_ObjectZone*owner, Fl_Widget*wid);
Rps_PayloadFltkWidget(Rps_ObjectRef obr)
: Rps_PayloadFltkThing(Rps_Type::PaylFltkWidget, obr, nullptr) {};
Rps_PayloadFltkWidget(Rps_ObjectRef obr, Fl_Widget*wid)
: Rps_PayloadFltkThing(Rps_Type::PaylFltkWidget, obr, wid) {};
virtual const std::string payload_type_name(void) const
{
if (fltk_widget)
{
std::string typwidname = typeid(*fltk_widget).name();
return "FltkWidget/" + typwidname;
}
else
return "FltkWidget-nil";
};
virtual uint32_t wordsize(void) const
{
return sizeof(*this)/sizeof(void*);
};
virtual bool is_erasable(void) const
{
return false;
};
virtual ~Rps_PayloadFltkWidget()
{
delete fltk_widget;
};
Fl_Widget* get_widget(void) const
{
return fltk_widget;
};
}; // end class Rps_PayloadFltkWidget
/// temporary payload for a reference to any FLTK widget
class Rps_PayloadFltkRefWidget :
public Rps_PayloadFltkThing
{
friend Rps_PayloadFltkRefWidget*
Rps_QuasiZone::rps_allocate1<Rps_PayloadFltkRefWidget,Rps_ObjectZone*>(Rps_ObjectZone*);
friend Rps_PayloadFltkRefWidget*
Rps_QuasiZone::rps_allocate2<Rps_PayloadFltkRefWidget,Rps_ObjectZone*,Fl_Widget*>(Rps_ObjectZone*,Fl_Widget*);
inline Rps_PayloadFltkRefWidget(Rps_ObjectZone*owner, Fl_Widget*wid);
Rps_PayloadFltkRefWidget(Rps_ObjectRef obr)
: Rps_PayloadFltkThing(Rps_Type::PaylFltkRefWidget, obr, nullptr) {};
Rps_PayloadFltkRefWidget(Rps_ObjectRef obr, Fl_Widget*wid)
: Rps_PayloadFltkThing(Rps_Type::PaylFltkRefWidget, obr, wid)
{
};
virtual const std::string payload_type_name(void) const
{
if (fltk_widget)
{
std::string typwidname = typeid(*fltk_widget).name();
return "FltkRefWidget/" + typwidname;
}
else
return "FltkRefWidget-nil";
};
virtual uint32_t wordsize(void) const
{
return sizeof(*this)/sizeof(void*);
};
virtual bool is_erasable(void) const
{
return false;
};
virtual ~Rps_PayloadFltkRefWidget()
{
if (!owner()) return;
std::unique_lock<std::recursive_mutex> guown (*(owner()->objmtxptr()));
owner()->clear_payload();
//FIXME: should we call explicitly ~Callback_User_Data()?
};
Fl_Widget* get_widget(void) const
{
return fltk_widget;
};
}; // end class Rps_PayloadFltkRefWidget
Rps_PayloadFltkRefWidget::Rps_PayloadFltkRefWidget(Rps_ObjectZone*owner, Fl_Widget*wid)
: Rps_PayloadFltkRefWidget(Rps_ObjectRef(owner),wid)
{
if (wid)
{
RPS_ASSERT(owner);
wid->user_data(this);
}
} // end Rps_PayloadFltkRefWidget::Rps_PayloadFltkRefWidget
////////////////
class Rps_FltkInputTextEditor : public Fl_Text_Editor
{
/* inspired by FLTK 1.4 examples/texteditor-with-dynamic-colors.cxx;
it contains two data pointers with a mutex */
public:
typedef void datadestroyer_sigt(Rps_FltkInputTextEditor*,void*,void*);
private:
mutable std::recursive_mutex inputx_mtx;
void* inputx_data1;
void* inputx_data2;
datadestroyer_sigt* destroy_datafunptr;
std::function<datadestroyer_sigt> destroy_clos;
Fl_Text_Buffer *inputx_textbuf; // text buffer
Fl_Text_Buffer *inputx_stylbuf; // style buffer
public:
template<typename Ty> Ty* data1() const
{
std::lock_guard<std::recursive_mutex> gu(inputx_mtx);
return (Ty*)inputx_data1;
};
template<typename Ty> Ty* data2() const
{
std::lock_guard<std::recursive_mutex> gu(inputx_mtx);
return (Ty*)inputx_data2;
};
void set_data(datadestroyer_sigt*fun,void*p1,void*p2)
{
std::lock_guard<std::recursive_mutex> gu(inputx_mtx);
destroy_datafunptr = fun;
destroy_clos = nullptr;
inputx_data1 = p1;
inputx_data2 = p2;
};
void set_data_with_closure(std::function<datadestroyer_sigt> clos, void*p1, void*p2)
{
std::lock_guard<std::recursive_mutex> gu(inputx_mtx);
destroy_datafunptr = nullptr;
destroy_clos = clos;
inputx_data1 = p1;
inputx_data2 = p2;
};
void clear_data(void)
{
std::lock_guard<std::recursive_mutex> gu(inputx_mtx);
if (destroy_clos)
destroy_clos(this,inputx_data1,inputx_data2);
else if (destroy_datafunptr)
(*destroy_datafunptr)(this,inputx_data1,inputx_data2);
inputx_data1 = nullptr;
inputx_data2 = nullptr;
destroy_datafunptr = nullptr;
destroy_clos = nullptr;
};
Rps_FltkInputTextEditor(int x, int y, int w, int h);
virtual ~Rps_FltkInputTextEditor();
}; // end Rps_FltkInputTextEditor
////////////////
class Rps_FltkOutputTextDisplay : public Fl_Text_Display
{
/* inspired by FLTK 1.4 examples/texteditor-with-dynamic-colors.cxx */
Fl_Text_Buffer *outputx_textbuf; // text buffer
Fl_Text_Buffer *outputx_stylbuf; // style buffer
#warning Rps_FltkOutputTextDisplay need a lot more code
public:
Rps_FltkOutputTextDisplay(int x, int y, int w, int h);
virtual ~Rps_FltkOutputTextDisplay();
}; // end Rps_FltkOutputTextDisplay
////////////////
class Rps_FltkMainWindow: public Fl_Window
{
friend class Rps_FltkDebugWindow;
Fl_Menu_Bar* _mainwin_menubar;
std::array<std::shared_ptr<Fl_Menu_Item>,
(std::size_t)(2+(int)RPS_DEBUG__LAST)>
_mainwin_dbgmenuarr;
std::vector<std::string> _mainwin_stringvect;
std::vector<char*> _mainwin_cstrvect;
Fl_Pack*_mainwin_vpack;
Rps_FltkInputTextEditor* _mainwin_inptextedit;
Rps_FltkOutputTextDisplay* _mainwin_outputdisp;
bool _mainwin_closing;
void add_menu_item_for_debug_option(Rps_Debug dbg);
protected:
void fill_main_window(void);
void register_mainwin_string(const std::string&str);
static void main_menu_cb(Fl_Widget*w, void*data);
static void close_cb(Fl_Widget*w, void*data);
public:
bool is_closing() const
{
return _mainwin_closing;
};
Rps_FltkMainWindow(int x, int y, int w, int h, const char*title);
Rps_FltkMainWindow(int w, int h, const char*title);
const char* asprintf_mainwin(const char*fmt, ...)
__attribute__((format(printf, 2, 3))); /// since first arg is this
virtual ~Rps_FltkMainWindow();
}; // end Rps_FltkMainWindow;
Rps_FltkOutputTextDisplay::Rps_FltkOutputTextDisplay(int x, int y, int w, int h)
: Fl_Text_Display(x,y,w,h)
{
RPS_WARNOUT("unimplemented Rps_FltkOutputTextDisplay this@" << (void*)this
<<" x=" << x << ", y=" << y
<< ", w=" << w << ", h=" << h << std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "Rps_FltkOutputTextDisplay::Rps_FltkOutputTextDisplay"));
#warning unimplemented Rps_FltkOutputTextDisplay::Rps_FltkOutputTextDisplay
} // end Rps_FltkOutputTextDisplay::Rps_FltkOutputTextDisplay
Rps_FltkOutputTextDisplay::~Rps_FltkOutputTextDisplay()
{
RPS_WARNOUT("unimplemented ~Rps_FltkOutputTextDisplay this@" << (void*)this
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "Rps_FltkOutputTextDisplay::Rps_FltkOutputTextDisplay"));
#warning unimplemented Rps_FltkOutputTextDisplay::~Rps_FltkOutputTextDisplay
} // end Rps_FltkOutputTextDisplay::~Rps_FltkOutputTextDisplay
class Rps_FltkDebugWindow: public Fl_Window
{
friend class Rps_FltkMainWindow;
Fl_Menu_Bar* _dbgwin_menubar;
Fl_Tile* _dbgwin_tile;
Fl_Text_Buffer* _dbgwin_text_buffer;
Fl_Text_Display* _dbgwin_top_text_display;
Fl_Text_Display* _dbgwin_bottom_text_display;
char _dbgwin_labuf[80];
void fill_debug_window(void);
static void debug_menu_cb(Fl_Widget*w, void*data);
public:
static constexpr int min_width=200;
static constexpr int min_height=100;
Rps_FltkDebugWindow(int x, int y, int w, int h);
Rps_FltkDebugWindow(int w, int h);
virtual ~Rps_FltkDebugWindow();
}; // end Rps_FltkDebugWindow
////////////////////////////////////////////////////////////////
//////// ******* IMPLEMENTATION *******
Rps_FltkInputTextEditor::Rps_FltkInputTextEditor(int x, int y, int w, int h)
: Fl_Text_Editor(x,y,w,h)
{
RPS_DEBUG_LOG(REPL, "this @" << (void*)this << " x=" << x
<< ",y=" << y << ",w=" << w << " h="<< h);
}; // end Rps_FltkInputTextEditor::Rps_FltkInputTextEditor
Rps_FltkInputTextEditor::~Rps_FltkInputTextEditor()
{
RPS_DEBUG_LOG(REPL, "destr this @" << (void*)this);
} // end Rps_FltkInputTextEditor destructor
////////////////////////////////////////////////////////////////
Rps_PayloadFltkWidget::Rps_PayloadFltkWidget(Rps_ObjectZone*owner, Fl_Widget*wid)
: Rps_PayloadFltkWidget(Rps_ObjectRef(owner), wid) {};
void
rps_fltk_add_input_fd(int fd,
Rps_EventHandler_sigt* f,
const char* explanation,
int ix)
{
RPS_DEBUG_LOG(REPL, "rps_fltk_add_input_fd fd#" << fd
<< (explanation?" ":"") << (explanation?explanation:"")
<< " ix#" << ix << std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_fltk_add_input_fd"));
Fl::add_fd(fd, POLLIN, rps_fltk_input_fd_handler, (void*)(intptr_t)ix);
} // end rps_fltk_add_input_fd
Fl_Color
rps_fltk_color_by_name (const char*name, bool*ok)
{
Fl_Color color= FL_BLACK;
if (!name)
goto failure;
if (name[0]=='#')
{
int red= -1, green= -1, blue= -1;
int pos= -1;
if (sscanf(name, "#%02x%02x%02x%n", &red, &green, &blue, &pos)>=3
&& red>=0 && red<256 && green>=0 && green<256 && blue>=0
&& blue<256 && pos>4)
{
color = fl_rgb_color(red,green,blue);
goto success;
}
}
else if (!isalpha(name[0]))
goto failure;
#define RPS_RGB_COLOR(Red,Green,Blue,Name) \
else if (!strcmp(name,Name)) \
{ color = fl_rgb_color(Red,Green,Blue); goto success; }
#include "generated/rps-rgb-colors.hh"
#undef RPS_RGB_COLOR
#undef RPS_NB_RGB_COLORS
#undef RPS_WIDEST_RGB_COLOR
goto failure;
success:
if (ok)
*ok = true;
return color;
failure:
if (ok)
*ok = false;
return FL_BLACK;
} // end rps_fltk_color_by_name
void
rps_fltk_add_output_fd(int fd,
Rps_EventHandler_sigt* f,
const char* explanation,
int ix)
{
RPS_DEBUG_LOG(REPL, "rps_fltk_add_input_fd fd#" << fd
<< (explanation?" ":"") << (explanation?explanation:"")
<< " ix#" << ix << std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_fltk_add_input_fd"));
Fl::add_fd(fd, POLLOUT, rps_fltk_output_fd_handler, (void*)(intptr_t)ix);
} // end rps_fltk_add_input_fd
/* this gets called by FLTK event loop when some input is readable on
fd */
void
rps_fltk_input_fd_handler(FL_SOCKET fd, void *hdata)
{
int ix=(int)(intptr_t)hdata;
Rps_EventHandler_sigt*funptr=nullptr;
struct pollfd pe= {};
const char*expl=nullptr;
void*data=nullptr;
bool ok=rps_event_loop_get_entry(ix, &funptr, &pe, &expl, &data);
RPS_DEBUG_LOG(REPL, "rps_fltk_input_fd_handler fd#" << fd
<< " ix#" << ix
<< (ok?"OK":"BAD") << " funptr@" << (void*)funptr
<< (expl?" expl:": "NOEXPL")
<< (expl?expl:"")
<< " data@" << data
<< " thread:" << rps_current_pthread_name()
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_fltk_input_fd_handler")
);
if (!ok)
return;
if (pe.fd != fd)
return;
if (!funptr)
return;
/* NOTICE: think more about garbage collection interaction with FLTK
event loop. */
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED,
/*callerframe:*/rps_curthread_callframe, //
/** locals **/
Rps_Value v;
);
_f.v = nullptr;
(*funptr) (&_, fd, data);
RPS_DEBUG_LOG(REPL, "rps_fltk_input_fd_handler done fd#" << fd
<< " ix#" << ix <<std::endl);
} // end rps_fltk_input_fd_handler
/* this gets called by FLTK event loop when some input is readable on
fd */
void
rps_fltk_output_fd_handler(FL_SOCKET fd, void *hdata)
{
int ix=(int)(intptr_t)hdata;
Rps_EventHandler_sigt*funptr=nullptr;
struct pollfd pe= {};
const char*expl=nullptr;
void*data=nullptr;
bool ok=rps_event_loop_get_entry(ix, &funptr, &pe, &expl, &data);
RPS_DEBUG_LOG(REPL, "rps_fltk_output_fd_handler fd#" << fd
<< " ix#" << ix
<< (ok?"OK":"BAD") << " funptr@" << (void*)funptr
<< (expl?" expl:": "NOEXPL")
<< (expl?expl:"")
<< " data@" << data
<< " thread:" << rps_current_pthread_name()
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_fltk_output_fd_handler")
);
if (!ok)
return;
if (pe.fd != fd)
return;
if (!funptr)
return;
/* NOTICE: think more about garbage collection interaction with FLTK
event loop. */
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED,
/*callerframe:*/rps_curthread_callframe, //
/** locals **/
Rps_Value v;
);
_f.v = nullptr;
(*funptr) (&_, fd, data);
RPS_DEBUG_LOG(REPL, "rps_fltk_output_fd_handler done fd#" << fd
<< " ix#" << ix <<std::endl);
} // end rps_fltk_output_fd_handler
void
rps_fltk_remove_input_fd(int fd)
{
Fl::remove_fd(fd, POLLIN);
RPS_DEBUG_LOG(REPL, "rps_fltk_remove_input_fd fd#" << fd
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_fltk_remove_input_fd"));
} // end rps_fltk_remove_input_fd
void
rps_fltk_remove_output_fd(int fd)
{
Fl::remove_fd(fd, POLLOUT);
RPS_DEBUG_LOG(REPL, "rps_fltk_remove_output_fd fd#" << fd
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_fltk_remove_output_fd"));
} // end rps_fltk_remove_output_fd
Rps_PayloadFltkThing::Rps_PayloadFltkThing(Rps_ObjectZone*owner)
: Rps_Payload(Rps_Type::PaylFltkThing,owner), fltk_ptr(nullptr)
{
#warning incomplete Rps_PayloadFltkThing::Rps_PayloadFltkThing
} // end of Rps_PayloadFltkThing::Rps_PayloadFltkThing
Rps_PayloadFltkThing::~Rps_PayloadFltkThing()
{
#warning incomplete Rps_PayloadFltkThing::~Rps_PayloadFltkThing
} // end destructor Rps_PayloadFltkThing::~Rps_PayloadFltkThing
void
Rps_PayloadFltkThing::gc_mark(Rps_GarbageCollector&gc) const
{
#warning incomplete Rps_PayloadFltkThing::gc_mark
} // end of Rps_PayloadFltkThing::gc_mark
void
Rps_PayloadFltkThing::dump_scan(Rps_Dumper*du) const
{
RPS_ASSERT(du);
RPS_POSSIBLE_BREAKPOINT();
// do nothing, since temporary payload
} // end Rps_PayloadFltkThing::dump_scan
void
Rps_PayloadFltkThing::dump_json_content(Rps_Dumper*du, Json::Value&jv) const
{
RPS_ASSERT(du);
RPS_POSSIBLE_BREAKPOINT();
// do nothing, since temporary payload
} // end Rps_PayloadFltkThing::dump_json_content
////////////////
Rps_FltkMainWindow::Rps_FltkMainWindow(int x, int y, int w, int h, const char*title)
: Fl_Window(x,y,w,h,title),
_mainwin_menubar(nullptr),
_mainwin_dbgmenuarr(), _mainwin_stringvect(), _mainwin_cstrvect(),
_mainwin_vpack(nullptr),
_mainwin_inptextedit(nullptr),
_mainwin_outputdisp(nullptr),
_mainwin_closing(false)
{
constexpr int estimatenbstring = 20;
_mainwin_stringvect.reserve(estimatenbstring);
constexpr int estimatenbcstr = 32;
_mainwin_cstrvect.reserve(estimatenbcstr);
RPS_INFORMOUT("Rps_FltkMainWindow x=" << x << ",y=" << y
<< ",w=" << w << ",h=" << h
<< ",title=" << Rps_Cjson_String(title)
<< " @" << (void*)this);
RPS_DEBUG_LOG(REPL, "Rps_FltkMainWindow x=" << x << ",y=" << y
<< ",w=" << w << ",h=" << h
<< ",title=" << Rps_Cjson_String(title)
<< " @" << (void*)this
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "Rps_FltkMainWindow/xywh"));
fill_main_window();
};
Rps_FltkMainWindow::Rps_FltkMainWindow(int w, int h, const char*title)
: Rps_FltkMainWindow(10 + (Rps_Random::random_32u() % 32)*10,
10 + (Rps_Random::random_32u() % 32)*10,
w, h, title)
{
RPS_DEBUG_LOG(REPL, "Rps_FltkMainWindow w=" << w << ",h=" << h
<< ",title=" << Rps_Cjson_String(title)
<< " @" << (void*)this
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "Rps_FltkMainWindow/wh"));
}; // end Rps_FltkMainWindow::Rps_FltkMainWindow
// This internal function is needed for memory management purposes, to
// be sure the data inside the strings is kept allocated
void
Rps_FltkMainWindow::register_mainwin_string(const std::string&s)
{
_mainwin_stringvect.push_back(s);
} // end Rps_FltkMainWindow::register_mainwin_string
const char*
Rps_FltkMainWindow::asprintf_mainwin(const char*fmt, ...)
{
char*res = nullptr;
RPS_POSSIBLE_BREAKPOINT();
RPS_ASSERT(fmt);
va_list args;
va_start (args, fmt);
int l = vasprintf(&res, fmt, args);
va_end (args);
if (l >= 0 && res)
{
_mainwin_cstrvect.push_back(res);
RPS_DEBUG_LOG(REPL, "Rps_FltkMainWindow::asprintf_mainwin #"
<< _mainwin_cstrvect.size()-1 << "@" << (void*)res
<< '"' << Rps_Cjson_String(res) << '"' << std::endl);
return res;
}
RPS_FATALOUT("Rps_FltkMainWindow::asprintf_mainwin fmt="
<< "\'" << Rps_Cjson_String(fmt) << "\' failed %m");
} // end Rps_FltkMainWindow::asprintf_mainwin
void
Rps_FltkMainWindow::add_menu_item_for_debug_option(Rps_Debug dbglev)
{
RPS_POSSIBLE_BREAKPOINT();
const char* dbgitcstr = asprintf_mainwin("Debug/%s", rps_cstr_of_debug(dbglev));
const char* datacstr = asprintf_mainwin("d:%s", rps_cstr_of_debug(dbglev));
RPS_DEBUG_LOG(REPL, "Rps_FltkMainWindow::add_menu_item_for_debug_option dbglev#"
<< (int)dbglev << ":" << rps_cstr_of_debug(dbglev)
<< " dbgitcstr:" << dbgitcstr << "@" << ((void*)(dbgitcstr))
<< " datacstr:" << datacstr << "@" << ((void*)(datacstr))
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1,
"Rps_FltkMainWindow::add_menu_item_for_debug_option"));
int rk = _mainwin_menubar->add(dbgitcstr,
nullptr, main_menu_cb, (void*) datacstr,
FL_MENU_TOGGLE);
RPS_DEBUG_LOG(REPL, "Rps_FltkMainWindow::add_menu_item_for_debug_option rk="
<< rk);
Fl_Menu_Item*mitem = const_cast<Fl_Menu_Item*>(_mainwin_menubar->menu()+rk);
RPS_ASSERT(mitem != nullptr);
if (rps_debug_flags & (1 << unsigned(dbglev)))
mitem->set();
else
mitem->clear();
RPS_POSSIBLE_BREAKPOINT();
} // end Rps_FltkMainWindow::add_menu_item_for_debug_option
void
Rps_FltkMainWindow::fill_main_window(void)
{
RPS_DEBUG_LOG(REPL, "Rps_FltkMainWindow::fill_main_window"
<< " w=" << w() << ",h=" << h() << std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "Rps_FltkMainWindow/fill_main_window"));
this->begin();
//////////// the menubar
int menubar_w = w();
int menubar_h = 25;
{
const char*mainmenucolor = rps_get_extra_arg("fltk_main_menu_color");
_mainwin_menubar = new Fl_Menu_Bar(0, 0, menubar_w, menubar_h);
RPS_DEBUG_LOG(REPL, "Rps_FltkMainWindow::fill_main_window _mainwin_menubar@" << (void*)_mainwin_menubar
<< " mainmenucolor=" << Rps_Cjson_String(mainmenucolor)
<< " w=" << w() << " h=" << h()
<< " menubar_w=" << menubar_w
<< " menubar_h=" << menubar_h);
if (mainmenucolor)
{
bool ok=false;
Fl_Color col = rps_fltk_color_by_name(mainmenucolor, &ok);
if (ok && col != FL_BLACK)
{
RPS_DEBUG_LOG(REPL, "mainwinmenucolor:" << mainmenucolor
<< "=" << (long)col);
_mainwin_menubar->color(col);
}
}
_mainwin_menubar->add("&App/e&Xit", "^x", main_menu_cb, (void*)"X");
_mainwin_menubar->add("&App/&Quit", "^q", main_menu_cb, (void*)"Q");
_mainwin_menubar->add("&App/&Dump", "^d", main_menu_cb, (void*)"D");
_mainwin_menubar->add("&Debug/Stop", "^s", main_menu_cb, (void*)"d-");
_mainwin_menubar->add("&Debug/Clear", "^c", main_menu_cb, (void*)"d_");
_mainwin_menubar->add("&Debug/Sho&w", "^w", main_menu_cb, (void*)"d+");
///
#define Rps_FLTK_debug_option(Dbgopt) add_menu_item_for_debug_option(RPS_DEBUG_##Dbgopt);
RPS_DEBUG_OPTIONS(Rps_FLTK_debug_option);
#undef Rps_FLTK_debug_option
};
/////////////
_mainwin_vpack = new Fl_Pack(/*x:*/0, /*y:*/menubar_h+1,
/*w:*/w(), /*h:*/h()-(menubar_h+1));
_mainwin_vpack->begin();
int label_h = 13;
{
Fl_Widget*firstlabel = nullptr;
char*labelstr=nullptr;
char labelbuf[80];
memset(labelbuf, 0, sizeof(labelbuf));
/// in FLTK the @ is a magic escaping char, see www.fltk.org/doc-1.4/common.html#common_labels
snprintf(labelbuf, sizeof(labelbuf), "refpersys-%s/p%d@@%s",
rps_shortgitid, (int)getpid(), rps_hostname());
RPS_POSSIBLE_BREAKPOINT();
labelstr = strdup(labelbuf);
RPS_DEBUG_LOG(REPL, "fill_main_window labelstr:" << labelstr);
firstlabel = new Fl_Box(/*x:*/0,/*y:*/menubar_h,
/*w:*/w(),/*h:*/label_h,
labelstr);
const char*labelcolor = rps_get_extra_arg("fltk_label_color");
if (labelcolor)
{
bool ok=false;
Fl_Color col = rps_fltk_color_by_name(labelcolor, &ok);
if (ok && col != FL_BLACK)
{
RPS_DEBUG_LOG(REPL, "fill_main_window labelcolor=" << labelcolor
<< "=" << col);
firstlabel->color(col);
}
}
firstlabel->show();
}
int texteditheight = h()/2-(menubar_h+label_h+1);
int textedity = menubar_h+label_h+1;
const char*inputcolor = rps_get_extra_arg("fltk_input_color");
_mainwin_inptextedit
= new Rps_FltkInputTextEditor(/*x:*/0,/*y:*/textedity,
/*w:*/w(), /*h:*/texteditheight);
if (inputcolor)
{
bool ok=false;
Fl_Color col = rps_fltk_color_by_name(inputcolor, &ok);
if (ok && col != FL_BLACK)
{
RPS_DEBUG_LOG(REPL, "fill_main_window inputcolor=" << inputcolor
<< "=" << col);
_mainwin_inptextedit->color(col);
}
}
#warning _mainwin_inptextedit should have a different background color
_mainwin_vpack->add(_mainwin_inptextedit);
_mainwin_outputdisp =
new Rps_FltkOutputTextDisplay(/*x:*/0, textedity+texteditheight+1,
/*w:*/w(), /*h:*/h()-( textedity+texteditheight+1));
_mainwin_vpack->add(_mainwin_outputdisp);
_mainwin_vpack->end();
_mainwin_inptextedit->show();
_mainwin_vpack->show();
this->end();
callback(close_cb, nullptr);
RPS_POSSIBLE_BREAKPOINT();
RPS_DEBUG_LOG(REPL, "Rps_FltkMainWindow::fill_main_window done w="
<< w() << ",h=" << h()
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "Rps_FltkMainWindow::fill_main_window-"));
} // end Rps_FltkMainWindow::fill_main_window
Rps_FltkMainWindow::~Rps_FltkMainWindow()
{
RPS_POSSIBLE_BREAKPOINT();
size_t nbstring = _mainwin_stringvect.size();
_mainwin_stringvect.clear();
size_t nbcstr = _mainwin_cstrvect.size();
for (int i=0; i<(int)nbcstr; i++)
{
RPS_ASSERT(_mainwin_cstrvect[i]);
_mainwin_cstrvect[i][0] = (char)0;
free (_mainwin_cstrvect[i]);
_mainwin_cstrvect[i] = nullptr;
};
_mainwin_cstrvect.clear();
RPS_DEBUG_LOG(REPL, "~Rps_FltkMainWindow @" << (void*)this
<< " nbstring=" << nbstring << " nbcstr=" << nbcstr
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "~Rps_FltkMainWindow"));
RPS_POSSIBLE_BREAKPOINT();
}; // end Rps_FltkMainWindow::~Rps_FltkMainWindow
void
Rps_FltkMainWindow::main_menu_cb(Fl_Widget*w, void*data)
{
/// the widget w is the menu bar
RPS_ASSERT((void*)rps_fltk_mainwin);
RPS_ASSERT(rps_fltk_mainwin->_mainwin_menubar != nullptr);
RPS_ASSERT((void*)w == (void*)rps_fltk_mainwin->_mainwin_menubar);
int logicalsize = rps_fltk_mainwin->_mainwin_menubar->size();
RPS_DEBUG_LOG(REPL, "Rps_FltkMainWindow::main_menu_cb w@" << (void*)w
<< " _mainwin_menubar@" << (void*)rps_fltk_mainwin->_mainwin_menubar
<< " of type:" << (unsigned)(w->type())
<< " data@" << data << "=" << (const char*)data
<< " logicalsize=" << logicalsize
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "Rps_FltkMainWindow::main_menu_cb"));
RPS_ASSERT(rps_is_main_thread());
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED,
/*callerframe:*/rps_curthread_callframe, //
/** locals **/
Rps_Value v;
);
if (!strcmp((const char*)data, "X")) // eXit
{
RPS_DEBUG_LOG(REPL, "Rps_FltkMainWindow::main_menu_cb eXit");
rps_dump_into(".", &_);
rps_fltk_stop();
}
else if (!strcmp((const char*)data, "Q")) // Quit
{
RPS_DEBUG_LOG(REPL, "Rps_FltkMainWindow::main_menu_cb Quit");
rps_fltk_stop();
}
else if (!strcmp((const char*)data, "D")) // Dump
{
RPS_DEBUG_LOG(REPL, "Rps_FltkMainWindow::main_menu_cb Dump");
rps_dump_into(".", &_);
}
else if (!strcmp((const char*)data, "d-")) // debug stop
{
#warning unimplemented debug stop
RPS_WARNOUT("unimplemented debug stop rps_fltk_debugwin@"
<< (void*)rps_fltk_debugwin
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "main_menu_cb debug stop"));
}
else if (!strcmp((const char*)data, "d+")) // debug show
{
#warning unimplemented debug show
RPS_DEBUG_LOG(REPL, "main_menu_cb debug show rps_fltk_debugwin@"
<< (void*)rps_fltk_debugwin);
if (!rps_fltk_debugwin)
rps_fltk_debugwin = new Rps_FltkDebugWindow(670,480);
rps_fltk_debugwin->show();
RPS_WARNOUT("unimplemented debug show rps_fltk_debugwin@" << (void*)rps_fltk_debugwin
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "main_menu_cb debug show"));
}
else if (!strcmp((const char*)data, "d_")) // debug clear
{
#warning unimplemented debug clear
RPS_WARNOUT("unimplemented debug clear rps_fltk_debugwin@"
<< (void*)rps_fltk_debugwin << " data=" << (const char*)data);
}
else if (!strncmp((const char*)data, "d:", 2)
&& isalpha(((const char*)data)[2])) // debug set
{
Rps_Debug dbgopt = rps_debug_of_string(std::string{(const char*)data+2});
if (dbgopt > RPS_DEBUG__NONE)
{
#warning unimplemented main_menu_cb to set or clear debug option
}
}
else
{
#warning unimplemented main_menu_cb
RPS_WARNOUT("unimplemented main_menu_cb " << (const char*)data
<< RPS_FULL_BACKTRACE_HERE(1, "Rps_FltkMainWindow::main_menu_cb strange"));
}
} // end Rps_FltkMainWindow::main_menu_cb
void
Rps_FltkMainWindow::close_cb(Fl_Widget*wid, void*data)
{
RPS_DEBUG_LOG(REPL, "Rps_FltkMainWindow::close_cb wid@" << (void*)wid
<< " data@" << data
<< " elapsed:" << rps_elapsed_real_time()
<< RPS_FULL_BACKTRACE_HERE(1, "Rps_FltkMainWindow::close_cb"));
if (RPS_DEBUG_ENABLED(REPL))
{
char pmapcmd[128];
memset(pmapcmd, 0, sizeof(pmapcmd));
snprintf(pmapcmd, sizeof(pmapcmd), "/usr/bin/pmap %d; /usr/bin/ps -lw %d",
(int)getpid(), (int)getpid());
fflush (nullptr);
std::clog << std::flush;
std::cerr << std::flush;
std::cout << std::flush;
if (system(pmapcmd))
RPS_FATALOUT("failed to run " << pmapcmd);
};
rps_fltk_mainwin->_mainwin_closing = true;
rps_do_stop_event_loop();