2014-07-25 14:58:24 -05:00
|
|
|
// 25 july 2014
|
|
|
|
|
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// #include "winapi_windows.h"
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
/*
|
2014-08-06 19:53:11 -05:00
|
|
|
On Windows, container controls are just regular controls that notify their parent when the user wants to do things; changing the contents of a switching container (such as a tab control) must be done manually.
|
2014-07-25 14:58:24 -05:00
|
|
|
|
2014-10-18 16:03:07 -05:00
|
|
|
We'll create a dummy window using the container window class for each tab page. This makes showing and hiding tabs a matter of showing and hiding one control.
|
2014-07-25 14:58:24 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
type tab struct {
|
2014-10-18 16:03:07 -05:00
|
|
|
*controlSingleHWND
|
|
|
|
tabs []*container
|
|
|
|
children []Control
|
|
|
|
chainresize func(x int, y int, width int, height int, d *sizing)
|
2014-07-25 14:58:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTab() Tab {
|
2014-08-03 20:52:21 -05:00
|
|
|
hwnd := C.newControl(C.xWC_TABCONTROL,
|
2014-10-02 09:05:53 -05:00
|
|
|
C.TCS_TOOLTIPS|C.WS_TABSTOP,
|
|
|
|
0) // don't set WS_EX_CONTROLPARENT here; see uitask_windows.c
|
2014-07-25 14:58:24 -05:00
|
|
|
t := &tab{
|
2014-10-18 16:03:07 -05:00
|
|
|
controlSingleHWND: newControlSingleHWND(hwnd),
|
2014-07-25 14:58:24 -05:00
|
|
|
}
|
2014-10-18 16:03:07 -05:00
|
|
|
t.fpreferredSize = t.xpreferredSize
|
|
|
|
t.chainresize = t.fresize
|
|
|
|
t.fresize = t.xresize
|
|
|
|
// count tabs as 1 tab stop; the actual number of tab stops varies
|
|
|
|
C.controlSetControlFont(t.hwnd)
|
|
|
|
C.setTabSubclass(t.hwnd, unsafe.Pointer(t))
|
2014-07-25 14:58:24 -05:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2014-10-18 16:03:07 -05:00
|
|
|
// TODO margined
|
2014-07-25 14:58:24 -05:00
|
|
|
func (t *tab) Append(name string, control Control) {
|
2014-10-18 16:03:07 -05:00
|
|
|
c := newContainer()
|
|
|
|
control.setParent(&controlParent{c.hwnd})
|
|
|
|
c.setParent(&controlParent{t.hwnd})
|
2014-08-04 19:04:16 -05:00
|
|
|
t.tabs = append(t.tabs, c)
|
2014-10-18 16:03:07 -05:00
|
|
|
t.children = append(t.children, control)
|
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 {
|
2014-10-02 09:05:53 -05:00
|
|
|
t.tabs[len(t.tabs)-1].hide()
|
2014-07-28 20:10:13 -05:00
|
|
|
}
|
2014-10-18 16:03:07 -05:00
|
|
|
C.tabAppend(t.hwnd, toUTF16(name))
|
2014-07-25 14:58:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//export tabChanging
|
|
|
|
func tabChanging(data unsafe.Pointer, current C.LRESULT) {
|
|
|
|
t := (*tab)(data)
|
2014-08-04 20:33:58 -05:00
|
|
|
t.tabs[int(current)].hide()
|
2014-07-25 14:58:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//export tabChanged
|
|
|
|
func tabChanged(data unsafe.Pointer, new C.LRESULT) {
|
|
|
|
t := (*tab)(data)
|
2014-08-04 20:33:58 -05:00
|
|
|
t.tabs[int(new)].show()
|
2014-07-25 14:58:24 -05:00
|
|
|
}
|
|
|
|
|
2014-08-14 12:05:31 -05:00
|
|
|
//export tabTabHasChildren
|
|
|
|
func tabTabHasChildren(data unsafe.Pointer, which C.LRESULT) C.BOOL {
|
|
|
|
t := (*tab)(data)
|
2014-10-02 09:05:53 -05:00
|
|
|
if len(t.tabs) == 0 { // currently no tabs
|
2014-08-21 12:02:33 -05:00
|
|
|
return C.FALSE
|
|
|
|
}
|
2014-10-18 16:03:07 -05:00
|
|
|
if t.children[int(which)].nTabStops() > 0 {
|
2014-08-14 12:05:31 -05:00
|
|
|
return C.TRUE
|
|
|
|
}
|
|
|
|
return C.FALSE
|
|
|
|
}
|
|
|
|
|
2014-10-18 16:03:07 -05:00
|
|
|
func (t *tab) xpreferredSize(d *sizing) (width, height int) {
|
|
|
|
for _, c := range t.children {
|
|
|
|
w, h := c.preferredSize(d)
|
2014-08-02 11:35:36 -05:00
|
|
|
if width < w {
|
|
|
|
width = w
|
|
|
|
}
|
|
|
|
if height < h {
|
|
|
|
height = h
|
|
|
|
}
|
|
|
|
}
|
2014-10-18 16:03:07 -05:00
|
|
|
return width, height + int(C.tabGetTabHeight(t.hwnd))
|
2014-08-02 11:35:36 -05:00
|
|
|
}
|
|
|
|
|
2014-07-25 14:58:24 -05:00
|
|
|
// a tab control contains other controls; size appropriately
|
2014-10-18 16:03:07 -05:00
|
|
|
func (t *tab) xresize(x int, y int, width int, height int, d *sizing) {
|
|
|
|
// first, chain up to the container base to keep the Z-order correct
|
|
|
|
t.chainresize(x, y, width, height, d)
|
|
|
|
|
|
|
|
// now resize the children
|
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...
|
2014-10-18 16:03:07 -05:00
|
|
|
// the tab contents are children of the tab itself, so ignore x and y, which are relative to the window!
|
2014-08-06 19:53:11 -05:00
|
|
|
r.left = C.LONG(0)
|
|
|
|
r.top = C.LONG(0)
|
2014-10-18 16:03:07 -05:00
|
|
|
r.right = C.LONG(width)
|
|
|
|
r.bottom = C.LONG(height)
|
|
|
|
C.tabGetContentRect(t.hwnd, &r)
|
2014-08-03 08:18:35 -05:00
|
|
|
// and resize tabs
|
|
|
|
// don't resize just the current tab; resize all tabs!
|
2014-10-18 16:03:07 -05:00
|
|
|
for i, _ := 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
|
2014-10-18 16:03:07 -05:00
|
|
|
t.tabs[i].resize(int(r.left), int(r.top), int(r.right - r.left), int(r.bottom - r.top), d)
|
|
|
|
// TODO get the actual client rect
|
|
|
|
t.children[i].resize(int(0), int(0), int(r.right - r.left), int(r.bottom - r.top), d)
|
2014-07-25 14:58:24 -05:00
|
|
|
}
|
|
|
|
}
|