1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2013 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
34 #elif defined(HAVE_CURSES_H)
57 static void build_message_lines(const char *title
, const char *prompt
,
58 int force_trim
, const char *extra
, int *h
, int *w
, char ***str
,
59 const char *fmt
, va_list ap
)
62 char *line
, **lines
= NULL
;
63 int width
= 0, height
= 0;
69 vasprintf(&line
, fmt
, ap
);
71 line
= Malloc(LINE_MAX
);
72 vsnprintf(line
, LINE_MAX
, fmt
, ap
);
75 /* Get the longest line to dynamically adjust the message box width. */
76 for (i
= n
= pos
= 0; line
[i
]; i
++, n
++) {
77 if (line
[i
] == '\n') {
85 pos
= (n
> pos
) ? n
: pos
;
94 for (i
= n
= pos
= 0; line
[i
]; i
++, n
++, pos
++) {
102 while (line
[--i
] != ' ')
116 lines
= split_str(p
, "\n", &total
, &width
, force_trim
);
118 if (prompt
&& width
< strlen(prompt
))
119 width
= strlen(prompt
);
121 if (extra
&& width
< strlen(extra
))
122 width
= strlen(extra
);
124 if (title
&& width
< strlen(title
))
125 width
= strlen(title
);
135 height
+= 4; // 1 padding, 2 box, 1 prompt
136 width
+= 4; // 2 padding, 2 box
142 static int display_message(WIN
*win
)
144 struct message_s
*m
= win
->data
;
148 keypad(win
->w
, TRUE
);
149 window_draw_title(win
->w
, win
->title
, m
->w
, CP_MESSAGE_TITLE
,
152 for (i
= 0; m
->lines
[i
]; i
++)
153 mvwprintw(win
->w
, (win
->title
) ? 2 + i
: 1 + i
,
154 (m
->center
|| (!i
&& !m
->lines
[i
+1])) ?
155 CENTERX(m
->w
, m
->lines
[i
]) : 1, "%s", m
->lines
[i
]);
158 window_draw_prompt(win
->w
, (m
->prompt
) ? m
->h
- 3 : m
->h
- 2, m
->w
,
159 m
->extra
, CP_MESSAGE_PROMPT
);
162 window_draw_prompt(win
->w
, m
->h
- 2, m
->w
, m
->prompt
, CP_MESSAGE_PROMPT
);
164 if (m
->func
&& win
->c
== m
->c
) {
170 for (i
= 0; m
->lines
[i
]; i
++)
186 * The force_trim parameter will trim whitespace reguardless if there is more
187 * than one line or not (help text vs. tag viewing).
189 WIN
*construct_message(const char *title
, const char *prompt
, int center
,
190 int force_trim
, const char *extra_help
, message_func
*func
, void *arg
,
191 window_exit_func
*efunc
, int ckey
, int freedata
, const char *fmt
, ...)
195 struct message_s
*m
= NULL
;
200 build_message_lines(title
, prompt
, force_trim
, extra_help
, &h
, &w
, &lines
, fmt
, ap
);
203 m
= Calloc(1, sizeof(struct message_s
));
213 m
->prompt
= strdup(prompt
);
216 m
->extra
= strdup(extra_help
);
218 win
= window_create(title
, h
, w
, CALCPOSY(h
), CALCPOSX(w
), display_message
,
221 win
->freedata
= freedata
;
222 wbkgd(win
->w
, CP_MESSAGE_WINDOW
);