some more work on the movehistory window
[xboard.git] / xhistory.c
1 /*
2  * xhistory.c -- Move list window, part of X front end for XBoard
3  *
4  * Copyright 2000,2009 Free Software Foundation, Inc.
5  * ------------------------------------------------------------------------
6  *
7  * GNU XBoard is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or (at
10  * your option) any later version.
11  *
12  * GNU XBoard is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see http://www.gnu.org/licenses/.  *
19  *
20  *------------------------------------------------------------------------
21  ** See the file ChangeLog for a revision history.  */
22
23 #include "config.h"
24
25 #include <gtk/gtk.h>
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <sys/types.h>
30
31 #if STDC_HEADERS
32 # include <stdlib.h>
33 # include <string.h>
34 #else /* not STDC_HEADERS */
35 extern char *getenv();
36 # if HAVE_STRING_H
37 #  include <string.h>
38 # else /* not HAVE_STRING_H */
39 #  include <strings.h>
40 # endif /* not HAVE_STRING_H */
41 #endif /* not STDC_HEADERS */
42
43 #if HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif
46
47 #include <X11/Intrinsic.h>
48 #include <X11/StringDefs.h>
49 #include <X11/Shell.h>
50 #include <X11/Xaw/Dialog.h>
51 #include <X11/Xaw/Form.h>
52 #include <X11/Xaw/List.h>
53 #include <X11/Xaw/Label.h>
54 #include <X11/Xaw/SimpleMenu.h>
55 #include <X11/Xaw/SmeBSB.h>
56 #include <X11/Xaw/SmeLine.h>
57 #include <X11/Xaw/Box.h>
58 #include <X11/Xaw/Paned.h>
59 #include <X11/Xaw/MenuButton.h>
60 #include <X11/cursorfont.h>
61 #include <X11/Xaw/Text.h>
62 #include <X11/Xaw/AsciiText.h>
63 #include <X11/Xaw/Viewport.h>
64
65 #include "common.h"
66 #include "frontend.h"
67 #include "backend.h"
68 #include "xboard.h"
69 #include "xhistory.h"
70 #include "gettext.h"
71 #include "gtk/gtk.h"
72
73 #ifdef ENABLE_NLS
74 # define  _(s) gettext (s)
75 # define N_(s) gettext_noop (s)
76 #else
77 # define  _(s) (s)
78 # define N_(s)  s
79 #endif
80
81 #define _LL_ 100
82
83 extern Widget formWidget, shellWidget, boardWidget, menuBarWidget;
84 extern Display *xDisplay;
85 extern int squareSize;
86 extern Pixmap xMarkPixmap;
87 extern char *layoutName;
88
89 extern GtkWidget               *GUI_History;
90 extern GtkListStore            *LIST_MoveHistory;
91
92
93 struct History{
94   String *Nr,*white,*black;
95   int     aNr;  /* space actually alocated */
96   Widget mvn,mvw,mvb,vbox,viewport,sh;
97   char Up;
98 };
99
100 struct History *hist=0;
101 String dots=" ... ";
102 Position gameHistoryX, gameHistoryY;
103
104 void
105 HistoryPopDown(object, user_data)
106      GtkObject *object;
107      gpointer user_data;
108 {
109   gtk_widget_hide (GUI_History);
110   return;
111 }
112
113 void HistoryMoveProc(Widget w, XtPointer closure, XtPointer call_data)
114 {
115     int to;
116     XawListReturnStruct *R = (XawListReturnStruct *) call_data;
117     if (w == hist->mvn || w == hist->mvw) {
118       to=2*R->list_index-1;
119       ToNrEvent(to);
120     }
121     else if (w == hist->mvb) {
122       to=2*R->list_index;
123       ToNrEvent(to);
124     }
125 }
126
127
128 void HistorySet(char movelist[][2*MOVE_LEN],int first,int last,int current)
129 {
130   int i,b,m;
131   char movewhite[2*MOVE_LEN],moveblack[2*MOVE_LEN],move[2*MOVE_LEN];
132   GtkTreeIter iter;
133
134   /* first clear everything, do we need this? */
135   gtk_list_store_clear(LIST_MoveHistory);
136
137   /* copy move list into history window */
138
139   /* go through all moves */
140   for(i=0;i<last;i++) 
141     {
142       /* test if there is a move */
143       if(movelist[i][0]) 
144         {
145           /* only copy everything before a  ' ' */
146           char* p = strchr(movelist[i], ' ');
147           if (p) 
148             {
149               strncpy(move, movelist[i], p-movelist[i]);
150               move[p-movelist[i]] = NULLCHAR;
151             } 
152           else 
153             {
154               strcpy(move,movelist[i]);
155             }
156         } 
157       else
158         strcpy(move,dots);
159       
160       if((i%2)==0) 
161         {
162           /* white move */
163           strcpy(movewhite,move);
164         }
165       else
166         {
167           /* black move */
168           strcpy(moveblack,move);
169
170           /* save move */
171           gtk_list_store_append (LIST_MoveHistory, &iter);
172           gtk_list_store_set (LIST_MoveHistory, &iter,
173                               0, i,
174                               1, movewhite,
175                               2, moveblack,
176                               -1);
177
178           strcpy(movewhite,"");
179           strcpy(moveblack,"");
180         };
181     }
182   /* check if ther is a white move left */
183   if(movewhite[0])
184     {
185       i++;
186       strcpy(moveblack,"");
187       /* save move */
188       gtk_list_store_append (LIST_MoveHistory, &iter);
189       gtk_list_store_set (LIST_MoveHistory, &iter,
190                           0, i,
191                           1, movewhite,
192                           2, moveblack,
193                           -1);
194     };
195   
196   return;
197   
198   
199   if(current<0){
200     //  XawListUnhighlight(hist->mvw);
201     //  XawListUnhighlight(hist->mvb);
202   }
203   else if((current%2)==0){
204     //  XawListHighlight(hist->mvw, current/2+1);
205     //  XawListUnhighlight(hist->mvb);
206   }
207   else{
208     //  XawListUnhighlight(hist->mvw);
209     //  if(current) XawListHighlight(hist->mvb, current/2+1);
210     //  else XawListUnhighlight(hist->mvb);
211   }
212
213 return;
214 }
215
216 void HistoryCreate()
217 {
218     String trstr=
219              "<Key>Up: BackwardProc() \n \
220              <Key>Left: BackwardProc() \n \
221              <Key>Down: ForwardProc() \n \
222              <Key>Right: ForwardProc() \n";
223
224     return;
225 }
226
227 void
228 HistoryPopUp()
229 {
230   //  if(!hist) HistoryCreate();
231
232   gtk_widget_show (GUI_History);
233   
234   return;
235 }
236
237
238 void
239 HistoryShowProc(object, user_data)
240      GtkObject *object;
241      gpointer user_data;
242 {
243   if (!hist) 
244     {
245       HistoryCreate();
246       HistoryPopUp();
247     } 
248   else if (hist->Up) 
249     {
250       HistoryPopDown(NULL,NULL);
251     } 
252   else 
253     {
254       HistoryPopUp();
255     }
256   ToNrEvent(currentMove);
257
258   return;
259 }
260