summaryrefslogtreecommitdiff
path: root/redo/layout_windows.go
blob: 7d55a58e77f5697d7353676fe6551765bf53b20a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// 4 august 2014

package ui

// TODO clean this up relative to window_windows.go

import (
	"fmt"
	"unsafe"
)

// #include "winapi_windows.h"
import "C"

type layout struct {
	hwnd		C.HWND

	closing		*event

	*sizer
}

func newLayout(title string, width int, height int, child C.BOOL, control Control) *layout {
	l := &layout{
		// hwnd set in WM_CREATE handler
		closing:		newEvent(),
		sizer:		new(sizer),
	}
	hwnd := C.newWindow(toUTF16(title), C.int(width), C.int(height), child, unsafe.Pointer(l))
	if hwnd != l.hwnd {
		panic(fmt.Errorf("inconsistency: hwnd returned by CreateWindowEx() (%p) and hwnd stored in window/layout (%p) differ", hwnd, l.hwnd))
	}
	l.child = control
	l.child.setParent(&controlParent{l.hwnd})
	return l
}

func (l *layout) setParent(p *controlParent) {
	C.controlSetParent(l.hwnd, p.hwnd)
}

//export storeWindowHWND
func storeWindowHWND(data unsafe.Pointer, hwnd C.HWND) {
	l := (*layout)(data)
	l.hwnd = hwnd
}

//export windowResize
func windowResize(data unsafe.Pointer, r *C.RECT) {
	l := (*layout)(data)
	// the origin of the window's content area is always (0, 0), but let's use the values from the RECT just to be safe
	l.resize(int(r.left), int(r.top), int(r.right - r.left), int(r.bottom - r.top))
}

//export windowClosing
func windowClosing(data unsafe.Pointer) {
	l := (*layout)(data)
	close := l.closing.fire()
	if close {
		C.windowClose(l.hwnd)
	}
}