1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2018 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
46 static struct country_codes
56 char line
[LINE_MAX
], *s
;
59 if ((fp
= fopen (config
.ccfile
, "r")) == NULL
)
61 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.ccfile
,
66 while ((s
= fgets (line
, sizeof (line
), fp
)) != NULL
)
70 if ((tmp
= strsep (&s
, " ")) == NULL
)
79 ccodes
= Realloc (ccodes
, (cindex
+ 2) * sizeof (struct country_codes
));
80 strncpy (ccodes
[cindex
].code
, tmp
, sizeof (ccodes
[cindex
].code
));
81 ccodes
[cindex
].code
[sizeof (ccodes
[cindex
].code
) - 1] = 0;
82 strncpy (ccodes
[cindex
].country
, s
, sizeof (ccodes
[cindex
].country
));
83 ccodes
[cindex
].country
[sizeof (ccodes
[cindex
].country
) - 1] = 0;
87 memset (&ccodes
[cindex
], '\0', sizeof (struct country_codes
));
92 static struct menu_item_s
**
93 get_cc_items (WIN
* win
)
96 struct menu_input_s
*m
= win
->data
;
97 struct menu_item_s
**items
= m
->items
;
101 for (i
= 0; items
[i
]; i
++)
105 for (i
= 0; ccodes
[i
].code
[0]; i
++)
107 items
= Realloc (items
, (i
+ 2) * sizeof (struct menu_item_s
*));
108 items
[i
] = Malloc (sizeof (struct menu_item_s
));
109 items
[i
]->name
= ccodes
[i
].code
;
110 items
[i
]->value
= ccodes
[i
].country
;
111 items
[i
]->selected
= 0;
122 do_cc_help (struct menu_input_s
*m
)
124 message (_("Country Code Keys"), ANY_KEY_STR
, "%s",
125 _(" UP/DOWN - previous/next menu item\n"
126 " HOME/END - first/last menu item\n"
127 " PGDN/PGUP - next/previous page\n"
128 " a-zA-Z0-9 - jump to item\n"
129 " ENTER - select item\n" " ESCAPE - cancel"));
133 do_cc_abort (struct menu_input_s
*m
)
139 do_cc_finalize (WIN
* win
)
141 struct input_s
*in
= win
->data
;
142 TAG
*t
= (TAG
*) in
->arg
;
144 set_field_buffer (in
->fields
[0], 0, t
->value
);
148 do_cc_save (struct menu_input_s
*m
)
150 struct input_s
*in
= m
->data
;
151 TAG
*t
= (TAG
*) in
->arg
;
152 int len
= strlen (m
->items
[m
->selected
]->name
);
154 t
->value
= Realloc (t
->value
, len
+ 1);
155 strcpy (t
->value
, m
->items
[m
->selected
]->name
);
162 struct menu_input_s
*m
= win
->data
;
164 mvwprintw (win
->w
, m
->print_line
, 1, "%s %-*s", m
->item
->name
,
165 win
->cols
- 6, m
->item
->value
);
169 country_codes (void *arg
)
171 struct menu_key_s
**keys
= NULL
;
175 if (init_country_codes ())
179 add_menu_key (&keys
, KEY_F (1), do_cc_help
);
180 add_menu_key (&keys
, KEY_ESCAPE
, do_cc_abort
);
181 add_menu_key (&keys
, '\n', do_cc_save
);
182 construct_menu (0, 0, -1, -1, _("Country Codes"), 0, get_cc_items
, keys
,
183 arg
, cc_print
, do_cc_finalize
, NULL
);
188 add_custom_tags (TAG
*** t
)
191 int total
= pgn_tag_total (config
.tag
);
196 for (i
= 0; i
< total
; i
++)
197 pgn_tag_add (t
, config
.tag
[i
]->name
, config
.tag
[i
]->value
);
202 static struct menu_item_s
**
203 get_tag_items (WIN
* win
)
206 struct menu_input_s
*m
= win
->data
;
207 struct menu_item_s
**items
= m
->items
;
208 TAG
**t
= (m
->data
) ? m
->data
: gp
->tag
;
212 for (i
= 0; items
[i
]; i
++)
216 n
= pgn_tag_total (t
);
218 for (i
= 0; i
< n
; i
++)
220 items
= Realloc (items
, (i
+ 2) * sizeof (struct menu_item_s
*));
221 items
[i
] = Malloc (sizeof (struct menu_item_s
));
222 items
[i
]->name
= t
[i
]->name
;
223 items
[i
]->value
= t
[i
]->value
;
224 items
[i
]->selected
= 0;
235 edit_tag_add_fen (struct menu_input_s
*m
)
238 struct userdata_s
*d
= gp
->data
;
239 char *fen
= pgn_game_to_fen (gp
, d
->b
);
241 pgn_tag_add (&t
, (char *) "FEN", fen
);
247 edit_tag_abort (struct menu_input_s
*m
)
254 update_status_notify (gp
, _("Tag edit aborted."));
258 edit_tag_add_finalize (WIN
* w
)
260 struct input_data_s
*in
= w
->data
;
261 char *name
= in
->moredata
;
262 char *value
= in
->str
;
263 struct menu_input_s
*m
= in
->data
;
271 if (!value
|| !*value
)
273 if (strcasecmp (name
, "Round") == 0)
274 value
= (char *) "-";
275 else if (strcasecmp (name
, "Result") == 0)
276 value
= (char *) "*";
277 else if (strcasecmp (name
, "Date") == 0)
280 tm
= localtime (&now
);
281 strftime (buf
, sizeof (buf
), PGN_TIME_FORMAT
, tm
);
285 value
= (char *) "?";
288 tmp
= trim_multi (value
);
289 count
= pgn_tag_total (t
);
290 pgn_tag_add (&t
, name
, tmp
);
294 if (count
!= pgn_tag_total (t
))
295 m
->selected
= m
->total
;
302 pushkey
= REFRESH_MENU
;
306 set_menu_stuff (TAG
** t
, char *name
, char **init
, int *type
,
307 int *lines
, input_func
** func
, wint_t * key
, char **eprompt
,
313 if ((n
= pgn_tag_find (t
, name
)) != -1)
319 if (strcasecmp (name
, "Date") == 0)
321 *type
= FIELD_TYPE_PGN_DATE
;
324 else if (strcasecmp (name
, "Site") == 0)
326 *func
= country_codes
;
327 *key
= CTRL_KEY ('t');
328 *eprompt
= _("Type CTRL-t for country codes");
331 else if (strcasecmp (name
, "Round") == 0)
333 *type
= FIELD_TYPE_PGN_ROUND
;
336 else if (strcasecmp (name
, "Result") == 0)
338 *type
= FIELD_TYPE_PGN_RESULT
;
344 edit_tag_value (struct menu_input_s
*m
)
347 struct input_data_s
*in
= Calloc (1, sizeof (struct input_data_s
));
352 input_func
*func
= NULL
;
354 char *eprompt
= NULL
;
356 int lines
= MAX_PGN_LINE_LEN
/ INPUT_WIDTH
;
360 name
= strdup (t
[m
->selected
]->name
);
362 in
->efunc
= edit_tag_add_finalize
;
363 wc
= str_to_wchar (name
);
364 snprintf (buf
, sizeof (buf
), "%s \"%ls\"", _("Editing Tag"), wc
);
366 set_menu_stuff (t
, name
, &init
, &type
, &lines
, &func
, &key
, &eprompt
, &arg
);
367 construct_input (buf
, init
, lines
, 0, eprompt
, func
, arg
, key
, in
, -1,
372 edit_tag_add_name_finalize (WIN
* w
)
374 struct input_data_s
*in
= w
->data
;
375 struct input_data_s
*inv
;
376 struct menu_input_s
*m
= in
->data
;
383 input_func
*func
= NULL
;
384 char *eprompt
= NULL
;
385 int lines
= MAX_PGN_LINE_LEN
/ INPUT_WIDTH
;
392 name
= strdup (in
->str
);
393 inv
= Calloc (1, sizeof (struct input_data_s
));
394 inv
->efunc
= edit_tag_add_finalize
;
395 inv
->data
= in
->data
;
396 inv
->moredata
= name
;
399 wc
= str_to_wchar (name
);
400 snprintf (buf
, sizeof (buf
), "%s \"%ls\"", _("Editing Tag"), wc
);
402 set_menu_stuff (t
, name
, &init
, &type
, &lines
, &func
, &key
, &eprompt
, &arg
);
403 construct_input (buf
, init
, lines
, 0, eprompt
, func
, arg
, key
, inv
, -1,
408 edit_tag_add (struct menu_input_s
*m
)
410 struct input_data_s
*in
= Calloc (1, sizeof (struct input_s
));
413 in
->efunc
= edit_tag_add_name_finalize
;
414 construct_input (_("New Tag Name"), NULL
, 1, 1, NULL
, NULL
, NULL
, 0, in
, -1,
415 NULL
, FIELD_TYPE_PGN_TAG_NAME
);
419 edit_tag_remove (struct menu_input_s
*m
)
423 int i
, n
= pgn_tag_total (t
);
427 cmessage (NULL
, ANY_KEY_STR
, "%s",
428 _("Cannot remove the Seven Tag Roster"));
432 for (i
= 0; i
< n
; i
++)
434 if (i
== m
->selected
)
437 pgn_tag_add (&data
, t
[i
]->name
, t
[i
]->value
);
446 edit_tag_add_custom (struct menu_input_s
*m
)
450 add_custom_tags (&t
);
455 edit_tag_save (struct menu_input_s
*m
)
458 struct userdata_s
*d
= gp
->data
;
463 pgn_tag_free (gp
->tag
);
467 SET_FLAG (d
->flags
, CF_MODIFIED
);
470 * In case of editing a FEN tag. Must not be MODE_PLAY. Also updates the
471 * games ply count for the fifty move draw rule.
473 if (d
->mode
!= MODE_PLAY
)
474 pgn_board_update (gp
, d
->b
, gp
->hindex
);
478 edit_tag_help (struct menu_input_s
*m
)
480 message (_("Tag Editing Keys"), ANY_KEY_STR
,
481 _(" UP/DOWN - previous/next menu item\n"
482 " HOME/END - first/last menu item\n"
483 " PGDN/PGUP - next/previous page\n"
484 " a-zA-Z0-9 - jump to item\n"
485 " ENTER - edit select item\n"
486 " CTRL-a - add an entry\n"
487 " CTRL-f - add FEN tag from current position\n"
488 " CTRL-r - remove selected entry\n"
489 " CTRL-t - add custom tags\n"
490 " CTRL-x - quit with changes\n"
491 " ESCAPE - quit without changes"));
495 view_tag_help (struct menu_input_s
*m
)
497 message (_("Tag Viewing Keys"), ANY_KEY_STR
,
498 _(" UP/DOWN - previous/next menu item\n"
499 " HOME/END - first/last menu item\n"
500 " PGDN/PGUP - next/previous page\n"
501 " a-zA-Z0-9 - jump to item\n"
502 " ENTER - view selected item\n" " ESCAPE - cancel"));
506 view_tag_quit (struct menu_input_s
*m
)
512 view_tag_value (struct menu_input_s
*m
)
514 struct menu_item_s
*item
= m
->items
[m
->selected
];
516 wchar_t *wc
= str_to_wchar (item
->name
);
518 snprintf (buf
, sizeof (buf
), "%s \"%ls\"", _("Viewing Tag"), wc
);
520 construct_message (buf
, ANY_KEY_STR
, 0, 1, NULL
,
521 NULL
, NULL
, NULL
, 0, 0, NULL
, "%s", item
->value
);
525 tag_print (WIN
* win
)
528 struct menu_input_s
*m
= win
->data
;
531 for (i
= 0; m
->items
[i
]; i
++)
533 wc
= translate_tag_name (m
->items
[i
]->name
);
540 wc
= translate_tag_name (m
->item
->name
);
541 mvwprintw (win
->w
, m
->print_line
, 1, "%ls", wc
);
543 for (n
= wcslen (wc
) + 1; n
<= len
; n
++)
544 mvwprintw (win
->w
, m
->print_line
, n
, "%c", '.');
547 mvwprintw (win
->w
, m
->print_line
, n
, ": ");
548 i
= win
->cols
- n
- 3;
549 wc
= str_etc (m
->item
->value
, i
, 0);
550 mvwprintw (win
->w
, m
->print_line
, n
+ 2, "%-*ls", i
, wc
);
555 wmove (stdscr
, 0, 0);
563 edit_tags (GAME g
, BOARD b
, int edit
)
565 struct menu_key_s
**keys
= NULL
;
571 for (i
= 0; gp
->tag
[i
]; i
++)
572 pgn_tag_add (&data
, gp
->tag
[i
]->name
, gp
->tag
[i
]->value
);
574 add_menu_key (&keys
, '\n', edit_tag_value
);
575 add_menu_key (&keys
, CTRL_KEY ('f'), edit_tag_add_fen
);
576 add_menu_key (&keys
, CTRL_KEY ('a'), edit_tag_add
);
577 add_menu_key (&keys
, CTRL_KEY ('r'), edit_tag_remove
);
578 add_menu_key (&keys
, CTRL_KEY ('t'), edit_tag_add_custom
);
579 add_menu_key (&keys
, CTRL_KEY ('x'), edit_tag_save
);
580 add_menu_key (&keys
, KEY_ESCAPE
, edit_tag_abort
);
581 add_menu_key (&keys
, KEY_F (1), edit_tag_help
);
585 add_menu_key (&keys
, '\n', view_tag_value
);
587 add_menu_key (&keys
, KEY_ESCAPE
, view_tag_quit
);
588 add_menu_key (&keys
, KEY_F (1), view_tag_help
);
591 construct_menu (0, 0, -1, -1,
592 (edit
) ? _("Editing Roster Tags") :
593 _("Viewing Roster Tags"), 0, get_tag_items
, keys
, data
,
594 tag_print
, NULL
, NULL
);