Removed RCS headers.
[cboard.git] / src / message.c
blobbfc913e8ce081eccb4d527cec2d620e135609092
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2002-2006 Ben Kibbey <bjk@arbornet.org>
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 #include "common.h"
29 #include "colors.h"
30 #include "message.h"
32 int dump_message(const char *title, const char *prompt, int center,
33 const char *extra_help, void(*custom_func)(void*), void *arg, int ckey,
34 const char *format, ...)
36 WINDOW *win;
37 PANEL *panel;
38 char *line, **lines = NULL;
39 int width = 0, height;
40 int i, n, pos;
41 int total = 0;
42 char buf[LINE_MAX], *p;
43 char *tmp;
44 va_list ap;
46 va_start(ap, format);
48 #ifdef HAVE_VASPRINTF
49 vasprintf(&line, format, ap);
50 #else
51 line = Malloc(LINE_MAX);
52 vsnprintf(line, LINE_MAX, format, ap);
53 #endif
55 va_end(ap);
57 /* Get the longest line to dynamically adjust the message box width. */
58 for (i = n = pos = 0; line[i]; i++, n++) {
59 if (line[i] == '\n') {
60 if (n > pos)
61 pos = n;
63 n = 0;
67 pos = (n > pos) ? n : pos;
69 if (pos) {
70 if (pos > MSG_WIDTH)
71 width = MSG_WIDTH;
72 else
73 width = pos;
76 for (i = n = pos = 0; line[i]; i++, n++, pos++) {
77 if (line[i] == '\t')
78 continue;
80 if (line[i] == '\n')
81 pos = 0;
83 if (pos > width) {
84 while (line[--i] != ' ')
85 buf[--n] = ' ';
87 buf[n++] = '\n';
88 pos = 0;
91 buf[n] = line[i];
94 free(line);
96 buf[n] = '\0';
97 p = buf;
99 while ((tmp = strsep(&p, "\n")) != NULL) {
100 tmp = trim(tmp);
102 if (!*tmp)
103 continue;
105 lines = Realloc(lines, (total + 2) * sizeof(char *));
106 lines[total++] = strdup(tmp);
109 lines[total] = NULL;
111 if (prompt && width < strlen(prompt))
112 width = strlen(prompt);
114 if (extra_help && width < strlen(extra_help))
115 width = strlen(extra_help);
117 if (title && width < strlen(title))
118 width = strlen(title);
120 width += 2;
121 height = total;
123 if (extra_help)
124 height++;
126 win = newwin((title) ? height + 5 : height + 4, width,
127 CALCPOSY(((title) ? height + 5 : height + 4)), CALCPOSX(width));
128 panel = new_panel(win);
129 wbkgd(win, CP_MESSAGE_WINDOW);
130 draw_window_title(win, title, width, CP_MESSAGE_TITLE, CP_MESSAGE_BORDER);
132 for (i = 0; lines[i]; i++)
133 mvwprintw(win, (title) ? 2 + i: 1 + i,
134 (center) ? CENTERX(width, lines[i]) : 1, "%s", lines[i]);
136 if (extra_help)
137 draw_prompt(win, (title) ? height + 2 : height + 1, width, extra_help,
138 CP_MESSAGE_PROMPT);
140 draw_prompt(win, (title) ? height + 3 : height + 2, width, prompt,
141 CP_MESSAGE_PROMPT);
143 while (1) {
144 update_panels();
145 doupdate();
147 n = wgetch(win);
149 if (!custom_func || n != ckey)
150 break;
152 custom_func(arg);
155 del_panel(panel);
156 delwin(win);
158 for (i = 0; i < total; i++)
159 free(lines[i]);
161 free(lines);
162 return n;