File: cgbuts.c

package info (click to toggle)
cgoban 1.9.14-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,360 kB
  • ctags: 3,910
  • sloc: ansic: 36,036; sh: 702; makefile: 252
file content (1004 lines) | stat: -rw-r--r-- 30,868 bytes parent folder | download | duplicates (7)
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
1001
1002
1003
1004
/*
 * $Source: /cvsroot/cgoban1/cgoban1/src/cgbuts.c,v $
 * $Revision: 1.2 $
 * $Author: wmshub $
 * $Date: 2002/05/31 23:40:54 $
 *
 * src/cgbuts.c, part of Complete Goban (game program)
 * Copyright (C) 1995-1996 William Shubert.
 * See "configure.h.in" for more copyright information.
 *
 * This code extends the wmslib/but library to add special buttons needed
 *   for cgoban.
 *
 * The globe data was extracted from the CIA World Data Bank II map database.
 *   via XEarth.  See the main copyright for information about this.
 */

#include <math.h>
#include <wms.h>
#include <but/but.h>
#include <but/net.h>
#include <wms/rnd.h>
#include <wms/str.h>
#include "goBoard.h"
#include "drawStone.h"
#ifdef  _CGBUTS_H_
  Levelization Error.
#endif
#include "cgbuts.h"


/**********************************************************************
 * Forward Declarations
 **********************************************************************/
static ButOut  grid_mmove(But *but, int x, int y);
static ButOut  grid_mleave(But *but);
static ButOut  grid_mpress(But *but, int butnum, int x, int y);
static ButOut  grid_mrelease(But *but, int butnum, int x, int y);
static void  grid_draw(But *but, int x,int y,int w,int h);
static ButOut  grid_destroy(But *but);
static void  grid_newflags(But *but, uint fl);
static ButOut  gridNetPress(But *but, void *buf, int bufLen);
static void  drawRewChar(void *packet, ButWin *win,
			 int x, int y, int w, int h);
static void  drawBackChar(void *packet, ButWin *win,
			  int x, int y, int w, int h);
static void  drawFwdChar(void *packet, ButWin *win,
			 int x, int y, int w, int h);
static void  drawFfChar(void *packet, ButWin *win,
			int x, int y, int w, int h);
static void  drawWStoneChar(void *packet, ButWin *win,
			    int x, int y, int w, int h);
static void  drawBStoneChar(void *packet, ButWin *win,
			    int x, int y, int w, int h);
static void  grid_morePixmaps(Cgbuts *b, int newNumPixmaps);
static void  gobanPic_draw(But *but, int x,int y, int w,int h);
static ButOut  gobanPic_destroy(But *but);
static void  stoneGroup_draw(But *but, int x,int y, int w,int h);
static ButOut  stoneGroup_destroy(But *but);
static void  computerPic_draw(But *but, int x,int y, int w,int h);
static ButOut  computerPic_destroy(But *but);
static void  toolPic_draw(But *but, int x,int y, int w,int h);
static ButOut  toolPic_destroy(But *but);


/**********************************************************************
 * Global Variables
 **********************************************************************/
const char  *cgbuts_stoneChar[2] = {CGBUTS_WSTONECHAR, CGBUTS_BSTONECHAR};

static const ButAction  grid_action = {
  grid_mmove, grid_mleave, grid_mpress, grid_mrelease,
  NULL, NULL, grid_draw, grid_destroy, grid_newflags, gridNetPress};

static const ButAction  gobanPic_action = {
  NULL, NULL, NULL, NULL,
  NULL, NULL, gobanPic_draw, gobanPic_destroy, but_flags, NULL};

static const ButAction  stoneGroup_action = {
  NULL, NULL, NULL, NULL,
  NULL, NULL, stoneGroup_draw, stoneGroup_destroy, but_flags, NULL};

static const ButAction  computerPic_action = {
  NULL, NULL, NULL, NULL,
  NULL, NULL, computerPic_draw, computerPic_destroy, but_flags, NULL};

static const ButAction  toolPic_action = {
  NULL, NULL, NULL, NULL,
  NULL, NULL, toolPic_draw, toolPic_destroy, but_flags, NULL};


/**********************************************************************
 * Functions
 **********************************************************************/
void  cgbuts_init(Cgbuts *cgbuts, ButEnv *env, Rnd *rnd, int color,
		  bool  hold, bool hiContrast, int timeWarn)  {
  MAGIC_SET(cgbuts);

  cgbuts->env = env;
  cgbuts->dpyDepth = DefaultDepth(butEnv_dpy(env),
				  DefaultScreen(butEnv_dpy(env)));
  cgbuts->dpyRootWindow = RootWindow(butEnv_dpy(env),
				     DefaultScreen(butEnv_dpy(env)));
  cgbuts->pics = NULL;
  cgbuts->numPics = 0;
  cgbuts->rnd = rnd;
  cgbuts->color = color;
  cgbuts->holdStart = 0;
  cgbuts->hiContrast = hiContrast;
  cgbuts->holdEnabled = hold;
  cgbuts->timeWarn = timeWarn;

  butEnv_setChar(env, 1.0, CGBUTS_REWCHAR, drawRewChar, NULL);
  butEnv_setChar(env, 0.5, CGBUTS_BACKCHAR, drawBackChar, NULL);
  butEnv_setChar(env, 0.5, CGBUTS_FWDCHAR, drawFwdChar, NULL);
  butEnv_setChar(env, 1.0, CGBUTS_FFCHAR, drawFfChar, NULL);
  butEnv_setChar(env, 1.0, CGBUTS_WSTONECHAR, drawWStoneChar, cgbuts);
  butEnv_setChar(env, 1.0, CGBUTS_BSTONECHAR, drawBStoneChar, cgbuts);
}


