1package glib 2 3// #include <gio/gio.h> 4// #include <glib.h> 5// #include <glib-object.h> 6// #include "glib.go.h" 7import "C" 8import "unsafe" 9 10// Predefined attribute names for GMenu 11var ( 12 MENU_ATTRIBUTE_ACTION string = C.G_MENU_ATTRIBUTE_ACTION 13 MENU_ATTRIBUTE_ACTION_NAMESPACE string = C.G_MENU_ATTRIBUTE_ACTION_NAMESPACE 14 MENU_ATTRIBUTE_TARGET string = C.G_MENU_ATTRIBUTE_TARGET 15 MENU_ATTRIBUTE_LABEL string = C.G_MENU_ATTRIBUTE_LABEL 16 MENU_ATTRIBUTE_ICON string = C.G_MENU_ATTRIBUTE_ICON 17) 18 19// Predefined link names for GMenu 20var ( 21 MENU_LINK_SECTION string = C.G_MENU_LINK_SECTION 22 MENU_LINK_SUBMENU string = C.G_MENU_LINK_SUBMENU 23) 24 25// MenuModel is a representation of GMenuModel. 26type MenuModel struct { 27 *Object 28} 29 30// native returns a pointer to the underlying GMenuModel. 31func (v *MenuModel) native() *C.GMenuModel { 32 if v == nil || v.GObject == nil { 33 return nil 34 } 35 return C.toGMenuModel(unsafe.Pointer(v.GObject)) 36} 37 38// Native returns a pointer to the underlying GMenuModel. 39func (v *MenuModel) Native() uintptr { 40 return uintptr(unsafe.Pointer(v.native())) 41} 42 43func marshalMenuModel(p uintptr) (interface{}, error) { 44 c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p))) 45 return wrapMenuModel(wrapObject(unsafe.Pointer(c))), nil 46} 47 48func wrapMenuModel(obj *Object) *MenuModel { 49 return &MenuModel{obj} 50} 51 52// IsMutable is a wrapper around g_menu_model_is_mutable(). 53func (v *MenuModel) IsMutable() bool { 54 return gobool(C.g_menu_model_is_mutable(v.native())) 55} 56 57// GetNItems is a wrapper around g_menu_model_get_n_items(). 58func (v *MenuModel) GetNItems() int { 59 return int(C.g_menu_model_get_n_items(v.native())) 60} 61 62// GetItemLink is a wrapper around g_menu_model_get_item_link(). 63func (v *MenuModel) GetItemLink(index int, link string) *MenuModel { 64 cstr := (*C.gchar)(C.CString(link)) 65 defer C.free(unsafe.Pointer(cstr)) 66 c := C.g_menu_model_get_item_link(v.native(), C.gint(index), cstr) 67 if c == nil { 68 return nil 69 } 70 return wrapMenuModel(wrapObject(unsafe.Pointer(c))) 71} 72 73// ItemsChanged is a wrapper around g_menu_model_items_changed(). 74func (v *MenuModel) ItemsChanged(position, removed, added int) { 75 C.g_menu_model_items_changed(v.native(), C.gint(position), C.gint(removed), C.gint(added)) 76} 77 78// GVariant * g_menu_model_get_item_attribute_value () 79// gboolean g_menu_model_get_item_attribute () 80// GMenuAttributeIter * g_menu_model_iterate_item_attributes () 81// GMenuLinkIter * g_menu_model_iterate_item_links () 82 83// Menu is a representation of GMenu. 84type Menu struct { 85 MenuModel 86} 87 88// native() returns a pointer to the underlying GMenu. 89func (v *Menu) native() *C.GMenu { 90 if v == nil || v.GObject == nil { 91 return nil 92 } 93 p := unsafe.Pointer(v.GObject) 94 return C.toGMenu(p) 95} 96 97func marshalMenu(p uintptr) (interface{}, error) { 98 c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p))) 99 return wrapMenu(wrapObject(unsafe.Pointer(c))), nil 100} 101 102func wrapMenu(obj *Object) *Menu { 103 return &Menu{MenuModel{obj}} 104} 105 106// MenuNew is a wrapper around g_menu_new(). 107func MenuNew() *Menu { 108 c := C.g_menu_new() 109 if c == nil { 110 return nil 111 } 112 return wrapMenu(wrapObject(unsafe.Pointer(c))) 113} 114 115// Freeze is a wrapper around g_menu_freeze(). 116func (v *Menu) Freeze() { 117 C.g_menu_freeze(v.native()) 118} 119 120// Insert is a wrapper around g_menu_insert(). 121func (v *Menu) Insert(position int, label, detailedAction string) { 122 cstr1 := (*C.gchar)(C.CString(label)) 123 defer C.free(unsafe.Pointer(cstr1)) 124 125 cstr2 := (*C.gchar)(C.CString(detailedAction)) 126 defer C.free(unsafe.Pointer(cstr2)) 127 128 C.g_menu_insert(v.native(), C.gint(position), cstr1, cstr2) 129} 130 131// Prepend is a wrapper around g_menu_prepend(). 132func (v *Menu) Prepend(label, detailedAction string) { 133 cstr1 := (*C.gchar)(C.CString(label)) 134 defer C.free(unsafe.Pointer(cstr1)) 135 136 cstr2 := (*C.gchar)(C.CString(detailedAction)) 137 defer C.free(unsafe.Pointer(cstr2)) 138 139 C.g_menu_prepend(v.native(), cstr1, cstr2) 140} 141 142// Append is a wrapper around g_menu_append(). 143func (v *Menu) Append(label, detailedAction string) { 144 cstr1 := (*C.gchar)(C.CString(label)) 145 defer C.free(unsafe.Pointer(cstr1)) 146 147 cstr2 := (*C.gchar)(C.CString(detailedAction)) 148 defer C.free(unsafe.Pointer(cstr2)) 149 150 C.g_menu_append(v.native(), cstr1, cstr2) 151} 152 153// InsertItem is a wrapper around g_menu_insert_item(). 154func (v *Menu) InsertItem(position int, item *MenuItem) { 155 C.g_menu_insert_item(v.native(), C.gint(position), item.native()) 156} 157 158// AppendItem is a wrapper around g_menu_append_item(). 159func (v *Menu) AppendItem(item *MenuItem) { 160 C.g_menu_append_item(v.native(), item.native()) 161} 162 163// PrependItem is a wrapper around g_menu_prepend_item(). 164func (v *Menu) PrependItem(item *MenuItem) { 165 C.g_menu_prepend_item(v.native(), item.native()) 166} 167 168// InsertSection is a wrapper around g_menu_insert_section(). 169func (v *Menu) InsertSection(position int, label string, section *MenuModel) { 170 cstr1 := (*C.gchar)(C.CString(label)) 171 defer C.free(unsafe.Pointer(cstr1)) 172 173 C.g_menu_insert_section(v.native(), C.gint(position), cstr1, section.native()) 174} 175 176// PrependSection is a wrapper around g_menu_prepend_section(). 177func (v *Menu) PrependSection(label string, section *MenuModel) { 178 cstr1 := (*C.gchar)(C.CString(label)) 179 defer C.free(unsafe.Pointer(cstr1)) 180 181 C.g_menu_prepend_section(v.native(), cstr1, section.native()) 182} 183 184// AppendSection is a wrapper around g_menu_append_section(). 185func (v *Menu) AppendSection(label string, section *MenuModel) { 186 cstr1 := (*C.gchar)(C.CString(label)) 187 defer C.free(unsafe.Pointer(cstr1)) 188 189 C.g_menu_append_section(v.native(), cstr1, section.native()) 190} 191 192// InsertSectionWithoutLabel is a wrapper around g_menu_insert_section() 193// with label set to null. 194func (v *Menu) InsertSectionWithoutLabel(position int, section *MenuModel) { 195 C.g_menu_insert_section(v.native(), C.gint(position), nil, section.native()) 196} 197 198// PrependSectionWithoutLabel is a wrapper around 199// g_menu_prepend_section() with label set to null. 200func (v *Menu) PrependSectionWithoutLabel(section *MenuModel) { 201 C.g_menu_prepend_section(v.native(), nil, section.native()) 202} 203 204// AppendSectionWithoutLabel is a wrapper around g_menu_append_section() 205// with label set to null. 206func (v *Menu) AppendSectionWithoutLabel(section *MenuModel) { 207 C.g_menu_append_section(v.native(), nil, section.native()) 208} 209 210// InsertSubmenu is a wrapper around g_menu_insert_submenu(). 211func (v *Menu) InsertSubmenu(position int, label string, submenu *MenuModel) { 212 cstr1 := (*C.gchar)(C.CString(label)) 213 defer C.free(unsafe.Pointer(cstr1)) 214 215 C.g_menu_insert_submenu(v.native(), C.gint(position), cstr1, submenu.native()) 216} 217 218// PrependSubmenu is a wrapper around g_menu_prepend_submenu(). 219func (v *Menu) PrependSubmenu(label string, submenu *MenuModel) { 220 cstr1 := (*C.gchar)(C.CString(label)) 221 defer C.free(unsafe.Pointer(cstr1)) 222 223 C.g_menu_prepend_submenu(v.native(), cstr1, submenu.native()) 224} 225 226// AppendSubmenu is a wrapper around g_menu_append_submenu(). 227func (v *Menu) AppendSubmenu(label string, submenu *MenuModel) { 228 cstr1 := (*C.gchar)(C.CString(label)) 229 defer C.free(unsafe.Pointer(cstr1)) 230 231 C.g_menu_append_submenu(v.native(), cstr1, submenu.native()) 232} 233 234// Remove is a wrapper around g_menu_remove(). 235func (v *Menu) Remove(position int) { 236 C.g_menu_remove(v.native(), C.gint(position)) 237} 238 239// RemoveAll is a wrapper around g_menu_remove_all(). 240func (v *Menu) RemoveAll() { 241 C.g_menu_remove_all(v.native()) 242} 243 244// MenuItem is a representation of GMenuItem. 245type MenuItem struct { 246 *Object 247} 248 249// native() returns a pointer to the underlying GMenuItem. 250func (v *MenuItem) native() *C.GMenuItem { 251 if v == nil || v.GObject == nil { 252 return nil 253 } 254 p := unsafe.Pointer(v.GObject) 255 return C.toGMenuItem(p) 256} 257 258func marshalMenuItem(p uintptr) (interface{}, error) { 259 c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p))) 260 return wrapMenuItem(wrapObject(unsafe.Pointer(c))), nil 261} 262 263func wrapMenuItem(obj *Object) *MenuItem { 264 return &MenuItem{obj} 265} 266 267// MenuItemNew is a wrapper around g_menu_item_new(NULL, NULL). 268func MenuItemNew() *MenuItem { 269 c := C.g_menu_item_new(nil, nil) 270 if c == nil { 271 return nil 272 } 273 return wrapMenuItem(wrapObject(unsafe.Pointer(c))) 274} 275 276// MenuItemNewWithLabel is a wrapper around g_menu_item_new(label, NULL). 277func MenuItemNewWithLabel(label string) *MenuItem { 278 cstr1 := (*C.gchar)(C.CString(label)) 279 defer C.free(unsafe.Pointer(cstr1)) 280 281 c := C.g_menu_item_new(cstr1, nil) 282 if c == nil { 283 return nil 284 } 285 return wrapMenuItem(wrapObject(unsafe.Pointer(c))) 286} 287 288// MenuItemNewWithAction is a wrapper around g_menu_item_new(NULL, detailedAction). 289func MenuItemNewWithAction(detailedAction string) *MenuItem { 290 cstr1 := (*C.gchar)(C.CString(detailedAction)) 291 defer C.free(unsafe.Pointer(cstr1)) 292 293 c := C.g_menu_item_new(nil, cstr1) 294 if c == nil { 295 return nil 296 } 297 return wrapMenuItem(wrapObject(unsafe.Pointer(c))) 298} 299 300// MenuItemNewWithLabelAndAction is a wrapper around g_menu_item_new(label, detailedAction). 301func MenuItemNewWithLabelAndAction(label, detailedAction string) *MenuItem { 302 cstr1 := (*C.gchar)(C.CString(label)) 303 defer C.free(unsafe.Pointer(cstr1)) 304 305 cstr2 := (*C.gchar)(C.CString(detailedAction)) 306 defer C.free(unsafe.Pointer(cstr2)) 307 308 c := C.g_menu_item_new(cstr1, cstr2) 309 if c == nil { 310 return nil 311 } 312 return wrapMenuItem(wrapObject(unsafe.Pointer(c))) 313} 314 315// MenuItemNewSection is a wrapper around g_menu_item_new_section(). 316func MenuItemNewSection(label string, section *MenuModel) *MenuItem { 317 cstr1 := (*C.gchar)(C.CString(label)) 318 defer C.free(unsafe.Pointer(cstr1)) 319 320 c := C.g_menu_item_new_section(cstr1, section.native()) 321 if c == nil { 322 return nil 323 } 324 return wrapMenuItem(wrapObject(unsafe.Pointer(c))) 325} 326 327// MenuItemNewSubmenu is a wrapper around g_menu_item_new_submenu(). 328func MenuItemNewSubmenu(label string, submenu *MenuModel) *MenuItem { 329 cstr1 := (*C.gchar)(C.CString(label)) 330 defer C.free(unsafe.Pointer(cstr1)) 331 332 c := C.g_menu_item_new_submenu(cstr1, submenu.native()) 333 if c == nil { 334 return nil 335 } 336 return wrapMenuItem(wrapObject(unsafe.Pointer(c))) 337} 338 339// MenuItemNewFromModel is a wrapper around g_menu_item_new_from_model(). 340func MenuItemNewFromModel(model *MenuModel, index int) *MenuItem { 341 c := C.g_menu_item_new_from_model(model.native(), C.gint(index)) 342 if c == nil { 343 return nil 344 } 345 return wrapMenuItem(wrapObject(unsafe.Pointer(c))) 346} 347 348// SetLabel is a wrapper around g_menu_item_set_label(). 349func (v *MenuItem) SetLabel(label string) { 350 cstr1 := (*C.gchar)(C.CString(label)) 351 defer C.free(unsafe.Pointer(cstr1)) 352 353 C.g_menu_item_set_label(v.native(), cstr1) 354} 355 356// UnsetLabel is a wrapper around g_menu_item_set_label(NULL). 357func (v *MenuItem) UnsetLabel() { 358 C.g_menu_item_set_label(v.native(), nil) 359} 360 361// SetDetailedAction is a wrapper around g_menu_item_set_detailed_action(). 362func (v *MenuItem) SetDetailedAction(act string) { 363 cstr1 := (*C.gchar)(C.CString(act)) 364 defer C.free(unsafe.Pointer(cstr1)) 365 366 C.g_menu_item_set_detailed_action(v.native(), cstr1) 367} 368 369// SetSection is a wrapper around g_menu_item_set_section(). 370func (v *MenuItem) SetSection(section *MenuModel) { 371 C.g_menu_item_set_section(v.native(), section.native()) 372} 373 374// SetSubmenu is a wrapper around g_menu_item_set_submenu(). 375func (v *MenuItem) SetSubmenu(submenu *MenuModel) { 376 C.g_menu_item_set_submenu(v.native(), submenu.native()) 377} 378 379// GetLink is a wrapper around g_menu_item_get_link(). 380func (v *MenuItem) GetLink(link string) *MenuModel { 381 cstr1 := (*C.gchar)(C.CString(link)) 382 defer C.free(unsafe.Pointer(cstr1)) 383 384 c := C.g_menu_item_get_link(v.native(), cstr1) 385 if c == nil { 386 return nil 387 } 388 return wrapMenuModel(wrapObject(unsafe.Pointer(c))) 389} 390 391// SetLink is a wrapper around g_menu_item_Set_link(). 392func (v *MenuItem) SetLink(link string, model *MenuModel) { 393 cstr1 := (*C.gchar)(C.CString(link)) 394 defer C.free(unsafe.Pointer(cstr1)) 395 396 C.g_menu_item_set_link(v.native(), cstr1, model.native()) 397} 398 399// SetActionAndTargetValue is a wrapper around g_menu_item_set_action_and_target_value() 400func (v *MenuItem) SetActionAndTargetValue(action string, targetValue IVariant) { 401 cstr1 := (*C.gchar)(C.CString(action)) 402 defer C.free(unsafe.Pointer(cstr1)) 403 404 var c *C.GVariant 405 if targetValue != nil { 406 c = targetValue.ToGVariant() 407 } 408 409 C.g_menu_item_set_action_and_target_value(v.native(), cstr1, c) 410} 411 412// UnsetAction is a wrapper around g_menu_item_set_action_and_target_value(NULL, NULL) 413// Unsets both action and target value. Unsetting the action also clears the target value. 414func (v *MenuItem) UnsetAction() { 415 C.g_menu_item_set_action_and_target_value(v.native(), nil, nil) 416} 417 418// SetAttributeValue is a wrapper around g_menu_item_set_attribute_value() 419func (v *MenuItem) SetAttributeValue(attribute string, value IVariant) { 420 var c *C.GVariant 421 if value != nil { 422 c = value.ToGVariant() 423 } 424 425 cstr1 := (*C.gchar)(C.CString(attribute)) 426 defer C.free(unsafe.Pointer(cstr1)) 427 428 C.g_menu_item_set_attribute_value(v.native(), cstr1, c) 429} 430 431// GetAttributeValue is a wrapper around g_menu_item_get_attribute_value() 432func (v *MenuItem) GetAttributeValue(attribute string, expectedType *VariantType) *Variant { 433 cstr1 := (*C.gchar)(C.CString(attribute)) 434 defer C.free(unsafe.Pointer(cstr1)) 435 436 c := C.g_menu_item_get_attribute_value(v.native(), cstr1, expectedType.native()) 437 if c == nil { 438 return nil 439 } 440 return newVariant(c) 441} 442 443// TODO: These require positional parameters with *any* type, according to the format string passed. 444// This is likely not possible to represent 1:1 in go. 445// gboolean g_menu_item_get_attribute () 446// void g_menu_item_set_attribute () 447// void g_menu_item_set_action_and_target () 448