source: viewwin.c @ fe4c786

release-1.7release-1.8release-1.9
Last change on this file since fe4c786 was fe4c786, checked in by David Benjamin <davidben@mit.edu>, 11 years ago
Avoids segfault on NULL curswin for viewwin When the screen is too small, newwin() fails and gives back a NULL window. This avoids the crash.
  • Property mode set to 100644
File size: 3.9 KB
RevLine 
[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 */
[e19eb97]9void owl_viewwin_init_text(owl_viewwin *v, WINDOW *win, int winlines, int wincols, const char *text)
[b2b0773]10{
[7d4fbcd]11  owl_fmtext_init_null(&(v->fmtext));
12  if (text) {
13    owl_fmtext_append_normal(&(v->fmtext), text);
[4083c49]14    if (text[0] != '\0' && text[strlen(text) - 1] != '\n') {
[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
[e19eb97]27void owl_viewwin_append_text(owl_viewwin *v, const char *text) {
[afbf668]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 */
[075ba92]35void owl_viewwin_init_fmtext(owl_viewwin *v, WINDOW *win, int winlines, int wincols, const owl_fmtext *fmtext)
[b2b0773]36{
[dd6af02]37  char *text;
38
[7d4fbcd]39  owl_fmtext_copy(&(v->fmtext), fmtext);
[dd6af02]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  }
[3e55268]44  owl_free(text);
[7d4fbcd]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
[b2b0773]53void owl_viewwin_set_curswin(owl_viewwin *v, WINDOW *w, int winlines, int wincols)
54{
[7d4fbcd]55  v->curswin=w;
56  v->winlines=winlines;
57  v->wincols=wincols;
58}
59
[afbf668]60void 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
[b2b0773]65/* regenerate text on the curses window. */
[4cca591]66void owl_viewwin_redisplay(owl_viewwin *v)
[b2b0773]67{
[7d4fbcd]68  owl_fmtext fm1, fm2;
[fe4c786]69
70  /* avoid segfault when screen too small to create curswin */
71  if (v->curswin == NULL)
72    return;
[7d4fbcd]73 
74  werase(v->curswin);
75  wmove(v->curswin, 0, 0);
76
[af2ca19]77  owl_fmtext_init_null(&fm1);
78  owl_fmtext_init_null(&fm2);
79 
[7d4fbcd]80  owl_fmtext_truncate_lines(&(v->fmtext), v->topline, v->winlines-BOTTOM_OFFSET, &fm1);
81  owl_fmtext_truncate_cols(&fm1, v->rightshift, v->wincols-1+v->rightshift, &fm2);
82
[47519e1b]83  owl_fmtext_curs_waddstr_without_search(&fm2, v->curswin);
[7d4fbcd]84
85  /* print the message at the bottom */
86  wmove(v->curswin, v->winlines-1, 0);
87  wattrset(v->curswin, A_REVERSE);
[3a2daac]88  if (v->textlines - v->topline > v->winlines-BOTTOM_OFFSET) {
[7d4fbcd]89    waddstr(v->curswin, "--More-- (Space to see more, 'q' to quit)");
90  } else {
91    waddstr(v->curswin, "--End-- (Press 'q' to quit)");
92  }
93  wattroff(v->curswin, A_REVERSE);
94
[7ab0020]95  owl_fmtext_cleanup(&fm1);
96  owl_fmtext_cleanup(&fm2);
[7d4fbcd]97}
98
[b2b0773]99void owl_viewwin_pagedown(owl_viewwin *v)
100{
[7d4fbcd]101  v->topline+=v->winlines - BOTTOM_OFFSET;
102  if ( (v->topline+v->winlines-BOTTOM_OFFSET) > v->textlines) {
103    v->topline = v->textlines - v->winlines + BOTTOM_OFFSET;
104  }
105}
106
[b2b0773]107void owl_viewwin_linedown(owl_viewwin *v)
108{
[7d4fbcd]109  v->topline++;
110  if ( (v->topline+v->winlines-BOTTOM_OFFSET) > v->textlines) {
111    v->topline = v->textlines - v->winlines + BOTTOM_OFFSET;
112  }
113}
114
[b2b0773]115void owl_viewwin_pageup(owl_viewwin *v)
116{
[7d4fbcd]117  v->topline-=v->winlines;
118  if (v->topline<0) v->topline=0;
119}
120
[b2b0773]121void owl_viewwin_lineup(owl_viewwin *v)
122{
[7d4fbcd]123  v->topline--;
124  if (v->topline<0) v->topline=0;
125}
126
[b2b0773]127void owl_viewwin_right(owl_viewwin *v, int n)
128{
[7d4fbcd]129  v->rightshift+=n;
130}
131
[b2b0773]132void owl_viewwin_left(owl_viewwin *v, int n)
133{
[7d4fbcd]134  v->rightshift-=n;
135  if (v->rightshift<0) v->rightshift=0;
136}
137
[b2b0773]138void owl_viewwin_top(owl_viewwin *v)
139{
[7d4fbcd]140  v->topline=0;
141  v->rightshift=0;
142}
143
[b2b0773]144void owl_viewwin_bottom(owl_viewwin *v)
145{
[7d4fbcd]146  v->topline = v->textlines - v->winlines + BOTTOM_OFFSET;
147}
148
[963b471]149void owl_viewwin_cleanup(owl_viewwin *v)
[b2b0773]150{
[afbf668]151  if (v->onclose_hook) {
152    v->onclose_hook(v, v->onclose_hook_data);
[8721756]153    v->onclose_hook = NULL;
154    v->onclose_hook_data = NULL;
[afbf668]155  }
[7ab0020]156  owl_fmtext_cleanup(&(v->fmtext));
[7d4fbcd]157}
Note: See TracBrowser for help on using the repository browser.