From 2314a322d401081dc105762332ba17ad94d9c124 Mon Sep 17 00:00:00 2001 From: Robert Fewell <14uBobIT@gmail.com> Date: Fri, 29 Dec 2017 10:20:28 +0000 Subject: [PATCH] Make sure the tree view has focus on Account tree page load --- gnucash/gnome/gnc-plugin-account-tree.c | 40 ++++++++++++++++++++ gnucash/gnome/gnc-plugin-page-account-tree.c | 13 +++++++ gnucash/gnome/gnc-plugin-page-account-tree.h | 9 +++++ 3 files changed, 62 insertions(+) diff --git a/gnucash/gnome/gnc-plugin-account-tree.c b/gnucash/gnome/gnc-plugin-account-tree.c index 675da22d18..c9ebc0c9d3 100644 --- a/gnucash/gnome/gnc-plugin-account-tree.c +++ b/gnucash/gnome/gnc-plugin-account-tree.c @@ -44,6 +44,8 @@ static void gnc_plugin_account_tree_class_init (GncPluginAccountTreeClass *klass); static void gnc_plugin_account_tree_init (GncPluginAccountTree *plugin); static void gnc_plugin_account_tree_finalize (GObject *object); +static void gnc_plugin_account_tree_add_to_window (GncPlugin *plugin, + GncMainWindow *window, GQuark type); /* Command callbacks */ static void gnc_plugin_account_tree_cmd_new_account_tree (GtkAction *action, GncMainWindowActionData *data); @@ -125,6 +127,27 @@ gnc_plugin_account_tree_new (void) } +static void +gnc_plugin_account_tree_main_window_page_changed (GncMainWindow *window, + GncPluginPage *plugin_page, gpointer user_data) +{ + // We continue only if the plugin_page is a valid + if (!plugin_page || !GNC_IS_PLUGIN_PAGE(plugin_page)) + return; + + if (gnc_main_window_get_current_page (window) == plugin_page) + { + if (!GNC_IS_PLUGIN_PAGE_ACCOUNT_TREE(plugin_page)) + return; + + // The page changed signal is emitted multiple times so we need + // to use an idle_add to change the focus to the tree view + g_idle_add ((GSourceFunc)gnc_plugin_page_account_tree_focus, + GNC_PLUGIN_PAGE_ACCOUNT_TREE (plugin_page)); + } +} + + /** Initialize the class for a new account tree plugin. This will set * up any function pointers that override functions in the parent * class, and also configure the private data storage for this @@ -145,6 +168,9 @@ gnc_plugin_account_tree_class_init (GncPluginAccountTreeClass *klass) /* plugin info */ plugin_class->plugin_name = GNC_PLUGIN_ACCOUNT_TREE_NAME; + /* function overrides */ + plugin_class->add_to_window = gnc_plugin_account_tree_add_to_window; + /* widget addition/removal */ plugin_class->actions_name = PLUGIN_ACTIONS_NAME; plugin_class->actions = gnc_plugin_actions; @@ -182,6 +208,20 @@ gnc_plugin_account_tree_finalize (GObject *object) G_OBJECT_CLASS (parent_class)->finalize (object); } + +/** + * Called when this plugin is added to a main window. Connect a few callbacks + * here to track page changes. + * + */ +static void gnc_plugin_account_tree_add_to_window (GncPlugin *plugin, + GncMainWindow *mainwindow, + GQuark type) +{ + g_signal_connect(mainwindow, "page_changed", + G_CALLBACK(gnc_plugin_account_tree_main_window_page_changed), + plugin); +} /************************************************************ * Command Callbacks * ************************************************************/ diff --git a/gnucash/gnome/gnc-plugin-page-account-tree.c b/gnucash/gnome/gnc-plugin-page-account-tree.c index 1630dfe46b..402f51e69c 100644 --- a/gnucash/gnome/gnc-plugin-page-account-tree.c +++ b/gnucash/gnome/gnc-plugin-page-account-tree.c @@ -587,6 +587,19 @@ gnc_plugin_page_account_tree_get_current_account (GncPluginPageAccountTree *page return account; } +gboolean +gnc_plugin_page_account_tree_focus (GncPluginPageAccountTree *page) +{ + if (GNC_IS_PLUGIN_PAGE_ACCOUNT_TREE(page)) + { + GncPluginPageAccountTreePrivate *priv = GNC_PLUGIN_PAGE_ACCOUNT_TREE_GET_PRIVATE(page); + GtkTreeView *view = GTK_TREE_VIEW(priv->tree_view); + + if (!gtk_widget_is_focus (GTK_WIDGET(view))) + gtk_widget_grab_focus (GTK_WIDGET(view)); + } + return FALSE; +} /* Virtual Functions */ diff --git a/gnucash/gnome/gnc-plugin-page-account-tree.h b/gnucash/gnome/gnc-plugin-page-account-tree.h index 57b8629878..2dad1e7e42 100644 --- a/gnucash/gnome/gnc-plugin-page-account-tree.h +++ b/gnucash/gnome/gnc-plugin-page-account-tree.h @@ -95,6 +95,15 @@ GncPluginPage *gnc_plugin_page_account_tree_new (void); Account * gnc_plugin_page_account_tree_get_current_account (GncPluginPageAccountTree *page); +/** Given a pointer to an account tree plugin page, set the focus to + * the GtkTreeView. This is used in a g_idle_add so return FALSE. + * + * @param page The "account tree" page. + * + * @return FALSE; + */ +gboolean gnc_plugin_page_account_tree_focus (GncPluginPageAccountTree *page); + /** Given a pointer to an account, the account tree will open * and the account will be selected (if any). *