Merge branch 'master' into register-feature
[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
20 @interface DebuggerWindowController (Private)
21
22 - (void)updateSourceViewer;
23
24 @end
25
26 @implementation DebuggerWindowController
27
28 /**
29 * Initializes the window controller and sets the connection
30 */
31 - (id)initWithConnection: (DebuggerConnection *)cnx
32 {
33 if (self = [super initWithWindowNibName: @"Debugger"])
34 {
35 _connection = [cnx retain];
36 }
37 return self;
38 }
39
40 /**
41 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
42 */
43 - (void)awakeFromNib
44 {
45 // set up the scroller for the source viewer
46 [_sourceViewer setMaxSize: NSMakeSize(FLT_MAX, FLT_MAX)];
47 [[_sourceViewer textContainer] setContainerSize: NSMakeSize(FLT_MAX, FLT_MAX)];
48 [[_sourceViewer textContainer] setWidthTracksTextView: NO];
49 [_sourceViewer setHorizontallyResizable: YES];
50 [_sourceViewerScroller setHasHorizontalScroller: YES];
51 [_sourceViewerScroller display];
52 }
53
54 /**
55 * Release object members
56 */
57 - (void)dealloc
58 {
59 [_connection release];
60
61 [super dealloc];
62 }
63
64 /**
65 * Sets the status and clears any error message
66 */
67 - (void)setStatus: (NSString *)status
68 {
69 [_error setHidden: YES];
70 [_status setStringValue: status];
71 [[self window] setTitle: [NSString stringWithFormat: @"GDBp @ %@:%d/%@", [_connection remoteHost], [_connection port], [_connection session]]];
72 }
73
74 /**
75 * Sets the status to be "Error" and then displays the error message
76 */
77 - (void)setError: (NSString *)error
78 {
79 [_error setStringValue: error];
80 [self setStatus: @"Error"];
81 [_error setHidden: NO];
82 }
83
84 /**
85 * Sets the root node element of the stacktrace
86 */
87 - (void)setStack: (NSArray *)stack
88 {
89 if (_stack != nil)
90 {
91 [_stack release];
92 }
93
94 _stack = stack;
95 [_stack retain];
96 [self updateSourceViewer];
97 }
98
99 /**
100 * Sets the stack root element so that the NSOutlineView can display it
101 */
102 - (void)setRegister: (NSXMLElement *)elm
103 {
104 if (_register != nil)
105 {
106 [_register release];
107 }
108
109 _register = elm;
110 [_register retain];
111 }
112
113 /**
114 * Forwards the message to run script execution to the connection
115 */
116 - (IBAction)run: (id)sender
117 {
118 [_connection run];
119 }
120
121 /**
122 * Forwards the message to "step in" to the connection
123 */
124 - (IBAction)stepIn: (id)sender
125 {
126 [_connection stepIn];
127 }
128
129 /**
130 * Forwards the message to "step out" to the connection
131 */
132 - (IBAction)stepOut: (id)sender
133 {
134 [_connection stepOut];
135 }
136
137 /**
138 * Forwards the message to "step over" to the connection
139 */
140 - (IBAction)stepOver: (id)sender
141 {
142 [_connection stepOver];
143 }
144
145 /**
146 * NSTableView delegate method that informs the controller that the stack selection did change and that
147 * we should update the source viewer
148 */
149 - (void)tableViewSelectionDidChange: (NSNotification *)notif
150 {
151 [self updateSourceViewer];
152 }
153 /**
154 * Does the actual updating of the source viewer by reading in the file
155 */
156 - (void)updateSourceViewer
157 {
158 int selection = [_stackController selectionIndex];
159 if (selection == NSNotFound)
160 {
161 [_sourceViewer setString: @""];
162 return;
163 }
164
165 // get the filename and then set the text
166 NSString *filename = [[_stack objectAtIndex: selection] valueForKey: @"filename"];
167 filename = [[NSURL URLWithString: filename] path];
168 NSString *text = [NSString stringWithContentsOfFile: filename];
169 [_sourceViewer setString: text];
170
171 // go through the document until we find the NSRange for the line we want
172 int destination = [[[_stack objectAtIndex: selection] valueForKey: @"lineno"] intValue];
173 int rangeIndex = 0;
174 for (int line = 0; line < destination; line++)
175 {
176 rangeIndex = NSMaxRange([text lineRangeForRange: NSMakeRange(rangeIndex, 0)]);
177 }
178
179 // now get the true start/end markers for it
180 unsigned lineStart, lineEnd;
181 [text getLineStart: &lineStart end: NULL contentsEnd: &lineEnd forRange: NSMakeRange(rangeIndex - 1, 0)];
182 NSRange lineRange = NSMakeRange(lineStart, lineEnd - lineStart);
183
184 // colorize it so the user knows which line we're on in the stack
185 [[_sourceViewer textStorage] setAttributes: [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: [NSColor redColor], [NSColor yellowColor], nil]
186 forKeys: [NSArray arrayWithObjects: NSForegroundColorAttributeName, NSBackgroundColorAttributeName, nil]]
187 range: lineRange];
188 [_sourceViewer scrollRangeToVisible: [text lineRangeForRange: NSMakeRange(lineStart, lineEnd - lineStart)]];
189
190 // make sure the font stays Monaco
191 [_sourceViewer setFont: [NSFont fontWithName: @"Monaco" size: 10.0]];
192 }
193
194 @end