source: viewwin.c @ fc5eef4

release-1.10release-1.7release-1.8release-1.9
Last change on this file since fc5eef4 was 60c1386, checked in by David Benjamin <davidben@mit.edu>, 14 years ago
Split the viewwin's status bar into its own window
  • Property mode set to 100644
File size: 7.4 KB
Line 
1#include <string.h>
2#include "owl.h"
3
4#define BOTTOM_OFFSET 1
5#define EMPTY_INDICATOR "~"
6
7static void owl_viewwin_redraw_content(owl_window *w, WINDOW *curswin, void *user_data);
8static void owl_viewwin_redraw_status(owl_window *w, WINDOW *curswin, void *user_data);
9static void owl_viewwin_layout(owl_viewwin *v);
10static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w);
11
12/* Create a viewwin.  'win' is an already initialized owl_window that
13 * will be used by the viewwin
14 */
15owl_viewwin *owl_viewwin_new_text(owl_window *win, const char *text)
16{
17  owl_viewwin *v = owl_malloc(sizeof(owl_viewwin));
18  memset(v, 0, sizeof(*v));
19  owl_fmtext_init_null(&(v->fmtext));
20  if (text) {
21    owl_fmtext_append_normal(&(v->fmtext), text);
22    if (text[0] != '\0' && text[strlen(text) - 1] != '\n') {
23      owl_fmtext_append_normal(&(v->fmtext), "\n");
24    }
25    v->textlines=owl_fmtext_num_lines(&(v->fmtext));
26  }
27  v->topline=0;
28  v->rightshift=0;
29  v->onclose_hook = NULL;
30
31  owl_viewwin_set_window(v, win);
32  return v;
33}
34
35/* Create a viewwin.  'win' is an already initialized owl_window that
36 * will be used by the viewwin
37 */
38owl_viewwin *owl_viewwin_new_fmtext(owl_window *win, const owl_fmtext *fmtext)
39{
40  char *text;
41  owl_viewwin *v = owl_malloc(sizeof(owl_viewwin));
42  memset(v, 0, sizeof(*v));
43
44  owl_fmtext_copy(&(v->fmtext), fmtext);
45  text = owl_fmtext_print_plain(fmtext);
46  if (text[0] != '\0' && text[strlen(text) - 1] != '\n') {
47      owl_fmtext_append_normal(&(v->fmtext), "\n");
48  }
49  owl_free(text);
50  v->textlines=owl_fmtext_num_lines(&(v->fmtext));
51  v->topline=0;
52  v->rightshift=0;
53
54  owl_viewwin_set_window(v, win);
55  return v;
56}
57
58static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w)
59{
60  v->window = g_object_ref(w);
61  v->content = owl_window_new(v->window);
62  v->status = owl_window_new(v->window);
63
64  v->sig_content_redraw_id =
65    g_signal_connect(v->content, "redraw", G_CALLBACK(owl_viewwin_redraw_content), v);
66  v->sig_status_redraw_id =
67    g_signal_connect(v->status, "redraw", G_CALLBACK(owl_viewwin_redraw_status), v);
68  v->sig_resize_id =
69    g_signal_connect_swapped(v->window, "resized", G_CALLBACK(owl_viewwin_layout), v);
70  owl_viewwin_layout(v);
71
72  owl_window_show(v->content);
73  owl_window_show(v->status);
74}
75
76void owl_viewwin_set_onclose_hook(owl_viewwin *v, void (*onclose_hook) (owl_viewwin *vwin, void *data), void *onclose_hook_data) {
77  v->onclose_hook = onclose_hook;
78  v->onclose_hook_data = onclose_hook_data;
79}
80
81static void owl_viewwin_layout(owl_viewwin *v)
82{
83  int lines, cols;
84  owl_window_get_position(v->window, &lines, &cols, NULL, NULL);
85  owl_window_set_position(v->content, lines - BOTTOM_OFFSET, cols, 0, 0);
86  owl_window_set_position(v->status, BOTTOM_OFFSET, cols, lines - BOTTOM_OFFSET, 0);
87}
88
89/* regenerate text on the curses window. */
90static void owl_viewwin_redraw_content(owl_window *w, WINDOW *curswin, void *user_data)
91{
92  owl_fmtext fm1, fm2;
93  owl_viewwin *v = user_data;
94  int winlines, wincols;
95  int y;
96
97  owl_window_get_position(w, &winlines, &wincols, 0, 0);
98
99  werase(curswin);
100  wmove(curswin, 0, 0);
101
102  owl_fmtext_init_null(&fm1);
103  owl_fmtext_init_null(&fm2);
104
105  owl_fmtext_truncate_lines(&(v->fmtext), v->topline, winlines, &fm1);
106  owl_fmtext_truncate_cols(&fm1, v->rightshift, wincols-1+v->rightshift, &fm2);
107
108  owl_fmtext_curs_waddstr(&fm2, curswin);
109
110  /* Fill remaining lines with tildes. */
111  y = v->textlines - v->topline;
112  wmove(curswin, y, 0);
113  for (; y < winlines; y++) {
114    waddstr(curswin, EMPTY_INDICATOR);
115    waddstr(curswin, "\n");
116  }
117
118  owl_fmtext_cleanup(&fm1);
119  owl_fmtext_cleanup(&fm2);
120}
121
122static void owl_viewwin_redraw_status(owl_window *w, WINDOW *curswin, void *user_data)
123{
124  owl_viewwin *v = user_data;
125  int winlines, wincols;
126
127  owl_window_get_position(v->content, &winlines, &wincols, 0, 0);
128
129  werase(curswin);
130  wmove(curswin, 0, 0);
131  wattrset(curswin, A_REVERSE);
132  if (v->textlines - v->topline > winlines) {
133    waddstr(curswin, "--More-- (Space to see more, 'q' to quit)");
134  } else {
135    waddstr(curswin, "--End-- (Press 'q' to quit)");
136  }
137  wattroff(curswin, A_REVERSE);
138}
139
140void owl_viewwin_append_text(owl_viewwin *v, const char *text) {
141    owl_fmtext_append_normal(&(v->fmtext), text);
142    v->textlines=owl_fmtext_num_lines(&(v->fmtext));
143    owl_viewwin_dirty(v);
144}
145
146/* Schedule a redraw of 'v'. Exported for hooking into the search
147   string; when we have some way of listening for changes, this can be
148   removed. */
149void owl_viewwin_dirty(owl_viewwin *v)
150{
151  owl_window_dirty(v->content);
152  owl_window_dirty(v->status);
153}
154
155void owl_viewwin_down(owl_viewwin *v, int amount) {
156  int winlines;
157  owl_window_get_position(v->content, &winlines, 0, 0, 0);
158  /* Don't scroll past the bottom. */
159  amount = MIN(amount, v->textlines - (v->topline + winlines));
160  /* But if we're already past the bottom, don't back up either. */
161  if (amount > 0) {
162    v->topline += amount;
163    owl_viewwin_dirty(v);
164  }
165}
166
167void owl_viewwin_up(owl_viewwin *v, int amount)
168{
169  v->topline -= amount;
170  if (v->topline<0) v->topline=0;
171  owl_viewwin_dirty(v);
172}
173
174void owl_viewwin_pagedown(owl_viewwin *v)
175{
176  int winlines;
177  owl_window_get_position(v->content, &winlines, 0, 0, 0);
178  owl_viewwin_down(v, winlines);
179}
180
181void owl_viewwin_linedown(owl_viewwin *v)
182{
183  owl_viewwin_down(v, 1);
184}
185
186void owl_viewwin_pageup(owl_viewwin *v)
187{
188  int winlines;
189  owl_window_get_position(v->content, &winlines, 0, 0, 0);
190  owl_viewwin_up(v, winlines);
191}
192
193void owl_viewwin_lineup(owl_viewwin *v)
194{
195  owl_viewwin_up(v, 1);
196}
197
198void owl_viewwin_right(owl_viewwin *v, int n)
199{
200  v->rightshift+=n;
201  owl_viewwin_dirty(v);
202}
203
204void owl_viewwin_left(owl_viewwin *v, int n)
205{
206  v->rightshift-=n;
207  if (v->rightshift<0) v->rightshift=0;
208  owl_viewwin_dirty(v);
209}
210
211void owl_viewwin_top(owl_viewwin *v)
212{
213  v->topline=0;
214  v->rightshift=0;
215  owl_viewwin_dirty(v);
216}
217
218void owl_viewwin_bottom(owl_viewwin *v)
219{
220  int winlines;
221  owl_window_get_position(v->content, &winlines, 0, 0, 0);
222  v->topline = v->textlines - winlines;
223  owl_viewwin_dirty(v);
224}
225
226/* Scroll in 'direction' to the next line containing 're' in 'v',
227 * starting from the current line. Returns 0 if no occurrence is
228 * found.
229 *
230 * If mode==0 then stay on the current line if it matches.
231 */
232int owl_viewwin_search(owl_viewwin *v, const owl_regex *re, int mode, int direction)
233{
234  int start, end, offset;
235  owl_fmtext_line_extents(&v->fmtext, v->topline, &start, &end);
236  if (direction == OWL_DIRECTION_DOWNWARDS) {
237    offset = owl_fmtext_search(&v->fmtext, re, mode ? end : start);
238    if (offset < 0)
239      return 0;
240    v->topline = owl_fmtext_line_number(&v->fmtext, offset);
241    owl_viewwin_dirty(v);
242    return 1;
243  } else {
244    /* FIXME */
245    owl_function_error("Upwards searching is not implemented in viewwin.");
246    return 1;
247  }
248}
249
250void owl_viewwin_delete(owl_viewwin *v)
251{
252  if (v->onclose_hook) {
253    v->onclose_hook(v, v->onclose_hook_data);
254    v->onclose_hook = NULL;
255    v->onclose_hook_data = NULL;
256  }
257  /* TODO: This is far too tedious. owl_viewwin should own v->window
258   * and just unlink it in one go. Signals should also go away for
259   * free. */
260  g_signal_handler_disconnect(v->window, v->sig_resize_id);
261  g_signal_handler_disconnect(v->content, v->sig_content_redraw_id);
262  g_signal_handler_disconnect(v->status, v->sig_status_redraw_id);
263  owl_window_unlink(v->content);
264  owl_window_unlink(v->status);
265  g_object_unref(v->window);
266  g_object_unref(v->content);
267  g_object_unref(v->status);
268  owl_fmtext_cleanup(&(v->fmtext));
269  owl_free(v);
270}
Note: See TracBrowser for help on using the repository browser.