-
Notifications
You must be signed in to change notification settings - Fork 0
/
Toolbar.m
267 lines (217 loc) · 9.12 KB
/
Toolbar.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//
// Toolbar.m
// CronniX
//
// Created by sas on Sun Jun 10 2001.
// Copyright (c) 2001 __CompanyName__. All rights reserved.
//
#import "Toolbar.h"
#import "UserImageView.h"
NSString *ItemName1 = @"New";
NSString *ItemName2 = @"Delete";
NSString *ItemName3 = @"Open";
NSString *ItemName4 = @"Save";
NSString *ItemName5 = @"User";
NSString *ItemName6 = @"RunNow";
NSString *ItemName7 = @"InsertProgram";
NSString *ItemName8 = @"Edit";
@implementation Toolbar
- (id)initWithController: (crController *)aController {
if ( self = [ super init ] ) {
NSString *itemName;
NSToolbarItem *item;
controller = aController;
// toolbar setup ---------------------------------
items = [ [ NSMutableDictionary alloc ] init ];
// new entry item
itemName = ItemName1;
item = [ [ NSToolbarItem alloc ] initWithItemIdentifier: itemName ];
[ item setPaletteLabel: NSLocalizedString( @"New", @"toolbar item name" ) ]; // name for the "Customize Toolbar" sheet
[ item setLabel: NSLocalizedString( @"New", @"toolbar item name" ) ]; // name for the item in the toolbar
[ item setToolTip: NSLocalizedString( @"Create new crontab entry", @"toolbar item tooltip" ) ]; // tooltip
[ item setTarget: self ]; // what should happen when it's clicked
[ item setAction: @selector(newLineToolbarItemClicked:) ];
[ items setObject: item forKey: itemName ]; // add to toolbar list
[ self setImageWithName: @"new" forItem: item ];
[ item release ];
// remove entry item
itemName = ItemName2;
item = [ [ NSToolbarItem alloc ] initWithItemIdentifier: itemName ];
[ item setPaletteLabel: NSLocalizedString( @"Delete", @"toolbar item name" ) ];
[ item setLabel: NSLocalizedString( @"Delete", @"toolbar item name" ) ];
[ item setToolTip: NSLocalizedString( @"Remove crontab entry", @"toolbar item tooltip" ) ];
[ item setTarget: self ];
[ item setAction: @selector(removeLineToolbarItemClicked:) ];
[ items setObject: item forKey: itemName ];
[ self setImageWithName: @"delete" forItem: item ];
[ item release ];
// "open for user" entry item
itemName = ItemName3;
item = [ [ NSToolbarItem alloc ] initWithItemIdentifier: itemName ];
[ item setPaletteLabel: NSLocalizedString( @"Open", @"toolbar item name" ) ];
[ item setLabel: NSLocalizedString( @"Open", @"toolbar item name" ) ];
[ item setToolTip: NSLocalizedString( @"Open crontab for different user", @"toolbar item tooltip" ) ];
[ item setTarget: self ];
[ item setAction: @selector(openForUserToolbarItemClicked:) ];
[ items setObject: item forKey: itemName ];
[ self setImageWithName: @"user" forItem: item ];
[ item release ];
// "Save" entry item
itemName = ItemName4;
item = [ [ NSToolbarItem alloc ] initWithItemIdentifier: itemName ];
[ item setPaletteLabel: NSLocalizedString( @"Save", @"toolbar item name" ) ];
[ item setLabel: NSLocalizedString( @"Save", @"toolbar item name" ) ];
[ item setToolTip: NSLocalizedString( @"Save crontab", @"toolbar item tooltip" ) ];
[ item setTarget: self ];
[ item setAction: @selector(writeCrontabToolbarItemClicked:) ];
[ items setObject: item forKey: itemName ];
[ self setImageWithName: @"save" forItem: item ];
[ item release ];
// "user" entry item
itemName = ItemName5;
item = [ [ NSToolbarItem alloc ] initWithItemIdentifier: itemName ];
[ item setPaletteLabel: NSLocalizedString( @"Current crontab", @"toolbar palette label" ) ];
[ item setLabel: NSLocalizedString( @"Current crontab", @"toolbar label" ) ];
[ item setToolTip: NSLocalizedString( @"Current crontab", @"toolbar item tooltip" ) ];
[ items setObject: item forKey: itemName ];
useritem = item;
[ self setUser: [ controller crontabForUser ] ];
[ item release ];
// "RunNow" entry item
itemName = ItemName6;
item = [ [ NSToolbarItem alloc ] initWithItemIdentifier: itemName ];
[ item setPaletteLabel: NSLocalizedString( @"Run now", @"toolbar item name" ) ];
[ item setLabel: NSLocalizedString( @"Run Now", @"toolbar item name" ) ];
[ item setToolTip: NSLocalizedString( @"Run task now", @"toolbar item tooltip" ) ];
[ item setTarget: self ];
[ item setAction: @selector(runNowToolbarItemClicked:) ];
[ items setObject: item forKey: itemName ];
[ self setImageWithName: @"runnow" forItem: item ];
[ item release ];
// "Edit" entry item
itemName = ItemName8;
item = [ [ NSToolbarItem alloc ] initWithItemIdentifier: itemName ];
[ item setPaletteLabel: NSLocalizedString( @"Edit", @"toolbar item name" ) ];
[ item setLabel: NSLocalizedString( @"Edit", @"toolbar item name" ) ];
[ item setToolTip: NSLocalizedString( @"Edit task", @"toolbar item tooltip" ) ];
[ item setTarget: self ];
[ item setAction: @selector(editToolbarItemClicked:) ];
[ items setObject: item forKey: itemName ];
[ self setImageWithName: @"edit" forItem: item ];
[ item release ];
// create toolbar
toolbar = [ [ NSToolbar alloc ] initWithIdentifier: @"Toolbar" ];
[ toolbar setDelegate: self ];
[ toolbar setAllowsUserCustomization: YES ];
[ toolbar setAutosavesConfiguration: YES ];
[ [ controller mainWindow ] setToolbar:toolbar];
}
return self;
}
- (void)dealloc {
[ toolbar release ];
[ items release ];
}
// --------------------------------------------------------------
// delegates
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag {
return [ items objectForKey:itemIdentifier];
}
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar {
NSMutableArray *arr = [ NSMutableArray array ];
[ arr addObject: ItemName1 ];
[ arr addObject: ItemName2 ];
[ arr addObject: ItemName3 ];
[ arr addObject: ItemName4 ];
[ arr addObject: ItemName5 ];
[ arr addObject: ItemName8 ];
return arr;
}
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar {
return [ items allKeys];
}
- (int)count {
return [ items count];
}
- (BOOL)validateToolbarItem: (NSToolbarItem *)item {
// delete button
if ( [ item isEqual: [ items objectForKey: ItemName2 ] ] ) {
return ( [ controller selectedRow ] != -1 );
}
// save button
if ( [ item isEqual: [ items objectForKey: ItemName4 ] ] ) {
return ( [ controller isDirty ] );
}
// run now button
if ( [ item isEqual: [ items objectForKey: ItemName6 ] ] ) {
return ( [ controller selectedRow ] != -1 );
}
// edit button
if ( [ item isEqual: [ items objectForKey: ItemName8 ] ] ) {
return ( [ controller selectedRow ] != -1 );
}
return YES;
}
// actions
- (void)customize:(id)sender {
[toolbar runCustomizationPalette:sender];
}
- (void)showhide:(id)sender {
[toolbar setVisible:![toolbar isVisible]];
}
- (void)newLineToolbarItemClicked:(NSToolbarItem*)item {
//[ controller newLine: nil ];
[ controller newLineWithDialog ];
}
- (void)removeLineToolbarItemClicked:(NSToolbarItem*)item {
[ controller removeLine: nil ];
}
- (void)openForUserToolbarItemClicked:(NSToolbarItem*)item {
[ controller crontabShouldLoad ];
}
- (void)writeCrontabToolbarItemClicked:(NSToolbarItem*)item {
[ controller writeCrontab ];
}
- (void)runNowToolbarItemClicked:(NSToolbarItem*)item {
[ controller runSelectedCommand ];
}
- (void)editToolbarItemClicked:(NSToolbarItem*)item {
[ controller editSelectedTask ];
}
- (void)setUser: (NSString *)username {
/*
NSImage *itemImage = nil;
UserImageRep *userImageRep;
userImageRep = [ [ UserImageRep alloc ] initWithUserName: username ? username : NSUserName() ];
// itemImage = [ [ NSImage alloc ] initWithSize: [ userImageRep size ] ];
[ useritem setMaxSize: NSMakeSize( 100, 100 ) ];
[ useritem setMinSize: NSMakeSize( 100, 100 ) ];
itemImage = [ [ NSImage alloc ] initWithSize: NSMakeSize( 100, 100 ) ];
if ( itemImage ) {
[ itemImage addRepresentation: userImageRep ];
[ useritem setImage: itemImage ];
}
[ itemImage release ];
[ userImageRep release ];
*/
UserImageView *userView = [ [ UserImageView alloc ] init ];
[ userView setUser: username ? username : NSUserName() ];
[ useritem setMaxSize: [ userView size ] ];
[ useritem setMinSize: [ userView size ] ];
[ useritem setView: userView ];
[ userView release ];
}
- (BOOL)isVisible {
return [ toolbar isVisible ];
}
- (void) setImageWithName: (NSString *)name forItem: (NSToolbarItem *)item {
NSImage *itemImage = nil;
NSBundle *thisBundle = [ NSBundle bundleForClass: [controller class] ];
NSString *imagePath = [ thisBundle pathForResource: name ofType: @"tiff" ];
if ( imagePath )
itemImage = [ [ NSImage alloc ] initWithContentsOfFile: imagePath ];
if ( itemImage )
[ item setImage: itemImage ];
[ itemImage release ];
}
@end