NetSurf
cw_helper.c
Go to the documentation of this file.
1/*
2 * Copyright 2019 Daniel Silverstone <dsilvers@netsurf-browser.org>
3 * Copyright 2019 Michael Drake <tlsa@netsurf-browser.org>
4 *
5 * This file is part of NetSurf, http://www.netsurf-browser.org/
6 *
7 * NetSurf is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * NetSurf is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/**
21 * \file
22 *
23 * Helpers to simplify core use of corewindow.
24 */
25
26#include "utils/errors.h"
27#include "netsurf/browser.h"
28#include "netsurf/core_window.h"
29#include "netsurf/types.h"
30#include "css/utils.h"
31#include "desktop/cw_helper.h"
32
33/* exported interface documented in cw_helper.h */
35 const struct core_window_callback_table *cw_t,
36 struct core_window *cw_h,
37 const struct rect *r)
38{
39 nserror err;
40 int height;
41 int width;
42 int x0;
43 int y0;
44 int x1;
45 int y1;
46
47 assert(cw_t != NULL);
48 assert(cw_h != NULL);
49 assert(cw_t->get_scroll != NULL);
50 assert(cw_t->set_scroll != NULL);
51 assert(cw_t->get_window_dimensions != NULL);
52
53 err = cw_t->get_window_dimensions(cw_h, &width, &height);
54 if (err != NSERROR_OK) {
55 return err;
56 }
57
58 cw_t->get_scroll(cw_h, &x0, &y0);
59 if (err != NSERROR_OK) {
60 return err;
61 }
62
63 y1 = y0 + height;
64 x1 = x0 + width;
65
66 if (r->y1 > y1) {
67 /* The bottom of the rectangle is off the bottom of the
68 * window, so scroll down to fit it
69 */
70 y0 = r->y1 - height;
71 }
72 if (r->y0 < y0) {
73 /* The top of the rectangle is off the top of the window,
74 * so scroll up to fit it
75 */
76 y0 = r->y0;
77 }
78 if (r->x1 > x1) {
79 /* The right of the rectangle is off the right of the window
80 * so scroll right to fit it
81 */
82 x0 = r->x1 - width;
83 }
84 if (r->x0 < x0) {
85 /* The left of the rectangle is off the left of the window
86 * so scroll left to fit it
87 */
88 x0 = r->x0;
89 }
90
91 return cw_t->set_scroll(cw_h, x0, y0);
92}
Interface to core window handling.
nserror cw_helper_scroll_visible(const struct core_window_callback_table *cw_t, struct core_window *cw_h, const struct rect *r)
Scroll a core window to make the given rectangle visible.
Definition: cw_helper.c:34
Helpers to simplify core use of corewindow.
Error codes.
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_OK
No error.
Definition: errors.h:30
static struct core_window_callback_table cw_t
Declare Core Window Callbacks:
Definition: treeview.c:534
Browser interfaces.
int width
Definition: gui.c:159
int height
Definition: gui.c:160
Callbacks to achieve various core window functionality.
Definition: core_window.h:51
nserror(* get_scroll)(const struct core_window *cw, int *x, int *y)
Get the current scroll offsets.
Definition: core_window.h:103
nserror(* get_window_dimensions)(const struct core_window *cw, int *width, int *height)
Get window viewport dimensions.
Definition: core_window.h:113
nserror(* set_scroll)(struct core_window *cw, int x, int y)
Scroll the window to given scroll offsets.
Definition: core_window.h:93
Rectangle coordinates.
Definition: types.h:40
int x0
Definition: types.h:41
int y0
Top left.
Definition: types.h:41
int x1
Definition: types.h:42
int y1
Bottom right.
Definition: types.h:42
NetSurf types.