-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPZPreferencesWindowController.m
409 lines (347 loc) · 14.3 KB
/
PZPreferencesWindowController.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//
// PZPreferencesWindowController.m
// PzzApp
//
// Created by Ana Cañizares on 01.04.12.
// Copyright 2012 PilPel Soft. All rights reserved.
//
#import "PZPreferencesWindowController.h"
#import "NSUserDefaultsWithExtras.h"
static PZPreferencesWindowController *_sharedPZPreferencesWindowController = nil;
@interface PZPreferencesWindowController()
- (void) updateGeneralView;
- (void) updateCurrentPuzzleView;
@end
@implementation PZPreferencesWindowController
- (id) init
{
self = [super initWithWindowNibName:@"Preferences"];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(updateGeneralView)
name: NSUserDefaultsDidChangeNotification
object: nil];
// TODO: apuntarse a PZWindowPreferencesUpdated
} else {
[self release];
return nil;
}
return self;
}
+ (PZPreferencesWindowController *) sharedPZPreferencesWindowController
{
if (!_sharedPZPreferencesWindowController) {
_sharedPZPreferencesWindowController = [[self alloc] init];
}
return _sharedPZPreferencesWindowController;
}
- (void) dealloc {
[super dealloc];
}
/*
-(NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar {
return [[toolbar items] valueForKey:@"itemIdentifier"];
}
*/
-(void)awakeFromNib {
[self.window setContentSize:[generalPreferenceView frame].size];
[[self.window contentView] addSubview:generalPreferenceView];
[bar setSelectedItemIdentifier:@"General"];
[self.window center];
[self updateGeneralView];
}
-(NSView *)viewForTag:(int)tag {
NSView *view = nil;
switch(tag) {
case 0: default: view = generalPreferenceView; break;
case 1: view = currentPuzzlePreferenceView; break;
}
return view;
}
-(NSRect)newFrameForNewContentView:(NSView *)view {
NSRect newFrameRect = [self.window frameRectForContentRect:[view frame]];
NSRect oldFrameRect = [self.window frame];
NSSize newSize = newFrameRect.size;
NSSize oldSize = oldFrameRect.size;
NSRect frame = [self.window frame];
frame.size = newSize;
frame.origin.y -= (newSize.height - oldSize.height);
return frame;
}
-(IBAction)switchView:(id)sender {
int tag = [sender tag];
NSView *view = [self viewForTag:tag];
NSView *previousView = [self viewForTag: currentViewTag];
currentViewTag = tag;
NSRect newFrame = [self newFrameForNewContentView:view];
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.1];
if ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask)
[[NSAnimationContext currentContext] setDuration:1.0];
[[[self.window contentView] animator] replaceSubview:previousView with:view];
[[self.window animator] setFrame:newFrame display:YES];
[NSAnimationContext endGrouping];
}
- (void) updateGeneralView {
NSUserDefaults *defaults = [[NSUserDefaultsController sharedUserDefaultsController]
defaults];
NSString *defImageURL = [[defaults persistentDomainForName:@"com.pilpelsoft.PzzApp"]
objectForKey:@"ImageURL"];
if (defImageURL) {
//show image in viewer if there is one in our persistent domain
NSImage *defImage = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:defImageURL]];
[generalImageView setImage:defImage];
[generalImageViewLabel setAlphaValue:0.0];
[defImage release];
}
else {
[generalImageView setImage:nil];
[generalImageViewLabel setAlphaValue:1.0];
}
[generalLevelPopUp selectItemWithTag:[defaults integerForKey:@"Level"]];
if ([defaults integerForKey:@"Rows"] == [defaults integerForKey:@"Columns"])
[generalSizePopUp selectItemWithTag:[defaults integerForKey:@"Rows"]];
else
NSLog(@"Error: different values for keys Rows and Columns.");
[generalColorWell setColor:[defaults colorForKey:@"BackgroundColor"]];
if ([defaults boolForKey:@"ShowRestartSheet"] &&
[defaults boolForKey:@"ShowShuffleSheet"] &&
[defaults boolForKey:@"ShowChangeLevelSheet"] &&
[defaults boolForKey:@"ShowChangeSizeSheet"] &&
[defaults boolForKey:@"ShowChooseEmptyBlockSheet"] &&
[defaults boolForKey:@"ShowChooseImageSheet"])
[showAlertCheckBox setState:NSOnState];
else
[showAlertCheckBox setState:NSOffState];
}
- (void) updateCurrentPuzzleView {
// TODO: responder a PZWindowPreferencesUpdated
}
-(IBAction) chooseImage: (id) sender {
NSOpenPanel* oPanel = [NSOpenPanel openPanel];
[oPanel setCanChooseDirectories:NO];
[oPanel setCanChooseFiles:YES];
[oPanel setCanCreateDirectories:YES];
[oPanel setAllowsMultipleSelection:NO];
[oPanel setAlphaValue:0.95];
[oPanel setTitle:@"Select an image to open"];
// Allow only supported image files (this also displays the media folder
// in the sidebar)
[oPanel setAllowedFileTypes:(NSArray*)CGImageSourceCopyTypeIdentifiers()];
// Display the dialog. If the OK button was pressed, process the file
if ( [oPanel runModal] == NSOKButton ) {
NSURL *imageURL = [[oPanel URLs] lastObject];
NSImage *image = [[NSImage alloc] initWithContentsOfURL:imageURL];
[[[NSUserDefaultsController sharedUserDefaultsController] defaults]
setObject: [imageURL absoluteString] forKey:@"ImageURL"];
[generalImageView setImage:image];
[generalImageViewLabel setAlphaValue:0.0];
[image release];
}
}
- (IBAction) changeLevel: (id) sender {
[[[NSUserDefaultsController sharedUserDefaultsController] defaults]
setObject: [NSNumber numberWithInt:[sender tag]] forKey:@"Level"];
}
- (IBAction) changeSize: (id) sender {
[[[NSUserDefaultsController sharedUserDefaultsController] defaults]
setObject: [NSNumber numberWithInt:[sender tag]] forKey:@"Rows"];
[[[NSUserDefaultsController sharedUserDefaultsController] defaults]
setObject: [NSNumber numberWithInt:[sender tag]] forKey:@"Columns"];
}
- (IBAction) changeBackgroundColor: (id) sender {
[[[NSUserDefaultsController sharedUserDefaultsController] defaults]
setColor: [sender color] forKey:@"BackgroundColor"];
}
- (IBAction) configureAlertDialogs: (id) sender {
BOOL newValue = ([sender state] != 0);
[[[NSUserDefaultsController sharedUserDefaultsController] defaults]
setBool: newValue forKey:@"ShowRestartSheet"];
[[[NSUserDefaultsController sharedUserDefaultsController] defaults]
setBool: newValue forKey:@"ShowShuffleSheet"];
[[[NSUserDefaultsController sharedUserDefaultsController] defaults]
setBool: newValue forKey:@"ShowChangeLevelSheet"];
[[[NSUserDefaultsController sharedUserDefaultsController] defaults]
setBool: newValue forKey:@"ShowChangeSizeSheet"];
[[[NSUserDefaultsController sharedUserDefaultsController] defaults]
setBool: newValue forKey:@"ShowChooseEmptyBlockSheet"];
[[[NSUserDefaultsController sharedUserDefaultsController] defaults]
setBool: newValue forKey:@"ShowChooseImageSheet"];
}
- (IBAction) restorePreferences: (id) sender {
NSDictionary *inVal = [[NSUserDefaultsController sharedUserDefaultsController]
initialValues];
[[NSUserDefaultsController sharedUserDefaultsController]
revertToInitialValues: inVal];
}
- (void) imageDropped: (NSURL *) url {
[generalImageViewLabel setAlphaValue:0.0];
[[[NSUserDefaultsController sharedUserDefaultsController] defaults]
setObject: [url absoluteString] forKey:@"ImageURL"];
}
- (void) imageRemoved {
[generalImageViewLabel setAlphaValue:1.0];
[[[NSUserDefaultsController sharedUserDefaultsController] defaults]
removeObjectForKey:@"ImageURL"];
}
@end
/******************************************************************************
PZDropImageView
******************************************************************************/
//
// Created by Vladimir Boychentsov on 2/26/10.
// Copyright 2010 www.injoit.com. All rights
@implementation PZDropImageView
- (void) keyDown:(NSEvent *)theEvent
{
if ([theEvent keyCode] == 0x33) {
id <NSWindowDelegate> del = [[self window] delegate];
if ([(PZPreferencesWindowController*)del
respondsToSelector:@selector(imageRemoved)])
[(PZPreferencesWindowController*)del imageRemoved];
}
[super keyDown:theEvent];
}
//Handling mouse-dragging in a mouse-tracking loop (con nubecita)
- (void) mouseDown:(NSEvent *)theEvent {
if (![self image]) return;
BOOL keepOn = YES;
BOOL isInside = YES;
NSPoint mouseLoc;
NSCursor *cursor;
while (keepOn) {
theEvent = [[self window] nextEventMatchingMask: NSLeftMouseUpMask |
NSLeftMouseDraggedMask];
mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
isInside = [self mouse:mouseLoc inRect:[self bounds]];
switch ([theEvent type]) {
case NSLeftMouseDragged:
if (!isInside) {
cursor = [NSCursor disappearingItemCursor];
[cursor set];
}
else {
cursor = [NSCursor arrowCursor];
[cursor set];
}
break;
case NSLeftMouseUp:
if (!isInside) {
NSShowAnimationEffect(
NSAnimationEffectPoof,
[NSEvent mouseLocation],
NSZeroSize,
nil,
nil,
nil);
id <NSWindowDelegate> del = [[self window] delegate];
if ([(PZPreferencesWindowController*)del
respondsToSelector:@selector(imageRemoved)])
[(PZPreferencesWindowController*)del imageRemoved];
}
keepOn = NO;
break;
default:
/* Ignore any other kind of event. */
break;
}
};
cursor = [NSCursor arrowCursor];
[cursor set];
return;
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
== NSDragOperationGeneric)
{
//this means that the sender is offering the type of operation we want
//return that we want the NSDragOperationGeneric operation that they
//are offering
return NSDragOperationGeneric;
}
else
{
//since they aren't offering the type of operation we want, we have
//to tell them we aren't interested
return NSDragOperationNone;
}
}
- (void)draggingExited:(id <NSDraggingInfo>)sender
{
//we aren't particularily interested in this so we will do nothing
//this is one of the methods that we do not have to implement
//NSLog(@"%@", sender);
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
NSPasteboard *pboard = [sender draggingPasteboard];
if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
// NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
//NSLog(@"%@", files);
// Perform operation using the list of files
}
NSPasteboard *paste = [sender draggingPasteboard];
//gets the dragging-specific pasteboard from the sender
NSArray *types = [NSArray arrayWithObjects:NSPasteboardTypeTIFF,
NSFilenamesPboardType, nil];
//a list of types that we can accept
NSString *desiredType = [paste availableTypeFromArray:types];
NSData *carriedData = [paste dataForType:desiredType];
if (nil == carriedData)
{
//the operation failed for some reason
NSRunAlertPanel(@"Paste Error", @"Sorry, but the past operation failed",
nil, nil, nil);
return NO;
}
else
{ NSLog(@"desiredType = %@",desiredType);
//the pasteboard was able to give us some meaningful data
if ([desiredType isEqualToString:NSPasteboardTypeTIFF])
{
//we have TIFF bitmap data in the NSData object
NSImage *newImage = [[NSImage alloc] initWithData:carriedData];
[self setImage:newImage];
[newImage release];
//we are no longer interested in this so we need to release it
}
else if ([desiredType isEqualToString:NSFilenamesPboardType])
{
//we have a list of file names in an NSData object
NSArray *fileArray =
[paste propertyListForType:@"NSFilenamesPboardType"];
//be caseful since this method returns id.
//We just happen to know that it will be an array.
NSString *path = [fileArray objectAtIndex:0];
id <NSWindowDelegate> del = [[self window] delegate];
if ([(PZPreferencesWindowController*)del
respondsToSelector:@selector(imageDropped:)]) {
NSURL *imageURL = [NSURL fileURLWithPath:path];
[(PZPreferencesWindowController*)del imageDropped:imageURL];
}
//assume that we can ignore all but the first path in the list
NSImage *newImage = [[NSImage alloc] initWithContentsOfFile:path];
if (nil == newImage) {
//we failed for some reason
NSRunAlertPanel(@"File Reading Error",
@"Sorry, but I failed to open the file at \"%@\"",
nil, nil, nil, path);
return NO;
} else {
//newImage is now a new valid image
[self setImage:newImage];
}
[newImage release];
}
else
{
//this can't happen
NSAssert(NO, @"This can't happen");
return NO;
}
}
[self setNeedsDisplay:YES]; //redraw us with the new image
return YES;
}
@end