summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPietro Gagliardi <[email protected]>2014-03-03 14:50:03 -0500
committerPietro Gagliardi <[email protected]>2014-03-03 14:50:03 -0500
commit8bacbf8cd6bbdc89ef8f596c2310c147ccdbc509 (patch)
tree296963398c5463c0c9d91410d966d91c1bbc99ff
parentdd2abbaccef9e29644baa0090f10ef062235cae4 (diff)
Wrapped Mac OS X Listboxes in NSScrollViews. Their preferredSize seems to be more reasonable now. Now to just add scrollbars...
-rw-r--r--listbox_darwin.go44
-rw-r--r--prefsize_darwin.go4
2 files changed, 39 insertions, 9 deletions
diff --git a/listbox_darwin.go b/listbox_darwin.go
index e32443d..71227af 100644
--- a/listbox_darwin.go
+++ b/listbox_darwin.go
@@ -139,7 +139,7 @@ func bindListboxArray(tableColumn C.id, array C.id) {
C.nilid) // no options
}
-func listboxArray(tableColumn C.id) C.id {
+func listboxArrayController(tableColumn C.id) C.id {
dict := C.objc_msgSend_id(tableColumn, _infoForBinding, tableColumnBinding)
return C.objc_msgSend_id(dict, _objectForKey, *C._NSObservedObjectKey)
}
@@ -170,6 +170,37 @@ func listboxTableColumn(listbox C.id) C.id {
}
/*
+The NSTableViews don't draw their own scrollbars; we have to drop our NSTableViews in NSScrollViews for this.
+*/
+
+var (
+ _NSScrollView = objc_getClass("NSScrollView")
+
+ _setDocumentView = sel_getUid("setDocumentView:")
+ _documentView = sel_getUid("documentView")
+)
+
+func newListboxScrollView(listbox C.id) C.id {
+ scrollview := objc_alloc(_NSScrollView)
+ scrollview = objc_msgSend_rect(scrollview, _initWithFrame,
+ 0, 0, 100, 100)
+ C.objc_msgSend_id(scrollview, _setDocumentView, listbox)
+ return scrollview
+}
+
+func listboxInScrollView(scrollview C.id) C.id {
+ return C.objc_msgSend_noargs(scrollview, _documentView)
+}
+
+/*
+And now, a helper function that takes a scroll view and gets out the array.
+*/
+
+func listboxArray(listbox C.id) C.id {
+ return listboxArrayController(listboxTableColumn(listboxInScrollView(listbox)))
+}
+
+/*
...and finally, we work with the NSTableView directly. These are the methods sysData calls.
We'll handle selections from the NSTableView too. The only trickery is dealing with the return value of -[NSTableView selectedRowIndexes]: NSIndexSet. The only way to get indices out of a NSIndexSet is to get them all out wholesale, and working with C arrays in Go is Not Fun.
@@ -199,18 +230,19 @@ func makeListbox(parentWindow C.id, alternate bool) C.id {
C.objc_msgSend_bool(listbox, _setAllowsEmptySelection, C.BOOL(C.YES))
C.objc_msgSend_id(listbox, _setHeaderView, C.nilid)
// TODO others?
+ listbox = newListboxScrollView(listbox)
windowView := C.objc_msgSend_noargs(parentWindow, _contentView)
C.objc_msgSend_id(windowView, _addSubview, listbox)
return listbox
}
func appendListbox(listbox C.id, what string, alternate bool) {
- array := listboxArray(listboxTableColumn(listbox))
+ array := listboxArray(listbox)
appendListboxArray(array, what)
}
func insertListboxBefore(listbox C.id, what string, before int, alternate bool) {
- array := listboxArray(listboxTableColumn(listbox))
+ array := listboxArray(listbox)
insertListboxArrayBefore(array, what, before)
}
@@ -220,7 +252,7 @@ func insertListboxBefore(listbox C.id, what string, before int, alternate bool)
func selectedListboxIndices(listbox C.id) (list []int) {
var cindices []C.uintptr_t
- indices := C.objc_msgSend_noargs(listbox, _selectedRowIndexes)
+ indices := C.objc_msgSend_noargs(listboxInScrollView(listbox), _selectedRowIndexes)
count := int(C.objc_msgSend_uintret_noargs(indices, _count))
if count == 0 {
return nil
@@ -243,7 +275,7 @@ func selectedListboxTexts(listbox C.id) (texts []string) {
if len(indices) == 0 {
return nil
}
- array := listboxArray(listboxTableColumn(listbox))
+ array := listboxArray(listbox)
texts = make([]string, len(indices))
for i := 0; i < len(texts); i++ {
texts[i] = indexListboxArray(array, indices[i])
@@ -252,6 +284,6 @@ func selectedListboxTexts(listbox C.id) (texts []string) {
}
func deleteListbox(listbox C.id, index int) {
- array := listboxArray(listboxTableColumn(listbox))
+ array := listboxArray(listbox)
deleteListboxArray(array, index)
}
diff --git a/prefsize_darwin.go b/prefsize_darwin.go
index 0629153..f958c71 100644
--- a/prefsize_darwin.go
+++ b/prefsize_darwin.go
@@ -12,7 +12,6 @@ Cocoa doesn't provide a reliable way to get the preferred size of a control (you
var (
_sizeToFit = sel_getUid("sizeToFit")
// _frame in sysdata_darwin.go
- // _documentView in listbox_darwin.go
)
// standard case: control immediately passed in
@@ -24,8 +23,7 @@ func controlPrefSize(control C.id) (width int, height int) {
// NSTableView is actually in a NSScrollView so we have to get it out first
func listboxPrefSize(control C.id) (width int, height int) {
-// return controlPrefSize(C.objc_msgSend_noargs(control, _documentView))
- return controlPrefSize(control)
+ return controlPrefSize(listboxInScrollView(control))
}
var prefsizefuncs = [nctypes]func(C.id) (int, int){