source: viewwin.c @ 2640b63

release-1.10release-1.7release-1.8release-1.9
Last change on this file since 2640b63 was 2640b63, checked in by David Benjamin <davidben@mit.edu>, 14 years ago
Rearrange some viewwin functions The two constructor should be together. Otherwise, it's a bit hard to read.
  • Property mode set to 100644
File size: 6.1 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
[9eb38bb]33/* Create a viewwin.  'win' is an already initialized owl_window that
34 * will be used by the viewwin
[b2b0773]35 */
[9eb38bb]36owl_viewwin *owl_viewwin_new_fmtext(owl_window *win, const owl_fmtext *fmtext)
[b2b0773]37{
[dd6af02]38  char *text;
[9eb38bb]39  owl_viewwin *v = owl_malloc(sizeof(owl_viewwin));
40  memset(v, 0, sizeof(*v));
[dd6af02]41
[7d4fbcd]42  owl_fmtext_copy(&(v->fmtext), fmtext);
[dd6af02]43  text = owl_fmtext_print_plain(fmtext);
44  if (text[0] != '\0' && text[strlen(text) - 1] != '\n') {
45      owl_fmtext_append_normal(&(v->fmtext), "\n");
46  }
[3e55268]47  owl_free(text);
[7d4fbcd]48  v->textlines=owl_fmtext_num_lines(&(v->fmtext));
49  v->topline=0;
50  v->rightshift=0;
[68f63a2]51
52  owl_viewwin_set_window(v, win);
[9eb38bb]53  return v;
[7d4fbcd]54}
55
[61c1f19]56static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w)
[b2b0773]57{
[68f63a2]58  v->window = w;
59  if (w) {
[69873f7]60    g_object_ref(v->window);
[0b9e607]61    v->sig_redraw_id = g_signal_connect(w, "redraw", G_CALLBACK(owl_viewwin_redraw), v);
[68f63a2]62  }
[7d4fbcd]63}
64
[afbf668]65void owl_viewwin_set_onclose_hook(owl_viewwin *v, void (*onclose_hook) (owl_viewwin *vwin, void *data), void *onclose_hook_data) {
66  v->onclose_hook = onclose_hook;
67  v->onclose_hook_data = onclose_hook_data;
68}
69
[b2b0773]70/* regenerate text on the curses window. */
[0b9e607]71static void owl_viewwin_redraw(owl_window *w, WINDOW *curswin, void *user_data)
[b2b0773]72{
[7d4fbcd]73  owl_fmtext fm1, fm2;
[68f63a2]74  owl_viewwin *v = user_data;
[c447d9c]75  int winlines, wincols;
[9116936]76  int y;
[c447d9c]77
78  owl_window_get_position(w, &winlines, &wincols, 0, 0);
[7d4fbcd]79 
[68f63a2]80  werase(curswin);
81  wmove(curswin, 0, 0);
[7d4fbcd]82
[af2ca19]83  owl_fmtext_init_null(&fm1);
84  owl_fmtext_init_null(&fm2);
85 
[c447d9c]86  owl_fmtext_truncate_lines(&(v->fmtext), v->topline, winlines-BOTTOM_OFFSET, &fm1);
87  owl_fmtext_truncate_cols(&fm1, v->rightshift, wincols-1+v->rightshift, &fm2);
[7d4fbcd]88
[449c682]89  owl_fmtext_curs_waddstr(&fm2, curswin);
[7d4fbcd]90
[9116936]91  /* Fill remaining lines with tildes. */
92  y = v->textlines - v->topline;
93  wmove(curswin, y, 0);
94  for (; y < winlines-BOTTOM_OFFSET; y++) {
95    waddstr(curswin, EMPTY_INDICATOR);
96    waddstr(curswin, "\n");
97  }
98
[7d4fbcd]99  /* print the message at the bottom */
[c447d9c]100  wmove(curswin, winlines-1, 0);
[68f63a2]101  wattrset(curswin, A_REVERSE);
[c447d9c]102  if (v->textlines - v->topline > winlines-BOTTOM_OFFSET) {
[68f63a2]103    waddstr(curswin, "--More-- (Space to see more, 'q' to quit)");
[7d4fbcd]104  } else {
[68f63a2]105    waddstr(curswin, "--End-- (Press 'q' to quit)");
[7d4fbcd]106  }
[68f63a2]107  wattroff(curswin, A_REVERSE);
[7d4fbcd]108
[7ab0020]109  owl_fmtext_cleanup(&fm1);
110  owl_fmtext_cleanup(&fm2);
[7d4fbcd]111}
112
[2640b63]113void owl_viewwin_append_text(owl_viewwin *v, const char *text) {
114    owl_fmtext_append_normal(&(v->fmtext), text);
115    v->textlines=owl_fmtext_num_lines(&(v->fmtext));
116    owl_viewwin_dirty(v);
117}
118
119/* Schedule a redraw of 'v'. Exported for hooking into the search
120   string; when we have some way of listening for changes, this can be
121   removed. */
122void owl_viewwin_dirty(owl_viewwin *v)
123{
124  if (v->window)
125    owl_window_dirty(v->window);
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
[b8742ba]199/* Scroll in 'direction' to the next line containing 're' in 'v',
200 * starting from the current line. Returns 0 if no occurrence is
201 * found.
202 *
203 * If mode==0 then stay on the current line if it matches.
204 */
205int owl_viewwin_search(owl_viewwin *v, const owl_regex *re, int mode, int direction)
206{
207  int start, end, offset;
208  owl_fmtext_line_extents(&v->fmtext, v->topline, &start, &end);
209  if (direction == OWL_DIRECTION_DOWNWARDS) {
210    offset = owl_fmtext_search(&v->fmtext, re, mode ? end : start);
211    if (offset < 0)
212      return 0;
213    v->topline = owl_fmtext_line_number(&v->fmtext, offset);
214    owl_viewwin_dirty(v);
215    return 1;
216  } else {
217    /* FIXME */
218    owl_function_error("Upwards searching is not implemented in viewwin.");
219    return 1;
220  }
221}
222
[9eb38bb]223void owl_viewwin_delete(owl_viewwin *v)
[b2b0773]224{
[afbf668]225  if (v->onclose_hook) {
226    v->onclose_hook(v, v->onclose_hook_data);
[8721756]227    v->onclose_hook = NULL;
228    v->onclose_hook_data = NULL;
[afbf668]229  }
[9eb38bb]230  if (v->window) {
231    g_signal_handler_disconnect(v->window, v->sig_redraw_id);
232    g_object_unref(v->window);
233    v->window = NULL;
234  }
[7ab0020]235  owl_fmtext_cleanup(&(v->fmtext));
[9eb38bb]236  owl_free(v);
[7d4fbcd]237}
Note: See TracBrowser for help on using the repository browser.