But  *grid_create(Cgbuts *b, ButOut (*func)(But *but), void *packet,
		  ButWin *win, int layer, int flags, int pos)  {
  Grid  *grid;
  But  *but;

  grid = wms_malloc(sizeof(Grid));
  MAGIC_SET(grid);
  but = but_create(win, grid, &grid_action);
  but->uPacket = packet;
  but->layer = layer;
  but->flags = flags;

  grid->b = b;
  grid->callback = func;
  grid->lineGroup = gridLines_center;
  grid->starPoint = FALSE;
  grid->pos = pos;
  grid->pressColor = goStone_black;
  grid->stoneVersion = rnd_int(b->rnd) % CGBUTS_NUMWHITE;
  grid->ptype = goStone_empty;
  grid->grey = FALSE;
  grid->holdRequired = FALSE;
  grid->markType = goMark_none;
  grid->markAux = 0;
  but_init(but);
  return(but);
}


void  grid_setStone(But *but, GoStone piece, bool grey)  {
  Grid  *grid = but->iPacket;

  assert(but->action == &grid_action);
  if ((piece != grid->ptype) || (grey != grid->grey))  {
    grid->ptype = piece;
    grid->grey = grey;
    but_draw(but);
  }
}


void  grid_setMark(But *but, GoMarkType mark, int aux)  {
  Grid  *grid = but->iPacket;

  assert(but->action == &grid_action);
  if ((grid->markType != mark) || (grid->markAux != aux))  {
    grid->markType = mark;
    grid->markAux = aux;
    but_draw(but);
  }
}


static ButOut  grid_destroy(But *but)  {
  Grid  *grid = but->iPacket;

  wms_free(grid);
  return(0);
}


static void  grid_newflags(But *but, uint fl)  {
  uint  ofl = but->flags;

  but->flags = fl;
  if ((ofl & (BUT_TWITCHED|BUT_PRESSED|BUT_NETTWITCH|BUT_NETPRESS)) !=
      (fl & (BUT_TWITCHED|BUT_PRESSED|BUT_NETTWITCH|BUT_NETPRESS)))  {
    but_draw(but);
  }
}


static ButOut  grid_mmove(But *but, int x, int y)  {
  uint  newflags = but->flags, retval = BUTOUT_CAUGHT;
  Grid  *grid;

  if (!(but->flags & BUT_PRESSABLE))
    return(BUTOUT_CAUGHT);
  if ((x < but->x) || (y < but->y) ||
      (x >= but->x + but->w) || (y >= but->y + but->h))  {
    newflags &= ~BUT_TWITCHED;
    if (newflags != but->flags)  {
      butEnv_setCursor(but->win->env, but, butCur_idle);
      but_newFlags(but, newflags);
    }
    if (!(newflags & BUT_LOCKED))
      retval = 0;
  } else  {
    newflags |= BUT_TWITCHED;
    if (newflags != but->flags)  {
      grid = but->iPacket;
      assert(MAGIC(grid));
      grid->b->holdStart = butWin_env(but_win(but))->eventTime;
      butEnv_setCursor(but->win->env, but, butCur_twitch);
      but_newFlags(but, newflags);
    }
  }
  return(retval);
}
      

static ButOut  grid_mleave(But *but)  {
  int  newflags = but->flags;

  newflags &= ~BUT_TWITCHED;
  if (newflags != but->flags)  {
    butEnv_setCursor(but->win->env, but, butCur_idle);
    but_newFlags(but, newflags);
  }
  return(BUTOUT_CAUGHT);
}
      

static ButOut  grid_mpress(But *but, int butnum, int x, int y)  {
  int  newflags = but->flags, retval = BUTOUT_CAUGHT;
  Grid  *grid;

  if (butnum == 1)  {
    grid = but->iPacket;
    assert(MAGIC(grid));
    /*
     * Check to make sure that the user has been holding the mouse there
     *   long enough.
     * Holding the mouse is only necessare if all of these are true:
     *   - Cgbuts.holdEnabled is TRUE.  This lets the user turn on and off
     *     all holds globally.
     *   - This particular button has hold enabled.  This lets cgoban turn
     *     off hold when you are in a situation where an undo is no problem.
     *   - No modifiers are pressed.  Same reason as the previous.
     */
    if (((but->win->env->keyModifiers & (ShiftMask | ControlMask)) == 0) &&
	grid->holdRequired && grid->b->holdEnabled &&
	(butWin_env(but_win(but))->eventTime - grid->b->holdStart <
	 CGBUTS_HOLDTIME))
      retval |= BUTOUT_ERR;
    else
      newflags |= BUT_PRESSED|BUT_LOCKED;
  } else
    retval |= BUTOUT_ERR;
  /*
   * if (!(but->flags & BUT_PRESSED) && (newflags & BUT_PRESSED))
   *   snd_play(&pe_move_snd);
   */
  if (newflags != but->flags)
    but_newFlags(but, newflags);
  return(retval);
}
      

