source: viewwin.c @ 9116936

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