Refactoring of game-list-options dialog
[xboard.git] / gamelistopt.c
1 /*
2  * gamelistopt.c -- Game list window for WinBoard
3  *
4  * Copyright 1995,2009 Free Software Foundation, Inc.
5  *
6  * Enhancements Copyright 2005 Alessandro Scotti
7  *
8  * ------------------------------------------------------------------------
9  *
10  * GNU XBoard is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or (at
13  * your option) any later version.
14  *
15  * GNU XBoard is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see http://www.gnu.org/licenses/.  *
22  *
23  *------------------------------------------------------------------------
24  ** See the file ChangeLog for a revision history.  */
25
26 #include "config.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <malloc.h>
31 #include <fcntl.h>
32 #include <math.h>
33
34 #include "common.h"
35 #include "frontend.h"
36 #include "backend.h"
37
38 void GLT_ClearList();
39 void GLT_DeSelectList();
40 void GLT_AddToList( char *name );
41 Boolean GLT_GetFromList( int index, char *name );
42
43 // back-end
44 typedef struct {
45     char id;
46     char * name;
47 } GLT_Item;
48
49 // back-end: translation table tag id-char <-> full tag name
50 static GLT_Item GLT_ItemInfo[] = {
51     { GLT_EVENT,      "Event" },
52     { GLT_SITE,       "Site" },
53     { GLT_DATE,       "Date" },
54     { GLT_ROUND,      "Round" },
55     { GLT_PLAYERS,    "Players" },
56     { GLT_RESULT,     "Result" },
57     { GLT_WHITE_ELO,  "White Rating" },
58     { GLT_BLACK_ELO,  "Black Rating" },
59     { GLT_TIME_CONTROL,"Time Control" },
60     { GLT_VARIANT,    "Variant" },
61     { GLT_OUT_OF_BOOK,PGN_OUT_OF_BOOK },
62     { GLT_RESULT_COMMENT, "Result Comment" }, // [HGM] rescom
63     { 0, 0 }
64 };
65
66 char lpUserGLT[64];
67
68 // back-end: convert the tag id-char to a full tag name
69 char * GLT_FindItem( char id )
70 {
71     char * result = 0;
72
73     GLT_Item * list = GLT_ItemInfo;
74
75     while( list->id != 0 ) {
76         if( list->id == id ) {
77             result = list->name;
78             break;
79         }
80
81         list++;
82     }
83
84     return result;
85 }
86
87 // back-end: build the list of tag names
88 void
89 GLT_TagsToList( char * tags )
90 {
91     char * pc = tags;
92
93     GLT_ClearList();
94
95     while( *pc ) {
96         GLT_AddToList( GLT_FindItem(*pc) );
97         pc++;
98     }
99
100     GLT_AddToList( "\t --- Hidden tags ---" );
101
102     pc = GLT_ALL_TAGS;
103
104     while( *pc ) {
105         if( strchr( tags, *pc ) == 0 ) {
106             GLT_AddToList( GLT_FindItem(*pc) );
107         }
108         pc++;
109     }
110
111     GLT_DeSelectList();
112 }
113
114 // back-end: retrieve item from dialog and translate to id-char
115 char
116 GLT_ListItemToTag( int index )
117 {
118     char result = '\0';
119     char name[128];
120
121     GLT_Item * list = GLT_ItemInfo;
122
123     if( GLT_GetFromList(index, name) ) {
124         while( list->id != 0 ) {
125             if( strcmp( list->name, name ) == 0 ) {
126                 result = list->id;
127                 break;
128             }
129
130             list++;
131         }
132     }
133
134     return result;
135 }
136
137 // back-end: add items id-chars one-by-one to temp tags string
138 void
139 GLT_ParseList()
140 {
141     char * pc = lpUserGLT;
142     int idx = 0;
143     char id;
144
145     do {
146         id = GLT_ListItemToTag( idx );
147         *pc++ = id;
148         idx++;
149     } while( id != '\0' );
150 }
151