static ButOut  grid_mrelease(But *but, int butnum, int x, int y)  {
  int  newflags = but->flags, retval = BUTOUT_CAUGHT;
  Grid  *grid = but->iPacket;
  bool  makeCallback = FALSE;

  if (butnum != 1)
    return(retval);
  if (but->flags & BUT_TWITCHED)  {
    if (but->flags & BUT_PRESSED)  {
      if (but->id)
	butRnet_butSpecSend(but, NULL, 0);
      makeCallback = TRUE;
    }
  } else
    retval |= BUTOUT_ERR;
  newflags &= ~(BUT_PRESSED|BUT_LOCKED);
  if (newflags != but->flags)
    but_newFlags(but, newflags);
  if (makeCallback)
    retval |= grid->callback(but);
  return(retval);
}
      

static void  grid_draw(But *but, int x,int y,int w,int h)  {
  static const struct  {
    int  numPoints, points[5];
  } lineFormats[9] = {
    {3, {4,2,3}},     /* ul */
    {4, {1,3,2,4}},   /* top */
    {3, {1,2,4}},     /* ur */
    {4, {0,4,2,3}},   /* left */
    {5, {1,3,2,0,4}}, /* center */
    {4, {0,4,2,1}},   /* right */
    {3, {0,2,3}},     /* ll */
    {4, {1,3,2,0}},   /* bottom */
    {3, {0,2,1}}};    /* lr */
  Grid  *grid = but->iPacket;
  int  i, lineWidth, starSize, flags = but->flags;
  Display  *dpy;
  XPoint  points[5], lines[5];
  GC  gc = but->win->env->gc;
  GoStone  markColor;
  Cgbuts  *b = grid->b;
  
  assert(MAGIC(b));
  dpy = but->win->env->dpy;
  /* Points are:
   *     0
   *     |
   * 1 - 2 - 3
   *     |
   *     4
   */
  if ((grid->markType != goMark_letter) && (grid->markType != goMark_number) &&
      (grid->lineGroup != gridLines_none) &&
      ((grid->ptype == goStone_empty) || grid->grey))  {
    butEnv_setXFg(but->win->env, BUT_FG);
    lineWidth = (but->w + 15) / 30;
    if (lineWidth < 1)
      lineWidth = 1;
    XSetLineAttributes(dpy, gc, lineWidth, LineSolid, CapButt, JoinMiter);
    points[0].x = points[2].x = points[4].x = but->x + but->w/2;
    points[1].x = but->x;
    points[3].x = but->x + but->w;
    points[0].y = but->y;
    points[1].y = points[2].y = points[3].y = but->y + but->h/2;
    points[4].y = but->y + but->h;
    for (i = 0;  i < lineFormats[grid->lineGroup].numPoints;  ++i)
      lines[i] = points[lineFormats[grid->lineGroup].points[i]];
    XDrawLines(dpy, but->win->win, gc, lines,
	       lineFormats[grid->lineGroup].numPoints, CoordModeOrigin);
    if (grid->starPoint)  {
      butEnv_setXFg(but->win->env, BUT_FG);
      starSize = (((but->w + 6 - lineWidth*6) / 12) * 2) + lineWidth+1;
      if (starSize > 1)  {
	XFillArc(dpy, but->win->win, gc, but->x+(but->w - starSize)/2,
		 but->y + (but->h-starSize)/2, starSize, starSize,
		 0, 360*64);
      }
    }
  }
  if ((grid->ptype == goStone_empty) &&
      !(flags & (BUT_TWITCHED|BUT_NETTWITCH)) &&
      (grid->markType == goMark_none))
    return;
  markColor = grid->ptype;
  if (grid->ptype != goStone_empty)  {
    if (flags & (BUT_PRESSED|BUT_NETPRESS))  {
      /* Pressed.  Invert the greyness. */
      cgbuts_drawp(b, but->win, grid->ptype, !grid->grey, but->x, but->y,
		   but->w, grid->stoneVersion, x,y,w,h);
    } else  {
      cgbuts_drawp(b, but->win, grid->ptype, grid->grey, but->x, but->y,
		   but->w, grid->stoneVersion, x,y,w,h);
      if (flags & (BUT_TWITCHED|BUT_NETTWITCH))  {
	/* Twitched.  Draw a small stone in the middle of it. */
	if (grid->grey)  {
	  cgbuts_drawp(b, but->win, grid->ptype, FALSE,
		       but->x+(but->w+1)/4, but->y+(but->w+1)/4,
		       but->w/2, grid->stoneVersion, x,y,w,h);
	} else  {
	  cgbuts_drawp(b, but->win, goStone_opponent(grid->ptype), TRUE,
		       but->x+(but->w+1)/4, but->y+(but->w+1)/4,
		       but->w/2, grid->stoneVersion, x,y,w,h);
	}
      }
    }
  } else  {
    assert(grid->ptype == goStone_empty);
    if (flags & (BUT_PRESSED|BUT_NETPRESS|BUT_TWITCHED|BUT_NETTWITCH))  {
      markColor = grid->pressColor;
      if (flags & (BUT_PRESSED |BUT_NETPRESS))  {
	cgbuts_drawp(b, but->win, grid->pressColor, FALSE,
		     but->x, but->y, but->w, grid->stoneVersion, x,y,w,h);
      } else  {
	/* Grid is twitched only. */
	cgbuts_drawp(b, but->win, grid->pressColor, TRUE,
		     but->x, but->y, but->w, grid->stoneVersion, x,y,w,h);
      }
    }
  }
  if ((grid->markType != goMark_none) &&
      (!(flags & (BUT_PRESSED|BUT_NETPRESS))))  {
    cgbuts_markPiece(b, but->win, markColor, grid->markType, grid->markAux,
		     but->x, but->y, but->w, grid->stoneVersion, x,y,w,h);
  }
}


