Skip to content

Commit

Permalink
Merge pull request #78 from twstrike/application_menu_methods
Browse files Browse the repository at this point in the history
Add support for most GtkApplication methods - especially those that r…
  • Loading branch information
olabini committed Feb 2, 2016
2 parents aa294e8 + a16c250 commit 2eac414
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 14 deletions.
4 changes: 4 additions & 0 deletions glib/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func (v *MenuModel) native() *C.GMenuModel {
return C.toGMenuModel(unsafe.Pointer(v.GObject))
}

func (v *MenuModel) Native() uintptr {
return uintptr(unsafe.Pointer(v.native()))
}

func marshalMenuModel(p uintptr) (interface{}, error) {
c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p)))
return wrapMenuModel(wrapObject(unsafe.Pointer(c))), nil
Expand Down
70 changes: 56 additions & 14 deletions gtk/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ import (
"github.com/gotk3/gotk3/glib"
)

// ApplicationInhibitFlags is a representation of GTK's GtkApplicationInhibitFlags.
type ApplicationInhibitFlags int

const (
APPLICATION_INHIBIT_LOGOUT ApplicationInhibitFlags = C.GTK_APPLICATION_INHIBIT_LOGOUT
APPLICATION_INHIBIT_SWITCH ApplicationInhibitFlags = C.GTK_APPLICATION_INHIBIT_SWITCH
APPLICATION_INHIBIT_SUSPEND ApplicationInhibitFlags = C.GTK_APPLICATION_INHIBIT_SUSPEND
APPLICATION_INHIBIT_IDLE ApplicationInhibitFlags = C.GTK_APPLICATION_INHIBIT_IDLE
)

/*
* GtkApplication
*/
Expand Down Expand Up @@ -78,17 +88,49 @@ func (v *Application) Uninhibit(cookie uint) {
C.gtk_application_uninhibit(v.native(), C.guint(cookie))
}

// GMenuModel * gtk_application_get_app_menu ()
// void gtk_application_set_app_menu ()
// GMenuModel * gtk_application_get_menubar ()
// void gtk_application_set_menubar ()
// GMenu * gtk_application_get_menu_by_id ()
// void gtk_application_add_accelerator ()
// void gtk_application_remove_accelerator ()
// gboolean gtk_application_is_inhibited ()
// guint gtk_application_inhibit ()
// GList * gtk_application_get_windows ()
// gchar ** gtk_application_list_action_descriptions ()
// gchar ** gtk_application_get_accels_for_action ()
// void gtk_application_set_accels_for_action ()
// gchar ** gtk_application_get_actions_for_accel ()
// GetAppMenu is a wrapper around gtk_application_get_app_menu().
func (v *Application) GetAppMenu() *glib.MenuModel {
c := C.gtk_application_get_app_menu(v.native())
if c == nil {
return nil
}
return &glib.MenuModel{wrapObject(unsafe.Pointer(c))}
}

// SetAppMenu is a wrapper around gtk_application_set_app_menu().
func (v *Application) SetAppMenu(m *glib.MenuModel) {
mptr := (*C.GMenuModel)(unsafe.Pointer(m.Native()))
C.gtk_application_set_app_menu(v.native(), mptr)
}

// GetMenubar is a wrapper around gtk_application_get_menubar().
func (v *Application) GetMenubar() *glib.MenuModel {
c := C.gtk_application_get_menubar(v.native())
if c == nil {
return nil
}
return &glib.MenuModel{wrapObject(unsafe.Pointer(c))}
}

// SetMenubar is a wrapper around gtk_application_set_menubar().
func (v *Application) SetMenubar(m *glib.MenuModel) {
mptr := (*C.GMenuModel)(unsafe.Pointer(m.Native()))
C.gtk_application_set_menubar(v.native(), mptr)
}

// IsInhibited is a wrapper around gtk_application_is_inhibited().
func (v *Application) IsInhibited(flags ApplicationInhibitFlags) bool {
return gobool(C.gtk_application_is_inhibited(v.native(), C.GtkApplicationInhibitFlags(flags)))
}

