source: viewwin.c @ fa23002

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