static ButOut  gridNetPress(But *but, void *buf, int bufLen)  {
  Grid  *grid = but->iPacket;

  assert(bufLen == 0);
  /* snd_play(&pe_move_snd); */
  return(grid->callback(but));
}


void  cgbuts_drawp(Cgbuts *b, ButWin *win, GoStone piece, bool grey,
		   int x, int y, int size, int stoneVersion,
		   int dx,int dy, int dw,int dh)  {
  ButEnv  *env = win->env;
  Display  *dpy = env->dpy;
  GC  gc = env->gc2;
  int  copyStart;
  int  worldNum = 0;

  if (size >= b->numPics)
    grid_morePixmaps(b, size + 1);
  if (b->pics[size].stonePixmaps == None)
    drawStone_newPics(env, b->rnd, CGBUTS_GREY(0),
		      &b->pics[size].stonePixmaps,
		      &b->pics[size].maskBitmaps, size,
		      butEnv_isColor(env) && !b->hiContrast);
  if (stoneVersion >= CGBUTS_NUMWHITE)  {
    worldNum = 2 + stoneVersion / CGBUTS_NUMWHITE;
    stoneVersion %= CGBUTS_NUMWHITE;
  }
  
  if ((dx >= x+size) || (dy >= y+size) ||
      (dx+dw <= x) || (dy+dh <= y))
    return;
  if (dx < x)  {
    dw -= x - dx;
    dx = x;
  }
  if (dy < y)  {
    dh -= y - dy;
    dy = y;
  }
  if (dx + dw > x + size)
    dw = x + size - dx;
  if (dy + dh > y + size)
    dh = y + size - dy;
  XSetClipMask(dpy, gc, b->pics[size].maskBitmaps);
  if (grey)  {
    if ((x & 1) == (y & 1))
      XSetClipOrigin(dpy, gc, x - size - win->xOff, y - win->yOff);
    else
      XSetClipOrigin(dpy, gc, x - size * 2 - win->xOff, y - win->yOff);
  } else  {
    XSetClipOrigin(dpy, gc, x - win->xOff, y - win->yOff);
  }
  copyStart = 0;
  if (piece == goStone_white)
    copyStart = (stoneVersion + 1) * size;
  XCopyArea(dpy, b->pics[size].stonePixmaps, win->win, gc,
	    copyStart+dx-x,dy-y, dw,dh, dx - win->xOff, dy - win->yOff);
  if (worldNum)  {
    copyStart = 0;
    if (piece == goStone_black)
      copyStart = (stoneVersion + 1) * size;
    XSetClipOrigin(dpy, gc, x-size*worldNum - win->xOff, y - win->yOff);
    XCopyArea(dpy, b->pics[size].stonePixmaps, win->win, gc,
	      copyStart+dx-x,dy-y, dw,dh, dx - win->xOff, dy - win->yOff);
  }
  XSetClipMask(dpy, gc, None);
}


