From 09f2a34898585574868ee72fd426b1fac5c12471 Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Sat, 18 Jan 2014 21:17:05 -0500 Subject: [PATCH] Add gtk.OffscreenWindow implementation and bindings. Closes #33. --- cairo/cairo.go | 12 ++++++++++ gtk/gtk.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ gtk/gtk.go.h | 6 +++++ gtk/gtk_3_10.go | 2 +- 4 files changed, 82 insertions(+), 1 deletion(-) diff --git a/cairo/cairo.go b/cairo/cairo.go index 75fcd4e..251cf0c 100644 --- a/cairo/cairo.go +++ b/cairo/cairo.go @@ -572,6 +572,18 @@ func wrapSurface(surface *C.cairo_surface_t) *Surface { return &Surface{surface} } +// NewSurface creates a gotk3 cairo Surface from a pointer to a +// C cairo_surface_t. This is primarily designed for use with other +// gotk3 packages and should be avoided by applications. +func NewSurface(s *C.cairo_surface_t, needsRef bool) *Surface { + surface := wrapSurface(s) + if needsRef { + surface.reference() + } + runtime.SetFinalizer(surface, (*Surface).destroy) + return surface +} + // CreateSimilar is a wrapper around cairo_surface_create_similar(). func (v *Surface) CreateSimilar(content Content, width, height int) *Surface { c := C.cairo_surface_create_similar(v.Native(), diff --git a/gtk/gtk.go b/gtk/gtk.go index 1222e7c..68c1ab8 100644 --- a/gtk/gtk.go +++ b/gtk/gtk.go @@ -51,6 +51,7 @@ import "C" import ( "errors" "fmt" + "github.com/conformal/gotk3/cairo" "github.com/conformal/gotk3/gdk" "github.com/conformal/gotk3/glib" "runtime" @@ -3339,6 +3340,66 @@ func (v *Notebook) GetActionWidget(packType PackType) (*Widget, error) { return w, nil } +/* + * GtkOffscreenWindow + */ + +// OffscreenWindow is a representation of GTK's GtkOffscreenWindow. +type OffscreenWindow struct { + Window +} + +// Native returns a pointer to the underlying GtkOffscreenWindow. +func (v *OffscreenWindow) Native() *C.GtkOffscreenWindow { + if v == nil || v.GObject == nil { + return nil + } + p := unsafe.Pointer(v.GObject) + return C.toGtkOffscreenWindow(p) +} + +func wrapOffscreenWindow(obj *glib.Object) *OffscreenWindow { + return &OffscreenWindow{Window{Bin{Container{Widget + glib.InitiallyUnowned{obj}}}}}} +} + +// OffscreenWindowNew is a wrapper around gtk_offscreen_window_new(). +func OffscreenWindowNew() (*OffscreenWindow, error) { + c := C.gtk_offscreen_window_new() + if c == nil { + return nil, nilPtrErr + } + obj := &glib.Object{glib.ToGObject(unsafe.Pointer(c))} + o := wrapOffscreenWindow(obj) + obj.RefSink() + runtime.SetFinalizer(obj, (*glib.Object).Unref) + return o, nil +} + +// GetSurface is a wrapper around gtk_offscreen_window_get_surface(). +// The returned surface is safe to use over window resizes. +func (v *OffscreenWindow) GetSurface() (*cairo.Surface, error) { + c := C.gtk_offscreen_window_get_surface(v.Native()) + if c == nil { + return nil, nilPtrErr + } + s := cairo.NewSurface(c, true) + return s, nil +} + +// GetPixbuf is a wrapper around gtk_offscreen_window_get_pixbuf(). +func (v *OffscreenWindow) GetPixbuf() (*gdk.Pixbuf, error) { + c := C.gtk_offscreen_window_get_pixbuf(v.Native()) + if c == nil { + return nil, nilPtrErr + } + obj := &glib.Object{glib.ToGObject(unsafe.Pointer(c))} + pb := &gdk.Pixbuf{obj} + // Pixbuf is returned with ref count of 1, so don't increment. + runtime.SetFinalizer(obj, (*glib.Object).Unref) + return pb, nil +} + /* * GtkOrientable */ @@ -5510,6 +5571,8 @@ func cast(c *C.GObject) (glib.IObject, error) { g = wrapMisc(obj) case "GtkNotebook": g = wrapNotebook(obj) + case "GtkOffscreenWindow": + g = wrapOffscreenWindow(obj) case "GtkOrientable": g = wrapOrientable(obj) case "GtkProgressBar": diff --git a/gtk/gtk.go.h b/gtk/gtk.go.h index 77c7029..1c9b9b4 100644 --- a/gtk/gtk.go.h +++ b/gtk/gtk.go.h @@ -314,6 +314,12 @@ toGtkSearchEntry(void *p) return (GTK_SEARCH_ENTRY(p)); } +static GtkOffscreenWindow * +toGtkOffscreenWindow(void *p) +{ + return (GTK_OFFSCREEN_WINDOW(p)); +} + static GType * alloc_types(int n) { return ((GType *)g_new0(GType, n)); diff --git a/gtk/gtk_3_10.go b/gtk/gtk_3_10.go index 2002775..14e0510 100644 --- a/gtk/gtk_3_10.go +++ b/gtk/gtk_3_10.go @@ -39,7 +39,7 @@ import ( */ const ( - ALIGN_BASELINE Align = C.GTK_ALIGN_BASELINE + ALIGN_BASELINE Align = C.GTK_ALIGN_BASELINE ) /*