-
Notifications
You must be signed in to change notification settings - Fork 1
/
gy_gtk.i
1485 lines (1212 loc) · 43 KB
/
gy_gtk.i
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
/*
Copyright 2013 Thibaut Paumard
This file is part of gy (GObject Introspection for Yorick).
Gyoto 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.
Gyoto 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 gy. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gy.i"
local gy_gtk_i;
/* DOCUMENT #include "gy_gtk.i"
gy is a Yorick plug-in around GObject-introspection. It can
notably be used to create Gtk GUIs from within Yorick. gy_gtk.i is
based on gy.i and adds convenience functions such as standard
event handlers for writing Gtk based graphical user interfaces
(GUI).
PROVIDED APPLICATIONS
gy_gtk comes with a few sample GUI utilities:
- gyterm is a command line in a Gtk window. It is useful for
keeping a Yorick command line while another GUI is running.
- gycmap is a wrapper around cmap.
- gywindow is a wrapper for Yorick windows (work in progress).
- gyhelloworld is a trivial example.
gyterm and gywindow can be easily embedded in custom applications
(indeed, gyterm *is* embedded in both gycmap and gywindow). See
gy_gtk_ycmd and gy_gtk_ywindow.
SIDE EFFECTS:
#include "gy_gtk.i" has the following side effect:
- the users locale is set, excluding LC_NUMERIC, using
gy_setlocale.
- mouse() is replaced by gy_gtk_mouse().
Whenever a GUI is running:
- gy_gtk_idler() runs repeatedly using adter(). Some functions
(such as mouse()) cannot run as long as this loop is running.
- after_error is set to a function which redirects errors to a
dialog and makes some clean-up.
See gy_gtk_main().
EXAMPLE:
The gy source code comes with a few example scripts. Here comes a
basic, commented helloworld:
// Create widget hierarchy
win = Gtk.Window();
button = Gtk.Button(label="Hello World!");
noop, win.add(button);
// Write a callback
func hello(widget, event, data) {
"Hello World!";
}
// Connect callback to button event
gy_signal_connect, button, "clicked", hello;
// Show window
gy_gtk_main, win, on_delete=1;
SEE ALSO: gy_i, gyterm, gycmap, gywindow, gy_gtk_main
and the C documentation for the Gtk+ 3 API.
*/
extern Gtk, Gdk, GLib, GdkPixbuf;
/* DOCUMENT Gtk, Gdk, GdkPixbuf, GLib
gy namespaces.
SEE ALSO: gy_gtk_i
*/
Gtk = gy.require("Gtk", "3.0");
GdkX11 = gy.require("GdkX11", "3.0");
Gdk = gy.Gdk;
GLib = gy.GLib;
GdkPixbuf = gy.GdkPixbuf;
func __gyterm_init
{
require, "string.i";
extern __gyterm_initialized, __gyterm_win;
__gyterm_win = Gtk.Window.new(Gtk.WindowType.toplevel);
noop, __gyterm_win.set_title("Yorick command line");
entry = gy_gtk_ycmd(1);
noop, __gyterm_win.add(entry);
noop, entry.set_width_chars(80);
__gyterm_initialized=1;
}
if (is_void(__gyterm_history_size)) __gyterm_history_size=100;
__gyterm_history=array(string, __gyterm_history_size);
__gyterm_idx=__gyterm_cur=1;
__gyterm_max=0;
func __gyterm_key_pressed(widget, event, udata) {
extern __gyterm_history, __gyterm_cur, __gyterm_max, __gyterm_idx;
ev = Gdk.EventKey(event);
ev, keyval, keyval;
if (keyval==Gdk.KEY_Up) {
if (__gyterm_idx==__gyterm_cur) {
__gyterm_history(__gyterm_cur)=widget.get_text();
if (__gyterm_max<__gyterm_cur) __gyterm_max=__gyterm_cur;
}
__gyterm_idx=(__gyterm_idx==1)?__gyterm_max:__gyterm_idx-1;
noop, widget.set_text(__gyterm_history(__gyterm_idx));
noop, Gtk.Editable(widget).set_position(-1);
return 1;
}
if (keyval==Gdk.KEY_Down) {
__gyterm_idx=(__gyterm_idx==__gyterm_max)?1:__gyterm_idx+1;
noop, widget.set_text(__gyterm_history(__gyterm_idx));
noop, Gtk.Editable(widget).set_position(-1);
return 1;
}
if (keyval==Gdk.KEY_Return) {
cmd=widget.get_text();
noop, widget.set_text("");
__gyterm_history(__gyterm_cur)=cmd;
__gyterm_cur=(__gyterm_cur<numberof(__gyterm_history))?__gyterm_cur+1:1;
__gyterm_max=min(numberof(__gyterm_history), __gyterm_max+1);
__gyterm_idx=__gyterm_cur;
write, format="> %s\n", cmd;
include, strchar(cmd), 1;
return 1;
}
return 0;
}
func gy_gtk_ycmd(noexpander)
/* DOCUMENT widget = gy_gtk_ycmd (noexpander)
Create a Gtk widget to type Yorick commands, similar to
gyterm. Unless NOEXPANDER is specified and evaluates to true, the
Gtk entry is put in an expander.
SEE ALSO: gy_i, gyentry, gy_gtk_ycmd_connect, gy_gtk_ywindow
*/
{
entry = Gtk.Entry.new();
noop, entry.set_vexpand(0);
gy_gtk_ycmd_connect, entry;
if (noexpander) return entry;
exp = Gtk.Expander.new("<small><span style=\"italic\" size=\"smaller\">Yorick command</span></small>");
noop, exp.add(entry);
noop, exp.set_use_markup(1);
noop, exp.set_resize_toplevel(1);
noop, exp.set_vexpand(0);
return exp;
}
func gy_gtk_ycmd_connect(widget) {
/* DOCUMENT gy_gtk_ycmd_connect, entry_widget
Makes entry_widget mimick gyterm behavior.
EXAMPLE
entry=Gtk.Entry.new();
gy_gtk_ycmd_connect, entry;
SEE ALSO: gy_i, gyterm, gy_gtk_main
*/
gy_signal_connect, widget, "key-press-event", __gyterm_key_pressed;
noop, widget.set_placeholder_text("Yorick command");
noop, widget.set_tooltip_text("Yorick command");
}
func gy_gtk_hide_on_delete(wdg, evt, udata)
/* DOCUMENT gy_gtk_destroy_on_delete, window, [evt, data]
If evt is void, set-up gy_gtk_hide_on_delete as a callback for
the "delete-event" Gtk Window event.
If evt is not void, act as a callback: hide the window. In
addition, stops the event loop if there are no visible windows
remaining.
SEE ALSO: gy_gtk_destroy_on_delete, gy_gtk_idler_maybe_stop,
gy_gtk_main, gy_gtk_i
*/
{
if (is_void(evt))
gy_signal_connect, wdg, "delete-event", gy_gtk_hide_on_delete;
noop, wdg.hide();
gy_gtk_idler_maybe_stop;
return 1;
}
func gy_gtk_suspend (wdg, evt, udata)
/* DOCUMENT deprecated: gy_gtk_suspend, wdg
This was used when gy_gtk_main used to call gy.Gtk.main().
SEE ALSO: gy_gtk_hide_on_delete, gy_gtk_destroy_on_delete
*/
{
extern __gy_gtk_suspend_warned;
if (!__gy_gtk_suspend_warned) {
__gy_gtk_suspend_warned=1;
write, "WARNING: gy_gtk_suspend is deprecated, please upgrade to the new API\n";
}
if (is_void(evt)) evt=1;
return gy_gtk_hide_on_delete(wdg, evt, udata);
}
func gy_gtk_destroy_on_delete(wdg, evt, udata)
/* DOCUMENT gy_gtk_destroy_on_delete, window, [evt, data]
If evt is void, set-up gy_gtk_destroy_on_delete as a callback for
the "delete-event" Gtk Window event.
If evt is not void, act as a callback: destroy the window. In
addition, stops the event loop if there are no visible windows
remaining.
SEE ALSO: gy_gtk_hide_on_delete, gy_gtk_idler_maybe_stop,
gy_gtk_main, gy_gtk_i
*/
{
if (is_void(evt))
gy_signal_connect, wdg, "delete-event", gy_gtk_destroy_on_delete;
noop, wdg.destroy();
gy_gtk_idler_maybe_stop;
return 1;
}
func gy_gtk_idler_maybe_stop
/* DOCUMENT gy_gtk_idler_maybe_stop
Stop the event handler if there remains no window visible.
This should always be called in your delete-event handler.
SEE ALSO: gy_gtk_main, gy_gtk_hide_on_delete, gy_gtk_destroy_on_delete
*/
{
extern after_error;
gy_gtk_idler; // perform pending actions such as hiding a window!
tlvs = Gtk.Window.list_toplevels();
res = 0;
if (tlvs.size) {
for (;!is_void(tlvs);tlvs=tlvs.next) res |= tlvs.data.visible();
}
if (!res) {
after_error=[];
gy_gtk_idler, 0;
}
}
func gy_gtk_main(win, on_delete=)
/* DOCUMENT gy_gtk_main, win, on_delete=CALLBACK;
Show Gtk.Window win and set-up gy_gtk.i event loop:
1. Set-up after_error handler,
2. Optionally connect delete-event callback,
3. Show win;
4. Launch event handler loop.
It is (probably) best to stop the event loop when no windows are
visible. To achieve this, the delete-event callback of any window
(and any other callback which hides or detroys a window) should
call gy_gtk_idler_maybe_stop.
KEYWORDS:
on_delete:
the name (string) of a Yorick function suitable as a
delete-event handler:
func on_delete(widget, event, data) {
// hide or destroy
gy_gtk_idler_maybe_stop;
return 1;
}
on_delete=1 is a short-hand for on_delete="gy_gtk_hide_on_delete";
on_delete=2 is a short-hand for on_delete="gy_gtk_destroy_on_delete";
SEE ALSO: gy_gtk_i, gy_gtk_hide_on_delete, gy_gtk_destroy_on_delete
*/
{
extern after_error;
after_error=__gy_gtk_on_error;
if (!is_void(win)) {
if (is_numerical(on_delete)) {
if (on_delete==0) on_delete=[];
else if (on_delete==1) on_delete="gy_gtk_hide_on_delete";
else if (on_delete==2) on_delete="gy_gtk_destroy_on_delete";
}
if (!is_void(on_delete)) gy_signal_connect, win, "delete-event", on_delete;
noop, win.show_all();
}
gy_gtk_idler,1;
}
func gyterm(cmd)
/* DOCUMENT gyterm
Open a window containing a single line in which arbitrary Yorick
commands can be typed. If you want to embed gyterm in another GUI, see
gy_gtk_ycmd and gy_gtk_ycmd_connect.
SEE ALSO: gy_i, gy_gtk_ycmd, gy_gtk_ycmd_connect, gycmap, gywindow
*/
{
extern __gyterm_initialized, __gyterm_win;
local on_delete;
if (!__gyterm_initialized) {
__gyterm_init;
on_delete=1;
}
gy_gtk_main, __gyterm_win, on_delete=on_delete;
}
/// gycmap: a GUI for cmap
func __gycmap_init(void) {
require, "pathfun.i";
extern __gycmap_initialized, __gycmap_builder, __gycmap_win, __gycmap_ebox,
__gycmap_gist_img, __gycmap_msh_img, __gycmap_mpl_img, __gycmap_cmd,
__gycmap_gpl_img, __gycmap_gmt_img, __gycmap_cur_img,
__gycmap_div_img, __gycmap_seq_img, __gycmap_qual_img,
__gycmap_cur_names, __gycmap_gist_names;
gist_png = find_in_path("gist-cmap.png", takefirst=1,
path=pathform(_(get_cwd(),
_(Y_SITES,
Y_SITE)+"data/")));
png_dir=dirname(gist_png);
glade = find_in_path("gycmap.xml", takefirst=1,
path=pathform(_(get_cwd(),
_(Y_SITES,
Y_SITE)+"glade/")));
__gycmap_gist_img = Gtk.Image.new();
noop, __gycmap_gist_img.set_from_file(gist_png);
__gycmap_gist_names=
["gray", "yarg", "heat", "earth", "stern", "rainbow", "ncar"];
__gycmap_msh_img = Gtk.Image.new();
noop, __gycmap_msh_img.set_from_file(png_dir+"/msh-cmap.png");
__gycmap_mpl_img = Gtk.Image.new();
noop, __gycmap_mpl_img.set_from_file(png_dir+"/mpl-cmap.png");
__gycmap_gpl_img = Gtk.Image.new();
noop, __gycmap_gpl_img.set_from_file(png_dir+"/gpl-cmap.png");
__gycmap_gmt_img = Gtk.Image.new();
noop, __gycmap_gmt_img.set_from_file(png_dir+"/gmt-cmap.png");
__gycmap_div_img = Gtk.Image.new();
noop, __gycmap_div_img.set_from_file(png_dir+"/cbc-div-cmap.png");
__gycmap_seq_img = Gtk.Image.new();
noop, __gycmap_seq_img.set_from_file(png_dir+"/cbc-seq-cmap.png");
__gycmap_qual_img = Gtk.Image.new();
noop, __gycmap_qual_img.set_from_file(png_dir+"/cb-qual-cmap.png");
__gycmap_builder = Gtk.Builder.new();
noop, __gycmap_builder.add_from_file(glade);
__gycmap_win = __gycmap_builder.get_object("window1");
__gycmap_ebox = __gycmap_builder.get_object("eventbox");
noop, __gycmap_ebox.add(__gycmap_gist_img);
__gycmap_cur_img = __gycmap_gist_img;
__gycmap_cur_names = __gycmap_gist_names;
__gycmap_cmd=gistct;
combo=__gycmap_builder.get_object("combobox");
noop, combo.set_active_id("gist");
if (__gycmap_old_yorick)
noop, combo.set_sensitive(0);
else
gy_signal_connect, combo, "changed", __gycmap_combo_changed;
gy_signal_connect, __gycmap_ebox, "button-press-event", __gycmap_callback;
noop, __gycmap_win.set_title("Yorick color table chooser");
noop, __gycmap_builder.get_object("box1").add(gy_gtk_ycmd());
// gy_gtk_window_hide_on_delete, __gycmap_win;
__gycmap_initialized=1;
}
func __gycmap_gistct(name) {
palette, name+".gp";
}
if (!is_func(gistct)) {
__gycmap_old_yorick=1;
gistct=__gycmap_gistct;
}
func __gycmap_callback(widget, event, udata) {
extern __gycmap_cur_names;
ev = Gdk.EventButton(event);
ev, x, x, y, y;
name= __gycmap_cur_names(long(y/19)+1);
if (is_void(__gycmap.callback))
__gycmap_cmd, __gycmap_cur_names(long(y/19)+1);
else
noop, __gycmap.callback(__gycmap_cmd, name);
}
func __gycmap_combo_changed(widget, event, udata) {
extern __gycmap_cur_img, __gycmap_cur_names, __gycmap_cmd;
lst = widget.get_active_id();
noop, __gycmap_ebox.remove(__gycmap_cur_img);
if (lst == "gist") {
__gycmap_cur_img=__gycmap_gist_img;
__gycmap_cur_names=__gycmap_gist_names;
__gycmap_cmd=gistct;
} else if (lst == "msh") {
__gycmap_cur_img=__gycmap_msh_img;
__gycmap_cur_names=
["coolwarm", "blutan", "ornpur", "grnred",
"purple", "blue", "green", "red", "brown"];
__gycmap_cmd=mshct;
} else if (lst == "mpl") {
__gycmap_cur_img=__gycmap_mpl_img;
__gycmap_cur_names=
["binary", "gray", "bone", "pink", "copper", "winter",
"spring", "summer", "autumn", "hot", "afmhot", "coolwarm",
"cool", "rainbow", "terrain", "jet", "spectral", "hsv",
"flag", "prism", "seismic", "bwr", "brg"];
__gycmap_cmd=mplct;
} else if (lst == "gmt") {
__gycmap_cur_img=__gycmap_gmt_img;
__gycmap_cur_names=
["cool", "copper", "cyclic", "drywet", "gebco", "globe", "gray", "haxby",
"hot", "jet", "nighttime", "no_green", "ocean", "paired", "panoply",
"polar", "rainbow", "red2green", "relief", "sealand", "seis", "split",
"topo", "wysiwyg"];
__gycmap_cmd=gmtct;
} else if (lst == "gpl") {
__gycmap_cur_img=__gycmap_gpl_img;
__gycmap_cur_names=
["ocean", "gnu_hot", "gnuplot", "gnuplot2",
"gnuplot3", "gnuplot4", "gnuplot5"];
__gycmap_cmd=cmap;
} else if (lst == "cb-seq") {
__gycmap_cur_img=__gycmap_seq_img;
__gycmap_cur_names=
["Greys", "Purples", "Blues", "Greens", "Oranges", "Reds",
"PuBu", "PuBuGn", "PuRd", "BuGn", "BuPu", "GnBu", "YlGn",
"YlGnBu", "YlOrBr", "YlOrRd", "OrRd", "RdPu"];
__gycmap_cmd=cmap;
} else if (lst == "cb-div") {
__gycmap_cur_img=__gycmap_div_img;
__gycmap_cur_names=
["BrBG", "PRGn", "PiYG", "PuOr", "RdBu",
"RdGy", "RdYlBu", "RdYlGn", "Spectral"];
__gycmap_cmd=cmap;
} else if (lst == "cb-qual") {
__gycmap_cur_img=__gycmap_qual_img;
__gycmap_cur_names=
["Set1", "Pastel1", "Dark2", "Set2", "Pastel2",
"Set3", "Paired", "Accent"];
__gycmap_cmd=cmap;
} else error, "unrecognized colormap kind";
noop, __gycmap_ebox.add(__gycmap_cur_img);
noop, __gycmap_cur_img.show();
}
__gycmap=save();
func gycmap(callback)
/* DOCUMENT gycmap
or gycmap, callback
A graphical wrapper around the cmap family of functions, gycmap
allows the user to interactively select a color table by viewing
sample colorbars and clicking on the bar of her choice. If an
image is displayed in the current Yorick graphic window, the new
colormap is applied immediately. cmap_test can be used for
displaying a test image.
If CALLBACK is specified, it is called as:
callback, fonction, colormap;
each time a new colormap is selected instead of simply setting the
color map.
gycmap requires a recent version of Yorick (from git, as of
2013-04).
SEE ALSO: cmap, cmap_test
*/
{
extern __gycmap_initialized, __gycmap_builder, __gycmap_win, __gycmap_ebox,
__gycmap;
local on_delete;
save, __gycmap, callback;
if (!__gycmap_initialized) {
__gycmap_init;
on_delete=1;
}
gy_gtk_main, __gycmap_win, on_delete=on_delete;
}
//// gywindow: a yorick window wrapper
func __gywindow_on_error
{
extern __gywindow_device;
__gywindow_ungrab;
}
func gy_gtk_allowgrab(allow)
/* DOCUMENT gy_gtk_allowgrab, mode
mode = 0: forbid grabbing the mouse.
mose = 1: allow grabbing.
SEE ALSO: gywindow
*/
{
extern __gy_gtk_allowgrab;
__gy_gtk_allowgrab=allow;
if (!allow) __gywindow_ungrab;
}
gy_gtk_allowgrab, 1;
func gywinkill(yid)
/* DOCUMENT gywinkill, yid
Forget all about gywindow YID.
SEE ALSO gywindow
*/
{
extern __gywindow;
tmp = save();
n = __gywindow(*);
for (i=1; i<=n; ++i) {
if (__gywindow( (nothing=i) ).yid==yid) {
winkill, yid;
gy_gtk_destroy, __gywindow( (nothing=i) ).win;
} else save, tmp, "", __gywindow( (nothing=i) );
}
__gywindow = tmp;
}
func gy_gtk_ywindow_reinit(yid, dpi=, style=)
/* DOCUMENT gy_gtk_ywindow_reinit, yid
RE-initialize Yorick window YID attached to a gy widget, for
instance to change DPI or STYLE.
KEYWORDS: dpi, style.
SEE ALSO: gy_i, gywindow, gy_gtk_ywindow
*/
{
cur = __gywindow_find_by_yid(yid);
if (!is_void(dpi)) {
if (dpi==0) dpi=75;
save, cur, dpi;
}
if (!is_void(style)) {
if (style==0) style="work.gs";
save, cur, style;
}
winkill, cur.yid;
window, cur.yid, parent=cur.xid, ypos=-24, dpi=cur.dpi,
width=long(8.5*cur.dpi), height=long(11*cur.dpi), style=cur.style;
noop, cur.da.set_size_request(long(8.5*dpi),long(11*dpi));
}
func __gywindow_event_handler(widget, event, udata) {
extern __gywindow, __gywindow_xs0, __gywindow_ys0, __gywindow_device;
local curwin, win;
cur = __gywindow_find_by_xid(gy_gtk_xid(widget));
if (is_void(cur)) return;
EventType=Gdk.EventType;
ev = Gdk.EventAny(event);
type = ev.type;
if (type == EventType.map && !cur.realized) {
window, cur.yid, parent=gy_gtk_xid(widget), ypos=-24, dpi=cur.dpi,
width=long(8.5*cur.dpi), height=long(11*cur.dpi), style=cur.style;
save, cur, realized=1;
sw = widget.get_parent().get_parent();
hadjustment = sw.get_hadjustment();
vadjustment = sw.get_vadjustment();
save, cur, sw, hadjustment, vadjustment;
xcenter = long(4.25*cur.dpi);
noop, cur.hadjustment.set_value(xcenter-cur.hadjustment.get_page_size()/2);
noop, cur.vadjustment.set_value(xcenter-cur.vadjustment.get_page_size()/2);
if (cur.on_realize) cur.on_realize;
return;
}
if (!cur.realized) return;
if (type == EventType.configure) {
xcenter = long(4.25*cur.dpi);
noop, cur.sw.set_size_request(-1,-1);
noop, cur.hadjustment.set_value(xcenter-cur.hadjustment.get_page_size()/2);
noop, cur.vadjustment.set_value(xcenter-cur.vadjustment.get_page_size()/2);
if (cur.on_configure) cur.on_configure;
return;
}
if (!cur.grab) return;
pix2ndc=72.27/cur.dpi*0.0013;
if (type == EventType.enter_notify && __gy_gtk_allowgrab) {
__gywindow_device = Gdk.Device(Gtk.get_current_event_device());
noop, Gtk.Widget(widget)(window, win);
noop, __gywindow_device.grab(win, Gdk.GrabOwnership.none, 1,
Gdk.EventMask.all_events_mask,
,
Gdk.CURRENT_TIME);
return;
}
if (type == EventType.leave_notify && Gdk.EventCrossing(ev).mode != Gdk.CrossingMode.grab) {
__gywindow_ungrab;
return;
}
if (type == EventType.button_release ||
type == EventType.button_press ||
type == EventType.motion_notify) {
curwin = current_window();
window, cur.yid;
ev = Gdk.EventButton(ev);
ev, x, x, y, y, button, button, state, state;
shft = state & Gdk.ModifierType.shift_mask;
meta = state & Gdk.ModifierType.mod1_mask;
if (shft && !meta && button==1) button=2;
if (meta && !shft && button==1) button=3;
xndc=pix2ndc*(x-2);
yndc=11.*72.27*0.0013-pix2ndc*(y-1);
vp = viewport();
lm = limits();
flags=long(lm(5));
xlog = flags & 128;
ylog = flags & 256;
if (xndc < vp(1)) xs = lm(1);
else if (xndc>vp(2)) xs=lm(2);
else {
if (xlog)
xs = lm(1) * (lm(2)/lm(1))^((xndc-vp(1))/(vp(2)-vp(1)));
else
xs = lm(1)+(xndc-vp(1))*(lm(2)-lm(1))/(vp(2)-vp(1));
}
if (yndc < vp(3)) ys = lm(3);
else if (yndc>vp(4)) ys=lm(4);
else {
if (xlog)
ys = lm(3) * (lm(4)/lm(3))^((yndc-vp(3))/(vp(4)-vp(3)));
else
ys = lm(3)+(yndc-vp(3))*(lm(4)-lm(3))/(vp(4)-vp(3));
}
}
if (type == EventType.button_press) {
__gywindow_xs0=xs;
__gywindow_ys0=ys;
}
if (type == EventType.button_release) {
lm = limits();
flags=long(lm(5));
if (is_func(cur.mouse_handler)) {
noop, cur.mouse_handler(cur.yid,
__gywindow_xs0, __gywindow_ys0,
xs, ys, button, flags);
if (!is_void(curwin) && curwin>=0) window, curwin;
return;
}
lm2=lm;
fact=1.;
if (button==1) fact=2./3.;
else if (button==3) fact=1.5;
xlog = flags & 128;
ylog = flags & 256;
if (noneof(__gywindow_xs0 == lm(1:2))) {
if (xlog) {
lm2(1:2)=__gywindow_xs0 / (xs/lm(1:2))^fact;
} else {
lm2(1:2)=__gywindow_xs0 - (xs-lm(1:2))*fact;
}
limits, lm2(1), lm2(2);
}
if (noneof(__gywindow_ys0 == lm(3:4))) {
if (ylog) {
lm2(3:4)=__gywindow_ys0 / (ys/lm(3:4))^fact;
} else {
lm2(3:4)=__gywindow_ys0 - (ys-lm(3:4))*fact;
}
range, lm2(3), lm2(4);
}
}
if (!is_void(curwin) && curwin>=0) window, curwin;
if (type == EventType.motion_notify) {
if (x<cur.hadjustment.get_value() ||
y<cur.vadjustment.get_value() ||
x>cur.hadjustment.get_value()+cur.hadjustment.get_page_size() ||
y>cur.vadjustment.get_value()+cur.vadjustment.get_page_size()) {
__gywindow_ungrab;
return;
}
noop, cur.slabel.set_text("?");
noop, cur.xlabel.set_text(swrite(xs));
noop, cur.ylabel.set_text(swrite(ys));
return;
}
//write, format="in __gywindow_realized. event: %d\n", type;
}
/// ywindows
if (is_void(__gywindow)) __gywindow=save();
func gy_gtk_ywindow_connect(&yid, win, da, slabel, xlabel, ylabel, dpi=, style=,
on_realize=, on_configure=, grab=)
/* DOCUMENT gy_gtk_ywindow_connect, yid, win, da, slabel, xlabel, ylabel
Connect widgets to embed a Yorick window in a Gtk DrawingArea (see
gywindow for a trivial example). For a lower level function, see
gy_gtk_ywindow.
If YID is nil, a new ID is taken and YID is set to this value.
ARGUMENTS
yid: Yorick window ID to embed
win: the toplevel Gtk.Window widget
da: the Gtk.DrawingArea in which the yorick window will be embedded.
s|x|ylabel: Gtk.Label widgets in which to report mouse motion.
KEYWORDS
on_realize: function to call when the window is realized
on_configure: function to call whenever the function is configured
(i.e. resized)
grab: 1 if window should grab the mouse, necessary to use
gy_gtk_ywindow_mouse_handler, otherwise discouraged
SEE ALSO: gy_i, gywindow, gy_gtk_ywindow, gy_gtk_ywindow_mouse_handler
*/
{
extern __gywindow;
if (is_void(yid)) yid=gy_gtk_ywindow_free_id(-1);
if (is_void(yid)) error, "unable to find free id";
if (is_void(dpi)) dpi=75;
gy_signal_connect, da, "event", __gywindow_event_handler;
save, __gywindow, "", save(yid, xid=[], win, da, slabel, xlabel, ylabel,
realized=0, dpi, style,
mouse_handler=[],
on_realize, on_configure, grab);
}
func gy_gtk_ywindow_mouse_handler(yid, handler)
/* DOCUMENT gy_gtk_ywindow_mouse_handler, yid, handler
Attach application-specific mouse event handler Yorick window
created with gy_gtk_ywindow (this includes gywindow windows).
The handler will be passed only press-release events and allows
similar actions to what mouse() provides for regular Yorick
windows.
ARGUMENTS:
yid: the Yorick window ID number, first argument of
gy_gtk_window.
handler: Yorick function with prototype
func mouse_toto(yid, x0, y0, x1, y1, button, flags);
where x0, y0, x1 and y1 are the window coordinates of
the button press and release events, button is the
button which was pressed, flags is limits()(5).
SEE ALSO: gy_i, gy_gtk_ywindow, gywindow, mouse, limits
*/
{
save, __gywindow_find_by_yid(yid), mouse_handler=handler, grab=1;
}
func gy_gtk_ywindow(&yid, dpi=, width=, height=, style=,
on_realize=, on_configure=, grab=)
/* DOCUMENT widget = gy_gtk_ywindow(yid)
Initialize a Gtk widget embedding Yorick window number YID. The
widget is scrollable and provides mouse position reading and
zoom/pan capabilities. The widget is connected using
gy_gtk_ywindow_connect.
If YID is nil, a new ID is taken and YID is set to this value.
KEYWORDS: dpi, width, height, style: see window
on_realize, on_configure, grab: see gy_gtk_window_connect
SEE ALSO: gy_i, gywindow, window, gy_gtk_ywindow_connect
*/
{
extern __gywindow;
if (is_void(dpi)) dpi=75;
//if (is_void(width)) width=long(6*dpi);
//if (is_void(height)) height=long(6*dpi);
if (is_void(yid)) yid=gy_gtk_ywindow_free_id(-1);
if (is_void(yid)) error, "unable to find free id";
box = Gtk.Box.new(Gtk.Orientation.vertical, 0);
box2=Gtk.Box.new(Gtk.Orientation.horizontal, 0);
noop, box.pack_start(box2, 0,0,0);
noop, box2.pack_start(Gtk.Label(label="System: "),0,0,0);
slabel=Gtk.Label(label="0", max_width_chars=1, width_chars=1);
noop, box2.pack_start(slabel, 0, 0, 0);
noop, box2.pack_start(Gtk.Label(label="( "),0,0,0);
xlabel=Gtk.Label(label="", max_width_chars=15, width_chars=15);
noop, box2.pack_start(xlabel, 0, 0, 0);
noop, box2.pack_start(Gtk.Label(label=", "),0,0,0);
ylabel=Gtk.Label(label="", max_width_chars=15, width_chars=15);
noop, box2.pack_start(ylabel, 0, 0, 0);
noop, box2.pack_start(Gtk.Label(label=")"),0,0,0);
sw = Gtk.ScrolledWindow.new(,);
if (!is_void(width) && !is_void(height))
noop, sw.set_size_request(width,height);
noop, box.pack_start(sw, 1, 1, 0);
tmp = Gtk.Viewport.new(,);
noop, sw.add(tmp);
da = Gtk.DrawingArea.new();
noop, da.add_events(Gdk.EventMask.all_events_mask);
noop, da.set_size_request(long(8.5*dpi),long(11*dpi));
noop, tmp.add(da);
gy_gtk_ywindow_connect, yid, win, da, slabel, xlabel, ylabel, dpi=dpi, style=style,
on_realize=on_realize, on_configure=on_configure, grab=grab;
return box;
}
func __gywindow_cmap(wdg, data)
{
gycmap;
return 1;
}
func __gywindow_save(wdg, data)
{
require, "pathfun.i";
extern result;
win = Gtk.FileChooserDialog();
fcfc = Gtk.FileChooser(win);
noop, fcfc.set_action(Gtk.FileChooserAction.save);
noop, fcfc.set_do_overwrite_confirmation(1);
noop, fcfc.set_create_folders(1);
noop, win.add_button(Gtk.STOCK_SAVE, Gtk.ResponseType.ok);
noop, win.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.cancel);
filter = Gtk.FileFilter();
noop, filter.add_pattern("*.[pP][dD][fF]");
noop, filter.add_pattern("*.[eE][pP][sS]");
noop, filter.add_pattern("*.[jJ][pP][eE][gG]");
noop, filter.add_pattern("*.[jJ][pP][gG]");
noop, filter.add_pattern("*.[jJ][fF][iI][fF]");
noop, filter.add_pattern("*.[pP][nN][gG]");
noop, filter.set_name("All supported files");
noop, fcfc.add_filter(filter);
filter = Gtk.FileFilter();
noop, filter.add_pattern("*.[pP][dD][fF]");
noop, filter.set_name("PDF documents");
noop, fcfc.add_filter(filter);
filter = Gtk.FileFilter();
noop, filter.add_pattern("*.[eE][pP][sS]");
noop, filter.set_name("EPS documents");
noop, fcfc.add_filter(filter);
filter = Gtk.FileFilter();
noop, filter.add_pattern("*.[jJ][pP][eE][gG]");
noop, filter.add_pattern("*.[jJ][pP][gG]");
noop, filter.add_pattern("*.[jJ][fF][iI][fF]");
noop, filter.set_name("JPEG images");
noop, fcfc.add_filter(filter);
filter = Gtk.FileFilter();
noop, filter.add_pattern("*.[pP][nN][gG]");
noop, filter.set_name("PNG images");
noop, fcfc.add_filter(filter);
filter = Gtk.FileFilter();
noop, filter.add_pattern("*");
noop, filter.set_name("All files");
noop, fcfc.add_filter(filter);
noop, win.show_all();
result = win.run();
fname=fcfc.get_filename();
noop, win.hide();
noop, win.destroy();
if (result != Gtk.ResponseType.ok || fname == string(0) ) return 1;
yid = __gywindow_find_by_xid(gy_gtk_xid(data)).yid;
bname=basename(fname);
ext=pathsplit(bname,delim=".");
if (numberof(ext)==1) {
gyerror, "Export failed: no extension in file name.";
return 1;
}
format=ext(0);
if (anyof(format==["JPEG","jpeg","jpg","jfif"])) fformat="jpeg";
else if (anyof(format==["PNG","png"])) fformat="png";
else if (anyof(format==["EPS","eps"])) fformat="eps";
else if (anyof(format==["PDF","pdf"])) fformat="pdf";
else {
gyerror, "Could not recognize extension \"" + format +"\".";
return 1;
}
symb=symbol_def(fformat);
if (!is_func(symb)) {
gyerror, "This Yorick lacks the function \"" + fformat + "\".";
return 1;
}
if (catch(-1)) {gyerror, catch_message; return 1;}
prev = current_window();
window, yid;
symb, fname;
window, prev;
return 1;
}
func __gywindow_init(&yid, dpi=, width=, height=, style=,
on_realize=, on_configure=, grab=)
{
extern __gywindow, adj;
if (is_void(yid)) yid=gy_gtk_ywindow_free_id(-1);
if (is_void(yid)) error, "unable to find free id";
win = Gtk.Window.new(Gtk.WindowType.toplevel);
noop, win.set_default_size(450, 488);
noop, win.set_title("Yorick "+pr1(yid));
box=Gtk.Box.new(Gtk.Orientation.vertical, 0);
noop, win.add(box);
noop, box.pack_start(gy_gtk_ywindow
(yid,
dpi=dpi, width=width, height=height, style=style,
on_realize=on_realize, on_configure=on_configure, grab=grab),
1, 1, 0);