summaryrefslogtreecommitdiff
path: root/tikzit/src/osx/TikzSourceController.m
blob: 6d1580c7e0e13e13ac1134c0a1bc3f8f99e4d995 (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
//
//  TikzSourceController.m
//  TikZiT
//  
//  Copyright 2010 Aleks Kissinger. All rights reserved.
//  
//  
//  This file is part of TikZiT.
//  
//  TikZiT 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 3 of the License, or
//  (at your option) any later version.
//  
//  TikZiT 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 TikZiT.  If not, see <http://www.gnu.org/licenses/>.
//  

#import "TikzSourceController.h"
#import "Graph.h"

@implementation TikzSourceController

@synthesize graphicsView, sourceView, source, status;
@synthesize documentUndoManager, tikzChanged;

- (void)endEditing {
	NSResponder *res = [[sourceView window] firstResponder];
	[[sourceView window] makeFirstResponder:nil];
	[[sourceView window] makeFirstResponder:res];
}

- (void)undoParseTikz:(Graph *)oldGraph {
	[graphicsView setGraph:oldGraph];
	[graphicsView setEnabled:NO];
	[graphicsView postGraphChange];
	[graphicsView refreshLayers];
	
	[documentUndoManager registerUndoWithTarget:self
									   selector:@selector(parseTikz:)
										 object:self];
	[documentUndoManager setActionName:@"Parse Tikz"];
}

- (void)undoRevertTikz:(NSString*)oldTikz {
	[self setTikz:oldTikz];
	[graphicsView setEnabled:NO];
	[graphicsView refreshLayers];
	
	[documentUndoManager registerUndoWithTarget:self
									   selector:@selector(revertTikz:)
										 object:self];
	[documentUndoManager setActionName:@"Revert Tikz"];
}

- (void)undoTikzChange:(id)ignore {
	[graphicsView setEnabled:YES];
	[graphicsView refreshLayers];
	[self endEditing];
	[self updateTikzFromGraph];
	[documentUndoManager registerUndoWithTarget:self
									   selector:@selector(redoTikzChange:)
										 object:nil];
	[documentUndoManager setActionName:@"Tikz Change"];
}

- (void)redoTikzChange:(id)ignore {
	[graphicsView setEnabled:NO];
	[graphicsView refreshLayers];
	[documentUndoManager registerUndoWithTarget:self
									   selector:@selector(undoTikzChange:)
										 object:nil];
	[documentUndoManager setActionName:@"Tikz Change"];
}


- (void)awakeFromNib {
	justUndid = NO;
	assembler = [[TikzGraphAssembler alloc] init];
	
	successColor = [NSColor colorWithCalibratedRed:0.0f
											 green:0.5f
											  blue:0.0f
											 alpha:1.0f];
	failedColor = [NSColor redColor];
	
	NSFont *font = [NSFont userFixedPitchFontOfSize:11.0f];
	
	if (font != nil) {
		textAttrs = [NSDictionary dictionaryWithObject:font
								forKey:NSFontAttributeName];
	} else {
		NSLog(@"WARNING: couldn't find monospaced font.");
		textAttrs = [NSDictionary dictionary];
	}
	
	
	[self graphChanged:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(graphChanged:)
												 name:@"GraphChanged"
											   object:graphicsView];
}

- (void)setTikz:(NSString *)str {
	[self willChangeValueForKey:@"source"];
	source = [[NSAttributedString alloc] initWithString:str attributes:textAttrs];
	[self didChangeValueForKey:@"source"];
}

- (NSString*)tikz {
	return [source string];
}

- (void)updateTikzFromGraph {
 	[self setTikz:[[graphicsView graph] tikz]];
}

- (void)graphChanged:(NSNotification*)n {
	if ([graphicsView enabled]) [self updateTikzFromGraph];
}

- (void)textDidBeginEditing:(NSNotification *)notification {
	if ([graphicsView enabled]) {
		[graphicsView setEnabled:NO];
		[graphicsView refreshLayers];
		[documentUndoManager registerUndoWithTarget:self
										   selector:@selector(undoTikzChange:)
											 object:nil];
		[documentUndoManager setActionName:@"Tikz Change"];
	}
}

- (BOOL)tryParseTikz {
    BOOL success = [assembler parseTikz:[self tikz]];
    
    if (success) {
        [graphicsView deselectAll:self];
        [graphicsView setGraph:[assembler graph]];
        [graphicsView refreshLayers];
        [self doRevertTikz];
    }
    
    return success;
}

- (void)doRevertTikz {
    [self updateTikzFromGraph];
    [self endEditing];
    [graphicsView setEnabled:YES];
    [graphicsView refreshLayers];
    [status setStringValue:@""];
}

- (void)parseTikz:(id)sender {
	if (![graphicsView enabled]) {
        Graph *oldGraph = [graphicsView graph];
		if ([self tryParseTikz]) {
            [self endEditing];
            [documentUndoManager registerUndoWithTarget:self
                                               selector:@selector(undoParseTikz:)
                                                 object:oldGraph];
            [documentUndoManager setActionName:@"Parse Tikz"];
            
            [status setStringValue:@"success"];
            [status setTextColor:successColor];
        } else {
            [status setStringValue:@"parse error"];
            [status setTextColor:failedColor];
        }
	}
}

- (void)revertTikz:(id)sender {
	if (![graphicsView enabled]) {
		NSString *oldTikz = [[self tikz] copy];
		[self doRevertTikz];
		
		[documentUndoManager registerUndoWithTarget:self
										   selector:@selector(undoRevertTikz:)
											 object:oldTikz];
		[documentUndoManager setActionName:@"Revert Tikz"];
	}
}

- (void)finalize {
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	[super finalize];
}

@end