// List Boxes do not dynamically handle horizontal scrollbars.
// We have to manually handle this ourselves.
// TODO make this run on the main thread when we switch to uitask taking function literals
// TODO this is inefficient; some caching would be nice
funcrecalcListboxWidth(hwnd_HWND){
varsize_SIZE
ret:=make(chanuiret)
deferclose(ret)
uitask<-&uimsg{
call:_sendMessage,
p:[]uintptr{
uintptr(hwnd),
uintptr(_LB_GETCOUNT),
uintptr(0),
uintptr(0),
},
ret:ret,
}
r:=<-ret
ifr.ret==uintptr(_LB_ERR){// failure
panic(fmt.Errorf("error getting number of items for Listbox width calculations: %v",r.err))
}
n:=int(r.ret)
uitask<-&uimsg{
call:_getWindowDC,
p:[]uintptr{uintptr(hwnd)},
ret:ret,
}
r=<-ret
ifr.ret==0{// failure
panic(fmt.Errorf("error getting DC for Listbox width calculations: %v",r.err))
}
dc:=_HANDLE(r.ret)
uitask<-&uimsg{
call:_selectObject,
p:[]uintptr{
uintptr(dc),
uintptr(controlFont),
},
ret:ret,
}
r=<-ret
ifr.ret==0{// failure
panic(fmt.Errorf("error loading control font into device context for Listbox width calculation: %v",r.err))
}
hextent:=uintptr(0)
fori:=0;i<n;i++{
uitask<-&uimsg{
call:_sendMessage,
p:[]uintptr{
uintptr(hwnd),
uintptr(_LB_GETTEXTLEN),
uintptr(_WPARAM(i)),
uintptr(0),
},
ret:ret,
}
r:=<-ret
ifr.ret==uintptr(_LB_ERR){
panic("UI library internal error: LB_ERR from LB_GETTEXTLEN in what we know is a valid listbox index (came from LB_GETSELITEMS)")
}
str:=make([]uint16,r.ret)
uitask<-&uimsg{
call:_sendMessage,
p:[]uintptr{
uintptr(hwnd),
uintptr(_LB_GETTEXT),
uintptr(_WPARAM(i)),
uintptr(_LPARAM(unsafe.Pointer(&str[0]))),
},
ret:ret,
}
r=<-ret
ifr.ret==uintptr(_LB_ERR){
panic("UI library internal error: LB_ERR from LB_GETTEXT in what we know is a valid listbox index (came from LB_GETSELITEMS)")
}
// r.ret is still the length of the string; this time without the null terminator
uitask<-&uimsg{
call:_getTextExtentPoint32,
p:[]uintptr{
uintptr(dc),
uintptr(unsafe.Pointer(&str[0])),
r.ret,
uintptr(unsafe.Pointer(&size)),
},
ret:ret,
}
r=<-ret
ifr.ret==0{// failure
panic(fmt.Errorf("error getting width of item %d text for Listbox width calculation: %v",i,r.err))
}
ifhextent<uintptr(size.cx){
hextent=uintptr(size.cx)
}
}
uitask<-&uimsg{
call:_releaseDC,
p:[]uintptr{
uintptr(hwnd),
uintptr(dc),
},
ret:ret,
}
r=<-ret
ifr.ret==0{// failure
panic(fmt.Errorf("error releasing DC for Listbox width calculations: %v",r.err))
}
uitask<-&uimsg{
call:_sendMessage,
p:[]uintptr{
uintptr(hwnd),
uintptr(_LB_SETHORIZONTALEXTENT),
hextent,
uintptr(0),
},
ret:ret,
}
<-ret
}
// DARWIN (does not work)
// NSTableView is actually in a NSScrollView so we have to get it out first
// NSTableView and NSTableColumn both provide sizeToFit methods, but they don't do what we want (NSTableView's sizes to fit the parent; NSTableColumn's sizes to fit the column header)
// We have to get the width manually; see also http://stackoverflow.com/questions/4674163/nstablecolumn-size-to-fit-contents
// We can use the NSTableView sizeToFit to get the height, though.
// TODO having this here is a hack; split it into a separate function in listbox_darwin.go
// the NSTableView:NSTableColumn ratio is what determines horizontal scrolling; see http://stackoverflow.com/questions/7050497/enable-scrolling-for-nstableview