2

I'm trying to make a color chooser using an NSPopupButton, and at the bottom i'd like a separator and a "Custom…" option. Is it possible to load the colors from array, then tack the separator and "custom" item on bottom? if so, how?

Thanks!

5 Answers5

1

I have created a similar PopUpButton, but am not yet binding the default colors to a fixed array (though I am working on that now). there are two appraoches here - both may be considered hacks by purists, but they do get the job done.

  1. Subclass the NSPopUpButtonCell and override attachPopUpWithFrame to add your own menu items. I have not tried this alongside bound items.

  2. Hard-code the 'Custom ..' object in your array, and have the action of the displayed color panel, add a new item to the array.

asheemm
  • 36
  • 1
  • 7
1

This is possible with bindings as of Mac OS X 10.5 using the "content placement" tag; see my answer here:

Separator item in NSPopupButton with bindings

Community
  • 1
  • 1
Kevin Grant
  • 5,363
  • 1
  • 21
  • 24
0

Not using Bindings, no. You could do the “Custom…” one easily enough, but not the separator.

Why not use an NSColorWell, anyway?

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • I've thought about using a colorwell, but the menu item really makes the interface more concise. They'd both have the same effect of getting the sharedColorPanel open. It seems my best solution, then, is to manually rebuild the menu every time my array changes from my view class. –  Aug 08 '09 at 05:49
0

You should really use a NSColorWell instead of hand-crafting your own. One of the reasons why Apple has superior GUIs than other platforms (especially Linux) is because developers use the standard components for doing this kind of thing. Arguments like 'because I think it makes the interface more concise' are the reasons why GIMP is such a prime example of how not to design a GUI.

That said, basically what you're trying to do is define a dynamic menu, rather than a fixed-size list (as one might do in InterfaceBuilder). You can do this via the NSMenu and NSMenuItem classes.

MenuList documentation guide

What you'd need to do, is rather than display the menu on-demand, is fill it when the application starts up with the default array. Then, when the array changes (via your model objects) trigger the re-creation of the menu. Alternatively, trap the menu with the menuNeedsUpdate: message.

AlBlue
  • 23,254
  • 14
  • 71
  • 91
  • thanks for the response. everyone seems dedicated to the NSColorWell, when, in reality, there are plenty of instances where it isn't appropriate. case in point: iCal. "get info" on a calendar in the source list, and click on the color menu on the right. that is almost exactly what i'm trying to do. you can fill up an interface with apple's standard components, that doesn't make it a good GUI. the inverse is also true. thanks for pointing me in the right direction, though! –  Aug 08 '09 at 21:54
0
  1. Configure your bindings on the NSPopupButton as usual
  2. In your bindings configuration for the NSPopupButton, set the Content Placement Tag value to 1

Content Placement Tag for the dynamic content

  1. In the Storyboard, expand the NSPopupButton & select the NSMenu associated with the NSPopupButton
  2. Make sure you have two items in your menu:

    • First item that will "represent" the dynamic content. Set the tag value to 1 --> this is important for the bindings dynamic content to be inserted at the right place. Menu Item to receive the right tag
    • Second item will be your "Custom..." menu item

Optional: Add a Separator Menu Item between dynamic and static content:

  1. Set your ViewController as delegate for the NSMenu
  2. Make your your ViewController a NSMenuDelegate and add the following code in your ViewController:

    extension YourViewController: NSMenuDelegate {
    
        func menuNeedsUpdate(_ menu: NSMenu) {
    
        menu.insertItem(NSMenuItem.separator(), at: menu.numberOfItems-1)
        }
    }
    

which will simply properly insert a separator between your dynamic content and your static "Custom..." menu item

vomi
  • 993
  • 8
  • 18