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
|
/*
GSToolbarCustomizationPalette.m
The palette which allows to customize toolbar
Copyright (C) 2007 Free Software Foundation, Inc.
Author: Quentin Mathe <qmathe@club-internet.fr>
Date: January 2007
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#import <Foundation/NSArray.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSNotification.h>
#import "AppKit/NSNibLoading.h"
#import "AppKit/NSWindow.h"
#import "AppKit/NSToolbar.h"
#import "AppKit/NSToolbarItem.h"
#import "AppKit/NSPopUpButton.h"
#import "NSToolbarFrameworkPrivate.h"
#import "GSToolbarCustomizationPalette.h"
#define DEBUG_LEVEL @"Toolbar"
/* Customization View */
@interface GSToolbarCustomizationView : NSView
{
NSArray *_paletteItems;
}
- (void) setToolbarItems: (NSArray *)items;
- (NSArray *) paletteItemsWithToolbarItems: (NSArray *)items;
- (void) layout;
@end
@implementation GSToolbarCustomizationView
- (void)dealloc
{
[_paletteItems release];
[super dealloc];
}
/* If the toolbar item has a custom view and the item is in use in the
toolbar, this view has already a superview. We need to make a copy of it
in order to be able to put it in the customization view.
As a safety measure, we make a copy of all toolbar items, thereby of all
views. This ensures the toolbar items displayed in the palette don't
reference a toolbar. */
- (NSArray *) paletteItemsWithToolbarItems: (NSArray *)items
{
NSMutableArray *paletteItems = [NSMutableArray array];
NSEnumerator *e = [items objectEnumerator];
NSToolbarItem *item = nil;
while ((item = [e nextObject]) != nil)
{
NSToolbarItem *newItem = [item copy];
if ([newItem paletteLabel] != nil)
[newItem setLabel: [newItem paletteLabel]];
[newItem setEnabled: YES];
[newItem _layout];
[paletteItems addObject: newItem];
RELEASE(newItem);
}
NSDebugLLog(DEBUG_LEVEL, @"Generated palette items %@ from toolbar items %@",
paletteItems, items);
return paletteItems;
}
- (void) layout
{
NSSize boundsSize = [self bounds].size;
float maxWidth = boundsSize.width;
float maxHeight = boundsSize.height;
float hAccumulator = 0.0;
float vAccumulator = 0.0;
NSEnumerator *e = [[self subviews] objectEnumerator];
NSView *layoutedView = nil;
int index = 0;
// Loop over all subviews
while ((layoutedView = [e nextObject]) != nil)
{
NSRect frame;
NSSize size;
float height;
float width;
[(id)layoutedView layout];
frame = [layoutedView frame];
size = frame.size;
height = size.height;
width = size.width;
if ((hAccumulator + width) <= maxWidth)
{
// Position view in row
if (vAccumulator < height)
{
vAccumulator = height;
// FIXME: need to adjust all other elements of row
}
}
else
{
// Move on to next row
maxHeight -= vAccumulator;
hAccumulator = 0.0;
vAccumulator = height;
}
[layoutedView setFrameOrigin: NSMakePoint(hAccumulator,
maxHeight - vAccumulator)];
[layoutedView setAutoresizingMask:NSViewMinYMargin];
hAccumulator += width;
index++;
}
maxHeight -= vAccumulator; // adjust for final row
if (maxHeight != 0) // need to grow (or shrink) the window to accommodate more (or fewer) items
{
NSRect windowFrame = [[self window] frame];
windowFrame.origin.y += maxHeight;
windowFrame.size.height -= maxHeight;
[[self window] setFrame:windowFrame display:NO];
}
}
- (void) setToolbarItems: (NSArray *)items
{
NSEnumerator *e;
NSView *itemView;
NSToolbarItem *item;
[_paletteItems release];
_paletteItems = [[self paletteItemsWithToolbarItems: items] retain];
// Remove all old subviews
e = [[self subviews] objectEnumerator];
while ((itemView = [e nextObject]) != nil)
{
[itemView removeFromSuperview];
}
NSDebugLLog(DEBUG_LEVEL, @"Will insert the views of toolbar items %@ in \
customization view", _paletteItems);
e = [_paletteItems objectEnumerator];
while ((item = [e nextObject]) != nil)
{
itemView = [item _backView];
if (itemView != nil)
{
[self addSubview: itemView];
}
else
{
NSLog(@"Toolbar item %@ will not be visible in the customization \
view or if you insert it in a toolbar now. The view is already in \
use in superview or does not implement NSCoding protocol", item);
}
}
[self layout];
}
@end
/* Main implementation */
@implementation GSToolbarCustomizationPalette
+ (id) palette
{
return AUTORELEASE([[GSToolbarCustomizationPalette alloc]
init]);
}
- (id) init
{
self = [super init];
if (self != nil)
{
BOOL nibLoaded = [NSBundle loadNibNamed: @"GSToolbarCustomizationPalette"
owner: self];
if (nibLoaded == NO)
{
NSLog(@"Failed to load GSToolbarCustomizationPalette");
RELEASE(self);
return nil;
}
_allowedItems = [NSMutableArray new];
_defaultItems = [NSMutableArray new];
}
return self;
}
- (void) dealloc
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver: self];
// DESTROY(_customizationWindow);
// DESTROY(_customizationView);
// DESTROY(_defaultTemplateView);
// DESTROY(_sizeCheckBox);
// DESTROY(_displayPopup);
// DESTROY(_doneButton);
DESTROY(_defaultItems);
DESTROY(_allowedItems);
[super dealloc];
}
- (void) awakeFromNib
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSDebugLLog(DEBUG_LEVEL, @"GSToolbarCustomizationPalette awaking from nib");
[_defaultTemplateView setAutoresizingMask:NSViewWidthSizable | NSViewMaxYMargin];
{
NSRect dtvFrame;
NSRect cvFrame;
// for now, _defaultTemplateView isn't implemented, so remove it
dtvFrame = [_defaultTemplateView frame];
[_defaultTemplateView removeFromSuperview];
// expand _customizationView to fill the space
cvFrame = [_customizationView frame];
cvFrame.size.height += dtvFrame.size.height;
cvFrame.origin.y -= dtvFrame.size.height;
[_customizationView setFrame:cvFrame];
}
[nc addObserver: self
selector: @selector(paletteDidEnd:)
name: NSWindowWillCloseNotification
object: _customizationWindow];
}
/* It's safer to keep no reference on the toolbar itself. The customization
palette isn't bound to the toolbar passed as parameter, in other words the
palette can be used to customize other toolbars with the same identifier.
In spite of this, a reference must be kept on the toolbar which initiated
the customization to let it know when the customization has ended. */
- (void) showForToolbar: (NSToolbar *)toolbar
{
NSArray *itemIdentifiers = nil;
NSEnumerator *e = nil;
NSString *identifier = nil;
NSToolbarDisplayMode tag = [toolbar displayMode];
NSToolbarSizeMode size = [toolbar sizeMode];
NSView *toolbarView;
NSRect toolbarFrame;
NSPoint bottomCenter;
NSRect windowFrame;
NSPoint topCenter;
[_allowedItems removeAllObjects];
[_defaultItems removeAllObjects];
[_displayPopup selectItemWithTag: tag];
// Set toolbar size checkbox...
if(size == NSToolbarSizeModeSmall)
{
[_sizeCheckBox setState: NSOnState];
}
else
{
[_sizeCheckBox setState: NSOffState];
}
itemIdentifiers = [toolbar _allowedItemIdentifiers];
e = [itemIdentifiers objectEnumerator];
while ((identifier = [e nextObject]) != nil)
{
NSToolbarItem *item = [toolbar _toolbarItemForIdentifier: identifier
willBeInsertedIntoToolbar: NO];
NSDebugLLog(DEBUG_LEVEL, @"item %@ for ident %@", item, identifier);
[_allowedItems addObject: item];
}
itemIdentifiers = [toolbar _defaultItemIdentifiers];
e = [itemIdentifiers objectEnumerator];
while ((identifier = [e nextObject]) != nil)
{
NSToolbarItem *item = [toolbar _toolbarItemForIdentifier: identifier
willBeInsertedIntoToolbar: NO];
[_defaultItems addObject: item];
}
[_customizationView setToolbarItems: _allowedItems];
/* We retain ourself to keep us alive until the palette is closed (this is
useful in case nobody retains us). */
RETAIN(self);
/* No need to retain the toolbar because it will first request us to close
when it goes away. */
_toolbar = toolbar;
// position the customization window centered just below the toolbar
toolbarView = [_toolbar _toolbarView];
toolbarFrame = [toolbarView convertRect:[toolbarView bounds] toView:nil];
bottomCenter = NSMakePoint(toolbarFrame.origin.x + (toolbarFrame.size.width/2), toolbarFrame.origin.y);
bottomCenter = [[toolbarView window] convertBaseToScreen:bottomCenter];
windowFrame = [_customizationWindow frame];
topCenter = NSMakePoint(windowFrame.origin.x + (windowFrame.size.width/2), windowFrame.origin.y + windowFrame.size.height);
windowFrame.origin.x += bottomCenter.x-topCenter.x;
windowFrame.origin.y += bottomCenter.y-topCenter.y;
[_customizationWindow setFrame:windowFrame display:NO];
[_customizationWindow setLevel:NSFloatingWindowLevel];
[_customizationWindow makeKeyAndOrderFront: self];
}
- (void) close
{
[_customizationWindow close];
}
- (void) reset: (id) sender
{
// reset the toolbar to it's defaults.
[_toolbar _resetConfig];
}
- (void) size: (id) sender
{
NSToolbarSizeMode mode = NSToolbarSizeModeRegular;
if([sender state] == NSOnState)
{
mode = NSToolbarSizeModeSmall;
}
[_toolbar setSizeMode: mode];
[_toolbar _saveConfig];
}
- (void) paletteDidEnd: (NSNotification *)notif
{
[_toolbar _setCustomizationPaletteIsRunning: NO];
_toolbar = nil;
/* We can now get rid safely of the extra retain done in -showForToolbar: */
RELEASE(self);
}
- (void) show: (id) sender
{
NSToolbarDisplayMode displayMode = (NSToolbarDisplayMode)
[[sender selectedItem] tag];
[_toolbar setDisplayMode: displayMode];
[_toolbar _saveConfig];
}
@end
|