| 1 | #include <string.h> |
|---|
| 2 | #include "owl.h" |
|---|
| 3 | |
|---|
| 4 | #define BOTTOM_OFFSET 1 |
|---|
| 5 | #define EMPTY_INDICATOR "~" |
|---|
| 6 | |
|---|
| 7 | static void owl_viewwin_redraw(owl_window *w, WINDOW *curswin, void *user_data); |
|---|
| 8 | static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w); |
|---|
| 9 | |
|---|
| 10 | /* Create a viewwin. 'win' is an already initialized owl_window that |
|---|
| 11 | * will be used by the viewwin |
|---|
| 12 | */ |
|---|
| 13 | owl_viewwin *owl_viewwin_new_text(owl_window *win, const char *text) |
|---|
| 14 | { |
|---|
| 15 | owl_viewwin *v = owl_malloc(sizeof(owl_viewwin)); |
|---|
| 16 | memset(v, 0, sizeof(*v)); |
|---|
| 17 | owl_fmtext_init_null(&(v->fmtext)); |
|---|
| 18 | if (text) { |
|---|
| 19 | owl_fmtext_append_normal(&(v->fmtext), text); |
|---|
| 20 | if (text[0] != '\0' && text[strlen(text) - 1] != '\n') { |
|---|
| 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; |
|---|
| 27 | v->onclose_hook = NULL; |
|---|
| 28 | |
|---|
| 29 | owl_viewwin_set_window(v, win); |
|---|
| 30 | return v; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | /* Create a viewwin. 'win' is an already initialized owl_window that |
|---|
| 34 | * will be used by the viewwin |
|---|
| 35 | */ |
|---|
| 36 | owl_viewwin *owl_viewwin_new_fmtext(owl_window *win, const owl_fmtext *fmtext) |
|---|
| 37 | { |
|---|
| 38 | char *text; |
|---|
| 39 | owl_viewwin *v = owl_malloc(sizeof(owl_viewwin)); |
|---|
| 40 | memset(v, 0, sizeof(*v)); |
|---|
| 41 | |
|---|
| 42 | owl_fmtext_copy(&(v->fmtext), fmtext); |
|---|
| 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 | } |
|---|
| 47 | owl_free(text); |
|---|
| 48 | v->textlines=owl_fmtext_num_lines(&(v->fmtext)); |
|---|
| 49 | v->topline=0; |
|---|
| 50 | v->rightshift=0; |
|---|
| 51 | |
|---|
| 52 | owl_viewwin_set_window(v, win); |
|---|
| 53 | return v; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w) |
|---|
| 57 | { |
|---|
| 58 | v->window = w; |
|---|
| 59 | if (w) { |
|---|
| 60 | g_object_ref(v->window); |
|---|
| 61 | v->sig_redraw_id = g_signal_connect(w, "redraw", G_CALLBACK(owl_viewwin_redraw), v); |
|---|
| 62 | } |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | void 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 | |
|---|
| 70 | /* regenerate text on the curses window. */ |
|---|
| 71 | static void owl_viewwin_redraw(owl_window *w, WINDOW *curswin, void *user_data) |
|---|
| 72 | { |
|---|
| 73 | owl_fmtext fm1, fm2; |
|---|
| 74 | owl_viewwin *v = user_data; |
|---|
| 75 | int winlines, wincols; |
|---|
| 76 | int y; |
|---|
| 77 | |
|---|
| 78 | owl_window_get_position(w, &winlines, &wincols, 0, 0); |
|---|
| 79 | |
|---|
| 80 | werase(curswin); |
|---|
| 81 | wmove(curswin, 0, 0); |
|---|
| 82 | |
|---|
| 83 | owl_fmtext_init_null(&fm1); |
|---|
| 84 | owl_fmtext_init_null(&fm2); |
|---|
| 85 | |
|---|
| 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); |
|---|
| 88 | |
|---|
| 89 | owl_fmtext_curs_waddstr(&fm2, curswin); |
|---|
| 90 | |
|---|
| 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 | |
|---|
| 99 | /* print the message at the bottom */ |
|---|
| 100 | wmove(curswin, winlines-1, 0); |
|---|
| 101 | wattrset(curswin, A_REVERSE); |
|---|
| 102 | if (v->textlines - v->topline > winlines-BOTTOM_OFFSET) { |
|---|
| 103 | waddstr(curswin, "--More-- (Space to see more, 'q' to quit)"); |
|---|
| 104 | } else { |
|---|
| 105 | waddstr(curswin, "--End-- (Press 'q' to quit)"); |
|---|
| 106 | } |
|---|
| 107 | wattroff(curswin, A_REVERSE); |
|---|
| 108 | |
|---|
| 109 | owl_fmtext_cleanup(&fm1); |
|---|
| 110 | owl_fmtext_cleanup(&fm2); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | void 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. */ |
|---|
| 122 | void owl_viewwin_dirty(owl_viewwin *v) |
|---|
| 123 | { |
|---|
| 124 | if (v->window) |
|---|
| 125 | owl_window_dirty(v->window); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | void owl_viewwin_down(owl_viewwin *v, int amount) { |
|---|
| 129 | int winlines; |
|---|
| 130 | owl_window_get_position(v->window, &winlines, 0, 0, 0); |
|---|
| 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); |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | void 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 | |
|---|
| 147 | void owl_viewwin_pagedown(owl_viewwin *v) |
|---|
| 148 | { |
|---|
| 149 | int winlines; |
|---|
| 150 | owl_window_get_position(v->window, &winlines, 0, 0, 0); |
|---|
| 151 | owl_viewwin_down(v, winlines - BOTTOM_OFFSET); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | void owl_viewwin_linedown(owl_viewwin *v) |
|---|
| 155 | { |
|---|
| 156 | owl_viewwin_down(v, 1); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | void owl_viewwin_pageup(owl_viewwin *v) |
|---|
| 160 | { |
|---|
| 161 | int winlines; |
|---|
| 162 | owl_window_get_position(v->window, &winlines, 0, 0, 0); |
|---|
| 163 | owl_viewwin_up(v, winlines - BOTTOM_OFFSET); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | void owl_viewwin_lineup(owl_viewwin *v) |
|---|
| 167 | { |
|---|
| 168 | owl_viewwin_up(v, 1); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | void owl_viewwin_right(owl_viewwin *v, int n) |
|---|
| 172 | { |
|---|
| 173 | v->rightshift+=n; |
|---|
| 174 | owl_viewwin_dirty(v); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | void owl_viewwin_left(owl_viewwin *v, int n) |
|---|
| 178 | { |
|---|
| 179 | v->rightshift-=n; |
|---|
| 180 | if (v->rightshift<0) v->rightshift=0; |
|---|
| 181 | owl_viewwin_dirty(v); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | void owl_viewwin_top(owl_viewwin *v) |
|---|
| 185 | { |
|---|
| 186 | v->topline=0; |
|---|
| 187 | v->rightshift=0; |
|---|
| 188 | owl_viewwin_dirty(v); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | void owl_viewwin_bottom(owl_viewwin *v) |
|---|
| 192 | { |
|---|
| 193 | int winlines; |
|---|
| 194 | owl_window_get_position(v->window, &winlines, 0, 0, 0); |
|---|
| 195 | v->topline = v->textlines - winlines + BOTTOM_OFFSET; |
|---|
| 196 | owl_viewwin_dirty(v); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 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 | */ |
|---|
| 205 | int 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 | |
|---|
| 223 | void owl_viewwin_delete(owl_viewwin *v) |
|---|
| 224 | { |
|---|
| 225 | if (v->onclose_hook) { |
|---|
| 226 | v->onclose_hook(v, v->onclose_hook_data); |
|---|
| 227 | v->onclose_hook = NULL; |
|---|
| 228 | v->onclose_hook_data = NULL; |
|---|
| 229 | } |
|---|
| 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 | } |
|---|
| 235 | owl_fmtext_cleanup(&(v->fmtext)); |
|---|
| 236 | owl_free(v); |
|---|
| 237 | } |
|---|