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(ncwin->core.cb_table,
164 (struct core_window *)ncwin,
165 bw,
166 &ncwin->session);
167 if (res != NSERROR_OK) {
168 free(ncwin);
169 return res;
170 }
171
172 /* memoise window so it can be represented when necessary
173 * instead of recreating every time.
174 */
175 *win_out = ncwin;
176
177 return NSERROR_OK;
178}
179
180
181/* exported interface documented in windows/local_history.h */
183nsw32_local_history_present(HWND hWndParent, struct browser_window *bw)
184{
185 nserror res;
186 HINSTANCE hInstance;
187 RECT parentr;
188 int width, height;
189 int margin = 50;
190
191 hInstance = (HINSTANCE)GetWindowLongPtr(hWndParent, GWLP_HINSTANCE);
192
193 res = nsw32_local_history_init(hInstance, bw, &local_history_window);
194 if (res == NSERROR_OK) {
195 GetWindowRect(hWndParent, &parentr);
196
197 /* resize history widget ensureing the drawing area is
198 * no larger than parent window
199 */
201 &width,
202 &height);
203 width += margin;
204 height += margin;
205 if ((parentr.right - parentr.left - margin) < width) {
206 width = parentr.right - parentr.left - margin;
207 }
208 if ((parentr.bottom - parentr.top - margin) < height) {
209 height = parentr.bottom - parentr.top - margin;
210 }
211 SetWindowPos(local_history_window->core.hWnd,
212 HWND_TOP,
213 parentr.left + (margin/2),
214 parentr.top + (margin/2),
215 width,
216 height,
217 SWP_SHOWWINDOW);
219 }
220 return res;
221}
222
223
224/* exported interface documented in windows/local_history.h */
226{
227 nserror res = NSERROR_OK;
228
229 if (local_history_window != NULL) {
231
233 }
234
235 return res;
236}
237
238/* exported interface documented in windows/local_history.h */
240{
241 nserror res;
242
243 if (local_history_window == NULL) {
244 return NSERROR_OK;
245 }
246
248 if (res == NSERROR_OK) {
250 DestroyWindow(local_history_window->core.hWnd);
253 }
254
255 return res;
256}
nserror local_history_init(struct core_window_callback_table *cw_t, void *core_window_handle, struct browser_window *bw, struct local_history_session **session)
Initialise the local history.
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_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.
Definition: mouse.h:43
Target independent plotting interface.
Interface to key press operations.
#define ShowWindow(...)
Definition: os3support.h:172
int width
Definition: gui.c:159
int height
Definition: gui.c:160
Browser window data.
local history viewer context
Definition: local_history.c:50
nsw32 core window state
Definition: corewindow.h:27
nserror(* close)(struct nsw32_corewindow *nsw32_cw)
callback for window close event
Definition: corewindow.h:83
const char * title
window title
Definition: corewindow.h:38
struct core_window_callback_table * cb_table
table of callbacks for core window operations
Definition: corewindow.h:44
nserror(* key)(struct nsw32_corewindow *nsw32_cw, uint32_t nskey)
callback for keypress on nsw32 core window
Definition: corewindow.h:64
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:75
HWND hWnd
window handle
Definition: corewindow.h:29
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:53
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:517
nserror nsw32_corewindow_fini(struct nsw32_corewindow *nsw32_cw)
finalise elements of nsw32 core window.
Definition: corewindow.c:576
const struct plotter_table win_plotters
win32 API plot operation table
Definition: plot.c:1040