2016-05-23 17:50:02 -05:00
|
|
|
// 14 august 2015
|
|
|
|
#import "uipriv_darwin.h"
|
|
|
|
|
2016-05-24 00:19:57 -05:00
|
|
|
// So why did I split uiCombobox into uiCombobox and uiEditableCombobox? Here's (90% of the; the other 10% is GTK+ events) answer:
|
|
|
|
// When you type a value into a NSComboBox that just happens to be in the list, it will autoselect that item!
|
|
|
|
// I can't seem to find a workaround.
|
|
|
|
// Fortunately, there's other weird behaviors that made this split worth it.
|
|
|
|
// And besides, selected items make little sense with editable comboboxes... you either separate or combine them with the text entry :V
|
|
|
|
|
2016-05-23 17:50:02 -05:00
|
|
|
// NSComboBoxes have no intrinsic width; we'll use the default Interface Builder width for them.
|
|
|
|
#define comboboxWidth 96
|
|
|
|
|
|
|
|
@interface libui_intrinsicWidthNSComboBox : NSComboBox
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation libui_intrinsicWidthNSComboBox
|
|
|
|
|
|
|
|
- (NSSize)intrinsicContentSize
|
|
|
|
{
|
|
|
|
NSSize s;
|
|
|
|
|
|
|
|
s = [super intrinsicContentSize];
|
|
|
|
s.width = comboboxWidth;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
struct uiEditableCombobox {
|
|
|
|
uiDarwinControl c;
|
|
|
|
NSComboBox *cb;
|
|
|
|
void (*onChanged)(uiEditableCombobox *, void *);
|
|
|
|
void *onChangedData;
|
|
|
|
};
|
|
|
|
|
|
|
|
@interface editableComboboxDelegateClass : NSObject<NSComboBoxDelegate> {
|
2018-05-03 22:00:50 -05:00
|
|
|
uiprivMap *comboboxes;
|
2016-05-23 17:50:02 -05:00
|
|
|
}
|
|
|
|
- (void)controlTextDidChange:(NSNotification *)note;
|
|
|
|
- (void)comboBoxSelectionDidChange:(NSNotification *)note;
|
|
|
|
- (void)registerCombobox:(uiEditableCombobox *)c;
|
|
|
|
- (void)unregisterCombobox:(uiEditableCombobox *)c;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation editableComboboxDelegateClass
|
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self)
|
2018-05-03 22:00:50 -05:00
|
|
|
self->comboboxes = uiprivNewMap();
|
2016-05-23 17:50:02 -05:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2018-05-03 22:00:50 -05:00
|
|
|
uiprivMapDestroy(self->comboboxes);
|
2016-05-23 17:50:02 -05:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)controlTextDidChange:(NSNotification *)note
|
|
|
|
{
|
|
|
|
uiEditableCombobox *c;
|
|
|
|
|
2018-05-03 22:00:50 -05:00
|
|
|
// TODO normalize the cast styles in these calls
|
|
|
|
c = uiEditableCombobox(uiprivMapGet(self->comboboxes, [note object]));
|
2016-05-23 17:50:02 -05:00
|
|
|
(*(c->onChanged))(c, c->onChangedData);
|
|
|
|
}
|
|
|
|
|
|
|
|
// the above doesn't handle when an item is selected; this will
|
|
|
|
- (void)comboBoxSelectionDidChange:(NSNotification *)note
|
|
|
|
{
|
|
|
|
// except this is sent BEFORE the entry is changed, and that doesn't send the above, so
|
|
|
|
// this is via http://stackoverflow.com/a/21059819/3408572 - it avoids the need to manage selected items
|
|
|
|
// this still isn't perfect — I get residual changes to the same value while navigating the list — but it's good enough
|
|
|
|
[self performSelector:@selector(controlTextDidChange:)
|
|
|
|
withObject:note
|
|
|
|
afterDelay:0];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)registerCombobox:(uiEditableCombobox *)c
|
|
|
|
{
|
2018-05-03 22:00:50 -05:00
|
|
|
uiprivMapSet(self->comboboxes, c->cb, c);
|
2016-05-23 17:50:02 -05:00
|
|
|
[c->cb setDelegate:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)unregisterCombobox:(uiEditableCombobox *)c
|
|
|
|
{
|
|
|
|
[c->cb setDelegate:nil];
|
2018-05-03 22:00:50 -05:00
|
|
|
uiprivMapDelete(self->comboboxes, c->cb);
|
2016-05-23 17:50:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
static editableComboboxDelegateClass *comboboxDelegate = nil;
|
|
|
|
|
|
|
|
uiDarwinControlAllDefaultsExceptDestroy(uiEditableCombobox, cb)
|
|
|
|
|
|
|
|
static void uiEditableComboboxDestroy(uiControl *cc)
|
|
|
|
{
|
|
|
|
uiEditableCombobox *c = uiEditableCombobox(cc);
|
|
|
|
|
|
|
|
[comboboxDelegate unregisterCombobox:c];
|
|
|
|
[c->cb release];
|
|
|
|
uiFreeControl(uiControl(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiEditableComboboxAppend(uiEditableCombobox *c, const char *text)
|
|
|
|
{
|
2018-05-03 21:38:21 -05:00
|
|
|
[c->cb addItemWithObjectValue:uiprivToNSString(text)];
|
2016-05-23 17:50:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
char *uiEditableComboboxText(uiEditableCombobox *c)
|
|
|
|
{
|
|
|
|
return uiDarwinNSStringToText([c->cb stringValue]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiEditableComboboxSetText(uiEditableCombobox *c, const char *text)
|
|
|
|
{
|
|
|
|
NSString *t;
|
|
|
|
|
2018-05-03 21:38:21 -05:00
|
|
|
t = uiprivToNSString(text);
|
2016-05-23 17:50:02 -05:00
|
|
|
[c->cb setStringValue:t];
|
2016-05-24 00:19:57 -05:00
|
|
|
// yes, let's imitate the behavior that caused uiEditableCombobox to be separate in the first place!
|
|
|
|
// just to avoid confusion when users see an option in the list in the text field but not selected in the list
|
2016-05-23 17:50:02 -05:00
|
|
|
[c->cb selectItemWithObjectValue:t];
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2016-05-28 17:01:25 -05:00
|
|
|
// LONGTERM
|
2016-06-13 20:37:50 -05:00
|
|
|
void uiEditableComboboxSetSelected(uiEditableCombobox *c, int n)
|
2016-05-23 17:50:02 -05:00
|
|
|
{
|
|
|
|
if (c->editable) {
|
|
|
|
// see https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ComboBox/Tasks/SettingComboBoxValue.html#//apple_ref/doc/uid/20000256
|
|
|
|
id delegate;
|
|
|
|
|
|
|
|
// this triggers the delegate; turn it off for now
|
|
|
|
delegate = [c->cb delegate];
|
|
|
|
[c->cb setDelegate:nil];
|
|
|
|
|
|
|
|
// this seems to work fine for -1 too
|
|
|
|
[c->cb selectItemAtIndex:n];
|
|
|
|
if (n == -1)
|
|
|
|
[c->cb setObjectValue:@""];
|
|
|
|
else
|
|
|
|
[c->cb setObjectValue:[c->cb objectValueOfSelectedItem]];
|
|
|
|
|
|
|
|
[c->cb setDelegate:delegate];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[c->pb selectItemAtIndex:n];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void uiEditableComboboxOnChanged(uiEditableCombobox *c, void (*f)(uiEditableCombobox *c, void *data), void *data)
|
|
|
|
{
|
|
|
|
c->onChanged = f;
|
|
|
|
c->onChangedData = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void defaultOnChanged(uiEditableCombobox *c, void *data)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
uiEditableCombobox *uiNewEditableCombobox(void)
|
|
|
|
{
|
|
|
|
uiEditableCombobox *c;
|
|
|
|
|
|
|
|
uiDarwinNewControl(uiEditableCombobox, c);
|
|
|
|
|
|
|
|
c->cb = [[libui_intrinsicWidthNSComboBox alloc] initWithFrame:NSZeroRect];
|
|
|
|
[c->cb setUsesDataSource:NO];
|
|
|
|
[c->cb setButtonBordered:YES];
|
|
|
|
[c->cb setCompletes:NO];
|
|
|
|
uiDarwinSetControlFont(c->cb, NSRegularControlSize);
|
|
|
|
|
|
|
|
if (comboboxDelegate == nil) {
|
2016-05-24 22:17:08 -05:00
|
|
|
comboboxDelegate = [[editableComboboxDelegateClass new] autorelease];
|
2018-05-05 18:46:57 -05:00
|
|
|
[uiprivDelegates addObject:comboboxDelegate];
|
2016-05-23 17:50:02 -05:00
|
|
|
}
|
|
|
|
[comboboxDelegate registerCombobox:c];
|
|
|
|
uiEditableComboboxOnChanged(c, defaultOnChanged, NULL);
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|