3 * Copyright (c) 2007 - 2011, Blue Static <http://www.bluestatic.org>
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17 #import "BSSourceView.h"
19 #import "BSLineNumberRulerView.h"
20 #import "BSSourceViewTextView.h"
22 @interface BSSourceView (Private)
24 - (void)errorHighlightingFile:(NSNotification*)notif;
25 - (void)setPlainTextStringFromFile:(NSString*)filePath;
28 @implementation BSSourceView
30 @synthesize textView = textView_;
31 @synthesize scrollView = scrollView_;
32 @synthesize markers = markers_;
33 @synthesize markedLine = markedLine_;
34 @synthesize delegate = delegate_;
35 @synthesize file = file_;
38 * Initializes the source view with the path of a file
40 - (id)initWithFrame:(NSRect)frame
42 if (self = [super initWithFrame:frame]) {
44 [[NSNotificationCenter defaultCenter]
46 selector:@selector(errorHighlightingFile:)
47 name:NSFileHandleReadToEndOfFileCompletionNotification
62 [scrollView_ removeFromSuperview];
63 [textView_ removeFromSuperview];
68 - (void)setMarkers:(NSSet*)markers {
70 markers_ = [markers copy];
72 [ruler_ setNeedsDisplay:YES];
75 - (void)setMarkedLine:(NSUInteger)markedLine {
76 markedLine_ = markedLine;
77 [ruler_ setNeedsDisplay:YES];
81 * Sets the file name as well as the text of the source view
83 - (void)setFile:(NSString*)f
90 if (![[NSFileManager defaultManager] fileExistsAtPath:f]) {
91 [textView_ setString:@""];
96 // Attempt to use the PHP CLI to highlight the source file as HTML
97 NSPipe* outPipe = [NSPipe pipe];
98 NSPipe* errPipe = [NSPipe pipe];
99 NSTask* task = [[NSTask new] autorelease];
101 [task setLaunchPath:@"/usr/bin/php"]; // This is the path to the default Leopard PHP executable
102 [task setArguments:[NSArray arrayWithObjects:@"-s", f, nil]];
103 [task setStandardOutput:outPipe];
104 [task setStandardError:errPipe];
107 [[errPipe fileHandleForReading] readToEndOfFileInBackgroundAndNotify];
109 NSData* data = [[outPipe fileHandleForReading] readDataToEndOfFile];
110 NSAttributedString* source = [[NSAttributedString alloc] initWithHTML:data
111 options:@{ NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding) }
112 documentAttributes:nil];
113 [[textView_ textStorage] setAttributedString:source];
115 } @catch (NSException* exception) {
116 // If the PHP executable is not available then the NSTask will throw an exception
117 [self setPlainTextStringFromFile:f];
120 [ruler_ performLayout];
124 * Sets the contents of the SourceView via a string rather than loading from a path
126 - (void)setString:(NSString*)source asFile:(NSString*)path
128 // create the temp file
129 NSError* error = nil;
130 NSString* tmpPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"MacGDBpHighlighter"];
131 [source writeToFile:tmpPath atomically:NO encoding:NSUTF8StringEncoding error:&error];
133 [textView_ setString:source];
137 // highlight the temporary file
138 [self setFile:tmpPath];
140 // delete the temp file
141 [[NSFileManager defaultManager] removeItemAtPath:tmpPath error:NULL];
143 // plop in our fake path so nobody knows the difference
149 [ruler_ performLayout];
153 * If an error occurs in reading the highlighted PHP source, this will merely set the string
155 - (void)errorHighlightingFile:(NSNotification*)notif
157 NSData* data = [[notif userInfo] objectForKey:NSFileHandleNotificationDataItem];
158 if ([data length] > 0 && file_) // there's something on stderr, so the PHP CLI failed
159 [self setPlainTextStringFromFile:file_];
163 * Flip the coordinates
171 * Tells the text view to scroll to a certain line
173 - (void)scrollToLine:(NSUInteger)line
175 if ([[textView_ textStorage] length] == 0)
178 // go through the document until we find the NSRange for the line we want
179 NSUInteger rangeIndex = 0;
180 for (NSUInteger i = 0; i < line; i++) {
181 rangeIndex = NSMaxRange([[textView_ string] lineRangeForRange:NSMakeRange(rangeIndex, 0)]);
184 // now get the true start/end markers for it
185 NSUInteger lineStart, lineEnd;
186 [[textView_ string] getLineStart:&lineStart
189 forRange:NSMakeRange(rangeIndex - 1, 0)];
190 [textView_ scrollRangeToVisible:[[textView_ string]
191 lineRangeForRange:NSMakeRange(lineStart, lineEnd - lineStart)]];
192 [scrollView_ setNeedsDisplay:YES];
196 * Setup all the subviews for the source metaview
200 // Create the scroll view.
201 scrollView_ = [[[NSScrollView alloc] initWithFrame:[self bounds]] autorelease];
202 [scrollView_ setHasHorizontalScroller:YES];
203 [scrollView_ setHasVerticalScroller:YES];
204 [scrollView_ setAutohidesScrollers:YES];
205 [scrollView_ setBorderType:NSBezelBorder];
206 [scrollView_ setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
207 [[scrollView_ contentView] setAutoresizesSubviews:YES];
208 [self addSubview:scrollView_];
210 // add the text view to the scroll view
212 textFrame.origin = NSMakePoint(0.0, 0.0);
213 textFrame.size = [scrollView_ contentSize];
214 textView_ = [[[BSSourceViewTextView alloc] initWithFrame:textFrame] autorelease];
215 [textView_ setSourceView:self];
216 [textView_ setEditable:NO];
217 [textView_ setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
218 [textView_ setHorizontallyResizable:YES];
219 [textView_ setVerticallyResizable:YES];
220 [textView_ setMinSize:textFrame.size];
221 [textView_ setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
222 [[textView_ textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
223 [[textView_ textContainer] setWidthTracksTextView:NO];
224 [[textView_ textContainer] setHeightTracksTextView:NO];
225 [textView_ setAutoresizingMask:NSViewNotSizable];
226 [scrollView_ setDocumentView:textView_];
229 ruler_ = [[[BSLineNumberRulerView alloc] initWithSourceView:self] autorelease];
230 [scrollView_ setVerticalRulerView:ruler_];
231 [scrollView_ setHasHorizontalRuler:NO];
232 [scrollView_ setHasVerticalRuler:YES];
233 [scrollView_ setRulersVisible:YES];
235 NSArray* types = [NSArray arrayWithObject:NSFilenamesPboardType];
236 [self registerForDraggedTypes:types];
240 * Gets the plain-text representation of the file at |filePath| and sets the
241 * contents in the source view.
243 - (void)setPlainTextStringFromFile:(NSString*)filePath
245 NSError* error = nil;
246 NSString* contents = [NSString stringWithContentsOfFile:filePath
247 encoding:NSUTF8StringEncoding
250 NSLog(@"Error reading file at %@: %@", filePath, error);
253 [textView_ setString:contents];
256 // Drag Handlers ///////////////////////////////////////////////////////////////
259 * Validates an initiated drag operation.
261 - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender
263 return NSDragOperationCopy;
267 * Performs a dragging operation of files to set the contents of the file.
269 - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender
271 NSPasteboard* pboard = [sender draggingPasteboard];
272 if ([[pboard types] containsObject:NSFilenamesPboardType]) {
273 NSArray* files = [pboard propertyListForType:NSFilenamesPboardType];
275 NSString* filename = [files objectAtIndex:0];
276 [self setFile:filename];