diff options
Diffstat (limited to 'riscos/global_history.c')
-rw-r--r-- | riscos/global_history.c | 534 |
1 files changed, 198 insertions, 336 deletions
diff --git a/riscos/global_history.c b/riscos/global_history.c index 5cccae0b4..e9f5ea6ad 100644 --- a/riscos/global_history.c +++ b/riscos/global_history.c @@ -1,5 +1,6 @@ /* * Copyright 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/ * @@ -29,12 +30,14 @@ #include "oslib/wimp.h" #include "oslib/wimpspriteop.h" #include "content/urldb.h" +#include "desktop/history_global_core.h" #include "desktop/tree.h" #include "riscos/dialog.h" #include "riscos/global_history.h" #include "riscos/gui.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" @@ -44,401 +47,260 @@ #include "utils/url.h" #include "utils/utils.h" -#define MAXIMUM_URL_LENGTH 1024 -#define MAXIMUM_BASE_NODES 16 +static void ro_gui_global_history_menu_prepare(wimp_w window, wimp_menu *menu); +static bool ro_gui_global_history_menu_select(wimp_w window, wimp_menu *menu, + wimp_selection *selection, menu_action action); +static void ro_gui_global_history_menu_warning(wimp_w window, wimp_menu *menu, + wimp_selection *selection, menu_action action); -static struct node *global_history_base_node[MAXIMUM_BASE_NODES]; -static int global_history_base_node_time[MAXIMUM_BASE_NODES]; -static int global_history_base_node_count = 0; +/* The RISC OS global history window, toolbar and treeview data */ -static char *global_history_recent_url[GLOBAL_HISTORY_RECENT_URLS]; -static int global_history_recent_count = 0; - -static bool global_history_init; - -static bool ro_gui_global_history_click(wimp_pointer *pointer); -static void ro_gui_global_history_initialise_nodes(void); -static void ro_gui_global_history_initialise_node(const char *title, - time_t base, int days_back); -static struct node *ro_gui_global_history_find(const char *url); -static bool global_history_add_internal(const char *url, - const struct url_data *data); - -/* The history window, toolbar and plot origins */ -static wimp_w global_history_window; -struct tree *global_history_tree; +static struct ro_global_history_window { + wimp_w window; + struct toolbar *toolbar; + ro_treeview *tv; + wimp_menu *menu; +} global_history_window; /** - * Initialise global history tree + * Pre-Initialise the global history tree. This is called for things that + * need to be done at the gui_init() stage, such as loading templates. */ -void ro_gui_global_history_initialise(void) + +void ro_gui_global_history_preinitialise(void) { - char s[MAXIMUM_URL_LENGTH]; - FILE *fp; + /* Create our window. */ - /* create our window */ - global_history_window = ro_gui_dialog_create("tree"); - ro_gui_set_window_title(global_history_window, + global_history_window.window = ro_gui_dialog_create("tree"); + ro_gui_set_window_title(global_history_window.window, messages_get("GlobalHistory")); - ro_gui_wimp_event_register_redraw_window(global_history_window, - ro_gui_tree_redraw); - ro_gui_wimp_event_register_open_window(global_history_window, - ro_gui_tree_open); - ro_gui_wimp_event_register_mouse_click(global_history_window, - ro_gui_global_history_click); - - /* Create an empty tree */ - global_history_tree = calloc(sizeof(struct tree), 1); - if (!global_history_tree) { - warn_user("NoMemory", 0); - return; - } - global_history_tree->root = tree_create_folder_node(NULL, "Root"); - if (!global_history_tree->root) { - warn_user("NoMemory", 0); - free(global_history_tree); - global_history_tree = NULL; - return; - } - global_history_tree->root->expanded = true; - ro_gui_global_history_initialise_nodes(); - global_history_tree->handle = (int)global_history_window; - global_history_tree->movable = false; - ro_gui_wimp_event_set_user_data(global_history_window, - global_history_tree); - ro_gui_wimp_event_register_keypress(global_history_window, - ro_gui_tree_keypress); - - /* Create our toolbar */ - global_history_tree->toolbar = ro_gui_theme_create_toolbar(NULL, - THEME_HISTORY_TOOLBAR); - if (global_history_tree->toolbar) - ro_gui_theme_attach_toolbar(global_history_tree->toolbar, - global_history_window); - - /* load recent URLs */ - fp = fopen(option_recent_path, "r"); - if (!fp) - LOG(("Failed to open file '%s' for reading", - option_recent_path)); - else { - while (fgets(s, MAXIMUM_URL_LENGTH, fp)) { - if (s[strlen(s) - 1] == '\n') - s[strlen(s) - 1] = '\0'; - global_history_add_recent(s); - } - fclose(fp); - } - - global_history_init = true; - urldb_iterate_entries(global_history_add_internal); - global_history_init = false; - tree_initialise(global_history_tree); } /** - * Initialises the base nodes + * Initialise global history tree, at the gui_init2() stage. */ -void ro_gui_global_history_initialise_nodes(void) -{ - struct tm *full_time; - time_t t; - int weekday; - int i; - - /* get the current time */ - t = time(NULL); - if (t == -1) - return; - /* get the time at the start of today */ - full_time = localtime(&t); - weekday = full_time->tm_wday; - full_time->tm_sec = 0; - full_time->tm_min = 0; - full_time->tm_hour = 0; - t = mktime(full_time); - if (t == -1) - return; +void ro_gui_global_history_postinitialise(void) +{ - ro_gui_global_history_initialise_node(messages_get("DateToday"), t, 0); - if (weekday > 0) - ro_gui_global_history_initialise_node( - messages_get("DateYesterday"), t, -1); - for (i = 2; i <= weekday; i++) - ro_gui_global_history_initialise_node(NULL, t, -i); - ro_gui_global_history_initialise_node(messages_get("Date1Week"), - t, -weekday - 7); - ro_gui_global_history_initialise_node(messages_get("Date2Week"), - t, -weekday - 14); - ro_gui_global_history_initialise_node(messages_get("Date3Week"), - t, -weekday - 21); -} + /* Create our toolbar. */ -/** - * Create and initialise a node - */ -void ro_gui_global_history_initialise_node(const char *title, - time_t base, int days_back) -{ - struct tm *full_time; - char buffer[64]; - struct node *node; - - base += days_back * 60 * 60 * 24; - if (!title) { - full_time = localtime(&base); - strftime((char *)&buffer, (size_t)64, "%A", full_time); - node = tree_create_folder_node(NULL, buffer); - } else - node = tree_create_folder_node(NULL, title); - - if (!node) + global_history_window.toolbar = ro_gui_theme_create_toolbar(NULL, + THEME_HISTORY_TOOLBAR); + if (global_history_window.toolbar) + ro_gui_theme_attach_toolbar(global_history_window.toolbar, + global_history_window.window); + + /* Create the treeview with the window and toolbar. */ + + global_history_window.tv = + ro_treeview_create(global_history_window.window, + global_history_window.toolbar, + history_global_get_tree_flags()); + if (global_history_window.tv == NULL) { + LOG(("Failed to allocate treeview")); return; + } - node->retain_in_memory = true; - node->deleted = true; - node->editable = false; - global_history_base_node[global_history_base_node_count] = node; - global_history_base_node_time[global_history_base_node_count] = base; - global_history_base_node_count++; + /* Initialise the global history into the tree. */ + + history_global_initialise( + ro_treeview_get_tree(global_history_window.tv)); + + /* Build the global history window menu. */ + + static const struct ns_menu global_history_definition = { + "History", { + { "History", NO_ACTION, 0 }, + { "_History.Export", HISTORY_EXPORT, &dialog_saveas }, + { "History.Expand", TREE_EXPAND_ALL, 0 }, + { "History.Expand.All", TREE_EXPAND_ALL, 0 }, + { "History.Expand.Folders", TREE_EXPAND_FOLDERS, 0 }, + { "History.Expand.Links", TREE_EXPAND_LINKS, 0 }, + { "History.Collapse", TREE_COLLAPSE_ALL, 0 }, + { "History.Collapse.All", TREE_COLLAPSE_ALL, 0 }, + { "History.Collapse.Folders", TREE_COLLAPSE_FOLDERS, 0 }, + { "History.Collapse.Links", TREE_COLLAPSE_LINKS, 0 }, + { "History.Toolbars", NO_ACTION, 0 }, + { "_History.Toolbars.ToolButtons", TOOLBAR_BUTTONS, 0 }, + { "History.Toolbars.EditToolbar",TOOLBAR_EDIT, 0 }, + { "Selection", TREE_SELECTION, 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} + } + }; + global_history_window.menu = ro_gui_menu_define_menu( + &global_history_definition); + + ro_gui_wimp_event_register_window_menu(global_history_window.window, + global_history_window.menu, + ro_gui_global_history_menu_prepare, + ro_gui_global_history_menu_select, NULL, + ro_gui_global_history_menu_warning, false); } - /** - * Saves the global history's recent URL data. + * Open the global history window. */ -void ro_gui_global_history_save(void) + +void ro_gui_global_history_open(void) { - FILE *fp; - int i; - - /* save recent URLs */ - fp = fopen(option_recent_save, "w"); - if (!fp) - LOG(("Failed to open file '%s' for writing", - option_recent_save)); - else { - for (i = global_history_recent_count - 1; i >= 0; i--) - if (strlen(global_history_recent_url[i]) < - MAXIMUM_URL_LENGTH) - fprintf(fp, "%s\n", - global_history_recent_url[i]); - fclose(fp); + tree_set_redraw(ro_treeview_get_tree(global_history_window.tv), true); + + if (!ro_gui_dialog_open_top(global_history_window.window, + global_history_window.toolbar, 600, 800)) { + ro_treeview_set_origin(global_history_window.tv, 0, + -(ro_gui_theme_toolbar_height( + global_history_window.toolbar))); } } - /** - * Respond to a mouse click + * Prepare the global history menu for opening * - * \param pointer the pointer state - * \return true to indicate click handled + * \param window The window owning the menu. + * \param *menu The menu about to be opened. */ -bool ro_gui_global_history_click(wimp_pointer *pointer) + +void ro_gui_global_history_menu_prepare(wimp_w window, wimp_menu *menu) { - ro_gui_tree_click(pointer, global_history_tree); - if (pointer->buttons == wimp_CLICK_MENU) - ro_gui_menu_create(global_history_menu, pointer->pos.x, - pointer->pos.y, pointer->w); - else - ro_gui_menu_prepare_action(pointer->w, TREE_SELECTION, false); - return true; + bool selection; + + selection = ro_treeview_has_selection(global_history_window.tv); + + ro_gui_menu_set_entry_shaded(global_history_window.menu, + TREE_SELECTION, !selection); + ro_gui_menu_set_entry_shaded(global_history_window.menu, + TREE_CLEAR_SELECTION, !selection); + + ro_gui_menu_set_entry_shaded(global_history_window.menu, + TOOLBAR_BUTTONS, + (global_history_window.toolbar == NULL || + global_history_window.toolbar->editor)); + ro_gui_menu_set_entry_ticked(global_history_window.menu, + TOOLBAR_BUTTONS, + (global_history_window.toolbar != NULL && + (global_history_window.toolbar->display_buttons || + (global_history_window.toolbar->editor)))); + + ro_gui_menu_set_entry_shaded(global_history_window.menu, TOOLBAR_EDIT, + global_history_window.toolbar == NULL); + ro_gui_menu_set_entry_ticked(global_history_window.menu, TOOLBAR_EDIT, + (global_history_window.toolbar != NULL && + global_history_window.toolbar->editor)); + + ro_gui_save_prepare(GUI_SAVE_HISTORY_EXPORT_HTML, + NULL, NULL, NULL, NULL); } - /** - * Adds to the global history + * Handle submenu warnings for the global_hostory menu * - * \param url The URL to add + * \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 global_history_add(const char *url) -{ - const struct url_data *data; - data = urldb_get_url_data(url); - if (!data) - return; - - global_history_add_internal(url, data); +void ro_gui_global_history_menu_warning(wimp_w window, wimp_menu *menu, + wimp_selection *selection, menu_action action) +{ + /* Do nothing */ } /** - * Internal routine to actually perform global history addition + * Handle selections from the global history menu * - * \param url The URL to add - * \param data URL data associated with URL - * \return true (for urldb_iterate_entries) + * \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 global_history_add_internal(const char *url, - const struct url_data *data) -{ - int i, j; - struct node *parent = NULL; - struct node *link; - struct node *node; - bool before = false; - int visit_date; - - assert(url && data); - - visit_date = data->last_visit; - - /* find parent node */ - for (i = 0; i < global_history_base_node_count; i++) { - if (global_history_base_node_time[i] <= visit_date) { - parent = global_history_base_node[i]; - break; - } - } - /* the entry is too old to care about */ - if (!parent) +bool ro_gui_global_history_menu_select(wimp_w window, wimp_menu *menu, + wimp_selection *selection, menu_action action) +{ + switch (action) { + case HISTORY_EXPORT: + ro_gui_dialog_open_persistent(window, dialog_saveas, true); return true; - - if (parent->deleted) { - /* parent was deleted, so find place to insert it */ - link = global_history_tree->root; - - for (j = global_history_base_node_count - 1; j >= 0; j--) { - if (!global_history_base_node[j]->deleted && - global_history_base_node_time[j] > - global_history_base_node_time[i]) { - link = global_history_base_node[j]; - before = true; - break; - } - } - - tree_set_node_selected(global_history_tree, - parent, false); - tree_set_node_expanded(global_history_tree, - parent, false); - tree_link_node(link, parent, before); - - if (!global_history_init) { - tree_recalculate_node(global_history_tree, parent, true); - tree_recalculate_node_positions(global_history_tree, - global_history_tree->root); - tree_redraw_area(global_history_tree, - 0, 0, 16384, 16384); - } - } - - /* find any previous occurance */ - if (!global_history_init) { - node = ro_gui_global_history_find(url); - if (node) { - /* \todo: calculate old/new positions and redraw - * only the relevant portion */ - tree_redraw_area(global_history_tree, - 0, 0, 16384, 16384); - tree_update_URL_node(node, url, data); - tree_delink_node(node); - tree_link_node(parent, node, false); - tree_handle_node_changed(global_history_tree, - node, false, true); -/* ro_gui_tree_scroll_visible(hotlist_tree, - &node->data); -*/ return true; - } - } - - /* Add the node at the bottom */ - node = tree_create_URL_node_shared(parent, url, data); - if ((!global_history_init) && (node)) { - tree_redraw_area(global_history_tree, - node->box.x - NODE_INSTEP, - 0, NODE_INSTEP, 16384); - tree_handle_node_changed(global_history_tree, node, - true, false); + case TREE_EXPAND_ALL: + history_global_expand_all(); + return true; + case TREE_EXPAND_FOLDERS: + history_global_expand_directories(); + return true; + case TREE_EXPAND_LINKS: + history_global_expand_addresses(); + return true; + case TREE_COLLAPSE_ALL: + history_global_collapse_all(); + return true; + case TREE_COLLAPSE_FOLDERS: + history_global_collapse_directories(); + return true; + case TREE_COLLAPSE_LINKS: + history_global_collapse_addresses(); + return true; + case TREE_SELECTION_LAUNCH: + history_global_launch_selected(); + return true; + case TREE_SELECTION_DELETE: + history_global_delete_selected(); + return true; + case TREE_SELECT_ALL: + history_global_select_all(); + return true; + case TREE_CLEAR_SELECTION: + history_global_clear_selection(); + return true; + default: + return false; } - return true; + return false; } /** - * Find an entry in the global history - * - * \param url The URL to find - * \return Pointer to node, or NULL if not found + * Update the theme details of the global history window. */ -struct node *ro_gui_global_history_find(const char *url) + +void ro_gui_global_history_update_theme(void) { - int i; - struct node *node; - struct node_element *element; - - for (i = 0; i < global_history_base_node_count; i++) { - if (!global_history_base_node[i]->deleted) { - for (node = global_history_base_node[i]->child; - node; node = node->next) { - element = tree_find_element(node, - TREE_ELEMENT_URL); - if ((element) && !strcmp(url, element->text)) - return node; - } - } - } - return NULL; + ro_treeview_update_theme(global_history_window.tv); } - /** - * Adds an URL to the recently used list + * Check if a particular window handle is the global history window * - * \param url the URL to add (copied) + * \param window the window in question + * \return true if this window is the global history */ -void global_history_add_recent(const char *url) -{ - int i; - int j = -1; - char *current; - - /* try to find a string already there */ - for (i = 0; i < global_history_recent_count; i++) - if (global_history_recent_url[i] && - !strcmp(global_history_recent_url[i], url)) - j = i; - - /* already at head of list */ - if (j == 0) - return; - if (j < 0) { - /* add to head of list */ - free(global_history_recent_url[ - GLOBAL_HISTORY_RECENT_URLS - 1]); - memmove(&global_history_recent_url[1], - &global_history_recent_url[0], - (GLOBAL_HISTORY_RECENT_URLS - 1) * - sizeof(char *)); - global_history_recent_url[0] = strdup(url); - global_history_recent_count++; - if (global_history_recent_count > GLOBAL_HISTORY_RECENT_URLS) - global_history_recent_count = - GLOBAL_HISTORY_RECENT_URLS; - if (global_history_recent_count == 1) - ro_gui_window_prepare_navigate_all(); - } else { - /* move to head of list */ - current = global_history_recent_url[j]; - for (i = j; i > 0; i--) - global_history_recent_url[i] = - global_history_recent_url[i - 1]; - global_history_recent_url[0] = current; - } +bool ro_gui_global_history_check_window(wimp_w window) +{ +/* if (global_history_window.w == window) + return true; + else*/ + return false; } - /** - * Gets details of the currently used URL list. + * Check if a particular menu handle is the global history menu * - * \param count set to the current number of entries in the URL array on exit - * \return the current URL array + * \param *menu The menu in question. + * \return true if this menu is the global history menu */ -char **global_history_get_recent(int *count) + +bool ro_gui_global_history_check_menu(wimp_menu *menu) { - *count = global_history_recent_count; - return global_history_recent_url; + if (global_history_window.menu == menu) + return true; + else + return false; } + |