-
Notifications
You must be signed in to change notification settings - Fork 6
/
StringInputDialog.sc
125 lines (91 loc) · 2.17 KB
/
StringInputDialog.sc
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
123
124
125
/*
StringInputDialog
(c) 2010 by Patrick Borgeat <patrick@borgeat.de>
http://www.cappel-nord.de
Part of BenoitLib
http://github.com/cappelnord/BenoitLib
http://www.the-mandelbrots.de
*/
StringInputDialog {
var window, txt, function;
classvar <>defaultPos = \center;
*new {|title, msg, function, width=400, pos|
^super.new.init(title, msg, function,width, pos);
}
init {|title, msg, a_function, width, pos|
var x;
var y;
var xPos = 0.5;
// used to make the text input a little bit larger when using swing
var swingBigger = 0;
(GUI.id == \swing).if {
swingBigger = 7;
};
function = a_function;
defaultPos.isKindOf(Point).if {
pos = pos ? defaultPos;
};
pos.isNil.if ({
(defaultPos == \center).if { xPos = 0.5}; // explicit center
(defaultPos == \left).if { xPos = 1/3};
(defaultPos == \right).if { xPos = 2/3};
x = Window.screenBounds.width * xPos - (width / 2);
y = Window.screenBounds.height / 2 - 10;
}, {
x = pos.x;
y = pos.y;
});
window = Window.new(title, Rect( x,
y,
width,
35 + swingBigger), false);
txt = TextField(window, Rect(7,7,width-80,20 + swingBigger));
Button(window, Rect(width - 67,7,57,20 + swingBigger))
.states_([[msg,Color.black,Color.clear]])
.action_({|button| this.doAction;});
(GUI.id == \cocoa).not.if({
txt.keyDownAction_({ |b, char, modifiers, unicode, keycode|
// return
(unicode == 13).if {
this.doAction;
this.close;
};
// escape
(unicode == 27).if {
this.close;
};
});
}, {
window.view.keyDownAction_({ |b, char, modifiers, unicode, keycode|
(char.isPrint).if {
txt.string_(txt.string ++ char);
};
// backspace
(unicode == 127).if {
(txt.string.size > 1).if({
txt.string_(txt.string[0..(txt.string.size-2)]);
},{
txt.string_("");
});
};
// return
(unicode == 13).if {
this.doAction;
this.close;
};
// escape
(unicode == 27).if {
this.close;
};
});
});
window.front;
}
doAction {
window.close;
function.value(txt.string);
}
close {
window.close;
}
}