source: viewwin.c @ b3b1b05

release-1.7release-1.8release-1.9
Last change on this file since b3b1b05 was 5b68c05, checked in by David Benjamin <davidben@mit.edu>, 12 years ago
Update the viewwin when the global search string changes This will want to be made less hacky when we have some notification mechanism for all of owl_global's properties. Also, use the new owl_viewwin_dirty in internal functions to avoid dirtying a NULL window.
  • Property mode set to 100644
File size: 4.8 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);
[5b68c05]30    v->textlines=owl_fmtext_num_lines(&(v->fmtext));
31    owl_viewwin_dirty(v);
32}
33
34/* Schedule a redraw of 'v'. Exported for hooking into the search
35   string; when we have some way of listening for changes, this can be
36   removed. */
37void owl_viewwin_dirty(owl_viewwin *v)
38{
39  if (v->window)
[68f63a2]40    owl_window_dirty(v->window);
[7d4fbcd]41}
42
[b2b0773]43/* initialize the viewwin e.  'win' is an already initialzed curses
44 * window that will be used by viewwin
45 */
[68f63a2]46void owl_viewwin_init_fmtext(owl_viewwin *v, owl_window *win, const owl_fmtext *fmtext)
[b2b0773]47{
[dd6af02]48  char *text;
49
[7d4fbcd]50  owl_fmtext_copy(&(v->fmtext), fmtext);
[dd6af02]51  text = owl_fmtext_print_plain(fmtext);
52  if (text[0] != '\0' && text[strlen(text) - 1] != '\n') {
53      owl_fmtext_append_normal(&(v->fmtext), "\n");
54  }
[3e55268]55  owl_free(text);
[7d4fbcd]56  v->textlines=owl_fmtext_num_lines(&(v->fmtext));
57  v->topline=0;
58  v->rightshift=0;
[68f63a2]59
60  owl_viewwin_set_window(v, win);
[7d4fbcd]61}
62
[68f63a2]63void owl_viewwin_set_window(owl_viewwin *v, owl_window *w)
[b2b0773]64{
[68f63a2]65  if (v->window) {
[7a6e6c7]66    g_signal_handler_disconnect(v->window, v->sig_redraw_id);
[69873f7]67    g_object_unref(v->window);
[68f63a2]68  }
69  v->window = w;
70  if (w) {
[69873f7]71    g_object_ref(v->window);
[0b9e607]72    v->sig_redraw_id = g_signal_connect(w, "redraw", G_CALLBACK(owl_viewwin_redraw), v);
[68f63a2]73  }
[7d4fbcd]74}
75
[afbf668]76void owl_viewwin_set_onclose_hook(owl_viewwin *v, void (*onclose_hook) (owl_viewwin *vwin, void *data), void *onclose_hook_data) {
77  v->onclose_hook = onclose_hook;
78  v->onclose_hook_data = onclose_hook_data;
79}
80
[b2b0773]81/* regenerate text on the curses window. */
[0b9e607]82static void owl_viewwin_redraw(owl_window *w, WINDOW *curswin, void *user_data)
[b2b0773]83{
[7d4fbcd]84  owl_fmtext fm1, fm2;
[68f63a2]85  owl_viewwin *v = user_data;
[c447d9c]86  int winlines, wincols;
87
88  owl_window_get_position(w, &winlines, &wincols, 0, 0);
[7d4fbcd]89 
[68f63a2]90  werase(curswin);
91  wmove(curswin, 0, 0);
[7d4fbcd]92
[af2ca19]93  owl_fmtext_init_null(&fm1);
94  owl_fmtext_init_null(&fm2);
95 
[c447d9c]96  owl_fmtext_truncate_lines(&(v->fmtext), v->topline, winlines-BOTTOM_OFFSET, &fm1);
97  owl_fmtext_truncate_cols(&fm1, v->rightshift, wincols-1+v->rightshift, &fm2);
[7d4fbcd]98
[449c682]99  owl_fmtext_curs_waddstr(&fm2, curswin);
[7d4fbcd]100
101  /* print the message at the bottom */
[c447d9c]102  wmove(curswin, winlines-1, 0);
[68f63a2]103  wattrset(curswin, A_REVERSE);
[c447d9c]104  if (v->textlines - v->topline > winlines-BOTTOM_OFFSET) {
[68f63a2]105    waddstr(curswin, "--More-- (Space to see more, 'q' to quit)");
[7d4fbcd]106  } else {
[68f63a2]107    waddstr(curswin, "--End-- (Press 'q' to quit)");
[7d4fbcd]108  }
[68f63a2]109  wattroff(curswin, A_REVERSE);
[7d4fbcd]110
[7ab0020]111  owl_fmtext_cleanup(&fm1);
112  owl_fmtext_cleanup(&fm2);
[7d4fbcd]113}
114
[b2b0773]115void owl_viewwin_pagedown(owl_viewwin *v)
116{
[c447d9c]117  int winlines;
118  owl_window_get_position(v->window, &winlines, 0, 0, 0);
119  v->topline+=winlines - BOTTOM_OFFSET;
120  if ( (v->topline+winlines-BOTTOM_OFFSET) > v->textlines) {
121    v->topline = v->textlines - winlines + BOTTOM_OFFSET;
[7d4fbcd]122  }
[5b68c05]123  owl_viewwin_dirty(v);
[7d4fbcd]124}
125
[b2b0773]126void owl_viewwin_linedown(owl_viewwin *v)
127{
[c447d9c]128  int winlines;
129  owl_window_get_position(v->window, &winlines, 0, 0, 0);
[7d4fbcd]130  v->topline++;
[c447d9c]131  if ( (v->topline+winlines-BOTTOM_OFFSET) > v->textlines) {
132    v->topline = v->textlines - winlines + BOTTOM_OFFSET;
[7d4fbcd]133  }
[5b68c05]134  owl_viewwin_dirty(v);
[7d4fbcd]135}
136
[b2b0773]137void owl_viewwin_pageup(owl_viewwin *v)
138{
[c447d9c]139  int winlines;
140  owl_window_get_position(v->window, &winlines, 0, 0, 0);
141  v->topline-=winlines;
[7d4fbcd]142  if (v->topline<0) v->topline=0;
[5b68c05]143  owl_viewwin_dirty(v);
144
[7d4fbcd]145}
146
[b2b0773]147void owl_viewwin_lineup(owl_viewwin *v)
148{
[7d4fbcd]149  v->topline--;
150  if (v->topline<0) v->topline=0;
[5b68c05]151  owl_viewwin_dirty(v);
[7d4fbcd]152}
153
[b2b0773]154void owl_viewwin_right(owl_viewwin *v, int n)
155{
[7d4fbcd]156  v->rightshift+=n;
[5b68c05]157  owl_viewwin_dirty(v);
[7d4fbcd]158}
159
[b2b0773]160void owl_viewwin_left(owl_viewwin *v, int n)
161{
[7d4fbcd]162  v->rightshift-=n;
163  if (v->rightshift<0) v->rightshift=0;
[5b68c05]164  owl_viewwin_dirty(v);
[7d4fbcd]165}
166
[b2b0773]167void owl_viewwin_top(owl_viewwin *v)
168{
[7d4fbcd]169  v->topline=0;
170  v->rightshift=0;
[5b68c05]171  owl_viewwin_dirty(v);
[7d4fbcd]172}
173
[b2b0773]174void owl_viewwin_bottom(owl_viewwin *v)
175{
[c447d9c]176  int winlines;
177  owl_window_get_position(v->window, &winlines, 0, 0, 0);
178  v->topline = v->textlines - winlines + BOTTOM_OFFSET;
[5b68c05]179  owl_viewwin_dirty(v);
[7d4fbcd]180}
181
[963b471]182void owl_viewwin_cleanup(owl_viewwin *v)
[b2b0773]183{
[5f7eadf]184  owl_viewwin_set_window(v, NULL);
[afbf668]185  if (v->onclose_hook) {
186    v->onclose_hook(v, v->onclose_hook_data);
[8721756]187    v->onclose_hook = NULL;
188    v->onclose_hook_data = NULL;
[afbf668]189  }
[7ab0020]190  owl_fmtext_cleanup(&(v->fmtext));
[7d4fbcd]191}
Note: See TracBrowser for help on using the repository browser.