1 | /* Copyright (c) 2002,2003,2004,2009 James M. Kretchmar |
---|
2 | * |
---|
3 | * This file is part of Owl. |
---|
4 | * |
---|
5 | * Owl is free software: you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU General Public License as published by |
---|
7 | * the Free Software Foundation, either version 3 of the License, or |
---|
8 | * (at your option) any later version. |
---|
9 | * |
---|
10 | * Owl is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public License |
---|
16 | * along with Owl. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | * |
---|
18 | * --------------------------------------------------------------- |
---|
19 | * |
---|
20 | * As of Owl version 2.1.12 there are patches contributed by |
---|
21 | * developers of the the branched BarnOwl project, Copyright (c) |
---|
22 | * 2006-2008 The BarnOwl Developers. All rights reserved. |
---|
23 | */ |
---|
24 | |
---|
25 | #include <string.h> |
---|
26 | #include "owl.h" |
---|
27 | |
---|
28 | static const char fileIdent[] = "$Id$"; |
---|
29 | |
---|
30 | #define BOTTOM_OFFSET 1 |
---|
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_text(owl_viewwin *v, WINDOW *win, int winlines, int wincols, char *text) |
---|
36 | { |
---|
37 | owl_fmtext_init_null(&(v->fmtext)); |
---|
38 | if (text) { |
---|
39 | owl_fmtext_append_normal(&(v->fmtext), text); |
---|
40 | if (text[strlen(text)-1]!='\n' && text[0]!='\0') { |
---|
41 | owl_fmtext_append_normal(&(v->fmtext), "\n"); |
---|
42 | } |
---|
43 | v->textlines=owl_fmtext_num_lines(&(v->fmtext)); |
---|
44 | } |
---|
45 | v->topline=0; |
---|
46 | v->rightshift=0; |
---|
47 | v->winlines=winlines; |
---|
48 | v->wincols=wincols; |
---|
49 | v->curswin=win; |
---|
50 | v->onclose_hook = NULL; |
---|
51 | } |
---|
52 | |
---|
53 | void owl_viewwin_append_text(owl_viewwin *v, char *text) { |
---|
54 | owl_fmtext_append_normal(&(v->fmtext), text); |
---|
55 | v->textlines=owl_fmtext_num_lines(&(v->fmtext)); |
---|
56 | } |
---|
57 | |
---|
58 | /* initialize the viewwin e. 'win' is an already initialzed curses |
---|
59 | * window that will be used by viewwin |
---|
60 | */ |
---|
61 | void owl_viewwin_init_fmtext(owl_viewwin *v, WINDOW *win, int winlines, int wincols, owl_fmtext *fmtext) |
---|
62 | { |
---|
63 | owl_fmtext_copy(&(v->fmtext), fmtext); |
---|
64 | v->textlines=owl_fmtext_num_lines(&(v->fmtext)); |
---|
65 | v->topline=0; |
---|
66 | v->rightshift=0; |
---|
67 | v->winlines=winlines; |
---|
68 | v->wincols=wincols; |
---|
69 | v->curswin=win; |
---|
70 | } |
---|
71 | |
---|
72 | void owl_viewwin_set_curswin(owl_viewwin *v, WINDOW *w, int winlines, int wincols) |
---|
73 | { |
---|
74 | v->curswin=w; |
---|
75 | v->winlines=winlines; |
---|
76 | v->wincols=wincols; |
---|
77 | } |
---|
78 | |
---|
79 | void owl_viewwin_set_onclose_hook(owl_viewwin *v, void (*onclose_hook) (owl_viewwin *vwin, void *data), void *onclose_hook_data) { |
---|
80 | v->onclose_hook = onclose_hook; |
---|
81 | v->onclose_hook_data = onclose_hook_data; |
---|
82 | } |
---|
83 | |
---|
84 | /* regenerate text on the curses window. */ |
---|
85 | /* if update == 1 then do a doupdate() */ |
---|
86 | void owl_viewwin_redisplay(owl_viewwin *v, int update) |
---|
87 | { |
---|
88 | owl_fmtext fm1, fm2; |
---|
89 | |
---|
90 | werase(v->curswin); |
---|
91 | wmove(v->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, v->winlines-BOTTOM_OFFSET, &fm1); |
---|
97 | owl_fmtext_truncate_cols(&fm1, v->rightshift, v->wincols-1+v->rightshift, &fm2); |
---|
98 | |
---|
99 | owl_fmtext_curs_waddstr(&fm2, v->curswin); |
---|
100 | |
---|
101 | /* print the message at the bottom */ |
---|
102 | wmove(v->curswin, v->winlines-1, 0); |
---|
103 | wattrset(v->curswin, A_REVERSE); |
---|
104 | if (v->textlines - v->topline > v->winlines-BOTTOM_OFFSET) { |
---|
105 | waddstr(v->curswin, "--More-- (Space to see more, 'q' to quit)"); |
---|
106 | } else { |
---|
107 | waddstr(v->curswin, "--End-- (Press 'q' to quit)"); |
---|
108 | } |
---|
109 | wattroff(v->curswin, A_REVERSE); |
---|
110 | wnoutrefresh(v->curswin); |
---|
111 | |
---|
112 | if (update==1) { |
---|
113 | doupdate(); |
---|
114 | } |
---|
115 | |
---|
116 | owl_fmtext_free(&fm1); |
---|
117 | owl_fmtext_free(&fm2); |
---|
118 | } |
---|
119 | |
---|
120 | void owl_viewwin_pagedown(owl_viewwin *v) |
---|
121 | { |
---|
122 | v->topline+=v->winlines - BOTTOM_OFFSET; |
---|
123 | if ( (v->topline+v->winlines-BOTTOM_OFFSET) > v->textlines) { |
---|
124 | v->topline = v->textlines - v->winlines + BOTTOM_OFFSET; |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | void owl_viewwin_linedown(owl_viewwin *v) |
---|
129 | { |
---|
130 | v->topline++; |
---|
131 | if ( (v->topline+v->winlines-BOTTOM_OFFSET) > v->textlines) { |
---|
132 | v->topline = v->textlines - v->winlines + BOTTOM_OFFSET; |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | void owl_viewwin_pageup(owl_viewwin *v) |
---|
137 | { |
---|
138 | v->topline-=v->winlines; |
---|
139 | if (v->topline<0) v->topline=0; |
---|
140 | } |
---|
141 | |
---|
142 | void owl_viewwin_lineup(owl_viewwin *v) |
---|
143 | { |
---|
144 | v->topline--; |
---|
145 | if (v->topline<0) v->topline=0; |
---|
146 | } |
---|
147 | |
---|
148 | void owl_viewwin_right(owl_viewwin *v, int n) |
---|
149 | { |
---|
150 | v->rightshift+=n; |
---|
151 | } |
---|
152 | |
---|
153 | void owl_viewwin_left(owl_viewwin *v, int n) |
---|
154 | { |
---|
155 | v->rightshift-=n; |
---|
156 | if (v->rightshift<0) v->rightshift=0; |
---|
157 | } |
---|
158 | |
---|
159 | void owl_viewwin_top(owl_viewwin *v) |
---|
160 | { |
---|
161 | v->topline=0; |
---|
162 | v->rightshift=0; |
---|
163 | } |
---|
164 | |
---|
165 | void owl_viewwin_bottom(owl_viewwin *v) |
---|
166 | { |
---|
167 | v->topline = v->textlines - v->winlines + BOTTOM_OFFSET; |
---|
168 | } |
---|
169 | |
---|
170 | void owl_viewwin_free(owl_viewwin *v) |
---|
171 | { |
---|
172 | if (v->onclose_hook) { |
---|
173 | v->onclose_hook(v, v->onclose_hook_data); |
---|
174 | v->onclose_hook = NULL; |
---|
175 | v->onclose_hook_data = NULL; |
---|
176 | } |
---|
177 | owl_fmtext_free(&(v->fmtext)); |
---|
178 | } |
---|