1//
2//  PXToolPaletteController.m
3//  Pixen-XCode
4//
5// Copyright (c) 2004 Open Sword Group
6
7// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
8// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use,
9//copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
10// to whom the Software is furnished to do so, subject to the following conditions:
11
12// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
16// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18
19//  Author : Andy Matuschak
20
21
22#import "PXToolPaletteController.h"
23#import "PXToolSwitcher.h"
24#import "PXPanelManager.h"
25
26
27#import <Foundation/NSNotification.h>
28#import <Foundation/NSUserDefaults.h>
29
30#import <AppKit/NSButton.h>
31#import <AppKit/NSColorPanel.h>
32#import <AppKit/NSNibLoading.h>
33#import <AppKit/NSEvent.h>
34#import <AppKit/NSPanel.h>
35
36
37static PXToolPaletteController *singleInstance = nil;
38
39@interface PXToolPaletteController (Private)
40- (void)_openRightToolSwitcher;
41- (void)_closeRightToolSwitcher;
42- (void)_paletteControllerChoseLeftColor:(NSNotification*) aNotification;
43- (void)_paletteControllerChoseRightColor:(NSNotification *)aNotification;
44@end
45
46
47//
48// PXToolPalette : Private categories
49//
50
51@implementation PXToolPaletteController (Private)
52
53- (void)_openRightToolSwitcher
54{
55	[minimalView setFrameOrigin:NSMakePoint(0, [rightSwitchView frame].size.height)];
56	[rightSwitchView setFrameOrigin:NSMakePoint(0, 0)];
57	[panel setFrame:NSMakeRect([panel frame].origin.x, [panel frame].origin.y-[rightSwitchView frame].size.height, [panel frame].size.width, [panel frame].size.height+[rightSwitchView frame].size.height)
58			display:YES
59			animate:NO];
60	[triangle setState:NSOnState];
61	[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"PXRightToolSwitcherIsOpen"];
62}
63
64- (void)_closeRightToolSwitcher
65{
66	[minimalView setFrameOrigin:NSMakePoint(0, 0)];
67	[rightSwitchView setFrameOrigin:NSMakePoint(0, 0-[rightSwitchView frame].size.height)];
68	[panel setFrame:NSMakeRect([panel frame].origin.x, [panel frame].origin.y+[rightSwitchView frame].size.height, [panel frame].size.width, [panel frame].size.height-[rightSwitchView frame].size.height)
69			display:YES
70			animate:NO];
71
72	[triangle setState:NSOffState];
73	[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"PXRightToolSwitcherIsOpen"];
74}
75
76- (void)_paletteControllerChoseLeftColor:(NSNotification*) aNotification
77{
78	[leftSwitcher setColor:[[aNotification userInfo] objectForKey:@"color"]];
79}
80
81- (void)_paletteControllerChoseRightColor:(NSNotification *)aNotification
82{
83	[rightSwitcher setColor:[[aNotification userInfo] objectForKey:@"color"]];
84}
85
86@end
87
88
89//
90// PXToolPaletteController implementation
91//
92
93@implementation PXToolPaletteController
94
95
96-(id) init
97{
98	if ( singleInstance )
99    {
100		[self dealloc];
101		return singleInstance;
102    }
103
104	if ( ! (self = [super init] ) )
105		return nil;
106
107	if ( ! [NSBundle loadNibNamed:@"PXToolPalette" owner:self] )
108    {
109		//NSLog(@"warm the user here ?? ");
110		[self dealloc];
111		return nil;
112    }
113
114	singleInstance = self;
115	return singleInstance;
116}
117
118
119- (void)dealloc
120{
121    [leftSwitcher release];
122    [rightSwitcher release];
123    [super dealloc];
124}
125
126+(id) sharedToolPaletteController
127{
128	if(! singleInstance )
129		singleInstance = [[self alloc] init];
130
131	return singleInstance;
132}
133
134- (void)leftToolDoubleClicked:notification
135{
136	[[PXPanelManager sharedManager] toggleLeftToolProperties:nil];
137}
138
139- (void)rightToolDoubleClicked:notification
140{
141	[[PXPanelManager sharedManager] toggleRightToolProperties:nil];
142}
143
144-(void) awakeFromNib
145{
146	[[NSColorPanel sharedColorPanel] setShowsAlpha:YES];
147	[panel setBecomesKeyOnlyIfNeeded:YES];
148
149	[leftSwitcher useToolTagged:PXPencilToolTag];
150	[rightSwitcher useToolTagged:PXEraserToolTag];
151
152	[[NSNotificationCenter defaultCenter] addObserver:self
153											 selector:@selector(_paletteControllerChoseLeftColor:)
154												 name:@"PXPaletteLeftColorChosen"
155											   object:nil];
156
157	[[NSNotificationCenter defaultCenter] addObserver:self
158											 selector:@selector(_paletteControllerChoseRightColor:)
159												 name:@"PXPaletteRightColorChosen"
160											   object:nil];
161
162	[[NSNotificationCenter defaultCenter] addObserver:self
163											 selector:@selector(leftToolDoubleClicked:)
164												 name:@"PXToolDoubleClicked"
165											   object:leftSwitcher];
166
167	[[NSNotificationCenter defaultCenter] addObserver:self
168											 selector:@selector(rightToolDoubleClicked:)
169												 name:@"PXToolDoubleClicked"
170											   object:rightSwitcher];
171
172	if([[NSUserDefaults standardUserDefaults] boolForKey:@"PXRightToolSwitcherIsOpen"])
173		[self _openRightToolSwitcher];
174
175	[panel setFrameAutosaveName:@"PXToolPaletteFrame"];
176	keyMask = 0x0;
177}
178
179
180//Action method
181- (IBAction)disclosureClicked:sender
182{
183	if([sender state] == NSOnState)
184		[self _openRightToolSwitcher];
185	else
186		[self _closeRightToolSwitcher];
187}
188
189
190//Events methods
191- (void)keyDown:(NSEvent *)event
192{
193	if([event modifierFlags] & NSControlKeyMask)
194		[rightSwitcher keyDown:event];
195	else
196		[leftSwitcher keyDown:event];
197}
198
199- (BOOL)keyWasDown:(unsigned int)mask
200{
201    return (keyMask & mask) == mask;
202}
203
204- (BOOL)isMask:(unsigned int)newMask upEventForModifierMask:(unsigned int)mask
205{
206    return [self keyWasDown:mask] && ((newMask & mask) == 0x0000);
207}
208
209- (BOOL)isMask:(unsigned int)newMask downEventForModifierMask:(unsigned int)mask
210{
211    return ![self keyWasDown:mask] && ((newMask & mask) == mask);
212}
213
214- (void)flagsChanged:(NSEvent *)theEvent
215{
216	if([self isMask:[theEvent modifierFlags] downEventForModifierMask:NSAlternateKeyMask])
217    {
218		[leftSwitcher optionKeyDown];
219		[rightSwitcher optionKeyDown];
220		keyMask |= NSAlternateKeyMask;
221    }
222	else if([self isMask:[theEvent modifierFlags] upEventForModifierMask:NSAlternateKeyMask])
223    {
224		[leftSwitcher optionKeyUp];
225		[rightSwitcher optionKeyUp];
226		keyMask ^= NSAlternateKeyMask;
227    }
228
229	if([self isMask:[theEvent modifierFlags] downEventForModifierMask:NSShiftKeyMask])
230    {
231		[leftSwitcher shiftKeyDown];
232		[rightSwitcher shiftKeyDown];
233		keyMask |= NSShiftKeyMask;
234    }
235	else if([self isMask:[theEvent modifierFlags] upEventForModifierMask:NSShiftKeyMask])
236    {
237		[leftSwitcher shiftKeyUp];
238		[rightSwitcher shiftKeyUp];
239		keyMask ^= NSShiftKeyMask;
240    }
241}
242
243//
244//Accessors methods
245//
246-(id) leftTool
247{
248	return [leftSwitcher tool];
249}
250
251-(id)rightTool
252{
253	return [rightSwitcher tool];
254}
255
256- (PXToolSwitcher *) leftSwitcher
257{
258	return leftSwitcher;
259}
260
261- (PXToolSwitcher *) rightSwitcher
262{
263	return rightSwitcher;
264}
265
266-(NSPanel *) toolPanel
267{
268	return panel;
269}
270
271
272@end
273