summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Seguin <guillaume@segu.in>2008-08-22 17:36:04 +0200
committerGuillaume Seguin <guillaume@segu.in>2008-08-22 17:36:04 +0200
commit3b09394a01a7b2f282713108c3dfc10b9f8ff09c (patch)
tree6058c9416146b8cdc46eacb78240e6fb1cf5b4ef
parent1133d98c4b221b1d01e11c15f799ee9f5bd7831d (diff)
downloadgmathlib-3b09394a01a7b2f282713108c3dfc10b9f8ff09c.tar.gz
gmathlib-3b09394a01a7b2f282713108c3dfc10b9f8ff09c.tar.bz2
* Import GMathSelector, a mathematic symbol selector widget for Gtk
-rw-r--r--gmathselector/CMakeLists.txt42
-rw-r--r--gmathselector/gmathselector.c307
-rw-r--r--gmathselector/gmathselector_marshallers.list1
-rw-r--r--gmathselector/gmathselector_private.h72
-rw-r--r--gmathselector/gtkcairoimage.c116
-rw-r--r--gmathselector/gtkcairoimage.h71
6 files changed, 609 insertions, 0 deletions
diff --git a/gmathselector/CMakeLists.txt b/gmathselector/CMakeLists.txt
new file mode 100644
index 0000000..e8a76e9
--- /dev/null
+++ b/gmathselector/CMakeLists.txt
@@ -0,0 +1,42 @@
+include_directories (
+ ${gMathLib_SOURCE_DIR}/include
+ ${GLIB_INCLUDE_DIRS}
+ ${GTK_INCLUDE_DIRS}
+)
+
+link_directories (
+ ${GLIB_LINK_DIRS}
+ ${GTK_LINK_DIRS}
+)
+
+add_custom_command (
+ OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/gmathselector_marshallers.h
+ COMMAND ${GENMARSHAL_EXECUTABLE} --prefix _gms_marshall --header
+ ${CMAKE_CURRENT_SOURCE_DIR}/gmathselector_marshallers.list >
+ ${CMAKE_CURRENT_BINARY_DIR}/gmathselector_marshallers.h
+ DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/gmathselector_marshallers.list
+)
+
+add_custom_command (
+ OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/gmathselector_marshallers.c
+ COMMAND ${GENMARSHAL_EXECUTABLE} --prefix _gms_marshall --body
+ ${CMAKE_CURRENT_SOURCE_DIR}/gmathselector_marshallers.list >
+ ${CMAKE_CURRENT_BINARY_DIR}/gmathselector_marshallers.c
+ DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/gmathselector_marshallers.list
+ ${CMAKE_CURRENT_BINARY_DIR}/gmathselector_marshallers.h
+)
+
+add_library (
+ gmathselector SHARED gmathselector.c
+ gtkcairoimage.c
+ gmathselector_marshallers.c
+)
+
+target_link_libraries (
+ gmathselector ${GLIB_LIBRARIES} ${GTK_LIBRARIES} gmathcairo gmathwrapbox
+)
+
+install (
+ TARGETS gmathselector
+ LIBRARY DESTINATION lib
+)
diff --git a/gmathselector/gmathselector.c b/gmathselector/gmathselector.c
new file mode 100644
index 0000000..99930c2
--- /dev/null
+++ b/gmathselector/gmathselector.c
@@ -0,0 +1,307 @@
+/*
+ * gMathSelector, mathematic symbol selector widget
+ *
+ * # Main widget source
+ *
+ * Author : Guillaume Seguin
+ * Email : guillaume@segu.in
+ *
+ * Copyright (c) 2007 Guillaume Seguin <guillaume@segu.in>
+ *
+ * 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.
+ */
+
+#include "gtkcairoimage.h"
+
+#include "gmathwrapbox.h"
+
+#include <gmathselector.h>
+#include "gmathselector_private.h"
+
+#include "gmathselector_marshallers.h"
+
+G_DEFINE_TYPE (GMathSelector, g_math_selector, GTK_TYPE_VBOX)
+
+static gboolean
+g_math_selector_do_destroy (GtkWidget *widget, GdkEventAny *event);
+
+static void
+g_math_selector_display_category (GtkWidget *widget, gpointer closure);
+
+static void
+g_math_selector_select_symbol (GtkWidget *widget, gpointer closure);
+
+enum
+{
+ G_M_S_SYMBOL_SELECTED,
+ G_M_S_SIGNAL_COUNT
+};
+
+static guint g_math_selector_signals[G_M_S_SIGNAL_COUNT] = { 0 };
+
+static void
+g_math_selector_class_init (GMathSelectorClass *class)
+{
+ GObjectClass *object_class;
+ GtkWidgetClass *widget_class;
+
+ object_class = G_OBJECT_CLASS (class);
+ widget_class = GTK_WIDGET_CLASS (class);
+
+ widget_class->destroy_event = g_math_selector_do_destroy;
+
+ g_math_selector_signals[G_M_S_SYMBOL_SELECTED] = g_signal_new (
+ "symbol-selected",
+ G_OBJECT_CLASS_TYPE (object_class),
+ G_SIGNAL_RUN_FIRST,
+ G_STRUCT_OFFSET (GMathSelectorClass, symbol_selected),
+ NULL, NULL,
+ _gms_marshall_VOID__STRING_STRING,
+ G_TYPE_NONE, 1,
+ G_TYPE_STRING);
+
+ g_type_class_add_private (object_class, sizeof (GMathSelectorPrivate));
+}
+
+static void
+g_math_selector_init (GMathSelector *mathselector)
+{
+ GMathSelectorPrivate *priv;
+ GtkAlignment *alignment;
+ alignment = GTK_ALIGNMENT (gtk_alignment_new (0.5, 0.5, 1, 0));
+
+ priv = G_MATH_SELECTOR_GET_PRIVATE (mathselector);
+
+ priv->categories_box = G_MATH_WRAPBOX (g_math_wrapbox_new (FALSE, FALSE,
+ FALSE));
+
+ gtk_container_add (GTK_CONTAINER (alignment),
+ GTK_WIDGET (priv->categories_box));
+
+ priv->categories = g_hash_table_new_full (g_str_hash, g_str_equal,
+ g_free, NULL);
+
+ priv->buttons = g_hash_table_new_full (g_str_hash, g_str_equal,
+ g_free, NULL);
+
+ priv->current_box = NULL;
+ priv->current_button = NULL;
+
+ priv->context = gmathcontext_new ();
+ priv->cairo_context = gmathcairocontext_new (priv->context);
+
+ priv->cairo_context->main_size = PREVIEW_SIZE;
+
+ gtk_box_pack_start (GTK_BOX (mathselector),
+ GTK_WIDGET (alignment), FALSE, FALSE, 0);
+ gtk_box_pack_start (GTK_BOX (mathselector), gtk_hseparator_new (),
+ FALSE, FALSE, 0);
+}
+
+static void
+g_math_selector_display_category (GtkWidget *widget, gpointer data)
+{
+ GMathSelectorCatClosure *closure;
+ GMathSelectorPrivate *priv;
+ GtkToggleButton *current_button;
+
+ closure = (GMathSelectorCatClosure *) data;
+ priv = G_MATH_SELECTOR_GET_PRIVATE (G_MATH_SELECTOR (closure->selector));
+
+ if (!gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
+ {
+ if (priv->current_button == closure->button)
+ gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
+ return;
+ }
+
+ if (priv->current_button == closure->button)
+ return;
+
+ if (priv->current_box)
+ gtk_container_remove (GTK_CONTAINER (closure->selector),
+ GTK_WIDGET (priv->current_box));
+ if (priv->current_button)
+ {
+ current_button = priv->current_button;
+ priv->current_button = closure->button;
+ gtk_toggle_button_set_active (current_button, FALSE);
+ }
+ else
+ priv->current_button = closure->button;
+
+ priv->current_box = closure->box;
+
+ gtk_box_pack_start (GTK_BOX (closure->selector), GTK_WIDGET (closure->box),
+ FALSE, FALSE, 0);
+
+ gtk_widget_show (GTK_WIDGET (closure->box));
+}
+
+static void
+g_math_selector_select_symbol (GtkWidget *widget, gpointer data)
+{
+ (void) widget;
+ GMathSelectorSymClosure *closure;
+
+ closure = (GMathSelectorSymClosure *) data;
+ g_signal_emit (G_OBJECT (closure->selector),
+ g_math_selector_signals[G_M_S_SYMBOL_SELECTED],
+ 0, closure->symbol->symbol, closure->symbol->value);
+}
+
+static gboolean
+g_math_selector_do_destroy (GtkWidget *widget, GdkEventAny *event)
+{
+ (void) event;
+ GMathSelectorPrivate *priv;
+ GList *categories;
+ GMathWrapBox *box;
+
+ priv = G_MATH_SELECTOR_GET_PRIVATE (G_MATH_SELECTOR (widget));
+ if (priv->categories)
+ {
+ categories = g_hash_table_get_keys (priv->categories);
+ while (categories)
+ {
+ box = g_hash_table_lookup (priv->categories, categories->data);
+ gtk_widget_destroy (GTK_WIDGET (box));
+ categories = categories->next;
+ }
+ g_list_free (categories);
+ g_hash_table_destroy (priv->categories);
+ }
+
+ if (priv->buttons)
+ g_hash_table_destroy (priv->buttons);
+
+ gmathcontext_free (priv->context);
+ gmathcairocontext_free (priv->cairo_context);
+
+ gtk_widget_destroy (widget);
+
+ return TRUE;
+}
+
+void
+g_math_selector_add_category (GMathSelector *mathselector, gchar *category)
+{
+ GMathSelectorPrivate *priv;
+ GtkToggleButton *category_button;
+ GMathWrapBox *category_box;
+ GMathSelectorCatClosure *closure;
+
+ priv = G_MATH_SELECTOR_GET_PRIVATE (mathselector);
+ if (g_hash_table_lookup (priv->categories, category))
+ return;
+
+ category_box = G_MATH_WRAPBOX (g_math_wrapbox_new (FALSE, FALSE, FALSE));
+ g_object_ref (category_box);
+
+ category_button = GTK_TOGGLE_BUTTON (gtk_toggle_button_new ());
+ gtk_button_set_label (GTK_BUTTON (category_button), g_strdup (category));
+
+ closure = g_new (GMathSelectorCatClosure, 1);
+ closure->selector = mathselector;
+ closure->box = category_box;
+ closure->button = category_button;
+ g_object_connect (G_OBJECT (category_button), "signal::toggled",
+ g_math_selector_display_category,
+ (gpointer) closure, NULL);
+
+ g_math_wrapbox_add (GTK_CONTAINER (priv->categories_box),
+ GTK_WIDGET (category_button));
+
+ gtk_widget_show (GTK_WIDGET (category_button));
+
+ g_hash_table_insert (priv->categories, g_strdup (category), category_box);
+ g_hash_table_insert (priv->buttons, g_strdup (category), category_button);
+}
+
+static cairo_surface_t*
+g_math_selector_get_surface (GMathSelector *mathselector,
+ gchar *type, gchar *value)
+{
+ GMathSelectorPrivate *priv;
+ GMathSymbol *symbol;
+ GMathFormula *formula;
+
+ priv = G_MATH_SELECTOR_GET_PRIVATE (mathselector);
+
+ symbol = gmathsymbol_new (priv->context, type);
+ gmathsymbol_set_value (symbol, value);
+
+ formula = gmathformula_new (priv->context);
+ gmathformula_set_root (formula, symbol);
+
+ return gmathcairo_render (priv->cairo_context, formula);
+}
+
+void
+g_math_selector_add_symbol (GMathSelector *mathselector,
+ gchar *symbol, gchar *value,
+ gchar *category)
+{
+ GMathSelectorPrivate *priv;
+ GMathWrapBox *category_box;
+ GtkButton *symbol_button;
+ GMathSelectorSymClosure *closure;
+ GMathSelectorSymbol *sym;
+ cairo_surface_t *surface;
+ GtkCairoImage *image;
+
+ priv = G_MATH_SELECTOR_GET_PRIVATE (mathselector);
+
+ /* First of all, try to render the symbol to check if it is supported */
+ surface = g_math_selector_get_surface (mathselector, symbol, value);
+ if (!surface)
+ return;
+
+ category_box = g_hash_table_lookup (priv->categories, category);
+ if (!category_box)
+ {
+ g_math_selector_add_category (mathselector, category);
+ category_box = g_hash_table_lookup (priv->categories, category);
+ }
+
+ symbol_button = GTK_BUTTON (gtk_button_new ());
+ image = GTK_CAIRO_IMAGE (gtk_cairo_image_new (surface));
+ gtk_container_add (GTK_CONTAINER (symbol_button), GTK_WIDGET (image));
+
+ sym = g_new (GMathSelectorSymbol, 1);
+ sym->symbol = g_strdup (symbol);
+ sym->value = g_strdup (value);
+
+ closure = g_new (GMathSelectorSymClosure, 1);
+ closure->selector = mathselector;
+ closure->symbol = sym;
+
+ g_object_connect (G_OBJECT (symbol_button), "signal::clicked",
+ g_math_selector_select_symbol,
+ (gpointer) closure, NULL);
+
+ g_math_wrapbox_add (GTK_CONTAINER (category_box),
+ GTK_WIDGET (symbol_button));
+
+ gtk_widget_show (GTK_WIDGET (image));
+ gtk_widget_show (GTK_WIDGET (symbol_button));
+}
+
+GtkWidget*
+g_math_selector_new (void)
+{
+ return g_object_new (G_TYPE_MATH_SELECTOR, NULL);
+}
diff --git a/gmathselector/gmathselector_marshallers.list b/gmathselector/gmathselector_marshallers.list
new file mode 100644
index 0000000..72f9937
--- /dev/null
+++ b/gmathselector/gmathselector_marshallers.list
@@ -0,0 +1 @@
+VOID:STRING,STRING
diff --git a/gmathselector/gmathselector_private.h b/gmathselector/gmathselector_private.h
new file mode 100644
index 0000000..33212d1
--- /dev/null
+++ b/gmathselector/gmathselector_private.h
@@ -0,0 +1,72 @@
+/*
+ * gMathSelector, mathematic symbol selector widget
+ *
+ * # Internal widget header
+ *
+ * Author : Guillaume Seguin
+ * Email : guillaume@segu.in
+ *
+ * Copyright (c) 2007 Guillaume Seguin <guillaume@segu.in>
+ *
+ * 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.
+ */
+
+#ifndef _GMATHSELECTOR_PRIVATE_H
+#define _GMATHSELECTOR_PRIVATE_H
+
+#include <gmathselector.h>
+
+#define G_MATH_SELECTOR_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
+ G_TYPE_MATH_SELECTOR, \
+ GMathSelectorPrivate))
+
+typedef struct _GMathSelectorPrivate GMathSelectorPrivate;
+typedef struct _GMathSelectorCatClosure GMathSelectorCatClosure;
+typedef struct _GMathSelectorSymClosure GMathSelectorSymClosure;
+typedef struct _GMathSelectorSymbol GMathSelectorSymbol;
+
+struct _GMathSelectorCatClosure
+{
+ GMathSelector *selector;
+ GMathWrapBox *box;
+ GtkToggleButton *button;
+};
+
+struct _GMathSelectorSymClosure
+{
+ GMathSelector *selector;
+ GMathSelectorSymbol *symbol;
+};
+
+struct _GMathSelectorSymbol
+{
+ gchar *symbol;
+ gchar *value;
+};
+
+struct _GMathSelectorPrivate
+{
+ GMathWrapBox *categories_box;
+ GHashTable *categories;
+ GHashTable *buttons;
+ GMathWrapBox *current_box;
+ GtkToggleButton *current_button;
+
+ GMathContext *context;
+ GMathCairoContext *cairo_context;
+};
+
+#endif
diff --git a/gmathselector/gtkcairoimage.c b/gmathselector/gtkcairoimage.c
new file mode 100644
index 0000000..aa7345b
--- /dev/null
+++ b/gmathselector/gtkcairoimage.c
@@ -0,0 +1,116 @@
+/*
+ * GtkCairoImage, simple widget to display a Cairo Surface
+ *
+ * # Main widget source
+ *
+ * Author : Guillaume Seguin
+ * Email : guillaume@segu.in
+ *
+ * Copyright (c) 2007 Guillaume Seguin <guillaume@segu.in>
+ *
+ * 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.
+ */
+
+#include "gtkcairoimage.h"
+
+G_DEFINE_TYPE (GtkCairoImage, gtk_cairo_image, GTK_TYPE_WIDGET)
+
+static gboolean
+gtk_cairo_image_do_expose (GtkWidget *widget, GdkEventExpose *event);
+
+static gboolean
+gtk_cairo_image_do_destroy (GtkWidget *widget, GdkEventAny *event);
+
+static void
+gtk_cairo_image_class_init (GtkCairoImageClass *class)
+{
+ GtkWidgetClass *widget_class;
+
+ widget_class = GTK_WIDGET_CLASS (class);
+
+ widget_class->destroy_event = gtk_cairo_image_do_destroy;
+ widget_class->expose_event = gtk_cairo_image_do_expose;
+}
+
+static void
+gtk_cairo_image_init (GtkCairoImage *image)
+{
+ GTK_WIDGET_SET_FLAGS (GTK_WIDGET (image), GTK_NO_WINDOW);
+}
+
+static gboolean
+gtk_cairo_image_do_destroy (GtkWidget *widget, GdkEventAny *event)
+{
+ (void) event;
+ GtkCairoImage *image;
+
+ image = GTK_CAIRO_IMAGE (widget);
+ if (image->surface)
+ {
+ cairo_surface_destroy (image->surface);
+ image->surface = NULL;
+ }
+
+ gtk_widget_destroy (widget);
+
+ return TRUE;
+}
+
+static gboolean
+gtk_cairo_image_do_expose (GtkWidget *widget, GdkEventExpose *event)
+{
+ cairo_t *cr;
+ GtkCairoImage *image;
+
+ image = GTK_CAIRO_IMAGE (widget);
+ if (!image->surface)
+ return FALSE;
+
+ cr = gdk_cairo_create (widget->window);
+
+ cairo_rectangle (cr, event->area.x, event->area.y,
+ event->area.width, event->area.height);
+ cairo_clip (cr);
+
+ cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
+ cairo_set_source_surface (cr, image->surface,
+ widget->allocation.x,
+ widget->allocation.y);
+ cairo_paint (cr);
+
+ cairo_destroy (cr);
+
+ return FALSE;
+}
+
+GtkWidget*
+gtk_cairo_image_new (cairo_surface_t *surface)
+{
+ GtkWidget *widget;
+ int width, height;
+
+ widget = g_object_new (GTK_TYPE_CAIRO_IMAGE, NULL);
+ GTK_CAIRO_IMAGE (widget)->surface = surface;
+
+ if (surface)
+ {
+ width = cairo_image_surface_get_width (surface);
+ height = cairo_image_surface_get_height (surface);
+ gtk_widget_set_size_request (widget, width, height);
+ }
+
+ return widget;
+}
diff --git a/gmathselector/gtkcairoimage.h b/gmathselector/gtkcairoimage.h
new file mode 100644
index 0000000..7ca6098
--- /dev/null
+++ b/gmathselector/gtkcairoimage.h
@@ -0,0 +1,71 @@
+/*
+ * GtkCairoImage, simple widget to display a Cairo Surface
+ *
+ * # Widget header
+ *
+ * Author : Guillaume Seguin
+ * Email : guillaume@segu.in
+ *
+ * Copyright (c) 2007 Guillaume Seguin <guillaume@segu.in>
+ *
+ * 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.
+ */
+
+#ifndef _GTKCAIROIMAGE_H
+#define _GTKCAIROIMAGE_H
+
+#include <gtk/gtk.h>
+#include <cairo.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_CAIRO_IMAGE (gtk_cairo_image_get_type ())
+#define GTK_CAIRO_IMAGE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+ GTK_TYPE_CAIRO_IMAGE, GtkCairoImage))
+#define GTK_CAIRO_IMAGE_CLASS(obj) (G_TYPE_CHECK_CLASS_CAST ((obj), \
+ GTK_CAIRO_IMAGE, GtkCairoImageClass))
+#define IS_GTK_CAIRO_IMAGE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+ GTK_TYPE_CAIRO_IMAGE))
+#define IS_GTK_CAIRO_IMAGEW_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE ((obj), \
+ GTK_TYPE_CAIRO_IMAGE))
+#define GTK_CAIRO_IMAGE_GET_CLASS (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+ GTK_TYPE_CAIRO_IMAGE, \
+ GtkCairoImageClass))
+
+typedef struct _GtkCairoImage GtkCairoImage;
+typedef struct _GtkCairoImageClass GtkCairoImageClass;
+
+struct _GtkCairoImage
+{
+ GtkWidget parent;
+
+ cairo_surface_t *surface;
+};
+
+struct _GtkCairoImageClass
+{
+ GtkWidgetClass parent_class;
+};
+
+GType
+gtk_cairo_image_get_type (void) G_GNUC_CONST;
+
+GtkWidget*
+gtk_cairo_image_new (cairo_surface_t* surface);
+
+G_END_DECLS
+
+#endif