source: viewwin.c @ 8ee712e0

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