Fix BSSourceView not re-drawing when the marked line or breakpoints change.
[macgdbp.git] / Source / BSSourceView.mm
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2011, Blue Static <http://www.bluestatic.org>
4 *
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.
8 *
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.
12 *
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
15 */
16
17 #import "BSSourceView.h"
18
19 #import "BSLineNumberRulerView.h"
20 #import "BSSourceViewTextView.h"
21
22 @interface BSSourceView (Private)
23 - (void)setupViews;
24 - (void)errorHighlightingFile:(NSNotification*)notif;
25 - (void)setPlainTextStringFromFile:(NSString*)filePath;
26 @end
27
28 @implementation BSSourceView
29
30 @synthesize textView = textView_;
31 @synthesize scrollView = scrollView_;
32 @synthesize markers = markers_;
33 @synthesize markedLine = markedLine_;
34 @synthesize delegate = delegate_;
35 @synthesize file = file_;
36
37 /**
38 * Initializes the source view with the path of a file
39 */
40 - (id)initWithFrame:(NSRect)frame
41 {
42 if (self = [super initWithFrame:frame]) {
43 [self setupViews];
44 [[NSNotificationCenter defaultCenter]
45 addObserver:self
46 selector:@selector(errorHighlightingFile:)
47 name:NSFileHandleReadToEndOfFileCompletionNotification
48 object:nil
49 ];
50 }
51 return self;
52 }
53
54 /**
55 * Dealloc
56 */
57 - (void)dealloc
58 {
59 [file_ release];
60
61 [scrollView_ removeFromSuperview];
62 [textView_ removeFromSuperview];
63
64 [super dealloc];
65 }
66
67 - (void)setMarkers:(NSSet*)markers {
68 [markers_ release];
69 markers_ = [markers copy];
70
71 [ruler_ setNeedsDisplay:YES];
72 }
73
74 - (void)setMarkedLine:(NSUInteger)markedLine {
75 markedLine_ = markedLine;
76 [ruler_ setNeedsDisplay:YES];
77 }
78
79 /**
80 * Sets the file name as well as the text of the source view
81 */
82 - (void)setFile:(NSString*)f
83 {
84 if (file_ != f) {
85 [file_ release];
86 file_ = [f retain];
87 }
88
89 if (![[NSFileManager defaultManager] fileExistsAtPath:f]) {
90 [textView_ setString:@""];
91 return;
92 }
93
94 @try {
95 // Attempt to use the PHP CLI to highlight the source file as HTML
96 NSPipe* outPipe = [NSPipe pipe];
97 NSPipe* errPipe = [NSPipe pipe];
98 NSTask* task = [[NSTask new] autorelease];
99
100 [task setLaunchPath:@"/usr/bin/php"]; // This is the path to the default Leopard PHP executable
101 [task setArguments:[NSArray arrayWithObjects:@"-s", f, nil]];
102 [task setStandardOutput:outPipe];
103 [task setStandardError:errPipe];
104 [task launch];
105
106 [[errPipe fileHandleForReading] readToEndOfFileInBackgroundAndNotify];
107
108 NSData* data = [[outPipe fileHandleForReading] readDataToEndOfFile];
109 NSAttributedString* source = [[NSAttributedString alloc] initWithHTML:data documentAttributes:NULL];
110 [[textView_ textStorage] setAttributedString:source];
111 [source release];
112 } @catch (NSException* exception) {
113 // If the PHP executable is not available then the NSTask will throw an exception
114 [self setPlainTextStringFromFile:f];
115 }
116
117 [ruler_ performLayout];
118 }
119
120 /**
121 * Sets the contents of the SourceView via a string rather than loading from a path
122 */
123 - (void)setString:(NSString*)source asFile:(NSString*)path
124 {
125 // create the temp file
126 NSError* error = nil;
127 NSString* tmpPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"MacGDBpHighlighter"];
128 [source writeToFile:tmpPath atomically:NO encoding:NSUTF8StringEncoding error:&error];
129 if (error) {
130 [textView_ setString:source];
131 return;
132 }
133
134 // highlight the temporary file
135 [self setFile:tmpPath];
136
137 // delete the temp file
138 [[NSFileManager defaultManager] removeItemAtPath:tmpPath error:NULL];
139
140 // plop in our fake path so nobody knows the difference
141 if (path != file_) {
142 [file_ release];
143 file_ = [path copy];
144 }
145
146 [ruler_ performLayout];
147 }
148
149 /**
150 * If an error occurs in reading the highlighted PHP source, this will merely set the string
151 */
152 - (void)errorHighlightingFile:(NSNotification*)notif
153 {
154 NSData* data = [[notif userInfo] objectForKey:NSFileHandleNotificationDataItem];
155 if ([data length] > 0 && file_) // there's something on stderr, so the PHP CLI failed
156 [self setPlainTextStringFromFile:file_];
157 }
158
159 /**
160 * Flip the coordinates
161 */
162 - (BOOL)isFlipped
163 {
164 return YES;
165 }
166
167 /**
168 * Tells the text view to scroll to a certain line
169 */
170 - (void)scrollToLine:(NSUInteger)line
171 {
172 if ([[textView_ textStorage] length] == 0)
173 return;
174
175 // go through the document until we find the NSRange for the line we want
176 NSUInteger rangeIndex = 0;
177 for (NSUInteger i = 0; i < line; i++) {
178 rangeIndex = NSMaxRange([[textView_ string] lineRangeForRange:NSMakeRange(rangeIndex, 0)]);
179 }
180
181 // now get the true start/end markers for it
182 NSUInteger lineStart, lineEnd;
183 [[textView_ string] getLineStart:&lineStart
184 end:NULL
185 contentsEnd:&lineEnd
186 forRange:NSMakeRange(rangeIndex - 1, 0)];
187 [textView_ scrollRangeToVisible:[[textView_ string]
188 lineRangeForRange:NSMakeRange(lineStart, lineEnd - lineStart)]];
189 [scrollView_ setNeedsDisplay:YES];
190 }
191
192 /**
193 * Setup all the subviews for the source metaview
194 */
195 - (void)setupViews
196 {
197 // Create the scroll view.
198 scrollView_ = [[[NSScrollView alloc] initWithFrame:[self bounds]] autorelease];
199 [scrollView_ setHasHorizontalScroller:YES];
200 [scrollView_ setHasVerticalScroller:YES];
201 [scrollView_ setAutohidesScrollers:YES];
202 [scrollView_ setBorderType:NSBezelBorder];
203 [scrollView_ setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
204 [[scrollView_ contentView] setAutoresizesSubviews:YES];
205 [self addSubview:scrollView_];
206
207 // add the text view to the scroll view
208 NSRect textFrame;
209 textFrame.origin = NSMakePoint(0.0, 0.0);
210 textFrame.size = [scrollView_ contentSize];
211 textView_ = [[[BSSourceViewTextView alloc] initWithFrame:textFrame] autorelease];
212 [textView_ setSourceView:self];
213 [textView_ setEditable:NO];
214 [textView_ setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
215 [textView_ setHorizontallyResizable:YES];
216 [textView_ setVerticallyResizable:YES];
217 [textView_ setMinSize:textFrame.size];
218 [textView_ setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
219 [[textView_ textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
220 [[textView_ textContainer] setWidthTracksTextView:NO];
221 [[textView_ textContainer] setHeightTracksTextView:NO];
222 [textView_ setAutoresizingMask:NSViewNotSizable];
223 [scrollView_ setDocumentView:textView_];
224
225 // Set up the ruler.
226 ruler_ = [[[BSLineNumberRulerView alloc] initWithSourceView:self] autorelease];
227 [scrollView_ setVerticalRulerView:ruler_];
228 [scrollView_ setHasHorizontalRuler:NO];
229 [scrollView_ setHasVerticalRuler:YES];
230 [scrollView_ setRulersVisible:YES];
231
232 NSArray* types = [NSArray arrayWithObject:NSFilenamesPboardType];
233 [self registerForDraggedTypes:types];
234 }
235
236 /**
237 * Gets the plain-text representation of the file at |filePath| and sets the
238 * contents in the source view.
239 */
240 - (void)setPlainTextStringFromFile:(NSString*)filePath
241 {
242 NSError* error = nil;
243 NSString* contents = [NSString stringWithContentsOfFile:filePath
244 encoding:NSUTF8StringEncoding
245 error:&error];
246 if (error) {
247 NSLog(@"Error reading file at %@: %@", filePath, error);
248 return;
249 }
250 [textView_ setString:contents];
251 }
252
253 // Drag Handlers ///////////////////////////////////////////////////////////////
254
255 /**
256 * Validates an initiated drag operation.
257 */
258 - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender
259 {
260 if ([delegate_ respondsToSelector:@selector(sourceView:acceptsDropOfFile:)])
261 return NSDragOperationCopy;
262 return NSDragOperationNone;
263 }
264
265 /**
266 * Performs a dragging operation of files to set the contents of the file.
267 */
268 - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender
269 {
270 NSPasteboard* pboard = [sender draggingPasteboard];
271 if ([[pboard types] containsObject:NSFilenamesPboardType]) {
272 NSArray* files = [pboard propertyListForType:NSFilenamesPboardType];
273 if ([files count]) {
274 NSString* filename = [files objectAtIndex:0];
275 if ([delegate_ respondsToSelector:@selector(sourceView:acceptsDropOfFile:)] &&
276 [delegate_ sourceView:self acceptsDropOfFile:filename]) {
277 [self setFile:filename];
278 return YES;
279 }
280 }
281 }
282 return NO;
283 }
284
285 @end