Open and fill the Find Window

Discussion in 'Mac Programming' started by flagdan, Aug 23, 2006.

  1. macrumors newbie

    Joined:
    Aug 23, 2006
    Location:
    Quebec, Canada
    #1
    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
     
  2. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #2
    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.
     
  3. macrumors 6502

    Joined:
    Jul 25, 2006
    #3
    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.
     
  4. thread starter macrumors newbie

    Joined:
    Aug 23, 2006
    Location:
    Quebec, Canada
    #4
    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).
     
  5. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #5
    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.
     
  6. thread starter macrumors newbie

    Joined:
    Aug 23, 2006
    Location:
    Quebec, Canada
    #6
    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];
    }
    
     

Share This Page