forked from ghewgill/puzzles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtk.c
2968 lines (2556 loc) · 81 KB
/
gtk.c
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
/*
* gtk.c: GTK front end for my puzzle collection.
*/
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <time.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gdk/gdkx.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include "puzzles.h"
#if GTK_CHECK_VERSION(2,0,0)
# define USE_PANGO
# ifdef PANGO_VERSION_CHECK
# if PANGO_VERSION_CHECK(1,8,0)
# define HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION
# endif
# endif
#endif
#if !GTK_CHECK_VERSION(2,4,0)
# define OLD_FILESEL
#endif
#if GTK_CHECK_VERSION(2,8,0)
# define USE_CAIRO
#endif
/* #undef USE_CAIRO */
/* #define NO_THICK_LINE */
#ifdef DEBUGGING
static FILE *debug_fp = NULL;
void dputs(char *buf)
{
if (!debug_fp) {
debug_fp = fopen("debug.log", "w");
}
fputs(buf, stderr);
if (debug_fp) {
fputs(buf, debug_fp);
fflush(debug_fp);
}
}
void debug_printf(char *fmt, ...)
{
char buf[4096];
va_list ap;
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
dputs(buf);
va_end(ap);
}
#endif
/* ----------------------------------------------------------------------
* Error reporting functions used elsewhere.
*/
void fatal(char *fmt, ...)
{
va_list ap;
fprintf(stderr, "fatal error: ");
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
exit(1);
}
/* ----------------------------------------------------------------------
* GTK front end to puzzles.
*/
static void changed_preset(frontend *fe);
struct font {
#ifdef USE_PANGO
PangoFontDescription *desc;
#else
GdkFont *font;
#endif
int type;
int size;
};
/*
* This structure holds all the data relevant to a single window.
* In principle this would allow us to open multiple independent
* puzzle windows, although I can't currently see any real point in
* doing so. I'm just coding cleanly because there's no
* particularly good reason not to.
*/
struct frontend {
GtkWidget *window;
GtkAccelGroup *accelgroup;
GtkWidget *area;
GtkWidget *statusbar;
GtkWidget *menubar;
guint statusctx;
int w, h;
midend *me;
#ifdef USE_CAIRO
const float *colours;
cairo_t *cr;
cairo_surface_t *image;
GdkPixmap *pixmap;
GdkColor background; /* for painting outside puzzle area */
#else
GdkPixmap *pixmap;
GdkGC *gc;
GdkColor *colours;
GdkColormap *colmap;
int backgroundindex; /* which of colours[] is background */
#endif
int ncolours;
int bbox_l, bbox_r, bbox_u, bbox_d;
int timer_active, timer_id;
struct timeval last_time;
struct font *fonts;
int nfonts, fontsize;
config_item *cfg;
int cfg_which, cfgret;
GtkWidget *cfgbox;
void *paste_data;
int paste_data_len;
int pw, ph; /* pixmap size (w, h are area size) */
int ox, oy; /* offset of pixmap in drawing area */
#ifdef OLD_FILESEL
char *filesel_name;
#endif
int drawing_area_shrink_pending;
GSList *preset_radio;
int n_preset_menu_items;
int preset_threaded;
GtkWidget *preset_custom;
GtkWidget *copy_menu_item;
int menubar_is_local;
};
struct blitter {
#ifdef USE_CAIRO
cairo_surface_t *image;
#else
GdkPixmap *pixmap;
#endif
int w, h, x, y;
};
void get_random_seed(void **randseed, int *randseedsize)
{
struct timeval *tvp = snew(struct timeval);
gettimeofday(tvp, NULL);
*randseed = (void *)tvp;
*randseedsize = sizeof(struct timeval);
}
void frontend_default_colour(frontend *fe, float *output)
{
GdkColor col = fe->window->style->bg[GTK_STATE_NORMAL];
output[0] = col.red / 65535.0;
output[1] = col.green / 65535.0;
output[2] = col.blue / 65535.0;
}
void gtk_status_bar(void *handle, char *text)
{
frontend *fe = (frontend *)handle;
assert(fe->statusbar);
gtk_statusbar_pop(GTK_STATUSBAR(fe->statusbar), fe->statusctx);
gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx, text);
}
/* ----------------------------------------------------------------------
* Cairo drawing functions.
*/
#ifdef USE_CAIRO
static void setup_drawing(frontend *fe)
{
fe->cr = cairo_create(fe->image);
cairo_set_antialias(fe->cr, CAIRO_ANTIALIAS_GRAY);
cairo_set_line_width(fe->cr, 1.0);
cairo_set_line_cap(fe->cr, CAIRO_LINE_CAP_SQUARE);
cairo_set_line_join(fe->cr, CAIRO_LINE_JOIN_ROUND);
}
static void teardown_drawing(frontend *fe)
{
cairo_t *cr;
cairo_destroy(fe->cr);
fe->cr = NULL;
cr = gdk_cairo_create(fe->pixmap);
cairo_set_source_surface(cr, fe->image, 0, 0);
cairo_rectangle(cr,
fe->bbox_l - 1,
fe->bbox_u - 1,
fe->bbox_r - fe->bbox_l + 2,
fe->bbox_d - fe->bbox_u + 2);
cairo_fill(cr);
cairo_destroy(cr);
}
static void snaffle_colours(frontend *fe)
{
fe->colours = midend_colours(fe->me, &fe->ncolours);
}
static void set_colour(frontend *fe, int colour)
{
cairo_set_source_rgb(fe->cr,
fe->colours[3*colour + 0],
fe->colours[3*colour + 1],
fe->colours[3*colour + 2]);
}
static void set_window_background(frontend *fe, int colour)
{
GdkColormap *colmap;
colmap = gdk_colormap_get_system();
fe->background.red = fe->colours[3*colour + 0] * 65535;
fe->background.green = fe->colours[3*colour + 1] * 65535;
fe->background.blue = fe->colours[3*colour + 2] * 65535;
if (!gdk_colormap_alloc_color(colmap, &fe->background, FALSE, FALSE)) {
g_error("couldn't allocate background (#%02x%02x%02x)\n",
fe->background.red >> 8, fe->background.green >> 8,
fe->background.blue >> 8);
}
gdk_window_set_background(fe->area->window, &fe->background);
gdk_window_set_background(fe->window->window, &fe->background);
}
static PangoLayout *make_pango_layout(frontend *fe)
{
return (pango_cairo_create_layout(fe->cr));
}
static void draw_pango_layout(frontend *fe, PangoLayout *layout,
int x, int y)
{
cairo_move_to(fe->cr, x, y);
pango_cairo_show_layout(fe->cr, layout);
}
static void save_screenshot_png(frontend *fe, const char *screenshot_file)
{
cairo_surface_write_to_png(fe->image, screenshot_file);
}
static void do_clip(frontend *fe, int x, int y, int w, int h)
{
cairo_new_path(fe->cr);
cairo_rectangle(fe->cr, x, y, w, h);
cairo_clip(fe->cr);
}
static void do_unclip(frontend *fe)
{
cairo_reset_clip(fe->cr);
}
static void do_draw_rect(frontend *fe, int x, int y, int w, int h)
{
cairo_save(fe->cr);
cairo_new_path(fe->cr);
cairo_set_antialias(fe->cr, CAIRO_ANTIALIAS_NONE);
cairo_rectangle(fe->cr, x, y, w, h);
cairo_fill(fe->cr);
cairo_restore(fe->cr);
}
static void do_draw_line(frontend *fe, int x1, int y1, int x2, int y2)
{
cairo_new_path(fe->cr);
cairo_move_to(fe->cr, x1 + 0.5, y1 + 0.5);
cairo_line_to(fe->cr, x2 + 0.5, y2 + 0.5);
cairo_stroke(fe->cr);
}
static void do_draw_thick_line(frontend *fe, float thickness,
float x1, float y1, float x2, float y2)
{
cairo_save(fe->cr);
cairo_set_line_width(fe->cr, thickness);
cairo_new_path(fe->cr);
cairo_move_to(fe->cr, x1, y1);
cairo_line_to(fe->cr, x2, y2);
cairo_stroke(fe->cr);
cairo_restore(fe->cr);
}
static void do_draw_poly(frontend *fe, int *coords, int npoints,
int fillcolour, int outlinecolour)
{
int i;
cairo_new_path(fe->cr);
for (i = 0; i < npoints; i++)
cairo_line_to(fe->cr, coords[i*2] + 0.5, coords[i*2 + 1] + 0.5);
cairo_close_path(fe->cr);
if (fillcolour >= 0) {
set_colour(fe, fillcolour);
cairo_fill_preserve(fe->cr);
}
assert(outlinecolour >= 0);
set_colour(fe, outlinecolour);
cairo_stroke(fe->cr);
}
static void do_draw_circle(frontend *fe, int cx, int cy, int radius,
int fillcolour, int outlinecolour)
{
cairo_new_path(fe->cr);
cairo_arc(fe->cr, cx + 0.5, cy + 0.5, radius, 0, 2*PI);
cairo_close_path(fe->cr); /* Just in case... */
if (fillcolour >= 0) {
set_colour(fe, fillcolour);
cairo_fill_preserve(fe->cr);
}
assert(outlinecolour >= 0);
set_colour(fe, outlinecolour);
cairo_stroke(fe->cr);
}
static void setup_blitter(blitter *bl, int w, int h)
{
bl->image = cairo_image_surface_create(CAIRO_FORMAT_RGB24, w, h);
}
static void teardown_blitter(blitter *bl)
{
cairo_surface_destroy(bl->image);
}
static void do_blitter_save(frontend *fe, blitter *bl, int x, int y)
{
cairo_t *cr = cairo_create(bl->image);
cairo_set_source_surface(cr, fe->image, -x, -y);
cairo_paint(cr);
cairo_destroy(cr);
}
static void do_blitter_load(frontend *fe, blitter *bl, int x, int y)
{
cairo_set_source_surface(fe->cr, bl->image, x, y);
cairo_paint(fe->cr);
}
static void clear_backing_store(frontend *fe)
{
fe->image = NULL;
}
static void setup_backing_store(frontend *fe)
{
cairo_t *cr;
int i;
fe->pixmap = gdk_pixmap_new(fe->area->window, fe->pw, fe->ph, -1);
fe->image = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
fe->pw, fe->ph);
for (i = 0; i < 3; i++) {
switch (i) {
case 0: cr = cairo_create(fe->image); break;
case 1: cr = gdk_cairo_create(fe->pixmap); break;
case 2: cr = gdk_cairo_create(fe->area->window); break;
}
cairo_set_source_rgb(cr,
fe->colours[0], fe->colours[1], fe->colours[2]);
cairo_paint(cr);
cairo_destroy(cr);
}
}
static int backing_store_ok(frontend *fe)
{
return (!!fe->image);
}
static void teardown_backing_store(frontend *fe)
{
cairo_surface_destroy(fe->image);
gdk_pixmap_unref(fe->pixmap);
fe->image = NULL;
}
#endif
/* ----------------------------------------------------------------------
* GDK drawing functions.
*/
#ifndef USE_CAIRO
static void setup_drawing(frontend *fe)
{
fe->gc = gdk_gc_new(fe->area->window);
}
static void teardown_drawing(frontend *fe)
{
gdk_gc_unref(fe->gc);
fe->gc = NULL;
}
static void snaffle_colours(frontend *fe)
{
int i, ncolours;
float *colours;
gboolean *success;
fe->colmap = gdk_colormap_get_system();
colours = midend_colours(fe->me, &ncolours);
fe->ncolours = ncolours;
fe->colours = snewn(ncolours, GdkColor);
for (i = 0; i < ncolours; i++) {
fe->colours[i].red = colours[i*3] * 0xFFFF;
fe->colours[i].green = colours[i*3+1] * 0xFFFF;
fe->colours[i].blue = colours[i*3+2] * 0xFFFF;
}
success = snewn(ncolours, gboolean);
gdk_colormap_alloc_colors(fe->colmap, fe->colours, ncolours,
FALSE, FALSE, success);
for (i = 0; i < ncolours; i++) {
if (!success[i]) {
g_error("couldn't allocate colour %d (#%02x%02x%02x)\n",
i, fe->colours[i].red >> 8,
fe->colours[i].green >> 8,
fe->colours[i].blue >> 8);
}
}
}
static void set_window_background(frontend *fe, int colour)
{
fe->backgroundindex = colour;
gdk_window_set_background(fe->area->window, &fe->colours[colour]);
gdk_window_set_background(fe->window->window, &fe->colours[colour]);
}
static void set_colour(frontend *fe, int colour)
{
gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
}
#ifdef USE_PANGO
static PangoLayout *make_pango_layout(frontend *fe)
{
return (pango_layout_new(gtk_widget_get_pango_context(fe->area)));
}
static void draw_pango_layout(frontend *fe, PangoLayout *layout,
int x, int y)
{
gdk_draw_layout(fe->pixmap, fe->gc, x, y, layout);
}
#endif
static void save_screenshot_png(frontend *fe, const char *screenshot_file)
{
GdkPixbuf *pb;
GError *gerror = NULL;
midend_redraw(fe->me);
pb = gdk_pixbuf_get_from_drawable(NULL, fe->pixmap,
NULL, 0, 0, 0, 0, -1, -1);
gdk_pixbuf_save(pb, screenshot_file, "png", &gerror, NULL);
}
static void do_clip(frontend *fe, int x, int y, int w, int h)
{
GdkRectangle rect;
rect.x = x;
rect.y = y;
rect.width = w;
rect.height = h;
gdk_gc_set_clip_rectangle(fe->gc, &rect);
}
static void do_unclip(frontend *fe)
{
GdkRectangle rect;
rect.x = 0;
rect.y = 0;
rect.width = fe->w;
rect.height = fe->h;
gdk_gc_set_clip_rectangle(fe->gc, &rect);
}
static void do_draw_rect(frontend *fe, int x, int y, int w, int h)
{
gdk_draw_rectangle(fe->pixmap, fe->gc, 1, x, y, w, h);
}
static void do_draw_line(frontend *fe, int x1, int y1, int x2, int y2)
{
gdk_draw_line(fe->pixmap, fe->gc, x1, y1, x2, y2);
}
static void do_draw_thick_line(frontend *fe, float thickness,
float x1, float y1, float x2, float y2)
{
GdkGCValues save;
gdk_gc_get_values(fe->gc, &save);
gdk_gc_set_line_attributes(fe->gc,
thickness,
GDK_LINE_SOLID,
GDK_CAP_BUTT,
GDK_JOIN_BEVEL);
gdk_draw_line(fe->pixmap, fe->gc, x1, y1, x2, y2);
gdk_gc_set_line_attributes(fe->gc,
save.line_width,
save.line_style,
save.cap_style,
save.join_style);
}
static void do_draw_poly(frontend *fe, int *coords, int npoints,
int fillcolour, int outlinecolour)
{
GdkPoint *points = snewn(npoints, GdkPoint);
int i;
for (i = 0; i < npoints; i++) {
points[i].x = coords[i*2];
points[i].y = coords[i*2+1];
}
if (fillcolour >= 0) {
set_colour(fe, fillcolour);
gdk_draw_polygon(fe->pixmap, fe->gc, TRUE, points, npoints);
}
assert(outlinecolour >= 0);
set_colour(fe, outlinecolour);
/*
* In principle we ought to be able to use gdk_draw_polygon for
* the outline as well. In fact, it turns out to interact badly
* with a clipping region, for no terribly obvious reason, so I
* draw the outline as a sequence of lines instead.
*/
for (i = 0; i < npoints; i++)
gdk_draw_line(fe->pixmap, fe->gc,
points[i].x, points[i].y,
points[(i+1)%npoints].x, points[(i+1)%npoints].y);
sfree(points);
}
static void do_draw_circle(frontend *fe, int cx, int cy, int radius,
int fillcolour, int outlinecolour)
{
if (fillcolour >= 0) {
set_colour(fe, fillcolour);
gdk_draw_arc(fe->pixmap, fe->gc, TRUE,
cx - radius, cy - radius,
2 * radius, 2 * radius, 0, 360 * 64);
}
assert(outlinecolour >= 0);
set_colour(fe, outlinecolour);
gdk_draw_arc(fe->pixmap, fe->gc, FALSE,
cx - radius, cy - radius,
2 * radius, 2 * radius, 0, 360 * 64);
}
static void setup_blitter(blitter *bl, int w, int h)
{
/*
* We can't create the pixmap right now, because fe->window
* might not yet exist. So we just cache w and h and create it
* during the firs call to blitter_save.
*/
bl->pixmap = NULL;
}
static void teardown_blitter(blitter *bl)
{
if (bl->pixmap)
gdk_pixmap_unref(bl->pixmap);
}
static void do_blitter_save(frontend *fe, blitter *bl, int x, int y)
{
if (!bl->pixmap)
bl->pixmap = gdk_pixmap_new(fe->area->window, bl->w, bl->h, -1);
gdk_draw_pixmap(bl->pixmap,
fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
fe->pixmap,
x, y, 0, 0, bl->w, bl->h);
}
static void do_blitter_load(frontend *fe, blitter *bl, int x, int y)
{
assert(bl->pixmap);
gdk_draw_pixmap(fe->pixmap,
fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
bl->pixmap,
0, 0, x, y, bl->w, bl->h);
}
static void clear_backing_store(frontend *fe)
{
fe->pixmap = NULL;
}
static void setup_backing_store(frontend *fe)
{
GdkGC *gc;
fe->pixmap = gdk_pixmap_new(fe->area->window, fe->pw, fe->ph, -1);
gc = gdk_gc_new(fe->area->window);
gdk_gc_set_foreground(gc, &fe->colours[0]);
gdk_draw_rectangle(fe->pixmap, gc, 1, 0, 0, fe->pw, fe->ph);
gdk_draw_rectangle(fe->area->window, gc, 1, 0, 0, fe->w, fe->h);
gdk_gc_unref(gc);
}
static int backing_store_ok(frontend *fe)
{
return (!!fe->pixmap);
}
static void teardown_backing_store(frontend *fe)
{
gdk_pixmap_unref(fe->pixmap);
fe->pixmap = NULL;
}
#endif
static void repaint_rectangle(frontend *fe, GtkWidget *widget,
int x, int y, int w, int h)
{
GdkGC *gc = gdk_gc_new(widget->window);
#ifdef USE_CAIRO
gdk_gc_set_foreground(gc, &fe->background);
#else
gdk_gc_set_foreground(gc, &fe->colours[fe->backgroundindex]);
#endif
if (x < fe->ox) {
gdk_draw_rectangle(widget->window, gc,
TRUE, x, y, fe->ox - x, h);
w -= (fe->ox - x);
x = fe->ox;
}
if (y < fe->oy) {
gdk_draw_rectangle(widget->window, gc,
TRUE, x, y, w, fe->oy - y);
h -= (fe->oy - y);
y = fe->oy;
}
if (w > fe->pw) {
gdk_draw_rectangle(widget->window, gc,
TRUE, x + fe->pw, y, w - fe->pw, h);
w = fe->pw;
}
if (h > fe->ph) {
gdk_draw_rectangle(widget->window, gc,
TRUE, x, y + fe->ph, w, h - fe->ph);
h = fe->ph;
}
gdk_draw_pixmap(widget->window, gc, fe->pixmap,
x - fe->ox, y - fe->oy, x, y, w, h);
gdk_gc_unref(gc);
}
/* ----------------------------------------------------------------------
* Pango font functions.
*/
#ifdef USE_PANGO
static void add_font(frontend *fe, int index, int fonttype, int fontsize)
{
/*
* Use Pango to find the closest match to the requested
* font.
*/
PangoFontDescription *fd;
fd = pango_font_description_new();
/* `Monospace' and `Sans' are meta-families guaranteed to exist */
pango_font_description_set_family(fd, fonttype == FONT_FIXED ?
"Monospace" : "Sans");
pango_font_description_set_weight(fd, PANGO_WEIGHT_BOLD);
/*
* I found some online Pango documentation which
* described a function called
* pango_font_description_set_absolute_size(), which is
* _exactly_ what I want here. Unfortunately, none of
* my local Pango installations have it (presumably
* they're too old), so I'm going to have to hack round
* it by figuring out the point size myself. This
* limits me to X and probably also breaks in later
* Pango installations, so ideally I should add another
* CHECK_VERSION type ifdef and use set_absolute_size
* where available. All very annoying.
*/
#ifdef HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION
pango_font_description_set_absolute_size(fd, PANGO_SCALE*fontsize);
#else
{
Display *d = GDK_DISPLAY();
int s = DefaultScreen(d);
double resolution =
(PANGO_SCALE * 72.27 / 25.4) *
((double) DisplayWidthMM(d, s) / DisplayWidth (d, s));
pango_font_description_set_size(fd, resolution * fontsize);
}
#endif
fe->fonts[index].desc = fd;
}
static void align_and_draw_text(frontend *fe,
int index, int align, int x, int y,
const char *text)
{
PangoLayout *layout;
PangoRectangle rect;
layout = make_pango_layout(fe);
/*
* Create a layout.
*/
pango_layout_set_font_description(layout, fe->fonts[index].desc);
pango_layout_set_text(layout, text, strlen(text));
pango_layout_get_pixel_extents(layout, NULL, &rect);
if (align & ALIGN_VCENTRE)
rect.y -= rect.height / 2;
else
rect.y -= rect.height;
if (align & ALIGN_HCENTRE)
rect.x -= rect.width / 2;
else if (align & ALIGN_HRIGHT)
rect.x -= rect.width;
draw_pango_layout(fe, layout, rect.x + x, rect.y + y);
g_object_unref(layout);
}
#endif
/* ----------------------------------------------------------------------
* Old-fashioned font functions.
*/
#ifndef USE_PANGO
static void add_font(int index, int fonttype, int fontsize)
{
/*
* In GTK 1.2, I don't know of any plausible way to
* pick a suitable font, so I'm just going to be
* tedious.
*/
fe->fonts[i].font = gdk_font_load(fonttype == FONT_FIXED ?
"fixed" : "variable");
}
static void align_and_draw_text(int index, int align, int x, int y,
const char *text)
{
int lb, rb, wid, asc, desc;
/*
* Measure vertical string extents with respect to the same
* string always...
*/
gdk_string_extents(fe->fonts[i].font,
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
&lb, &rb, &wid, &asc, &desc);
if (align & ALIGN_VCENTRE)
y += asc - (asc+desc)/2;
else
y += asc;
/*
* ... but horizontal extents with respect to the provided
* string. This means that multiple pieces of text centred
* on the same y-coordinate don't have different baselines.
*/
gdk_string_extents(fe->fonts[i].font, text,
&lb, &rb, &wid, &asc, &desc);
if (align & ALIGN_HCENTRE)
x -= wid / 2;
else if (align & ALIGN_HRIGHT)
x -= wid;
/*
* Actually draw the text.
*/
gdk_draw_string(fe->pixmap, fe->fonts[i].font, fe->gc, x, y, text);
}
#endif
/* ----------------------------------------------------------------------
* The exported drawing functions.
*/
void gtk_start_draw(void *handle)
{
frontend *fe = (frontend *)handle;
fe->bbox_l = fe->w;
fe->bbox_r = 0;
fe->bbox_u = fe->h;
fe->bbox_d = 0;
setup_drawing(fe);
}
void gtk_clip(void *handle, int x, int y, int w, int h)
{
frontend *fe = (frontend *)handle;
do_clip(fe, x, y, w, h);
}
void gtk_unclip(void *handle)
{
frontend *fe = (frontend *)handle;
do_unclip(fe);
}
void gtk_draw_text(void *handle, int x, int y, int fonttype, int fontsize,
int align, int colour, char *text)
{
frontend *fe = (frontend *)handle;
int i;
/*
* Find or create the font.
*/
for (i = 0; i < fe->nfonts; i++)
if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
break;
if (i == fe->nfonts) {
if (fe->fontsize <= fe->nfonts) {
fe->fontsize = fe->nfonts + 10;
fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
}
fe->nfonts++;
fe->fonts[i].type = fonttype;
fe->fonts[i].size = fontsize;
add_font(fe, i, fonttype, fontsize);
}
/*
* Do the job.
*/
set_colour(fe, colour);
align_and_draw_text(fe, i, align, x, y, text);
}
void gtk_draw_rect(void *handle, int x, int y, int w, int h, int colour)
{
frontend *fe = (frontend *)handle;
set_colour(fe, colour);
do_draw_rect(fe, x, y, w, h);
}
void gtk_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour)
{
frontend *fe = (frontend *)handle;
set_colour(fe, colour);
do_draw_line(fe, x1, y1, x2, y2);
}
void gtk_draw_thick_line(void *handle, float thickness,
float x1, float y1, float x2, float y2, int colour)
{
frontend *fe = (frontend *)handle;
set_colour(fe, colour);
do_draw_thick_line(fe, thickness, x1, y1, x2, y2);
}
void gtk_draw_poly(void *handle, int *coords, int npoints,
int fillcolour, int outlinecolour)
{
frontend *fe = (frontend *)handle;
do_draw_poly(fe, coords, npoints, fillcolour, outlinecolour);
}
void gtk_draw_circle(void *handle, int cx, int cy, int radius,
int fillcolour, int outlinecolour)
{
frontend *fe = (frontend *)handle;
do_draw_circle(fe, cx, cy, radius, fillcolour, outlinecolour);
}
blitter *gtk_blitter_new(void *handle, int w, int h)
{
blitter *bl = snew(blitter);
setup_blitter(bl, w, h);
bl->w = w;
bl->h = h;
return bl;
}
void gtk_blitter_free(void *handle, blitter *bl)
{
teardown_blitter(bl);
sfree(bl);
}
void gtk_blitter_save(void *handle, blitter *bl, int x, int y)
{
frontend *fe = (frontend *)handle;
do_blitter_save(fe, bl, x, y);
bl->x = x;
bl->y = y;
}
void gtk_blitter_load(void *handle, blitter *bl, int x, int y)
{
frontend *fe = (frontend *)handle;
if (x == BLITTER_FROMSAVED && y == BLITTER_FROMSAVED) {
x = bl->x;
y = bl->y;
}
do_blitter_load(fe, bl, x, y);
}
void gtk_draw_update(void *handle, int x, int y, int w, int h)
{
frontend *fe = (frontend *)handle;
if (fe->bbox_l > x ) fe->bbox_l = x ;
if (fe->bbox_r < x+w) fe->bbox_r = x+w;
if (fe->bbox_u > y ) fe->bbox_u = y ;
if (fe->bbox_d < y+h) fe->bbox_d = y+h;
}
void gtk_end_draw(void *handle)
{
frontend *fe = (frontend *)handle;
teardown_drawing(fe);
if (fe->bbox_l < fe->bbox_r && fe->bbox_u < fe->bbox_d) {
repaint_rectangle(fe, fe->area,
fe->bbox_l - 1 + fe->ox,
fe->bbox_u - 1 + fe->oy,
fe->bbox_r - fe->bbox_l + 2,
fe->bbox_d - fe->bbox_u + 2);
}
}
#ifdef USE_PANGO
char *gtk_text_fallback(void *handle, const char *const *strings, int nstrings)
{
/*
* We assume Pango can cope with any UTF-8 likely to be emitted
* by a puzzle.
*/
return dupstr(strings[0]);
}
#endif