NetSurf
cookies.c
Go to the documentation of this file.
1/*
2 * Copyright 2016 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 RISC OS cookie manager.
22 */
23
24#include <stdint.h>
25#include <stdlib.h>
26#include <oslib/wimp.h>
27
28#include "utils/log.h"
29#include "utils/nsoption.h"
30#include "utils/messages.h"
31#include "netsurf/plotters.h"
32#include "netsurf/keypress.h"
34
35#include "riscos/gui.h"
36#include "riscos/wimp.h"
37#include "riscos/wimp_event.h"
38#include "riscos/dialog.h"
39#include "riscos/toolbar.h"
40#include "riscos/corewindow.h"
41#include "riscos/cookies.h"
42
45 wimp_menu *menu;
46};
47
48/** cookie window is a singleton */
49static struct ro_cookie_window *cookie_window = NULL;
50
51/** riscos template for cookie window */
52static wimp_window *dialog_cookie_template;
53
54
55/**
56 * callback to draw on drawable area of ro cookie window
57 *
58 * \param ro_cw The riscos core window structure.
59 * \param r The rectangle of the window that needs updating.
60 * \param originx The risc os plotter x origin.
61 * \param originy The risc os plotter y origin.
62 * \return NSERROR_OK on success otherwise apropriate error code
63 */
64static nserror
66 int originx,
67 int originy,
68 struct rect *r)
69{
70 struct redraw_context ctx = {
71 .interactive = true,
72 .background_images = true,
73 .plot = &ro_plotters
74 };
75
76 ro_plot_origin_x = originx;
77 ro_plot_origin_y = originy;
78 no_font_blending = true;
79 cookie_manager_redraw(0, 0, r, &ctx);
80 no_font_blending = false;
81
82 return NSERROR_OK;
83}
84
85
86/**
87 * callback for keypress on ro cookie window
88 *
89 * \param ro_cw The ro core window structure.
90 * \param nskey The netsurf key code.
91 * \return NSERROR_OK if key processed,
92 * NSERROR_NOT_IMPLEMENTED if key not processed
93 * otherwise apropriate error code
94 */
95static nserror cookie_key(struct ro_corewindow *ro_cw, uint32_t nskey)
96{
97 if (cookie_manager_keypress(nskey)) {
98 return NSERROR_OK;
99 }
101}
102
103
104/**
105 * callback for mouse event on ro cookie window
106 *
107 * \param ro_cw The ro core window structure.
108 * \param mouse_state mouse state
109 * \param x location of event
110 * \param y location of event
111 * \return NSERROR_OK on sucess otherwise apropriate error code.
112 */
113static nserror
115 browser_mouse_state mouse_state,
116 int x, int y)
117{
118 cookie_manager_mouse_action(mouse_state, x, y);
119
120 return NSERROR_OK;
121}
122
123
124/**
125 * handle clicks in ro core window toolbar.
126 *
127 * \param ro_cw The ro core window structure.
128 * \param action The button bar action.
129 * \return NSERROR_OK if config saved, otherwise apropriate error code
130 */
131static nserror
133{
134 switch (action) {
137 break;
138
141 break;
142
145 break;
146
149 break;
150
153 break;
154
155 default:
156 break;
157 }
158
159 return NSERROR_OK;
160}
161
162
163/**
164 * Handle updating state of buttons in ro core window toolbar.
165 *
166 * \param ro_cw The ro core window structure.
167 * \return NSERROR_OK if config saved, otherwise apropriate error code
168 */
170{
174 return NSERROR_OK;
175}
176
177
178/**
179 * callback for saving of toolbar state in ro cookie window
180 *
181 * \param ro_cw The ro core window structure.
182 * \param config The new toolbar configuration.
183 * \return NSERROR_OK if config saved, otherwise apropriate error code
184 */
185static nserror cookie_toolbar_save(struct ro_corewindow *ro_cw, char *config)
186{
187 nsoption_set_charp(toolbar_cookies, config);
189
190 return NSERROR_OK;
191}
192
193
194/**
195 * Prepare the cookie meu for display
196 *
197 * \param w The window owning the menu.
198 * \param i The icon owning the menu.
199 * \param menu The menu from which the selection was made.
200 * \param pointer The pointer shape
201 * \return true if action accepted; else false.
202 */
203static bool
205 wimp_i i,
206 wimp_menu *menu,
207 wimp_pointer *pointer)
208{
209 bool selection;
210 struct ro_cookie_window *cookiew;
211
213
214 if ((cookiew == NULL) ||
215 (menu != cookiew->menu)) {
216 return false;
217 }
218
220
223
228
233
234 return true;
235}
236
237
238/**
239 * Handle submenu warnings for the cookies menu
240 *
241 * \param w The window owning the menu.
242 * \param i The icon owning the menu.
243 * \param menu The menu to which the warning applies.
244 * \param selection The wimp menu selection data.
245 * \param action The selected menu action.
246 */
247static void
249 wimp_i i,
250 wimp_menu *menu,
251 wimp_selection *selection,
252 menu_action action)
253{
254 /* Do nothing */
255}
256
257
258/**
259 * Handle selections from the cookies menu
260 *
261 * \param w The window owning the menu.
262 * \param i The icon owning the menu.
263 * \param menu The menu from which the selection was made.
264 * \param selection The wimp menu selection data.
265 * \param action The selected menu action.
266 * \return true if action accepted; else false.
267 */
268static bool
270 wimp_i i,
271 wimp_menu *menu,
272 wimp_selection *selection,
273 menu_action action)
274{
275 struct ro_cookie_window *cookiew;
276
278
279 if ((cookiew == NULL) ||
280 (menu != cookiew->menu)) {
281 return false;
282 }
283
284 switch (action) {
285 case TREE_EXPAND_ALL:
287 return true;
288
291 return true;
292
295 return true;
296
299 return true;
300
303 return true;
304
307 return true;
308
311 return true;
312
313 case TREE_SELECT_ALL:
315 return true;
316
319 return true;
320
321 case TOOLBAR_BUTTONS:
324 return true;
325
326 case TOOLBAR_EDIT:
328 return true;
329
330 default:
331 return false;
332 }
333
334 return false;
335}
336
337
338/**
339 * Creates the window for the cookie tree.
340 *
341 * \return NSERROR_OK on success else appropriate error code on faliure.
342 */
344{
345 os_error *error;
346 struct ro_cookie_window *ncwin;
347 nserror res;
348 static const struct ns_menu cookie_menu_def = {
349 "Cookies", {
350 { "Cookies", NO_ACTION, 0 },
351 { "Cookies.Expand", TREE_EXPAND_ALL, 0 },
352 { "Cookies.Expand.All", TREE_EXPAND_ALL, 0 },
353 { "Cookies.Expand.Folders", TREE_EXPAND_FOLDERS, 0 },
354 { "Cookies.Expand.Links", TREE_EXPAND_LINKS, 0 },
355 { "Cookies.Collapse", TREE_COLLAPSE_ALL, 0 },
356 { "Cookies.Collapse.All", TREE_COLLAPSE_ALL, 0 },
357 { "Cookies.Collapse.Folders", TREE_COLLAPSE_FOLDERS, 0 },
358 { "Cookies.Collapse.Links", TREE_COLLAPSE_LINKS, 0 },
359 { "Cookies.Toolbars", NO_ACTION, 0 },
360 { "_Cookies.Toolbars.ToolButtons", TOOLBAR_BUTTONS, 0 },
361 { "Cookies.Toolbars.EditToolbar",TOOLBAR_EDIT, 0 },
362 { "Selection", TREE_SELECTION, 0 },
363 { "Selection.Delete", TREE_SELECTION_DELETE, 0 },
364 { "SelectAll", TREE_SELECT_ALL, 0 },
365 { "Clear", TREE_CLEAR_SELECTION, 0 },
366 { NULL, 0, 0}
367 }
368 };
369
370 static const struct button_bar_buttons cookies_toolbar_buttons[] = {
371 { "delete", TOOLBAR_BUTTON_DELETE, TOOLBAR_BUTTON_NONE, '0', "0"},
372 { "expand", TOOLBAR_BUTTON_EXPAND, TOOLBAR_BUTTON_COLLAPSE, '1', "1"},
373 { "open", TOOLBAR_BUTTON_OPEN, TOOLBAR_BUTTON_CLOSE, '2', "2"},
374 { NULL, TOOLBAR_BUTTON_NONE, TOOLBAR_BUTTON_NONE, '\0', ""}
375 };
376
377 if (cookie_window != NULL) {
378 return NSERROR_OK;
379 }
380
381 ncwin = calloc(1, sizeof(*ncwin));
382 if (ncwin == NULL) {
383 return NSERROR_NOMEM;
384 }
385
386 /* create window from template */
387 error = xwimp_create_window(dialog_cookie_template, &ncwin->core.wh);
388 if (error) {
389 NSLOG(netsurf, INFO, "xwimp_create_window: 0x%x: %s",
390 error->errnum, error->errmess);
391 ro_warn_user("WimpError", error->errmess);
392 free(ncwin);
393 return NSERROR_NOMEM;
394 }
395
396 ro_gui_set_window_title(ncwin->core.wh, messages_get("Cookies"));
397
398 ncwin->core.draw = cookie_draw;
399 ncwin->core.key = cookie_key;
400 ncwin->core.mouse = cookie_mouse;
403 /* update is not valid untill cookie manager is initialised */
404 ncwin->core.toolbar_update = NULL;
405
406 /* initialise core window */
407 res = ro_corewindow_init(&ncwin->core,
408 cookies_toolbar_buttons,
409 nsoption_charp(toolbar_cookies),
411 "HelpCookiesToolbar");
412 if (res != NSERROR_OK) {
413 free(ncwin);
414 return res;
415 }
416
417 res = cookie_manager_init((struct core_window *)ncwin);
418 if (res != NSERROR_OK) {
419 free(ncwin);
420 return res;
421 }
422
423 /* setup toolbar update post cookie manager initialisation */
426
427 /* Build the cookies window menu. */
428 ncwin->menu = ro_gui_menu_define_menu(&cookie_menu_def);
429
431 ncwin->menu, false, false);
438
439 /* memoise window so it can be represented when necessary
440 * instead of recreating every time.
441 */
442 cookie_window = ncwin;
443
444 return NSERROR_OK;
445}
446
447
448/* exported interface documented in riscos/cookies.h */
449nserror ro_gui_cookies_present(const char *search_term)
450{
451 nserror res;
452
453 res = ro_cookie_init();
454 if (res == NSERROR_OK) {
455 NSLOG(netsurf, INFO, "Presenting");
458 600, 800);
459 if (search_term != NULL) {
460 res = cookie_manager_set_search_string(search_term);
461 }
462 } else {
463 NSLOG(netsurf, INFO, "Failed presenting code %d", res);
464 }
465
466 return res;
467}
468
469
470/* exported interface documented in riscos/cookies.h */
472{
474}
475
476
477/* exported interface documented in riscos/cookies.h */
479{
480 nserror res;
481
482 if (cookie_window == NULL) {
483 return NSERROR_OK;
484 }
485
486 res = cookie_manager_fini();
487 if (res == NSERROR_OK) {
489
490 free(cookie_window);
491 cookie_window = NULL;
492 }
493
494 return res;
495}
496
497
498/* exported interface documented in riscos/cookies.h */
500{
501 if ((cookie_window != NULL) &&
502 (cookie_window->core.wh == wh)) {
503 return true;
504 }
505 return false;
506}
507
508
509/* exported interface documented in riscos/cookies.h */
510bool ro_gui_cookies_check_menu(wimp_menu *menu)
511{
512 if ((cookie_window != NULL) &&
513 (cookie_window->menu == menu)) {
514 return true;
515 }
516 return false;
517}
menu_action
Definition: scaffolding.h:77
@ TREE_EXPAND_FOLDERS
Definition: scaffolding.h:157
@ TREE_EXPAND_ALL
Definition: scaffolding.h:156
@ TREE_COLLAPSE_FOLDERS
Definition: scaffolding.h:160
@ TREE_SELECTION
Definition: scaffolding.h:162
@ TREE_SELECT_ALL
Definition: scaffolding.h:166
@ TREE_CLEAR_SELECTION
Definition: scaffolding.h:167
@ TREE_EXPAND_LINKS
Definition: scaffolding.h:158
@ TOOLBAR_BUTTONS
Definition: scaffolding.h:170
@ TREE_SELECTION_DELETE
Definition: scaffolding.h:165
@ TOOLBAR_EDIT
Definition: scaffolding.h:173
@ TREE_COLLAPSE_ALL
Definition: scaffolding.h:159
@ TREE_COLLAPSE_LINKS
Definition: scaffolding.h:161
button_bar_action
Definition: button_bar.h:32
@ TOOLBAR_BUTTON_NONE
Definition: button_bar.h:33
@ TOOLBAR_BUTTON_CLOSE
Definition: button_bar.h:57
@ TOOLBAR_BUTTON_DELETE
Definition: button_bar.h:53
@ TOOLBAR_BUTTON_COLLAPSE
Definition: button_bar.h:55
@ TOOLBAR_BUTTON_OPEN
Definition: button_bar.h:56
@ TOOLBAR_BUTTON_EXPAND
Definition: button_bar.h:54
nserror cookie_manager_contract(bool all)
Contract the treeview's nodes.
nserror cookie_manager_expand(bool only_folders)
Expand the treeview's nodes.
void cookie_manager_redraw(int x, int y, struct rect *clip, const struct redraw_context *ctx)
Redraw the cookies manager.
bool cookie_manager_has_selection(void)
Determine whether there is a selection.
void cookie_manager_mouse_action(enum browser_mouse_state mouse, int x, int y)
Handles all kinds of mouse action.
nserror cookie_manager_init(void *core_window_handle)
Initialise the cookie manager.
nserror cookie_manager_fini(void)
Finalise the cookie manager.
nserror cookie_manager_set_search_string(const char *string)
Set the cookie manager search string.
bool cookie_manager_keypress(uint32_t key)
Key press handling.
Cookie Manager (interface).
wimp_window * ro_gui_dialog_load_template(const char *template_name)
Load a template without creating a window.
Definition: dialog.c:242
void ro_gui_save_options(void)
Save the current options.
Definition: dialog.c:670
bool ro_gui_dialog_open_top(wimp_w w, struct toolbar *toolbar, int width, int height)
Moves a window to the top of the stack.
Definition: dialog.c:413
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
bool no_font_blending
Definition: font.c:48
@ THEME_STYLE_COOKIES_TOOLBAR
Definition: theme.h:35
#define NO_ACTION
Definition: idna.c:695
browser_mouse_state
Mouse state.
Definition: mouse.h:43
Target independent plotting interface.
Interface to key press operations.
@ NS_KEY_SELECT_ALL
Definition: keypress.h:32
@ NS_KEY_CLEAR_SELECTION
Definition: keypress.h:45
@ NS_KEY_DELETE_LEFT
Definition: keypress.h:35
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
void ro_gui_menu_set_entry_ticked(wimp_menu *menu, menu_action action, bool ticked)
Sets an action within a menu as having a specific ticked status.
Definition: menus.c:831
void ro_gui_menu_set_entry_shaded(wimp_menu *menu, menu_action action, bool shaded)
Sets an action within a menu as having a specific ticked status.
Definition: menus.c:804
wimp_menu * ro_gui_menu_define_menu(const struct ns_menu *menu)
Creates a wimp_menu and adds it to the list to handle actions for.
Definition: menus.c:510
const char * messages_get(const char *key)
Fast lookup of a message by key from the standard Messages hash.
Definition: messages.c:241
Localised message support (interface).
nserror ro_gui_cookies_finalise(void)
Free any resources allocated for the cookie window.
Definition: cookies.c:478
static nserror cookie_toolbar_update(struct ro_corewindow *ro_cw)
Handle updating state of buttons in ro core window toolbar.
Definition: cookies.c:169
static nserror cookie_toolbar_save(struct ro_corewindow *ro_cw, char *config)
callback for saving of toolbar state in ro cookie window
Definition: cookies.c:185
static nserror cookie_mouse(struct ro_corewindow *ro_cw, browser_mouse_state mouse_state, int x, int y)
callback for mouse event on ro cookie window
Definition: cookies.c:114
static void cookie_menu_warning(wimp_w w, wimp_i i, wimp_menu *menu, wimp_selection *selection, menu_action action)
Handle submenu warnings for the cookies menu.
Definition: cookies.c:248
static wimp_window * dialog_cookie_template
riscos template for cookie window
Definition: cookies.c:52
static bool cookie_menu_select(wimp_w w, wimp_i i, wimp_menu *menu, wimp_selection *selection, menu_action action)
Handle selections from the cookies menu.
Definition: cookies.c:269
void ro_gui_cookies_initialise(void)
initialise the cookies window template ready for subsequent use.
Definition: cookies.c:471
static nserror ro_cookie_init(void)
Creates the window for the cookie tree.
Definition: cookies.c:343
bool ro_gui_cookies_check_window(wimp_w wh)
check if window handle is for the cookies window
Definition: cookies.c:499
bool ro_gui_cookies_check_menu(wimp_menu *menu)
check if menu handle is for the cookies menu
Definition: cookies.c:510
static struct ro_cookie_window * cookie_window
cookie window is a singleton
Definition: cookies.c:49
static nserror cookie_toolbar_click(struct ro_corewindow *ro_cw, button_bar_action action)
handle clicks in ro core window toolbar.
Definition: cookies.c:132
static nserror cookie_draw(struct ro_corewindow *ro_cw, int originx, int originy, struct rect *r)
callback to draw on drawable area of ro cookie window
Definition: cookies.c:65
nserror ro_gui_cookies_present(const char *search_term)
make the cookie window visible.
Definition: cookies.c:449
static bool cookie_menu_prepare(wimp_w w, wimp_i i, wimp_menu *menu, wimp_pointer *pointer)
Prepare the cookie meu for display.
Definition: cookies.c:204
static nserror cookie_key(struct ro_corewindow *ro_cw, uint32_t nskey)
callback for keypress on ro cookie window
Definition: cookies.c:95
Interface to riscos cookie viewing using riscos core window.
nserror ro_corewindow_fini(struct ro_corewindow *ro_cw)
finalise elements of ro core window.
Definition: corewindow.c:1078
nserror ro_corewindow_init(struct ro_corewindow *ro_cw, const struct button_bar_buttons *tb_buttons, char *tb_order, theme_style tb_style, const char *tb_help)
initialise elements of riscos core window.
Definition: corewindow.c:1003
RISC OS core window interface.
nserror ro_warn_user(const char *warning, const char *detail)
Display a warning for a serious problem (eg memory exhaustion).
Definition: gui.c:2077
int ro_plot_origin_x
Definition: plotters.c:40
int ro_plot_origin_y
Definition: plotters.c:41
const struct plotter_table ro_plotters
RISC OS plotter operation table.
Definition: plotters.c:727
void ro_toolbar_set_display_buttons(struct toolbar *toolbar, bool display)
Set the display button bar state for a toolbar.
Definition: toolbar.c:1703
bool ro_toolbar_toggle_edit(struct toolbar *toolbar)
Toggle toolbar edit mode on the given toolbar.
Definition: toolbar.c:1780
void ro_toolbar_set_button_shaded_state(struct toolbar *toolbar, button_bar_action action, bool shaded)
Set the shaded state of a toolbar button.
Definition: toolbar.c:1586
bool ro_toolbar_get_display_buttons(struct toolbar *toolbar)
Return true or false depending on whether the given toolbar is set to display the button bar.
Definition: toolbar.c:1745
Window toolbars (interface).
#define ro_toolbar_menu_edit_tick(toolbar)
Definition: toolbar.h:97
#define ro_toolbar_menu_option_shade(toolbar)
Definition: toolbar.h:82
#define ro_toolbar_menu_buttons_tick(toolbar)
Definition: toolbar.h:85
#define ro_toolbar_menu_edit_shade(toolbar)
Definition: toolbar.h:95
Definition: menus.h:159
Rectangle coordinates.
Definition: types.h:40
Redraw context.
Definition: plotters.h:51
bool interactive
Redraw to show interactive features.
Definition: plotters.h:59
ro core window state
Definition: corewindow.h:39
nserror(* toolbar_click)(struct ro_corewindow *ro_cw, button_bar_action action)
callback for clicks in ro core window toolbar.
Definition: corewindow.h:98
nserror(* mouse)(struct ro_corewindow *ro_cw, browser_mouse_state mouse_state, int x, int y)
callback for mouse event on ro core window
Definition: corewindow.h:89
nserror(* toolbar_save)(struct ro_corewindow *ro_cw, char *config)
callback for saving ro core window toolbar state.
Definition: corewindow.h:115
wimp_w wh
window handle
Definition: corewindow.h:41
nserror(* key)(struct ro_corewindow *ro_cw, uint32_t nskey)
callback for keypress on ro core window
Definition: corewindow.h:78
nserror(* draw)(struct ro_corewindow *ro_cw, int originx, int originy, struct rect *r)
callback to draw on drawable area of ro core window
Definition: corewindow.h:67
nserror(* toolbar_update)(struct ro_corewindow *ro_cw)
callback for updating state of buttons in ro core window toolbar.
Definition: corewindow.h:106
struct toolbar * toolbar
toolbar
Definition: corewindow.h:44
Option reading and saving interface.
#define nsoption_charp(OPTION)
Get the value of a string option.
Definition: nsoption.h:331
#define nsoption_set_charp(OPTION, VALUE)
set string option in default table
Definition: nsoption.h:372
void ro_gui_set_window_title(wimp_w w, const char *text)
Set a window title.
Definition: wimp.c:675
General RISC OS WIMP/OS library functions (interface).
void * ro_gui_wimp_event_get_user_data(wimp_w w)
Gets the user data associated with a window.
Definition: wimp_event.c:486
bool ro_gui_wimp_event_register_menu_selection(wimp_w w, bool(*callback)(wimp_w w, wimp_i i, wimp_menu *m, wimp_selection *s, menu_action a))
Register a function to be called following a menu selection.
Definition: wimp_event.c:1581
bool ro_gui_wimp_event_register_menu_prepare(wimp_w w, bool(*callback)(wimp_w w, wimp_i i, wimp_menu *m, wimp_pointer *p))
Register a function to be called before a menu is (re-)opened.
Definition: wimp_event.c:1559
bool ro_gui_wimp_event_register_menu_warning(wimp_w w, void(*callback)(wimp_w w, wimp_i i, wimp_menu *m, wimp_selection *s, menu_action a))
Register a function to be called when a sub-menu warning is received.
Definition: wimp_event.c:1603
bool ro_gui_wimp_event_register_menu(wimp_w w, wimp_menu *m, bool menu_auto, bool position_ibar)
Register a window menu to be (semi-)automatically handled.
Definition: wimp_event.c:1270
Automated RISC OS WIMP event handling (interface).