1 (* Copyright (C) DooM 2D:Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE ../shared/a_modes.inc}
30 // ////////////////////////////////////////////////////////////////////////// //
32 THControlClass
= class of THControl
;
36 type TActionCB
= procedure (me
: THControl
; uinfo
: Integer);
42 mWidth
, mHeight
: Integer;
43 mFrameWidth
, mFrameHeight
: Integer;
46 mChildren
: array of THControl
;
47 mFocused
: THControl
; // valid only for top-level controls
48 mGrab
: THControl
; // valid only for top-level controls
49 mEscClose
: Boolean; // valid only for top-level controls
58 function getEnabled (): Boolean;
59 procedure setEnabled (v
: Boolean); inline;
61 function getFocused (): Boolean; inline;
62 procedure setFocused (v
: Boolean); inline;
64 function isMyChild (ctl
: THControl
): Boolean;
66 function findFirstFocus (): THControl
;
67 function findLastFocus (): THControl
;
69 function findNextFocus (cur
: THControl
): THControl
;
70 function findPrevFocus (cur
: THControl
): THControl
;
72 procedure activated (); virtual;
73 procedure blurred (); virtual;
75 //WARNING! do not call scissor functions outside `.draw*()` API!
76 // set scissor to this rect (in local coords)
77 procedure setScissor (lx
, ly
, lw
, lh
: Integer);
78 // reset scissor to whole control
79 procedure resetScissor (fullArea
: Boolean); inline; // "full area" means "with frame"
82 // set scissor to this rect (in global coords)
83 procedure setScissorGLInternal (x
, y
, w
, h
: Integer);
89 mDefSize
: TLaySize
; // default size
90 mMaxSize
: TLaySize
; // maximum size
99 mLayDefSize
: TLaySize
;
100 mLayMaxSize
: TLaySize
;
103 // layouter interface
104 function getDefSize (): TLaySize
; inline; // default size; <0: use max size
105 //procedure setDefSize (const sz: TLaySize); inline; // default size; <0: use max size
106 function getMargins (): TLayMargins
; inline;
107 function getMaxSize (): TLaySize
; inline; // max size; <0: set to some huge value
108 //procedure setMaxSize (const sz: TLaySize); inline; // max size; <0: set to some huge value
109 function getFlex (): Integer; inline; // <=0: not flexible
110 function isHorizBox (): Boolean; inline; // horizontal layout for children?
111 procedure setHorizBox (v
: Boolean); inline; // horizontal layout for children?
112 function canWrap (): Boolean; inline; // for horizontal boxes: can wrap children? for child: `false` means 'nonbreakable at *next* ctl'
113 procedure setCanWrap (v
: Boolean); inline; // for horizontal boxes: can wrap children? for child: `false` means 'nonbreakable at *next* ctl'
114 function isLineStart (): Boolean; inline; // `true` if this ctl should start a new line; ignored for vertical boxes
115 procedure setLineStart (v
: Boolean); inline; // `true` if this ctl should start a new line; ignored for vertical boxes
116 function getAlign (): Integer; inline; // aligning in non-main direction: <0: left/up; 0: center; >0: right/down
117 procedure setAlign (v
: Integer); inline; // aligning in non-main direction: <0: left/up; 0: center; >0: right/down
118 function getExpand (): Boolean; inline; // expanding in non-main direction: `true` will ignore align and eat all available space
119 procedure setExpand (v
: Boolean); inline; // expanding in non-main direction: `true` will ignore align and eat all available space
120 function getHGroup (): AnsiString; inline; // empty: not grouped
121 procedure setHGroup (const v
: AnsiString); inline; // empty: not grouped
122 function getVGroup (): AnsiString; inline; // empty: not grouped
123 procedure setVGroup (const v
: AnsiString); inline; // empty: not grouped
125 procedure setActualSizePos (constref apos
: TLayPos
; constref asize
: TLaySize
); inline;
127 procedure layPrepare (); virtual; // called before registering control in layouter
130 property flex
: Integer read mFlex write mFlex
;
131 property flDefaultSize
: TLaySize read mDefSize write mDefSize
;
132 property flMaxSize
: TLaySize read mMaxSize write mMaxSize
;
133 property flHoriz
: Boolean read isHorizBox write setHorizBox
;
134 property flCanWrap
: Boolean read canWrap write setCanWrap
;
135 property flLineStart
: Boolean read isLineStart write setLineStart
;
136 property flAlign
: Integer read getAlign write setAlign
;
137 property flExpand
: Boolean read getExpand write setExpand
;
138 property flHGroup
: AnsiString read getHGroup write setHGroup
;
139 property flVGroup
: AnsiString read getVGroup write setVGroup
;
142 function parsePos (par
: TTextParser
): TLayPos
;
143 function parseSize (par
: TTextParser
): TLaySize
;
144 function parseBool (par
: TTextParser
): Boolean;
145 function parseAnyAlign (par
: TTextParser
): Integer;
146 function parseHAlign (par
: TTextParser
): Integer;
147 function parseVAlign (par
: TTextParser
): Integer;
148 procedure parseTextAlign (par
: TTextParser
; var h
, v
: Integer);
149 procedure parseChildren (par
: TTextParser
); // par should be on '{'; final '}' is eaten
152 // par is on property data
153 // there may be more data in text stream, don't eat it!
154 // return `true` if property name is valid and value was parsed
155 // return `false` if property name is invalid; don't advance parser in this case
156 // throw on property data errors
157 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; virtual;
159 // par should be on '{'; final '}' is eaten
160 procedure parseProperties (par
: TTextParser
);
163 constructor Create ();
164 constructor Create (ax
, ay
, aw
, ah
: Integer);
165 destructor Destroy (); override;
167 // `sx` and `sy` are screen coordinates
168 procedure drawControl (gx
, gy
: Integer); virtual;
170 // called after all children drawn
171 procedure drawControlPost (gx
, gy
: Integer); virtual;
173 procedure draw (); virtual;
175 function topLevel (): THControl
; inline;
177 // returns `true` if global coords are inside this control
178 function toLocal (var x
, y
: Integer): Boolean;
179 function toLocal (gx
, gy
: Integer; out x
, y
: Integer): Boolean; inline;
180 procedure toGlobal (var x
, y
: Integer);
181 procedure toGlobal (lx
, ly
: Integer; out x
, y
: Integer); inline;
183 // x and y are global coords
184 function controlAtXY (x
, y
: Integer): THControl
;
186 function mouseEvent (var ev
: THMouseEvent
): Boolean; virtual; // returns `true` if event was eaten
187 function keyEvent (var ev
: THKeyEvent
): Boolean; virtual; // returns `true` if event was eaten
189 function prevSibling (): THControl
;
190 function nextSibling (): THControl
;
191 function firstChild (): THControl
; inline;
192 function lastChild (): THControl
; inline;
194 procedure appendChild (ctl
: THControl
); virtual;
197 property id
: AnsiString read mId
;
198 property x0
: Integer read mX
;
199 property y0
: Integer read mY
;
200 property height
: Integer read mHeight
;
201 property width
: Integer read mWidth
;
202 property enabled
: Boolean read getEnabled write setEnabled
;
203 property parent
: THControl read mParent
;
204 property focused
: Boolean read getFocused write setFocused
;
205 property escClose
: Boolean read mEscClose write mEscClose
;
206 property eatKeys
: Boolean read mEatKeys write mEatKeys
;
210 THTopWindow
= class(THControl
)
214 mDragStartX
, mDragStartY
: Integer;
215 mWaitingClose
: Boolean;
217 mFreeOnClose
: Boolean; // default: false
220 procedure blurred (); override;
223 closeCB
: TActionCB
; // called after window was removed from ui window list
226 constructor Create (const atitle
: AnsiString; ax
, ay
: Integer; aw
: Integer=-1; ah
: Integer=-1);
228 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
230 procedure centerInScreen ();
232 // `sx` and `sy` are screen coordinates
233 procedure drawControl (gx
, gy
: Integer); override;
234 procedure drawControlPost (gx
, gy
: Integer); override;
236 function keyEvent (var ev
: THKeyEvent
): Boolean; override; // returns `true` if event was eaten
237 function mouseEvent (var ev
: THMouseEvent
): Boolean; override; // returns `true` if event was eaten
240 property freeOnClose
: Boolean read mFreeOnClose write mFreeOnClose
;
244 THCtlSimpleText
= class(THControl
)
254 mItems
: array of TItem
;
257 constructor Create (ax
, ay
: Integer);
258 destructor Destroy (); override;
260 procedure appendItem (const atext
: AnsiString; acentered
: Boolean=false; ahline
: Boolean=false);
262 procedure drawControl (gx
, gy
: Integer); override;
264 function mouseEvent (var ev
: THMouseEvent
): Boolean; override;
265 function keyEvent (var ev
: THKeyEvent
): Boolean; override;
269 THCtlCBListBox
= class(THControl
)
279 mItems
: array of TItem
;
283 constructor Create (ax
, ay
: Integer);
284 destructor Destroy (); override;
286 procedure appendItem (const atext
: AnsiString; bv
: PBoolean; aaction
: TActionCB
=nil);
288 procedure drawControl (gx
, gy
: Integer); override;
290 function mouseEvent (var ev
: THMouseEvent
): Boolean; override;
291 function keyEvent (var ev
: THKeyEvent
): Boolean; override;
294 // ////////////////////////////////////////////////////////////////////// //
295 THCtlBox
= class(THControl
)
298 mCaption
: AnsiString;
301 constructor Create (ahoriz
: Boolean);
303 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
305 procedure drawControl (gx
, gy
: Integer); override;
307 function mouseEvent (var ev
: THMouseEvent
): Boolean; override;
308 function keyEvent (var ev
: THKeyEvent
): Boolean; override;
311 THCtlHBox
= class(THCtlBox
)
313 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
316 THCtlVBox
= class(THCtlBox
)
318 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
322 THCtlTextLabel
= class(THControl
)
325 mHAlign
: Integer; // -1: left; 0: center; 1: right; default: left
326 mVAlign
: Integer; // -1: top; 0: center; 1: bottom; default: center
329 constructor Create (const atext
: AnsiString);
330 //destructor Destroy (); override;
332 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
334 procedure drawControl (gx
, gy
: Integer); override;
336 function mouseEvent (var ev
: THMouseEvent
): Boolean; override;
337 function keyEvent (var ev
: THKeyEvent
): Boolean; override;
341 // ////////////////////////////////////////////////////////////////////////// //
342 function uiMouseEvent (ev
: THMouseEvent
): Boolean;
343 function uiKeyEvent (ev
: THKeyEvent
): Boolean;
347 // ////////////////////////////////////////////////////////////////////////// //
348 procedure uiAddWindow (ctl
: THControl
);
349 procedure uiRemoveWindow (ctl
: THControl
); // will free window if `mFreeOnClose` is `true`
350 function uiVisibleWindow (ctl
: THControl
): Boolean;
353 // ////////////////////////////////////////////////////////////////////////// //
355 procedure uiLayoutCtl (ctl
: THControl
);
358 // ////////////////////////////////////////////////////////////////////////// //
360 gh_ui_scale
: Single = 1.0;
370 // ////////////////////////////////////////////////////////////////////////// //
372 knownCtlClasses
: array of record
373 klass
: THControlClass
;
378 procedure registerCtlClass (aklass
: THControlClass
; const aname
: AnsiString);
380 assert(aklass
<> nil);
381 assert(Length(aname
) > 0);
382 SetLength(knownCtlClasses
, Length(knownCtlClasses
)+1);
383 knownCtlClasses
[High(knownCtlClasses
)].klass
:= aklass
;
384 knownCtlClasses
[High(knownCtlClasses
)].name
:= aname
;
388 function findCtlClass (const aname
: AnsiString): THControlClass
;
392 for f
:= 0 to High(knownCtlClasses
) do
394 if (strEquCI1251(aname
, knownCtlClasses
[f
].name
)) then
396 result
:= knownCtlClasses
[f
].klass
;
404 // ////////////////////////////////////////////////////////////////////////// //
406 TFlexLayouter
= specialize TFlexLayouterBase
<THControl
>;
408 procedure uiLayoutCtl (ctl
: THControl
);
412 if (ctl
= nil) then exit
;
413 lay
:= TFlexLayouter
.Create();
418 //writeln('============================'); lay.dumpFlat();
420 //writeln('=== initial ==='); lay.dump();
422 //lay.calcMaxSizeInternal(0);
425 writeln('=== after first pass ===');
429 writeln('=== after second pass ===');
434 //writeln('=== final ==='); lay.dump();
442 // ////////////////////////////////////////////////////////////////////////// //
444 uiTopList
: array of THControl
= nil;
447 function uiMouseEvent (ev
: THMouseEvent
): Boolean;
453 ev
.x
:= trunc(ev
.x
/gh_ui_scale
);
454 ev
.y
:= trunc(ev
.y
/gh_ui_scale
);
455 ev
.dx
:= trunc(ev
.dx
/gh_ui_scale
); //FIXME
456 ev
.dy
:= trunc(ev
.dy
/gh_ui_scale
); //FIXME
457 if (Length(uiTopList
) = 0) then result
:= false else result
:= uiTopList
[High(uiTopList
)].mouseEvent(ev
);
458 if not result
and (ev
.press
) then
460 for f
:= High(uiTopList
) downto 0 do
462 if uiTopList
[f
].toLocal(ev
.x
, ev
.y
, lx
, ly
) then
465 if uiTopList
[f
].mEnabled
and (f
<> High(uiTopList
)) then
467 uiTopList
[High(uiTopList
)].blurred();
468 ctmp
:= uiTopList
[f
];
470 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
471 uiTopList
[High(uiTopList
)] := ctmp
;
473 result
:= ctmp
.mouseEvent(ev
);
482 function uiKeyEvent (ev
: THKeyEvent
): Boolean;
484 ev
.x
:= trunc(ev
.x
/gh_ui_scale
);
485 ev
.y
:= trunc(ev
.y
/gh_ui_scale
);
486 if (Length(uiTopList
) = 0) then result
:= false else result
:= uiTopList
[High(uiTopList
)].keyEvent(ev
);
487 if (ev
.release
) then begin result
:= true; exit
; end;
496 glMatrixMode(GL_MODELVIEW
);
500 glScalef(gh_ui_scale
, gh_ui_scale
, 1);
501 for f
:= 0 to High(uiTopList
) do
505 if (f
<> High(uiTopList
)) then darkenRect(ctl
.x0
, ctl
.y0
, ctl
.width
, ctl
.height
, 128);
508 glMatrixMode(GL_MODELVIEW
);
514 procedure uiAddWindow (ctl
: THControl
);
518 if (ctl
= nil) then exit
;
520 if not (ctl
is THTopWindow
) then exit
; // alas
521 for f
:= 0 to High(uiTopList
) do
523 if (uiTopList
[f
] = ctl
) then
525 if (f
<> High(uiTopList
)) then
527 uiTopList
[High(uiTopList
)].blurred();
528 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
529 uiTopList
[High(uiTopList
)] := ctl
;
535 if (Length(uiTopList
) > 0) then uiTopList
[High(uiTopList
)].blurred();
536 SetLength(uiTopList
, Length(uiTopList
)+1);
537 uiTopList
[High(uiTopList
)] := ctl
;
542 procedure uiRemoveWindow (ctl
: THControl
);
546 if (ctl
= nil) then exit
;
548 if not (ctl
is THTopWindow
) then exit
; // alas
549 for f
:= 0 to High(uiTopList
) do
551 if (uiTopList
[f
] = ctl
) then
554 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
555 SetLength(uiTopList
, Length(uiTopList
)-1);
556 if (ctl
is THTopWindow
) then
559 if assigned(THTopWindow(ctl
).closeCB
) then THTopWindow(ctl
).closeCB(ctl
, 0);
561 if (THTopWindow(ctl
).mFreeOnClose
) then FreeAndNil(ctl
);
570 function uiVisibleWindow (ctl
: THControl
): Boolean;
575 if (ctl
= nil) then exit
;
577 if not (ctl
is THTopWindow
) then exit
; // alas
578 for f
:= 0 to High(uiTopList
) do
580 if (uiTopList
[f
] = ctl
) then begin result
:= true; exit
; end;
585 // ////////////////////////////////////////////////////////////////////////// //
586 constructor THControl
.Create ();
603 mDrawShadow
:= false;
605 // layouter interface
606 //mDefSize := TLaySize.Create(64, 8); // default size
607 mDefSize
:= TLaySize
.Create(0, 0); // default size
608 mMaxSize
:= TLaySize
.Create(-1, -1); // maximum size
615 mAlign
:= -1; // left/top
620 constructor THControl
.Create (ax
, ay
, aw
, ah
: Integer);
630 destructor THControl
.Destroy ();
634 if (mParent
<> nil) then
637 for f
:= 0 to High(mParent
.mChildren
) do
639 if (mParent
.mChildren
[f
] = self
) then
641 for c
:= f
+1 to High(mParent
.mChildren
) do mParent
.mChildren
[c
-1] := mParent
.mChildren
[c
];
642 SetLength(mParent
.mChildren
, Length(mParent
.mChildren
)-1);
646 for f
:= 0 to High(mChildren
) do
648 mChildren
[f
].mParent
:= nil;
655 // ////////////////////////////////////////////////////////////////////////// //
656 function THControl
.getDefSize (): TLaySize
; inline; begin result
:= mLayDefSize
; end;
657 function THControl
.getMaxSize (): TLaySize
; inline; begin result
:= mLayMaxSize
; end;
658 function THControl
.getFlex (): Integer; inline; begin result
:= mFlex
; end;
659 function THControl
.isHorizBox (): Boolean; inline; begin result
:= mHoriz
; end;
660 procedure THControl
.setHorizBox (v
: Boolean); inline; begin mHoriz
:= v
; end;
661 function THControl
.canWrap (): Boolean; inline; begin result
:= mCanWrap
; end;
662 procedure THControl
.setCanWrap (v
: Boolean); inline; begin mCanWrap
:= v
; end;
663 function THControl
.isLineStart (): Boolean; inline; begin result
:= mLineStart
; end;
664 procedure THControl
.setLineStart (v
: Boolean); inline; begin mLineStart
:= v
; end;
665 function THControl
.getAlign (): Integer; inline; begin result
:= mAlign
; end;
666 procedure THControl
.setAlign (v
: Integer); inline; begin mAlign
:= v
; end;
667 function THControl
.getExpand (): Boolean; inline; begin result
:= mExpand
; end;
668 procedure THControl
.setExpand (v
: Boolean); inline; begin mExpand
:= v
; end;
669 function THControl
.getHGroup (): AnsiString; inline; begin result
:= mHGroup
; end;
670 procedure THControl
.setHGroup (const v
: AnsiString); inline; begin mHGroup
:= v
; end;
671 function THControl
.getVGroup (): AnsiString; inline; begin result
:= mVGroup
; end;
672 procedure THControl
.setVGroup (const v
: AnsiString); inline; begin mVGroup
:= v
; end;
674 function THControl
.getMargins (): TLayMargins
; inline;
676 result
:= TLayMargins
.Create(mFrameHeight
, mFrameWidth
, mFrameHeight
, mFrameWidth
);
679 procedure THControl
.setActualSizePos (constref apos
: TLayPos
; constref asize
: TLaySize
); inline; begin
680 //writeln(self.className, '; pos=', apos.toString, '; size=', asize.toString);
681 if (mParent
<> nil) then
690 procedure THControl
.layPrepare ();
692 mLayDefSize
:= mDefSize
;
693 mLayMaxSize
:= mMaxSize
;
694 if (mLayMaxSize
.w
>= 0) then mLayMaxSize
.w
+= mFrameWidth
*2;
695 if (mLayMaxSize
.h
>= 0) then mLayMaxSize
.h
+= mFrameHeight
*2;
699 // ////////////////////////////////////////////////////////////////////////// //
700 function THControl
.parsePos (par
: TTextParser
): TLayPos
;
704 if (par
.eatDelim('[')) then ech
:= ']' else par
.expectDelim('(');
705 result
.x
:= par
.expectInt();
706 par
.eatDelim(','); // optional comma
707 result
.y
:= par
.expectInt();
708 par
.eatDelim(','); // optional comma
709 par
.expectDelim(ech
);
712 function THControl
.parseSize (par
: TTextParser
): TLaySize
;
716 if (par
.eatDelim('[')) then ech
:= ']' else par
.expectDelim('(');
717 result
.h
:= par
.expectInt();
718 par
.eatDelim(','); // optional comma
719 result
.w
:= par
.expectInt();
720 par
.eatDelim(','); // optional comma
721 par
.expectDelim(ech
);
724 function THControl
.parseBool (par
: TTextParser
): Boolean;
727 par
.eatIdOrStr('true', false) or
728 par
.eatIdOrStr('yes', false) or
729 par
.eatIdOrStr('tan', false);
732 if (not par
.eatIdOrStr('false', false)) and (not par
.eatIdOrStr('no', false)) and (not par
.eatIdOrStr('ona', false)) then
734 par
.error('boolean value expected');
739 function THControl
.parseAnyAlign (par
: TTextParser
): Integer;
741 if (par
.eatIdOrStr('left', false)) or (par
.eatIdOrStr('top', false)) then result
:= -1
742 else if (par
.eatIdOrStr('right', false)) or (par
.eatIdOrStr('bottom', false)) then result
:= 1
743 else if (par
.eatIdOrStr('center', false)) then result
:= 0
744 else par
.error('invalid align value');
747 function THControl
.parseHAlign (par
: TTextParser
): Integer;
749 if (par
.eatIdOrStr('left', false)) then result
:= -1
750 else if (par
.eatIdOrStr('right', false)) then result
:= 1
751 else if (par
.eatIdOrStr('center', false)) then result
:= 0
752 else par
.error('invalid horizontal align value');
755 function THControl
.parseVAlign (par
: TTextParser
): Integer;
757 if (par
.eatIdOrStr('top', false)) then result
:= -1
758 else if (par
.eatIdOrStr('bottom', false)) then result
:= 1
759 else if (par
.eatIdOrStr('center', false)) then result
:= 0
760 else par
.error('invalid vertical align value');
763 procedure THControl
.parseTextAlign (par
: TTextParser
; var h
, v
: Integer);
765 wasH
: Boolean = false;
766 wasV
: Boolean = false;
770 if (par
.eatIdOrStr('left', false)) then
772 if wasH
then par
.error('too many align directives');
777 if (par
.eatIdOrStr('right', false)) then
779 if wasH
then par
.error('too many align directives');
784 if (par
.eatIdOrStr('hcenter', false)) then
786 if wasH
then par
.error('too many align directives');
791 if (par
.eatIdOrStr('top', false)) then
793 if wasV
then par
.error('too many align directives');
798 if (par
.eatIdOrStr('bottom', false)) then
800 if wasV
then par
.error('too many align directives');
805 if (par
.eatIdOrStr('vcenter', false)) then
807 if wasV
then par
.error('too many align directives');
812 if (par
.eatIdOrStr('center', false)) then
814 if wasV
or wasH
then par
.error('too many align directives');
823 if not wasV
and not wasH
then par
.error('invalid align value');
826 // par should be on '{'; final '}' is eaten
827 procedure THControl
.parseProperties (par
: TTextParser
);
831 if (not par
.eatDelim('{')) then exit
;
832 while (not par
.eatDelim('}')) do
834 if (not par
.isIdOrStr
) then par
.error('property name expected');
837 par
.eatDelim(':'); // optional
838 if not parseProperty(pn
, par
) then par
.errorfmt('invalid property name ''%s''', [pn
]);
839 par
.eatDelim(','); // optional
843 // par should be on '{'
844 procedure THControl
.parseChildren (par
: TTextParser
);
849 par
.expectDelim('{');
850 while (not par
.eatDelim('}')) do
852 if (not par
.isIdOrStr
) then par
.error('control name expected');
853 cc
:= findCtlClass(par
.tokStr
);
854 if (cc
= nil) then par
.errorfmt('unknown control name: ''%s''', [par
.tokStr
]);
855 //writeln('children for <', par.tokStr, '>: <', cc.className, '>');
857 par
.eatDelim(':'); // optional
859 //writeln(' mHoriz=', ctl.mHoriz);
861 ctl
.parseProperties(par
);
866 //writeln(': ', ctl.mDefSize.toString);
868 par
.eatDelim(','); // optional
873 function THControl
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
876 if (strEquCI1251(prname
, 'id')) then begin mId
:= par
.expectStrOrId(true); exit
; end; // allow empty strings
877 if (strEquCI1251(prname
, 'flex')) then begin flex
:= par
.expectInt(); exit
; end;
879 if (strEquCI1251(prname
, 'defsize')) then begin mDefSize
:= parseSize(par
); exit
; end;
880 if (strEquCI1251(prname
, 'maxsize')) then begin mMaxSize
:= parseSize(par
); exit
; end;
881 if (strEquCI1251(prname
, 'wrap')) then begin mCanWrap
:= parseBool(par
); exit
; end;
882 if (strEquCI1251(prname
, 'linestart')) then begin mLineStart
:= parseBool(par
); exit
; end;
883 if (strEquCI1251(prname
, 'expand')) then begin mExpand
:= parseBool(par
); exit
; end;
885 if (strEquCI1251(prname
, 'align')) then begin mAlign
:= parseAnyAlign(par
); exit
; end;
886 if (strEquCI1251(prname
, 'hgroup')) then begin mHGroup
:= par
.expectStrOrId(true); exit
; end; // allow empty strings
887 if (strEquCI1251(prname
, 'vgroup')) then begin mVGroup
:= par
.expectStrOrId(true); exit
; end; // allow empty strings
889 if (strEquCI1251(prname
, 'canfocus')) then begin mCanFocus
:= parseBool(par
); exit
; end;
890 if (strEquCI1251(prname
, 'enabled')) then begin mEnabled
:= parseBool(par
); exit
; end;
891 if (strEquCI1251(prname
, 'disabled')) then begin mEnabled
:= not parseBool(par
); exit
; end;
892 if (strEquCI1251(prname
, 'escclose')) then begin mEscClose
:= not parseBool(par
); exit
; end;
893 if (strEquCI1251(prname
, 'eatkeys')) then begin mEatKeys
:= not parseBool(par
); exit
; end;
898 // ////////////////////////////////////////////////////////////////////////// //
899 procedure THControl
.activated ();
904 procedure THControl
.blurred ();
910 function THControl
.topLevel (): THControl
; inline;
913 while (result
.mParent
<> nil) do result
:= result
.mParent
;
917 function THControl
.getEnabled (): Boolean;
922 if (not mEnabled
) or (mWidth
< 1) or (mHeight
< 1) then exit
;
924 while (ctl
<> nil) do
926 if (not ctl
.mEnabled
) or (ctl
.mWidth
< 1) or (ctl
.mHeight
< 1) then exit
;
933 procedure THControl
.setEnabled (v
: Boolean); inline;
935 if (mEnabled
= v
) then exit
;
937 if not v
and focused
then setFocused(false);
941 function THControl
.getFocused (): Boolean; inline;
943 if (mParent
= nil) then result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = self
) else result
:= (topLevel
.mFocused
= self
);
947 procedure THControl
.setFocused (v
: Boolean); inline;
954 if (tl
.mFocused
= self
) then
957 tl
.mFocused
:= tl
.findNextFocus(self
);
958 if (tl
.mFocused
= self
) then tl
.mFocused
:= nil;
962 if (not mEnabled
) or (not mCanFocus
) then exit
;
963 if (tl
.mFocused
<> self
) then
965 tl
.mFocused
.blurred();
967 if (tl
.mGrab
<> self
) then tl
.mGrab
:= nil;
973 function THControl
.isMyChild (ctl
: THControl
): Boolean;
976 while (ctl
<> nil) do
978 if (ctl
.mParent
= self
) then exit
;
985 // returns `true` if global coords are inside this control
986 function THControl
.toLocal (var x
, y
: Integer): Boolean;
991 while (ctl
<> nil) do
997 result
:= (x
>= 0) and (y
>= 0) and (x
< mWidth
) and (y
< mHeight
);
1000 function THControl
.toLocal (gx
, gy
: Integer; out x
, y
: Integer): Boolean; inline;
1004 result
:= toLocal(x
, y
);
1007 procedure THControl
.toGlobal (var x
, y
: Integer);
1012 while (ctl
<> nil) do
1020 procedure THControl
.toGlobal (lx
, ly
: Integer; out x
, y
: Integer); inline;
1028 // x and y are global coords
1029 function THControl
.controlAtXY (x
, y
: Integer): THControl
;
1035 if (not mEnabled
) or (mWidth
< 1) or (mHeight
< 1) then exit
;
1036 if not toLocal(x
, y
, lx
, ly
) then exit
;
1037 for f
:= High(mChildren
) downto 0 do
1039 result
:= mChildren
[f
].controlAtXY(x
, y
);
1040 if (result
<> nil) then exit
;
1046 function THControl
.prevSibling (): THControl
;
1050 if (mParent
<> nil) then
1052 for f
:= 1 to High(mParent
.mChildren
) do
1054 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
-1]; exit
; end;
1060 function THControl
.nextSibling (): THControl
;
1064 if (mParent
<> nil) then
1066 for f
:= 0 to High(mParent
.mChildren
)-1 do
1068 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
+1]; exit
; end;
1074 function THControl
.firstChild (): THControl
; inline;
1076 if (Length(mChildren
) <> 0) then result
:= mChildren
[0] else result
:= nil;
1079 function THControl
.lastChild (): THControl
; inline;
1081 if (Length(mChildren
) <> 0) then result
:= mChildren
[High(mChildren
)] else result
:= nil;
1085 function THControl
.findFirstFocus (): THControl
;
1092 for f
:= 0 to High(mChildren
) do
1094 result
:= mChildren
[f
].findFirstFocus();
1095 if (result
<> nil) then exit
;
1097 if mCanFocus
then result
:= self
;
1102 function THControl
.findLastFocus (): THControl
;
1109 for f
:= High(mChildren
) downto 0 do
1111 result
:= mChildren
[f
].findLastFocus();
1112 if (result
<> nil) then exit
;
1114 if mCanFocus
then result
:= self
;
1119 function THControl
.findNextFocus (cur
: THControl
): THControl
;
1124 if not isMyChild(cur
) then cur
:= nil;
1125 if (cur
= nil) then begin result
:= findFirstFocus(); exit
; end;
1126 result
:= cur
.findFirstFocus();
1127 if (result
<> nil) and (result
<> cur
) then exit
;
1130 cur
:= cur
.nextSibling
;
1131 if (cur
= nil) then break
;
1132 result
:= cur
.findFirstFocus();
1133 if (result
<> nil) then exit
;
1135 result
:= findFirstFocus();
1140 function THControl
.findPrevFocus (cur
: THControl
): THControl
;
1145 if not isMyChild(cur
) then cur
:= nil;
1146 if (cur
= nil) then begin result
:= findLastFocus(); exit
; end;
1148 result
:= cur
.findLastFocus();
1149 if (result
<> nil) and (result
<> cur
) then exit
;
1152 cur
:= cur
.prevSibling
;
1153 if (cur
= nil) then break
;
1154 result
:= cur
.findLastFocus();
1155 if (result
<> nil) then exit
;
1157 result
:= findLastFocus();
1162 procedure THControl
.appendChild (ctl
: THControl
);
1164 if (ctl
= nil) then exit
;
1165 if (ctl
.mParent
<> nil) then exit
;
1166 SetLength(mChildren
, Length(mChildren
)+1);
1167 mChildren
[High(mChildren
)] := ctl
;
1168 ctl
.mParent
:= self
;
1169 Inc(ctl
.mX
, mFrameWidth
);
1170 Inc(ctl
.mY
, mFrameHeight
);
1171 if (ctl
.mWidth
> 0) and (ctl
.mHeight
> 0) and
1172 (ctl
.mX
+ctl
.mWidth
> mFrameWidth
) and (ctl
.mY
+ctl
.mHeight
> mFrameHeight
) then
1174 if (mWidth
+mFrameWidth
< ctl
.mX
+ctl
.mWidth
) then mWidth
:= ctl
.mX
+ctl
.mWidth
+mFrameWidth
;
1175 if (mHeight
+mFrameHeight
< ctl
.mY
+ctl
.mHeight
) then mHeight
:= ctl
.mY
+ctl
.mHeight
+mFrameHeight
;
1177 if (mFocused
= nil) and ctl
.mEnabled
and ctl
.mCanFocus
and (ctl
.mWidth
> 0) and (ctl
.mHeight
> 0) then mFocused
:= ctl
;
1181 // ////////////////////////////////////////////////////////////////////////// //
1182 procedure THControl
.setScissorGLInternal (x
, y
, w
, h
: Integer);
1184 if not scallowed
then exit
;
1185 x
:= trunc(x
*gh_ui_scale
);
1186 y
:= trunc(y
*gh_ui_scale
);
1187 w
:= trunc(w
*gh_ui_scale
);
1188 h
:= trunc(h
*gh_ui_scale
);
1189 scis
.combineRect(x
, y
, w
, h
);
1192 procedure THControl
.setScissor (lx
, ly
, lw
, lh
: Integer);
1195 //ox, oy, ow, oh: Integer;
1197 if not scallowed
then exit
;
1198 //ox := lx; oy := ly; ow := lw; oh := lh;
1199 if not intersectRect(lx
, ly
, lw
, lh
, 0, 0, mWidth
, mHeight
) then
1201 //writeln('oops: <', self.className, '>: old=(', ox, ',', oy, ')-[', ow, ',', oh, ']');
1202 glScissor(0, 0, 0, 0);
1205 toGlobal(lx
, ly
, gx
, gy
);
1206 setScissorGLInternal(gx
, gy
, lw
, lh
);
1209 procedure THControl
.resetScissor (fullArea
: Boolean); inline;
1211 if not scallowed
then exit
;
1214 setScissor(0, 0, mWidth
, mHeight
);
1218 setScissor(mFrameWidth
, mFrameHeight
, mWidth
-mFrameWidth
*2, mHeight
-mFrameHeight
*2);
1223 // ////////////////////////////////////////////////////////////////////////// //
1224 procedure THControl
.draw ();
1229 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1230 toGlobal(0, 0, gx
, gy
);
1231 //conwritefln('[%s]: (%d,%d)-(%d,%d) (%d,%d)', [ClassName, mX, mY, mWidth, mHeight, x, y]);
1233 scis
.save(true); // scissoring enabled
1236 resetScissor(true); // full area
1237 drawControl(gx
, gy
);
1238 resetScissor(false); // client area
1239 for f
:= 0 to High(mChildren
) do mChildren
[f
].draw();
1240 resetScissor(true); // full area
1241 drawControlPost(gx
, gy
);
1248 procedure THControl
.drawControl (gx
, gy
: Integer);
1250 if (mParent
= nil) then darkenRect(gx
, gy
, mWidth
, mHeight
, 64);
1253 procedure THControl
.drawControlPost (gx
, gy
: Integer);
1256 if mDrawShadow
and (mWidth
> 0) and (mHeight
> 0) then
1258 setScissorGLInternal(gx
+8, gy
+8, mWidth
, mHeight
);
1259 darkenRect(gx
+mWidth
, gy
+8, 8, mHeight
, 128);
1260 darkenRect(gx
+8, gy
+mHeight
, mWidth
-8, 8, 128);
1265 // ////////////////////////////////////////////////////////////////////////// //
1266 function THControl
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1271 if not mEnabled
then exit
;
1272 if (mParent
= nil) then
1274 if (mGrab
<> nil) then
1276 result
:= mGrab
.mouseEvent(ev
);
1277 if (ev
.release
) then mGrab
:= nil;
1281 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1282 ctl
:= controlAtXY(ev
.x
, ev
.y
);
1283 if (ctl
<> nil) and (ctl
<> self
) then
1285 if (ctl
<> topLevel
.mFocused
) then ctl
.setFocused(true);
1286 result
:= ctl
.mouseEvent(ev
);
1288 else if (ctl
= self
) and assigned(actionCB
) then
1295 function THControl
.keyEvent (var ev
: THKeyEvent
): Boolean;
1300 if not mEnabled
then exit
;
1301 if (topLevel
.mFocused
<> self
) and isMyChild(topLevel
.mFocused
) and topLevel
.mFocused
.mEnabled
then result
:= topLevel
.mFocused
.keyEvent(ev
);
1302 if (mParent
= nil) then
1304 if (ev
= 'S-Tab') then
1307 ctl
:= findPrevFocus(mFocused
);
1308 if (ctl
<> mFocused
) then
1315 if (ev
= 'Tab') then
1318 ctl
:= findNextFocus(mFocused
);
1319 if (ctl
<> mFocused
) then
1326 if mEscClose
and (ev
= 'Escape') then
1329 uiRemoveWindow(self
);
1333 if mEatKeys
then result
:= true;
1337 // ////////////////////////////////////////////////////////////////////////// //
1338 constructor THTopWindow
.Create (const atitle
: AnsiString; ax
, ay
: Integer; aw
: Integer=-1; ah
: Integer=-1);
1340 inherited Create(ax
, ay
, aw
, ah
);
1344 if (mWidth
< mFrameWidth
*2+3*8) then mWidth
:= mFrameWidth
*2+3*8;
1345 if (mHeight
< mFrameHeight
*2) then mHeight
:= mFrameHeight
*2;
1346 if (Length(mTitle
) > 0) then
1348 if (mWidth
< Length(mTitle
)*8+mFrameWidth
*2+3*8) then mWidth
:= Length(mTitle
)*8+mFrameWidth
*2+3*8;
1351 mDrawShadow
:= true;
1352 mWaitingClose
:= false;
1358 function THTopWindow
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1360 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
1362 mTitle
:= par
.expectStrOrId(true);
1366 if (strEquCI1251(prname
, 'children')) then
1372 if (strEquCI1251(prname
, 'orientation')) or (strEquCI1251(prname
, 'orient')) then
1374 if (par
.eatIdOrStr('horizontal', false)) or (par
.eatIdOrStr('horiz', false)) then mHoriz
:= true
1375 else if (par
.eatIdOrStr('vertical', false)) or (par
.eatIdOrStr('vert', false)) then mHoriz
:= false
1376 else par
.error('`horizontal` or `vertical` expected');
1380 result
:= inherited parseProperty(prname
, par
);
1384 procedure THTopWindow
.centerInScreen ();
1386 if (mWidth
> 0) and (mHeight
> 0) then
1388 mX
:= trunc((gScrWidth
/gh_ui_scale
-mWidth
)/2);
1389 mY
:= trunc((gScrHeight
/gh_ui_scale
-mHeight
)/2);
1394 procedure THTopWindow
.drawControl (gx
, gy
: Integer);
1396 fillRect(gx
, gy
, mWidth
, mHeight
, 0, 0, 128);
1400 procedure THTopWindow
.drawControlPost (gx
, gy
: Integer);
1409 drawRectUI(mX
+4, mY
+4, mWidth
-8, mHeight
-8, r
, g
, b
);
1413 drawRectUI(mX
+3, mY
+3, mWidth
-6, mHeight
-6, r
, g
, b
);
1414 drawRectUI(mX
+5, mY
+5, mWidth
-10, mHeight
-10, r
, g
, b
);
1415 setScissor(mFrameWidth
, 0, 3*8, 8);
1416 fillRect(mX
+mFrameWidth
, mY
, 3*8, 8, 0, 0, 128);
1417 drawText8(mX
+mFrameWidth
, mY
, '[ ]', r
, g
, b
);
1418 if mInClose
then drawText8(mX
+mFrameWidth
+7, mY
, '#', 0, 255, 0)
1419 else drawText8(mX
+mFrameWidth
+7, mY
, '*', 0, 255, 0);
1421 if (Length(mTitle
) > 0) then
1423 setScissor(mFrameWidth
+3*8, 0, mWidth
-mFrameWidth
*2-3*8, 8);
1424 tx
:= (mX
+3*8)+((mWidth
-3*8)-Length(mTitle
)*8) div 2;
1425 fillRect(tx
-3, mY
, Length(mTitle
)*8+3+2, 8, 0, 0, 128);
1426 drawText8(tx
, mY
, mTitle
, r
, g
, b
);
1428 inherited drawControlPost(gx
, gy
);
1432 procedure THTopWindow
.blurred ();
1435 mWaitingClose
:= false;
1441 function THTopWindow
.keyEvent (var ev
: THKeyEvent
): Boolean;
1443 result
:= inherited keyEvent(ev
);
1444 if not getFocused
then exit
;
1445 if (ev
= 'M-F3') then
1447 uiRemoveWindow(self
);
1454 function THTopWindow
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1459 if not mEnabled
then exit
;
1460 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1464 mX
+= ev
.x
-mDragStartX
;
1465 mY
+= ev
.y
-mDragStartY
;
1466 mDragStartX
:= ev
.x
;
1467 mDragStartY
:= ev
.y
;
1468 if (ev
.release
) then mDragging
:= false;
1473 if toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1479 if (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8) then
1481 //uiRemoveWindow(self);
1482 mWaitingClose
:= true;
1488 mDragStartX
:= ev
.x
;
1489 mDragStartY
:= ev
.y
;
1494 if (lx
< mFrameWidth
) or (lx
>= mWidth
-mFrameWidth
) or (ly
>= mHeight
-mFrameHeight
) then
1497 mDragStartX
:= ev
.x
;
1498 mDragStartY
:= ev
.y
;
1504 if (ev
.release
) then
1506 if mWaitingClose
and (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8) then
1508 uiRemoveWindow(self
);
1512 mWaitingClose
:= false;
1518 if mWaitingClose
then
1520 mInClose
:= (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8);
1529 if (not ev
.motion
) then mWaitingClose
:= false;
1532 result
:= inherited mouseEvent(ev
);
1536 // ////////////////////////////////////////////////////////////////////////// //
1537 constructor THCtlSimpleText
.Create (ax
, ay
: Integer);
1540 inherited Create(ax
, ay
, 4, 4);
1544 destructor THCtlSimpleText
.Destroy ();
1551 procedure THCtlSimpleText
.appendItem (const atext
: AnsiString; acentered
: Boolean=false; ahline
: Boolean=false);
1555 if (Length(atext
)*8+3*8+2 > mWidth
) then mWidth
:= Length(atext
)*8+3*8+2;
1556 SetLength(mItems
, Length(mItems
)+1);
1557 it
:= @mItems
[High(mItems
)];
1559 it
.centered
:= acentered
;
1561 if (Length(mItems
)*8 > mHeight
) then mHeight
:= Length(mItems
)*8;
1565 procedure THCtlSimpleText
.drawControl (gx
, gy
: Integer);
1571 for f
:= 0 to High(mItems
) do
1578 if it
.centered
then begin b
:= 255; tx
:= gx
+(mWidth
-Length(it
.title
)*8) div 2; end;
1582 if (Length(it
.title
) = 0) then
1584 drawHLine(gx
+4, gy
+3, mWidth
-8, r
, g
, b
);
1586 else if (tx
-3 > gx
+4) then
1588 drawHLine(gx
+4, gy
+3, tx
-3-(gx
+3), r
, g
, b
);
1589 drawHLine(tx
+Length(it
.title
)*8, gy
+3, mWidth
-4, r
, g
, b
);
1592 drawText8(tx
, gy
, it
.title
, r
, g
, b
);
1598 function THCtlSimpleText
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1602 result
:= inherited mouseEvent(ev
);
1603 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1610 function THCtlSimpleText
.keyEvent (var ev
: THKeyEvent
): Boolean;
1612 result
:= inherited keyEvent(ev
);
1616 // ////////////////////////////////////////////////////////////////////////// //
1617 constructor THCtlCBListBox
.Create (ax
, ay
: Integer);
1621 inherited Create(ax
, ay
, 4, 4);
1625 destructor THCtlCBListBox
.Destroy ();
1632 procedure THCtlCBListBox
.appendItem (const atext
: AnsiString; bv
: PBoolean; aaction
: TActionCB
=nil);
1636 if (Length(atext
)*8+3*8+2 > mWidth
) then mWidth
:= Length(atext
)*8+3*8+2;
1637 SetLength(mItems
, Length(mItems
)+1);
1638 it
:= @mItems
[High(mItems
)];
1641 it
.actionCB
:= aaction
;
1642 if (Length(mItems
)*8 > mHeight
) then mHeight
:= Length(mItems
)*8;
1643 if (mCurIndex
< 0) then mCurIndex
:= 0;
1647 procedure THCtlCBListBox
.drawControl (gx
, gy
: Integer);
1652 for f
:= 0 to High(mItems
) do
1655 if (mCurIndex
= f
) then fillRect(gx
, gy
, mWidth
, 8, 0, 128, 0);
1656 if (it
.varp
<> nil) then
1658 if it
.varp
^ then drawText8(gx
, gy
, '[x]', 255, 255, 255) else drawText8(gx
, gy
, '[ ]', 255, 255, 255);
1659 drawText8(gx
+3*8+2, gy
, it
.title
, 255, 255, 0);
1661 else if (Length(it
.title
) > 0) then
1663 tx
:= gx
+(mWidth
-Length(it
.title
)*8) div 2;
1664 if (tx
-3 > gx
+4) then
1666 drawHLine(gx
+4, gy
+3, tx
-3-(gx
+3), 255, 255, 255);
1667 drawHLine(tx
+Length(it
.title
)*8, gy
+3, mWidth
-4, 255, 255, 255);
1669 drawText8(tx
, gy
, it
.title
, 255, 255, 255);
1673 drawHLine(gx
+4, gy
+3, mWidth
-8, 255, 255, 255);
1680 function THCtlCBListBox
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1685 result
:= inherited mouseEvent(ev
);
1686 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1689 if (ev
= 'lmb') then
1692 if (ly
>= 0) and (ly
< Length(mItems
)) then
1695 if (it
.varp
<> nil) then
1698 it
.varp
^ := not it
.varp
^;
1699 if assigned(it
.actionCB
) then it
.actionCB(self
, Integer(it
.varp
^));
1700 if assigned(actionCB
) then actionCB(self
, ly
);
1708 function THCtlCBListBox
.keyEvent (var ev
: THKeyEvent
): Boolean;
1712 result
:= inherited keyEvent(ev
);
1713 if not getFocused
then exit
;
1715 if (ev
= 'Home') or (ev
= 'PageUp') then
1720 if (ev
= 'End') or (ev
= 'PageDown') then
1723 mCurIndex
:= High(mItems
);
1728 if (Length(mItems
) > 0) then
1730 if (mCurIndex
< 0) then mCurIndex
:= Length(mItems
);
1731 while (mCurIndex
> 0) do
1734 if (mItems
[mCurIndex
].varp
<> nil) then break
;
1742 if (ev
= 'Down') then
1745 if (Length(mItems
) > 0) then
1747 if (mCurIndex
< 0) then mCurIndex
:= -1;
1748 while (mCurIndex
< High(mItems
)) do
1751 if (mItems
[mCurIndex
].varp
<> nil) then break
;
1759 if (ev
= 'Space') or (ev
= 'Enter') then
1762 if (mCurIndex
>= 0) and (mCurIndex
< Length(mItems
)) and (mItems
[mCurIndex
].varp
<> nil) then
1764 it
:= @mItems
[mCurIndex
];
1765 it
.varp
^ := not it
.varp
^;
1766 if assigned(it
.actionCB
) then it
.actionCB(self
, Integer(it
.varp
^));
1767 if assigned(actionCB
) then actionCB(self
, mCurIndex
);
1773 // ////////////////////////////////////////////////////////////////////////// //
1774 constructor THCtlBox
.Create (ahoriz
: Boolean);
1781 function THCtlBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1783 if (strEquCI1251(prname
, 'orientation')) or (strEquCI1251(prname
, 'orient')) then
1785 if (par
.eatIdOrStr('horizontal', false)) or (par
.eatIdOrStr('horiz', false)) then mHoriz
:= true
1786 else if (par
.eatIdOrStr('vertical', false)) or (par
.eatIdOrStr('vert', false)) then mHoriz
:= false
1787 else par
.error('`horizontal` or `vertical` expected');
1791 if (strEquCI1251(prname
, 'frame')) then
1793 mHasFrame
:= parseBool(par
);
1794 if (mHasFrame
) then begin mFrameWidth
:= 8; mFrameHeight
:= 8; end else begin mFrameWidth
:= 0; mFrameHeight
:= 0; end;
1798 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
1800 mCaption
:= par
.expectStrOrId(true);
1801 mDefSize
:= TLaySize
.Create(Length(mCaption
)*8+2, 8);
1805 if (strEquCI1251(prname
, 'children')) then
1811 result
:= inherited parseProperty(prname
, par
);
1815 procedure THCtlBox
.drawControl (gx
, gy
: Integer);
1820 if focused
then begin r
:= 255; g
:= 255; b
:= 255; end else begin r
:= 255; g
:= 255; b
:= 0; end;
1824 drawRectUI(gx
+3, gy
+3, mWidth
-6, mHeight
-6, r
, g
, b
);
1827 if (Length(mCaption
) > 0) then
1829 setScissor(mFrameWidth
+1, 0, mWidth
-mFrameWidth
-2, 8);
1830 tx
:= gx
+((mWidth
-Length(mCaption
)*8) div 2)-1;
1831 if mHasFrame
then fillRect(tx
, gy
, Length(mCaption
)*8+2, 8, 0, 0, 128);
1832 drawText8(tx
+1, gy
, mCaption
, r
, g
, b
);
1837 function THCtlBox
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1841 result
:= inherited mouseEvent(ev
);
1842 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1849 //TODO: navigation with arrow keys, according to box orientation
1850 function THCtlBox
.keyEvent (var ev
: THKeyEvent
): Boolean;
1852 result
:= inherited keyEvent(ev
);
1856 // ////////////////////////////////////////////////////////////////////////// //
1857 procedure THCtlHBox
.AfterConstruction ();
1859 inherited AfterConstruction();
1864 // ////////////////////////////////////////////////////////////////////////// //
1865 procedure THCtlVBox
.AfterConstruction ();
1867 inherited AfterConstruction();
1871 // ////////////////////////////////////////////////////////////////////////// //
1872 constructor THCtlTextLabel
.Create (const atext
: AnsiString);
1878 mDefSize
:= TLaySize
.Create(Length(atext
)*8, 8);
1882 function THCtlTextLabel
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1884 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
1886 mText
:= par
.expectStrOrId(true);
1887 mDefSize
:= TLaySize
.Create(Length(mText
)*8, 8);
1891 if (strEquCI1251(prname
, 'textalign')) then
1893 parseTextAlign(par
, mHAlign
, mVAlign
);
1897 result
:= inherited parseProperty(prname
, par
);
1901 procedure THCtlTextLabel
.drawControl (gx
, gy
: Integer);
1903 xpos
, ypos
: Integer;
1906 fillRect(gx
, gy
, mWidth
, mHeight
, 96, 96, 0);
1907 drawRectUI(gx
, gy
, mWidth
, mHeight
, 96, 96, 96);
1909 if (Length(mText
) > 0) then
1911 if (mHAlign
< 0) then xpos
:= 0
1912 else if (mHAlign
> 0) then xpos
:= mWidth
-Length(mText
)*8
1913 else xpos
:= (mWidth
-Length(mText
)*8) div 2;
1915 if (mVAlign
< 0) then ypos
:= 0
1916 else if (mVAlign
> 0) then ypos
:= mHeight
-8
1917 else ypos
:= (mHeight
-8) div 2;
1919 drawText8(gx
+xpos
, gy
+ypos
, mText
, 255, 255, 255);
1924 function THCtlTextLabel
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1928 result
:= inherited mouseEvent(ev
);
1929 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1936 function THCtlTextLabel
.keyEvent (var ev
: THKeyEvent
): Boolean;
1938 result
:= inherited keyEvent(ev
);
1943 registerCtlClass(THCtlBox
, 'box');
1944 registerCtlClass(THCtlHBox
, 'hbox');
1945 registerCtlClass(THCtlVBox
, 'vbox');
1946 registerCtlClass(THCtlTextLabel
, 'label');