summaryrefslogtreecommitdiff
path: root/tikzit/src/gtk/ContextWindow.m
blob: 9f376a0cbd4a35eb06a1039408e83c5bb366b76f (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
/*
 * 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 "ContextWindow.h"

#import "Configuration.h"
#import "EdgeStylesModel.h"
#import "NodeStylesModel.h"
#import "PropertiesPane.h"
#import "SelectionPane.h"
#import "StyleManager.h"
#import "Window.h"

#import "gtkhelpers.h"

static gboolean props_window_delete_event_cb (GtkWidget *widget, GdkEvent *event, ContextWindow *window);

@implementation ContextWindow

- (id) init {
    [self release];
    return nil;
}

- (id) initWithStyleManager:(StyleManager*)sm {
    return [self initWithNodeStylesModel:[NodeStylesModel modelWithStyleManager:sm]
                      andEdgeStylesModel:[EdgeStylesModel modelWithStyleManager:sm]];
}

- (id) initWithNodeStylesModel:(NodeStylesModel*)nsm
            andEdgeStylesModel:(EdgeStylesModel*)esm {
    self = [super init];

    if (self) {
        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        g_object_ref_sink (window);
        gtk_window_set_title (GTK_WINDOW (window), "Context");
        gtk_window_set_role (GTK_WINDOW (window), "context");
        gtk_window_set_type_hint (GTK_WINDOW (window),
                                  GDK_WINDOW_TYPE_HINT_UTILITY);
        gtk_window_set_default_size (GTK_WINDOW (window), 200, 500);
        g_signal_connect (G_OBJECT (window),
            "delete-event",
            G_CALLBACK (props_window_delete_event_cb),
            self);

        layout = gtk_vbox_new (FALSE, 3);
        g_object_ref_sink (layout);
        gtk_widget_show (layout);
        gtk_container_set_border_width (GTK_CONTAINER (layout), 6);

        gtk_container_add (GTK_CONTAINER (window), layout);

        propsPane = [[PropertiesPane alloc] init];
        gtk_box_pack_start (GTK_BOX (layout), [propsPane gtkWidget],
                            TRUE, TRUE, 0);

        GtkWidget *sep = gtk_hseparator_new ();
        gtk_widget_show (sep);
        gtk_box_pack_start (GTK_BOX (layout), sep,
                            FALSE, FALSE, 0);

        selPane = [[SelectionPane alloc] initWithNodeStylesModel:nsm
                                              andEdgeStylesModel:esm];
        gtk_box_pack_start (GTK_BOX (layout), [selPane gtkWidget],
                            FALSE, FALSE, 0);

        // hack to position the context window somewhere sensible
        // (upper right)
        gtk_window_parse_geometry (GTK_WINDOW (window), "-0+0");
    }

    return self;
}

- (void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    g_object_unref (layout);
    g_object_unref (window);

    [propsPane release];

    [super dealloc];
}

- (TikzDocument*) document {
    return [propsPane document];
}

- (void) setDocument:(TikzDocument*)doc {
    [propsPane setDocument:doc];
    [selPane setDocument:doc];
}

- (BOOL) visible {
    return gtk_widget_get_visible (window);
}

- (void) setVisible:(BOOL)visible {
    gtk_widget_set_visible (window, visible);
}

- (void) present {
    gtk_window_present (GTK_WINDOW (window));
}

- (void) setTransientFor:(Window*)parent {
    gtk_window_set_transient_for (GTK_WINDOW (window), [parent gtkWindow]);
}

- (void) loadConfiguration:(Configuration*)config {
    [propsPane loadConfiguration:config];
    [selPane loadConfiguration:config];

    if ([config hasGroup:@"ContextWindow"]) {
        tz_restore_window (GTK_WINDOW (window),
                [config integerEntry:@"x" inGroup:@"ContextWindow"],
                [config integerEntry:@"y" inGroup:@"ContextWindow"],
                [config integerEntry:@"w" inGroup:@"ContextWindow"],
                [config integerEntry:@"h" inGroup:@"ContextWindow"]);
    }
    [self setVisible:[config booleanEntry:@"visible"
                                  inGroup:@"ContextWindow"
                              withDefault:YES]];
}

- (void) saveConfiguration:(Configuration*)config {
    gint x, y, w, h;

    gtk_window_get_position (GTK_WINDOW (window), &x, &y);
    gtk_window_get_size (GTK_WINDOW (window), &w, &h);

    [config setIntegerEntry:@"x" inGroup:@"ContextWindow" value:x];
    [config setIntegerEntry:@"y" inGroup:@"ContextWindow" value:y];
    [config setIntegerEntry:@"w" inGroup:@"ContextWindow" value:w];
    [config setIntegerEntry:@"h" inGroup:@"ContextWindow" value:h];
    [config setBooleanEntry:@"visible"
                    inGroup:@"ContextWindow"
                      value:[self visible]];

    [propsPane saveConfiguration:config];
    [selPane saveConfiguration:config];
}

@end

static gboolean props_window_delete_event_cb (GtkWidget *widget, GdkEvent *event, ContextWindow *window) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [window setVisible:NO];
    [pool drain];
    return TRUE;
}

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