source: viewwin.c @ 7a6e6c7

release-1.7release-1.8release-1.9
Last change on this file since 7a6e6c7 was 7a6e6c7, checked in by David Benjamin <davidben@mit.edu>, 11 years ago
Use signals for redraw and resize
  • Property mode set to 100644
File size: 4.5 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) {
[7a6e6c7]55    g_signal_handler_disconnect(v->window, v->sig_redraw_id);
[68f63a2]56  }
57  v->window = w;
58  if (w) {
[7a6e6c7]59    v->sig_redraw_id = g_signal_connect(w, "redraw", G_CALLBACK(owl_viewwin_redisplay), v);
[68f63a2]60  }
[7d4fbcd]61}
62
[afbf668]63void owl_viewwin_set_onclose_hook(owl_viewwin *v, void (*onclose_hook) (owl_viewwin *vwin, void *data), void *onclose_hook_data) {
64  v->onclose_hook = onclose_hook;
65  v->onclose_hook_data = onclose_hook_data;
66}
67
[b2b0773]68/* regenerate text on the curses window. */
[68f63a2]69void owl_viewwin_redisplay(owl_window *w, WINDOW *curswin, void *user_data)
[b2b0773]70{
[7d4fbcd]71  owl_fmtext fm1, fm2;
[68f63a2]72  owl_viewwin *v = user_data;
[c447d9c]73  int winlines, wincols;
74
75  owl_window_get_position(w, &winlines, &wincols, 0, 0);
[7d4fbcd]76 
[68f63a2]77  werase(curswin);
78  wmove(curswin, 0, 0);
[7d4fbcd]79
[af2ca19]80  owl_fmtext_init_null(&fm1);
81  owl_fmtext_init_null(&fm2);
82 
[c447d9c]83  owl_fmtext_truncate_lines(&(v->fmtext), v->topline, winlines-BOTTOM_OFFSET, &fm1);
84  owl_fmtext_truncate_cols(&fm1, v->rightshift, wincols-1+v->rightshift, &fm2);
[7d4fbcd]85
[68f63a2]86  owl_fmtext_curs_waddstr_without_search(&fm2, curswin);
[7d4fbcd]87
88  /* print the message at the bottom */
[c447d9c]89  wmove(curswin, winlines-1, 0);
[68f63a2]90  wattrset(curswin, A_REVERSE);
[c447d9c]91  if (v->textlines - v->topline > winlines-BOTTOM_OFFSET) {
[68f63a2]92    waddstr(curswin, "--More-- (Space to see more, 'q' to quit)");
[7d4fbcd]93  } else {
[68f63a2]94    waddstr(curswin, "--End-- (Press 'q' to quit)");
[7d4fbcd]95  }
[68f63a2]96  wattroff(curswin, A_REVERSE);
[7d4fbcd]97
[7ab0020]98  owl_fmtext_cleanup(&fm1);
99  owl_fmtext_cleanup(&fm2);
[7d4fbcd]100}
101
[b2b0773]102void owl_viewwin_pagedown(owl_viewwin *v)
103{
[c447d9c]104  int winlines;
105  owl_window_get_position(v->window, &winlines, 0, 0, 0);
106  v->topline+=winlines - BOTTOM_OFFSET;
107  if ( (v->topline+winlines-BOTTOM_OFFSET) > v->textlines) {
108    v->topline = v->textlines - winlines + BOTTOM_OFFSET;
[7d4fbcd]109  }
[68f63a2]110  owl_window_dirty(v->window);
[7d4fbcd]111}
112
[b2b0773]113void owl_viewwin_linedown(owl_viewwin *v)
114{
[c447d9c]115  int winlines;
116  owl_window_get_position(v->window, &winlines, 0, 0, 0);
[7d4fbcd]117  v->topline++;
[c447d9c]118  if ( (v->topline+winlines-BOTTOM_OFFSET) > v->textlines) {
119    v->topline = v->textlines - winlines + BOTTOM_OFFSET;
[7d4fbcd]120  }
[68f63a2]121  owl_window_dirty(v->window);
[7d4fbcd]122}
123
[b2b0773]124void owl_viewwin_pageup(owl_viewwin *v)
125{
[c447d9c]126  int winlines;
127  owl_window_get_position(v->window, &winlines, 0, 0, 0);
128  v->topline-=winlines;
[7d4fbcd]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{
[c447d9c]162  int winlines;
163  owl_window_get_position(v->window, &winlines, 0, 0, 0);
164  v->topline = v->textlines - winlines + BOTTOM_OFFSET;
[68f63a2]165  owl_window_dirty(v->window);
[7d4fbcd]166}
167
[963b471]168void owl_viewwin_cleanup(owl_viewwin *v)
[b2b0773]169{
[5f7eadf]170  owl_viewwin_set_window(v, NULL);
[afbf668]171  if (v->onclose_hook) {
172    v->onclose_hook(v, v->onclose_hook_data);
[8721756]173    v->onclose_hook = NULL;
174    v->onclose_hook_data = NULL;
[afbf668]175  }
[7ab0020]176  owl_fmtext_cleanup(&(v->fmtext));
[7d4fbcd]177}
Note: See TracBrowser for help on using the repository browser.