Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

flagdan

macrumors newbie
Original poster
Aug 23, 2006
3
0
Quebec, Canada
I'm doing a project based on Cocoa. I'm almost done, it's a little project, but I want to know how my application could open the Find window (Command-F) and set the string to search.

Thanks
 

caveman_uk

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
Sadly there is no NSFindPanel; it's actually part of the NSTextView functionality so check out the docs for that if that's applicable. Otherwise you're rolling your own. If you decide to do the latter then the (fairly old but still valid) code here should help.
 

kpua

macrumors 6502
Jul 25, 2006
294
0
Actually, you can do this in a fashion by manually setting the value that you want to appear in the find panel on the NSFindPboard. This is what applications use so that when you find something in one application and switch to another, its find panel will show the same string.

The code that caveman_uk linked to actually has a good example of how to do this.

Code:
- (void)loadFindStringToPasteboard {
	NSPasteboard *pasteboard = [NSPasteboard pasteboardWithName:NSFindPboard];
	[pasteboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
	[pasteboard setString:[self findString] forType:NSStringPboardType];
}

However, doing this has the probably undesirable effect that the find string will be overwritten for other applications as well, even if the user doesn't search for something using your find panel.
 

flagdan

macrumors newbie
Original poster
Aug 23, 2006
3
0
Quebec, Canada
Great! I have a NSTextView actually. I look at the code on stone.com.
First I saw the PastBoard thing and added it. Then I kept digging and implement the find/findstring combo and it works!

My app is only to browse source code faster while I'm doing investigations (many many projetcs and files, and they're not XCode projects).
 

caveman_uk

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
If you have an NSTextView then take a look at the class reference. You might not need to write your own. There are methods in NSTextView to support the use of a find panel.
 

flagdan

macrumors newbie
Original poster
Aug 23, 2006
3
0
Quebec, Canada
My first though was to use a find panel to go to the text I want to search.
But what I have now is better. :) I have a NSTableView fill with function names and a source file path were I can see how the function is used. When I clicked on a function name, I load the source file in the NSTextView and scroll down to the funtion I want to investigate.

My only bug is when I click on a column header to sort the NSTableView,
the name of the function does not match the path name. :( The TableView
is bound to an ArrayController.

Code:
- (IBAction) fOpenSourceButtonClicked: (id)sender
{
//get the row selected in the scroll view
   int irow = [scrollview selectedRow]; 
//get the columns of the scroll view
   NSArray * aColumns = [scrollview tableColumns];

//get the source path column (index = 1)
   NSTableColumn * tcolpath = [aColumns objectAtIndex:1];
//get the selected cell for the source path
// BUG: DOESN'T MATCH WHAT I SEE IN THE TABLEVIEW
   NSTextFieldCell * tfcell = [tcolpath dataCellForRow:irow];
//get the source path text 
   NSString * strPath = [tfcell stringValue];

//get the function name column (index = 0)
   NSTableColumn * tcolfuncname = [aColumns objectAtIndex:0];
//get the selected cell for the function name
   NSTextFieldCell * tftosearch = [tcolfuncname dataCellForRow:irow];
   NSString * strToSearch = [tftosearch stringValue];
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.