NSButton MouseHover

Discussion in 'Mac Programming' started by McBgnr, Apr 15, 2009.

  1. McBgnr macrumors regular

    Joined:
    Apr 13, 2009
    #1
    Does anybody know how can I get a mouse hover event. I want to handle this event because I want to change the image on the button when mouse is over it.

    Any suggestions are welcome.
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Setup a tracking rect/area for the view. On 10.5+ use addTrackingArea: and on 10.4+ use addTrackingRect:eek:wner:userData:assumeInside:. Then the view will receive mouseEntered: and mouseExited: events which you can use to set the image.
     
  3. McBgnr thread starter macrumors regular

    Joined:
    Apr 13, 2009
    #3
    that sounds gr8... will give it a try later today.
     
  4. McBgnr thread starter macrumors regular

    Joined:
    Apr 13, 2009
    #4
    I am somehow not able to set tracking area for the view. Any ideas in which function I will have to do this and how?

    My button is on NSTabView.
     
  5. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    Make sure you're doing this inside a subclass of NSButton because you need to override the two mouse event methods, and you should setup the tracking rects inside the viewDidMoveToWindow method.

    If you're still having trouble post your code.
     
  6. McBgnr thread starter macrumors regular

    Joined:
    Apr 13, 2009
    #6
    Thanks a bunch.. I have tried the following on a simple project :

    1. Made a new XCode Cocoa Project
    2. In the default window added a button
    3. Added Objective C controller class which contains referneces for the Button and the ContentView of the mainwindow.
    4.Subclass NSButton.
    5. Added the method viewDidMoveToWindow() and mouse entered and mouseexited methods to the NSButton subclass.

    This is not working... game me some exception. Have I done something wrong here? Do I need to subclass view as well?


    Later I subclassed mainWindow's ContentView and added mouseentered and mouseexited functions in it as well... but it did not work either :confused:

    My code has become untidy after trying many things ... so I'll clean it and post it later.
     
  7. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #7
    Here's a little project that demonstrates this. Notice that in the nib the button's class is set to the NSButton subclass name.
     

    Attached Files:

  8. McBgnr thread starter macrumors regular

    Joined:
    Apr 13, 2009
    #8
    My goodness. It was as simple as this. Thanks to you for this. :)

    Its working now.
     
  9. McBgnr thread starter macrumors regular

    Joined:
    Apr 13, 2009
    #9
    I am now trying to implement this for all the buttons that I have in my app. For this I have taken following in the HoverButton.h class

    Code:
            NSString *normalImageName;
    	NSString *hoverImageName;
    	
    	- (void)setNormalImageName:(NSString *)aString;
    	- (void)setHoverImageName:(NSString *)aString;
    
    and the following in HoverButton.m class
    Code:
    - (void)setNormalImageName:(NSString *)aString
    {
    	normalImageName = aString;
    }
    - (void)setHoverImageName:(NSString *)aString
    {
    	hoverImageName = aString;
    }
    
    Also I have included Hoverbutton.h in the delegate class and m trying to set the image names in delegate.m
    as
    Code:
    
    -(void)awakeFromNib
    {
    	[button setNormalImageName:@"test.jpg"];
    
    }
    
    Here I have declared button as type of HoverButton. 
    
    
    I am gettting compile error "syntax error before '-' token in Hoverbutton.h.

    Am I missing something here?
     
  10. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    Well, I'm pretty sure HoverButton.cpp should be HoverButton.m otherwise the compiler will treat it as C++ code, not Objective-C.
     
  11. McBgnr thread starter macrumors regular

    Joined:
    Apr 13, 2009
    #11
    Yes thanks for the correction.. the file is actually Hoverbutton.m and i made a mistake while typing.
     
  12. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #12
    If you're still getting the syntax error post your entire class, both the .h and .m files. If you copy and paste chunks of code that you think are related it usually is the wrong part :)

    Also, unless you have GC enabled, those accessors are incorrect and should look something like this (it'd be better to replace them with @properties if you can):

    Code:
    - (void)setHoverImageName:(NSString *)aString
    {
        if (hoverImageName != aString) {
            [hoverImageName release];
            hoverImageName = [aString copy];
        }
    }
     
  13. McBgnr thread starter macrumors regular

    Joined:
    Apr 13, 2009
    #13
    Figured out the reason of compile issue... and the code is working perfectly fine now.

    Thanks for pointing out the flaw in the accessor body... these tips are so helpful :)
     

Share This Page