1 | /****************************************************************************** |
---|
2 | * $Id: GroupsWindowController.m 7192 2008-11-29 21:58:09Z livings124 $ |
---|
3 | * |
---|
4 | * Copyright (c) 2007-2008 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #import "GroupsWindowController.h" |
---|
26 | #import "GroupsController.h" |
---|
27 | #import "NSApplicationAdditions.h" |
---|
28 | |
---|
29 | #define GROUP_TABLE_VIEW_DATA_TYPE @"GroupTableViewDataType" |
---|
30 | |
---|
31 | #define ADD_TAG 0 |
---|
32 | #define REMOVE_TAG 1 |
---|
33 | |
---|
34 | @interface GroupsWindowController (Private) |
---|
35 | |
---|
36 | - (void) updateSelectedColor; |
---|
37 | |
---|
38 | @end |
---|
39 | |
---|
40 | @implementation GroupsWindowController |
---|
41 | |
---|
42 | #warning should this still be a window controller? |
---|
43 | - (void) awakeFromNib |
---|
44 | { |
---|
45 | [[[fTableView tableColumnWithIdentifier: @"Button"] dataCell] setTitle: NSLocalizedString(@"Color", "Groups -> color button")]; |
---|
46 | |
---|
47 | [fTableView registerForDraggedTypes: [NSArray arrayWithObject: GROUP_TABLE_VIEW_DATA_TYPE]]; |
---|
48 | |
---|
49 | if ([NSApp isOnLeopardOrBetter]) |
---|
50 | [[self window] setContentBorderThickness: [[fTableView enclosingScrollView] frame].origin.y forEdge: NSMinYEdge]; |
---|
51 | else |
---|
52 | { |
---|
53 | [fAddRemoveControl sizeToFit]; |
---|
54 | [fAddRemoveControl setLabel: @"+" forSegment: ADD_TAG]; |
---|
55 | [fAddRemoveControl setLabel: @"-" forSegment: REMOVE_TAG]; |
---|
56 | } |
---|
57 | |
---|
58 | [fAddRemoveControl setEnabled: NO forSegment: REMOVE_TAG]; |
---|
59 | [fSelectedColorView addObserver: self forKeyPath: @"color" options: 0 context: NULL]; |
---|
60 | |
---|
61 | [self updateSelectedColor]; |
---|
62 | } |
---|
63 | |
---|
64 | - (NSInteger) numberOfRowsInTableView: (NSTableView *) tableview |
---|
65 | { |
---|
66 | return [[GroupsController groups] numberOfGroups]; |
---|
67 | } |
---|
68 | |
---|
69 | - (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) tableColumn row: (NSInteger) row |
---|
70 | { |
---|
71 | GroupsController * groupsController = [GroupsController groups]; |
---|
72 | NSInteger groupsIndex = [groupsController indexForRow: row]; |
---|
73 | |
---|
74 | NSString * identifier = [tableColumn identifier]; |
---|
75 | if ([identifier isEqualToString: @"Color"]) |
---|
76 | return [groupsController imageForIndex: groupsIndex]; |
---|
77 | else |
---|
78 | return [groupsController nameForIndex: groupsIndex]; |
---|
79 | } |
---|
80 | |
---|
81 | - (void) tableViewSelectionDidChange: (NSNotification *) notification |
---|
82 | { |
---|
83 | [self updateSelectedColor]; |
---|
84 | } |
---|
85 | |
---|
86 | - (void) observeValueForKeyPath: (NSString *) keyPath ofObject: (id) object change: (NSDictionary *) change context: (void *) context |
---|
87 | { |
---|
88 | if (object == fSelectedColorView && [fTableView numberOfSelectedRows] == 1) |
---|
89 | { |
---|
90 | NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]]; |
---|
91 | [[GroupsController groups] setColor: [fSelectedColorView color] forIndex: index]; |
---|
92 | [fTableView setNeedsDisplay: YES]; |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | - (void) controlTextDidEndEditing: (NSNotification *) notification |
---|
97 | { |
---|
98 | if ([notification object] == fSelectedColorNameField) |
---|
99 | { |
---|
100 | NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]]; |
---|
101 | [[GroupsController groups] setName: [fSelectedColorNameField stringValue] forIndex: index]; |
---|
102 | [fTableView setNeedsDisplay: YES]; |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | - (BOOL) tableView: (NSTableView *) tableView writeRowsWithIndexes: (NSIndexSet *) rowIndexes toPasteboard: (NSPasteboard *) pboard |
---|
107 | { |
---|
108 | [pboard declareTypes: [NSArray arrayWithObject: GROUP_TABLE_VIEW_DATA_TYPE] owner: self]; |
---|
109 | [pboard setData: [NSKeyedArchiver archivedDataWithRootObject: rowIndexes] forType: GROUP_TABLE_VIEW_DATA_TYPE]; |
---|
110 | return YES; |
---|
111 | } |
---|
112 | |
---|
113 | - (NSDragOperation) tableView: (NSTableView *) tableView validateDrop: (id <NSDraggingInfo>) info |
---|
114 | proposedRow: (NSInteger) row proposedDropOperation: (NSTableViewDropOperation) operation |
---|
115 | { |
---|
116 | NSPasteboard * pasteboard = [info draggingPasteboard]; |
---|
117 | if ([[pasteboard types] containsObject: GROUP_TABLE_VIEW_DATA_TYPE]) |
---|
118 | { |
---|
119 | [fTableView setDropRow: row dropOperation: NSTableViewDropAbove]; |
---|
120 | return NSDragOperationGeneric; |
---|
121 | } |
---|
122 | |
---|
123 | return NSDragOperationNone; |
---|
124 | } |
---|
125 | |
---|
126 | - (BOOL) tableView: (NSTableView *) tableView acceptDrop: (id <NSDraggingInfo>) info row: (NSInteger) newRow |
---|
127 | dropOperation: (NSTableViewDropOperation) operation |
---|
128 | { |
---|
129 | NSPasteboard * pasteboard = [info draggingPasteboard]; |
---|
130 | if ([[pasteboard types] containsObject: GROUP_TABLE_VIEW_DATA_TYPE]) |
---|
131 | { |
---|
132 | NSIndexSet * indexes = [NSKeyedUnarchiver unarchiveObjectWithData: [pasteboard dataForType: GROUP_TABLE_VIEW_DATA_TYPE]], |
---|
133 | * selectedIndexes = [[GroupsController groups] moveGroupsAtRowIndexes: indexes toRow: newRow |
---|
134 | oldSelected: [fTableView selectedRowIndexes]]; |
---|
135 | |
---|
136 | [fTableView selectRowIndexes: selectedIndexes byExtendingSelection: NO]; |
---|
137 | [fTableView reloadData]; |
---|
138 | } |
---|
139 | |
---|
140 | return YES; |
---|
141 | } |
---|
142 | |
---|
143 | - (void) addRemoveGroup: (id) sender |
---|
144 | { |
---|
145 | [[NSColorPanel sharedColorPanel] close]; |
---|
146 | |
---|
147 | NSInteger row; |
---|
148 | |
---|
149 | switch ([[sender cell] tagForSegment: [sender selectedSegment]]) |
---|
150 | { |
---|
151 | case ADD_TAG: |
---|
152 | [[GroupsController groups] addNewGroup]; |
---|
153 | |
---|
154 | [fTableView reloadData]; |
---|
155 | |
---|
156 | row = [fTableView numberOfRows]-1; |
---|
157 | [fTableView selectRow: row byExtendingSelection: NO]; |
---|
158 | [fTableView scrollRowToVisible: row]; |
---|
159 | |
---|
160 | break; |
---|
161 | |
---|
162 | case REMOVE_TAG: |
---|
163 | row = [fTableView selectedRow]; |
---|
164 | [[GroupsController groups] removeGroupWithRowIndex: row]; |
---|
165 | |
---|
166 | [fTableView reloadData]; |
---|
167 | |
---|
168 | if ([fTableView numberOfRows] > 0) |
---|
169 | [fTableView scrollRowToVisible: [fTableView selectedRow]]; |
---|
170 | |
---|
171 | break; |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | @end |
---|
176 | |
---|
177 | @implementation GroupsWindowController (Private) |
---|
178 | |
---|
179 | - (void) updateSelectedColor |
---|
180 | { |
---|
181 | [fAddRemoveControl setEnabled: [fTableView numberOfSelectedRows] > 0 forSegment: REMOVE_TAG]; |
---|
182 | if ([fTableView numberOfSelectedRows] == 1) |
---|
183 | { |
---|
184 | NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]]; |
---|
185 | [fSelectedColorView setColor: [[GroupsController groups] colorForIndex: index]]; |
---|
186 | [fSelectedColorView setEnabled: YES]; |
---|
187 | [fSelectedColorNameField setStringValue: [[GroupsController groups] nameForIndex: index]]; |
---|
188 | [fSelectedColorNameField setEnabled: YES]; |
---|
189 | } |
---|
190 | else |
---|
191 | { |
---|
192 | [fSelectedColorView setColor: [NSColor whiteColor]]; |
---|
193 | [fSelectedColorView setEnabled: NO]; |
---|
194 | [fSelectedColorNameField setStringValue: @""]; |
---|
195 | [fSelectedColorNameField setEnabled: NO]; |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | @end |
---|