Turning off GC and adding back manual memory management
[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 }
49
50 /**
51 * Sets the file name as well as the text of the source view
52 */
53 - (void)setFile:(NSString *)f
54 {
55 [file release];
56 file = [f retain];
57 [textView setString:[NSString stringWithContentsOfFile:f]];
58 }
59
60 /**
61 * Flip the coordinates
62 */
63 - (BOOL)isFlipped
64 {
65 return YES;
66 }
67
68 /**
69 * Tells the text view to scroll to a certain line
70 */
71 - (void)scrollToLine:(int)line
72 {
73 // go through the document until we find the NSRange for the line we want
74 int rangeIndex = 0;
75 for (int i = 0; i < line; i++)
76 {
77 rangeIndex = NSMaxRange([[textView string] lineRangeForRange:NSMakeRange(rangeIndex, 0)]);
78 }
79
80 // now get the true start/end markers for it
81 unsigned lineStart, lineEnd;
82 [[textView string] getLineStart:&lineStart end:NULL contentsEnd:&lineEnd forRange:NSMakeRange(rangeIndex - 1, 0)];
83 [textView scrollRangeToVisible:[[textView string] lineRangeForRange:NSMakeRange(lineStart, lineEnd - lineStart)]];
84 }
85
86 /**
87 * Setup all the subviews for the source metaview
88 */
89 - (void)setupViews
90 {
91 int gutterWidth = 30;
92
93 // setup the line number view
94 NSRect numberFrame = [self bounds];
95 numberFrame.origin = NSMakePoint(0.0, 0.0);
96 numberFrame.size.width = gutterWidth;
97 numberView = [[BSLineNumberView alloc] initWithFrame:numberFrame];
98 [numberView setAutoresizingMask:NSViewHeightSizable];
99 [numberView setSourceView:self];
100 [self addSubview:numberView];
101
102 // create the scroll view
103 NSRect scrollFrame = [self bounds];
104 scrollFrame.origin.x = gutterWidth;
105 scrollFrame.size.width = scrollFrame.size.width - gutterWidth;
106 scrollView = [[NSScrollView alloc] initWithFrame:scrollFrame];
107 [scrollView setHasHorizontalScroller:YES];
108 [scrollView setHasVerticalScroller:YES];
109 [scrollView setBorderType:NSBezelBorder];
110 [scrollView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
111 [[scrollView contentView] setAutoresizesSubviews:YES];
112 [self addSubview:scrollView];
113
114 // add the text view to the scroll view
115 NSRect textFrame;
116 textFrame.origin = NSMakePoint(0.0, 0.0);
117 textFrame.size = [scrollView contentSize];
118 textView = [[BSSourceViewTextView alloc] initWithFrame:textFrame];
119 [textView setSourceView:self];
120 [textView setEditable:NO];
121 [textView setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
122 [textView setHorizontallyResizable:YES];
123 [textView setVerticallyResizable:YES];
124 [textView setMinSize:textFrame.size];
125 [textView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
126 [[textView textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
127 [[textView textContainer] setWidthTracksTextView:NO];
128 [[textView textContainer] setHeightTracksTextView:NO];
129 [textView setAutoresizingMask:NSViewNotSizable];
130 [scrollView setDocumentView:textView];
131 }
132
133 @end