1 | #include <string.h> |
---|
2 | #include "owl.h" |
---|
3 | |
---|
4 | #define BOTTOM_OFFSET 1 |
---|
5 | #define EMPTY_INDICATOR "~" |
---|
6 | |
---|
7 | static void owl_viewwin_redraw(owl_window *w, WINDOW *curswin, void *user_data); |
---|
8 | static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w); |
---|
9 | |
---|
10 | /* Create a viewwin. 'win' is an already initialized owl_window that |
---|
11 | * will be used by the viewwin |
---|
12 | */ |
---|
13 | owl_viewwin *owl_viewwin_new_text(owl_window *win, const char *text) |
---|
14 | { |
---|
15 | owl_viewwin *v = owl_malloc(sizeof(owl_viewwin)); |
---|
16 | memset(v, 0, sizeof(*v)); |
---|
17 | owl_fmtext_init_null(&(v->fmtext)); |
---|
18 | if (text) { |
---|
19 | owl_fmtext_append_normal(&(v->fmtext), text); |
---|
20 | if (text[0] != '\0' && text[strlen(text) - 1] != '\n') { |
---|
21 | owl_fmtext_append_normal(&(v->fmtext), "\n"); |
---|
22 | } |
---|
23 | v->textlines=owl_fmtext_num_lines(&(v->fmtext)); |
---|
24 | } |
---|
25 | v->topline=0; |
---|
26 | v->rightshift=0; |
---|
27 | v->onclose_hook = NULL; |
---|
28 | |
---|
29 | owl_viewwin_set_window(v, win); |
---|
30 | return v; |
---|
31 | } |
---|
32 | |
---|
33 | void owl_viewwin_append_text(owl_viewwin *v, const char *text) { |
---|
34 | owl_fmtext_append_normal(&(v->fmtext), text); |
---|
35 | v->textlines=owl_fmtext_num_lines(&(v->fmtext)); |
---|
36 | owl_viewwin_dirty(v); |
---|
37 | } |
---|
38 | |
---|
39 | /* Schedule a redraw of 'v'. Exported for hooking into the search |
---|
40 | string; when we have some way of listening for changes, this can be |
---|
41 | removed. */ |
---|
42 | void owl_viewwin_dirty(owl_viewwin *v) |
---|
43 | { |
---|
44 | if (v->window) |
---|
45 | owl_window_dirty(v->window); |
---|
46 | } |
---|
47 | |
---|
48 | /* Create a viewwin. 'win' is an already initialized owl_window that |
---|
49 | * will be used by the viewwin |
---|
50 | */ |
---|
51 | owl_viewwin *owl_viewwin_new_fmtext(owl_window *win, const owl_fmtext *fmtext) |
---|
52 | { |
---|
53 | char *text; |
---|
54 | owl_viewwin *v = owl_malloc(sizeof(owl_viewwin)); |
---|
55 | memset(v, 0, sizeof(*v)); |
---|
56 | |
---|
57 | owl_fmtext_copy(&(v->fmtext), fmtext); |
---|
58 | text = owl_fmtext_print_plain(fmtext); |
---|
59 | if (text[0] != '\0' && text[strlen(text) - 1] != '\n') { |
---|
60 | owl_fmtext_append_normal(&(v->fmtext), "\n"); |
---|
61 | } |
---|
62 | owl_free(text); |
---|
63 | v->textlines=owl_fmtext_num_lines(&(v->fmtext)); |
---|
64 | v->topline=0; |
---|
65 | v->rightshift=0; |
---|
66 | |
---|
67 | owl_viewwin_set_window(v, win); |
---|
68 | return v; |
---|
69 | } |
---|
70 | |
---|
71 | static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w) |
---|
72 | { |
---|
73 | v->window = w; |
---|
74 | if (w) { |
---|
75 | g_object_ref(v->window); |
---|
76 | v->sig_redraw_id = g_signal_connect(w, "redraw", G_CALLBACK(owl_viewwin_redraw), v); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | void owl_viewwin_set_onclose_hook(owl_viewwin *v, void (*onclose_hook) (owl_viewwin *vwin, void *data), void *onclose_hook_data) { |
---|
81 | v->onclose_hook = onclose_hook; |
---|
82 | v->onclose_hook_data = onclose_hook_data; |
---|
83 | } |
---|
84 | |
---|
85 | /* regenerate text on the curses window. */ |
---|
86 | static void owl_viewwin_redraw(owl_window *w, WINDOW *curswin, void *user_data) |
---|
87 | { |
---|
88 | owl_fmtext fm1, fm2; |
---|
89 | owl_viewwin *v = user_data; |
---|
90 | int winlines, wincols; |
---|
91 | int y; |
---|
92 | |
---|
93 | owl_window_get_position(w, &winlines, &wincols, 0, 0); |
---|
94 | |
---|
95 | werase(curswin); |
---|
96 | wmove(curswin, 0, 0); |
---|
97 | |
---|
98 | owl_fmtext_init_null(&fm1); |
---|
99 | owl_fmtext_init_null(&fm2); |
---|
100 | |
---|
101 | owl_fmtext_truncate_lines(&(v->fmtext), v->topline, winlines-BOTTOM_OFFSET, &fm1); |
---|
102 | owl_fmtext_truncate_cols(&fm1, v->rightshift, wincols-1+v->rightshift, &fm2); |
---|
103 | |
---|
104 | owl_fmtext_curs_waddstr(&fm2, curswin); |
---|
105 | |
---|
106 | /* Fill remaining lines with tildes. */ |
---|
107 | y = v->textlines - v->topline; |
---|
108 | wmove(curswin, y, 0); |
---|
109 | for (; y < winlines-BOTTOM_OFFSET; y++) { |
---|
110 | waddstr(curswin, EMPTY_INDICATOR); |
---|
111 | waddstr(curswin, "\n"); |
---|
112 | } |
---|
113 | |
---|
114 | /* print the message at the bottom */ |
---|
115 | wmove(curswin, winlines-1, 0); |
---|
116 | wattrset(curswin, A_REVERSE); |
---|
117 | if (v->textlines - v->topline > winlines-BOTTOM_OFFSET) { |
---|
118 | waddstr(curswin, "--More-- (Space to see more, 'q' to quit)"); |
---|
119 | } else { |
---|
120 | waddstr(curswin, "--End-- (Press 'q' to quit)"); |
---|
121 | } |
---|
122 | wattroff(curswin, A_REVERSE); |
---|
123 | |
---|
124 | owl_fmtext_cleanup(&fm1); |
---|
125 | owl_fmtext_cleanup(&fm2); |
---|
126 | } |
---|
127 | |
---|
128 | void owl_viewwin_down(owl_viewwin *v, int amount) { |
---|
129 | int winlines; |
---|
130 | owl_window_get_position(v->window, &winlines, 0, 0, 0); |
---|
131 | /* Don't scroll past the bottom. */ |
---|
132 | amount = MIN(amount, v->textlines - (v->topline + winlines - BOTTOM_OFFSET)); |
---|
133 | /* But if we're already past the bottom, don't back up either. */ |
---|
134 | if (amount > 0) { |
---|
135 | v->topline += amount; |
---|
136 | owl_viewwin_dirty(v); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | void owl_viewwin_up(owl_viewwin *v, int amount) |
---|
141 | { |
---|
142 | v->topline -= amount; |
---|
143 | if (v->topline<0) v->topline=0; |
---|
144 | owl_viewwin_dirty(v); |
---|
145 | } |
---|
146 | |
---|
147 | void owl_viewwin_pagedown(owl_viewwin *v) |
---|
148 | { |
---|
149 | int winlines; |
---|
150 | owl_window_get_position(v->window, &winlines, 0, 0, 0); |
---|
151 | owl_viewwin_down(v, winlines - BOTTOM_OFFSET); |
---|
152 | } |
---|
153 | |
---|
154 | void owl_viewwin_linedown(owl_viewwin *v) |
---|
155 | { |
---|
156 | owl_viewwin_down(v, 1); |
---|
157 | } |
---|
158 | |
---|
159 | void owl_viewwin_pageup(owl_viewwin *v) |
---|
160 | { |
---|
161 | int winlines; |
---|
162 | owl_window_get_position(v->window, &winlines, 0, 0, 0); |
---|
163 | owl_viewwin_up(v, winlines - BOTTOM_OFFSET); |
---|
164 | } |
---|
165 | |
---|
166 | void owl_viewwin_lineup(owl_viewwin *v) |
---|
167 | { |
---|
168 | owl_viewwin_up(v, 1); |
---|
169 | } |
---|
170 | |
---|
171 | void owl_viewwin_right(owl_viewwin *v, int n) |
---|
172 | { |
---|
173 | v->rightshift+=n; |
---|
174 | owl_viewwin_dirty(v); |
---|
175 | } |
---|
176 | |
---|
177 | void owl_viewwin_left(owl_viewwin *v, int n) |
---|
178 | { |
---|
179 | v->rightshift-=n; |
---|
180 | if (v->rightshift<0) v->rightshift=0; |
---|
181 | owl_viewwin_dirty(v); |
---|
182 | } |
---|
183 | |
---|
184 | void owl_viewwin_top(owl_viewwin *v) |
---|
185 | { |
---|
186 | v->topline=0; |
---|
187 | v->rightshift=0; |
---|
188 | owl_viewwin_dirty(v); |
---|
189 | } |
---|
190 | |
---|
191 | void owl_viewwin_bottom(owl_viewwin *v) |
---|
192 | { |
---|
193 | int winlines; |
---|
194 | owl_window_get_position(v->window, &winlines, 0, 0, 0); |
---|
195 | v->topline = v->textlines - winlines + BOTTOM_OFFSET; |
---|
196 | owl_viewwin_dirty(v); |
---|
197 | } |
---|
198 | |
---|
199 | void owl_viewwin_delete(owl_viewwin *v) |
---|
200 | { |
---|
201 | if (v->onclose_hook) { |
---|
202 | v->onclose_hook(v, v->onclose_hook_data); |
---|
203 | v->onclose_hook = NULL; |
---|
204 | v->onclose_hook_data = NULL; |
---|
205 | } |
---|
206 | if (v->window) { |
---|
207 | g_signal_handler_disconnect(v->window, v->sig_redraw_id); |
---|
208 | g_object_unref(v->window); |
---|
209 | v->window = NULL; |
---|
210 | } |
---|
211 | owl_fmtext_cleanup(&(v->fmtext)); |
---|
212 | owl_free(v); |
---|
213 | } |
---|