2014-07-25 14:58:24 -05:00
|
|
|
// 25 july 2014
|
|
|
|
|
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// #include "winapi_windows.h"
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
/*
|
2014-07-28 14:02:27 -05:00
|
|
|
On Windows, container controls are just regular controls; their children have to be children of the parent window, and changing the contents of a switching container (such as a tab control) must be done manually.
|
2014-07-25 14:58:24 -05:00
|
|
|
|
|
|
|
TODO
|
|
|
|
- make sure all tabs cannot be deselected (that is, make sure the current tab can never have index -1)
|
2014-07-28 20:10:13 -05:00
|
|
|
- see if we can safely make the controls children of the tab control itself or if that would just screw our subclassing
|
2014-07-25 14:58:24 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
type tab struct {
|
2014-07-30 01:06:01 -05:00
|
|
|
*controlbase
|
|
|
|
tabs []*container
|
|
|
|
supersetParent func(p *controlParent)
|
|
|
|
superallocate func(x int, y int, width int, height int, d *sizing) []*allocation
|
2014-07-25 14:58:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTab() Tab {
|
2014-07-30 01:06:01 -05:00
|
|
|
c := newControl(C.xWC_TABCONTROL,
|
2014-07-25 14:58:24 -05:00
|
|
|
C.TCS_TOOLTIPS | C.WS_TABSTOP,
|
|
|
|
0)
|
|
|
|
t := &tab{
|
2014-07-30 01:06:01 -05:00
|
|
|
controlbase: c,
|
2014-07-25 14:58:24 -05:00
|
|
|
}
|
2014-07-30 01:06:01 -05:00
|
|
|
t.supersetParent = t.fsetParent
|
|
|
|
t.fsetParent = t.tabsetParent
|
|
|
|
t.superallocate = t.fallocate
|
|
|
|
t.fallocate = t.taballocate
|
|
|
|
C.controlSetControlFont(t.hwnd)
|
|
|
|
C.setTabSubclass(t.hwnd, unsafe.Pointer(t))
|
2014-07-25 14:58:24 -05:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2014-07-30 01:06:01 -05:00
|
|
|
func (t *tab) tabsetParent(p *controlParent) {
|
|
|
|
t.supersetParent(p)
|
2014-07-25 14:58:24 -05:00
|
|
|
for _, c := range t.tabs {
|
2014-07-29 22:01:28 -05:00
|
|
|
c.child.setParent(p)
|
2014-07-25 14:58:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tab) Append(name string, control Control) {
|
2014-07-28 14:02:27 -05:00
|
|
|
c := new(container)
|
|
|
|
t.tabs = append(t.tabs, c)
|
|
|
|
c.child = control
|
2014-07-25 16:34:45 -05:00
|
|
|
if t.parent != nil {
|
2014-07-29 22:01:28 -05:00
|
|
|
c.child.setParent(&controlParent{t.parent})
|
2014-07-25 14:58:24 -05:00
|
|
|
}
|
2014-07-28 20:10:13 -05:00
|
|
|
// initially hide tab 1..n controls; if we don't, they'll appear over other tabs, resulting in weird behavior
|
|
|
|
if len(t.tabs) != 1 {
|
|
|
|
c.child.containerHide()
|
|
|
|
}
|
2014-07-25 14:58:24 -05:00
|
|
|
C.tabAppend(t.hwnd, toUTF16(name))
|
|
|
|
}
|
|
|
|
|
|
|
|
//export tabChanging
|
|
|
|
func tabChanging(data unsafe.Pointer, current C.LRESULT) {
|
|
|
|
t := (*tab)(data)
|
2014-07-28 14:02:27 -05:00
|
|
|
t.tabs[int(current)].child.containerHide()
|
2014-07-25 14:58:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//export tabChanged
|
|
|
|
func tabChanged(data unsafe.Pointer, new C.LRESULT) {
|
|
|
|
t := (*tab)(data)
|
2014-07-28 14:02:27 -05:00
|
|
|
t.tabs[int(new)].child.containerShow()
|
2014-07-25 14:58:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// a tab control contains other controls; size appropriately
|
2014-07-30 01:06:01 -05:00
|
|
|
func (t *tab) taballocate(x int, y int, width int, height int, d *sizing) []*allocation {
|
2014-07-25 14:58:24 -05:00
|
|
|
var r C.RECT
|
|
|
|
|
2014-07-28 14:02:27 -05:00
|
|
|
// figure out what the rect for each child is...
|
|
|
|
r.left = C.LONG(x) // load structure with the window's rect
|
2014-07-25 14:58:24 -05:00
|
|
|
r.top = C.LONG(y)
|
|
|
|
r.right = C.LONG(x + width)
|
|
|
|
r.bottom = C.LONG(y + height)
|
|
|
|
C.tabGetContentRect(t.hwnd, &r)
|
|
|
|
// and allocate
|
2014-07-28 14:02:27 -05:00
|
|
|
// don't allocate to just the current tab; allocate to all tabs!
|
2014-07-25 14:58:24 -05:00
|
|
|
for _, c := range t.tabs {
|
2014-07-28 14:02:27 -05:00
|
|
|
// because each widget is actually a child of the Window, the origin is the one we calculated above
|
|
|
|
c.resize(int(r.left), int(r.top), int(r.right - r.left), int(r.bottom - r.top))
|
2014-07-25 14:58:24 -05:00
|
|
|
}
|
2014-07-28 14:02:27 -05:00
|
|
|
// and now allocate the tab control itself
|
2014-07-30 01:06:01 -05:00
|
|
|
return t.superallocate(x, y, width, height, d)
|
2014-07-25 14:58:24 -05:00
|
|
|
}
|