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 function parseOrientation (const prname
: AnsiString; par
: TTextParser
): Boolean;
149 procedure parseTextAlign (par
: TTextParser
; var h
, v
: Integer);
150 procedure parseChildren (par
: TTextParser
); // par should be on '{'; final '}' is eaten
153 // par is on property data
154 // there may be more data in text stream, don't eat it!
155 // return `true` if property name is valid and value was parsed
156 // return `false` if property name is invalid; don't advance parser in this case
157 // throw on property data errors
158 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; virtual;
160 // par should be on '{'; final '}' is eaten
161 procedure parseProperties (par
: TTextParser
);
164 constructor Create ();
165 constructor Create (ax
, ay
, aw
, ah
: Integer);
166 destructor Destroy (); override;
168 // `sx` and `sy` are screen coordinates
169 procedure drawControl (gx
, gy
: Integer); virtual;
171 // called after all children drawn
172 procedure drawControlPost (gx
, gy
: Integer); virtual;
174 procedure draw (); virtual;
176 function topLevel (): THControl
; inline;
178 // returns `true` if global coords are inside this control
179 function toLocal (var x
, y
: Integer): Boolean;
180 function toLocal (gx
, gy
: Integer; out x
, y
: Integer): Boolean; inline;
181 procedure toGlobal (var x
, y
: Integer);
182 procedure toGlobal (lx
, ly
: Integer; out x
, y
: Integer); inline;
184 // x and y are global coords
185 function controlAtXY (x
, y
: Integer): THControl
;
187 function mouseEvent (var ev
: THMouseEvent
): Boolean; virtual; // returns `true` if event was eaten
188 function keyEvent (var ev
: THKeyEvent
): Boolean; virtual; // returns `true` if event was eaten
190 function prevSibling (): THControl
;
191 function nextSibling (): THControl
;
192 function firstChild (): THControl
; inline;
193 function lastChild (): THControl
; inline;
195 procedure appendChild (ctl
: THControl
); virtual;
198 property id
: AnsiString read mId
;
199 property x0
: Integer read mX
;
200 property y0
: Integer read mY
;
201 property height
: Integer read mHeight
;
202 property width
: Integer read mWidth
;
203 property enabled
: Boolean read getEnabled write setEnabled
;
204 property parent
: THControl read mParent
;
205 property focused
: Boolean read getFocused write setFocused
;
206 property escClose
: Boolean read mEscClose write mEscClose
;
207 property eatKeys
: Boolean read mEatKeys write mEatKeys
;
211 THTopWindow
= class(THControl
)
215 mDragStartX
, mDragStartY
: Integer;
216 mWaitingClose
: Boolean;
218 mFreeOnClose
: Boolean; // default: false
221 procedure activated (); override;
222 procedure blurred (); override;
225 closeCB
: TActionCB
; // called after window was removed from ui window list
228 constructor Create (const atitle
: AnsiString; ax
, ay
: Integer; aw
: Integer=-1; ah
: Integer=-1);
230 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
232 procedure centerInScreen ();
234 // `sx` and `sy` are screen coordinates
235 procedure drawControl (gx
, gy
: Integer); override;
236 procedure drawControlPost (gx
, gy
: Integer); override;
238 function keyEvent (var ev
: THKeyEvent
): Boolean; override; // returns `true` if event was eaten
239 function mouseEvent (var ev
: THMouseEvent
): Boolean; override; // returns `true` if event was eaten
242 property freeOnClose
: Boolean read mFreeOnClose write mFreeOnClose
;
246 THCtlSimpleText
= class(THControl
)
256 mItems
: array of TItem
;
259 constructor Create (ax
, ay
: Integer);
260 destructor Destroy (); override;
262 procedure appendItem (const atext
: AnsiString; acentered
: Boolean=false; ahline
: Boolean=false);
264 procedure drawControl (gx
, gy
: Integer); override;
266 function mouseEvent (var ev
: THMouseEvent
): Boolean; override;
267 function keyEvent (var ev
: THKeyEvent
): Boolean; override;
271 THCtlCBListBox
= class(THControl
)
281 mItems
: array of TItem
;
285 constructor Create (ax
, ay
: Integer);
286 destructor Destroy (); override;
288 procedure appendItem (const atext
: AnsiString; bv
: PBoolean; aaction
: TActionCB
=nil);
290 procedure drawControl (gx
, gy
: Integer); override;
292 function mouseEvent (var ev
: THMouseEvent
): Boolean; override;
293 function keyEvent (var ev
: THKeyEvent
): Boolean; override;
296 // ////////////////////////////////////////////////////////////////////// //
297 THCtlBox
= class(THControl
)
300 mCaption
: AnsiString;
303 constructor Create (ahoriz
: Boolean);
305 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
307 procedure drawControl (gx
, gy
: Integer); override;
309 function mouseEvent (var ev
: THMouseEvent
): Boolean; override;
310 function keyEvent (var ev
: THKeyEvent
): Boolean; override;
313 THCtlHBox
= class(THCtlBox
)
315 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
318 THCtlVBox
= class(THCtlBox
)
320 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
323 // ////////////////////////////////////////////////////////////////////// //
324 THCtlSpan
= class(THControl
)
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 THCtlLine
= class(THControl
)
336 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
338 procedure drawControl (gx
, gy
: Integer); override;
341 THCtlHLine
= class(THCtlLine
)
343 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
346 THCtlVLine
= class(THCtlLine
)
348 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
351 // ////////////////////////////////////////////////////////////////////// //
352 THCtlTextLabel
= class(THControl
)
355 mHAlign
: Integer; // -1: left; 0: center; 1: right; default: left
356 mVAlign
: Integer; // -1: top; 0: center; 1: bottom; default: center
359 constructor Create (const atext
: AnsiString);
361 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
363 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
365 procedure drawControl (gx
, gy
: Integer); override;
367 function mouseEvent (var ev
: THMouseEvent
): Boolean; override;
368 function keyEvent (var ev
: THKeyEvent
): Boolean; override;
372 // ////////////////////////////////////////////////////////////////////////// //
373 function uiMouseEvent (ev
: THMouseEvent
): Boolean;
374 function uiKeyEvent (ev
: THKeyEvent
): Boolean;
378 // ////////////////////////////////////////////////////////////////////////// //
379 procedure uiAddWindow (ctl
: THControl
);
380 procedure uiRemoveWindow (ctl
: THControl
); // will free window if `mFreeOnClose` is `true`
381 function uiVisibleWindow (ctl
: THControl
): Boolean;
384 // ////////////////////////////////////////////////////////////////////////// //
386 procedure uiLayoutCtl (ctl
: THControl
);
389 // ////////////////////////////////////////////////////////////////////////// //
391 gh_ui_scale
: Single = 1.0;
401 // ////////////////////////////////////////////////////////////////////////// //
403 knownCtlClasses
: array of record
404 klass
: THControlClass
;
409 procedure registerCtlClass (aklass
: THControlClass
; const aname
: AnsiString);
411 assert(aklass
<> nil);
412 assert(Length(aname
) > 0);
413 SetLength(knownCtlClasses
, Length(knownCtlClasses
)+1);
414 knownCtlClasses
[High(knownCtlClasses
)].klass
:= aklass
;
415 knownCtlClasses
[High(knownCtlClasses
)].name
:= aname
;
419 function findCtlClass (const aname
: AnsiString): THControlClass
;
423 for f
:= 0 to High(knownCtlClasses
) do
425 if (strEquCI1251(aname
, knownCtlClasses
[f
].name
)) then
427 result
:= knownCtlClasses
[f
].klass
;
435 // ////////////////////////////////////////////////////////////////////////// //
437 TFlexLayouter
= specialize TFlexLayouterBase
<THControl
>;
439 procedure uiLayoutCtl (ctl
: THControl
);
443 if (ctl
= nil) then exit
;
444 lay
:= TFlexLayouter
.Create();
449 //writeln('============================'); lay.dumpFlat();
451 //writeln('=== initial ==='); lay.dump();
453 //lay.calcMaxSizeInternal(0);
456 writeln('=== after first pass ===');
460 writeln('=== after second pass ===');
465 //writeln('=== final ==='); lay.dump();
473 // ////////////////////////////////////////////////////////////////////////// //
475 uiTopList
: array of THControl
= nil;
478 function uiMouseEvent (ev
: THMouseEvent
): Boolean;
484 ev
.x
:= trunc(ev
.x
/gh_ui_scale
);
485 ev
.y
:= trunc(ev
.y
/gh_ui_scale
);
486 ev
.dx
:= trunc(ev
.dx
/gh_ui_scale
); //FIXME
487 ev
.dy
:= trunc(ev
.dy
/gh_ui_scale
); //FIXME
488 if (Length(uiTopList
) = 0) then result
:= false else result
:= uiTopList
[High(uiTopList
)].mouseEvent(ev
);
489 if not result
and (ev
.press
) then
491 for f
:= High(uiTopList
) downto 0 do
493 if uiTopList
[f
].toLocal(ev
.x
, ev
.y
, lx
, ly
) then
496 if uiTopList
[f
].mEnabled
and (f
<> High(uiTopList
)) then
498 uiTopList
[High(uiTopList
)].blurred();
499 ctmp
:= uiTopList
[f
];
501 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
502 uiTopList
[High(uiTopList
)] := ctmp
;
504 result
:= ctmp
.mouseEvent(ev
);
513 function uiKeyEvent (ev
: THKeyEvent
): Boolean;
515 ev
.x
:= trunc(ev
.x
/gh_ui_scale
);
516 ev
.y
:= trunc(ev
.y
/gh_ui_scale
);
517 if (Length(uiTopList
) = 0) then result
:= false else result
:= uiTopList
[High(uiTopList
)].keyEvent(ev
);
518 if (ev
.release
) then begin result
:= true; exit
; end;
527 glMatrixMode(GL_MODELVIEW
);
531 glScalef(gh_ui_scale
, gh_ui_scale
, 1);
532 for f
:= 0 to High(uiTopList
) do
536 if (f
<> High(uiTopList
)) then darkenRect(ctl
.x0
, ctl
.y0
, ctl
.width
, ctl
.height
, 128);
539 glMatrixMode(GL_MODELVIEW
);
545 procedure uiAddWindow (ctl
: THControl
);
549 if (ctl
= nil) then exit
;
551 if not (ctl
is THTopWindow
) then exit
; // alas
552 for f
:= 0 to High(uiTopList
) do
554 if (uiTopList
[f
] = ctl
) then
556 if (f
<> High(uiTopList
)) then
558 uiTopList
[High(uiTopList
)].blurred();
559 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
560 uiTopList
[High(uiTopList
)] := ctl
;
566 if (Length(uiTopList
) > 0) then uiTopList
[High(uiTopList
)].blurred();
567 SetLength(uiTopList
, Length(uiTopList
)+1);
568 uiTopList
[High(uiTopList
)] := ctl
;
573 procedure uiRemoveWindow (ctl
: THControl
);
577 if (ctl
= nil) then exit
;
579 if not (ctl
is THTopWindow
) then exit
; // alas
580 for f
:= 0 to High(uiTopList
) do
582 if (uiTopList
[f
] = ctl
) then
585 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
586 SetLength(uiTopList
, Length(uiTopList
)-1);
587 if (ctl
is THTopWindow
) then
590 if assigned(THTopWindow(ctl
).closeCB
) then THTopWindow(ctl
).closeCB(ctl
, 0);
592 if (THTopWindow(ctl
).mFreeOnClose
) then FreeAndNil(ctl
);
601 function uiVisibleWindow (ctl
: THControl
): Boolean;
606 if (ctl
= nil) then exit
;
608 if not (ctl
is THTopWindow
) then exit
; // alas
609 for f
:= 0 to High(uiTopList
) do
611 if (uiTopList
[f
] = ctl
) then begin result
:= true; exit
; end;
616 // ////////////////////////////////////////////////////////////////////////// //
617 constructor THControl
.Create ();
634 mDrawShadow
:= false;
636 // layouter interface
637 //mDefSize := TLaySize.Create(64, 8); // default size
638 mDefSize
:= TLaySize
.Create(0, 0); // default size
639 mMaxSize
:= TLaySize
.Create(-1, -1); // maximum size
646 mAlign
:= -1; // left/top
651 constructor THControl
.Create (ax
, ay
, aw
, ah
: Integer);
661 destructor THControl
.Destroy ();
665 if (mParent
<> nil) then
668 for f
:= 0 to High(mParent
.mChildren
) do
670 if (mParent
.mChildren
[f
] = self
) then
672 for c
:= f
+1 to High(mParent
.mChildren
) do mParent
.mChildren
[c
-1] := mParent
.mChildren
[c
];
673 SetLength(mParent
.mChildren
, Length(mParent
.mChildren
)-1);
677 for f
:= 0 to High(mChildren
) do
679 mChildren
[f
].mParent
:= nil;
686 // ////////////////////////////////////////////////////////////////////////// //
687 function THControl
.getDefSize (): TLaySize
; inline; begin result
:= mLayDefSize
; end;
688 function THControl
.getMaxSize (): TLaySize
; inline; begin result
:= mLayMaxSize
; end;
689 function THControl
.getFlex (): Integer; inline; begin result
:= mFlex
; end;
690 function THControl
.isHorizBox (): Boolean; inline; begin result
:= mHoriz
; end;
691 procedure THControl
.setHorizBox (v
: Boolean); inline; begin mHoriz
:= v
; end;
692 function THControl
.canWrap (): Boolean; inline; begin result
:= mCanWrap
; end;
693 procedure THControl
.setCanWrap (v
: Boolean); inline; begin mCanWrap
:= v
; end;
694 function THControl
.isLineStart (): Boolean; inline; begin result
:= mLineStart
; end;
695 procedure THControl
.setLineStart (v
: Boolean); inline; begin mLineStart
:= v
; end;
696 function THControl
.getAlign (): Integer; inline; begin result
:= mAlign
; end;
697 procedure THControl
.setAlign (v
: Integer); inline; begin mAlign
:= v
; end;
698 function THControl
.getExpand (): Boolean; inline; begin result
:= mExpand
; end;
699 procedure THControl
.setExpand (v
: Boolean); inline; begin mExpand
:= v
; end;
700 function THControl
.getHGroup (): AnsiString; inline; begin result
:= mHGroup
; end;
701 procedure THControl
.setHGroup (const v
: AnsiString); inline; begin mHGroup
:= v
; end;
702 function THControl
.getVGroup (): AnsiString; inline; begin result
:= mVGroup
; end;
703 procedure THControl
.setVGroup (const v
: AnsiString); inline; begin mVGroup
:= v
; end;
705 function THControl
.getMargins (): TLayMargins
; inline;
707 result
:= TLayMargins
.Create(mFrameHeight
, mFrameWidth
, mFrameHeight
, mFrameWidth
);
710 procedure THControl
.setActualSizePos (constref apos
: TLayPos
; constref asize
: TLaySize
); inline; begin
711 //writeln(self.className, '; pos=', apos.toString, '; size=', asize.toString);
712 if (mParent
<> nil) then
721 procedure THControl
.layPrepare ();
723 mLayDefSize
:= mDefSize
;
724 mLayMaxSize
:= mMaxSize
;
725 if (mLayMaxSize
.w
>= 0) then mLayMaxSize
.w
+= mFrameWidth
*2;
726 if (mLayMaxSize
.h
>= 0) then mLayMaxSize
.h
+= mFrameHeight
*2;
730 // ////////////////////////////////////////////////////////////////////////// //
731 function THControl
.parsePos (par
: TTextParser
): TLayPos
;
735 if (par
.eatDelim('[')) then ech
:= ']' else par
.expectDelim('(');
736 result
.x
:= par
.expectInt();
737 par
.eatDelim(','); // optional comma
738 result
.y
:= par
.expectInt();
739 par
.eatDelim(','); // optional comma
740 par
.expectDelim(ech
);
743 function THControl
.parseSize (par
: TTextParser
): TLaySize
;
747 if (par
.eatDelim('[')) then ech
:= ']' else par
.expectDelim('(');
748 result
.w
:= par
.expectInt();
749 par
.eatDelim(','); // optional comma
750 result
.h
:= par
.expectInt();
751 par
.eatDelim(','); // optional comma
752 par
.expectDelim(ech
);
755 function THControl
.parseBool (par
: TTextParser
): Boolean;
758 par
.eatIdOrStr('true', false) or
759 par
.eatIdOrStr('yes', false) or
760 par
.eatIdOrStr('tan', false);
763 if (not par
.eatIdOrStr('false', false)) and (not par
.eatIdOrStr('no', false)) and (not par
.eatIdOrStr('ona', false)) then
765 par
.error('boolean value expected');
770 function THControl
.parseAnyAlign (par
: TTextParser
): Integer;
772 if (par
.eatIdOrStr('left', false)) or (par
.eatIdOrStr('top', false)) then result
:= -1
773 else if (par
.eatIdOrStr('right', false)) or (par
.eatIdOrStr('bottom', false)) then result
:= 1
774 else if (par
.eatIdOrStr('center', false)) then result
:= 0
775 else par
.error('invalid align value');
778 function THControl
.parseHAlign (par
: TTextParser
): Integer;
780 if (par
.eatIdOrStr('left', false)) then result
:= -1
781 else if (par
.eatIdOrStr('right', false)) then result
:= 1
782 else if (par
.eatIdOrStr('center', false)) then result
:= 0
783 else par
.error('invalid horizontal align value');
786 function THControl
.parseVAlign (par
: TTextParser
): Integer;
788 if (par
.eatIdOrStr('top', false)) then result
:= -1
789 else if (par
.eatIdOrStr('bottom', false)) then result
:= 1
790 else if (par
.eatIdOrStr('center', false)) then result
:= 0
791 else par
.error('invalid vertical align value');
794 procedure THControl
.parseTextAlign (par
: TTextParser
; var h
, v
: Integer);
796 wasH
: Boolean = false;
797 wasV
: Boolean = false;
801 if (par
.eatIdOrStr('left', false)) then
803 if wasH
then par
.error('too many align directives');
808 if (par
.eatIdOrStr('right', false)) then
810 if wasH
then par
.error('too many align directives');
815 if (par
.eatIdOrStr('hcenter', false)) then
817 if wasH
then par
.error('too many align directives');
822 if (par
.eatIdOrStr('top', false)) then
824 if wasV
then par
.error('too many align directives');
829 if (par
.eatIdOrStr('bottom', false)) then
831 if wasV
then par
.error('too many align directives');
836 if (par
.eatIdOrStr('vcenter', false)) then
838 if wasV
then par
.error('too many align directives');
843 if (par
.eatIdOrStr('center', false)) then
845 if wasV
or wasH
then par
.error('too many align directives');
854 if not wasV
and not wasH
then par
.error('invalid align value');
857 function THControl
.parseOrientation (const prname
: AnsiString; par
: TTextParser
): Boolean;
859 if (strEquCI1251(prname
, 'orientation')) or (strEquCI1251(prname
, 'orient')) then
861 if (par
.eatIdOrStr('horizontal', false)) or (par
.eatIdOrStr('horiz', false)) then mHoriz
:= true
862 else if (par
.eatIdOrStr('vertical', false)) or (par
.eatIdOrStr('vert', false)) then mHoriz
:= false
863 else par
.error('`horizontal` or `vertical` expected');
872 // par should be on '{'; final '}' is eaten
873 procedure THControl
.parseProperties (par
: TTextParser
);
877 if (not par
.eatDelim('{')) then exit
;
878 while (not par
.eatDelim('}')) do
880 if (not par
.isIdOrStr
) then par
.error('property name expected');
883 par
.eatDelim(':'); // optional
884 if not parseProperty(pn
, par
) then par
.errorfmt('invalid property name ''%s''', [pn
]);
885 par
.eatDelim(','); // optional
889 // par should be on '{'
890 procedure THControl
.parseChildren (par
: TTextParser
);
895 par
.expectDelim('{');
896 while (not par
.eatDelim('}')) do
898 if (not par
.isIdOrStr
) then par
.error('control name expected');
899 cc
:= findCtlClass(par
.tokStr
);
900 if (cc
= nil) then par
.errorfmt('unknown control name: ''%s''', [par
.tokStr
]);
901 //writeln('children for <', par.tokStr, '>: <', cc.className, '>');
903 par
.eatDelim(':'); // optional
905 //writeln(' mHoriz=', ctl.mHoriz);
907 ctl
.parseProperties(par
);
912 //writeln(': ', ctl.mDefSize.toString);
914 par
.eatDelim(','); // optional
919 function THControl
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
922 if (strEquCI1251(prname
, 'id')) then begin mId
:= par
.expectStrOrId(true); exit
; end; // allow empty strings
923 if (strEquCI1251(prname
, 'flex')) then begin flex
:= par
.expectInt(); exit
; end;
925 if (strEquCI1251(prname
, 'defsize')) or (strEquCI1251(prname
, 'size')) then begin mDefSize
:= parseSize(par
); exit
; end;
926 if (strEquCI1251(prname
, 'maxsize')) then begin mMaxSize
:= parseSize(par
); exit
; end;
927 if (strEquCI1251(prname
, 'defwidth')) or (strEquCI1251(prname
, 'width')) then begin mDefSize
.w
:= par
.expectInt(); exit
; end;
928 if (strEquCI1251(prname
, 'defheight')) or (strEquCI1251(prname
, 'height')) then begin mDefSize
.h
:= par
.expectInt(); exit
; end;
929 if (strEquCI1251(prname
, 'maxwidth')) then begin mMaxSize
.w
:= par
.expectInt(); exit
; end;
930 if (strEquCI1251(prname
, 'maxheight')) then begin mMaxSize
.h
:= par
.expectInt(); exit
; end;
932 if (strEquCI1251(prname
, 'wrap')) then begin mCanWrap
:= parseBool(par
); exit
; end;
933 if (strEquCI1251(prname
, 'linestart')) then begin mLineStart
:= parseBool(par
); exit
; end;
934 if (strEquCI1251(prname
, 'expand')) then begin mExpand
:= parseBool(par
); exit
; end;
936 if (strEquCI1251(prname
, 'align')) then begin mAlign
:= parseAnyAlign(par
); exit
; end;
937 if (strEquCI1251(prname
, 'hgroup')) then begin mHGroup
:= par
.expectStrOrId(true); exit
; end; // allow empty strings
938 if (strEquCI1251(prname
, 'vgroup')) then begin mVGroup
:= par
.expectStrOrId(true); exit
; end; // allow empty strings
940 if (strEquCI1251(prname
, 'canfocus')) then begin mCanFocus
:= parseBool(par
); exit
; end;
941 if (strEquCI1251(prname
, 'enabled')) then begin mEnabled
:= parseBool(par
); exit
; end;
942 if (strEquCI1251(prname
, 'disabled')) then begin mEnabled
:= not parseBool(par
); exit
; end;
943 if (strEquCI1251(prname
, 'escclose')) then begin mEscClose
:= not parseBool(par
); exit
; end;
944 if (strEquCI1251(prname
, 'eatkeys')) then begin mEatKeys
:= not parseBool(par
); exit
; end;
949 // ////////////////////////////////////////////////////////////////////////// //
950 procedure THControl
.activated ();
955 procedure THControl
.blurred ();
961 function THControl
.topLevel (): THControl
; inline;
964 while (result
.mParent
<> nil) do result
:= result
.mParent
;
968 function THControl
.getEnabled (): Boolean;
973 if (not mEnabled
) or (mWidth
< 1) or (mHeight
< 1) then exit
;
975 while (ctl
<> nil) do
977 if (not ctl
.mEnabled
) or (ctl
.mWidth
< 1) or (ctl
.mHeight
< 1) then exit
;
984 procedure THControl
.setEnabled (v
: Boolean); inline;
986 if (mEnabled
= v
) then exit
;
988 if not v
and focused
then setFocused(false);
992 function THControl
.getFocused (): Boolean; inline;
994 if (mParent
= nil) then result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = self
) else result
:= (topLevel
.mFocused
= self
);
998 procedure THControl
.setFocused (v
: Boolean); inline;
1005 if (tl
.mFocused
= self
) then
1008 tl
.mFocused
:= tl
.findNextFocus(self
);
1009 if (tl
.mFocused
= self
) then tl
.mFocused
:= nil;
1013 if (not mEnabled
) or (not mCanFocus
) then exit
;
1014 if (tl
.mFocused
<> self
) then
1016 if (tl
.mFocused
<> nil) then tl
.mFocused
.blurred();
1017 tl
.mFocused
:= self
;
1018 if (tl
.mGrab
<> self
) then tl
.mGrab
:= nil;
1024 function THControl
.isMyChild (ctl
: THControl
): Boolean;
1027 while (ctl
<> nil) do
1029 if (ctl
.mParent
= self
) then exit
;
1036 // returns `true` if global coords are inside this control
1037 function THControl
.toLocal (var x
, y
: Integer): Boolean;
1042 while (ctl
<> nil) do
1048 result
:= (x
>= 0) and (y
>= 0) and (x
< mWidth
) and (y
< mHeight
);
1051 function THControl
.toLocal (gx
, gy
: Integer; out x
, y
: Integer): Boolean; inline;
1055 result
:= toLocal(x
, y
);
1058 procedure THControl
.toGlobal (var x
, y
: Integer);
1063 while (ctl
<> nil) do
1071 procedure THControl
.toGlobal (lx
, ly
: Integer; out x
, y
: Integer); inline;
1079 // x and y are global coords
1080 function THControl
.controlAtXY (x
, y
: Integer): THControl
;
1086 if (not mEnabled
) or (mWidth
< 1) or (mHeight
< 1) then exit
;
1087 if not toLocal(x
, y
, lx
, ly
) then exit
;
1088 for f
:= High(mChildren
) downto 0 do
1090 result
:= mChildren
[f
].controlAtXY(x
, y
);
1091 if (result
<> nil) then exit
;
1097 function THControl
.prevSibling (): THControl
;
1101 if (mParent
<> nil) then
1103 for f
:= 1 to High(mParent
.mChildren
) do
1105 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
-1]; exit
; end;
1111 function THControl
.nextSibling (): THControl
;
1115 if (mParent
<> nil) then
1117 for f
:= 0 to High(mParent
.mChildren
)-1 do
1119 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
+1]; exit
; end;
1125 function THControl
.firstChild (): THControl
; inline;
1127 if (Length(mChildren
) <> 0) then result
:= mChildren
[0] else result
:= nil;
1130 function THControl
.lastChild (): THControl
; inline;
1132 if (Length(mChildren
) <> 0) then result
:= mChildren
[High(mChildren
)] else result
:= nil;
1136 function THControl
.findFirstFocus (): THControl
;
1143 for f
:= 0 to High(mChildren
) do
1145 result
:= mChildren
[f
].findFirstFocus();
1146 if (result
<> nil) then exit
;
1148 if mCanFocus
then result
:= self
;
1153 function THControl
.findLastFocus (): THControl
;
1160 for f
:= High(mChildren
) downto 0 do
1162 result
:= mChildren
[f
].findLastFocus();
1163 if (result
<> nil) then exit
;
1165 if mCanFocus
then result
:= self
;
1170 function THControl
.findNextFocus (cur
: THControl
): THControl
;
1175 if not isMyChild(cur
) then cur
:= nil;
1176 if (cur
= nil) then begin result
:= findFirstFocus(); exit
; end;
1177 result
:= cur
.findFirstFocus();
1178 if (result
<> nil) and (result
<> cur
) then exit
;
1181 cur
:= cur
.nextSibling
;
1182 if (cur
= nil) then break
;
1183 result
:= cur
.findFirstFocus();
1184 if (result
<> nil) then exit
;
1186 result
:= findFirstFocus();
1191 function THControl
.findPrevFocus (cur
: THControl
): THControl
;
1196 if not isMyChild(cur
) then cur
:= nil;
1197 if (cur
= nil) then begin result
:= findLastFocus(); exit
; end;
1199 result
:= cur
.findLastFocus();
1200 if (result
<> nil) and (result
<> cur
) then exit
;
1203 cur
:= cur
.prevSibling
;
1204 if (cur
= nil) then break
;
1205 result
:= cur
.findLastFocus();
1206 if (result
<> nil) then exit
;
1208 result
:= findLastFocus();
1213 procedure THControl
.appendChild (ctl
: THControl
);
1215 if (ctl
= nil) then exit
;
1216 if (ctl
.mParent
<> nil) then exit
;
1217 SetLength(mChildren
, Length(mChildren
)+1);
1218 mChildren
[High(mChildren
)] := ctl
;
1219 ctl
.mParent
:= self
;
1220 Inc(ctl
.mX
, mFrameWidth
);
1221 Inc(ctl
.mY
, mFrameHeight
);
1222 if (ctl
.mWidth
> 0) and (ctl
.mHeight
> 0) and
1223 (ctl
.mX
+ctl
.mWidth
> mFrameWidth
) and (ctl
.mY
+ctl
.mHeight
> mFrameHeight
) then
1225 if (mWidth
+mFrameWidth
< ctl
.mX
+ctl
.mWidth
) then mWidth
:= ctl
.mX
+ctl
.mWidth
+mFrameWidth
;
1226 if (mHeight
+mFrameHeight
< ctl
.mY
+ctl
.mHeight
) then mHeight
:= ctl
.mY
+ctl
.mHeight
+mFrameHeight
;
1228 //if (mFocused = nil) and ctl.mEnabled and ctl.mCanFocus and (ctl.mWidth > 0) and (ctl.mHeight > 0) then mFocused := ctl;
1232 // ////////////////////////////////////////////////////////////////////////// //
1233 procedure THControl
.setScissorGLInternal (x
, y
, w
, h
: Integer);
1235 if not scallowed
then exit
;
1236 x
:= trunc(x
*gh_ui_scale
);
1237 y
:= trunc(y
*gh_ui_scale
);
1238 w
:= trunc(w
*gh_ui_scale
);
1239 h
:= trunc(h
*gh_ui_scale
);
1240 scis
.combineRect(x
, y
, w
, h
);
1243 procedure THControl
.setScissor (lx
, ly
, lw
, lh
: Integer);
1246 //ox, oy, ow, oh: Integer;
1248 if not scallowed
then exit
;
1249 //ox := lx; oy := ly; ow := lw; oh := lh;
1250 if not intersectRect(lx
, ly
, lw
, lh
, 0, 0, mWidth
, mHeight
) then
1252 //writeln('oops: <', self.className, '>: old=(', ox, ',', oy, ')-[', ow, ',', oh, ']');
1253 glScissor(0, 0, 0, 0);
1256 toGlobal(lx
, ly
, gx
, gy
);
1257 setScissorGLInternal(gx
, gy
, lw
, lh
);
1260 procedure THControl
.resetScissor (fullArea
: Boolean); inline;
1262 if not scallowed
then exit
;
1265 setScissor(0, 0, mWidth
, mHeight
);
1269 setScissor(mFrameWidth
, mFrameHeight
, mWidth
-mFrameWidth
*2, mHeight
-mFrameHeight
*2);
1274 // ////////////////////////////////////////////////////////////////////////// //
1275 procedure THControl
.draw ();
1280 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1281 toGlobal(0, 0, gx
, gy
);
1282 //conwritefln('[%s]: (%d,%d)-(%d,%d) (%d,%d)', [ClassName, mX, mY, mWidth, mHeight, x, y]);
1284 scis
.save(true); // scissoring enabled
1287 resetScissor(true); // full area
1288 drawControl(gx
, gy
);
1289 resetScissor(false); // client area
1290 for f
:= 0 to High(mChildren
) do mChildren
[f
].draw();
1291 resetScissor(true); // full area
1292 drawControlPost(gx
, gy
);
1299 procedure THControl
.drawControl (gx
, gy
: Integer);
1301 if (mParent
= nil) then darkenRect(gx
, gy
, mWidth
, mHeight
, 64);
1304 procedure THControl
.drawControlPost (gx
, gy
: Integer);
1307 if mDrawShadow
and (mWidth
> 0) and (mHeight
> 0) then
1309 setScissorGLInternal(gx
+8, gy
+8, mWidth
, mHeight
);
1310 darkenRect(gx
+mWidth
, gy
+8, 8, mHeight
, 128);
1311 darkenRect(gx
+8, gy
+mHeight
, mWidth
-8, 8, 128);
1316 // ////////////////////////////////////////////////////////////////////////// //
1317 function THControl
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1322 if not mEnabled
then exit
;
1323 if (mParent
= nil) then
1325 if (mGrab
<> nil) then
1327 result
:= mGrab
.mouseEvent(ev
);
1328 if (ev
.release
) then mGrab
:= nil;
1332 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1333 ctl
:= controlAtXY(ev
.x
, ev
.y
);
1334 if (ctl
<> nil) and (ctl
<> self
) then
1336 if (ctl
<> topLevel
.mFocused
) then ctl
.setFocused(true);
1337 result
:= ctl
.mouseEvent(ev
);
1339 else if (ctl
= self
) and assigned(actionCB
) then
1346 function THControl
.keyEvent (var ev
: THKeyEvent
): Boolean;
1351 if not mEnabled
then exit
;
1352 if (topLevel
.mFocused
<> self
) and isMyChild(topLevel
.mFocused
) and topLevel
.mFocused
.mEnabled
then result
:= topLevel
.mFocused
.keyEvent(ev
);
1353 if (mParent
= nil) then
1355 if (ev
= 'S-Tab') then
1358 ctl
:= findPrevFocus(mFocused
);
1359 if (ctl
<> mFocused
) then
1366 if (ev
= 'Tab') then
1369 ctl
:= findNextFocus(mFocused
);
1370 if (ctl
<> mFocused
) then
1377 if mEscClose
and (ev
= 'Escape') then
1380 uiRemoveWindow(self
);
1384 if mEatKeys
then result
:= true;
1388 // ////////////////////////////////////////////////////////////////////////// //
1389 constructor THTopWindow
.Create (const atitle
: AnsiString; ax
, ay
: Integer; aw
: Integer=-1; ah
: Integer=-1);
1391 inherited Create(ax
, ay
, aw
, ah
);
1395 if (mWidth
< mFrameWidth
*2+3*8) then mWidth
:= mFrameWidth
*2+3*8;
1396 if (mHeight
< mFrameHeight
*2) then mHeight
:= mFrameHeight
*2;
1397 if (Length(mTitle
) > 0) then
1399 if (mWidth
< Length(mTitle
)*8+mFrameWidth
*2+3*8) then mWidth
:= Length(mTitle
)*8+mFrameWidth
*2+3*8;
1402 mDrawShadow
:= true;
1403 mWaitingClose
:= false;
1409 function THTopWindow
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1411 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
1413 mTitle
:= par
.expectStrOrId(true);
1417 if (strEquCI1251(prname
, 'children')) then
1423 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
1424 result
:= inherited parseProperty(prname
, par
);
1428 procedure THTopWindow
.centerInScreen ();
1430 if (mWidth
> 0) and (mHeight
> 0) then
1432 mX
:= trunc((gScrWidth
/gh_ui_scale
-mWidth
)/2);
1433 mY
:= trunc((gScrHeight
/gh_ui_scale
-mHeight
)/2);
1438 procedure THTopWindow
.drawControl (gx
, gy
: Integer);
1440 fillRect(gx
, gy
, mWidth
, mHeight
, TGxRGBA
.Create(0, 0, 128));
1444 procedure THTopWindow
.drawControlPost (gx
, gy
: Integer);
1453 drawRectUI(mX
+4, mY
+4, mWidth
-8, mHeight
-8, TGxRGBA
.Create(r
, g
, b
));
1457 drawRectUI(mX
+3, mY
+3, mWidth
-6, mHeight
-6, TGxRGBA
.Create(r
, g
, b
));
1458 drawRectUI(mX
+5, mY
+5, mWidth
-10, mHeight
-10, TGxRGBA
.Create(r
, g
, b
));
1459 setScissor(mFrameWidth
, 0, 3*8, 8);
1460 fillRect(mX
+mFrameWidth
, mY
, 3*8, 8, TGxRGBA
.Create(0, 0, 128));
1461 drawText8(mX
+mFrameWidth
, mY
, '[ ]', TGxRGBA
.Create(r
, g
, b
));
1462 if mInClose
then drawText8(mX
+mFrameWidth
+7, mY
, '#', TGxRGBA
.Create(0, 255, 0))
1463 else drawText8(mX
+mFrameWidth
+7, mY
, '*', TGxRGBA
.Create(0, 255, 0));
1465 if (Length(mTitle
) > 0) then
1467 setScissor(mFrameWidth
+3*8, 0, mWidth
-mFrameWidth
*2-3*8, 8);
1468 tx
:= (mX
+3*8)+((mWidth
-3*8)-Length(mTitle
)*8) div 2;
1469 fillRect(tx
-3, mY
, Length(mTitle
)*8+3+2, 8, TGxRGBA
.Create(0, 0, 128));
1470 drawText8(tx
, mY
, mTitle
, TGxRGBA
.Create(r
, g
, b
));
1472 inherited drawControlPost(gx
, gy
);
1476 procedure THTopWindow
.activated ();
1478 if (mFocused
= nil) or (mFocused
= self
) then
1480 mFocused
:= findFirstFocus();
1481 if (mFocused
<> nil) and (mFocused
<> self
) then mFocused
.activated();
1487 procedure THTopWindow
.blurred ();
1490 mWaitingClose
:= false;
1496 function THTopWindow
.keyEvent (var ev
: THKeyEvent
): Boolean;
1498 result
:= inherited keyEvent(ev
);
1499 if not getFocused
then exit
;
1500 if (ev
= 'M-F3') then
1502 uiRemoveWindow(self
);
1509 function THTopWindow
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1514 if not mEnabled
then exit
;
1515 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1519 mX
+= ev
.x
-mDragStartX
;
1520 mY
+= ev
.y
-mDragStartY
;
1521 mDragStartX
:= ev
.x
;
1522 mDragStartY
:= ev
.y
;
1523 if (ev
.release
) then mDragging
:= false;
1528 if toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1534 if (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8) then
1536 //uiRemoveWindow(self);
1537 mWaitingClose
:= true;
1543 mDragStartX
:= ev
.x
;
1544 mDragStartY
:= ev
.y
;
1549 if (lx
< mFrameWidth
) or (lx
>= mWidth
-mFrameWidth
) or (ly
>= mHeight
-mFrameHeight
) then
1552 mDragStartX
:= ev
.x
;
1553 mDragStartY
:= ev
.y
;
1559 if (ev
.release
) then
1561 if mWaitingClose
and (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8) then
1563 uiRemoveWindow(self
);
1567 mWaitingClose
:= false;
1573 if mWaitingClose
then
1575 mInClose
:= (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8);
1584 if (not ev
.motion
) then mWaitingClose
:= false;
1587 result
:= inherited mouseEvent(ev
);
1591 // ////////////////////////////////////////////////////////////////////////// //
1592 constructor THCtlSimpleText
.Create (ax
, ay
: Integer);
1595 inherited Create(ax
, ay
, 4, 4);
1599 destructor THCtlSimpleText
.Destroy ();
1606 procedure THCtlSimpleText
.appendItem (const atext
: AnsiString; acentered
: Boolean=false; ahline
: Boolean=false);
1610 if (Length(atext
)*8+3*8+2 > mWidth
) then mWidth
:= Length(atext
)*8+3*8+2;
1611 SetLength(mItems
, Length(mItems
)+1);
1612 it
:= @mItems
[High(mItems
)];
1614 it
.centered
:= acentered
;
1616 if (Length(mItems
)*8 > mHeight
) then mHeight
:= Length(mItems
)*8;
1620 procedure THCtlSimpleText
.drawControl (gx
, gy
: Integer);
1626 for f
:= 0 to High(mItems
) do
1633 if it
.centered
then begin b
:= 255; tx
:= gx
+(mWidth
-Length(it
.title
)*8) div 2; end;
1637 if (Length(it
.title
) = 0) then
1639 drawHLine(gx
+4, gy
+3, mWidth
-8, TGxRGBA
.Create(r
, g
, b
));
1641 else if (tx
-3 > gx
+4) then
1643 drawHLine(gx
+4, gy
+3, tx
-3-(gx
+3), TGxRGBA
.Create(r
, g
, b
));
1644 drawHLine(tx
+Length(it
.title
)*8, gy
+3, mWidth
-4, TGxRGBA
.Create(r
, g
, b
));
1647 drawText8(tx
, gy
, it
.title
, TGxRGBA
.Create(r
, g
, b
));
1653 function THCtlSimpleText
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1657 result
:= inherited mouseEvent(ev
);
1658 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1665 function THCtlSimpleText
.keyEvent (var ev
: THKeyEvent
): Boolean;
1667 result
:= inherited keyEvent(ev
);
1671 // ////////////////////////////////////////////////////////////////////////// //
1672 constructor THCtlCBListBox
.Create (ax
, ay
: Integer);
1676 inherited Create(ax
, ay
, 4, 4);
1680 destructor THCtlCBListBox
.Destroy ();
1687 procedure THCtlCBListBox
.appendItem (const atext
: AnsiString; bv
: PBoolean; aaction
: TActionCB
=nil);
1691 if (Length(atext
)*8+3*8+2 > mWidth
) then mWidth
:= Length(atext
)*8+3*8+2;
1692 SetLength(mItems
, Length(mItems
)+1);
1693 it
:= @mItems
[High(mItems
)];
1696 it
.actionCB
:= aaction
;
1697 if (Length(mItems
)*8 > mHeight
) then mHeight
:= Length(mItems
)*8;
1698 if (mCurIndex
< 0) then mCurIndex
:= 0;
1702 procedure THCtlCBListBox
.drawControl (gx
, gy
: Integer);
1707 for f
:= 0 to High(mItems
) do
1710 if (mCurIndex
= f
) then fillRect(gx
, gy
, mWidth
, 8, TGxRGBA
.Create(0, 128, 0));
1711 if (it
.varp
<> nil) then
1713 if it
.varp
^ then drawText8(gx
, gy
, '[x]', TGxRGBA
.Create(255, 255, 255)) else drawText8(gx
, gy
, '[ ]', TGxRGBA
.Create(255, 255, 255));
1714 drawText8(gx
+3*8+2, gy
, it
.title
, TGxRGBA
.Create(255, 255, 0));
1716 else if (Length(it
.title
) > 0) then
1718 tx
:= gx
+(mWidth
-Length(it
.title
)*8) div 2;
1719 if (tx
-3 > gx
+4) then
1721 drawHLine(gx
+4, gy
+3, tx
-3-(gx
+3), TGxRGBA
.Create(255, 255, 255));
1722 drawHLine(tx
+Length(it
.title
)*8, gy
+3, mWidth
-4, TGxRGBA
.Create(255, 255, 255));
1724 drawText8(tx
, gy
, it
.title
, TGxRGBA
.Create(255, 255, 255));
1728 drawHLine(gx
+4, gy
+3, mWidth
-8, TGxRGBA
.Create(255, 255, 255));
1735 function THCtlCBListBox
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1740 result
:= inherited mouseEvent(ev
);
1741 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1744 if (ev
= 'lmb') then
1747 if (ly
>= 0) and (ly
< Length(mItems
)) then
1750 if (it
.varp
<> nil) then
1753 it
.varp
^ := not it
.varp
^;
1754 if assigned(it
.actionCB
) then it
.actionCB(self
, Integer(it
.varp
^));
1755 if assigned(actionCB
) then actionCB(self
, ly
);
1763 function THCtlCBListBox
.keyEvent (var ev
: THKeyEvent
): Boolean;
1767 result
:= inherited keyEvent(ev
);
1768 if not getFocused
then exit
;
1770 if (ev
= 'Home') or (ev
= 'PageUp') then
1775 if (ev
= 'End') or (ev
= 'PageDown') then
1778 mCurIndex
:= High(mItems
);
1783 if (Length(mItems
) > 0) then
1785 if (mCurIndex
< 0) then mCurIndex
:= Length(mItems
);
1786 while (mCurIndex
> 0) do
1789 if (mItems
[mCurIndex
].varp
<> nil) then break
;
1797 if (ev
= 'Down') then
1800 if (Length(mItems
) > 0) then
1802 if (mCurIndex
< 0) then mCurIndex
:= -1;
1803 while (mCurIndex
< High(mItems
)) do
1806 if (mItems
[mCurIndex
].varp
<> nil) then break
;
1814 if (ev
= 'Space') or (ev
= 'Enter') then
1817 if (mCurIndex
>= 0) and (mCurIndex
< Length(mItems
)) and (mItems
[mCurIndex
].varp
<> nil) then
1819 it
:= @mItems
[mCurIndex
];
1820 it
.varp
^ := not it
.varp
^;
1821 if assigned(it
.actionCB
) then it
.actionCB(self
, Integer(it
.varp
^));
1822 if assigned(actionCB
) then actionCB(self
, mCurIndex
);
1828 // ////////////////////////////////////////////////////////////////////////// //
1829 constructor THCtlBox
.Create (ahoriz
: Boolean);
1837 function THCtlBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1839 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
1840 if (strEquCI1251(prname
, 'frame')) then
1842 mHasFrame
:= parseBool(par
);
1843 if (mHasFrame
) then begin mFrameWidth
:= 8; mFrameHeight
:= 8; end else begin mFrameWidth
:= 0; mFrameHeight
:= 0; end;
1847 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
1849 mCaption
:= par
.expectStrOrId(true);
1850 mDefSize
:= TLaySize
.Create(Length(mCaption
)*8+3, 8);
1854 if (strEquCI1251(prname
, 'children')) then
1860 result
:= inherited parseProperty(prname
, par
);
1864 procedure THCtlBox
.drawControl (gx
, gy
: Integer);
1869 if focused
then begin r
:= 255; g
:= 255; b
:= 255; end else begin r
:= 255; g
:= 255; b
:= 0; end;
1873 drawRectUI(gx
+3, gy
+3, mWidth
-6, mHeight
-6, TGxRGBA
.Create(r
, g
, b
));
1876 if (Length(mCaption
) > 0) then
1878 setScissor(mFrameWidth
+1, 0, mWidth
-mFrameWidth
-2, 8);
1879 tx
:= gx
+((mWidth
-Length(mCaption
)*8) div 2);
1880 if mHasFrame
then fillRect(tx
-2, gy
, Length(mCaption
)*8+3, 8, TGxRGBA
.Create(0, 0, 128));
1881 drawText8(tx
, gy
, mCaption
, TGxRGBA
.Create(r
, g
, b
));
1886 function THCtlBox
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1890 result
:= inherited mouseEvent(ev
);
1891 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1898 //TODO: navigation with arrow keys, according to box orientation
1899 function THCtlBox
.keyEvent (var ev
: THKeyEvent
): Boolean;
1901 result
:= inherited keyEvent(ev
);
1905 // ////////////////////////////////////////////////////////////////////////// //
1906 procedure THCtlHBox
.AfterConstruction ();
1908 inherited AfterConstruction();
1913 // ////////////////////////////////////////////////////////////////////////// //
1914 procedure THCtlVBox
.AfterConstruction ();
1916 inherited AfterConstruction();
1922 // ////////////////////////////////////////////////////////////////////////// //
1923 procedure THCtlSpan
.AfterConstruction ();
1925 inherited AfterConstruction();
1931 function THCtlSpan
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1933 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
1934 result
:= inherited parseProperty(prname
, par
);
1938 procedure THCtlSpan
.drawControl (gx
, gy
: Integer);
1943 // ////////////////////////////////////////////////////////////////////// //
1944 function THCtlLine
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1946 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
1947 result
:= inherited parseProperty(prname
, par
);
1951 procedure THCtlLine
.drawControl (gx
, gy
: Integer);
1955 drawHLine(gx
, gy
+(mHeight
div 2), mWidth
, TGxRGBA
.Create(255, 255, 255));
1959 drawVLine(gx
+(mWidth
div 2), gy
, mHeight
, TGxRGBA
.Create(255, 255, 255));
1964 // ////////////////////////////////////////////////////////////////////////// //
1965 procedure THCtlHLine
.AfterConstruction ();
1973 // ////////////////////////////////////////////////////////////////////////// //
1974 procedure THCtlVLine
.AfterConstruction ();
1983 // ////////////////////////////////////////////////////////////////////////// //
1984 constructor THCtlTextLabel
.Create (const atext
: AnsiString);
1988 mDefSize
:= TLaySize
.Create(Length(atext
)*8, 8);
1992 procedure THCtlTextLabel
.AfterConstruction ();
1994 inherited AfterConstruction();
1998 if (mDefSize
.h
<= 0) then mDefSize
.h
:= 8;
2002 function THCtlTextLabel
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2004 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
2006 mText
:= par
.expectStrOrId(true);
2007 mDefSize
:= TLaySize
.Create(Length(mText
)*8, 8);
2011 if (strEquCI1251(prname
, 'textalign')) then
2013 parseTextAlign(par
, mHAlign
, mVAlign
);
2017 result
:= inherited parseProperty(prname
, par
);
2021 procedure THCtlTextLabel
.drawControl (gx
, gy
: Integer);
2023 xpos
, ypos
: Integer;
2026 fillRect(gx
, gy
, mWidth
, mHeight
, TGxRGBA
.Create(96, 96, 0));
2027 drawRectUI(gx
, gy
, mWidth
, mHeight
, TGxRGBA
.Create(96, 96, 96));
2029 if (Length(mText
) > 0) then
2031 if (mHAlign
< 0) then xpos
:= 0
2032 else if (mHAlign
> 0) then xpos
:= mWidth
-Length(mText
)*8
2033 else xpos
:= (mWidth
-Length(mText
)*8) div 2;
2035 if (mVAlign
< 0) then ypos
:= 0
2036 else if (mVAlign
> 0) then ypos
:= mHeight
-8
2037 else ypos
:= (mHeight
-8) div 2;
2039 drawText8(gx
+xpos
, gy
+ypos
, mText
, TGxRGBA
.Create(255, 255, 255));
2044 function THCtlTextLabel
.mouseEvent (var ev
: THMouseEvent
): Boolean;
2048 result
:= inherited mouseEvent(ev
);
2049 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
2056 function THCtlTextLabel
.keyEvent (var ev
: THKeyEvent
): Boolean;
2058 result
:= inherited keyEvent(ev
);
2063 registerCtlClass(THCtlHBox
, 'hbox');
2064 registerCtlClass(THCtlVBox
, 'vbox');
2065 registerCtlClass(THCtlSpan
, 'span');
2066 registerCtlClass(THCtlHLine
, 'hline');
2067 registerCtlClass(THCtlVLine
, 'vline');
2068 registerCtlClass(THCtlTextLabel
, 'label');