[7d4fbcd] | 1 | #include <string.h> |
---|
| 2 | #include "owl.h" |
---|
| 3 | |
---|
| 4 | #define BOTTOM_OFFSET 1 |
---|
| 5 | |
---|
[b2b0773] | 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, char *text) |
---|
| 10 | { |
---|
[7d4fbcd] | 11 | owl_fmtext_init_null(&(v->fmtext)); |
---|
| 12 | if (text) { |
---|
| 13 | owl_fmtext_append_normal(&(v->fmtext), text); |
---|
[afbf668] | 14 | if (text[strlen(text)-1]!='\n' && text[0]!='\0') { |
---|
[7d4fbcd] | 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; |
---|
[afbf668] | 24 | v->onclose_hook = NULL; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | void 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)); |
---|
[7d4fbcd] | 30 | } |
---|
| 31 | |
---|
[b2b0773] | 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, owl_fmtext *fmtext) |
---|
| 36 | { |
---|
[7d4fbcd] | 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 | |
---|
[b2b0773] | 46 | void owl_viewwin_set_curswin(owl_viewwin *v, WINDOW *w, int winlines, int wincols) |
---|
| 47 | { |
---|
[7d4fbcd] | 48 | v->curswin=w; |
---|
| 49 | v->winlines=winlines; |
---|
| 50 | v->wincols=wincols; |
---|
| 51 | } |
---|
| 52 | |
---|
[afbf668] | 53 | void 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 | |
---|
[b2b0773] | 58 | /* regenerate text on the curses window. */ |
---|
| 59 | /* if update == 1 then do a doupdate() */ |
---|
| 60 | void owl_viewwin_redisplay(owl_viewwin *v, int update) |
---|
| 61 | { |
---|
[7d4fbcd] | 62 | owl_fmtext fm1, fm2; |
---|
| 63 | |
---|
| 64 | werase(v->curswin); |
---|
| 65 | wmove(v->curswin, 0, 0); |
---|
| 66 | |
---|
[af2ca19] | 67 | owl_fmtext_init_null(&fm1); |
---|
| 68 | owl_fmtext_init_null(&fm2); |
---|
| 69 | |
---|
[7d4fbcd] | 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 | |
---|
[47519e1b] | 73 | owl_fmtext_curs_waddstr_without_search(&fm2, v->curswin); |
---|
[7d4fbcd] | 74 | |
---|
| 75 | /* print the message at the bottom */ |
---|
| 76 | wmove(v->curswin, v->winlines-1, 0); |
---|
| 77 | wattrset(v->curswin, A_REVERSE); |
---|
[3a2daac] | 78 | if (v->textlines - v->topline > v->winlines-BOTTOM_OFFSET) { |
---|
[7d4fbcd] | 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 | } |
---|
[af2ca19] | 89 | |
---|
| 90 | owl_fmtext_free(&fm1); |
---|
| 91 | owl_fmtext_free(&fm2); |
---|
[7d4fbcd] | 92 | } |
---|
| 93 | |
---|
[b2b0773] | 94 | void owl_viewwin_pagedown(owl_viewwin *v) |
---|
| 95 | { |
---|
[7d4fbcd] | 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 | |
---|
[b2b0773] | 102 | void owl_viewwin_linedown(owl_viewwin *v) |
---|
| 103 | { |
---|
[7d4fbcd] | 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 | |
---|
[b2b0773] | 110 | void owl_viewwin_pageup(owl_viewwin *v) |
---|
| 111 | { |
---|
[7d4fbcd] | 112 | v->topline-=v->winlines; |
---|
| 113 | if (v->topline<0) v->topline=0; |
---|
| 114 | } |
---|
| 115 | |
---|
[b2b0773] | 116 | void owl_viewwin_lineup(owl_viewwin *v) |
---|
| 117 | { |
---|
[7d4fbcd] | 118 | v->topline--; |
---|
| 119 | if (v->topline<0) v->topline=0; |
---|
| 120 | } |
---|
| 121 | |
---|
[b2b0773] | 122 | void owl_viewwin_right(owl_viewwin *v, int n) |
---|
| 123 | { |
---|
[7d4fbcd] | 124 | v->rightshift+=n; |
---|
| 125 | } |
---|
| 126 | |
---|
[b2b0773] | 127 | void owl_viewwin_left(owl_viewwin *v, int n) |
---|
| 128 | { |
---|
[7d4fbcd] | 129 | v->rightshift-=n; |
---|
| 130 | if (v->rightshift<0) v->rightshift=0; |
---|
| 131 | } |
---|
| 132 | |
---|
[b2b0773] | 133 | void owl_viewwin_top(owl_viewwin *v) |
---|
| 134 | { |
---|
[7d4fbcd] | 135 | v->topline=0; |
---|
| 136 | v->rightshift=0; |
---|
| 137 | } |
---|
| 138 | |
---|
[b2b0773] | 139 | void owl_viewwin_bottom(owl_viewwin *v) |
---|
| 140 | { |
---|
[7d4fbcd] | 141 | v->topline = v->textlines - v->winlines + BOTTOM_OFFSET; |
---|
| 142 | } |
---|
| 143 | |
---|
[b2b0773] | 144 | void owl_viewwin_free(owl_viewwin *v) |
---|
| 145 | { |
---|
[afbf668] | 146 | if (v->onclose_hook) { |
---|
| 147 | v->onclose_hook(v, v->onclose_hook_data); |
---|
[8721756] | 148 | v->onclose_hook = NULL; |
---|
| 149 | v->onclose_hook_data = NULL; |
---|
[afbf668] | 150 | } |
---|
[7d4fbcd] | 151 | owl_fmtext_free(&(v->fmtext)); |
---|
| 152 | } |
---|