1 | #include <string.h> |
---|
2 | #include "owl.h" |
---|
3 | |
---|
4 | #define BOTTOM_OFFSET 1 |
---|
5 | |
---|
6 | /* initialize the viewwin e. 'win' is an already initialzed curses |
---|
7 | * window that will be used by viewwin |
---|
8 | */ |
---|
9 | void owl_viewwin_init_text(owl_viewwin *v, owl_window *win, const char *text) |
---|
10 | { |
---|
11 | owl_fmtext_init_null(&(v->fmtext)); |
---|
12 | if (text) { |
---|
13 | owl_fmtext_append_normal(&(v->fmtext), text); |
---|
14 | if (text[0] != '\0' && text[strlen(text) - 1] != '\n') { |
---|
15 | owl_fmtext_append_normal(&(v->fmtext), "\n"); |
---|
16 | } |
---|
17 | v->textlines=owl_fmtext_num_lines(&(v->fmtext)); |
---|
18 | } |
---|
19 | v->topline=0; |
---|
20 | v->rightshift=0; |
---|
21 | v->onclose_hook = NULL; |
---|
22 | |
---|
23 | owl_viewwin_set_window(v, win); |
---|
24 | } |
---|
25 | |
---|
26 | void owl_viewwin_append_text(owl_viewwin *v, const char *text) { |
---|
27 | owl_fmtext_append_normal(&(v->fmtext), text); |
---|
28 | v->textlines=owl_fmtext_num_lines(&(v->fmtext)); |
---|
29 | owl_window_dirty(v->window); |
---|
30 | } |
---|
31 | |
---|
32 | /* initialize the viewwin e. 'win' is an already initialzed curses |
---|
33 | * window that will be used by viewwin |
---|
34 | */ |
---|
35 | void owl_viewwin_init_fmtext(owl_viewwin *v, owl_window *win, const owl_fmtext *fmtext) |
---|
36 | { |
---|
37 | char *text; |
---|
38 | |
---|
39 | owl_fmtext_copy(&(v->fmtext), fmtext); |
---|
40 | text = owl_fmtext_print_plain(fmtext); |
---|
41 | if (text[0] != '\0' && text[strlen(text) - 1] != '\n') { |
---|
42 | owl_fmtext_append_normal(&(v->fmtext), "\n"); |
---|
43 | } |
---|
44 | owl_free(text); |
---|
45 | v->textlines=owl_fmtext_num_lines(&(v->fmtext)); |
---|
46 | v->topline=0; |
---|
47 | v->rightshift=0; |
---|
48 | |
---|
49 | owl_viewwin_set_window(v, win); |
---|
50 | } |
---|
51 | |
---|
52 | void owl_viewwin_set_window(owl_viewwin *v, owl_window *w) |
---|
53 | { |
---|
54 | if (v->window) { |
---|
55 | owl_window_set_redraw_cb(v->window, 0, 0, 0); |
---|
56 | owl_window_set_resize_cb(v->window, 0, 0, 0); |
---|
57 | } |
---|
58 | v->window = w; |
---|
59 | if (w) { |
---|
60 | owl_window_get_position(w, &v->winlines, &v->wincols, 0, 0); |
---|
61 | owl_window_set_redraw_cb(w, owl_viewwin_redisplay, v, 0); |
---|
62 | owl_window_set_resize_cb(w, owl_viewwin_resize_hook, v, 0); |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | void owl_viewwin_set_onclose_hook(owl_viewwin *v, void (*onclose_hook) (owl_viewwin *vwin, void *data), void *onclose_hook_data) { |
---|
67 | v->onclose_hook = onclose_hook; |
---|
68 | v->onclose_hook_data = onclose_hook_data; |
---|
69 | } |
---|
70 | |
---|
71 | void owl_viewwin_resize_hook(owl_window *w, void *user_data) |
---|
72 | { |
---|
73 | owl_viewwin *v = user_data; |
---|
74 | owl_window_get_position(w, &v->winlines, &v->wincols, 0, 0); |
---|
75 | } |
---|
76 | |
---|
77 | /* regenerate text on the curses window. */ |
---|
78 | void owl_viewwin_redisplay(owl_window *w, WINDOW *curswin, void *user_data) |
---|
79 | { |
---|
80 | owl_fmtext fm1, fm2; |
---|
81 | owl_viewwin *v = user_data; |
---|
82 | |
---|
83 | werase(curswin); |
---|
84 | wmove(curswin, 0, 0); |
---|
85 | |
---|
86 | owl_fmtext_init_null(&fm1); |
---|
87 | owl_fmtext_init_null(&fm2); |
---|
88 | |
---|
89 | owl_fmtext_truncate_lines(&(v->fmtext), v->topline, v->winlines-BOTTOM_OFFSET, &fm1); |
---|
90 | owl_fmtext_truncate_cols(&fm1, v->rightshift, v->wincols-1+v->rightshift, &fm2); |
---|
91 | |
---|
92 | owl_fmtext_curs_waddstr_without_search(&fm2, curswin); |
---|
93 | |
---|
94 | /* print the message at the bottom */ |
---|
95 | wmove(curswin, v->winlines-1, 0); |
---|
96 | wattrset(curswin, A_REVERSE); |
---|
97 | if (v->textlines - v->topline > v->winlines-BOTTOM_OFFSET) { |
---|
98 | waddstr(curswin, "--More-- (Space to see more, 'q' to quit)"); |
---|
99 | } else { |
---|
100 | waddstr(curswin, "--End-- (Press 'q' to quit)"); |
---|
101 | } |
---|
102 | wattroff(curswin, A_REVERSE); |
---|
103 | |
---|
104 | owl_fmtext_cleanup(&fm1); |
---|
105 | owl_fmtext_cleanup(&fm2); |
---|
106 | } |
---|
107 | |
---|
108 | void owl_viewwin_pagedown(owl_viewwin *v) |
---|
109 | { |
---|
110 | v->topline+=v->winlines - BOTTOM_OFFSET; |
---|
111 | if ( (v->topline+v->winlines-BOTTOM_OFFSET) > v->textlines) { |
---|
112 | v->topline = v->textlines - v->winlines + BOTTOM_OFFSET; |
---|
113 | } |
---|
114 | owl_window_dirty(v->window); |
---|
115 | } |
---|
116 | |
---|
117 | void owl_viewwin_linedown(owl_viewwin *v) |
---|
118 | { |
---|
119 | v->topline++; |
---|
120 | if ( (v->topline+v->winlines-BOTTOM_OFFSET) > v->textlines) { |
---|
121 | v->topline = v->textlines - v->winlines + BOTTOM_OFFSET; |
---|
122 | } |
---|
123 | owl_window_dirty(v->window); |
---|
124 | } |
---|
125 | |
---|
126 | void owl_viewwin_pageup(owl_viewwin *v) |
---|
127 | { |
---|
128 | v->topline-=v->winlines; |
---|
129 | if (v->topline<0) v->topline=0; |
---|
130 | owl_window_dirty(v->window); |
---|
131 | } |
---|
132 | |
---|
133 | void owl_viewwin_lineup(owl_viewwin *v) |
---|
134 | { |
---|
135 | v->topline--; |
---|
136 | if (v->topline<0) v->topline=0; |
---|
137 | owl_window_dirty(v->window); |
---|
138 | } |
---|
139 | |
---|
140 | void owl_viewwin_right(owl_viewwin *v, int n) |
---|
141 | { |
---|
142 | v->rightshift+=n; |
---|
143 | owl_window_dirty(v->window); |
---|
144 | } |
---|
145 | |
---|
146 | void owl_viewwin_left(owl_viewwin *v, int n) |
---|
147 | { |
---|
148 | v->rightshift-=n; |
---|
149 | if (v->rightshift<0) v->rightshift=0; |
---|
150 | owl_window_dirty(v->window); |
---|
151 | } |
---|
152 | |
---|
153 | void owl_viewwin_top(owl_viewwin *v) |
---|
154 | { |
---|
155 | v->topline=0; |
---|
156 | v->rightshift=0; |
---|
157 | owl_window_dirty(v->window); |
---|
158 | } |
---|
159 | |
---|
160 | void owl_viewwin_bottom(owl_viewwin *v) |
---|
161 | { |
---|
162 | v->topline = v->textlines - v->winlines + BOTTOM_OFFSET; |
---|
163 | owl_window_dirty(v->window); |
---|
164 | } |
---|
165 | |
---|
166 | void owl_viewwin_cleanup(owl_viewwin *v) |
---|
167 | { |
---|
168 | if (v->onclose_hook) { |
---|
169 | v->onclose_hook(v, v->onclose_hook_data); |
---|
170 | v->onclose_hook = NULL; |
---|
171 | v->onclose_hook_data = NULL; |
---|
172 | } |
---|
173 | owl_fmtext_cleanup(&(v->fmtext)); |
---|
174 | } |
---|