Pop Up Button, size and style

When you use Xcode, Pop Up Buttons can have different sizes (Regular, small and mini) as well as different styles. This is not (yet) possible in Xojo. I assume it’s part of Cocoa, so is it possible to use them via declares or some other workaround? Does any of you use these - and in case, are you willing to share your know-how?

Try setting the font size to System, SmallSystem
That should switch between the Regular & Small sizes

sorry to ask… what is the pop up button??

also known as a popup menu in Xojo

oh those… i used combo box most of the time

comboboxes are fine if you want to allow someone to type something in as a value

but that’s not always the case and a popup makes sense then

That is true… I have to webpopupmenu since there isn’t any webcombobox

To get the pull down menu, you can use the following.

// - (void)setPullsDown:(BOOL)flag declare sub setPullsDown lib "Cocoa" selector "setPullsDown:" ( handle as integer, inValue as boolean ) setPullsDown( me.handle, true )

Thanks. Maybe not the most obvious place to look, when you have to set the size, but never mind :slight_smile:

based on an article by Thomas Tempelmann I made a new example.

another version with a subclass


in XCode you can change the width+height of a popup button once
you’ve set bezelstyle to : NSSmallSquareBezelStyle

I am wondering why it doesn’t work in Xojo. I seems like control size is the same but the image ist
stretched/clinch if you alter the size. I actually like to create a small bevel popup button.

  declare function getBounds lib "Cocoa.framework" selector "bounds" (obj_id as integer) as NSRect
  declare sub setBounds lib "Cocoa.framework" selector "setBounds:" (id as integer, frameRect as NSRect)
  declare function cell lib "Cocoa.framework" selector "cell" (id as integer) as integer
  declare sub setBezelStyle lib "Cocoa.framework" selector "setBezelStyle:" (obj_id as integer, value as integer)
  declare sub setPullsDown lib "Cocoa.framework" selector "setPullsDown:"  (id as integer, value as Boolean)
  declare sub setArrowPosition lib "Cocoa.framework" selector "setArrowPosition:" (id as integer, value as integer)
  
  dim h as integer = cell(me.Handle)
  setBezelStyle(h, NSSmallSquareBezelStyle)
  
  dim rect as NSRect = getBounds(me.handle)
  rect.h = 20.0
  setBounds(me.Handle, rect)
  setpullsDown( h, true )
  setArrowPosition( h, NSPopUpArrowAtBottom)

Well, haven’t done much with cocoa for a few weeks and I forgot the basics…I should have known it to use:

declare function frame lib "Cocoa.framework" selector "frame" (id as integer) as NSRect declare sub setFrame lib "Cocoa.framework" selector "setFrame:" (id as integer, frameRect as NSRect)

instead of.

[quote=180656:@Rob Egal]Well, haven’t done much with cocoa for a few weeks and I forgot the basics…I should have known it to use:

declare function frame lib "Cocoa.framework" selector "frame" (id as integer) as NSRect declare sub setFrame lib "Cocoa.framework" selector "setFrame:" (id as integer, frameRect as NSRect)

instead of.[/quote]

Rob, this looks excellent, and I could use that. Could you be so kind to post the final code ? I do not quite understand where the additional code goes. Thank you in advance.

Hi Michel,
Yeah, was only snipped. Here’s the code and example:

[code]

const NSPopUpArrowAtBottom = 2
const NSSmallSquareBezelStyle = 10

const CocoaLib = “Cocoa.framework”

declare function NSClassFromString lib CocoaLib (className as CFStringRef) as ptr

declare function cell lib CocoaLib selector “cell” (id as integer) as integer
declare function frame lib CocoaLib selector “frame” (id as integer) as NSRect
declare sub setFrame lib CocoaLib selector “setFrame:” (id as integer, frameRect as NSRect)
declare sub setPullsDown lib CocoaLib selector “setPullsDown:” (id as integer, value as Boolean)
declare sub setArrowPosition lib CocoaLib selector “setArrowPosition:” (id as integer, value as integer)
declare sub setBezelStyle lib CocoaLib selector “setBezelStyle:” (obj_id as integer, value as integer)

dim ctrlHandle as integer = me.Handle
dim cellHandle as integer = cell(ctrlHandle)

setBezelStyle(cellHandle, NSSmallSquareBezelStyle)
setpullsDown(cellHandle, true )
setArrowPosition(cellHandle, NSPopUpArrowAtBottom)

dim r as NSRect = frame(me.Handle)
r.h = 22
setFrame(me.Handle,r)

