1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2024 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;
125 do_cc_help (struct menu_input_s
*m
)
127 message (_("Country Code Keys"), ANY_KEY_STR
, "%s",
128 _(" UP/DOWN - previous/next menu item\n"
129 " HOME/END - first/last menu item\n"
130 " PGDN/PGUP - next/previous page\n"
131 " a-zA-Z0-9 - jump to item\n"
132 " ENTER - select item\n" " ESCAPE - cancel"));
136 do_cc_abort (struct menu_input_s
*m
)
142 do_cc_finalize (WIN
* win
)
144 struct input_s
*in
= win
->data
;
145 TAG
*t
= (TAG
*) in
->arg
;
147 set_field_buffer (in
->fields
[0], 0, t
->value
);
151 do_cc_save (struct menu_input_s
*m
)
153 struct input_s
*in
= m
->data
;
154 TAG
*t
= (TAG
*) in
->arg
;
155 int len
= strlen (m
->items
[m
->selected
]->name
);
157 t
->value
= Realloc (t
->value
, len
+ 1);
158 strcpy (t
->value
, m
->items
[m
->selected
]->name
);
165 struct menu_input_s
*m
= win
->data
;
167 mvwprintw (win
->w
, m
->print_line
, 1, "%s %-*s", m
->item
->name
,
168 win
->cols
- 6, m
->item
->value
);
172 country_codes (void *arg
)
174 struct menu_key_s
**keys
= NULL
;
178 if (init_country_codes ())
182 add_menu_key (&keys
, keycode_lookup (global_keys
, do_global_help
),
184 add_menu_key (&keys
, KEY_ESCAPE
, do_cc_abort
);
185 add_menu_key (&keys
, '\n', do_cc_save
);
186 construct_menu (0, 0, -1, -1, _("Country Codes"), 0, get_cc_items
, keys
,
187 arg
, cc_print
, do_cc_finalize
, NULL
);
192 add_custom_tags (TAG
*** t
)
195 int total
= pgn_tag_total (config
.tag
);
200 for (i
= 0; i
< total
; i
++)
201 pgn_tag_add (t
, config
.tag
[i
]->name
, config
.tag
[i
]->value
);
206 static struct menu_item_s
**
207 get_tag_items (WIN
* win
)
210 struct menu_input_s
*m
= win
->data
;
211 struct menu_item_s
**items
= m
->items
;
212 TAG
**t
= (m
->data
) ? m
->data
: gp
->tag
;
216 for (i
= 0; items
[i
]; i
++)
220 n
= pgn_tag_total (t
);
222 for (i
= 0; i
< n
; i
++)
224 items
= Realloc (items
, (i
+ 2) * sizeof (struct menu_item_s
*));
225 items
[i
] = Malloc (sizeof (struct menu_item_s
));
226 items
[i
]->name
= t
[i
]->name
;
227 items
[i
]->value
= t
[i
]->value
;
228 items
[i
]->selected
= 0;
241 edit_tag_add_fen (struct menu_input_s
*m
)
244 struct userdata_s
*d
= gp
->data
;
245 char *fen
= pgn_game_to_fen (gp
, d
->b
);
247 pgn_tag_add (&t
, (char *) "FEN", fen
);
253 edit_tag_abort (struct menu_input_s
*m
)
260 update_status_notify (gp
, _("Tag edit aborted."));
264 edit_tag_add_finalize (WIN
* w
)
266 struct input_data_s
*in
= w
->data
;
267 char *name
= in
->moredata
;
268 char *value
= in
->str
;
269 struct menu_input_s
*m
= in
->data
;
277 if (!value
|| !*value
)
279 if (strcasecmp (name
, "Round") == 0)
280 value
= (char *) "-";
281 else if (strcasecmp (name
, "Result") == 0)
282 value
= (char *) "*";
283 else if (strcasecmp (name
, "Date") == 0)
286 tm
= localtime (&now
);
287 strftime (buf
, sizeof (buf
), PGN_TIME_FORMAT
, tm
);
291 value
= (char *) "?";
294 tmp
= trim_multi (value
);
295 count
= pgn_tag_total (t
);
296 pgn_tag_add (&t
, name
, tmp
);
300 if (count
!= pgn_tag_total (t
))
301 m
->selected
= m
->total
;
308 pushkey
= REFRESH_MENU
;
312 set_menu_stuff (TAG
** t
, char *name
, char **init
, int *type
,
313 int *lines
, input_func
** func
, wint_t * key
, char **eprompt
,
319 if ((n
= pgn_tag_find (t
, name
)) != -1)
325 if (strcasecmp (name
, "Date") == 0)
327 *type
= FIELD_TYPE_PGN_DATE
;
330 else if (strcasecmp (name
, "Site") == 0)
332 *func
= country_codes
;
333 *key
= CTRL_KEY ('t');
334 *eprompt
= _("Type CTRL-t for country codes");
337 else if (strcasecmp (name
, "Round") == 0)
339 *type
= FIELD_TYPE_PGN_ROUND
;
342 else if (strcasecmp (name
, "Result") == 0)
344 *type
= FIELD_TYPE_PGN_RESULT
;
350 edit_tag_value (struct menu_input_s
*m
)
353 struct input_data_s
*in
= Calloc (1, sizeof (struct input_data_s
));
358 input_func
*func
= NULL
;
360 char *eprompt
= NULL
;
362 int lines
= MAX_PGN_LINE_LEN
/ INPUT_WIDTH
;
366 name
= strdup (t
[m
->selected
]->name
);
368 in
->efunc
= edit_tag_add_finalize
;
369 wc
= str_to_wchar (name
);
370 snprintf (buf
, sizeof (buf
), "%s \"%ls\"", _("Editing Tag"), wc
);
372 set_menu_stuff (t
, name
, &init
, &type
, &lines
, &func
, &key
, &eprompt
, &arg
);
373 construct_input (buf
, init
, lines
, 0, eprompt
, func
, arg
, key
, in
, -1,
378 edit_tag_add_name_finalize (WIN
* w
)
380 struct input_data_s
*in
= w
->data
;
381 struct input_data_s
*inv
;
382 struct menu_input_s
*m
= in
->data
;
389 input_func
*func
= NULL
;
390 char *eprompt
= NULL
;
391 int lines
= MAX_PGN_LINE_LEN
/ INPUT_WIDTH
;
398 name
= strdup (in
->str
);
399 inv
= Calloc (1, sizeof (struct input_data_s
));
400 inv
->efunc
= edit_tag_add_finalize
;
401 inv
->data
= in
->data
;
402 inv
->moredata
= name
;
405 wc
= str_to_wchar (name
);
406 snprintf (buf
, sizeof (buf
), "%s \"%ls\"", _("Editing Tag"), wc
);
408 set_menu_stuff (t
, name
, &init
, &type
, &lines
, &func
, &key
, &eprompt
, &arg
);
409 construct_input (buf
, init
, lines
, 0, eprompt
, func
, arg
, key
, inv
, -1,
414 edit_tag_add (struct menu_input_s
*m
)
416 struct input_data_s
*in
= Calloc (1, sizeof (struct input_s
));
419 in
->efunc
= edit_tag_add_name_finalize
;
420 construct_input (_("New Tag Name"), NULL
, 1, 1, NULL
, NULL
, NULL
, 0, in
, -1,
421 NULL
, FIELD_TYPE_PGN_TAG_NAME
);
425 edit_tag_remove (struct menu_input_s
*m
)
429 int i
, n
= pgn_tag_total (t
);
433 cmessage (NULL
, ANY_KEY_STR
, "%s",
434 _("Cannot remove the Seven Tag Roster"));
438 for (i
= 0; i
< n
; i
++)
440 if (i
== m
->selected
)
443 pgn_tag_add (&data
, t
[i
]->name
, t
[i
]->value
);
452 edit_tag_add_custom (struct menu_input_s
*m
)
456 add_custom_tags (&t
);
461 edit_tag_save (struct menu_input_s
*m
)
464 struct userdata_s
*d
= gp
->data
;
469 pgn_tag_free (gp
->tag
);
473 SET_FLAG (d
->flags
, CF_MODIFIED
);
476 * In case of editing a FEN tag. Must not be MODE_PLAY. Also updates the
477 * games ply count for the fifty move draw rule.
479 if (d
->mode
!= MODE_PLAY
)
480 pgn_board_update (gp
, d
->b
, gp
->hindex
);
484 edit_tag_help (struct menu_input_s
*m
)
486 message (_("Tag Editing Keys"), ANY_KEY_STR
,
487 _(" UP/DOWN - previous/next menu item\n"
488 " HOME/END - first/last menu item\n"
489 " PGDN/PGUP - next/previous page\n"
490 " a-zA-Z0-9 - jump to item\n"
491 " ENTER - edit select item\n"
492 " CTRL-a - add an entry\n"
493 " CTRL-f - add FEN tag from current position\n"
494 " CTRL-r - remove selected entry\n"
495 " CTRL-t - add custom tags\n"
496 " CTRL-x - quit with changes\n"
497 " ESCAPE - quit without changes"));
501 view_tag_help (struct menu_input_s
*m
)
503 message (_("Tag Viewing Keys"), ANY_KEY_STR
,
504 _(" UP/DOWN - previous/next menu item\n"
505 " HOME/END - first/last menu item\n"
506 " PGDN/PGUP - next/previous page\n"
507 " a-zA-Z0-9 - jump to item\n"
508 " ENTER - view selected item\n" " ESCAPE - cancel"));
512 view_tag_quit (struct menu_input_s
*m
)
518 view_tag_value (struct menu_input_s
*m
)
520 struct menu_item_s
*item
= m
->items
[m
->selected
];
522 wchar_t *wc
= str_to_wchar (item
->name
);
524 snprintf (buf
, sizeof (buf
), "%s \"%ls\"", _("Viewing Tag"), wc
);
526 construct_message (buf
, ANY_KEY_STR
, 0, 1, NULL
,
527 NULL
, NULL
, NULL
, 0, 0, NULL
, "%s", item
->value
);
531 tag_print (WIN
* win
)
534 struct menu_input_s
*m
= win
->data
;
537 for (i
= 0; m
->items
[i
]; i
++)
539 wc
= translate_tag_name (m
->items
[i
]->name
);
546 wc
= translate_tag_name (m
->item
->name
);
547 mvwprintw (win
->w
, m
->print_line
, 1, "%ls", wc
);
549 for (n
= wcslen (wc
) + 1; n
<= len
; n
++)
550 mvwprintw (win
->w
, m
->print_line
, n
, "%c", '.');
553 mvwprintw (win
->w
, m
->print_line
, n
, ": ");
554 i
= win
->cols
- n
- 3;
555 wc
= str_etc (m
->item
->value
, i
, 0);
556 mvwprintw (win
->w
, m
->print_line
, n
+ 2, "%-*ls", i
, wc
);
561 wmove (stdscr
, 0, 0);
569 edit_tags (GAME g
, BOARD b
, int edit
)
571 struct menu_key_s
**keys
= NULL
;
577 for (i
= 0; gp
->tag
[i
]; i
++)
578 pgn_tag_add (&data
, gp
->tag
[i
]->name
, gp
->tag
[i
]->value
);
580 add_menu_key (&keys
, '\n', edit_tag_value
);
581 add_menu_key (&keys
, CTRL_KEY ('f'), edit_tag_add_fen
);
582 add_menu_key (&keys
, CTRL_KEY ('a'), edit_tag_add
);
583 add_menu_key (&keys
, CTRL_KEY ('r'), edit_tag_remove
);
584 add_menu_key (&keys
, CTRL_KEY ('t'), edit_tag_add_custom
);
585 add_menu_key (&keys
, CTRL_KEY ('x'), edit_tag_save
);
586 add_menu_key (&keys
, KEY_ESCAPE
, edit_tag_abort
);
587 add_menu_key (&keys
, keycode_lookup (global_keys
, do_global_help
),
592 add_menu_key (&keys
, '\n', view_tag_value
);
594 add_menu_key (&keys
, KEY_ESCAPE
, view_tag_quit
);
595 add_menu_key (&keys
, keycode_lookup (global_keys
, do_global_help
),
599 construct_menu (0, 0, -1, -1,
600 (edit
) ? _("Editing Roster Tags") :
601 _("Viewing Roster Tags"), 0, get_tag_items
, keys
, data
,
602 tag_print
, NULL
, NULL
);