Check and see if stderr has any data on it when syntax highlighting. If so, only...
[macgdbp.git] / Source / BSSourceView.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2008, 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 @interface BSSourceView (Private)
20 - (void)setupViews;
21 - (void)errorHighlightingFile:(NSNotification *)notif;
22 @end
23
24 @implementation BSSourceView
25
26 @synthesize numberView, textView, scrollView, markedLine, delegate, file;
27
28 /**
29 * Initializes the source view with the path of a file
30 */
31 - (id)initWithFrame:(NSRect)frame
32 {
33 if (self = [super initWithFrame:frame])
34 {
35 [self setupViews];
36 [[NSNotificationCenter defaultCenter] addObserver:self
37 selector:@selector(errorHighlightingFile:)
38 name:NSFileHandleReadToEndOfFileCompletionNotification
39 object:nil];
40 }
41 return self;
42 }
43
44 /**
45 * Dealloc
46 */
47 - (void)dealloc
48 {
49 [file release];
50
51 [numberView removeFromSuperview];
52 [scrollView removeFromSuperview];
53 [textView removeFromSuperview];
54
55 [super dealloc];
56 }
57
58 /**
59 * Sets the file name as well as the text of the source view
60 */
61 - (void)setFile:(NSString *)f
62 {
63 if (file != f)
64 {
65 [file release];
66 file = [f retain];
67 }
68
69 @try
70 {
71 // Attempt to use the PHP CLI to highlight the source file as HTML
72 NSPipe *outPipe = [NSPipe pipe];
73 NSPipe *errPipe = [NSPipe pipe];
74 NSTask *task = [[NSTask new] autorelease];
75
76 [task setLaunchPath:@"/usr/bin/php"]; // This is the path to the default Leopard PHP executable
77 [task setArguments:[NSArray arrayWithObjects:@"-s", f, nil]];
78 [task setStandardOutput:outPipe];
79 [task setStandardError:errPipe];
80 [task launch];
81
82 [[errPipe fileHandleForReading] readToEndOfFileInBackgroundAndNotify];
83
84 NSData *data = [[outPipe fileHandleForReading] readDataToEndOfFile];
85 NSAttributedString *source = [[NSAttributedString alloc] initWithHTML:data documentAttributes:NULL];
86 [[textView textStorage] setAttributedString:source];
87 [source release];
88 }
89 @catch (NSException *exception)
90 {
91 // If the PHP executable is not available then the NSTask will throw an exception
92 [textView setString:[NSString stringWithContentsOfFile:f]];
93 }
94 }
95
96 /**
97 * If an error occurs in reading the highlighted PHP source, this will merely set the string
98 */
99 - (void)errorHighlightingFile:(NSNotification *)notif
100 {
101 NSData *data = [[notif userInfo] objectForKey:NSFileHandleNotificationDataItem];
102 if ([data length] > 0) // there's something on stderr, so the PHP CLI failed
103 [textView setString:[NSString stringWithContentsOfFile:file]];
104 }
105
106 /**
107 * Flip the coordinates
108 */
109 - (BOOL)isFlipped
110 {
111 return YES;
112 }
113
114 /**
115 * Tells the text view to scroll to a certain line
116 */
117 - (void)scrollToLine:(int)line
118 {
119 // go through the document until we find the NSRange for the line we want
120 int rangeIndex = 0;
121 for (int i = 0; i < line; i++)
122 {
123 rangeIndex = NSMaxRange([[textView string] lineRangeForRange:NSMakeRange(rangeIndex, 0)]);
124 }
125
126 // now get the true start/end markers for it
127 unsigned lineStart, lineEnd;
128 [[textView string] getLineStart:&lineStart end:NULL contentsEnd:&lineEnd forRange:NSMakeRange(rangeIndex - 1, 0)];
129 [textView scrollRangeToVisible:[[textView string] lineRangeForRange:NSMakeRange(lineStart, lineEnd - lineStart)]];
130 }
131
132 /**
133 * Setup all the subviews for the source metaview
134 */
135 - (void)setupViews
136 {
137 int gutterWidth = 30;
138
139 // setup the line number view
140 NSRect numberFrame = [self bounds];
141 numberFrame.origin = NSMakePoint(0.0, 0.0);
142 numberFrame.size.width = gutterWidth;
143 numberView = [[BSLineNumberView alloc] initWithFrame:numberFrame];
144 [numberView setAutoresizingMask:NSViewHeightSizable];
145 [numberView setSourceView:self];
146 [self addSubview:numberView];
147
148 // create the scroll view
149 NSRect scrollFrame = [self bounds];
150 scrollFrame.origin.x = gutterWidth;
151 scrollFrame.size.width = scrollFrame.size.width - gutterWidth;
152 scrollView = [[NSScrollView alloc] initWithFrame:scrollFrame];
153 [scrollView setHasHorizontalScroller:YES];
154 [scrollView setHasVerticalScroller:YES];
155 [scrollView setAutohidesScrollers:YES];
156 [scrollView setBorderType:NSBezelBorder];
157 [scrollView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
158 [[scrollView contentView] setAutoresizesSubviews:YES];
159 [self addSubview:scrollView];
160
161 // add the text view to the scroll view
162 NSRect textFrame;
163 textFrame.origin = NSMakePoint(0.0, 0.0);
164 textFrame.size = [scrollView contentSize];
165 textView = [[BSSourceViewTextView alloc] initWithFrame:textFrame];
166 [textView setSourceView:self];
167 [textView setEditable:NO];
168 [textView setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
169 [textView setHorizontallyResizable:YES];
170 [textView setVerticallyResizable:YES];
171 [textView setMinSize:textFrame.size];
172 [textView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
173 [[textView textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
174 [[textView textContainer] setWidthTracksTextView:NO];
175 [[textView textContainer] setHeightTracksTextView:NO];
176 [textView setAutoresizingMask:NSViewNotSizable];
177 [scrollView setDocumentView:textView];
178 }
179
180 @end