1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2011 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
55 static void build_message_lines(const char *title
, const char *prompt
,
56 int force_trim
, const char *extra
, int *h
, int *w
, char ***str
,
57 const char *fmt
, va_list ap
)
60 char *line
, **lines
= NULL
;
61 int width
= 0, height
= 0;
67 vasprintf(&line
, fmt
, ap
);
69 line
= Malloc(LINE_MAX
);
70 vsnprintf(line
, LINE_MAX
, fmt
, ap
);
73 /* Get the longest line to dynamically adjust the message box width. */
74 for (i
= n
= pos
= 0; line
[i
]; i
++, n
++) {
75 if (line
[i
] == '\n') {
83 pos
= (n
> pos
) ? n
: pos
;
92 for (i
= n
= pos
= 0; line
[i
]; i
++, n
++, pos
++) {
100 while (line
[--i
] != ' ')
114 lines
= split_str(p
, "\n", &total
, &width
, force_trim
);
116 if (prompt
&& width
< strlen(prompt
))
117 width
= strlen(prompt
);
119 if (extra
&& width
< strlen(extra
))
120 width
= strlen(extra
);
122 if (title
&& width
< strlen(title
))
123 width
= strlen(title
);
133 height
+= 4; // 1 padding, 2 box, 1 prompt
134 width
+= 4; // 2 padding, 2 box
140 static int display_message(WIN
*win
)
142 struct message_s
*m
= win
->data
;
146 keypad(win
->w
, TRUE
);
147 window_draw_title(win
->w
, win
->title
, m
->w
, CP_MESSAGE_TITLE
,
150 for (i
= 0; m
->lines
[i
]; i
++)
151 mvwprintw(win
->w
, (win
->title
) ? 2 + i
: 1 + i
,
152 (m
->center
|| (!i
&& !m
->lines
[i
+1])) ?
153 CENTERX(m
->w
, m
->lines
[i
]) : 1, "%s", m
->lines
[i
]);
156 window_draw_prompt(win
->w
, (m
->prompt
) ? m
->h
- 3 : m
->h
- 2, m
->w
,
157 m
->extra
, CP_MESSAGE_PROMPT
);
160 window_draw_prompt(win
->w
, m
->h
- 2, m
->w
, m
->prompt
, CP_MESSAGE_PROMPT
);
162 if (m
->func
&& win
->c
== m
->c
) {
168 for (i
= 0; m
->lines
[i
]; i
++)
191 * The force_trim parameter will trim whitespace reguardless if there is more
192 * than one line or not (help text vs. tag viewing).
194 WIN
*construct_message(const char *title
, const char *prompt
, int center
,
195 int force_trim
, const char *extra_help
, message_func
*func
, void *arg
,
196 window_exit_func
*efunc
, int ckey
, int freedata
, const char *fmt
, ...)
200 struct message_s
*m
= NULL
;
205 build_message_lines(title
, prompt
, force_trim
, extra_help
, &h
, &w
, &lines
, fmt
, ap
);
208 m
= Calloc(1, sizeof(struct message_s
));
218 m
->prompt
= strdup(prompt
);
221 m
->extra
= strdup(extra_help
);
223 win
= window_create(title
, h
, w
, CALCPOSY(h
), CALCPOSX(w
), display_message
, m
,
226 win
->freedata
= freedata
;
227 wbkgd(win
->w
, CP_MESSAGE_WINDOW
);