summaryrefslogtreecommitdiff
path: root/riscos/hotlist.c
blob: 4c2c084d2215586445747ddaad54d2a032c37818 (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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 * Copyright 2004, 2005 Richard Wilson <info@tinct.net>
 * Copyright 2010 Stephen Fryatt <stevef@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
 * Hotlist (implementation).
 */

#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "oslib/osfile.h"
#include "oslib/wimp.h"
#include "content/content.h"
#include "content/hlcache.h"
#include "content/urldb.h"
#include "desktop/hotlist.h"
#include "desktop/tree.h"
#include "riscos/dialog.h"
#include "riscos/hotlist.h"
#include "riscos/menus.h"
#include "riscos/options.h"
#include "riscos/save.h"
#include "riscos/theme.h"
#include "riscos/treeview.h"
#include "riscos/wimp.h"
#include "riscos/wimp_event.h"
#include "utils/log.h"
#include "utils/messages.h"
#include "utils/utils.h"
#include "utils/url.h"

/* The RISC OS hotlist window, toolbar and treeview data. */

static struct ro_hotlist {
	wimp_w		window;		/*< The hotlist RO window handle. */
	struct toolbar	*toolbar;	/*< The hotlist toolbar handle.   */
	ro_treeview	*tv;		/*< The hotlist treeview handle.  */
	wimp_menu	*menu;		/*< The hotlist window menu.      */
} hotlist_window;

/**
 * Pre-Initialise the hotlist tree.  This is called for things that need to
 * be done at the gui_init() stage, such as loading templates.
 */

void ro_gui_hotlist_preinitialise(void)
{
	/* Create our window. */

	hotlist_window.window = ro_gui_dialog_create("tree");
	ro_gui_set_window_title(hotlist_window.window,
			messages_get("Hotlist"));
}

/**
 * Initialise the hotlist tree, at the gui_init2() stage.
 */

void ro_gui_hotlist_postinitialise(void)
{
	/* Create our toolbar. */

	hotlist_window.toolbar = ro_gui_theme_create_toolbar(NULL,
			THEME_HOTLIST_TOOLBAR);
	if (hotlist_window.toolbar)
		ro_gui_theme_attach_toolbar(hotlist_window.toolbar,
				hotlist_window.window);

	/* Create the treeview with the window and toolbar. */

	hotlist_window.tv = ro_treeview_create(hotlist_window.window,
			hotlist_window.toolbar, hotlist_get_tree_flags());
	if (hotlist_window.tv == NULL) {
		LOG(("Failed to allocate treeview"));
		return;
	}

	/* Initialise the hotlist into the tree. */

	hotlist_initialise(ro_treeview_get_tree(hotlist_window.tv),
			option_hotlist_path);


	/* Build the hotlist window menu. */

	static const struct ns_menu hotlist_definition = {
		"Hotlist", {
			{ "Hotlist", NO_ACTION, 0 },
			{ "Hotlist.New", NO_ACTION, 0 },
			{ "Hotlist.New.Folder", TREE_NEW_FOLDER, 0 },
			{ "Hotlist.New.Link", TREE_NEW_LINK, 0 },
			{ "_Hotlist.Export", HOTLIST_EXPORT, &dialog_saveas },
			{ "Hotlist.Expand", TREE_EXPAND_ALL, 0 },
			{ "Hotlist.Expand.All", TREE_EXPAND_ALL, 0 },
			{ "Hotlist.Expand.Folders", TREE_EXPAND_FOLDERS, 0 },
			{ "Hotlist.Expand.Links", TREE_EXPAND_LINKS, 0 },
			{ "Hotlist.Collapse", TREE_COLLAPSE_ALL, 0 },
			{ "Hotlist.Collapse.All", TREE_COLLAPSE_ALL, 0 },
			{ "Hotlist.Collapse.Folders", TREE_COLLAPSE_FOLDERS, 0 },
			{ "Hotlist.Collapse.Links", TREE_COLLAPSE_LINKS, 0 },
			{ "Hotlist.Toolbars", NO_ACTION, 0 },
			{ "_Hotlist.Toolbars.ToolButtons", TOOLBAR_BUTTONS, 0 },
			{ "Hotlist.Toolbars.EditToolbar", TOOLBAR_EDIT, 0 },
			{ "Selection", TREE_SELECTION, 0 },
			{ "Selection.Edit", TREE_SELECTION_EDIT, 0 },
			{ "Selection.Launch", TREE_SELECTION_LAUNCH, 0 },
			{ "Selection.Delete", TREE_SELECTION_DELETE, 0 },
			{ "SelectAll", TREE_SELECT_ALL, 0 },
			{ "Clear", TREE_CLEAR_SELECTION, 0 },
			{NULL, 0, 0}
		}
	};

	hotlist_window.menu = ro_gui_menu_define_menu(&hotlist_definition);

	ro_gui_wimp_event_register_window_menu(hotlist_window.window,
			hotlist_window.menu, ro_gui_hotlist_menu_prepare,
			ro_gui_hotlist_menu_select, NULL,
			ro_gui_hotlist_menu_warning, false);
}


/**
 * Open the hotlist window.
 *
 */

void ro_gui_hotlist_open(void)
{
	tree_set_redraw(ro_treeview_get_tree(hotlist_window.tv), true);

	if (!ro_gui_dialog_open_top(hotlist_window.window,
			hotlist_window.toolbar, 600, 800)) {

	xwimp_set_caret_position(hotlist_window.window, -1, -100, -100, 32, -1);
// \todo	ro_gui_theme_process_toolbar(hotlist_window.toolbar, -1);
		ro_treeview_set_origin(hotlist_window.tv, 0,
				-(ro_gui_theme_toolbar_height(
				hotlist_window.toolbar)));
//		ro_gui_tree_stop_edit(tree);
//
//		if (tree->root->child) {
//			tree_set_node_selected(tree, tree->root, false);
//			tree_handle_node_changed(tree, tree->root,
//				false, true);
//		}
	}
}

/**
 * Handle Mouse Click events on the toolbar.
 *
 * \param  *pointer		Pointer to the Mouse Click Event block.
 * \return			Return true if click handled; else false.
 */

bool ro_gui_hotlist_toolbar_click(wimp_pointer *pointer)
{
	if (pointer->buttons == wimp_CLICK_MENU)
		return ro_gui_wimp_event_process_window_menu_click(pointer);

	if (hotlist_window.toolbar->editor != NULL) {
		ro_gui_theme_toolbar_editor_click(hotlist_window.toolbar,
				pointer);
		return true;
	}

	switch (pointer->i) {
	case ICON_TOOLBAR_DELETE:
		if (pointer->buttons == wimp_CLICK_SELECT) {
			hotlist_delete_selected();
			return true;
		}
		break;
	case ICON_TOOLBAR_EXPAND:
		if (pointer->buttons == wimp_CLICK_SELECT) {
			hotlist_expand_addresses();
			return true;
		} else if (pointer->buttons == wimp_CLICK_ADJUST) {
			hotlist_collapse_addresses();
			return true;
		}
		break;
	case ICON_TOOLBAR_OPEN:
		if (pointer->buttons == wimp_CLICK_SELECT) {
			hotlist_expand_directories();
			return true;
		} else if (pointer->buttons == wimp_CLICK_ADJUST) {
			hotlist_collapse_directories();
			return true;
		}
		break;
	case ICON_TOOLBAR_LAUNCH:
		if (pointer->buttons == wimp_CLICK_SELECT) {
			hotlist_launch_selected();
			return true;
		}
		break;
	case ICON_TOOLBAR_CREATE:
		if (pointer->buttons == wimp_CLICK_SELECT) {
			hotlist_add_folder();
			return true;
		}
		break;
	}

	return true;
}


/**
 * Prepare the hotlist menu for opening
 *
 * \param  window		The window owning the menu.
 * \param  *menu		The menu about to be opened.
 */

void ro_gui_hotlist_menu_prepare(wimp_w window, wimp_menu *menu)
{
	bool selection;

	if (menu != hotlist_window.menu && menu != tree_toolbar_menu)
		return;

	if (menu == hotlist_window.menu) {
		selection = ro_treeview_has_selection(hotlist_window.tv);

		ro_gui_menu_set_entry_shaded(hotlist_window.menu,
				TREE_SELECTION, !selection);
		ro_gui_menu_set_entry_shaded(hotlist_window.menu,
				TREE_CLEAR_SELECTION, !selection);

		ro_gui_save_prepare(GUI_SAVE_HOTLIST_EXPORT_HTML,
				NULL, NULL, NULL, NULL);
	}

	ro_gui_menu_set_entry_shaded(menu, TOOLBAR_BUTTONS,
			(hotlist_window.toolbar == NULL ||
			hotlist_window.toolbar->editor != NULL));
	ro_gui_menu_set_entry_ticked(menu, TOOLBAR_BUTTONS,
			(hotlist_window.toolbar != NULL &&
			(hotlist_window.toolbar->display_buttons ||
			(hotlist_window.toolbar->editor != NULL))));

	ro_gui_menu_set_entry_shaded(menu, TOOLBAR_EDIT,
			hotlist_window.toolbar == NULL);
	ro_gui_menu_set_entry_ticked(menu, TOOLBAR_EDIT,
			(hotlist_window.toolbar != NULL &&
			hotlist_window.toolbar->editor != NULL));
}


/**
 * Handle submenu warnings for the hotlist menu
 *
 * \param  window		The window owning the menu.
 * \param  *menu		The menu to which the warning applies.
 * \param  *selection		The wimp menu selection data.
 * \param  action		The selected menu action.
 */

void ro_gui_hotlist_menu_warning(wimp_w window, wimp_menu *menu,
		wimp_selection *selection, menu_action action)
{
	/* Do nothing */
}

/**
 * Handle selections from the hotlist menu
 *
 * \param  window		The window owning the menu.
 * \param  *menu		The menu from which the selection was made.
 * \param  *selection		The wimp menu selection data.
 * \param  action		The selected menu action.
 * \return			true if action accepted; else false.
 */

bool ro_gui_hotlist_menu_select(wimp_w window, wimp_menu *menu,
		wimp_selection *selection, menu_action action)
{
	switch (action) {
	case HOTLIST_EXPORT:
		ro_gui_dialog_open_persistent(window, dialog_saveas, true);
		return true;
	case TREE_NEW_FOLDER:
		hotlist_add_folder();
		return true;
	case TREE_NEW_LINK:
		hotlist_add_entry();
		return true;
	case TREE_EXPAND_ALL:
		hotlist_expand_all();
		return true;
	case TREE_EXPAND_FOLDERS:
		hotlist_expand_directories();
		return true;
	case TREE_EXPAND_LINKS:
		hotlist_expand_addresses();
		return true;
	case TREE_COLLAPSE_ALL:
		hotlist_collapse_all();
		return true;
	case TREE_COLLAPSE_FOLDERS:
		hotlist_collapse_directories();
		return true;
	case TREE_COLLAPSE_LINKS:
		hotlist_collapse_addresses();
		return true;
	case TREE_SELECTION_EDIT:
		hotlist_edit_selected();
		return true;
	case TREE_SELECTION_LAUNCH:
		hotlist_launch_selected();
		return true;
	case TREE_SELECTION_DELETE:
		hotlist_delete_selected();
		return true;
	case TREE_SELECT_ALL:
		hotlist_select_all();
		return true;
	case TREE_CLEAR_SELECTION:
		hotlist_clear_selection();
		return true;
	case TOOLBAR_BUTTONS:
		hotlist_window.toolbar->display_buttons =
				!hotlist_window.toolbar->display_buttons;
		ro_gui_theme_refresh_toolbar(hotlist_window.toolbar);
		return true;
	case TOOLBAR_EDIT:
		ro_gui_theme_toggle_edit(hotlist_window.toolbar);
		return true;
	default:
		return false;
	}

	return false;
}

/**
 * Update the theme details of the hotlist window.
 *
 * \param full_update		true to force a full theme change; false to
 *				refresh the toolbar size.
 */

void ro_gui_hotlist_update_theme(bool full_update)
{
	if (full_update)
		ro_treeview_update_theme(hotlist_window.tv);
	else
		ro_treeview_update_toolbar(hotlist_window.tv);
}

/**
 * Check if a particular window handle is the hotlist window
 *
 * \param window	The window in question
 * \return		true if this window is the hotlist
 */
bool ro_gui_hotlist_check_window(wimp_w window)
{
	if (hotlist_window.window == window)
		return true;
	else
		return false;
}

/**
 * Check if a particular menu handle is the hotlist menu
 *
 * \param  *menu		The menu in question.
 * \return			true if this menu is the hotlist menu
 */

bool ro_gui_hotlist_check_menu(wimp_menu *menu)
{
	if (hotlist_window.menu == menu)
		return true;
	else
		return false;
}

#if 0
/**
 * Handle URL dropped on hotlist
 *
 * \param message  the wimp message we're acting on
 * \param url	   the URL to add
 */
void ro_gui_hotlist_url_drop(wimp_message *message, const char *url)
{
	int x, y;
	if (hotlist_window.window != message->data.data_xfer.w)
		return;

	ro_gui_tree_get_tree_coordinates(hotlist_window.tree,
				message->data.data_xfer.pos.x,
				message->data.data_xfer.pos.y,
				&x, &y);
	hotlist_add_page_xy(url, x, y);
}
#endif