Eliminated Edit-Position menu
[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 "common.h"
48 #include "frontend.h"
49 #include "backend.h"
50 #include "xboard.h"
51 #include "xhistory.h"
52 #include "gettext.h"
53
54 #ifdef ENABLE_NLS
55 # define  _(s) gettext (s)
56 # define N_(s) gettext_noop (s)
57 #else
58 # define  _(s) (s)
59 # define N_(s)  s
60 #endif
61
62 extern GtkWidget               *GUI_History;
63 extern GtkListStore            *LIST_MoveHistory;
64 extern GtkTreeView             *TREE_History;
65
66 String dots=" ... ";
67 Position gameHistoryX, gameHistoryY;
68 Dimension gameHistoryW, gameHistoryH;
69
70 void
71 HistoryPopDown(object, user_data)
72      GtkObject *object;
73      gpointer user_data;
74 {
75   /* hides the history window */
76
77   gtk_widget_hide (GUI_History);
78   return;
79 }
80
81 void 
82 HistoryMoveProc(window, event, data)
83      GtkWindow *window;
84      GdkEvent *event;
85      gpointer data;
86 {
87   int to; /* the move we want to go to */
88   
89   /* check if the mouse was clicked */
90   if(event->type == GDK_BUTTON_PRESS)
91     {   
92       GtkTreeViewColumn *column;
93       GtkTreePath *path;
94       GList *cols;
95       gint *indices;
96       gint row,col;
97       
98       /* can we convert this into an element of the history list? */
99       if(gtk_tree_view_get_path_at_pos(TREE_History,
100                                        (gint)event->button.x, (gint)event->button.y,
101                                        &path,&column,NULL,NULL))
102         {
103           /* find out which row and column the user clicked on */
104           indices = gtk_tree_path_get_indices(path);
105           row     = indices[0];
106           cols    = gtk_tree_view_get_columns(GTK_TREE_VIEW(column->tree_view));
107           col     = g_list_index(cols,(gpointer)column);
108           g_list_free(cols);
109           gtk_tree_path_free(path);
110           
111           printf("DEBUG: row %d col %d\n",row,col);
112
113
114           if(col)
115             {
116               /* user didn't click on the move number */
117
118               to = 2*row + col;
119               printf("DEBUG: going to %d\n",to);fflush(stdout);
120
121               /* set board to that move */
122               ToNrEvent(to);
123             }
124         }
125     }
126   return;
127 }
128
129
130 void HistorySet(char movelist[][2*MOVE_LEN],int first,int last,int current)
131 {
132   int i,b,m;
133   char movewhite[2*MOVE_LEN],moveblack[2*MOVE_LEN],move[2*MOVE_LEN];
134   GtkTreeIter iter;
135
136   /* TODO need to add highlights for current move */
137   /* TODO need to add navigation by keyboard or mouse (double click on move) */
138
139   strcpy(movewhite,"");
140   strcpy(moveblack,"");
141
142   /* first clear everything, do we need this? */
143   gtk_list_store_clear(LIST_MoveHistory);
144
145   /* copy move list into history window */
146
147   /* go through all moves */
148   for(i=0;i<last;i++) 
149     {
150       /* test if there is a move */
151       if(movelist[i][0]) 
152         {
153           /* only copy everything before a  ' ' */
154           char* p = strchr(movelist[i], ' ');
155           if (p) 
156             {
157               strncpy(move, movelist[i], p-movelist[i]);
158               move[p-movelist[i]] = NULLCHAR;
159             } 
160           else 
161             {
162               strcpy(move,movelist[i]);
163             }
164         } 
165       else
166         strcpy(move,dots);
167       
168       if((i%2)==0) 
169         {
170           /* white move */
171           strcpy(movewhite,move);
172         }
173       else
174         {
175           /* black move */
176           strcpy(moveblack,move);
177
178           /* save move */
179           gtk_list_store_append (LIST_MoveHistory, &iter);
180           gtk_list_store_set (LIST_MoveHistory, &iter,
181                               0, (i/2 +1),
182                               1, movewhite,
183                               2, moveblack,
184                               -1);
185
186           strcpy(movewhite,"");
187           strcpy(moveblack,"");
188         };
189     }
190
191   /* check if there is a white move left */
192   if(movewhite[0])
193     {
194       strcpy(moveblack,"");
195
196       /* save move */
197       gtk_list_store_append (LIST_MoveHistory, &iter);
198       gtk_list_store_set (LIST_MoveHistory, &iter,
199                           0, (i/2 +1),
200                           1, movewhite,
201                           2, moveblack,
202                           -1);
203     };
204
205
206   //TODO
207   //  EvalGraphSet( first, last, current, pvInfoList ); // piggy-backed
208   
209   return;
210 }
211
212 void HistoryCreate()
213 {
214     String trstr=
215              "<Key>Up: BackwardProc() \n \
216              <Key>Left: BackwardProc() \n \
217              <Key>Down: ForwardProc() \n \
218              <Key>Right: ForwardProc() \n";
219
220     return;
221 //    if(wpMoveHistory.width > 0) {
222 //      gameHistoryW = wpMoveHistory.width;
223 //      gameHistoryH = wpMoveHistory.height;
224 //      gameHistoryX = wpMoveHistory.x;
225 //      gameHistoryY = wpMoveHistory.y;
226 //    }
227 //
228 //  // [HGM] restore old position
229 //  if(gameHistoryW > 0) {
230 //  j = 0;
231 //    XtSetArg(args[j], XtNx, gameHistoryX);  j++;
232 //  XtSetArg(args[j], XtNy, gameHistoryY);  j++;
233 //    XtSetArg(args[j], XtNwidth, gameHistoryW);  j++;
234 //    XtSetArg(args[j], XtNheight, gameHistoryH);  j++;
235 //  XtSetValues(hist->sh, args, j);
236 //  }
237 }
238
239 void
240 HistoryPopUp()
241 {
242   /* show history window */
243
244   gtk_widget_show (GUI_History);
245   return;
246 }
247
248
249 void
250 HistoryShowProc(object, user_data)
251      GtkObject *object;
252      gpointer user_data;
253 {
254   HistoryCreate();
255   HistoryPopUp();
256   //TODO:  ToNrEvent(currentMove);
257
258   return;
259 }
260
261 Boolean
262 MoveHistoryIsUp()
263 {
264   /* return status of history window */
265   
266   return gtk_widget_get_visible (GUI_History);
267 }