source: viewwin.c @ 69873f7

release-1.7release-1.8release-1.9
Last change on this file since 69873f7 was 69873f7, checked in by David Benjamin <davidben@mit.edu>, 11 years ago
The viewwin should take references to its window
  • Property mode set to 100644
File size: 4.6 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    g_object_unref(v->window);
57  }
58  v->window = w;
59  if (w) {
60    g_object_ref(v->window);
61    v->sig_redraw_id = g_signal_connect(w, "redraw", G_CALLBACK(owl_viewwin_redisplay), v);
62  }
63}
64
65void owl_viewwin_set_onclose_hook(owl_viewwin *v, void (*onclose_hook) (owl_viewwin *vwin, void *data), void *onclose_hook_data) {
66  v->onclose_hook = onclose_hook;
67  v->onclose_hook_data = onclose_hook_data;
68}
69
70/* regenerate text on the curses window. */
71void owl_viewwin_redisplay(owl_window *w, WINDOW *curswin, void *user_data)
72{
73  owl_fmtext fm1, fm2;
74  owl_viewwin *v = user_data;
75  int winlines, wincols;
76
77  owl_window_get_position(w, &winlines, &wincols, 0, 0);
78 
79  werase(curswin);
80  wmove(curswin, 0, 0);
81
82  owl_fmtext_init_null(&fm1);
83  owl_fmtext_init_null(&fm2);
84 
85  owl_fmtext_truncate_lines(&(v->fmtext), v->topline, winlines-BOTTOM_OFFSET, &fm1);
86  owl_fmtext_truncate_cols(&fm1, v->rightshift, wincols-1+v->rightshift, &fm2);
87
88  owl_fmtext_curs_waddstr_without_search(&fm2, curswin);
89
90  /* print the message at the bottom */
91  wmove(curswin, winlines-1, 0);
92  wattrset(curswin, A_REVERSE);
93  if (v->textlines - v->topline > winlines-BOTTOM_OFFSET) {
94    waddstr(curswin, "--More-- (Space to see more, 'q' to quit)");
95  } else {
96    waddstr(curswin, "--End-- (Press 'q' to quit)");
97  }
98  wattroff(curswin, A_REVERSE);
99
100  owl_fmtext_cleanup(&fm1);
101  owl_fmtext_cleanup(&fm2);
102}
103
104void owl_viewwin_pagedown(owl_viewwin *v)
105{
106  int winlines;
107  owl_window_get_position(v->window, &winlines, 0, 0, 0);
108  v->topline+=winlines - BOTTOM_OFFSET;
109  if ( (v->topline+winlines-BOTTOM_OFFSET) > v->textlines) {
110    v->topline = v->textlines - winlines + BOTTOM_OFFSET;
111  }
112  owl_window_dirty(v->window);
113}
114
115void owl_viewwin_linedown(owl_viewwin *v)
116{
117  int winlines;
118  owl_window_get_position(v->window, &winlines, 0, 0, 0);
119  v->topline++;
120  if ( (v->topline+winlines-BOTTOM_OFFSET) > v->textlines) {
121    v->topline = v->textlines - winlines + BOTTOM_OFFSET;
122  }
123  owl_window_dirty(v->window);
124}
125
126void owl_viewwin_pageup(owl_viewwin *v)
127{
128  int winlines;
129  owl_window_get_position(v->window, &winlines, 0, 0, 0);
130  v->topline-=winlines;
131  if (v->topline<0) v->topline=0;
132  owl_window_dirty(v->window);
133}
134
135void owl_viewwin_lineup(owl_viewwin *v)
136{
137  v->topline--;
138  if (v->topline<0) v->topline=0;
139  owl_window_dirty(v->window);
140}
141
142void owl_viewwin_right(owl_viewwin *v, int n)
143{
144  v->rightshift+=n;
145  owl_window_dirty(v->window);
146}
147
148void owl_viewwin_left(owl_viewwin *v, int n)
149{
150  v->rightshift-=n;
151  if (v->rightshift<0) v->rightshift=0;
152  owl_window_dirty(v->window);
153}
154
155void owl_viewwin_top(owl_viewwin *v)
156{
157  v->topline=0;
158  v->rightshift=0;
159  owl_window_dirty(v->window);
160}
161
162void owl_viewwin_bottom(owl_viewwin *v)
163{
164  int winlines;
165  owl_window_get_position(v->window, &winlines, 0, 0, 0);
166  v->topline = v->textlines - winlines + BOTTOM_OFFSET;
167  owl_window_dirty(v->window);
168}
169
170void owl_viewwin_cleanup(owl_viewwin *v)
171{
172  owl_viewwin_set_window(v, NULL);
173  if (v->onclose_hook) {
174    v->onclose_hook(v, v->onclose_hook_data);
175    v->onclose_hook = NULL;
176    v->onclose_hook_data = NULL;
177  }
178  owl_fmtext_cleanup(&(v->fmtext));
179}
Note: See TracBrowser for help on using the repository browser.