-
Notifications
You must be signed in to change notification settings - Fork 56
/
EditPaneTextView.m
128 lines (107 loc) · 4.77 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// 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"
NSString * const kEditPaneTextViewChangedNotification = @"EditPaneTextViewChangedNotification";
NSString * const kEditPaneColorChangedNotification = @"EditPaneColorChangedNotification";
@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:(__bridge void *)kEditPaneColorChangedNotification];
[defaultsController addObserver:self
forKeyPath:[NSString stringWithFormat:@"values.%@", kEditPaneBackgroundColor]
options:0
context:(__bridge void *)kEditPaneColorChangedNotification];
[defaultsController addObserver:self
forKeyPath:[NSString stringWithFormat:@"values.%@", kEditPaneSelectionColor]
options:0
context:(__bridge void *)kEditPaneColorChangedNotification];
[defaultsController addObserver:self
forKeyPath:[NSString stringWithFormat:@"values.%@", kEditPaneCaretColor]
options:0
context:(__bridge void *)kEditPaneColorChangedNotification];
[self setUsesFontPanel:NO];
NSTextContainer *textContainer = [[NSTextContainer alloc] init];
[textContainer setContainerSize:[[self textContainer] containerSize]];
[textContainer setWidthTracksTextView:YES];
[self replaceTextContainer:textContainer];
EditPaneLayoutManager *lManager = [[EditPaneLayoutManager alloc] init];
[textContainer replaceLayoutManager:lManager];
layoutMan = lManager;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
NSUserDefaultsController *defaultsController = [NSUserDefaultsController sharedUserDefaultsController];
[defaultsController removeObserver:self
forKeyPath:[NSString stringWithFormat:@"values.%@", kEditPaneForegroundColor]
context:(__bridge void *)kEditPaneColorChangedNotification];
[defaultsController removeObserver:self
forKeyPath:[NSString stringWithFormat:@"values.%@", kEditPaneBackgroundColor]
context:(__bridge void *)kEditPaneColorChangedNotification];
[defaultsController removeObserver:self
forKeyPath:[NSString stringWithFormat:@"values.%@", kEditPaneSelectionColor]
context:(__bridge void *)kEditPaneColorChangedNotification];
[defaultsController removeObserver:self
forKeyPath:[NSString stringWithFormat:@"values.%@", kEditPaneCaretColor]
context:(__bridge void *)kEditPaneColorChangedNotification];
}
- (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];
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 ([(__bridge NSString *)context isEqualToString:kEditPaneColorChangedNotification]) {
[self updateColors];
}
}
@end