-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialogs.red
108 lines (92 loc) · 3.63 KB
/
dialogs.red
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
Red [
Title: "Alert, confirm, prompt dialogs with examples."
Date: "27-Dec-2017"
Author: "Mike Parr"
File: %dialogs.red
needs 'view
]
;-- alert-popup displays a message, and waits for the user to click 'OK',
;-- or close the window. It is modal due to the use of /all, and [modal]
alert-popup: function [;-- based on work by Nodrygo
"Displays an alert message"
msg [string!] "Message to display"
] [
view/flags [
title ""
msg-text: text msg center return ;-- center the string INSIDE the text face
OK-btn: button "OK" [unview] ;-- or user can close window with 'X'
do [;-- centre the button
OK-btn/offset/x: msg-text/offset/x + (msg-text/size/x / 2) - (OK-btn/size/x / 2)]
] [modal popup]
]
alert-popup-demo: func [] [
alert-popup "Hello"
print "Alert closed."
alert-popup "and----------------- a long goodbye -----------------!"
print "Alert closed."
]
;-- prompt-popup displays a message, a field for single-line input, and
;-- 'OK' and 'Cancel' buttons. Normally it returns a string, though it returns
;-- false if 'Cancel' is clicked, and none if the window is closed with 'X'.
prompt-popup: function [
"Prompts for a string. Has OK/Cancel"
msg [string!] "Message to display"
] [
result: none ;-- in case user closes window with 'X'
view/flags [
title ""
msg-text: text msg center return
in-field: field return
yes-btn: button "OK" [result: in-field/text unview]
no-btn: button "Cancel" [result: false unview]
do [
gap: 10 ;--between OK and Cancel
;-- enlarge text if small
unless msg-text/size/x > (yes-btn/size/x + no-btn/size/x + gap) [
msg-text/size/x: yes-btn/size/x + no-btn/size/x + gap
]
win-centre: (2 * msg-text/offset/x + msg-text/size/x) / 2 ;-- centre buttons
yes-btn/offset/x: win-centre - yes-btn/size/x - (gap / 2)
no-btn/offset/x: win-centre + (gap / 2)
in-field/size/x: 150
in-field/offset/x: win-centre - (in-field/size/x / 2)
]
] [modal popup]
result
]
prompt-popup-demo: func [] [
print ["prompt-popup returns: " prompt-popup "Enter your name:"]
print ["prompt-popup returns: " prompt-popup "Enter your --------long --------name:"]
]
;-- confirm-popup displays a message, and 'OK' and 'Cancel' buttons. Normally it returns a
;-- string, though it returns false if 'Cancel' is clicked, and none if the window
;-- is closed with 'X'.
confirm-popup: function [
"Displays message. Clicking OK: returns true, Cancel: false, window-close: none"
msg [string!] "Message to display"
] [
result: none ;-- in case user closes window with 'X'
view/flags [
title ""
msg-text: text msg center return
yes-btn: button "OK" [result: true unview]
no-btn: button "Cancel" [result: false unview]
do [
gap: 10 ;--between OK and Cancel
;-- enlarge text if small
unless msg-text/size/x > (yes-btn/size/x + no-btn/size/x + gap) [
msg-text/size/x: yes-btn/size/x + no-btn/size/x + gap
]
win-centre: (2 * msg-text/offset/x + msg-text/size/x) / 2 ;-- centre buttons
yes-btn/offset/x: win-centre - yes-btn/size/x - (gap / 2)
no-btn/offset/x: win-centre + (gap / 2)
]
]
[modal popup]
result
]
confirm-popup-demo: func [] [
print ["Confirm-popup returns: " confirm-popup "Do it?"]
print ["Confirm-popup returns: " confirm-popup
"A longggggg question ------------------------!"]
]