From 6c081cf31ec3c7638c5cd006ffcd381c4f9094c5 Mon Sep 17 00:00:00 2001 From: Shish Date: Sat, 6 Sep 2014 12:20:35 +0100 Subject: [PATCH 1/3] Wrappers for GtkFileChooser / GtkFileFilter --- gtk/gtk.go | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++ gtk/gtk.go.h | 40 +++++++++++++++ 2 files changed, 176 insertions(+) diff --git a/gtk/gtk.go b/gtk/gtk.go index 5d7177f..f7e4897 100644 --- a/gtk/gtk.go +++ b/gtk/gtk.go @@ -125,6 +125,7 @@ func init() { {glib.Type(C.gtk_event_box_get_type()), marshalEventBox}, {glib.Type(C.gtk_file_chooser_get_type()), marshalFileChooser}, {glib.Type(C.gtk_file_chooser_button_get_type()), marshalFileChooserButton}, + {glib.Type(C.gtk_file_chooser_dialog_get_type()), marshalFileChooserDialog}, {glib.Type(C.gtk_file_chooser_widget_get_type()), marshalFileChooserWidget}, {glib.Type(C.gtk_frame_get_type()), marshalFrame}, {glib.Type(C.gtk_grid_get_type()), marshalGrid}, @@ -3591,6 +3592,11 @@ func (v *FileChooser) GetFilename() string { return s } +// AddFilter is a wrapper around gtk_file_chooser_add_filter(). +func (v *FileChooser) AddFilter(filter *FileFilter) { + C.gtk_file_chooser_add_filter(v.native(), filter.native()) +} + /* * GtkFileChooserButton */ @@ -3639,6 +3645,81 @@ func FileChooserButtonNew(title string, action FileChooserAction) (*FileChooserB return f, nil } +/* + * GtkFileChooserDialog + */ + +// FileChooserDialog is a representation of GTK's GtkFileChooserDialog. +type FileChooserDialog struct { + Dialog + + // Interfaces + FileChooser +} + +// native returns a pointer to the underlying GtkFileChooserDialog. +func (v *FileChooserDialog) native() *C.GtkFileChooserDialog { + if v == nil || v.GObject == nil { + return nil + } + p := unsafe.Pointer(v.GObject) + return C.toGtkFileChooserDialog(p) +} + +func marshalFileChooserDialog(p uintptr) (interface{}, error) { + c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p))) + obj := &glib.Object{glib.ToGObject(unsafe.Pointer(c))} + return wrapFileChooserDialog(obj), nil +} + +func wrapFileChooserDialog(obj *glib.Object) *FileChooserDialog { + fc := wrapFileChooser(obj) + return &FileChooserDialog{Dialog{Window{Bin{Container{Widget{glib.InitiallyUnowned{obj}}}}}}, *fc} +} + +// FileChooserDialogNew is a wrapper around gtk_file_chooser_dialog_new(). +func FileChooserDialogNew1( + title string, + parent *Window, + action FileChooserAction, + first_button_text string, + first_button_id ResponseType) (*FileChooserDialog, error) { + c := C.gtk_file_chooser_dialog_new_1( + (*C.gchar)(C.CString(title)), parent.native(), C.GtkFileChooserAction(action), + (*C.gchar)(C.CString(first_button_text)), C.int(first_button_id)) + if c == nil { + return nil, nilPtrErr + } + obj := &glib.Object{glib.ToGObject(unsafe.Pointer(c))} + a := wrapFileChooserDialog(obj) + obj.RefSink() + runtime.SetFinalizer(obj, (*glib.Object).Unref) + return a, nil +} + +// FileChooserDialogNew is a wrapper around gtk_file_chooser_dialog_new(). +func FileChooserDialogNew2( + title string, + parent *Window, + action FileChooserAction, + first_button_text string, + first_button_id ResponseType, + second_button_text string, + second_button_id ResponseType) (*FileChooserDialog, error) { + c := C.gtk_file_chooser_dialog_new_2( + (*C.gchar)(C.CString(title)), parent.native(), C.GtkFileChooserAction(action), + (*C.gchar)(C.CString(first_button_text)), C.int(first_button_id), + (*C.gchar)(C.CString(second_button_text)), C.int(second_button_id)) + if c == nil { + return nil, nilPtrErr + } + obj := &glib.Object{glib.ToGObject(unsafe.Pointer(c))} + a := wrapFileChooserDialog(obj) + obj.RefSink() + runtime.SetFinalizer(obj, (*glib.Object).Unref) + return a, nil +} + /* * GtkFileChooserWidget */ @@ -3684,6 +3765,61 @@ func FileChooserWidgetNew(action FileChooserAction) (*FileChooserWidget, error) return f, nil } +/* + * GtkFileFilter + */ + +// FileChoser is a representation of GTK's GtkFileFilter GInterface. +type FileFilter struct { + *glib.Object +} + +// native returns a pointer to the underlying GObject as a GtkFileFilter. +func (v *FileFilter) native() *C.GtkFileFilter { + if v == nil || v.GObject == nil { + return nil + } + p := unsafe.Pointer(v.GObject) + return C.toGtkFileFilter(p) +} + +func marshalFileFilter(p uintptr) (interface{}, error) { + c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p))) + obj := &glib.Object{glib.ToGObject(unsafe.Pointer(c))} + return wrapFileFilter(obj), nil +} + +func wrapFileFilter(obj *glib.Object) *FileFilter { + return &FileFilter{obj} +} + +// FileFilterNew is a wrapper around gtk_file_filter_new(). +func FileFilterNew() (*FileFilter, error) { + c := C.gtk_file_filter_new() + if c == nil { + return nil, nilPtrErr + } + obj := &glib.Object{glib.ToGObject(unsafe.Pointer(c))} + f := wrapFileFilter(obj) + obj.RefSink() + runtime.SetFinalizer(obj, (*glib.Object).Unref) + return f, nil +} + +// SetName is a wrapper around gtk_file_filter_set_name(). +func (v *FileFilter) SetName(name string) { + cstr := C.CString(name) + defer C.free(unsafe.Pointer(cstr)) + C.gtk_file_filter_set_name(v.native(), (*C.gchar)(cstr)) +} + +// AddPattern is a wrapper around gtk_file_filter_add_pattern(). +func (v *FileFilter) AddPattern(pattern string) { + cstr := C.CString(pattern) + defer C.free(unsafe.Pointer(cstr)) + C.gtk_file_filter_add_pattern(v.native(), (*C.gchar)(cstr)) +} + /* * GtkFrame */ diff --git a/gtk/gtk.go.h b/gtk/gtk.go.h index 3e86344..6b88c15 100644 --- a/gtk/gtk.go.h +++ b/gtk/gtk.go.h @@ -392,12 +392,24 @@ toGtkFileChooserButton(void *p) return (GTK_FILE_CHOOSER_BUTTON(p)); } +static GtkFileChooserDialog * +toGtkFileChooserDialog(void *p) +{ + return (GTK_FILE_CHOOSER_DIALOG(p)); +} + static GtkFileChooserWidget * toGtkFileChooserWidget(void *p) { return (GTK_FILE_CHOOSER_WIDGET(p)); } +static GtkFileFilter * +toGtkFileFilter(void *p) +{ + return (GTK_FILE_FILTER(p)); +} + static GtkMenuButton * toGtkMenuButton(void *p) { @@ -514,3 +526,31 @@ object_get_class_name(GObject *object) { return G_OBJECT_CLASS_NAME(G_OBJECT_GET_CLASS(object)); } + +GtkWidget * +gtk_file_chooser_dialog_new_1( + const gchar *title, + GtkWindow *parent, + GtkFileChooserAction action, + const gchar *first_button_text, int first_button_id +) { + return gtk_file_chooser_dialog_new( + title, parent, action, + first_button_text, first_button_id, + NULL); +} + +GtkWidget * +gtk_file_chooser_dialog_new_2( + const gchar *title, + GtkWindow *parent, + GtkFileChooserAction action, + const gchar *first_button_text, int first_button_id, + const gchar *second_button_text, int second_button_id +) { + return gtk_file_chooser_dialog_new( + title, parent, action, + first_button_text, first_button_id, + second_button_text, second_button_id, + NULL); +} From a49e750b9479c93d80ede7163d1b243024dd5622 Mon Sep 17 00:00:00 2001 From: Shish Date: Sat, 6 Sep 2014 12:28:58 +0100 Subject: [PATCH 2/3] Avoid memory leaks in FileChooserDialogNew1/2, and improve docs a litte --- gtk/gtk.go | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/gtk/gtk.go b/gtk/gtk.go index f7e4897..bf5f146 100644 --- a/gtk/gtk.go +++ b/gtk/gtk.go @@ -3677,16 +3677,20 @@ func wrapFileChooserDialog(obj *glib.Object) *FileChooserDialog { return &FileChooserDialog{Dialog{Window{Bin{Container{Widget{glib.InitiallyUnowned{obj}}}}}}, *fc} } -// FileChooserDialogNew is a wrapper around gtk_file_chooser_dialog_new(). +// FileChooserDialogNew1 is a wrapper around gtk_file_chooser_dialog_new() with one button. func FileChooserDialogNew1( title string, parent *Window, action FileChooserAction, first_button_text string, first_button_id ResponseType) (*FileChooserDialog, error) { + c_title := C.CString(title) + defer C.free(unsafe.Pointer(c_title)) + c_first_button_text := C.CString(first_button_text) + defer C.free(unsafe.Pointer(c_first_button_text)) c := C.gtk_file_chooser_dialog_new_1( - (*C.gchar)(C.CString(title)), parent.native(), C.GtkFileChooserAction(action), - (*C.gchar)(C.CString(first_button_text)), C.int(first_button_id)) + (*C.gchar)(c_title), parent.native(), C.GtkFileChooserAction(action), + (*C.gchar)(c_first_button_text), C.int(first_button_id)) if c == nil { return nil, nilPtrErr } @@ -3697,7 +3701,7 @@ func FileChooserDialogNew1( return a, nil } -// FileChooserDialogNew is a wrapper around gtk_file_chooser_dialog_new(). +// FileChooserDialogNew2 is a wrapper around gtk_file_chooser_dialog_new() with two buttons. func FileChooserDialogNew2( title string, parent *Window, @@ -3706,10 +3710,16 @@ func FileChooserDialogNew2( first_button_id ResponseType, second_button_text string, second_button_id ResponseType) (*FileChooserDialog, error) { + c_title := C.CString(title) + defer C.free(unsafe.Pointer(c_title)) + c_first_button_text := C.CString(first_button_text) + defer C.free(unsafe.Pointer(c_first_button_text)) + c_second_button_text := C.CString(second_button_text) + defer C.free(unsafe.Pointer(c_second_button_text)) c := C.gtk_file_chooser_dialog_new_2( - (*C.gchar)(C.CString(title)), parent.native(), C.GtkFileChooserAction(action), - (*C.gchar)(C.CString(first_button_text)), C.int(first_button_id), - (*C.gchar)(C.CString(second_button_text)), C.int(second_button_id)) + (*C.gchar)(c_title), parent.native(), C.GtkFileChooserAction(action), + (*C.gchar)(c_first_button_text), C.int(first_button_id), + (*C.gchar)(c_second_button_text), C.int(second_button_id)) if c == nil { return nil, nilPtrErr } From 39d075863024d314c6c36ec93cb86d087907ae81 Mon Sep 17 00:00:00 2001 From: Shish Date: Wed, 10 Sep 2014 23:28:20 +0100 Subject: [PATCH 3/3] rename FileChooserDialogNew[12] -> FileChooserDialogNewWith[12]Button(s?) --- gtk/gtk.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gtk/gtk.go b/gtk/gtk.go index bf5f146..1c6dd56 100644 --- a/gtk/gtk.go +++ b/gtk/gtk.go @@ -3677,8 +3677,8 @@ func wrapFileChooserDialog(obj *glib.Object) *FileChooserDialog { return &FileChooserDialog{Dialog{Window{Bin{Container{Widget{glib.InitiallyUnowned{obj}}}}}}, *fc} } -// FileChooserDialogNew1 is a wrapper around gtk_file_chooser_dialog_new() with one button. -func FileChooserDialogNew1( +// FileChooserDialogNewWith1Button is a wrapper around gtk_file_chooser_dialog_new() with one button. +func FileChooserDialogNewWith1Button( title string, parent *Window, action FileChooserAction, @@ -3701,8 +3701,8 @@ func FileChooserDialogNew1( return a, nil } -// FileChooserDialogNew2 is a wrapper around gtk_file_chooser_dialog_new() with two buttons. -func FileChooserDialogNew2( +// FileChooserDialogNewWith2Buttons is a wrapper around gtk_file_chooser_dialog_new() with two buttons. +func FileChooserDialogNewWith2Buttons( title string, parent *Window, action FileChooserAction,