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"
33
34/* exported interface documented in cw_helper.h */
35nserror cw_helper_scroll_visible(struct core_window *cw_h, const struct rect *r)
36{
37 nserror err;
38 int height;
39 int width;
40 int x0;
41 int y0;
42 int x1;
43 int y1;
44
45 assert(cw_h != NULL);
46
47 err = guit->corewindow->get_dimensions(cw_h, &width, &height);
48 if (err != NSERROR_OK) {
49 return err;
50 }
51
52 guit->corewindow->get_scroll(cw_h, &x0, &y0);
53 if (err != NSERROR_OK) {
54 return err;
55 }
56
57 y1 = y0 + height;
58 x1 = x0 + width;
59
60 if (r->y1 > y1) {
61 /* The bottom of the rectangle is off the bottom of the
62 * window, so scroll down to fit it
63 */
64 y0 = r->y1 - height;
65 }
66 if (r->y0 < y0) {
67 /* The top of the rectangle is off the top of the window,
68 * so scroll up to fit it
69 */
70 y0 = r->y0;
71 }
72 if (r->x1 > x1) {
73 /* The right of the rectangle is off the right of the window
74 * so scroll right to fit it
75 */
76 x0 = r->x1 - width;
77 }
78 if (r->x0 < x0) {
79 /* The left of the rectangle is off the left of the window
80 * so scroll left to fit it
81 */
82 x0 = r->x0;
83 }
84
85 return guit->corewindow->set_scroll(cw_h, x0, y0);
86}
Interface to core window handling.
nserror cw_helper_scroll_visible(struct core_window *cw_h, const struct rect *r)
Scroll a core window to make the given rectangle visible.
Definition: cw_helper.c:35
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
struct netsurf_table * guit
The global interface table.
Definition: gui_factory.c:50
Interface to core interface table.
Browser interfaces.
int width
Definition: gui.c:160
int height
Definition: gui.c:161
nserror(* get_dimensions)(const struct core_window *cw, int *width, int *height)
Get window viewport dimensions.
Definition: core_window.h:115
nserror(* get_scroll)(const struct core_window *cw, int *x, int *y)
Get the current scroll offsets.
Definition: core_window.h:105
nserror(* set_scroll)(struct core_window *cw, int x, int y)
Scroll the window to given scroll offsets.
Definition: core_window.h:95
struct core_window_table * corewindow
Core window table.
Definition: gui_table.h:75
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.