source: trunk/ICeCoffEE/ICeCoffEE/ICeCoffEEServicePrefController.m @ 425

Last change on this file since 425 was 425, checked in by Nicholas Riley, 13 years ago

English.lproj/APE Manager plugin.nib: Wording cleanups; the edit
button works now.

English.lproj/APEInfo.rtfd: Updated for 1.5b1.

English.lproj/Select services.nib: Removed reference to
non-highlighting outline view; use source list highlighting on
Leopard.

ICeCoffEE.xcodeproj: More decruftification.

ICeCoffEEInvertingTextFieldCell.[hm]: Non-colored text becomes white
(why doesn't Apple's implementation do this?).

ICeCoffEEServicePrefController.m: Service shown/hidden/mixed
selections slightly more abstracted, and now stored in tags rather
than represented object (which is already in use). Still no
localization support. Fix scroll bar non-appearance.

Installer components/ui/ui.plist: Replaced version info with empty
placeholders since it's now script-populated; require OS X 10.5 until
I've had a chance to test on an earlier version.

VERSION.xcconfig: Updated for 1.5b1.

File size: 16.7 KB
Line 
1//
2//  ICeCoffEEServicePrefController.m
3//  ICeCoffEE APE
4//
5//  Created by Nicholas Riley on Fri Jun 06 2003.
6//  Copyright (c) 2003 Nicholas Riley. All rights reserved.
7//
8
9#import "ICeCoffEEShared.h"
10#import "ICeCoffEEServices.h"
11#import "ICeCoffEEServicePrefController.h"
12#import "ICeCoffEENonHighlightingButtonCell.h"
13#import "ICeCoffEEInvertingTextFieldCell.h"
14#import "ICeCoffEELabeledIconCell.h"
15#import <objc/objc.h>
16#import <ApplicationEnhancer/ApplicationEnhancer.h>
17
18const int ICCF_SERVICE_UNKNOWN = 0;
19const int ICCF_SERVICE_SHOWN = 1;
20const int ICCF_SERVICE_HIDDEN = 2;
21const int ICCF_SERVICE_MIXED = 3;
22
23static NSDictionary *ICCF_SERVICE_OPTION_HIDDEN;
24
25static float ICCF_TableViewCellHeight(NSTableView *tableView) {
26    return ([tableView rowHeight] + [tableView intercellSpacing].height);
27}
28
29static NSMutableDictionary *keyEquivalents;
30
31static void ICCF_RemoveSingleKeyEquivalents() {
32    NSMutableArray *singleKeys = [[NSMutableArray alloc] init];
33    NSEnumerator *e = [[keyEquivalents allKeys] objectEnumerator];
34    NSString *keyEquivalent;
35    ICLog(@"before ICCF_RemoveSingleKeyEquivalents: %@", keyEquivalents);
36    while ( (keyEquivalent = [e nextObject]) != nil) {
37        if ([[keyEquivalents objectForKey: keyEquivalent] count] == 1)
38            [singleKeys addObject: keyEquivalent];
39    }
40    [keyEquivalents removeObjectsForKeys: singleKeys];
41    [singleKeys release];
42    ICLog(@"after ICCF_RemoveSingleKeyEquivalents: %@", keyEquivalents);
43}
44
45static inline unsigned ICCF_CountForKeyEquivalent(NSString *keyEquivalent) {
46    if (keyEquivalent == nil) return 0;
47    NSMutableSet *setOrNil = (NSMutableSet *)[keyEquivalents objectForKey: keyEquivalent];
48    return (setOrNil == nil) ? nil : [setOrNil count];
49}
50
51static inline void ICCF_AddKeyEquivalentForItem(NSMenuItem *item) {
52    NSString *keyEquivalent = [item toolTip];
53    if (keyEquivalent == nil) return;
54    NSMutableSet *setOrNil = (NSMutableSet *)[keyEquivalents objectForKey: keyEquivalent];
55    if (setOrNil == nil) return;
56    [setOrNil addObject: item];
57}
58
59static inline void ICCF_RemoveKeyEquivalentForItem(NSMenuItem *item) {
60    NSString *keyEquivalent = [item toolTip];
61    if (keyEquivalent == nil) return;
62    NSMutableSet *setOrNil = (NSMutableSet *)[keyEquivalents objectForKey: keyEquivalent];
63    if (setOrNil == nil) return;
64    [setOrNil removeObject: item];
65}
66
67static int ICCF_GetServiceState(NSMenuItem *item) {
68    return [item tag];
69}
70
71static void ICCF_SetServiceState(NSMenuItem *item, int state) {
72    [item setTag: state];
73}
74
75static inline void ICCF_UpdateKeyEquivalentForItem(NSMenuItem *item, int state) {
76    int oldState = ICCF_GetServiceState(item);
77    if ((oldState == ICCF_SERVICE_UNKNOWN || oldState == ICCF_SERVICE_SHOWN) && state == ICCF_SERVICE_HIDDEN)
78        ICCF_RemoveKeyEquivalentForItem(item);
79    else if (oldState == ICCF_SERVICE_HIDDEN && (state == ICCF_SERVICE_SHOWN || state == ICCF_SERVICE_UNKNOWN))
80        ICCF_AddKeyEquivalentForItem(item);
81}
82
83
84static inline NSCellStateValue ICCF_ServiceItemState(NSMenuItem *item) {
85    int state = ICCF_GetServiceState(item);
86    if (state == ICCF_SERVICE_HIDDEN)
87        return NSOffState;
88    if (state == ICCF_SERVICE_MIXED)
89            return NSMixedState;
90    return NSOnState;
91}
92
93static void ICCF_PropagateServiceStateChange(NSMenu *menu, int state) {
94    NSEnumerator *e = [[menu itemArray] objectEnumerator];
95    NSMenuItem *item;
96    NSMenu *submenu;
97
98    while ( (item = [e nextObject]) != nil) {
99        submenu = [item submenu];
100        if (submenu != nil)
101            ICCF_PropagateServiceStateChange(submenu, state);
102        else
103            ICCF_UpdateKeyEquivalentForItem(item, state);
104       
105        ICCF_SetServiceState(item, state);
106    }
107}
108
109static NSCellStateValue ICCF_PropagateServiceState(NSMenuItem *item, NSMenuItem *changedItem) {
110    NSMenu *submenu = [item submenu];
111    if (submenu == nil) return ICCF_ServiceItemState(item);
112
113    if (item == changedItem) ICCF_PropagateServiceStateChange(submenu, [item tag]);
114
115    BOOL areOn = NO, areOff = NO;
116    NSEnumerator *e = [[submenu itemArray] objectEnumerator];
117    NSMenuItem *subItem;
118    while ( (subItem = [e nextObject]) != nil) {
119        switch (ICCF_PropagateServiceState(subItem, changedItem)) {
120            case NSOnState: if (!areOff) { areOn = YES; continue; }
121                break;
122            case NSOffState: if (!areOn) { areOff = YES; continue; }
123                break;
124            case NSMixedState:
125                break;
126        }
127        ICCF_SetServiceState(item, ICCF_SERVICE_MIXED);
128        return NSMixedState;
129    }
130    if (areOn) {
131        ICCF_SetServiceState(item, ICCF_SERVICE_SHOWN);
132        return NSOnState;
133    } else {
134        ICCF_SetServiceState(item, ICCF_SERVICE_HIDDEN);
135        return NSOffState;
136    }
137}
138
139static NSMutableDictionary *ICCF_RetainedServiceOptionsDictionary(NSMenu *menu) {
140    NSEnumerator *e = [[menu itemArray] objectEnumerator];
141    NSMenuItem *item;
142    NSMenu *submenu;
143    NSMutableDictionary *dict = nil, *subDict = nil, *submenuDict = nil;
144
145    while ( (item = [e nextObject]) != nil) {
146        submenu = [item submenu];
147        if (ICCF_ServiceItemState(item) == NSOffState) {
148            subDict = [ICCF_SERVICE_OPTION_HIDDEN retain];
149        } else if (submenu != nil) {
150            submenuDict = ICCF_RetainedServiceOptionsDictionary(submenu);
151            if (submenuDict == nil)
152                continue;
153            subDict = [[NSDictionary alloc] initWithObjectsAndKeys: submenuDict, kICServiceSubmenu, nil];
154            [submenuDict release];
155        } else continue;
156        if (dict == nil) {
157            dict = [[NSMutableDictionary alloc] init];
158        }
159        [dict setObject: subDict forKey: [item title]];
160        [subDict release];
161    }
162    return dict;
163}
164
165static void ICCF_RestoreServiceOptionsDictionary(NSMenu *menu, NSDictionary *dict) {
166    NSEnumerator *e = [dict keyEnumerator];
167    NSString *itemTitle;
168    NSDictionary *subDict, *submenuDict;
169    NSMenuItem *item;
170    NSMenu *submenu;
171
172    // XXX handle exceptions
173    while ( (itemTitle = [e nextObject]) != nil) {
174        item = (NSMenuItem *)[menu itemWithTitle: itemTitle];
175        if (item == nil) continue;
176        subDict = [dict objectForKey: itemTitle];
177        if ([[subDict objectForKey: (NSString *)kICServiceHidden] boolValue]) {
178            ICCF_SetServiceState(item, ICCF_SERVICE_HIDDEN);
179            ICCF_RemoveKeyEquivalentForItem(item);
180        }
181        if ( (submenu = [item submenu]) != nil) {
182            submenuDict = [subDict objectForKey: (NSString *)kICServiceSubmenu];
183            if ([submenuDict count] == 0)
184                ICCF_PropagateServiceStateChange(submenu, ICCF_SERVICE_HIDDEN);
185            else
186                ICCF_RestoreServiceOptionsDictionary(submenu, submenuDict);
187        }
188    }
189}
190
191static void ICCF_AddServiceKeyEquivalentsAndIcons(NSMenu *menu, NSDictionary *serviceInfo) {
192    if (serviceInfo == nil) return;
193    NSEnumerator *enumerator = [[menu itemArray] objectEnumerator];
194    NSMenuItem *menuItem;
195    NSMenu *submenu;
196    NSDictionary *itemInfo = nil;
197    while ( (menuItem = [enumerator nextObject]) != nil) {
198        itemInfo = [serviceInfo objectForKey: [menuItem title]];
199        if (itemInfo == nil) continue;
200       
201        if ( (submenu = [menuItem submenu]) != nil) {
202            ICCF_AddServiceKeyEquivalentsAndIcons(submenu, [itemInfo objectForKey: (NSString *)kICServiceSubmenu]);
203        } else {
204            NSString *keyEquivalent = (NSString *)[itemInfo objectForKey: (NSString *)kICServiceShortcut];
205            if (keyEquivalent != nil) {
206                [menuItem setToolTip: keyEquivalent];
207                NSMutableSet *equivalentItems = (NSMutableSet *)[keyEquivalents objectForKey: keyEquivalent];
208                if (equivalentItems == nil) {
209                    equivalentItems = [[NSMutableSet alloc] initWithObjects: menuItem, nil];
210                    [keyEquivalents setObject: equivalentItems forKey: keyEquivalent];
211                    [equivalentItems release];
212                } else {
213                    [equivalentItems addObject: menuItem];
214                }
215            }
216        }
217
218        NSString *bundlePath = (NSString *)[itemInfo objectForKey: (NSString *)kICServiceBundlePath];
219        if (bundlePath == NULL) continue;
220        IconRef serviceIcon = ICCF_CopyIconRefForPath(bundlePath);
221        if (serviceIcon == NULL) continue;
222        [menuItem _setIconRef: serviceIcon];
223        ReleaseIconRef(serviceIcon);
224    }
225}
226
227@implementation ICeCoffEEServicePrefController
228
229#pragma mark class initialization
230
231+ (void)initialize;
232{
233    ICCF_SERVICE_OPTION_HIDDEN = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithBool: YES], kICServiceHidden, nil];
234}
235
236#pragma mark initialize-release
237
238- (id)initWithParentWindow:(NSWindow *)parent;
239{
240    if ( (self = [self initWithWindowNibName: @"Select services"])) {
241        NSWindow *window = [self window]; // connect outlets
242        [serviceOutline setAutoresizesOutlineColumn: NO];
243       
244        NSButtonCell *checkBoxCell = [[ICeCoffEENonHighlightingButtonCell alloc] init];
245        [checkBoxCell setButtonType: NSSwitchButton];
246        [checkBoxCell setImagePosition: NSImageOnly];
247        [checkBoxCell setAllowsMixedState: YES];
248        [[serviceOutline tableColumnWithIdentifier: @"show"] setDataCell: checkBoxCell];
249        [checkBoxCell release];
250
251        NSTextFieldCell *textFieldCell = [[serviceOutline tableColumnWithIdentifier: @"service"] dataCell];
252        [textFieldCell setWraps: YES];
253        [[serviceOutline tableColumnWithIdentifier: @"service"] setDataCell:
254            [ICeCoffEELabeledIconCell copyFromTextFieldCell: textFieldCell]];
255       
256        textFieldCell = [[serviceOutline tableColumnWithIdentifier: @"key"] dataCell];
257        ((struct objc_object *)textFieldCell)->isa = [ICeCoffEEInvertingTextFieldCell class];
258       
259        [serviceOutline noteNumberOfRowsChanged]; // or we get no active scroll bar or initial selection
260
261        [window setResizeIncrements: NSMakeSize(1, ICCF_TableViewCellHeight(serviceOutline))];
262        if (parent != nil) {
263            [NSApp beginSheet: window modalForWindow: parent modalDelegate: self didEndSelector: nil contextInfo: nil];
264        } else {
265            [window center];
266            [window makeKeyAndOrderFront: nil];
267        }
268    }
269    return self;
270}
271
272- (void)dealloc;
273{
274    [keyEquivalents release];
275    [servicesMenu release];
276    [super dealloc];
277}
278
279#pragma mark actions
280
281- (IBAction)showAll:(NSButton *)sender;
282{
283    ICCF_PropagateServiceStateChange(servicesMenu, nil);
284    [serviceOutline reloadData];
285}
286
287- (IBAction)hideAll:(NSButton *)sender;
288{
289    ICCF_PropagateServiceStateChange(servicesMenu, ICCF_SERVICE_HIDDEN);
290    [serviceOutline reloadData];
291}
292
293- (void)closeWithReturnCode:(int)returnCode;
294{
295    if ([[self window] isSheet]) {
296        [NSApp endSheet: [self window] returnCode: NSRunAbortedResponse];
297    }
298    [self close];
299}
300
301- (IBAction)cancel:(NSButton *)sender;
302{
303    [self closeWithReturnCode: NSRunAbortedResponse];
304}
305
306- (IBAction)saveChanges:(NSButton *)sender;
307{
308    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
309    NSMutableDictionary *icDefaults = [[defaults persistentDomainForName: (NSString *)kICBundleIdentifier] mutableCopy];
310    NSDictionary *serviceOptions = ICCF_RetainedServiceOptionsDictionary(servicesMenu);
311    if (serviceOptions != nil) {
312        [icDefaults setObject: serviceOptions forKey: (NSString *)kICServiceOptions];
313        [serviceOptions release];
314    } else {
315        [icDefaults removeObjectForKey: (NSString *)kICServiceOptions];
316    }
317    [defaults setPersistentDomain: icDefaults forName: (NSString *)kICBundleIdentifier];
318    [defaults synchronize];
319    APEMessageBroadcast(kICBundleIdentifier, kICPreferencesChanged, NULL);
320    [icDefaults release];
321    [self closeWithReturnCode: NSRunStoppedResponse];
322}
323
324@end
325
326@implementation ICeCoffEEServicePrefController (NSOutlineViewDataSource)
327
328- (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item;
329{
330    if (servicesMenu == nil) {
331        keyEquivalents = [[NSMutableDictionary alloc] init];
332        servicesMenu = [[NSMenu alloc] initWithTitle: @""];
333        ICCF_SetServicesMenu(servicesMenu);
334        ICCF_AddServiceKeyEquivalentsAndIcons(servicesMenu, ICCF_GetServicesInfo());
335        ICCF_RemoveSingleKeyEquivalents();
336       
337        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
338        NSDictionary *icDefaults = [defaults persistentDomainForName: (NSString *)kICBundleIdentifier];
339        NSDictionary *serviceOptions = [icDefaults objectForKey: (NSString *)kICServiceOptions];
340        ICCF_RestoreServiceOptionsDictionary(servicesMenu, serviceOptions);
341    }
342    return [(item == nil ? servicesMenu : [item submenu]) numberOfItems];
343}
344
345- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item;
346{
347    return (item == nil ? YES : [item submenu] != nil);
348}
349
350- (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item;
351{
352    return [(item == nil ? servicesMenu : [item submenu]) itemAtIndex: index];
353}
354
355NSAttributedString *ICCF_KeyEquivalentAttributedStringWithModifierFlags(NSString *self, unsigned int modifierFlags, unsigned count);
356
357- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item;
358{
359    if ([[tableColumn identifier] isEqualToString: @"service"]) {
360        static NSDictionary *attrDict = nil;
361        if (attrDict == nil) { // XXX we leak this, but so does the Apple sample code...
362            NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
363            [style setLineBreakMode: NSLineBreakByTruncatingMiddle];
364            attrDict = [[NSDictionary alloc] initWithObjectsAndKeys: style, NSParagraphStyleAttributeName, nil];
365            [style release];
366        }
367        return [[[NSAttributedString alloc] initWithString: [item title] attributes: attrDict] autorelease];
368    } else if ([[tableColumn identifier] isEqualToString: @"show"]) {
369        int state = ICCF_GetServiceState(item);
370        if ([item submenu] != nil && state == ICCF_SERVICE_UNKNOWN) {
371            return [NSNumber numberWithInt: ICCF_PropagateServiceState(item, nil)];
372        }
373        return [NSNumber numberWithInt: ICCF_ServiceItemState(item)];
374    } else if ([[tableColumn identifier] isEqualToString: @"key"]) {
375        NSString *equivalent = [item toolTip];
376        if (equivalent == nil || [equivalent length] != 1) return nil;
377        // XXX Inconsistency between Cocoa and Carbon: always command-shift in Carbon, not in Cocoa.  Since we only patch Cocoa for the moment, keep as is.
378        return ICCF_KeyEquivalentAttributedStringWithModifierFlags(equivalent, [item keyEquivalentModifierMask] | ([[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember: [equivalent characterAtIndex: 0]] ? NSShiftKeyMask : 0), ICCF_CountForKeyEquivalent(equivalent));
379    }
380    return nil;
381}
382
383- (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item;
384{
385    if ([[tableColumn identifier] isEqualToString: @"show"]) {
386        int newState = [object boolValue] ? ICCF_SERVICE_SHOWN : ICCF_SERVICE_HIDDEN;
387        ICCF_UpdateKeyEquivalentForItem(item, newState);
388        ICCF_SetServiceState(item, newState);
389       
390        NSMenu *submenu = [item menu];
391        if (submenu == servicesMenu) {
392            ICCF_PropagateServiceState(item, item);
393        } else {
394            NSMenu *supermenu;
395            while ( (supermenu = [submenu supermenu]) != servicesMenu) {
396                submenu = supermenu;
397            }
398            ICCF_PropagateServiceState((NSMenuItem *)[supermenu itemAtIndex: [supermenu indexOfItemWithSubmenu: submenu]], item);
399        }
400        [outlineView reloadData];
401    }
402}
403
404@end
405
406@implementation ICeCoffEEServicePrefController (NSOutlineViewDelegate)
407
408- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item;
409{
410    if (![[tableColumn identifier] isEqualToString: @"service"])
411        return;
412    [(ICeCoffEELabeledIconCell *)cell setIconRef: [item _iconRef]];
413}
414
415@end
416
417@implementation ICeCoffEEServicePrefController (NSWindowDelegate)
418
419- (NSRect)windowWillUseStandardFrame:(NSWindow *)sender defaultFrame:(NSRect)defaultFrame;
420{
421    NSWindow *window = [serviceOutline window];
422    NSRect frame = [window frame];
423    NSScrollView *scrollView = [serviceOutline enclosingScrollView];
424    float displayedHeight = [[scrollView contentView] bounds].size.height;
425    float heightChange = [[scrollView documentView] bounds].size.height - displayedHeight;
426    float heightExcess;
427
428    if (heightChange >= 0 && heightChange <= 1) {
429        // either the window is already optimal size, or it's too big
430        float rowHeight = ICCF_TableViewCellHeight(serviceOutline);
431        heightChange = (rowHeight * [serviceOutline numberOfRows]) - displayedHeight;
432    }
433
434    frame.size.height += heightChange;
435
436    if ( (heightExcess = [window minSize].height - frame.size.height) > 1 ||
437         (heightExcess = [window maxSize].height - frame.size.height) < 1) {
438        heightChange += heightExcess;
439        frame.size.height += heightExcess;
440    }
441
442    frame.origin.y -= heightChange;
443
444    return frame;
445}
446
447@end
448
449@implementation ICeCoffEEServicePrefController (NSWindowNotifications)
450
451- (void)windowWillClose:(NSNotification *)notification;
452{
453    [self autorelease];
454}
455
456@end
Note: See TracBrowser for help on using the repository browser.