void  cgbuts_markPiece(Cgbuts *b, ButWin *win, GoStone piece,
		       GoMarkType markType, int markAux,
		       int x, int y, int w, int stoneVersion,
		       int dx,int dy, int dw,int dh)  {
  ButEnv *env = win->env;
  Display *dpy = env->dpy;
  GC  gc = env->gc;
  int  lw = 0;
  XPoint  points[4];
  int  off;
  char  text[4];

  if ((markType >= goMark_lastMove) && (markType <= goMark_circle))  {
    /* Must set up line attributes. */
    lw = (w + 7) / 15;
    if (lw < 1)
      lw = 1;
    XSetLineAttributes(dpy, gc, lw, LineSolid, CapButt, JoinMiter);
  }
  if (piece == goStone_black)
    butEnv_setXFg(env, BUT_WHITE);
  else
    butEnv_setXFg(env, BUT_BLACK);
  switch(markType)  {
  case goMark_none:
    break;
  case goMark_triangle:
    points[0].x = x+w/2 - win->xOff;
    points[0].y = y+lw - win->yOff;
    points[1].x = x+((w*0.9330127)-(lw*0.8660254)+0.5) - win->xOff;
    points[1].y = y+((w*0.75)-(lw*0.5)+0.5) - win->yOff;
    points[2].x = x+((w*0.0669873)+(lw*0.8660254)+0.5) - win->xOff;
    points[2].y = points[1].y;
    points[3] = points[0];
    XDrawLines(dpy, win->win, gc, points, 4, CoordModeOrigin);
    break;
  case goMark_x:
    off = (int)(w*0.14644661+lw*0.5+1.5);
    XDrawLine(dpy, win->win, gc,
	      x + off - win->xOff, y + off - win->yOff,
	      x + w - off - win->xOff-(lw&1), y + w - off - win->yOff-(lw&1));
    XDrawLine(dpy, win->win, gc,
	      x + off - win->xOff,  y + w - off  - win->yOff-(lw&1),
	      x + w - off - win->xOff-(lw&1), y + off - win->yOff);
    break;
  case goMark_ko:
  case goMark_square:
    off = (int)(w*0.14644661+lw*0.5+0.5);
    XDrawRectangle(dpy, win->win, gc, x + off - win->xOff, y + off - win->yOff,
		   w-off*2-(lw&1),w-off*2-(lw&1));
    break;
  case goMark_lastMove:
  case goMark_circle:
    off = (w + 1) / 4;
    XDrawArc(dpy, win->win, gc, x + off - win->xOff, y + off - win->yOff,
	     w-off*2-1,w-off*2-1, 0,360*64);
    break;
  case goMark_smBlack:
  case goMark_smWhite:
    if (markType == goMark_smBlack)
      cgbuts_drawp(b, win, goStone_black, FALSE, x+(w+1)/4,y+(w+1)/4, w/2,
		   stoneVersion, dx,dy,dw,dh);
    else
      cgbuts_drawp(b, win, goStone_white, FALSE, x+(w+1)/4,y+(w+1)/4, w/2,
		   stoneVersion, dx,dy,dw,dh);
    break;
  case goMark_letter:
    text[0] = markAux;
    text[1] = '\0';
    butWin_write(win, x + (w - butEnv_textWidth(env, text, 1)) / 2,
		 y + (w - butEnv_fontH(env, 1)) / 2, text, 1);
    break;
  case goMark_number:
    assert(markAux >= 0);
    if (markAux > 999)
      markAux = 999;
    if ((markAux > 99) && (w < butEnv_fontH(env, 0) * 2))
      sprintf(text, "%02d", markAux % 100);
    else
      sprintf(text, "%d", markAux);
    butWin_write(win, x + (w - butEnv_textWidth(env, text, 1)) / 2,
		 y + (w - butEnv_fontH(env, 1)) / 2, text, 1);
    break;
  }
}


void  grid_setPress(But *but, GoStone pressColor)  {
  Grid  *grid;

  assert(but->action == &grid_action);
  if (pressColor == goStone_empty)  {
    if (but->flags & BUT_PRESSABLE)
      but_setFlags(but, BUT_NOPRESS);
  } else  {
    if (!(but->flags & BUT_PRESSABLE))
      but_setFlags(but, BUT_PRESSABLE);
    grid = but->iPacket;
    grid->pressColor = pressColor;
  }
  if (but->flags & BUT_TWITCHED)
    but_draw(but);
}


static void  drawRewChar(void *packet, ButWin *win,
			 int x, int y, int w, int h)  {
  XPoint  points[8];
  Display  *dpy = butEnv_dpy(butWin_env(win));
  Window  xwin = butWin_xwin(win);
  GC  gc = butEnv_gc(butWin_env(win));

  h &= ~1;
  w &= ~1;
  points[0].x = x;
  points[0].y = y+h/2;
  points[1].x = x+w/2;
  points[1].y = y;
  points[2].x = x+w/2;
  points[2].y = y+h/2;
  points[3].x = x+w;
  points[3].y = y;
  points[4].x = x+w;
  points[4].y = y+h;
  points[5] = points[2];
  points[6].x = x+w/2;
  points[6].y = y+h;
  points[7] = points[0];
  XFillPolygon(dpy, xwin, gc, points, 8, Nonconvex, CoordModeOrigin);
}


static void  drawBackChar(void *packet, ButWin *win,
			  int x, int y, int w, int h)  {
  XPoint  points[4];
  Display  *dpy = butEnv_dpy(butWin_env(win));
  Window  xwin = butWin_xwin(win);
  GC  gc = butEnv_gc(butWin_env(win));

  h &= ~1;
  points[0].x = x;
  points[0].y = y+h/2;
  points[1].x = x+w;
  points[1].y = y;
  points[2].x = x+w;
  points[2].y = y+h;
  points[3] = points[0];
  XFillPolygon(dpy, xwin, gc, points, 4, Nonconvex, CoordModeOrigin);
}


static void  drawFwdChar(void *packet, ButWin *win,
			 int x, int y, int w, int h)  {
  XPoint  points[4];
  Display  *dpy = butEnv_dpy(butWin_env(win));
  Window  xwin = butWin_xwin(win);
  GC  gc = butEnv_gc(butWin_env(win));

  h &= ~1;
  points[0].x = x;
  points[0].y = y;
  points[1].x = x+w;
  points[1].y = y+h/2;
  points[2].x = x;
  points[2].y = y+h;
  points[3] = points[0];
  XFillPolygon(dpy, xwin, gc, points, 4, Nonconvex, CoordModeOrigin);
}


