Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

Wrapper for GtkFileChooserDialog #80

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions gtk/gtk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -3639,6 +3645,91 @@ 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}
}

// FileChooserDialogNew1 is a wrapper around gtk_file_chooser_dialog_new() with one button.
func FileChooserDialogNew1(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep consistency with other multiple-constructor functions, this should be named something like FileChooserDialogNewWith1Button(). Looks great otherwise!

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_title), parent.native(), C.GtkFileChooserAction(action),
(*C.gchar)(c_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
}

// FileChooserDialogNew2 is a wrapper around gtk_file_chooser_dialog_new() with two buttons.
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_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_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
}
obj := &glib.Object{glib.ToGObject(unsafe.Pointer(c))}
a := wrapFileChooserDialog(obj)
obj.RefSink()
runtime.SetFinalizer(obj, (*glib.Object).Unref)
return a, nil
}

/*
* GtkFileChooserWidget
*/
Expand Down Expand Up @@ -3684,6 +3775,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
*/
Expand Down
40 changes: 40 additions & 0 deletions gtk/gtk.go.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is their some weird CGo thing that requires you to wrap the ...dialog_new() function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cgo can't handle C's varargs functions, so you need to make a non-varargs C function for each combination of args you want to send :(

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);
}