Remove the dedicated Breakpoints source view.
[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
28 @synthesize breakpoints, connection;
29
30 /**
31 * Initializer
32 */
33 - (id)init
34 {
35 if (self = [super init])
36 {
37 if (!breakpoints)
38 {
39 breakpoints = [[NSMutableArray alloc] init];
40 }
41
42 savedBreakpoints = [[[NSUserDefaults standardUserDefaults] arrayForKey:kPrefBreakpoints] mutableCopy];
43 if (savedBreakpoints)
44 {
45 for (NSDictionary* d in savedBreakpoints)
46 {
47 [breakpoints addObject:[[[Breakpoint alloc] initWithDictionary:d] autorelease]];
48 }
49 }
50 else
51 {
52 savedBreakpoints = [NSMutableArray new];
53 }
54 }
55 return self;
56 }
57
58 /**
59 * Returns the shared manager (singleton)
60 */
61 + (BreakpointManager*)sharedManager
62 {
63 static BreakpointManager* manager;
64 if (!manager)
65 {
66 manager = [[BreakpointManager alloc] init];
67 }
68 return manager;
69 }
70
71 /**
72 * Registers a breakpoint at a given line
73 */
74 - (void)addBreakpoint:(Breakpoint*)bp;
75 {
76 if (![breakpoints containsObject:bp])
77 {
78 [self willChangeValueForKey:@"breakpoints"];
79 [breakpoints addObject:bp];
80 [self didChangeValueForKey:@"breakpoints"];
81
82 [connection addBreakpoint:bp];
83
84 [savedBreakpoints addObject:[bp dictionary]];
85 [[NSUserDefaults standardUserDefaults] setObject:savedBreakpoints forKey:kPrefBreakpoints];
86
87 [self updateDisplaysForFile:[bp file]];
88 }
89 }
90
91 /**
92 * Removes a breakpoint at a given line/file combination, or nil if nothing was removed
93 */
94 - (Breakpoint*)removeBreakpointAt:(NSUInteger)line inFile:(NSString*)file
95 {
96 for (Breakpoint* b in breakpoints)
97 {
98 if ([b line] == line && [[b file] isEqualToString:file])
99 {
100 // Keep the breakpoint alive after it is removed from the breakpoints
101 // array.
102 [[b retain] autorelease];
103
104 [self willChangeValueForKey:@"breakpoints"];
105 [breakpoints removeObject:b];
106 [self didChangeValueForKey:@"breakpoints"];
107
108 [connection removeBreakpoint:b];
109
110 [savedBreakpoints removeObject:[b dictionary]];
111 [[NSUserDefaults standardUserDefaults] setObject:savedBreakpoints forKey:kPrefBreakpoints];
112
113 [self updateDisplaysForFile:file];
114 return b;
115 }
116 }
117 return nil;
118 }
119
120 /**
121 * Returns all the breakpoints for a given file
122 */
123 - (NSSet<NSNumber*>*)breakpointsForFile:(NSString*)file
124 {
125 NSMutableSet<NSNumber*>* matches = [NSMutableSet set];
126 for (Breakpoint* b in breakpoints) {
127 if ([b.file isEqualToString:file]) {
128 [matches addObject:@(b.line)];
129 }
130 }
131
132 return matches;
133 }
134
135 /**
136 * Checks to see if a given file has a breakpoint on a given line
137 */
138 - (BOOL)hasBreakpointAt:(NSUInteger)line inFile:(NSString*)file
139 {
140 return [breakpoints containsObject:[[[Breakpoint alloc] initWithLine:line inFile:file] autorelease]];
141 }
142
143 #pragma mark Private
144
145 /**
146 * This marks BSSourceView needsDisplay, rearranges the objects in the breakpoints controller,
147 * and sets the markers for the BSLineNumberView
148 */
149 - (void)updateDisplaysForFile:(NSString*)file
150 {
151 AppDelegate* appDel = [NSApp delegate];
152 [[[appDel breakpoint] arrayController] rearrangeObjects];
153 [[[appDel debugger] sourceViewer] setNeedsDisplay:YES];
154 [[[appDel debugger] sourceViewer] setMarkers:[self breakpointsForFile:file]];
155 }
156
157 @end