Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically open an NSComboBox's list?

I've been around this for a while.. I thought this should be an easy task, but it isn't =D

What I am trying to do, is to display the combobox's list when the user clicks the combobox but not specifically in the button.

Any Idea? Thanks in advance!

like image 870
Omer Avatar asked Dec 21 '10 12:12

Omer


2 Answers

This answer fits the title of the question, but not question itself. Omer wanted to touch a text field and have the box popup.

This solution shows the popup when the user enters text.

I found this answer on cocoabuilder from Jens Alfke. I reposted his code here. Thanks Jens.

original cocoabuilder post: (http://www.cocoabuilder.com/archive/cocoa)

@interface NSComboBox (MYExpansionAPI)
@property (getter=isExpanded) BOOL expanded;
@end

@implementation NSComboBox (MYExpansionAPI)

- (BOOL) isExpanded
{
    id ax = NSAccessibilityUnignoredDescendant(self);
    return [[ax accessibilityAttributeValue:
                NSAccessibilityExpandedAttribute] boolValue];
}

- (void) setExpanded: (BOOL)expanded
{
    id ax = NSAccessibilityUnignoredDescendant(self);
    [ax accessibilitySetValue: [NSNumber numberWithBool: expanded]
                 forAttribute: NSAccessibilityExpandedAttribute];
}

I used this code in my controlTextDidChange: method.

- (void) controlTextDidChange:(NSNotification *) aNotification {
  NSTextField *textField = [aNotification object];
  NSString *value = [textField stringValue];
  NSComboBox *box = [self comboBox];
  if (value == nil || [value length] == 0) {
    if ([box isExpanded]) { [box setExpanded:NO]; }
  } else {
    if (![box isExpanded]) { [box setExpanded:YES]; }
  }
}
like image 52
jmoody Avatar answered Oct 22 '22 21:10

jmoody


  1. Returns true if the NSComboBox's list is expanded

    comboBox.cell?.isAccessibilityExpanded() ?? false
    
  2. Open the NSComboBox's list

    comboBox.cell?.setAccessibilityExpanded(true)
    
  3. Close the NSComboBox's list

    comboBox.cell?.setAccessibilityExpanded(false)
    

Ref. jmoody’s answer.

like image 23
jqgsninimo Avatar answered Oct 22 '22 21:10

jqgsninimo