Cleanup code, comments and refactor a bit.

This commit is contained in:
Matthew Brush 2011-09-22 00:08:39 -07:00 committed by Matthew Brush
parent a42161181a
commit 7c9430884f
5 changed files with 113 additions and 80 deletions

View File

@ -1,7 +1,24 @@
/*
* Compatibility wrapper for old Glade2 code generation file.
* interface.c - this file is part of Geany, a fast and lightweight IDE
*
* DO NOT ADD NEW CODE HERE!
* Copyright 2006-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
* Copyright 2006-2011 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
*
* This program 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; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $Id$
*/
#ifdef HAVE_CONFIG_H
@ -14,31 +31,37 @@
#include "interface.h"
#include "support.h"
#define GLADE_HOOKUP_OBJECT(component, widget, name) \
g_object_set_data_full(G_OBJECT(component), name, \
g_object_ref(G_OBJECT(widget)), (GDestroyNotify) g_object_unref)
#define GLADE_HOOKUP_OBJECT_NO_REF(component, widget, name) \
g_object_set_data(G_OBJECT(component), name, G_OBJECT(widget))
static gchar *ui_file = NULL;
static GtkBuilder *builder = NULL;
static GSList *objects = NULL;
static void init_builder(void);
static gchar* interface_file = NULL;
static GtkBuilder* builder = NULL;
static GSList* local_objects = NULL;
/* Returns a widget from a name.
*
* Call it with the name of the GObject you want returned. This is
* similar to @a ui_lookup_widget except that it supports arbitrary
* GObjects.
*
* @note The GObject must either be in the GtkBuilder/Glade file or
* have been added with the function @a interface_add_object.
*
* @param widget Widget with the @a widget_name property set.
* @param widget_name Name to lookup.
* @return The widget found.
*
* @see ui_hookup_widget().
* @see interface_add_object().
* @since 0.21
*/
GObject *interface_get_object(const gchar *name)
{
GSList *iter;
init_builder();
g_return_val_if_fail(name != NULL, NULL);
g_return_val_if_fail(objects != NULL, NULL);
g_return_val_if_fail(local_objects != NULL, NULL);
for (iter = objects; iter != NULL; iter = g_slist_next(iter))
for (iter = local_objects; iter != NULL; iter = g_slist_next(iter))
{
if (G_IS_OBJECT(iter->data))
{
@ -59,15 +82,23 @@ GObject *interface_get_object(const gchar *name)
}
}
g_debug("Can't locate: %s", name);
return NULL;
}
void interface_set_object(GObject *obj, const gchar *name)
/* Sets a name to lookup GObject @a obj.
*
* This is similar to @a ui_hookup_widget() except that it supports
* arbitrary GObjects.
*
* @param obj GObject.
* @param name Name.
*
* @see ui_hookup_widget().
* @since 0.21
**/
void interface_add_object(GObject *obj, const gchar *name)
{
init_builder();
g_return_if_fail(G_IS_OBJECT(obj));
if (! name)
@ -76,107 +107,102 @@ void interface_set_object(GObject *obj, const gchar *name)
name = g_object_get_data(obj, "gtk-builder-name");
if (! name && GTK_IS_BUILDABLE(obj))
name = gtk_buildable_get_name(GTK_BUILDABLE(obj));
else if (! name && GTK_IS_WIDGET(obj))
if (! name && GTK_IS_WIDGET(obj))
name = gtk_widget_get_name(GTK_WIDGET(obj));
g_return_if_fail(name);
g_object_set_data_full(obj, "name", g_strdup(name), g_free);
objects = g_slist_append(objects, g_object_ref(obj));
local_objects = g_slist_append(local_objects, g_object_ref(obj));
}
static void init_builder(void)
void interface_init(void)
{
GError *error;
GSList *iter, *all_objects;
GtkCellRenderer *renderer;
if (builder) /* only run once */
return;
g_return_if_fail(builder == NULL);
ui_file = g_build_filename(GEANY_DATADIR, "geany", "geany.glade", NULL);
interface_file = g_build_filename(GEANY_DATADIR, "geany", "geany.glade", NULL);
if (! (builder = gtk_builder_new()))
{
g_error("failed to initialize the user-interface");
g_error("Failed to initialize the user-interface");
return;
}
error = NULL;
if (! gtk_builder_add_from_file(builder, ui_file, &error))
if (! gtk_builder_add_from_file(builder, interface_file, &error))
{
g_error("failed to load the user-interface file: %s",
error->message);
g_error("Failed to load the user-interface file: %s", error->message);
g_error_free(error);
return;
}
/* TODO: make sure all callbacks have G_MODULE_EXPORT */
gtk_builder_connect_signals(builder, NULL);
/* TODO: set translation domain? */;
/* TODO: set translation domain on GtkBuilder? */;
all_objects = gtk_builder_get_objects(builder);
for (iter = all_objects; iter != NULL; iter = g_slist_next(iter))
{
interface_set_object(iter->data, NULL);
//g_debug("ADDED OBJECT: %s", g_object_get_data(iter->data, "name"));
interface_add_object(iter->data, NULL);
/* FIXME: Hack to get Glade 3/GtkBuilder combo boxes working */
if (GTK_IS_COMBO_BOX(iter->data))
{
GtkCellRenderer *r = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(iter->data), r, TRUE);
renderer = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(iter->data), renderer, TRUE);
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(iter->data),
r, "text", 0, NULL);
renderer, "text", 0, NULL);
}
}
g_slist_free(all_objects);
}
/* TODO: Hookup to atexit() or make public and call at program exit? */
/* Ensure everything is freed. */
static void finalize_builder(void)
void interface_finalize(void)
{
GSList *iter;
g_return_if_fail(GTK_IS_BUILDER(builder));
for (iter = objects; iter != NULL; iter = g_slist_next(iter))
g_object_unref(iter->data);
g_slist_free(objects);
g_free(ui_file);
g_slist_free(local_objects);
g_free(interface_file);
g_object_unref(builder);
}
/* Compatibility functions with Glade 2 generated code.
*
* TODO: remove these
*/
GtkWidget *create_window1(void)
{
init_builder();
return GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
}
GtkWidget *create_toolbar_popup_menu1(void)
{
init_builder();
return GTK_WIDGET(gtk_builder_get_object(builder, "toolbar_popup_menu1"));
}
GtkWidget *create_edit_menu1(void)
{
init_builder();
return GTK_WIDGET(gtk_builder_get_object(builder, "edit_menu1"));
}
GtkWidget *create_prefs_dialog(void)
{
init_builder();
return GTK_WIDGET(gtk_builder_get_object(builder, "prefs_dialog"));
}
GtkWidget *create_project_dialog(void)
{
init_builder();
return GTK_WIDGET(gtk_builder_get_object(builder, "project_dialog"));
}

View File

@ -1,14 +1,36 @@
/*
* Compatibility wrapper for old Glade2 code generation file.
* interface.h - this file is part of Geany, a fast and lightweight IDE
*
* DO NOT ADD NEW CODE HERE!
* Copyright 2006-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
* Copyright 2006-2011 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
*
* This program 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; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $Id$
*/
GtkWidget* create_window1 (void);
GtkWidget* create_toolbar_popup_menu1 (void);
GtkWidget* create_edit_menu1 (void);
GtkWidget* create_prefs_dialog (void);
GtkWidget* create_project_dialog (void);
GObject *interface_get_object(const gchar *name);
void interface_set_object(GObject *obj, const gchar *name);
void interface_init (void);
void interface_finalize (void);
GObject* interface_get_object (const gchar *name);
void interface_add_object (GObject *obj, const gchar *name);
/* TODO: remove these and just use ui_lookup_widget() */
GtkWidget* create_window1 (void);
GtkWidget* create_toolbar_popup_menu1 (void);
GtkWidget* create_edit_menu1 (void);
GtkWidget* create_prefs_dialog (void);
GtkWidget* create_project_dialog (void);

View File

@ -223,6 +223,8 @@ static void apply_settings(void)
static void main_init(void)
{
/* inits */
interface_init();
main_widgets.window = NULL;
app->project = NULL;
ui_widgets.open_fontsel = NULL;
@ -1179,6 +1181,7 @@ void main_quit()
filetypes_free_types();
ui_finalize();
log_finalize();
interface_finalize();
tm_workspace_free(TM_WORK_OBJECT(app->tm_workspace));
g_free(app->configdir);

View File

@ -2214,22 +2214,6 @@ void ui_widget_set_tooltip_text(GtkWidget *widget, const gchar *text)
}
/** Sets a name to lookup @a widget from @a owner.
* @param owner Usually a window, dialog or popup menu.
* @param widget Widget.
* @param widget_name Name.
* @see ui_lookup_widget().
*
* @since 0.16
**/
void ui_hookup_widget_real(GtkWidget *owner, GObject *widget, const gchar *widget_name)
{
g_return_if_fail(GTK_IS_WIDGET(widget));
g_return_if_fail(widget_name != NULL);
interface_set_object(G_OBJECT(widget), widget_name);
}
/** Returns a widget from a name in a component, usually created by Glade.
* Call it with the toplevel widget in the component (i.e. a window/dialog),
* or alternatively any widget in the component, and the name of the widget

View File

@ -32,7 +32,7 @@
* @since 0.16
**/
#define ui_hookup_widget(owner, widget, widget_name) \
ui_hookup_widget_real(owner, G_OBJECT(widget), widget_name)
interface_add_object(G_OBJECT(widget), widget_name)
/** Interface preferences */
@ -205,8 +205,6 @@ void ui_auto_separator_add_ref(GeanyAutoSeparator *autosep, GtkWidget *item);
void ui_widget_set_tooltip_text(GtkWidget *widget, const gchar *text);
void ui_hookup_widget_real(GtkWidget *owner, GObject *widget, const gchar *widget_name);
GtkWidget *ui_lookup_widget(GtkWidget *widget, const gchar *widget_name);
void ui_widget_set_sensitive(GtkWidget *widget, gboolean set);