2014-02-16 14:55:51 -06:00
|
|
|
// +build !windows,!darwin,!plan9
|
|
|
|
// TODO is there a way to simplify the above? :/
|
|
|
|
|
|
|
|
// 16 february 2014
|
2014-02-19 10:41:10 -06:00
|
|
|
package ui
|
2014-02-16 14:55:51 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// #cgo pkg-config: gtk+-3.0
|
|
|
|
// #include <stdlib.h>
|
|
|
|
// #include <gtk/gtk.h>
|
|
|
|
// /* because cgo is flaky with macros */
|
2014-02-16 16:09:58 -06:00
|
|
|
// void gSignalConnect(GtkWidget *widget, char *signal, GCallback callback, void *data) { g_signal_connect(widget, signal, callback, data); }
|
2014-02-16 14:55:51 -06:00
|
|
|
import "C"
|
|
|
|
|
|
|
|
type (
|
|
|
|
gtkWidget C.GtkWidget
|
|
|
|
)
|
|
|
|
|
|
|
|
func gtk_init() bool {
|
|
|
|
// TODO allow GTK+ standard command-line argument processing
|
2014-02-16 16:09:58 -06:00
|
|
|
return fromgbool(C.gtk_init_check((*C.int)(nil), (***C.char)(nil)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func gdk_threads_add_idle(what func() bool) {
|
|
|
|
C.gdk_threads_add_idle(callbacks["idle"], C.gpointer(unsafe.Pointer(&what)))
|
2014-02-16 14:55:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func gtk_main() {
|
|
|
|
C.gtk_main()
|
|
|
|
}
|
|
|
|
|
|
|
|
func gtk_window_new() *gtkWidget {
|
|
|
|
// 0 == GTK_WINDOW_TOPLEVEL (the only other type, _POPUP, should not be used)
|
2014-02-17 14:45:26 -06:00
|
|
|
return fromgtkwidget(C.gtk_window_new(0))
|
2014-02-16 14:55:51 -06:00
|
|
|
}
|
|
|
|
|
2014-02-18 13:22:31 -06:00
|
|
|
// the garbage collector has been found to eat my callback functions; this will stop it
|
|
|
|
var callbackstore = make([]*func() bool, 0, 50)
|
|
|
|
|
2014-02-16 15:43:48 -06:00
|
|
|
func g_signal_connect(obj *gtkWidget, sig string, callback func() bool) {
|
2014-02-18 13:22:31 -06:00
|
|
|
callbackstore = append(callbackstore, &callback)
|
2014-02-16 15:43:48 -06:00
|
|
|
ccallback := callbacks[sig]
|
2014-02-16 14:55:51 -06:00
|
|
|
csig := C.CString(sig)
|
|
|
|
defer C.free(unsafe.Pointer(csig))
|
2014-02-17 14:45:26 -06:00
|
|
|
C.gSignalConnect(togtkwidget(obj), csig, ccallback, unsafe.Pointer(&callback))
|
2014-02-16 14:55:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO ensure this works if called on an individual control
|
|
|
|
func gtk_widget_show(widget *gtkWidget) {
|
2014-02-17 14:45:26 -06:00
|
|
|
C.gtk_widget_show_all(togtkwidget(widget))
|
2014-02-16 14:55:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func gtk_widget_hide(widget *gtkWidget) {
|
2014-02-17 14:45:26 -06:00
|
|
|
C.gtk_widget_hide(togtkwidget(widget))
|
2014-02-16 14:55:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func gtk_window_set_title(window *gtkWidget, title string) {
|
|
|
|
ctitle := C.CString(title)
|
|
|
|
defer C.free(unsafe.Pointer(ctitle))
|
2014-02-17 14:45:26 -06:00
|
|
|
C.gtk_window_set_title(togtkwindow(window), togchar(ctitle))
|
2014-02-16 14:55:51 -06:00
|
|
|
}
|
|
|
|
|
2014-02-16 17:57:50 -06:00
|
|
|
func gtk_window_get_title(window *gtkWidget) string {
|
2014-02-17 14:45:26 -06:00
|
|
|
return C.GoString(fromgchar(C.gtk_window_get_title(togtkwindow(window))))
|
2014-02-16 17:57:50 -06:00
|
|
|
}
|
|
|
|
|
2014-02-16 14:55:51 -06:00
|
|
|
func gtk_window_resize(window *gtkWidget, width int, height int) {
|
2014-02-17 14:45:26 -06:00
|
|
|
C.gtk_window_resize(togtkwindow(window), C.gint(width), C.gint(height))
|
2014-02-16 14:55:51 -06:00
|
|
|
}
|
|
|
|
|
2014-02-16 17:04:57 -06:00
|
|
|
func gtk_window_get_size(window *gtkWidget) (int, int) {
|
|
|
|
var width, height C.gint
|
|
|
|
|
2014-02-17 14:45:26 -06:00
|
|
|
C.gtk_window_get_size(togtkwindow(window), &width, &height)
|
2014-02-16 17:04:57 -06:00
|
|
|
return int(width), int(height)
|
|
|
|
}
|
|
|
|
|
2014-02-16 14:55:51 -06:00
|
|
|
func gtk_fixed_new() *gtkWidget {
|
2014-02-17 14:45:26 -06:00
|
|
|
return fromgtkwidget(C.gtk_fixed_new())
|
2014-02-16 14:55:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func gtk_container_add(container *gtkWidget, widget *gtkWidget) {
|
2014-02-17 14:45:26 -06:00
|
|
|
C.gtk_container_add(togtkcontainer(container), togtkwidget(widget))
|
2014-02-16 14:55:51 -06:00
|
|
|
}
|
|
|
|
|
2014-02-16 16:30:58 -06:00
|
|
|
func gtk_fixed_move(container *gtkWidget, widget *gtkWidget, x int, y int) {
|
2014-02-17 14:45:26 -06:00
|
|
|
C.gtk_fixed_move(togtkfixed(container), togtkwidget(widget),
|
2014-02-16 14:55:51 -06:00
|
|
|
C.gint(x), C.gint(y))
|
|
|
|
}
|
|
|
|
|
|
|
|
func gtk_widget_set_size_request(widget *gtkWidget, width int, height int) {
|
2014-02-17 14:45:26 -06:00
|
|
|
C.gtk_widget_set_size_request(togtkwidget(widget), C.gint(width), C.gint(height))
|
2014-02-16 14:55:51 -06:00
|
|
|
}
|
2014-02-16 16:30:58 -06:00
|
|
|
|
|
|
|
func gtk_button_new() *gtkWidget {
|
2014-02-17 14:45:26 -06:00
|
|
|
return fromgtkwidget(C.gtk_button_new())
|
2014-02-16 16:30:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func gtk_button_set_label(button *gtkWidget, label string) {
|
|
|
|
clabel := C.CString(label)
|
|
|
|
defer C.free(unsafe.Pointer(clabel))
|
2014-02-17 14:45:26 -06:00
|
|
|
C.gtk_button_set_label(togtkbutton(button), togchar(clabel))
|
2014-02-16 16:30:58 -06:00
|
|
|
}
|
2014-02-16 17:41:29 -06:00
|
|
|
|
2014-02-16 17:57:50 -06:00
|
|
|
func gtk_button_get_label(button *gtkWidget) string {
|
2014-02-17 14:45:26 -06:00
|
|
|
return C.GoString(fromgchar(C.gtk_button_get_label(togtkbutton(button))))
|
2014-02-16 17:57:50 -06:00
|
|
|
}
|
|
|
|
|
2014-02-16 17:41:29 -06:00
|
|
|
func gtk_check_button_new() *gtkWidget {
|
2014-02-17 14:45:26 -06:00
|
|
|
return fromgtkwidget(C.gtk_check_button_new())
|
2014-02-16 17:41:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func gtk_toggle_button_get_active(widget *gtkWidget) bool {
|
2014-02-17 14:45:26 -06:00
|
|
|
return fromgbool(C.gtk_toggle_button_get_active(togtktogglebutton(widget)))
|
2014-02-16 17:41:29 -06:00
|
|
|
}
|
2014-02-16 18:50:52 -06:00
|
|
|
|
|
|
|
func gtk_combo_box_text_new() *gtkWidget {
|
2014-02-17 14:45:26 -06:00
|
|
|
return fromgtkwidget(C.gtk_combo_box_text_new())
|
2014-02-16 18:50:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func gtk_combo_box_text_new_with_entry() *gtkWidget {
|
2014-02-17 14:45:26 -06:00
|
|
|
return fromgtkwidget(C.gtk_combo_box_text_new_with_entry())
|
2014-02-16 18:50:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func gtk_combo_box_text_get_active_text(widget *gtkWidget) string {
|
2014-02-17 14:45:26 -06:00
|
|
|
return C.GoString(fromgchar(C.gtk_combo_box_text_get_active_text(togtkcombobox(widget))))
|
2014-02-16 18:50:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func gtk_combo_box_text_append_text(widget *gtkWidget, text string) {
|
|
|
|
ctext := C.CString(text)
|
|
|
|
defer C.free(unsafe.Pointer(ctext))
|
2014-02-17 14:45:26 -06:00
|
|
|
C.gtk_combo_box_text_append_text(togtkcombobox(widget), togchar(ctext))
|
2014-02-16 18:50:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func gtk_combo_box_text_insert_text(widget *gtkWidget, index int, text string) {
|
|
|
|
ctext := C.CString(text)
|
|
|
|
defer C.free(unsafe.Pointer(ctext))
|
2014-02-17 14:45:26 -06:00
|
|
|
C.gtk_combo_box_text_insert_text(togtkcombobox(widget), C.gint(index), togchar(ctext))
|
2014-02-16 18:50:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func gtk_combo_box_get_active(widget *gtkWidget) int {
|
2014-02-17 14:45:26 -06:00
|
|
|
cb := (*C.GtkComboBox)(unsafe.Pointer(widget))
|
|
|
|
return int(C.gtk_combo_box_get_active(cb))
|
2014-02-16 18:50:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func gtk_combo_box_text_remove(widget *gtkWidget, index int) {
|
2014-02-17 14:45:26 -06:00
|
|
|
C.gtk_combo_box_text_remove(togtkcombobox(widget), C.gint(index))
|
2014-02-16 18:50:52 -06:00
|
|
|
}
|
2014-02-16 20:40:59 -06:00
|
|
|
|
|
|
|
func gtk_entry_new() *gtkWidget {
|
2014-02-17 14:45:26 -06:00
|
|
|
return fromgtkwidget(C.gtk_entry_new())
|
2014-02-16 20:40:59 -06:00
|
|
|
}
|
|
|
|
|
2014-02-25 14:06:51 -06:00
|
|
|
func gtkPasswordEntryNew() *gtkWidget {
|
|
|
|
e := gtk_entry_new()
|
|
|
|
C.gtk_entry_set_visibility(togtkentry(e), C.FALSE)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2014-02-16 20:40:59 -06:00
|
|
|
func gtk_entry_set_text(widget *gtkWidget, text string) {
|
|
|
|
ctext := C.CString(text)
|
|
|
|
defer C.free(unsafe.Pointer(ctext))
|
2014-02-17 14:45:26 -06:00
|
|
|
C.gtk_entry_set_text(togtkentry(widget), togchar(ctext))
|
2014-02-16 20:40:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func gtk_entry_get_text(widget *gtkWidget) string {
|
2014-02-17 14:45:26 -06:00
|
|
|
return C.GoString(fromgchar(C.gtk_entry_get_text(togtkentry(widget))))
|
2014-02-16 20:40:59 -06:00
|
|
|
}
|
2014-02-16 21:03:14 -06:00
|
|
|
|
|
|
|
var _emptystring = [1]C.gchar{0}
|
|
|
|
var emptystring = &_emptystring[0]
|
|
|
|
|
|
|
|
func gtk_label_new() *gtkWidget {
|
2014-02-17 14:45:26 -06:00
|
|
|
return fromgtkwidget(C.gtk_label_new(emptystring))
|
2014-02-16 21:03:14 -06:00
|
|
|
// TODO left-justify?
|
|
|
|
}
|
|
|
|
|
|
|
|
func gtk_label_set_text(widget *gtkWidget, text string) {
|
|
|
|
ctext := C.CString(text)
|
|
|
|
defer C.free(unsafe.Pointer(ctext))
|
2014-02-17 14:45:26 -06:00
|
|
|
C.gtk_label_set_text(togtklabel(widget), togchar(ctext))
|
2014-02-16 21:03:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func gtk_label_get_text(widget *gtkWidget) string {
|
2014-02-17 14:45:26 -06:00
|
|
|
return C.GoString(fromgchar(C.gtk_label_get_text(togtklabel(widget))))
|
2014-02-16 21:03:14 -06:00
|
|
|
}
|
2014-02-23 19:04:33 -06:00
|
|
|
|
|
|
|
func gtk_widget_get_preferred_size(w *gtkWidget) (minWidth int, minHeight int, natWidth int, natHeight int) {
|
|
|
|
var minimum, natural C.GtkRequisition
|
|
|
|
|
|
|
|
C.gtk_widget_get_preferred_size(togtkwidget(w), &minimum, &natural)
|
|
|
|
return int(minimum.width), int(minimum.height),
|
|
|
|
int(natural.width), int(natural.height)
|
|
|
|
}
|
2014-02-24 23:48:23 -06:00
|
|
|
|
|
|
|
func gtk_progress_bar_new() *gtkWidget {
|
|
|
|
return fromgtkwidget(C.gtk_progress_bar_new())
|
|
|
|
}
|
|
|
|
|
|
|
|
func gtk_progress_bar_set_fraction(w *gtkWidget, percent int) {
|
|
|
|
p := C.gdouble(percent) / 100
|
|
|
|
C.gtk_progress_bar_set_fraction(togtkprogressbar(w), p)
|
|
|
|
}
|