static void  drawFfChar(void *packet, ButWin *win,
			int x, int y, int w, int h)  {
  XPoint  points[8];
  Display  *dpy = butEnv_dpy(butWin_env(win));
  Window  xwin = butWin_xwin(win);
  GC  gc = butEnv_gc(butWin_env(win));

  h &= ~1;
  w &= ~1;
  points[0].x = x;
  points[0].y = y;
  points[1].x = x+w/2;
  points[1].y = y+h/2;
  points[2].x = x+w/2;
  points[2].y = y;
  points[3].x = x+w;
  points[3].y = y+h/2;
  points[4].x = x+w/2;
  points[4].y = y+h;
  points[5] = points[1];
  points[6].x = x;
  points[6].y = y+h;
  points[7] = points[0];
  XFillPolygon(dpy, xwin, gc, points, 8, Nonconvex, CoordModeOrigin);
}


static void  drawWStoneChar(void *packet, ButWin *win,
			    int x, int y, int w, int h)  {
  Cgbuts  *b = packet;

  cgbuts_drawp(b, win, goStone_white, FALSE, x,y, w, 1, x,y,w,h);
}


static void  drawBStoneChar(void *packet, ButWin *win,
			    int x, int y, int w, int h)  {
  Cgbuts  *b = packet;

  cgbuts_drawp(b, win, goStone_black, FALSE, x,y, w, 1, x,y,w,h);
}


static void  grid_morePixmaps(Cgbuts *b, int newNumPics)  {
  int  i;
  CgbutsPic  *newPics;

  assert(MAGIC(b));
  newPics = wms_malloc(newNumPics * sizeof(CgbutsPic));
  for (i = 0;  i < b->numPics;  ++i)  {
    newPics[i] = b->pics[i];
  }
  for (;  i < newNumPics;  ++i)  {
    newPics[i].stonePixmaps = None;
    newPics[i].maskBitmaps = None;
  }
  b->numPics = newNumPics;
  if (b->pics)  {
    wms_free(b->pics);
  }
  b->pics = newPics;
}

    
But  *gobanPic_create(Cgbuts *b, ButWin *win, int layer, int flags)  {
  But  *but;

  but = but_create(win, b, &gobanPic_action);
  but->uPacket = NULL;
  but->layer = layer;
  but->flags = flags;
  return(but);
}


static void  gobanPic_draw(But *but, int x,int y, int w,int h)  {
  ButWin  *win = but->win;
  ButEnv  *env = win->env;
  int  w13 = (but->w+1)/3;
  int  w16 = (but->w + 2) / 6;
  int  stonew = but->w/3;
  int  bw = butEnv_stdBw(env);
  int  cgtop = but->y + (but->w - stonew - bw - (w13 + w16)) / 2;
  int  bdtop = cgtop + stonew + bw;
  XPoint  points[4];

  butEnv_setXFg(env, CGBUTS_COLORBOARD(0));
  XFillRectangle(env->dpy, win->win, env->gc, but->x,bdtop+w13,
		 but->w,w16);
  butEnv_setXFg(env, CGBUTS_COLORBOARD(225));
  points[0].x = but->x;
  points[0].y = bdtop+w13;
  points[1].x = but->x + w16;
  points[1].y = bdtop;
  points[2].x = but->x + but->w - w16;
  points[2].y = points[1].y;
  points[3].x = but->x + but->w;
  points[3].y = points[0].y;
  XFillPolygon(env->dpy, win->win, env->gc, points, 4,
	       Convex, CoordModeOrigin);
}


static ButOut  gobanPic_destroy(But *but)  {
  return(0);
}


But  *stoneGroup_create(Cgbuts *b, ButWin *win, int layer, int flags)  {
  But  *but;

  but = but_create(win, b, &stoneGroup_action);
  but->uPacket = NULL;
  but->layer = layer;
  but->flags = flags;
  return(but);
}


static void  stoneGroup_draw(But *but, int x,int y, int w,int h)  {
  ButWin  *win = but->win;
  int  stoneW;

  stoneW = but->w >> 1;
  cgbuts_drawp(but->iPacket, win, goStone_black, FALSE,
	       but->x, but->y, stoneW, 0, x, y, w, h);
  cgbuts_drawp(but->iPacket, win, goStone_white, FALSE,
	       but->x + stoneW, but->y, stoneW, 1, x, y, w, h);
  cgbuts_drawp(but->iPacket, win, goStone_white, FALSE,
	       but->x, but->y + stoneW, stoneW, 2, x, y, w, h);
  cgbuts_drawp(but->iPacket, win, goStone_black, FALSE,
	       but->x + stoneW, but->y + stoneW, stoneW, 3, x, y, w, h);
}


static ButOut  stoneGroup_destroy(But *but)  {
  return(0);
}


But  *computerPic_create(Cgbuts *b, ButWin *win, int layer, int flags)  {
  But  *but;

  but = but_create(win, b, &computerPic_action);
  but->uPacket = NULL;
  but->layer = layer;
  but->flags = flags;
  return(but);
}


