GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-embedded-window.c
1 /*
2  * gnc-main-window.c -- GtkWindow which represents the
3  * GnuCash main window.
4  *
5  * Copyright (C) 2003 Jan Arne Petersen <[email protected]>
6  * Copyright (C) 2003 David Hampton <[email protected]>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, contact:
20  *
21  * Free Software Foundation Voice: +1-617-542-5942
22  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
23  * Boston, MA 02110-1301, USA [email protected]
24  */
25 
26 #include "config.h"
27 
28 #include <gtk/gtk.h>
29 
30 #include "gnc-embedded-window.h"
31 
32 #include "gnc-engine.h"
33 #include "gnc-filepath-utils.h"
34 #include "gnc-gnome-utils.h"
35 #include "gnc-gobject-utils.h"
36 #include "gnc-gui-query.h"
37 #include "gnc-plugin.h"
38 #include "gnc-plugin-manager.h"
39 #include "gnc-ui.h"
40 #include "gnc-window.h"
41 
42 /* Static Globals *******************************************************/
43 
45 static QofLogModule log_module = GNC_MOD_GUI;
47 static GObjectClass *parent_class = NULL;
48 
49 
50 /* Declarations *********************************************************/
51 static void gnc_embedded_window_class_init (GncEmbeddedWindowClass *klass);
52 static void gnc_embedded_window_init (GncEmbeddedWindow *window, GncEmbeddedWindowClass *klass);
53 static void gnc_embedded_window_finalize (GObject *object);
54 static void gnc_embedded_window_dispose (GObject *object);
55 
56 static void gnc_window_embedded_window_init (GncWindowIface *iface);
57 
58 static void gnc_embedded_window_setup_window (GncEmbeddedWindow *window);
59 
60 
63 {
68  GtkWidget *menu_dock;
69  /* The toolbar created by the UI manager. This pointer provides
70  * easy access for showing/hiding the toolbar. */
71  GtkWidget *toolbar;
75  GtkWidget *statusbar;
76 
80  GtkActionGroup *action_group;
81 
86  GtkWidget *parent_window;
88 
89 #define GNC_EMBEDDED_WINDOW_GET_PRIVATE(o) \
90  (G_TYPE_INSTANCE_GET_PRIVATE ((o), GNC_TYPE_EMBEDDED_WINDOW, GncEmbeddedWindowPrivate))
91 
92 
93 
94 /* Get the type of a gnc embedded window. */
95 GType
97 {
98  static GType gnc_embedded_window_type = 0;
99 
100  if (gnc_embedded_window_type == 0)
101  {
102  static const GTypeInfo our_info =
103  {
104  sizeof (GncEmbeddedWindowClass),
105  NULL,
106  NULL,
107  (GClassInitFunc) gnc_embedded_window_class_init,
108  NULL,
109  NULL,
110  sizeof (GncEmbeddedWindow),
111  0,
112  (GInstanceInitFunc) gnc_embedded_window_init
113  };
114 
115  static const GInterfaceInfo plugin_info =
116  {
117  (GInterfaceInitFunc) gnc_window_embedded_window_init,
118  NULL,
119  NULL
120  };
121 
122  gnc_embedded_window_type = g_type_register_static (GTK_TYPE_VBOX,
123  "GncEmbeddedWindow",
124  &our_info, 0);
125  g_type_add_interface_static (gnc_embedded_window_type,
126  GNC_TYPE_WINDOW,
127  &plugin_info);
128  }
129 
130  return gnc_embedded_window_type;
131 }
132 
133 
134 /* Display a data plugin page in a window. */
135 void
137  GncPluginPage *page)
138 {
140 
141  g_return_if_fail (GNC_IS_EMBEDDED_WINDOW (window));
142  g_return_if_fail (GNC_IS_PLUGIN_PAGE (page));
143  priv = GNC_EMBEDDED_WINDOW_GET_PRIVATE(window);
144  g_return_if_fail (priv->page == NULL);
145 
146  ENTER("window %p, page %p", window, page);
147  priv->page = page;
148  page->window = GTK_WIDGET(window);
150 
151  gtk_box_pack_end(GTK_BOX(window), page->notebook_page, TRUE, TRUE, 2);
152  gnc_plugin_page_inserted (page);
153 
154  gnc_plugin_page_merge_actions (page, window->ui_merge);
155  LEAVE(" ");
156 }
157 
158 
159 /* Remove a data plugin page from a window. */
160 void
162  GncPluginPage *page)
163 {
165 
166  g_return_if_fail (GNC_IS_EMBEDDED_WINDOW (window));
167  g_return_if_fail (GNC_IS_PLUGIN_PAGE (page));
168  priv = GNC_EMBEDDED_WINDOW_GET_PRIVATE(window);
169  g_return_if_fail (priv->page == page);
170 
171  ENTER("window %p, page %p", window, page);
172 
173  if (!page->notebook_page)
174  {
175  LEAVE("no displayed widget");
176  return;
177  }
178 
179  gtk_container_remove (GTK_CONTAINER(window), GTK_WIDGET(page->notebook_page));
180  priv->page = NULL;
181  gnc_plugin_page_removed (page);
182 
184  gtk_ui_manager_ensure_update (window->ui_merge);
185 
187  g_object_unref(page);
188  LEAVE(" ");
189 }
190 
191 
192 /* Retrieve the plugin that is embedded in the specified window. */
195 {
197 
198  priv = GNC_EMBEDDED_WINDOW_GET_PRIVATE(window);
199  return priv->page;
200 }
201 
202 
209 static void
210 gnc_embedded_window_class_init (GncEmbeddedWindowClass *klass)
211 {
212  GObjectClass *object_class;
213  ENTER("klass %p", klass);
214  object_class = G_OBJECT_CLASS (klass);
215 
216  parent_class = g_type_class_peek_parent (klass);
217 
218  object_class->finalize = gnc_embedded_window_finalize;
219  object_class->dispose = gnc_embedded_window_dispose;
220 
221  g_type_class_add_private(klass, sizeof(GncEmbeddedWindowPrivate));
222  LEAVE(" ");
223 }
224 
225 
234 static void
235 gnc_embedded_window_init (GncEmbeddedWindow *window,
236  GncEmbeddedWindowClass *klass)
237 {
238  ENTER("window %p", window);
239 
240  gnc_embedded_window_setup_window (window);
241 
242  gnc_gobject_tracking_remember(G_OBJECT(window),
243  G_OBJECT_CLASS(klass));
244  LEAVE(" ");
245 }
246 
247 
251 static void
252 gnc_embedded_window_finalize (GObject *object)
253 {
254  g_return_if_fail (object != NULL);
255  g_return_if_fail (GNC_IS_EMBEDDED_WINDOW (object));
256 
257  ENTER("object %p", object);
259  G_OBJECT_CLASS (parent_class)->finalize (object);
260  LEAVE(" ");
261 }
262 
263 
268 static void
269 gnc_embedded_window_dispose (GObject *object)
270 {
271  GncEmbeddedWindow *window;
273 
274  g_return_if_fail (object != NULL);
275  g_return_if_fail (GNC_IS_EMBEDDED_WINDOW (object));
276 
277  ENTER("object %p", object);
278  window = GNC_EMBEDDED_WINDOW (object);
279  priv = GNC_EMBEDDED_WINDOW_GET_PRIVATE(window);
280  if (priv->page)
281  {
282  DEBUG("unreffing page %p (count currently %d)", priv->page,
283  G_OBJECT(priv->page)->ref_count);
284  g_object_unref(priv->page);
285  priv->page = NULL;
286  }
287 
288  G_OBJECT_CLASS (parent_class)->dispose (object);
289  LEAVE(" ");
290 }
291 
292 
293 static void
294 gnc_embedded_window_add_widget (GtkUIManager *merge,
295  GtkWidget *widget,
296  GncEmbeddedWindow *window)
297 {
299 
300  ENTER("merge %p, new widget %p, window %p", merge, widget, window);
301  priv = GNC_EMBEDDED_WINDOW_GET_PRIVATE(window);
302  if (GTK_IS_TOOLBAR (widget))
303  {
304  priv->toolbar = widget;
305  }
306 
307  gtk_box_pack_start (GTK_BOX (priv->menu_dock), widget, FALSE, FALSE, 0);
308  gtk_widget_show (widget);
309  LEAVE(" ");
310 }
311 
312 
316 static void
317 gnc_embedded_window_setup_window (GncEmbeddedWindow *window)
318 {
320 
321  ENTER("window %p", window);
322  priv = GNC_EMBEDDED_WINDOW_GET_PRIVATE(window);
323 
324  /* Create widgets and add them to the window */
325  gtk_widget_show (GTK_WIDGET(window));
326 
327  priv->menu_dock = gtk_vbox_new (FALSE, 0);
328  gtk_widget_show (priv->menu_dock);
329  gtk_box_pack_start (GTK_BOX (window), priv->menu_dock, FALSE, TRUE, 0);
330 
331  priv->statusbar = gtk_statusbar_new ();
332  gtk_statusbar_set_has_resize_grip (GTK_STATUSBAR(priv->statusbar), FALSE);
333  gtk_widget_show (priv->statusbar);
334  gtk_box_pack_end (GTK_BOX (window), priv->statusbar, FALSE, TRUE, 0);
335 
336  window->ui_merge = gtk_ui_manager_new ();
337  g_signal_connect (G_OBJECT (window->ui_merge), "add_widget",
338  G_CALLBACK (gnc_embedded_window_add_widget), window);
339 
340  priv->action_group = NULL;
341  LEAVE(" ");
342 }
343 
344 
347 gnc_embedded_window_new (const gchar *action_group_name,
348  GtkActionEntry *action_entries,
349  gint n_action_entries,
350  const gchar *ui_filename,
351  GtkWidget *enclosing_win,
352  gboolean add_accelerators,
353  gpointer user_data)
354 {
356  GncEmbeddedWindow *window;
357  gchar *ui_fullname;
358  GError *error = NULL;
359  guint merge_id;
360 
361  ENTER("group %s, first %p, num %d, ui file %s, parent %p, add accelerators %d, user data %p",
362  action_group_name, action_entries, n_action_entries, ui_filename,
363  enclosing_win, add_accelerators, user_data);
364  window = g_object_new (GNC_TYPE_EMBEDDED_WINDOW, NULL);
365  priv = GNC_EMBEDDED_WINDOW_GET_PRIVATE(window);
366 
367  /* Determine the full pathname of the ui file */
368  ui_fullname = gnc_filepath_locate_ui_file (ui_filename);
369  g_return_val_if_fail (ui_fullname != NULL, NULL);
370 
371  priv->parent_window = enclosing_win;
372 
373  /* Create menu and toolbar information */
374  priv->action_group = gtk_action_group_new (action_group_name);
376  gtk_action_group_add_actions (priv->action_group, action_entries,
377  n_action_entries, user_data);
378  gtk_ui_manager_insert_action_group (window->ui_merge, priv->action_group, 0);
379  merge_id = gtk_ui_manager_add_ui_from_file (window->ui_merge, ui_fullname,
380  &error);
381 
382  /* Error checking */
383  g_assert(merge_id || error);
384  if (error)
385  {
386  g_critical("Failed to load ui file.\n Filename %s\n Error %s",
387  ui_fullname, error->message);
388  g_error_free(error);
389  g_free(ui_fullname);
390  LEAVE("window %p", window);
391  return window;
392  }
393 
394  /* Add accelerators (if wanted) */
395  if (add_accelerators)
396  gtk_window_add_accel_group (GTK_WINDOW(enclosing_win),
397  gtk_ui_manager_get_accel_group(window->ui_merge));
398 
399  gtk_ui_manager_ensure_update (window->ui_merge);
400  g_free(ui_fullname);
401  LEAVE("window %p", window);
402  return window;
403 }
404 
405 
411 static GtkWindow *
412 gnc_embedded_window_get_gtk_window (GncWindow *window_in)
413 {
414  GncEmbeddedWindow *window;
416 
417  g_return_val_if_fail (GNC_IS_EMBEDDED_WINDOW (window_in), NULL);
418 
419  window = GNC_EMBEDDED_WINDOW(window_in);
420  priv = GNC_EMBEDDED_WINDOW_GET_PRIVATE(window);
421  return GTK_WINDOW(priv->parent_window);
422 }
423 
424 
430 static GtkWidget *
431 gnc_embedded_window_get_statusbar (GncWindow *window_in)
432 {
434  GncEmbeddedWindow *window;
435 
436  g_return_val_if_fail (GNC_IS_EMBEDDED_WINDOW (window_in), NULL);
437 
438  window = GNC_EMBEDDED_WINDOW(window_in);
439  priv = GNC_EMBEDDED_WINDOW_GET_PRIVATE(window);
440  return priv->statusbar;
441 }
442 
443 
448 static void
449 gnc_window_embedded_window_init (GncWindowIface *iface)
450 {
451  iface->get_gtk_window = gnc_embedded_window_get_gtk_window;
452  iface->get_statusbar = gnc_embedded_window_get_statusbar;
453 }
GncEmbeddedWindow * gnc_embedded_window_new(const gchar *action_group_name, GtkActionEntry *action_entries, gint n_action_entries, const gchar *ui_filename, GtkWidget *enclosing_win, gboolean add_accelerators, gpointer user_data)
void gnc_plugin_page_destroy_widget(GncPluginPage *plugin_page)
void gnc_gobject_tracking_remember(GObject *object, GObjectClass *klass)
void gnc_embedded_window_close_page(GncEmbeddedWindow *window, GncPluginPage *page)
void gnc_gobject_tracking_forget(GObject *object)
#define DEBUG(format, args...)
Definition: qoflog.h:255
Functions that are supported by all types of windows.
void gnc_embedded_window_open_page(GncEmbeddedWindow *window, GncPluginPage *page)
void gnc_plugin_page_merge_actions(GncPluginPage *page, GtkUIManager *ui_merge)
Plugin management functions for the GnuCash UI.
GtkWidget * window
gchar * gnc_filepath_locate_ui_file(const gchar *name)
#define ENTER(format, args...)
Definition: qoflog.h:261
void gnc_plugin_page_unmerge_actions(GncPluginPage *page, GtkUIManager *ui_merge)
GtkWidget * gnc_plugin_page_create_widget(GncPluginPage *plugin_page)
Gobject helper routines.
Gnome specific utility functions.
All type declarations for the whole Gnucash engine.
GncPluginPage * gnc_embedded_window_get_page(GncEmbeddedWindow *window)
Functions that are supported by all types of windows.
Functions for adding plugins to a GnuCash window.
GType gnc_embedded_window_get_type(void)
GtkUIManager * ui_merge
#define LEAVE(format, args...)
Definition: qoflog.h:271
File path resolution utility functions.
void gnc_gtk_action_group_set_translation_domain(GtkActionGroup *action_group, const gchar *domain)
GtkWidget * notebook_page
const gchar * QofLogModule
Definition: qofid.h:89