source: viewwin.c @ 5b68c05

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