source: viewwin.c @ 8ee712e0

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