1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2019 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
47 static struct country_codes
57 char line
[LINE_MAX
], *s
;
60 if ((fp
= fopen (config
.ccfile
, "r")) == NULL
)
62 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.ccfile
,
67 while ((s
= fgets (line
, sizeof (line
), fp
)) != NULL
)
71 if ((tmp
= strsep (&s
, " ")) == NULL
)
80 ccodes
= Realloc (ccodes
, (cindex
+ 2) * sizeof (struct country_codes
));
81 strncpy (ccodes
[cindex
].code
, tmp
, sizeof (ccodes
[cindex
].code
));
82 ccodes
[cindex
].code
[sizeof (ccodes
[cindex
].code
) - 1] = 0;
83 strncpy (ccodes
[cindex
].country
, s
, sizeof (ccodes
[cindex
].country
));
84 ccodes
[cindex
].country
[sizeof (ccodes
[cindex
].country
) - 1] = 0;
88 memset (&ccodes
[cindex
], '\0', sizeof (struct country_codes
));
93 static struct menu_item_s
**
94 get_cc_items (WIN
* win
)
97 struct menu_input_s
*m
= win
->data
;
98 struct menu_item_s
**items
= m
->items
;
102 for (i
= 0; items
[i
]; i
++)
106 for (i
= 0; ccodes
[i
].code
[0]; i
++)
108 items
= Realloc (items
, (i
+ 2) * sizeof (struct menu_item_s
*));
109 items
[i
] = Malloc (sizeof (struct menu_item_s
));
110 items
[i
]->name
= ccodes
[i
].code
;
111 items
[i
]->value
= ccodes
[i
].country
;
112 items
[i
]->selected
= 0;
123 do_cc_help (struct menu_input_s
*m
)
125 message (_("Country Code Keys"), ANY_KEY_STR
, "%s",
126 _(" UP/DOWN - previous/next menu item\n"
127 " HOME/END - first/last menu item\n"
128 " PGDN/PGUP - next/previous page\n"
129 " a-zA-Z0-9 - jump to item\n"
130 " ENTER - select item\n" " ESCAPE - cancel"));
134 do_cc_abort (struct menu_input_s
*m
)
140 do_cc_finalize (WIN
* win
)
142 struct input_s
*in
= win
->data
;
143 TAG
*t
= (TAG
*) in
->arg
;
145 set_field_buffer (in
->fields
[0], 0, t
->value
);
149 do_cc_save (struct menu_input_s
*m
)
151 struct input_s
*in
= m
->data
;
152 TAG
*t
= (TAG
*) in
->arg
;
153 int len
= strlen (m
->items
[m
->selected
]->name
);
155 t
->value
= Realloc (t
->value
, len
+ 1);
156 strcpy (t
->value
, m
->items
[m
->selected
]->name
);
163 struct menu_input_s
*m
= win
->data
;
165 mvwprintw (win
->w
, m
->print_line
, 1, "%s %-*s", m
->item
->name
,
166 win
->cols
- 6, m
->item
->value
);
170 country_codes (void *arg
)
172 struct menu_key_s
**keys
= NULL
;
176 if (init_country_codes ())
180 add_menu_key (&keys
, keycode_lookup (global_keys
, do_global_help
),
182 add_menu_key (&keys
, KEY_ESCAPE
, do_cc_abort
);
183 add_menu_key (&keys
, '\n', do_cc_save
);
184 construct_menu (0, 0, -1, -1, _("Country Codes"), 0, get_cc_items
, keys
,
185 arg
, cc_print
, do_cc_finalize
, NULL
);
190 add_custom_tags (TAG
*** t
)
193 int total
= pgn_tag_total (config
.tag
);
198 for (i
= 0; i
< total
; i
++)
199 pgn_tag_add (t
, config
.tag
[i
]->name
, config
.tag
[i
]->value
);
204 static struct menu_item_s
**
205 get_tag_items (WIN
* win
)
208 struct menu_input_s
*m
= win
->data
;
209 struct menu_item_s
**items
= m
->items
;
210 TAG
**t
= (m
->data
) ? m
->data
: gp
->tag
;
214 for (i
= 0; items
[i
]; i
++)
218 n
= pgn_tag_total (t
);
220 for (i
= 0; i
< n
; i
++)
222 items
= Realloc (items
, (i
+ 2) * sizeof (struct menu_item_s
*));
223 items
[i
] = Malloc (sizeof (struct menu_item_s
));
224 items
[i
]->name
= t
[i
]->name
;
225 items
[i
]->value
= t
[i
]->value
;
226 items
[i
]->selected
= 0;
237 edit_tag_add_fen (struct menu_input_s
*m
)
240 struct userdata_s
*d
= gp
->data
;
241 char *fen
= pgn_game_to_fen (gp
, d
->b
);
243 pgn_tag_add (&t
, (char *) "FEN", fen
);
249 edit_tag_abort (struct menu_input_s
*m
)
256 update_status_notify (gp
, _("Tag edit aborted."));
260 edit_tag_add_finalize (WIN
* w
)
262 struct input_data_s
*in
= w
->data
;
263 char *name
= in
->moredata
;
264 char *value
= in
->str
;
265 struct menu_input_s
*m
= in
->data
;
273 if (!value
|| !*value
)
275 if (strcasecmp (name
, "Round") == 0)
276 value
= (char *) "-";
277 else if (strcasecmp (name
, "Result") == 0)
278 value
= (char *) "*";
279 else if (strcasecmp (name
, "Date") == 0)
282 tm
= localtime (&now
);
283 strftime (buf
, sizeof (buf
), PGN_TIME_FORMAT
, tm
);
287 value
= (char *) "?";
290 tmp
= trim_multi (value
);
291 count
= pgn_tag_total (t
);
292 pgn_tag_add (&t
, name
, tmp
);
296 if (count
!= pgn_tag_total (t
))
297 m
->selected
= m
->total
;
304 pushkey
= REFRESH_MENU
;
308 set_menu_stuff (TAG
** t
, char *name
, char **init
, int *type
,
309 int *lines
, input_func
** func
, wint_t * key
, char **eprompt
,
315 if ((n
= pgn_tag_find (t
, name
)) != -1)
321 if (strcasecmp (name
, "Date") == 0)
323 *type
= FIELD_TYPE_PGN_DATE
;
326 else if (strcasecmp (name
, "Site") == 0)
328 *func
= country_codes
;
329 *key
= CTRL_KEY ('t');
330 *eprompt
= _("Type CTRL-t for country codes");
333 else if (strcasecmp (name
, "Round") == 0)
335 *type
= FIELD_TYPE_PGN_ROUND
;
338 else if (strcasecmp (name
, "Result") == 0)
340 *type
= FIELD_TYPE_PGN_RESULT
;
346 edit_tag_value (struct menu_input_s
*m
)
349 struct input_data_s
*in
= Calloc (1, sizeof (struct input_data_s
));
354 input_func
*func
= NULL
;
356 char *eprompt
= NULL
;
358 int lines
= MAX_PGN_LINE_LEN
/ INPUT_WIDTH
;
362 name
= strdup (t
[m
->selected
]->name
);
364 in
->efunc
= edit_tag_add_finalize
;
365 wc
= str_to_wchar (name
);
366 snprintf (buf
, sizeof (buf
), "%s \"%ls\"", _("Editing Tag"), wc
);
368 set_menu_stuff (t
, name
, &init
, &type
, &lines
, &func
, &key
, &eprompt
, &arg
);
369 construct_input (buf
, init
, lines
, 0, eprompt
, func
, arg
, key
, in
, -1,
374 edit_tag_add_name_finalize (WIN
* w
)
376 struct input_data_s
*in
= w
->data
;
377 struct input_data_s
*inv
;
378 struct menu_input_s
*m
= in
->data
;
385 input_func
*func
= NULL
;
386 char *eprompt
= NULL
;
387 int lines
= MAX_PGN_LINE_LEN
/ INPUT_WIDTH
;
394 name
= strdup (in
->str
);
395 inv
= Calloc (1, sizeof (struct input_data_s
));
396 inv
->efunc
= edit_tag_add_finalize
;
397 inv
->data
= in
->data
;
398 inv
->moredata
= name
;
401 wc
= str_to_wchar (name
);
402 snprintf (buf
, sizeof (buf
), "%s \"%ls\"", _("Editing Tag"), wc
);
404 set_menu_stuff (t
, name
, &init
, &type
, &lines
, &func
, &key
, &eprompt
, &arg
);
405 construct_input (buf
, init
, lines
, 0, eprompt
, func
, arg
, key
, inv
, -1,
410 edit_tag_add (struct menu_input_s
*m
)
412 struct input_data_s
*in
= Calloc (1, sizeof (struct input_s
));
415 in
->efunc
= edit_tag_add_name_finalize
;
416 construct_input (_("New Tag Name"), NULL
, 1, 1, NULL
, NULL
, NULL
, 0, in
, -1,
417 NULL
, FIELD_TYPE_PGN_TAG_NAME
);
421 edit_tag_remove (struct menu_input_s
*m
)
425 int i
, n
= pgn_tag_total (t
);
429 cmessage (NULL
, ANY_KEY_STR
, "%s",
430 _("Cannot remove the Seven Tag Roster"));
434 for (i
= 0; i
< n
; i
++)
436 if (i
== m
->selected
)
439 pgn_tag_add (&data
, t
[i
]->name
, t
[i
]->value
);
448 edit_tag_add_custom (struct menu_input_s
*m
)
452 add_custom_tags (&t
);
457 edit_tag_save (struct menu_input_s
*m
)
460 struct userdata_s
*d
= gp
->data
;
465 pgn_tag_free (gp
->tag
);
469 SET_FLAG (d
->flags
, CF_MODIFIED
);
472 * In case of editing a FEN tag. Must not be MODE_PLAY. Also updates the
473 * games ply count for the fifty move draw rule.
475 if (d
->mode
!= MODE_PLAY
)
476 pgn_board_update (gp
, d
->b
, gp
->hindex
);
480 edit_tag_help (struct menu_input_s
*m
)
482 message (_("Tag Editing Keys"), ANY_KEY_STR
,
483 _(" UP/DOWN - previous/next menu item\n"
484 " HOME/END - first/last menu item\n"
485 " PGDN/PGUP - next/previous page\n"
486 " a-zA-Z0-9 - jump to item\n"
487 " ENTER - edit select item\n"
488 " CTRL-a - add an entry\n"
489 " CTRL-f - add FEN tag from current position\n"
490 " CTRL-r - remove selected entry\n"
491 " CTRL-t - add custom tags\n"
492 " CTRL-x - quit with changes\n"
493 " ESCAPE - quit without changes"));
497 view_tag_help (struct menu_input_s
*m
)
499 message (_("Tag Viewing Keys"), ANY_KEY_STR
,
500 _(" UP/DOWN - previous/next menu item\n"
501 " HOME/END - first/last menu item\n"
502 " PGDN/PGUP - next/previous page\n"
503 " a-zA-Z0-9 - jump to item\n"
504 " ENTER - view selected item\n" " ESCAPE - cancel"));
508 view_tag_quit (struct menu_input_s
*m
)
514 view_tag_value (struct menu_input_s
*m
)
516 struct menu_item_s
*item
= m
->items
[m
->selected
];
518 wchar_t *wc
= str_to_wchar (item
->name
);
520 snprintf (buf
, sizeof (buf
), "%s \"%ls\"", _("Viewing Tag"), wc
);
522 construct_message (buf
, ANY_KEY_STR
, 0, 1, NULL
,
523 NULL
, NULL
, NULL
, 0, 0, NULL
, "%s", item
->value
);
527 tag_print (WIN
* win
)
530 struct menu_input_s
*m
= win
->data
;
533 for (i
= 0; m
->items
[i
]; i
++)
535 wc
= translate_tag_name (m
->items
[i
]->name
);
542 wc
= translate_tag_name (m
->item
->name
);
543 mvwprintw (win
->w
, m
->print_line
, 1, "%ls", wc
);
545 for (n
= wcslen (wc
) + 1; n
<= len
; n
++)
546 mvwprintw (win
->w
, m
->print_line
, n
, "%c", '.');
549 mvwprintw (win
->w
, m
->print_line
, n
, ": ");
550 i
= win
->cols
- n
- 3;
551 wc
= str_etc (m
->item
->value
, i
, 0);
552 mvwprintw (win
->w
, m
->print_line
, n
+ 2, "%-*ls", i
, wc
);
557 wmove (stdscr
, 0, 0);
565 edit_tags (GAME g
, BOARD b
, int edit
)
567 struct menu_key_s
**keys
= NULL
;
573 for (i
= 0; gp
->tag
[i
]; i
++)
574 pgn_tag_add (&data
, gp
->tag
[i
]->name
, gp
->tag
[i
]->value
);
576 add_menu_key (&keys
, '\n', edit_tag_value
);
577 add_menu_key (&keys
, CTRL_KEY ('f'), edit_tag_add_fen
);
578 add_menu_key (&keys
, CTRL_KEY ('a'), edit_tag_add
);
579 add_menu_key (&keys
, CTRL_KEY ('r'), edit_tag_remove
);
580 add_menu_key (&keys
, CTRL_KEY ('t'), edit_tag_add_custom
);
581 add_menu_key (&keys
, CTRL_KEY ('x'), edit_tag_save
);
582 add_menu_key (&keys
, KEY_ESCAPE
, edit_tag_abort
);
583 add_menu_key (&keys
, keycode_lookup (global_keys
, do_global_help
),
588 add_menu_key (&keys
, '\n', view_tag_value
);
590 add_menu_key (&keys
, KEY_ESCAPE
, view_tag_quit
);
591 add_menu_key (&keys
, keycode_lookup (global_keys
, do_global_help
),
595 construct_menu (0, 0, -1, -1,
596 (edit
) ? _("Editing Roster Tags") :
597 _("Viewing Roster Tags"), 0, get_tag_items
, keys
, data
,
598 tag_print
, NULL
, NULL
);