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')) or (strEquCI1251(prname
, 'size')) then begin mDefSize
:= parseSize(par
); exit
; end;
892 if (strEquCI1251(prname
, 'maxsize')) then begin mMaxSize
:= parseSize(par
); exit
; end;
893 if (strEquCI1251(prname
, 'defwidth')) or (strEquCI1251(prname
, 'width')) then begin mDefSize
.w
:= par
.expectInt(); exit
; end;
894 if (strEquCI1251(prname
, 'defheight')) or (strEquCI1251(prname
, 'height')) then begin mDefSize
.h
:= par
.expectInt(); exit
; end;
895 if (strEquCI1251(prname
, 'maxwidth')) then begin mMaxSize
.w
:= par
.expectInt(); exit
; end;
896 if (strEquCI1251(prname
, 'maxheight')) then begin mMaxSize
.h
:= par
.expectInt(); exit
; end;
898 if (strEquCI1251(prname
, 'wrap')) then begin mCanWrap
:= parseBool(par
); exit
; end;
899 if (strEquCI1251(prname
, 'linestart')) then begin mLineStart
:= parseBool(par
); exit
; end;
900 if (strEquCI1251(prname
, 'expand')) then begin mExpand
:= parseBool(par
); exit
; end;
902 if (strEquCI1251(prname
, 'align')) then begin mAlign
:= parseAnyAlign(par
); exit
; end;
903 if (strEquCI1251(prname
, 'hgroup')) then begin mHGroup
:= par
.expectStrOrId(true); exit
; end; // allow empty strings
904 if (strEquCI1251(prname
, 'vgroup')) then begin mVGroup
:= par
.expectStrOrId(true); exit
; end; // allow empty strings
906 if (strEquCI1251(prname
, 'canfocus')) then begin mCanFocus
:= parseBool(par
); exit
; end;
907 if (strEquCI1251(prname
, 'enabled')) then begin mEnabled
:= parseBool(par
); exit
; end;
908 if (strEquCI1251(prname
, 'disabled')) then begin mEnabled
:= not parseBool(par
); exit
; end;
909 if (strEquCI1251(prname
, 'escclose')) then begin mEscClose
:= not parseBool(par
); exit
; end;
910 if (strEquCI1251(prname
, 'eatkeys')) then begin mEatKeys
:= not parseBool(par
); exit
; end;
915 // ////////////////////////////////////////////////////////////////////////// //
916 procedure THControl
.activated ();
921 procedure THControl
.blurred ();
927 function THControl
.topLevel (): THControl
; inline;
930 while (result
.mParent
<> nil) do result
:= result
.mParent
;
934 function THControl
.getEnabled (): Boolean;
939 if (not mEnabled
) or (mWidth
< 1) or (mHeight
< 1) then exit
;
941 while (ctl
<> nil) do
943 if (not ctl
.mEnabled
) or (ctl
.mWidth
< 1) or (ctl
.mHeight
< 1) then exit
;
950 procedure THControl
.setEnabled (v
: Boolean); inline;
952 if (mEnabled
= v
) then exit
;
954 if not v
and focused
then setFocused(false);
958 function THControl
.getFocused (): Boolean; inline;
960 if (mParent
= nil) then result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = self
) else result
:= (topLevel
.mFocused
= self
);
964 procedure THControl
.setFocused (v
: Boolean); inline;
971 if (tl
.mFocused
= self
) then
974 tl
.mFocused
:= tl
.findNextFocus(self
);
975 if (tl
.mFocused
= self
) then tl
.mFocused
:= nil;
979 if (not mEnabled
) or (not mCanFocus
) then exit
;
980 if (tl
.mFocused
<> self
) then
982 tl
.mFocused
.blurred();
984 if (tl
.mGrab
<> self
) then tl
.mGrab
:= nil;
990 function THControl
.isMyChild (ctl
: THControl
): Boolean;
993 while (ctl
<> nil) do
995 if (ctl
.mParent
= self
) then exit
;
1002 // returns `true` if global coords are inside this control
1003 function THControl
.toLocal (var x
, y
: Integer): Boolean;
1008 while (ctl
<> nil) do
1014 result
:= (x
>= 0) and (y
>= 0) and (x
< mWidth
) and (y
< mHeight
);
1017 function THControl
.toLocal (gx
, gy
: Integer; out x
, y
: Integer): Boolean; inline;
1021 result
:= toLocal(x
, y
);
1024 procedure THControl
.toGlobal (var x
, y
: Integer);
1029 while (ctl
<> nil) do
1037 procedure THControl
.toGlobal (lx
, ly
: Integer; out x
, y
: Integer); inline;
1045 // x and y are global coords
1046 function THControl
.controlAtXY (x
, y
: Integer): THControl
;
1052 if (not mEnabled
) or (mWidth
< 1) or (mHeight
< 1) then exit
;
1053 if not toLocal(x
, y
, lx
, ly
) then exit
;
1054 for f
:= High(mChildren
) downto 0 do
1056 result
:= mChildren
[f
].controlAtXY(x
, y
);
1057 if (result
<> nil) then exit
;
1063 function THControl
.prevSibling (): THControl
;
1067 if (mParent
<> nil) then
1069 for f
:= 1 to High(mParent
.mChildren
) do
1071 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
-1]; exit
; end;
1077 function THControl
.nextSibling (): THControl
;
1081 if (mParent
<> nil) then
1083 for f
:= 0 to High(mParent
.mChildren
)-1 do
1085 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
+1]; exit
; end;
1091 function THControl
.firstChild (): THControl
; inline;
1093 if (Length(mChildren
) <> 0) then result
:= mChildren
[0] else result
:= nil;
1096 function THControl
.lastChild (): THControl
; inline;
1098 if (Length(mChildren
) <> 0) then result
:= mChildren
[High(mChildren
)] else result
:= nil;
1102 function THControl
.findFirstFocus (): THControl
;
1109 for f
:= 0 to High(mChildren
) do
1111 result
:= mChildren
[f
].findFirstFocus();
1112 if (result
<> nil) then exit
;
1114 if mCanFocus
then result
:= self
;
1119 function THControl
.findLastFocus (): THControl
;
1126 for f
:= High(mChildren
) downto 0 do
1128 result
:= mChildren
[f
].findLastFocus();
1129 if (result
<> nil) then exit
;
1131 if mCanFocus
then result
:= self
;
1136 function THControl
.findNextFocus (cur
: THControl
): THControl
;
1141 if not isMyChild(cur
) then cur
:= nil;
1142 if (cur
= nil) then begin result
:= findFirstFocus(); exit
; end;
1143 result
:= cur
.findFirstFocus();
1144 if (result
<> nil) and (result
<> cur
) then exit
;
1147 cur
:= cur
.nextSibling
;
1148 if (cur
= nil) then break
;
1149 result
:= cur
.findFirstFocus();
1150 if (result
<> nil) then exit
;
1152 result
:= findFirstFocus();
1157 function THControl
.findPrevFocus (cur
: THControl
): THControl
;
1162 if not isMyChild(cur
) then cur
:= nil;
1163 if (cur
= nil) then begin result
:= findLastFocus(); exit
; end;
1165 result
:= cur
.findLastFocus();
1166 if (result
<> nil) and (result
<> cur
) then exit
;
1169 cur
:= cur
.prevSibling
;
1170 if (cur
= nil) then break
;
1171 result
:= cur
.findLastFocus();
1172 if (result
<> nil) then exit
;
1174 result
:= findLastFocus();
1179 procedure THControl
.appendChild (ctl
: THControl
);
1181 if (ctl
= nil) then exit
;
1182 if (ctl
.mParent
<> nil) then exit
;
1183 SetLength(mChildren
, Length(mChildren
)+1);
1184 mChildren
[High(mChildren
)] := ctl
;
1185 ctl
.mParent
:= self
;
1186 Inc(ctl
.mX
, mFrameWidth
);
1187 Inc(ctl
.mY
, mFrameHeight
);
1188 if (ctl
.mWidth
> 0) and (ctl
.mHeight
> 0) and
1189 (ctl
.mX
+ctl
.mWidth
> mFrameWidth
) and (ctl
.mY
+ctl
.mHeight
> mFrameHeight
) then
1191 if (mWidth
+mFrameWidth
< ctl
.mX
+ctl
.mWidth
) then mWidth
:= ctl
.mX
+ctl
.mWidth
+mFrameWidth
;
1192 if (mHeight
+mFrameHeight
< ctl
.mY
+ctl
.mHeight
) then mHeight
:= ctl
.mY
+ctl
.mHeight
+mFrameHeight
;
1194 if (mFocused
= nil) and ctl
.mEnabled
and ctl
.mCanFocus
and (ctl
.mWidth
> 0) and (ctl
.mHeight
> 0) then mFocused
:= ctl
;
1198 // ////////////////////////////////////////////////////////////////////////// //
1199 procedure THControl
.setScissorGLInternal (x
, y
, w
, h
: Integer);
1201 if not scallowed
then exit
;
1202 x
:= trunc(x
*gh_ui_scale
);
1203 y
:= trunc(y
*gh_ui_scale
);
1204 w
:= trunc(w
*gh_ui_scale
);
1205 h
:= trunc(h
*gh_ui_scale
);
1206 scis
.combineRect(x
, y
, w
, h
);
1209 procedure THControl
.setScissor (lx
, ly
, lw
, lh
: Integer);
1212 //ox, oy, ow, oh: Integer;
1214 if not scallowed
then exit
;
1215 //ox := lx; oy := ly; ow := lw; oh := lh;
1216 if not intersectRect(lx
, ly
, lw
, lh
, 0, 0, mWidth
, mHeight
) then
1218 //writeln('oops: <', self.className, '>: old=(', ox, ',', oy, ')-[', ow, ',', oh, ']');
1219 glScissor(0, 0, 0, 0);
1222 toGlobal(lx
, ly
, gx
, gy
);
1223 setScissorGLInternal(gx
, gy
, lw
, lh
);
1226 procedure THControl
.resetScissor (fullArea
: Boolean); inline;
1228 if not scallowed
then exit
;
1231 setScissor(0, 0, mWidth
, mHeight
);
1235 setScissor(mFrameWidth
, mFrameHeight
, mWidth
-mFrameWidth
*2, mHeight
-mFrameHeight
*2);
1240 // ////////////////////////////////////////////////////////////////////////// //
1241 procedure THControl
.draw ();
1246 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1247 toGlobal(0, 0, gx
, gy
);
1248 //conwritefln('[%s]: (%d,%d)-(%d,%d) (%d,%d)', [ClassName, mX, mY, mWidth, mHeight, x, y]);
1250 scis
.save(true); // scissoring enabled
1253 resetScissor(true); // full area
1254 drawControl(gx
, gy
);
1255 resetScissor(false); // client area
1256 for f
:= 0 to High(mChildren
) do mChildren
[f
].draw();
1257 resetScissor(true); // full area
1258 drawControlPost(gx
, gy
);
1265 procedure THControl
.drawControl (gx
, gy
: Integer);
1267 if (mParent
= nil) then darkenRect(gx
, gy
, mWidth
, mHeight
, 64);
1270 procedure THControl
.drawControlPost (gx
, gy
: Integer);
1273 if mDrawShadow
and (mWidth
> 0) and (mHeight
> 0) then
1275 setScissorGLInternal(gx
+8, gy
+8, mWidth
, mHeight
);
1276 darkenRect(gx
+mWidth
, gy
+8, 8, mHeight
, 128);
1277 darkenRect(gx
+8, gy
+mHeight
, mWidth
-8, 8, 128);
1282 // ////////////////////////////////////////////////////////////////////////// //
1283 function THControl
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1288 if not mEnabled
then exit
;
1289 if (mParent
= nil) then
1291 if (mGrab
<> nil) then
1293 result
:= mGrab
.mouseEvent(ev
);
1294 if (ev
.release
) then mGrab
:= nil;
1298 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1299 ctl
:= controlAtXY(ev
.x
, ev
.y
);
1300 if (ctl
<> nil) and (ctl
<> self
) then
1302 if (ctl
<> topLevel
.mFocused
) then ctl
.setFocused(true);
1303 result
:= ctl
.mouseEvent(ev
);
1305 else if (ctl
= self
) and assigned(actionCB
) then
1312 function THControl
.keyEvent (var ev
: THKeyEvent
): Boolean;
1317 if not mEnabled
then exit
;
1318 if (topLevel
.mFocused
<> self
) and isMyChild(topLevel
.mFocused
) and topLevel
.mFocused
.mEnabled
then result
:= topLevel
.mFocused
.keyEvent(ev
);
1319 if (mParent
= nil) then
1321 if (ev
= 'S-Tab') then
1324 ctl
:= findPrevFocus(mFocused
);
1325 if (ctl
<> mFocused
) then
1332 if (ev
= 'Tab') then
1335 ctl
:= findNextFocus(mFocused
);
1336 if (ctl
<> mFocused
) then
1343 if mEscClose
and (ev
= 'Escape') then
1346 uiRemoveWindow(self
);
1350 if mEatKeys
then result
:= true;
1354 // ////////////////////////////////////////////////////////////////////////// //
1355 constructor THTopWindow
.Create (const atitle
: AnsiString; ax
, ay
: Integer; aw
: Integer=-1; ah
: Integer=-1);
1357 inherited Create(ax
, ay
, aw
, ah
);
1361 if (mWidth
< mFrameWidth
*2+3*8) then mWidth
:= mFrameWidth
*2+3*8;
1362 if (mHeight
< mFrameHeight
*2) then mHeight
:= mFrameHeight
*2;
1363 if (Length(mTitle
) > 0) then
1365 if (mWidth
< Length(mTitle
)*8+mFrameWidth
*2+3*8) then mWidth
:= Length(mTitle
)*8+mFrameWidth
*2+3*8;
1368 mDrawShadow
:= true;
1369 mWaitingClose
:= false;
1375 function THTopWindow
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1377 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
1379 mTitle
:= par
.expectStrOrId(true);
1383 if (strEquCI1251(prname
, 'children')) then
1389 if (strEquCI1251(prname
, 'orientation')) or (strEquCI1251(prname
, 'orient')) then
1391 if (par
.eatIdOrStr('horizontal', false)) or (par
.eatIdOrStr('horiz', false)) then mHoriz
:= true
1392 else if (par
.eatIdOrStr('vertical', false)) or (par
.eatIdOrStr('vert', false)) then mHoriz
:= false
1393 else par
.error('`horizontal` or `vertical` expected');
1397 result
:= inherited parseProperty(prname
, par
);
1401 procedure THTopWindow
.centerInScreen ();
1403 if (mWidth
> 0) and (mHeight
> 0) then
1405 mX
:= trunc((gScrWidth
/gh_ui_scale
-mWidth
)/2);
1406 mY
:= trunc((gScrHeight
/gh_ui_scale
-mHeight
)/2);
1411 procedure THTopWindow
.drawControl (gx
, gy
: Integer);
1413 fillRect(gx
, gy
, mWidth
, mHeight
, 0, 0, 128);
1417 procedure THTopWindow
.drawControlPost (gx
, gy
: Integer);
1426 drawRectUI(mX
+4, mY
+4, mWidth
-8, mHeight
-8, r
, g
, b
);
1430 drawRectUI(mX
+3, mY
+3, mWidth
-6, mHeight
-6, r
, g
, b
);
1431 drawRectUI(mX
+5, mY
+5, mWidth
-10, mHeight
-10, r
, g
, b
);
1432 setScissor(mFrameWidth
, 0, 3*8, 8);
1433 fillRect(mX
+mFrameWidth
, mY
, 3*8, 8, 0, 0, 128);
1434 drawText8(mX
+mFrameWidth
, mY
, '[ ]', r
, g
, b
);
1435 if mInClose
then drawText8(mX
+mFrameWidth
+7, mY
, '#', 0, 255, 0)
1436 else drawText8(mX
+mFrameWidth
+7, mY
, '*', 0, 255, 0);
1438 if (Length(mTitle
) > 0) then
1440 setScissor(mFrameWidth
+3*8, 0, mWidth
-mFrameWidth
*2-3*8, 8);
1441 tx
:= (mX
+3*8)+((mWidth
-3*8)-Length(mTitle
)*8) div 2;
1442 fillRect(tx
-3, mY
, Length(mTitle
)*8+3+2, 8, 0, 0, 128);
1443 drawText8(tx
, mY
, mTitle
, r
, g
, b
);
1445 inherited drawControlPost(gx
, gy
);
1449 procedure THTopWindow
.blurred ();
1452 mWaitingClose
:= false;
1458 function THTopWindow
.keyEvent (var ev
: THKeyEvent
): Boolean;
1460 result
:= inherited keyEvent(ev
);
1461 if not getFocused
then exit
;
1462 if (ev
= 'M-F3') then
1464 uiRemoveWindow(self
);
1471 function THTopWindow
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1476 if not mEnabled
then exit
;
1477 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1481 mX
+= ev
.x
-mDragStartX
;
1482 mY
+= ev
.y
-mDragStartY
;
1483 mDragStartX
:= ev
.x
;
1484 mDragStartY
:= ev
.y
;
1485 if (ev
.release
) then mDragging
:= false;
1490 if toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1496 if (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8) then
1498 //uiRemoveWindow(self);
1499 mWaitingClose
:= true;
1505 mDragStartX
:= ev
.x
;
1506 mDragStartY
:= ev
.y
;
1511 if (lx
< mFrameWidth
) or (lx
>= mWidth
-mFrameWidth
) or (ly
>= mHeight
-mFrameHeight
) then
1514 mDragStartX
:= ev
.x
;
1515 mDragStartY
:= ev
.y
;
1521 if (ev
.release
) then
1523 if mWaitingClose
and (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8) then
1525 uiRemoveWindow(self
);
1529 mWaitingClose
:= false;
1535 if mWaitingClose
then
1537 mInClose
:= (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8);
1546 if (not ev
.motion
) then mWaitingClose
:= false;
1549 result
:= inherited mouseEvent(ev
);
1553 // ////////////////////////////////////////////////////////////////////////// //
1554 constructor THCtlSimpleText
.Create (ax
, ay
: Integer);
1557 inherited Create(ax
, ay
, 4, 4);
1561 destructor THCtlSimpleText
.Destroy ();
1568 procedure THCtlSimpleText
.appendItem (const atext
: AnsiString; acentered
: Boolean=false; ahline
: Boolean=false);
1572 if (Length(atext
)*8+3*8+2 > mWidth
) then mWidth
:= Length(atext
)*8+3*8+2;
1573 SetLength(mItems
, Length(mItems
)+1);
1574 it
:= @mItems
[High(mItems
)];
1576 it
.centered
:= acentered
;
1578 if (Length(mItems
)*8 > mHeight
) then mHeight
:= Length(mItems
)*8;
1582 procedure THCtlSimpleText
.drawControl (gx
, gy
: Integer);
1588 for f
:= 0 to High(mItems
) do
1595 if it
.centered
then begin b
:= 255; tx
:= gx
+(mWidth
-Length(it
.title
)*8) div 2; end;
1599 if (Length(it
.title
) = 0) then
1601 drawHLine(gx
+4, gy
+3, mWidth
-8, r
, g
, b
);
1603 else if (tx
-3 > gx
+4) then
1605 drawHLine(gx
+4, gy
+3, tx
-3-(gx
+3), r
, g
, b
);
1606 drawHLine(tx
+Length(it
.title
)*8, gy
+3, mWidth
-4, r
, g
, b
);
1609 drawText8(tx
, gy
, it
.title
, r
, g
, b
);
1615 function THCtlSimpleText
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1619 result
:= inherited mouseEvent(ev
);
1620 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1627 function THCtlSimpleText
.keyEvent (var ev
: THKeyEvent
): Boolean;
1629 result
:= inherited keyEvent(ev
);
1633 // ////////////////////////////////////////////////////////////////////////// //
1634 constructor THCtlCBListBox
.Create (ax
, ay
: Integer);
1638 inherited Create(ax
, ay
, 4, 4);
1642 destructor THCtlCBListBox
.Destroy ();
1649 procedure THCtlCBListBox
.appendItem (const atext
: AnsiString; bv
: PBoolean; aaction
: TActionCB
=nil);
1653 if (Length(atext
)*8+3*8+2 > mWidth
) then mWidth
:= Length(atext
)*8+3*8+2;
1654 SetLength(mItems
, Length(mItems
)+1);
1655 it
:= @mItems
[High(mItems
)];
1658 it
.actionCB
:= aaction
;
1659 if (Length(mItems
)*8 > mHeight
) then mHeight
:= Length(mItems
)*8;
1660 if (mCurIndex
< 0) then mCurIndex
:= 0;
1664 procedure THCtlCBListBox
.drawControl (gx
, gy
: Integer);
1669 for f
:= 0 to High(mItems
) do
1672 if (mCurIndex
= f
) then fillRect(gx
, gy
, mWidth
, 8, 0, 128, 0);
1673 if (it
.varp
<> nil) then
1675 if it
.varp
^ then drawText8(gx
, gy
, '[x]', 255, 255, 255) else drawText8(gx
, gy
, '[ ]', 255, 255, 255);
1676 drawText8(gx
+3*8+2, gy
, it
.title
, 255, 255, 0);
1678 else if (Length(it
.title
) > 0) then
1680 tx
:= gx
+(mWidth
-Length(it
.title
)*8) div 2;
1681 if (tx
-3 > gx
+4) then
1683 drawHLine(gx
+4, gy
+3, tx
-3-(gx
+3), 255, 255, 255);
1684 drawHLine(tx
+Length(it
.title
)*8, gy
+3, mWidth
-4, 255, 255, 255);
1686 drawText8(tx
, gy
, it
.title
, 255, 255, 255);
1690 drawHLine(gx
+4, gy
+3, mWidth
-8, 255, 255, 255);
1697 function THCtlCBListBox
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1702 result
:= inherited mouseEvent(ev
);
1703 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1706 if (ev
= 'lmb') then
1709 if (ly
>= 0) and (ly
< Length(mItems
)) then
1712 if (it
.varp
<> nil) then
1715 it
.varp
^ := not it
.varp
^;
1716 if assigned(it
.actionCB
) then it
.actionCB(self
, Integer(it
.varp
^));
1717 if assigned(actionCB
) then actionCB(self
, ly
);
1725 function THCtlCBListBox
.keyEvent (var ev
: THKeyEvent
): Boolean;
1729 result
:= inherited keyEvent(ev
);
1730 if not getFocused
then exit
;
1732 if (ev
= 'Home') or (ev
= 'PageUp') then
1737 if (ev
= 'End') or (ev
= 'PageDown') then
1740 mCurIndex
:= High(mItems
);
1745 if (Length(mItems
) > 0) then
1747 if (mCurIndex
< 0) then mCurIndex
:= Length(mItems
);
1748 while (mCurIndex
> 0) do
1751 if (mItems
[mCurIndex
].varp
<> nil) then break
;
1759 if (ev
= 'Down') then
1762 if (Length(mItems
) > 0) then
1764 if (mCurIndex
< 0) then mCurIndex
:= -1;
1765 while (mCurIndex
< High(mItems
)) do
1768 if (mItems
[mCurIndex
].varp
<> nil) then break
;
1776 if (ev
= 'Space') or (ev
= 'Enter') then
1779 if (mCurIndex
>= 0) and (mCurIndex
< Length(mItems
)) and (mItems
[mCurIndex
].varp
<> nil) then
1781 it
:= @mItems
[mCurIndex
];
1782 it
.varp
^ := not it
.varp
^;
1783 if assigned(it
.actionCB
) then it
.actionCB(self
, Integer(it
.varp
^));
1784 if assigned(actionCB
) then actionCB(self
, mCurIndex
);
1790 // ////////////////////////////////////////////////////////////////////////// //
1791 constructor THCtlBox
.Create (ahoriz
: Boolean);
1798 function THCtlBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1800 if (strEquCI1251(prname
, 'orientation')) or (strEquCI1251(prname
, 'orient')) then
1802 if (par
.eatIdOrStr('horizontal', false)) or (par
.eatIdOrStr('horiz', false)) then mHoriz
:= true
1803 else if (par
.eatIdOrStr('vertical', false)) or (par
.eatIdOrStr('vert', false)) then mHoriz
:= false
1804 else par
.error('`horizontal` or `vertical` expected');
1808 if (strEquCI1251(prname
, 'frame')) then
1810 mHasFrame
:= parseBool(par
);
1811 if (mHasFrame
) then begin mFrameWidth
:= 8; mFrameHeight
:= 8; end else begin mFrameWidth
:= 0; mFrameHeight
:= 0; end;
1815 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
1817 mCaption
:= par
.expectStrOrId(true);
1818 mDefSize
:= TLaySize
.Create(Length(mCaption
)*8+2, 8);
1822 if (strEquCI1251(prname
, 'children')) then
1828 result
:= inherited parseProperty(prname
, par
);
1832 procedure THCtlBox
.drawControl (gx
, gy
: Integer);
1837 if focused
then begin r
:= 255; g
:= 255; b
:= 255; end else begin r
:= 255; g
:= 255; b
:= 0; end;
1841 drawRectUI(gx
+3, gy
+3, mWidth
-6, mHeight
-6, r
, g
, b
);
1844 if (Length(mCaption
) > 0) then
1846 setScissor(mFrameWidth
+1, 0, mWidth
-mFrameWidth
-2, 8);
1847 tx
:= gx
+((mWidth
-Length(mCaption
)*8) div 2)-1;
1848 if mHasFrame
then fillRect(tx
, gy
, Length(mCaption
)*8+2, 8, 0, 0, 128);
1849 drawText8(tx
+1, gy
, mCaption
, r
, g
, b
);
1854 function THCtlBox
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1858 result
:= inherited mouseEvent(ev
);
1859 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1866 //TODO: navigation with arrow keys, according to box orientation
1867 function THCtlBox
.keyEvent (var ev
: THKeyEvent
): Boolean;
1869 result
:= inherited keyEvent(ev
);
1873 // ////////////////////////////////////////////////////////////////////////// //
1874 procedure THCtlHBox
.AfterConstruction ();
1876 inherited AfterConstruction();
1881 // ////////////////////////////////////////////////////////////////////////// //
1882 procedure THCtlVBox
.AfterConstruction ();
1884 inherited AfterConstruction();
1889 // ////////////////////////////////////////////////////////////////////////// //
1890 constructor THCtlSpan
.Create ();
1896 function THCtlSpan
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1898 if (strEquCI1251(prname
, 'orientation')) or (strEquCI1251(prname
, 'orient')) then
1900 if (par
.eatIdOrStr('horizontal', false)) or (par
.eatIdOrStr('horiz', false)) then mHoriz
:= true
1901 else if (par
.eatIdOrStr('vertical', false)) or (par
.eatIdOrStr('vert', false)) then mHoriz
:= false
1902 else par
.error('`horizontal` or `vertical` expected');
1906 result
:= inherited parseProperty(prname
, par
);
1909 procedure THCtlSpan
.drawControl (gx
, gy
: Integer);
1914 procedure THCtlSpan
.AfterConstruction ();
1916 inherited AfterConstruction();
1917 //mDefSize := TLaySize.Create(0, 8);
1922 // ////////////////////////////////////////////////////////////////////////// //
1923 constructor THCtlTextLabel
.Create (const atext
: AnsiString);
1929 mDefSize
:= TLaySize
.Create(Length(atext
)*8, 8);
1933 function THCtlTextLabel
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1935 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
1937 mText
:= par
.expectStrOrId(true);
1938 mDefSize
:= TLaySize
.Create(Length(mText
)*8, 8);
1942 if (strEquCI1251(prname
, 'textalign')) then
1944 parseTextAlign(par
, mHAlign
, mVAlign
);
1948 result
:= inherited parseProperty(prname
, par
);
1952 procedure THCtlTextLabel
.drawControl (gx
, gy
: Integer);
1954 xpos
, ypos
: Integer;
1957 fillRect(gx
, gy
, mWidth
, mHeight
, 96, 96, 0);
1958 drawRectUI(gx
, gy
, mWidth
, mHeight
, 96, 96, 96);
1960 if (Length(mText
) > 0) then
1962 if (mHAlign
< 0) then xpos
:= 0
1963 else if (mHAlign
> 0) then xpos
:= mWidth
-Length(mText
)*8
1964 else xpos
:= (mWidth
-Length(mText
)*8) div 2;
1966 if (mVAlign
< 0) then ypos
:= 0
1967 else if (mVAlign
> 0) then ypos
:= mHeight
-8
1968 else ypos
:= (mHeight
-8) div 2;
1970 drawText8(gx
+xpos
, gy
+ypos
, mText
, 255, 255, 255);
1975 function THCtlTextLabel
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1979 result
:= inherited mouseEvent(ev
);
1980 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1987 function THCtlTextLabel
.keyEvent (var ev
: THKeyEvent
): Boolean;
1989 result
:= inherited keyEvent(ev
);
1994 registerCtlClass(THCtlBox
, 'box');
1995 registerCtlClass(THCtlHBox
, 'hbox');
1996 registerCtlClass(THCtlVBox
, 'vbox');
1997 registerCtlClass(THCtlSpan
, 'span');
1998 registerCtlClass(THCtlTextLabel
, 'label');