File: message.bs

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (111 lines) | stat: -rw-r--r-- 2,496 bytes parent folder | download | duplicates (3)
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
use core:geometry;

/**
 * Basic message dialog.
 *
 * Similar to MsgBox in Win32.
 */
class MessageDialog extends Dialog {
	init(Str title, Str message) {
		init(title) {
			label(message);
			padding = 8;
		}

		add(label);
	}

	// The message.
	private Label label;

	// Buttons.
	private Button[] buttons;

	// Padding in the dialog.
	private Float padding;

	// Compute minimum size.
	Size minSize() : override {
		Size msg = label.minSize;

		Size buttonSz;
		for (b in buttons) {
			Size s = b.minSize();
			buttonSz.h = max(buttonSz.h, s.h);
			buttonSz.w += s.w + padding;
		}

		Size(max(msg.w, buttonSz.w) + 2*padding, msg.h + buttonSz.h + 3*padding);
	}

	// Update the contents on resize.
	void resized(Size size) : override {
		Size buttonSz;
		Point pos(size.w, size.h - padding);
		for (b in buttons) {
			Size s = b.minSize();
			buttonSz.h = max(buttonSz.h, s.h);
			buttonSz.w += s.w + padding;

			b.pos = Rect(Point(pos.x - s.w - padding, pos.y - s.h), s);
			pos.x -= s.w + padding;
		}

		label.pos = Rect(padding, padding, size.w - padding, size.h - buttonSz.h - 2*padding);
	}

	// Add a button.
	void addButton(Str text, Int result) {
		var me = this;
		Button b(text, () => me.close(result));
		buttons << b;
		add(b);
	}

	// Add the default button.
	void addDefButton(Str text, Int result) {
		var me = this;
		Button b(text);
		defaultChoice(b);
		b.onClick = () => me.close(result);
		buttons << b;
		add(b);
	}
}

/**
 * Response from a message.
 */
enum MessageResponse {
	ok, cancel, yes, no,
}

// Show a notification. I.e. a box with only OK.
void showMessage(Frame parent, Str title, Str message) on Ui {
	MessageDialog d(title, message);
	d.addDefButton("OK", 0);
	d.show(parent);
}

// Show a yes-no question.
MessageResponse showYesNoQuestion(Frame parent, Str title, Str message, Str yes, Str no) on Ui {
	MessageDialog d(title, message);
	d.addButton(no, MessageResponse:no.v.int);
	d.addDefButton(yes, MessageResponse:yes.v.int);
	Int r = d.show(parent);
	if (r < 0)
		return MessageResponse:cancel;
	MessageResponse(r.nat);
}

// Show a yes-no-cancel question.
MessageResponse showYesNoCancelQuestion(Frame parent, Str title, Str message, Str yes, Str no, Str cancel) on Ui {
	MessageDialog d(title, message);
	d.addButton(cancel, MessageResponse:cancel.v.int);
	d.addButton(no, MessageResponse:no.v.int);
	d.addDefButton(yes, MessageResponse:yes.v.int);
	Int r = d.show(parent);
	if (r < 0)
		return MessageResponse:cancel;
	MessageResponse(r.nat);
}