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 | */ |
---|
9 | void owl_viewwin_init_text(owl_viewwin *v, WINDOW *win, int winlines, int wincols, 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->winlines=winlines; |
---|
22 | v->wincols=wincols; |
---|
23 | v->curswin=win; |
---|
24 | v->onclose_hook = NULL; |
---|
25 | } |
---|
26 | |
---|
27 | void owl_viewwin_append_text(owl_viewwin *v, const 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 | */ |
---|
35 | void owl_viewwin_init_fmtext(owl_viewwin *v, WINDOW *win, int winlines, int wincols, 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 | v->winlines=winlines; |
---|
49 | v->wincols=wincols; |
---|
50 | v->curswin=win; |
---|
51 | } |
---|
52 | |
---|
53 | void owl_viewwin_set_curswin(owl_viewwin *v, WINDOW *w, int winlines, int wincols) |
---|
54 | { |
---|
55 | v->curswin=w; |
---|
56 | v->winlines=winlines; |
---|
57 | v->wincols=wincols; |
---|
58 | } |
---|
59 | |
---|
60 | void owl_viewwin_set_onclose_hook(owl_viewwin *v, void (*onclose_hook) (owl_viewwin *vwin, void *data), void *onclose_hook_data) { |
---|
61 | v->onclose_hook = onclose_hook; |
---|
62 | v->onclose_hook_data = onclose_hook_data; |
---|
63 | } |
---|
64 | |
---|
65 | /* regenerate text on the curses window. */ |
---|
66 | /* if update == 1 then do a doupdate() */ |
---|
67 | void owl_viewwin_redisplay(owl_viewwin *v, int update) |
---|
68 | { |
---|
69 | owl_fmtext fm1, fm2; |
---|
70 | |
---|
71 | werase(v->curswin); |
---|
72 | wmove(v->curswin, 0, 0); |
---|
73 | |
---|
74 | owl_fmtext_init_null(&fm1); |
---|
75 | owl_fmtext_init_null(&fm2); |
---|
76 | |
---|
77 | owl_fmtext_truncate_lines(&(v->fmtext), v->topline, v->winlines-BOTTOM_OFFSET, &fm1); |
---|
78 | owl_fmtext_truncate_cols(&fm1, v->rightshift, v->wincols-1+v->rightshift, &fm2); |
---|
79 | |
---|
80 | owl_fmtext_curs_waddstr_without_search(&fm2, v->curswin); |
---|
81 | |
---|
82 | /* print the message at the bottom */ |
---|
83 | wmove(v->curswin, v->winlines-1, 0); |
---|
84 | wattrset(v->curswin, A_REVERSE); |
---|
85 | if (v->textlines - v->topline > v->winlines-BOTTOM_OFFSET) { |
---|
86 | waddstr(v->curswin, "--More-- (Space to see more, 'q' to quit)"); |
---|
87 | } else { |
---|
88 | waddstr(v->curswin, "--End-- (Press 'q' to quit)"); |
---|
89 | } |
---|
90 | wattroff(v->curswin, A_REVERSE); |
---|
91 | update_panels(); |
---|
92 | |
---|
93 | if (update==1) { |
---|
94 | doupdate(); |
---|
95 | } |
---|
96 | |
---|
97 | owl_fmtext_cleanup(&fm1); |
---|
98 | owl_fmtext_cleanup(&fm2); |
---|
99 | } |
---|
100 | |
---|
101 | void owl_viewwin_pagedown(owl_viewwin *v) |
---|
102 | { |
---|
103 | v->topline+=v->winlines - BOTTOM_OFFSET; |
---|
104 | if ( (v->topline+v->winlines-BOTTOM_OFFSET) > v->textlines) { |
---|
105 | v->topline = v->textlines - v->winlines + BOTTOM_OFFSET; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | void owl_viewwin_linedown(owl_viewwin *v) |
---|
110 | { |
---|
111 | v->topline++; |
---|
112 | if ( (v->topline+v->winlines-BOTTOM_OFFSET) > v->textlines) { |
---|
113 | v->topline = v->textlines - v->winlines + BOTTOM_OFFSET; |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | void owl_viewwin_pageup(owl_viewwin *v) |
---|
118 | { |
---|
119 | v->topline-=v->winlines; |
---|
120 | if (v->topline<0) v->topline=0; |
---|
121 | } |
---|
122 | |
---|
123 | void owl_viewwin_lineup(owl_viewwin *v) |
---|
124 | { |
---|
125 | v->topline--; |
---|
126 | if (v->topline<0) v->topline=0; |
---|
127 | } |
---|
128 | |
---|
129 | void owl_viewwin_right(owl_viewwin *v, int n) |
---|
130 | { |
---|
131 | v->rightshift+=n; |
---|
132 | } |
---|
133 | |
---|
134 | void owl_viewwin_left(owl_viewwin *v, int n) |
---|
135 | { |
---|
136 | v->rightshift-=n; |
---|
137 | if (v->rightshift<0) v->rightshift=0; |
---|
138 | } |
---|
139 | |
---|
140 | void owl_viewwin_top(owl_viewwin *v) |
---|
141 | { |
---|
142 | v->topline=0; |
---|
143 | v->rightshift=0; |
---|
144 | } |
---|
145 | |
---|
146 | void owl_viewwin_bottom(owl_viewwin *v) |
---|
147 | { |
---|
148 | v->topline = v->textlines - v->winlines + BOTTOM_OFFSET; |
---|
149 | } |
---|
150 | |
---|
151 | void owl_viewwin_cleanup(owl_viewwin *v) |
---|
152 | { |
---|
153 | if (v->onclose_hook) { |
---|
154 | v->onclose_hook(v, v->onclose_hook_data); |
---|
155 | v->onclose_hook = NULL; |
---|
156 | v->onclose_hook_data = NULL; |
---|
157 | } |
---|
158 | owl_fmtext_cleanup(&(v->fmtext)); |
---|
159 | } |
---|