summaryrefslogtreecommitdiff
path: root/vendor/0xacab.org/leap/go-dialog/dlgs_linux.go
blob: ea8ac3d75ff54287cb9e22042c5933e4bdf20ffe (plain)
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
package dialog

import (
	"os"
	"path/filepath"

	"github.com/gotk3/gotk3/gdk"
	"github.com/gotk3/gotk3/glib"
	"github.com/gotk3/gotk3/gtk"
)

func closeDialog(dlg *gtk.Dialog) {
	dlg.Destroy()
}

func (b *MsgBuilder) yesNo() bool {
	ch := make(chan bool)
	_, err := glib.IdleAdd(b._yesNo, ch)
	if err != nil {
		return false
	}
	return <-ch
}

func (b *MsgBuilder) _yesNo(ch chan bool) bool {
	dlg := gtk.MessageDialogNew(nil, 0, gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, "%s", b.Msg)
	dlg.SetTitle(firstOf(b.Dlg.Title, "Confirm?"))
	b.setIcon(dlg)
	defer closeDialog(&dlg.Dialog)
	ch <- dlg.Run() == gtk.RESPONSE_YES
	return false
}

func (b *MsgBuilder) info() {
	ch := make(chan bool)
	glib.IdleAdd(b._info, ch)
	<-ch
}

func (b *MsgBuilder) _info(ch chan bool) {
	dlg := gtk.MessageDialogNew(nil, 0, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, "%s", b.Msg)
	dlg.SetTitle(firstOf(b.Dlg.Title, "Information"))
	b.setIcon(dlg)
	defer closeDialog(&dlg.Dialog)
	dlg.Run()
	ch <- true
}

func (b *MsgBuilder) error() {
	ch := make(chan bool)
	glib.IdleAdd(b._info, ch)
	<-ch
}

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"))
	b.setIcon(dlg)
	defer closeDialog(&dlg.Dialog)
	dlg.Run()
}

func (b *MsgBuilder) setIcon(dlg *gtk.MessageDialog) {
	if b.IconPath == "" {
		return
	}

	pixbuf, err := gdk.PixbufNewFromFile(b.IconPath)
	if err != nil {
		return
	}
	dlg.SetIcon(pixbuf)
}

func (b *FileBuilder) load() (string, error) {
	return chooseFile("Load", gtk.FILE_CHOOSER_ACTION_OPEN, b)
}

func (b *FileBuilder) save() (string, error) {
	f, err := chooseFile("Save", gtk.FILE_CHOOSER_ACTION_SAVE, b)
	if err != nil {
		return "", err
	}
	_, err = os.Stat(f)
	if !os.IsNotExist(err) && !Message("%s already exists, overwrite?", filepath.Base(f)).yesNo() {
		return "", Cancelled
	}
	return f, nil
}

func chooseFile(title string, action gtk.FileChooserAction, b *FileBuilder) (string, error) {
	dlg, err := gtk.FileChooserDialogNewWith2Buttons(firstOf(b.Dlg.Title, title), nil, action, "Ok", gtk.RESPONSE_ACCEPT, "Cancel", gtk.RESPONSE_CANCEL)
	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)
	}
	r := dlg.Run()
	defer closeDialog(&dlg.Dialog)
	if r == gtk.RESPONSE_ACCEPT {
		return dlg.GetFilename(), nil
	}
	return "", Cancelled
}

func (b *DirectoryBuilder) browse() (string, error) {
	return chooseFile("Open Directory", gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER, &FileBuilder{Dlg: b.Dlg})
}