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
321 // ////////////////////////////////////////////////////////////////////// //
322 THCtlSpan
= class(THControl
)
324 constructor Create ();
326 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
328 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
330 procedure drawControl (gx
, gy
: Integer); override;
333 // ////////////////////////////////////////////////////////////////////// //
334 THCtlTextLabel
= class(THControl
)
337 mHAlign
: Integer; // -1: left; 0: center; 1: right; default: left
338 mVAlign
: Integer; // -1: top; 0: center; 1: bottom; default: center
341 constructor Create (const atext
: AnsiString);
342 //destructor Destroy (); override;
344 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
346 procedure drawControl (gx
, gy
: Integer); override;
348 function mouseEvent (var ev
: THMouseEvent
): Boolean; override;
349 function keyEvent (var ev
: THKeyEvent
): Boolean; override;
353 // ////////////////////////////////////////////////////////////////////////// //
354 function uiMouseEvent (ev
: THMouseEvent
): Boolean;
355 function uiKeyEvent (ev
: THKeyEvent
): Boolean;
359 // ////////////////////////////////////////////////////////////////////////// //
360 procedure uiAddWindow (ctl
: THControl
);
361 procedure uiRemoveWindow (ctl
: THControl
); // will free window if `mFreeOnClose` is `true`
362 function uiVisibleWindow (ctl
: THControl
): Boolean;
365 // ////////////////////////////////////////////////////////////////////////// //
367 procedure uiLayoutCtl (ctl
: THControl
);
370 // ////////////////////////////////////////////////////////////////////////// //
372 gh_ui_scale
: Single = 1.0;
382 // ////////////////////////////////////////////////////////////////////////// //
384 knownCtlClasses
: array of record
385 klass
: THControlClass
;
390 procedure registerCtlClass (aklass
: THControlClass
; const aname
: AnsiString);
392 assert(aklass
<> nil);
393 assert(Length(aname
) > 0);
394 SetLength(knownCtlClasses
, Length(knownCtlClasses
)+1);
395 knownCtlClasses
[High(knownCtlClasses
)].klass
:= aklass
;
396 knownCtlClasses
[High(knownCtlClasses
)].name
:= aname
;
400 function findCtlClass (const aname
: AnsiString): THControlClass
;
404 for f
:= 0 to High(knownCtlClasses
) do
406 if (strEquCI1251(aname
, knownCtlClasses
[f
].name
)) then
408 result
:= knownCtlClasses
[f
].klass
;
416 // ////////////////////////////////////////////////////////////////////////// //
418 TFlexLayouter
= specialize TFlexLayouterBase
<THControl
>;
420 procedure uiLayoutCtl (ctl
: THControl
);
424 if (ctl
= nil) then exit
;
425 lay
:= TFlexLayouter
.Create();
430 //writeln('============================'); lay.dumpFlat();
432 //writeln('=== initial ==='); lay.dump();
434 //lay.calcMaxSizeInternal(0);
437 writeln('=== after first pass ===');
441 writeln('=== after second pass ===');
446 //writeln('=== final ==='); lay.dump();
454 // ////////////////////////////////////////////////////////////////////////// //
456 uiTopList
: array of THControl
= nil;
459 function uiMouseEvent (ev
: THMouseEvent
): Boolean;
465 ev
.x
:= trunc(ev
.x
/gh_ui_scale
);
466 ev
.y
:= trunc(ev
.y
/gh_ui_scale
);
467 ev
.dx
:= trunc(ev
.dx
/gh_ui_scale
); //FIXME
468 ev
.dy
:= trunc(ev
.dy
/gh_ui_scale
); //FIXME
469 if (Length(uiTopList
) = 0) then result
:= false else result
:= uiTopList
[High(uiTopList
)].mouseEvent(ev
);
470 if not result
and (ev
.press
) then
472 for f
:= High(uiTopList
) downto 0 do
474 if uiTopList
[f
].toLocal(ev
.x
, ev
.y
, lx
, ly
) then
477 if uiTopList
[f
].mEnabled
and (f
<> High(uiTopList
)) then
479 uiTopList
[High(uiTopList
)].blurred();
480 ctmp
:= uiTopList
[f
];
482 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
483 uiTopList
[High(uiTopList
)] := ctmp
;
485 result
:= ctmp
.mouseEvent(ev
);
494 function uiKeyEvent (ev
: THKeyEvent
): Boolean;
496 ev
.x
:= trunc(ev
.x
/gh_ui_scale
);
497 ev
.y
:= trunc(ev
.y
/gh_ui_scale
);
498 if (Length(uiTopList
) = 0) then result
:= false else result
:= uiTopList
[High(uiTopList
)].keyEvent(ev
);
499 if (ev
.release
) then begin result
:= true; exit
; end;
508 glMatrixMode(GL_MODELVIEW
);
512 glScalef(gh_ui_scale
, gh_ui_scale
, 1);
513 for f
:= 0 to High(uiTopList
) do
517 if (f
<> High(uiTopList
)) then darkenRect(ctl
.x0
, ctl
.y0
, ctl
.width
, ctl
.height
, 128);
520 glMatrixMode(GL_MODELVIEW
);
526 procedure uiAddWindow (ctl
: THControl
);
530 if (ctl
= nil) then exit
;
532 if not (ctl
is THTopWindow
) then exit
; // alas
533 for f
:= 0 to High(uiTopList
) do
535 if (uiTopList
[f
] = ctl
) then
537 if (f
<> High(uiTopList
)) then
539 uiTopList
[High(uiTopList
)].blurred();
540 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
541 uiTopList
[High(uiTopList
)] := ctl
;
547 if (Length(uiTopList
) > 0) then uiTopList
[High(uiTopList
)].blurred();
548 SetLength(uiTopList
, Length(uiTopList
)+1);
549 uiTopList
[High(uiTopList
)] := ctl
;
554 procedure uiRemoveWindow (ctl
: THControl
);
558 if (ctl
= nil) then exit
;
560 if not (ctl
is THTopWindow
) then exit
; // alas
561 for f
:= 0 to High(uiTopList
) do
563 if (uiTopList
[f
] = ctl
) then
566 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
567 SetLength(uiTopList
, Length(uiTopList
)-1);
568 if (ctl
is THTopWindow
) then
571 if assigned(THTopWindow(ctl
).closeCB
) then THTopWindow(ctl
).closeCB(ctl
, 0);
573 if (THTopWindow(ctl
).mFreeOnClose
) then FreeAndNil(ctl
);
582 function uiVisibleWindow (ctl
: THControl
): Boolean;
587 if (ctl
= nil) then exit
;
589 if not (ctl
is THTopWindow
) then exit
; // alas
590 for f
:= 0 to High(uiTopList
) do
592 if (uiTopList
[f
] = ctl
) then begin result
:= true; exit
; end;
597 // ////////////////////////////////////////////////////////////////////////// //
598 constructor THControl
.Create ();
615 mDrawShadow
:= false;
617 // layouter interface
618 //mDefSize := TLaySize.Create(64, 8); // default size
619 mDefSize
:= TLaySize
.Create(0, 0); // default size
620 mMaxSize
:= TLaySize
.Create(-1, -1); // maximum size
627 mAlign
:= -1; // left/top
632 constructor THControl
.Create (ax
, ay
, aw
, ah
: Integer);
642 destructor THControl
.Destroy ();
646 if (mParent
<> nil) then
649 for f
:= 0 to High(mParent
.mChildren
) do
651 if (mParent
.mChildren
[f
] = self
) then
653 for c
:= f
+1 to High(mParent
.mChildren
) do mParent
.mChildren
[c
-1] := mParent
.mChildren
[c
];
654 SetLength(mParent
.mChildren
, Length(mParent
.mChildren
)-1);
658 for f
:= 0 to High(mChildren
) do
660 mChildren
[f
].mParent
:= nil;
667 // ////////////////////////////////////////////////////////////////////////// //
668 function THControl
.getDefSize (): TLaySize
; inline; begin result
:= mLayDefSize
; end;
669 function THControl
.getMaxSize (): TLaySize
; inline; begin result
:= mLayMaxSize
; end;
670 function THControl
.getFlex (): Integer; inline; begin result
:= mFlex
; end;
671 function THControl
.isHorizBox (): Boolean; inline; begin result
:= mHoriz
; end;
672 procedure THControl
.setHorizBox (v
: Boolean); inline; begin mHoriz
:= v
; end;
673 function THControl
.canWrap (): Boolean; inline; begin result
:= mCanWrap
; end;
674 procedure THControl
.setCanWrap (v
: Boolean); inline; begin mCanWrap
:= v
; end;
675 function THControl
.isLineStart (): Boolean; inline; begin result
:= mLineStart
; end;
676 procedure THControl
.setLineStart (v
: Boolean); inline; begin mLineStart
:= v
; end;
677 function THControl
.getAlign (): Integer; inline; begin result
:= mAlign
; end;
678 procedure THControl
.setAlign (v
: Integer); inline; begin mAlign
:= v
; end;
679 function THControl
.getExpand (): Boolean; inline; begin result
:= mExpand
; end;
680 procedure THControl
.setExpand (v
: Boolean); inline; begin mExpand
:= v
; end;
681 function THControl
.getHGroup (): AnsiString; inline; begin result
:= mHGroup
; end;
682 procedure THControl
.setHGroup (const v
: AnsiString); inline; begin mHGroup
:= v
; end;
683 function THControl
.getVGroup (): AnsiString; inline; begin result
:= mVGroup
; end;
684 procedure THControl
.setVGroup (const v
: AnsiString); inline; begin mVGroup
:= v
; end;
686 function THControl
.getMargins (): TLayMargins
; inline;
688 result
:= TLayMargins
.Create(mFrameHeight
, mFrameWidth
, mFrameHeight
, mFrameWidth
);
691 procedure THControl
.setActualSizePos (constref apos
: TLayPos
; constref asize
: TLaySize
); inline; begin
692 //writeln(self.className, '; pos=', apos.toString, '; size=', asize.toString);
693 if (mParent
<> nil) then
702 procedure THControl
.layPrepare ();
704 mLayDefSize
:= mDefSize
;
705 mLayMaxSize
:= mMaxSize
;
706 if (mLayMaxSize
.w
>= 0) then mLayMaxSize
.w
+= mFrameWidth
*2;
707 if (mLayMaxSize
.h
>= 0) then mLayMaxSize
.h
+= mFrameHeight
*2;
711 // ////////////////////////////////////////////////////////////////////////// //
712 function THControl
.parsePos (par
: TTextParser
): TLayPos
;
716 if (par
.eatDelim('[')) then ech
:= ']' else par
.expectDelim('(');
717 result
.x
:= par
.expectInt();
718 par
.eatDelim(','); // optional comma
719 result
.y
:= par
.expectInt();
720 par
.eatDelim(','); // optional comma
721 par
.expectDelim(ech
);
724 function THControl
.parseSize (par
: TTextParser
): TLaySize
;
728 if (par
.eatDelim('[')) then ech
:= ']' else par
.expectDelim('(');
729 result
.w
:= par
.expectInt();
730 par
.eatDelim(','); // optional comma
731 result
.h
:= par
.expectInt();
732 par
.eatDelim(','); // optional comma
733 par
.expectDelim(ech
);
736 function THControl
.parseBool (par
: TTextParser
): Boolean;
739 par
.eatIdOrStr('true', false) or
740 par
.eatIdOrStr('yes', false) or
741 par
.eatIdOrStr('tan', false);
744 if (not par
.eatIdOrStr('false', false)) and (not par
.eatIdOrStr('no', false)) and (not par
.eatIdOrStr('ona', false)) then
746 par
.error('boolean value expected');
751 function THControl
.parseAnyAlign (par
: TTextParser
): Integer;
753 if (par
.eatIdOrStr('left', false)) or (par
.eatIdOrStr('top', false)) then result
:= -1
754 else if (par
.eatIdOrStr('right', false)) or (par
.eatIdOrStr('bottom', false)) then result
:= 1
755 else if (par
.eatIdOrStr('center', false)) then result
:= 0
756 else par
.error('invalid align value');
759 function THControl
.parseHAlign (par
: TTextParser
): Integer;
761 if (par
.eatIdOrStr('left', false)) then result
:= -1
762 else if (par
.eatIdOrStr('right', false)) then result
:= 1
763 else if (par
.eatIdOrStr('center', false)) then result
:= 0
764 else par
.error('invalid horizontal align value');
767 function THControl
.parseVAlign (par
: TTextParser
): Integer;
769 if (par
.eatIdOrStr('top', false)) then result
:= -1
770 else if (par
.eatIdOrStr('bottom', false)) then result
:= 1
771 else if (par
.eatIdOrStr('center', false)) then result
:= 0
772 else par
.error('invalid vertical align value');
775 procedure THControl
.parseTextAlign (par
: TTextParser
; var h
, v
: Integer);
777 wasH
: Boolean = false;
778 wasV
: Boolean = false;
782 if (par
.eatIdOrStr('left', false)) then
784 if wasH
then par
.error('too many align directives');
789 if (par
.eatIdOrStr('right', false)) then
791 if wasH
then par
.error('too many align directives');
796 if (par
.eatIdOrStr('hcenter', false)) then
798 if wasH
then par
.error('too many align directives');
803 if (par
.eatIdOrStr('top', false)) then
805 if wasV
then par
.error('too many align directives');
810 if (par
.eatIdOrStr('bottom', false)) then
812 if wasV
then par
.error('too many align directives');
817 if (par
.eatIdOrStr('vcenter', false)) then
819 if wasV
then par
.error('too many align directives');
824 if (par
.eatIdOrStr('center', false)) then
826 if wasV
or wasH
then par
.error('too many align directives');
835 if not wasV
and not wasH
then par
.error('invalid align value');
838 // par should be on '{'; final '}' is eaten
839 procedure THControl
.parseProperties (par
: TTextParser
);
843 if (not par
.eatDelim('{')) then exit
;
844 while (not par
.eatDelim('}')) do
846 if (not par
.isIdOrStr
) then par
.error('property name expected');
849 par
.eatDelim(':'); // optional
850 if not parseProperty(pn
, par
) then par
.errorfmt('invalid property name ''%s''', [pn
]);
851 par
.eatDelim(','); // optional
855 // par should be on '{'
856 procedure THControl
.parseChildren (par
: TTextParser
);
861 par
.expectDelim('{');
862 while (not par
.eatDelim('}')) do
864 if (not par
.isIdOrStr
) then par
.error('control name expected');
865 cc
:= findCtlClass(par
.tokStr
);
866 if (cc
= nil) then par
.errorfmt('unknown control name: ''%s''', [par
.tokStr
]);
867 //writeln('children for <', par.tokStr, '>: <', cc.className, '>');
869 par
.eatDelim(':'); // optional
871 //writeln(' mHoriz=', ctl.mHoriz);
873 ctl
.parseProperties(par
);
878 //writeln(': ', ctl.mDefSize.toString);
880 par
.eatDelim(','); // optional
885 function THControl
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
888 if (strEquCI1251(prname
, 'id')) then begin mId
:= par
.expectStrOrId(true); exit
; end; // allow empty strings
889 if (strEquCI1251(prname
, 'flex')) then begin flex
:= par
.expectInt(); exit
; end;
891 if (strEquCI1251(prname
, 'defsize')) then begin mDefSize
:= parseSize(par
); exit
; end;
892 if (strEquCI1251(prname
, 'maxsize')) then begin mMaxSize
:= parseSize(par
); exit
; end;
893 if (strEquCI1251(prname
, 'wrap')) then begin mCanWrap
:= parseBool(par
); exit
; end;
894 if (strEquCI1251(prname
, 'linestart')) then begin mLineStart
:= parseBool(par
); exit
; end;
895 if (strEquCI1251(prname
, 'expand')) then begin mExpand
:= parseBool(par
); exit
; end;
897 if (strEquCI1251(prname
, 'align')) then begin mAlign
:= parseAnyAlign(par
); exit
; end;
898 if (strEquCI1251(prname
, 'hgroup')) then begin mHGroup
:= par
.expectStrOrId(true); exit
; end; // allow empty strings
899 if (strEquCI1251(prname
, 'vgroup')) then begin mVGroup
:= par
.expectStrOrId(true); exit
; end; // allow empty strings
901 if (strEquCI1251(prname
, 'canfocus')) then begin mCanFocus
:= parseBool(par
); exit
; end;
902 if (strEquCI1251(prname
, 'enabled')) then begin mEnabled
:= parseBool(par
); exit
; end;
903 if (strEquCI1251(prname
, 'disabled')) then begin mEnabled
:= not parseBool(par
); exit
; end;
904 if (strEquCI1251(prname
, 'escclose')) then begin mEscClose
:= not parseBool(par
); exit
; end;
905 if (strEquCI1251(prname
, 'eatkeys')) then begin mEatKeys
:= not parseBool(par
); exit
; end;
910 // ////////////////////////////////////////////////////////////////////////// //
911 procedure THControl
.activated ();
916 procedure THControl
.blurred ();
922 function THControl
.topLevel (): THControl
; inline;
925 while (result
.mParent
<> nil) do result
:= result
.mParent
;
929 function THControl
.getEnabled (): Boolean;
934 if (not mEnabled
) or (mWidth
< 1) or (mHeight
< 1) then exit
;
936 while (ctl
<> nil) do
938 if (not ctl
.mEnabled
) or (ctl
.mWidth
< 1) or (ctl
.mHeight
< 1) then exit
;
945 procedure THControl
.setEnabled (v
: Boolean); inline;
947 if (mEnabled
= v
) then exit
;
949 if not v
and focused
then setFocused(false);
953 function THControl
.getFocused (): Boolean; inline;
955 if (mParent
= nil) then result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = self
) else result
:= (topLevel
.mFocused
= self
);
959 procedure THControl
.setFocused (v
: Boolean); inline;
966 if (tl
.mFocused
= self
) then
969 tl
.mFocused
:= tl
.findNextFocus(self
);
970 if (tl
.mFocused
= self
) then tl
.mFocused
:= nil;
974 if (not mEnabled
) or (not mCanFocus
) then exit
;
975 if (tl
.mFocused
<> self
) then
977 tl
.mFocused
.blurred();
979 if (tl
.mGrab
<> self
) then tl
.mGrab
:= nil;
985 function THControl
.isMyChild (ctl
: THControl
): Boolean;
988 while (ctl
<> nil) do
990 if (ctl
.mParent
= self
) then exit
;
997 // returns `true` if global coords are inside this control
998 function THControl
.toLocal (var x
, y
: Integer): Boolean;
1003 while (ctl
<> nil) do
1009 result
:= (x
>= 0) and (y
>= 0) and (x
< mWidth
) and (y
< mHeight
);
1012 function THControl
.toLocal (gx
, gy
: Integer; out x
, y
: Integer): Boolean; inline;
1016 result
:= toLocal(x
, y
);
1019 procedure THControl
.toGlobal (var x
, y
: Integer);
1024 while (ctl
<> nil) do
1032 procedure THControl
.toGlobal (lx
, ly
: Integer; out x
, y
: Integer); inline;
1040 // x and y are global coords
1041 function THControl
.controlAtXY (x
, y
: Integer): THControl
;
1047 if (not mEnabled
) or (mWidth
< 1) or (mHeight
< 1) then exit
;
1048 if not toLocal(x
, y
, lx
, ly
) then exit
;
1049 for f
:= High(mChildren
) downto 0 do
1051 result
:= mChildren
[f
].controlAtXY(x
, y
);
1052 if (result
<> nil) then exit
;
1058 function THControl
.prevSibling (): THControl
;
1062 if (mParent
<> nil) then
1064 for f
:= 1 to High(mParent
.mChildren
) do
1066 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
-1]; exit
; end;
1072 function THControl
.nextSibling (): THControl
;
1076 if (mParent
<> nil) then
1078 for f
:= 0 to High(mParent
.mChildren
)-1 do
1080 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
+1]; exit
; end;
1086 function THControl
.firstChild (): THControl
; inline;
1088 if (Length(mChildren
) <> 0) then result
:= mChildren
[0] else result
:= nil;
1091 function THControl
.lastChild (): THControl
; inline;
1093 if (Length(mChildren
) <> 0) then result
:= mChildren
[High(mChildren
)] else result
:= nil;
1097 function THControl
.findFirstFocus (): THControl
;
1104 for f
:= 0 to High(mChildren
) do
1106 result
:= mChildren
[f
].findFirstFocus();
1107 if (result
<> nil) then exit
;
1109 if mCanFocus
then result
:= self
;
1114 function THControl
.findLastFocus (): THControl
;
1121 for f
:= High(mChildren
) downto 0 do
1123 result
:= mChildren
[f
].findLastFocus();
1124 if (result
<> nil) then exit
;
1126 if mCanFocus
then result
:= self
;
1131 function THControl
.findNextFocus (cur
: THControl
): THControl
;
1136 if not isMyChild(cur
) then cur
:= nil;
1137 if (cur
= nil) then begin result
:= findFirstFocus(); exit
; end;
1138 result
:= cur
.findFirstFocus();
1139 if (result
<> nil) and (result
<> cur
) then exit
;
1142 cur
:= cur
.nextSibling
;
1143 if (cur
= nil) then break
;
1144 result
:= cur
.findFirstFocus();
1145 if (result
<> nil) then exit
;
1147 result
:= findFirstFocus();
1152 function THControl
.findPrevFocus (cur
: THControl
): THControl
;
1157 if not isMyChild(cur
) then cur
:= nil;
1158 if (cur
= nil) then begin result
:= findLastFocus(); exit
; end;
1160 result
:= cur
.findLastFocus();
1161 if (result
<> nil) and (result
<> cur
) then exit
;
1164 cur
:= cur
.prevSibling
;
1165 if (cur
= nil) then break
;
1166 result
:= cur
.findLastFocus();
1167 if (result
<> nil) then exit
;
1169 result
:= findLastFocus();
1174 procedure THControl
.appendChild (ctl
: THControl
);
1176 if (ctl
= nil) then exit
;
1177 if (ctl
.mParent
<> nil) then exit
;
1178 SetLength(mChildren
, Length(mChildren
)+1);
1179 mChildren
[High(mChildren
)] := ctl
;
1180 ctl
.mParent
:= self
;
1181 Inc(ctl
.mX
, mFrameWidth
);
1182 Inc(ctl
.mY
, mFrameHeight
);
1183 if (ctl
.mWidth
> 0) and (ctl
.mHeight
> 0) and
1184 (ctl
.mX
+ctl
.mWidth
> mFrameWidth
) and (ctl
.mY
+ctl
.mHeight
> mFrameHeight
) then
1186 if (mWidth
+mFrameWidth
< ctl
.mX
+ctl
.mWidth
) then mWidth
:= ctl
.mX
+ctl
.mWidth
+mFrameWidth
;
1187 if (mHeight
+mFrameHeight
< ctl
.mY
+ctl
.mHeight
) then mHeight
:= ctl
.mY
+ctl
.mHeight
+mFrameHeight
;
1189 if (mFocused
= nil) and ctl
.mEnabled
and ctl
.mCanFocus
and (ctl
.mWidth
> 0) and (ctl
.mHeight
> 0) then mFocused
:= ctl
;
1193 // ////////////////////////////////////////////////////////////////////////// //
1194 procedure THControl
.setScissorGLInternal (x
, y
, w
, h
: Integer);
1196 if not scallowed
then exit
;
1197 x
:= trunc(x
*gh_ui_scale
);
1198 y
:= trunc(y
*gh_ui_scale
);
1199 w
:= trunc(w
*gh_ui_scale
);
1200 h
:= trunc(h
*gh_ui_scale
);
1201 scis
.combineRect(x
, y
, w
, h
);
1204 procedure THControl
.setScissor (lx
, ly
, lw
, lh
: Integer);
1207 //ox, oy, ow, oh: Integer;
1209 if not scallowed
then exit
;
1210 //ox := lx; oy := ly; ow := lw; oh := lh;
1211 if not intersectRect(lx
, ly
, lw
, lh
, 0, 0, mWidth
, mHeight
) then
1213 //writeln('oops: <', self.className, '>: old=(', ox, ',', oy, ')-[', ow, ',', oh, ']');
1214 glScissor(0, 0, 0, 0);
1217 toGlobal(lx
, ly
, gx
, gy
);
1218 setScissorGLInternal(gx
, gy
, lw
, lh
);
1221 procedure THControl
.resetScissor (fullArea
: Boolean); inline;
1223 if not scallowed
then exit
;
1226 setScissor(0, 0, mWidth
, mHeight
);
1230 setScissor(mFrameWidth
, mFrameHeight
, mWidth
-mFrameWidth
*2, mHeight
-mFrameHeight
*2);
1235 // ////////////////////////////////////////////////////////////////////////// //
1236 procedure THControl
.draw ();
1241 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1242 toGlobal(0, 0, gx
, gy
);
1243 //conwritefln('[%s]: (%d,%d)-(%d,%d) (%d,%d)', [ClassName, mX, mY, mWidth, mHeight, x, y]);
1245 scis
.save(true); // scissoring enabled
1248 resetScissor(true); // full area
1249 drawControl(gx
, gy
);
1250 resetScissor(false); // client area
1251 for f
:= 0 to High(mChildren
) do mChildren
[f
].draw();
1252 resetScissor(true); // full area
1253 drawControlPost(gx
, gy
);
1260 procedure THControl
.drawControl (gx
, gy
: Integer);
1262 if (mParent
= nil) then darkenRect(gx
, gy
, mWidth
, mHeight
, 64);
1265 procedure THControl
.drawControlPost (gx
, gy
: Integer);
1268 if mDrawShadow
and (mWidth
> 0) and (mHeight
> 0) then
1270 setScissorGLInternal(gx
+8, gy
+8, mWidth
, mHeight
);
1271 darkenRect(gx
+mWidth
, gy
+8, 8, mHeight
, 128);
1272 darkenRect(gx
+8, gy
+mHeight
, mWidth
-8, 8, 128);
1277 // ////////////////////////////////////////////////////////////////////////// //
1278 function THControl
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1283 if not mEnabled
then exit
;
1284 if (mParent
= nil) then
1286 if (mGrab
<> nil) then
1288 result
:= mGrab
.mouseEvent(ev
);
1289 if (ev
.release
) then mGrab
:= nil;
1293 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1294 ctl
:= controlAtXY(ev
.x
, ev
.y
);
1295 if (ctl
<> nil) and (ctl
<> self
) then
1297 if (ctl
<> topLevel
.mFocused
) then ctl
.setFocused(true);
1298 result
:= ctl
.mouseEvent(ev
);
1300 else if (ctl
= self
) and assigned(actionCB
) then
1307 function THControl
.keyEvent (var ev
: THKeyEvent
): Boolean;
1312 if not mEnabled
then exit
;
1313 if (topLevel
.mFocused
<> self
) and isMyChild(topLevel
.mFocused
) and topLevel
.mFocused
.mEnabled
then result
:= topLevel
.mFocused
.keyEvent(ev
);
1314 if (mParent
= nil) then
1316 if (ev
= 'S-Tab') then
1319 ctl
:= findPrevFocus(mFocused
);
1320 if (ctl
<> mFocused
) then
1327 if (ev
= 'Tab') then
1330 ctl
:= findNextFocus(mFocused
);
1331 if (ctl
<> mFocused
) then
1338 if mEscClose
and (ev
= 'Escape') then
1341 uiRemoveWindow(self
);
1345 if mEatKeys
then result
:= true;
1349 // ////////////////////////////////////////////////////////////////////////// //
1350 constructor THTopWindow
.Create (const atitle
: AnsiString; ax
, ay
: Integer; aw
: Integer=-1; ah
: Integer=-1);
1352 inherited Create(ax
, ay
, aw
, ah
);
1356 if (mWidth
< mFrameWidth
*2+3*8) then mWidth
:= mFrameWidth
*2+3*8;
1357 if (mHeight
< mFrameHeight
*2) then mHeight
:= mFrameHeight
*2;
1358 if (Length(mTitle
) > 0) then
1360 if (mWidth
< Length(mTitle
)*8+mFrameWidth
*2+3*8) then mWidth
:= Length(mTitle
)*8+mFrameWidth
*2+3*8;
1363 mDrawShadow
:= true;
1364 mWaitingClose
:= false;
1370 function THTopWindow
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1372 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
1374 mTitle
:= par
.expectStrOrId(true);
1378 if (strEquCI1251(prname
, 'children')) then
1384 if (strEquCI1251(prname
, 'orientation')) or (strEquCI1251(prname
, 'orient')) then
1386 if (par
.eatIdOrStr('horizontal', false)) or (par
.eatIdOrStr('horiz', false)) then mHoriz
:= true
1387 else if (par
.eatIdOrStr('vertical', false)) or (par
.eatIdOrStr('vert', false)) then mHoriz
:= false
1388 else par
.error('`horizontal` or `vertical` expected');
1392 result
:= inherited parseProperty(prname
, par
);
1396 procedure THTopWindow
.centerInScreen ();
1398 if (mWidth
> 0) and (mHeight
> 0) then
1400 mX
:= trunc((gScrWidth
/gh_ui_scale
-mWidth
)/2);
1401 mY
:= trunc((gScrHeight
/gh_ui_scale
-mHeight
)/2);
1406 procedure THTopWindow
.drawControl (gx
, gy
: Integer);
1408 fillRect(gx
, gy
, mWidth
, mHeight
, 0, 0, 128);
1412 procedure THTopWindow
.drawControlPost (gx
, gy
: Integer);
1421 drawRectUI(mX
+4, mY
+4, mWidth
-8, mHeight
-8, r
, g
, b
);
1425 drawRectUI(mX
+3, mY
+3, mWidth
-6, mHeight
-6, r
, g
, b
);
1426 drawRectUI(mX
+5, mY
+5, mWidth
-10, mHeight
-10, r
, g
, b
);
1427 setScissor(mFrameWidth
, 0, 3*8, 8);
1428 fillRect(mX
+mFrameWidth
, mY
, 3*8, 8, 0, 0, 128);
1429 drawText8(mX
+mFrameWidth
, mY
, '[ ]', r
, g
, b
);
1430 if mInClose
then drawText8(mX
+mFrameWidth
+7, mY
, '#', 0, 255, 0)
1431 else drawText8(mX
+mFrameWidth
+7, mY
, '*', 0, 255, 0);
1433 if (Length(mTitle
) > 0) then
1435 setScissor(mFrameWidth
+3*8, 0, mWidth
-mFrameWidth
*2-3*8, 8);
1436 tx
:= (mX
+3*8)+((mWidth
-3*8)-Length(mTitle
)*8) div 2;
1437 fillRect(tx
-3, mY
, Length(mTitle
)*8+3+2, 8, 0, 0, 128);
1438 drawText8(tx
, mY
, mTitle
, r
, g
, b
);
1440 inherited drawControlPost(gx
, gy
);
1444 procedure THTopWindow
.blurred ();
1447 mWaitingClose
:= false;
1453 function THTopWindow
.keyEvent (var ev
: THKeyEvent
): Boolean;
1455 result
:= inherited keyEvent(ev
);
1456 if not getFocused
then exit
;
1457 if (ev
= 'M-F3') then
1459 uiRemoveWindow(self
);
1466 function THTopWindow
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1471 if not mEnabled
then exit
;
1472 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1476 mX
+= ev
.x
-mDragStartX
;
1477 mY
+= ev
.y
-mDragStartY
;
1478 mDragStartX
:= ev
.x
;
1479 mDragStartY
:= ev
.y
;
1480 if (ev
.release
) then mDragging
:= false;
1485 if toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1491 if (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8) then
1493 //uiRemoveWindow(self);
1494 mWaitingClose
:= true;
1500 mDragStartX
:= ev
.x
;
1501 mDragStartY
:= ev
.y
;
1506 if (lx
< mFrameWidth
) or (lx
>= mWidth
-mFrameWidth
) or (ly
>= mHeight
-mFrameHeight
) then
1509 mDragStartX
:= ev
.x
;
1510 mDragStartY
:= ev
.y
;
1516 if (ev
.release
) then
1518 if mWaitingClose
and (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8) then
1520 uiRemoveWindow(self
);
1524 mWaitingClose
:= false;
1530 if mWaitingClose
then
1532 mInClose
:= (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8);
1541 if (not ev
.motion
) then mWaitingClose
:= false;
1544 result
:= inherited mouseEvent(ev
);
1548 // ////////////////////////////////////////////////////////////////////////// //
1549 constructor THCtlSimpleText
.Create (ax
, ay
: Integer);
1552 inherited Create(ax
, ay
, 4, 4);
1556 destructor THCtlSimpleText
.Destroy ();
1563 procedure THCtlSimpleText
.appendItem (const atext
: AnsiString; acentered
: Boolean=false; ahline
: Boolean=false);
1567 if (Length(atext
)*8+3*8+2 > mWidth
) then mWidth
:= Length(atext
)*8+3*8+2;
1568 SetLength(mItems
, Length(mItems
)+1);
1569 it
:= @mItems
[High(mItems
)];
1571 it
.centered
:= acentered
;
1573 if (Length(mItems
)*8 > mHeight
) then mHeight
:= Length(mItems
)*8;
1577 procedure THCtlSimpleText
.drawControl (gx
, gy
: Integer);
1583 for f
:= 0 to High(mItems
) do
1590 if it
.centered
then begin b
:= 255; tx
:= gx
+(mWidth
-Length(it
.title
)*8) div 2; end;
1594 if (Length(it
.title
) = 0) then
1596 drawHLine(gx
+4, gy
+3, mWidth
-8, r
, g
, b
);
1598 else if (tx
-3 > gx
+4) then
1600 drawHLine(gx
+4, gy
+3, tx
-3-(gx
+3), r
, g
, b
);
1601 drawHLine(tx
+Length(it
.title
)*8, gy
+3, mWidth
-4, r
, g
, b
);
1604 drawText8(tx
, gy
, it
.title
, r
, g
, b
);
1610 function THCtlSimpleText
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1614 result
:= inherited mouseEvent(ev
);
1615 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1622 function THCtlSimpleText
.keyEvent (var ev
: THKeyEvent
): Boolean;
1624 result
:= inherited keyEvent(ev
);
1628 // ////////////////////////////////////////////////////////////////////////// //
1629 constructor THCtlCBListBox
.Create (ax
, ay
: Integer);
1633 inherited Create(ax
, ay
, 4, 4);
1637 destructor THCtlCBListBox
.Destroy ();
1644 procedure THCtlCBListBox
.appendItem (const atext
: AnsiString; bv
: PBoolean; aaction
: TActionCB
=nil);
1648 if (Length(atext
)*8+3*8+2 > mWidth
) then mWidth
:= Length(atext
)*8+3*8+2;
1649 SetLength(mItems
, Length(mItems
)+1);
1650 it
:= @mItems
[High(mItems
)];
1653 it
.actionCB
:= aaction
;
1654 if (Length(mItems
)*8 > mHeight
) then mHeight
:= Length(mItems
)*8;
1655 if (mCurIndex
< 0) then mCurIndex
:= 0;
1659 procedure THCtlCBListBox
.drawControl (gx
, gy
: Integer);
1664 for f
:= 0 to High(mItems
) do
1667 if (mCurIndex
= f
) then fillRect(gx
, gy
, mWidth
, 8, 0, 128, 0);
1668 if (it
.varp
<> nil) then
1670 if it
.varp
^ then drawText8(gx
, gy
, '[x]', 255, 255, 255) else drawText8(gx
, gy
, '[ ]', 255, 255, 255);
1671 drawText8(gx
+3*8+2, gy
, it
.title
, 255, 255, 0);
1673 else if (Length(it
.title
) > 0) then
1675 tx
:= gx
+(mWidth
-Length(it
.title
)*8) div 2;
1676 if (tx
-3 > gx
+4) then
1678 drawHLine(gx
+4, gy
+3, tx
-3-(gx
+3), 255, 255, 255);
1679 drawHLine(tx
+Length(it
.title
)*8, gy
+3, mWidth
-4, 255, 255, 255);
1681 drawText8(tx
, gy
, it
.title
, 255, 255, 255);
1685 drawHLine(gx
+4, gy
+3, mWidth
-8, 255, 255, 255);
1692 function THCtlCBListBox
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1697 result
:= inherited mouseEvent(ev
);
1698 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1701 if (ev
= 'lmb') then
1704 if (ly
>= 0) and (ly
< Length(mItems
)) then
1707 if (it
.varp
<> nil) then
1710 it
.varp
^ := not it
.varp
^;
1711 if assigned(it
.actionCB
) then it
.actionCB(self
, Integer(it
.varp
^));
1712 if assigned(actionCB
) then actionCB(self
, ly
);
1720 function THCtlCBListBox
.keyEvent (var ev
: THKeyEvent
): Boolean;
1724 result
:= inherited keyEvent(ev
);
1725 if not getFocused
then exit
;
1727 if (ev
= 'Home') or (ev
= 'PageUp') then
1732 if (ev
= 'End') or (ev
= 'PageDown') then
1735 mCurIndex
:= High(mItems
);
1740 if (Length(mItems
) > 0) then
1742 if (mCurIndex
< 0) then mCurIndex
:= Length(mItems
);
1743 while (mCurIndex
> 0) do
1746 if (mItems
[mCurIndex
].varp
<> nil) then break
;
1754 if (ev
= 'Down') then
1757 if (Length(mItems
) > 0) then
1759 if (mCurIndex
< 0) then mCurIndex
:= -1;
1760 while (mCurIndex
< High(mItems
)) do
1763 if (mItems
[mCurIndex
].varp
<> nil) then break
;
1771 if (ev
= 'Space') or (ev
= 'Enter') then
1774 if (mCurIndex
>= 0) and (mCurIndex
< Length(mItems
)) and (mItems
[mCurIndex
].varp
<> nil) then
1776 it
:= @mItems
[mCurIndex
];
1777 it
.varp
^ := not it
.varp
^;
1778 if assigned(it
.actionCB
) then it
.actionCB(self
, Integer(it
.varp
^));
1779 if assigned(actionCB
) then actionCB(self
, mCurIndex
);
1785 // ////////////////////////////////////////////////////////////////////////// //
1786 constructor THCtlBox
.Create (ahoriz
: Boolean);
1793 function THCtlBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1795 if (strEquCI1251(prname
, 'orientation')) or (strEquCI1251(prname
, 'orient')) then
1797 if (par
.eatIdOrStr('horizontal', false)) or (par
.eatIdOrStr('horiz', false)) then mHoriz
:= true
1798 else if (par
.eatIdOrStr('vertical', false)) or (par
.eatIdOrStr('vert', false)) then mHoriz
:= false
1799 else par
.error('`horizontal` or `vertical` expected');
1803 if (strEquCI1251(prname
, 'frame')) then
1805 mHasFrame
:= parseBool(par
);
1806 if (mHasFrame
) then begin mFrameWidth
:= 8; mFrameHeight
:= 8; end else begin mFrameWidth
:= 0; mFrameHeight
:= 0; end;
1810 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
1812 mCaption
:= par
.expectStrOrId(true);
1813 mDefSize
:= TLaySize
.Create(Length(mCaption
)*8+2, 8);
1817 if (strEquCI1251(prname
, 'children')) then
1823 result
:= inherited parseProperty(prname
, par
);
1827 procedure THCtlBox
.drawControl (gx
, gy
: Integer);
1832 if focused
then begin r
:= 255; g
:= 255; b
:= 255; end else begin r
:= 255; g
:= 255; b
:= 0; end;
1836 drawRectUI(gx
+3, gy
+3, mWidth
-6, mHeight
-6, r
, g
, b
);
1839 if (Length(mCaption
) > 0) then
1841 setScissor(mFrameWidth
+1, 0, mWidth
-mFrameWidth
-2, 8);
1842 tx
:= gx
+((mWidth
-Length(mCaption
)*8) div 2)-1;
1843 if mHasFrame
then fillRect(tx
, gy
, Length(mCaption
)*8+2, 8, 0, 0, 128);
1844 drawText8(tx
+1, gy
, mCaption
, r
, g
, b
);
1849 function THCtlBox
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1853 result
:= inherited mouseEvent(ev
);
1854 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1861 //TODO: navigation with arrow keys, according to box orientation
1862 function THCtlBox
.keyEvent (var ev
: THKeyEvent
): Boolean;
1864 result
:= inherited keyEvent(ev
);
1868 // ////////////////////////////////////////////////////////////////////////// //
1869 procedure THCtlHBox
.AfterConstruction ();
1871 inherited AfterConstruction();
1876 // ////////////////////////////////////////////////////////////////////////// //
1877 procedure THCtlVBox
.AfterConstruction ();
1879 inherited AfterConstruction();
1884 // ////////////////////////////////////////////////////////////////////////// //
1885 constructor THCtlSpan
.Create ();
1891 function THCtlSpan
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1893 if (strEquCI1251(prname
, 'orientation')) or (strEquCI1251(prname
, 'orient')) then
1895 if (par
.eatIdOrStr('horizontal', false)) or (par
.eatIdOrStr('horiz', false)) then mHoriz
:= true
1896 else if (par
.eatIdOrStr('vertical', false)) or (par
.eatIdOrStr('vert', false)) then mHoriz
:= false
1897 else par
.error('`horizontal` or `vertical` expected');
1901 result
:= inherited parseProperty(prname
, par
);
1904 procedure THCtlSpan
.drawControl (gx
, gy
: Integer);
1909 procedure THCtlSpan
.AfterConstruction ();
1911 inherited AfterConstruction();
1912 //mDefSize := TLaySize.Create(0, 8);
1917 // ////////////////////////////////////////////////////////////////////////// //
1918 constructor THCtlTextLabel
.Create (const atext
: AnsiString);
1924 mDefSize
:= TLaySize
.Create(Length(atext
)*8, 8);
1928 function THCtlTextLabel
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1930 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
1932 mText
:= par
.expectStrOrId(true);
1933 mDefSize
:= TLaySize
.Create(Length(mText
)*8, 8);
1937 if (strEquCI1251(prname
, 'textalign')) then
1939 parseTextAlign(par
, mHAlign
, mVAlign
);
1943 result
:= inherited parseProperty(prname
, par
);
1947 procedure THCtlTextLabel
.drawControl (gx
, gy
: Integer);
1949 xpos
, ypos
: Integer;
1952 fillRect(gx
, gy
, mWidth
, mHeight
, 96, 96, 0);
1953 drawRectUI(gx
, gy
, mWidth
, mHeight
, 96, 96, 96);
1955 if (Length(mText
) > 0) then
1957 if (mHAlign
< 0) then xpos
:= 0
1958 else if (mHAlign
> 0) then xpos
:= mWidth
-Length(mText
)*8
1959 else xpos
:= (mWidth
-Length(mText
)*8) div 2;
1961 if (mVAlign
< 0) then ypos
:= 0
1962 else if (mVAlign
> 0) then ypos
:= mHeight
-8
1963 else ypos
:= (mHeight
-8) div 2;
1965 drawText8(gx
+xpos
, gy
+ypos
, mText
, 255, 255, 255);
1970 function THCtlTextLabel
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1974 result
:= inherited mouseEvent(ev
);
1975 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1982 function THCtlTextLabel
.keyEvent (var ev
: THKeyEvent
): Boolean;
1984 result
:= inherited keyEvent(ev
);
1989 registerCtlClass(THCtlBox
, 'box');
1990 registerCtlClass(THCtlHBox
, 'hbox');
1991 registerCtlClass(THCtlVBox
, 'vbox');
1992 registerCtlClass(THCtlSpan
, 'span');
1993 registerCtlClass(THCtlTextLabel
, 'label');