| 1 | #include <string.h> |
|---|
| 2 | #include "owl.h" |
|---|
| 3 | |
|---|
| 4 | #define BOTTOM_OFFSET 1 |
|---|
| 5 | |
|---|
| 6 | static void owl_viewwin_redraw(owl_window *w, WINDOW *curswin, void *user_data); |
|---|
| 7 | static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w); |
|---|
| 8 | |
|---|
| 9 | /* Create a viewwin. 'win' is an already initialized owl_window that |
|---|
| 10 | * will be used by the viewwin |
|---|
| 11 | */ |
|---|
| 12 | owl_viewwin *owl_viewwin_new_text(owl_window *win, const char *text) |
|---|
| 13 | { |
|---|
| 14 | owl_viewwin *v = owl_malloc(sizeof(owl_viewwin)); |
|---|
| 15 | memset(v, 0, sizeof(*v)); |
|---|
| 16 | owl_fmtext_init_null(&(v->fmtext)); |
|---|
| 17 | if (text) { |
|---|
| 18 | owl_fmtext_append_normal(&(v->fmtext), text); |
|---|
| 19 | if (text[0] != '\0' && text[strlen(text) - 1] != '\n') { |
|---|
| 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; |
|---|
| 26 | v->onclose_hook = NULL; |
|---|
| 27 | |
|---|
| 28 | owl_viewwin_set_window(v, win); |
|---|
| 29 | return v; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | void owl_viewwin_append_text(owl_viewwin *v, const char *text) { |
|---|
| 33 | owl_fmtext_append_normal(&(v->fmtext), text); |
|---|
| 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. */ |
|---|
| 41 | void owl_viewwin_dirty(owl_viewwin *v) |
|---|
| 42 | { |
|---|
| 43 | if (v->window) |
|---|
| 44 | owl_window_dirty(v->window); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | /* Create a viewwin. 'win' is an already initialized owl_window that |
|---|
| 48 | * will be used by the viewwin |
|---|
| 49 | */ |
|---|
| 50 | owl_viewwin *owl_viewwin_new_fmtext(owl_window *win, const owl_fmtext *fmtext) |
|---|
| 51 | { |
|---|
| 52 | char *text; |
|---|
| 53 | owl_viewwin *v = owl_malloc(sizeof(owl_viewwin)); |
|---|
| 54 | memset(v, 0, sizeof(*v)); |
|---|
| 55 | |
|---|
| 56 | owl_fmtext_copy(&(v->fmtext), fmtext); |
|---|
| 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 | } |
|---|
| 61 | owl_free(text); |
|---|
| 62 | v->textlines=owl_fmtext_num_lines(&(v->fmtext)); |
|---|
| 63 | v->topline=0; |
|---|
| 64 | v->rightshift=0; |
|---|
| 65 | |
|---|
| 66 | owl_viewwin_set_window(v, win); |
|---|
| 67 | return v; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w) |
|---|
| 71 | { |
|---|
| 72 | v->window = w; |
|---|
| 73 | if (w) { |
|---|
| 74 | g_object_ref(v->window); |
|---|
| 75 | v->sig_redraw_id = g_signal_connect(w, "redraw", G_CALLBACK(owl_viewwin_redraw), v); |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | void 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 | |
|---|
| 84 | /* regenerate text on the curses window. */ |
|---|
| 85 | static void owl_viewwin_redraw(owl_window *w, WINDOW *curswin, void *user_data) |
|---|
| 86 | { |
|---|
| 87 | owl_fmtext fm1, fm2; |
|---|
| 88 | owl_viewwin *v = user_data; |
|---|
| 89 | int winlines, wincols; |
|---|
| 90 | |
|---|
| 91 | owl_window_get_position(w, &winlines, &wincols, 0, 0); |
|---|
| 92 | |
|---|
| 93 | werase(curswin); |
|---|
| 94 | wmove(curswin, 0, 0); |
|---|
| 95 | |
|---|
| 96 | owl_fmtext_init_null(&fm1); |
|---|
| 97 | owl_fmtext_init_null(&fm2); |
|---|
| 98 | |
|---|
| 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); |
|---|
| 101 | |
|---|
| 102 | owl_fmtext_curs_waddstr(&fm2, curswin); |
|---|
| 103 | |
|---|
| 104 | /* print the message at the bottom */ |
|---|
| 105 | wmove(curswin, winlines-1, 0); |
|---|
| 106 | wattrset(curswin, A_REVERSE); |
|---|
| 107 | if (v->textlines - v->topline > winlines-BOTTOM_OFFSET) { |
|---|
| 108 | waddstr(curswin, "--More-- (Space to see more, 'q' to quit)"); |
|---|
| 109 | } else { |
|---|
| 110 | waddstr(curswin, "--End-- (Press 'q' to quit)"); |
|---|
| 111 | } |
|---|
| 112 | wattroff(curswin, A_REVERSE); |
|---|
| 113 | |
|---|
| 114 | owl_fmtext_cleanup(&fm1); |
|---|
| 115 | owl_fmtext_cleanup(&fm2); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | void owl_viewwin_pagedown(owl_viewwin *v) |
|---|
| 119 | { |
|---|
| 120 | int winlines; |
|---|
| 121 | owl_window_get_position(v->window, &winlines, 0, 0, 0); |
|---|
| 122 | v->topline+=winlines - BOTTOM_OFFSET; |
|---|
| 123 | if ( (v->topline+winlines-BOTTOM_OFFSET) > v->textlines) { |
|---|
| 124 | v->topline = v->textlines - winlines + BOTTOM_OFFSET; |
|---|
| 125 | } |
|---|
| 126 | owl_viewwin_dirty(v); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | void owl_viewwin_linedown(owl_viewwin *v) |
|---|
| 130 | { |
|---|
| 131 | int winlines; |
|---|
| 132 | owl_window_get_position(v->window, &winlines, 0, 0, 0); |
|---|
| 133 | v->topline++; |
|---|
| 134 | if ( (v->topline+winlines-BOTTOM_OFFSET) > v->textlines) { |
|---|
| 135 | v->topline = v->textlines - winlines + BOTTOM_OFFSET; |
|---|
| 136 | } |
|---|
| 137 | owl_viewwin_dirty(v); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | void owl_viewwin_pageup(owl_viewwin *v) |
|---|
| 141 | { |
|---|
| 142 | int winlines; |
|---|
| 143 | owl_window_get_position(v->window, &winlines, 0, 0, 0); |
|---|
| 144 | v->topline-=winlines; |
|---|
| 145 | if (v->topline<0) v->topline=0; |
|---|
| 146 | owl_viewwin_dirty(v); |
|---|
| 147 | |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | void owl_viewwin_lineup(owl_viewwin *v) |
|---|
| 151 | { |
|---|
| 152 | v->topline--; |
|---|
| 153 | if (v->topline<0) v->topline=0; |
|---|
| 154 | owl_viewwin_dirty(v); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | void owl_viewwin_right(owl_viewwin *v, int n) |
|---|
| 158 | { |
|---|
| 159 | v->rightshift+=n; |
|---|
| 160 | owl_viewwin_dirty(v); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | void owl_viewwin_left(owl_viewwin *v, int n) |
|---|
| 164 | { |
|---|
| 165 | v->rightshift-=n; |
|---|
| 166 | if (v->rightshift<0) v->rightshift=0; |
|---|
| 167 | owl_viewwin_dirty(v); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | void owl_viewwin_top(owl_viewwin *v) |
|---|
| 171 | { |
|---|
| 172 | v->topline=0; |
|---|
| 173 | v->rightshift=0; |
|---|
| 174 | owl_viewwin_dirty(v); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | void owl_viewwin_bottom(owl_viewwin *v) |
|---|
| 178 | { |
|---|
| 179 | int winlines; |
|---|
| 180 | owl_window_get_position(v->window, &winlines, 0, 0, 0); |
|---|
| 181 | v->topline = v->textlines - winlines + BOTTOM_OFFSET; |
|---|
| 182 | owl_viewwin_dirty(v); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | void owl_viewwin_delete(owl_viewwin *v) |
|---|
| 186 | { |
|---|
| 187 | if (v->onclose_hook) { |
|---|
| 188 | v->onclose_hook(v, v->onclose_hook_data); |
|---|
| 189 | v->onclose_hook = NULL; |
|---|
| 190 | v->onclose_hook_data = NULL; |
|---|
| 191 | } |
|---|
| 192 | if (v->window) { |
|---|
| 193 | g_signal_handler_disconnect(v->window, v->sig_redraw_id); |
|---|
| 194 | g_object_unref(v->window); |
|---|
| 195 | v->window = NULL; |
|---|
| 196 | } |
|---|
| 197 | owl_fmtext_cleanup(&(v->fmtext)); |
|---|
| 198 | owl_free(v); |
|---|
| 199 | } |
|---|