declare function imageNamed lib CocoaLib selector “imageNamed:” (id as ptr, imgName as CFStringRef) as ptr
declare function itemAtIndex lib CocoaLib selector “itemAtIndex:” (id as ptr, idx as integer) as ptr
declare function menu lib CocoaLib selector “menu” (id as integer) as ptr
declare sub setImage lib CocoaLib selector “setImage:” (id as ptr, img as ptr)

// in order set an image you need to set the image for the first menu item of the popupmenu (index =0)
// see : https://developer.apple.com/library/prerelease/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSPopUpButton_Class/index.html#//apple_ref/occ/instm/NSPopUpButton/setImage:

if me.ListIndex>-1 then
dim mi as ptr = itemAtIndex(menu(ctrlHandle), 0)
dim imgRef as Ptr = imageNamed( NSClassFromString(“NSImage”), stringConstant(“NSImageNameActionTemplate”, “com.apple.Cocoa”))
setImage( mi, imgRef )
end if[/code]
Cocoa PullDownPopUpMenu

[quote=180928:@Rob Egal]Hi Michel,
Yeah, was only snipped. Here’s the code and example:

[code]

const NSPopUpArrowAtBottom = 2
const NSSmallSquareBezelStyle = 10

const CocoaLib = “Cocoa.framework”

declare function NSClassFromString lib CocoaLib (className as CFStringRef) as ptr

declare function cell lib CocoaLib selector “cell” (id as integer) as integer
declare function frame lib CocoaLib selector “frame” (id as integer) as NSRect
declare sub setFrame lib CocoaLib selector “setFrame:” (id as integer, frameRect as NSRect)
declare sub setPullsDown lib CocoaLib selector “setPullsDown:” (id as integer, value as Boolean)
declare sub setArrowPosition lib CocoaLib selector “setArrowPosition:” (id as integer, value as integer)
declare sub setBezelStyle lib CocoaLib selector “setBezelStyle:” (obj_id as integer, value as integer)

dim ctrlHandle as integer = me.Handle
dim cellHandle as integer = cell(ctrlHandle)

setBezelStyle(cellHandle, NSSmallSquareBezelStyle)
setpullsDown(cellHandle, true )
setArrowPosition(cellHandle, NSPopUpArrowAtBottom)

dim r as NSRect = frame(me.Handle)
r.h = 22
setFrame(me.Handle,r)

declare function imageNamed lib CocoaLib selector “imageNamed:” (id as ptr, imgName as CFStringRef) as ptr
declare function itemAtIndex lib CocoaLib selector “itemAtIndex:” (id as ptr, idx as integer) as ptr
declare function menu lib CocoaLib selector “menu” (id as integer) as ptr
declare sub setImage lib CocoaLib selector “setImage:” (id as ptr, img as ptr)

// in order set an image you need to set the image for the first menu item of the popupmenu (index =0)
// see : https://developer.apple.com/library/prerelease/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSPopUpButton_Class/index.html#//apple_ref/occ/instm/NSPopUpButton/setImage:

if me.ListIndex>-1 then
dim mi as ptr = itemAtIndex(menu(ctrlHandle), 0)
dim imgRef as Ptr = imageNamed( NSClassFromString(“NSImage”), stringConstant(“NSImageNameActionTemplate”, “com.apple.Cocoa”))
setImage( mi, imgRef )
end if[/code]
Cocoa PullDownPopUpMenu[/quote]

Superb !

I have been trying to change the height of PopupMenus and Comboboxes in Cocoa as they do in Windows without finding a way, until now. You have brightened my day.

Thank you so much, Rob.

You’re welcome.
Actually Sam got me to digging for the apple documentation as I spotted some cool things in his Retina Kit trial version.

[quote=181107:@Rob Egal]You’re welcome.
Actually Sam got me to digging for the apple documentation as I spotted some cool things in his Retina Kit trial version.[/quote]

Sam taught me how to enable custom fonts dynamically, and helped me understand better how to build Mac OS X declares. But I should admit it still remains a very difficult thing for me. It got easier since Swift, as the syntax is somewhat less alien than Objective-C.

I was under the impression that it was not possible at all to modify Cocoa controls height. I vaguely recall Norman saying that as well. And you come up with that wonderful piece of code, which works beautifully. Again, thank you :slight_smile:

[quote=181125:@Michel Bujardet]Sam taught me how to enable custom fonts dynamically, and helped me understand better how to build Mac OS X declares. But I should admit it still remains a very difficult thing for me. It got easier since Swift, as the syntax is somewhat less alien than Objective-C.

I was under the impression that it was not possible at all to modify Cocoa controls height. I vaguely recall Norman saying that as well. And you come up with that wonderful piece of code, which works beautifully. Again, thank you :)[/quote]

Xcode is sometime usefull to check out for control behavior. If this works on XCode it should work in Xojo too.

XCode is in my dock :wink: