summaryrefslogtreecommitdiff
path: root/framebuffer/fbtk_widget_scroll.c
blob: c28887bda1e840c91f33e1624e8a6d74ab949958 (plain)
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
/*
 * Copyright 2008 Vincent Sanders <vince@simtec.co.uk>
 *
 * Framebuffer windowing toolkit scrollbar widgets
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf 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; version 2 of the License.
 *
 * NetSurf 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, see <http://www.gnu.org/licenses/>.
 */

#include <stdbool.h>

#include <libnsfb.h>
#include <libnsfb_plot.h>
#include <libnsfb_event.h>
#include <libnsfb_cursor.h>

#include "utils/log.h"
#include "desktop/browser.h"

#include "framebuffer/gui.h"
#include "framebuffer/fbtk.h"
#include "framebuffer/fbtk_widget.h"
#include "framebuffer/bitmap.h"
#include "framebuffer/image_data.h"

/** Vertical scroll widget */

static int
vscroll_redraw(fbtk_widget_t *widget, fbtk_callback_info *cbi)
{
        int vscroll;
        int vpos;

        nsfb_bbox_t bbox;
        nsfb_bbox_t rect;
	fbtk_widget_t *root = get_root_widget(widget);

        fbtk_get_bbox(widget, &bbox);

        nsfb_claim(root->u.root.fb, &bbox);

        rect = bbox;

	/* background */
        nsfb_plot_rectangle_fill(root->u.root.fb, &rect, widget->bg);

        rect.x0 = bbox.x0 + 2;
        rect.y0 = bbox.y0 + 1;
        rect.x1 = bbox.x1 - 3;
        rect.y1 = bbox.y1 - 2;
        nsfb_plot_rectangle_fill(root->u.root.fb, &rect, widget->fg);

	/* scroll well */
        nsfb_plot_rectangle(root->u.root.fb, &rect, 1, 0xFF999999, false, false);

	/* scroll well outline */
        vscroll = ((widget->height - 4) * widget->u.scroll.pct) / 100 ;
        vpos = ((widget->height - 4) * widget->u.scroll.pos) / 100 ;

        LOG(("scroll %d",vscroll));

        rect.x0 = bbox.x0 + 5;
        rect.y0 = bbox.y0 + 3 + vpos;
        rect.x1 = bbox.x0 + widget->width - 5;
        rect.y1 = bbox.y0 + vscroll + vpos;

        nsfb_plot_rectangle_fill(root->u.root.fb, &rect, widget->bg);

        nsfb_update(root->u.root.fb, &bbox);

        return 0;
}

static int
vscrollu_click(fbtk_widget_t *widget, fbtk_callback_info *cbi)
{
	int ret = 0;
        if (cbi->event->type == NSFB_EVENT_KEY_DOWN)
		ret = fbtk_post_callback(cbi->context, FBTK_CBT_SCROLLY, -1);
	return ret;
}

static int
vscrolld_click(fbtk_widget_t *widget, fbtk_callback_info *cbi)
{
	int ret = 0;
        if (cbi->event->type == NSFB_EVENT_KEY_DOWN)
		ret = fbtk_post_callback(cbi->context, FBTK_CBT_SCROLLY, 1);
	return ret;
}

static int
vscrollarea_click(fbtk_widget_t *widget, fbtk_callback_info *cbi)
{
        int vscroll;
        int vpos;
	int ret = 0;

        if (cbi->event->type != NSFB_EVENT_KEY_DOWN)
        	return 0;

        vscroll = ((widget->height - 4) * widget->u.scroll.pct) / 100 ;
        vpos = ((widget->height - 4) * widget->u.scroll.pos) / 100 ;

	if (cbi->y < vpos) {
                /* above bar */
		ret = fbtk_post_callback(cbi->context, FBTK_CBT_SCROLLY, -1);
	} else if (cbi->y > (vpos + vscroll)) {
                /* below bar */
		ret = fbtk_post_callback(cbi->context, FBTK_CBT_SCROLLY, 1);
	}
	return ret;
}


fbtk_widget_t *
fbtk_create_vscroll(fbtk_widget_t *window, 
		    int x, int y, 
		    int width, int height, 
		    colour fg, 
		    colour bg,
		    fbtk_callback callback,
		    void *context)
{
        fbtk_widget_t *neww = new_widget(FB_WIDGET_TYPE_VSCROLL);

        neww->x = x;
        neww->y = y + scrollu.height;
        neww->width = width;
        neww->height = height - scrollu.height - scrolld.height;
        neww->fg = fg;
        neww->bg = bg;

	fbtk_set_handler(neww, FBTK_CBT_REDRAW, vscroll_redraw, NULL);

	fbtk_set_handler(neww, FBTK_CBT_CLICK, vscrollarea_click, neww);

	fbtk_set_handler(neww, FBTK_CBT_SCROLLY, callback, context);

	neww->u.scroll.btnul = fbtk_create_button(window, x + (width - scrollu.width) / 2, y, fg, &scrollu, vscrollu_click, neww);

	neww->u.scroll.btndr = fbtk_create_button(window, x + (width - scrolld.width) / 2, y + height - scrolld.height, fg, &scrolld, vscrolld_click, neww);


        return add_widget_to_window(window, neww);
}

