source: viewwin.c @ 1383b58

release-1.7release-1.8release-1.9
Last change on this file since 1383b58 was 68f63a2, checked in by David Benjamin <davidben@mit.edu>, 11 years ago
Port owl_viewwin to owl_window A number of hacks. This is just a test run of the API.
  • Property mode set to 100644
File size: 4.4 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 */
[68f63a2]9void owl_viewwin_init_text(owl_viewwin *v, owl_window *win, 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;
[afbf668]21  v->onclose_hook = NULL;
[68f63a2]22
23  owl_viewwin_set_window(v, win);
[afbf668]24}
25
[e19eb97]26void owl_viewwin_append_text(owl_viewwin *v, const char *text) {
[afbf668]27    owl_fmtext_append_normal(&(v->fmtext), text);
28    v->textlines=owl_fmtext_num_lines(&(v->fmtext)); 
[68f63a2]29    owl_window_dirty(v->window);
[7d4fbcd]30}
31
[b2b0773]32/* initialize the viewwin e.  'win' is an already initialzed curses
33 * window that will be used by viewwin
34 */
[68f63a2]35void owl_viewwin_init_fmtext(owl_viewwin *v, owl_window *win, 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;
[68f63a2]48
49  owl_viewwin_set_window(v, win);
[7d4fbcd]50}
51
[68f63a2]52void owl_viewwin_set_window(owl_viewwin *v, owl_window *w)
[b2b0773]53{
[68f63a2]54  if (v->window) {
55    owl_window_set_redraw_cb(v->window, 0, 0, 0);
56    owl_window_set_resize_cb(v->window, 0, 0, 0);
57  }
58  v->window = w;
59  if (w) {
60    owl_window_get_position(w, &v->winlines, &v->wincols, 0, 0);
61    owl_window_set_redraw_cb(w, owl_viewwin_redisplay, v, 0);
62    owl_window_set_resize_cb(w, owl_viewwin_resize_hook, v, 0);
63  }
[7d4fbcd]64}
65
[afbf668]66void owl_viewwin_set_onclose_hook(owl_viewwin *v, void (*onclose_hook) (owl_viewwin *vwin, void *data), void *onclose_hook_data) {
67  v->onclose_hook = onclose_hook;
68  v->onclose_hook_data = onclose_hook_data;
69}
70
[68f63a2]71void owl_viewwin_resize_hook(owl_window *w, void *user_data)
72{
73  owl_viewwin *v = user_data;
74  owl_window_get_position(w, &v->winlines, &v->wincols, 0, 0);
75}
76
[b2b0773]77/* regenerate text on the curses window. */
[68f63a2]78void owl_viewwin_redisplay(owl_window *w, WINDOW *curswin, void *user_data)
[b2b0773]79{
[7d4fbcd]80  owl_fmtext fm1, fm2;
[68f63a2]81  owl_viewwin *v = user_data;
[7d4fbcd]82 
[68f63a2]83  werase(curswin);
84  wmove(curswin, 0, 0);
[7d4fbcd]85
[af2ca19]86  owl_fmtext_init_null(&fm1);
87  owl_fmtext_init_null(&fm2);
88 
[7d4fbcd]89  owl_fmtext_truncate_lines(&(v->fmtext), v->topline, v->winlines-BOTTOM_OFFSET, &fm1);
90  owl_fmtext_truncate_cols(&fm1, v->rightshift, v->wincols-1+v->rightshift, &fm2);
91
[68f63a2]92  owl_fmtext_curs_waddstr_without_search(&fm2, curswin);
[7d4fbcd]93
94  /* print the message at the bottom */
[68f63a2]95  wmove(curswin, v->winlines-1, 0);
96  wattrset(curswin, A_REVERSE);
[3a2daac]97  if (v->textlines - v->topline > v->winlines-BOTTOM_OFFSET) {
[68f63a2]98    waddstr(curswin, "--More-- (Space to see more, 'q' to quit)");
[7d4fbcd]99  } else {
[68f63a2]100    waddstr(curswin, "--End-- (Press 'q' to quit)");
[7d4fbcd]101  }
[68f63a2]102  wattroff(curswin, A_REVERSE);
[7d4fbcd]103
[7ab0020]104  owl_fmtext_cleanup(&fm1);
105  owl_fmtext_cleanup(&fm2);
[7d4fbcd]106}
107
[b2b0773]108void owl_viewwin_pagedown(owl_viewwin *v)
109{
[7d4fbcd]110  v->topline+=v->winlines - BOTTOM_OFFSET;
111  if ( (v->topline+v->winlines-BOTTOM_OFFSET) > v->textlines) {
112    v->topline = v->textlines - v->winlines + BOTTOM_OFFSET;
113  }
[68f63a2]114  owl_window_dirty(v->window);
[7d4fbcd]115}
116
[b2b0773]117void owl_viewwin_linedown(owl_viewwin *v)
118{
[7d4fbcd]119  v->topline++;
120  if ( (v->topline+v->winlines-BOTTOM_OFFSET) > v->textlines) {
121    v->topline = v->textlines - v->winlines + BOTTOM_OFFSET;
122  }
[68f63a2]123  owl_window_dirty(v->window);
[7d4fbcd]124}
125
[b2b0773]126void owl_viewwin_pageup(owl_viewwin *v)
127{
[7d4fbcd]128  v->topline-=v->winlines;
129  if (v->topline<0) v->topline=0;
[68f63a2]130  owl_window_dirty(v->window);
[7d4fbcd]131}
132
[b2b0773]133void owl_viewwin_lineup(owl_viewwin *v)
134{
[7d4fbcd]135  v->topline--;
136  if (v->topline<0) v->topline=0;
[68f63a2]137  owl_window_dirty(v->window);
[7d4fbcd]138}
139
[b2b0773]140void owl_viewwin_right(owl_viewwin *v, int n)
141{
[7d4fbcd]142  v->rightshift+=n;
[68f63a2]143  owl_window_dirty(v->window);
[7d4fbcd]144}
145
[b2b0773]146void owl_viewwin_left(owl_viewwin *v, int n)
147{
[7d4fbcd]148  v->rightshift-=n;
149  if (v->rightshift<0) v->rightshift=0;
[68f63a2]150  owl_window_dirty(v->window);
[7d4fbcd]151}
152
[b2b0773]153void owl_viewwin_top(owl_viewwin *v)
154{
[7d4fbcd]155  v->topline=0;
156  v->rightshift=0;
[68f63a2]157  owl_window_dirty(v->window);
[7d4fbcd]158}
159
[b2b0773]160void owl_viewwin_bottom(owl_viewwin *v)
161{
[7d4fbcd]162  v->topline = v->textlines - v->winlines + BOTTOM_OFFSET;
[68f63a2]163  owl_window_dirty(v->window);
[7d4fbcd]164}
165
[963b471]166void owl_viewwin_cleanup(owl_viewwin *v)
[b2b0773]167{
[afbf668]168  if (v->onclose_hook) {
169    v->onclose_hook(v, v->onclose_hook_data);
[8721756]170    v->onclose_hook = NULL;
171    v->onclose_hook_data = NULL;
[afbf668]172  }
[7ab0020]173  owl_fmtext_cleanup(&(v->fmtext));
[7d4fbcd]174}
Note: See TracBrowser for help on using the repository browser.