4abca99f28e0ee9dbca75218cdd9065aa32af524
[xboard.git] / winboard-dm-beta4 / wgamelist.c
1 /*
2  * wgamelist.c -- Game list window for WinBoard
3  * $Id$
4  *
5  * Copyright 1995 Free Software Foundation, Inc.
6  *
7  * ------------------------------------------------------------------------
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  * ------------------------------------------------------------------------
22  */
23
24 #include "config.h"
25
26 #include <windows.h> /* required for all Windows applications */
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <malloc.h>
30 #include <io.h>
31 #include <fcntl.h>
32 #include <math.h>
33 #include <commdlg.h>
34 #include <dlgs.h>
35
36 #include "common.h"
37 #include "winboard.h"
38 #include "frontend.h"
39 #include "backend.h"
40
41 /* Module globals */
42 HWND gameListDialog = NULL;
43 BOOLEAN gameListUp = FALSE;
44 FILE* gameFile;
45 char* gameFileName = NULL;
46 int gameListX, gameListY, gameListW, gameListH;
47
48 /* Imports from winboard.c */
49 extern HINSTANCE hInst;
50 extern HWND hwndMain;
51
52
53 LRESULT CALLBACK
54 GameListDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
55 {
56   static HANDLE hwndText;
57   int nItem;
58   ListGame *lg;
59   RECT rect;
60   static int sizeX, sizeY;
61   int newSizeX, newSizeY, flags;
62   MINMAXINFO *mmi;
63
64   switch (message) {
65   case WM_INITDIALOG: 
66     if (gameListDialog) {
67       SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
68     }
69     /* Initialize the dialog items */
70     hwndText = GetDlgItem(hDlg, OPT_TagsText);
71     lg = (ListGame *) gameList.head;
72     for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
73       char *st = GameListLine(lg->number, &lg->gameInfo);
74       SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);
75       free(st);
76       lg = (ListGame *) lg->node.succ;
77     }
78     SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);
79     /* Size and position the dialog */
80     if (!gameListDialog) {
81       gameListDialog = hDlg;
82       flags = SWP_NOZORDER;
83       GetClientRect(hDlg, &rect);
84       sizeX = rect.right;
85       sizeY = rect.bottom;
86       if (gameListX != CW_USEDEFAULT && gameListY != CW_USEDEFAULT &&
87           gameListW != CW_USEDEFAULT && gameListH != CW_USEDEFAULT) {
88         WINDOWPLACEMENT wp;
89         EnsureOnScreen(&gameListX, &gameListY);
90         wp.length = sizeof(WINDOWPLACEMENT);
91         wp.flags = 0;
92         wp.showCmd = SW_SHOW;
93         wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;
94         wp.rcNormalPosition.left = gameListX;
95         wp.rcNormalPosition.right = gameListX + gameListW;
96         wp.rcNormalPosition.top = gameListY;
97         wp.rcNormalPosition.bottom = gameListY + gameListH;
98         SetWindowPlacement(hDlg, &wp);
99
100         GetClientRect(hDlg, &rect);
101         newSizeX = rect.right;
102         newSizeY = rect.bottom;
103         ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,
104                               newSizeX, newSizeY);
105         sizeX = newSizeX;
106         sizeY = newSizeY;
107       }
108     }
109     return FALSE;
110     
111   case WM_SIZE:
112     newSizeX = LOWORD(lParam);
113     newSizeY = HIWORD(lParam);
114     ResizeEditPlusButtons(hDlg, GetDlgItem(hDlg, OPT_GameListText),
115       sizeX, sizeY, newSizeX, newSizeY);
116     sizeX = newSizeX;
117     sizeY = newSizeY;
118     break;
119
120   case WM_GETMINMAXINFO:
121     /* Prevent resizing window too small */
122     mmi = (MINMAXINFO *) lParam;
123     mmi->ptMinTrackSize.x = 100;
124     mmi->ptMinTrackSize.y = 100;
125     break;
126
127   case WM_COMMAND:
128     switch (LOWORD(wParam)) {
129     case IDOK:
130     case OPT_GameListLoad:
131       nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
132       if (nItem < 0) {
133         /* is this possible? */
134         DisplayError("No game selected", 0);
135         return TRUE;
136       }
137       break; /* load the game*/
138       
139     case OPT_GameListNext:
140       nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
141       nItem++;
142       if (nItem >= ((ListGame *) gameList.tailPred)->number) {
143         DisplayError("Can't go forward any further", 0);
144         return TRUE;
145       }
146       SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
147       break; /* load the game*/
148       
149     case OPT_GameListPrev:
150       nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
151       nItem--;
152       if (nItem < 0) {
153         DisplayError("Can't back up any further", 0);
154       }
155       SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
156       break; /* load the game*/
157       
158     case IDCANCEL:
159     case OPT_GameListClose:
160       GameListPopDown();
161       return TRUE;
162       
163     case OPT_GameListText:
164       switch (HIWORD(wParam)) {
165       case LBN_DBLCLK:
166         nItem = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);
167         break; /* load the game*/
168         
169       default:
170         return FALSE;
171       }
172       break;
173       
174     default:
175       return FALSE;
176     }
177     /* Load the game */
178     if (cmailMsgLoaded) {
179       CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);
180     } else {
181       LoadGame(gameFile, nItem + 1, gameFileName, TRUE);
182     }
183     return TRUE;
184
185   default:
186     break;
187   }
188   return FALSE;
189 }
190
191
192 VOID GameListPopUp(FILE *fp, char *filename)
193 {
194   FARPROC lpProc;
195   
196   gameFile = fp;
197   if (gameFileName != filename) {
198     if (gameFileName) free(gameFileName);
199     gameFileName = StrSave(filename);
200   }
201   CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_CHECKED);
202   if (gameListDialog) {
203     SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);
204     if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);
205   } else {
206     lpProc = MakeProcInstance((FARPROC)GameListDialog, hInst);
207     CreateDialog(hInst, MAKEINTRESOURCE(DLG_GameList),
208       hwndMain, (DLGPROC)lpProc);
209     FreeProcInstance(lpProc);
210   }
211   gameListUp = TRUE;
212 }
213
214 VOID GameListPopDown(void)
215 {
216   CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);
217   if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);
218   gameListUp = FALSE;
219 }
220
221
222 VOID GameListHighlight(int index)
223 {
224   if (gameListDialog == NULL) return;
225   SendDlgItemMessage(gameListDialog, OPT_GameListText, 
226     LB_SETCURSEL, index - 1, 0);
227 }
228
229
230 VOID GameListDestroy()
231 {
232   GameListPopDown();
233   if (gameFileName) {
234     free(gameFileName);
235     gameFileName = NULL;
236   }
237 }
238
239 VOID ShowGameListProc()
240 {
241   if (gameListUp) {
242     GameListPopDown();
243   } else {
244     if (gameFileName) {
245       GameListPopUp(gameFile, gameFileName);
246     } else {
247       DisplayError("No game list", 0);
248     }
249   }
250 }