Re: Pull-down menu weirdness
Re: Pull-down menu weirdness
- Subject: Re: Pull-down menu weirdness
- From: Keith Blount <email@hidden>
- Date: Mon, 17 Jul 2006 10:46:59 -0700 (PDT)
Hi Matt,
Sorry, but, although your replies are usually very
helpful, this one didn't help much. :) I've already
read the documentation and am well-aware of the
difference, as I hoped my original post implied. My
question was about how to override an aspect of the
default behaviour of a pull-down list in a situation
in which a pop-up list is not appropriate. But a
pull-down list is still, technically, a pop-up button
(it is just a setting of NSPopUpButton), which is why
I used both terms in my post; apologies if my wording
seemed ambiguous. My problem was merely that I
required a pull-down list (because an action button
should be used for a menu of actions, whereas a pop-up
list is for selecting an item), but, because I am
using an image instead of a title, I didn't want the
pull-down list to eat up the first item in its given
menu for its title (which wouldn't get shown, and
which is the default behaviour for a pull-down list as
opposed to a pop-up list). Hope that explains my
initial question more clearly...
Anyway, I solved this on my own. In my subclass of
NSPopUpButtonCell, I just insert a dummy menu item
with an unlikely tag at the top of the menu in my
-setMenu: override. This item gets "eaten" for the
title and so is never seen, and the subclass uses an
NSButtonCell to display an image instead. For anyone
searching the archives, I have included the subclass
code below.
Thanks for trying to help anyway,
Keith
KBActionPopUpButton:
//
// KBActionPopUpButton.m
// ---------------------
//
// Created by Keith Blount on 14/07/2006.
//
#import "KBActionPopUpButton.h"
// Cell subclass
@interface KBActionPopUpButtonCell : NSPopUpButtonCell
{
NSButtonCell *buttonCell;
}
@end
@implementation KBActionPopUpButtonCell
- (id)initTextCell:(NSString *)stringValue
pullsDown:(BOOL)pullDown
{
if (self = [super initTextCell:stringValue
pullsDown:pullDown])
{
buttonCell = [[NSButtonCell alloc]
initImageCell:[self image]];
[buttonCell setButtonType:NSPushOnPushOffButton];
[buttonCell setImagePosition:NSImageOnly];
[buttonCell setImageDimsWhenDisabled:YES];
[buttonCell setBordered:NO];
[self synchronizeTitleAndSelectedItem];
}
return self;
}
- (void)dealloc
{
// Note that we have to set buttonCell to nil after
releasing it, because
// super's dealloc method calls setImage:nil, so our
overridden -setImage:
// method will call crashes if buttonCell hasn't been
set to nil
[buttonCell release];
buttonCell = nil;
[super dealloc];
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView
*)controlView
{
[buttonCell drawWithFrame:cellFrame
inView:controlView];
}
- (void)highlight:(BOOL)flag
withFrame:(NSRect)cellFrame inView:(NSView
*)controlView
{
[buttonCell highlight:flag withFrame:cellFrame
inView:controlView];
}
- (void)setImage:(NSImage *)anImage
{
[buttonCell setImage:anImage];
[super setImage:anImage];
}
- (void)setMenu:(NSMenu *)aMenu
{
// Given that we are using an image, we add a dummy
menu at the start of
// the menu which would normally be used for the
title.
NSMenuItem *dummyItem = [[[NSMenuItem alloc]
initWithTitle:@"dummy" action:nil keyEquivalent:@""]
autorelease];
[dummyItem setTag:-1366]; // Set an unlikely tag in
the hopes that it won't clash with others
[aMenu insertItem:dummyItem
atIndex:0];
[super setMenu:aMenu];
}
@end
// Control subclass
@implementation KBActionPopUpButton
- (id)initWithCoder:(NSCoder *)decoder
{
if ( self = [super initWithCoder:decoder] )
{
if (![[self cell]
isKindOfClass:[KBActionPopUpButtonCell class]])
{
NSImage *img = [self image];
NSMenu *theMenu = [[self menu] copyWithZone:[self
zone]];
[self setCell:[[[KBActionPopUpButtonCell alloc]
initTextCell:[self title]
pullsDown:[self pullsDown]]
autorelease]];
[[self cell] setImage:img];
[self setMenu:theMenu];
[theMenu release];
}
}
return self;
}
+ (Class)cellClass
{
return [KBActionPopUpButtonCell class];
}
@end
--- Matt Neuburg <email@hidden> wrote:
> On Sat, 15 Jul 2006 02:24:19 -0700 (PDT), Keith
> Blount
> <email@hidden> said:
> >Hello,
> >
> >I am currently implementing an action pull-down
> menu
> >like the one at the bottom of the mailboxes in
> >Mail.app. I expected this to be very trivial, but
> it
> >is not. I had to subclass NSPopUpButtonCell and
> >NSPopUpButton to call through to an NSButtonCell to
> do
> >all the drawing, and now that is all looking good.
> >
> >However, the problem I now have has nothing to do
> with
> >the subclass, as it occurs in normal
> NSPopUpButtons.
> >My pop up button is set to be a pull down menu. By
> >default, pull-down menus use the first item in the
> >menu for the title. How do I prevent this
> behaviour?
>
> Disinguish between a pop-up list and a pull-down
> list.
>
>
<http://developer.apple.com/documentation/Cocoa/Conceptual/MenuList/Concepts
> /HowPullDownListsWork.html>
>
> m.
> --
> matt neuburg, phd = email@hidden,
> <http://www.tidbits.com/matt/>
> A fool + a tool + an autorelease pool = cool!
> AppleScript: the Definitive Guide - Second Edition!
> <http://www.amazon.com/gp/product/0596102119>
>
>
>
>
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
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