GnuCash  3.0-75-g4a4e7a9
gnc-plugin-register.c
1 /*
2  * gnc-plugin-register.c --
3  *
4  * Copyright (C) 2003 Jan Arne Petersen
5  * Author: Jan Arne Petersen <jpetersen@uni-bonn.de>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, contact:
19  *
20  * Free Software Foundation Voice: +1-617-542-5942
21  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
22  * Boston, MA 02110-1301, USA gnu@gnu.org
23  */
24 
25 #include <config.h>
26 
27 #include <gtk/gtk.h>
28 #include <glib/gi18n.h>
29 #include <string.h>
30 
31 #include "gnc-component-manager.h"
32 #include "gnc-plugin-register.h"
34 #include "gnc-prefs.h"
35 
36 
37 static void gnc_plugin_register_class_init (GncPluginRegisterClass *klass);
38 static void gnc_plugin_register_init (GncPluginRegister *plugin);
39 static void gnc_plugin_register_finalize (GObject *object);
40 
41 static void gnc_plugin_register_add_to_window (GncPlugin *plugin, GncMainWindow *window, GQuark type);
42 static void gnc_plugin_register_remove_from_window (GncPlugin *plugin, GncMainWindow *window, GQuark type);
43 
44 /* Command callbacks */
45 static void gnc_plugin_register_cmd_general_ledger (GtkAction *action, GncMainWindowActionData *data);
46 
47 #define PLUGIN_ACTIONS_NAME "gnc-plugin-register-actions"
48 #define PLUGIN_UI_FILENAME "gnc-plugin-register-ui.xml"
49 
50 static GtkActionEntry gnc_plugin_actions [] =
51 {
52  {
53 #ifdef REGISTER2_ENABLED
54  "ToolsGeneralJournalAction", NULL, N_("Old St_yle General Journal"), NULL,
55  N_("Open an old style general journal window"),
56  G_CALLBACK (gnc_plugin_register_cmd_general_ledger)
57 #else
58  "ToolsGeneralJournalAction", NULL, N_("_General Journal"), NULL,
59  N_("Open general journal window"),
60  G_CALLBACK (gnc_plugin_register_cmd_general_ledger)
61 #endif
62  },
63 };
64 static guint gnc_plugin_n_actions = G_N_ELEMENTS (gnc_plugin_actions);
65 
67 {
68  gpointer dummy;
70 
71 #define GNC_PLUGIN_REGISTER_GET_PRIVATE(o) \
72  (G_TYPE_INSTANCE_GET_PRIVATE ((o), GNC_TYPE_PLUGIN_REGISTER, GncPluginRegisterPrivate))
73 
74 static GObjectClass *parent_class = NULL;
75 static QofLogModule log_module = GNC_MOD_GUI;
76 
77 /************************************************************
78  * Other Functions *
79  ************************************************************/
80 
93 static void
94 gnc_plugin_register_pref_changed (gpointer prefs, gchar *pref,
95  gpointer user_data)
96 {
97  ENTER("");
98  gnc_gui_refresh_all ();
99  LEAVE("");
100 }
101 
102 /************************************************************
103  * Object Implementation *
104  ************************************************************/
105 
106 GType
107 gnc_plugin_register_get_type (void)
108 {
109  static GType gnc_plugin_register_type = 0;
110 
111  if (gnc_plugin_register_type == 0)
112  {
113  static const GTypeInfo our_info =
114  {
115  sizeof (GncPluginRegisterClass),
116  NULL, /* base_init */
117  NULL, /* base_finalize */
118  (GClassInitFunc) gnc_plugin_register_class_init,
119  NULL, /* class_finalize */
120  NULL, /* class_data */
121  sizeof (GncPluginRegister),
122  0, /* n_preallocs */
123  (GInstanceInitFunc) gnc_plugin_register_init
124  };
125 
126  gnc_plugin_register_type = g_type_register_static (GNC_TYPE_PLUGIN,
127  "GncPluginRegister",
128  &our_info, 0);
129  }
130 
131  return gnc_plugin_register_type;
132 }
133 
134 GncPlugin *
135 gnc_plugin_register_new (void)
136 {
137  GncPluginRegister *plugin;
138 
139  /* Reference the register page plugin to ensure it exists in
140  * the gtk type system. */
141  GNC_TYPE_PLUGIN_PAGE_REGISTER;
142 
143  plugin = g_object_new (GNC_TYPE_PLUGIN_REGISTER,
144  NULL);
145 
146  return GNC_PLUGIN (plugin);
147 }
148 
149 static void
150 gnc_plugin_register_main_window_page_changed(GncMainWindow *window,
151  GncPluginPage *plugin_page, gpointer user_data)
152 {
153  // We continue only if the plugin_page is a valid
154  if (!plugin_page || !GNC_IS_PLUGIN_PAGE(plugin_page))
155  return;
156 
157  if (gnc_main_window_get_current_page (window) == plugin_page)
158  {
159  if (!GNC_IS_PLUGIN_PAGE_REGISTER(plugin_page))
160  return;
161 
162  // The page changed signal is emitted multiple times so we need
163  // to use an idle_add to change the focus to the register
164  g_idle_remove_by_data (GNC_PLUGIN_PAGE_REGISTER (plugin_page));
165  g_idle_add ((GSourceFunc)gnc_plugin_page_register_focus,
166  GNC_PLUGIN_PAGE_REGISTER (plugin_page));
167  }
168 }
169 
170 static void
171 gnc_plugin_register_class_init (GncPluginRegisterClass *klass)
172 {
173  GObjectClass *object_class = G_OBJECT_CLASS (klass);
174  GncPluginClass *plugin_class = GNC_PLUGIN_CLASS (klass);
175 
176  parent_class = g_type_class_peek_parent (klass);
177 
178  object_class->finalize = gnc_plugin_register_finalize;
179 
180  /* plugin info */
181  plugin_class->plugin_name = GNC_PLUGIN_REGISTER_NAME;
182 
183  /* function overrides */
184  plugin_class->add_to_window = gnc_plugin_register_add_to_window;
185  plugin_class->remove_from_window =
186  gnc_plugin_register_remove_from_window;
187 
188  /* widget addition/removal */
189  plugin_class->actions_name = PLUGIN_ACTIONS_NAME;
190  plugin_class->actions = gnc_plugin_actions;
191  plugin_class->n_actions = gnc_plugin_n_actions;
192  plugin_class->ui_filename = PLUGIN_UI_FILENAME;
193 
194  g_type_class_add_private(klass, sizeof(GncPluginRegisterPrivate));
195 }
196 
197 static void
198 gnc_plugin_register_init (GncPluginRegister *plugin)
199 {
200 }
201 
202 static void
203 gnc_plugin_register_finalize (GObject *object)
204 {
205  g_return_if_fail (GNC_IS_PLUGIN_REGISTER (object));
206 
207  G_OBJECT_CLASS (parent_class)->finalize (object);
208 }
209 
210 /************************************************************
211  * Plugin Function Implementation *
212  ************************************************************/
213 
228 static void
229 gnc_plugin_register_add_to_window (GncPlugin *plugin,
230  GncMainWindow *window,
231  GQuark type)
232 {
233  gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL_REGISTER, NULL,
234  gnc_plugin_register_pref_changed, window);
235 
236  g_signal_connect(window, "page_changed",
237  G_CALLBACK(gnc_plugin_register_main_window_page_changed),
238  plugin);
239 }
240 
241 
253 static void
254 gnc_plugin_register_remove_from_window (GncPlugin *plugin,
255  GncMainWindow *window,
256  GQuark type)
257 {
258  gnc_prefs_remove_cb_by_func (GNC_PREFS_GROUP_GENERAL_REGISTER, NULL,
259  gnc_plugin_register_pref_changed, window);
260 }
261 
262 
263 /************************************************************
264  * Command Callbacks *
265  ************************************************************/
266 
267 static void
268 gnc_plugin_register_cmd_general_ledger (GtkAction *action,
270 {
271  GncPluginPage *page;
272 
273  g_return_if_fail (data != NULL);
274 
276  gnc_main_window_open_page (data->window, page);
277 }
guint n_actions
The number of actions in the actions array.
Definition: gnc-plugin.h:124
gboolean gnc_plugin_page_register_focus(GncPluginPageRegister *page)
Given a pointer to a register plugin page, set the focus to the sheet.
The instance data structure for a content plugin.
gulong gnc_prefs_register_cb(const char *group, const gchar *pref_name, gpointer func, gpointer user_data)
Register a callback that gets triggered when the given preference changes.
Definition: gnc-prefs.c:122
The class data structure for a menu-only plugin.
Definition: gnc-plugin.h:107
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:266
const gchar * ui_filename
The relative name of the XML file describing the menu/toolbar action items.
Definition: gnc-plugin.h:137
GncPluginPage * gnc_main_window_get_current_page(GncMainWindow *window)
Retrieve a pointer to the page that is currently at the front of the specified window.
void gnc_main_window_open_page(GncMainWindow *window, GncPluginPage *page)
Display a data plugin page in a window.
GtkActionEntry * actions
An array of actions that should automatically be added to any GnuCash "main" content window that is o...
Definition: gnc-plugin.h:122
Functions providing a register page for the GnuCash UI.
void(* remove_from_window)(GncPlugin *plugin, GncMainWindow *window, GQuark type)
A callback that will be invoked when this plugin is removed from a window.
Definition: gnc-plugin.h:171
void(* add_to_window)(GncPlugin *plugin, GncMainWindow *window, GQuark type)
A callback that will be invoked when this plugin is added to a window.
Definition: gnc-plugin.h:155
const gchar * actions_name
A name for the set of actions that will be added by this plugin.
Definition: gnc-plugin.h:119
Generic api to store and retrieve preferences.
const gchar * plugin_name
The textual name of this plugin.
Definition: gnc-plugin.h:112
GncPluginPage * gnc_plugin_page_register_new_gl(void)
Create a new "register" plugin page containing a general journal.
The instance data structure for a menu-only plugin.
Definition: gnc-plugin.h:100
#define PLUGIN_ACTIONS_NAME
The label given to the main window for this plugin.
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:276
The instance data structure for a main window object.
#define PLUGIN_UI_FILENAME
The name of the UI description file for this plugin.
const gchar * QofLogModule
QofLogModule declaration.
Definition: qofid.h:89
void gnc_prefs_remove_cb_by_func(const gchar *group, const gchar *pref_name, gpointer func, gpointer user_data)
Remove a function that was registered for a callback when the given preference changed.
Definition: gnc-prefs.c:135