1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
/*
* Copyright 2004-2005 Timo Hirvonen
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include "window.h"
#include "xmalloc.h"
#include "debug.h"
#include <stdlib.h>
static void sel_changed(struct window *win)
{
if (win->sel_changed)
win->sel_changed();
win->changed = 1;
}
struct window *window_new(int (*get_prev)(struct iter *), int (*get_next)(struct iter *))
{
struct window *win;
win = xnew(struct window, 1);
win->get_next = get_next;
win->get_prev = get_prev;
win->sel_changed = NULL;
win->nr_rows = 1;
win->changed = 1;
iter_init(&win->head);
iter_init(&win->top);
iter_init(&win->sel);
return win;
}
void window_free(struct window *win)
{
free(win);
}
void window_set_empty(struct window *win)
{
iter_init(&win->head);
iter_init(&win->top);
iter_init(&win->sel);
sel_changed(win);
}
void window_set_contents(struct window *win, void *head)
{
struct iter first;
win->head.data0 = head;
win->head.data1 = NULL;
win->head.data2 = NULL;
first = win->head;
win->get_next(&first);
win->top = first;
win->sel = first;
sel_changed(win);
}
void window_set_nr_rows(struct window *win, int nr_rows)
{
struct iter old_sel;
if (nr_rows < 1)
return;
win->nr_rows = nr_rows;
old_sel = win->sel;
window_changed(win);
win->changed = 1;
}
void window_up(struct window *win, int rows)
{
int i;
for (i = 0; i < rows; i++) {
struct iter prev = win->sel;
if (!win->get_prev(&prev))
break;
if (iters_equal(&win->sel, &win->top))
win->top = prev;
win->sel = prev;
}
if (i)
sel_changed(win);
}
void window_down(struct window *win, int rows)
{
struct iter iter;
int delta, sel_down, top_down;
/* distance between top and sel */
delta = 0;
iter = win->top;
while (!iters_equal(&iter, &win->sel)) {
win->get_next(&iter);
delta++;
}
for (sel_down = 0; sel_down < rows; sel_down++) {
iter = win->sel;
if (!win->get_next(&iter))
break;
win->sel = iter;
}
top_down = sel_down - (win->nr_rows - delta - 1);
while (top_down > 0) {
win->get_next(&win->top);
top_down--;
}
if (sel_down)
sel_changed(win);
}
/*
* minimize number of empty lines visible
* make sure selection is visible
*/
void window_changed(struct window *win)
{
struct iter iter;
int delta, rows;
if (iter_is_null(&win->head)) {
BUG_ON(!iter_is_null(&win->top));
BUG_ON(!iter_is_null(&win->sel));
return;
}
BUG_ON(iter_is_null(&win->top));
BUG_ON(iter_is_null(&win->sel));
/* make sure top and sel point to real row if possible */
if (iter_is_head(&win->top)) {
win->get_next(&win->top);
win->sel = win->top;
sel_changed(win);
return;
}
/* make sure the selected row is visible */
/* get distance between top and sel */
delta = 0;
iter = win->top;
while (!iters_equal(&iter, &win->sel)) {
if (!win->get_next(&iter)) {
/* sel < top, scroll up until top == sel */
while (!iters_equal(&win->top, &win->sel))
win->get_prev(&win->top);
goto minimize;
}
delta++;
}
/* scroll down until sel is visible */
while (delta > win->nr_rows - 1) {
win->get_next(&win->top);
delta--;
}
minimize:
/* minimize number of empty lines shown */
iter = win->top;
rows = 1;
while (rows < win->nr_rows) {
if (!win->get_next(&iter))
break;
rows++;
}
while (rows < win->nr_rows) {
iter = win->top;
if (!win->get_prev(&iter))
break;
win->top = iter;
rows++;
}
win->changed = 1;
}
void window_row_vanishes(struct window *win, struct iter *iter)
{
struct iter new = *iter;
BUG_ON(iter->data0 != win->head.data0);
if (!win->get_next(&new)) {
new = *iter;
win->get_prev(&new);
}
if (iters_equal(&win->top, iter))
win->top = new;
if (iters_equal(&win->sel, iter)) {
win->sel = new;
sel_changed(win);
}
win->changed = 1;
}
int window_get_top(struct window *win, struct iter *iter)
{
*iter = win->top;
return !iter_is_empty(iter);
}
int window_get_sel(struct window *win, struct iter *iter)
{
*iter = win->sel;
return !iter_is_empty(iter);
}
int window_get_prev(struct window *win, struct iter *iter)
{
return win->get_prev(iter);
}
int window_get_next(struct window *win, struct iter *iter)
{
return win->get_next(iter);
}
void window_set_sel(struct window *win, struct iter *iter)
{
int sel_nr, top_nr;
struct iter tmp;
BUG_ON(iter_is_empty(&win->top));
BUG_ON(iter_is_empty(iter));
BUG_ON(iter->data0 != win->head.data0);
if (iters_equal(&win->sel, iter))
return;
win->sel = *iter;
tmp = win->head;
win->get_next(&tmp);
top_nr = 0;
while (!iters_equal(&tmp, &win->top)) {
win->get_next(&tmp);
top_nr++;
}
tmp = win->head;
win->get_next(&tmp);
sel_nr = 0;
while (!iters_equal(&tmp, &win->sel)) {
BUG_ON(!win->get_next(&tmp));
sel_nr++;
}
if (sel_nr < top_nr)
win->top = win->sel;
while (sel_nr - top_nr >= win->nr_rows) {
win->get_next(&win->top);
top_nr++;
}
sel_changed(win);
}
void window_goto_top(struct window *win)
{
struct iter old_sel;
old_sel = win->sel;
win->sel = win->head;
win->get_next(&win->sel);
win->top = win->sel;
if (!iters_equal(&old_sel, &win->sel))
sel_changed(win);
}
void window_goto_bottom(struct window *win)
{
struct iter old_sel;
int count;
old_sel = win->sel;
win->sel = win->head;
win->get_prev(&win->sel);
win->top = win->sel;
count = win->nr_rows - 1;
while (count) {
struct iter iter = win->top;
if (!win->get_prev(&iter))
break;
win->top = iter;
count--;
}
if (!iters_equal(&old_sel, &win->sel))
sel_changed(win);
}
void window_page_up(struct window *win)
{
window_up(win, win->nr_rows - 1);
}
void window_page_down(struct window *win)
{
window_down(win, win->nr_rows - 1);
}
int window_get_nr_rows(struct window *win)
{
return win->nr_rows;
}
|