/* Horizontal scroll widget */

static int
hscroll_redraw(fbtk_widget_t *widget, fbtk_callback_info *cbi)
{
        int hscroll;
        int hpos;
        nsfb_bbox_t bbox;
        nsfb_bbox_t rect;
	fbtk_widget_t *root = get_root_widget(widget);

        fbtk_get_bbox(widget, &bbox);

        nsfb_claim(root->u.root.fb, &bbox);

        rect = bbox;

	/* background */
        nsfb_plot_rectangle_fill(root->u.root.fb, &rect, widget->bg);

	/* scroll well */
        rect.x0 = bbox.x0 + 1;
        rect.y0 = bbox.y0 + 2;
        rect.x1 = bbox.x1 - 2;
        rect.y1 = bbox.y1 - 3;
        nsfb_plot_rectangle_fill(root->u.root.fb, &rect, widget->fg);

	/* scroll well outline */
        nsfb_plot_rectangle(root->u.root.fb, &rect, 1, 0xFF999999, false, false);

        hscroll = ((widget->width - 4) * widget->u.scroll.pct) / 100 ;
        hpos = ((widget->width - 4) * widget->u.scroll.pos) / 100 ;

        LOG(("hscroll %d",hscroll));

        rect.x0 = bbox.x0 + 3 + hpos;
        rect.y0 = bbox.y0 + 5;
        rect.x1 = bbox.x0 + hscroll + hpos;
        rect.y1 = bbox.y0 + widget->height - 5;

        nsfb_plot_rectangle_fill(root->u.root.fb, &rect, widget->bg);

        nsfb_update(root->u.root.fb, &bbox);

        return 0;
}

static int
hscrolll_click(fbtk_widget_t *widget, fbtk_callback_info *cbi)
{
	int ret = 0;
        if (cbi->event->type == NSFB_EVENT_KEY_DOWN)
		ret = fbtk_post_callback(cbi->context, FBTK_CBT_SCROLLX, -1);
	return ret;
}

static int
hscrollr_click(fbtk_widget_t *widget, fbtk_callback_info *cbi)
{
	int ret = 0;
        if (cbi->event->type == NSFB_EVENT_KEY_DOWN)
		ret = fbtk_post_callback(cbi->context, FBTK_CBT_SCROLLX, 1);
	return ret;
}

static int
hscrollarea_click(fbtk_widget_t *widget, fbtk_callback_info *cbi)
{
        int hscroll;
        int hpos;
	int ret;

        if (cbi->event->type != NSFB_EVENT_KEY_DOWN)
        	return 0;

        hscroll = ((widget->width - 4) * widget->u.scroll.pct) / 100 ;
        hpos = ((widget->width - 4) * widget->u.scroll.pos) / 100 ;

	if (cbi->x < hpos) {
                /* above bar */
		ret = fbtk_post_callback(cbi->context, FBTK_CBT_SCROLLX, -1);
	} else if (cbi->x > (hpos + hscroll)) {
                /* below bar */
		ret = fbtk_post_callback(cbi->context, FBTK_CBT_SCROLLX, 1);
	}
	return ret;
}

fbtk_widget_t *
fbtk_create_hscroll(fbtk_widget_t *window, 
		    int x, int y, 
		    int width, int height, 
		    colour fg, 
		    colour bg,
		    fbtk_callback callback,
		    void *context)
{
        fbtk_widget_t *neww = new_widget(FB_WIDGET_TYPE_HSCROLL);

        neww->x = x + scrolll.width;
        neww->y = y;
        neww->width = width - scrolll.width - scrollr.width;
        neww->height = height;
        neww->fg = fg;
        neww->bg = bg;

	fbtk_set_handler(neww, FBTK_CBT_REDRAW, hscroll_redraw, NULL);
	fbtk_set_handler(neww, FBTK_CBT_CLICK, hscrollarea_click, neww);
	fbtk_set_handler(neww, FBTK_CBT_SCROLLX, callback, context);

	neww->u.scroll.btnul = fbtk_create_button(window, x, y + ((height - scrolll.height) / 2), fg, &scrolll, hscrolll_click, neww);

	neww->u.scroll.btndr = fbtk_create_button(window, x + width - scrollr.width, y + ((height - scrolll.height) / 2), fg, &scrollr, hscrollr_click, neww);

        return add_widget_to_window(window, neww);
}


void
fbtk_set_scroll(fbtk_widget_t *widget, int pct)
{
        if (widget == NULL)
                return;

        if ((widget->type == FB_WIDGET_TYPE_HSCROLL) ||
            (widget->type == FB_WIDGET_TYPE_VSCROLL)) {

                widget->u.scroll.pct = pct;
                fbtk_request_redraw(widget);
        }
}

void
fbtk_set_scroll_pos(fbtk_widget_t *widget, int pos)
{
        if (widget == NULL)
                return;

        if ((widget->type == FB_WIDGET_TYPE_HSCROLL) ||
            (widget->type == FB_WIDGET_TYPE_VSCROLL)) {

        widget->u.scroll.pos = pos;

        fbtk_request_redraw(widget);
        }
}