Re: How do I make a flexible NSToolbarItem? [half solved]
Re: How do I make a flexible NSToolbarItem? [half solved]
- Subject: Re: How do I make a flexible NSToolbarItem? [half solved]
- From: PGM <email@hidden>
- Date: Fri, 8 Sep 2006 09:52:21 -0400
I found out how to make the view in the palette smaller than the view
in the toolbar. In the
toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar: delegate
method I set the minSize and maxSize using NSSize minSize =
[searchBoxView bounds].size, stupidly assuming that the bounds of the
view are the ones I set in Interface Builder. Of course these bounds
change as soon as the view is inserted into the toolbar. I did set
the maximum size, but for actually drawing the view the biggest value
of minSize and the maxSize is taken, not simply the value of maxSize.
Now all that remains is the problem that the flexible searchfield and
the adjacent flexibleSpace do not compete equally for the available
space. But this is less of a problem as the user can drag multiple
flexibleSpaces into the toolbar.
On 7-Sep-06, at 21:35 PM, PGM wrote:
To show the problem, I made a new project with a custom controller,
with only two outlets: the main window and a custom view of 52x32
pixels, that contains a NSSearchField; both the view and the
searchfield are set to be horizontally stretchable. The only task
of the custom controller is to create a toolbar for the window with
only the searchFieldView and a
NSToolbarFlexibleSpaceItemIdentifier. The controller contains the
following code:
Controller.h:
#import <Cocoa/Cocoa.h>
@interface Controller : NSObject
{
IBOutlet id theWindow;
IBOutlet NSView *searchBoxView;
}
//Required NSToolbar delegate methods
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
itemForItemIdentifier:(NSString *)itemIdentifier
willBeInsertedIntoToolbar:(BOOL)flag;
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar;
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar;
@end
Controller.m:
#import "Controller.h"
@implementation Controller
-(void)awakeFromNib
{
NSToolbar *toolbar=[[[NSToolbar alloc]
initWithIdentifier:@"myToolbar"] autorelease];
[toolbar setDelegate:self];
[toolbar setAllowsUserCustomization:YES];
[toolbar setAutosavesConfiguration: YES];
[toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
[theWindow setToolbar:toolbar];
}
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
itemForItemIdentifier:(NSString *)itemIdentifier
willBeInsertedIntoToolbar:(BOOL)flag
{
NSToolbarItem *newItem = [[[NSToolbarItem alloc]
initWithItemIdentifier:itemIdentifier] autorelease];
[newItem setLabel:@"Find"];
[newItem setPaletteLabel:@"Find"];
[newItem setView:searchBoxView];
NSSize minSize = [searchBoxView bounds].size;
NSSize maxSize = [searchBoxView bounds].size;
maxSize.width = 2000; //this is the maxSize returned by
NSToolbarFlexibleSpaceItem
[newItem setMinSize:minSize];
if(flag){ //here I try to make it big if inserted into toolbar...
[newItem setMaxSize:maxSize];
}
else{ //...and small if inserted into customisation palette
[newItem setMaxSize:minSize];
}
return newItem;
}
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
{
return [NSArray arrayWithObjects:@"Find",
NSToolbarFlexibleSpaceItemIdentifier, nil];
}
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
{
return [NSArray arrayWithObjects:@"Find",
NSToolbarFlexibleSpaceItemIdentifier, nil];
}
@end
On 7-Sep-06, at 14:46 PM, I. Savant wrote:
Please post the relevant code so people can review and comment.
--
I.S.
On Sep 7, 2006, at 10:49 AM, PGM wrote:
Hi everybody
My app has a toolbar in which I would like to have a view with a
searchbox that stretches automatically, like the one SearchBoxes
in most Apple apps. The documentation says about sizing
NSToolbarItem:
"If a view item’s view does something intelligent when it is
stretched, then you will set its maxSize greater than its minSize
in height, width, or both. Horizontally-stretchable view items,
including the Flexible Space standard item, compete equally for
available horizontal space."
The question now rises: how do I make my view do "something
intelligent"?
In Interface Builder, I have set my view to be flexible. And I
have implemented NSToolbar's toolbarWillAddItem: to set the
maxSize.width of my view to 2000 and the minSize.width to 51.
However, my searchbox and an adjacent NSToolbarFlexibleSpaceItem
do NOT compete equally for space; my searchbox is huge and the
flexibleSpace is tiny. Furthermore, when I customise my toolbar,
the customisation sheet shows the searchBox is the same huge size
as it is in the toolbar and not in the small size that you see
for the searchbox when you customise a toolbar in an Apple app.
Has anybody succeeded in getting this to work as it should?
Serachboxes are becoming increasingly popular, so I would guess
that people have struggled with this before.
Thanks in advance, Patrick
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40gmail.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40sympatico.ca
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden