Add a bunch of new NSView classes (to form a meta-view BSSourceView) that adds a...
[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
22 @interface DebuggerWindowController (Private)
23
24 - (void)updateSourceViewer;
25
26 @end
27
28 @implementation DebuggerWindowController
29
30 @synthesize connection;
31
32 /**
33 * Initializes the window controller and sets the connection
34 */
35 - (id)initWithPort:(int)aPort session:(NSString *)aSession
36 {
37 if (self = [super initWithWindowNibName:@"Debugger"])
38 {
39 connection = [[DebuggerConnection alloc] initWithWindowController:self port:aPort session:aSession];
40 expandedRegisters = [[NSMutableSet alloc] init];
41 [[self window] makeKeyAndOrderFront:nil];
42 }
43 return self;
44 }
45
46 /**
47 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
48 */
49 - (void)awakeFromNib
50 {
51 [self setStatus:@"Connecting"];
52 }
53
54 /**
55 * Called right before the window closes so that we can tell the socket to close down
56 */
57 - (void)windowWillClose:(NSNotification *)notif
58 {
59 [[connection socket] close];
60 }
61
62 /**
63 * Resets all the displays to be empty
64 */
65 - (void)resetDisplays
66 {
67 [registerController setContent:nil];
68 [stackController setContent:nil];
69 [[sourceViewer textView] setString:@""];
70 }
71
72 /**
73 * Sets the status and clears any error message
74 */
75 - (void)setStatus:(NSString *)aStatus
76 {
77 [errormsg setHidden:YES];
78 [statusmsg setStringValue:aStatus];
79 [[self window] setTitle:[NSString stringWithFormat:@"GDBp @ %@:%d/%@", [connection remoteHost], [connection port], [connection session]]];
80
81 [stepInButton setEnabled:NO];
82 [stepOutButton setEnabled:NO];
83 [stepOverButton setEnabled:NO];
84 [runButton setEnabled:NO];
85 [reconnectButton setEnabled:NO];
86
87 if ([connection isConnected])
88 {
89 if ([aStatus isEqualToString:@"Starting"])
90 {
91 [stepInButton setEnabled:YES];
92 [runButton setEnabled:YES];
93 }
94 }
95 else
96 {
97 [reconnectButton setEnabled:YES];
98 }
99 }
100
101 /**
102 * Sets the status to be "Error" and then displays the error message
103 */
104 - (void)setError:(NSString *)anError
105 {
106 [errormsg setStringValue:anError];
107 [self setStatus:@"Error"];
108 [errormsg setHidden:NO];
109 }
110
111 /**
112 * Sets the root node element of the stacktrace
113 */
114 - (void)setStack:(NSArray *)node
115 {
116 stack = node;
117
118 if ([stack count] > 1)
119 {
120 [stepOutButton setEnabled:YES];
121 }
122 [stepInButton setEnabled:YES];
123 [stepOverButton setEnabled:YES];
124 [runButton setEnabled:YES];
125
126 [self updateSourceViewer];
127 }
128
129 /**
130 * Sets the stack root element so that the NSOutlineView can display it
131 */
132 - (void)setRegister:(NSXMLDocument *)elm
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 for (int i = 0; i < [registerView numberOfRows]; i++)
145 {
146 NSTreeNode *node = [registerView itemAtRow:i];
147 if ([expandedRegisters containsObject:[[node representedObject] fullname]])
148 {
149 [registerView expandItem:node];
150 }
151 }
152 }
153
154 /**
155 * Forwards the message to run script execution to the connection
156 */
157 - (IBAction)run:(id)sender
158 {
159 [connection run];
160 }
161
162 /**
163 * Tells the connection to ask the server to reconnect
164 */
165 - (IBAction)reconnect:(id)sender
166 {
167 [connection reconnect];
168 }
169
170 /**
171 * Forwards the message to "step in" to the connection
172 */
173 - (IBAction)stepIn:(id)sender
174 {
175 [connection stepIn];
176 }
177
178 /**
179 * Forwards the message to "step out" to the connection
180 */
181 - (IBAction)stepOut:(id)sender
182 {
183 [connection stepOut];
184 }
185
186 /**
187 * Forwards the message to "step over" to the connection
188 */
189 - (IBAction)stepOver:(id)sender
190 {
191 [connection stepOver];
192 }
193
194 /**
195 * NSTableView delegate method that informs the controller that the stack selection did change and that
196 * we should update the source viewer
197 */
198 - (void)tableViewSelectionDidChange:(NSNotification *)notif
199 {
200 [self updateSourceViewer];
201 }
202
203 /**
204 * Does the actual updating of the source viewer by reading in the file
205 */
206 - (void)updateSourceViewer
207 {
208 id selectedLevel = [[stackController selection] valueForKey:@"level"];
209 if (selectedLevel == NSNoSelectionMarker)
210 {
211 [[sourceViewer textView] setString:@""];
212 return;
213 }
214 int selection = [selectedLevel intValue];
215
216 // get the filename and then set the text
217 NSString *filename = [[stack objectAtIndex:selection] valueForKey:@"filename"];
218 filename = [[NSURL URLWithString:filename] path];
219 NSString *text = [NSString stringWithContentsOfFile:filename];
220 [[sourceViewer textView] setString:text];
221
222 // go through the document until we find the NSRange for the line we want
223 int destination = [[[stack objectAtIndex:selection] valueForKey:@"lineno"] intValue];
224 int rangeIndex = 0;
225 for (int line = 0; line < destination; line++)
226 {
227 rangeIndex = NSMaxRange([text lineRangeForRange:NSMakeRange(rangeIndex, 0)]);
228 }
229
230 // now get the true start/end markers for it
231 unsigned lineStart, lineEnd;
232 [text getLineStart:&lineStart end:NULL contentsEnd:&lineEnd forRange:NSMakeRange(rangeIndex - 1, 0)];
233 NSRange lineRange = NSMakeRange(lineStart, lineEnd - lineStart);
234
235 // colorize it so the user knows which line we're on in the stack
236 [[[sourceViewer textView] textStorage] setAttributes:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSColor redColor], [NSColor yellowColor], nil]
237 forKeys:[NSArray arrayWithObjects:NSForegroundColorAttributeName, NSBackgroundColorAttributeName, nil]]
238 range:lineRange];
239 [[sourceViewer textView] scrollRangeToVisible:[text lineRangeForRange:NSMakeRange(lineStart, lineEnd - lineStart)]];
240
241 // make sure the font stays Monaco
242 //[sourceViewer setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
243 }
244
245 /**
246 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
247 */
248 - (void)outlineViewItemDidExpand:(NSNotification *)notif
249 {
250 NSTreeNode *node = [[notif userInfo] objectForKey:@"NSObject"];
251
252 // we're not a leaf but have no children. this must be beyond our depth, so go make us deeper
253 if (![node isLeaf] && [[node childNodes] count] < 1)
254 {
255 [connection getProperty:[[node representedObject] fullname] forNode:node];
256 }
257
258 [expandedRegisters addObject:[[node representedObject] fullname]];
259 }
260
261 /**
262 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
263 */
264 - (void)outlineViewItemDidCollapse:(NSNotification *)notif
265 {
266 [expandedRegisters removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullname]];
267 }
268
269 /**
270 * Updates the register view by reinserting a given node back into the outline view
271 */
272 - (void)addChildren:(NSArray *)children toNode:(NSTreeNode *)node
273 {
274 NSXMLElement *parent = [node representedObject];
275 for (NSXMLNode *child in children)
276 {
277 [parent addChild:child];
278 }
279
280 [registerController rearrangeObjects];
281 }
282
283 @end