From 2ab14c0fb8dbf8850423fbb2e38a24ea881a8e49 Mon Sep 17 00:00:00 2001 From: Alex Merry Date: Fri, 14 Dec 2012 15:03:33 +0000 Subject: Factor out the model for the edge style selector This allows us to use it in other widgets. --- tikzit/src/Makefile.am | 1 + tikzit/src/gtk/EdgeStyleSelector.h | 26 +-- tikzit/src/gtk/EdgeStyleSelector.m | 351 ++++--------------------------------- tikzit/src/gtk/EdgeStylesModel.h | 60 +++++++ tikzit/src/gtk/EdgeStylesModel.m | 346 ++++++++++++++++++++++++++++++++++++ tikzit/src/gtk/EdgeStylesPalette.m | 6 +- 6 files changed, 460 insertions(+), 330 deletions(-) create mode 100644 tikzit/src/gtk/EdgeStylesModel.h create mode 100644 tikzit/src/gtk/EdgeStylesModel.m diff --git a/tikzit/src/Makefile.am b/tikzit/src/Makefile.am index 1b8a7ca..276723d 100644 --- a/tikzit/src/Makefile.am +++ b/tikzit/src/Makefile.am @@ -37,6 +37,7 @@ tikzit_SOURCES = gtk/Application.m \ gtk/EdgeStyle+Storage.m \ gtk/EdgeStyleEditor.m \ gtk/EdgeStyleSelector.m \ + gtk/EdgeStylesModel.m \ gtk/EdgeStylesPalette.m \ gtk/FileChooserDialog.m \ gtk/HandTool.m \ diff --git a/tikzit/src/gtk/EdgeStyleSelector.h b/tikzit/src/gtk/EdgeStyleSelector.h index 1ae1f3c..904bd93 100644 --- a/tikzit/src/gtk/EdgeStyleSelector.h +++ b/tikzit/src/gtk/EdgeStyleSelector.h @@ -17,25 +17,27 @@ #import "TZFoundation.h" #import -#import "StyleManager.h" + +@class EdgeStyle; +@class EdgeStylesModel; +@class StyleManager; @interface EdgeStyleSelector: NSObject { - GtkListStore *store; + EdgeStylesModel *model; GtkTreeView *view; - StyleManager *styleManager; } /*! @property widget @brief The GTK widget */ -@property (readonly) GtkWidget *widget; +@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) EdgeStylesModel *model; /*! @property selectedStyle @@ -43,16 +45,16 @@ When this changes, a SelectedStyleChanged notification will be posted */ -@property (assign) EdgeStyle *selectedStyle; +@property (assign) EdgeStyle *selectedStyle; /*! - * Initialise with the default style manager + * Initialise with a new model for the given style manager */ -- (id) init; +- (id) initWithStyleManager:(StyleManager*)m; /*! - * Initialise with the given style manager + * Initialise with the given model */ -- (id) initWithStyleManager:(StyleManager*)m; +- (id) initWithModel:(EdgeStylesModel*)model; @end diff --git a/tikzit/src/gtk/EdgeStyleSelector.m b/tikzit/src/gtk/EdgeStyleSelector.m index 6257b95..6a9db33 100644 --- a/tikzit/src/gtk/EdgeStyleSelector.m +++ b/tikzit/src/gtk/EdgeStyleSelector.m @@ -17,89 +17,43 @@ #import "EdgeStyleSelector.h" -#import "CairoRenderContext.h" -#import "Edge.h" -#import "Edge+Render.h" -#import "Node.h" -#import "Shape.h" -#import "Shape+Render.h" -#import "ShapeNames.h" -#import "StyleManager.h" - -#import "gtkhelpers.h" - -#import +#import "EdgeStylesModel.h" // {{{ Internal interfaces -// {{{ Signals static void selection_changed_cb (GtkTreeSelection *sel, EdgeStyleSelector *mgr); -// }}} - -enum { - STYLES_NAME_COL = 0, - STYLES_ICON_COL, - STYLES_PTR_COL, - STYLES_N_COLS -}; - -@interface EdgeStyleSelector (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 EdgeStyleSelector (Private) -- (void) clearModel; -- (cairo_surface_t*) createEdgeIconSurface; -- (GdkPixbuf*) pixbufOfEdgeInStyle:(EdgeStyle*)style; -- (GdkPixbuf*) pixbufOfEdgeInStyle:(EdgeStyle*)style usingSurface:(cairo_surface_t*)surface; -- (void) addStyle:(EdgeStyle*)style; -- (void) observeStyle:(EdgeStyle*)style; -- (void) stopObservingStyle:(EdgeStyle*)style; -- (void) reloadStyles; -@end - // }}} // {{{ API @implementation EdgeStyleSelector - (id) init { - self = [self initWithStyleManager:[StyleManager manager]]; - return self; + [self release]; + return nil; } - (id) initWithStyleManager:(StyleManager*)m { + return [self initWithModel:[EdgeStylesModel modelWithStyleManager:m]]; +} +- (id) initWithModel:(EdgeStylesModel*)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_TREE_VIEW (gtk_tree_view_new_with_model (GTK_TREE_MODEL (store))); + view = GTK_TREE_VIEW (gtk_tree_view_new_with_model ([m model])); gtk_tree_view_set_headers_visible (view, FALSE); g_object_ref (view); GtkCellRenderer *renderer; GtkTreeViewColumn *column; renderer = gtk_cell_renderer_pixbuf_new (); - column = gtk_tree_view_column_new_with_attributes ("Preview", - renderer, - "pixbuf", STYLES_ICON_COL, - NULL); + column = gtk_tree_view_column_new_with_attributes ( + "Preview", + renderer, + "pixbuf", EDGE_STYLES_ICON_COL, + NULL); gtk_tree_view_append_column (view, column); - gtk_tree_view_set_tooltip_column (view, STYLES_NAME_COL); + gtk_tree_view_set_tooltip_column (view, EDGE_STYLES_NAME_COL); GtkTreeSelection *sel = gtk_tree_view_get_selection (view); gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE); @@ -108,13 +62,6 @@ enum { "changed", G_CALLBACK (selection_changed_cb), self); - - [self setStyleManager:m]; - - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(shapeDictionaryReplaced:) - name:@"ShapeDictionaryReplaced" - object:[Shape class]]; } return self; @@ -122,42 +69,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; +- (EdgeStylesModel*) 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:@"EdgeStylesReplaced" - object:m]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(styleAdded:) - name:@"EdgeStyleAdded" - object:m]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(styleRemoved:) - name:@"EdgeStyleRemoved" - object:m]; - - [styleManager release]; - styleManager = m; - - [self reloadStyles]; +- (void) setModel:(EdgeStylesModel*)m { + if (m == model) + return; + + EdgeStylesModel *oldModel = model; + model = [m retain]; + gtk_tree_view_set_model (view, [model model]); + [oldModel release]; } - (GtkWidget*) widget { @@ -173,7 +103,7 @@ enum { } EdgeStyle *style = nil; - gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, STYLES_PTR_COL, &style, -1); + gtk_tree_model_get ([model model], &iter, EDGE_STYLES_PTR_COL, &style, -1); return style; } @@ -186,234 +116,25 @@ enum { return; } - GtkTreeModel *m = GTK_TREE_MODEL (store); - GtkTreeIter row; - if (gtk_tree_model_get_iter_first (m, &row)) { - do { - EdgeStyle *rowStyle; - gtk_tree_model_get (m, &row, STYLES_PTR_COL, &rowStyle, -1); - if (style == rowStyle) { - gtk_tree_selection_unselect_all (sel); - GtkTreePath *path = gtk_tree_model_get_path (m, &row); - gtk_tree_selection_select_path (sel, 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_tree_selection_unselect_all (sel); + gtk_tree_selection_select_path (sel, path); + gtk_tree_path_free (path); } } - @end // }}} -// {{{ Notifications - -@implementation EdgeStyleSelector (Notifications) - -- (void) stylesReplaced:(NSNotification*)notification { - [self reloadStyles]; -} - -- (void) styleAdded:(NSNotification*)notification { - [self addStyle:[[notification userInfo] objectForKey:@"style"]]; -} - -- (void) styleRemoved:(NSNotification*)notification { - EdgeStyle *style = [[notification userInfo] objectForKey:@"style"]; - - GtkTreeModel *model = GTK_TREE_MODEL (store); - GtkTreeIter row; - if (gtk_tree_model_get_iter_first (model, &row)) { - do { - EdgeStyle *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] != [EdgeStyle class]) - return; - - EdgeStyle *style = object; - - GtkTreeModel *model = GTK_TREE_MODEL (store); - GtkTreeIter row; - if (gtk_tree_model_get_iter_first (model, &row)) { - do { - EdgeStyle *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 pixbufOfEdgeInStyle: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 (GtkTreeSelection *sel, EdgeStyleSelector *mgr) { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; -- (void) selectionChanged { [[NSNotificationCenter defaultCenter] postNotificationName:@"SelectedStyleChanged" - object:self]; -} -@end - -// }}} -// {{{ Private - -@implementation EdgeStyleSelector (Private) -- (void) clearModel { - [self setSelectedStyle:nil]; - GtkTreeModel *model = GTK_TREE_MODEL (store); - GtkTreeIter row; - if (gtk_tree_model_get_iter_first (model, &row)) { - do { - EdgeStyle *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); -} - -- (cairo_surface_t*) createEdgeIconSurface { - return cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 48, 18); -} + object:mgr]; -- (GdkPixbuf*) pixbufOfEdgeInStyle:(EdgeStyle*)style { - cairo_surface_t *surface = [self createEdgeIconSurface]; - GdkPixbuf *pixbuf = [self pixbufOfEdgeInStyle:style usingSurface:surface]; - cairo_surface_destroy (surface); - return pixbuf; -} - -- (GdkPixbuf*) pixbufOfEdgeInStyle:(EdgeStyle*)style usingSurface:(cairo_surface_t*)surface { - Transformer *transformer = [Transformer defaultTransformer]; - [transformer setFlippedAboutXAxis:YES]; - - 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); - NSRect graphBounds = [transformer rectFromScreen:pixbufBounds]; - - NSPoint start = NSMakePoint (NSMinX (graphBounds) + 0.1f, NSMidY (graphBounds)); - NSPoint end = NSMakePoint (NSMaxX (graphBounds) - 0.1f, NSMidY (graphBounds)); - Node *src = [Node nodeWithPoint:start]; - Node *tgt = [Node nodeWithPoint:end]; - Edge *e = [Edge edgeWithSource:src andTarget:tgt]; - [e setStyle:style]; - - CairoRenderContext *context = [[CairoRenderContext alloc] initForSurface:surface]; - [context clearSurface]; - [e renderBasicEdgeInContext:context withTransformer:transformer selected:NO]; - [context release]; - - return pixbuf_get_from_surface (surface); -} - -- (void) addStyle:(EdgeStyle*)style usingSurface:(cairo_surface_t*)surface { - GtkTreeIter iter; - gtk_list_store_append (store, &iter); - - GdkPixbuf *pixbuf = [self pixbufOfEdgeInStyle: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]; -} - -- (void) addStyle:(EdgeStyle*)style { - cairo_surface_t *surface = [self createEdgeIconSurface]; - [self addStyle:style usingSurface:surface]; - cairo_surface_destroy (surface); -} - -- (void) observeStyle:(EdgeStyle*)style { - [style addObserver:self - forKeyPath:@"name" - options:NSKeyValueObservingOptionNew - context:NULL]; - [style addObserver:self - forKeyPath:@"thickness" - options:0 - context:NULL]; - [style addObserver:self - forKeyPath:@"headStyle" - options:0 - context:NULL]; - [style addObserver:self - forKeyPath:@"tailStyle" - options:0 - context:NULL]; - [style addObserver:self - forKeyPath:@"decorationStyle" - options:0 - context:NULL]; - [style addObserver:self - forKeyPath:@"colorRGB.red" - options:0 - context:NULL]; - [style addObserver:self - forKeyPath:@"colorRGB.green" - options:0 - context:NULL]; - [style addObserver:self - forKeyPath:@"colorRGB.blue" - options:0 - context:NULL]; -} - -- (void) stopObservingStyle:(EdgeStyle*)style { - [style removeObserver:self forKeyPath:@"name"]; - [style removeObserver:self forKeyPath:@"thickness"]; - [style removeObserver:self forKeyPath:@"headStyle"]; - [style removeObserver:self forKeyPath:@"tailStyle"]; - [style removeObserver:self forKeyPath:@"decorationStyle"]; - [style removeObserver:self forKeyPath:@"colorRGB.red"]; - [style removeObserver:self forKeyPath:@"colorRGB.green"]; - [style removeObserver:self forKeyPath:@"colorRGB.blue"]; -} - -- (void) reloadStyles { - [self clearModel]; - cairo_surface_t *surface = [self createEdgeIconSurface]; - for (EdgeStyle *style in [styleManager edgeStyles]) { - [self addStyle:style usingSurface:surface]; - } - cairo_surface_destroy (surface); -} -@end - -// }}} -// {{{ GTK+ callbacks - -static void selection_changed_cb (GtkTreeSelection *sel, EdgeStyleSelector *mgr) { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - [mgr selectionChanged]; [pool drain]; } // }}} diff --git a/tikzit/src/gtk/EdgeStylesModel.h b/tikzit/src/gtk/EdgeStylesModel.h new file mode 100644 index 0000000..b22ba8b --- /dev/null +++ b/tikzit/src/gtk/EdgeStylesModel.h @@ -0,0 +1,60 @@ +/* + * Copyright 2011-2012 Alex Merry + * + * 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 . + */ + +#import "TZFoundation.h" +#import + +@class EdgeStyle; +@class StyleManager; + +enum { + EDGE_STYLES_NAME_COL = 0, + EDGE_STYLES_ICON_COL, + EDGE_STYLES_PTR_COL, + EDGE_STYLES_N_COLS +}; + +@interface EdgeStylesModel: 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; + +- (EdgeStyle*) styleFromPath:(GtkTreePath*)path; +- (GtkTreePath*) pathFromStyle:(EdgeStyle*)style; + +@end + +// vim:ft=objc:ts=8:et:sts=4:sw=4 diff --git a/tikzit/src/gtk/EdgeStylesModel.m b/tikzit/src/gtk/EdgeStylesModel.m new file mode 100644 index 0000000..835aa35 --- /dev/null +++ b/tikzit/src/gtk/EdgeStylesModel.m @@ -0,0 +1,346 @@ +/* + * Copyright 2012 Alex Merry + * + * 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 . + */ + +#import "EdgeStylesModel.h" + +#import "CairoRenderContext.h" +#import "Edge.h" +#import "Edge+Render.h" +#import "EdgeStyle.h" +#import "Node.h" +#import "StyleManager.h" + +#import "gtkhelpers.h" + +#import + +// {{{ Internal interfaces + +@interface EdgeStylesModel (Notifications) +- (void) edgeStylesReplaced:(NSNotification*)notification; +- (void) edgeStyleAdded:(NSNotification*)notification; +- (void) edgeStyleRemoved:(NSNotification*)notification; +- (void) observeValueForKeyPath:(NSString*)keyPath + ofObject:(id)object + change:(NSDictionary*)change + context:(void*)context; +@end + +@interface EdgeStylesModel (Private) +- (cairo_surface_t*) createEdgeIconSurface; +- (GdkPixbuf*) pixbufOfEdgeInStyle:(EdgeStyle*)style; +- (GdkPixbuf*) pixbufOfEdgeInStyle:(EdgeStyle*)style usingSurface:(cairo_surface_t*)surface; +- (void) addEdgeStyle:(EdgeStyle*)style; +- (void) addEdgeStyle:(EdgeStyle*)style usingSurface:(cairo_surface_t*)surface; +- (void) observeEdgeStyle:(EdgeStyle*)style; +- (void) stopObservingEdgeStyle:(EdgeStyle*)style; +- (void) clearEdgeStylesModel; +- (void) reloadEdgeStyles; +@end + +// }}} +// {{{ API + +@implementation EdgeStylesModel + ++ (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 (EDGE_STYLES_N_COLS, + G_TYPE_STRING, + GDK_TYPE_PIXBUF, + G_TYPE_POINTER); + g_object_ref_sink (store); + + [self setStyleManager:m]; + } + + return self; +} + +- (void) dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; + + [self clearEdgeStylesModel]; + g_object_unref (store); + [styleManager release]; + + [super dealloc]; +} + +- (GtkTreeModel*) model { + return GTK_TREE_MODEL (store); +} + +- (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(edgeStylesReplaced:) + name:@"EdgeStylesReplaced" + object:m]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(edgeStyleAdded:) + name:@"EdgeStyleAdded" + object:m]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(edgeStyleRemoved:) + name:@"EdgeStyleRemoved" + object:m]; + + [styleManager release]; + styleManager = m; + + [self reloadEdgeStyles]; +} + +- (EdgeStyle*) styleFromPath:(GtkTreePath*)path { + GtkTreeIter iter; + gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &iter, path); + EdgeStyle *style = nil; + gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, EDGE_STYLES_PTR_COL, &style, -1); + return style; +} + +- (GtkTreePath*) pathFromStyle:(EdgeStyle*)style { + GtkTreeModel *m = GTK_TREE_MODEL (store); + GtkTreeIter row; + if (gtk_tree_model_get_iter_first (m, &row)) { + do { + EdgeStyle *rowStyle; + gtk_tree_model_get (m, &row, EDGE_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 EdgeStylesModel (Notifications) + +- (void) edgeStylesReplaced:(NSNotification*)notification { + [self reloadEdgeStyles]; +} + +- (void) edgeStyleAdded:(NSNotification*)notification { + [self addEdgeStyle:[[notification userInfo] objectForKey:@"style"]]; +} + +- (void) edgeStyleRemoved:(NSNotification*)notification { + EdgeStyle *style = [[notification userInfo] objectForKey:@"style"]; + + GtkTreeModel *model = GTK_TREE_MODEL (store); + GtkTreeIter row; + if (gtk_tree_model_get_iter_first (model, &row)) { + do { + EdgeStyle *rowStyle; + gtk_tree_model_get (model, &row, EDGE_STYLES_PTR_COL, &rowStyle, -1); + if (style == rowStyle) { + gtk_list_store_remove (store, &row); + [self stopObservingEdgeStyle: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] != [EdgeStyle class]) + return; + + EdgeStyle *style = object; + + GtkTreeModel *model = GTK_TREE_MODEL (store); + GtkTreeIter row; + if (gtk_tree_model_get_iter_first (model, &row)) { + do { + EdgeStyle *rowStyle; + gtk_tree_model_get (model, &row, EDGE_STYLES_PTR_COL, &rowStyle, -1); + if (style == rowStyle) { + if ([@"name" isEqual:keyPath]) { + gtk_list_store_set (store, &row, EDGE_STYLES_NAME_COL, [[style name] UTF8String], -1); + } else { + GdkPixbuf *pixbuf = [self pixbufOfEdgeInStyle:style]; + gtk_list_store_set (store, &row, EDGE_STYLES_ICON_COL, pixbuf, -1); + g_object_unref (pixbuf); + } + } + } while (gtk_tree_model_iter_next (model, &row)); + } +} +@end + +// }}} +// {{{ Private + +@implementation EdgeStylesModel (Private) +- (cairo_surface_t*) createEdgeIconSurface { + return cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 48, 18); +} + +- (GdkPixbuf*) pixbufOfEdgeInStyle:(EdgeStyle*)style { + cairo_surface_t *surface = [self createEdgeIconSurface]; + GdkPixbuf *pixbuf = [self pixbufOfEdgeInStyle:style usingSurface:surface]; + cairo_surface_destroy (surface); + return pixbuf; +} + +- (GdkPixbuf*) pixbufOfEdgeInStyle:(EdgeStyle*)style usingSurface:(cairo_surface_t*)surface { + Transformer *transformer = [Transformer defaultTransformer]; + [transformer setFlippedAboutXAxis:YES]; + + 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); + NSRect graphBounds = [transformer rectFromScreen:pixbufBounds]; + + NSPoint start = NSMakePoint (NSMinX (graphBounds) + 0.1f, NSMidY (graphBounds)); + NSPoint end = NSMakePoint (NSMaxX (graphBounds) - 0.1f, NSMidY (graphBounds)); + Node *src = [Node nodeWithPoint:start]; + Node *tgt = [Node nodeWithPoint:end]; + Edge *e = [Edge edgeWithSource:src andTarget:tgt]; + [e setStyle:style]; + + CairoRenderContext *context = [[CairoRenderContext alloc] initForSurface:surface]; + [context clearSurface]; + [e renderBasicEdgeInContext:context withTransformer:transformer selected:NO]; + [context release]; + + return pixbuf_get_from_surface (surface); +} + +- (void) addEdgeStyle:(EdgeStyle*)style usingSurface:(cairo_surface_t*)surface { + GtkTreeIter iter; + gtk_list_store_append (store, &iter); + + GdkPixbuf *pixbuf = [self pixbufOfEdgeInStyle:style usingSurface:surface]; + gtk_list_store_set (store, &iter, + EDGE_STYLES_NAME_COL, [[style name] UTF8String], + EDGE_STYLES_ICON_COL, pixbuf, + EDGE_STYLES_PTR_COL, (gpointer)[style retain], + -1); + g_object_unref (pixbuf); + [self observeEdgeStyle:style]; +} + +- (void) addEdgeStyle:(EdgeStyle*)style { + cairo_surface_t *surface = [self createEdgeIconSurface]; + [self addEdgeStyle:style usingSurface:surface]; + cairo_surface_destroy (surface); +} + +- (void) observeEdgeStyle:(EdgeStyle*)style { + [style addObserver:self + forKeyPath:@"name" + options:NSKeyValueObservingOptionNew + context:NULL]; + [style addObserver:self + forKeyPath:@"thickness" + options:0 + context:NULL]; + [style addObserver:self + forKeyPath:@"headStyle" + options:0 + context:NULL]; + [style addObserver:self + forKeyPath:@"tailStyle" + options:0 + context:NULL]; + [style addObserver:self + forKeyPath:@"decorationStyle" + options:0 + context:NULL]; + [style addObserver:self + forKeyPath:@"colorRGB.red" + options:0 + context:NULL]; + [style addObserver:self + forKeyPath:@"colorRGB.green" + options:0 + context:NULL]; + [style addObserver:self + forKeyPath:@"colorRGB.blue" + options:0 + context:NULL]; +} + +- (void) stopObservingEdgeStyle:(EdgeStyle*)style { + [style removeObserver:self forKeyPath:@"name"]; + [style removeObserver:self forKeyPath:@"thickness"]; + [style removeObserver:self forKeyPath:@"headStyle"]; + [style removeObserver:self forKeyPath:@"tailStyle"]; + [style removeObserver:self forKeyPath:@"decorationStyle"]; + [style removeObserver:self forKeyPath:@"colorRGB.red"]; + [style removeObserver:self forKeyPath:@"colorRGB.green"]; + [style removeObserver:self forKeyPath:@"colorRGB.blue"]; +} + +- (void) clearEdgeStylesModel { + GtkTreeModel *model = GTK_TREE_MODEL (store); + GtkTreeIter row; + if (gtk_tree_model_get_iter_first (model, &row)) { + do { + EdgeStyle *rowStyle; + gtk_tree_model_get (model, &row, EDGE_STYLES_PTR_COL, &rowStyle, -1); + [self stopObservingEdgeStyle:rowStyle]; + [rowStyle release]; + } while (gtk_tree_model_iter_next (model, &row)); + } + gtk_list_store_clear (store); +} + +- (void) reloadEdgeStyles { + [self clearEdgeStylesModel]; + cairo_surface_t *surface = [self createEdgeIconSurface]; + for (EdgeStyle *style in [styleManager edgeStyles]) { + [self addEdgeStyle: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/EdgeStylesPalette.m b/tikzit/src/gtk/EdgeStylesPalette.m index 1d55300..7e42552 100644 --- a/tikzit/src/gtk/EdgeStylesPalette.m +++ b/tikzit/src/gtk/EdgeStylesPalette.m @@ -144,11 +144,11 @@ static void clear_style_button_cb (GtkButton *widget, EdgeStylesPalette *palette } - (StyleManager*) styleManager { - return [selector styleManager]; + return [[selector model] styleManager]; } - (void) setStyleManager:(StyleManager*)m { - [selector setStyleManager:m]; + [[selector model] setStyleManager:m]; } - (TikzDocument*) document { @@ -221,7 +221,7 @@ static void clear_style_button_cb (GtkButton *widget, EdgeStylesPalette *palette - (void) removeSelectedStyle { EdgeStyle *style = [selector selectedStyle]; if (style) - [[selector styleManager] removeEdgeStyle:style]; + [[[selector model] styleManager] removeEdgeStyle:style]; } - (void) applySelectedStyle { -- cgit v1.2.3