summaryrefslogtreecommitdiff
path: root/tikzit/src/gtk/EdgeStyleSelector.m
blob: 8c4e0df4b555bdc4f8f8a0e7ea3a755b90549fc2 (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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
 * Copyright 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 "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 <gdk-pixbuf/gdk-pixbuf.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*) pixbufFromSurface:(cairo_surface_t*)surface;
- (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;
}

- (id) initWithStyleManager:(StyleManager*)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);

        view = GTK_TREE_VIEW (gtk_tree_view_new_with_model (GTK_TREE_MODEL (store)));
        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);
        gtk_tree_view_append_column (view, column);
        gtk_tree_view_set_tooltip_column (view, STYLES_NAME_COL);

        GtkTreeSelection *sel = gtk_tree_view_get_selection (view);
        gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);

        g_signal_connect (G_OBJECT (sel),
                          "changed",
                          G_CALLBACK (selection_changed_cb),
                          self);

        [self setStyleManager:m];

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(shapeDictionaryReplaced:)
                                                     name:@"ShapeDictionaryReplaced"
                                                   object:[Shape class]];
    }

    return self;
}

- (void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    g_object_unref (view);
    [self clearModel];
    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(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];
}

- (GtkWidget*) widget {
    return GTK_WIDGET (view);
}

- (EdgeStyle*) selectedStyle {
    GtkTreeSelection *sel = gtk_tree_view_get_selection (view);
    GtkTreeIter iter;

    if (!gtk_tree_selection_get_selected (sel, NULL, &iter)) {
        return nil;
    }

    EdgeStyle *style = nil;
    gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, STYLES_PTR_COL, &style, -1);

    return style;
}

- (void) setSelectedStyle:(EdgeStyle*)style {
    GtkTreeSelection *sel = gtk_tree_view_get_selection (view);

    if (style == nil) {
        gtk_tree_selection_unselect_all (sel);
        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));
    }
}

@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));
    }
}

- (void) shapeDictionaryReplaced:(NSNotification*)n {
    [self reloadStyles];
}

- (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);
}

- (GdkPixbuf*) pixbufOfEdgeInStyle:(EdgeStyle*)style {
    cairo_surface_t *surface = [self createEdgeIconSurface];
    GdkPixbuf *pixbuf = [self pixbufOfEdgeInStyle:style usingSurface:surface];
    cairo_surface_destroy (surface);
    return pixbuf;
}

// Bring on GTK+3 and gdk_pixbuf_get_from_surface()
- (GdkPixbuf*) pixbufFromSurface:(cairo_surface_t*)surface {
    cairo_surface_flush (surface);

    int width = cairo_image_surface_get_width (surface);
    int height = cairo_image_surface_get_height (surface);
    int stride = cairo_image_surface_get_stride (surface);
    unsigned char *data = cairo_image_surface_get_data (surface);

    GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
                                        TRUE,
                                        8,
                                        width,
                                        height);
    unsigned char *pbdata = gdk_pixbuf_get_pixels (pixbuf);
    int pbstride = gdk_pixbuf_get_rowstride (pixbuf);

    for (int y = 0; y < height; ++y) {
        uint32_t *line = (uint32_t*)(data + y*stride);
        unsigned char *pbline = pbdata + (y*pbstride);
        for (int x = 0; x < width; ++x) {
            uint32_t pixel = *(line + x);
            unsigned char *pbpixel = pbline + (x*4);
            // NB: We should un-pre-mult the alpha here.
            //     However, in our world, alpha is always
            //     on or off, so it doesn't really matter
            pbpixel[3] = ((pixel & 0xff000000) >> 24);
            pbpixel[0] = ((pixel & 0x00ff0000) >> 16);
            pbpixel[1] = ((pixel & 0x0000ff00) >> 8);
            pbpixel[2] =  (pixel & 0x000000ff);
        }
    }

    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 [self pixbufFromSurface: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];
}
// }}}

// vim:ft=objc:ts=8:et:sts=4:sw=4:foldmethod=marker