forked from rentzsch/markdownlive
-
Notifications
You must be signed in to change notification settings - Fork 2
/
EditPaneTextView.m
113 lines (95 loc) · 3.66 KB
/
EditPaneTextView.m
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
//
// EditPaneTextView.m
// MarkdownLive
//
// Created by Akihiro Noguchi on 9/05/11.
// Copyright 2011 Aki. All rights reserved.
//
#import "EditPaneTextView.h"
#import "EditPaneLayoutManager.h"
#import "PreferencesManager.h"
#import "PreferencesController.h"
@implementation EditPaneTextView
- (void)awakeFromNib {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateFont)
name:kEditPaneFontNameChangedNotification
object:nil];
NSUserDefaultsController *defaultsController = [NSUserDefaultsController sharedUserDefaultsController];
[defaultsController addObserver:self
forKeyPath:[NSString stringWithFormat:@"values.%@", kEditPaneForegroundColor]
options:0
context:@"ColorChange"];
[defaultsController addObserver:self
forKeyPath:[NSString stringWithFormat:@"values.%@", kEditPaneBackgroundColor]
options:0
context:@"ColorChange"];
[defaultsController addObserver:self
forKeyPath:[NSString stringWithFormat:@"values.%@", kEditPaneSelectionColor]
options:0
context:@"ColorChange"];
[defaultsController addObserver:self
forKeyPath:[NSString stringWithFormat:@"values.%@", kEditPaneCaretColor]
options:0
context:@"ColorChange"];
[self setUsesFontPanel:NO];
NSTextContainer *textContainer = [[NSTextContainer alloc] init];
[textContainer setContainerSize:[[self textContainer] containerSize]];
[textContainer setWidthTracksTextView:YES];
layoutMan = [[EditPaneLayoutManager alloc] init];
[self replaceTextContainer:textContainer];
[textContainer replaceLayoutManager:layoutMan];
[textContainer release];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self];
[layoutMan release];
[super dealloc];
}
- (void)keyDown:(NSEvent *)aEvent {
[super keyDown:aEvent];
[[NSNotificationCenter defaultCenter] postNotificationName:kEditPaneTextViewChangedNotification
object:self];
}
- (void)setMarkedText:(id)aString
selectedRange:(NSRange)selectedRange replacementRange:(NSRange)replacementRange {
id resultString;
if ([aString isKindOfClass:[NSAttributedString class]]) {
resultString = [[aString mutableCopy] autorelease];
selectedRange = NSMakeRange(0, [resultString length]);
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:NSUnderlineStyleSingle], NSUnderlineStyleAttributeName,
[PreferencesManager editPaneForegroundColor], NSUnderlineColorAttributeName,
nil];
[resultString setAttributes:attrs range:selectedRange];
} else {
resultString = aString;
}
[super setMarkedText:resultString
selectedRange:selectedRange replacementRange:replacementRange];
}
- (void)updateColors {
[[self enclosingScrollView] setBackgroundColor:[PreferencesManager editPaneBackgroundColor]];
[self setTextColor:[PreferencesManager editPaneForegroundColor]];
[self setInsertionPointColor:[PreferencesManager editPaneCaretColor]];
NSDictionary *selectedAttr = [NSDictionary dictionaryWithObject:[PreferencesManager editPaneSelectionColor]
forKey:NSBackgroundColorAttributeName];
[self setSelectedTextAttributes:selectedAttr];
}
- (void)updateFont {
layoutMan.font = [PreferencesManager editPaneFont];
[self setFont:layoutMan.font];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
#pragma unused(keyPath)
#pragma unused(object)
#pragma unused(change)
if ([(NSString *)context isEqualToString:@"ColorChange"]) {
[self updateColors];
}
}
@end