1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8 #import <AppKit/NSToolbarCustomizationView.h>
9 #import <AppKit/NSToolbar.h>
10 #import <AppKit/NSToolbarItem.h>
11 #import <AppKit/NSToolbarView.h>
12 #import <AppKit/NSColor.h>
13 #import <AppKit/NSGraphics.h>
14 #import <AppKit/NSImage.h>
15 #import <AppKit/NSPasteboard.h>
16 #import <AppKit/NSDragging.h>
18 @interface NSToolbar (Private)
19 -(NSArray *)_allowedToolbarItems;
20 -(NSArray *)_defaultToolbarItems;
23 @interface NSToolbarItem (NSToolbarItem_private)
24 -(NSSize)sizeForSizeMode:(NSToolbarSizeMode)sizeMode displayMode:(NSToolbarDisplayMode)displayMode;
25 -(void)drawInRect:(NSRect)rect highlighted:(BOOL)highlighted;
28 @implementation NSToolbarCustomizationView
35 -(NSToolbar *)toolbar {
39 -(void)setToolbar:(NSToolbar *)toolbar {
40 toolbar=[toolbar retain];
45 -(void)setDefaultSetView:(BOOL)flag {
46 _isDefaultSetView = flag;
49 -(BOOL)isDefaultSetView {
50 return _isDefaultSetView;
53 -(NSArray *)toolbarItems {
55 return [_toolbar _defaultToolbarItems];
57 return [_toolbar _allowedToolbarItems];
64 -(void)layoutFrames:(NSRect *)frames count:(NSUInteger)count {
65 NSArray *items=[self toolbarItems];
67 NSRect bounds=[self bounds];
68 CGFloat maxx=NSMaxX(bounds);
70 CGFloat nextx=bounds.origin.x;
71 CGFloat nexty=bounds.origin.y;
72 CGFloat currentHeight=0;
75 NSToolbarItem *item=[items objectAtIndex:i];
76 NSSize size=[item sizeForSizeMode:NSToolbarSizeModeDefault displayMode:NSToolbarDisplayModeDefault];
78 if(nextx+size.width>maxx && currentHeight>0){
79 nextx=bounds.origin.x;
80 nexty+=currentHeight+padding;
84 frames[i].origin.x=nextx;
85 frames[i].origin.y=nexty;
87 nextx+=size.width+padding;
89 currentHeight=MAX(size.height,currentHeight);
94 -(NSSize)desiredSize {
95 NSRect unionRect=NSZeroRect;
96 NSArray *items=[self toolbarItems];
97 NSUInteger i,count=[items count];
100 [self layoutFrames:frames count:count];
101 for(i=0;i<count;i++){
105 unionRect=NSUnionRect(unionRect,frames[i]);
108 return unionRect.size;
111 -(void)drawRect:(NSRect)ignore {
112 NSArray *items=[self toolbarItems];
113 NSUInteger i,count=[items count];
114 NSRect frames[count];
116 [self layoutFrames:frames count:count];
117 for(i=0;i<count;i++){
118 NSToolbarItem *item=[items objectAtIndex:i];
119 [item drawInRect:frames[i] highlighted:NO];
124 -(void)mouseDown:(NSEvent *)event {
125 NSPasteboard *pasteboard=[NSPasteboard pasteboardWithName:NSDragPboard];
129 if (_isDefaultSetView) {
130 image=[[[NSImage alloc] initWithSize:[self bounds].size] autorelease];
131 data=[NSArchiver archivedDataWithRootObject:[[_toolbar _defaultToolbarItems] valueForKey:@"itemIdentifier"]];
133 [image setCachedSeparately:YES];
135 [self drawRect:[self bounds]];
140 NSPoint point=[self convertPoint:[event locationInWindow] fromView:nil];
141 NSArray *items=[self toolbarItems];
142 NSUInteger i,count=[items count];
143 NSRect frames[count];
145 [self layoutFrames:frames count:count];
147 if(NSPointInRect(point,frames[i])){
148 NSToolbarItem *item=[items objectAtIndex:i];
150 image=[[[NSImage alloc] initWithSize:frames[i].size] autorelease];
151 data=[NSArchiver archivedDataWithRootObject:[item itemIdentifier]];
153 [image setCachedSeparately:YES];
155 [item drawInRect:frames[i] highlighted:NO];
162 [pasteboard declareTypes:[NSArray arrayWithObject:NSToolbarItemIdentifierPboardType] owner:nil];
163 [pasteboard setData:data forType:NSToolbarItemIdentifierPboardType];
165 [self dragImage:image at:NSMakePoint(0,0) offset:NSMakeSize(0,0) event:event pasteboard:pasteboard source:self slideBack:YES];
169 - (unsigned)draggingSourceOperationMaskForLocal:(BOOL)isLocal
171 return NSDragOperationCopy;
176 NSString * const NSToolbarItemIdentifierPboardType = @"NSToolbarItemIdentifierPboardType";