summaryrefslogtreecommitdiff
path: root/frontends/fltk/window.cpp
blob: 19aa265565e00191cb7e113e0ea62357b9933f15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * Copyright 2021 Vincent Sanders <vince@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * \file
 * Implementation of netsurf window (widget) for fltk.
 */

#include <stddef.h>
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/fl_draw.H>

extern "C" {

#include "utils/errors.h"
#include "netsurf/types.h"
#include "netsurf/mouse.h"
#include "netsurf/window.h"
#include "netsurf/plotters.h"
#include "netsurf/content.h"
#include "netsurf/browser_window.h"
#include "netsurf/mouse.h"

}

#include "fltk/window.h"
#include "fltk/plotters.h"

extern bool nsfltk_done;

class NS_Widget : public Fl_Widget
{
private:
	struct browser_window *mbw;

protected:
	void draw();
	int handle(int event);

public:
	NS_Widget(int X,int Y,int W,int H, struct browser_window *bw)
		: Fl_Widget(X,Y,W,H), mbw(bw) {}
};


class NS_Window : public Fl_Double_Window
{
private:
	struct browser_window *mbw;
	NS_Widget *mnswidget;

	void close_callback(Fl_Widget *w);
	static void static_close_callback(Fl_Widget *w, void *f) { ((NS_Window *)f)->close_callback(w); }

public:
	NS_Window(int W, int H, struct browser_window *bw)
		: Fl_Double_Window(W,H), mbw(bw) {
		this->callback(static_close_callback, (void *)this);
		mnswidget = new NS_Widget(0, 0, W, H, bw);
		this->end();
	}
	~NS_Window() { nsfltk_done=true; }

	NS_Widget *get_nswidget() { return mnswidget; }
};

struct gui_window {
	struct browser_window *bw;
	NS_Window *window;
};

/**
 * method to handle events on the netsurf browsing widget
 */
int NS_Widget::handle(int event)
{
	int state = BROWSER_MOUSE_HOVER;
	int button;

	switch (event) {
	case FL_PUSH:
		button = Fl::event_button();
		if (button == FL_LEFT_MOUSE) {
			state |= BROWSER_MOUSE_PRESS_1;
		}
		browser_window_mouse_click(mbw,
					   (browser_mouse_state)state,
					   Fl::event_x() - x(),
					   Fl::event_y() - y());
		return 1;

	case FL_RELEASE:
		button = Fl::event_button();
		if (button == FL_LEFT_MOUSE) {
			state |= BROWSER_MOUSE_CLICK_1;

		}
		browser_window_mouse_click(mbw,
					   (browser_mouse_state)state,
					   Fl::event_x() - x(),
					   Fl::event_y() - y());

		return 1;
	default:
		return Fl_Widget::handle(event);
	}
}


/**
 * method to redraw the netsurf browsing widget
 */
void NS_Widget::draw()
{
	struct rect clip;
	fl_clip_box(x(), y(), w(), h(), clip.x0, clip.y0, clip.x1, clip.y1);
	/* clip box generates width/height so convert to absolute */
	clip.x1 += clip.x0;
	clip.y1 += clip.y0;
	struct redraw_context ctx = {
		.interactive = true,
		.background_images = true,
		.plot = &nsfltk_plotters,
		.priv = NULL,
	};

	browser_window_redraw(mbw, x(), y(), &clip, &ctx);

}

/**
 * callback when fltk window is closed
 */
void NS_Window::close_callback(Fl_Widget *w)
{
	browser_window_destroy(mbw);
}

/**
 * Create and open a gui window for a browsing context.
 *
 * The implementing front end must create a context suitable
 *  for it to display a window referred to as the "gui window".
 *
 * The frontend will be expected to request the core redraw
 *  areas of the gui window which have become invalidated
 *  either from toolkit expose events or as a result of a
 *  invalidate() call.
 *
 * Most core operations used by the frontend concerning browser
 *  windows require passing the browser window context therefor
 *  the gui window must include a reference to the browser
 *  window passed here.
 *
 * If GW_CREATE_CLONE flag is set existing is non-NULL.
 *
 * \param bw The core browsing context associated with the gui window
 * \param existing An existing gui_window, may be NULL.
 * \param flags flags to control the gui window creation.
 * \return gui window, or NULL on error.
 */
static struct gui_window *
nsfltk_window_create(struct browser_window *bw,
		     struct gui_window *existing,
		     gui_window_create_flags flags)
{
	struct gui_window *gw;
	gw = (struct gui_window *)calloc(1, sizeof(struct gui_window));

	gw->window = new NS_Window(800,600, bw);

	gw->window->show();

	return gw;
}

