diff --git a/RKSyntaxView/RKSyntaxView.h b/RKSyntaxView/RKSyntaxView.h index 3047aa0..9d26e18 100644 --- a/RKSyntaxView/RKSyntaxView.h +++ b/RKSyntaxView/RKSyntaxView.h @@ -22,6 +22,12 @@ @property (retain) NSDictionary *scheme; @property (retain) NSDictionary *syntax; +@property (nonatomic, retain) NSString* defaultSchemePath; +@property (nonatomic, retain) NSString* defaultSyntaxPath; + + +- (id) initWithDefaultSchemePath:(NSString *)schemePath andSyntaxPath:(NSString *)syntaxPath; // do not use with Nibs + - (void) _setup; #pragma mark - Handling text change @@ -47,4 +53,7 @@ - (void) _setBackgroundColor:(NSColor *)color range:(NSRange)range content:(NSMutableAttributedString *)content; - (void) _setFont:(NSFont *)font range:(NSRange)range content:(NSMutableAttributedString *)content; + + + @end diff --git a/RKSyntaxView/RKSyntaxView.m b/RKSyntaxView/RKSyntaxView.m index d388d64..e9a1da3 100644 --- a/RKSyntaxView/RKSyntaxView.m +++ b/RKSyntaxView/RKSyntaxView.m @@ -13,6 +13,7 @@ @implementation RKSyntaxView @synthesize scheme=_scheme; @synthesize syntax=_syntax; +@synthesize defaultSchemePath, defaultSyntaxPath; #pragma mark - Lifecycle @@ -23,6 +24,18 @@ - (id)init { return self; } +- (id) initWithDefaultSchemePath:(NSString *)schemePath andSyntaxPath:(NSString *)syntaxPath { + self = [super init]; + if (self) { + [self setDefaultSchemePath:schemePath]; + [self setDefaultSyntaxPath:syntaxPath]; + [self loadScheme:[self defaultSchemePath]]; + [self loadSyntax:[self defaultSyntaxPath]]; + [self _setup]; + } + return self; +} + - (void)awakeFromNib { [self _setup]; } @@ -30,12 +43,13 @@ - (void)awakeFromNib { - (void)_setup { [self setTextContainerInset:NSMakeSize(10.0, 10.0)]; [self highlight]; - [self addObserver:self forKeyPath:@"string" options:0 context:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_textDidChange:) name:NSTextDidChangeNotification object:self]; } - (void) dealloc { + [defaultSyntaxPath release]; + [defaultSchemePath release]; [super dealloc]; } @@ -52,11 +66,22 @@ - (void)_textDidChange:(NSNotification *)notif { #pragma mark - Scheme - (void)loadScheme:(NSString *)schemeFilename { - NSString *schemePath = [[NSBundle mainBundle] pathForResource:schemeFilename ofType:@"plist" inDirectory:nil]; - self.scheme = [NSDictionary dictionaryWithContentsOfFile:schemePath]; + if (schemeFilename) { + NSString *schemePath = [[NSBundle mainBundle] pathForResource:schemeFilename ofType:@"plist" inDirectory:nil]; + self.scheme = [NSDictionary dictionaryWithContentsOfFile:schemePath]; + } } - (NSColor *) _colorFor:(NSString *)key { + if (![self scheme] || ![self syntax]) { + if ([self defaultSyntaxPath] && [self defaultSchemePath]) { + [self loadSyntax:[self defaultSyntaxPath]]; + [self loadScheme:[self defaultSchemePath]]; + } else { + [self loadScheme:@"PageScheme"]; + [self loadSyntax:@"PageSyntax"]; + } + } NSString *colorCode = [[self.scheme objectForKey:@"colors"] objectForKey:key]; if (!colorCode) return nil; NSColor *color = [NSColor colorFromHexRGB:colorCode]; @@ -90,8 +115,10 @@ - (NSInteger) _defaultSize { #pragma mark - Syntax - (void)loadSyntax:(NSString *)syntaxFilename { - NSString *schemePath = [[NSBundle mainBundle] pathForResource:syntaxFilename ofType:@"plist" inDirectory:nil]; - self.syntax = [NSDictionary dictionaryWithContentsOfFile:schemePath]; + if (syntaxFilename) { + NSString *schemePath = [[NSBundle mainBundle] pathForResource:syntaxFilename ofType:@"plist" inDirectory:nil]; + self.syntax = [NSDictionary dictionaryWithContentsOfFile:schemePath]; + } } #pragma mark - Highlighting