-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtkgame.c
8830 lines (7298 loc) · 319 KB
/
gtkgame.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
/*
* Copyright (C) 2000-2003 Gary Wong <gtw@gnu.org>
* Copyright (C) 2001-2023 the AUTHORS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gnubgmodule.h"
#include "gtkuidefs.h"
#include <glib.h>
#include <ctype.h>
#include <glib/gstdio.h>
#include <math.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <locale.h>
#if defined(HAVE_UNISTD_H)
#include <unistd.h>
#endif
#if defined(HAVE_LIB_READLINE)
#include <readline/history.h>
#include <readline/readline.h>
#endif
#if defined(WIN32)
#include <io.h>
#endif
#include "analysis.h"
#include "backgammon.h"
#include "sgf.h"
#include "dice.h"
#include "drawboard.h"
#include "gtkboard.h"
#include "gtkchequer.h"
#include "gtkcube.h"
#include "gtkgame.h"
#include "gtkmet.h"
#include "gtkmovefilter.h"
#include "gtkprefs.h"
#include "gtksplash.h"
#include "gtkrelational.h"
#include "gtkfile.h"
#include "matchequity.h"
#include "openurl.h"
#include "positionid.h"
#include "sound.h"
#include "gtkoptions.h"
#include "gtktoolbar.h"
#include "format.h"
#include "formatgs.h"
#include "renderprefs.h"
#include "credits.h"
#include "matchid.h"
#include "gtkwindows.h"
#include "export.h"
#include "gtkmovelistctrl.h"
#include "rollout.h"
#include "util.h"
#include "gtkscoremap.h"
#if defined(USE_BOARD3D)
#include "inc3d.h"
#endif
#include "gnubgstock.h"
#define KEY_ESCAPE -229
#if defined(USE_BOARD3D)
gboolean widget3dValid;
#if defined(USE_GTKITEMFACTORY)
/* Offset action to avoid predefined values */
#define MENU_OFFSET 50
#endif
#endif
#if !defined(USE_GTKITEMFACTORY)
static void TogglePanel(guint iType, guint iActionID, GtkToggleAction * action,
GtkToggleAction * alt, gpointer user_data);
static void ToolbarStyle(guint iType, guint iActionID, GtkRadioAction * action,
GtkRadioAction * alt, gpointer user_data);
#endif
static char *newLang;
/* Hack this for now to stop re-entering - should be fixed when menu switched to actions */
int inCallback = FALSE;
/* Enumeration to be used as index to the table of command strings below
* (since GTK will only let us put integers into a GtkItemFactoryEntry,
* and that might not be big enough to hold a pointer). Must be kept in
* sync with the string array aszCommands! */
typedef enum {
CMD_ACCEPT,
CMD_ANALYSE_CLEAR_MOVE,
CMD_ANALYSE_CLEAR_GAME,
CMD_ANALYSE_CLEAR_MATCH,
CMD_ANALYSE_MOVE,
CMD_ANALYSE_GAME,
CMD_ANALYSE_MATCH,
CMD_ANALYSE_ROLLOUT_CUBE,
CMD_ANALYSE_ROLLOUT_MOVE,
CMD_ANALYSE_ROLLOUT_GAME,
CMD_ANALYSE_ROLLOUT_MATCH,
CMD_CLEAR_TURN,
CMD_CMARK_CUBE_CLEAR,
CMD_CMARK_CUBE_SHOW,
CMD_CMARK_MOVE_CLEAR,
CMD_CMARK_MOVE_SHOW,
CMD_CMARK_GAME_CLEAR,
CMD_CMARK_GAME_SHOW,
CMD_CMARK_MATCH_CLEAR,
CMD_CMARK_MATCH_SHOW,
CMD_END_GAME,
CMD_DECLINE,
CMD_DOUBLE,
CMD_EVAL,
CMD_HELP,
CMD_HINT,
CMD_LIST_GAME,
CMD_NEXT,
CMD_NEXT_GAME,
CMD_NEXT_MARKED,
CMD_NEXT_CMARKED,
CMD_NEXT_ROLL,
CMD_NEXT_ROLLED,
CMD_PLAY,
CMD_PREV,
CMD_PREV_GAME,
CMD_PREV_MARKED,
CMD_PREV_CMARKED,
CMD_PREV_ROLL,
CMD_PREV_ROLLED,
CMD_QUIT,
CMD_REJECT,
CMD_RELATIONAL_ADD_MATCH,
CMD_ROLL,
CMD_ROLLOUT,
CMD_SET_ANNOTATION_ON,
CMD_SET_APPEARANCE,
CMD_SET_MESSAGE_ON,
CMD_SET_TURN_0,
CMD_SET_TURN_1,
CMD_SHOW_CALIBRATION,
CMD_SHOW_COPYING,
CMD_SHOW_ENGINE,
CMD_SHOW_EXPORT,
CMD_SHOW_HISTORY,
CMD_SHOW_MARKETWINDOW,
CMD_SHOW_MATCHEQUITYTABLE,
CMD_SHOW_KLEINMAN,
CMD_SHOW_MANUAL_ABOUT,
CMD_SHOW_MANUAL_WEB,
CMD_SHOW_ROLLS,
CMD_SHOW_STATISTICS_MATCH,
CMD_SHOW_TEMPERATURE_MAP,
CMD_SHOW_TEMPERATURE_MAP_CUBE,
CMD_SHOW_SCORE_MAP_CUBE,
CMD_SHOW_SCORE_MAP_MOVE,
CMD_SHOW_VERSION,
CMD_SHOW_WARRANTY,
CMD_SWAP_PLAYERS,
NUM_CMDS,
TOGGLE_GAMELIST,
TOGGLE_ANALYSIS,
TOGGLE_COMMENTARY,
TOGGLE_MESSAGE,
TOGGLE_THEORY,
TOGGLE_COMMAND,
VIEW_TOOLBAR_ICONSONLY,
VIEW_TOOLBAR_TEXTONLY,
VIEW_TOOLBAR_BOTH
} gnubgcommand;
/* TRUE if GNUbg is automatically setting the state of a menu item. */
static int fAutoCommand;
#if !defined(USE_GTKITEMFACTORY)
/*
* Some of the callback functions below, either explicit or
* constructed with macros, are unused (were they added "just in case" ?)
* Keep them for reference, but between #if 0 / #endif to avoid
* compiler warnings.
*/
#if 0
static void
ExecToggleActionCommand_internal(guint UNUSED(iWidgetType), guint UNUSED(iCommand), gchar *szCommand,
gpointer *widget, gpointer *UNUSED(widgetalt), gpointer UNUSED(user_data))
{
char sz[80];
if (fAutoCommand)
return;
sprintf(sz, "%s %s", szCommand, gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(widget)) ? "on" : "off");
UserCommand(sz);
return;
}
#endif
static void
ExecRadioActionCommand_internal(guint UNUSED(iWidgetType), guint iCommand, gchar * UNUSED(szCommand), gpointer * widget,
gpointer * UNUSED(widgetalt), gpointer UNUSED(user_data))
{
char sz[80];
gint actionID;
if (fAutoCommand)
return;
actionID = gtk_radio_action_get_current_value(GTK_RADIO_ACTION(widget));
switch (actionID) {
case CMD_SET_TURN_0:
case CMD_SET_TURN_1:
sprintf(sz, "set turn %s", ap[actionID - iCommand].szName);
UserCommand(sz);
break;
}
return;
}
static void
ExecActionCommand_internal(guint UNUSED(iWidgetType), guint iCommand, gchar * szCommand, gpointer * UNUSED(widget),
gpointer * UNUSED(widgetalt), gpointer UNUSED(user_data))
{
char sz[80];
if (fAutoCommand)
return;
switch (iCommand) {
case CMD_SET_APPEARANCE:
BoardPreferences(pwBoard);
return;
case CMD_SET_TURN_0:
case CMD_SET_TURN_1:
sprintf(sz, "set turn %s", ap[iCommand - CMD_SET_TURN_0].szName);
UserCommand(sz);
return;
case CMD_ANALYSE_MATCH:
UserCommand("analyse match");
if (fAutoDB) {
/* add match to db */
CommandRelationalAddMatch(NULL);
}
UserCommand("show statistics match");
return;
default:
UserCommand(szCommand);
}
}
#define CREATE_CMD_ACTION_CALLBACK(CMDID, szCommand) \
static void CMDID##_action_cb ( GtkAction *action, gpointer user_data ) \
{ \
ExecActionCommand_internal ( 0, CMDID, szCommand, (gpointer)action, NULL, user_data ); \
return; \
} ;
#if 0
#define CREATE_CMD_TOGGLE_CALLBACK(CMDID, szCommand) \
static void CMDID##_toggle_cb ( GtkToggleAction *action, gpointer user_data ) \
{ \
ExecToggleActionCommand_internal ( 1, CMDID, szCommand, (gpointer)action, NULL, user_data ); \
return; \
} ;
#endif
#define CREATE_CMD_RADIO_CALLBACK(CMDID, szCommand) \
static void CMDID##_radio_cb ( GtkRadioAction *action, GtkRadioAction *current, gpointer user_data ) \
{ \
ExecRadioActionCommand_internal ( 2, CMDID, szCommand, (gpointer)action, (gpointer)current, user_data ); \
return; \
} ;
#if 0
#define CREATE_GENERIC_ACTION_CALLBACK(ACTIONID, callbackfunc) \
static void ACTIONID##_action_g_cb ( GtkAction *action, gpointer user_data ) \
{ \
callbackfunc ( 0, ACTIONID, action, NULL, user_data ); \
return; \
} ;
#endif
#define CREATE_GENERIC_TOGGLE_CALLBACK(ACTIONID, callbackfunc) \
static void ACTIONID##_toggle_g_cb ( GtkToggleAction *action, gpointer user_data ) \
{ \
callbackfunc ( 1, ACTIONID, action, NULL, user_data ); \
return; \
} ;
#define CREATE_GENERIC_RADIO_CALLBACK(ACTIONID, callbackfunc) \
static void ACTIONID##_radio_g_cb ( GtkRadioAction *action, GtkRadioAction *current, gpointer user_data ) \
{ \
callbackfunc ( 2, ACTIONID, action, current, user_data ); \
return; \
} ;
#define CMD_ACTION_CALLBACK_FROMID(CMDID) (GCallback)CMDID##_action_cb
#if 0
#define CMD_TOGGLE_CALLBACK_FROMID(CMDID) (GCallback)CMDID##_toggle_cb
#endif
#define CMD_RADIO_CALLBACK_FROMID(CMDID) (GCallback)CMDID##_radio_cb
#if 0
#define GENERIC_ACTION_CALLBACK_FROMID(ACTIONID) (GCallback)ACTIONID##_action_g_cb
#endif
#define GENERIC_TOGGLE_CALLBACK_FROMID(ACTIONID) (GCallback)ACTIONID##_toggle_g_cb
#define GENERIC_RADIO_CALLBACK_FROMID(ACTIONID) (GCallback)ACTIONID##_radio_g_cb
/* Create callback functions for all the commands. These do no need to be in any particular order */
CREATE_CMD_ACTION_CALLBACK(CMD_ACCEPT, "accept");
CREATE_CMD_ACTION_CALLBACK(CMD_ANALYSE_CLEAR_MOVE, "analyse clear move");
CREATE_CMD_ACTION_CALLBACK(CMD_ANALYSE_CLEAR_GAME, "analyse clear game");
CREATE_CMD_ACTION_CALLBACK(CMD_ANALYSE_CLEAR_MATCH, "analyse clear match");
CREATE_CMD_ACTION_CALLBACK(CMD_ANALYSE_MOVE, "analyse move");
CREATE_CMD_ACTION_CALLBACK(CMD_ANALYSE_GAME, "analyse game");
CREATE_CMD_ACTION_CALLBACK(CMD_ANALYSE_MATCH, "analyse match");
CREATE_CMD_ACTION_CALLBACK(CMD_ANALYSE_ROLLOUT_CUBE, "analyse rollout cube");
CREATE_CMD_ACTION_CALLBACK(CMD_ANALYSE_ROLLOUT_MOVE, "analyse rollout move");
CREATE_CMD_ACTION_CALLBACK(CMD_ANALYSE_ROLLOUT_GAME, "analyse rollout game");
CREATE_CMD_ACTION_CALLBACK(CMD_ANALYSE_ROLLOUT_MATCH, "analyse rollout match");
CREATE_CMD_ACTION_CALLBACK(CMD_CLEAR_TURN, "clear turn");
CREATE_CMD_ACTION_CALLBACK(CMD_CMARK_CUBE_CLEAR, "cmark cube clear");
CREATE_CMD_ACTION_CALLBACK(CMD_CMARK_CUBE_SHOW, "cmark cube show");
CREATE_CMD_ACTION_CALLBACK(CMD_CMARK_MOVE_CLEAR, "cmark move clear");
CREATE_CMD_ACTION_CALLBACK(CMD_CMARK_MOVE_SHOW, "cmark move show");
CREATE_CMD_ACTION_CALLBACK(CMD_CMARK_GAME_CLEAR, "cmark game clear");
CREATE_CMD_ACTION_CALLBACK(CMD_CMARK_GAME_SHOW, "cmark game show");
CREATE_CMD_ACTION_CALLBACK(CMD_CMARK_MATCH_CLEAR, "cmark match clear");
CREATE_CMD_ACTION_CALLBACK(CMD_CMARK_MATCH_SHOW, "cmark match show");
CREATE_CMD_ACTION_CALLBACK(CMD_END_GAME, "end game");
#if 0
CREATE_CMD_ACTION_CALLBACK(CMD_DECLINE, "decline");
#endif
CREATE_CMD_ACTION_CALLBACK(CMD_DOUBLE, "double");
CREATE_CMD_ACTION_CALLBACK(CMD_EVAL, "eval");
CREATE_CMD_ACTION_CALLBACK(CMD_HELP, "help");
CREATE_CMD_ACTION_CALLBACK(CMD_HINT, "hint");
#if 0
CREATE_CMD_ACTION_CALLBACK(CMD_LIST_GAME, "list game");
CREATE_CMD_ACTION_CALLBACK(CMD_NEXT, "next");
#endif
CREATE_CMD_ACTION_CALLBACK(CMD_NEXT_GAME, "next game");
CREATE_CMD_ACTION_CALLBACK(CMD_NEXT_MARKED, "next marked");
CREATE_CMD_ACTION_CALLBACK(CMD_NEXT_CMARKED, "next cmarked");
CREATE_CMD_ACTION_CALLBACK(CMD_NEXT_ROLL, "next roll");
#if 0
CREATE_CMD_ACTION_CALLBACK(CMD_NEXT_ROLLED, "next rolled");
#endif
CREATE_CMD_ACTION_CALLBACK(CMD_PLAY, "play");
#if 0
CREATE_CMD_ACTION_CALLBACK(CMD_PREV, "previous");
#endif
CREATE_CMD_ACTION_CALLBACK(CMD_PREV_GAME, "previous game");
CREATE_CMD_ACTION_CALLBACK(CMD_PREV_MARKED, "previous marked");
CREATE_CMD_ACTION_CALLBACK(CMD_PREV_CMARKED, "previous cmarked");
CREATE_CMD_ACTION_CALLBACK(CMD_PREV_ROLL, "previous roll");
#if 0
CREATE_CMD_ACTION_CALLBACK(CMD_PREV_ROLLED, "previous rolled");
#endif
CREATE_CMD_ACTION_CALLBACK(CMD_QUIT, "quit");
CREATE_CMD_ACTION_CALLBACK(CMD_REJECT, "reject");
#if 0
CREATE_CMD_ACTION_CALLBACK(CMD_RELATIONAL_ADD_MATCH, "relational add match");
#endif
CREATE_CMD_ACTION_CALLBACK(CMD_ROLL, "roll");
CREATE_CMD_ACTION_CALLBACK(CMD_ROLLOUT, "rollout");
#if 0
CREATE_CMD_ACTION_CALLBACK(CMD_SET_ANNOTATION_ON, "set annotation on");
#endif
CREATE_CMD_ACTION_CALLBACK(CMD_SET_APPEARANCE, NULL); /* set appearance */
#if 0
CREATE_CMD_ACTION_CALLBACK(CMD_SET_MESSAGE_ON, "set message on");
CREATE_CMD_ACTION_CALLBACK(CMD_SET_TURN_0, NULL); /* set turn 0 */
CREATE_CMD_ACTION_CALLBACK(CMD_SET_TURN_1, NULL); /* set turn 1 */
#endif
CREATE_CMD_ACTION_CALLBACK(CMD_SHOW_CALIBRATION, "show calibration");
#if 0
CREATE_CMD_ACTION_CALLBACK(CMD_SHOW_COPYING, "show copying");
CREATE_CMD_ACTION_CALLBACK(CMD_SHOW_ENGINE, "show engine");
#endif
CREATE_CMD_ACTION_CALLBACK(CMD_SHOW_EXPORT, "show export");
CREATE_CMD_ACTION_CALLBACK(CMD_SHOW_HISTORY, "show history");
CREATE_CMD_ACTION_CALLBACK(CMD_SHOW_MARKETWINDOW, "show marketwindow");
CREATE_CMD_ACTION_CALLBACK(CMD_SHOW_MATCHEQUITYTABLE, "show matchequitytable");
CREATE_CMD_ACTION_CALLBACK(CMD_SHOW_KLEINMAN, "show kleinman"); /* opens race theory window */
CREATE_CMD_ACTION_CALLBACK(CMD_SHOW_MANUAL_ABOUT, "show manual about");
CREATE_CMD_ACTION_CALLBACK(CMD_SHOW_MANUAL_WEB, "show manual web");
CREATE_CMD_ACTION_CALLBACK(CMD_SHOW_ROLLS, "show rolls");
CREATE_CMD_ACTION_CALLBACK(CMD_SHOW_STATISTICS_MATCH, "show statistics match");
CREATE_CMD_ACTION_CALLBACK(CMD_SHOW_TEMPERATURE_MAP, "show temperaturemap");
CREATE_CMD_ACTION_CALLBACK(CMD_SHOW_TEMPERATURE_MAP_CUBE, "show temperaturemap =cube");
CREATE_CMD_ACTION_CALLBACK(CMD_SHOW_SCORE_MAP_CUBE, "show scoremap");
CREATE_CMD_ACTION_CALLBACK(CMD_SHOW_SCORE_MAP_MOVE, "show scoremap =move");
CREATE_CMD_ACTION_CALLBACK(CMD_SHOW_VERSION, "show version");
#if 0
CREATE_CMD_ACTION_CALLBACK(CMD_SHOW_WARRANTY, "show warranty");
#endif
CREATE_CMD_ACTION_CALLBACK(CMD_SWAP_PLAYERS, "swap players");
CREATE_GENERIC_TOGGLE_CALLBACK(TOGGLE_GAMELIST, TogglePanel);
CREATE_GENERIC_TOGGLE_CALLBACK(TOGGLE_COMMENTARY, TogglePanel);
CREATE_GENERIC_TOGGLE_CALLBACK(TOGGLE_MESSAGE, TogglePanel);
CREATE_GENERIC_TOGGLE_CALLBACK(TOGGLE_ANALYSIS, TogglePanel);
CREATE_GENERIC_TOGGLE_CALLBACK(TOGGLE_THEORY, TogglePanel);
CREATE_GENERIC_TOGGLE_CALLBACK(TOGGLE_COMMAND, TogglePanel);
CREATE_GENERIC_RADIO_CALLBACK(VIEW_TOOLBAR_ICONSONLY, ToolbarStyle);
CREATE_CMD_RADIO_CALLBACK(CMD_SET_TURN_0, NULL);
#else
static const char *aszCommands[NUM_CMDS] = {
"accept",
"analyse clear move",
"analyse clear game",
"analyse clear match",
"analyse move",
"analyse game",
"analyse match",
"analyse rollout cube",
"analyse rollout move",
"analyse rollout game",
"analyse rollout match",
"clear turn",
"cmark cube clear",
"cmark cube show",
"cmark move clear",
"cmark move show",
"cmark game clear",
"cmark game show",
"cmark match clear",
"cmark match show",
"end game",
"decline",
"double",
"eval",
"help",
"hint",
"list game",
"next",
"next game",
"next marked",
"next cmarked",
"next roll",
"next rolled",
"play",
"previous",
"previous game",
"previous marked",
"previous cmarked",
"previous roll",
"previous rolled",
"quit",
"reject",
"relational add match",
"roll",
"rollout",
"set annotation on",
NULL, /* set appearance */
"set message on",
NULL, /* set turn 0 */
NULL, /* set turn 1 */
"show calibration",
"show copying",
"show engine",
"show export",
"show history",
"show marketwindow",
"show matchequitytable",
"show kleinman", /* opens race theory window */
"show manual about",
"show manual web",
"show rolls",
"show statistics match",
"show temperaturemap",
"show temperaturemap =cube",
"show scoremap",
"show scoremap =move",
"show version",
"show warranty",
"swap players",
};
static void
Command(gpointer UNUSED(p), guint iCommand, GtkWidget * widget)
{
char sz[80];
if (fAutoCommand)
return;
/* FIXME this isn't very good -- if UserCommand fails, the setting
* won't have been changed, but the widget will automatically have
* updated itself. */
if (GTK_IS_RADIO_MENU_ITEM(widget)) {
if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)))
return;
} else if (GTK_IS_CHECK_MENU_ITEM(widget)) {
sprintf(sz, "%s %s", aszCommands[iCommand],
gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)) ? "on" : "off");
UserCommand(sz);
return;
}
switch (iCommand) {
case CMD_SET_APPEARANCE:
BoardPreferences(pwBoard);
return;
case CMD_SET_TURN_0:
case CMD_SET_TURN_1:
sprintf(sz, "set turn %s", ap[iCommand - CMD_SET_TURN_0].szName);
UserCommand(sz);
return;
case CMD_ANALYSE_MATCH:
UserCommand(aszCommands[CMD_ANALYSE_MATCH]);
if (fAutoDB) {
/* add match to db */
CommandRelationalAddMatch(NULL);
}
UserCommand(aszCommands[CMD_SHOW_STATISTICS_MATCH]);
return;
default:
UserCommand(aszCommands[iCommand]);
}
}
#endif
typedef struct {
const char *title;
evalcontext *esChequer;
movefilter *mfChequer;
evalcontext *esCube;
movefilter *mfCube;
GtkWidget *pwCube, *pwChequer, *pwOptionMenu, *pwSettingWidgets;
int cubeDisabled;
int fWeakLevels;
} AnalysisDetails;
typedef struct {
evalsetup esChequer;
evalsetup esCube;
movefilter aamf[MAX_FILTER_PLIES][MAX_FILTER_PLIES];
evalsetup esEvalChequer;
evalsetup esEvalCube;
movefilter aaEvalmf[MAX_FILTER_PLIES][MAX_FILTER_PLIES];
GtkWidget *pwNoteBook;
GtkAdjustment *apadjSkill[3], *apadjLuck[4];
GtkWidget *pwMoves, *pwCube, *pwLuck, *pwHintSame, *pwCubeSummary;
GtkWidget *apwAnalysePlayers[2];
GtkWidget *pwAutoDB;
GtkWidget *pwBackgroundAnalysis;
GtkWidget *apwAnalyzeFileSetting[NUM_AnalyzeFileSettings];
GtkWidget *pwScoreMap;
GtkWidget *apwScoreMapPly[NUM_PLY];
GtkWidget *apwScoreMapMatchLength[NUM_MATCH_LENGTH];
GtkWidget *apwScoreMapLabel[NUM_LABEL];
GtkWidget *apwScoreMapJacoby[NUM_JACOBY];
GtkWidget *apwScoreMapCubeEquityDisplay[NUM_CUBEDISP];
GtkWidget *apwScoreMapMoveEquityDisplay[NUM_MOVEDISP];
GtkWidget *apwScoreMapColour[NUM_COLOUR];
GtkWidget *apwScoreMapLayout[NUM_LAYOUT];
/* defining these just to be able to g_free them */
AnalysisDetails *pAnalDetailSettings1;
AnalysisDetails *pAnalDetailSettings2;
} analysiswidget;
/* A dummy widget that can grab events when others shouldn't see them. */
GtkWidget *pwGrab;
GtkWidget *pwOldGrab;
GtkWidget *pwBoard;
GtkWidget *pwMain = NULL;
static GtkWidget *pwMenuBar;
GtkWidget *pwToolbar;
static GtkWidget *pwStatus;
static GtkWidget *pwIDBox;
GtkWidget *pwGnubgID;
static GtkWidget *pwProgress;
GtkWidget *pwMessageText;
GtkWidget *pwPanelVbox;
GtkWidget *pwAnalysis;
GtkWidget *pwCommentary;
static moverecord *pmrAnnotation;
GtkAccelGroup *pagMain;
#if defined(USE_GTKITEMFACTORY)
GtkItemFactory *pif;
#else
GtkUIManager *puim = NULL;
#endif
guint nNextTurn = 0; /* GTK idle function */
static guint idOutput, idProgress;
int fTTY = TRUE;
int fGUISetWindowPos = TRUE;
int frozen = FALSE;
static GString *output_str = NULL;
static int fullScreenOnStartup = FALSE;
static guint nStdin, nDisabledCount = 1;
static GIOChannel *pStdin;
/* Save state of windows for full screen */
static int showingPanels, showingIDs, maximised;
static gulong grabIdSignal;
static int suspendCount = 0;
static GtkWidget *grabbedWidget;
/* Language selection code */
static GtkWidget *curSel;
static GtkWidget *pwLangDialog, *pwLangRadio1, *pwLangRadio2, *pwLangTable;
static GtkWidget *pwHelpTree, *pwHelpLabel;
GtkWidget *hpaned;
static GtkWidget *pwGameBox;
static GtkWidget *pwPanelGameBox;
static GtkWidget *pwEventBox;
static int panelSize = 325;
static GtkWidget *pwStop;
extern void
GTKSuspendInput(void)
{
if (!fX)
return;
/* when the fBackgroundAnalysis global variable is set, we allow the user to
* (1) continue browsing and (2) stop the computation;
* else we grab the focus and kill any user input
*/
if (!fBackgroundAnalysis) {
if (suspendCount == 0 && pwGrab && GDK_IS_WINDOW(gtk_widget_get_window(pwGrab))) {
/* Grab events so that the board window knows this is a re-entrant */
/* call, and won't allow commands like roll, move or double. */
grabbedWidget = pwGrab;
if (pwGrab == pwStop) {
gtk_widget_grab_focus(pwStop);
gtk_widget_set_sensitive(pwStop, TRUE);
}
gtk_grab_add(pwGrab);
grabIdSignal = g_signal_connect_after(G_OBJECT(pwGrab), "key-press-event", G_CALLBACK(gtk_true), NULL);
}
/* Don't check stdin here; readline isn't ready yet. */
GTKDisallowStdin();
} else {
if (pwGrab == pwStop) {
gtk_widget_grab_focus(pwStop);
gtk_widget_set_sensitive(pwStop, TRUE);
}
}
suspendCount++;
}
extern void
GTKResumeInput(void)
{
if (!fX)
return;
g_assert(suspendCount > 0);
suspendCount--;
if (suspendCount == 0) {
if (GTK_IS_WIDGET(grabbedWidget) && gtk_widget_has_grab(grabbedWidget)) {
if (g_signal_handler_is_connected(G_OBJECT(grabbedWidget), grabIdSignal))
g_signal_handler_disconnect(G_OBJECT(grabbedWidget), grabIdSignal);
gtk_grab_remove(grabbedWidget);
}
if (pwGrab == pwStop)
gtk_widget_set_sensitive(pwStop, FALSE);
}
GTKAllowStdin();
}
static gboolean
StdinReadNotify(GIOChannel * UNUSED(source), GIOCondition UNUSED(cond), gpointer UNUSED(p))
{
#if defined(HAVE_LIB_READLINE)
/* Handle "next turn" processing before more input (otherwise we might
* not even have a readline handler installed!) */
while (nNextTurn)
NextTurnNotify(NULL);
rl_callback_read_char();
return TRUE;
#else
char sz[2048], *pch;
while (nNextTurn)
NextTurnNotify(NULL);
if (fgets(sz, sizeof(sz), stdin) == NULL) {
if (!isatty(STDIN_FILENO))
exit(EXIT_SUCCESS);
PromptForExit();
return TRUE;
}
if ((pch = strchr(sz, '\n')))
*pch = 0;
fInterrupt = FALSE;
HandleCommand(sz, acTop);
ResetInterrupt();
if (nNextTurn)
fNeedPrompt = TRUE;
else
Prompt();
return TRUE;
#endif
}
extern void
GTKAllowStdin(void)
{
if (!fTTY || !nDisabledCount)
return;
if (!--nDisabledCount) {
pStdin = g_io_channel_unix_new(STDIN_FILENO);
nStdin = g_io_add_watch_full(pStdin, G_PRIORITY_HIGH, G_IO_IN | G_IO_PRI, StdinReadNotify, NULL, NULL);
}
}
extern void
GTKDisallowStdin(void)
{
if (!fTTY)
return;
nDisabledCount++;
if (nStdin) {
g_source_remove(nStdin);
g_io_channel_unref(pStdin);
nStdin = 0;
}
}
int fEndDelay;
extern void
GTKDelay(void)
{
GTKSuspendInput();
while (!fInterrupt && !fEndDelay)
gtk_main_iteration();
fEndDelay = FALSE;
GTKResumeInput();
}
static void
gui_clear_turn(GtkWidget * UNUSED(pw), GtkWidget * dialog)
{
if (dialog)
gtk_widget_destroy(dialog);
CommandClearTurn(NULL);
}
extern int
GTKGetManualDice(unsigned int an[2])
{
GtkWidget *dialog;
GtkWidget *dice;
GtkWidget *buttons;
GtkWidget *clear;
BoardData *bd = BOARD(pwBoard)->board_data;
manualDiceType mdt;
if (ToolbarIsEditing(pwToolbar))
mdt = MT_EDIT;
else if (plLastMove && ((moverecord *) plLastMove->p)->mt == MOVE_GAMEINFO)
mdt = MT_FIRSTMOVE;
else
mdt = MT_STANDARD;
dialog = GTKCreateDialog(_("GNU Backgammon - Dice"), DT_INFO, NULL,
DIALOG_FLAG_MODAL | DIALOG_FLAG_CLOSEBUTTON, NULL, NULL);
dice = board_dice_widget(BOARD(pwBoard), mdt);
gtk_container_add(GTK_CONTAINER(DialogArea(dialog, DA_MAIN)), dice);
buttons = DialogArea(dialog, DA_BUTTONS);
clear = gtk_button_new_with_label(_("Clear Dice"));
gtk_container_add(GTK_CONTAINER(buttons), clear);
g_signal_connect(G_OBJECT(clear), "clicked", G_CALLBACK(gui_clear_turn), dialog);
gtk_widget_set_sensitive(GTK_WIDGET(clear), bd->diceShown == DICE_ON_BOARD);
g_object_set_data(G_OBJECT(dice), "user_data", an);
an[0] = 0;
GTKRunDialog(dialog);
if (mdt == MT_EDIT && an[0]) {
if (an[0] > an[1] && bd->turn != -1)
UserCommand("set turn 0");
else if (an[0] < an[1] && bd->turn != 1)
UserCommand("set turn 1");
}
return an[0] ? 0 : -1;
}
extern void
GTKSetDice(gpointer UNUSED(p), guint UNUSED(n), GtkWidget * UNUSED(pw))
{
unsigned int an[2];
if (!GTKGetManualDice(an)) {
char sz[13]; /* "set dice x y" */
sprintf(sz, "set dice %u %u", an[0], an[1]);
UserCommand(sz);
}
}
extern void
GTKSetCube(gpointer UNUSED(p), guint UNUSED(n), GtkWidget * UNUSED(pw))
{
gchar *sz;
GtkWidget *pwDialog, *pwCube;
int an[2];
int valChanged;
if (ms.gs != GAME_PLAYING || ms.fCrawford || !ms.fCubeUse)
return;
pwDialog = GTKCreateDialog(_("GNU Backgammon - Cube"), DT_INFO,
NULL, DIALOG_FLAG_MODAL | DIALOG_FLAG_CLOSEBUTTON, NULL, NULL);
pwCube = board_cube_widget(BOARD(pwBoard));
an[0] = -1;
gtk_container_add(GTK_CONTAINER(DialogArea(pwDialog, DA_MAIN)), pwCube);
g_object_set_data(G_OBJECT(pwCube), "user_data", an);
g_signal_connect(G_OBJECT(pwCube), "destroy", G_CALLBACK(DestroySetCube), pwDialog);
GTKRunDialog(pwDialog);
if (an[0] < 0)
return;
valChanged = (1 << an[0] != ms.nCube);
if (valChanged) {
sz = g_strdup_printf("set cube value %d", 1 << an[0]);
UserCommand(sz);
g_free(sz);
}
if (an[1] != ms.fCubeOwner) {
if (an[1] >= 0) {
sz = g_strdup_printf("set cube owner %d", an[1]);
UserCommand(sz);
g_free(sz);
} else
UserCommand("set cube centre");
}
}
static int fAutoCommentaryChange;
extern void
CommentaryChanged(GtkWidget * UNUSED(pw), GtkTextBuffer * buffer)
{
char *pch;
GtkTextIter begin, end;
if (fAutoCommentaryChange)
return;
g_assert(pmrAnnotation);
/* FIXME Copying the entire text every time it's changed is horribly
* inefficient, but the only alternatives seem to be lazy copying
* (which is much harder to get right) or requiring a specific command
* to update the text (which is probably inconvenient for the user). */
if (pmrAnnotation->sz)
g_free(pmrAnnotation->sz);
gtk_text_buffer_get_bounds(buffer, &begin, &end);
pch = gtk_text_buffer_get_text(buffer, &begin, &end, FALSE);
/* This copy is absolutely disgusting, but is necessary because GTK
* insists on giving us something allocated with g_malloc() instead
* of malloc(). */
pmrAnnotation->sz = g_strdup(pch);
g_free(pch);
}
extern void
GTKFreeze(void)
{
frozen = TRUE;
}
extern void
GTKThaw(void)
{
frozen = FALSE;
/* Make sure analysis window is correct */
if (plLastMove)
GTKSetMoveRecord(plLastMove->p);
}
static GtkWidget *
ResignAnalysis(float arResign[NUM_ROLLOUT_OUTPUTS], int nResigned, evalsetup * pesResign)
{
cubeinfo ci;
#if GTK_CHECK_VERSION(3,0,0)
GtkWidget *pwGrid = gtk_grid_new();
#else
GtkWidget *pwTable = gtk_table_new(3, 2, FALSE);
#endif
GtkWidget *pwLabel;
float rAfter, rBefore;
char sz[64];
if (pesResign->et == EVAL_NONE)
return NULL;
GetMatchStateCubeInfo(&ci, &ms);
/* First column with text */
pwLabel = gtk_label_new(_("Equity before resignation: "));
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_set_halign(pwLabel, GTK_ALIGN_START);
gtk_widget_set_valign(pwLabel, GTK_ALIGN_CENTER);
gtk_grid_attach(GTK_GRID(pwGrid), pwLabel, 0, 0, 1, 1);