source: viewwin.c @ b8742ba

release-1.7release-1.8release-1.9
Last change on this file since b8742ba was b8742ba, checked in by David Benjamin <davidben@mit.edu>, 11 years ago
Add half an implementation of owl_viewwin_search
  • Property mode set to 100644
File size: 6.1 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(owl_window *w, WINDOW *curswin, void *user_data);
8static 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 */
13owl_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
33void 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. */
42void 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 */
51owl_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
71static 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
80void 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. */
86static 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
128void 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
140void 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
147void 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
154void owl_viewwin_linedown(owl_viewwin *v)
155{
156  owl_viewwin_down(v, 1);
157}
158
159void 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
166void owl_viewwin_lineup(owl_viewwin *v)
167{
168  owl_viewwin_up(v, 1);
169}
170
171void owl_viewwin_right(owl_viewwin *v, int n)
172{
173  v->rightshift+=n;
174  owl_viewwin_dirty(v);
175}
176
177void 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
184void owl_viewwin_top(owl_viewwin *v)
185{
186  v->topline=0;
187  v->rightshift=0;
188  owl_viewwin_dirty(v);
189}
190
191void 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/* Scroll in 'direction' to the next line containing 're' in 'v',
200 * starting from the current line. Returns 0 if no occurrence is
201 * found.
202 *
203 * If mode==0 then stay on the current line if it matches.
204 */
205int owl_viewwin_search(owl_viewwin *v, const owl_regex *re, int mode, int direction)
206{
207  int start, end, offset;
208  owl_fmtext_line_extents(&v->fmtext, v->topline, &start, &end);
209  if (direction == OWL_DIRECTION_DOWNWARDS) {
210    offset = owl_fmtext_search(&v->fmtext, re, mode ? end : start);
211    if (offset < 0)
212      return 0;
213    v->topline = owl_fmtext_line_number(&v->fmtext, offset);
214    owl_viewwin_dirty(v);
215    return 1;
216  } else {
217    /* FIXME */
218    owl_function_error("Upwards searching is not implemented in viewwin.");
219    return 1;
220  }
221}
222
223void owl_viewwin_delete(owl_viewwin *v)
224{
225  if (v->onclose_hook) {
226    v->onclose_hook(v, v->onclose_hook_data);
227    v->onclose_hook = NULL;
228    v->onclose_hook_data = NULL;
229  }
230  if (v->window) {
231    g_signal_handler_disconnect(v->window, v->sig_redraw_id);
232    g_object_unref(v->window);
233    v->window = NULL;
234  }
235  owl_fmtext_cleanup(&(v->fmtext));
236  owl_free(v);
237}
Note: See TracBrowser for help on using the repository browser.