cd55f40353
- MyDocument.xib produced a crash wehn decoding where the NSSplitView is observing it's children, particularly the WebView. Fixed by rebuilding the xib. - Conversion to ARC created a cycle between EditPaneTextView and it's EditPaneLayoutManager instance variable; fix with a weak references, which requires targeting 10.7. - MyDocument had a cyle with it's NSTimer; this most likely was a leak under GC also, as the timer would pin the document in memory beyond the window close. - removeObserver: was being called on NSUserDefaultsController, which isn't a valid selector; most likely the NSTimer leak was preventing this from causing a crash. Replace with removing specific KVO observers.
129 lines
4.8 KiB
Objective-C
129 lines
4.8 KiB
Objective-C
//
|
|
// 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
|