We now remember which items in the register have been expanded so they stay open
[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 _expandedRegisters = [[NSMutableArray alloc] init];
38 }
39 return self;
40 }
41
42 /**
43 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
44 */
45 - (void)awakeFromNib
46 {
47 // set up the scroller for the source viewer
48 [_sourceViewer setMaxSize: NSMakeSize(FLT_MAX, FLT_MAX)];
49 [[_sourceViewer textContainer] setContainerSize: NSMakeSize(FLT_MAX, FLT_MAX)];
50 [[_sourceViewer textContainer] setWidthTracksTextView: NO];
51 [_sourceViewer setHorizontallyResizable: YES];
52 [_sourceViewerScroller setHasHorizontalScroller: YES];
53 [_sourceViewerScroller display];
54 }
55
56 /**
57 * Release object members
58 */
59 - (void)dealloc
60 {
61 [_connection release];
62 [_expandedRegisters release];
63
64 [super dealloc];
65 }
66
67 /**
68 * Sets the status and clears any error message
69 */
70 - (void)setStatus: (NSString *)status
71 {
72 [_error setHidden: YES];
73 [_status setStringValue: status];
74 [[self window] setTitle: [NSString stringWithFormat: @"GDBp @ %@:%d/%@", [_connection remoteHost], [_connection port], [_connection session]]];
75
76 [_stepInButton setEnabled: NO];
77 [_stepOutButton setEnabled: NO];
78 [_stepOverButton setEnabled: NO];
79 [_runButton setEnabled: NO];
80
81 if ([_connection isConnected])
82 {
83 if ([status isEqualToString: @"Starting"])
84 {
85 [_stepInButton setEnabled: YES];
86 [_runButton setEnabled: YES];
87 }
88 }
89 }
90
91 /**
92 * Sets the status to be "Error" and then displays the error message
93 */
94 - (void)setError: (NSString *)error
95 {
96 [_error setStringValue: error];
97 [self setStatus: @"Error"];
98 [_error setHidden: NO];
99 }
100
101 /**
102 * Sets the root node element of the stacktrace
103 */
104 - (void)setStack: (NSArray *)stack
105 {
106 if (_stack != nil)
107 {
108 [_stack release];
109 }
110
111 _stack = stack;
112 [_stack retain];
113
114 if ([_stack count] > 1)
115 {
116 [_stepOutButton setEnabled: YES];
117 }
118 [_stepInButton setEnabled: YES];
119 [_stepOverButton setEnabled: YES];
120 [_runButton setEnabled: YES];
121
122 [self updateSourceViewer];
123 }
124
125 /**
126 * Sets the stack root element so that the NSOutlineView can display it
127 */
128 - (void)setRegister: (NSXMLDocument *)elm
129 {
130 /*
131 [_registerController willChangeValueForKey: @"rootElement.children"];
132 [_registerController unbind: @"contentArray"];
133 [_registerController bind: @"contentArray" toObject: elm withKeyPath: @"rootElement.children" options: nil];
134 [_registerController didChangeValueForKey: @"rootElement.children"];
135 */
136 // XXX: Doing anything short of this will cause bindings to crash spectacularly for no reason whatsoever, and
137 // in seemingly arbitrary places. The class that crashes is _NSKeyValueObservationInfoCreateByRemoving.
138 // http://boredzo.org/blog/archives/2006-01-29/have-you-seen-this-crash says that this means nothing is
139 // being observed, but I doubt that he was using an NSOutlineView which seems to be one f!cking piece of
140 // sh!t when used with NSTreeController. http://www.cocoadev.com/index.pl?NSTreeControllerBugOrDeveloperError
141 // was the inspiration for this fix (below) but the author says that inserting does not work too well, but
142 // that's okay for us as we just need to replace the entire thing.
143 [_registerController setContent: nil];
144 [_registerController setContent: [[elm rootElement] children]];
145
146 for (int i = 0; i < [_registerView numberOfRows]; i++)
147 {
148 int index = [_expandedRegisters indexOfObject: [[[_registerView itemAtRow: i] observedObject] variable]];
149 if (index != NSNotFound)
150 {
151 [_registerView expandItem: [_registerView itemAtRow: i]];
152 }
153 }
154 }
155
156 /**
157 * Forwards the message to run script execution to the connection
158 */
159 - (IBAction)run: (id)sender
160 {
161 [_connection run];
162 }
163
164 /**
165 * Forwards the message to "step in" to the connection
166 */
167 - (IBAction)stepIn: (id)sender
168 {
169 [_connection stepIn];
170 }
171
172 /**
173 * Forwards the message to "step out" to the connection
174 */
175 - (IBAction)stepOut: (id)sender
176 {
177 [_connection stepOut];
178 }
179
180 /**
181 * Forwards the message to "step over" to the connection
182 */
183 - (IBAction)stepOver: (id)sender
184 {
185 [_connection stepOver];
186 }
187
188 /**
189 * NSTableView delegate method that informs the controller that the stack selection did change and that
190 * we should update the source viewer
191 */
192 - (void)tableViewSelectionDidChange: (NSNotification *)notif
193 {
194 NSLog(@"selection changed");
195 [self updateSourceViewer];
196 }
197 /**
198 * Does the actual updating of the source viewer by reading in the file
199 */
200 - (void)updateSourceViewer
201 {
202 int selection = [_stackController selectionIndex];
203 if (selection == NSNotFound)
204 {
205 [_sourceViewer setString: @""];
206 return;
207 }
208
209 // get the filename and then set the text
210 NSString *filename = [[_stack objectAtIndex: selection] valueForKey: @"filename"];
211 filename = [[NSURL URLWithString: filename] path];
212 NSString *text = [NSString stringWithContentsOfFile: filename];
213 [_sourceViewer setString: text];
214
215 // go through the document until we find the NSRange for the line we want
216 int destination = [[[_stack objectAtIndex: selection] valueForKey: @"lineno"] intValue];
217 int rangeIndex = 0;
218 for (int line = 0; line < destination; line++)
219 {
220 rangeIndex = NSMaxRange([text lineRangeForRange: NSMakeRange(rangeIndex, 0)]);
221 }
222
223 // now get the true start/end markers for it
224 unsigned lineStart, lineEnd;
225 [text getLineStart: &lineStart end: NULL contentsEnd: &lineEnd forRange: NSMakeRange(rangeIndex - 1, 0)];
226 NSRange lineRange = NSMakeRange(lineStart, lineEnd - lineStart);
227
228 // colorize it so the user knows which line we're on in the stack
229 [[_sourceViewer textStorage] setAttributes: [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: [NSColor redColor], [NSColor yellowColor], nil]
230 forKeys: [NSArray arrayWithObjects: NSForegroundColorAttributeName, NSBackgroundColorAttributeName, nil]]
231 range: lineRange];
232 [_sourceViewer scrollRangeToVisible: [text lineRangeForRange: NSMakeRange(lineStart, lineEnd - lineStart)]];
233
234 // make sure the font stays Monaco
235 [_sourceViewer setFont: [NSFont fontWithName: @"Monaco" size: 10.0]];
236 }
237
238 /**
239 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
240 */
241 - (void)outlineViewItemDidExpand: (NSNotification *)notif
242 {
243 // XXX: This very well may break because NSTreeController sends us a _NSArrayControllerTreeNode object
244 // which is presumably private, and thus this is not a reliable method for getting the object. But
245 // we damn well need it, so f!ck the rules and we're using it. <rdar://problem/5387001>
246 id notifObj = [[notif userInfo] objectForKey: @"NSObject"];
247 NSXMLElement *obj = [notifObj observedObject];
248
249 // we're not a leaf but have no children. this must be beyond our depth, so go make us deeper
250 if (![obj isLeaf] && [[obj children] count] < 1)
251 {
252 [_connection getProperty: [[obj attributeForName: @"fullname"] stringValue] forElement: notifObj];
253 }
254
255 [_expandedRegisters addObject: [obj variable]];
256 }
257
258 /**
259 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
260 */
261 - (void)outlineViewItemDidCollapse: (NSNotification *)notif
262 {
263 [_expandedRegisters removeObject: [[[[notif userInfo] objectForKey: @"NSObject"] observedObject] variable]];
264 }
265
266 /**
267 * Updates the register view by reinserting a given node back into the outline view
268 */
269 - (void)addChildren: (NSArray *)children toNode: (id)node
270 {
271 // XXX: this may break like in outlineViewItemDidExpand: <rdar://problem/5387001>
272 NSIndexPath *masterPath = [node indexPath];
273 for (int i = 0; i < [children count]; i++)
274 {
275 [_registerController insertObject: [children objectAtIndex: i] atArrangedObjectIndexPath: [masterPath indexPathByAddingIndex: i]];
276 }
277
278 [_registerController rearrangeObjects];
279 }
280
281 @end