1 (* Copyright (C) DooM 2D:Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE ../shared/a_modes.inc}
30 // ////////////////////////////////////////////////////////////////////////// //
32 THControlClass
= class of THControl
;
36 type TActionCB
= procedure (me
: THControl
; uinfo
: Integer);
42 mWidth
, mHeight
: Integer;
43 mFrameWidth
, mFrameHeight
: Integer;
46 mChildren
: array of THControl
;
47 mFocused
: THControl
; // valid only for top-level controls
48 mGrab
: THControl
; // valid only for top-level controls
49 mEscClose
: Boolean; // valid only for top-level controls
58 function getEnabled (): Boolean;
59 procedure setEnabled (v
: Boolean); inline;
61 function getFocused (): Boolean; inline;
62 procedure setFocused (v
: Boolean); inline;
64 function isMyChild (ctl
: THControl
): Boolean;
66 function findFirstFocus (): THControl
;
67 function findLastFocus (): THControl
;
69 function findNextFocus (cur
: THControl
): THControl
;
70 function findPrevFocus (cur
: THControl
): THControl
;
72 procedure activated (); virtual;
73 procedure blurred (); virtual;
75 //WARNING! do not call scissor functions outside `.draw*()` API!
76 // set scissor to this rect (in local coords)
77 procedure setScissor (lx
, ly
, lw
, lh
: Integer);
78 // reset scissor to whole control
79 procedure resetScissor (fullArea
: Boolean); inline; // "full area" means "with frame"
82 // set scissor to this rect (in global coords)
83 procedure setScissorGLInternal (x
, y
, w
, h
: Integer);
89 mDefSize
: TLaySize
; // default size
90 mMaxSize
: TLaySize
; // maximum size
99 mLayDefSize
: TLaySize
;
100 mLayMaxSize
: TLaySize
;
103 // layouter interface
104 function getDefSize (): TLaySize
; inline; // default size; <0: use max size
105 //procedure setDefSize (const sz: TLaySize); inline; // default size; <0: use max size
106 function getMargins (): TLayMargins
; inline;
107 function getMaxSize (): TLaySize
; inline; // max size; <0: set to some huge value
108 //procedure setMaxSize (const sz: TLaySize); inline; // max size; <0: set to some huge value
109 function getFlex (): Integer; inline; // <=0: not flexible
110 function isHorizBox (): Boolean; inline; // horizontal layout for children?
111 procedure setHorizBox (v
: Boolean); inline; // horizontal layout for children?
112 function canWrap (): Boolean; inline; // for horizontal boxes: can wrap children? for child: `false` means 'nonbreakable at *next* ctl'
113 procedure setCanWrap (v
: Boolean); inline; // for horizontal boxes: can wrap children? for child: `false` means 'nonbreakable at *next* ctl'
114 function isLineStart (): Boolean; inline; // `true` if this ctl should start a new line; ignored for vertical boxes
115 procedure setLineStart (v
: Boolean); inline; // `true` if this ctl should start a new line; ignored for vertical boxes
116 function getAlign (): Integer; inline; // aligning in non-main direction: <0: left/up; 0: center; >0: right/down
117 procedure setAlign (v
: Integer); inline; // aligning in non-main direction: <0: left/up; 0: center; >0: right/down
118 function getExpand (): Boolean; inline; // expanding in non-main direction: `true` will ignore align and eat all available space
119 procedure setExpand (v
: Boolean); inline; // expanding in non-main direction: `true` will ignore align and eat all available space
120 function getHGroup (): AnsiString; inline; // empty: not grouped
121 procedure setHGroup (const v
: AnsiString); inline; // empty: not grouped
122 function getVGroup (): AnsiString; inline; // empty: not grouped
123 procedure setVGroup (const v
: AnsiString); inline; // empty: not grouped
125 procedure setActualSizePos (constref apos
: TLayPos
; constref asize
: TLaySize
); inline;
127 procedure layPrepare (); virtual; // called before registering control in layouter
130 property flex
: Integer read mFlex write mFlex
;
131 property flDefaultSize
: TLaySize read mDefSize write mDefSize
;
132 property flMaxSize
: TLaySize read mMaxSize write mMaxSize
;
133 property flHoriz
: Boolean read isHorizBox write setHorizBox
;
134 property flCanWrap
: Boolean read canWrap write setCanWrap
;
135 property flLineStart
: Boolean read isLineStart write setLineStart
;
136 property flAlign
: Integer read getAlign write setAlign
;
137 property flExpand
: Boolean read getExpand write setExpand
;
138 property flHGroup
: AnsiString read getHGroup write setHGroup
;
139 property flVGroup
: AnsiString read getVGroup write setVGroup
;
142 function parsePos (par
: TTextParser
): TLayPos
;
143 function parseSize (par
: TTextParser
): TLaySize
;
144 function parseBool (par
: TTextParser
): Boolean;
145 function parseAnyAlign (par
: TTextParser
): Integer;
146 function parseHAlign (par
: TTextParser
): Integer;
147 function parseVAlign (par
: TTextParser
): Integer;
148 procedure parseTextAlign (par
: TTextParser
; var h
, v
: Integer);
149 procedure parseChildren (par
: TTextParser
); // par should be on '{'; final '}' is eaten
152 // par is on property data
153 // there may be more data in text stream, don't eat it!
154 // return `true` if property name is valid and value was parsed
155 // return `false` if property name is invalid; don't advance parser in this case
156 // throw on property data errors
157 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; virtual;
159 // par should be on '{'; final '}' is eaten
160 procedure parseProperties (par
: TTextParser
);
163 constructor Create ();
164 constructor Create (ax
, ay
, aw
, ah
: Integer);
165 destructor Destroy (); override;
167 // `sx` and `sy` are screen coordinates
168 procedure drawControl (gx
, gy
: Integer); virtual;
170 // called after all children drawn
171 procedure drawControlPost (gx
, gy
: Integer); virtual;
173 procedure draw (); virtual;
175 function topLevel (): THControl
; inline;
177 // returns `true` if global coords are inside this control
178 function toLocal (var x
, y
: Integer): Boolean;
179 function toLocal (gx
, gy
: Integer; out x
, y
: Integer): Boolean; inline;
180 procedure toGlobal (var x
, y
: Integer);
181 procedure toGlobal (lx
, ly
: Integer; out x
, y
: Integer); inline;
183 // x and y are global coords
184 function controlAtXY (x
, y
: Integer): THControl
;
186 function mouseEvent (var ev
: THMouseEvent
): Boolean; virtual; // returns `true` if event was eaten
187 function keyEvent (var ev
: THKeyEvent
): Boolean; virtual; // returns `true` if event was eaten
189 function prevSibling (): THControl
;
190 function nextSibling (): THControl
;
191 function firstChild (): THControl
; inline;
192 function lastChild (): THControl
; inline;
194 procedure appendChild (ctl
: THControl
); virtual;
197 property id
: AnsiString read mId
;
198 property x0
: Integer read mX
;
199 property y0
: Integer read mY
;
200 property height
: Integer read mHeight
;
201 property width
: Integer read mWidth
;
202 property enabled
: Boolean read getEnabled write setEnabled
;
203 property parent
: THControl read mParent
;
204 property focused
: Boolean read getFocused write setFocused
;
205 property escClose
: Boolean read mEscClose write mEscClose
;
206 property eatKeys
: Boolean read mEatKeys write mEatKeys
;
210 THTopWindow
= class(THControl
)
214 mDragStartX
, mDragStartY
: Integer;
215 mWaitingClose
: Boolean;
217 mFreeOnClose
: Boolean; // default: false
220 procedure blurred (); override;
223 closeCB
: TActionCB
; // called after window was removed from ui window list
226 constructor Create (const atitle
: AnsiString; ax
, ay
: Integer; aw
: Integer=-1; ah
: Integer=-1);
228 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
230 procedure centerInScreen ();
232 // `sx` and `sy` are screen coordinates
233 procedure drawControl (gx
, gy
: Integer); override;
234 procedure drawControlPost (gx
, gy
: Integer); override;
236 function keyEvent (var ev
: THKeyEvent
): Boolean; override; // returns `true` if event was eaten
237 function mouseEvent (var ev
: THMouseEvent
): Boolean; override; // returns `true` if event was eaten
240 property freeOnClose
: Boolean read mFreeOnClose write mFreeOnClose
;
244 THCtlSimpleText
= class(THControl
)
254 mItems
: array of TItem
;
257 constructor Create (ax
, ay
: Integer);
258 destructor Destroy (); override;
260 procedure appendItem (const atext
: AnsiString; acentered
: Boolean=false; ahline
: Boolean=false);
262 procedure drawControl (gx
, gy
: Integer); override;
264 function mouseEvent (var ev
: THMouseEvent
): Boolean; override;
265 function keyEvent (var ev
: THKeyEvent
): Boolean; override;
269 THCtlCBListBox
= class(THControl
)
279 mItems
: array of TItem
;
283 constructor Create (ax
, ay
: Integer);
284 destructor Destroy (); override;
286 procedure appendItem (const atext
: AnsiString; bv
: PBoolean; aaction
: TActionCB
=nil);
288 procedure drawControl (gx
, gy
: Integer); override;
290 function mouseEvent (var ev
: THMouseEvent
): Boolean; override;
291 function keyEvent (var ev
: THKeyEvent
): Boolean; override;
294 // ////////////////////////////////////////////////////////////////////// //
295 THCtlBox
= class(THControl
)
298 mCaption
: AnsiString;
301 constructor Create (ahoriz
: Boolean);
303 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
305 procedure drawControl (gx
, gy
: Integer); override;
307 function mouseEvent (var ev
: THMouseEvent
): Boolean; override;
308 function keyEvent (var ev
: THKeyEvent
): Boolean; override;
311 THCtlHBox
= class(THCtlBox
)
313 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
316 THCtlVBox
= class(THCtlBox
)
318 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
322 THCtlTextLabel
= class(THControl
)
325 mHAlign
: Integer; // -1: left; 0: center; 1: right; default: left
326 mVAlign
: Integer; // -1: top; 0: center; 1: bottom; default: center
329 constructor Create (const atext
: AnsiString);
330 //destructor Destroy (); override;
332 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
334 procedure drawControl (gx
, gy
: Integer); override;
336 function mouseEvent (var ev
: THMouseEvent
): Boolean; override;
337 function keyEvent (var ev
: THKeyEvent
): Boolean; override;
341 // ////////////////////////////////////////////////////////////////////////// //
342 function uiMouseEvent (ev
: THMouseEvent
): Boolean;
343 function uiKeyEvent (ev
: THKeyEvent
): Boolean;
347 // ////////////////////////////////////////////////////////////////////////// //
348 procedure uiAddWindow (ctl
: THControl
);
349 procedure uiRemoveWindow (ctl
: THControl
); // will free window if `mFreeOnClose` is `true`
350 function uiVisibleWindow (ctl
: THControl
): Boolean;
353 // ////////////////////////////////////////////////////////////////////////// //
355 procedure uiLayoutCtl (ctl
: THControl
);
358 // ////////////////////////////////////////////////////////////////////////// //
360 gh_ui_scale
: Single = 1.0;
370 // ////////////////////////////////////////////////////////////////////////// //
372 knownCtlClasses
: array of record
373 klass
: THControlClass
;
378 procedure registerCtlClass (aklass
: THControlClass
; const aname
: AnsiString);
380 assert(aklass
<> nil);
381 assert(Length(aname
) > 0);
382 SetLength(knownCtlClasses
, Length(knownCtlClasses
)+1);
383 knownCtlClasses
[High(knownCtlClasses
)].klass
:= aklass
;
384 knownCtlClasses
[High(knownCtlClasses
)].name
:= aname
;
388 function findCtlClass (const aname
: AnsiString): THControlClass
;
392 for f
:= 0 to High(knownCtlClasses
) do
394 if (strEquCI1251(aname
, knownCtlClasses
[f
].name
)) then
396 result
:= knownCtlClasses
[f
].klass
;
404 // ////////////////////////////////////////////////////////////////////////// //
406 TFlexLayouter
= specialize TFlexLayouterBase
<THControl
>;
408 procedure uiLayoutCtl (ctl
: THControl
);
412 if (ctl
= nil) then exit
;
413 lay
:= TFlexLayouter
.Create();
418 writeln('============================');
421 writeln('=== initial ===');
424 //lay.calcMaxSizeInternal(0);
427 writeln('=== after first pass ===');
431 writeln('=== after second pass ===');
436 writeln('=== final ===');
445 // ////////////////////////////////////////////////////////////////////////// //
447 uiTopList
: array of THControl
= nil;
450 function uiMouseEvent (ev
: THMouseEvent
): Boolean;
456 ev
.x
:= trunc(ev
.x
/gh_ui_scale
);
457 ev
.y
:= trunc(ev
.y
/gh_ui_scale
);
458 ev
.dx
:= trunc(ev
.dx
/gh_ui_scale
); //FIXME
459 ev
.dy
:= trunc(ev
.dy
/gh_ui_scale
); //FIXME
460 if (Length(uiTopList
) = 0) then result
:= false else result
:= uiTopList
[High(uiTopList
)].mouseEvent(ev
);
461 if not result
and (ev
.press
) then
463 for f
:= High(uiTopList
) downto 0 do
465 if uiTopList
[f
].toLocal(ev
.x
, ev
.y
, lx
, ly
) then
468 if uiTopList
[f
].mEnabled
and (f
<> High(uiTopList
)) then
470 uiTopList
[High(uiTopList
)].blurred();
471 ctmp
:= uiTopList
[f
];
473 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
474 uiTopList
[High(uiTopList
)] := ctmp
;
476 result
:= ctmp
.mouseEvent(ev
);
485 function uiKeyEvent (ev
: THKeyEvent
): Boolean;
487 ev
.x
:= trunc(ev
.x
/gh_ui_scale
);
488 ev
.y
:= trunc(ev
.y
/gh_ui_scale
);
489 if (Length(uiTopList
) = 0) then result
:= false else result
:= uiTopList
[High(uiTopList
)].keyEvent(ev
);
490 if (ev
.release
) then begin result
:= true; exit
; end;
499 glMatrixMode(GL_MODELVIEW
);
503 glScalef(gh_ui_scale
, gh_ui_scale
, 1);
504 for f
:= 0 to High(uiTopList
) do
508 if (f
<> High(uiTopList
)) then darkenRect(ctl
.x0
, ctl
.y0
, ctl
.width
, ctl
.height
, 128);
511 glMatrixMode(GL_MODELVIEW
);
517 procedure uiAddWindow (ctl
: THControl
);
521 if (ctl
= nil) then exit
;
523 if not (ctl
is THTopWindow
) then exit
; // alas
524 for f
:= 0 to High(uiTopList
) do
526 if (uiTopList
[f
] = ctl
) then
528 if (f
<> High(uiTopList
)) then
530 uiTopList
[High(uiTopList
)].blurred();
531 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
532 uiTopList
[High(uiTopList
)] := ctl
;
538 if (Length(uiTopList
) > 0) then uiTopList
[High(uiTopList
)].blurred();
539 SetLength(uiTopList
, Length(uiTopList
)+1);
540 uiTopList
[High(uiTopList
)] := ctl
;
545 procedure uiRemoveWindow (ctl
: THControl
);
549 if (ctl
= nil) then exit
;
551 if not (ctl
is THTopWindow
) then exit
; // alas
552 for f
:= 0 to High(uiTopList
) do
554 if (uiTopList
[f
] = ctl
) then
557 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
558 SetLength(uiTopList
, Length(uiTopList
)-1);
559 if (ctl
is THTopWindow
) then
562 if assigned(THTopWindow(ctl
).closeCB
) then THTopWindow(ctl
).closeCB(ctl
, 0);
564 if (THTopWindow(ctl
).mFreeOnClose
) then FreeAndNil(ctl
);
573 function uiVisibleWindow (ctl
: THControl
): Boolean;
578 if (ctl
= nil) then exit
;
580 if not (ctl
is THTopWindow
) then exit
; // alas
581 for f
:= 0 to High(uiTopList
) do
583 if (uiTopList
[f
] = ctl
) then begin result
:= true; exit
; end;
588 // ////////////////////////////////////////////////////////////////////////// //
589 constructor THControl
.Create ();
606 mDrawShadow
:= false;
608 // layouter interface
609 mDefSize
:= TLaySize
.Create(64, 8); // default size
610 mMaxSize
:= TLaySize
.Create(-1, -1); // maximum size
617 mAlign
:= -1; // left/top
622 constructor THControl
.Create (ax
, ay
, aw
, ah
: Integer);
632 destructor THControl
.Destroy ();
636 if (mParent
<> nil) then
639 for f
:= 0 to High(mParent
.mChildren
) do
641 if (mParent
.mChildren
[f
] = self
) then
643 for c
:= f
+1 to High(mParent
.mChildren
) do mParent
.mChildren
[c
-1] := mParent
.mChildren
[c
];
644 SetLength(mParent
.mChildren
, Length(mParent
.mChildren
)-1);
648 for f
:= 0 to High(mChildren
) do
650 mChildren
[f
].mParent
:= nil;
657 // ////////////////////////////////////////////////////////////////////////// //
658 function THControl
.getDefSize (): TLaySize
; inline; begin result
:= mLayDefSize
; end;
659 function THControl
.getMaxSize (): TLaySize
; inline; begin result
:= mLayMaxSize
; end;
660 function THControl
.getFlex (): Integer; inline; begin result
:= mFlex
; end;
661 function THControl
.isHorizBox (): Boolean; inline; begin result
:= mHoriz
; end;
662 procedure THControl
.setHorizBox (v
: Boolean); inline; begin mHoriz
:= v
; end;
663 function THControl
.canWrap (): Boolean; inline; begin result
:= mCanWrap
; end;
664 procedure THControl
.setCanWrap (v
: Boolean); inline; begin mCanWrap
:= v
; end;
665 function THControl
.isLineStart (): Boolean; inline; begin result
:= mLineStart
; end;
666 procedure THControl
.setLineStart (v
: Boolean); inline; begin mLineStart
:= v
; end;
667 function THControl
.getAlign (): Integer; inline; begin result
:= mAlign
; end;
668 procedure THControl
.setAlign (v
: Integer); inline; begin mAlign
:= v
; end;
669 function THControl
.getExpand (): Boolean; inline; begin result
:= mExpand
; end;
670 procedure THControl
.setExpand (v
: Boolean); inline; begin mExpand
:= v
; end;
671 function THControl
.getHGroup (): AnsiString; inline; begin result
:= mHGroup
; end;
672 procedure THControl
.setHGroup (const v
: AnsiString); inline; begin mHGroup
:= v
; end;
673 function THControl
.getVGroup (): AnsiString; inline; begin result
:= mVGroup
; end;
674 procedure THControl
.setVGroup (const v
: AnsiString); inline; begin mVGroup
:= v
; end;
676 function THControl
.getMargins (): TLayMargins
; inline;
678 result
:= TLayMargins
.Create(mFrameHeight
, mFrameWidth
, mFrameHeight
, mFrameWidth
);
681 procedure THControl
.setActualSizePos (constref apos
: TLayPos
; constref asize
: TLaySize
); inline; begin
682 //writeln(self.className, '; pos=', apos.toString, '; size=', asize.toString);
683 if (mParent
<> nil) then
692 procedure THControl
.layPrepare ();
694 mLayDefSize
:= mDefSize
;
695 mLayMaxSize
:= mMaxSize
;
696 if (mLayMaxSize
.w
>= 0) then mLayMaxSize
.w
+= mFrameWidth
*2;
697 if (mLayMaxSize
.h
>= 0) then mLayMaxSize
.h
+= mFrameHeight
*2;
701 // ////////////////////////////////////////////////////////////////////////// //
702 function THControl
.parsePos (par
: TTextParser
): TLayPos
;
706 if (par
.eatDelim('[')) then ech
:= ']' else par
.expectDelim('(');
707 result
.x
:= par
.expectInt();
708 par
.eatDelim(','); // optional comma
709 result
.y
:= par
.expectInt();
710 par
.eatDelim(','); // optional comma
711 par
.expectDelim(ech
);
714 function THControl
.parseSize (par
: TTextParser
): TLaySize
;
718 if (par
.eatDelim('[')) then ech
:= ']' else par
.expectDelim('(');
719 result
.h
:= par
.expectInt();
720 par
.eatDelim(','); // optional comma
721 result
.w
:= par
.expectInt();
722 par
.eatDelim(','); // optional comma
723 par
.expectDelim(ech
);
726 function THControl
.parseBool (par
: TTextParser
): Boolean;
729 par
.eatIdOrStr('true', false) or
730 par
.eatIdOrStr('yes', false) or
731 par
.eatIdOrStr('tan', false);
734 if (not par
.eatIdOrStr('false', false)) and (not par
.eatIdOrStr('no', false)) and (not par
.eatIdOrStr('ona', false)) then
736 par
.error('boolean value expected');
741 function THControl
.parseAnyAlign (par
: TTextParser
): Integer;
743 if (par
.eatIdOrStr('left', false)) or (par
.eatIdOrStr('top', false)) then result
:= -1
744 else if (par
.eatIdOrStr('right', false)) or (par
.eatIdOrStr('bottom', false)) then result
:= 1
745 else if (par
.eatIdOrStr('center', false)) then result
:= 0
746 else par
.error('invalid align value');
749 function THControl
.parseHAlign (par
: TTextParser
): Integer;
751 if (par
.eatIdOrStr('left', false)) then result
:= -1
752 else if (par
.eatIdOrStr('right', false)) then result
:= 1
753 else if (par
.eatIdOrStr('center', false)) then result
:= 0
754 else par
.error('invalid horizontal align value');
757 function THControl
.parseVAlign (par
: TTextParser
): Integer;
759 if (par
.eatIdOrStr('top', false)) then result
:= -1
760 else if (par
.eatIdOrStr('bottom', false)) then result
:= 1
761 else if (par
.eatIdOrStr('center', false)) then result
:= 0
762 else par
.error('invalid vertical align value');
765 procedure THControl
.parseTextAlign (par
: TTextParser
; var h
, v
: Integer);
767 wasH
: Boolean = false;
768 wasV
: Boolean = false;
772 if (par
.eatIdOrStr('left', false)) then
774 if wasH
then par
.error('too many align directives');
779 if (par
.eatIdOrStr('right', false)) then
781 if wasH
then par
.error('too many align directives');
786 if (par
.eatIdOrStr('hcenter', false)) then
788 if wasH
then par
.error('too many align directives');
793 if (par
.eatIdOrStr('top', false)) then
795 if wasV
then par
.error('too many align directives');
800 if (par
.eatIdOrStr('bottom', false)) then
802 if wasV
then par
.error('too many align directives');
807 if (par
.eatIdOrStr('vcenter', false)) then
809 if wasV
then par
.error('too many align directives');
814 if (par
.eatIdOrStr('center', false)) then
816 if wasV
or wasH
then par
.error('too many align directives');
825 if not wasV
and not wasH
then par
.error('invalid align value');
828 // par should be on '{'; final '}' is eaten
829 procedure THControl
.parseProperties (par
: TTextParser
);
833 if (not par
.eatDelim('{')) then exit
;
834 while (not par
.eatDelim('}')) do
836 if (not par
.isIdOrStr
) then par
.error('property name expected');
839 par
.eatDelim(':'); // optional
840 if not parseProperty(pn
, par
) then par
.errorfmt('invalid property name ''%s''', [pn
]);
841 par
.eatDelim(','); // optional
845 // par should be on '{'
846 procedure THControl
.parseChildren (par
: TTextParser
);
851 par
.expectDelim('{');
852 while (not par
.eatDelim('}')) do
854 if (not par
.isIdOrStr
) then par
.error('control name expected');
855 cc
:= findCtlClass(par
.tokStr
);
856 if (cc
= nil) then par
.errorfmt('unknown control name: ''%s''', [par
.tokStr
]);
857 //writeln('children for <', par.tokStr, '>: <', cc.className, '>');
859 par
.eatDelim(':'); // optional
861 //writeln(' mHoriz=', ctl.mHoriz);
863 ctl
.parseProperties(par
);
868 //writeln(': ', ctl.mDefSize.toString);
870 par
.eatDelim(','); // optional
875 function THControl
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
878 if (strEquCI1251(prname
, 'id')) then begin mId
:= par
.expectStrOrId(true); exit
; end; // allow empty strings
879 if (strEquCI1251(prname
, 'flex')) then begin flex
:= par
.expectInt(); exit
; end;
881 if (strEquCI1251(prname
, 'defsize')) then begin mDefSize
:= parseSize(par
); exit
; end;
882 if (strEquCI1251(prname
, 'maxsize')) then begin mMaxSize
:= parseSize(par
); exit
; end;
883 if (strEquCI1251(prname
, 'wrap')) then begin mCanWrap
:= parseBool(par
); exit
; end;
884 if (strEquCI1251(prname
, 'linestart')) then begin mLineStart
:= parseBool(par
); exit
; end;
885 if (strEquCI1251(prname
, 'expand')) then begin mExpand
:= parseBool(par
); exit
; end;
887 if (strEquCI1251(prname
, 'align')) then begin mAlign
:= parseAnyAlign(par
); exit
; end;
888 if (strEquCI1251(prname
, 'hgroup')) then begin mHGroup
:= par
.expectStrOrId(true); exit
; end; // allow empty strings
889 if (strEquCI1251(prname
, 'vgroup')) then begin mVGroup
:= par
.expectStrOrId(true); exit
; end; // allow empty strings
891 if (strEquCI1251(prname
, 'canfocus')) then begin mCanFocus
:= parseBool(par
); exit
; end;
892 if (strEquCI1251(prname
, 'enabled')) then begin mEnabled
:= parseBool(par
); exit
; end;
893 if (strEquCI1251(prname
, 'disabled')) then begin mEnabled
:= not parseBool(par
); exit
; end;
894 if (strEquCI1251(prname
, 'escclose')) then begin mEscClose
:= not parseBool(par
); exit
; end;
895 if (strEquCI1251(prname
, 'eatkeys')) then begin mEatKeys
:= not parseBool(par
); exit
; end;
900 // ////////////////////////////////////////////////////////////////////////// //
901 procedure THControl
.activated ();
906 procedure THControl
.blurred ();
912 function THControl
.topLevel (): THControl
; inline;
915 while (result
.mParent
<> nil) do result
:= result
.mParent
;
919 function THControl
.getEnabled (): Boolean;
924 if (not mEnabled
) or (mWidth
< 1) or (mHeight
< 1) then exit
;
926 while (ctl
<> nil) do
928 if (not ctl
.mEnabled
) or (ctl
.mWidth
< 1) or (ctl
.mHeight
< 1) then exit
;
935 procedure THControl
.setEnabled (v
: Boolean); inline;
937 if (mEnabled
= v
) then exit
;
939 if not v
and focused
then setFocused(false);
943 function THControl
.getFocused (): Boolean; inline;
945 if (mParent
= nil) then result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = self
) else result
:= (topLevel
.mFocused
= self
);
949 procedure THControl
.setFocused (v
: Boolean); inline;
956 if (tl
.mFocused
= self
) then
959 tl
.mFocused
:= tl
.findNextFocus(self
);
960 if (tl
.mFocused
= self
) then tl
.mFocused
:= nil;
964 if (not mEnabled
) or (not mCanFocus
) then exit
;
965 if (tl
.mFocused
<> self
) then
967 tl
.mFocused
.blurred();
969 if (tl
.mGrab
<> self
) then tl
.mGrab
:= nil;
975 function THControl
.isMyChild (ctl
: THControl
): Boolean;
978 while (ctl
<> nil) do
980 if (ctl
.mParent
= self
) then exit
;
987 // returns `true` if global coords are inside this control
988 function THControl
.toLocal (var x
, y
: Integer): Boolean;
993 while (ctl
<> nil) do
999 result
:= (x
>= 0) and (y
>= 0) and (x
< mWidth
) and (y
< mHeight
);
1002 function THControl
.toLocal (gx
, gy
: Integer; out x
, y
: Integer): Boolean; inline;
1006 result
:= toLocal(x
, y
);
1009 procedure THControl
.toGlobal (var x
, y
: Integer);
1014 while (ctl
<> nil) do
1022 procedure THControl
.toGlobal (lx
, ly
: Integer; out x
, y
: Integer); inline;
1030 // x and y are global coords
1031 function THControl
.controlAtXY (x
, y
: Integer): THControl
;
1037 if (not mEnabled
) or (mWidth
< 1) or (mHeight
< 1) then exit
;
1038 if not toLocal(x
, y
, lx
, ly
) then exit
;
1039 for f
:= High(mChildren
) downto 0 do
1041 result
:= mChildren
[f
].controlAtXY(x
, y
);
1042 if (result
<> nil) then exit
;
1048 function THControl
.prevSibling (): THControl
;
1052 if (mParent
<> nil) then
1054 for f
:= 1 to High(mParent
.mChildren
) do
1056 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
-1]; exit
; end;
1062 function THControl
.nextSibling (): THControl
;
1066 if (mParent
<> nil) then
1068 for f
:= 0 to High(mParent
.mChildren
)-1 do
1070 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
+1]; exit
; end;
1076 function THControl
.firstChild (): THControl
; inline;
1078 if (Length(mChildren
) <> 0) then result
:= mChildren
[0] else result
:= nil;
1081 function THControl
.lastChild (): THControl
; inline;
1083 if (Length(mChildren
) <> 0) then result
:= mChildren
[High(mChildren
)] else result
:= nil;
1087 function THControl
.findFirstFocus (): THControl
;
1094 for f
:= 0 to High(mChildren
) do
1096 result
:= mChildren
[f
].findFirstFocus();
1097 if (result
<> nil) then exit
;
1099 if mCanFocus
then result
:= self
;
1104 function THControl
.findLastFocus (): THControl
;
1111 for f
:= High(mChildren
) downto 0 do
1113 result
:= mChildren
[f
].findLastFocus();
1114 if (result
<> nil) then exit
;
1116 if mCanFocus
then result
:= self
;
1121 function THControl
.findNextFocus (cur
: THControl
): THControl
;
1126 if not isMyChild(cur
) then cur
:= nil;
1127 if (cur
= nil) then begin result
:= findFirstFocus(); exit
; end;
1128 result
:= cur
.findFirstFocus();
1129 if (result
<> nil) and (result
<> cur
) then exit
;
1132 cur
:= cur
.nextSibling
;
1133 if (cur
= nil) then break
;
1134 result
:= cur
.findFirstFocus();
1135 if (result
<> nil) then exit
;
1137 result
:= findFirstFocus();
1142 function THControl
.findPrevFocus (cur
: THControl
): THControl
;
1147 if not isMyChild(cur
) then cur
:= nil;
1148 if (cur
= nil) then begin result
:= findLastFocus(); exit
; end;
1150 result
:= cur
.findLastFocus();
1151 if (result
<> nil) and (result
<> cur
) then exit
;
1154 cur
:= cur
.prevSibling
;
1155 if (cur
= nil) then break
;
1156 result
:= cur
.findLastFocus();
1157 if (result
<> nil) then exit
;
1159 result
:= findLastFocus();
1164 procedure THControl
.appendChild (ctl
: THControl
);
1166 if (ctl
= nil) then exit
;
1167 if (ctl
.mParent
<> nil) then exit
;
1168 SetLength(mChildren
, Length(mChildren
)+1);
1169 mChildren
[High(mChildren
)] := ctl
;
1170 ctl
.mParent
:= self
;
1171 Inc(ctl
.mX
, mFrameWidth
);
1172 Inc(ctl
.mY
, mFrameHeight
);
1173 if (ctl
.mWidth
> 0) and (ctl
.mHeight
> 0) and
1174 (ctl
.mX
+ctl
.mWidth
> mFrameWidth
) and (ctl
.mY
+ctl
.mHeight
> mFrameHeight
) then
1176 if (mWidth
+mFrameWidth
< ctl
.mX
+ctl
.mWidth
) then mWidth
:= ctl
.mX
+ctl
.mWidth
+mFrameWidth
;
1177 if (mHeight
+mFrameHeight
< ctl
.mY
+ctl
.mHeight
) then mHeight
:= ctl
.mY
+ctl
.mHeight
+mFrameHeight
;
1179 if (mFocused
= nil) and ctl
.mEnabled
and ctl
.mCanFocus
and (ctl
.mWidth
> 0) and (ctl
.mHeight
> 0) then mFocused
:= ctl
;
1183 // ////////////////////////////////////////////////////////////////////////// //
1184 procedure THControl
.setScissorGLInternal (x
, y
, w
, h
: Integer);
1186 if not scallowed
then exit
;
1187 x
:= trunc(x
*gh_ui_scale
);
1188 y
:= trunc(y
*gh_ui_scale
);
1189 w
:= trunc(w
*gh_ui_scale
);
1190 h
:= trunc(h
*gh_ui_scale
);
1191 scis
.combineRect(x
, y
, w
, h
);
1194 procedure THControl
.setScissor (lx
, ly
, lw
, lh
: Integer);
1197 //ox, oy, ow, oh: Integer;
1199 if not scallowed
then exit
;
1200 //ox := lx; oy := ly; ow := lw; oh := lh;
1201 if not intersectRect(lx
, ly
, lw
, lh
, 0, 0, mWidth
, mHeight
) then
1203 //writeln('oops: <', self.className, '>: old=(', ox, ',', oy, ')-[', ow, ',', oh, ']');
1204 glScissor(0, 0, 0, 0);
1207 toGlobal(lx
, ly
, gx
, gy
);
1208 setScissorGLInternal(gx
, gy
, lw
, lh
);
1211 procedure THControl
.resetScissor (fullArea
: Boolean); inline;
1213 if not scallowed
then exit
;
1216 setScissor(0, 0, mWidth
, mHeight
);
1220 setScissor(mFrameWidth
, mFrameHeight
, mWidth
-mFrameWidth
*2, mHeight
-mFrameHeight
*2);
1225 // ////////////////////////////////////////////////////////////////////////// //
1226 procedure THControl
.draw ();
1231 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1232 toGlobal(0, 0, gx
, gy
);
1233 //conwritefln('[%s]: (%d,%d)-(%d,%d) (%d,%d)', [ClassName, mX, mY, mWidth, mHeight, x, y]);
1235 scis
.save(true); // scissoring enabled
1238 resetScissor(true); // full area
1239 drawControl(gx
, gy
);
1240 resetScissor(false); // client area
1241 for f
:= 0 to High(mChildren
) do mChildren
[f
].draw();
1242 resetScissor(true); // full area
1243 drawControlPost(gx
, gy
);
1250 procedure THControl
.drawControl (gx
, gy
: Integer);
1252 if (mParent
= nil) then darkenRect(gx
, gy
, mWidth
, mHeight
, 64);
1255 procedure THControl
.drawControlPost (gx
, gy
: Integer);
1258 if mDrawShadow
and (mWidth
> 0) and (mHeight
> 0) then
1260 setScissorGLInternal(gx
+8, gy
+8, mWidth
, mHeight
);
1261 darkenRect(gx
+mWidth
, gy
+8, 8, mHeight
, 128);
1262 darkenRect(gx
+8, gy
+mHeight
, mWidth
-8, 8, 128);
1267 // ////////////////////////////////////////////////////////////////////////// //
1268 function THControl
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1273 if not mEnabled
then exit
;
1274 if (mParent
= nil) then
1276 if (mGrab
<> nil) then
1278 result
:= mGrab
.mouseEvent(ev
);
1279 if (ev
.release
) then mGrab
:= nil;
1283 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1284 ctl
:= controlAtXY(ev
.x
, ev
.y
);
1285 if (ctl
<> nil) and (ctl
<> self
) then
1287 if (ctl
<> topLevel
.mFocused
) then ctl
.setFocused(true);
1288 result
:= ctl
.mouseEvent(ev
);
1290 else if (ctl
= self
) and assigned(actionCB
) then
1297 function THControl
.keyEvent (var ev
: THKeyEvent
): Boolean;
1302 if not mEnabled
then exit
;
1303 if (topLevel
.mFocused
<> self
) and isMyChild(topLevel
.mFocused
) and topLevel
.mFocused
.mEnabled
then result
:= topLevel
.mFocused
.keyEvent(ev
);
1304 if (mParent
= nil) then
1306 if (ev
= 'S-Tab') then
1309 ctl
:= findPrevFocus(mFocused
);
1310 if (ctl
<> mFocused
) then
1317 if (ev
= 'Tab') then
1320 ctl
:= findNextFocus(mFocused
);
1321 if (ctl
<> mFocused
) then
1328 if mEscClose
and (ev
= 'Escape') then
1331 uiRemoveWindow(self
);
1335 if mEatKeys
then result
:= true;
1339 // ////////////////////////////////////////////////////////////////////////// //
1340 constructor THTopWindow
.Create (const atitle
: AnsiString; ax
, ay
: Integer; aw
: Integer=-1; ah
: Integer=-1);
1342 inherited Create(ax
, ay
, aw
, ah
);
1346 if (mWidth
< mFrameWidth
*2+3*8) then mWidth
:= mFrameWidth
*2+3*8;
1347 if (mHeight
< mFrameHeight
*2) then mHeight
:= mFrameHeight
*2;
1348 if (Length(mTitle
) > 0) then
1350 if (mWidth
< Length(mTitle
)*8+mFrameWidth
*2+3*8) then mWidth
:= Length(mTitle
)*8+mFrameWidth
*2+3*8;
1353 mDrawShadow
:= true;
1354 mWaitingClose
:= false;
1360 function THTopWindow
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1362 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
1364 mTitle
:= par
.expectStrOrId(true);
1368 if (strEquCI1251(prname
, 'children')) then
1374 if (strEquCI1251(prname
, 'orientation')) or (strEquCI1251(prname
, 'orient')) then
1376 if (par
.eatIdOrStr('horizontal', false)) or (par
.eatIdOrStr('horiz', false)) then mHoriz
:= true
1377 else if (par
.eatIdOrStr('vertical', false)) or (par
.eatIdOrStr('vert', false)) then mHoriz
:= false
1378 else par
.error('`horizontal` or `vertical` expected');
1382 result
:= inherited parseProperty(prname
, par
);
1386 procedure THTopWindow
.centerInScreen ();
1388 if (mWidth
> 0) and (mHeight
> 0) then
1390 mX
:= trunc((gScrWidth
/gh_ui_scale
-mWidth
)/2);
1391 mY
:= trunc((gScrHeight
/gh_ui_scale
-mHeight
)/2);
1396 procedure THTopWindow
.drawControl (gx
, gy
: Integer);
1398 fillRect(gx
, gy
, mWidth
, mHeight
, 0, 0, 128);
1402 procedure THTopWindow
.drawControlPost (gx
, gy
: Integer);
1411 drawRectUI(mX
+4, mY
+4, mWidth
-8, mHeight
-8, r
, g
, b
);
1415 drawRectUI(mX
+3, mY
+3, mWidth
-6, mHeight
-6, r
, g
, b
);
1416 drawRectUI(mX
+5, mY
+5, mWidth
-10, mHeight
-10, r
, g
, b
);
1417 setScissor(mFrameWidth
, 0, 3*8, 8);
1418 fillRect(mX
+mFrameWidth
, mY
, 3*8, 8, 0, 0, 128);
1419 drawText8(mX
+mFrameWidth
, mY
, '[ ]', r
, g
, b
);
1420 if mInClose
then drawText8(mX
+mFrameWidth
+7, mY
, '#', 0, 255, 0)
1421 else drawText8(mX
+mFrameWidth
+7, mY
, '*', 0, 255, 0);
1423 if (Length(mTitle
) > 0) then
1425 setScissor(mFrameWidth
+3*8, 0, mWidth
-mFrameWidth
*2-3*8, 8);
1426 tx
:= (mX
+3*8)+((mWidth
-3*8)-Length(mTitle
)*8) div 2;
1427 fillRect(tx
-3, mY
, Length(mTitle
)*8+3+2, 8, 0, 0, 128);
1428 drawText8(tx
, mY
, mTitle
, r
, g
, b
);
1430 inherited drawControlPost(gx
, gy
);
1434 procedure THTopWindow
.blurred ();
1437 mWaitingClose
:= false;
1443 function THTopWindow
.keyEvent (var ev
: THKeyEvent
): Boolean;
1445 result
:= inherited keyEvent(ev
);
1446 if not getFocused
then exit
;
1447 if (ev
= 'M-F3') then
1449 uiRemoveWindow(self
);
1456 function THTopWindow
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1461 if not mEnabled
then exit
;
1462 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1466 mX
+= ev
.x
-mDragStartX
;
1467 mY
+= ev
.y
-mDragStartY
;
1468 mDragStartX
:= ev
.x
;
1469 mDragStartY
:= ev
.y
;
1470 if (ev
.release
) then mDragging
:= false;
1475 if toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1481 if (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8) then
1483 //uiRemoveWindow(self);
1484 mWaitingClose
:= true;
1490 mDragStartX
:= ev
.x
;
1491 mDragStartY
:= ev
.y
;
1496 if (lx
< mFrameWidth
) or (lx
>= mWidth
-mFrameWidth
) or (ly
>= mHeight
-mFrameHeight
) then
1499 mDragStartX
:= ev
.x
;
1500 mDragStartY
:= ev
.y
;
1506 if (ev
.release
) then
1508 if mWaitingClose
and (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8) then
1510 uiRemoveWindow(self
);
1514 mWaitingClose
:= false;
1520 if mWaitingClose
then
1522 mInClose
:= (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8);
1531 if (not ev
.motion
) then mWaitingClose
:= false;
1534 result
:= inherited mouseEvent(ev
);
1538 // ////////////////////////////////////////////////////////////////////////// //
1539 constructor THCtlSimpleText
.Create (ax
, ay
: Integer);
1542 inherited Create(ax
, ay
, 4, 4);
1546 destructor THCtlSimpleText
.Destroy ();
1553 procedure THCtlSimpleText
.appendItem (const atext
: AnsiString; acentered
: Boolean=false; ahline
: Boolean=false);
1557 if (Length(atext
)*8+3*8+2 > mWidth
) then mWidth
:= Length(atext
)*8+3*8+2;
1558 SetLength(mItems
, Length(mItems
)+1);
1559 it
:= @mItems
[High(mItems
)];
1561 it
.centered
:= acentered
;
1563 if (Length(mItems
)*8 > mHeight
) then mHeight
:= Length(mItems
)*8;
1567 procedure THCtlSimpleText
.drawControl (gx
, gy
: Integer);
1573 for f
:= 0 to High(mItems
) do
1580 if it
.centered
then begin b
:= 255; tx
:= gx
+(mWidth
-Length(it
.title
)*8) div 2; end;
1584 if (Length(it
.title
) = 0) then
1586 drawHLine(gx
+4, gy
+3, mWidth
-8, r
, g
, b
);
1588 else if (tx
-3 > gx
+4) then
1590 drawHLine(gx
+4, gy
+3, tx
-3-(gx
+3), r
, g
, b
);
1591 drawHLine(tx
+Length(it
.title
)*8, gy
+3, mWidth
-4, r
, g
, b
);
1594 drawText8(tx
, gy
, it
.title
, r
, g
, b
);
1600 function THCtlSimpleText
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1604 result
:= inherited mouseEvent(ev
);
1605 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1612 function THCtlSimpleText
.keyEvent (var ev
: THKeyEvent
): Boolean;
1614 result
:= inherited keyEvent(ev
);
1618 // ////////////////////////////////////////////////////////////////////////// //
1619 constructor THCtlCBListBox
.Create (ax
, ay
: Integer);
1623 inherited Create(ax
, ay
, 4, 4);
1627 destructor THCtlCBListBox
.Destroy ();
1634 procedure THCtlCBListBox
.appendItem (const atext
: AnsiString; bv
: PBoolean; aaction
: TActionCB
=nil);
1638 if (Length(atext
)*8+3*8+2 > mWidth
) then mWidth
:= Length(atext
)*8+3*8+2;
1639 SetLength(mItems
, Length(mItems
)+1);
1640 it
:= @mItems
[High(mItems
)];
1643 it
.actionCB
:= aaction
;
1644 if (Length(mItems
)*8 > mHeight
) then mHeight
:= Length(mItems
)*8;
1645 if (mCurIndex
< 0) then mCurIndex
:= 0;
1649 procedure THCtlCBListBox
.drawControl (gx
, gy
: Integer);
1654 for f
:= 0 to High(mItems
) do
1657 if (mCurIndex
= f
) then fillRect(gx
, gy
, mWidth
, 8, 0, 128, 0);
1658 if (it
.varp
<> nil) then
1660 if it
.varp
^ then drawText8(gx
, gy
, '[x]', 255, 255, 255) else drawText8(gx
, gy
, '[ ]', 255, 255, 255);
1661 drawText8(gx
+3*8+2, gy
, it
.title
, 255, 255, 0);
1663 else if (Length(it
.title
) > 0) then
1665 tx
:= gx
+(mWidth
-Length(it
.title
)*8) div 2;
1666 if (tx
-3 > gx
+4) then
1668 drawHLine(gx
+4, gy
+3, tx
-3-(gx
+3), 255, 255, 255);
1669 drawHLine(tx
+Length(it
.title
)*8, gy
+3, mWidth
-4, 255, 255, 255);
1671 drawText8(tx
, gy
, it
.title
, 255, 255, 255);
1675 drawHLine(gx
+4, gy
+3, mWidth
-8, 255, 255, 255);
1682 function THCtlCBListBox
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1687 result
:= inherited mouseEvent(ev
);
1688 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1691 if (ev
= 'lmb') then
1694 if (ly
>= 0) and (ly
< Length(mItems
)) then
1697 if (it
.varp
<> nil) then
1700 it
.varp
^ := not it
.varp
^;
1701 if assigned(it
.actionCB
) then it
.actionCB(self
, Integer(it
.varp
^));
1702 if assigned(actionCB
) then actionCB(self
, ly
);
1710 function THCtlCBListBox
.keyEvent (var ev
: THKeyEvent
): Boolean;
1714 result
:= inherited keyEvent(ev
);
1715 if not getFocused
then exit
;
1717 if (ev
= 'Home') or (ev
= 'PageUp') then
1722 if (ev
= 'End') or (ev
= 'PageDown') then
1725 mCurIndex
:= High(mItems
);
1730 if (Length(mItems
) > 0) then
1732 if (mCurIndex
< 0) then mCurIndex
:= Length(mItems
);
1733 while (mCurIndex
> 0) do
1736 if (mItems
[mCurIndex
].varp
<> nil) then break
;
1744 if (ev
= 'Down') then
1747 if (Length(mItems
) > 0) then
1749 if (mCurIndex
< 0) then mCurIndex
:= -1;
1750 while (mCurIndex
< High(mItems
)) do
1753 if (mItems
[mCurIndex
].varp
<> nil) then break
;
1761 if (ev
= 'Space') or (ev
= 'Enter') then
1764 if (mCurIndex
>= 0) and (mCurIndex
< Length(mItems
)) and (mItems
[mCurIndex
].varp
<> nil) then
1766 it
:= @mItems
[mCurIndex
];
1767 it
.varp
^ := not it
.varp
^;
1768 if assigned(it
.actionCB
) then it
.actionCB(self
, Integer(it
.varp
^));
1769 if assigned(actionCB
) then actionCB(self
, mCurIndex
);
1775 // ////////////////////////////////////////////////////////////////////////// //
1776 constructor THCtlBox
.Create (ahoriz
: Boolean);
1783 function THCtlBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1785 if (strEquCI1251(prname
, 'orientation')) or (strEquCI1251(prname
, 'orient')) then
1787 if (par
.eatIdOrStr('horizontal', false)) or (par
.eatIdOrStr('horiz', false)) then mHoriz
:= true
1788 else if (par
.eatIdOrStr('vertical', false)) or (par
.eatIdOrStr('vert', false)) then mHoriz
:= false
1789 else par
.error('`horizontal` or `vertical` expected');
1793 if (strEquCI1251(prname
, 'frame')) then
1795 mHasFrame
:= parseBool(par
);
1796 if (mHasFrame
) then begin mFrameWidth
:= 8; mFrameHeight
:= 8; end else begin mFrameWidth
:= 0; mFrameHeight
:= 0; end;
1800 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
1802 mCaption
:= par
.expectStrOrId(true);
1803 mDefSize
:= TLaySize
.Create(Length(mCaption
)*8+2+8*2, 8*2+2);
1807 if (strEquCI1251(prname
, 'children')) then
1813 result
:= inherited parseProperty(prname
, par
);
1817 procedure THCtlBox
.drawControl (gx
, gy
: Integer);
1822 if focused
then begin r
:= 255; g
:= 255; b
:= 255; end else begin r
:= 255; g
:= 255; b
:= 0; end;
1826 drawRectUI(gx
+3, gy
+3, mWidth
-6, mHeight
-6, r
, g
, b
);
1829 if (Length(mCaption
) > 0) then
1831 setScissor(mFrameWidth
+1, 0, mWidth
-mFrameWidth
-2, 8);
1832 tx
:= gx
+((mWidth
-Length(mCaption
)*8) div 2)-1;
1833 if mHasFrame
then fillRect(tx
, gy
, Length(mCaption
)*8+2, 8, 0, 0, 128);
1834 drawText8(tx
+1, gy
, mCaption
, r
, g
, b
);
1839 function THCtlBox
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1843 result
:= inherited mouseEvent(ev
);
1844 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1851 //TODO: navigation with arrow keys, according to box orientation
1852 function THCtlBox
.keyEvent (var ev
: THKeyEvent
): Boolean;
1854 result
:= inherited keyEvent(ev
);
1858 // ////////////////////////////////////////////////////////////////////////// //
1859 procedure THCtlHBox
.AfterConstruction ();
1861 inherited AfterConstruction();
1866 // ////////////////////////////////////////////////////////////////////////// //
1867 procedure THCtlVBox
.AfterConstruction ();
1869 inherited AfterConstruction();
1873 // ////////////////////////////////////////////////////////////////////////// //
1874 constructor THCtlTextLabel
.Create (const atext
: AnsiString);
1880 mDefSize
:= TLaySize
.Create(Length(atext
)*8, 8);
1884 function THCtlTextLabel
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1886 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
1888 mText
:= par
.expectStrOrId(true);
1889 mDefSize
:= TLaySize
.Create(Length(mText
)*8, 8);
1893 if (strEquCI1251(prname
, 'textalign')) then
1895 parseTextAlign(par
, mHAlign
, mVAlign
);
1899 result
:= inherited parseProperty(prname
, par
);
1903 procedure THCtlTextLabel
.drawControl (gx
, gy
: Integer);
1905 xpos
, ypos
: Integer;
1908 fillRect(gx
, gy
, mWidth
, mHeight
, 96, 96, 0);
1909 drawRectUI(gx
, gy
, mWidth
, mHeight
, 96, 96, 96);
1911 if (Length(mText
) > 0) then
1913 if (mHAlign
< 0) then xpos
:= 0
1914 else if (mHAlign
> 0) then xpos
:= mWidth
-Length(mText
)*8
1915 else xpos
:= (mWidth
-Length(mText
)*8) div 2;
1917 if (mVAlign
< 0) then ypos
:= 0
1918 else if (mVAlign
> 0) then ypos
:= mHeight
-8
1919 else ypos
:= (mHeight
-8) div 2;
1921 drawText8(gx
+xpos
, gy
+ypos
, mText
, 255, 255, 255);
1926 function THCtlTextLabel
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1930 result
:= inherited mouseEvent(ev
);
1931 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1938 function THCtlTextLabel
.keyEvent (var ev
: THKeyEvent
): Boolean;
1940 result
:= inherited keyEvent(ev
);
1945 registerCtlClass(THCtlBox
, 'box');
1946 registerCtlClass(THCtlHBox
, 'hbox');
1947 registerCtlClass(THCtlVBox
, 'vbox');
1948 registerCtlClass(THCtlTextLabel
, 'label');