1//
2//  PXToolPropertiesController.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// Fri Mar 12 2004.
21
22
23// This is now a real singleton.
24// It just manage the panel with its position and  add/remove propertyView(s)
25// Fabien
26
27
28#import "PXToolPropertiesController.h"
29#import "PXToolPropertiesView.h"
30#import "PXToolSwitcher.h"
31#import "PXToolPaletteController.h"
32#import "PXTool.h"
33
34#import <Foundation/NSDictionary.h>
35#import <Foundation/NSNotification.h>
36#import <Foundation/NSUserDefaults.h>
37#import <AppKit/NSNibLoading.h>
38#import <AppKit/NSPanel.h>
39
40static PXToolPropertiesController *leftInstance = nil;
41static PXToolPropertiesController *rightInstance = nil;
42
43@interface PXToolPropertiesController (Private)
44-(void) _toolDidChange:(NSNotification *) notification;
45@end
46
47@implementation PXToolPropertiesController (Private)
48- (void) _toolDidChange:aNotification
49{
50	NSMutableString *title = [NSMutableString string];
51
52	[title appendString:[[[aNotification userInfo] objectForKey:@"newTool"] name]];
53	[title appendString:@" ("];
54	if (self == leftInstance) { // Kind of a HACK
55		[title appendString:NSLocalizedString(@"LEFT", @"Left")];
56	} else {
57		[title appendString:NSLocalizedString(@"RIGHT", @"Right")];
58	}
59	[title appendString:@") "];
60	[title appendString:NSLocalizedString(@"PROPERTIES", @"Properties")];
61	[panel setTitle:title];
62	if (! [[[aNotification userInfo] objectForKey:@"newTool"] propertiesView] )
63		[self setPropertiesView:[[PXToolPropertiesView alloc] init]];
64	else
65    {
66		[self setPropertiesView:[[[aNotification userInfo] objectForKey:@"newTool"] propertiesView]];
67    }
68}
69
70@end
71
72
73@implementation PXToolPropertiesController
74
75-(void) awakeFromNib
76{
77	[panel setBecomesKeyOnlyIfNeeded:YES];
78
79	if (self == leftInstance) { // Kind of a HACK
80		[panel setFrameAutosaveName:@"PXLeftToolPropertiesFrame"];
81	} else {
82		[panel setFrameAutosaveName:@"PXRightToolPropertiesFrame"];
83	}
84
85	//propbably not here to do that ???
86
87	// I think it's the right place... where else would it go?
88	//
89	// Oh, and this whole series of Tool Properties classes is just about the worst
90	// design EVER.  Andy, YOU FAIL IT.  They're all NSViews which are acting as both
91	// models and controllers. >_<
92	//
93	//      -Ian
94
95	[self setPropertiesView: [[PXToolPropertiesView alloc] init]];
96
97}
98
99- (void)dealloc
100{
101	[[NSNotificationCenter defaultCenter] removeObserver:self];
102	[super dealloc];
103}
104
105+ (id)_toolPropertiesPanelWithSide:(BOOL)leftRight
106{
107	PXToolPropertiesController **instance = leftRight ? &leftInstance : &rightInstance; // I heart pointers.  No, seriously, I do.
108
109	if (!*instance) {
110
111		if (! (*instance = [[self alloc] init]) )
112			return nil;
113
114		if ( ! [NSBundle loadNibNamed:@"PXToolProperties" owner:*instance] )
115		{
116			// The user is, unfortunately, quite cold at this point.
117			//NSLog(@"warm the user here !?? !!");
118			[*instance dealloc];
119			return nil;
120		}
121	}
122
123	return *instance;
124}
125
126+ (id)leftToolPropertiesController
127{
128	id object = [self _toolPropertiesPanelWithSide:YES];
129	[[NSNotificationCenter defaultCenter] addObserver:object
130											 selector:@selector(_toolDidChange:)
131												 name:PXToolDidChangeNotificationName
132											   object:[[PXToolPaletteController sharedToolPaletteController] leftSwitcher]];
133	[[[PXToolPaletteController sharedToolPaletteController] leftSwitcher] requestToolChangeNotification];
134	return object;
135}
136
137+ (id)rightToolPropertiesController
138{
139	id object = [self _toolPropertiesPanelWithSide:NO];
140	[[NSNotificationCenter defaultCenter] addObserver:object
141											 selector:@selector(_toolDidChange:)
142												 name:PXToolDidChangeNotificationName
143											   object:[[PXToolPaletteController sharedToolPaletteController] rightSwitcher]];
144	[[[PXToolPaletteController sharedToolPaletteController] rightSwitcher] requestToolChangeNotification];
145	return object;
146}
147
148
149//Accessor
150-(NSPanel *) propertiesPanel
151{
152	return panel;
153}
154
155- (void)setPropertiesView:(id)propertiesView
156{
157	NSRect newPropertiesFrame = [propertiesView    frame] ;
158	//NSRect newViewFrame = NSMakeRect(0, 0, newPropertiesFrame.size.width, newPropertiesFrame.size.height);
159
160	NSRect newPanelFrame = [NSPanel frameRectForContentRect:NSMakeRect([panel frame].origin.x, [panel frame].origin.y, newPropertiesFrame.size.width, newPropertiesFrame.size.height)
161												  styleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask];
162	//Waring chek mem leaks  !!!
163	[[propertiesView view] retain]; // What?!  (I know this isn't actually going to leak, because there is a finite, very small number of these things, but seriously!  HACK ALERT)
164	[panel setContentView: [propertiesView view]];
165	[panel setFrame:newPanelFrame display:YES animate:YES];
166}
167
168
169@end
170
171
172
173