2 * \brief Browser history buffer
4 * \author Norman Feske <norman.feske@genode-labs.com>
8 * Copyright (C) 2005-2009
9 * Genode Labs, Feske & Helmuth Systementwicklung GbR
11 * This file is part of the Genode OS framework, which is distributed
12 * under the terms of the GNU General Public License version 2.
20 using Scout_gfx::Widget;
26 static const int _size = 128; /* history size */
28 int _idx; /* current position in history */
29 Widget *_history[_size]; /* ring buffer of history references */
32 * Increment history position
34 * \offset The incrementation offset must be higher than -_size
36 inline void _inc(int offset) { _idx = (_idx + _size + offset) % _size; }
39 * Return next index of current position
41 inline int _next() { return (_idx + 1) % _size; }
44 * Return previous index of current position
46 inline int _prev() { return (_idx + _size - 1) % _size; }
55 memset(_history, 0, sizeof(_history));
59 * Request element at current history position
61 Widget *curr() { return _history[_idx]; }
64 * Add element to history.
66 * We increment the current history position and insert the
67 * new element there. If the new element is identical with
68 * with the old element at that position, we keep the following
69 * history elements. Otherwise, we follow a new link 'branch'
70 * and cut off the old one.
74 /* discard invalid history elements */
78 /* increment history position */
81 /* do we just follow the forward path? */
85 /* cut off old forward history branch */
86 _history[_next()] = 0;
88 /* insert new element */
93 * Assign new element to current history entry
95 void assign(Widget *e) { if (e) _history[_idx] = e; }
98 * Travel forward or backward in history
100 * \return 1 on success,
101 * 0 if end of history is reached
103 enum direction { FORWARD, BACKWARD };
104 int go(direction dir)
106 /* stop at the boundaries of the history */
107 if (!_history[dir == FORWARD ? _next() : _prev()]) return 0;
109 /* travel in history */
110 _inc(dir == FORWARD ? 1 : -1);