summaryrefslogtreecommitdiff
path: root/redo/window_unix.go
blob: f110e5fd7b220e9e95d5d5dbd4c4a9b4ad052fab (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// +build !darwin

// 7 july 2014

package ui

import (
	"unsafe"
"fmt"
)

// #include "gtk_unix.h"
// extern gboolean windowClosing(GtkWidget *, GdkEvent *, gpointer);
// extern gboolean windowResizing(GtkWidget *, GdkEvent *, gpointer);
import "C"

type window struct {
	widget	*C.GtkWidget
	container	*C.GtkContainer
	bin		*C.GtkBin
	window	*C.GtkWindow

	layoutc	*C.GtkContainer
	layout	*C.GtkLayout

	child		Control

	closing	*event
}

func newWindow(title string, width int, height int) *Request {
	c := make(chan interface{})
	return &Request{
		op:		func() {
			widget := C.gtk_window_new(C.GTK_WINDOW_TOPLEVEL)
			ctitle := togstr(title)
			defer freegstr(ctitle)
			layoutw := C.gtk_layout_new(nil, nil)
			w := &window{
				widget:		widget,
				container:		(*C.GtkContainer)(unsafe.Pointer(widget)),
				bin:			(*C.GtkBin)(unsafe.Pointer(widget)),
				window:		(*C.GtkWindow)(unsafe.Pointer(widget)),
				layoutc:		(*C.GtkContainer)(unsafe.Pointer(layoutw)),
				layout:		(*C.GtkLayout)(unsafe.Pointer(layoutw)),
				closing:		newEvent(),
			}
			C.gtk_window_set_title(w.window, ctitle)
			g_signal_connect(
				C.gpointer(unsafe.Pointer(w.window)),
				"delete-event",
				C.GCallback(C.windowClosing),
				C.gpointer(unsafe.Pointer(w)))
			g_signal_connect(
				C.gpointer(unsafe.Pointer(w.window)),
				"configure-event",
				C.GCallback(C.windowResizing),
				C.gpointer(unsafe.Pointer(w)))
			// TODO size
			C.gtk_container_add(w.container, layoutw)
			c <- w
		},
		resp:		c,
	}
}

func (w *window) SetControl(control Control) *Request {
	c := make(chan interface{})
	return &Request{
		op:		func() {
			if w.child != nil {		// unparent existing control
				w.child.unparent()
			}
			control.unparent()
			control.parent(w)
			w.child = control
			c <- struct{}{}
		},
		resp:		c,
	}
}

func (w *window) Title() *Request {
	c := make(chan interface{})
	return &Request{
		op:		func() {
			c <- fromgstr(C.gtk_window_get_title(w.window))
		},
		resp:		c,
	}
}

func (w *window) SetTitle(title string) *Request {
	c := make(chan interface{})
	return &Request{
		op:		func() {
			ctitle := togstr(title)
			defer freegstr(ctitle)
			C.gtk_window_set_title(w.window, ctitle)
			c <- struct{}{}
		},
		resp:		c,
	}
}


func (w *window) Show() *Request {
	c := make(chan interface{})
	return &Request{
		op:		func() {
			C.gtk_widget_show_all(w.widget)
			c <- struct{}{}
		},
		resp:		c,
	}
}

func (w *window) Hide() *Request {
	c := make(chan interface{})
	return &Request{
		op:		func() {
			C.gtk_widget_hide(w.widget)
			c <- struct{}{}
		},
		resp:		c,
	}
}

func (w *window) Close() *Request {
	c := make(chan interface{})
	return &Request{
		op:		func() {
			C.gtk_widget_destroy(w.widget)
			c <- struct{}{}
		},
		resp:		c,
	}
}

func (w *window) OnClosing(e func(c Doer) bool) *Request {
	c := make(chan interface{})
	return &Request{
		op:		func() {
			w.closing.setbool(e)
			c <- struct{}{}
		},
		resp:		c,
	}
}

//export windowClosing
func windowClosing(wid *C.GtkWidget, e *C.GdkEvent, data C.gpointer) C.gboolean {
	w := (*window)(unsafe.Pointer(data))
	close := w.closing.fire()
	if close {
		return C.GDK_EVENT_PROPAGATE		// will do gtk_widget_destroy(), which is what we want (thanks ebassi in irc.gimp.net/#gtk+)
	}
	return C.GDK_EVENT_STOP				// keeps window alive
}

//export windowResizing
func windowResizing(wid *C.GtkWidget, event *C.GdkEvent, data C.gpointer) C.gboolean {
	w := (*window)(unsafe.Pointer(data))
	e := (*C.GdkEventConfigure)(unsafe.Pointer(event))
	_ = w // TODO
	// TODO this does not take CSD into account; my attempts at doing so so far have failed to work correctly in the face of rapid live resizing
	// TODO triggered twice on each resize or maximize for some reason???
	fmt.Printf("new size %d x %d\n", e.width, e.height)
	return C.GDK_EVENT_PROPAGATE		// let's be safe
}