first draft of a 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   /* copy move list into history window */
135
136   /* go through all moves */
137   for(i=0;i<last;i++) 
138     {
139       /* test if there is a move */
140       if(movelist[i][0]) 
141         {
142           /* only copy everything before a  ' ' */
143           char* p = strchr(movelist[i], ' ');
144           if (p) 
145             {
146               strncpy(move, movelist[i], p-movelist[i]);
147               move[p-movelist[i]] = NULLCHAR;
148             } 
149           else 
150             {
151               strcpy(move,movelist[i]);
152             }
153         } 
154       else
155         strcpy(move,dots);
156       
157       if((i%2)==0) 
158         {
159           /* white move */
160           strcpy(movewhite,move);
161         }
162       else
163         {
164           /* black move */
165           strcpy(moveblack,move);
166
167           /* save move */
168           gtk_list_store_append (LIST_MoveHistory, &iter);
169           gtk_list_store_set (LIST_MoveHistory, &iter,
170                               0, i,
171                               1, movewhite,
172                               2, moveblack,
173                               -1);
174
175           strcpy(movewhite,"");
176           strcpy(moveblack,"");
177         };
178     }
179   /* check if ther is a white move left */
180   if(movewhite[0])
181     {
182       i++;
183       strcpy(moveblack,"");
184       /* save move */
185       gtk_list_store_append (LIST_MoveHistory, &iter);
186       gtk_list_store_set (LIST_MoveHistory, &iter,
187                           0, i,
188                           1, movewhite,
189                           2, moveblack,
190                           -1);
191     };
192   
193   return;
194   
195   
196   if(current<0){
197     //  XawListUnhighlight(hist->mvw);
198     //  XawListUnhighlight(hist->mvb);
199   }
200   else if((current%2)==0){
201     //  XawListHighlight(hist->mvw, current/2+1);
202     //  XawListUnhighlight(hist->mvb);
203   }
204   else{
205     //  XawListUnhighlight(hist->mvw);
206     //  if(current) XawListHighlight(hist->mvb, current/2+1);
207     //  else XawListUnhighlight(hist->mvb);
208   }
209
210 return;
211 }
212
213 void HistoryCreate()
214 {
215     String trstr=
216              "<Key>Up: BackwardProc() \n \
217              <Key>Left: BackwardProc() \n \
218              <Key>Down: ForwardProc() \n \
219              <Key>Right: ForwardProc() \n";
220
221     return;
222 }
223
224 void
225 HistoryPopUp()
226 {
227   //  if(!hist) HistoryCreate();
228
229   gtk_widget_show (GUI_History);
230   
231   return;
232 }
233
234
235 void
236 HistoryShowProc(object, user_data)
237      GtkObject *object;
238      gpointer user_data;
239 {
240   if (!hist) 
241     {
242       HistoryCreate();
243       HistoryPopUp();
244     } 
245   else if (hist->Up) 
246     {
247       HistoryPopDown(NULL,NULL);
248     } 
249   else 
250     {
251       HistoryPopUp();
252     }
253   ToNrEvent(currentMove);
254
255   return;
256 }
257