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 win32 local history interface.
22 */
23
24#include <stdint.h>
25#include <stdlib.h>
26#include <windows.h>
27
28#include "utils/log.h"
29#include "utils/nsoption.h"
30#include "netsurf/keypress.h"
31#include "netsurf/plotters.h"
33
34#include "windows/plot.h"
35#include "windows/corewindow.h"
37
38
41
43};
44
46
47/**
48 * callback for keypress on local_history window
49 *
50 * \param nsw32_cw The nsw32 core window structure.
51 * \param nskey The netsurf key code
52 * \return NSERROR_OK on success otherwise apropriate error code
53 */
54static nserror
55nsw32_local_history_key(struct nsw32_corewindow *nsw32_cw, uint32_t nskey)
56{
58
59 lhw = (struct nsw32_local_history_window *)nsw32_cw;
60
61 if (local_history_keypress(lhw->session,nskey)) {
62 return NSERROR_OK;
63 }
65}
66
67/**
68 * callback for mouse action on local_history window
69 *
70 * \param nsw32_cw The nsw32 core window structure.
71 * \param mouse_state netsurf mouse state on event
72 * \param x location of event
73 * \param y location of event
74 * \return NSERROR_OK on success otherwise apropriate error code
75 */
76static nserror
78 browser_mouse_state mouse_state,
79 int x, int y)
80{
82
83 lhw = (struct nsw32_local_history_window *)nsw32_cw;
84
85 local_history_mouse_action(lhw->session, mouse_state, x, y);
86
87 return NSERROR_OK;
88}
89
90/**
91 * callback on draw event for local_history window
92 *
93 * \param nsw32_cw The nsw32 core window structure.
94 * \param scrollx The horizontal scroll offset.
95 * \param scrolly The vertical scroll offset.
96 * \param r The rectangle of the window that needs updating.
97 * \return NSERROR_OK on success otherwise apropriate error code
98 */
99static nserror
101 int scrollx,
102 int scrolly,
103 struct rect *r)
104{
105 struct nsw32_local_history_window *lhw;
106 struct redraw_context ctx = {
107 .interactive = true,
108 .background_images = true,
109 .plot = &win_plotters
110 };
111
112 lhw = (struct nsw32_local_history_window *)nsw32_cw;
113
114 local_history_redraw(lhw->session, -scrollx, -scrolly, r, &ctx);
115
116 return NSERROR_OK;
117}
118
119
120static nserror
122{
123 ShowWindow(nsw32_cw->hWnd, SW_HIDE);
124
125 return NSERROR_OK;
126}
127
128/**
129 * Creates the window for the local_history tree.
130 *
131 * \return NSERROR_OK on success else appropriate error code on faliure.
132 */
133static nserror
134nsw32_local_history_init(HINSTANCE hInstance,
135 struct browser_window *bw,
136 struct nsw32_local_history_window **win_out)
137{
138 struct nsw32_local_history_window *ncwin;
139 nserror res;
140
141 if ((*win_out) != NULL) {
142 res = local_history_set((*win_out)->session, bw);
143 return res;
144 }
145
146 ncwin = calloc(1, sizeof(*ncwin));
147 if (ncwin == NULL) {
148 return NSERROR_NOMEM;
149 }
150
151 ncwin->core.title = "NetSurf Local History";
156
157 res = nsw32_corewindow_init(hInstance, NULL, &ncwin->core);
158 if (res != NSERROR_OK) {
159 free(ncwin);
160 return res;
161 }
162
163 res = local_history_init((struct core_window *)ncwin,
164 bw,
165 &ncwin->session);
166 if (res != NSERROR_OK) {
167 free(ncwin);
168 return res;
169 }
170
171 /* memoise window so it can be represented when necessary
172 * instead of recreating every time.
173 */
174 *win_out = ncwin;
175
176 return NSERROR_OK;
177}
178
179
180/* exported interface documented in windows/local_history.h */
182nsw32_local_history_present(HWND hWndParent, struct browser_window *bw)
183{
184 nserror res;
185 HINSTANCE hInstance;
186 RECT parentr;
187 int width, height;
188 int margin = 50;
189
190 hInstance = (HINSTANCE)GetWindowLongPtr(hWndParent, GWLP_HINSTANCE);
191
192 res = nsw32_local_history_init(hInstance, bw, &local_history_window);
193 if (res == NSERROR_OK) {
194 GetWindowRect(hWndParent, &parentr);
195
196 /* resize history widget ensureing the drawing area is
197 * no larger than parent window
198 */
200 &width,
201 &height);
202 width += margin;
203 height += margin;
204 if ((parentr.right - parentr.left - margin) < width) {
205 width = parentr.right - parentr.left - margin;
206 }
207 if ((parentr.bottom - parentr.top - margin) < height) {
208 height = parentr.bottom - parentr.top - margin;
209 }
210 SetWindowPos(local_history_window->core.hWnd,
211 HWND_TOP,
212 parentr.left + (margin/2),
213 parentr.top + (margin/2),
214 width,
215 height,
216 SWP_SHOWWINDOW);
218 }
219 return res;
220}
221
222
223/* exported interface documented in windows/local_history.h */
225{
226 nserror res = NSERROR_OK;
227
228 if (local_history_window != NULL) {
230
232 }
233
234 return res;
235}
236
237/* exported interface documented in windows/local_history.h */
239{
240 nserror res;
241
242 if (local_history_window == NULL) {
243 return NSERROR_OK;
244 }
245
247 if (res == NSERROR_OK) {
249 DestroyWindow(local_history_window->core.hWnd);
252 }
253
254 return res;
255}
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.
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 nsw32_local_history_close(struct nsw32_corewindow *nsw32_cw)
static nserror nsw32_local_history_init(HINSTANCE hInstance, struct browser_window *bw, struct nsw32_local_history_window **win_out)
Creates the window for the local_history tree.
static nserror nsw32_local_history_mouse(struct nsw32_corewindow *nsw32_cw, browser_mouse_state mouse_state, int x, int y)
callback for mouse action on local_history window
Definition: local_history.c:77
static nserror nsw32_local_history_key(struct nsw32_corewindow *nsw32_cw, uint32_t nskey)
callback for keypress on local_history window
Definition: local_history.c:55
nserror nsw32_local_history_hide(void)
hide the local history window.
static nserror nsw32_local_history_draw(struct nsw32_corewindow *nsw32_cw, int scrollx, int scrolly, struct rect *r)
callback on draw event for local_history window
static struct nsw32_local_history_window * local_history_window
Definition: local_history.c:45
nserror nsw32_local_history_present(HWND hWndParent, struct browser_window *bw)
make the local history window visible.
nserror nsw32_local_history_finalise(void)
Destroys the local history window and performs any other necessary cleanup actions.
Interface to win32 local history manager using nsw32 core window.
browser_mouse_state
Mouse state: 1 is primary mouse button.
Definition: mouse.h:52
Target independent plotting interface.
Interface to key press operations.
#define ShowWindow(...)
Definition: os3support.h:172
int width
Definition: gui.c:160
int height
Definition: gui.c:161
Browser window data.
local history viewer context
Definition: local_history.c:50
nsw32 core window state
Definition: corewindow.h:29
nserror(* close)(struct nsw32_corewindow *nsw32_cw)
callback for window close event
Definition: corewindow.h:82
const char * title
window title
Definition: corewindow.h:40
nserror(* key)(struct nsw32_corewindow *nsw32_cw, uint32_t nskey)
callback for keypress on nsw32 core window
Definition: corewindow.h:63
nserror(* mouse)(struct nsw32_corewindow *nsw32_cw, browser_mouse_state mouse_state, int x, int y)
callback for mouse event on nsw32 core window
Definition: corewindow.h:74
HWND hWnd
window handle
Definition: corewindow.h:31
nserror(* draw)(struct nsw32_corewindow *nsw32_cw, int scrollx, int scrolly, struct rect *r)
callback to draw on drawable area of nsw32 core window
Definition: corewindow.h:52
struct local_history_session * session
Definition: local_history.c:42
struct nsw32_corewindow core
Definition: local_history.c:40
Rectangle coordinates.
Definition: types.h:40
Redraw context.
Definition: plotters.h:51
bool interactive
Redraw to show interactive features.
Definition: plotters.h:59
Option reading and saving interface.
nserror nsw32_corewindow_init(HINSTANCE hInstance, HWND hWndParent, struct nsw32_corewindow *nsw32_cw)
initialise elements of nsw32 core window.
Definition: corewindow.c:519
nserror nsw32_corewindow_fini(struct nsw32_corewindow *nsw32_cw)
finalise elements of nsw32 core window.
Definition: corewindow.c:577
const struct plotter_table win_plotters
win32 API plot operation table
Definition: plot.c:1040