-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNSPopUpButton+Dejal.m
179 lines (131 loc) · 5.37 KB
/
NSPopUpButton+Dejal.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// NSPopUpButton+Dejal.m
// Dejal Open Source Categories
//
// Created by David Sinclair on Fri Mar 12 2004.
// Copyright (c) 2004-2015 Dejal Systems, LLC. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#import "NSPopUpButton+Dejal.h"
#import "NSString+Dejal.h"
@implementation NSPopUpButton (Dejal)
/**
Adds a separator item to the menu.
@author DJS 2006-10.
*/
- (void)dejal_addSeparatorItem;
{
[[self menu] addItem:[NSMenuItem separatorItem]];
[self synchronizeTitleAndSelectedItem];
}
/**
Similar to -addItemWithTitle:, but allows specifying a tag. It does not remove any other items with the same name, however, as -[NSPopUpButton addItemWithTitle:] does. It also returns the created item.
@author DJS 2004-03.
*/
- (NSMenuItem *)dejal_addItemWithTitle:(NSString *)aString tag:(NSInteger)tag
{
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:aString action:nil keyEquivalent:@""];
[item setTag:tag];
[[self menu] addItem:item];
[self synchronizeTitleAndSelectedItem];
return item;
}
/**
Similar to -addItemWithTitle:, but allows specifying a tag and represented object. It does not remove any other items with the same name, however, as -[NSPopUpButton addItemWithTitle:] does. It also returns the created item.
@author DJS 2004-03.
*/
- (NSMenuItem *)dejal_addItemWithTitle:(NSString *)aString tag:(NSInteger)tag representedObject:(id)object
{
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:aString action:nil keyEquivalent:@""];
[item setTag:tag];
[item setRepresentedObject:object];
[[self menu] addItem:item];
[self synchronizeTitleAndSelectedItem];
return item;
}
/**
Given an array of file paths, adds items to the receiver. The item titles are the last path components sans extensions, and the item tags are set as requested. The item represented objects are set to the abbreviated or full paths, based on the abbreviatedObject value. If prefixDivider is YES, a divider line is added before the other items, iff there are any. You can call -pathsWithExtension:atPath:deepScan: (in NSFileManager+Dejal) to get the paths.
@author DJS 2005-05.
@version DJS 2014-01: changed to support a default path.
*/
- (void)dejal_addItemsWithPaths:(NSArray *)paths
prefixDivider:(BOOL)prefixDivider
tag:(NSInteger)tag
abbreviatedObject:(BOOL)abbreviatedObject
defaultPath:(NSString *)defaultPath;
{
NSString *path;
for (path in paths)
{
if (prefixDivider)
{
[[self menu] addItem:[NSMenuItem separatorItem]];
prefixDivider = NO;
}
NSString *title = [path dejal_lastPathComponentWithoutExtension];
NSString *object = path;
if (defaultPath && ![object hasPrefix:@"/"])
object = [defaultPath stringByAppendingPathComponent:object];
if (abbreviatedObject)
object = [object dejal_abbreviatedPath];
[self dejal_addItemWithTitle:title tag:tag representedObject:object];
}
[self synchronizeTitleAndSelectedItem];
}
/**
Convenience method to return the menu item with the given tag.
@author DJS 2004-03.
*/
- (NSMenuItem *)dejal_itemWithTag:(NSInteger)aTag
{
NSMenu *menu = [self menu];
NSMenuItem *item = [menu itemWithTag:aTag];
return item;
}
/**
Convenience method to return the tag of the selected item, similar to indexOfSelectedItem etc.
@author DJS 2007-07.
*/
- (NSInteger)dejal_tagOfSelectedItem;
{
return [[self selectedItem] tag];
}
/**
Convenience method to select the menu item with the given represented object.
@author DJS 2005-05.
*/
- (NSInteger)dejal_selectItemWithRepresentedObject:(id)anObj
{
NSUInteger i = [self indexOfItemWithRepresentedObject:anObj];
[self selectItemAtIndex:i];
return i;
}
/**
Convenience method to return the represented object of the selected item, similar to indexOfSelectedItem etc.
@author DJS 2010-07.
*/
- (id)dejal_representedObjectOfSelectedItem;
{
return [[self selectedItem] representedObject];
}
@end