2 * Copyright (C) 2013 IBM Corporation
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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #if defined(HAVE_CONFIG_H)
24 #include <talloc/talloc.h>
25 #include <types/types.h>
27 #include <fold/fold.h>
28 #include <util/util.h>
31 #include "nc-textscreen.h"
33 struct text_screen *text_screen_from_scr(struct nc_scr *scr)
35 struct text_screen *text_screen;
36 assert(scr->sig == pb_text_screen_sig);
37 text_screen = container_of(scr, struct text_screen, scr);
41 void text_screen_draw(struct text_screen *screen)
45 max_y = getmaxy(screen->scr.sub_ncw);
47 max_y = min(max_y, screen->scroll_y + screen->n_lines);
49 for (i = screen->scroll_y; i < max_y; i++)
50 mvwaddstr(screen->scr.sub_ncw, i, 1, screen->lines[i]);
52 wrefresh(screen->scr.sub_ncw);
55 static void text_screen_scroll(struct text_screen *screen, int key)
57 int win_lines = getmaxy(screen->scr.sub_ncw);
62 else if (key == KEY_DOWN)
67 if (screen->scroll_y + delta < 0)
69 if (screen->scroll_y + delta + win_lines > screen->n_lines)
72 screen->scroll_y += delta;
73 wscrl(screen->scr.sub_ncw, delta);
76 mvwaddstr(screen->scr.sub_ncw, win_lines - 1, 1,
77 screen->lines[screen->scroll_y+win_lines-1]);
78 } else if (delta < 0) {
79 mvwaddstr(screen->scr.sub_ncw, 0, 1,
80 screen->lines[screen->scroll_y]);
83 wrefresh(screen->scr.sub_ncw);
86 void text_screen_clear(struct text_screen *screen)
88 talloc_free(screen->lines);
90 screen->n_alloc_lines = 16;
91 screen->lines = talloc_array(screen, const char *,
92 screen->n_alloc_lines);
95 static void __text_screen_append_line(struct text_screen *screen,
98 if (screen->n_lines == screen->n_alloc_lines) {
99 screen->n_alloc_lines *= 2;
100 screen->lines = talloc_realloc(screen, screen->lines,
102 screen->n_alloc_lines);
105 screen->lines[screen->n_lines] = line;
109 void text_screen_append_line(struct text_screen *screen, const char *fmt, ...)
116 line = talloc_vasprintf(screen->lines, fmt, ap);
122 __text_screen_append_line(screen, line);
125 static int text_screen_fold_cb(void *arg, const char *buf, int len)
127 struct text_screen *screen = arg;
129 buf = len ? talloc_strndup(screen->lines, buf, len) : "";
130 __text_screen_append_line(screen, buf);
135 void text_screen_set_text(struct text_screen *screen, const char *text)
137 fold_text(text, getmaxx(screen->scr.sub_ncw), text_screen_fold_cb,
141 void text_screen_process_key(struct nc_scr *scr, int key)
143 struct text_screen *screen = text_screen_from_scr(scr);
148 screen->on_exit(screen->cui);
152 text_screen_scroll(screen, key);
155 if (screen->help_text)
156 cui_show_help(screen->cui, screen->help_title,
164 static void text_screen_resize(struct nc_scr *scr)
166 struct text_screen *screen = text_screen_from_scr(scr);
167 text_screen_draw(screen);
170 struct nc_scr *text_screen_scr(struct text_screen *screen)
175 void text_screen_set_help(struct text_screen *screen, const char *title,
178 screen->help_title = title;
179 screen->help_text = text;
180 screen->scr.frame.help = "x=exit, h=help";
183 static int text_screen_post(struct nc_scr *scr)
185 nc_scr_frame_draw(scr);
186 redrawwin(scr->main_ncw);
187 wrefresh(scr->main_ncw);
191 void text_screen_init(struct text_screen *screen, struct cui *cui,
192 const char *title, void (*on_exit)(struct cui *))
194 nc_scr_init(&screen->scr, pb_text_screen_sig, 0,
195 cui, text_screen_process_key,
196 text_screen_post, NULL, text_screen_resize);
198 /* this will establish our array of lines */
199 screen->lines = NULL;
200 text_screen_clear(screen);
203 screen->on_exit = on_exit;
205 screen->scr.frame.ltitle = talloc_strdup(screen, title);
206 screen->scr.frame.rtitle = NULL;
207 screen->scr.frame.help = "x=exit";
208 scrollok(screen->scr.sub_ncw, true);