From 23baffe55ed5964a24f41e719b8f538f1bb04168 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Mon, 4 Aug 2014 21:08:18 -0400 Subject: [PATCH] Applied the container change to the GTK+ backend. Woo! --- redo/{sizer_unix.go => container_unix.go} | 80 +++++++++++------------ redo/tab_unix.go | 9 +-- redo/window_unix.go | 15 ++--- 3 files changed, 48 insertions(+), 56 deletions(-) rename redo/{sizer_unix.go => container_unix.go} (56%) diff --git a/redo/sizer_unix.go b/redo/container_unix.go similarity index 56% rename from redo/sizer_unix.go rename to redo/container_unix.go index 4a3c7b4..964df5a 100644 --- a/redo/sizer_unix.go +++ b/redo/container_unix.go @@ -10,9 +10,45 @@ import ( ) // #include "gtk_unix.h" -// extern void layoutResizing(GtkWidget *, GdkRectangle *, gpointer); +// extern void containerResizing(GtkWidget *, GdkRectangle *, gpointer); import "C" +type container struct { + containerbase + layoutwidget *C.GtkWidget + layoutcontainer *C.GtkContainer + layout *C.GtkLayout +} + +func newContainer(child Control) *container { + widget := C.gtk_layout_new(nil, nil) + c := &container{ + layoutwidget: widget, + layoutcontainer: (*C.GtkContainer)(unsafe.Pointer(widget)), + layout: (*C.GtkLayout)(unsafe.Pointer(widget)), + } + c.child = child + c.child.setParent(&controlParent{c.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 basecommitResize() in control_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(c.layout)), + "size-allocate", + C.GCallback(C.containerResizing), + C.gpointer(unsafe.Pointer(c))) + return c +} + +//export containerResizing +func containerResizing(wid *C.GtkWidget, r *C.GdkRectangle, data C.gpointer) { + c := (*container)(unsafe.Pointer(data)) + // the GtkLayout's coordinate system is localized, so the origin is (0, 0) + c.resize(0, 0, int(r.width), int(r.height)) +fmt.Printf("new size %d x %d\n", r.width, r.height) +} + type sizing struct { sizingbase @@ -30,7 +66,7 @@ const ( gtkYPadding = 6 ) -func (s *sizer) beginResize() (d *sizing) { +func (c *container) beginResize() (d *sizing) { d = new(sizing) if spaced { d.xmargin = gtkXMargin @@ -41,44 +77,6 @@ func (s *sizer) beginResize() (d *sizing) { return d } -func (s *sizer) translateAllocationCoords(allocations []*allocation, winwidth, winheight int) { +func (c *container) 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) -} diff --git a/redo/tab_unix.go b/redo/tab_unix.go index 22a0c66..e815935 100644 --- a/redo/tab_unix.go +++ b/redo/tab_unix.go @@ -15,7 +15,7 @@ type tab struct { _widget *C.GtkWidget notebook *C.GtkNotebook - tabs []*layout + tabs []*container } func newTab() Tab { @@ -30,12 +30,13 @@ func newTab() Tab { } func (t *tab) Append(name string, control Control) { - tl := newLayout(control) - t.tabs = append(t.tabs, tl) + c := newContainer(control) + t.tabs = append(t.tabs, c) cname := togstr(name) defer freegstr(cname) tab := C.gtk_notebook_append_page(t.notebook, - tl.layoutwidget, + // TODO figure out how to keep this private + c.layoutwidget, C.gtk_label_new(cname)) if tab == -1 { panic("gtk_notebook_append_page() failed") diff --git a/redo/window_unix.go b/redo/window_unix.go index 3274d77..7dcd60f 100644 --- a/redo/window_unix.go +++ b/redo/window_unix.go @@ -10,7 +10,6 @@ import ( // #include "gtk_unix.h" // extern gboolean windowClosing(GtkWidget *, GdkEvent *, gpointer); -// extern void windowResizing(GtkWidget *, GdkRectangle *, gpointer); import "C" type window struct { @@ -21,7 +20,7 @@ type window struct { closing *event - *layout + *container } func newWindow(title string, width int, height int, control Control) *window { @@ -42,8 +41,9 @@ func newWindow(title string, width int, height int, control Control) *window { C.GCallback(C.windowClosing), C.gpointer(unsafe.Pointer(w))) C.gtk_window_resize(w.window, C.gint(width), C.gint(height)) - w.layout = newLayout(control) - C.gtk_container_add(w.wc, w.layout.layoutwidget) + w.container = newContainer(control) + // TODO make a method of container + C.gtk_container_add(w.wc, w.container.layoutwidget) return w } @@ -82,10 +82,3 @@ func windowClosing(wid *C.GtkWidget, e *C.GdkEvent, data C.gpointer) C.gboolean } return C.GDK_EVENT_STOP // keeps window alive } - -//export windowResizing -func windowResizing(wid *C.GtkWidget, r *C.GdkRectangle, data C.gpointer) { - w := (*window)(unsafe.Pointer(data)) - // the origin of the window's content area is always (0, 0) - w.resize(0, 0, int(r.width), int(r.height)) -}