diff --git a/gdk/gdk.go b/gdk/gdk.go index 6a16373..1dc4d45 100644 --- a/gdk/gdk.go +++ b/gdk/gdk.go @@ -20,6 +20,10 @@ package gdk // #cgo pkg-config: gdk-3.0 // #include // #include "gdk.go.h" +// +// GdkEventType get_event_type(GdkEvent *e) { +// return e->type; +// } import "C" import ( "errors" @@ -161,6 +165,14 @@ func (v Atom) native() C.GdkAtom { return C.toGdkAtom(unsafe.Pointer(uintptr(v))) } +// GdkAtomIntern is a wrapper around gdk_atom_intern +func GdkAtomIntern(atomName string, onlyIfExists bool) Atom { + cstr := C.CString(atomName) + defer C.free(unsafe.Pointer(cstr)) + c := C.gdk_atom_intern((*C.gchar)(cstr), gbool(onlyIfExists)) + return Atom(uintptr(unsafe.Pointer(c))) +} + /* * GdkDevice */ @@ -509,6 +521,12 @@ func (v *Event) native() *C.GdkEvent { return v.GdkEvent } +func (v *Event) Type() uint { + // lol hack to access type without keyword clashes + c := C.get_event_type(v.native()) + return uint(c) +} + // Native returns a pointer to the underlying GdkEvent. func (v *Event) Native() uintptr { return uintptr(unsafe.Pointer(v.native())) @@ -546,6 +564,36 @@ func (v *EventKey) KeyVal() uint { return uint(c) } +/* + * GdkEventButton + */ + +const ( + BUTTON_PRESS = C.GDK_BUTTON_PRESS + TWO_BUTTON_PRESS = C.GDK_2BUTTON_PRESS + THREE_BUTTON_PRESS = C.GDK_3BUTTON_PRESS + BUTTON_RELEASE = C.GDK_BUTTON_RELEASE +) + +// EventButton is a representation of GDK's GdkEventButton. +type EventButton struct { + *Event +} + +// Native returns a pointer to the underlying GdkEventButton. +func (v *EventButton) Native() uintptr { + return uintptr(unsafe.Pointer(v.native())) +} + +func (v *EventButton) native() *C.GdkEventButton { + return (*C.GdkEventButton)(unsafe.Pointer(v.Event.native())) +} + +func (v *EventButton) Button() uint { + c := v.native().button + return uint(c) +} + /* * GdkPixbuf */ diff --git a/gtk/gtk.go b/gtk/gtk.go index 53051a5..8ffdd26 100644 --- a/gtk/gtk.go +++ b/gtk/gtk.go @@ -51,6 +51,7 @@ import "C" import ( "errors" "fmt" + "reflect" "runtime" "unsafe" @@ -97,6 +98,7 @@ func init() { {glib.Type(C.gtk_wrap_mode_get_type()), marshalWrapMode}, // Objects/Interfaces + {glib.Type(C.gtk_selection_data_get_type()), marshalSelectionData}, {glib.Type(C.gtk_about_dialog_get_type()), marshalAboutDialog}, {glib.Type(C.gtk_adjustment_get_type()), marshalAdjustment}, {glib.Type(C.gtk_alignment_get_type()), marshalAlignment}, @@ -760,6 +762,11 @@ func Init(args *[]string) { } } +// MainIterationDo is a wrapper around gtk_main_iteration_do +func MainIterationDo(blocking bool) bool { + return gobool(C.gtk_main_iteration_do(gbool(blocking))) +} + // Main() is a wrapper around gtk_main() and runs the GTK main loop, // blocking until MainQuit() is called. func Main() { @@ -772,6 +779,47 @@ func MainQuit() { C.gtk_main_quit() } +/* + * GtkSelectionData + */ +type SelectionData struct { + GtkSelectionData *C.GtkSelectionData +} + +func marshalSelectionData(p uintptr) (interface{}, error) { + c := C.g_value_get_boxed((*C.GValue)(unsafe.Pointer(p))) + return (*SelectionData)(unsafe.Pointer(c)), nil +} + +// native returns a pointer to the underlying GtkSelectionData. +func (v *SelectionData) native() *C.GtkSelectionData { + if v == nil { + return nil + } + return v.GtkSelectionData +} + +// GetLength is a wrapper around gtk_selection_data_get_length +func (v *SelectionData) GetLength() int { + return int(C.gtk_selection_data_get_length(v.native())) +} + +// GetData is a wrapper around gtk_selection_data_get_data_with_length. +// It returns a slice of the correct size with the selection's data. +func (v *SelectionData) GetData() (data []byte) { + var length C.gint + c := C.gtk_selection_data_get_data_with_length(v.native(), &length) + sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&data)) + sliceHeader.Data = uintptr(unsafe.Pointer(c)) + sliceHeader.Len = int(length) + sliceHeader.Cap = int(length) + return +} + +func (v *SelectionData) free() { + C.gtk_selection_data_free(v.native()) +} + /* * GtkAboutDialog */ @@ -2228,6 +2276,17 @@ func (v *Clipboard) native() *C.GtkClipboard { return C.toGtkClipboard(p) } +// WaitForContents is a wrapper around gtk_clipboard_wait_for_contents +func (v *Clipboard) WaitForContents(target gdk.Atom) (*SelectionData, error) { + c := C.gtk_clipboard_wait_for_contents(v.native(), C.GdkAtom(unsafe.Pointer(target))) + if c == nil { + return nil, nilPtrErr + } + p := &SelectionData{c} + runtime.SetFinalizer(p, (*SelectionData).free) + return p, nil +} + // WaitForText is a wrapper around gtk_clipboard_wait_for_text func (v *Clipboard) WaitForText() (string, error) { c := C.gtk_clipboard_wait_for_text(v.native()) @@ -2238,6 +2297,24 @@ func (v *Clipboard) WaitForText() (string, error) { return C.GoString((*C.char)(c)), nil } +// WaitForImage is a wrapper around gtk_clipboard_wait_for_image +func (v *Clipboard) WaitForImage() (*gdk.Pixbuf, error) { + c := C.gtk_clipboard_wait_for_image(v.native()) + if c == nil { + return nil, nilPtrErr + } + obj := &glib.Object{glib.ToGObject(unsafe.Pointer(c))} + p := &gdk.Pixbuf{obj} + obj.Ref() + runtime.SetFinalizer(obj, (*glib.Object).Unref) + return p, nil +} + +// Store is a wrapper around gtk_clipboard_store +func (v *Clipboard) Store() { + C.gtk_clipboard_store(v.native()) +} + func marshalClipboard(p uintptr) (interface{}, error) { c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p))) obj := &glib.Object{glib.ToGObject(unsafe.Pointer(c))} @@ -2284,6 +2361,11 @@ func (v *Clipboard) SetText(text string) { C.gint(len(text))) } +// SetImage is a wrapper around gtk_clipboard_set_image +func (v *Clipboard) SetImage(pixbuf *gdk.Pixbuf) { + C.gtk_clipboard_set_image(v.native(), (*C.GdkPixbuf)(unsafe.Pointer(pixbuf.Native()))) +} + /* * GtkComboBox */