2014-02-17 01:54:58 -06:00
|
|
|
// +build !windows,!darwin,!plan9
|
|
|
|
|
|
|
|
// 7 february 2014
|
2014-03-12 20:55:45 -05:00
|
|
|
|
2014-02-19 10:41:10 -06:00
|
|
|
package ui
|
2014-02-17 01:54:58 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2014-03-16 09:34:12 -05:00
|
|
|
// #include "gtk_unix.h"
|
2014-02-17 01:54:58 -06:00
|
|
|
// /* because cgo seems to choke on ... */
|
2014-06-04 23:24:05 -05:00
|
|
|
// /* parent will be NULL if there is no parent, so this is fine */
|
2014-06-08 10:50:11 -05:00
|
|
|
// static inline GtkWidget *gtkNewMsgBox(GtkWindow *parent, GtkMessageType type, GtkButtonsType buttons, char *title, char *text)
|
2014-04-10 11:34:30 -05:00
|
|
|
// {
|
|
|
|
// GtkWidget *k;
|
2014-06-10 13:49:54 -05:00
|
|
|
//
|
2014-06-04 23:24:05 -05:00
|
|
|
// k = gtk_message_dialog_new(parent, GTK_DIALOG_MODAL, type, buttons, "%s", (gchar *) title);
|
2014-04-10 11:34:30 -05:00
|
|
|
// if (text != NULL)
|
|
|
|
// gtk_message_dialog_format_secondary_text((GtkMessageDialog *) k, "%s", (gchar *) text);
|
|
|
|
// return k;
|
|
|
|
// }
|
2014-02-17 01:54:58 -06:00
|
|
|
import "C"
|
|
|
|
|
2014-06-30 21:48:12 -05:00
|
|
|
// the actual type of these constants is GtkResponseType but gtk_dialog_run() returns a gint
|
|
|
|
var dialogResponses = map[C.gint]Response{
|
|
|
|
C.GTK_RESPONSE_OK: OK,
|
|
|
|
}
|
|
|
|
|
2014-06-08 14:23:54 -05:00
|
|
|
// dialog performs the bookkeeping involved for having a GtkDialog behave the way we want.
|
2014-06-08 10:50:11 -05:00
|
|
|
type dialog struct {
|
2014-06-10 13:49:54 -05:00
|
|
|
parent *Window
|
|
|
|
pwin *C.GtkWindow
|
|
|
|
hadgroup C.gboolean
|
|
|
|
prevgroup *C.GtkWindowGroup
|
|
|
|
newgroup *C.GtkWindowGroup
|
2014-06-30 21:48:12 -05:00
|
|
|
result Response
|
2014-06-08 10:50:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func mkdialog(parent *Window) *dialog {
|
|
|
|
return &dialog{
|
2014-06-10 13:49:54 -05:00
|
|
|
parent: parent,
|
2014-06-08 10:50:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dialog) prepare() {
|
|
|
|
// to implement parent, we need to put the GtkMessageDialog into a new window group along with parent
|
|
|
|
// a GtkWindow can only be part of one group
|
|
|
|
// so we use this to save the parent window group (if there is one) and store the new window group
|
|
|
|
// after showing the message box, we restore the previous window group, so future parent == dialogWindow can work properly
|
|
|
|
// thanks to pbor and mclasen in irc.gimp.net/#gtk+
|
|
|
|
if d.parent != dialogWindow {
|
|
|
|
d.pwin = togtkwindow(d.parent.sysData.widget)
|
|
|
|
d.hadgroup = C.gtk_window_has_group(d.pwin)
|
|
|
|
// we can't remove a window from the "default window group"; otherwise this throws up Gtk-CRITICAL warnings
|
|
|
|
if d.hadgroup != C.FALSE {
|
|
|
|
d.prevgroup = C.gtk_window_get_group(d.pwin)
|
|
|
|
C.gtk_window_group_remove_window(d.prevgroup, d.pwin)
|
|
|
|
}
|
|
|
|
d.newgroup = C.gtk_window_group_new()
|
|
|
|
C.gtk_window_group_add_window(d.newgroup, d.pwin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dialog) cleanup(box *C.GtkWidget) {
|
|
|
|
// have to explicitly close the dialog box, otherwise wacky things will happen
|
|
|
|
C.gtk_widget_destroy(box)
|
|
|
|
if d.parent != dialogWindow {
|
|
|
|
C.gtk_window_group_remove_window(d.newgroup, d.pwin)
|
2014-06-10 13:49:54 -05:00
|
|
|
C.g_object_unref(C.gpointer(unsafe.Pointer(d.newgroup))) // free the group
|
2014-06-08 10:50:11 -05:00
|
|
|
if d.prevgroup != nil {
|
|
|
|
C.gtk_window_group_add_window(d.prevgroup, d.pwin)
|
2014-06-10 13:49:54 -05:00
|
|
|
} // otherwise it'll go back into the default group on its own
|
2014-06-08 10:50:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-30 21:48:12 -05:00
|
|
|
func (d *dialog) run(mk func() *C.GtkWidget) {
|
|
|
|
d.prepare()
|
|
|
|
box := mk()
|
|
|
|
r := C.gtk_dialog_run((*C.GtkDialog)(unsafe.Pointer(box)))
|
|
|
|
d.cleanup(box)
|
|
|
|
d.result = dialogResponses[r]
|
2014-06-08 10:50:11 -05:00
|
|
|
}
|
|
|
|
|
2014-06-30 21:48:12 -05:00
|
|
|
func _msgBox(parent *Window, primarytext string, secondarytext string, msgtype C.GtkMessageType, buttons C.GtkButtonsType) (response Response) {
|
2014-06-08 10:50:11 -05:00
|
|
|
d := mkdialog(parent)
|
2014-06-30 21:48:12 -05:00
|
|
|
cprimarytext := C.CString(primarytext)
|
|
|
|
defer C.free(unsafe.Pointer(cprimarytext))
|
|
|
|
csecondarytext := (*C.char)(nil)
|
|
|
|
if secondarytext != "" {
|
|
|
|
csecondarytext = C.CString(secondarytext)
|
|
|
|
defer C.free(unsafe.Pointer(csecondarytext))
|
2014-06-08 10:50:11 -05:00
|
|
|
}
|
2014-06-30 21:48:12 -05:00
|
|
|
d.run(func() *C.GtkWidget {
|
|
|
|
return C.gtkNewMsgBox(d.pwin, msgtype, buttons, cprimarytext, csecondarytext)
|
|
|
|
})
|
2014-06-08 10:50:11 -05:00
|
|
|
return d.result
|
2014-02-17 01:54:58 -06:00
|
|
|
}
|
|
|
|
|
2014-06-30 21:48:12 -05:00
|
|
|
func (w *Window) msgBox(primarytext string, secondarytext string) {
|
|
|
|
_msgBox(w, primarytext, secondarytext, C.GtkMessageType(C.GTK_MESSAGE_OTHER), C.GtkButtonsType(C.GTK_BUTTONS_OK))
|
2014-02-17 01:54:58 -06:00
|
|
|
}
|
|
|
|
|
2014-06-30 21:48:12 -05:00
|
|
|
func (w *Window) msgBoxError(primarytext string, secondarytext string) {
|
|
|
|
_msgBox(w, primarytext, secondarytext, C.GtkMessageType(C.GTK_MESSAGE_ERROR), C.GtkButtonsType(C.GTK_BUTTONS_OK))
|
2014-02-17 01:54:58 -06:00
|
|
|
}
|