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