summaryrefslogtreecommitdiff
path: root/dialog_windows.go
blob: 21beee083245ecf9b3347ff13a22d84009294e41 (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
// 7 february 2014
package ui

import (
	"fmt"
	"os"
	"syscall"
	"unsafe"
)

// TODO change what the default window titles are?

// MessageBox button types.
const (
	_MB_ABORTRETRYIGNORE = 0x00000002
	_MB_CANCELTRYCONTINUE = 0x00000006
	_MB_HELP = 0x00004000
	_MB_OK = 0x00000000
	_MB_OKCANCEL = 0x00000001
	_MB_RETRYCANCEL = 0x00000005
	_MB_YESNO = 0x00000004
	_MB_YESNOCANCEL = 0x00000003
)

// MessageBox icon types.
const (
	_MB_ICONEXCLAMATION = 0x00000030
	_MB_ICONWARNING = 0x00000030
	_MB_ICONINFORMATION = 0x00000040
	_MB_ICONASTERISK = 0x00000040
	_MB_ICONQUESTION = 0x00000020
	_MB_ICONSTOP = 0x00000010
	_MB_ICONERROR = 0x00000010
	_MB_ICONHAND = 0x00000010
)

// MessageBox default button types.
const (
	_MB_DEFBUTTON1 = 0x00000000
	_MB_DEFBUTTON2 = 0x00000100
	_MB_DEFBUTTON3 = 0x00000200
	_MB_DEFBUTTON4 = 0x00000300
)

// MessageBox modality types.
const (
	_MB_APPLMODAL = 0x00000000
	_MB_SYSTEMMODAL = 0x00001000
	_MB_TASKMODAL = 0x00002000
)

// MessageBox miscellaneous types.
const (
	_MB_DEFAULT_DESKTOP_ONLY = 0x00020000
	_MB_RIGHT = 0x00080000
	_MB_RTLREADING = 0x00100000
	_MB_SETFOREGROUND = 0x00010000
	_MB_TOPMOST = 0x00040000
	_MB_SERVICE_NOTIFICATION = 0x00200000
)

// MessageBox return values.
const (
	_IDABORT = 3
	_IDCANCEL = 2
	_IDCONTINUE = 11
	_IDIGNORE = 5
	_IDNO = 7
	_IDOK = 1
	_IDRETRY = 4
	_IDTRYAGAIN = 10
	_IDYES = 6
)

var (
	_messageBox = user32.NewProc("MessageBoxW")
)

func _msgBox(primarytext string, secondarytext string, uType uint32) (result int) {
	// http://msdn.microsoft.com/en-us/library/windows/desktop/aa511267.aspx says "Use task dialogs whenever appropriate to achieve a consistent look and layout. Task dialogs require Windows Vista® or later, so they aren't suitable for earlier versions of Windows. If you must use a message box, separate the main instruction from the supplemental instruction with two line breaks."
	text := primarytext + "\n\n" + secondarytext
	r1, _, err := _messageBox.Call(
		uintptr(_NULL),
		uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))),
		uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(os.Args[0]))),
		uintptr(uType))
	if r1 == 0 {		// failure
		panic(fmt.Sprintf("error displaying message box to user: %v\nstyle: 0x%08X\ntitle: %q\ntext:\n%s", err, uType, os.Args[0], text))
	}
	return int(r1)
}

func msgBox(primarytext string, secondarytext string) {
	// TODO add an icon?
	_msgBox(primarytext, secondarytext, _MB_OK)
}

func msgBoxError(primarytext string, secondarytext string) {
	_msgBox(primarytext, secondarytext, _MB_OK | _MB_ICONERROR)
}