Moving the depth checker from NSXMLElementAdditions.m to be in DebuggerWindowController
[macgdbp.git] / Source / DebuggerWindowController.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2002 - 2007, 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 "DebuggerWindowController.h"
18 #import "DebuggerConnection.h"
19 #import "NSXMLElementAdditions.h"
20
21 @interface DebuggerWindowController (Private)
22
23 - (void)updateSourceViewer;
24
25 @end
26
27 @implementation DebuggerWindowController
28
29 /**
30 * Initializes the window controller and sets the connection
31 */
32 - (id)initWithConnection: (DebuggerConnection *)cnx
33 {
34 if (self = [super initWithWindowNibName: @"Debugger"])
35 {
36 _connection = [cnx retain];
37 }
38 return self;
39 }
40
41 /**
42 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
43 */
44 - (void)awakeFromNib
45 {
46 // set up the scroller for the source viewer
47 [_sourceViewer setMaxSize: NSMakeSize(FLT_MAX, FLT_MAX)];
48 [[_sourceViewer textContainer] setContainerSize: NSMakeSize(FLT_MAX, FLT_MAX)];
49 [[_sourceViewer textContainer] setWidthTracksTextView: NO];
50 [_sourceViewer setHorizontallyResizable: YES];
51 [_sourceViewerScroller setHasHorizontalScroller: YES];
52 [_sourceViewerScroller display];
53 }
54
55 /**
56 * Release object members
57 */
58 - (void)dealloc
59 {
60 [_connection release];
61
62 [super dealloc];
63 }
64
65 /**
66 * Sets the status and clears any error message
67 */
68 - (void)setStatus: (NSString *)status
69 {
70 [_error setHidden: YES];
71 [_status setStringValue: status];
72 [[self window] setTitle: [NSString stringWithFormat: @"GDBp @ %@:%d/%@", [_connection remoteHost], [_connection port], [_connection session]]];
73 }
74
75 /**
76 * Sets the status to be "Error" and then displays the error message
77 */
78 - (void)setError: (NSString *)error
79 {
80 [_error setStringValue: error];
81 [self setStatus: @"Error"];
82 [_error setHidden: NO];
83 }
84
85 /**
86 * Sets the root node element of the stacktrace
87 */
88 - (void)setStack: (NSArray *)stack
89 {
90 if (_stack != nil)
91 {
92 [_stack release];
93 }
94
95 _stack = stack;
96 [_stack retain];
97 [self updateSourceViewer];
98 }
99
100 /**
101 * Sets the stack root element so that the NSOutlineView can display it
102 */
103 - (void)setRegister: (NSXMLElement *)elm
104 {
105 if (_register != nil)
106 {
107 [_register release];
108 }
109
110 _register = elm;
111 [_register retain];
112 }
113
114 /**
115 * Forwards the message to run script execution to the connection
116 */
117 - (IBAction)run: (id)sender
118 {
119 [_connection run];
120 }
121
122 /**
123 * Forwards the message to "step in" to the connection
124 */
125 - (IBAction)stepIn: (id)sender
126 {
127 [_connection stepIn];
128 }
129
130 /**
131 * Forwards the message to "step out" to the connection
132 */
133 - (IBAction)stepOut: (id)sender
134 {
135 [_connection stepOut];
136 }
137
138 /**
139 * Forwards the message to "step over" to the connection
140 */
141 - (IBAction)stepOver: (id)sender
142 {
143 [_connection stepOver];
144 }
145
146 /**
147 * NSTableView delegate method that informs the controller that the stack selection did change and that
148 * we should update the source viewer
149 */
150 - (void)tableViewSelectionDidChange: (NSNotification *)notif
151 {
152 NSLog(@"selection changed");
153 [self updateSourceViewer];
154 }
155 /**
156 * Does the actual updating of the source viewer by reading in the file
157 */
158 - (void)updateSourceViewer
159 {
160 int selection = [_stackController selectionIndex];
161 if (selection == NSNotFound)
162 {
163 [_sourceViewer setString: @""];
164 return;
165 }
166
167 // get the filename and then set the text
168 NSString *filename = [[_stack objectAtIndex: selection] valueForKey: @"filename"];
169 filename = [[NSURL URLWithString: filename] path];
170 NSString *text = [NSString stringWithContentsOfFile: filename];
171 [_sourceViewer setString: text];
172
173 // go through the document until we find the NSRange for the line we want
174 int destination = [[[_stack objectAtIndex: selection] valueForKey: @"lineno"] intValue];
175 int rangeIndex = 0;
176 for (int line = 0; line < destination; line++)
177 {
178 rangeIndex = NSMaxRange([text lineRangeForRange: NSMakeRange(rangeIndex, 0)]);
179 }
180
181 // now get the true start/end markers for it
182 unsigned lineStart, lineEnd;
183 [text getLineStart: &lineStart end: NULL contentsEnd: &lineEnd forRange: NSMakeRange(rangeIndex - 1, 0)];
184 NSRange lineRange = NSMakeRange(lineStart, lineEnd - lineStart);
185
186 // colorize it so the user knows which line we're on in the stack
187 [[_sourceViewer textStorage] setAttributes: [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: [NSColor redColor], [NSColor yellowColor], nil]
188 forKeys: [NSArray arrayWithObjects: NSForegroundColorAttributeName, NSBackgroundColorAttributeName, nil]]
189 range: lineRange];
190 [_sourceViewer scrollRangeToVisible: [text lineRangeForRange: NSMakeRange(lineStart, lineEnd - lineStart)]];
191
192 // make sure the font stays Monaco
193 [_sourceViewer setFont: [NSFont fontWithName: @"Monaco" size: 10.0]];
194 }
195
196 /**
197 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
198 */
199 - (void)outlineViewItemDidExpand: (NSNotification *)notif
200 {
201 // XXX: This very well may break because NSTreeController sends us a _NSArrayControllerTreeNode object
202 // which is presumably private, and thus this is not a reliable method for getting the object. But
203 // we damn well need it, so f!ck the rules and we're using it.
204 NSXMLElement *obj = [[[notif userInfo] objectForKey: @"NSObject"] observedObject];
205
206 // hmm... we're not a leaf but have no children. this must be beyond our depth, so go make us
207 // deeper
208 if (![obj isLeaf] && [[obj children] count] < 1)
209 {
210 // count upwards to see how deep we should go
211 int depth = 0;
212 NSXMLElement *elm = obj;
213 while (elm != nil)
214 {
215 depth++;
216 elm = (NSXMLElement *)[elm parent];
217 }
218 NSLog(@"let's go to depth %d for %@", depth, obj);
219 }
220 }
221
222 @end