Fix PGN saving with annotations.
[cboard.git] / src / message.c
blob887f88d510f472ee1c5532a083fdf6a9b10825df
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2002-2006 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
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <string.h>
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
28 #ifdef HAVE_NCURSES_H
29 #include <ncurses.h>
30 #endif
32 #ifdef HAVE_PANEL_H
33 #include <panel.h>
34 #endif
36 #ifdef HAVE_LIMITS_H
37 #include <limits.h>
38 #endif
40 #include "chess.h"
41 #include "conf.h"
42 #include "window.h"
43 #include "colors.h"
44 #include "misc.h"
45 #include "message.h"
47 #ifdef WITH_DMALLOC
48 #include <dmalloc.h>
49 #endif
51 int dump_message(const char *title, const char *prompt, int center,
52 const char *extra_help, void(*custom_func)(void*), void *arg, int ckey,
53 const char *format, ...)
55 WINDOW *win;
56 PANEL *panel;
57 char *line, **lines = NULL;
58 int width = 0, height;
59 int i, n, pos;
60 int total = 0;
61 char buf[LINE_MAX], *p;
62 char *tmp;
63 va_list ap;
65 va_start(ap, format);
67 #ifdef HAVE_VASPRINTF
68 vasprintf(&line, format, ap);
69 #else
70 line = Malloc(LINE_MAX);
71 vsnprintf(line, LINE_MAX, format, ap);
72 #endif
74 va_end(ap);
76 /* Get the longest line to dynamically adjust the message box width. */
77 for (i = n = pos = 0; line[i]; i++, n++) {
78 if (line[i] == '\n') {
79 if (n > pos)
80 pos = n;
82 n = 0;
86 pos = (n > pos) ? n : pos;
88 if (pos) {
89 if (pos > MSG_WIDTH)
90 width = MSG_WIDTH;
91 else
92 width = pos;
95 for (i = n = pos = 0; line[i]; i++, n++, pos++) {
96 if (line[i] == '\t')
97 continue;
99 if (line[i] == '\n')
100 pos = 0;
102 if (pos > width) {
103 while (line[--i] != ' ')
104 buf[--n] = ' ';
106 buf[n++] = '\n';
107 pos = 0;
110 buf[n] = line[i];
113 free(line);
115 buf[n] = '\0';
116 p = buf;
118 while ((tmp = strsep(&p, "\n")) != NULL) {
119 tmp = trim(tmp);
121 if (!*tmp)
122 continue;
124 lines = Realloc(lines, (total + 2) * sizeof(char *));
125 lines[total++] = strdup(tmp);
128 lines[total] = NULL;
130 if (prompt && width < strlen(prompt))
131 width = strlen(prompt);
133 if (extra_help && width < strlen(extra_help))
134 width = strlen(extra_help);
136 if (title && width < strlen(title))
137 width = strlen(title);
139 width += 2;
140 height = total;
142 if (extra_help)
143 height++;
145 win = newwin((title) ? height + 5 : height + 4, width,
146 CALCPOSY(((title) ? height + 5 : height + 4)), CALCPOSX(width));
147 keypad(win, TRUE);
148 panel = new_panel(win);
149 wbkgd(win, CP_MESSAGE_WINDOW);
150 draw_window_title(win, title, width, CP_MESSAGE_TITLE, CP_MESSAGE_BORDER);
152 for (i = 0; lines[i]; i++)
153 mvwprintw(win, (title) ? 2 + i: 1 + i,
154 (center) ? CENTERX(width, lines[i]) : 1, "%s", lines[i]);
156 if (extra_help)
157 draw_prompt(win, (title) ? height + 2 : height + 1, width, extra_help,
158 CP_MESSAGE_PROMPT);
160 draw_prompt(win, (title) ? height + 3 : height + 2, width, prompt,
161 CP_MESSAGE_PROMPT);
163 while (1) {
164 update_panels();
165 doupdate();
167 n = wgetch(win);
169 if (!custom_func || n != ckey)
170 break;
172 custom_func(arg);
175 del_panel(panel);
176 delwin(win);
178 for (i = 0; i < total; i++)
179 free(lines[i]);
181 free(lines);
182 return n;