egra: agg mini clipping bugfix; some fixes in widget rendering (buttons, etc.)
[iv.d.git] / tuing / controls / button.d
blob41cbb56bc6588eeb3488b5c3d617692003647cae
1 /* Invisible Vector Library
2 * simple FlexBox-based TUI engine
4 * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
5 * Understanding is not required. Only obedience.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 3 of the License ONLY.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 // button
20 module iv.tuing.controls.button /*is aliced*/;
22 import iv.alice;
23 import iv.eventbus;
24 import iv.flexlayout;
25 import iv.strex;
26 import iv.rawtty;
28 import iv.tuing.events;
29 import iv.tuing.tty;
30 import iv.tuing.tui;
31 import iv.tuing.types;
34 // ////////////////////////////////////////////////////////////////////////// //
35 public class FuiButton : FuiControl {
36 alias onMyEvent = super.onMyEvent;
38 this (FuiControl aparent, string atext) {
39 this.connectListeners();
40 super(aparent);
41 caption = atext;
42 minSize.w = XtWindow.hotStrLen(atext)+4;
43 horizontal = true;
44 aligning = Align.Start;
45 minSize.h = maxSize.h = 1;
46 canBeFocused = true;
47 hotkeyed = true;
48 acceptClick(TtyEvent.MButton.Left);
49 assert(clickMask != 0);
52 override void doAction () {
53 if (canBeFocused) {
54 if (auto desk = getDesk) desk.switchFocusTo(this, false);
56 if (onAction !is null) { onAction(this); return; }
57 closetop;
60 protected override void drawSelf (XtWindow win) {
61 win.color = (focused ? palColor!"sel"() : disabled ? palColor!"disabled"() : palColor!"def"());
62 auto hotcolor = (focused ? palColor!"hotsel"() : disabled ? palColor!"disabled" : palColor!"hot"());
63 win.fill(0, 0, win.width, win.height);
64 if (defctl) {
65 win.writeCharAt(0, 0, '<');
66 win.writeCharAt(win.width-1, 0, '>');
67 } else if (escctl) {
68 win.writeCharAt(0, 0, '{');
69 win.writeCharAt(win.width-1, 0, '}');
70 } else {
71 win.writeCharAt(0, 0, '[');
72 win.writeCharAt(win.width-1, 0, ']');
74 win.writeHotStrAt(2, 0, win.width-4, caption, hotcolor, win.Align.Center, hotkeyed, focused);
77 void onMyEvent (FuiEventClick evt) {
78 if (evt.left) {
79 evt.eat();
80 doAction();
84 void onMyEvent (FuiEventKey evt) {
85 if (evt.key == "Space" || evt.key == "Enter") { evt.eat(); doAction(); return; }
86 if (tryHotKey(evt.key)) { evt.eat(); doAction(); return; }
91 // ////////////////////////////////////////////////////////////////////////// //
92 public class FuiCheckBox : FuiControl {
93 alias onMyEvent = super.onMyEvent;
95 string groupid;
97 this (FuiControl aparent, string atext, string agroupid) {
98 this.connectListeners();
99 super(aparent);
100 cbSetup(atext, agroupid);
103 this (FuiControl aparent, string atext, string agroupid, bool defval) {
104 this.connectListeners();
105 super(aparent);
106 cbSetup(atext, agroupid);
107 if (agroupid.length) {
108 if (auto w = topwindow) w.checkbox(agroupid, defval);
112 private final void cbSetup (string atext, string agroupid) {
113 groupid = agroupid;
114 caption = atext;
115 minSize.w = XtWindow.hotStrLen(atext)+4;
116 horizontal = true;
117 aligning = Align.Start;
118 minSize.h = maxSize.h = 1;
119 canBeFocused = true;
120 hotkeyed = true;
121 acceptClick(TtyEvent.MButton.Left);
124 override void doAction () {
125 if (canBeFocused) {
126 if (auto desk = getDesk) desk.switchFocusTo(this, false);
128 if (onAction !is null) { onAction(this); return; }
129 if (groupid.length) {
130 if (auto w = topwindow) {
131 auto nv = !w.checkbox(groupid);
132 static assert(is(typeof(nv) == bool));
133 w.checkbox(groupid, nv);
134 (new FuiEventCheckBoxChanged(this, groupid, nv)).post;
139 protected override void drawSelf (XtWindow win) {
140 win.color = (focused ? palColor!"sel"() : disabled ? palColor!"disabled"() : palColor!"def"());
141 auto hotcolor = (focused ? palColor!"hotsel"() : disabled ? palColor!"disabled" : palColor!"hot"());
142 win.fill(0, 0, win.width, win.height);
143 win.writeStrAt(0, 0, "[ ]");
144 if (auto w = topwindow) {
145 if (w.checkbox(groupid)) win.writeCharAt(1, 0, 'x');
147 win.writeHotStrAt(4, 0, win.width-4, caption, hotcolor, win.Align.Center, hotkeyed);
148 if (focused) win.gotoXY(1, 0);
151 void onMyEvent (FuiEventClick evt) {
152 if (evt.left) {
153 evt.eat();
154 doAction();
158 void onMyEvent (FuiEventKey evt) {
159 if (evt.key == "Space" /*|| evt.key == "Enter"*/) { evt.eat(); doAction(); return; }
160 if (tryHotKey(evt.key)) { evt.eat(); doAction(); return; }
165 // ////////////////////////////////////////////////////////////////////////// //
166 public class FuiRadio : FuiControl {
167 alias onMyEvent = super.onMyEvent;
169 string groupid;
170 int myval;
172 this (FuiControl aparent, string atext, string agroupid, int amyval) {
173 this.connectListeners();
174 super(aparent);
175 groupid = agroupid;
176 myval = amyval;
177 caption = atext;
178 minSize.w = XtWindow.hotStrLen(atext)+4;
179 horizontal = true;
180 aligning = Align.Start;
181 minSize.h = maxSize.h = 1;
182 canBeFocused = true;
183 hotkeyed = true;
184 acceptClick(TtyEvent.MButton.Left);
185 assert(clickMask != 0);
188 override void doAction () {
189 if (canBeFocused) {
190 if (auto desk = getDesk) desk.switchFocusTo(this, false);
192 if (onAction !is null) { onAction(this); return; }
193 if (groupid.length && myval >= 0) {
194 if (auto w = topwindow) {
195 auto nv = w.radio(groupid);
196 if (nv != myval) {
197 w.radio(groupid, myval);
198 (new FuiEventRadioChanged(this, groupid, myval)).post;
204 protected override void drawSelf (XtWindow win) {
205 win.color = (focused ? palColor!"sel"() : disabled ? palColor!"disabled"() : palColor!"def"());
206 auto hotcolor = (focused ? palColor!"hotsel"() : disabled ? palColor!"disabled" : palColor!"hot"());
207 win.fill(0, 0, win.width, win.height);
208 win.writeStrAt(0, 0, "( )");
209 if (myval >= 0) {
210 if (auto w = topwindow) {
211 if (w.radio(groupid) == myval) win.writeCharAt(1, 0, '*');
214 win.writeHotStrAt(4, 0, win.width-4, caption, hotcolor, win.Align.Center, hotkeyed);
215 if (focused) win.gotoXY(1, 0);
218 void onMyEvent (FuiEventClick evt) {
219 if (evt.left) {
220 evt.eat();
221 doAction();
225 void onMyEvent (FuiEventKey evt) {
226 if (evt.key == "Space" /*|| evt.key == "Enter"*/) { evt.eat(); doAction(); return; }
227 if (tryHotKey(evt.key)) { evt.eat(); doAction(); return; }