ColorWell from menu bar

Discussion in 'Mac Programming' started by Darkroom, Oct 12, 2007.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    hi... i have a large ColorWell in my window that when clicked, the select color window appears automatically... that's great... apple makes this possible without having to write code for the select color window to appear... however, i would like to include a menu item "Select Color" that essentially does the same thing as clicking on the window, but i don't know what code to write for the controller since it's all automated... it's not for a font so adding the built-in font menu items doesn't work.

    anyone know what i should do?
     
  2. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    I guess I'd start with calling [[NSColorPanel sharedColorPanel] makeKeyAndOrderFront]; in the action method called by your menu item.
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    i'm not sure i'm doing this properly... actually scratch that, i'm certain i'm not doing this properly... any help would be appreciated...

    1) i made an object called "SelectColorFromMenu" and instantiated it
    2) i made one action called "openColorSelectWindow"
    3) wired the "Select Color" menu item to the controller and connected the action, and made the .h and .m files
    4) my .m file was coded and now looks like this:

    Code:
    #import "SelectColorFromMenu.h"
    
    @implementation SelectColorFromMenu
    
    - (IBAction)openColorSelectWindow:(id)sender
    {
    [[NSColorPanel sharedColorPanel] makeKeyAndOrderFront];
    }
    
    @end
    
    5) tested it: selecting the Select Color from the menu bar doesn't do anything, and it gives me this warning:

    Code:
    -[NSColorPanel makeKeyAndOrderFront]: selector not recognized [self = 0x388620]
    
    *hangs head in embarrassment*
     
  4. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #4
    Sorry about that, you have to send nil as an argument:

    [[NSColorPanel sharedColorPanel] makeKeyAndOrderFront:nil];

    Anyway, I wrote up a sample which I'm attaching. When you choose Select Color from the Test menu, it calls selectColor: from the controller. That sets the target (in this case, my custom view which simply fills with that color) and action to send when a color is changed in the panel, and then brings up the color panel. I *think* that's what you're looking for? It shouldn't be too hard to make it set the color of a well instead of a custom view. There might even be better ways to do it, such as through bindings or notifications.

    View attachment ColorPanelTest.zip

    EDIT: Actually, it's really easy to change the color in a well, just make a reference to the color well in your controller and connect it in IB, then call [myColorWell activate:YES] in your setColor: method. That has the effect of clicking on the well and it'll bring up the color panel with the color already linked to the well.
     
  5. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #5
    oh perfect! thanks so much for the sample file... i'm sure it will help me out lots! thanks again
     

Share This Page