summaryrefslogtreecommitdiff
path: root/tikzit/src/osx/PreambleController.m
blob: e09a3fc633375f0ff4808454e355dc82740592ea (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
//
//  PreambleController.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 "PreambleController.h"


@implementation PreambleController

@synthesize preambleText, preambles;

- (id)initWithWindowNibName:(NSString *)windowNibName plist:(NSString*)plist styles:(NSArray*)sty {
	[super initWithWindowNibName:windowNibName];
	
	preambles = (Preambles*)[NSKeyedUnarchiver unarchiveObjectWithFile:plist];
	[preambles setStyles:sty];
	if (preambles == nil) preambles = [[Preambles alloc] init];
	
	preambleText = nil;
	
	NSFont *font = [NSFont userFixedPitchFontOfSize:11.0f];
	textAttrs = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
	ghostColor = [NSColor colorWithDeviceRed:0.9f green:0.9f blue:0.9f alpha:1.0f];
	
	
	
	return self;
}

- (void)awakeFromNib {
	NSArray *arr = [preambleDictionaryController arrangedObjects];
	NSString *current = [preambles selectedPreambleName];
	
	if (current != nil && ![current isEqual:@"default"]) {
		for (int i = 0; i < [arr count]; ++i) {
			if ([[[arr objectAtIndex:i] key] isEqual:current]) {
				[self setSelectionIndexes:[NSIndexSet indexSetWithIndex:i]];
				break;
			}
		}
	}
}

- (BOOL)useDefaultPreamble {
	return [[preambles selectedPreambleName] isEqualToString:@"default"];
}

- (void)flushText {
	if (preambleText != nil && ![self useDefaultPreamble]) {
		[preambles setCurrentPreamble:[preambleText string]];
	}
}

- (void)setCurrentPreamble:(NSString*)current {
	[self flushText];
	
	[self willChangeValueForKey:@"useDefaultPreamble"];
	[preambles setSelectedPreambleName:current];
	[self didChangeValueForKey:@"useDefaultPreamble"];
	
	[self setPreambleText:
	[[NSAttributedString alloc] initWithString:[preambles currentPreamble]
									attributes:textAttrs]];
}

- (void)showWindow:(id)sender {
	[super showWindow:sender];
	if ([self useDefaultPreamble]) {
		[toolbar setSelectedItemIdentifier:[defaultToolbarItem itemIdentifier]];
	} else {
		[toolbar setSelectedItemIdentifier:[customToolbarItem itemIdentifier]];
	}
	
	[self setPreamble:self];
}

- (void)savePreambles:(NSString*)plist {
	[self flushText];
	[NSKeyedArchiver archiveRootObject:preambles toFile:plist];
}

- (NSString*)currentPreamble {
	[self flushText];
	return [preambles currentPreamble];
}

- (NSString*)currentPostamble {
	return [preambles currentPostamble];
}

- (void)setSelectionIndexes:(NSIndexSet *)idx {
	[self willChangeValueForKey:@"selectionIndexes"];
	selectionIndexes = idx;
	[self didChangeValueForKey:@"selectionIndexes"];
	
	[self setPreamble:self];
}

- (NSIndexSet*)selectionIndexes {
	return selectionIndexes;
}

- (IBAction)setPreamble:(id)sender {
	if ([[toolbar selectedItemIdentifier] isEqualToString:[defaultToolbarItem itemIdentifier]]) {
		[self setCurrentPreamble:@"default"];
		[textView setBackgroundColor:ghostColor];
	} else if ([[toolbar selectedItemIdentifier] isEqualToString:[customToolbarItem itemIdentifier]]) {
		NSString *key = nil;
		if ([selectionIndexes count]==1) {
			int i = [selectionIndexes firstIndex];
			key = [[[preambleDictionaryController arrangedObjects] objectAtIndex:i] key];
		}
		if (key != nil) {
			[self setCurrentPreamble:key];
			//NSLog(@"preamble set to %@", key);
		} else {
			[self setCurrentPreamble:@"custom"];
			//NSLog(@"preamble set to custom");
		}
		[textView setBackgroundColor:[NSColor whiteColor]];
	}
}

- (IBAction)insertDefaultStyles:(id)sender {
	[textView insertText:[preambles styleDefinitions]];
}

- (IBAction)addPreamble:(id)sender {
	[preambleDictionaryController setInitialKey:@"new preamble"];
	[preambleDictionaryController setInitialValue:[preambles defaultPreamble]];
	[preambleDictionaryController add:sender];
}

- (void)controlTextDidEndEditing:(NSNotification *)obj {
	//NSLog(@"got a text change");
	[self setPreamble:[obj object]];
}


// NOT IMPLEMENTED
- (IBAction)duplicatePreamble:(id)sender {
//	NSLog(@"set text to: %@", [preambles currentPreamble]);
//	[preambleDictionaryController setInitialKey:[preambles selectedPreambleName]];
//	[preambleDictionaryController setInitialValue:[preambles currentPreamble]];
//	[preambleDictionaryController add:sender];
}


@end