static void  computerPic_draw(But *but, int x,int y, int w,int h)  {
  XPoint  points[4];
  ButWin  *win = but->win;
  Display  *dpy = butEnv_dpy(butWin_env(win));
  Window  xwin = butWin_xwin(win);
  GC  gc = butEnv_gc(butWin_env(win));
  int  h2 = but->h / 2, h3 = but->h / 3, h12 = but->h / 12;
  int  w6 = but->w / 6, w9 = but->w / 9;

  x = but->x;
  y = but->y;
  w = but->w;
  h = but->h;
  y += (h - (h12 + h2 + h12 + h3)) / 2;
  points[0].x = x;
  points[0].y = y + h12 + h2 + h3;
  points[1].x = x + w6;
  points[1].y = y + h12 + h2 + h12;
  points[2].x = x + w - w6;
  points[2].y = y + h12 + h2 + h12;
  points[3].x = x + w;
  points[3].y = y + h12 + h2 + h3;
  butEnv_setXFg(butWin_env(win), BUT_BLACK);
  XFillPolygon(dpy, xwin, gc, points, 4, Nonconvex, CoordModeOrigin);
  XFillRectangle(dpy, xwin, gc, x+w6,y+h12, w-2*w6,h2);
  butEnv_setXFg(butWin_env(win), BUT_PBG);
  XFillRectangle(dpy, xwin, gc, x+w6+w9,y+h12*2, w-2*w6-2*w9,h2-h12*2);
}


static ButOut  computerPic_destroy(But *but)  {
  return(0);
}


But  *toolPic_create(Cgbuts *b, ButWin *win, int layer, int flags)  {
  But  *but;

  but = but_create(win, b, &toolPic_action);
  but->uPacket = NULL;
  but->layer = layer;
  but->flags = flags;
  return(but);
}


