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