source: viewwin.c @ fa4562c

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since fa4562c was d43edd2, checked in by Anders Kaseorg <andersk@mit.edu>, 15 years ago
Death to RCS keywords. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 3.7 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, WINDOW *win, int winlines, int wincols, char *text)
10{
11  owl_fmtext_init_null(&(v->fmtext));
12  if (text) {
13    owl_fmtext_append_normal(&(v->fmtext), text);
14    if (text[strlen(text)-1]!='\n' && text[0]!='\0') {
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->winlines=winlines;
22  v->wincols=wincols;
23  v->curswin=win;
24  v->onclose_hook = NULL;
25}
26
27void owl_viewwin_append_text(owl_viewwin *v, char *text) {
28    owl_fmtext_append_normal(&(v->fmtext), text);
29    v->textlines=owl_fmtext_num_lines(&(v->fmtext)); 
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, WINDOW *win, int winlines, int wincols, owl_fmtext *fmtext)
36{
37  owl_fmtext_copy(&(v->fmtext), fmtext);
38  v->textlines=owl_fmtext_num_lines(&(v->fmtext));
39  v->topline=0;
40  v->rightshift=0;
41  v->winlines=winlines;
42  v->wincols=wincols;
43  v->curswin=win;
44}
45
46void owl_viewwin_set_curswin(owl_viewwin *v, WINDOW *w, int winlines, int wincols)
47{
48  v->curswin=w;
49  v->winlines=winlines;
50  v->wincols=wincols;
51}
52
53void owl_viewwin_set_onclose_hook(owl_viewwin *v, void (*onclose_hook) (owl_viewwin *vwin, void *data), void *onclose_hook_data) {
54  v->onclose_hook = onclose_hook;
55  v->onclose_hook_data = onclose_hook_data;
56}
57
58/* regenerate text on the curses window. */
59/* if update == 1 then do a doupdate() */
60void owl_viewwin_redisplay(owl_viewwin *v, int update)
61{
62  owl_fmtext fm1, fm2;
63 
64  werase(v->curswin);
65  wmove(v->curswin, 0, 0);
66
67  owl_fmtext_init_null(&fm1);
68  owl_fmtext_init_null(&fm2);
69 
70  owl_fmtext_truncate_lines(&(v->fmtext), v->topline, v->winlines-BOTTOM_OFFSET, &fm1);
71  owl_fmtext_truncate_cols(&fm1, v->rightshift, v->wincols-1+v->rightshift, &fm2);
72
73  owl_fmtext_curs_waddstr_without_search(&fm2, v->curswin);
74
75  /* print the message at the bottom */
76  wmove(v->curswin, v->winlines-1, 0);
77  wattrset(v->curswin, A_REVERSE);
78  if (v->textlines - v->topline > v->winlines-BOTTOM_OFFSET) {
79    waddstr(v->curswin, "--More-- (Space to see more, 'q' to quit)");
80  } else {
81    waddstr(v->curswin, "--End-- (Press 'q' to quit)");
82  }
83  wattroff(v->curswin, A_REVERSE);
84  wnoutrefresh(v->curswin);
85
86  if (update==1) {
87    doupdate();
88  }
89
90  owl_fmtext_free(&fm1);
91  owl_fmtext_free(&fm2);
92}
93
94void owl_viewwin_pagedown(owl_viewwin *v)
95{
96  v->topline+=v->winlines - BOTTOM_OFFSET;
97  if ( (v->topline+v->winlines-BOTTOM_OFFSET) > v->textlines) {
98    v->topline = v->textlines - v->winlines + BOTTOM_OFFSET;
99  }
100}
101
102void owl_viewwin_linedown(owl_viewwin *v)
103{
104  v->topline++;
105  if ( (v->topline+v->winlines-BOTTOM_OFFSET) > v->textlines) {
106    v->topline = v->textlines - v->winlines + BOTTOM_OFFSET;
107  }
108}
109
110void owl_viewwin_pageup(owl_viewwin *v)
111{
112  v->topline-=v->winlines;
113  if (v->topline<0) v->topline=0;
114}
115
116void owl_viewwin_lineup(owl_viewwin *v)
117{
118  v->topline--;
119  if (v->topline<0) v->topline=0;
120}
121
122void owl_viewwin_right(owl_viewwin *v, int n)
123{
124  v->rightshift+=n;
125}
126
127void owl_viewwin_left(owl_viewwin *v, int n)
128{
129  v->rightshift-=n;
130  if (v->rightshift<0) v->rightshift=0;
131}
132
133void owl_viewwin_top(owl_viewwin *v)
134{
135  v->topline=0;
136  v->rightshift=0;
137}
138
139void owl_viewwin_bottom(owl_viewwin *v)
140{
141  v->topline = v->textlines - v->winlines + BOTTOM_OFFSET;
142}
143
144void owl_viewwin_free(owl_viewwin *v)
145{
146  if (v->onclose_hook) {
147    v->onclose_hook(v, v->onclose_hook_data);
148    v->onclose_hook = NULL;
149    v->onclose_hook_data = NULL;
150  }
151  owl_fmtext_free(&(v->fmtext));
152}
Note: See TracBrowser for help on using the repository browser.