Add drag and drop for the table view.
[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 #import "AppDelegate.h"
19
20
21 @implementation BreakpointController
22
23 @synthesize tableView = tableView_;
24 @synthesize arrayController = arrayController_;
25 @synthesize sourceView = sourceView_;
26
27 /**
28 * Constructor
29 */
30 - (id)init
31 {
32 if (self = [super initWithWindowNibName:@"Breakpoints"])
33 {
34 manager = [BreakpointManager sharedManager];
35 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"BreakpointsWindowVisible"])
36 [[self window] orderBack:nil];
37 }
38 return self;
39 }
40
41 /**
42 * Awake from NIB.
43 */
44 - (void)awakeFromNib
45 {
46 NSArray* dragTypes = [NSArray arrayWithObject:NSFilenamesPboardType];
47 [tableView_ registerForDraggedTypes:dragTypes];
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 return;
60
61 [sourceView_ setFile:[panel filename]];
62 }
63
64 /**
65 * Removes a breakpoint
66 */
67 - (IBAction)removeBreakpoint:(id)sender
68 {
69 NSArray* selection = [arrayController_ selectedObjects];
70 if ([selection count] < 1)
71 return;
72
73 for (Breakpoint* bp in selection)
74 [manager removeBreakpointAt:[bp line] inFile:[bp file]];
75 }
76
77 #pragma mark NSTableView Delegate
78
79 /**
80 * NSTableView delegate method that informs the controller that the stack selection did change and that
81 * we should update the source viewer
82 */
83 - (void)tableViewSelectionDidChange:(NSNotification*)notif
84 {
85 NSArray* selection = [arrayController_ selectedObjects];
86 if ([selection count] < 1)
87 return;
88
89 Breakpoint* bp = [selection objectAtIndex:0];
90 [sourceView_ setFile:[bp file]];
91 if ([bp line] > 0)
92 [sourceView_ scrollToLine:[bp line]];
93 [[sourceView_ numberView] setMarkers:[NSSet setWithArray:[manager breakpointsForFile:[bp file]]]];
94 }
95
96 #pragma mark NSTableView Data Source
97
98 /**
99 * Handles the beginning of a drag operation.
100 */
101 - (BOOL)tableView:(NSTableView*)aTableView
102 writeRowsWithIndexes:(NSIndexSet*)rowIndexes
103 toPasteboard:(NSPasteboard*)pboard
104 {
105 NSLog(@"begin");
106 return [[pboard types] containsObject:NSFilenamesPboardType];
107 }
108
109 /**
110 * Validates the drag operation.
111 */
112 - (NSDragOperation)tableView:(NSTableView*)aTableView
113 validateDrop:(id<NSDraggingInfo>)info
114 proposedRow:(NSInteger)row
115 proposedDropOperation:(NSTableViewDropOperation)operation
116 {
117 NSLog(@"validate");
118 NSPasteboard* pboard = [info draggingPasteboard];
119 if ([[pboard types] containsObject:NSFilenamesPboardType]) {
120 NSArray* files = [pboard propertyListForType:NSFilenamesPboardType];
121 if ([files count])
122 return NSDragOperationGeneric;
123 }
124 return NSDragOperationNone;
125 }
126
127 /**
128 * Incorporates the dropped data.
129 */
130 - (BOOL)tableView:(NSTableView*)aTableView
131 acceptDrop:(id<NSDraggingInfo>)info
132 row:(NSInteger)row
133 dropOperation:(NSTableViewDropOperation)operation
134 {
135 NSLog(@"accept");
136 BOOL valid = [self tableView:aTableView
137 validateDrop:info
138 proposedRow:row
139 proposedDropOperation:operation] == NSDragOperationGeneric;
140 if (valid) {
141 NSPasteboard* pboard = [info draggingPasteboard];
142 NSArray* files = [pboard propertyListForType:NSFilenamesPboardType];
143 for (NSString* file in files) {
144 Breakpoint* bp = [[[Breakpoint alloc] initWithLine:0 inFile:file] autorelease];
145 [manager addBreakpoint:bp];
146 }
147 return YES;
148 }
149 return NO;
150 }
151
152 #pragma mark BSSourceView Delegate
153
154 /**
155 * The gutter was clicked, which indicates that a breakpoint needs to be changed
156 */
157 - (void)gutterClickedAtLine:(int)line forFile:(NSString*)file
158 {
159 if ([manager hasBreakpointAt:line inFile:file]) {
160 [manager removeBreakpointAt:line inFile:file];
161 } else {
162 Breakpoint* bp = [[Breakpoint alloc] initWithLine:line inFile:file];
163 [manager addBreakpoint:bp];
164 [bp release];
165 }
166
167 [[sourceView_ numberView] setMarkers:[NSSet setWithArray:[manager breakpointsForFile:file]]];
168 [[sourceView_ numberView] setNeedsDisplay:YES];
169 }
170
171 @end