[dab82f29] | 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 |
---|
[fa00c5c] | 21 | * developers of the branched BarnOwl project, Copyright (c) |
---|
| 22 | * 2006-2009 The BarnOwl Developers. All rights reserved. |
---|
[dab82f29] | 23 | */ |
---|
| 24 | |
---|
[7d4fbcd] | 25 | #include <string.h> |
---|
| 26 | #include "owl.h" |
---|
| 27 | |
---|
[1aee7d9] | 28 | static const char fileIdent[] = "$Id$"; |
---|
| 29 | |
---|
[7d4fbcd] | 30 | #define BOTTOM_OFFSET 1 |
---|
| 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_text(owl_viewwin *v, WINDOW *win, int winlines, int wincols, char *text) |
---|
| 36 | { |
---|
[7d4fbcd] | 37 | owl_fmtext_init_null(&(v->fmtext)); |
---|
| 38 | if (text) { |
---|
| 39 | owl_fmtext_append_normal(&(v->fmtext), text); |
---|
[afbf668] | 40 | if (text[strlen(text)-1]!='\n' && text[0]!='\0') { |
---|
[7d4fbcd] | 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; |
---|
[afbf668] | 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)); |
---|
[7d4fbcd] | 56 | } |
---|
| 57 | |
---|
[b2b0773] | 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 | { |
---|
[7d4fbcd] | 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 | |
---|
[b2b0773] | 72 | void owl_viewwin_set_curswin(owl_viewwin *v, WINDOW *w, int winlines, int wincols) |
---|
| 73 | { |
---|
[7d4fbcd] | 74 | v->curswin=w; |
---|
| 75 | v->winlines=winlines; |
---|
| 76 | v->wincols=wincols; |
---|
| 77 | } |
---|
| 78 | |
---|
[afbf668] | 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 | |
---|
[b2b0773] | 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 | { |
---|
[7d4fbcd] | 88 | owl_fmtext fm1, fm2; |
---|
| 89 | |
---|
| 90 | werase(v->curswin); |
---|
| 91 | wmove(v->curswin, 0, 0); |
---|
| 92 | |
---|
[af2ca19] | 93 | owl_fmtext_init_null(&fm1); |
---|
| 94 | owl_fmtext_init_null(&fm2); |
---|
| 95 | |
---|
[7d4fbcd] | 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); |
---|
[3a2daac] | 104 | if (v->textlines - v->topline > v->winlines-BOTTOM_OFFSET) { |
---|
[7d4fbcd] | 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 | } |
---|
[af2ca19] | 115 | |
---|
| 116 | owl_fmtext_free(&fm1); |
---|
| 117 | owl_fmtext_free(&fm2); |
---|
[7d4fbcd] | 118 | } |
---|
| 119 | |
---|
[b2b0773] | 120 | void owl_viewwin_pagedown(owl_viewwin *v) |
---|
| 121 | { |
---|
[7d4fbcd] | 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 | |
---|
[b2b0773] | 128 | void owl_viewwin_linedown(owl_viewwin *v) |
---|
| 129 | { |
---|
[7d4fbcd] | 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 | |
---|
[b2b0773] | 136 | void owl_viewwin_pageup(owl_viewwin *v) |
---|
| 137 | { |
---|
[7d4fbcd] | 138 | v->topline-=v->winlines; |
---|
| 139 | if (v->topline<0) v->topline=0; |
---|
| 140 | } |
---|
| 141 | |
---|
[b2b0773] | 142 | void owl_viewwin_lineup(owl_viewwin *v) |
---|
| 143 | { |
---|
[7d4fbcd] | 144 | v->topline--; |
---|
| 145 | if (v->topline<0) v->topline=0; |
---|
| 146 | } |
---|
| 147 | |
---|
[b2b0773] | 148 | void owl_viewwin_right(owl_viewwin *v, int n) |
---|
| 149 | { |
---|
[7d4fbcd] | 150 | v->rightshift+=n; |
---|
| 151 | } |
---|
| 152 | |
---|
[b2b0773] | 153 | void owl_viewwin_left(owl_viewwin *v, int n) |
---|
| 154 | { |
---|
[7d4fbcd] | 155 | v->rightshift-=n; |
---|
| 156 | if (v->rightshift<0) v->rightshift=0; |
---|
| 157 | } |
---|
| 158 | |
---|
[b2b0773] | 159 | void owl_viewwin_top(owl_viewwin *v) |
---|
| 160 | { |
---|
[7d4fbcd] | 161 | v->topline=0; |
---|
| 162 | v->rightshift=0; |
---|
| 163 | } |
---|
| 164 | |
---|
[b2b0773] | 165 | void owl_viewwin_bottom(owl_viewwin *v) |
---|
| 166 | { |
---|
[7d4fbcd] | 167 | v->topline = v->textlines - v->winlines + BOTTOM_OFFSET; |
---|
| 168 | } |
---|
| 169 | |
---|
[b2b0773] | 170 | void owl_viewwin_free(owl_viewwin *v) |
---|
| 171 | { |
---|
[afbf668] | 172 | if (v->onclose_hook) { |
---|
| 173 | v->onclose_hook(v, v->onclose_hook_data); |
---|
[8721756] | 174 | v->onclose_hook = NULL; |
---|
| 175 | v->onclose_hook_data = NULL; |
---|
[afbf668] | 176 | } |
---|
[7d4fbcd] | 177 | owl_fmtext_free(&(v->fmtext)); |
---|
| 178 | } |
---|