Move the Eval window into a tabbed section.
[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 [breakpoints addObject:bp];
79 [connection addBreakpoint:bp];
80
81 [savedBreakpoints addObject:[bp dictionary]];
82 [[NSUserDefaults standardUserDefaults] setObject:savedBreakpoints forKey:kPrefBreakpoints];
83
84 [self updateDisplaysForFile:[bp file]];
85 }
86 }
87
88 /**
89 * Removes a breakpoint at a given line/file combination, or nil if nothing was removed
90 */
91 - (Breakpoint*)removeBreakpointAt:(NSUInteger)line inFile:(NSString*)file
92 {
93 for (Breakpoint* b in breakpoints)
94 {
95 if ([b line] == line && [[b file] isEqualToString:file])
96 {
97 [breakpoints removeObject:b];
98 [connection removeBreakpoint:b];
99
100 [savedBreakpoints removeObject:[b dictionary]];
101 [[NSUserDefaults standardUserDefaults] setObject:savedBreakpoints forKey:kPrefBreakpoints];
102
103 [self updateDisplaysForFile:file];
104 return b;
105 }
106 }
107 return nil;
108 }
109
110 /**
111 * Returns all the breakpoints for a given file
112 */
113 - (NSArray*)breakpointsForFile:(NSString*)file
114 {
115 NSMutableArray* matches = [NSMutableArray array];
116 for (Breakpoint* b in breakpoints)
117 {
118 if ([[b file] isEqualToString:file])
119 {
120 [matches addObject:b];
121 }
122 }
123
124 return matches;
125 }
126
127 /**
128 * Checks to see if a given file has a breakpoint on a given line
129 */
130 - (BOOL)hasBreakpointAt:(NSUInteger)line inFile:(NSString*)file
131 {
132 return [breakpoints containsObject:[[[Breakpoint alloc] initWithLine:line inFile:file] autorelease]];
133 }
134
135 #pragma mark Private
136
137 /**
138 * This marks BSSourceView needsDisplay, rearranges the objects in the breakpoints controller,
139 * and sets the markers for the BSLineNumberView
140 */
141 - (void)updateDisplaysForFile:(NSString*)file
142 {
143 AppDelegate* appDel = [NSApp delegate];
144 [[[appDel breakpoint] arrayController] rearrangeObjects];
145 [[[appDel breakpoint] sourceView] setNeedsDisplay:YES];
146 [[[appDel breakpoint] sourceView] setMarkers:[NSSet setWithArray:[self breakpointsForFile:file]]];
147 [[[appDel debugger] sourceViewer] setNeedsDisplay:YES];
148 [[[appDel debugger] sourceViewer] setMarkers:[NSSet setWithArray:[self breakpointsForFile:file]]];
149 }
150
151 @end