Merge pull request #10 from gunyarakun/fix-invalid-return
[cocotron.git] / AppKit / NSToolbar.subproj / NSToolbarCustomizationView.m
blob2a491f693e8a97c6e72e071310f8fd3f5ccf90d2
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;
21 @end
23 @interface NSToolbarItem (NSToolbarItem_private)
24 -(NSSize)sizeForSizeMode:(NSToolbarSizeMode)sizeMode displayMode:(NSToolbarDisplayMode)displayMode;
25 -(void)drawInRect:(NSRect)rect highlighted:(BOOL)highlighted;
26 @end
28 @implementation NSToolbarCustomizationView
30 -(void)dealloc {
31    [_toolbar release];
32    [super dealloc];
35 -(NSToolbar *)toolbar {
36     return _toolbar;
39 -(void)setToolbar:(NSToolbar *)toolbar {
40    toolbar=[toolbar retain];
41    [_toolbar release];
42    _toolbar=toolbar;
45 -(void)setDefaultSetView:(BOOL)flag {
46    _isDefaultSetView = flag;
49 -(BOOL)isDefaultSetView {
50    return _isDefaultSetView;
53 -(NSArray *)toolbarItems {
54    if(_isDefaultSetView)
55     return [_toolbar _defaultToolbarItems];
56    else
57     return [_toolbar _allowedToolbarItems];
60 -(BOOL)isFlipped {
61    return YES;
64 -(void)layoutFrames:(NSRect *)frames count:(NSUInteger)count {
65    NSArray  *items=[self toolbarItems];
66    NSInteger i;
67    NSRect    bounds=[self bounds];
68    CGFloat   maxx=NSMaxX(bounds);
69    CGFloat   padding=4;
70    CGFloat   nextx=bounds.origin.x;
71    CGFloat   nexty=bounds.origin.y;
72    CGFloat   currentHeight=0;
73    
74    for(i=0;i<count;i++){
75     NSToolbarItem *item=[items objectAtIndex:i];
76     NSSize         size=[item sizeForSizeMode:NSToolbarSizeModeDefault displayMode:NSToolbarDisplayModeDefault];
77     
78     if(nextx+size.width>maxx && currentHeight>0){
79      nextx=bounds.origin.x;
80      nexty+=currentHeight+padding;
81      currentHeight=0;
82     }
83     
84     frames[i].origin.x=nextx;
85     frames[i].origin.y=nexty;
86     frames[i].size=size;
87     nextx+=size.width+padding;
89     currentHeight=MAX(size.height,currentHeight);
90    }
91    
94 -(NSSize)desiredSize {
95    NSRect     unionRect=NSZeroRect;
96    NSArray   *items=[self toolbarItems];
97    NSUInteger i,count=[items count];
98    NSRect     frames[count];
99    
100    [self layoutFrames:frames count:count];
101    for(i=0;i<count;i++){
102     if(i==0)
103      unionRect=frames[i];
104     else
105      unionRect=NSUnionRect(unionRect,frames[i]);
106    }
107    
108    return unionRect.size;
111 -(void)drawRect:(NSRect)ignore {
112    NSArray   *items=[self toolbarItems];
113    NSUInteger i,count=[items count];
114    NSRect     frames[count];
115    
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];
120    }
124 -(void)mouseDown:(NSEvent *)event {
125    NSPasteboard *pasteboard=[NSPasteboard pasteboardWithName:NSDragPboard];
126    NSImage      *image=nil;
127    NSData       *data=nil;
128    
129    if (_isDefaultSetView) {
130     image=[[[NSImage alloc] initWithSize:[self bounds].size] autorelease];
131     data=[NSArchiver archivedDataWithRootObject:[[_toolbar _defaultToolbarItems] valueForKey:@"itemIdentifier"]];
132         
133     [image setCachedSeparately:YES];
134     [image lockFocus];
135     [self drawRect:[self bounds]];        
136     [image unlockFocus];
137         
138    }
139    else {
140     NSPoint point=[self convertPoint:[event locationInWindow] fromView:nil];
141     NSArray   *items=[self toolbarItems];
142     NSUInteger i,count=[items count];
143     NSRect     frames[count];
144    
145     [self layoutFrames:frames count:count];
146     for(i=0;i<count;i++)
147      if(NSPointInRect(point,frames[i])){
148       NSToolbarItem *item=[items objectAtIndex:i];
149       
150       image=[[[NSImage alloc] initWithSize:frames[i].size] autorelease];
151       data=[NSArchiver archivedDataWithRootObject:[item itemIdentifier]];
152       
153       [image setCachedSeparately:YES];
154       [image lockFocus];
155       [item drawInRect:frames[i] highlighted:NO];
156       [image unlockFocus];
157       break;
158     }
159    }
160    
161    if(data!=nil){
162     [pasteboard declareTypes:[NSArray arrayWithObject:NSToolbarItemIdentifierPboardType] owner:nil];
163     [pasteboard setData:data forType:NSToolbarItemIdentifierPboardType];
164                 
165     [self dragImage:image at:NSMakePoint(0,0) offset:NSMakeSize(0,0) event:event pasteboard:pasteboard  source:self slideBack:YES];
166    }
169 - (unsigned)draggingSourceOperationMaskForLocal:(BOOL)isLocal 
171     return NSDragOperationCopy;
174 @end
176 NSString * const NSToolbarItemIdentifierPboardType = @"NSToolbarItemIdentifierPboardType";