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