]> granicus.if.org Git - graphviz/commitdiff
Mac tweak panel for graph, default node and default edge attributes; objects are...
authorglenlow <devnull@localhost>
Mon, 11 Feb 2008 12:34:07 +0000 (12:34 +0000)
committerglenlow <devnull@localhost>
Mon, 11 Feb 2008 12:34:07 +0000 (12:34 +0000)
macosx/GVGraphDefaultAttributes.h [new file with mode: 0644]
macosx/GVGraphDefaultAttributes.m [new file with mode: 0644]
macosx/GVWindowController.h
macosx/GVWindowController.m

diff --git a/macosx/GVGraphDefaultAttributes.h b/macosx/GVGraphDefaultAttributes.h
new file mode 100644 (file)
index 0000000..ef040d2
--- /dev/null
@@ -0,0 +1,44 @@
+/* $Id$ $Revision$ */
+/* vim:set shiftwidth=4 ts=8: */
+
+/**********************************************************
+*      This software is part of the graphviz package      *
+*                http://www.graphviz.org/                 *
+*                                                         *
+*            Copyright (c) 1994-2008 AT&T Corp.           *
+*                and is licensed under the                *
+*            Common Public License, Version 1.0           *
+*                      by AT&T Corp.                      *
+*                                                         *
+*        Information and Software Systems Research        *
+*              AT&T Research, Florham Park NJ             *
+**********************************************************/
+
+#import <Foundation/Foundation.h>
+
+#include <graphviz/types.h>
+#include <graphviz/graph.h>
+
+@class GVGraph;
+
+@interface GVGraphDefaultAttributes : NSMutableDictionary
+{
+       GVGraph *_graph;
+       Agdict_t *_defaultAttributes;
+       Agsym_t *(*_attributeDeclaration)(Agraph_t *, char *, char *);
+}
+
+@property(readonly) NSString *name;
+
+- (id)initWithGraph:(GVGraph *)graph defaultAttributes:(Agdict_t *)defaultAttributes attributeDeclaration:(Agsym_t *(*)(Agraph_t *, char *, char *))attributeDeclaration;
+
+/* dictionary primitive methods */
+- (NSUInteger)count;
+- (NSEnumerator *)keyEnumerator;
+- (id)objectForKey:(id)aKey;
+
+/* mutable dictionary primitive methods */
+- (void)setObject:(id)anObject forKey:(id)aKey;
+- (void)removeObjectForKey:(id)aKey;
+
+@end
diff --git a/macosx/GVGraphDefaultAttributes.m b/macosx/GVGraphDefaultAttributes.m
new file mode 100644 (file)
index 0000000..ed4bf79
--- /dev/null
@@ -0,0 +1,120 @@
+/* $Id$ $Revision$ */
+/* vim:set shiftwidth=4 ts=8: */
+
+/**********************************************************
+*      This software is part of the graphviz package      *
+*                http://www.graphviz.org/                 *
+*                                                         *
+*            Copyright (c) 1994-2008 AT&T Corp.           *
+*                and is licensed under the                *
+*            Common Public License, Version 1.0           *
+*                      by AT&T Corp.                      *
+*                                                         *
+*        Information and Software Systems Research        *
+*              AT&T Research, Florham Park NJ             *
+**********************************************************/
+
+#import "GVGraphDefaultAttributes.h"
+#import "GVGraph.h"
+
+@interface GVGraphDefaultAttributeKeyEnumerator : NSEnumerator
+{
+       Agsym_t **_nextSymbol;
+       Agsym_t **_lastSymbol;
+}
+
+- (id)initWithSymbols:(Agsym_t **)symbols count:(NSUInteger)count;
+- (NSArray *)allObjects;
+- (id)nextObject;
+
+@end
+
+@implementation GVGraphDefaultAttributeKeyEnumerator
+
+- (id)initWithSymbols:(Agsym_t **)symbols count:(NSUInteger)count
+{
+       if (self = [super init]) {
+               _nextSymbol = symbols;
+               _lastSymbol = symbols + count;
+       }
+       return self;
+}
+
+- (NSArray *)allObjects
+{
+       NSMutableArray* all = [NSMutableArray array];
+       for (; _nextSymbol < _lastSymbol; ++_nextSymbol)
+               if ((*_nextSymbol)->value && *(*_nextSymbol)->value)
+                       [all addObject:[NSString stringWithUTF8String:(*_nextSymbol)->name]];
+                       
+       return all;
+}
+
+- (id)nextObject
+{
+       char* nextName = NULL;
+       for (; _nextSymbol < _lastSymbol && !nextName; ++_nextSymbol)
+               if ((*_nextSymbol)->value && *(*_nextSymbol)->value)
+                       nextName = (*_nextSymbol)->name;
+               
+       return nextName ? [NSString stringWithUTF8String:nextName] : nil;
+}
+
+@end
+
+@implementation GVGraphDefaultAttributes
+
+- (id)initWithGraph:(GVGraph *)graph defaultAttributes:(Agdict_t *)defaultAttributes attributeDeclaration:(Agsym_t *(*)(Agraph_t *, char *, char *))attributeDeclaration
+{
+       if (self = [super init]) {
+               _graph = graph; /* not retained to avoid a retain cycle */
+               _defaultAttributes = defaultAttributes;
+               _attributeDeclaration = attributeDeclaration;
+       }
+       return self;
+}
+
+- (NSString*)name
+{
+       return _defaultAttributes->name ? [NSString stringWithUTF8String:_defaultAttributes->name] : nil;
+}
+
+- (NSUInteger)count
+{
+       NSUInteger symbolCount = 0;
+       Agsym_t **nextSymbol, **lastSymbol;
+       for (nextSymbol = _defaultAttributes->list, lastSymbol = _defaultAttributes->list + dtsize(_defaultAttributes->dict); nextSymbol < lastSymbol; ++nextSymbol)
+               if ((*nextSymbol)->value && *(*nextSymbol)->value)
+                       ++symbolCount;
+       return symbolCount;
+}
+
+- (NSEnumerator *)keyEnumerator
+{
+       return [[[GVGraphDefaultAttributeKeyEnumerator alloc] initWithSymbols: _defaultAttributes->list count:dtsize(_defaultAttributes->dict)] autorelease];
+}
+
+- (id)objectForKey:(id)aKey
+{
+       id object = nil;
+       Agsym_t *attributeSymbol = dtmatch(_defaultAttributes->dict, [aKey UTF8String]);
+       if (attributeSymbol) {
+               char *attributeValue = attributeSymbol->value;
+               if (attributeValue && *attributeValue)
+                       object = [NSString stringWithUTF8String:attributeSymbol->value];
+       }
+       return object;
+}
+
+- (void)setObject:(id)anObject forKey:(id)aKey
+{
+       _attributeDeclaration([_graph graph], (char *)[aKey UTF8String], (char *)[anObject UTF8String]);
+       [_graph noteChanged:YES];
+}
+
+- (void)removeObjectForKey:(id)aKey
+{
+       _attributeDeclaration([_graph graph], (char *)[aKey UTF8String], "");
+       [_graph noteChanged:YES];
+}
+@end
index 480b9316c879a003a5f4596ac117cd231a3dde10..48c7b8bc08e4df8e9bbc7cfbccfe8b004cdd3b48 100644 (file)
 #import <AppKit/AppKit.h>
 #import <Quartz/Quartz.h>
 
