Migrated the GTK+ backend to the new sizer system.
This commit is contained in:
parent
0356d0fd70
commit
99b6b47a49
|
@ -9,17 +9,13 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// #include "gtk_unix.h"
|
// #include "gtk_unix.h"
|
||||||
// extern void layoutResizing(GtkWidget *, GdkRectangle *, gpointer);
|
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
type tab struct {
|
type tab struct {
|
||||||
*controlbase
|
*controlbase
|
||||||
notebook *C.GtkNotebook
|
notebook *C.GtkNotebook
|
||||||
|
|
||||||
containers []*container
|
tabs []*layout
|
||||||
layoutws []*C.GtkWidget
|
|
||||||
layoutcs []*C.GtkContainer
|
|
||||||
layouts []*C.GtkLayout
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTab() Tab {
|
func newTab() Tab {
|
||||||
|
@ -34,35 +30,16 @@ func newTab() Tab {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tab) Append(name string, control Control) {
|
func (t *tab) Append(name string, control Control) {
|
||||||
// TODO isolate and standardize
|
tl := newLayout(control)
|
||||||
layout := C.gtk_layout_new(nil, nil)
|
t.tabs = append(t.tabs, tl)
|
||||||
t.layoutws = append(t.layoutws, layout)
|
|
||||||
t.layoutcs = append(t.layoutcs, (*C.GtkContainer)(unsafe.Pointer(layout)))
|
|
||||||
t.layouts = append(t.layouts, (*C.GtkLayout)(unsafe.Pointer(layout)))
|
|
||||||
c := new(container)
|
|
||||||
t.containers = append(t.containers, c)
|
|
||||||
c.child = control
|
|
||||||
c.child.setParent(&controlParent{(*C.GtkContainer)(unsafe.Pointer(layout))})
|
|
||||||
g_signal_connect_after(
|
|
||||||
C.gpointer(unsafe.Pointer(layout)),
|
|
||||||
"size-allocate",
|
|
||||||
C.GCallback(C.layoutResizing),
|
|
||||||
C.gpointer(unsafe.Pointer(c)))
|
|
||||||
cname := togstr(name)
|
cname := togstr(name)
|
||||||
defer freegstr(cname)
|
defer freegstr(cname)
|
||||||
tab := C.gtk_notebook_append_page(t.notebook,
|
tab := C.gtk_notebook_append_page(t.notebook,
|
||||||
layout,
|
tl.layoutwidget,
|
||||||
C.gtk_label_new(cname))
|
C.gtk_label_new(cname))
|
||||||
if tab == -1 {
|
if tab == -1 {
|
||||||
panic("gtk_notebook_append_page() failed")
|
panic("gtk_notebook_append_page() failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// no need to override Control.allocate() as only prepared the tabbed control; its children will be reallocated when that one is resized
|
// no need to override Control.commitResize() as only prepared the tabbed control; its children will be reallocated when that one is resized
|
||||||
|
|
||||||
//export layoutResizing
|
|
||||||
func layoutResizing(wid *C.GtkWidget, r *C.GdkRectangle, data C.gpointer) {
|
|
||||||
c := (*container)(unsafe.Pointer(data))
|
|
||||||
// the layout's coordinate system is localized, so the origin is (0, 0)
|
|
||||||
c.resize(0, 0, int(r.width), int(r.height))
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,84 @@
|
||||||
|
// +build !windows,!darwin
|
||||||
|
|
||||||
|
// 23 february 2014
|
||||||
|
|
||||||
|
package ui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"unsafe"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// #include "gtk_unix.h"
|
||||||
|
// extern void layoutResizing(GtkWidget *, GdkRectangle *, gpointer);
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
type sizing struct {
|
||||||
|
sizingbase
|
||||||
|
|
||||||
|
// for size calculations
|
||||||
|
// gtk+ needs nothing
|
||||||
|
|
||||||
|
// for the actual resizing
|
||||||
|
shouldVAlignTop bool
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
gtkXMargin = 12
|
||||||
|
gtkYMargin = 12
|
||||||
|
gtkXPadding = 12
|
||||||
|
gtkYPadding = 6
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *sizer) beginResize() (d *sizing) {
|
||||||
|
d = new(sizing)
|
||||||
|
if spaced {
|
||||||
|
d.xmargin = gtkXMargin
|
||||||
|
d.ymargin = gtkYMargin
|
||||||
|
d.xpadding = gtkXPadding
|
||||||
|
d.ypadding = gtkYPadding
|
||||||
|
}
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *sizer) translateAllocationCoords(allocations []*allocation, winwidth, winheight int) {
|
||||||
|
// no need for coordinate conversion with gtk+
|
||||||
|
}
|
||||||
|
|
||||||
|
// layout maintains the widget hierarchy by containing all of a sizer's children in a single layout widget
|
||||||
|
type layout struct {
|
||||||
|
*sizer
|
||||||
|
layoutwidget *C.GtkWidget
|
||||||
|
layoutcontainer *C.GtkContainer
|
||||||
|
layout *C.GtkLayout
|
||||||
|
}
|
||||||
|
|
||||||
|
func newLayout(child Control) *layout {
|
||||||
|
widget := C.gtk_layout_new(nil, nil)
|
||||||
|
l := &layout{
|
||||||
|
sizer: new(sizer),
|
||||||
|
layoutwidget: widget,
|
||||||
|
layoutcontainer: (*C.GtkContainer)(unsafe.Pointer(widget)),
|
||||||
|
layout: (*C.GtkLayout)(unsafe.Pointer(widget)),
|
||||||
|
}
|
||||||
|
l.child = child
|
||||||
|
l.child.setParent(&controlParent{l.layoutcontainer})
|
||||||
|
// we connect to the layout's size-allocate, not to the window's configure-event
|
||||||
|
// this allows us to handle client-side decoration-based configurations (such as GTK+ on Wayland) properly
|
||||||
|
// also see commitResize() in sizing_unix.go for additional notes
|
||||||
|
// thanks to many people in irc.gimp.net/#gtk+ for help (including tristan for suggesting g_signal_connect_after())
|
||||||
|
g_signal_connect_after(
|
||||||
|
C.gpointer(unsafe.Pointer(l.layout)),
|
||||||
|
"size-allocate",
|
||||||
|
C.GCallback(C.layoutResizing),
|
||||||
|
C.gpointer(unsafe.Pointer(l)))
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
//export layoutResizing
|
||||||
|
func layoutResizing(wid *C.GtkWidget, r *C.GdkRectangle, data C.gpointer) {
|
||||||
|
l := (*layout)(unsafe.Pointer(data))
|
||||||
|
// the layout's coordinate system is localized, so the origin is (0, 0)
|
||||||
|
l.resize(0, 0, int(r.width), int(r.height))
|
||||||
|
fmt.Printf("new size %d x %d\n", r.width, r.height)
|
||||||
|
}
|
|
@ -1,40 +0,0 @@
|
||||||
// +build !windows,!darwin
|
|
||||||
|
|
||||||
// 23 february 2014
|
|
||||||
|
|
||||||
package ui
|
|
||||||
|
|
||||||
// #include "gtk_unix.h"
|
|
||||||
import "C"
|
|
||||||
|
|
||||||
type sizing struct {
|
|
||||||
sizingbase
|
|
||||||
|
|
||||||
// for size calculations
|
|
||||||
// gtk+ needs nothing
|
|
||||||
|
|
||||||
// for the actual resizing
|
|
||||||
shouldVAlignTop bool
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
gtkXMargin = 12
|
|
||||||
gtkYMargin = 12
|
|
||||||
gtkXPadding = 12
|
|
||||||
gtkYPadding = 6
|
|
||||||
)
|
|
||||||
|
|
||||||
func (c *container) beginResize() (d *sizing) {
|
|
||||||
d = new(sizing)
|
|
||||||
if spaced {
|
|
||||||
d.xmargin = gtkXMargin
|
|
||||||
d.ymargin = gtkYMargin
|
|
||||||
d.xpadding = gtkXPadding
|
|
||||||
d.ypadding = gtkYPadding
|
|
||||||
}
|
|
||||||
return d
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *container) translateAllocationCoords(allocations []*allocation, winwidth, winheight int) {
|
|
||||||
// no need for coordinate conversion with gtk+
|
|
||||||
}
|
|
|
@ -6,7 +6,6 @@ package ui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"unsafe"
|
"unsafe"
|
||||||
"fmt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// #include "gtk_unix.h"
|
// #include "gtk_unix.h"
|
||||||
|
@ -20,30 +19,21 @@ type window struct {
|
||||||
bin *C.GtkBin
|
bin *C.GtkBin
|
||||||
window *C.GtkWindow
|
window *C.GtkWindow
|
||||||
|
|
||||||
layoutw *C.GtkWidget
|
|
||||||
layoutc *C.GtkContainer
|
|
||||||
layout *C.GtkLayout
|
|
||||||
|
|
||||||
closing *event
|
closing *event
|
||||||
|
|
||||||
*container
|
*layout
|
||||||
}
|
}
|
||||||
|
|
||||||
func newWindow(title string, width int, height int, control Control) *window {
|
func newWindow(title string, width int, height int, control Control) *window {
|
||||||
widget := C.gtk_window_new(C.GTK_WINDOW_TOPLEVEL)
|
widget := C.gtk_window_new(C.GTK_WINDOW_TOPLEVEL)
|
||||||
ctitle := togstr(title)
|
ctitle := togstr(title)
|
||||||
defer freegstr(ctitle)
|
defer freegstr(ctitle)
|
||||||
layoutw := C.gtk_layout_new(nil, nil)
|
|
||||||
w := &window{
|
w := &window{
|
||||||
widget: widget,
|
widget: widget,
|
||||||
wc: (*C.GtkContainer)(unsafe.Pointer(widget)),
|
wc: (*C.GtkContainer)(unsafe.Pointer(widget)),
|
||||||
bin: (*C.GtkBin)(unsafe.Pointer(widget)),
|
bin: (*C.GtkBin)(unsafe.Pointer(widget)),
|
||||||
window: (*C.GtkWindow)(unsafe.Pointer(widget)),
|
window: (*C.GtkWindow)(unsafe.Pointer(widget)),
|
||||||
layoutw: layoutw,
|
|
||||||
layoutc: (*C.GtkContainer)(unsafe.Pointer(layoutw)),
|
|
||||||
layout: (*C.GtkLayout)(unsafe.Pointer(layoutw)),
|
|
||||||
closing: newEvent(),
|
closing: newEvent(),
|
||||||
container: new(container),
|
|
||||||
}
|
}
|
||||||
C.gtk_window_set_title(w.window, ctitle)
|
C.gtk_window_set_title(w.window, ctitle)
|
||||||
g_signal_connect(
|
g_signal_connect(
|
||||||
|
@ -51,19 +41,9 @@ func newWindow(title string, width int, height int, control Control) *window {
|
||||||
"delete-event",
|
"delete-event",
|
||||||
C.GCallback(C.windowClosing),
|
C.GCallback(C.windowClosing),
|
||||||
C.gpointer(unsafe.Pointer(w)))
|
C.gpointer(unsafe.Pointer(w)))
|
||||||
// we connect to the layout's size-allocate, not to the window's configure-event
|
|
||||||
// this allows us to handle client-side decoration-based configurations (such as GTK+ on Wayland) properly
|
|
||||||
// also see commitResize() in sizing_unix.go for additional notes
|
|
||||||
// thanks to many people in irc.gimp.net/#gtk+ for help (including tristan for suggesting g_signal_connect_after())
|
|
||||||
g_signal_connect_after(
|
|
||||||
C.gpointer(unsafe.Pointer(layoutw)),
|
|
||||||
"size-allocate",
|
|
||||||
C.GCallback(C.windowResizing),
|
|
||||||
C.gpointer(unsafe.Pointer(w)))
|
|
||||||
C.gtk_window_resize(w.window, C.gint(width), C.gint(height))
|
C.gtk_window_resize(w.window, C.gint(width), C.gint(height))
|
||||||
C.gtk_container_add(w.wc, layoutw)
|
w.layout = newLayout(control)
|
||||||
w.child = control
|
C.gtk_container_add(w.wc, w.layout.layoutwidget)
|
||||||
w.child.setParent(&controlParent{w.layoutc})
|
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,5 +88,4 @@ func windowResizing(wid *C.GtkWidget, r *C.GdkRectangle, data C.gpointer) {
|
||||||
w := (*window)(unsafe.Pointer(data))
|
w := (*window)(unsafe.Pointer(data))
|
||||||
// the origin of the window's content area is always (0, 0)
|
// the origin of the window's content area is always (0, 0)
|
||||||
w.resize(0, 0, int(r.width), int(r.height))
|
w.resize(0, 0, int(r.width), int(r.height))
|
||||||
fmt.Printf("new size %d x %d\n", r.width, r.height)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue