Rename LdLibraryPane to LdCategoryView.

And make it load categories recursively using itself.
This commit is contained in:
Přemysl Eric Janouch 2012-08-30 00:54:38 +02:00
parent 77aad430ef
commit 3290aa877d
7 changed files with 404 additions and 306 deletions

View File

@ -124,7 +124,7 @@ set (liblogdiag_SOURCES
liblogdiag/ld-diagram-connection.c
liblogdiag/ld-diagram-view.c
liblogdiag/ld-library.c
liblogdiag/ld-library-pane.c
liblogdiag/ld-category-view.c
liblogdiag/ld-category.c
liblogdiag/ld-symbol.c
liblogdiag/ld-lua.c
@ -141,7 +141,7 @@ set (liblogdiag_HEADERS
liblogdiag/ld-diagram-connection.h
liblogdiag/ld-diagram-view.h
liblogdiag/ld-library.h
liblogdiag/ld-library-pane.h
liblogdiag/ld-category-view.h
liblogdiag/ld-category.h
liblogdiag/ld-symbol.h
liblogdiag/ld-lua.h

View File

@ -0,0 +1,323 @@
/*
* ld-category-view.c
*
* This file is a part of logdiag.
* Copyright Přemysl Janouch 2011. All rights reserved.
*
* See the file LICENSE for licensing information.
*
*/
#include "liblogdiag.h"
#include "config.h"
/**
* SECTION:ld-category-view
* @short_description: A category view widget
* @see_also: #LdCategory
*
* #LdCategoryView enables the user to drag symbols from #LdLibrary
* onto #LdDiagramView.
*/
/*
* LdCategoryViewPrivate:
* @category: a category object assigned as a model.
* @expander_prefix: a string to prepend to subcategory labels in expanders.
*/
struct _LdCategoryViewPrivate
{
LdCategory *category;
gchar *expander_prefix;
};
enum
{
PROP_0,
PROP_CATEGORY
};
static void ld_category_view_get_property (GObject *object, guint property_id,
GValue *value, GParamSpec *pspec);
static void ld_category_view_set_property (GObject *object, guint property_id,
const GValue *value, GParamSpec *pspec);
static void ld_category_view_dispose (GObject *gobject);
static void reload_category (LdCategoryView *self);
static void load_category_cb (gpointer data, gpointer user_data);
G_DEFINE_TYPE (LdCategoryView, ld_category_view, GTK_TYPE_VBOX);
static void
ld_category_view_class_init (LdCategoryViewClass *klass)
{
GObjectClass *object_class;
GParamSpec *pspec;
object_class = G_OBJECT_CLASS (klass);
object_class->get_property = ld_category_view_get_property;
object_class->set_property = ld_category_view_set_property;
object_class->dispose = ld_category_view_dispose;
/**
* LdCategoryView:category:
*
* The #LdCategory this widget retrieves content from.
*/
pspec = g_param_spec_object ("category", "Category",
"The symbol category that is shown by this widget.",
LD_TYPE_LIBRARY, G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_CATEGORY, pspec);
g_type_class_add_private (klass, sizeof (LdCategoryViewPrivate));
}
static void
ld_category_view_init (LdCategoryView *self)
{
self->priv = G_TYPE_INSTANCE_GET_PRIVATE
(self, LD_TYPE_CATEGORY_VIEW, LdCategoryViewPrivate);
}
static void
ld_category_view_dispose (GObject *gobject)
{
LdCategoryView *self;
self = LD_CATEGORY_VIEW (gobject);
ld_category_view_set_category (self, NULL);
g_free (self->priv->expander_prefix);
self->priv->expander_prefix = NULL;
/* Chain up to the parent class. */
G_OBJECT_CLASS (ld_category_view_parent_class)->dispose (gobject);
}
static void
ld_category_view_get_property (GObject *object, guint property_id,
GValue *value, GParamSpec *pspec)
{
LdCategoryView *self;
self = LD_CATEGORY_VIEW (object);
switch (property_id)
{
case PROP_CATEGORY:
g_value_set_object (value, ld_category_view_get_category (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
ld_category_view_set_property (GObject *object, guint property_id,
const GValue *value, GParamSpec *pspec)
{
LdCategoryView *self;
self = LD_CATEGORY_VIEW (object);
switch (property_id)
{
case PROP_CATEGORY:
ld_category_view_set_category (self,
LD_CATEGORY (g_value_get_object (value)));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
/**
* ld_category_view_new:
*
* Create an instance.
*/
GtkWidget *
ld_category_view_new (void)
{
return g_object_new (LD_TYPE_CATEGORY_VIEW, NULL);
}
/**
* ld_category_view_set_category:
* @self: an #LdCategoryView object.
* @category: (allow-none): the category to be assigned to the widget.
*
* Assign an #LdCategory object to the widget.
*/
void
ld_category_view_set_category (LdCategoryView *self, LdCategory *category)
{
g_return_if_fail (LD_IS_CATEGORY_VIEW (self));
g_return_if_fail (LD_IS_CATEGORY (category) || category == NULL);
if (self->priv->category)
{
g_signal_handlers_disconnect_by_func (self->priv->category,
reload_category, self);
g_object_unref (self->priv->category);
}
self->priv->category = category;
if (category)
{
g_signal_connect_data (category, "children-changed",
G_CALLBACK (reload_category), self,
NULL, G_CONNECT_AFTER | G_CONNECT_SWAPPED);
g_object_ref (category);
}
reload_category (self);
g_object_notify (G_OBJECT (self), "category");
}
/**
* ld_category_view_get_category:
* @self: an #LdCategoryView object.
*
* Return value: (transfer none): the #LdCategory object
* assigned to the widget.
*/
LdCategory *
ld_category_view_get_category (LdCategoryView *self)
{
g_return_val_if_fail (LD_IS_CATEGORY_VIEW (self), NULL);
return self->priv->category;
}
/**
* ld_category_view_set_expander_prefix:
* @self: an #LdCategoryView object.
* @category: (allow-none): the new prefix.
*
* Set the prefix for inner #GtkExpander labels.
*/
void
ld_category_view_set_expander_prefix (LdCategoryView *self,
const gchar *prefix)
{
g_return_if_fail (LD_IS_CATEGORY_VIEW (self));
g_free (self->priv->expander_prefix);
if (prefix)
self->priv->expander_prefix = g_strdup (prefix);
else
self->priv->expander_prefix = NULL;
reload_category (self);
}
/**
* ld_category_view_get_expander_prefix:
* @self: an #LdCategoryView object.
*
* Return value: the prefix for inner expander labels.
*/
const gchar *
ld_category_view_get_expander_prefix (LdCategoryView *self)
{
g_return_val_if_fail (LD_IS_CATEGORY_VIEW (self), NULL);
return self->priv->expander_prefix;
}
static GtkWidget *
create_empty_label (void)
{
GtkWidget *label;
PangoAttrList *attr;
label = gtk_label_new (_("Empty"));
gtk_widget_set_sensitive (label, FALSE);
gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
gtk_misc_set_padding (GTK_MISC (label), 5, 0);
attr = pango_attr_list_new ();
pango_attr_list_change (attr, pango_attr_style_new (PANGO_STYLE_ITALIC));
gtk_label_set_attributes (GTK_LABEL (label), attr);
pango_attr_list_unref (attr);
return label;
}
static void
reload_category (LdCategoryView *self)
{
g_return_if_fail (LD_IS_CATEGORY_VIEW (self));
/* Clear the toolbar first, if there was already something in it. */
gtk_container_foreach (GTK_CONTAINER (self),
(GtkCallback) gtk_widget_destroy, NULL);
if (self->priv->category)
{
GSList *children;
/* TODO: Also show the symbols. */
children = (GSList *) ld_category_get_children (self->priv->category);
if (children)
g_slist_foreach (children, load_category_cb, self);
else
/* TODO: Don't show this if there are any symbols. */
gtk_box_pack_start (GTK_BOX (self),
create_empty_label (), FALSE, FALSE, 0);
}
}
static void
load_category_cb (gpointer data, gpointer user_data)
{
LdCategoryView *self;
LdCategory *cat;
GtkWidget *expander, *child;
const gchar *name;
gchar *label, *label_markup;
g_return_if_fail (LD_IS_CATEGORY_VIEW (user_data));
g_return_if_fail (LD_IS_CATEGORY (data));
self = user_data;
cat = data;
name = ld_category_get_human_name (cat);
if (self->priv->expander_prefix)
{
/* It's the least I can do to make it not look bad right now. */
gchar *prefix_escaped, *name_escaped;
prefix_escaped = g_markup_escape_text (self->priv->expander_prefix, -1);
name_escaped = g_markup_escape_text (name, -1);
label = g_strdup_printf ("%s: %s", self->priv->expander_prefix, name);
label_markup = g_strdup_printf ("<small>%s:</small> %s",
prefix_escaped, name_escaped);
g_free (name_escaped);
g_free (prefix_escaped);
}
else
{
label = g_strdup (name);
label_markup = g_markup_escape_text (name, -1);
}
expander = gtk_expander_new (label_markup);
gtk_expander_set_expanded (GTK_EXPANDER (expander), TRUE);
gtk_expander_set_use_markup (GTK_EXPANDER (expander), TRUE);
child = ld_category_view_new ();
ld_category_view_set_expander_prefix (LD_CATEGORY_VIEW (child), label);
ld_category_view_set_category (LD_CATEGORY_VIEW (child), cat);
gtk_container_add (GTK_CONTAINER (expander), child);
gtk_box_pack_start (GTK_BOX (self), expander, FALSE, FALSE, 0);
g_free (label);
g_free (label_markup);
}

View File

@ -0,0 +1,64 @@
/*
* ld-category-view.h
*
* This file is a part of logdiag.
* Copyright Přemysl Janouch 2011. All rights reserved.
*
* See the file LICENSE for licensing information.
*
*/
#ifndef __LD_CATEGORY_VIEW_H__
#define __LD_CATEGORY_VIEW_H__
G_BEGIN_DECLS
#define LD_TYPE_CATEGORY_VIEW (ld_category_view_get_type ())
#define LD_CATEGORY_VIEW(obj) (G_TYPE_CHECK_INSTANCE_CAST \
((obj), LD_TYPE_CATEGORY_VIEW, LdCategoryView))
#define LD_CATEGORY_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST \
((klass), LD_TYPE_CATEGORY_VIEW, LdCategoryViewClass))
#define LD_IS_CATEGORY_VIEW(obj) (G_TYPE_CHECK_INSTANCE_TYPE \
((obj), LD_TYPE_CATEGORY_VIEW))
#define LD_IS_CATEGORY_VIEW_CLASS(klass) (G_TYPE_CHECK_INSTANCE_TYPE \
((klass), LD_TYPE_CATEGORY_VIEW))
#define LD_CATEGORY_VIEW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS \
((obj), LD_CATEGORY_VIEW, LdCategoryViewClass))
typedef struct _LdCategoryView LdCategoryView;
typedef struct _LdCategoryViewPrivate LdCategoryViewPrivate;
typedef struct _LdCategoryViewClass LdCategoryViewClass;
/**
* LdCategoryView:
*/
struct _LdCategoryView
{
/*< private >*/
GtkVBox parent_instance;
LdCategoryViewPrivate *priv;
};
struct _LdCategoryViewClass
{
/*< private >*/
GtkVBoxClass parent_class;
};
GType ld_category_view_get_type (void) G_GNUC_CONST;
GtkWidget *ld_category_view_new (void);
void ld_category_view_set_category (LdCategoryView *self, LdCategory *category);
LdCategory *ld_category_view_get_category (LdCategoryView *self);
void ld_category_view_set_expander_prefix (LdCategoryView *self,
const gchar *prefix);
const gchar *ld_category_view_get_expander_prefix (LdCategoryView *self);
G_END_DECLS
#endif /* ! __LD_CATEGORY_VIEW_H__ */

View File

@ -1,228 +0,0 @@
/*
* ld-library-pane.c
*
* This file is a part of logdiag.
* Copyright Přemysl Janouch 2011. All rights reserved.
*
* See the file LICENSE for licensing information.
*
*/
#include "liblogdiag.h"
#include "config.h"
/**
* SECTION:ld-library-pane
* @short_description: A library pane
* @see_also: #LdLibrary
*
* #LdLibraryPane enables the user to drag symbols from an #LdLibrary
* onto #LdDiagramView.
*/
/*
* LdLibraryPanePrivate:
* @library: a library object assigned as a model.
*/
struct _LdLibraryPanePrivate
{
LdLibrary *library;
};
enum
{
PROP_0,
PROP_LIBRARY
};
static void ld_library_pane_get_property (GObject *object, guint property_id,
GValue *value, GParamSpec *pspec);
static void ld_library_pane_set_property (GObject *object, guint property_id,
const GValue *value, GParamSpec *pspec);
static void ld_library_pane_dispose (GObject *gobject);
static void reload_library (LdLibraryPane *self);
static void load_category_cb (gpointer data, gpointer user_data);
G_DEFINE_TYPE (LdLibraryPane, ld_library_pane, GTK_TYPE_VBOX);
static void
ld_library_pane_class_init (LdLibraryPaneClass *klass)
{
GObjectClass *object_class;
GParamSpec *pspec;
object_class = G_OBJECT_CLASS (klass);
object_class->get_property = ld_library_pane_get_property;
object_class->set_property = ld_library_pane_set_property;
object_class->dispose = ld_library_pane_dispose;
/**
* LdLibraryPane:library:
*
* The #LdLibrary that this toolbar retrieves symbols from.
*/
pspec = g_param_spec_object ("library", "Library",
"The library that this toolbar retrieves symbols from.",
LD_TYPE_LIBRARY, G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_LIBRARY, pspec);
g_type_class_add_private (klass, sizeof (LdLibraryPanePrivate));
}
static void
ld_library_pane_init (LdLibraryPane *self)
{
self->priv = G_TYPE_INSTANCE_GET_PRIVATE
(self, LD_TYPE_LIBRARY_PANE, LdLibraryPanePrivate);
}
static void
ld_library_pane_dispose (GObject *gobject)
{
LdLibraryPane *self;
self = LD_LIBRARY_PANE (gobject);
ld_library_pane_set_library (self, NULL);
/* Chain up to the parent class. */
G_OBJECT_CLASS (ld_library_pane_parent_class)->dispose (gobject);
}
static void
ld_library_pane_get_property (GObject *object, guint property_id,
GValue *value, GParamSpec *pspec)
{
LdLibraryPane *self;
self = LD_LIBRARY_PANE (object);
switch (property_id)
{
case PROP_LIBRARY:
g_value_set_object (value, ld_library_pane_get_library (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
ld_library_pane_set_property (GObject *object, guint property_id,
const GValue *value, GParamSpec *pspec)
{
LdLibraryPane *self;
self = LD_LIBRARY_PANE (object);
switch (property_id)
{
case PROP_LIBRARY:
ld_library_pane_set_library (self,
LD_LIBRARY (g_value_get_object (value)));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
/**
* ld_library_pane_new:
*
* Create an instance.
*/
GtkWidget *
ld_library_pane_new (void)
{
return g_object_new (LD_TYPE_LIBRARY_PANE, NULL);
}
/**
* ld_library_pane_set_library:
* @self: an #LdLibraryPane object.
* @library: (allow-none): the library to be assigned to the pane.
*
* Assign an #LdLibrary object to the pane.
*/
void
ld_library_pane_set_library (LdLibraryPane *self, LdLibrary *library)
{
g_return_if_fail (LD_IS_LIBRARY_PANE (self));
g_return_if_fail (LD_IS_LIBRARY (library) || library == NULL);
if (self->priv->library)
{
g_signal_handlers_disconnect_by_func (self->priv->library,
reload_library, self);
g_object_unref (self->priv->library);
}
self->priv->library = library;
if (library)
{
g_signal_connect_data (library, "changed",
G_CALLBACK (reload_library), self,
NULL, G_CONNECT_AFTER | G_CONNECT_SWAPPED);
g_object_ref (library);
}
reload_library (self);
g_object_notify (G_OBJECT (self), "library");
}
/**
* ld_library_pane_get_library:
* @self: an #LdLibraryPane object.
*
* Return value: (transfer none): the #LdLibrary object
* assigned to the pane.
*/
LdLibrary *
ld_library_pane_get_library (LdLibraryPane *self)
{
g_return_val_if_fail (LD_IS_LIBRARY_PANE (self), NULL);
return self->priv->library;
}
static void
reload_library (LdLibraryPane *self)
{
g_return_if_fail (LD_IS_LIBRARY_PANE (self));
/* Clear the toolbar first, if there was already something in it. */
gtk_container_foreach (GTK_CONTAINER (self),
(GtkCallback) gtk_widget_destroy, NULL);
if (self->priv->library)
{
GSList *categories;
categories = (GSList *) ld_category_get_children
(ld_library_get_root (self->priv->library));
g_slist_foreach (categories, load_category_cb, self);
}
}
static void
load_category_cb (gpointer data, gpointer user_data)
{
LdLibraryPane *self;
LdCategory *cat;
GtkExpander *expander;
const gchar *human_name;
g_return_if_fail (LD_IS_LIBRARY_PANE (user_data));
g_return_if_fail (LD_IS_CATEGORY (data));
self = user_data;
cat = data;
/* TODO: Set a child for the expander, recurse into category children. */
human_name = ld_category_get_human_name (cat);
expander = GTK_EXPANDER (gtk_expander_new (human_name));
gtk_box_pack_start (GTK_BOX (self), GTK_WIDGET (expander), FALSE, FALSE, 0);
}

View File

@ -1,61 +0,0 @@
/*
* ld-library-pane.h
*
* This file is a part of logdiag.
* Copyright Přemysl Janouch 2011. All rights reserved.
*
* See the file LICENSE for licensing information.
*
*/
#ifndef __LD_LIBRARY_PANE_H__
#define __LD_LIBRARY_PANE_H__
G_BEGIN_DECLS
#define LD_TYPE_LIBRARY_PANE (ld_library_pane_get_type ())
#define LD_LIBRARY_PANE(obj) (G_TYPE_CHECK_INSTANCE_CAST \
((obj), LD_TYPE_LIBRARY_PANE, LdLibraryPane))
#define LD_LIBRARY_PANE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST \
((klass), LD_TYPE_LIBRARY_PANE, LdLibraryPaneClass))
#define LD_IS_LIBRARY_PANE(obj) (G_TYPE_CHECK_INSTANCE_TYPE \
((obj), LD_TYPE_LIBRARY_PANE))
#define LD_IS_LIBRARY_PANE_CLASS(klass) (G_TYPE_CHECK_INSTANCE_TYPE \
((klass), LD_TYPE_LIBRARY_PANE))
#define LD_LIBRARY_PANE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS \
((obj), LD_LIBRARY_PANE, LdLibraryPaneClass))
typedef struct _LdLibraryPane LdLibraryPane;
typedef struct _LdLibraryPanePrivate LdLibraryPanePrivate;
typedef struct _LdLibraryPaneClass LdLibraryPaneClass;
/**
* LdLibraryPane:
*/
struct _LdLibraryPane
{
/*< private >*/
GtkVBox parent_instance;
LdLibraryPanePrivate *priv;
};
struct _LdLibraryPaneClass
{
/*< private >*/
GtkVBoxClass parent_class;
};
GType ld_library_pane_get_type (void) G_GNUC_CONST;
GtkWidget *ld_library_pane_new (void);
void ld_library_pane_set_library (LdLibraryPane *self, LdLibrary *library);
LdLibrary *ld_library_pane_get_library (LdLibraryPane *self);
G_END_DECLS
#endif /* ! __LD_LIBRARY_PANE_H__ */

View File

@ -28,7 +28,7 @@
#include "ld-diagram.h"
#include "ld-diagram-view.h"
#include "ld-library-pane.h"
#include "ld-category-view.h"
#include "ld-lua.h"
#include "ld-lua-symbol.h"

View File

@ -27,9 +27,9 @@ struct _LdWindowMainPrivate
GtkWidget *menu;
GtkWidget *toolbar;
GtkWidget *library_pane;
GtkWidget *pane_window;
GtkWidget *pane_viewport;
GtkWidget *library_view;
GtkWidget *lv_window;
GtkWidget *lv_viewport;
LdLibrary *library;
@ -262,7 +262,7 @@ ld_window_main_init (LdWindowMain *self)
priv->toolbar = gtk_ui_manager_get_widget (priv->ui_manager, "/Toolbar");
/* Create the remaining widgets. */
priv->library_pane = ld_library_pane_new ();
priv->library_view = ld_category_view_new ();
priv->view = LD_DIAGRAM_VIEW (ld_diagram_view_new ());
priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
@ -277,19 +277,19 @@ ld_window_main_init (LdWindowMain *self)
priv->statusbar_symbol_context_id = gtk_statusbar_get_context_id
(GTK_STATUSBAR (priv->statusbar), "symbol");
priv->pane_viewport = gtk_viewport_new (NULL, NULL);
priv->lv_viewport = gtk_viewport_new (NULL, NULL);
gtk_viewport_set_shadow_type
(GTK_VIEWPORT (priv->pane_viewport), GTK_SHADOW_NONE);
gtk_container_add (GTK_CONTAINER (priv->pane_viewport), priv->library_pane);
(GTK_VIEWPORT (priv->lv_viewport), GTK_SHADOW_NONE);
gtk_container_add (GTK_CONTAINER (priv->lv_viewport), priv->library_view);
priv->pane_window = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->pane_window),
priv->lv_window = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->lv_window),
GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
gtk_container_add (GTK_CONTAINER (priv->pane_window), priv->pane_viewport);
gtk_container_add (GTK_CONTAINER (priv->lv_window), priv->lv_viewport);
priv->paned = gtk_hpaned_new ();
gtk_paned_pack1 (GTK_PANED (priv->paned),
priv->pane_window, FALSE, FALSE);
priv->lv_window, FALSE, FALSE);
gtk_paned_pack2 (GTK_PANED (priv->paned),
priv->scrolled_window, TRUE, TRUE);
@ -332,8 +332,8 @@ ld_window_main_init (LdWindowMain *self)
g_signal_connect (priv->view, "notify::zoom",
G_CALLBACK (on_view_zoom_changed), self);
ld_library_pane_set_library (LD_LIBRARY_PANE (priv->library_pane),
priv->library);
ld_category_view_set_category (LD_CATEGORY_VIEW (priv->library_view),
ld_library_get_root (priv->library));
diagram_set_filename (self, NULL);
@ -914,7 +914,7 @@ on_action_main_toolbar (GtkToggleAction *action, LdWindowMain *self)
static void
on_action_library_pane (GtkToggleAction *action, LdWindowMain *self)
{
gtk_widget_set_visible (self->priv->pane_window,
gtk_widget_set_visible (self->priv->lv_window,
gtk_toggle_action_get_active (action));
}