Initial import.
[cave.git] / src / message.c
blob653b890deebef5c9a2d66262ae4cd3169c2f8196
1 /* CAVE (Character Animation Viewer for Everyone)
2 Copyright (C) 2001-2002 Ben Kibbey <bjk@arbornet.org>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stdarg.h>
21 #include <string.h>
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #include "common.h"
29 int message(const char *title, const char *prompt, const char *format, ...)
31 va_list ap;
32 char *line, *s, *tmp, buf[LINE_MAX];
33 static WINDOW *win;
34 static PANEL *panel;
35 int x, y, c = 0, i = 0;
37 if (!format && !title && !prompt) {
38 del_panel(panel);
39 delwin(win);
40 return 0;
43 va_start(ap, format);
44 #ifdef HAVE_VASPRINTF
45 vasprintf(&line, format, ap);
46 #else
47 line = Malloc(LINE_MAX);
48 vsnprintf(line, LINE_MAX, format, ap);
49 #endif
50 va_end(ap);
52 y = CALCHEIGHT();
53 tmp = strdup(line);
54 strcpy(buf, line);
56 while ((s = strsep(&tmp, "\n")) != NULL) {
57 int n = strlen(s);
59 y++;
61 if (n > i) {
62 i = n;
63 strcpy(buf, s);
67 tmp = line;
69 if (prompt && strlen(prompt) > i)
70 x = CALCWIDTH(prompt);
71 else
72 x = CALCWIDTH(buf);
74 if (title && x < strlen(title))
75 x = CALCWIDTH(title);
77 if (!prompt)
78 y--;
80 if (!title)
81 y--;
83 cbreak();
84 noecho();
86 win = newwin(y, x, CALCPOSY(y), CALCPOSX(x));
87 box(win, ACS_VLINE, ACS_HLINE);
88 panel = new_panel(win);
90 if (title)
91 mvwprintw(win, 0, CENTERX(x, title), "%s", title);
93 i = 1;
95 while ((s = strsep(&tmp, "\n")) != NULL)
96 mvwaddstr(win, i++, CENTERX(x, s), s);
98 free(tmp);
99 free(line);
101 if (prompt)
102 mvwprintw(win, y - 2, CENTERX(x, prompt), "%s", prompt);
104 update_panels();
105 doupdate();
107 if (prompt) {
108 c = wgetch(win);
109 del_panel(panel);
110 delwin(win);
113 return c;