From 274fa0c2928e3d1573a83d2f68048963df5e16e4 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 12 Apr 2014 21:49:41 -0400 Subject: Fixed Comboboxes on Mac OS X having an initial selection. This also lays the groundwork for adding Combobox/Listbox.Select() as a public function... --- sysdata_darwin.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'sysdata_darwin.go') diff --git a/sysdata_darwin.go b/sysdata_darwin.go index 1b6795c..cfbd00e 100644 --- a/sysdata_darwin.go +++ b/sysdata_darwin.go @@ -33,6 +33,7 @@ type classData struct { selTexts func(id C.id) []string delete func(id C.id, index int) len func(id C.id) int + selectIndex func(id C.id, index int, alternate bool) } var ( @@ -82,6 +83,8 @@ var ( _setIndeterminate = sel_getUid("setIndeterminate:") _setDoubleValue = sel_getUid("setDoubleValue:") _numberOfItems = sel_getUid("numberOfItems") + _selectItemAtIndex = sel_getUid("selectItemAtIndex:") + _deselectItemAtIndex = sel_getUid("deselectItemAtIndex:") ) // because the only way to make a new NSControl/NSView is with a frame (it gets overridden later) @@ -238,6 +241,22 @@ var classTypes = [nctypes]*classData{ len: func(id C.id) int { return int(C.objc_msgSend_intret_noargs(id, _numberOfItems)) }, + selectIndex: func(id C.id, index int, alternate bool) { + // NSPopUpButton makes this easy + if !alternate { + C.objc_msgSend_int(id, _selectItemAtIndex, C.intptr_t(index)) + return + } + // NSComboBox doesn't document that we can do [cb selectItemAtIndex:-1], so we have to do this to be safe + if index == -1 { + idx := C.objc_msgSend_intret_noargs(id, _indexOfSelectedItem) + if idx != -1 { + C.objc_msgSend_int(id, _deselectItemAtIndex, idx) + } + return + } + C.objc_msgSend_int(id, _selectItemAtIndex, C.intptr_t(index)) + }, }, c_lineedit: &classData{ make: func(parentWindow C.id, alternate bool) C.id { @@ -542,3 +561,13 @@ func (s *sysData) setAreaSize(width int, height int) { } <-ret } + +func (s *sysData) selectIndex(index int) { + ret := make(chan struct{}) + defer close(ret) + uitask <- func() { + classTypes[s.ctype].selectIndex(s.id, index, s.alternate) + ret <- struct{}{} + } + <-ret +} -- cgit v1.2.3