source: viewwin.c @ 1d74663

release-1.10release-1.7release-1.8release-1.9
Last change on this file since 1d74663 was 60c1386, checked in by David Benjamin <davidben@mit.edu>, 14 years ago
Split the viewwin's status bar into its own window
  • Property mode set to 100644
File size: 7.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
[60c1386]7static void owl_viewwin_redraw_content(owl_window *w, WINDOW *curswin, void *user_data);
8static void owl_viewwin_redraw_status(owl_window *w, WINDOW *curswin, void *user_data);
9static void owl_viewwin_layout(owl_viewwin *v);
[61c1f19]10static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w);
[0b9e607]11
[9eb38bb]12/* Create a viewwin.  'win' is an already initialized owl_window that
13 * will be used by the viewwin
[b2b0773]14 */
[9eb38bb]15owl_viewwin *owl_viewwin_new_text(owl_window *win, const char *text)
[b2b0773]16{
[9eb38bb]17  owl_viewwin *v = owl_malloc(sizeof(owl_viewwin));
18  memset(v, 0, sizeof(*v));
[7d4fbcd]19  owl_fmtext_init_null(&(v->fmtext));
20  if (text) {
21    owl_fmtext_append_normal(&(v->fmtext), text);
[4083c49]22    if (text[0] != '\0' && text[strlen(text) - 1] != '\n') {
[7d4fbcd]23      owl_fmtext_append_normal(&(v->fmtext), "\n");
24    }
25    v->textlines=owl_fmtext_num_lines(&(v->fmtext));
26  }
27  v->topline=0;
28  v->rightshift=0;
[afbf668]29  v->onclose_hook = NULL;
[68f63a2]30
31  owl_viewwin_set_window(v, win);
[9eb38bb]32  return v;
[afbf668]33}
34
[9eb38bb]35/* Create a viewwin.  'win' is an already initialized owl_window that
36 * will be used by the viewwin
[b2b0773]37 */
[9eb38bb]38owl_viewwin *owl_viewwin_new_fmtext(owl_window *win, const owl_fmtext *fmtext)
[b2b0773]39{
[dd6af02]40  char *text;
[9eb38bb]41  owl_viewwin *v = owl_malloc(sizeof(owl_viewwin));
42  memset(v, 0, sizeof(*v));
[dd6af02]43
[7d4fbcd]44  owl_fmtext_copy(&(v->fmtext), fmtext);
[dd6af02]45  text = owl_fmtext_print_plain(fmtext);
46  if (text[0] != '\0' && text[strlen(text) - 1] != '\n') {
47      owl_fmtext_append_normal(&(v->fmtext), "\n");
48  }
[3e55268]49  owl_free(text);
[7d4fbcd]50  v->textlines=owl_fmtext_num_lines(&(v->fmtext));
51  v->topline=0;
52  v->rightshift=0;
[68f63a2]53
54  owl_viewwin_set_window(v, win);
[9eb38bb]55  return v;
[7d4fbcd]56}
57
[61c1f19]58static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w)
[b2b0773]59{
[60c1386]60  v->window = g_object_ref(w);
61  v->content = owl_window_new(v->window);
62  v->status = owl_window_new(v->window);
63
64  v->sig_content_redraw_id =
65    g_signal_connect(v->content, "redraw", G_CALLBACK(owl_viewwin_redraw_content), v);
66  v->sig_status_redraw_id =
67    g_signal_connect(v->status, "redraw", G_CALLBACK(owl_viewwin_redraw_status), v);
68  v->sig_resize_id =
69    g_signal_connect_swapped(v->window, "resized", G_CALLBACK(owl_viewwin_layout), v);
70  owl_viewwin_layout(v);
71
72  owl_window_show(v->content);
73  owl_window_show(v->status);
[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
[60c1386]81static void owl_viewwin_layout(owl_viewwin *v)
82{
83  int lines, cols;
84  owl_window_get_position(v->window, &lines, &cols, NULL, NULL);
85  owl_window_set_position(v->content, lines - BOTTOM_OFFSET, cols, 0, 0);
86  owl_window_set_position(v->status, BOTTOM_OFFSET, cols, lines - BOTTOM_OFFSET, 0);
87}
88
[b2b0773]89/* regenerate text on the curses window. */
[60c1386]90static void owl_viewwin_redraw_content(owl_window *w, WINDOW *curswin, void *user_data)
[b2b0773]91{
[7d4fbcd]92  owl_fmtext fm1, fm2;
[68f63a2]93  owl_viewwin *v = user_data;
[c447d9c]94  int winlines, wincols;
[9116936]95  int y;
[c447d9c]96
97  owl_window_get_position(w, &winlines, &wincols, 0, 0);
[60c1386]98
[68f63a2]99  werase(curswin);
100  wmove(curswin, 0, 0);
[7d4fbcd]101
[af2ca19]102  owl_fmtext_init_null(&fm1);
103  owl_fmtext_init_null(&fm2);
[60c1386]104
105  owl_fmtext_truncate_lines(&(v->fmtext), v->topline, winlines, &fm1);
[c447d9c]106  owl_fmtext_truncate_cols(&fm1, v->rightshift, wincols-1+v->rightshift, &fm2);
[7d4fbcd]107
[449c682]108  owl_fmtext_curs_waddstr(&fm2, curswin);
[7d4fbcd]109
[9116936]110  /* Fill remaining lines with tildes. */
111  y = v->textlines - v->topline;
112  wmove(curswin, y, 0);
[60c1386]113  for (; y < winlines; y++) {
[9116936]114    waddstr(curswin, EMPTY_INDICATOR);
115    waddstr(curswin, "\n");
116  }
117
[60c1386]118  owl_fmtext_cleanup(&fm1);
119  owl_fmtext_cleanup(&fm2);
120}
121
122static void owl_viewwin_redraw_status(owl_window *w, WINDOW *curswin, void *user_data)
123{
124  owl_viewwin *v = user_data;
125  int winlines, wincols;
126
127  owl_window_get_position(v->content, &winlines, &wincols, 0, 0);
128
129  werase(curswin);
130  wmove(curswin, 0, 0);
[68f63a2]131  wattrset(curswin, A_REVERSE);
[60c1386]132  if (v->textlines - v->topline > winlines) {
[68f63a2]133    waddstr(curswin, "--More-- (Space to see more, 'q' to quit)");
[7d4fbcd]134  } else {
[68f63a2]135    waddstr(curswin, "--End-- (Press 'q' to quit)");
[7d4fbcd]136  }
[68f63a2]137  wattroff(curswin, A_REVERSE);
[7d4fbcd]138}
139
[2640b63]140void owl_viewwin_append_text(owl_viewwin *v, const char *text) {
141    owl_fmtext_append_normal(&(v->fmtext), text);
142    v->textlines=owl_fmtext_num_lines(&(v->fmtext));
143    owl_viewwin_dirty(v);
144}
145
146/* Schedule a redraw of 'v'. Exported for hooking into the search
147   string; when we have some way of listening for changes, this can be
148   removed. */
149void owl_viewwin_dirty(owl_viewwin *v)
150{
[60c1386]151  owl_window_dirty(v->content);
152  owl_window_dirty(v->status);
[2640b63]153}
154
[d574d61]155void owl_viewwin_down(owl_viewwin *v, int amount) {
[c447d9c]156  int winlines;
[60c1386]157  owl_window_get_position(v->content, &winlines, 0, 0, 0);
[fa23002]158  /* Don't scroll past the bottom. */
[60c1386]159  amount = MIN(amount, v->textlines - (v->topline + winlines));
[fa23002]160  /* But if we're already past the bottom, don't back up either. */
161  if (amount > 0) {
162    v->topline += amount;
163    owl_viewwin_dirty(v);
[7d4fbcd]164  }
165}
166
[d574d61]167void owl_viewwin_up(owl_viewwin *v, int amount)
168{
169  v->topline -= amount;
170  if (v->topline<0) v->topline=0;
171  owl_viewwin_dirty(v);
172}
173
174void owl_viewwin_pagedown(owl_viewwin *v)
[b2b0773]175{
[c447d9c]176  int winlines;
[60c1386]177  owl_window_get_position(v->content, &winlines, 0, 0, 0);
178  owl_viewwin_down(v, winlines);
[d574d61]179}
180
181void owl_viewwin_linedown(owl_viewwin *v)
182{
183  owl_viewwin_down(v, 1);
[7d4fbcd]184}
185
[b2b0773]186void owl_viewwin_pageup(owl_viewwin *v)
187{
[c447d9c]188  int winlines;
[60c1386]189  owl_window_get_position(v->content, &winlines, 0, 0, 0);
190  owl_viewwin_up(v, winlines);
[7d4fbcd]191}
192
[b2b0773]193void owl_viewwin_lineup(owl_viewwin *v)
194{
[d574d61]195  owl_viewwin_up(v, 1);
[7d4fbcd]196}
197
[b2b0773]198void owl_viewwin_right(owl_viewwin *v, int n)
199{
[7d4fbcd]200  v->rightshift+=n;
[5b68c05]201  owl_viewwin_dirty(v);
[7d4fbcd]202}
203
[b2b0773]204void owl_viewwin_left(owl_viewwin *v, int n)
205{
[7d4fbcd]206  v->rightshift-=n;
207  if (v->rightshift<0) v->rightshift=0;
[5b68c05]208  owl_viewwin_dirty(v);
[7d4fbcd]209}
210
[b2b0773]211void owl_viewwin_top(owl_viewwin *v)
212{
[7d4fbcd]213  v->topline=0;
214  v->rightshift=0;
[5b68c05]215  owl_viewwin_dirty(v);
[7d4fbcd]216}
217
[b2b0773]218void owl_viewwin_bottom(owl_viewwin *v)
219{
[c447d9c]220  int winlines;
[60c1386]221  owl_window_get_position(v->content, &winlines, 0, 0, 0);
222  v->topline = v->textlines - winlines;
[5b68c05]223  owl_viewwin_dirty(v);
[7d4fbcd]224}
225
[b8742ba]226/* Scroll in 'direction' to the next line containing 're' in 'v',
227 * starting from the current line. Returns 0 if no occurrence is
228 * found.
229 *
230 * If mode==0 then stay on the current line if it matches.
231 */
232int owl_viewwin_search(owl_viewwin *v, const owl_regex *re, int mode, int direction)
233{
234  int start, end, offset;
235  owl_fmtext_line_extents(&v->fmtext, v->topline, &start, &end);
236  if (direction == OWL_DIRECTION_DOWNWARDS) {
237    offset = owl_fmtext_search(&v->fmtext, re, mode ? end : start);
238    if (offset < 0)
239      return 0;
240    v->topline = owl_fmtext_line_number(&v->fmtext, offset);
241    owl_viewwin_dirty(v);
242    return 1;
243  } else {
244    /* FIXME */
245    owl_function_error("Upwards searching is not implemented in viewwin.");
246    return 1;
247  }
248}
249
[9eb38bb]250void owl_viewwin_delete(owl_viewwin *v)
[b2b0773]251{
[afbf668]252  if (v->onclose_hook) {
253    v->onclose_hook(v, v->onclose_hook_data);
[8721756]254    v->onclose_hook = NULL;
255    v->onclose_hook_data = NULL;
[afbf668]256  }
[60c1386]257  /* TODO: This is far too tedious. owl_viewwin should own v->window
258   * and just unlink it in one go. Signals should also go away for
259   * free. */
260  g_signal_handler_disconnect(v->window, v->sig_resize_id);
261  g_signal_handler_disconnect(v->content, v->sig_content_redraw_id);
262  g_signal_handler_disconnect(v->status, v->sig_status_redraw_id);
263  owl_window_unlink(v->content);
264  owl_window_unlink(v->status);
265  g_object_unref(v->window);
266  g_object_unref(v->content);
267  g_object_unref(v->status);
[7ab0020]268  owl_fmtext_cleanup(&(v->fmtext));
[9eb38bb]269  owl_free(v);
[7d4fbcd]270}
Note: See TracBrowser for help on using the repository browser.