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