NetSurf
local_history.c
Go to the documentation of this file.
1/*
2 * Copyright 2017 Vincent Sanders <vince@netsurf-browser.org>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * \file
21 * Implementation of GTK local history manager.
22 */
23
24#include <stdint.h>
25#include <stdlib.h>
26#include <gtk/gtk.h>
27
28#include "utils/log.h"
29#include "netsurf/keypress.h"
30#include "netsurf/plotters.h"
32
33#include "gtk/compat.h"
34#include "gtk/plotters.h"
35#include "gtk/resources.h"
36#include "gtk/corewindow.h"
37#include "gtk/local_history.h"
38#include "gtk/scaffolding.h"
39
42
43 GtkBuilder *builder;
44
45 GtkWindow *wnd;
46
48};
49
51
52
53
54/**
55 * callback for mouse action on local history window
56 *
57 * \param nsgtk_cw The nsgtk core window structure.
58 * \param mouse_state netsurf mouse state on event
59 * \param x location of event
60 * \param y location of event
61 * \return NSERROR_OK on success otherwise apropriate error code
62 */
63static nserror
65 browser_mouse_state mouse_state,
66 int x, int y)
67{
69 /* technically degenerate container of */
70 lhw = (struct nsgtk_local_history_window *)nsgtk_cw;
71
72 local_history_mouse_action(lhw->session, mouse_state, x, y);
73
74 return NSERROR_OK;
75}
76
77
78/**
79 * callback for keypress on local history window
80 *
81 * \param nsgtk_cw The nsgtk core window structure.
82 * \param nskey The netsurf key code
83 * \return NSERROR_OK on success otherwise apropriate error code
84 */
85static nserror
86nsgtk_local_history_key(struct nsgtk_corewindow *nsgtk_cw, uint32_t nskey)
87{
89 /* technically degenerate container of */
90 lhw = (struct nsgtk_local_history_window *)nsgtk_cw;
91
92 if (local_history_keypress(lhw->session, nskey)) {
93 return NSERROR_OK;
94 }
96}
97
98
99/**
100 * callback on draw event for local history window
101 *
102 * \param nsgtk_cw The nsgtk core window structure.
103 * \param r The rectangle of the window that needs updating.
104 * \return NSERROR_OK on success otherwise apropriate error code
105 */
106static nserror
108{
109 struct redraw_context ctx = {
110 .interactive = true,
111 .background_images = true,
112 .plot = &nsgtk_plotters
113 };
114 struct nsgtk_local_history_window *lhw;
115
116 /* technically degenerate container of */
117 lhw = (struct nsgtk_local_history_window *)nsgtk_cw;
118
119 ctx.plot->clip(&ctx, r);
120 local_history_redraw(lhw->session, 0, 0, r, &ctx);
121
122 return NSERROR_OK;
123}
124
125/**
126 * Creates the window for the local history view.
127 *
128 * \return NSERROR_OK on success else appropriate error code on faliure.
129 */
130static nserror
132 struct nsgtk_local_history_window **win_out)
133{
134 struct nsgtk_local_history_window *ncwin;
135 nserror res;
136
137 /* memoise window so it can be represented when necessary
138 * instead of recreating every time.
139 */
140 if ((*win_out) != NULL) {
141 res = local_history_set((*win_out)->session, bw);
142 return res;
143 }
144
145 ncwin = calloc(1, sizeof(*ncwin));
146 if (ncwin == NULL) {
147 return NSERROR_NOMEM;
148 }
149
150 res = nsgtk_builder_new_from_resname("localhistory", &ncwin->builder);
151 if (res != NSERROR_OK) {
152 NSLOG(netsurf, INFO, "Local history UI builder init failed");
153 free(ncwin);
154 return res;
155 }
156
157 gtk_builder_connect_signals(ncwin->builder, NULL);
158
159 ncwin->wnd = GTK_WINDOW(gtk_builder_get_object(ncwin->builder,
160 "wndHistory"));
161
162 /* Configure for transient behaviour */
163 gtk_window_set_type_hint(GTK_WINDOW(ncwin->wnd),
164 GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU);
165 gtk_window_set_modal(GTK_WINDOW(ncwin->wnd), TRUE);
166
167
168 ncwin->core.scrolled = GTK_SCROLLED_WINDOW(
169 gtk_builder_get_object(ncwin->builder,
170 "HistoryScrolled"));
171
172 ncwin->core.drawing_area = GTK_DRAWING_AREA(
173 gtk_builder_get_object(ncwin->builder,
174 "HistoryDrawingArea"));
175
176 /* make the delete event hide the window */
177 g_signal_connect(G_OBJECT(ncwin->wnd),
178 "delete_event",
179 G_CALLBACK(gtk_widget_hide_on_delete),
180 NULL);
181 /* Ditto if we lose the grab */
182 g_signal_connect(G_OBJECT(ncwin->wnd),
183 "grab-broken-event",
184 G_CALLBACK(gtk_widget_hide_on_delete),
185 ncwin);
186 /* Handle button press events */
187 g_signal_connect(G_OBJECT(ncwin->wnd),
188 "button-press-event",
189 G_CALLBACK(gtk_widget_hide_on_delete),
190 ncwin);
191
195
196 res = nsgtk_corewindow_init(&ncwin->core);
197 if (res != NSERROR_OK) {
198 free(ncwin);
199 return res;
200 }
201
202 res = local_history_init((struct core_window *)ncwin,
203 bw,
204 &ncwin->session);
205 if (res != NSERROR_OK) {
206 free(ncwin);
207 return res;
208 }
209
210 *win_out = ncwin;
211
212 return NSERROR_OK;
213}
214
215
216/* exported function documented gtk/history.h */
218 struct browser_window *bw)
219{
220 nserror res;
221 int prnt_width, prnt_height;
222 int width, height;
224 if (res == NSERROR_OK) {
225 gtk_window_group_add_window(gtk_window_get_group(parent),
227 gtk_window_set_transient_for(local_history_window->wnd, parent);
228 gtk_window_set_screen(local_history_window->wnd,
229 gtk_widget_get_screen(GTK_WIDGET(parent)));
230
231 gtk_window_get_size(parent, &prnt_width, &prnt_height);
232
233 /* resize history widget ensureing the drawing area is
234 * no larger than parent window
235 */
237 &width,
238 &height);
239 if (width > prnt_width) {
240 width = prnt_width;
241 }
242 if (height > prnt_height) {
243 height = prnt_height;
244 }
245 gtk_window_resize(local_history_window->wnd, width, height);
246
247 /* Attempt to place the window in the right place */
249
250 gtk_widget_show(GTK_WIDGET(local_history_window->wnd));
251 gtk_widget_grab_focus(GTK_WIDGET(local_history_window->wnd));
252
254 }
255
256 return res;
257}
258
259
260/* exported function documented gtk/history.h */
262{
263 nserror res = NSERROR_OK;
264
265 if (local_history_window != NULL) {
266 gtk_widget_hide(GTK_WIDGET(local_history_window->wnd));
267
269 }
270
271 return res;
272}
273
274
275/* exported function documented gtk/history.h */
277{
278 nserror res;
279
280 if (local_history_window == NULL) {
281 return NSERROR_OK;
282 }
283
285 if (res == NSERROR_OK) {
287 gtk_widget_destroy(GTK_WIDGET(local_history_window->wnd));
288 g_object_unref(G_OBJECT(local_history_window->builder));
291 }
292
293 return res;
294
295}
296
297/* exported function documented gtk/history.h */
299{
300 NSLOG(netsurf, INFO, "x=%d y=%d", x, y);
301
302 gtk_window_move(local_history_window->wnd, x, y);
303}
Compatibility functions for older GTK versions (interface)
nserror local_history_redraw(struct local_history_session *session, int x, int y, struct rect *clip, const struct redraw_context *ctx)
Redraw the local history.
nserror local_history_scroll_to_cursor(struct local_history_session *session)
Scroll the local history window to ensure the current cursor is shown.
nserror local_history_get_size(struct local_history_session *session, int *width, int *height)
get size of local history content area.
nserror local_history_set(struct local_history_session *session, struct browser_window *bw)
Change the browser window to draw local history for.
nserror local_history_fini(struct local_history_session *session)
Finalise the local history.
bool local_history_keypress(struct local_history_session *session, uint32_t key)
Key press handling.
nserror local_history_init(void *core_window_handle, struct browser_window *bw, struct local_history_session **session)
Initialise the local history.
nserror local_history_mouse_action(struct local_history_session *session, enum browser_mouse_state mouse, int x, int y)
Handles all kinds of mouse action.
wimp_w parent
Definition: dialog.c:88
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_NOT_IMPLEMENTED
Functionality is not implemented.
Definition: errors.h:61
@ NSERROR_NOMEM
Memory exhaustion.
Definition: errors.h:32
@ NSERROR_OK
No error.
Definition: errors.h:30
static nserror nsgtk_local_history_init(struct browser_window *bw, struct nsgtk_local_history_window **win_out)
Creates the window for the local history view.
nserror nsgtk_local_history_hide(void)
hide the local history window from being visible.
void nsgtk_local_history_set_position(int x, int y)
set the local history window position.
static struct nsgtk_local_history_window * local_history_window
Definition: local_history.c:50
nserror nsgtk_local_history_present(GtkWindow *parent, struct browser_window *bw)
make the local history window visible.
static nserror nsgtk_local_history_mouse(struct nsgtk_corewindow *nsgtk_cw, browser_mouse_state mouse_state, int x, int y)
callback for mouse action on local history window
Definition: local_history.c:64
nserror nsgtk_local_history_destroy(void)
Destroys the local history window and performs any other necessary cleanup actions.
static nserror nsgtk_local_history_key(struct nsgtk_corewindow *nsgtk_cw, uint32_t nskey)
callback for keypress on local history window
Definition: local_history.c:86
static nserror nsgtk_local_history_draw(struct nsgtk_corewindow *nsgtk_cw, struct rect *r)
callback on draw event for local history window
Interface to GTK local history manager.
Target independent plotting GTK+ interface.
nserror nsgtk_corewindow_init(struct nsgtk_corewindow *nsgtk_cw)
initialise elements of gtk core window.
Definition: corewindow.c:733
nserror nsgtk_corewindow_fini(struct nsgtk_corewindow *nsgtk_cw)
finalise elements of gtk core window.
Definition: corewindow.c:786
const struct plotter_table nsgtk_plotters
GTK plotter table.
Definition: plotters.c:647
browser_mouse_state
Mouse state: 1 is primary mouse button.
Definition: mouse.h:52
Target independent plotting interface.
Interface to key press operations.
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
nserror nsgtk_builder_new_from_resname(const char *resname, GtkBuilder **builder_out)
Create gtk builder object for the named ui resource.
Definition: resources.c:526
Interface to gtk builtin resource handling.
int width
Definition: gui.c:160
int height
Definition: gui.c:161
nserror nsgtk_scaffolding_position_local_history(struct nsgtk_scaffolding *gs)
Position the local-history popup in the right place.
Definition: scaffolding.c:1600
struct nsgtk_scaffolding * nsgtk_current_scaffolding(void)
Obtain the most recently used scaffolding element.
Definition: scaffolding.c:1473
Browser window data.
local history viewer context
Definition: local_history.c:50
nsgtk core window state
Definition: corewindow.h:41
nserror(* key)(struct nsgtk_corewindow *nsgtk_cw, uint32_t nskey)
callback for keypress on nsgtk core window
Definition: corewindow.h:76
nserror(* draw)(struct nsgtk_corewindow *nsgtk_cw, struct rect *r)
callback to draw on drawable area of nsgtk core window
Definition: corewindow.h:65
GtkScrolledWindow * scrolled
scrollable area drawing area is within
Definition: corewindow.h:46
nserror(* mouse)(struct nsgtk_corewindow *nsgtk_cw, browser_mouse_state mouse_state, int x, int y)
callback for mouse event on nsgtk core window
Definition: corewindow.h:87
GtkDrawingArea * drawing_area
GTK drawable widget.
Definition: corewindow.h:44
struct nsgtk_corewindow core
Definition: local_history.c:41
struct local_history_session * session
Definition: local_history.c:47
nserror(* clip)(const struct redraw_context *ctx, const struct rect *clip)
Sets a clip rectangle for subsequent plot operations.
Definition: plotters.h:111
Rectangle coordinates.
Definition: types.h:40
Redraw context.
Definition: plotters.h:51
const struct plotter_table * plot
Current plot operation table.
Definition: plotters.h:73
bool interactive
Redraw to show interactive features.
Definition: plotters.h:59