forked from sqweek/dialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlgs_linux.go
89 lines (77 loc) · 2.42 KB
/
dlgs_linux.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
package dialog
import (
"github.com/gotk3/gotk3/gtk"
)
func init() {
gtk.Init(nil)
}
func closeDialog(dlg *gtk.Dialog) {
dlg.Destroy()
/* The Destroy call itself isn't enough to remove the dialog from the screen; apparently
** that happens once the GTK main loop processes some further events. But if we're
** in a non-GTK app the main loop isn't running, so we empty the event queue before
** returning from the dialog functions.
** Not sure how this interacts with an actual GTK app... */
for gtk.EventsPending() {
gtk.MainIteration()
}
}
func (b *MsgBuilder) yesNo() bool {
dlg := gtk.MessageDialogNew(nil, 0, gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, "%s", b.Msg)
dlg.SetTitle(firstOf(b.Dlg.Title, "Confirm?"))
defer closeDialog(&dlg.Dialog)
return dlg.Run() == gtk.RESPONSE_YES
}
func (b *MsgBuilder) info() {
dlg := gtk.MessageDialogNew(nil, 0, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, "%s", b.Msg)
dlg.SetTitle(firstOf(b.Dlg.Title, "Information"))
defer closeDialog(&dlg.Dialog)
dlg.Run()
}
func (b *MsgBuilder) error() {
dlg := gtk.MessageDialogNew(nil, 0, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, "%s", b.Msg)
dlg.SetTitle(firstOf(b.Dlg.Title, "Error"))
defer closeDialog(&dlg.Dialog)
dlg.Run()
}
func (b *FileBuilder) load() (string, error) {
return chooseFile("Open File", "Open", gtk.FILE_CHOOSER_ACTION_OPEN, b)
}
func (b *FileBuilder) save() (string, error) {
f, err := chooseFile("Save File", "Save", gtk.FILE_CHOOSER_ACTION_SAVE, b)
if err != nil {
return "", err
}
return f, nil
}
func chooseFile(title string, buttonText string, action gtk.FileChooserAction, b *FileBuilder) (string, error) {
dlg, err := gtk.FileChooserDialogNewWith2Buttons(firstOf(b.Dlg.Title, title),
nil, action, "Cancel", gtk.RESPONSE_CANCEL, buttonText, gtk.RESPONSE_ACCEPT)
if err != nil {
return "", err
}
for _, filt := range b.Filters {
filter, err := gtk.FileFilterNew()
if err != nil {
return "", err
}
filter.SetName(filt.Desc)
for _, ext := range filt.Extensions {
filter.AddPattern("*." + ext)
}
dlg.AddFilter(filter)
}
if b.StartDir != "" {
dlg.SetCurrentFolder(b.StartDir)
}
dlg.SetDoOverwriteConfirmation(true)
r := dlg.Run()
defer closeDialog(&dlg.Dialog)
if r == gtk.RESPONSE_ACCEPT {
return dlg.GetFilename(), nil
}
return "", ErrCancelled
}
func (b *DirectoryBuilder) browse() (string, error) {
return chooseFile("Open Folder", "Open", gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER, &FileBuilder{Dlg: b.Dlg})
}