Trying to use mutableChildNodes and appending mock-NSTreeNode objects
[macgdbp.git] / Source / DebuggerWindowController.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 "DebuggerWindowController.h"
18 #import "DebuggerConnection.h"
19 #import "NSXMLElementAdditions.h"
20 #import "AppDelegate.h"
21 #import "BreakpointManager.h"
22
23 @interface DebuggerWindowController (Private)
24
25 - (void)updateSourceViewer;
26
27 @end
28
29 @implementation DebuggerWindowController
30
31 @synthesize connection;
32
33 /**
34 * Initializes the window controller and sets the connection
35 */
36 - (id)initWithPort:(int)aPort session:(NSString *)aSession
37 {
38 if (self = [super initWithWindowNibName:@"Debugger"])
39 {
40 connection = [[DebuggerConnection alloc] initWithWindowController:self port:aPort session:aSession];
41 expandedRegisters = [[NSMutableSet alloc] init];
42 [[self window] makeKeyAndOrderFront:nil];
43 }
44 return self;
45 }
46
47 /**
48 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
49 */
50 - (void)awakeFromNib
51 {
52 [self setStatus:@"Connecting"];
53 [[self window] setExcludedFromWindowsMenu:YES];
54 [sourceViewer setDelegate:self];
55 }
56
57 /**
58 * Called right before the window closes so that we can tell the socket to close down
59 */
60 - (void)windowWillClose:(NSNotification *)notif
61 {
62 [[connection socket] close];
63 }
64
65 /**
66 * Resets all the displays to be empty
67 */
68 - (void)resetDisplays
69 {
70 [registerController setContent:nil];
71 [stackController setContent:nil];
72 [[sourceViewer textView] setString:@""];
73 }
74
75 /**
76 * Sets the status and clears any error message
77 */
78 - (void)setStatus:(NSString *)aStatus
79 {
80 [errormsg setHidden:YES];
81 [statusmsg setStringValue:aStatus];
82 [[self window] setTitle:[NSString stringWithFormat:@"GDBp @ %@:%d/%@", [connection remoteHost], [connection port], [connection session]]];
83
84 [stepInButton setEnabled:NO];
85 [stepOutButton setEnabled:NO];
86 [stepOverButton setEnabled:NO];
87 [runButton setEnabled:NO];
88 [reconnectButton setEnabled:NO];
89
90 if ([connection isConnected])
91 {
92 if ([aStatus isEqualToString:@"Starting"])
93 {
94 [stepInButton setEnabled:YES];
95 [runButton setEnabled:YES];
96 }
97 }
98 else
99 {
100 [reconnectButton setEnabled:YES];
101 }
102 }
103
104 /**
105 * Sets the status to be "Error" and then displays the error message
106 */
107 - (void)setError:(NSString *)anError
108 {
109 [errormsg setStringValue:anError];
110 [self setStatus:@"Error"];
111 [errormsg setHidden:NO];
112 }
113
114 /**
115 * Sets the root node element of the stacktrace
116 */
117 - (void)setStack:(NSArray *)node
118 {
119 stack = node;
120
121 if ([stack count] > 1)
122 {
123 [stepOutButton setEnabled:YES];
124 }
125 [stepInButton setEnabled:YES];
126 [stepOverButton setEnabled:YES];
127 [runButton setEnabled:YES];
128
129 [self updateSourceViewer];
130 }
131
132 /**
133 * Sets the stack root element so that the NSOutlineView can display it
134 */
135 - (void)setRegister:(NSXMLDocument *)elm
136 {
137 // XXX: Doing anything short of this will cause bindings to crash spectacularly for no reason whatsoever, and
138 // in seemingly arbitrary places. The class that crashes is _NSKeyValueObservationInfoCreateByRemoving.
139 // http://boredzo.org/blog/archives/2006-01-29/have-you-seen-this-crash says that this means nothing is
140 // being observed, but I doubt that he was using an NSOutlineView which seems to be one f!cking piece of
141 // sh!t when used with NSTreeController. http://www.cocoadev.com/index.pl?NSTreeControllerBugOrDeveloperError
142 // was the inspiration for this fix (below) but the author says that inserting does not work too well, but
143 // that's okay for us as we just need to replace the entire thing.
144 [registerController setContent:nil];
145 [registerController setContent:[[elm rootElement] children]];
146
147 for (int i = 0; i < [registerView numberOfRows]; i++)
148 {
149 NSTreeNode *node = [registerView itemAtRow:i];
150 if ([expandedRegisters containsObject:[[node representedObject] fullname]])
151 {
152 [registerView expandItem:node];
153 }
154 }
155 }
156
157 /**
158 * Forwards the message to run script execution to the connection
159 */
160 - (IBAction)run:(id)sender
161 {
162 [connection run];
163 }
164
165 /**
166 * Tells the connection to ask the server to reconnect
167 */
168 - (IBAction)reconnect:(id)sender
169 {
170 [connection reconnect];
171 }
172
173 /**
174 * Forwards the message to "step in" to the connection
175 */
176 - (IBAction)stepIn:(id)sender
177 {
178 [connection stepIn];
179 }
180
181 /**
182 * Forwards the message to "step out" to the connection
183 */
184 - (IBAction)stepOut:(id)sender
185 {
186 [connection stepOut];
187 }
188
189 /**
190 * Forwards the message to "step over" to the connection
191 */
192 - (IBAction)stepOver:(id)sender
193 {
194 [connection stepOver];
195 }
196
197 /**
198 * NSTableView delegate method that informs the controller that the stack selection did change and that
199 * we should update the source viewer
200 */
201 - (void)tableViewSelectionDidChange:(NSNotification *)notif
202 {
203 [self updateSourceViewer];
204 }
205
206 /**
207 * Does the actual updating of the source viewer by reading in the file
208 */
209 - (void)updateSourceViewer
210 {
211 id selectedLevel = [[stackController selection] valueForKey:@"level"];
212 if (selectedLevel == NSNoSelectionMarker)
213 {
214 [[sourceViewer textView] setString:@""];
215 return;
216 }
217 int selection = [selectedLevel intValue];
218
219 if ([stack count] < 1)
220 {
221 NSLog(@"huh... we don't have a stack");
222 return;
223 }
224
225 // get the filename and then set the text
226 NSString *filename = [[stack objectAtIndex:selection] valueForKey:@"filename"];
227 filename = [[NSURL URLWithString:filename] path];
228 if ([filename isEqualToString:@""])
229 {
230 return;
231 }
232
233 [sourceViewer setFile:filename];
234
235 int line = [[[stack objectAtIndex:selection] valueForKey:@"lineno"] intValue];
236 [sourceViewer setMarkedLine:line];
237 [sourceViewer scrollToLine:line];
238
239 // make sure the font stays Monaco
240 //[sourceViewer setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
241 }
242
243 /**
244 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
245 */
246 - (void)outlineViewItemDidExpand:(NSNotification *)notif
247 {
248 NSTreeNode *node = [[notif userInfo] objectForKey:@"NSObject"];
249
250 // we're not a leaf but have no children. this must be beyond our depth, so go make us deeper
251 if (![node isLeaf] && [[node childNodes] count] < 1)
252 {
253 [connection getProperty:[[node representedObject] fullname] forNode:node];
254 }
255
256 [expandedRegisters addObject:[[node representedObject] fullname]];
257 }
258
259 /**
260 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
261 */
262 - (void)outlineViewItemDidCollapse:(NSNotification *)notif
263 {
264 [expandedRegisters removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullname]];
265 }
266
267 /**
268 * Updates the register view by reinserting a given node back into the outline view
269 */
270 - (void)addChildren:(NSArray *)children toNode:(NSTreeNode *)node
271 {
272 NSMutableArray *childNodes = [node mutableChildNodes];
273 [childNodes removeAllObjects];
274
275 for (NSXMLNode *child in children)
276 {
277 NSTreeNode *newChild = [[NSTreeNode alloc] initWithRepresentedObject:child];
278 [childNodes addObject:newChild];
279 [newChild release];
280 }
281
282 [registerController rearrangeObjects];
283 }
284
285 #pragma mark BSSourceView Delegate
286
287 /**
288 * The gutter was clicked, which indicates that a breakpoint needs to be changed
289 */
290 - (void)gutterClickedAtLine:(int)line forFile:(NSString *)file
291 {
292 BreakpointManager *mngr = [BreakpointManager sharedManager];
293
294 if ([mngr hasBreakpointAt:line inFile:file])
295 {
296 [mngr removeBreakpointAt:line inFile:file];
297 }
298 else
299 {
300 Breakpoint *bp = [[Breakpoint alloc] initWithLine:line inFile:file];
301 [mngr addBreakpoint:bp];
302 }
303
304 [[sourceViewer numberView] setMarkers:[NSSet setWithArray:[mngr breakpointsForFile:file]]];
305 [[sourceViewer numberView] setNeedsDisplay:YES];
306 }
307
308 @end