blob: 5071dfbff3ab1211b3510e86664542ae811cc0fa (
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
|
# NiGui - extended message box
# This module provides an extended message box.
# The message box is shown as modal window.
# The message box can have up to 3 buttons with customizable titles.
# Call the proc msgBox() to open the message box.
# It will wait until the message box is closed.
# Meaning of the result value:
# 0 - message box was closed over the window close button
# 1..3 - button 1..3 clicked
# For an example see "example_04_msgboxes.nim".
import nigui
type MessageBoxWindow = ref object of WindowImpl
clickedButton: Button
proc buttonClick(event: ClickEvent) =
cast[MessageBoxWindow](event.control.parentWindow).clickedButton = cast[Button](event.control)
event.control.parentWindow.dispose()
proc msgBox*(parent: Window, message: string, title = "Message", button1 = "OK", button2, button3: string = nil): int {.discardable.} =
const buttonMinWidth = 100
var window = new MessageBoxWindow
window.init()
window.title = title
var container = newLayoutContainer(Layout_Vertical)
container.padding = 10
window.control = container
var labelContainer = newLayoutContainer(Layout_Horizontal)
container.add(labelContainer)
labelContainer.widthMode = WidthMode_Expand
labelContainer.heightMode = HeightMode_Expand
var label = newLabel(message)
labelContainer.add(label)
var buttonContainer = newLayoutContainer(Layout_Horizontal)
buttonContainer.widthMode = WidthMode_Expand
buttonContainer.xAlign = XAlign_Center
buttonContainer.spacing = 12
container.add(buttonContainer)
var b1, b2, b3: Button
b1 = newButton(button1)
b1.minWidth = buttonMinWidth
b1.onClick = buttonClick
buttonContainer.add(b1)
if button2 != nil:
b2 = newButton(button2)
b2.minWidth = buttonMinWidth
b2.onClick = buttonClick
buttonContainer.add(b2)
if button3 != nil:
b3 = newButton(button3)
b3.minWidth = buttonMinWidth
b3.onClick = buttonClick
buttonContainer.add(b3)
window.width = min(max(label.width + 40, buttonMinWidth * 3 + 65), 600)
window.height = min(label.height, 300) + buttonContainer.height + 70
# Center message box on window:
window.x = parent.x + ((parent.width - window.width) div 2)
window.y = parent.y + ((parent.height - window.height) div 2)
window.showModal(parent)
while not window.disposed:
app.sleep(100)
if window.clickedButton == b1:
result = 1
elif window.clickedButton == b2:
result = 2
elif window.clickedButton == b3:
result = 3
|