On 28/01/2011, at 14:43, Ben Golding wrote:

> Safari has a really nice "Find" bar that scrolls down from the title pane of 
> the window.  I'd like to do something like that in an application that I'm 
> developing but I can't find out whether that's generally available and how to 
> use it.

Thanks to couple of short but extremely informative e-mails from Uli Kusterer I 
know how to do this now.

The first thing to do is to wrap the content of the window in a single NSView.  
In IB, select all in your window and then enclose that in a custom view which 
doesn't need a subclass.  We'll change the size of that view in the window to 
leave space at the top of the window's content view to leave space for the find 
"panel".

In IB, create a new window/panel and populate it with the NSSearchField and 
other buttons.  Hook those up to the actions/outlets/bindings in file's owner 
that trigger the appropriate actions and make it the size that you want it to 
be.  You need to add an outlet in File's owner to this window's view so we can 
add it to the window.

Here's the piece of code I used.  CsvContentView is the view enclosing the main 
window's content, findPanelView is the view containing my find panel 
components, csvWindow is the window containing csvContentView (I might have 
used the window manager to get this but a link using IB seemed easier).

- (void)makeFindPanelVisible
{
    NSRect contentRect = [csvContentView frame];

    [csvContentView setFrameSize:NSMakeSize(contentRect.size.width, 
contentRect.size.height - [findPanelView bounds].size.height)];
    [findPanelView setFrame:NSMakeRect(contentRect.origin.x, [[csvWindow 
contentView] frame].size.height - [findPanelView bounds].size.height,
                                       contentRect.size.width, [findPanelView 
bounds].size.height)];
    [[csvWindow contentView] addSubview:findPanelView];
}

- (IBAction)closeFindPanelAction:(id)sender
{
    NSRect contentRect = [csvContentView frame];

    // extra retain to stop findPanelView being released (for some reason)
    [[findPanelView retain] removeFromSuperviewWithoutNeedingDisplay];
    [csvContentView setFrameSize:NSMakeSize(contentRect.size.width, 
contentRect.size.height + [findPanelView bounds].size.height)];

    _isFindPanelVisible = NO;
}

I haven't implemented animation though I'd like to, I'm trying to get the 
functionality working first.

The second part of my question about highlighting the found string as Safari 
does can be done using -showFindIndicatorForRange:.  That method only exists on 
NSTextView so it's not obvious (to me) how to use that to highlight the 
matching field in an NSTableView which is what I'm using to display my data.  
I'm just selecting the matching text instead.

Hope this helps someone.  There isn't much code but it takes a little fiddling 
to get it right.

        Ben.

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to