Add a new File Access preferences pane.
[macgdbp.git] / Source / BreakpointController.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 "BreakpointController.h"
18
19 #import "AppDelegate.h"
20 #import "PreferenceNames.h"
21
22 @implementation BreakpointController {
23 BreakpointManager* _manager;
24 Breakpoint* _selection;
25
26 BSSourceView* _sourceView;
27
28 NSArrayController* __weak _arrayController;
29 }
30
31 /**
32 * Constructor
33 */
34 - (instancetype)initWithBreakpointManager:(BreakpointManager*)breakpointManager
35 sourceView:(BSSourceView*)sourceView
36 {
37 if ((self = [super initWithNibName:@"Breakpoints" bundle:nil])) {
38 _manager = breakpointManager;
39 _sourceView = sourceView;
40 }
41 return self;
42 }
43
44 - (void)awakeFromNib
45 {
46 [[self.addBreakpointButton cell] setUsesItemFromMenu:NO];
47 [self.addBreakpointButton.cell setMenuItem:[self.addBreakpointButton.menu itemAtIndex:0]];
48 }
49
50 /**
51 * Adds a breakpoint by calling up a file chooser and selecting a file for
52 * breaking in
53 */
54 - (IBAction)addBreakpoint:(id)sender
55 {
56 NSOpenPanel* panel = [NSOpenPanel openPanel];
57
58 if ([panel runModal] != NSOKButton)
59 {
60 return;
61 }
62
63 [_sourceView setFile:[[panel URL] path]];
64 }
65
66 - (IBAction)addFunctionBreakpoint:(id)sender
67 {
68 [self.view.window beginSheet:self.addFunctionBreakpointWindow completionHandler:nil];
69 }
70
71 - (IBAction)cancelFunctionBreakpoint:(id)sender
72 {
73 [self.view.window endSheet:self.addFunctionBreakpointWindow];
74 }
75
76 - (IBAction)saveFunctionBreakpoint:(id)sender
77 {
78 [_manager addBreakpoint:[Breakpoint breakpointOnFunctionNamed:self.functionNameField.stringValue]];
79 [self.view.window endSheet:self.addFunctionBreakpointWindow];
80 }
81
82 /**
83 * Removes a breakpoint
84 */
85 - (IBAction)removeBreakpoint:(id)sender
86 {
87 NSArray* selection = [_arrayController selectedObjects];
88 if ([selection count] < 1)
89 {
90 return;
91 }
92
93 for (Breakpoint* bp in selection) {
94 [_manager removeBreakpoint:bp];
95 }
96 }
97
98 #pragma mark NSTableView Delegate
99
100 /**
101 * NSTableView delegate method that informs the controller that the stack selection did change and that
102 * we should update the source viewer
103 */
104 - (void)tableViewSelectionDidChange:(NSNotification*)notif
105 {
106 if (_selection.type == kBreakpointTypeFile) {
107 [_selection stopSecureFileAccess];
108 _selection = nil;
109 }
110
111 NSArray* selection = [_arrayController selectedObjects];
112 if ([selection count] < 1) {
113 [_sourceView setString:@"" asFile:nil];
114 [_sourceView setMarkers:[NSSet set]];
115 return;
116 }
117
118 _selection = [selection objectAtIndex:0];
119 if (_selection.type != kBreakpointTypeFile) {
120 return;
121 }
122
123 [_selection startSecureFileAccess];
124
125 [_sourceView setFile:[_selection file]];
126 [_sourceView scrollToLine:[_selection line]];
127 [_sourceView setMarkers:[_manager breakpointsForFile:_selection.file]];
128 }
129
130 @end