+@class GVGraph;
+
 @interface GVWindowController : NSWindowController
 {
        IBOutlet PDFView *documentView;
+       
+       GVGraph *_graph;
 }
 
-- (void)windowDidLoad;
+@property(readonly) GVGraph *graph;
+
+- (id)init;
+- (void)setDocument: (NSDocument *)document;
+- (void)awakeFromNib;
+
+- (void)graphDidChange:(NSNotification*)notification;
 
+- (void)dealloc;
 @end
index 04905f8a25fbe88bbccd1385ba53529ce1af93d8..04f3385aa38de7d69c973e247b0b57baba3af783 100644 (file)
 **********************************************************/
 
 #import "GVWindowController.h"
-#import "GVContext.h"
+#import "GVGraph.h"
 #import "GVDocument.h"
 
 @implementation GVWindowController
 
-- (void)windowDidLoad
+@synthesize graph = _graph;
+
+- (id)init
+{
+       if (self = [super initWithWindowNibName: @"Document"])
+               _graph = nil;
+       return self;
+}
+
+- (void)setDocument: (NSDocument *)document
 {
-       GVDocument *graphDocument = [self document];
-       if ([graphDocument respondsToSelector:@selector(graph)]) {
-               graph_t *graph = [graphDocument graph];
-               GVContext* context = [GVContext sharedContext];
-               [context layoutGraph:graph withEngine:@"dot"];
-               [documentView setDocument:[[[PDFDocument alloc] initWithData:[context renderGraph:graph withFormat:@"pdf:quartz"]] autorelease]];
+       if ([document respondsToSelector:@selector(graph)]) {
+               GVGraph *newGraph = [(GVDocument *)document graph];
+               if (_graph != newGraph) {
+                       /* retain the new document graph and start observing any changes from it */
+                       NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
+                       if (_graph) {
+                               [defaultCenter removeObserver:self name:@"GVGraphDidChange" object:_graph];
+                               [_graph release];
+                       }
+                       _graph = nil;
+                       if (newGraph) {
+                               _graph = [newGraph retain];
+                               [defaultCenter addObserver:self selector:@selector(graphDidChange:) name:@"GVGraphDidChange" object:newGraph];
+                       }
+               }
        }
+       
+       [super setDocument:document];
+}
+
+- (void)awakeFromNib
+{
+       [self graphDidChange:nil];
+}
+
+- (void)graphDidChange:(NSNotification*)notification
+{
+       /* whenever the graph changes, rerender its PDF and display that */
+       [documentView setDocument:[[[PDFDocument alloc] initWithData:[_graph renderWithFormat:@"pdf:quartz"]] autorelease]];
+}
+
+- (void)dealloc
+{
+       if (_graph) {
+               [[NSNotificationCenter defaultCenter] removeObserver:self name:@"GVGraphDidChange" object:_graph];
+               [_graph release];
+       }
+
+       [super dealloc];
 }
 
 @end