static void  toolPic_draw(But *but, int x,int y, int w,int h)  {
  XPoint  points[12];
  ButWin  *win = but->win;
  Display  *dpy = butEnv_dpy(butWin_env(win));
  Window  xwin = butWin_xwin(win);
  GC  gc = butEnv_gc(butWin_env(win));

  /* Size of hammer or screwdriver relative to whole picture. */
  const float  subBoxSize = 0.75;
  const float  bevel = 0.25;
  const float  hr2 = 0.70710678;  /* Half of Root 2 */

  /* Hammer constants. */
  const float  hHandleW = 0.1;
  const float  hJointW = 0.2;
  const float  hJointL = 0.2;
  const float  hClawL = 0.4;
  const float  hNeckW = 0.07;
  const float  hHeadL = 0.1;

  /* Screwdriver constants. */
  const float  sHandleW = 0.2;
  const float  sHandleL = 0.5;
  const float  sHEndW = 0.15;
  const float  sBladeW = 0.04;

  float  th;
  float  bx, by;

  x = but->x;
  y = but->y;
  h = but->h;
  th = h * subBoxSize;
  bx = x + h * (1.0 - subBoxSize);
  by = y + h * (1.0 - subBoxSize);

  /* The hammer's handle. */
  points[0].x = x;
  points[0].y = y + th * (1.0 - hHandleW) + 0.5;
  points[1].x = x + th * hHandleW + 0.5;
  points[1].y = points[0].y + (points[1].x - points[0].x);
  points[2].x = x + th * (1.0 - hJointL * hr2) + 0.5;
  points[2].y = points[1].y - (points[2].x - points[1].x);
  points[3].x = points[2].x - (points[1].x - points[0].x);
  points[3].y = points[2].y - (points[1].x - points[0].x);


  /* Add beveling to the handle. */
  points[4] = points[0];
  points[5].x = x + th * hHandleW * bevel + 0.5;
  points[5].y = points[4].y + (points[5].x - points[4].x);
  points[6].x = x + th * (1.0 - hJointL * hr2) + 0.5;
  points[6].y = points[5].y - (points[6].x - points[5].x);
  points[7] = points[3];

  points[9] = points[1];
  points[10] = points[2];
  points[8].x = points[9].x - th * hHandleW * bevel + 0.5;
  points[8].y = points[9].y - (points[9].x - points[8].x);
  points[11].x = points[10].x - (points[9].x - points[8].x);
  points[11].y = points[10].y - (points[9].x - points[8].x);

  butEnv_setXFg(butWin_env(win), CGBUTS_COLORBOARD(200));
  XFillPolygon(dpy, xwin, gc, points, 4, Nonconvex, CoordModeOrigin);
  butEnv_setXFg(butWin_env(win), CGBUTS_COLORBOARD(0));
  XFillPolygon(dpy, xwin, gc, points+4, 4, Nonconvex, CoordModeOrigin);
  butEnv_setXFg(butWin_env(win), CGBUTS_COLORBOARD(255));
  XFillPolygon(dpy, xwin, gc, points+8, 4, Nonconvex, CoordModeOrigin);

  /* The hammer's head. */
  points[0].x = x + th + 0.5;
  points[0].y = y + th * (hJointL * hr2) + 0.5;
  points[1].x = points[0].x - (points[0].y - y);
  points[1].y = y;
  points[2].x = x + th * (1.0 - hJointL * hr2 - hClawL) + 0.5;
  points[2].y = y;
  points[3].x = x + th * (1.0 - (hJointL + hJointW) * hr2) + 0.5;
  points[3].y = y + (points[1].x - points[3].x);
  points[4].x = points[3].x + (points[0].x - points[1].x);
  points[4].y = points[3].y + (points[4].x - points[3].x);
  points[5].x = x + th * (1.0 - (hJointW + hNeckW) * hr2 * 0.5) + 0.5;
  points[5].y = points[4].y - (points[5].x - points[4].x);
  points[6].x = points[5].x;
  points[6].y = points[4].y + (points[6].x - points[4].x);
  points[7].x = x + th * (1.0 + (hHeadL * 2.0 - hJointW) * hr2) + 0.5;
  points[7].y = points[6].y + (points[7].x - points[6].x);
  points[8].x = points[7].x + (points[0].x - points[4].x);
  points[8].y = points[7].y - (points[8].x - points[7].x);
  points[9].x = points[8].x - (points[7].x - points[6].x);
  points[9].y = points[8].y - (points[7].x - points[6].x);
  points[10].x = points[0].x - (points[9].y - points[0].y);
  points[10].y = points[9].y;

  butEnv_setXFg(butWin_env(win), BUT_BLACK);
  XFillPolygon(dpy, xwin, gc, points, 11, Nonconvex, CoordModeOrigin);

  /* The screwdriver's blade. */
  points[0].x = bx + th * (1.0 - sBladeW * hr2) + 0.5;
  points[0].y = by + 0.5;
  points[1].x = bx + th * sHandleW + 0.5;
  points[1].y = points[0].y + (points[0].x - points[1].x);
  points[3].x = bx + th + 0.5;
  points[3].y = points[0].y + (points[3].x - points[0].x);
  points[2].x = points[1].x + (points[3].x - points[0].x);
  points[2].y = points[1].y + (points[3].x - points[0].x);

  butEnv_setXFg(butWin_env(win), CGBUTS_GREY(120));
  XFillPolygon(dpy, xwin, gc, points, 4, Nonconvex, CoordModeOrigin);

  /* The screwdriver's handle. */
  points[4].x = bx + 0.5;
  points[4].y = by + th * (1.0 - sHandleW * hr2) + 0.5;
  points[5].x = points[4].x;
  points[5].y = by + th * (1.0 - sHEndW * hr2) + 0.5;
  points[6].y = by + th + 0.5;
  points[6].x = points[5].x + (points[6].y - points[5].y);
  points[7].y = points[6].y;
  points[7].x = points[6].x + (points[5].y - points[4].y);
  points[8].x = bx + th * (sHandleL + sHandleW) * hr2 + 0.5;
  points[8].y = points[7].y - (points[8].x - points[7].x);
  points[3].x = points[4].x + (points[8].x - points[7].x);
  points[3].y = points[4].y - (points[8].x - points[7].x);

  points[2].x = points[3].x + th * bevel * sHandleW * hr2 + 0.5;
  points[2].y = points[3].y + (points[2].x - points[3].x);
  points[9].x = points[8].x - (points[2].x - points[3].x);
  points[9].y = points[8].y - (points[2].x - points[3].x);
  points[1].x = points[4].x + (points[2].x - points[3].x);
  points[1].y = points[4].y + (points[2].x - points[3].x);
  points[10].x = points[7].x - (points[2].x - points[3].x);
  points[10].y = points[7].y - (points[2].x - points[3].x);
  points[0].x = points[5].x + th * bevel * sHEndW * hr2 + 0.5;
  points[0].y = points[5].y + (points[0].x - points[5].x);
  points[11].x = points[6].x - (points[0].x - points[5].x);
  points[11].y = points[6].y - (points[0].x - points[5].x);

  butEnv_setXFg(butWin_env(win), CGBUTS_COLORBOARD(200));
  XFillPolygon(dpy, xwin, gc, points+3, 6, Nonconvex, CoordModeOrigin);
  butEnv_setXFg(butWin_env(win), CGBUTS_COLORBOARD(0));
  XFillPolygon(dpy, xwin, gc, points, 6, Nonconvex, CoordModeOrigin);
  butEnv_setXFg(butWin_env(win), CGBUTS_COLORBOARD(255));
  XFillPolygon(dpy, xwin, gc, points+6, 6, Nonconvex, CoordModeOrigin);
}


static ButOut  toolPic_destroy(But *but)  {
  return(0);
}


void  cgbuts_redraw(Cgbuts *b)  {
  int  i;

  assert(MAGIC(b));
  for (i = 0;  i < b->numPics;  ++i)  {
    if (b->pics[i].stonePixmaps != None)  {
      XFreePixmap(butEnv_dpy(b->env), b->pics[i].stonePixmaps);
      XFreePixmap(butEnv_dpy(b->env), b->pics[i].maskBitmaps);
    }
  }
  wms_free(b->pics);
  b->pics = NULL;
  b->numPics = 0;
  butEnv_drawAll(b->env);
}