source: viewwin.c @ cb5a9f3

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