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 #include "BSLineNumberRulerView.h"
21 @interface BSSourceView (Private)
23 - (void)errorHighlightingFile:(NSNotification*)notif;
24 - (void)setPlainTextStringFromFile:(NSString*)filePath;
27 @implementation BSSourceView
29 @synthesize textView = textView_;
30 @synthesize scrollView = scrollView_;
31 @synthesize markers = markers_;
32 @synthesize markedLine;
37 * Initializes the source view with the path of a file
39 - (id)initWithFrame:(NSRect)frame
41 if (self = [super initWithFrame:frame])
44 [[NSNotificationCenter defaultCenter]
46 selector:@selector(errorHighlightingFile:)
47 name:NSFileHandleReadToEndOfFileCompletionNotification
61 [scrollView_ removeFromSuperview];
62 [textView_ removeFromSuperview];
68 * Sets the file name as well as the text of the source view
70 - (void)setFile:(NSString*)f
78 if (![[NSFileManager defaultManager] fileExistsAtPath:f])
80 [textView_ setString:@""];
86 // Attempt to use the PHP CLI to highlight the source file as HTML
87 NSPipe* outPipe = [NSPipe pipe];
88 NSPipe* errPipe = [NSPipe pipe];
89 NSTask* task = [[NSTask new] autorelease];
91 [task setLaunchPath:@"/usr/bin/php"]; // This is the path to the default Leopard PHP executable
92 [task setArguments:[NSArray arrayWithObjects:@"-s", f, nil]];
93 [task setStandardOutput:outPipe];
94 [task setStandardError:errPipe];
97 [[errPipe fileHandleForReading] readToEndOfFileInBackgroundAndNotify];
99 NSData* data = [[outPipe fileHandleForReading] readDataToEndOfFile];
100 NSAttributedString* source = [[NSAttributedString alloc] initWithHTML:data documentAttributes:NULL];
101 [[textView_ textStorage] setAttributedString:source];
104 @catch (NSException* exception)
106 // If the PHP executable is not available then the NSTask will throw an exception
107 [self setPlainTextStringFromFile:f];
112 * Sets the contents of the SourceView via a string rather than loading from a path
114 - (void)setString:(NSString*)source asFile:(NSString*)path
116 // create the temp file
117 NSError* error = nil;
118 NSString* tmpPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"MacGDBpHighlighter"];
119 [source writeToFile:tmpPath atomically:NO encoding:NSUTF8StringEncoding error:&error];
122 [textView_ setString:source];
126 // highlight the temporary file
127 [self setFile:tmpPath];
129 // delete the temp file
130 [[NSFileManager defaultManager] removeItemAtPath:tmpPath error:NULL];
132 // plop in our fake path so nobody knows the difference
141 * If an error occurs in reading the highlighted PHP source, this will merely set the string
143 - (void)errorHighlightingFile:(NSNotification*)notif
145 NSData* data = [[notif userInfo] objectForKey:NSFileHandleNotificationDataItem];
146 if ([data length] > 0) // there's something on stderr, so the PHP CLI failed
147 [self setPlainTextStringFromFile:file];
151 * Flip the coordinates
159 * Tells the text view to scroll to a certain line
161 - (void)scrollToLine:(int)line
163 if ([[textView_ textStorage] length] == 0)
166 // go through the document until we find the NSRange for the line we want
168 for (int i = 0; i < line; i++)
170 rangeIndex = NSMaxRange([[textView_ string] lineRangeForRange:NSMakeRange(rangeIndex, 0)]);
173 // now get the true start/end markers for it
174 unsigned lineStart, lineEnd;
175 [[textView_ string] getLineStart:&lineStart end:NULL contentsEnd:&lineEnd forRange:NSMakeRange(rangeIndex - 1, 0)];
176 [textView_ scrollRangeToVisible:[[textView_ string] lineRangeForRange:NSMakeRange(lineStart, lineEnd - lineStart)]];
180 * Setup all the subviews for the source metaview
184 // Create the scroll view.
185 scrollView_ = [[[NSScrollView alloc] initWithFrame:[self bounds]] autorelease];
186 [scrollView_ setHasHorizontalScroller:YES];
187 [scrollView_ setHasVerticalScroller:YES];
188 [scrollView_ setAutohidesScrollers:YES];
189 [scrollView_ setBorderType:NSBezelBorder];
190 [scrollView_ setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
191 [[scrollView_ contentView] setAutoresizesSubviews:YES];
192 [self addSubview:scrollView_];
195 BSLineNumberRulerView* lineNumberView =
196 [[[BSLineNumberRulerView alloc] initWithScrollView:scrollView_] autorelease];
197 [scrollView_ setVerticalRulerView:lineNumberView];
198 [scrollView_ setHasHorizontalRuler:NO];
199 [scrollView_ setHasVerticalRuler:YES];
200 [scrollView_ setRulersVisible:YES];
202 // add the text view to the scroll view
204 textFrame.origin = NSMakePoint(0.0, 0.0);
205 textFrame.size = [scrollView_ contentSize];
206 textView_ = [[[NSTextView alloc] initWithFrame:textFrame] autorelease];
207 [textView_ setEditable:NO];
208 [textView_ setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
209 [textView_ setHorizontallyResizable:YES];
210 [textView_ setVerticallyResizable:YES];
211 [textView_ setMinSize:textFrame.size];
212 [textView_ setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
213 [[textView_ textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
214 [[textView_ textContainer] setWidthTracksTextView:NO];
215 [[textView_ textContainer] setHeightTracksTextView:NO];
216 [textView_ setAutoresizingMask:NSViewNotSizable];
217 [scrollView_ setDocumentView:textView_];
219 NSArray* types = [NSArray arrayWithObject:NSFilenamesPboardType];
220 [self registerForDraggedTypes:types];
224 * Gets the plain-text representation of the file at |filePath| and sets the
225 * contents in the source view.
227 - (void)setPlainTextStringFromFile:(NSString*)filePath
230 NSString* contents = [NSString stringWithContentsOfFile:filePath
231 encoding:NSUTF8StringEncoding
234 NSLog(@"Error reading file at %@: %@", filePath, error);
237 [textView_ setString:contents];
241 * Validates an initiated drag operation.
243 - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender
245 if ([delegate respondsToSelector:@selector(sourceView:acceptsDropOfFile:)])
246 return NSDragOperationCopy;
247 return NSDragOperationNone;
251 * Performs a dragging operation of files to set the contents of the file.
253 - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender
255 NSPasteboard* pboard = [sender draggingPasteboard];
256 if ([[pboard types] containsObject:NSFilenamesPboardType]) {
257 NSArray* files = [pboard propertyListForType:NSFilenamesPboardType];
259 NSString* filename = [files objectAtIndex:0];
260 if ([delegate respondsToSelector:@selector(sourceView:acceptsDropOfFile:)] &&
261 [delegate sourceView:self acceptsDropOfFile:filename]) {
262 [self setFile:filename];