egra: splitted widgets to package with separate submodules
[iv.d.git] / egra / gui / widgets / labels.d
blob154241c1519e27448185f3e356d98686f7e5865d
1 /*
2 * Simple Framebuffer Gfx/GUI lib
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 module iv.egra.gui.widgets.labels /*is aliced*/;
20 private:
22 import arsd.simpledisplay;
24 import iv.egra.gfx;
26 import iv.alice;
27 import iv.cmdcon;
28 import iv.dynstring;
29 import iv.strex;
30 import iv.utfutil;
32 import iv.egra.gui.subwindows;
33 import iv.egra.gui.widgets.base;
36 // ////////////////////////////////////////////////////////////////////////// //
37 public class LabelWidget : Widget {
38 public:
39 enum HAlign {
40 Left,
41 Center,
42 Right,
45 enum VAlign {
46 Top,
47 Center,
48 Bottom,
51 protected:
52 dynstring mText;
53 int hotxpos;
54 int hotxlen;
56 public:
57 HAlign halign = HAlign.Left;
58 VAlign valign = VAlign.Center;
59 int hpad = 0;
60 int vpad = 0;
62 public:
63 this(T:const(char)[]) (Widget aparent, T atext, HAlign horiz=HAlign.Left, VAlign vert=VAlign.Center) {
64 super(aparent);
65 text = atext;
66 rect.size.w = gxTextWidthUtf(mText);
67 rect.size.h = gxTextHeightUtf;
68 tabStop = false;
69 halign = horiz;
70 valign = vert;
73 this(T:const(char)[]) (T atext, HAlign horiz=HAlign.Left, VAlign vert=VAlign.Center) {
74 assert(creatorCurrentParent !is null);
75 this(creatorCurrentParent, atext, horiz, vert);
78 mixin(WidgetStringPropertyMixin!("text", "mText"));
80 protected void drawLabel (GxRect grect) {
81 if (mText.length == 0) return;
82 int x;
83 final switch (halign) {
84 case HAlign.Left: x = grect.x0+hpad; break;
85 case HAlign.Center: x = grect.x0+(grect.width-gxTextWidthUtf(mText))/2; break;
86 case HAlign.Right: x = grect.x0+grect.width-hpad-gxTextWidthUtf(mText); break;
88 int y;
89 final switch (valign) {
90 case VAlign.Top: y = grect.y0+vpad; break;
91 case VAlign.Center: y = grect.y0+(grect.height-gxTextHeightUtf)/2; break;
92 case VAlign.Bottom: y = grect.y0+grect.height-vpad-gxTextHeightUtf; break;
94 //gxDrawTextUtf(x0+(width-gxTextWidthUtf(mText))/2, y0+(height-gxTextHeightUtf)/2, mText, parent.clrWinText);
95 gxDrawTextUtf(x, y, mText, getColor("text"));
96 //gxDrawTextOutScaledUtf(1, x, y, mText, getColor("text"), gxRGB!(255, 0, 0));
97 //{ import core.stdc.stdio : printf; printf("LBL: 0x%08x\n", getColor("text")); }
98 if (hotxlen > 0) gxHLine(x+hotxpos, y+gxTextUnderLineUtf, hotxlen, getColor("hotline"));
101 protected override void doPaint (GxRect grect) {
102 gxFillRect(grect, getColor("back"));
103 drawLabel(grect);
108 // ////////////////////////////////////////////////////////////////////////// //
109 public class HotLabelWidget : LabelWidget {
110 public:
111 Widget dest; // if `null`, activate next focusable sibling
113 public:
114 this(T:const(char)[]) (Widget aparent, T atext, HAlign horiz=HAlign.Left, VAlign vert=VAlign.Center) {
115 super(aparent, atext, horiz, vert);
116 if (extractHotKey(ref mText, ref mHotkey, ref hotxpos, ref hotxlen)) rect.size.w = gxTextWidthUtf(mText);
119 this(T:const(char)[]) (T atext, HAlign horiz=HAlign.Left, VAlign vert=VAlign.Center) {
120 assert(creatorCurrentParent !is null);
121 this(creatorCurrentParent, atext, horiz, vert);
124 // will be called if `isMyHotkey()` returned `true`
125 // return `true` if some action was done
126 override bool hotkeyActivated () {
127 Widget d = dest;
128 if (d is null && parent !is null) {
129 bool seenSelf = false;
130 Widget top = parent;
131 while (top.parent !is null) top = top.parent;
132 foreach (Widget w; top.allVisualDepth) {
133 if (!seenSelf) {
134 seenSelf = (w is this);
135 } else {
136 if (w.canAcceptFocus()) { d = w; break; }
140 if (d is null || !d.canAcceptFocus()) return false;
141 d.focus();
142 return true;