Move the status into the toolbar, and remove it from the bottom of the window.
[macgdbp.git] / Source / BreakpointManager.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2011, 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 "BreakpointManager.h"
18
19 #import "AppDelegate.h"
20 #import "PreferenceNames.h"
21
22 @interface BreakpointManager (Private)
23 - (void)updateDisplaysForFile:(NSString*)file;
24 @end
25
26 @implementation BreakpointManager {
27 NSMutableArray* _breakpoints;
28 NSMutableArray* _savedBreakpoints;
29
30 DebuggerBackEnd* _connection;
31 }
32
33 - (id)init
34 {
35 if (self = [super init])
36 {
37 _breakpoints = [[NSMutableArray alloc] init];
38 _savedBreakpoints = [[NSMutableArray alloc] init];
39
40 NSArray* savedBreakpoints = [[NSUserDefaults standardUserDefaults] arrayForKey:kPrefBreakpoints];
41 if (savedBreakpoints) {
42 [_savedBreakpoints addObjectsFromArray:savedBreakpoints];
43 for (NSDictionary* d in savedBreakpoints) {
44 [_breakpoints addObject:[[[Breakpoint alloc] initWithDictionary:d] autorelease]];
45 }
46 }
47 }
48 return self;
49 }
50
51 - (void)dealloc {
52 [_breakpoints release];
53 [_savedBreakpoints release];
54 [super dealloc];
55 }
56
57 /**
58 * Registers a breakpoint at a given line
59 */
60 - (void)addBreakpoint:(Breakpoint*)bp;
61 {
62 if (![_breakpoints containsObject:bp])
63 {
64 [self willChangeValueForKey:@"breakpoints"];
65 [_breakpoints addObject:bp];
66 [self didChangeValueForKey:@"breakpoints"];
67
68 [_connection addBreakpoint:bp];
69
70 [_savedBreakpoints addObject:[bp dictionary]];
71 [[NSUserDefaults standardUserDefaults] setObject:_savedBreakpoints forKey:kPrefBreakpoints];
72
73 [self updateDisplaysForFile:[bp file]];
74 }
75 }
76
77 /**
78 * Removes a breakpoint at a given line/file combination, or nil if nothing was removed
79 */
80 - (Breakpoint*)removeBreakpointAt:(NSUInteger)line inFile:(NSString*)file
81 {
82 for (Breakpoint* b in _breakpoints)
83 {
84 if ([b line] == line && [[b file] isEqualToString:file])
85 {
86 // Keep the breakpoint alive after it is removed from the breakpoints
87 // array.
88 [[b retain] autorelease];
89
90 [self willChangeValueForKey:@"breakpoints"];
91 [_breakpoints removeObject:b];
92 [self didChangeValueForKey:@"breakpoints"];
93
94 [_connection removeBreakpoint:b];
95
96 [_savedBreakpoints removeObject:[b dictionary]];
97 [[NSUserDefaults standardUserDefaults] setObject:_savedBreakpoints forKey:kPrefBreakpoints];
98
99 [self updateDisplaysForFile:file];
100 return b;
101 }
102 }
103 return nil;
104 }
105
106 /**
107 * Returns all the breakpoints for a given file
108 */
109 - (NSSet<NSNumber*>*)breakpointsForFile:(NSString*)file
110 {
111 NSMutableSet<NSNumber*>* matches = [NSMutableSet set];
112 for (Breakpoint* b in _breakpoints) {
113 if ([b.file isEqualToString:file]) {
114 [matches addObject:@(b.line)];
115 }
116 }
117
118 return matches;
119 }
120
121 /**
122 * Checks to see if a given file has a breakpoint on a given line
123 */
124 - (BOOL)hasBreakpointAt:(NSUInteger)line inFile:(NSString*)file
125 {
126 return [_breakpoints containsObject:[[[Breakpoint alloc] initWithLine:line inFile:file] autorelease]];
127 }
128
129 #pragma mark Private
130
131 /**
132 * This marks BSSourceView needsDisplay, rearranges the objects in the breakpoints controller,
133 * and sets the markers for the BSLineNumberView
134 */
135 - (void)updateDisplaysForFile:(NSString*)file
136 {
137 AppDelegate* appDel = [NSApp delegate];
138 [[[appDel breakpoint] arrayController] rearrangeObjects];
139 [[[appDel debugger] sourceViewer] setNeedsDisplay:YES];
140 [[[appDel debugger] sourceViewer] setMarkers:[self breakpointsForFile:file]];
141 }
142
143 @end