source: trunk/macosx/GroupsWindowController.m @ 7193

Last change on this file since 7193 was 7193, checked in by livings124, 12 years ago

make groups tab a bit taller; when adding a group focus on the name field

  • Property svn:keywords set to Date Rev Author Id
File size: 7.4 KB
Line 
1/******************************************************************************
2 * $Id: GroupsWindowController.m 7193 2008-11-29 22:14:40Z 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            [[fSelectedColorNameField window] makeFirstResponder: fSelectedColorNameField];
161           
162            break;
163       
164        case REMOVE_TAG:
165            row = [fTableView selectedRow];
166            [[GroupsController groups] removeGroupWithRowIndex: row];           
167                       
168            [fTableView reloadData];
169           
170            NSInteger selectedRow = [fTableView selectedRow];
171            if (selectedRow != -1)
172                [fTableView scrollRowToVisible: selectedRow];
173           
174            break;
175    }
176}
177
178@end
179
180@implementation GroupsWindowController (Private)
181
182- (void) updateSelectedColor
183{
184    [fAddRemoveControl setEnabled: [fTableView numberOfSelectedRows] > 0 forSegment: REMOVE_TAG];
185    if ([fTableView numberOfSelectedRows] == 1)
186    {
187        NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
188        [fSelectedColorView setColor: [[GroupsController groups] colorForIndex: index]];
189        [fSelectedColorView setEnabled: YES];
190        [fSelectedColorNameField setStringValue: [[GroupsController groups] nameForIndex: index]];
191        [fSelectedColorNameField setEnabled: YES];
192    }
193    else
194    {
195        [fSelectedColorView setColor: [NSColor whiteColor]];
196        [fSelectedColorView setEnabled: NO];
197        [fSelectedColorNameField setStringValue: @""];
198        [fSelectedColorNameField setEnabled: NO];
199    }
200}
201
202@end
Note: See TracBrowser for help on using the repository browser.