|
| 1 | +// +build darwin |
| 2 | + |
| 3 | +package gotk3osx |
| 4 | + |
| 5 | +// #cgo pkg-config: gtk-mac-integration gio-2.0 glib-2.0 gobject-2.0 gtk+-3.0 gdk-3.0 |
| 6 | +// #include <gtk/gtk.h> |
| 7 | +// #include <gdk/gdk.h> |
| 8 | +// #include <glib-object.h> |
| 9 | +// #include <gtkosxapplication.h> |
| 10 | +// #include "gtkosx.go.h" |
| 11 | +import "C" |
| 12 | +import "github.com/gotk3/gotk3/glib" |
| 13 | +import "github.com/gotk3/gotk3/gdk" |
| 14 | +import "github.com/gotk3/gotk3/gtk" |
| 15 | +import "unsafe" |
| 16 | +import "errors" |
| 17 | + |
| 18 | +func init() { |
| 19 | + tm := []glib.TypeMarshaler{ |
| 20 | + {glib.Type(C.gtkosx_application_get_type()), marshalGtkosxApplication}, |
| 21 | + } |
| 22 | + glib.RegisterGValueMarshalers(tm) |
| 23 | +} |
| 24 | + |
| 25 | +func gbool(b bool) C.gboolean { |
| 26 | + if b { |
| 27 | + return C.gboolean(1) |
| 28 | + } |
| 29 | + return C.gboolean(0) |
| 30 | +} |
| 31 | + |
| 32 | +func gobool(b C.gboolean) bool { |
| 33 | + return b != C.FALSE |
| 34 | +} |
| 35 | + |
| 36 | +func stringReturn(c *C.gchar) string { |
| 37 | + if c == nil { |
| 38 | + return "" |
| 39 | + } |
| 40 | + return C.GoString((*C.char)(c)) |
| 41 | +} |
| 42 | + |
| 43 | +var nilPtrErr = errors.New("cgo returned unexpected nil pointer") |
| 44 | + |
| 45 | +// GtkosxApplication represents a GTK OSX application |
| 46 | +type GtkosxApplication struct { |
| 47 | + glib.InitiallyUnowned |
| 48 | +} |
| 49 | + |
| 50 | +// Signals available to connect to: |
| 51 | +// - "NSApplicationBlockTermination" |
| 52 | +// - "NSApplicationDidBecomeActive" |
| 53 | +// - "NSApplicationOpenFile" |
| 54 | +// - "NSApplicationWillResignActive" |
| 55 | +// - "NSApplicationWillTerminate" |
| 56 | + |
| 57 | +func (v *GtkosxApplication) native() *C.GtkosxApplication { |
| 58 | + if v == nil || v.GObject == nil { |
| 59 | + return nil |
| 60 | + } |
| 61 | + p := unsafe.Pointer(v.GObject) |
| 62 | + return C.toGtkosxApplication(p) |
| 63 | +} |
| 64 | + |
| 65 | +func marshalGtkosxApplication(p uintptr) (interface{}, error) { |
| 66 | + c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p))) |
| 67 | + obj := glib.Take(unsafe.Pointer(c)) |
| 68 | + return wrapGtkosxApplication(obj), nil |
| 69 | +} |
| 70 | + |
| 71 | +func wrapGtkosxApplication(obj *glib.Object) *GtkosxApplication { |
| 72 | + return &GtkosxApplication{glib.InitiallyUnowned{obj}} |
| 73 | +} |
| 74 | + |
| 75 | +// GetGtkosxApplication returns the singleton application object |
| 76 | +func GetGtkosxApplication() (*GtkosxApplication, error) { |
| 77 | + c := C.gtkosx_application_get() |
| 78 | + if c == nil { |
| 79 | + return nil, nilPtrErr |
| 80 | + } |
| 81 | + |
| 82 | + return wrapGtkosxApplication(glib.Take(unsafe.Pointer(c))), nil |
| 83 | +} |
| 84 | + |
| 85 | +// Ready informs Cocoa that application initialization is complete. |
| 86 | +func (v *GtkosxApplication) Ready() { |
| 87 | + C.gtkosx_application_ready(v.native()) |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +// SetUseQuartzAccelerators sets quartz accelerator handling; TRUE (default) uses quartz; FALSE uses Gtk+. Quartz accelerator handling is required for normal OS X accelerators (e.g., command-q to quit) to work. |
| 92 | +func (v *GtkosxApplication) SetUseQuartzAccelerators(v2 bool) { |
| 93 | + C.gtkosx_application_set_use_quartz_accelerators(v.native(), gbool(v2)) |
| 94 | +} |
| 95 | + |
| 96 | +// UseQuartzAccelerators returns whether we are using Quartz or Gtk+ accelerator handling? |
| 97 | +func (v *GtkosxApplication) UseQuartzAccelerators() bool { |
| 98 | + return gobool(C.gtkosx_application_use_quartz_accelerators(v.native())) |
| 99 | +} |
| 100 | + |
| 101 | +// SetMenuBar sets a window's menubar as the application menu bar. Call this once for each window as you create them. It works best if the menubar is reasonably fully populated before you call it. Once set, it will stay syncronized through signals as long as you don't disconnect or block them. |
| 102 | +func (v *GtkosxApplication) SetMenuBar(menuShell *gtk.MenuShell) { |
| 103 | + C.gtkosx_application_set_menu_bar(v.native(), nativeMenuShell(menuShell)) |
| 104 | +} |
| 105 | + |
| 106 | +// SyncMenuBar will syncronize the active window's GtkMenuBar with the OSX menu bar. You should only need this if you have programmatically changed the menus with signals blocked or disconnected. |
| 107 | +func (v *GtkosxApplication) SyncMenuBar() { |
| 108 | + C.gtkosx_application_sync_menubar(v.native()) |
| 109 | +} |
| 110 | + |
| 111 | +// InsertAppMenuItem will insert a menu item in the a app menu |
| 112 | +func (v *GtkosxApplication) InsertAppMenuItem(menuItem *gtk.Widget, index int) { |
| 113 | + C.gtkosx_application_insert_app_menu_item(v.native(), nativeWidget(menuItem), (C.gint)(index)) |
| 114 | +} |
| 115 | + |
| 116 | +// SetWindowMenu sets a designated menu item already on the menu bar as the Window menu. This is the menu which contains a list of open windows for the application; by default it also provides menu items to minimize and zoom the current window and to bring all windows to the front. Call this after gtk_osx_application_set_menu_bar(). It operates on the currently active menubar. If nenu_item is NULL, it will create a new menu for you, which will not be gettext translatable. |
| 117 | +func (v *GtkosxApplication) SetWindowMenu(menuItem *gtk.MenuItem) { |
| 118 | + C.gtkosx_application_set_window_menu(v.native(), nativeMenuItem(menuItem)) |
| 119 | +} |
| 120 | + |
| 121 | +// SetHelpMenu sets a designated menu item already on the menu bar as the Help menu. Call this after gtk_osx_application_set_menu_bar(), but before gtk_osx_application_window_menu(), especially if you're letting GtkosxApplication create a Window menu for you (it helps position the Window menu correctly). It operates on the currently active menubar. If nenu_item is NULL, it will create a new menu for you, which will not be gettext translatable. |
| 122 | +func (v *GtkosxApplication) SetHelpMenu(menuItem *gtk.MenuItem) { |
| 123 | + C.gtkosx_application_set_help_menu(v.native(), nativeMenuItem(menuItem)) |
| 124 | +} |
| 125 | + |
| 126 | +// SetDockMenu Set a GtkMenu as the dock menu. This menu does not have a "sync" function, so changes made while signals are disconnected will not update the menu which appears in the dock, and may produce strange results or crashes if a GtkMenuItem or GtkAction associated with a dock menu item is deallocated. |
| 127 | +func (v *GtkosxApplication) SetDockMenu(menuItem *gtk.MenuShell) { |
| 128 | + C.gtkosx_application_set_dock_menu(v.native(), nativeMenuShell(menuItem)) |
| 129 | +} |
| 130 | + |
| 131 | +// SetDockIconPixbuf sets the dock icon from a GdkPixbuf |
| 132 | +func (v *GtkosxApplication) SetDockIconPixbuf(pixbuf *gdk.Pixbuf) { |
| 133 | + C.gtkosx_application_set_dock_icon_pixbuf(v.native(), nativePixbuf(pixbuf)) |
| 134 | +} |
| 135 | + |
| 136 | +// SetDockIconResource sets the dock icon from an image file in the bundle/ |
| 137 | +func (v *GtkosxApplication) SetDockIconResource(name, tp, subdir string) { |
| 138 | + cstrname := C.CString(name) |
| 139 | + defer C.free(unsafe.Pointer(cstrname)) |
| 140 | + |
| 141 | + cstrtype := C.CString(tp) |
| 142 | + defer C.free(unsafe.Pointer(cstrtype)) |
| 143 | + |
| 144 | + cstrsubdir := C.CString(subdir) |
| 145 | + defer C.free(unsafe.Pointer(cstrsubdir)) |
| 146 | + |
| 147 | + C.gtkosx_application_set_dock_icon_resource(v.native(), cstrname, cstrtype, cstrsubdir) |
| 148 | +} |
| 149 | + |
| 150 | +type GtkosxApplicationAttentionType int |
| 151 | + |
| 152 | +const ( |
| 153 | + ATTENTION_TYPE_CRITICAL_REQUEST GtkosxApplicationAttentionType = C.CRITICAL_REQUEST |
| 154 | + ATTENTION_TYPE_INFO_REQUEST GtkosxApplicationAttentionType = C.INFO_REQUEST |
| 155 | +) |
| 156 | + |
| 157 | +// AttentionRequest creates an attention request. If type is CRITICAL_REQUEST, the dock icon will bounce until cancelled the application receives focus; otherwise it will bounce for 1 second -- but the attention request will remain asserted until cancelled or the application receives focus. This function has no effect if the application has focus. |
| 158 | +func (v *GtkosxApplication) AttentionRequest(tp GtkosxApplicationAttentionType) int { |
| 159 | + res := C.gtkosx_application_attention_request(v.native(), C.GtkosxApplicationAttentionType(tp)) |
| 160 | + return int(res) |
| 161 | +} |
| 162 | + |
| 163 | +// CancelAttentionRequest cancels an attention request created with gtkosx_application_attention_request. |
| 164 | +func (v *GtkosxApplication) CancelAttentionRequest(id int) { |
| 165 | + C.gtkosx_application_cancel_attention_request(v.native(), C.gint(id)) |
| 166 | +} |
| 167 | + |
| 168 | +// GetBundlePath returns the root path of the bundle or the directory containing the executable if it isn't actually a bundle. |
| 169 | +func GetBundlePath() string { |
| 170 | + return stringReturn(C.gtkosx_application_get_bundle_path()) |
| 171 | +} |
| 172 | + |
| 173 | +// GetResourcePath returns the Resource path for the bundle or the directory containing the executable if it isn't actually a bundle. Use gtkosx_application_get_bundle_id() to check (it will return NULL if it's not a bundle). |
| 174 | +func GetResourcePath() string { |
| 175 | + return stringReturn(C.gtkosx_application_get_resource_path()) |
| 176 | +} |
| 177 | + |
| 178 | +// GetExecutablePath returns the executable path, including file name |
| 179 | +func GetExecutablePath() string { |
| 180 | + return stringReturn(C.gtkosx_application_get_executable_path()) |
| 181 | +} |
| 182 | + |
| 183 | +// GetBundleID returns the value of the CFBundleIdentifier key from the bundle's Info.plist. This will return NULL if it's not really a bundle, there's no Info.plist, or if Info.plist doesn't have a CFBundleIdentifier key (So if you need to detect being in a bundle, make sure that your bundle has that key!) |
| 184 | +func GetBundleID() string { |
| 185 | + return stringReturn(C.gtkosx_application_get_bundle_id()) |
| 186 | +} |
| 187 | + |
| 188 | +// GetBundleInfo queries the bundle's Info.plist with key. If the returned object is a string, returns that; otherwise returns NULL. |
| 189 | +func GetBundleInfo(key string) string { |
| 190 | + cstr := C.CString(key) |
| 191 | + defer C.free(unsafe.Pointer(cstr)) |
| 192 | + return stringReturn(C.gtkosx_application_get_bundle_info(cstr)) |
| 193 | +} |
0 commit comments