FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
solvespace/src/gtk/gtkmain.cpp at python · realthunder/solvespace · GitHub
realthunder
solvespace
Repository navigation
Code
Issues
3
(3)
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
solvespace
/
src
/
gtk
/
gtkmain.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
1611 lines (1290 loc) · 45.2 KB
Breadcrumbs
solvespace
/
src
/
gtk
/
gtkmain.cpp
Copy path
File metadata and controls
1611 lines (1290 loc) · 45.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
-----------------------------------------------------------------------------
//
Our main() function, and GTK3-specific stuff to set up our windows and
//
otherwise handle our interface to the operating system. Everything
//
outside gtk/... should be standard C++ and OpenGL.
//
//
Copyright 2015 <whitequark@whitequark.org>
//
-----------------------------------------------------------------------------
#
include
<
errno.h
>
#
include
<
sys/stat.h
>
#
include
<
unistd.h
>
#
include
<
time.h
>
#
include
<
iostream
>
#
include
<
json-c/json_object.h
>
#
include
<
json-c/json_util.h
>
#
include
<
glibmm/main.h
>
#
include
<
glibmm/convert.h
>
#
include
<
giomm/file.h
>
#
include
<
gdkmm/cursor.h
>
#
include
<
gtkmm/drawingarea.h
>
#
include
<
gtkmm/scrollbar.h
>
#
include
<
gtkmm/entry.h
>
#
include
<
gtkmm/eventbox.h
>
#
include
<
gtkmm/fixed.h
>
#
include
<
gtkmm/adjustment.h
>
#
include
<
gtkmm/separatormenuitem.h
>
#
include
<
gtkmm/menuitem.h
>
#
include
<
gtkmm/checkmenuitem.h
>
#
include
<
gtkmm/radiomenuitem.h
>
#
include
<
gtkmm/radiobuttongroup.h
>
#
include
<
gtkmm/menu.h
>
#
include
<
gtkmm/menubar.h
>
#
include
<
gtkmm/scrolledwindow.h
>
#
include
<
gtkmm/filechooserdialog.h
>
#
include
<
gtkmm/messagedialog.h
>
#
include
<
gtkmm/main.h
>
#
if
HAVE_GTK3
#
include
<
gtkmm/hvbox.h
>
#
else
#
include
<
gtkmm/box.h
>
#
endif
#
include
<
cairomm/xlib_surface.h
>
#
include
<
pangomm/fontdescription.h
>
#
include
<
gdk/gdkx.h
>
#
include
<
fontconfig/fontconfig.h
>
#
include
<
GL/glx.h
>
#
include
"
solvespace.h
"
#
include
"
config.h
"
#
include
"
../unix/gloffscreen.h
"
#
ifdef
HAVE_SPACEWARE
#
include
<
spnav.h
>
#
endif
namespace
SolveSpace
{
/*
Settings
*/
/*
Why not just use GSettings? Two reasons. It doesn't allow to easily see
whether the setting had the default value, and it requires to install
a schema globally.
*/
static
json_object *settings =
NULL
;
static
std::string
CnfPrepare
() {
//
Refer to http://standards.freedesktop.org/basedir-spec/latest/
std::string dir;
if
(
getenv
(
"
XDG_CONFIG_HOME
"
)) {
dir =
std::string
(
getenv
(
"
XDG_CONFIG_HOME
"
)) +
"
/solvespace
"
;
}
else
if
(
getenv
(
"
HOME
"
)) {
dir =
std::string
(
getenv
(
"
HOME
"
)) +
"
/.config/solvespace
"
;
}
else
{
dbp
(
"
neither XDG_CONFIG_HOME nor HOME are set
"
);
return
"
"
;
}
struct
stat
st;
if
(
stat
(dir.
c_str
(), &st)) {
if
(errno ==
ENOENT
) {
if
(
mkdir
(dir.
c_str
(),
0777
)) {
dbp
(
"
cannot mkdir %s: %s
"
, dir.
c_str
(),
strerror
(errno));
return
"
"
;
}
}
else
{
dbp
(
"
cannot stat %s: %s
"
, dir.
c_str
(),
strerror
(errno));
return
"
"
;
}
}
else
if
(!
S_ISDIR
(st.
st_mode
)) {
dbp
(
"
%s is not a directory
"
, dir.
c_str
());
return
"
"
;
}
return
dir +
"
/settings.json
"
;
}
static
void
CnfLoad
() {
std::string path =
CnfPrepare
();
if
(path.
empty
())
return
;
if
(settings)
json_object_put
(settings);
//
deallocate
settings =
json_object_from_file
(path.
c_str
());
if
(!settings) {
if
(errno !=
ENOENT
)
dbp
(
"
cannot load settings: %s
"
,
strerror
(errno));
settings =
json_object_new_object
();
}
}
static
void
CnfSave
() {
std::string path =
CnfPrepare
();
if
(path.
empty
())
return
;
/*
json-c <0.12 has the first argument non-const here
*/
if
(
json_object_to_file_ext
((
char
*) path.
c_str
(), settings,
JSON_C_TO_STRING_PRETTY
))
dbp
(
"
cannot save settings: %s
"
,
strerror
(errno));
}
void
CnfFreezeInt
(
uint32_t
val,
const
std::string &key) {
struct
json_object
*jval =
json_object_new_int
(val);
json_object_object_add
(settings, key.
c_str
(), jval);
CnfSave
();
}
uint32_t
CnfThawInt
(
uint32_t
val,
const
std::string &key) {
struct
json_object
*jval;
if
(
json_object_object_get_ex
(settings, key.
c_str
(), &jval))
return
json_object_get_int
(jval);
else
return
val;
}
void
CnfFreezeFloat
(
float
val,
const
std::string &key) {
struct
json_object
*jval =
json_object_new_double
(val);
json_object_object_add
(settings, key.
c_str
(), jval);
CnfSave
();
}
float
CnfThawFloat
(
float
val,
const
std::string &key) {
struct
json_object
*jval;
if
(
json_object_object_get_ex
(settings, key.
c_str
(), &jval))
return
json_object_get_double
(jval);
else
return
val;
}
void
CnfFreezeString
(
const
std::string &val,
const
std::string &key) {
struct
json_object
*jval =
json_object_new_string
(val.
c_str
());
json_object_object_add
(settings, key.
c_str
(), jval);
CnfSave
();
}
std::string
CnfThawString
(
const
std::string &val,
const
std::string &key) {
struct
json_object
*jval;
if
(
json_object_object_get_ex
(settings, key.
c_str
(), &jval))
return
json_object_get_string
(jval);
return
val;
}
static
void
CnfFreezeWindowPos
(Gtk::Window *win,
const
std::string &key) {
int
x, y, w, h;
win->
get_position
(x, y);
win->
get_size
(w, h);
CnfFreezeInt
(x, key +
"
_left
"
);
CnfFreezeInt
(y, key +
"
_top
"
);
CnfFreezeInt
(w, key +
"
_width
"
);
CnfFreezeInt
(h, key +
"
_height
"
);
}
static
void
CnfThawWindowPos
(Gtk::Window *win,
const
std::string &key) {
int
x, y, w, h;
win->
get_position
(x, y);
win->
get_size
(w, h);
x =
CnfThawInt
(x, key +
"
_left
"
);
y =
CnfThawInt
(y, key +
"
_top
"
);
w =
CnfThawInt
(w, key +
"
_width
"
);
h =
CnfThawInt
(h, key +
"
_height
"
);
win->
move
(x, y);
win->
resize
(w, h);
}
/*
Timers
*/
int64_t
GetMilliseconds
(
void
) {
struct
timespec
ts;
clock_gettime
(
CLOCK_MONOTONIC
, &ts);
return
1000
* (
uint64_t
) ts.
tv_sec
+ ts.
tv_nsec
/
1000000
;
}
static
bool
TimerCallback
() {
SS
.
GW
.
TimerCallback
();
SS
.
TW
.
TimerCallback
();
return
false
;
}
void
SetTimerFor
(
int
milliseconds) {
Glib::signal_timeout
().
connect
(&TimerCallback, milliseconds);
}
static
bool
AutosaveTimerCallback
() {
SS
.
Autosave
();
return
false
;
}
void
SetAutosaveTimerFor
(
int
minutes) {
Glib::signal_timeout
().
connect
(&AutosaveTimerCallback, minutes *
60
*
1000
);
}
static
bool
LaterCallback
() {
SS
.
DoLater
();
return
false
;
}
void
ScheduleLater
() {
Glib::signal_idle
().
connect
(&LaterCallback);
}
/*
GL wrapper
*/
#
define
GL_CHECK
() \
do
{ \
int
err = (
int
)
glGetError
(); \
if
(err)
dbp
(
"
%s:%d: glGetError() == 0x%X %s
"
, \
__FILE__, __LINE__, err,
gluErrorString
(err)); \
}
while
(
0
)
class
GlWidget
:
public
Gtk
::DrawingArea {
public:
GlWidget
() : _offscreen(
NULL
) {
_xdisplay =
gdk_x11_get_default_xdisplay
();
int
glxmajor, glxminor;
if
(!
glXQueryVersion
(_xdisplay, &glxmajor, &glxminor)) {
dbp
(
"
OpenGL is not supported
"
);
oops
();
}
if
(glxmajor <
1
|| (glxmajor ==
1
&& glxminor <
3
)) {
dbp
(
"
GLX version %d.%d is too old; 1.3 required
"
, glxmajor, glxminor);
oops
();
}
static
int
fbconfig_attrs[] = {
GLX_RENDER_TYPE
,
GLX_RGBA_BIT
,
GLX_RED_SIZE
,
8
,
GLX_GREEN_SIZE
,
8
,
GLX_BLUE_SIZE
,
8
,
GLX_DEPTH_SIZE
,
24
,
None
};
int
fbconfig_num =
0
;
GLXFBConfig *fbconfigs =
glXChooseFBConfig
(_xdisplay,
DefaultScreen
(_xdisplay),
fbconfig_attrs, &fbconfig_num);
if
(!fbconfigs || fbconfig_num ==
0
)
oops
();
/*
prefer FBConfigs with depth of 32;
* Mesa software rasterizer explodes with a BadMatch without this;
* without this, Intel on Mesa flickers horribly for some reason.
this does not seem to affect other rasterizers (ie NVidia).
see this Mesa bug:
http://lists.freedesktop.org/archives/mesa-dev/2015-January/074693.html
*/
GLXFBConfig fbconfig = fbconfigs[
0
];
for
(
int
i =
0
; i < fbconfig_num; i++) {
XVisualInfo *visual_info =
glXGetVisualFromFBConfig
(_xdisplay, fbconfigs[i]);
/*
some GL visuals, notably on Chromium GL, do not have an associated
X visual; this is not an obstacle as we always render offscreen.
*/
if
(!visual_info)
continue
;
int
depth = visual_info->
depth
;
XFree
(visual_info);
if
(depth ==
32
) {
fbconfig = fbconfigs[i];
break
;
}
}
_glcontext =
glXCreateNewContext
(_xdisplay,
fbconfig,
GLX_RGBA_TYPE
,
0
, True);
if
(!_glcontext) {
dbp
(
"
cannot create OpenGL context
"
);
oops
();
}
XFree
(fbconfigs);
/*
create a dummy X window to create a rendering context against.
we could use a Pbuffer, but some implementations (Chromium GL)
don't support these. we could use an existing window, but
some implementations (Chromium GL... do you see a pattern?)
do really strange things, i.e. draw a black rectangle on
the very front of the desktop if you do this.
*/
_xwindow =
XCreateSimpleWindow
(_xdisplay,
XRootWindow
(_xdisplay,
gdk_x11_get_default_screen
()),
/*
x
*/
0
,
/*
y
*/
0
,
/*
width
*/
1
,
/*
height
*/
1
,
/*
border_width
*/
0
,
/*
border
*/
0
,
/*
background
*/
0
);
}
~GlWidget
() {
glXMakeCurrent
(_xdisplay, None,
NULL
);
XDestroyWindow
(_xdisplay, _xwindow);
delete
_offscreen;
glXDestroyContext
(_xdisplay, _glcontext);
}
protected:
/*
Draw on a GLX framebuffer object, then read pixels out and draw them on
the Cairo context. Slower, but you get to overlay nice widgets.
*/
virtual
bool
on_draw
(
const
Cairo::RefPtr<Cairo::Context> &cr) {
if
(!
glXMakeCurrent
(_xdisplay, _xwindow, _glcontext))
oops
();
if
(!_offscreen)
_offscreen =
new
GLOffscreen;
Gdk::Rectangle allocation =
get_allocation
();
if
(!_offscreen->
begin
(allocation.
get_width
(), allocation.
get_height
()))
oops
();
on_gl_draw
();
glFlush
();
GL_CHECK
();
Cairo::RefPtr<Cairo::ImageSurface> surface =
Cairo::ImageSurface::create
(
_offscreen->
end
(), Cairo::
FORMAT_RGB24
,
allocation.
get_width
(), allocation.
get_height
(), allocation.
get_width
() *
4
);
cr->
set_source
(surface,
0
,
0
);
cr->
paint
();
surface->
finish
();
return
true
;
}
#
ifdef
HAVE_GTK2
virtual
bool
on_expose_event
(GdkEventExpose *) {
return
on_draw
(
get_window
()->
create_cairo_context
());
}
#
endif
virtual
void
on_gl_draw
() = 0;
private:
Display *_xdisplay;
GLXContext _glcontext;
GLOffscreen *_offscreen;
::Window _xwindow;
};
/*
Editor overlay
*/
class
EditorOverlay
:
public
Gtk
::Fixed {
public:
EditorOverlay
(Gtk::Widget &underlay) : _underlay(underlay) {
set_size_request
(
0
,
0
);
add
(_underlay);
_entry.
set_no_show_all
(
true
);
_entry.
set_has_frame
(
false
);
add
(_entry);
_entry.
signal_activate
().
connect
(
sigc::mem_fun
(
this
, &EditorOverlay::on_activate));
}
void
start_editing
(
int
x,
int
y,
int
font_height,
bool
is_monospace,
int
minWidthChars,
const
std::string &val) {
Pango::FontDescription font_desc;
font_desc.
set_family
(is_monospace ?
"
monospace
"
:
"
normal
"
);
font_desc.
set_absolute_size
(font_height * Pango::
SCALE
);
#
ifdef
HAVE_GTK3
/*
For some reason override_font doesn't take screen DPI into
account on GTK3 when working with font descriptors specified
in absolute sizes; modify_font does on GTK2.
*/
Pango::FontDescription
override_font_desc
(font_desc);
double
dpi =
get_screen
()->
get_resolution
();
override_font_desc.
set_size
(font_height *
72.0
/ dpi * Pango::
SCALE
);
_entry.
override_font
(override_font_desc);
#
else
_entry.
modify_font
(font_desc);
#
endif
/*
y coordinate denotes baseline
*/
Pango::FontMetrics font_metrics =
get_pango_context
()->
get_metrics
(font_desc);
y -= font_metrics.
get_ascent
() / Pango::
SCALE
;
Glib::RefPtr<Pango::Layout> layout =
Pango::Layout::create
(
get_pango_context
());
layout->
set_font_description
(font_desc);
layout->
set_text
(val +
"
"
);
/*
avoid scrolling
*/
int
width = layout->
get_logical_extents
().
get_width
();
#
ifdef
HAVE_GTK3
Gtk::Border border = _entry.
get_style_context
()->
get_padding
();
move
(_entry, x - border.
get_left
(), y - border.
get_top
());
_entry.
set_width_chars
(minWidthChars);
_entry.
set_size_request
(width / Pango::
SCALE
, -
1
);
#
else
/*
We need _gtk_entry_effective_inner_border, but it's not
in the public API, so emulate its logic.
*/
Gtk::Border border = {
2
,
2
,
2
,
2
}, *style_border;
gtk_widget_style_get
(
GTK_WIDGET
(_entry.
gobj
()),
"
inner-border
"
,
&style_border,
NULL
);
if
(style_border) border = *style_border;
move
(_entry, x - border.
left
, y - border.
top
);
/*
This is what set_width_chars does.
*/
int
minWidth = minWidthChars *
std::max
(font_metrics.
get_approximate_digit_width
(),
font_metrics.
get_approximate_char_width
());
_entry.
set_size_request
(
std::max
(width, minWidth) / Pango::
SCALE
, -
1
);
#
endif
_entry.
set_text
(val);
if
(!_entry.
is_visible
()) {
_entry.
show
();
_entry.
grab_focus
();
_entry.
add_modal_grab
();
}
}
void
stop_editing
() {
if
(_entry.
is_visible
())
_entry.
remove_modal_grab
();
_entry.
hide
();
}
bool
is_editing
()
const
{
return
_entry.
is_visible
();
}
sigc::signal<
void
, Glib::ustring>
signal_editing_done
() {
return
_signal_editing_done;
}
Gtk::Entry &
get_entry
() {
return
_entry;
}
protected:
virtual
bool
on_key_press_event
(GdkEventKey *event) {
if
(event->
keyval
== GDK_KEY_Escape) {
stop_editing
();
return
true
;
}
return
false
;
}
virtual
void
on_size_allocate
(Gtk::Allocation& allocation) {
Gtk::Fixed::on_size_allocate
(allocation);
_underlay.
size_allocate
(allocation);
}
virtual
void
on_activate
() {
_signal_editing_done
(_entry.
get_text
());
}
private:
Gtk::Widget &_underlay;
Gtk::Entry _entry;
sigc::signal<
void
, Glib::ustring> _signal_editing_done;
};
/*
Graphics window
*/
int
DeltaYOfScrollEvent
(GdkEventScroll *event) {
#
ifdef
HAVE_GTK3
int
delta_y = event->
delta_y
;
#
else
int
delta_y =
0
;
#
endif
if
(delta_y ==
0
) {
switch
(event->
direction
) {
case
GDK_SCROLL_UP
:
delta_y = -
1
;
break
;
case
GDK_SCROLL_DOWN
:
delta_y =
1
;
break
;
default
:
/*
do nothing
*/
return
false
;
}
}
return
delta_y;
}
class
GraphicsWidget
:
public
GlWidget
{
public:
GraphicsWidget
() {
set_events
(Gdk::
POINTER_MOTION_MASK
|
Gdk::
BUTTON_PRESS_MASK
| Gdk::
BUTTON_RELEASE_MASK
| Gdk::
BUTTON_MOTION_MASK
|
Gdk::
SCROLL_MASK
|
Gdk::
LEAVE_NOTIFY_MASK
);
set_double_buffered
(
true
);
}
protected:
virtual
bool
on_configure_event
(GdkEventConfigure *event) {
_w = event->
width
;
_h = event->
height
;
return
GlWidget::on_configure_event
(event);;
}
virtual
void
on_gl_draw
() {
SS
.
GW
.
Paint
();
}
virtual
bool
on_motion_notify_event
(GdkEventMotion *event) {
int
x, y;
ij_to_xy
(event->
x
, event->
y
, x, y);
SS
.
GW
.
MouseMoved
(x, y,
event->
state
&
GDK_BUTTON1_MASK
,
event->
state
&
GDK_BUTTON2_MASK
,
event->
state
&
GDK_BUTTON3_MASK
,
event->
state
&
GDK_SHIFT_MASK
,
event->
state
&
GDK_CONTROL_MASK
);
return
true
;
}
virtual
bool
on_button_press_event
(GdkEventButton *event) {
int
x, y;
ij_to_xy
(event->
x
, event->
y
, x, y);
switch
(event->
button
) {
case
1
:
if
(event->
type
==
GDK_BUTTON_PRESS
)
SS
.
GW
.
MouseLeftDown
(x, y);
else
if
(event->
type
==
GDK_2BUTTON_PRESS
)
SS
.
GW
.
MouseLeftDoubleClick
(x, y);
break
;
case
2
:
case
3
:
SS
.
GW
.
MouseMiddleOrRightDown
(x, y);
break
;
}
return
true
;
}
virtual
bool
on_button_release_event
(GdkEventButton *event) {
int
x, y;
ij_to_xy
(event->
x
, event->
y
, x, y);
switch
(event->
button
) {
case
1
:
SS
.
GW
.
MouseLeftUp
(x, y);
break
;
case
3
:
SS
.
GW
.
MouseRightUp
(x, y);
break
;
}
return
true
;
}
virtual
bool
on_scroll_event
(GdkEventScroll *event) {
int
x, y;
ij_to_xy
(event->
x
, event->
y
, x, y);
SS
.
GW
.
MouseScroll
(x, y, -
DeltaYOfScrollEvent
(event));
return
true
;
}
virtual
bool
on_leave_notify_event
(GdkEventCrossing *) {
SS
.
GW
.
MouseLeave
();
return
true
;
}
private:
int
_w, _h;
void
ij_to_xy
(
int
i,
int
j,
int
&x,
int
&y) {
//
Convert to xy (vs. ij) style coordinates,
//
with (0, 0) at center
x = i - _w /
2
;
y = _h /
2
- j;
}
};
class
GraphicsWindowGtk
:
public
Gtk
::Window {
public:
GraphicsWindowGtk
() : _overlay(_widget), _is_fullscreen(
false
) {
set_default_size
(
900
,
600
);
_box.
pack_start
(_menubar,
false
,
true
);
_box.
pack_start
(_overlay,
true
,
true
);
add
(_box);
_overlay.
signal_editing_done
().
connect
(
sigc::mem_fun
(
this
, &GraphicsWindowGtk::on_editing_done));
}
GraphicsWidget &
get_widget
() {
return
_widget;
}
EditorOverlay &
get_overlay
() {
return
_overlay;
}
Gtk::MenuBar &
get_menubar
() {
return
_menubar;
}
bool
is_fullscreen
()
const
{
return
_is_fullscreen;
}
bool
emulate_key_press
(GdkEventKey *event) {
return
on_key_press_event
(event);
}
protected:
virtual
void
on_show
() {
Gtk::Window::on_show
();
CnfThawWindowPos
(
this
,
"
GraphicsWindow
"
);
}
virtual
void
on_hide
() {
CnfFreezeWindowPos
(
this
,
"
GraphicsWindow
"
);
Gtk::Window::on_hide
();
}
virtual
bool
on_delete_event
(GdkEventAny *) {
if
(!
SS
.
OkayToStartNewFile
())
return
true
;
SS
.
Exit
();
return
true
;
}
virtual
bool
on_window_state_event
(GdkEventWindowState *event) {
_is_fullscreen = event->
new_window_state
&
GDK_WINDOW_STATE_FULLSCREEN
;
/*
The event arrives too late for the caller of ToggleFullScreen
to notice state change; and it's possible that the WM will
refuse our request, so we can't just toggle the saved state
*/
SS
.
GW
.
EnsureValidActives
();
return
Gtk::Window::on_window_state_event
(event);
}
virtual
bool
on_key_press_event
(GdkEventKey *event) {
int
chr;
switch
(event->
keyval
) {
case
GDK_KEY_Escape:
chr = GraphicsWindow::
ESCAPE_KEY
;
break
;
case
GDK_KEY_Delete:
chr = GraphicsWindow::
DELETE_KEY
;
break
;
case
GDK_KEY_Tab:
chr =
'
\t
'
;
break
;
case
GDK_KEY_BackSpace:
case
GDK_KEY_Back:
chr =
'
\b
'
;
break
;
default
:
if
(event->
keyval
>=
GDK_KEY_F1
&& event->
keyval
<=
GDK_KEY_F12
) {
chr = GraphicsWindow::
FUNCTION_KEY_BASE
+ (event->
keyval
-
GDK_KEY_F1
);
}
else
{
chr =
gdk_keyval_to_unicode
(event->
keyval
);
}
}
if
(event->
state
&
GDK_SHIFT_MASK
){
chr |= GraphicsWindow::
SHIFT_MASK
;
}
if
(event->
state
&
GDK_CONTROL_MASK
) {
chr |= GraphicsWindow::
CTRL_MASK
;
}
if
(chr &&
SS
.
GW
.
KeyDown
(chr)) {
return
true
;
}
if
(chr ==
'
\t
'
) {
//
Workaround for https://bugzilla.gnome.org/show_bug.cgi?id=123994.
GraphicsWindow::MenuView
(GraphicsWindow::
MNU_SHOW_TEXT_WND
);
return
true
;
}
return
Gtk::Window::on_key_press_event
(event);
}
virtual
void
on_editing_done
(Glib::ustring value) {
SS
.
GW
.
EditControlDone
(value.
c_str
());
}
private:
GraphicsWidget _widget;
EditorOverlay _overlay;
Gtk::MenuBar _menubar;
Gtk::VBox _box;
bool
_is_fullscreen;
};
std::unique_ptr<GraphicsWindowGtk>
GW
;
void
GetGraphicsWindowSize
(
int
*w,
int
*h) {
Gdk::Rectangle allocation =
GW
->
get_widget
().
get_allocation
();
*w = allocation.
get_width
();
*h = allocation.
get_height
();
}
void
InvalidateGraphics
(
void
) {
GW
->
get_widget
().
queue_draw
();
}
void
PaintGraphics
(
void
) {
GW
->
get_widget
().
queue_draw
();
/*
Process animation
*/
Glib::MainContext::get_default
()->
iteration
(
false
);
}
void
SetCurrentFilename
(
const
std::string &filename) {
if
(!filename.
empty
()) {
GW
->
set_title
(
"
SolveSpace -
"
+ filename);
}
else
{
GW
->
set_title
(
"
SolveSpace - (not yet saved)
"
);
}
}
void
ToggleFullScreen
(
void
) {
if
(
GW
->
is_fullscreen
())
GW
->
unfullscreen
();
else
GW
->
fullscreen
();
}
bool
FullScreenIsActive
(
void
) {
return
GW
->
is_fullscreen
();
}
void
ShowGraphicsEditControl
(
int
x,
int
y,
int
fontHeight,
int
minWidthChars,
const
std::string &val) {
Gdk::Rectangle rect =
GW
->
get_widget
().
get_allocation
();
//
Convert to ij (vs. xy) style coordinates,
//
and compensate for the input widget height due to inverse coord
int
i, j;
i = x + rect.
get_width
() /
2
;
j = -y + rect.
get_height
() /
2
;
GW
->
get_overlay
().
start_editing
(i, j, fontHeight,
/*
is_monospace=
*/
false
, minWidthChars, val);
}
void
HideGraphicsEditControl
(
void
) {
GW
->
get_overlay
().
stop_editing
();
}
bool
GraphicsEditControlIsVisible
(
void
) {
return
GW
->
get_overlay
().
is_editing
();
}
/*
TODO: removing menubar breaks accelerators.
*/
void
ToggleMenuBar
(
void
) {
GW
->
get_menubar
().
set_visible
(!
GW
->
get_menubar
().
is_visible
());
}
bool
MenuBarIsVisible
(
void
) {
return
GW
->
get_menubar
().
is_visible
();
}
/*
Context menus
*/
class
ContextMenuItem
:
public
Gtk
::MenuItem {
public:
static
int
choice;
ContextMenuItem
(
const
Glib::ustring &label,
int
id,
bool
mnemonic=
false
) :
Gtk::MenuItem
(label, mnemonic), _id(id) {
}
protected:
virtual
void
on_activate
() {
Gtk::MenuItem::on_activate
();
if
(
has_submenu
())
return
;
choice = _id;
}
/*
Workaround for https://bugzilla.gnome.org/show_bug.cgi?id=695488.
This is used in addition to on_activate() to catch mouse events.
Without on_activate(), it would be impossible to select a menu item
via keyboard.
This selects the item twice in some cases, but we are idempotent.
*/
virtual
bool
on_button_press_event
(GdkEventButton *event) {
if
(event->
button
==
1
&& event->
type
==
GDK_BUTTON_PRESS
) {
on_activate
();
return
true
;
}
return
Gtk::MenuItem::on_button_press_event
(event);
}
private:
int
_id;
};
int
ContextMenuItem::choice =
0
;
static
Gtk::Menu *context_menu =
NULL
, *context_submenu =
NULL
;
void
AddContextMenuItem
(
const
char
*label,
int
id) {
Gtk::MenuItem *menu_item;
if
(label)
menu_item =
new
ContextMenuItem
(label, id);
else
menu_item =
new
Gtk::SeparatorMenuItem
();
if
(id ==
CONTEXT_SUBMENU
) {
menu_item->
set_submenu
(*context_submenu);
context_submenu =
NULL
;
}
if
(context_submenu) {
context_submenu->
append
(*menu_item);
}
else
{
if
(!context_menu)
context_menu =
new
Gtk::Menu;
context_menu->
append
(*menu_item);
}
}
void
CreateContextSubmenu
(
void
) {
if
(context_submenu)
oops
();
context_submenu =
new
Gtk::Menu;
}
int
ShowContextMenu
(
void
) {
if
(!context_menu)
return
-
1
;
Glib::RefPtr<Glib::MainLoop> loop =
Glib::MainLoop::create
();
context_menu->
signal_deactivate
().
connect
(
sigc::mem_fun
(loop.
operator
->(), &Glib::MainLoop::quit));
ContextMenuItem::choice = -
1
;
context_menu->
show_all
();
context_menu->
popup
(
3
,
GDK_CURRENT_TIME
);
loop->
run
();
delete
context_menu;
context_menu =
NULL
;
return
ContextMenuItem::choice;
}
/*
Main menu
*/
template
<
class
MenuItem
>
class
MainMenuItem
:
public
MenuItem
{
public:
MainMenuItem
(
const
GraphicsWindow::MenuEntry &entry) :
MenuItem
(), _entry(entry), _synthetic(
false
) {
Glib::ustring
label
(_entry.
label
);
for
(
size_t
i =
0
; i < label.
length
(); i++) {
if
(label[i] ==
'
&
'
)
label.
replace
(i,
1
,
"
_
"
);
}
guint accel_key =
0
;
Gdk::ModifierType accel_mods =
Gdk::ModifierType
();
switch
(_entry.
accel
) {
case
GraphicsWindow::
DELETE_KEY
:
accel_key = GDK_KEY_Delete;
break
;
case
GraphicsWindow::
ESCAPE_KEY
:
accel_key = GDK_KEY_Escape;
break
;
case
'
\t
'
:
accel_key = GDK_KEY_Tab;
break
;
default
:
accel_key = _entry.
accel
& ~(GraphicsWindow::
SHIFT_MASK
| GraphicsWindow::
CTRL_MASK
);
if
(accel_key > GraphicsWindow::
FUNCTION_KEY_BASE
&&
accel_key <= GraphicsWindow::
FUNCTION_KEY_BASE
+
12
)
accel_key =
GDK_KEY_F1
+ (accel_key - GraphicsWindow::
FUNCTION_KEY_BASE
-
1
);
else
accel_key =
gdk_unicode_to_keyval
(accel_key);
if
(_entry.
accel
& GraphicsWindow::
SHIFT_MASK
)
accel_mods |= Gdk::
SHIFT_MASK
;
if
(_entry.
accel
& GraphicsWindow::
CTRL_MASK
)
accel_mods |= Gdk::
CONTROL_MASK
;
}
MenuItem::set_label
(label);
MenuItem::set_use_underline
(
true
);
if
(!(accel_key &
0x01000000
))
MenuItem::set_accel_key
(
Gtk::AccelKey
(accel_key, accel_mods));
}
void
set_active
(
bool
checked) {
if
(
MenuItem::get_active
() == checked)
return
;
_synthetic =
true
;
MenuItem::set_active
(checked);
}
protected:
virtual
void
on_activate
() {
MenuItem::on_activate
();
if
(_synthetic)
_synthetic =
false
;
else
if
(!
MenuItem::has_submenu
() && _entry.
fn
)
_entry.
fn
(_entry.
id
);
}
private:
const
GraphicsWindow::MenuEntry &_entry;
bool
_synthetic;
};
static
std::map<
int
, Gtk::MenuItem *> main_menu_items;
static
void
InitMainMenu
(Gtk::MenuShell *menu_shell) {
Gtk::MenuItem *menu_item =
NULL
;
Gtk::MenuShell *levels[
5
] = {menu_shell,
0
};
const
GraphicsWindow::MenuEntry *entry = &GraphicsWindow::menu[
0
];
int
current_level =
0
;
while
(entry->
level
>=
0
) {
if
(entry->
level
> current_level) {
Gtk::Menu *menu =
new
Gtk::Menu;
menu_item->
set_submenu
(*menu);
if
((
unsigned
)entry->
level
>=
sizeof
(levels) /
sizeof
(levels[
0
]))
oops
();
levels[entry->
level
] = menu;
}
current_level = entry->
level
;
if
(entry->
label
) {
switch
(entry->
kind
) {
case
GraphicsWindow::
MENU_ITEM_NORMAL
:
menu_item =
new
MainMenuItem<Gtk::MenuItem>(*entry);
break
;
case
GraphicsWindow::
MENU_ITEM_CHECK
:
menu_item =
new
MainMenuItem<Gtk::CheckMenuItem>(*entry);
break
;
case
GraphicsWindow::
MENU_ITEM_RADIO
:
MainMenuItem<Gtk::CheckMenuItem> *radio_item =
new
MainMenuItem<Gtk::CheckMenuItem>(*entry);
radio_item->
set_draw_as_radio
(
true
);
menu_item = radio_item;
break
;
}
}
else
{
menu_item =
new
Gtk::SeparatorMenuItem
();
}
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL