Move the library toolbar to its own class.
This commit is contained in:
parent
4a9a8e8f0a
commit
33beece3ab
@ -115,6 +115,7 @@ set (liblogdiag_SOURCES
|
||||
liblogdiag/ld-diagram-symbol.c
|
||||
liblogdiag/ld-canvas.c
|
||||
liblogdiag/ld-library.c
|
||||
liblogdiag/ld-library-toolbar.c
|
||||
liblogdiag/ld-symbol-category.c
|
||||
liblogdiag/ld-symbol.c
|
||||
liblogdiag/ld-lua.c
|
||||
@ -129,6 +130,7 @@ set (liblogdiag_HEADERS
|
||||
liblogdiag/ld-diagram-symbol.h
|
||||
liblogdiag/ld-canvas.h
|
||||
liblogdiag/ld-library.h
|
||||
liblogdiag/ld-library-toolbar.h
|
||||
liblogdiag/ld-symbol-category.h
|
||||
liblogdiag/ld-symbol.h
|
||||
liblogdiag/ld-lua.h
|
||||
|
773
liblogdiag/ld-library-toolbar.c
Normal file
773
liblogdiag/ld-library-toolbar.c
Normal file
@ -0,0 +1,773 @@
|
||||
/*
|
||||
* ld-library-toolbar.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-toolbar
|
||||
* @short_description: A library toolbar.
|
||||
* @see_also: #LdLibrary
|
||||
*
|
||||
* #LdLibraryToolbar enables the user to choose symbols from an #LdLibrary.
|
||||
*/
|
||||
|
||||
#define LIBRARY_TOOLBAR_ICON_WIDTH 32
|
||||
|
||||
/*
|
||||
* SymbolMenuItem:
|
||||
*
|
||||
* Data related to a symbol in an open symbol menu.
|
||||
*/
|
||||
typedef struct _SymbolMenuItem SymbolMenuItem;
|
||||
|
||||
struct _SymbolMenuItem
|
||||
{
|
||||
LdSymbol *symbol;
|
||||
gchar *klass;
|
||||
|
||||
gint width;
|
||||
gdouble dx;
|
||||
gdouble scale;
|
||||
};
|
||||
|
||||
/*
|
||||
* SymbolMenuData:
|
||||
*
|
||||
* Data related to the currently opened symbol menu.
|
||||
*/
|
||||
typedef struct _SymbolMenuData SymbolMenuData;
|
||||
|
||||
struct _SymbolMenuData
|
||||
{
|
||||
GtkToggleButton *active_button;
|
||||
|
||||
SymbolMenuItem *items;
|
||||
gint n_items;
|
||||
gint active_item;
|
||||
|
||||
gint menu_width;
|
||||
gint menu_height;
|
||||
gint menu_y;
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
CANVAS_HANDLER_EXPOSE,
|
||||
CANVAS_HANDLER_MOTION_NOTIFY,
|
||||
CANVAS_HANDLER_BUTTON_PRESS,
|
||||
CANVAS_HANDLER_BUTTON_RELEASE,
|
||||
CANVAS_HANDLER_COUNT
|
||||
};
|
||||
|
||||
/*
|
||||
* LdLibraryToolbarPrivate:
|
||||
* @library: A library object assigned to this canvas as a model.
|
||||
* @canvas: A canvas object for showing symbol menus.
|
||||
* @canvas_handlers: Signal handlers that hook the canvas.
|
||||
* @symbol_menu: Data related to menus.
|
||||
*/
|
||||
struct _LdLibraryToolbarPrivate
|
||||
{
|
||||
LdLibrary *library;
|
||||
LdCanvas *canvas;
|
||||
gulong canvas_handlers[CANVAS_HANDLER_COUNT];
|
||||
SymbolMenuData symbol_menu;
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
PROP_0,
|
||||
PROP_LIBRARY,
|
||||
PROP_CANVAS
|
||||
};
|
||||
|
||||
static void ld_library_toolbar_get_property (GObject *object, guint property_id,
|
||||
GValue *value, GParamSpec *pspec);
|
||||
static void ld_library_toolbar_set_property (GObject *object, guint property_id,
|
||||
const GValue *value, GParamSpec *pspec);
|
||||
static void ld_library_toolbar_dispose (GObject *gobject);
|
||||
|
||||
static void reload_library (LdLibraryToolbar *self);
|
||||
static void load_category_cb (gpointer data, gpointer user_data);
|
||||
static GdkPixbuf *recolor_pixbuf (GdkPixbuf *pbuf, GdkColor *color);
|
||||
|
||||
static void redraw_symbol_menu (LdLibraryToolbar *self);
|
||||
static void emit_symbol_signal (LdLibraryToolbar *self,
|
||||
guint signal_id, gint menu_index);
|
||||
static void on_category_toggle (GtkToggleButton *toggle_button,
|
||||
gpointer user_data);
|
||||
|
||||
static inline void block_canvas_handlers (LdLibraryToolbar *self);
|
||||
static inline void unblock_canvas_handlers (LdLibraryToolbar *self);
|
||||
static inline void disconnect_canvas_handlers (LdLibraryToolbar *self);
|
||||
static gboolean on_canvas_exposed (GtkWidget *widget,
|
||||
GdkEventExpose *event, gpointer user_data);
|
||||
static gboolean on_canvas_motion_notify (GtkWidget *widget,
|
||||
GdkEventMotion *event, gpointer user_data);
|
||||
static gboolean on_canvas_button_press (GtkWidget *widget,
|
||||
GdkEventButton *event, gpointer user_data);
|
||||
static gboolean on_canvas_button_release (GtkWidget *widget,
|
||||
GdkEventButton *event, gpointer user_data);
|
||||
|
||||
|
||||
G_DEFINE_TYPE (LdLibraryToolbar, ld_library_toolbar, GTK_TYPE_TOOLBAR);
|
||||
|
||||
static void
|
||||
ld_library_toolbar_class_init (LdLibraryToolbarClass *klass)
|
||||
{
|
||||
GObjectClass *object_class;
|
||||
GtkWidgetClass *widget_class;
|
||||
GParamSpec *pspec;
|
||||
|
||||
widget_class = GTK_WIDGET_CLASS (klass);
|
||||
|
||||
object_class = G_OBJECT_CLASS (klass);
|
||||
object_class->get_property = ld_library_toolbar_get_property;
|
||||
object_class->set_property = ld_library_toolbar_set_property;
|
||||
object_class->dispose = ld_library_toolbar_dispose;
|
||||
|
||||
/**
|
||||
* LdLibraryToolbar:library:
|
||||
*
|
||||
* The #LdLibrary that this canvas retrieves symbols from.
|
||||
*/
|
||||
pspec = g_param_spec_object ("library", "Library",
|
||||
"The library that this canvas retrieves symbols from.",
|
||||
LD_TYPE_LIBRARY, G_PARAM_READWRITE);
|
||||
g_object_class_install_property (object_class, PROP_LIBRARY, pspec);
|
||||
|
||||
/**
|
||||
* LdLibraryToolbar:canvas:
|
||||
*
|
||||
* The #LdCanvas misused for showing symbol menus.
|
||||
*/
|
||||
pspec = g_param_spec_object ("canvas", "Canvas",
|
||||
"The canvas misused for showing symbol menus.",
|
||||
LD_TYPE_CANVAS, G_PARAM_READWRITE);
|
||||
g_object_class_install_property (object_class, PROP_CANVAS, pspec);
|
||||
|
||||
/**
|
||||
* LdLibraryToolbar::symbol-chosen:
|
||||
* @self: An #LdLibraryToolbar object.
|
||||
* @symbol: The chosen #LdSymbol object.
|
||||
* @klass: Location of the symbol within the library.
|
||||
*
|
||||
* A symbol has been chosen.
|
||||
*/
|
||||
klass->symbol_chosen_signal = g_signal_new
|
||||
("symbol-chosen", G_TYPE_FROM_CLASS (klass),
|
||||
G_SIGNAL_RUN_LAST, 0, NULL, NULL,
|
||||
g_cclosure_user_marshal_VOID__OBJECT_STRING,
|
||||
G_TYPE_NONE, 2, LD_TYPE_SYMBOL,
|
||||
G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
|
||||
|
||||
/**
|
||||
* LdLibraryToolbar::symbol-selected:
|
||||
* @self: An #LdLibraryToolbar object.
|
||||
* @symbol: The selected #LdSymbol object.
|
||||
* @klass: Location of the symbol within the library.
|
||||
*
|
||||
* A symbol has been selected.
|
||||
*/
|
||||
klass->symbol_selected_signal = g_signal_new
|
||||
("symbol-selected", G_TYPE_FROM_CLASS (klass),
|
||||
G_SIGNAL_RUN_LAST, 0, NULL, NULL,
|
||||
g_cclosure_user_marshal_VOID__OBJECT_STRING,
|
||||
G_TYPE_NONE, 2, LD_TYPE_SYMBOL,
|
||||
G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
|
||||
|
||||
/**
|
||||
* LdLibraryToolbar::symbol-deselected:
|
||||
* @self: An #LdLibraryToolbar object.
|
||||
* @symbol: The deselected #LdSymbol object.
|
||||
* @klass: Location of the symbol within the library.
|
||||
*
|
||||
* A symbol has been deselected.
|
||||
*/
|
||||
klass->symbol_deselected_signal = g_signal_new
|
||||
("symbol-deselected", G_TYPE_FROM_CLASS (klass),
|
||||
G_SIGNAL_RUN_LAST, 0, NULL, NULL,
|
||||
g_cclosure_user_marshal_VOID__OBJECT_STRING,
|
||||
G_TYPE_NONE, 2, LD_TYPE_SYMBOL,
|
||||
G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
|
||||
|
||||
g_type_class_add_private (klass, sizeof (LdLibraryToolbarPrivate));
|
||||
}
|
||||
|
||||
static void
|
||||
ld_library_toolbar_init (LdLibraryToolbar *self)
|
||||
{
|
||||
self->priv = G_TYPE_INSTANCE_GET_PRIVATE
|
||||
(self, LD_TYPE_LIBRARY_TOOLBAR, LdLibraryToolbarPrivate);
|
||||
}
|
||||
|
||||
static void
|
||||
ld_library_toolbar_dispose (GObject *gobject)
|
||||
{
|
||||
LdLibraryToolbar *self;
|
||||
|
||||
self = LD_LIBRARY_TOOLBAR (gobject);
|
||||
|
||||
ld_library_toolbar_set_library (self, NULL);
|
||||
ld_library_toolbar_set_canvas (self, NULL);
|
||||
|
||||
/* Chain up to the parent class. */
|
||||
G_OBJECT_CLASS (ld_library_toolbar_parent_class)->dispose (gobject);
|
||||
}
|
||||
|
||||
static void
|
||||
ld_library_toolbar_get_property (GObject *object, guint property_id,
|
||||
GValue *value, GParamSpec *pspec)
|
||||
{
|
||||
LdLibraryToolbar *self;
|
||||
|
||||
self = LD_LIBRARY_TOOLBAR (object);
|
||||
switch (property_id)
|
||||
{
|
||||
case PROP_LIBRARY:
|
||||
g_value_set_object (value, ld_library_toolbar_get_library (self));
|
||||
break;
|
||||
case PROP_CANVAS:
|
||||
g_value_set_object (value, ld_library_toolbar_get_canvas (self));
|
||||
break;
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
ld_library_toolbar_set_property (GObject *object, guint property_id,
|
||||
const GValue *value, GParamSpec *pspec)
|
||||
{
|
||||
LdLibraryToolbar *self;
|
||||
|
||||
self = LD_LIBRARY_TOOLBAR (object);
|
||||
switch (property_id)
|
||||
{
|
||||
case PROP_LIBRARY:
|
||||
ld_library_toolbar_set_library (self,
|
||||
LD_LIBRARY (g_value_get_object (value)));
|
||||
break;
|
||||
case PROP_CANVAS:
|
||||
ld_library_toolbar_set_canvas (self,
|
||||
LD_CANVAS (g_value_get_object (value)));
|
||||
break;
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* ld_library_toolbar_new:
|
||||
*
|
||||
* Create an instance.
|
||||
*/
|
||||
GtkWidget *
|
||||
ld_library_toolbar_new (void)
|
||||
{
|
||||
return g_object_new (LD_TYPE_LIBRARY_TOOLBAR, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* ld_library_toolbar_set_library:
|
||||
* @self: An #LdLibraryToolbar object.
|
||||
* @library: (allow-none): The #LdLibrary to be assigned to the toolbar.
|
||||
*
|
||||
* Assign an #LdLibrary object to the toolbar.
|
||||
*/
|
||||
void
|
||||
ld_library_toolbar_set_library (LdLibraryToolbar *self, LdLibrary *library)
|
||||
{
|
||||
g_return_if_fail (LD_IS_LIBRARY_TOOLBAR (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_toolbar_get_library:
|
||||
* @self: An #LdLibraryToolbar object.
|
||||
*
|
||||
* Get the #LdLibrary object assigned to this toolbar.
|
||||
* The reference count on the library is not incremented.
|
||||
*/
|
||||
LdLibrary *
|
||||
ld_library_toolbar_get_library (LdLibraryToolbar *self)
|
||||
{
|
||||
g_return_val_if_fail (LD_IS_LIBRARY_TOOLBAR (self), NULL);
|
||||
return self->priv->library;
|
||||
}
|
||||
|
||||
/**
|
||||
* ld_library_toolbar_set_canvas:
|
||||
* @self: An #LdLibraryToolbar object.
|
||||
* @canvas: (allow-none): The #LdCanvas to be assigned to the toolbar.
|
||||
*
|
||||
* Assign an #LdCanvas object to the toolbar.
|
||||
*/
|
||||
void
|
||||
ld_library_toolbar_set_canvas (LdLibraryToolbar *self, LdCanvas *canvas)
|
||||
{
|
||||
g_return_if_fail (LD_IS_LIBRARY_TOOLBAR (self));
|
||||
g_return_if_fail (LD_IS_CANVAS (canvas) || canvas == NULL);
|
||||
|
||||
if (self->priv->canvas)
|
||||
{
|
||||
disconnect_canvas_handlers (self);
|
||||
g_object_unref (self->priv->canvas);
|
||||
}
|
||||
|
||||
self->priv->canvas = canvas;
|
||||
|
||||
if (canvas)
|
||||
{
|
||||
self->priv->canvas_handlers[CANVAS_HANDLER_EXPOSE]
|
||||
= g_signal_connect (canvas, "expose-event",
|
||||
G_CALLBACK (on_canvas_exposed), self);
|
||||
self->priv->canvas_handlers[CANVAS_HANDLER_MOTION_NOTIFY]
|
||||
= g_signal_connect (canvas, "motion-notify-event",
|
||||
G_CALLBACK (on_canvas_motion_notify), self);
|
||||
self->priv->canvas_handlers[CANVAS_HANDLER_BUTTON_PRESS]
|
||||
= g_signal_connect (canvas, "button-press-event",
|
||||
G_CALLBACK (on_canvas_button_press), self);
|
||||
self->priv->canvas_handlers[CANVAS_HANDLER_BUTTON_RELEASE]
|
||||
= g_signal_connect (canvas, "button-release-event",
|
||||
G_CALLBACK (on_canvas_button_release), self);
|
||||
|
||||
block_canvas_handlers (self);
|
||||
g_object_ref (canvas);
|
||||
}
|
||||
g_object_notify (G_OBJECT (self), "canvas");
|
||||
}
|
||||
|
||||
/**
|
||||
* ld_library_toolbar_get_canvas:
|
||||
* @self: An #LdLibraryToolbar object.
|
||||
*
|
||||
* Get the #LdLibrary object assigned to this toolbar.
|
||||
* The reference count on the canvas is not incremented.
|
||||
*/
|
||||
LdCanvas *
|
||||
ld_library_toolbar_get_canvas (LdLibraryToolbar *self)
|
||||
{
|
||||
g_return_val_if_fail (LD_IS_LIBRARY_TOOLBAR (self), NULL);
|
||||
return self->priv->canvas;
|
||||
}
|
||||
|
||||
static void
|
||||
reload_library (LdLibraryToolbar *self)
|
||||
{
|
||||
g_return_if_fail (LD_IS_LIBRARY_TOOLBAR (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_library_get_children (self->priv->library);
|
||||
g_slist_foreach (categories, load_category_cb, self);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
load_category_cb (gpointer data, gpointer user_data)
|
||||
{
|
||||
LdLibraryToolbar *self;
|
||||
LdSymbolCategory *cat;
|
||||
const gchar *human_name;
|
||||
GdkPixbuf *pbuf, *new_pbuf;
|
||||
GtkWidget *img;
|
||||
GtkToolItem *item;
|
||||
GtkWidget *button;
|
||||
GtkStyle *style;
|
||||
|
||||
g_return_if_fail (LD_IS_LIBRARY_TOOLBAR (user_data));
|
||||
g_return_if_fail (LD_IS_SYMBOL_CATEGORY (data));
|
||||
|
||||
self = user_data;
|
||||
cat = data;
|
||||
|
||||
pbuf = gdk_pixbuf_new_from_file_at_size (ld_symbol_category_get_image_path
|
||||
(cat), LIBRARY_TOOLBAR_ICON_WIDTH, -1, NULL);
|
||||
g_return_if_fail (pbuf != NULL);
|
||||
|
||||
button = gtk_toggle_button_new ();
|
||||
style = gtk_rc_get_style (button);
|
||||
|
||||
/* TODO: Handle all states. */
|
||||
new_pbuf = recolor_pixbuf (pbuf, &style->fg[GTK_STATE_NORMAL]);
|
||||
if (new_pbuf)
|
||||
{
|
||||
g_object_unref (pbuf);
|
||||
pbuf = new_pbuf;
|
||||
}
|
||||
|
||||
img = gtk_image_new_from_pixbuf (pbuf);
|
||||
g_object_unref (pbuf);
|
||||
|
||||
item = gtk_tool_item_new ();
|
||||
gtk_container_add (GTK_CONTAINER (button), img);
|
||||
gtk_container_add (GTK_CONTAINER (item), button);
|
||||
|
||||
/* Don't steal focus from the canvas. */
|
||||
g_object_set (button, "can-focus", FALSE, NULL);
|
||||
|
||||
/* Assign the category to the toggle button. */
|
||||
g_object_ref (cat);
|
||||
g_object_set_data_full (G_OBJECT (button),
|
||||
"category", cat, (GDestroyNotify) g_object_unref);
|
||||
|
||||
/* Hook toggling of the button. */
|
||||
g_signal_connect (button, "toggled", G_CALLBACK (on_category_toggle), self);
|
||||
|
||||
human_name = ld_symbol_category_get_human_name (cat);
|
||||
gtk_tool_item_set_tooltip_text (item, human_name);
|
||||
gtk_toolbar_insert (GTK_TOOLBAR (self), item, 0);
|
||||
}
|
||||
|
||||
static GdkPixbuf *
|
||||
recolor_pixbuf (GdkPixbuf *pbuf, GdkColor *color)
|
||||
{
|
||||
gint width, height;
|
||||
GdkPixbuf *new_pbuf;
|
||||
cairo_surface_t *cr_surface;
|
||||
cairo_t *cr;
|
||||
|
||||
width = gdk_pixbuf_get_width (pbuf);
|
||||
height = gdk_pixbuf_get_height (pbuf);
|
||||
|
||||
new_pbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
|
||||
g_return_val_if_fail (new_pbuf != NULL, NULL);
|
||||
|
||||
cr_surface = cairo_image_surface_create_for_data
|
||||
(gdk_pixbuf_get_pixels (new_pbuf), CAIRO_FORMAT_ARGB32,
|
||||
width, height, gdk_pixbuf_get_rowstride (new_pbuf));
|
||||
cr = cairo_create (cr_surface);
|
||||
|
||||
/* Change the color of all pixels but leave the alpha channel intact. */
|
||||
gdk_cairo_set_source_color (cr, color);
|
||||
cairo_paint (cr);
|
||||
|
||||
cairo_set_operator (cr, CAIRO_OPERATOR_DEST_IN);
|
||||
gdk_cairo_set_source_pixbuf (cr, pbuf, 0, 0);
|
||||
cairo_paint (cr);
|
||||
|
||||
cairo_destroy (cr);
|
||||
cairo_surface_destroy (cr_surface);
|
||||
return new_pbuf;
|
||||
}
|
||||
|
||||
static void
|
||||
redraw_symbol_menu (LdLibraryToolbar *self)
|
||||
{
|
||||
SymbolMenuData *data;
|
||||
|
||||
g_return_if_fail (LD_IS_LIBRARY_TOOLBAR (self));
|
||||
data = &self->priv->symbol_menu;
|
||||
|
||||
gtk_widget_queue_draw_area (GTK_WIDGET (self->priv->canvas),
|
||||
0, data->menu_y - 1, data->menu_width + 2, data->menu_height + 2);
|
||||
}
|
||||
|
||||
static void
|
||||
emit_symbol_signal (LdLibraryToolbar *self, guint signal_id, gint menu_index)
|
||||
{
|
||||
SymbolMenuData *data;
|
||||
SymbolMenuItem *item;
|
||||
|
||||
data = &self->priv->symbol_menu;
|
||||
if (menu_index == -1)
|
||||
menu_index = data->active_item;
|
||||
if (menu_index != -1)
|
||||
{
|
||||
item = &data->items[menu_index];
|
||||
g_signal_emit (self, signal_id, 0, item->symbol, item->klass);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
on_category_toggle (GtkToggleButton *toggle_button, gpointer user_data)
|
||||
{
|
||||
LdLibraryToolbar *self;
|
||||
LdLibraryToolbarPrivate *priv;
|
||||
LdSymbolCategory *cat;
|
||||
SymbolMenuData *data;
|
||||
const gchar *category_name, *symbol_name;
|
||||
|
||||
cat = g_object_get_data (G_OBJECT (toggle_button), "category");
|
||||
self = LD_LIBRARY_TOOLBAR (user_data);
|
||||
priv = self->priv;
|
||||
data = &priv->symbol_menu;
|
||||
|
||||
/* First untoggle any active button. */
|
||||
if (data->active_button)
|
||||
gtk_toggle_button_set_active (data->active_button, FALSE);
|
||||
|
||||
/* And toggle signal handlers that enable the user to add a symbol. */
|
||||
if (data->active_button == toggle_button)
|
||||
{
|
||||
gint i;
|
||||
|
||||
block_canvas_handlers (self);
|
||||
|
||||
g_object_unref (data->active_button);
|
||||
data->active_button = NULL;
|
||||
|
||||
emit_symbol_signal (self, LD_LIBRARY_TOOLBAR_GET_CLASS (self)
|
||||
->symbol_deselected_signal, -1);
|
||||
|
||||
/* Ashes to ashes, NULL to NULL. */
|
||||
for (i = 0; i < data->n_items; i++)
|
||||
{
|
||||
g_object_unref (data->items[i].symbol);
|
||||
g_free (data->items[i].klass);
|
||||
}
|
||||
|
||||
g_free (data->items);
|
||||
data->items = NULL;
|
||||
|
||||
gtk_grab_remove (GTK_WIDGET (self->priv->canvas));
|
||||
}
|
||||
else
|
||||
{
|
||||
const GSList *children, *symbol_iter;
|
||||
SymbolMenuItem *item;
|
||||
gint x, y, menu_width;
|
||||
|
||||
g_return_if_fail (gtk_widget_translate_coordinates (GTK_WIDGET
|
||||
(toggle_button), GTK_WIDGET (priv->canvas), 0, 0, &x, &y));
|
||||
|
||||
data->menu_y = y;
|
||||
data->menu_height = GTK_WIDGET (toggle_button)->allocation.height;
|
||||
|
||||
unblock_canvas_handlers (self);
|
||||
|
||||
data->active_button = toggle_button;
|
||||
g_object_ref (data->active_button);
|
||||
|
||||
category_name = ld_symbol_category_get_name (cat);
|
||||
children = ld_symbol_category_get_children (cat);
|
||||
|
||||
data->n_items = g_slist_length ((GSList *) children);
|
||||
data->items = g_new (SymbolMenuItem, data->n_items);
|
||||
data->active_item = -1;
|
||||
|
||||
item = data->items;
|
||||
menu_width = 0;
|
||||
for (symbol_iter = children; symbol_iter;
|
||||
symbol_iter = symbol_iter->next)
|
||||
{
|
||||
LdRectangle area;
|
||||
|
||||
item->symbol = LD_SYMBOL (symbol_iter->data);
|
||||
g_object_ref (item->symbol);
|
||||
|
||||
symbol_name = ld_symbol_get_name (item->symbol);
|
||||
item->klass = g_build_path (LD_LIBRARY_IDENTIFIER_SEPARATOR,
|
||||
category_name, symbol_name, NULL);
|
||||
|
||||
ld_symbol_get_area (item->symbol, &area);
|
||||
|
||||
/* This is the height when the center of the symbol is
|
||||
* in the center of it's symbol menu item.
|
||||
*/
|
||||
item->scale = data->menu_height * 0.5
|
||||
/ MAX (ABS (area.y), ABS (area.y + area.height)) * 0.5;
|
||||
item->width = data->menu_height * 0.5 + item->scale * area.width;
|
||||
item->dx = item->width * 0.5 + item->scale * (area.width * 0.5
|
||||
- MAX (ABS (area.x), ABS (area.x + area.width)));
|
||||
|
||||
menu_width += item++->width;
|
||||
}
|
||||
data->menu_width = menu_width;
|
||||
|
||||
gtk_grab_add (GTK_WIDGET (self->priv->canvas));
|
||||
}
|
||||
redraw_symbol_menu (self);
|
||||
}
|
||||
|
||||
#define DEFINE_CANVAS_HANDLER_FUNC(name) \
|
||||
static inline void \
|
||||
name ## _canvas_handlers (LdLibraryToolbar *self) \
|
||||
{ \
|
||||
gint i; \
|
||||
g_return_if_fail (LD_IS_CANVAS (self->priv->canvas)); \
|
||||
for (i = 0; i < CANVAS_HANDLER_COUNT; i++) \
|
||||
g_signal_handler_ ## name (self->priv->canvas, \
|
||||
self->priv->canvas_handlers[i]); \
|
||||
}
|
||||
|
||||
DEFINE_CANVAS_HANDLER_FUNC (block)
|
||||
DEFINE_CANVAS_HANDLER_FUNC (unblock)
|
||||
DEFINE_CANVAS_HANDLER_FUNC (disconnect)
|
||||
|
||||
static gboolean
|
||||
on_canvas_exposed (GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
|
||||
{
|
||||
cairo_t *cr;
|
||||
LdLibraryToolbar *self;
|
||||
SymbolMenuData *data;
|
||||
gint i, x;
|
||||
|
||||
cr = gdk_cairo_create (widget->window);
|
||||
self = LD_LIBRARY_TOOLBAR (user_data);
|
||||
data = &self->priv->symbol_menu;
|
||||
|
||||
/* Draw some border. */
|
||||
cairo_set_line_width (cr, 1);
|
||||
|
||||
cairo_rectangle (cr, 0, data->menu_y, data->menu_width, data->menu_height);
|
||||
cairo_set_source_rgb (cr, 1, 1, 1);
|
||||
cairo_fill (cr);
|
||||
|
||||
/* Draw all symbols from that category. */
|
||||
for (x = i = 0; i < data->n_items; i++)
|
||||
{
|
||||
SymbolMenuItem *item;
|
||||
|
||||
item = data->items + i;
|
||||
cairo_save (cr);
|
||||
|
||||
cairo_rectangle (cr, x, data->menu_y, item->width, data->menu_height);
|
||||
cairo_clip (cr);
|
||||
|
||||
if (i == data->active_item)
|
||||
{
|
||||
cairo_set_source_rgb (cr, 0.9, 0.9, 0.9);
|
||||
cairo_paint (cr);
|
||||
}
|
||||
|
||||
cairo_translate (cr, x + item->dx,
|
||||
data->menu_y + data->menu_height * 0.5);
|
||||
cairo_scale (cr, item->scale, item->scale);
|
||||
|
||||
cairo_set_source_rgb (cr, 0, 0, 0);
|
||||
cairo_set_line_width (cr, 1 / item->scale);
|
||||
ld_symbol_draw (item->symbol, cr);
|
||||
|
||||
cairo_restore (cr);
|
||||
x += item->width;
|
||||
}
|
||||
|
||||
cairo_rectangle (cr, 0, data->menu_y, data->menu_width, data->menu_height);
|
||||
cairo_set_source_rgb (cr, 0, 0, 0);
|
||||
cairo_stroke (cr);
|
||||
|
||||
cairo_destroy (cr);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
on_canvas_motion_notify (GtkWidget *widget, GdkEventMotion *event,
|
||||
gpointer user_data)
|
||||
{
|
||||
LdLibraryToolbar *self;
|
||||
SymbolMenuData *data;
|
||||
gint i, x, at_cursor = -1;
|
||||
|
||||
self = LD_LIBRARY_TOOLBAR (user_data);
|
||||
data = &self->priv->symbol_menu;
|
||||
|
||||
if (widget->window != event->window
|
||||
|| event->x < 0 || event->y < data->menu_y
|
||||
|| event->y >= data->menu_y + data->menu_height)
|
||||
goto on_canvas_motion_notify_end;
|
||||
|
||||
for (x = i = 0; i < data->n_items; i++)
|
||||
{
|
||||
x += data->items[i].width;
|
||||
if (event->x < x)
|
||||
{
|
||||
at_cursor = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
on_canvas_motion_notify_end:
|
||||
if (data->active_item != at_cursor)
|
||||
{
|
||||
emit_symbol_signal (self, LD_LIBRARY_TOOLBAR_GET_CLASS (self)
|
||||
->symbol_deselected_signal, -1);
|
||||
|
||||
if (at_cursor != -1)
|
||||
emit_symbol_signal (self, LD_LIBRARY_TOOLBAR_GET_CLASS (self)
|
||||
->symbol_selected_signal, at_cursor);
|
||||
}
|
||||
data->active_item = at_cursor;
|
||||
redraw_symbol_menu (self);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
on_canvas_button_press (GtkWidget *widget, GdkEventButton *event,
|
||||
gpointer user_data)
|
||||
{
|
||||
LdLibraryToolbar *self;
|
||||
SymbolMenuData *data;
|
||||
|
||||
self = LD_LIBRARY_TOOLBAR (user_data);
|
||||
data = &self->priv->symbol_menu;
|
||||
|
||||
/* If the event occured elsewhere, cancel the menu and put the event
|
||||
* back into the queue.
|
||||
*/
|
||||
if (widget->window != event->window && data->active_button)
|
||||
{
|
||||
gtk_toggle_button_set_active (data->active_button, FALSE);
|
||||
gdk_event_put ((GdkEvent *) event);
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
on_canvas_button_release (GtkWidget *widget, GdkEventButton *event,
|
||||
gpointer user_data)
|
||||
{
|
||||
LdLibraryToolbar *self;
|
||||
SymbolMenuData *data;
|
||||
|
||||
self = LD_LIBRARY_TOOLBAR (user_data);
|
||||
data = &self->priv->symbol_menu;
|
||||
|
||||
if (event->button != 1)
|
||||
return FALSE;
|
||||
|
||||
emit_symbol_signal (self, LD_LIBRARY_TOOLBAR_GET_CLASS (self)
|
||||
->symbol_chosen_signal, -1);
|
||||
|
||||
/* We've either chosen a symbol or canceled the menu, so hide it. */
|
||||
if (data->active_button)
|
||||
gtk_toggle_button_set_active (data->active_button, FALSE);
|
||||
|
||||
return FALSE;
|
||||
}
|
69
liblogdiag/ld-library-toolbar.h
Normal file
69
liblogdiag/ld-library-toolbar.h
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* ld-library-toolbar.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_TOOLBAR_H__
|
||||
#define __LD_LIBRARY_TOOLBAR_H__
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
|
||||
#define LD_TYPE_LIBRARY_TOOLBAR (ld_library_toolbar_get_type ())
|
||||
#define LD_LIBRARY_TOOLBAR(obj) (G_TYPE_CHECK_INSTANCE_CAST \
|
||||
((obj), LD_TYPE_LIBRARY_TOOLBAR, LdLibraryToolbar))
|
||||
#define LD_LIBRARY_TOOLBAR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST \
|
||||
((klass), LD_TYPE_LIBRARY_TOOLBAR, LdLibraryToolbarClass))
|
||||
#define LD_IS_LIBRARY_TOOLBAR(obj) (G_TYPE_CHECK_INSTANCE_TYPE \
|
||||
((obj), LD_TYPE_LIBRARY_TOOLBAR))
|
||||
#define LD_IS_LIBRARY_TOOLBAR_CLASS(klass) (G_TYPE_CHECK_INSTANCE_TYPE \
|
||||
((klass), LD_TYPE_LIBRARY_TOOLBAR))
|
||||
#define LD_LIBRARY_TOOLBAR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS \
|
||||
((obj), LD_LIBRARY_TOOLBAR, LdLibraryToolbarClass))
|
||||
|
||||
typedef struct _LdLibraryToolbar LdLibraryToolbar;
|
||||
typedef struct _LdLibraryToolbarPrivate LdLibraryToolbarPrivate;
|
||||
typedef struct _LdLibraryToolbarClass LdLibraryToolbarClass;
|
||||
|
||||
|
||||
/**
|
||||
* LdLibraryToolbar:
|
||||
*/
|
||||
struct _LdLibraryToolbar
|
||||
{
|
||||
/*< private >*/
|
||||
GtkToolbar parent_instance;
|
||||
LdLibraryToolbarPrivate *priv;
|
||||
};
|
||||
|
||||
struct _LdLibraryToolbarClass
|
||||
{
|
||||
/*< private >*/
|
||||
GtkToolbarClass parent_class;
|
||||
|
||||
guint symbol_chosen_signal;
|
||||
guint symbol_selected_signal;
|
||||
guint symbol_deselected_signal;
|
||||
};
|
||||
|
||||
|
||||
GType ld_library_toolbar_get_type (void) G_GNUC_CONST;
|
||||
|
||||
GtkWidget *ld_library_toolbar_new (void);
|
||||
|
||||
void ld_library_toolbar_set_library (LdLibraryToolbar *self,
|
||||
LdLibrary *library);
|
||||
LdLibrary *ld_library_toolbar_get_library (LdLibraryToolbar *self);
|
||||
void ld_library_toolbar_set_canvas (LdLibraryToolbar *self,
|
||||
LdCanvas *canvas);
|
||||
LdCanvas *ld_library_toolbar_get_canvas (LdLibraryToolbar *self);
|
||||
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* ! __LD_LIBRARY_TOOLBAR_H__ */
|
@ -86,3 +86,40 @@ g_cclosure_user_marshal_VOID__OBJECT_OBJECT (GClosure *closure,
|
||||
data2);
|
||||
}
|
||||
|
||||
/* VOID:OBJECT,STRING (ld-marshal.list:2) */
|
||||
void
|
||||
g_cclosure_user_marshal_VOID__OBJECT_STRING (GClosure *closure,
|
||||
GValue *return_value G_GNUC_UNUSED,
|
||||
guint n_param_values,
|
||||
const GValue *param_values,
|
||||
gpointer invocation_hint G_GNUC_UNUSED,
|
||||
gpointer marshal_data)
|
||||
{
|
||||
typedef void (*GMarshalFunc_VOID__OBJECT_STRING) (gpointer data1,
|
||||
gpointer arg_1,
|
||||
gpointer arg_2,
|
||||
gpointer data2);
|
||||
register GMarshalFunc_VOID__OBJECT_STRING callback;
|
||||
register GCClosure *cc = (GCClosure*) closure;
|
||||
register gpointer data1, data2;
|
||||
|
||||
g_return_if_fail (n_param_values == 3);
|
||||
|
||||
if (G_CCLOSURE_SWAP_DATA (closure))
|
||||
{
|
||||
data1 = closure->data;
|
||||
data2 = g_value_peek_pointer (param_values + 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
data1 = g_value_peek_pointer (param_values + 0);
|
||||
data2 = closure->data;
|
||||
}
|
||||
callback = (GMarshalFunc_VOID__OBJECT_STRING) (marshal_data ? marshal_data : cc->callback);
|
||||
|
||||
callback (data1,
|
||||
g_marshal_value_peek_object (param_values + 1),
|
||||
g_marshal_value_peek_string (param_values + 2),
|
||||
data2);
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,14 @@ extern void g_cclosure_user_marshal_VOID__OBJECT_OBJECT (GClosure *closure,
|
||||
gpointer invocation_hint,
|
||||
gpointer marshal_data);
|
||||
|
||||
/* VOID:OBJECT,STRING (ld-marshal.list:2) */
|
||||
extern void g_cclosure_user_marshal_VOID__OBJECT_STRING (GClosure *closure,
|
||||
GValue *return_value,
|
||||
guint n_param_values,
|
||||
const GValue *param_values,
|
||||
gpointer invocation_hint,
|
||||
gpointer marshal_data);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __g_cclosure_user_marshal_MARSHAL_H__ */
|
||||
|
@ -1 +1,2 @@
|
||||
VOID:OBJECT,OBJECT
|
||||
VOID:OBJECT,STRING
|
||||
|
@ -26,6 +26,7 @@
|
||||
#include "ld-diagram.h"
|
||||
|
||||
#include "ld-canvas.h"
|
||||
#include "ld-library-toolbar.h"
|
||||
|
||||
#include "ld-lua.h"
|
||||
#include "ld-lua-symbol.h"
|
||||
|
@ -23,47 +23,6 @@
|
||||
* #LdWindowMain is the main window of the application.
|
||||
*/
|
||||
|
||||
/*
|
||||
* SymbolMenuItem:
|
||||
*
|
||||
* Data related to a symbol in an open symbol menu.
|
||||
*/
|
||||
typedef struct _SymbolMenuItem SymbolMenuItem;
|
||||
|
||||
struct _SymbolMenuItem
|
||||
{
|
||||
LdSymbol *symbol;
|
||||
|
||||
gint width;
|
||||
gdouble dx;
|
||||
gdouble scale;
|
||||
};
|
||||
|
||||
/*
|
||||
* SymbolMenuData:
|
||||
*
|
||||
* Data related to the currently opened symbol menu.
|
||||
*/
|
||||
typedef struct _SymbolMenuData SymbolMenuData;
|
||||
|
||||
struct _SymbolMenuData
|
||||
{
|
||||
gulong expose_handler;
|
||||
gulong motion_notify_handler;
|
||||
gulong button_press_handler;
|
||||
gulong button_release_handler;
|
||||
|
||||
GtkToggleButton *active_button;
|
||||
|
||||
SymbolMenuItem *items;
|
||||
gint n_items;
|
||||
gint active_item;
|
||||
|
||||
gint menu_width;
|
||||
gint menu_height;
|
||||
gint menu_y;
|
||||
};
|
||||
|
||||
struct _LdWindowMainPrivate
|
||||
{
|
||||
GtkUIManager *ui_manager;
|
||||
@ -86,33 +45,13 @@ struct _LdWindowMainPrivate
|
||||
GtkWidget *statusbar;
|
||||
guint statusbar_symbol_context_id;
|
||||
guint statusbar_menu_context_id;
|
||||
|
||||
SymbolMenuData symbol_menu;
|
||||
};
|
||||
|
||||
#define LIBRARY_TOOLBAR_ICON_WIDTH 32
|
||||
|
||||
|
||||
/* ===== Local functions =================================================== */
|
||||
|
||||
static void ld_window_main_finalize (GObject *gobject);
|
||||
|
||||
static void load_library_toolbar (LdWindowMain *self);
|
||||
static void load_category_cb (gpointer data, gpointer user_data);
|
||||
static GdkPixbuf *recolor_pixbuf (GdkPixbuf *pbuf, GdkColor *color);
|
||||
|
||||
static void redraw_symbol_menu (LdWindowMain *self);
|
||||
static void on_category_toggle (GtkToggleButton *toggle_button,
|
||||
gpointer user_data);
|
||||
static gboolean on_canvas_exposed (GtkWidget *widget,
|
||||
GdkEventExpose *event, gpointer user_data);
|
||||
static gboolean on_canvas_motion_notify (GtkWidget *widget,
|
||||
GdkEventMotion *event, gpointer user_data);
|
||||
static gboolean on_canvas_button_press (GtkWidget *widget,
|
||||
GdkEventButton *event, gpointer user_data);
|
||||
static gboolean on_canvas_button_release (GtkWidget *widget,
|
||||
GdkEventButton *event, gpointer user_data);
|
||||
|
||||
static void on_ui_proxy_connected (GtkUIManager *ui, GtkAction *action,
|
||||
GtkWidget *proxy, LdWindowMain *window);
|
||||
static void on_ui_proxy_disconnected (GtkUIManager *ui, GtkAction *action,
|
||||
@ -143,6 +82,13 @@ static gboolean may_close_diagram (LdWindowMain *self,
|
||||
const gchar *dialog_message);
|
||||
static gboolean may_quit (LdWindowMain *self);
|
||||
|
||||
static void on_symbol_selected (LdLibraryToolbar *toolbar, LdSymbol *symbol,
|
||||
const gchar *klass, LdWindowMain *self);
|
||||
static void on_symbol_deselected (LdLibraryToolbar *toolbar, LdSymbol *symbol,
|
||||
const gchar *klass, LdWindowMain *self);
|
||||
static void on_symbol_chosen (LdLibraryToolbar *toolbar, LdSymbol *symbol,
|
||||
const gchar *klass, LdWindowMain *self);
|
||||
|
||||
static void on_action_new (GtkAction *action, LdWindowMain *self);
|
||||
static void on_action_open (GtkAction *action, LdWindowMain *self);
|
||||
static void on_action_save (GtkAction *action, LdWindowMain *self);
|
||||
@ -276,7 +222,7 @@ ld_window_main_init (LdWindowMain *self)
|
||||
priv->toolbar = gtk_ui_manager_get_widget (priv->ui_manager, "/Toolbar");
|
||||
|
||||
/* Create the remaining widgets. */
|
||||
priv->library_toolbar = gtk_toolbar_new ();
|
||||
priv->library_toolbar = ld_library_toolbar_new ();
|
||||
/* XXX: For GTK 2.16+, s/toolbar/orientable/ */
|
||||
gtk_toolbar_set_orientation (GTK_TOOLBAR (priv->library_toolbar),
|
||||
GTK_ORIENTATION_VERTICAL);
|
||||
@ -316,32 +262,6 @@ ld_window_main_init (LdWindowMain *self)
|
||||
gtk_window_set_default_size (GTK_WINDOW (self), 500, 400);
|
||||
gtk_window_set_position (GTK_WINDOW (self), GTK_WIN_POS_CENTER);
|
||||
|
||||
/* Hook canvas signals. */
|
||||
/* XXX: To be able to draw a symbol menu over the canvas, we may:
|
||||
* 1. Hook the expose-event and button-{press,release}-event signals.
|
||||
* 2. Create a hook mechanism in the LdCanvas object.
|
||||
* + The cairo context would not have to be created twice.
|
||||
* - More complex API.
|
||||
*/
|
||||
priv->symbol_menu.expose_handler = g_signal_connect (priv->canvas,
|
||||
"expose-event", G_CALLBACK (on_canvas_exposed), self);
|
||||
priv->symbol_menu.motion_notify_handler = g_signal_connect (priv->canvas,
|
||||
"motion-notify-event", G_CALLBACK (on_canvas_motion_notify), self);
|
||||
priv->symbol_menu.button_press_handler = g_signal_connect (priv->canvas,
|
||||
"button-press-event", G_CALLBACK (on_canvas_button_press), self);
|
||||
priv->symbol_menu.button_release_handler = g_signal_connect (priv->canvas,
|
||||
"button-release-event", G_CALLBACK (on_canvas_button_release), self);
|
||||
|
||||
/* Don't process the signals yet. */
|
||||
g_signal_handler_block (priv->canvas,
|
||||
priv->symbol_menu.expose_handler);
|
||||
g_signal_handler_block (priv->canvas,
|
||||
priv->symbol_menu.motion_notify_handler);
|
||||
g_signal_handler_block (priv->canvas,
|
||||
priv->symbol_menu.button_press_handler);
|
||||
g_signal_handler_block (priv->canvas,
|
||||
priv->symbol_menu.button_release_handler);
|
||||
|
||||
/* Initialize the backend. */
|
||||
priv->diagram = ld_diagram_new ();
|
||||
|
||||
@ -357,7 +277,18 @@ ld_window_main_init (LdWindowMain *self)
|
||||
ld_canvas_set_diagram (priv->canvas, priv->diagram);
|
||||
ld_canvas_set_library (priv->canvas, priv->library);
|
||||
|
||||
load_library_toolbar (self);
|
||||
ld_library_toolbar_set_library (LD_LIBRARY_TOOLBAR (priv->library_toolbar),
|
||||
priv->library);
|
||||
ld_library_toolbar_set_canvas (LD_LIBRARY_TOOLBAR (priv->library_toolbar),
|
||||
priv->canvas);
|
||||
|
||||
g_signal_connect_after (priv->library_toolbar, "symbol-selected",
|
||||
G_CALLBACK (on_symbol_selected), self);
|
||||
g_signal_connect_after (priv->library_toolbar, "symbol-deselected",
|
||||
G_CALLBACK (on_symbol_deselected), self);
|
||||
g_signal_connect_after (priv->library_toolbar, "symbol-chosen",
|
||||
G_CALLBACK (on_symbol_chosen), self);
|
||||
|
||||
diagram_set_filename (self, NULL);
|
||||
|
||||
action_set_sensitive (self, "Export", FALSE);
|
||||
@ -372,11 +303,6 @@ ld_window_main_init (LdWindowMain *self)
|
||||
gtk_widget_show_all (GTK_WIDGET (self));
|
||||
}
|
||||
|
||||
/*
|
||||
* ld_window_main_finalize:
|
||||
*
|
||||
* Dispose of all the resources owned by this window.
|
||||
*/
|
||||
static void
|
||||
ld_window_main_finalize (GObject *gobject)
|
||||
{
|
||||
@ -454,419 +380,6 @@ action_set_sensitive (LdWindowMain *self, const gchar *name, gboolean sensitive)
|
||||
}
|
||||
|
||||
|
||||
/* ===== Library toolbar and symbol menu =================================== */
|
||||
|
||||
/*
|
||||
* load_library_toolbar:
|
||||
*
|
||||
* Load symbols from the library into the library toolbar.
|
||||
*/
|
||||
static void
|
||||
load_library_toolbar (LdWindowMain *self)
|
||||
{
|
||||
GSList *categories;
|
||||
|
||||
g_return_if_fail (LD_IS_WINDOW_MAIN (self));
|
||||
|
||||
/* Clear the toolbar first, if there was already something in it. */
|
||||
gtk_container_foreach (GTK_CONTAINER (self->priv->library_toolbar),
|
||||
(GtkCallback) gtk_widget_destroy, NULL);
|
||||
|
||||
categories = (GSList *) ld_library_get_children (self->priv->library);
|
||||
g_slist_foreach (categories, load_category_cb, self);
|
||||
}
|
||||
|
||||
/*
|
||||
* load_category_cb:
|
||||
*
|
||||
* A foreach callback for adding categories into the library toolbar.
|
||||
*/
|
||||
static void
|
||||
load_category_cb (gpointer data, gpointer user_data)
|
||||
{
|
||||
LdWindowMain *self;
|
||||
LdSymbolCategory *cat;
|
||||
const gchar *human_name;
|
||||
GdkPixbuf *pbuf, *new_pbuf;
|
||||
GtkWidget *img;
|
||||
GtkToolItem *item;
|
||||
GtkWidget *button;
|
||||
GtkStyle *style;
|
||||
|
||||
g_return_if_fail (LD_IS_WINDOW_MAIN (user_data));
|
||||
g_return_if_fail (LD_IS_SYMBOL_CATEGORY (data));
|
||||
|
||||
self = user_data;
|
||||
cat = data;
|
||||
|
||||
pbuf = gdk_pixbuf_new_from_file_at_size (ld_symbol_category_get_image_path
|
||||
(cat), LIBRARY_TOOLBAR_ICON_WIDTH, -1, NULL);
|
||||
g_return_if_fail (pbuf != NULL);
|
||||
|
||||
button = gtk_toggle_button_new ();
|
||||
style = gtk_rc_get_style (button);
|
||||
|
||||
/* TODO: Handle all states. */
|
||||
new_pbuf = recolor_pixbuf (pbuf, &style->fg[GTK_STATE_NORMAL]);
|
||||
if (new_pbuf)
|
||||
{
|
||||
g_object_unref (pbuf);
|
||||
pbuf = new_pbuf;
|
||||
}
|
||||
|
||||
img = gtk_image_new_from_pixbuf (pbuf);
|
||||
g_object_unref (pbuf);
|
||||
|
||||
item = gtk_tool_item_new ();
|
||||
gtk_container_add (GTK_CONTAINER (button), img);
|
||||
gtk_container_add (GTK_CONTAINER (item), button);
|
||||
|
||||
/* Don't steal focus from the canvas. */
|
||||
g_object_set (button, "can-focus", FALSE, NULL);
|
||||
|
||||
/* Assign the category to the toggle button. */
|
||||
g_object_ref (cat);
|
||||
g_object_set_data_full (G_OBJECT (button),
|
||||
"category", cat, (GDestroyNotify) g_object_unref);
|
||||
|
||||
/* Hook toggling of the button. */
|
||||
g_signal_connect (button, "toggled", G_CALLBACK (on_category_toggle), self);
|
||||
|
||||
human_name = ld_symbol_category_get_human_name (cat);
|
||||
gtk_tool_item_set_tooltip_text (item, human_name);
|
||||
gtk_toolbar_insert (GTK_TOOLBAR (self->priv->library_toolbar), item, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* recolor_pixbuf:
|
||||
*
|
||||
* Change the color of all pixels to @color,
|
||||
* but leave the alpha channel intact.
|
||||
*/
|
||||
static GdkPixbuf *
|
||||
recolor_pixbuf (GdkPixbuf *pbuf, GdkColor *color)
|
||||
{
|
||||
gint width, height;
|
||||
GdkPixbuf *new_pbuf;
|
||||
cairo_surface_t *cr_surface;
|
||||
cairo_t *cr;
|
||||
|
||||
width = gdk_pixbuf_get_width (pbuf);
|
||||
height = gdk_pixbuf_get_height (pbuf);
|
||||
|
||||
new_pbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
|
||||
g_return_val_if_fail (new_pbuf != NULL, NULL);
|
||||
|
||||
cr_surface = cairo_image_surface_create_for_data
|
||||
(gdk_pixbuf_get_pixels (new_pbuf), CAIRO_FORMAT_ARGB32,
|
||||
width, height, gdk_pixbuf_get_rowstride (new_pbuf));
|
||||
cr = cairo_create (cr_surface);
|
||||
|
||||
gdk_cairo_set_source_color (cr, color);
|
||||
cairo_paint (cr);
|
||||
|
||||
cairo_set_operator (cr, CAIRO_OPERATOR_DEST_IN);
|
||||
gdk_cairo_set_source_pixbuf (cr, pbuf, 0, 0);
|
||||
cairo_paint (cr);
|
||||
|
||||
cairo_destroy (cr);
|
||||
cairo_surface_destroy (cr_surface);
|
||||
return new_pbuf;
|
||||
}
|
||||
|
||||
/*
|
||||
* redraw_symbol_menu:
|
||||
*
|
||||
* Make the area for symbol menu redraw itself.
|
||||
*/
|
||||
static void
|
||||
redraw_symbol_menu (LdWindowMain *self)
|
||||
{
|
||||
SymbolMenuData *data;
|
||||
|
||||
g_return_if_fail (LD_IS_WINDOW_MAIN (self));
|
||||
data = &self->priv->symbol_menu;
|
||||
|
||||
gtk_widget_queue_draw_area (GTK_WIDGET (self->priv->canvas),
|
||||
0, data->menu_y - 1, data->menu_width + 2, data->menu_height + 2);
|
||||
}
|
||||
|
||||
/*
|
||||
* on_category_toggle:
|
||||
*
|
||||
* Show or hide a symbol menu.
|
||||
*/
|
||||
static void
|
||||
on_category_toggle (GtkToggleButton *toggle_button, gpointer user_data)
|
||||
{
|
||||
LdWindowMain *self;
|
||||
LdWindowMainPrivate *priv;
|
||||
LdSymbolCategory *cat;
|
||||
SymbolMenuData *data;
|
||||
|
||||
cat = g_object_get_data (G_OBJECT (toggle_button), "category");
|
||||
self = LD_WINDOW_MAIN (user_data);
|
||||
priv = self->priv;
|
||||
data = &priv->symbol_menu;
|
||||
|
||||
/* First untoggle any active button. */
|
||||
if (data->active_button)
|
||||
gtk_toggle_button_set_active (data->active_button, FALSE);
|
||||
|
||||
/* And toggle signal handlers that enable the user to add a symbol. */
|
||||
if (data->active_button == toggle_button)
|
||||
{
|
||||
gint i;
|
||||
|
||||
g_signal_handler_block (priv->canvas,
|
||||
priv->symbol_menu.expose_handler);
|
||||
g_signal_handler_block (priv->canvas,
|
||||
priv->symbol_menu.motion_notify_handler);
|
||||
g_signal_handler_block (priv->canvas,
|
||||
priv->symbol_menu.button_press_handler);
|
||||
g_signal_handler_block (priv->canvas,
|
||||
priv->symbol_menu.button_release_handler);
|
||||
|
||||
g_object_unref (data->active_button);
|
||||
data->active_button = NULL;
|
||||
|
||||
/* Ashes to ashes, NULL to NULL. */
|
||||
for (i = 0; i < data->n_items; i++)
|
||||
g_object_unref (data->items[i].symbol);
|
||||
|
||||
g_free (data->items);
|
||||
data->items = NULL;
|
||||
|
||||
gtk_statusbar_pop (GTK_STATUSBAR (self->priv->statusbar),
|
||||
self->priv->statusbar_menu_context_id);
|
||||
|
||||
gtk_grab_remove (GTK_WIDGET (self->priv->canvas));
|
||||
}
|
||||
else
|
||||
{
|
||||
const GSList *children, *symbol_iter;
|
||||
SymbolMenuItem *item;
|
||||
gint x, y, menu_width;
|
||||
|
||||
g_return_if_fail (gtk_widget_translate_coordinates (GTK_WIDGET
|
||||
(toggle_button), GTK_WIDGET (priv->canvas), 0, 0, &x, &y));
|
||||
|
||||
data->menu_y = y;
|
||||
data->menu_height = GTK_WIDGET (toggle_button)->allocation.height;
|
||||
|
||||
g_signal_handler_unblock (priv->canvas,
|
||||
priv->symbol_menu.expose_handler);
|
||||
g_signal_handler_unblock (priv->canvas,
|
||||
priv->symbol_menu.motion_notify_handler);
|
||||
g_signal_handler_unblock (priv->canvas,
|
||||
priv->symbol_menu.button_press_handler);
|
||||
g_signal_handler_unblock (priv->canvas,
|
||||
priv->symbol_menu.button_release_handler);
|
||||
|
||||
data->active_button = toggle_button;
|
||||
g_object_ref (data->active_button);
|
||||
|
||||
children = ld_symbol_category_get_children (cat);
|
||||
|
||||
data->n_items = g_slist_length ((GSList *) children);
|
||||
data->items = g_new (SymbolMenuItem, data->n_items);
|
||||
data->active_item = -1;
|
||||
|
||||
item = data->items;
|
||||
menu_width = 0;
|
||||
for (symbol_iter = children; symbol_iter;
|
||||
symbol_iter = symbol_iter->next)
|
||||
{
|
||||
LdRectangle area;
|
||||
|
||||
item->symbol = LD_SYMBOL (symbol_iter->data);
|
||||
g_object_ref (item->symbol);
|
||||
|
||||
ld_symbol_get_area (item->symbol, &area);
|
||||
|
||||
/* This is the height when the center of the symbol is
|
||||
* in the center of it's symbol menu item.
|
||||
*/
|
||||
item->scale = data->menu_height * 0.5
|
||||
/ MAX (ABS (area.y), ABS (area.y + area.height)) * 0.5;
|
||||
item->width = data->menu_height * 0.5 + item->scale * area.width;
|
||||
item->dx = item->width * 0.5 + item->scale * (area.width * 0.5
|
||||
- MAX (ABS (area.x), ABS (area.x + area.width)));
|
||||
|
||||
menu_width += item++->width;
|
||||
}
|
||||
data->menu_width = menu_width;
|
||||
|
||||
gtk_grab_add (GTK_WIDGET (self->priv->canvas));
|
||||
}
|
||||
redraw_symbol_menu (self);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
on_canvas_exposed (GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
|
||||
{
|
||||
cairo_t *cr;
|
||||
LdWindowMain *self;
|
||||
SymbolMenuData *data;
|
||||
gint i, x;
|
||||
|
||||
cr = gdk_cairo_create (widget->window);
|
||||
self = LD_WINDOW_MAIN (user_data);
|
||||
data = &self->priv->symbol_menu;
|
||||
|
||||
/* Draw some border. */
|
||||
cairo_set_line_width (cr, 1);
|
||||
|
||||
cairo_rectangle (cr, 0, data->menu_y, data->menu_width, data->menu_height);
|
||||
cairo_set_source_rgb (cr, 1, 1, 1);
|
||||
cairo_fill (cr);
|
||||
|
||||
/* Draw all symbols from that category. */
|
||||
for (x = i = 0; i < data->n_items; i++)
|
||||
{
|
||||
SymbolMenuItem *item;
|
||||
|
||||
item = data->items + i;
|
||||
cairo_save (cr);
|
||||
|
||||
cairo_rectangle (cr, x, data->menu_y, item->width, data->menu_height);
|
||||
cairo_clip (cr);
|
||||
|
||||
if (i == data->active_item)
|
||||
{
|
||||
cairo_set_source_rgb (cr, 0.9, 0.9, 0.9);
|
||||
cairo_paint (cr);
|
||||
}
|
||||
|
||||
cairo_translate (cr, x + item->dx,
|
||||
data->menu_y + data->menu_height * 0.5);
|
||||
cairo_scale (cr, item->scale, item->scale);
|
||||
|
||||
cairo_set_source_rgb (cr, 0, 0, 0);
|
||||
cairo_set_line_width (cr, 1 / item->scale);
|
||||
ld_symbol_draw (item->symbol, cr);
|
||||
|
||||
cairo_restore (cr);
|
||||
x += item->width;
|
||||
}
|
||||
|
||||
cairo_rectangle (cr, 0, data->menu_y, data->menu_width, data->menu_height);
|
||||
cairo_set_source_rgb (cr, 0, 0, 0);
|
||||
cairo_stroke (cr);
|
||||
|
||||
cairo_destroy (cr);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
on_canvas_motion_notify (GtkWidget *widget, GdkEventMotion *event,
|
||||
gpointer user_data)
|
||||
{
|
||||
LdWindowMain *self;
|
||||
SymbolMenuData *data;
|
||||
gint i, x, at_cursor = -1;
|
||||
|
||||
self = LD_WINDOW_MAIN (user_data);
|
||||
data = &self->priv->symbol_menu;
|
||||
|
||||
if (widget->window != event->window
|
||||
|| event->x < 0 || event->y < data->menu_y
|
||||
|| event->y >= data->menu_y + data->menu_height)
|
||||
goto on_canvas_motion_notify_end;
|
||||
|
||||
for (x = i = 0; i < data->n_items; i++)
|
||||
{
|
||||
x += data->items[i].width;
|
||||
if (event->x < x)
|
||||
{
|
||||
at_cursor = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
on_canvas_motion_notify_end:
|
||||
if (data->active_item != at_cursor)
|
||||
{
|
||||
const gchar *symbol_name;
|
||||
|
||||
gtk_statusbar_pop (GTK_STATUSBAR (self->priv->statusbar),
|
||||
self->priv->statusbar_menu_context_id);
|
||||
|
||||
if (at_cursor != -1)
|
||||
{
|
||||
symbol_name = ld_symbol_get_human_name (data->items[i].symbol);
|
||||
gtk_statusbar_push (GTK_STATUSBAR (self->priv->statusbar),
|
||||
self->priv->statusbar_menu_context_id, symbol_name);
|
||||
}
|
||||
}
|
||||
data->active_item = at_cursor;
|
||||
redraw_symbol_menu (self);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
on_canvas_button_press (GtkWidget *widget, GdkEventButton *event,
|
||||
gpointer user_data)
|
||||
{
|
||||
LdWindowMain *self;
|
||||
SymbolMenuData *data;
|
||||
|
||||
self = LD_WINDOW_MAIN (user_data);
|
||||
data = &self->priv->symbol_menu;
|
||||
|
||||
/* If the event occured elsewhere, cancel the menu and put the event
|
||||
* back into the queue.
|
||||
*/
|
||||
if (widget->window != event->window && data->active_button)
|
||||
{
|
||||
gtk_toggle_button_set_active (data->active_button, FALSE);
|
||||
gdk_event_put ((GdkEvent *) event);
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
on_canvas_button_release (GtkWidget *widget, GdkEventButton *event,
|
||||
gpointer user_data)
|
||||
{
|
||||
LdWindowMain *self;
|
||||
SymbolMenuData *data;
|
||||
|
||||
self = LD_WINDOW_MAIN (user_data);
|
||||
data = &self->priv->symbol_menu;
|
||||
|
||||
if (event->button != 1)
|
||||
return FALSE;
|
||||
|
||||
if (data->active_item != -1)
|
||||
{
|
||||
LdDiagramSymbol *symbol;
|
||||
const gchar *category_name, *symbol_name;
|
||||
gchar *klass;
|
||||
|
||||
category_name = ld_symbol_category_get_name
|
||||
(g_object_get_data (G_OBJECT (data->active_button), "category"));
|
||||
symbol_name = ld_symbol_get_name
|
||||
(data->items[data->active_item].symbol);
|
||||
|
||||
klass = g_build_path (LD_LIBRARY_IDENTIFIER_SEPARATOR,
|
||||
category_name, symbol_name, NULL);
|
||||
symbol = ld_diagram_symbol_new (NULL);
|
||||
ld_diagram_symbol_set_class (symbol, klass);
|
||||
g_free (klass);
|
||||
|
||||
ld_canvas_add_object_begin (self->priv->canvas,
|
||||
LD_DIAGRAM_OBJECT (symbol));
|
||||
}
|
||||
|
||||
/* We've either chosen a symbol or canceled the menu, so hide it. */
|
||||
if (data->active_button)
|
||||
gtk_toggle_button_set_active (data->active_button, FALSE);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
/* ===== Menu items processing ============================================= */
|
||||
|
||||
static void
|
||||
@ -1215,6 +728,38 @@ may_quit (LdWindowMain *self)
|
||||
|
||||
/* ===== User interface actions ============================================ */
|
||||
|
||||
static void
|
||||
on_symbol_selected (LdLibraryToolbar *toolbar, LdSymbol *symbol,
|
||||
const gchar *klass, LdWindowMain *self)
|
||||
{
|
||||
const gchar *symbol_name;
|
||||
|
||||
symbol_name = ld_symbol_get_human_name (symbol);
|
||||
gtk_statusbar_push (GTK_STATUSBAR (self->priv->statusbar),
|
||||
self->priv->statusbar_menu_context_id, symbol_name);
|
||||
}
|
||||
|
||||
static void
|
||||
on_symbol_deselected (LdLibraryToolbar *toolbar, LdSymbol *symbol,
|
||||
const gchar *klass, LdWindowMain *self)
|
||||
{
|
||||
gtk_statusbar_pop (GTK_STATUSBAR (self->priv->statusbar),
|
||||
self->priv->statusbar_menu_context_id);
|
||||
}
|
||||
|
||||
static void
|
||||
on_symbol_chosen (LdLibraryToolbar *toolbar, LdSymbol *symbol,
|
||||
const gchar *klass, LdWindowMain *self)
|
||||
{
|
||||
LdDiagramSymbol *diagram_symbol;
|
||||
|
||||
diagram_symbol = ld_diagram_symbol_new (NULL);
|
||||
ld_diagram_symbol_set_class (diagram_symbol, klass);
|
||||
|
||||
ld_canvas_add_object_begin (self->priv->canvas,
|
||||
LD_DIAGRAM_OBJECT (diagram_symbol));
|
||||
}
|
||||
|
||||
static void
|
||||
on_action_new (GtkAction *action, LdWindowMain *self)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user