// Inhibit is a wrapper around gtk_application_inhibit().
func (v *Application) Inhibited(w *Window, flags ApplicationInhibitFlags, reason string) uint {
cstr1 := (*C.gchar)(C.CString(reason))
defer C.free(unsafe.Pointer(cstr1))

return uint(C.gtk_application_inhibit(v.native(), w.native(), C.GtkApplicationInhibitFlags(flags), cstr1))
}

// void gtk_application_add_accelerator () // deprecated and uses a gvariant paramater
// void gtk_application_remove_accelerator () // deprecated and uses a gvariant paramater
// GList * gtk_application_get_windows () // glist
62 changes: 62 additions & 0 deletions gtk/application_since_3_12.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// +build !gtk_3_6,!gtk_3_8,!gtk_3_10

// See: https://developer.gnome.org/gtk3/3.12/api-index-3-12.html

package gtk

// #include <gtk/gtk.h>
// #include "gtk.go.h"
import "C"
import "unsafe"

// GetAccelsForAction is a wrapper around gtk_application_get_accels_for_action().
func (v *Application) GetAccelsForAction(act string) []string {
cstr1 := (*C.gchar)(C.CString(act))
defer C.free(unsafe.Pointer(cstr1))

var descs []string
c := C.gtk_application_get_accels_for_action(v.native(), cstr1)
originalc := c
defer C.g_strfreev(originalc)

for *c != nil {
descs = append(descs, C.GoString((*C.char)(*c)))
c = C.next_gcharptr(c)
}

return descs
}

// SetAccelsForAction is a wrapper around gtk_application_set_accels_for_action().
func (v *Application) SetAccelsForAction(act string, accels []string) {
cstr1 := (*C.gchar)(C.CString(act))
defer C.free(unsafe.Pointer(cstr1))

caccels := C.make_strings(C.int(len(accels) + 1))
defer C.destroy_strings(caccels)

for i, accel := range accels {
cstr := C.CString(accel)
defer C.free(unsafe.Pointer(cstr))
C.set_string(caccels, C.int(i), (*C.gchar)(cstr))
}

C.set_string(caccels, C.int(len(accels)), nil)

C.gtk_application_set_accels_for_action(v.native(), cstr1, caccels)
}

// ListActionDescriptions is a wrapper around gtk_application_list_action_descriptions().
func (v *Application) ListActionDescriptions() []string {
var descs []string
c := C.gtk_application_list_action_descriptions(v.native())
originalc := c
defer C.g_strfreev(originalc)

for *c != nil {
descs = append(descs, C.GoString((*C.char)(*c)))
c = C.next_gcharptr(c)
}

return descs
}
35 changes: 35 additions & 0 deletions gtk/application_since_3_14.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,43 @@ package gtk
// #include <gtk/gtk.h>
// #include "gtk.go.h"
import "C"
import (
"unsafe"

"github.com/gotk3/gotk3/glib"
)

// PrefersAppMenu is a wrapper around gtk_application_prefers_app_menu().
func (v *Application) PrefersAppMenu() bool {
return gobool(C.gtk_application_prefers_app_menu(v.native()))
}

// GetActionsForAccel is a wrapper around gtk_application_get_actions_for_accel().
func (v *Application) GetActionsForAccel(acc string) []string {
cstr1 := (*C.gchar)(C.CString(acc))
defer C.free(unsafe.Pointer(cstr1))

var acts []string
c := C.gtk_application_get_actions_for_accel(v.native(), cstr1)
originalc := c
defer C.g_strfreev(originalc)

for *c != nil {
acts = append(acts, C.GoString((*C.char)(*c)))
c = C.next_gcharptr(c)
}

return acts
}

// GetMenuByID is a wrapper around gtk_application_get_menu_by_id().
func (v *Application) GetMenuByID(id string) *glib.Menu {
cstr1 := (*C.gchar)(C.CString(id))
defer C.free(unsafe.Pointer(cstr1))

c := C.gtk_application_get_menu_by_id(v.native(), cstr1)
if c == nil {
return nil
}
return &glib.Menu{glib.MenuModel{wrapObject(unsafe.Pointer(c))}}
}

0 comments on commit 2eac414

Please sign in to comment.