Merge branch 'bug-125'
[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 @end
22
23 @implementation BSSourceView
24
25 @synthesize numberView, textView, scrollView, markedLine, delegate, file;
26
27 /**
28 * Initializes the source view with the path of a file
29 */
30 - (id)initWithFrame:(NSRect)frame
31 {
32 if (self = [super initWithFrame:frame])
33 {
34 [self setupViews];
35 }
36 return self;
37 }
38
39 /**
40 * Dealloc
41 */
42 - (void)dealloc
43 {
44 if (file != nil)
45 {
46 [file release];
47 }
48 [super dealloc];
49 }
50
51 /**
52 * Sets the file name as well as the text of the source view
53 */
54 - (void)setFile:(NSString *)f
55 {
56 [file release];
57 file = [f retain];
58
59 @try
60 {
61 // Attempt to use the PHP CLI to highlight the source file as HTML
62 NSPipe* pipe = [NSPipe pipe];
63 NSTask* task = [NSTask new];
64 [task setLaunchPath:@"/usr/bin/php"]; // This is the path to the default Leopard PHP executable
65 [task setArguments:[NSArray arrayWithObjects:@"-s", f, nil]];
66 [task setStandardOutput:pipe];
67 [task launch];
68 NSData* data = [[pipe fileHandleForReading] readDataToEndOfFile];
69 NSAttributedString* source = [[NSAttributedString alloc] initWithHTML:data documentAttributes:NULL];
70 [[textView textStorage] setAttributedString:source];
71 }
72 @catch (NSException* exception)
73 {
74 // If the PHP executable is not available then the NSTask will throw an exception
75 [textView setString:[NSString stringWithContentsOfFile:f]];
76 }
77 }
78
79 /**
80 * Flip the coordinates
81 */
82 - (BOOL)isFlipped
83 {
84 return YES;
85 }
86
87 /**
88 * Tells the text view to scroll to a certain line
89 */
90 - (void)scrollToLine:(int)line
91 {
92 // go through the document until we find the NSRange for the line we want
93 int rangeIndex = 0;
94 for (int i = 0; i < line; i++)
95 {
96 rangeIndex = NSMaxRange([[textView string] lineRangeForRange:NSMakeRange(rangeIndex, 0)]);
97 }
98
99 // now get the true start/end markers for it
100 unsigned lineStart, lineEnd;
101 [[textView string] getLineStart:&lineStart end:NULL contentsEnd:&lineEnd forRange:NSMakeRange(rangeIndex - 1, 0)];
102 [textView scrollRangeToVisible:[[textView string] lineRangeForRange:NSMakeRange(lineStart, lineEnd - lineStart)]];
103 }
104
105 /**
106 * Setup all the subviews for the source metaview
107 */
108 - (void)setupViews
109 {
110 int gutterWidth = 30;
111
112 // setup the line number view
113 NSRect numberFrame = [self bounds];
114 numberFrame.origin = NSMakePoint(0.0, 0.0);
115 numberFrame.size.width = gutterWidth;
116 numberView = [[BSLineNumberView alloc] initWithFrame:numberFrame];
117 [numberView setAutoresizingMask:NSViewHeightSizable];
118 [numberView setSourceView:self];
119 [self addSubview:numberView];
120
121 // create the scroll view
122 NSRect scrollFrame = [self bounds];
123 scrollFrame.origin.x = gutterWidth;
124 scrollFrame.size.width = scrollFrame.size.width - gutterWidth;
125 scrollView = [[NSScrollView alloc] initWithFrame:scrollFrame];
126 [scrollView setHasHorizontalScroller:YES];
127 [scrollView setHasVerticalScroller:YES];
128 [scrollView setAutohidesScrollers:YES];
129 [scrollView setBorderType:NSBezelBorder];
130 [scrollView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
131 [[scrollView contentView] setAutoresizesSubviews:YES];
132 [self addSubview:scrollView];
133
134 // add the text view to the scroll view
135 NSRect textFrame;
136 textFrame.origin = NSMakePoint(0.0, 0.0);
137 textFrame.size = [scrollView contentSize];
138 textView = [[BSSourceViewTextView alloc] initWithFrame:textFrame];
139 [textView setSourceView:self];
140 [textView setEditable:NO];
141 [textView setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
142 [textView setHorizontallyResizable:YES];
143 [textView setVerticallyResizable:YES];
144 [textView setMinSize:textFrame.size];
145 [textView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
146 [[textView textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
147 [[textView textContainer] setWidthTracksTextView:NO];
148 [[textView textContainer] setHeightTracksTextView:NO];
149 [textView setAutoresizingMask:NSViewNotSizable];
150 [scrollView setDocumentView:textView];
151 }
152
153 @end