summaryrefslogtreecommitdiff
path: root/tikzit/src/gtk/NodeStylesModel.m
blob: 3cc5771bf5710b5787fcd69cf5fc79cdcf1555e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * 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;
}

- (NodeStyle*) styleFromIter:(GtkTreeIter*)iter {
    NodeStyle *style = nil;
    gtk_tree_model_get (GTK_TREE_MODEL (store), iter, NODE_STYLES_PTR_COL, &style, -1);
    return style;
}

- (GtkTreeIter*) iterFromStyle:(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_iter_copy (&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