/**
 * Destroy previously created gui window
 *
 * \param gw The gui window to destroy.
 */
static void nsfltk_window_destroy(struct gui_window *gw)
{
	Fl::delete_widget(gw->window);
	free(gw);
}


/**
 * Invalidate an area of a window.
 *
 * The specified area of the window should now be considered
 *  out of date. If the area is NULL the entire window must be
 *  invalidated. It is expected that the windowing system will
 *  then subsequently cause redraw/expose operations as
 *  necessary.
 *
 * \note the frontend should not attempt to actually start the
 *  redraw operations as a result of this callback because the
 *  core redraw functions may already be threaded.
 *
 * \param gw The gui window to invalidate.
 * \param rect area to redraw or NULL for the entire window area
 * \return NSERROR_OK on success or appropriate error code
 */
static nserror
nsfltk_window_invalidate(struct gui_window *gw, const struct rect *rect)
{
	NS_Widget *nswidget;
	nswidget = gw->window->get_nswidget();

	if (rect == NULL) {
		nswidget->damage(FL_DAMAGE_ALL);
	} else {
		nswidget->damage(FL_DAMAGE_ALL,
				 rect->x0,
				 rect->y0,
				 rect->x1 - rect->x0,
				 rect->y1 - rect->y0);
	}
	return NSERROR_OK;
}


/**
 * Get the scroll position of a browser window.
 *
 * \param gw The gui window to obtain the scroll position from.
 * \param sx receives x ordinate of point at top-left of window
 * \param sy receives y ordinate of point at top-left of window
 * \return true iff successful
 */
static bool nsfltk_window_get_scroll(struct gui_window *gw, int *sx, int *sy)
{
	return false;
}


/**
 * Set the scroll position of a browser window.
 *
 * scrolls the viewport to ensure the specified rectangle of
 *   the content is shown.
 * If the rectangle is of zero size i.e. x0 == x1 and y0 == y1
 *   the contents will be scrolled so the specified point in the
 *   content is at the top of the viewport.
 * If the size of the rectangle is non zero the frontend may
 *   add padding or centre the defined area or it may simply
 *   align as in the zero size rectangle
 *
 * \param gw The gui window to scroll.
 * \param rect The rectangle to ensure is shown.
 * \return NSERROR_OK on success or appropriate error code.
 */
static nserror
nsfltk_window_set_scroll(struct gui_window *gw, const struct rect *rect)
{
	return NSERROR_OK;
}


/**
 * Find the current dimensions of a browser window's content area.
 *
 * This is used to determine the actual available drawing size
 * in pixels. This allows contents that can be dynamically
 * reformatted, such as HTML, to better use the available
 * space.
 *
 * \param gw The gui window to measure content area of.
 * \param width receives width of window
 * \param height receives height of window
 * \return NSERROR_OK on success and width and height updated
 *          else error code.
 */
static nserror
nsfltk_window_get_dimensions(struct gui_window *gw, int *width, int *height)
{
	NS_Widget *nswidget;
	nswidget = gw->window->get_nswidget();

	*width = nswidget->w();
	*height = nswidget->h();

	return NSERROR_OK;
}


/**
 * Miscellaneous event occurred for a window
 *
 * This is used to inform the frontend of window events which
 *   require no additional parameters.
 *
 * \param gw The gui window the event occurred for
 * \param event Which event has occurred.
 * \return NSERROR_OK if the event was processed else error code.
 */
static nserror
nsfltk_window_event(struct gui_window *gw, enum gui_window_event event)
{
	return NSERROR_OK;
}

static struct gui_window_table window_table = {
	.create = nsfltk_window_create,
	.destroy = nsfltk_window_destroy,
	.invalidate = nsfltk_window_invalidate,
	.get_scroll = nsfltk_window_get_scroll,
	.set_scroll = nsfltk_window_set_scroll,
	.get_dimensions = nsfltk_window_get_dimensions,
	.event = nsfltk_window_event,
	.set_title = NULL,
	.set_url = NULL,
	.set_icon = NULL,
	.set_status = NULL,
	.set_pointer = NULL,
	.place_caret = NULL,
	.drag_start = NULL,
	.save_link = NULL,
	.create_form_select_menu = NULL,
	.file_gadget_open = NULL,
	.drag_save_object = NULL,
	.drag_save_selection = NULL,
	.console_log = NULL,
};

struct gui_window_table *nsfltk_window_table = &window_table;