source: viewwin.c @ fcce3f1

release-1.10release-1.7release-1.8release-1.9
Last change on this file since fcce3f1 was 0b9e607, checked in by David Benjamin <davidben@mit.edu>, 15 years ago
Rename redisplays to redraw Gives a target to grep for and matches the signal name.
  • Property mode set to 100644
File size: 4.6 KB
RevLine 
[7d4fbcd]1#include <string.h>
2#include "owl.h"
3
4#define BOTTOM_OFFSET 1
5
[0b9e607]6static void owl_viewwin_redraw(owl_window *w, WINDOW *curswin, void *user_data);
7
[b2b0773]8/* initialize the viewwin e.  'win' is an already initialzed curses
9 * window that will be used by viewwin
10 */
[68f63a2]11void owl_viewwin_init_text(owl_viewwin *v, owl_window *win, const char *text)
[b2b0773]12{
[7d4fbcd]13  owl_fmtext_init_null(&(v->fmtext));
14  if (text) {
15    owl_fmtext_append_normal(&(v->fmtext), text);
[4083c49]16    if (text[0] != '\0' && text[strlen(text) - 1] != '\n') {
[7d4fbcd]17      owl_fmtext_append_normal(&(v->fmtext), "\n");
18    }
19    v->textlines=owl_fmtext_num_lines(&(v->fmtext));
20  }
21  v->topline=0;
22  v->rightshift=0;
[afbf668]23  v->onclose_hook = NULL;
[68f63a2]24
25  owl_viewwin_set_window(v, win);
[afbf668]26}
27
[e19eb97]28void owl_viewwin_append_text(owl_viewwin *v, const char *text) {
[afbf668]29    owl_fmtext_append_normal(&(v->fmtext), text);
30    v->textlines=owl_fmtext_num_lines(&(v->fmtext)); 
[68f63a2]31    owl_window_dirty(v->window);
[7d4fbcd]32}
33
[b2b0773]34/* initialize the viewwin e.  'win' is an already initialzed curses
35 * window that will be used by viewwin
36 */
[68f63a2]37void owl_viewwin_init_fmtext(owl_viewwin *v, owl_window *win, const owl_fmtext *fmtext)
[b2b0773]38{
[dd6af02]39  char *text;
40
[7d4fbcd]41  owl_fmtext_copy(&(v->fmtext), fmtext);
[dd6af02]42  text = owl_fmtext_print_plain(fmtext);
43  if (text[0] != '\0' && text[strlen(text) - 1] != '\n') {
44      owl_fmtext_append_normal(&(v->fmtext), "\n");
45  }
[3e55268]46  owl_free(text);
[7d4fbcd]47  v->textlines=owl_fmtext_num_lines(&(v->fmtext));
48  v->topline=0;
49  v->rightshift=0;
[68f63a2]50
51  owl_viewwin_set_window(v, win);
[7d4fbcd]52}
53
[68f63a2]54void owl_viewwin_set_window(owl_viewwin *v, owl_window *w)
[b2b0773]55{
[68f63a2]56  if (v->window) {
[7a6e6c7]57    g_signal_handler_disconnect(v->window, v->sig_redraw_id);
[69873f7]58    g_object_unref(v->window);
[68f63a2]59  }
60  v->window = w;
61  if (w) {
[69873f7]62    g_object_ref(v->window);
[0b9e607]63    v->sig_redraw_id = g_signal_connect(w, "redraw", G_CALLBACK(owl_viewwin_redraw), v);
[68f63a2]64  }
[7d4fbcd]65}
66
[afbf668]67void owl_viewwin_set_onclose_hook(owl_viewwin *v, void (*onclose_hook) (owl_viewwin *vwin, void *data), void *onclose_hook_data) {
68  v->onclose_hook = onclose_hook;
69  v->onclose_hook_data = onclose_hook_data;
70}
71
[b2b0773]72/* regenerate text on the curses window. */
[0b9e607]73static void owl_viewwin_redraw(owl_window *w, WINDOW *curswin, void *user_data)
[b2b0773]74{
[7d4fbcd]75  owl_fmtext fm1, fm2;
[68f63a2]76  owl_viewwin *v = user_data;
[c447d9c]77  int winlines, wincols;
78
79  owl_window_get_position(w, &winlines, &wincols, 0, 0);
[7d4fbcd]80 
[68f63a2]81  werase(curswin);
82  wmove(curswin, 0, 0);
[7d4fbcd]83
[af2ca19]84  owl_fmtext_init_null(&fm1);
85  owl_fmtext_init_null(&fm2);
86 
[c447d9c]87  owl_fmtext_truncate_lines(&(v->fmtext), v->topline, winlines-BOTTOM_OFFSET, &fm1);
88  owl_fmtext_truncate_cols(&fm1, v->rightshift, wincols-1+v->rightshift, &fm2);
[7d4fbcd]89
[68f63a2]90  owl_fmtext_curs_waddstr_without_search(&fm2, curswin);
[7d4fbcd]91
92  /* print the message at the bottom */
[c447d9c]93  wmove(curswin, winlines-1, 0);
[68f63a2]94  wattrset(curswin, A_REVERSE);
[c447d9c]95  if (v->textlines - v->topline > winlines-BOTTOM_OFFSET) {
[68f63a2]96    waddstr(curswin, "--More-- (Space to see more, 'q' to quit)");
[7d4fbcd]97  } else {
[68f63a2]98    waddstr(curswin, "--End-- (Press 'q' to quit)");
[7d4fbcd]99  }
[68f63a2]100  wattroff(curswin, A_REVERSE);
[7d4fbcd]101
[7ab0020]102  owl_fmtext_cleanup(&fm1);
103  owl_fmtext_cleanup(&fm2);
[7d4fbcd]104}
105
[b2b0773]106void owl_viewwin_pagedown(owl_viewwin *v)
107{
[c447d9c]108  int winlines;
109  owl_window_get_position(v->window, &winlines, 0, 0, 0);
110  v->topline+=winlines - BOTTOM_OFFSET;
111  if ( (v->topline+winlines-BOTTOM_OFFSET) > v->textlines) {
112    v->topline = v->textlines - winlines + BOTTOM_OFFSET;
[7d4fbcd]113  }
[68f63a2]114  owl_window_dirty(v->window);
[7d4fbcd]115}
116
[b2b0773]117void owl_viewwin_linedown(owl_viewwin *v)
118{
[c447d9c]119  int winlines;
120  owl_window_get_position(v->window, &winlines, 0, 0, 0);
[7d4fbcd]121  v->topline++;
[c447d9c]122  if ( (v->topline+winlines-BOTTOM_OFFSET) > v->textlines) {
123    v->topline = v->textlines - winlines + BOTTOM_OFFSET;
[7d4fbcd]124  }
[68f63a2]125  owl_window_dirty(v->window);
[7d4fbcd]126}
127
[b2b0773]128void owl_viewwin_pageup(owl_viewwin *v)
129{
[c447d9c]130  int winlines;
131  owl_window_get_position(v->window, &winlines, 0, 0, 0);
132  v->topline-=winlines;
[7d4fbcd]133  if (v->topline<0) v->topline=0;
[68f63a2]134  owl_window_dirty(v->window);
[7d4fbcd]135}
136
[b2b0773]137void owl_viewwin_lineup(owl_viewwin *v)
138{
[7d4fbcd]139  v->topline--;
140  if (v->topline<0) v->topline=0;
[68f63a2]141  owl_window_dirty(v->window);
[7d4fbcd]142}
143
[b2b0773]144void owl_viewwin_right(owl_viewwin *v, int n)
145{
[7d4fbcd]146  v->rightshift+=n;
[68f63a2]147  owl_window_dirty(v->window);
[7d4fbcd]148}
149
[b2b0773]150void owl_viewwin_left(owl_viewwin *v, int n)
151{
[7d4fbcd]152  v->rightshift-=n;
153  if (v->rightshift<0) v->rightshift=0;
[68f63a2]154  owl_window_dirty(v->window);
[7d4fbcd]155}
156
[b2b0773]157void owl_viewwin_top(owl_viewwin *v)
158{
[7d4fbcd]159  v->topline=0;
160  v->rightshift=0;
[68f63a2]161  owl_window_dirty(v->window);
[7d4fbcd]162}
163
[b2b0773]164void owl_viewwin_bottom(owl_viewwin *v)
165{
[c447d9c]166  int winlines;
167  owl_window_get_position(v->window, &winlines, 0, 0, 0);
168  v->topline = v->textlines - winlines + BOTTOM_OFFSET;
[68f63a2]169  owl_window_dirty(v->window);
[7d4fbcd]170}
171
[963b471]172void owl_viewwin_cleanup(owl_viewwin *v)
[b2b0773]173{
[5f7eadf]174  owl_viewwin_set_window(v, NULL);
[afbf668]175  if (v->onclose_hook) {
176    v->onclose_hook(v, v->onclose_hook_data);
[8721756]177    v->onclose_hook = NULL;
178    v->onclose_hook_data = NULL;
[afbf668]179  }
[7ab0020]180  owl_fmtext_cleanup(&(v->fmtext));
[7d4fbcd]181}
Note: See TracBrowser for help on using the repository browser.