diff options
Diffstat (limited to 'tikzit/src/gtk')
-rw-r--r-- | tikzit/src/gtk/EdgeStyleSelector.m | 64 | ||||
-rw-r--r-- | tikzit/src/gtk/NodeStyleSelector.m | 84 |
2 files changed, 128 insertions, 20 deletions
diff --git a/tikzit/src/gtk/EdgeStyleSelector.m b/tikzit/src/gtk/EdgeStyleSelector.m index 4e7736f..b6139b8 100644 --- a/tikzit/src/gtk/EdgeStyleSelector.m +++ b/tikzit/src/gtk/EdgeStyleSelector.m @@ -45,9 +45,12 @@ enum { - (void) styleAdded:(NSNotification*)notification; - (void) styleRemoved:(NSNotification*)notification; - (void) activeStyleChanged:(NSNotification*)notification; -- (void) stylePropertyChanged:(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) @@ -58,6 +61,8 @@ enum { - (GdkPixbuf*) pixbufOfEdgeInStyle:(EdgeStyle*)style usingSurface:(cairo_surface_t*)surface; - (void) addStyle:(EdgeStyle*)style; - (void) postSelectedStyleChanged; +- (void) observeStyle:(EdgeStyle*)style; +- (void) stopObservingStyle:(EdgeStyle*)style; - (void) reloadStyles; @end @@ -109,10 +114,6 @@ enum { [self setStyleManager:m]; [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(stylePropertyChanged:) - name:@"EdgeStylePropertyChanged" - object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shapeDictionaryReplaced:) name:@"ShapeDictionaryReplaced" object:[Shape class]]; @@ -249,6 +250,7 @@ enum { 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; } @@ -265,8 +267,15 @@ enum { } } -- (void) stylePropertyChanged:(NSNotification*)notification { - EdgeStyle *style = [notification object]; +- (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; @@ -275,9 +284,9 @@ enum { EdgeStyle *rowStyle; gtk_tree_model_get (model, &row, STYLES_PTR_COL, &rowStyle, -1); if (style == rowStyle) { - if ([@"name" isEqual:[[notification userInfo] objectForKey:@"propertyName"]]) { + if ([@"name" isEqual:keyPath]) { gtk_list_store_set (store, &row, STYLES_NAME_COL, [[style name] UTF8String], -1); - } else if (![@"scale" isEqual:[[notification userInfo] objectForKey:@"propertyName"]]) { + } else { GdkPixbuf *pixbuf = [self pixbufOfEdgeInStyle:style]; gtk_list_store_set (store, &row, STYLES_ICON_COL, pixbuf, -1); gdk_pixbuf_unref (pixbuf); @@ -314,6 +323,7 @@ enum { 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)); } @@ -402,6 +412,7 @@ enum { STYLES_PTR_COL, (gpointer)[style retain], -1); gdk_pixbuf_unref (pixbuf); + [self observeStyle:style]; } - (void) addStyle:(EdgeStyle*)style { @@ -414,11 +425,44 @@ enum { [[NSNotificationCenter defaultCenter] postNotificationName:@"SelectedStyleChanged" object:self]; } +- (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]; +} + +- (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"]; +} + - (void) reloadStyles { [self clearModel]; + cairo_surface_t *surface = [self createEdgeIconSurface]; for (EdgeStyle *style in [styleManager edgeStyles]) { - [self addStyle:style]; + [self addStyle:style usingSurface:surface]; } + cairo_surface_destroy (surface); } @end diff --git a/tikzit/src/gtk/NodeStyleSelector.m b/tikzit/src/gtk/NodeStyleSelector.m index 7e9757a..af02338 100644 --- a/tikzit/src/gtk/NodeStyleSelector.m +++ b/tikzit/src/gtk/NodeStyleSelector.m @@ -42,9 +42,12 @@ enum { - (void) styleAdded:(NSNotification*)notification; - (void) styleRemoved:(NSNotification*)notification; - (void) activeStyleChanged:(NSNotification*)notification; -- (void) stylePropertyChanged:(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) @@ -54,6 +57,8 @@ enum { - (GdkPixbuf*) pixbufOfNodeInStyle:(NodeStyle*)style usingSurface:(cairo_surface_t*)surface; - (void) addStyle:(NodeStyle*)style; - (void) postSelectedStyleChanged; +- (void) observeStyle:(NodeStyle*)style; +- (void) stopObservingStyle:(NodeStyle*)style; - (void) clearModel; - (void) reloadStyles; @end @@ -97,10 +102,6 @@ enum { [self setStyleManager:m]; [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(stylePropertyChanged:) - name:@"NodeStylePropertyChanged" - object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shapeDictionaryReplaced:) name:@"ShapeDictionaryReplaced" object:[Shape class]]; @@ -242,6 +243,7 @@ enum { 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; } @@ -258,8 +260,15 @@ enum { } } -- (void) stylePropertyChanged:(NSNotification*)notification { - NodeStyle *style = [notification object]; +- (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; @@ -268,9 +277,9 @@ enum { NodeStyle *rowStyle; gtk_tree_model_get (model, &row, STYLES_PTR_COL, &rowStyle, -1); if (style == rowStyle) { - if ([@"name" isEqual:[[notification userInfo] objectForKey:@"propertyName"]]) { + if ([@"name" isEqual:keyPath]) { gtk_list_store_set (store, &row, STYLES_NAME_COL, [[style name] UTF8String], -1); - } else if (![@"scale" isEqual:[[notification userInfo] objectForKey:@"propertyName"]]) { + } else { GdkPixbuf *pixbuf = [self pixbufOfNodeInStyle:style]; gtk_list_store_set (store, &row, STYLES_ICON_COL, pixbuf, -1); gdk_pixbuf_unref (pixbuf); @@ -379,6 +388,7 @@ enum { STYLES_PTR_COL, (gpointer)[style retain], -1); gdk_pixbuf_unref (pixbuf); + [self observeStyle:style]; } - (void) addStyle:(NodeStyle*)style { @@ -391,6 +401,57 @@ enum { [[NSNotificationCenter defaultCenter] postNotificationName:@"SelectedStyleChanged" object:self]; } +- (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); @@ -399,6 +460,7 @@ enum { 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)); } @@ -407,9 +469,11 @@ enum { - (void) reloadStyles { [self clearModel]; + cairo_surface_t *surface = [self createNodeIconSurface]; for (NodeStyle *style in [styleManager nodeStyles]) { - [self addStyle:style]; + [self addStyle:style usingSurface:surface]; } + cairo_surface_destroy (surface); } @end |