summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Merry <alex.merry@cs.ox.ac.uk>2012-12-14 14:22:38 +0000
committerAlex Merry <alex.merry@cs.ox.ac.uk>2012-12-14 14:42:07 +0000
commitda7ca216f1d79ee1d79ad5d0ba270b09247a87b7 (patch)
tree2fd81182a13907d13a88e6c24b82052be67ac15f
parent7677929bc29056dbeda537c64acc62102fff7586 (diff)
Factor out the model for the node style selector
This allows us to use it in other widgets.
-rw-r--r--tikzit/src/Makefile.am1
-rw-r--r--tikzit/src/gtk/NodeStyleSelector.h22
-rw-r--r--tikzit/src/gtk/NodeStyleSelector.m350
-rw-r--r--tikzit/src/gtk/NodeStylesModel.h60
-rw-r--r--tikzit/src/gtk/NodeStylesModel.m360
-rw-r--r--tikzit/src/gtk/NodeStylesPalette.m6
6 files changed, 469 insertions, 330 deletions
diff --git a/tikzit/src/Makefile.am b/tikzit/src/Makefile.am
index 1d6f67a..1b8a7ca 100644
--- a/tikzit/src/Makefile.am
+++ b/tikzit/src/Makefile.am
@@ -47,6 +47,7 @@ tikzit_SOURCES = gtk/Application.m \
gtk/NodeStyle+Gtk.m \
gtk/NodeStyle+Storage.m \
gtk/NodeStyleEditor.m \
+ gtk/NodeStylesModel.m \
gtk/NodeStyleSelector.m \
gtk/NodeStylesPalette.m \
gtk/NSError+Glib.m \
diff --git a/tikzit/src/gtk/NodeStyleSelector.h b/tikzit/src/gtk/NodeStyleSelector.h
index 0c83dc0..a699dc8 100644
--- a/tikzit/src/gtk/NodeStyleSelector.h
+++ b/tikzit/src/gtk/NodeStyleSelector.h
@@ -17,12 +17,14 @@
#import "TZFoundation.h"
#import <gtk/gtk.h>
-#import "StyleManager.h"
+
+@class NodeStyle;
+@class NodeStylesModel;
+@class StyleManager;
@interface NodeStyleSelector: NSObject {
- GtkListStore *store;
+ NodeStylesModel *model;
GtkIconView *view;
- StyleManager *styleManager;
}
/*!
@@ -32,10 +34,10 @@
@property (readonly) GtkWidget *widget;
/*!
- @property manager
- @brief The StyleManager to use. Default is [StyleManager manager]
+ @property model
+ @brief The model to use.
*/
-@property (retain) StyleManager *styleManager;
+@property (retain) NodeStylesModel *model;
/*!
@property selectedStyle
@@ -46,13 +48,13 @@
@property (assign) NodeStyle *selectedStyle;
/*!
- * Initialise with the default style manager
+ * Initialise with a new model for the given style manager
*/
-- (id) init;
+- (id) initWithStyleManager:(StyleManager*)manager;
/*!
- * Initialise with the given style manager
+ * Initialise with the given model
*/
-- (id) initWithStyleManager:(StyleManager*)m;
+- (id) initWithModel:(NodeStylesModel*)model;
@end
diff --git a/tikzit/src/gtk/NodeStyleSelector.m b/tikzit/src/gtk/NodeStyleSelector.m
index d44f446..14cdc75 100644
--- a/tikzit/src/gtk/NodeStyleSelector.m
+++ b/tikzit/src/gtk/NodeStyleSelector.m
@@ -1,5 +1,5 @@
/*
- * Copyright 2011 Alex Merry <dev@randomguy3.me.uk>
+ * Copyright 2011-2012 Alex Merry <dev@randomguy3.me.uk>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@@ -17,92 +17,41 @@
#import "NodeStyleSelector.h"
-#import "CairoRenderContext.h"
-#import "Shape.h"
-#import "Shape+Render.h"
-#import "ShapeNames.h"
-#import "StyleManager.h"
-
-#import "gtkhelpers.h"
-
-#import <gdk-pixbuf/gdk-pixbuf.h>
+#import "NodeStylesModel.h"
// {{{ Internal interfaces
-// {{{ Signals
static void selection_changed_cb (GtkIconView *widget, NodeStyleSelector *mgr);
// }}}
-
-enum {
- STYLES_NAME_COL = 0,
- STYLES_ICON_COL,
- STYLES_PTR_COL,
- STYLES_N_COLS
-};
-
-@interface NodeStyleSelector (Notifications)
-- (void) stylesReplaced:(NSNotification*)notification;
-- (void) styleAdded:(NSNotification*)notification;
-- (void) styleRemoved:(NSNotification*)notification;
-- (void) shapeDictionaryReplaced:(NSNotification*)n;
-- (void) selectionChanged;
-- (void) observeValueForKeyPath:(NSString*)keyPath
- ofObject:(id)object
- change:(NSDictionary*)change
- context:(void*)context;
-@end
-
-@interface NodeStyleSelector (Private)
-- (cairo_surface_t*) createNodeIconSurface;
-- (GdkPixbuf*) pixbufOfNodeInStyle:(NodeStyle*)style;
-- (GdkPixbuf*) pixbufOfNodeInStyle:(NodeStyle*)style usingSurface:(cairo_surface_t*)surface;
-- (void) addStyle:(NodeStyle*)style;
-- (void) observeStyle:(NodeStyle*)style;
-- (void) stopObservingStyle:(NodeStyle*)style;
-- (void) clearModel;
-- (void) reloadStyles;
-@end
-
-// }}}
// {{{ API
@implementation NodeStyleSelector
- (id) init {
- self = [self initWithStyleManager:[StyleManager manager]];
- return self;
+ [self release];
+ return nil;
}
- (id) initWithStyleManager:(StyleManager*)m {
+ return [self initWithModel:[NodeStylesModel modelWithStyleManager:m]];
+}
+- (id) initWithModel:(NodeStylesModel*)m {
self = [super init];
if (self) {
- styleManager = nil;
-
- store = gtk_list_store_new (STYLES_N_COLS,
- G_TYPE_STRING,
- GDK_TYPE_PIXBUF,
- G_TYPE_POINTER);
- g_object_ref (store);
+ model = [m retain];
view = GTK_ICON_VIEW (gtk_icon_view_new ());
- g_object_ref (view);
+ g_object_ref_sink (view);
- gtk_icon_view_set_model (view, GTK_TREE_MODEL (store));
- gtk_icon_view_set_pixbuf_column (view, STYLES_ICON_COL);
- gtk_icon_view_set_tooltip_column (view, STYLES_NAME_COL);
+ gtk_icon_view_set_model (view, [model model]);
+ gtk_icon_view_set_pixbuf_column (view, NODE_STYLES_ICON_COL);
+ gtk_icon_view_set_tooltip_column (view, NODE_STYLES_NAME_COL);
gtk_icon_view_set_selection_mode (view, GTK_SELECTION_SINGLE);
g_signal_connect (G_OBJECT (view),
"selection-changed",
G_CALLBACK (selection_changed_cb),
self);
-
- [self setStyleManager:m];
-
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(shapeDictionaryReplaced:)
- name:@"ShapeDictionaryReplaced"
- object:[Shape class]];
}
return self;
@@ -110,42 +59,25 @@ enum {
- (void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
+
g_object_unref (view);
- [self clearModel];
- g_object_unref (store);
- [styleManager release];
+ [model release];
[super dealloc];
}
-- (StyleManager*) styleManager {
- return styleManager;
+- (NodeStylesModel*) model {
+ return model;
}
-- (void) setStyleManager:(StyleManager*)m {
- if (m == nil) {
- [NSException raise:NSInvalidArgumentException format:@"Style manager cannot be nil"];
- }
- [m retain];
-
- [[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:styleManager];
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(stylesReplaced:)
- name:@"NodeStylesReplaced"
- object:m];
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(styleAdded:)
- name:@"NodeStyleAdded"
- object:m];
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(styleRemoved:)
- name:@"NodeStyleRemoved"
- object:m];
-
- [styleManager release];
- styleManager = m;
+- (void) setModel:(NodeStylesModel*)m {
+ if (m == model)
+ return;
- [self reloadStyles];
+ NodeStylesModel *oldModel = model;
+ model = [m retain];
+ gtk_icon_view_set_model (view, [model model]);
+ [oldModel release];
}
- (GtkWidget*) widget {
@@ -162,10 +94,7 @@ enum {
}
GtkTreePath *path = (GtkTreePath*) list->data;
- GtkTreeIter iter;
- gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &iter, path);
- NodeStyle *style = nil;
- gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, STYLES_PTR_COL, &style, -1);
+ NodeStyle *style = [model styleFromPath:path];
g_list_foreach (list, (GFunc)gtk_tree_path_free, NULL);
g_list_free (list);
@@ -179,239 +108,26 @@ enum {
return;
}
- GtkTreeModel *m = GTK_TREE_MODEL (store);
- GtkTreeIter row;
- if (gtk_tree_model_get_iter_first (m, &row)) {
- do {
- NodeStyle *rowStyle;
- gtk_tree_model_get (m, &row, STYLES_PTR_COL, &rowStyle, -1);
- if (style == rowStyle) {
- gtk_icon_view_unselect_all (view);
- GtkTreePath *path = gtk_tree_model_get_path (m, &row);
- gtk_icon_view_select_path (view, path);
- gtk_tree_path_free (path);
- // styleManager.activeStyle will be updated by the GTK+ callback
- return;
- }
- } while (gtk_tree_model_iter_next (m, &row));
+ GtkTreePath *path = [model pathFromStyle:style];
+ if (path) {
+ gtk_icon_view_unselect_all (view);
+ gtk_icon_view_select_path (view, path);
+ gtk_tree_path_free (path);
}
}
@end
// }}}
-// {{{ Notifications
-
-@implementation NodeStyleSelector (Notifications)
-
-- (void) stylesReplaced:(NSNotification*)notification {
- [self reloadStyles];
-}
-
-- (void) styleAdded:(NSNotification*)notification {
- [self addStyle:[[notification userInfo] objectForKey:@"style"]];
-}
-
-- (void) styleRemoved:(NSNotification*)notification {
- NodeStyle *style = [[notification userInfo] objectForKey:@"style"];
-
- GtkTreeModel *model = GTK_TREE_MODEL (store);
- GtkTreeIter row;
- if (gtk_tree_model_get_iter_first (model, &row)) {
- do {
- NodeStyle *rowStyle;
- gtk_tree_model_get (model, &row, STYLES_PTR_COL, &rowStyle, -1);
- if (style == rowStyle) {
- gtk_list_store_remove (store, &row);
- [self stopObservingStyle:rowStyle];
- [rowStyle release];
- return;
- }
- } while (gtk_tree_model_iter_next (model, &row));
- }
-}
-
-- (void) observeValueForKeyPath:(NSString*)keyPath
- ofObject:(id)object
- change:(NSDictionary*)change
- context:(void*)context
-{
- if ([object class] != [NodeStyle class])
- return;
-
- NodeStyle *style = object;
-
- GtkTreeModel *model = GTK_TREE_MODEL (store);
- GtkTreeIter row;
- if (gtk_tree_model_get_iter_first (model, &row)) {
- do {
- NodeStyle *rowStyle;
- gtk_tree_model_get (model, &row, STYLES_PTR_COL, &rowStyle, -1);
- if (style == rowStyle) {
- if ([@"name" isEqual:keyPath]) {
- gtk_list_store_set (store, &row, STYLES_NAME_COL, [[style name] UTF8String], -1);
- } else {
- GdkPixbuf *pixbuf = [self pixbufOfNodeInStyle:style];
- gtk_list_store_set (store, &row, STYLES_ICON_COL, pixbuf, -1);
- g_object_unref (pixbuf);
- }
- }
- } while (gtk_tree_model_iter_next (model, &row));
- }
-}
+// {{{ GTK+ callbacks
-- (void) shapeDictionaryReplaced:(NSNotification*)n {
- [self reloadStyles];
-}
+static void selection_changed_cb (GtkIconView *view, NodeStyleSelector *mgr) {
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-- (void) selectionChanged {
[[NSNotificationCenter defaultCenter]
postNotificationName:@"SelectedStyleChanged"
- object:self];
-}
-@end
-
-// }}}
-// {{{ Private
-
-@implementation NodeStyleSelector (Private)
-- (cairo_surface_t*) createNodeIconSurface {
- return cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 24, 24);
-}
-
-- (GdkPixbuf*) pixbufOfNodeInStyle:(NodeStyle*)style {
- cairo_surface_t *surface = [self createNodeIconSurface];
- GdkPixbuf *pixbuf = [self pixbufOfNodeInStyle:style usingSurface:surface];
- cairo_surface_destroy (surface);
- return pixbuf;
-}
-
-- (GdkPixbuf*) pixbufOfNodeInStyle:(NodeStyle*)style usingSurface:(cairo_surface_t*)surface {
- Shape *shape = [Shape shapeForName:[style shapeName]];
-
- int width = cairo_image_surface_get_width (surface);
- int height = cairo_image_surface_get_height (surface);
- NSRect pixbufBounds = NSMakeRect(0.0, 0.0, width, height);
- const CGFloat lineWidth = [style strokeThickness];
- Transformer *shapeTrans = [Transformer transformerToFit:[shape boundingRect]
- intoScreenRect:NSInsetRect(pixbufBounds, lineWidth, lineWidth)
- flippedAboutXAxis:YES];
- if ([style scale] < 1.0)
- [shapeTrans setScale:[style scale] * [shapeTrans scale]];
-
- CairoRenderContext *context = [[CairoRenderContext alloc] initForSurface:surface];
- [context clearSurface];
- [shape drawPathWithTransform:shapeTrans andContext:context];
- [context setLineWidth:lineWidth];
- [context strokePathWithColor:[[style strokeColorRGB] rColor]
- andFillWithColor:[[style fillColorRGB] rColor]];
- [context release];
-
- return pixbuf_get_from_surface (surface);
-}
-
-- (void) addStyle:(NodeStyle*)style usingSurface:(cairo_surface_t*)surface {
- GtkTreeIter iter;
- gtk_list_store_append (store, &iter);
-
- GdkPixbuf *pixbuf = [self pixbufOfNodeInStyle:style usingSurface:surface];
- gtk_list_store_set (store, &iter,
- STYLES_NAME_COL, [[style name] UTF8String],
- STYLES_ICON_COL, pixbuf,
- STYLES_PTR_COL, (gpointer)[style retain],
- -1);
- g_object_unref (pixbuf);
- [self observeStyle:style];
-}
+ object:mgr];
-- (void) addStyle:(NodeStyle*)style {
- cairo_surface_t *surface = [self createNodeIconSurface];
- [self addStyle:style usingSurface:surface];
- cairo_surface_destroy (surface);
-}
-
-- (void) observeStyle:(NodeStyle*)style {
- [style addObserver:self
- forKeyPath:@"name"
- options:NSKeyValueObservingOptionNew
- context:NULL];
- [style addObserver:self
- forKeyPath:@"strokeThickness"
- options:0
- context:NULL];
- [style addObserver:self
- forKeyPath:@"strokeColorRGB.red"
- options:0
- context:NULL];
- [style addObserver:self
- forKeyPath:@"strokeColorRGB.green"
- options:0
- context:NULL];
- [style addObserver:self
- forKeyPath:@"strokeColorRGB.blue"
- options:0
- context:NULL];
- [style addObserver:self
- forKeyPath:@"fillColorRGB.red"
- options:0
- context:NULL];
- [style addObserver:self
- forKeyPath:@"fillColorRGB.green"
- options:0
- context:NULL];
- [style addObserver:self
- forKeyPath:@"fillColorRGB.blue"
- options:0
- context:NULL];
- [style addObserver:self
- forKeyPath:@"shapeName"
- options:0
- context:NULL];
-}
-
-- (void) stopObservingStyle:(NodeStyle*)style {
- [style removeObserver:self forKeyPath:@"name"];
- [style removeObserver:self forKeyPath:@"strokeThickness"];
- [style removeObserver:self forKeyPath:@"strokeColorRGB.red"];
- [style removeObserver:self forKeyPath:@"strokeColorRGB.green"];
- [style removeObserver:self forKeyPath:@"strokeColorRGB.blue"];
- [style removeObserver:self forKeyPath:@"fillColorRGB.red"];
- [style removeObserver:self forKeyPath:@"fillColorRGB.green"];
- [style removeObserver:self forKeyPath:@"fillColorRGB.blue"];
- [style removeObserver:self forKeyPath:@"shapeName"];
-}
-
-- (void) clearModel {
- [self setSelectedStyle:nil];
- GtkTreeModel *model = GTK_TREE_MODEL (store);
- GtkTreeIter row;
- if (gtk_tree_model_get_iter_first (model, &row)) {
- do {
- NodeStyle *rowStyle;
- gtk_tree_model_get (model, &row, STYLES_PTR_COL, &rowStyle, -1);
- [self stopObservingStyle:rowStyle];
- [rowStyle release];
- } while (gtk_tree_model_iter_next (model, &row));
- }
- gtk_list_store_clear (store);
-}
-
-- (void) reloadStyles {
- [self clearModel];
- cairo_surface_t *surface = [self createNodeIconSurface];
- for (NodeStyle *style in [styleManager nodeStyles]) {
- [self addStyle:style usingSurface:surface];
- }
- cairo_surface_destroy (surface);
-}
-@end
-
-// }}}
-// {{{ GTK+ callbacks
-
-static void selection_changed_cb (GtkIconView *view, NodeStyleSelector *mgr) {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- [mgr selectionChanged];
[pool drain];
}
// }}}
diff --git a/tikzit/src/gtk/NodeStylesModel.h b/tikzit/src/gtk/NodeStylesModel.h
new file mode 100644
index 0000000..1a7bf02
--- /dev/null
+++ b/tikzit/src/gtk/NodeStylesModel.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2011-2012 Alex Merry <dev@randomguy3.me.uk>
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#import "TZFoundation.h"
+#import <gtk/gtk.h>
+
+@class NodeStyle;
+@class StyleManager;
+
+enum {
+ NODE_STYLES_NAME_COL = 0,
+ NODE_STYLES_ICON_COL,
+ NODE_STYLES_PTR_COL,
+ NODE_STYLES_N_COLS
+};
+
+@interface NodeStylesModel: NSObject {
+ GtkListStore *store;
+ StyleManager *styleManager;
+}
+
+/*!
+ @property model
+ @brief The GTK+ tree model
+ */
+@property (readonly) GtkTreeModel *model;
+
+/*!
+ @property manager
+ @brief The StyleManager to use.
+ */
+@property (retain) StyleManager *styleManager;
+
+/*!
+ * Initialise with the given style manager
+ */
+- (id) initWithStyleManager:(StyleManager*)m;
+
++ (id) modelWithStyleManager:(StyleManager*)m;
+
+- (NodeStyle*) styleFromPath:(GtkTreePath*)path;
+- (GtkTreePath*) pathFromStyle:(NodeStyle*)style;
+
+@end
+
+// vim:ft=objc:ts=8:et:sts=4:sw=4
diff --git a/tikzit/src/gtk/NodeStylesModel.m b/tikzit/src/gtk/NodeStylesModel.m
new file mode 100644
index 0000000..68242e5
--- /dev/null
+++ b/tikzit/src/gtk/NodeStylesModel.m
@@ -0,0 +1,360 @@
+/*
+ * Copyright 2011-2012 Alex Merry <dev@randomguy3.me.uk>
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#import "NodeStylesModel.h"
+
+#import "CairoRenderContext.h"
+#import "NodeStyle.h"
+#import "Shape.h"
+#import "Shape+Render.h"
+#import "ShapeNames.h"
+#import "StyleManager.h"
+
+#import "gtkhelpers.h"
+
+#import <gdk-pixbuf/gdk-pixbuf.h>
+
+// {{{ Internal interfaces
+
+@interface NodeStylesModel (Notifications)
+- (void) nodeStylesReplaced:(NSNotification*)notification;
+- (void) nodeStyleAdded:(NSNotification*)notification;
+- (void) nodeStyleRemoved:(NSNotification*)notification;
+- (void) shapeDictionaryReplaced:(NSNotification*)n;
+- (void) observeValueForKeyPath:(NSString*)keyPath
+ ofObject:(id)object
+ change:(NSDictionary*)change
+ context:(void*)context;
+@end
+
+@interface NodeStylesModel (Private)
+- (cairo_surface_t*) createNodeIconSurface;
+- (GdkPixbuf*) pixbufOfNodeInStyle:(NodeStyle*)style;
+- (GdkPixbuf*) pixbufOfNodeInStyle:(NodeStyle*)style usingSurface:(cairo_surface_t*)surface;
+- (void) addNodeStyle:(NodeStyle*)style;
+- (void) addNodeStyle:(NodeStyle*)style usingSurface:(cairo_surface_t*)surface;
+- (void) observeNodeStyle:(NodeStyle*)style;
+- (void) stopObservingNodeStyle:(NodeStyle*)style;
+- (void) clearNodeStylesModel;
+- (void) reloadNodeStyles;
+@end
+
+// }}}
+// {{{ API
+
+@implementation NodeStylesModel
+
++ (id) modelWithStyleManager:(StyleManager*)m {
+ return [[[self alloc] initWithStyleManager:m] autorelease];
+}
+
+- (id) init {
+ [self release];
+ return nil;
+}
+
+- (id) initWithStyleManager:(StyleManager*)m {
+ self = [super init];
+
+ if (self) {
+ store = gtk_list_store_new (NODE_STYLES_N_COLS,
+ G_TYPE_STRING,
+ GDK_TYPE_PIXBUF,
+ G_TYPE_POINTER);
+ g_object_ref_sink (store);
+
+ [self setStyleManager:m];
+
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(shapeDictionaryReplaced:)
+ name:@"ShapeDictionaryReplaced"
+ object:[Shape class]];
+ }
+
+ return self;
+}
+
+- (void) dealloc {
+ [[NSNotificationCenter defaultCenter] removeObserver:self];
+
+ [self clearNodeStylesModel];
+ g_object_unref (store);
+ [styleManager release];
+
+ [super dealloc];
+}
+
+- (StyleManager*) styleManager {
+ return styleManager;
+}
+
+- (void) setStyleManager:(StyleManager*)m {
+ if (m == nil) {
+ [NSException raise:NSInvalidArgumentException format:@"Style manager cannot be nil"];
+ }
+ [m retain];
+
+ [[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:styleManager];
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(nodeStylesReplaced:)
+ name:@"NodeStylesReplaced"
+ object:m];
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(nodeStyleAdded:)
+ name:@"NodeStyleAdded"
+ object:m];
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(nodeStyleRemoved:)
+ name:@"NodeStyleRemoved"
+ object:m];
+
+ [styleManager release];
+ styleManager = m;
+
+ [self reloadNodeStyles];
+}
+
+- (GtkTreeModel*) model {
+ return GTK_TREE_MODEL (store);
+}
+
+- (NodeStyle*) styleFromPath:(GtkTreePath*)path {
+ GtkTreeIter iter;
+ gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &iter, path);
+ NodeStyle *style = nil;
+ gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, NODE_STYLES_PTR_COL, &style, -1);
+ return style;
+}
+
+- (GtkTreePath*) pathFromStyle:(NodeStyle*)style {
+ GtkTreeModel *m = GTK_TREE_MODEL (store);
+ GtkTreeIter row;
+ if (gtk_tree_model_get_iter_first (m, &row)) {
+ do {
+ NodeStyle *rowStyle;
+ gtk_tree_model_get (m, &row, NODE_STYLES_PTR_COL, &rowStyle, -1);
+ if (style == rowStyle) {
+ return gtk_tree_model_get_path (m, &row);
+ }
+ } while (gtk_tree_model_iter_next (m, &row));
+ }
+ return NULL;
+}
+@end
+
+// }}}
+// {{{ Notifications
+
+@implementation NodeStylesModel (Notifications)
+
+- (void) nodeStylesReplaced:(NSNotification*)notification {
+ [self reloadNodeStyles];
+}
+
+- (void) nodeStyleAdded:(NSNotification*)notification {
+ [self addNodeStyle:[[notification userInfo] objectForKey:@"style"]];
+}
+
+- (void) nodeStyleRemoved:(NSNotification*)notification {
+ NodeStyle *style = [[notification userInfo] objectForKey:@"style"];
+
+ GtkTreeModel *model = GTK_TREE_MODEL (store);
+ GtkTreeIter row;
+ if (gtk_tree_model_get_iter_first (model, &row)) {
+ do {
+ NodeStyle *rowStyle;
+ gtk_tree_model_get (model, &row, NODE_STYLES_PTR_COL, &rowStyle, -1);
+ if (style == rowStyle) {
+ gtk_list_store_remove (store, &row);
+ [self stopObservingNodeStyle:rowStyle];
+ [rowStyle release];
+ return;
+ }
+ } while (gtk_tree_model_iter_next (model, &row));
+ }
+}
+
+- (void) observeValueForKeyPath:(NSString*)keyPath
+ ofObject:(id)object
+ change:(NSDictionary*)change
+ context:(void*)context
+{
+ if ([object class] == [NodeStyle class]) {
+ NodeStyle *style = object;
+
+ GtkTreeModel *model = GTK_TREE_MODEL (store);
+ GtkTreeIter row;
+ if (gtk_tree_model_get_iter_first (model, &row)) {
+ do {
+ NodeStyle *rowStyle;
+ gtk_tree_model_get (model, &row, NODE_STYLES_PTR_COL, &rowStyle, -1);
+ if (style == rowStyle) {
+ if ([@"name" isEqual:keyPath]) {
+ gtk_list_store_set (store, &row, NODE_STYLES_NAME_COL, [[style name] UTF8String], -1);
+ } else {
+ GdkPixbuf *pixbuf = [self pixbufOfNodeInStyle:style];
+ gtk_list_store_set (store, &row, NODE_STYLES_ICON_COL, pixbuf, -1);
+ g_object_unref (pixbuf);
+ }
+ }
+ } while (gtk_tree_model_iter_next (model, &row));
+ }
+ }
+}
+
+- (void) shapeDictionaryReplaced:(NSNotification*)n {
+ [self reloadNodeStyles];
+}
+@end
+
+// }}}
+// {{{ Private
+
+@implementation NodeStylesModel (Private)
+- (cairo_surface_t*) createNodeIconSurface {
+ return cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 24, 24);
+}
+
+- (GdkPixbuf*) pixbufOfNodeInStyle:(NodeStyle*)style {
+ cairo_surface_t *surface = [self createNodeIconSurface];
+ GdkPixbuf *pixbuf = [self pixbufOfNodeInStyle:style usingSurface:surface];
+ cairo_surface_destroy (surface);
+ return pixbuf;
+}
+
+- (GdkPixbuf*) pixbufOfNodeInStyle:(NodeStyle*)style usingSurface:(cairo_surface_t*)surface {
+ Shape *shape = [Shape shapeForName:[style shapeName]];
+
+ int width = cairo_image_surface_get_width (surface);
+ int height = cairo_image_surface_get_height (surface);
+ NSRect pixbufBounds = NSMakeRect(0.0, 0.0, width, height);
+ const CGFloat lineWidth = [style strokeThickness];
+ Transformer *shapeTrans = [Transformer transformerToFit:[shape boundingRect]
+ intoScreenRect:NSInsetRect(pixbufBounds, lineWidth, lineWidth)
+ flippedAboutXAxis:YES];
+ if ([style scale] < 1.0)
+ [shapeTrans setScale:[style scale] * [shapeTrans scale]];
+
+ CairoRenderContext *context = [[CairoRenderContext alloc] initForSurface:surface];
+ [context clearSurface];
+ [shape drawPathWithTransform:shapeTrans andContext:context];
+ [context setLineWidth:lineWidth];
+ [context strokePathWithColor:[[style strokeColorRGB] rColor]
+ andFillWithColor:[[style fillColorRGB] rColor]];
+ [context release];
+
+ return pixbuf_get_from_surface (surface);
+}
+
+- (void) addNodeStyle:(NodeStyle*)style usingSurface:(cairo_surface_t*)surface {
+ GtkTreeIter iter;
+ gtk_list_store_append (store, &iter);
+
+ GdkPixbuf *pixbuf = [self pixbufOfNodeInStyle:style usingSurface:surface];
+ gtk_list_store_set (store, &iter,
+ NODE_STYLES_NAME_COL, [[style name] UTF8String],
+ NODE_STYLES_ICON_COL, pixbuf,
+ NODE_STYLES_PTR_COL, (gpointer)[style retain],
+ -1);
+ g_object_unref (pixbuf);
+ [self observeNodeStyle:style];
+}
+
+- (void) addNodeStyle:(NodeStyle*)style {
+ cairo_surface_t *surface = [self createNodeIconSurface];
+ [self addNodeStyle:style usingSurface:surface];
+ cairo_surface_destroy (surface);
+}
+
+- (void) observeNodeStyle:(NodeStyle*)style {
+ [style addObserver:self
+ forKeyPath:@"name"
+ options:NSKeyValueObservingOptionNew
+ context:NULL];
+ [style addObserver:self
+ forKeyPath:@"strokeThickness"
+ options:0
+ context:NULL];
+ [style addObserver:self
+ forKeyPath:@"strokeColorRGB.red"
+ options:0
+ context:NULL];
+ [style addObserver:self
+ forKeyPath:@"strokeColorRGB.green"
+ options:0
+ context:NULL];
+ [style addObserver:self
+ forKeyPath:@"strokeColorRGB.blue"
+ options:0
+ context:NULL];
+ [style addObserver:self
+ forKeyPath:@"fillColorRGB.red"
+ options:0
+ context:NULL];
+ [style addObserver:self
+ forKeyPath:@"fillColorRGB.green"
+ options:0
+ context:NULL];
+ [style addObserver:self
+ forKeyPath:@"fillColorRGB.blue"
+ options:0
+ context:NULL];
+ [style addObserver:self
+ forKeyPath:@"shapeName"
+ options:0
+ context:NULL];
+}
+
+- (void) stopObservingNodeStyle:(NodeStyle*)style {
+ [style removeObserver:self forKeyPath:@"name"];
+ [style removeObserver:self forKeyPath:@"strokeThickness"];
+ [style removeObserver:self forKeyPath:@"strokeColorRGB.red"];
+ [style removeObserver:self forKeyPath:@"strokeColorRGB.green"];
+ [style removeObserver:self forKeyPath:@"strokeColorRGB.blue"];
+ [style removeObserver:self forKeyPath:@"fillColorRGB.red"];
+ [style removeObserver:self forKeyPath:@"fillColorRGB.green"];
+ [style removeObserver:self forKeyPath:@"fillColorRGB.blue"];
+ [style removeObserver:self forKeyPath:@"shapeName"];
+}
+
+- (void) clearNodeStylesModel {
+ GtkTreeModel *model = GTK_TREE_MODEL (store);
+ GtkTreeIter row;
+ if (gtk_tree_model_get_iter_first (model, &row)) {
+ do {
+ NodeStyle *rowStyle;
+ gtk_tree_model_get (model, &row, NODE_STYLES_PTR_COL, &rowStyle, -1);
+ [self stopObservingNodeStyle:rowStyle];
+ [rowStyle release];
+ } while (gtk_tree_model_iter_next (model, &row));
+ }
+ gtk_list_store_clear (store);
+}
+
+- (void) reloadNodeStyles {
+ [self clearNodeStylesModel];
+ cairo_surface_t *surface = [self createNodeIconSurface];
+ for (NodeStyle *style in [styleManager nodeStyles]) {
+ [self addNodeStyle:style usingSurface:surface];
+ }
+ cairo_surface_destroy (surface);
+}
+@end
+
+// }}}
+
+// vim:ft=objc:ts=8:et:sts=4:sw=4:foldmethod=marker
diff --git a/tikzit/src/gtk/NodeStylesPalette.m b/tikzit/src/gtk/NodeStylesPalette.m
index 6964f4c..23ca7ea 100644
--- a/tikzit/src/gtk/NodeStylesPalette.m
+++ b/tikzit/src/gtk/NodeStylesPalette.m
@@ -142,11 +142,11 @@ static void clear_style_button_cb (GtkButton *widget, NodeStylesPalette *palette
}
- (StyleManager*) styleManager {
- return [selector styleManager];
+ return [[selector model] styleManager];
}
- (void) setStyleManager:(StyleManager*)m {
- [selector setStyleManager:m];
+ [[selector model] setStyleManager:m];
}
- (TikzDocument*) document {
@@ -217,7 +217,7 @@ static void clear_style_button_cb (GtkButton *widget, NodeStylesPalette *palette
- (void) removeSelectedStyle {
NodeStyle *style = [selector selectedStyle];
if (style)
- [[selector styleManager] removeNodeStyle:style];
+ [[[selector model] styleManager] removeNodeStyle:style];
}
- (void) applySelectedStyle {