-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmenu.go
184 lines (164 loc) · 5.83 KB
/
menu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package fltk_go
/*
#include <stdlib.h>
#include "menu.h"
*/
import "C"
import (
"unsafe"
)
type menu struct {
widget
deletionHandlerId uintptr
itemCallbacks []uintptr
}
func (m *menu) init() {
if m.deletionHandlerId > 0 {
panic("menu already initialized")
}
m.deletionHandlerId = m.addDeletionHandler(m.onDelete)
}
func (m *menu) onDelete() {
if m.deletionHandlerId > 0 {
globalCallbackMap.unregister(m.deletionHandlerId)
}
m.deletionHandlerId = 0
for _, itemCallbackId := range m.itemCallbacks {
globalCallbackMap.unregister(itemCallbackId)
}
m.itemCallbacks = m.itemCallbacks[:0]
}
func (m *menu) Destroy() {
for _, itemCallbackId := range m.itemCallbacks {
globalCallbackMap.unregister(itemCallbackId)
}
m.itemCallbacks = m.itemCallbacks[:0]
m.widget.Destroy()
}
// Add adds a new menu item with the given label that when chosen will execute
// the given callback. Returns the new item's index.
func (m *menu) Add(label string, callback func()) int {
callbackId := globalCallbackMap.register(callback)
m.itemCallbacks = append(m.itemCallbacks, callbackId)
labelStr := C.CString(label)
defer C.free(unsafe.Pointer(labelStr))
return int(C.go_fltk_Menu_add((*C.Fl_Menu_)(m.ptr()), labelStr, 0, C.int(callbackId), 0))
}
// Add adds a new menu item with the given label and shortcut that when
// chosen (or when the shortcut is pressed) will execute the given callback.
// Set flags to fltk_go.MENU_DIVIDER to create a separator after this menu
// item. Returns the new item's index.
func (m *menu) AddEx(label string, shortcut int, callback func(), flags int) int {
callbackId := globalCallbackMap.register(callback)
m.itemCallbacks = append(m.itemCallbacks, callbackId)
labelStr := C.CString(label)
defer C.free(unsafe.Pointer(labelStr))
return int(C.go_fltk_Menu_add((*C.Fl_Menu_)(m.ptr()), labelStr, C.int(shortcut), C.int(callbackId), C.int(flags)))
}
func (m *menu) AddExWithIcon(label string, shortcut int, callback func(), flags int, img Image) int {
callbackId := globalCallbackMap.register(callback)
m.itemCallbacks = append(m.itemCallbacks, callbackId)
labelStr := C.CString(label)
defer C.free(unsafe.Pointer(labelStr))
return int(C.go_fltk_Menu_add_with_icon((*C.Fl_Menu_)(m.ptr()), labelStr, C.int(shortcut), C.int(callbackId), C.int(flags), img.getImage().ptr()))
}
func (m *menu) Insert(index int, label string, callback func()) int {
callbackId := globalCallbackMap.register(callback)
m.itemCallbacks = append(m.itemCallbacks, callbackId)
labelStr := C.CString(label)
defer C.free(unsafe.Pointer(labelStr))
return int(C.go_fltk_Menu_insert((*C.Fl_Menu_)(m.ptr()), C.int(index), labelStr, 0, C.int(callbackId), 0))
}
func (m *menu) InsertEx(index int, label string, shortcut int, callback func(), flags int) int {
callbackId := globalCallbackMap.register(callback)
m.itemCallbacks = append(m.itemCallbacks, callbackId)
labelStr := C.CString(label)
defer C.free(unsafe.Pointer(labelStr))
return int(C.go_fltk_Menu_insert((*C.Fl_Menu_)(m.ptr()), C.int(index), labelStr, C.int(shortcut), C.int(callbackId), C.int(flags)))
}
// Removes removes the menu item at the given index position.
func (m *menu) Remove(index int) {
C.go_fltk_Menu_remove((*C.Fl_Menu_)(m.ptr()), C.int(index))
}
// Clear removes all the menu's items.
func (m *menu) Clear() {
C.go_fltk_Menu_clear((*C.Fl_Menu_)(m.ptr()))
}
func (m *menu) Replace(index int, label string) {
labelStr := C.CString(label)
defer C.free(unsafe.Pointer(labelStr))
C.go_fltk_Menu_replace((*C.Fl_Menu_)(m.ptr()), C.int(index), labelStr)
}
func (m *menu) FindIndex(label string) int {
labelStr := C.CString(label)
defer C.free(unsafe.Pointer(labelStr))
return int(C.go_fltk_Menu_find_index((*C.Fl_Menu_)(m.ptr()), labelStr))
}
func (m *menu) SetValue(value int) {
C.go_fltk_Menu_set_value((*C.Fl_Menu_)(m.ptr()), C.int(value))
}
// Value returns index of the last item chosen by the user.
func (m *menu) Value() int {
return int(C.go_fltk_Menu_value((*C.Fl_Menu_)(m.ptr())))
}
// SelectedText returns the title of the last item chosen by the user.
func (m *menu) SelectedText() string {
return C.GoString(C.go_fltk_Menu_selected_text((*C.Fl_Menu_)(m.ptr())))
}
// Text returns the title of the last item at index.
func (m *menu) Text(index int) string {
return C.GoString(C.go_fltk_Menu_text((*C.Fl_Menu_)(m.ptr()), C.int(index)))
}
func (m *menu) Size() int {
return int(C.go_fltk_Menu_size((*C.Fl_Menu_)(m.ptr())))
}
type MenuButton struct {
menu
}
func NewMenuButton(x, y, w, h int, text ...string) *MenuButton {
m := &MenuButton{}
initWidget(m, unsafe.Pointer(C.go_fltk_new_MenuButton(C.int(x), C.int(y), C.int(w), C.int(h), cStringOpt(text))))
m.menu.init()
return m
}
type MenuType int
var (
POPUP1 = MenuType(C.go_FL_POPUP1)
POPUP2 = MenuType(C.go_FL_POPUP2)
POPUP12 = MenuType(C.go_FL_POPUP12)
POPUP3 = MenuType(C.go_FL_POPUP3)
POPUP13 = MenuType(C.go_FL_POPUP13)
POPUP23 = MenuType(C.go_FL_POPUP23)
POPUP123 = MenuType(C.go_FL_POPUP123)
)
var (
MENU_INACTIVE = int(C.go_FL_MENU_INACTIVE)
MENU_TOGGLE = int(C.go_FL_MENU_TOGGLE)
MENU_VALUE = int(C.go_FL_MENU_VALUE)
MENU_RADIO = int(C.go_FL_MENU_RADIO)
MENU_INVISIBLE = int(C.go_FL_MENU_INVISIBLE)
SUBMENU = int(C.go_FL_SUBMENU)
MENU_DIVIDER = int(C.go_FL_MENU_DIVIDER)
)
func (m *MenuButton) SetType(menuType MenuType) {
C.go_fltk_MenuButton_set_type((*C.Fl_Menu_Button)(m.ptr()), C.int(menuType))
}
func (m *MenuButton) Popup() {
if m.Size() > 0 {
// Fltk may crash when Popup is called on empty menu.
// https://github.com/pwiecz/go-fltk/issues/64
C.go_fltk_MenuButton_popup((*C.Fl_Menu_Button)(m.ptr()))
}
}
func (m *MenuButton) Destroy() {
m.menu.Destroy()
}
type MenuBar struct {
menu
}
func NewMenuBar(x, y, w, h int, text ...string) *MenuBar {
m := &MenuBar{}
initWidget(m, unsafe.Pointer(C.go_fltk_new_MenuBar(C.int(x), C.int(y), C.int(w), C.int(h), cStringOpt(text))))
m.menu.init()
return m
}