translated a handfull of strings and set the codeset to UTF-8 for the translations.
[xboard.git] / winboard-dm-beta4 / wedittags.c
1 /*
2  * wedittags.c -- EditTags 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 static char *editTagsText;
43 HWND editTagsDialog = NULL;
44 BOOL editTagsUp = FALSE;
45 BOOL canEditTags = FALSE;
46 int editTagsX, editTagsY, editTagsW, editTagsH;
47
48 /* Imports from winboard.c */
49 extern HINSTANCE hInst;
50 extern HWND hwndMain;
51 extern BoardSize boardSize;
52
53 LRESULT CALLBACK
54 EditTagsDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
55 {
56   static HANDLE hwndText;
57   static int sizeX, sizeY;
58   int len, newSizeX, newSizeY, flags;
59   char *str;
60   RECT rect;
61   MINMAXINFO *mmi;
62   int err;
63   
64   switch (message) {
65   case WM_INITDIALOG: /* message: initialize dialog box */
66     /* Initialize the dialog items */
67     hwndText = GetDlgItem(hDlg, OPT_TagsText);
68     SendMessage(hwndText, WM_SETFONT, 
69       (WPARAM)font[boardSize][EDITTAGS_FONT]->hf, MAKELPARAM(FALSE, 0));
70     SetDlgItemText(hDlg, OPT_TagsText, editTagsText);
71     EnableWindow(GetDlgItem(hDlg, OPT_TagsCancel), canEditTags);
72     EnableWindow(GetDlgItem(hDlg, OPT_EditTags), !canEditTags);
73     SendMessage(hwndText, EM_SETREADONLY, !canEditTags, 0);
74     if (canEditTags) {
75       SetWindowText(hDlg, "Edit Tags");
76       SetFocus(hwndText);
77     } else {
78       SetWindowText(hDlg, "Tags");
79       SetFocus(GetDlgItem(hDlg, IDOK));
80     }
81     if (!editTagsDialog) {
82       editTagsDialog = hDlg;
83       flags = SWP_NOZORDER;
84       GetClientRect(hDlg, &rect);
85       sizeX = rect.right;
86       sizeY = rect.bottom;
87       if (editTagsX != CW_USEDEFAULT && editTagsY != CW_USEDEFAULT &&
88           editTagsW != CW_USEDEFAULT && editTagsH != CW_USEDEFAULT) {
89         WINDOWPLACEMENT wp;
90         EnsureOnScreen(&editTagsX, &editTagsY);
91         wp.length = sizeof(WINDOWPLACEMENT);
92         wp.flags = 0;
93         wp.showCmd = SW_SHOW;
94         wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;
95         wp.rcNormalPosition.left = editTagsX;
96         wp.rcNormalPosition.right = editTagsX + editTagsW;
97         wp.rcNormalPosition.top = editTagsY;
98         wp.rcNormalPosition.bottom = editTagsY + editTagsH;
99         SetWindowPlacement(hDlg, &wp);
100
101         GetClientRect(hDlg, &rect);
102         newSizeX = rect.right;
103         newSizeY = rect.bottom;
104         ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,
105                               newSizeX, newSizeY);
106         sizeX = newSizeX;
107         sizeY = newSizeY;
108       }
109     }
110     return FALSE;
111     
112   case WM_COMMAND:
113     switch (LOWORD(wParam)) {
114     case IDOK:
115       if (canEditTags) {
116         char *p, *q;
117         /* Read changed options from the dialog box */
118         len = GetWindowTextLength(hwndText);
119         str = (char *) malloc(len + 1);
120         GetWindowText(hwndText, str, len + 1);
121         p = q = str;
122         while (*q) {
123           if (*q == '\r'|| *q == '\n')
124             q++;
125           else
126             *p++ = *q++;
127         }
128         *p = NULLCHAR;
129         err = ReplaceTags(str, &gameInfo);
130         if (err) DisplayError("Error replacing tags.", err);
131
132         free(str);
133       }
134       TagsPopDown();
135       return TRUE;
136       
137     case IDCANCEL:
138     case OPT_TagsCancel:
139       TagsPopDown();
140       return TRUE;
141       
142     case OPT_EditTags:
143       EditTagsEvent();
144       return TRUE;
145
146     default:
147       break;
148     }
149     break;
150
151   case WM_SIZE:
152     newSizeX = LOWORD(lParam);
153     newSizeY = HIWORD(lParam);
154     ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY, newSizeX, newSizeY);
155     sizeX = newSizeX;
156     sizeY = newSizeY;
157     break;
158
159   case WM_GETMINMAXINFO:
160     /* Prevent resizing window too small */
161     mmi = (MINMAXINFO *) lParam;
162     mmi->ptMinTrackSize.x = 100;
163     mmi->ptMinTrackSize.y = 100;
164     break;
165   }
166   return FALSE;
167 }
168
169 VOID TagsPopDown(void)
170 {
171   if (editTagsDialog) ShowWindow(editTagsDialog, SW_HIDE);
172   CheckMenuItem(GetMenu(hwndMain), IDM_EditTags, MF_UNCHECKED);
173   editTagsUp = FALSE;
174 }
175
176
177 VOID EitherTagsPopUp(char *tags, char *msg, BOOLEAN edit)
178 {
179   FARPROC lpProc;
180   char *p, *q;
181   
182   if (msg == NULL) msg = "";
183   p = (char *) malloc(2 * (strlen(tags) + strlen(msg)) + 2);
184   q = p;
185   while (*tags) {
186     if (*tags == '\n') *q++ = '\r';
187     *q++ = *tags++;
188   }
189   if (*msg != NULLCHAR) {
190     *q++ = '\r';
191     *q++ = '\n';
192     while (*msg) {
193       if (*msg == '\n') *q++ = '\r';
194       *q++ = *msg++;
195     }
196   }
197   *q = NULLCHAR;
198   if (editTagsText != NULL) free(editTagsText);
199   editTagsText = p;
200   canEditTags = edit;
201   
202   CheckMenuItem(GetMenu(hwndMain), IDM_EditTags, MF_CHECKED);
203   if (editTagsDialog) {
204     SendMessage(editTagsDialog, WM_INITDIALOG, 0, 0);
205     ShowWindow(editTagsDialog, SW_SHOW);
206   } else {
207     lpProc = MakeProcInstance((FARPROC)EditTagsDialog, hInst);
208     CreateDialog(hInst, MAKEINTRESOURCE(DLG_EditTags),
209       hwndMain, (DLGPROC)lpProc);
210     FreeProcInstance(lpProc);
211   }
212   editTagsUp = TRUE;
213 }
214
215 VOID TagsPopUp(char *tags, char *msg)
216 {
217   HWND hwnd = GetActiveWindow();
218   EitherTagsPopUp(tags, msg, FALSE);
219   SetActiveWindow(hwnd);
220 }
221
222 VOID EditTagsPopUp(char *tags)
223 {
224   EitherTagsPopUp(tags, "", TRUE);
225 }
226
227 VOID EditTagsProc()
228 {
229   if (editTagsUp) {
230     TagsPopDown();
231   } else {
232     EditTagsEvent();
233   }
234 }