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 blurred (); override;
224 closeCB
: TActionCB
; // called after window was removed from ui window list
227 constructor Create (const atitle
: AnsiString; ax
, ay
: Integer; aw
: Integer=-1; ah
: Integer=-1);
229 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
231 procedure centerInScreen ();
233 // `sx` and `sy` are screen coordinates
234 procedure drawControl (gx
, gy
: Integer); override;
235 procedure drawControlPost (gx
, gy
: Integer); override;
237 function keyEvent (var ev
: THKeyEvent
): Boolean; override; // returns `true` if event was eaten
238 function mouseEvent (var ev
: THMouseEvent
): Boolean; override; // returns `true` if event was eaten
241 property freeOnClose
: Boolean read mFreeOnClose write mFreeOnClose
;
245 THCtlSimpleText
= class(THControl
)
255 mItems
: array of TItem
;
258 constructor Create (ax
, ay
: Integer);
259 destructor Destroy (); override;
261 procedure appendItem (const atext
: AnsiString; acentered
: Boolean=false; ahline
: Boolean=false);
263 procedure drawControl (gx
, gy
: Integer); override;
265 function mouseEvent (var ev
: THMouseEvent
): Boolean; override;
266 function keyEvent (var ev
: THKeyEvent
): Boolean; override;
270 THCtlCBListBox
= class(THControl
)
280 mItems
: array of TItem
;
284 constructor Create (ax
, ay
: Integer);
285 destructor Destroy (); override;
287 procedure appendItem (const atext
: AnsiString; bv
: PBoolean; aaction
: TActionCB
=nil);
289 procedure drawControl (gx
, gy
: Integer); override;
291 function mouseEvent (var ev
: THMouseEvent
): Boolean; override;
292 function keyEvent (var ev
: THKeyEvent
): Boolean; override;
295 // ////////////////////////////////////////////////////////////////////// //
296 THCtlBox
= class(THControl
)
299 mCaption
: AnsiString;
302 constructor Create (ahoriz
: Boolean);
304 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
306 procedure drawControl (gx
, gy
: Integer); override;
308 function mouseEvent (var ev
: THMouseEvent
): Boolean; override;
309 function keyEvent (var ev
: THKeyEvent
): Boolean; override;
312 THCtlHBox
= class(THCtlBox
)
314 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
317 THCtlVBox
= class(THCtlBox
)
319 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
322 // ////////////////////////////////////////////////////////////////////// //
323 THCtlSpan
= class(THControl
)
325 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
327 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
329 procedure drawControl (gx
, gy
: Integer); override;
332 // ////////////////////////////////////////////////////////////////////// //
333 THCtlLine
= class(THControl
)
335 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
337 procedure drawControl (gx
, gy
: Integer); override;
340 THCtlHLine
= class(THCtlLine
)
342 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
345 THCtlVLine
= class(THCtlLine
)
347 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
350 // ////////////////////////////////////////////////////////////////////// //
351 THCtlTextLabel
= class(THControl
)
354 mHAlign
: Integer; // -1: left; 0: center; 1: right; default: left
355 mVAlign
: Integer; // -1: top; 0: center; 1: bottom; default: center
358 constructor Create (const atext
: AnsiString);
360 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
362 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
364 procedure drawControl (gx
, gy
: Integer); override;
366 function mouseEvent (var ev
: THMouseEvent
): Boolean; override;
367 function keyEvent (var ev
: THKeyEvent
): Boolean; override;
371 // ////////////////////////////////////////////////////////////////////////// //
372 function uiMouseEvent (ev
: THMouseEvent
): Boolean;
373 function uiKeyEvent (ev
: THKeyEvent
): Boolean;
377 // ////////////////////////////////////////////////////////////////////////// //
378 procedure uiAddWindow (ctl
: THControl
);
379 procedure uiRemoveWindow (ctl
: THControl
); // will free window if `mFreeOnClose` is `true`
380 function uiVisibleWindow (ctl
: THControl
): Boolean;
383 // ////////////////////////////////////////////////////////////////////////// //
385 procedure uiLayoutCtl (ctl
: THControl
);
388 // ////////////////////////////////////////////////////////////////////////// //
390 gh_ui_scale
: Single = 1.0;
400 // ////////////////////////////////////////////////////////////////////////// //
402 knownCtlClasses
: array of record
403 klass
: THControlClass
;
408 procedure registerCtlClass (aklass
: THControlClass
; const aname
: AnsiString);
410 assert(aklass
<> nil);
411 assert(Length(aname
) > 0);
412 SetLength(knownCtlClasses
, Length(knownCtlClasses
)+1);
413 knownCtlClasses
[High(knownCtlClasses
)].klass
:= aklass
;
414 knownCtlClasses
[High(knownCtlClasses
)].name
:= aname
;
418 function findCtlClass (const aname
: AnsiString): THControlClass
;
422 for f
:= 0 to High(knownCtlClasses
) do
424 if (strEquCI1251(aname
, knownCtlClasses
[f
].name
)) then
426 result
:= knownCtlClasses
[f
].klass
;
434 // ////////////////////////////////////////////////////////////////////////// //
436 TFlexLayouter
= specialize TFlexLayouterBase
<THControl
>;
438 procedure uiLayoutCtl (ctl
: THControl
);
442 if (ctl
= nil) then exit
;
443 lay
:= TFlexLayouter
.Create();
448 //writeln('============================'); lay.dumpFlat();
450 //writeln('=== initial ==='); lay.dump();
452 //lay.calcMaxSizeInternal(0);
455 writeln('=== after first pass ===');
459 writeln('=== after second pass ===');
464 //writeln('=== final ==='); lay.dump();
472 // ////////////////////////////////////////////////////////////////////////// //
474 uiTopList
: array of THControl
= nil;
477 function uiMouseEvent (ev
: THMouseEvent
): Boolean;
483 ev
.x
:= trunc(ev
.x
/gh_ui_scale
);
484 ev
.y
:= trunc(ev
.y
/gh_ui_scale
);
485 ev
.dx
:= trunc(ev
.dx
/gh_ui_scale
); //FIXME
486 ev
.dy
:= trunc(ev
.dy
/gh_ui_scale
); //FIXME
487 if (Length(uiTopList
) = 0) then result
:= false else result
:= uiTopList
[High(uiTopList
)].mouseEvent(ev
);
488 if not result
and (ev
.press
) then
490 for f
:= High(uiTopList
) downto 0 do
492 if uiTopList
[f
].toLocal(ev
.x
, ev
.y
, lx
, ly
) then
495 if uiTopList
[f
].mEnabled
and (f
<> High(uiTopList
)) then
497 uiTopList
[High(uiTopList
)].blurred();
498 ctmp
:= uiTopList
[f
];
500 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
501 uiTopList
[High(uiTopList
)] := ctmp
;
503 result
:= ctmp
.mouseEvent(ev
);
512 function uiKeyEvent (ev
: THKeyEvent
): Boolean;
514 ev
.x
:= trunc(ev
.x
/gh_ui_scale
);
515 ev
.y
:= trunc(ev
.y
/gh_ui_scale
);
516 if (Length(uiTopList
) = 0) then result
:= false else result
:= uiTopList
[High(uiTopList
)].keyEvent(ev
);
517 if (ev
.release
) then begin result
:= true; exit
; end;
526 glMatrixMode(GL_MODELVIEW
);
530 glScalef(gh_ui_scale
, gh_ui_scale
, 1);
531 for f
:= 0 to High(uiTopList
) do
535 if (f
<> High(uiTopList
)) then darkenRect(ctl
.x0
, ctl
.y0
, ctl
.width
, ctl
.height
, 128);
538 glMatrixMode(GL_MODELVIEW
);
544 procedure uiAddWindow (ctl
: THControl
);
548 if (ctl
= nil) then exit
;
550 if not (ctl
is THTopWindow
) then exit
; // alas
551 for f
:= 0 to High(uiTopList
) do
553 if (uiTopList
[f
] = ctl
) then
555 if (f
<> High(uiTopList
)) then
557 uiTopList
[High(uiTopList
)].blurred();
558 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
559 uiTopList
[High(uiTopList
)] := ctl
;
565 if (Length(uiTopList
) > 0) then uiTopList
[High(uiTopList
)].blurred();
566 SetLength(uiTopList
, Length(uiTopList
)+1);
567 uiTopList
[High(uiTopList
)] := ctl
;
572 procedure uiRemoveWindow (ctl
: THControl
);
576 if (ctl
= nil) then exit
;
578 if not (ctl
is THTopWindow
) then exit
; // alas
579 for f
:= 0 to High(uiTopList
) do
581 if (uiTopList
[f
] = ctl
) then
584 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
585 SetLength(uiTopList
, Length(uiTopList
)-1);
586 if (ctl
is THTopWindow
) then
589 if assigned(THTopWindow(ctl
).closeCB
) then THTopWindow(ctl
).closeCB(ctl
, 0);
591 if (THTopWindow(ctl
).mFreeOnClose
) then FreeAndNil(ctl
);
600 function uiVisibleWindow (ctl
: THControl
): Boolean;
605 if (ctl
= nil) then exit
;
607 if not (ctl
is THTopWindow
) then exit
; // alas
608 for f
:= 0 to High(uiTopList
) do
610 if (uiTopList
[f
] = ctl
) then begin result
:= true; exit
; end;
615 // ////////////////////////////////////////////////////////////////////////// //
616 constructor THControl
.Create ();
633 mDrawShadow
:= false;
635 // layouter interface
636 //mDefSize := TLaySize.Create(64, 8); // default size
637 mDefSize
:= TLaySize
.Create(0, 0); // default size
638 mMaxSize
:= TLaySize
.Create(-1, -1); // maximum size
645 mAlign
:= -1; // left/top
650 constructor THControl
.Create (ax
, ay
, aw
, ah
: Integer);
660 destructor THControl
.Destroy ();
664 if (mParent
<> nil) then
667 for f
:= 0 to High(mParent
.mChildren
) do
669 if (mParent
.mChildren
[f
] = self
) then
671 for c
:= f
+1 to High(mParent
.mChildren
) do mParent
.mChildren
[c
-1] := mParent
.mChildren
[c
];
672 SetLength(mParent
.mChildren
, Length(mParent
.mChildren
)-1);
676 for f
:= 0 to High(mChildren
) do
678 mChildren
[f
].mParent
:= nil;
685 // ////////////////////////////////////////////////////////////////////////// //
686 function THControl
.getDefSize (): TLaySize
; inline; begin result
:= mLayDefSize
; end;
687 function THControl
.getMaxSize (): TLaySize
; inline; begin result
:= mLayMaxSize
; end;
688 function THControl
.getFlex (): Integer; inline; begin result
:= mFlex
; end;
689 function THControl
.isHorizBox (): Boolean; inline; begin result
:= mHoriz
; end;
690 procedure THControl
.setHorizBox (v
: Boolean); inline; begin mHoriz
:= v
; end;
691 function THControl
.canWrap (): Boolean; inline; begin result
:= mCanWrap
; end;
692 procedure THControl
.setCanWrap (v
: Boolean); inline; begin mCanWrap
:= v
; end;
693 function THControl
.isLineStart (): Boolean; inline; begin result
:= mLineStart
; end;
694 procedure THControl
.setLineStart (v
: Boolean); inline; begin mLineStart
:= v
; end;
695 function THControl
.getAlign (): Integer; inline; begin result
:= mAlign
; end;
696 procedure THControl
.setAlign (v
: Integer); inline; begin mAlign
:= v
; end;
697 function THControl
.getExpand (): Boolean; inline; begin result
:= mExpand
; end;
698 procedure THControl
.setExpand (v
: Boolean); inline; begin mExpand
:= v
; end;
699 function THControl
.getHGroup (): AnsiString; inline; begin result
:= mHGroup
; end;
700 procedure THControl
.setHGroup (const v
: AnsiString); inline; begin mHGroup
:= v
; end;
701 function THControl
.getVGroup (): AnsiString; inline; begin result
:= mVGroup
; end;
702 procedure THControl
.setVGroup (const v
: AnsiString); inline; begin mVGroup
:= v
; end;
704 function THControl
.getMargins (): TLayMargins
; inline;
706 result
:= TLayMargins
.Create(mFrameHeight
, mFrameWidth
, mFrameHeight
, mFrameWidth
);
709 procedure THControl
.setActualSizePos (constref apos
: TLayPos
; constref asize
: TLaySize
); inline; begin
710 //writeln(self.className, '; pos=', apos.toString, '; size=', asize.toString);
711 if (mParent
<> nil) then
720 procedure THControl
.layPrepare ();
722 mLayDefSize
:= mDefSize
;
723 mLayMaxSize
:= mMaxSize
;
724 if (mLayMaxSize
.w
>= 0) then mLayMaxSize
.w
+= mFrameWidth
*2;
725 if (mLayMaxSize
.h
>= 0) then mLayMaxSize
.h
+= mFrameHeight
*2;
729 // ////////////////////////////////////////////////////////////////////////// //
730 function THControl
.parsePos (par
: TTextParser
): TLayPos
;
734 if (par
.eatDelim('[')) then ech
:= ']' else par
.expectDelim('(');
735 result
.x
:= par
.expectInt();
736 par
.eatDelim(','); // optional comma
737 result
.y
:= par
.expectInt();
738 par
.eatDelim(','); // optional comma
739 par
.expectDelim(ech
);
742 function THControl
.parseSize (par
: TTextParser
): TLaySize
;
746 if (par
.eatDelim('[')) then ech
:= ']' else par
.expectDelim('(');
747 result
.w
:= par
.expectInt();
748 par
.eatDelim(','); // optional comma
749 result
.h
:= par
.expectInt();
750 par
.eatDelim(','); // optional comma
751 par
.expectDelim(ech
);
754 function THControl
.parseBool (par
: TTextParser
): Boolean;
757 par
.eatIdOrStr('true', false) or
758 par
.eatIdOrStr('yes', false) or
759 par
.eatIdOrStr('tan', false);
762 if (not par
.eatIdOrStr('false', false)) and (not par
.eatIdOrStr('no', false)) and (not par
.eatIdOrStr('ona', false)) then
764 par
.error('boolean value expected');
769 function THControl
.parseAnyAlign (par
: TTextParser
): Integer;
771 if (par
.eatIdOrStr('left', false)) or (par
.eatIdOrStr('top', false)) then result
:= -1
772 else if (par
.eatIdOrStr('right', false)) or (par
.eatIdOrStr('bottom', false)) then result
:= 1
773 else if (par
.eatIdOrStr('center', false)) then result
:= 0
774 else par
.error('invalid align value');
777 function THControl
.parseHAlign (par
: TTextParser
): Integer;
779 if (par
.eatIdOrStr('left', false)) then result
:= -1
780 else if (par
.eatIdOrStr('right', false)) then result
:= 1
781 else if (par
.eatIdOrStr('center', false)) then result
:= 0
782 else par
.error('invalid horizontal align value');
785 function THControl
.parseVAlign (par
: TTextParser
): Integer;
787 if (par
.eatIdOrStr('top', false)) then result
:= -1
788 else if (par
.eatIdOrStr('bottom', false)) then result
:= 1
789 else if (par
.eatIdOrStr('center', false)) then result
:= 0
790 else par
.error('invalid vertical align value');
793 procedure THControl
.parseTextAlign (par
: TTextParser
; var h
, v
: Integer);
795 wasH
: Boolean = false;
796 wasV
: Boolean = false;
800 if (par
.eatIdOrStr('left', false)) then
802 if wasH
then par
.error('too many align directives');
807 if (par
.eatIdOrStr('right', false)) then
809 if wasH
then par
.error('too many align directives');
814 if (par
.eatIdOrStr('hcenter', false)) then
816 if wasH
then par
.error('too many align directives');
821 if (par
.eatIdOrStr('top', false)) then
823 if wasV
then par
.error('too many align directives');
828 if (par
.eatIdOrStr('bottom', false)) then
830 if wasV
then par
.error('too many align directives');
835 if (par
.eatIdOrStr('vcenter', false)) then
837 if wasV
then par
.error('too many align directives');
842 if (par
.eatIdOrStr('center', false)) then
844 if wasV
or wasH
then par
.error('too many align directives');
853 if not wasV
and not wasH
then par
.error('invalid align value');
856 function THControl
.parseOrientation (const prname
: AnsiString; par
: TTextParser
): Boolean;
858 if (strEquCI1251(prname
, 'orientation')) or (strEquCI1251(prname
, 'orient')) then
860 if (par
.eatIdOrStr('horizontal', false)) or (par
.eatIdOrStr('horiz', false)) then mHoriz
:= true
861 else if (par
.eatIdOrStr('vertical', false)) or (par
.eatIdOrStr('vert', false)) then mHoriz
:= false
862 else par
.error('`horizontal` or `vertical` expected');
871 // par should be on '{'; final '}' is eaten
872 procedure THControl
.parseProperties (par
: TTextParser
);
876 if (not par
.eatDelim('{')) then exit
;
877 while (not par
.eatDelim('}')) do
879 if (not par
.isIdOrStr
) then par
.error('property name expected');
882 par
.eatDelim(':'); // optional
883 if not parseProperty(pn
, par
) then par
.errorfmt('invalid property name ''%s''', [pn
]);
884 par
.eatDelim(','); // optional
888 // par should be on '{'
889 procedure THControl
.parseChildren (par
: TTextParser
);
894 par
.expectDelim('{');
895 while (not par
.eatDelim('}')) do
897 if (not par
.isIdOrStr
) then par
.error('control name expected');
898 cc
:= findCtlClass(par
.tokStr
);
899 if (cc
= nil) then par
.errorfmt('unknown control name: ''%s''', [par
.tokStr
]);
900 //writeln('children for <', par.tokStr, '>: <', cc.className, '>');
902 par
.eatDelim(':'); // optional
904 //writeln(' mHoriz=', ctl.mHoriz);
906 ctl
.parseProperties(par
);
911 //writeln(': ', ctl.mDefSize.toString);
913 par
.eatDelim(','); // optional
918 function THControl
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
921 if (strEquCI1251(prname
, 'id')) then begin mId
:= par
.expectStrOrId(true); exit
; end; // allow empty strings
922 if (strEquCI1251(prname
, 'flex')) then begin flex
:= par
.expectInt(); exit
; end;
924 if (strEquCI1251(prname
, 'defsize')) or (strEquCI1251(prname
, 'size')) then begin mDefSize
:= parseSize(par
); exit
; end;
925 if (strEquCI1251(prname
, 'maxsize')) then begin mMaxSize
:= parseSize(par
); exit
; end;
926 if (strEquCI1251(prname
, 'defwidth')) or (strEquCI1251(prname
, 'width')) then begin mDefSize
.w
:= par
.expectInt(); exit
; end;
927 if (strEquCI1251(prname
, 'defheight')) or (strEquCI1251(prname
, 'height')) then begin mDefSize
.h
:= par
.expectInt(); exit
; end;
928 if (strEquCI1251(prname
, 'maxwidth')) then begin mMaxSize
.w
:= par
.expectInt(); exit
; end;
929 if (strEquCI1251(prname
, 'maxheight')) then begin mMaxSize
.h
:= par
.expectInt(); exit
; end;
931 if (strEquCI1251(prname
, 'wrap')) then begin mCanWrap
:= parseBool(par
); exit
; end;
932 if (strEquCI1251(prname
, 'linestart')) then begin mLineStart
:= parseBool(par
); exit
; end;
933 if (strEquCI1251(prname
, 'expand')) then begin mExpand
:= parseBool(par
); exit
; end;
935 if (strEquCI1251(prname
, 'align')) then begin mAlign
:= parseAnyAlign(par
); exit
; end;
936 if (strEquCI1251(prname
, 'hgroup')) then begin mHGroup
:= par
.expectStrOrId(true); exit
; end; // allow empty strings
937 if (strEquCI1251(prname
, 'vgroup')) then begin mVGroup
:= par
.expectStrOrId(true); exit
; end; // allow empty strings
939 if (strEquCI1251(prname
, 'canfocus')) then begin mCanFocus
:= parseBool(par
); exit
; end;
940 if (strEquCI1251(prname
, 'enabled')) then begin mEnabled
:= parseBool(par
); exit
; end;
941 if (strEquCI1251(prname
, 'disabled')) then begin mEnabled
:= not parseBool(par
); exit
; end;
942 if (strEquCI1251(prname
, 'escclose')) then begin mEscClose
:= not parseBool(par
); exit
; end;
943 if (strEquCI1251(prname
, 'eatkeys')) then begin mEatKeys
:= not parseBool(par
); exit
; end;
948 // ////////////////////////////////////////////////////////////////////////// //
949 procedure THControl
.activated ();
954 procedure THControl
.blurred ();
960 function THControl
.topLevel (): THControl
; inline;
963 while (result
.mParent
<> nil) do result
:= result
.mParent
;
967 function THControl
.getEnabled (): Boolean;
972 if (not mEnabled
) or (mWidth
< 1) or (mHeight
< 1) then exit
;
974 while (ctl
<> nil) do
976 if (not ctl
.mEnabled
) or (ctl
.mWidth
< 1) or (ctl
.mHeight
< 1) then exit
;
983 procedure THControl
.setEnabled (v
: Boolean); inline;
985 if (mEnabled
= v
) then exit
;
987 if not v
and focused
then setFocused(false);
991 function THControl
.getFocused (): Boolean; inline;
993 if (mParent
= nil) then result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = self
) else result
:= (topLevel
.mFocused
= self
);
997 procedure THControl
.setFocused (v
: Boolean); inline;
1004 if (tl
.mFocused
= self
) then
1007 tl
.mFocused
:= tl
.findNextFocus(self
);
1008 if (tl
.mFocused
= self
) then tl
.mFocused
:= nil;
1012 if (not mEnabled
) or (not mCanFocus
) then exit
;
1013 if (tl
.mFocused
<> self
) then
1015 tl
.mFocused
.blurred();
1016 tl
.mFocused
:= self
;
1017 if (tl
.mGrab
<> self
) then tl
.mGrab
:= nil;
1023 function THControl
.isMyChild (ctl
: THControl
): Boolean;
1026 while (ctl
<> nil) do
1028 if (ctl
.mParent
= self
) then exit
;
1035 // returns `true` if global coords are inside this control
1036 function THControl
.toLocal (var x
, y
: Integer): Boolean;
1041 while (ctl
<> nil) do
1047 result
:= (x
>= 0) and (y
>= 0) and (x
< mWidth
) and (y
< mHeight
);
1050 function THControl
.toLocal (gx
, gy
: Integer; out x
, y
: Integer): Boolean; inline;
1054 result
:= toLocal(x
, y
);
1057 procedure THControl
.toGlobal (var x
, y
: Integer);
1062 while (ctl
<> nil) do
1070 procedure THControl
.toGlobal (lx
, ly
: Integer; out x
, y
: Integer); inline;
1078 // x and y are global coords
1079 function THControl
.controlAtXY (x
, y
: Integer): THControl
;
1085 if (not mEnabled
) or (mWidth
< 1) or (mHeight
< 1) then exit
;
1086 if not toLocal(x
, y
, lx
, ly
) then exit
;
1087 for f
:= High(mChildren
) downto 0 do
1089 result
:= mChildren
[f
].controlAtXY(x
, y
);
1090 if (result
<> nil) then exit
;
1096 function THControl
.prevSibling (): THControl
;
1100 if (mParent
<> nil) then
1102 for f
:= 1 to High(mParent
.mChildren
) do
1104 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
-1]; exit
; end;
1110 function THControl
.nextSibling (): THControl
;
1114 if (mParent
<> nil) then
1116 for f
:= 0 to High(mParent
.mChildren
)-1 do
1118 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
+1]; exit
; end;
1124 function THControl
.firstChild (): THControl
; inline;
1126 if (Length(mChildren
) <> 0) then result
:= mChildren
[0] else result
:= nil;
1129 function THControl
.lastChild (): THControl
; inline;
1131 if (Length(mChildren
) <> 0) then result
:= mChildren
[High(mChildren
)] else result
:= nil;
1135 function THControl
.findFirstFocus (): THControl
;
1142 for f
:= 0 to High(mChildren
) do
1144 result
:= mChildren
[f
].findFirstFocus();
1145 if (result
<> nil) then exit
;
1147 if mCanFocus
then result
:= self
;
1152 function THControl
.findLastFocus (): THControl
;
1159 for f
:= High(mChildren
) downto 0 do
1161 result
:= mChildren
[f
].findLastFocus();
1162 if (result
<> nil) then exit
;
1164 if mCanFocus
then result
:= self
;
1169 function THControl
.findNextFocus (cur
: THControl
): THControl
;
1174 if not isMyChild(cur
) then cur
:= nil;
1175 if (cur
= nil) then begin result
:= findFirstFocus(); exit
; end;
1176 result
:= cur
.findFirstFocus();
1177 if (result
<> nil) and (result
<> cur
) then exit
;
1180 cur
:= cur
.nextSibling
;
1181 if (cur
= nil) then break
;
1182 result
:= cur
.findFirstFocus();
1183 if (result
<> nil) then exit
;
1185 result
:= findFirstFocus();
1190 function THControl
.findPrevFocus (cur
: THControl
): THControl
;
1195 if not isMyChild(cur
) then cur
:= nil;
1196 if (cur
= nil) then begin result
:= findLastFocus(); exit
; end;
1198 result
:= cur
.findLastFocus();
1199 if (result
<> nil) and (result
<> cur
) then exit
;
1202 cur
:= cur
.prevSibling
;
1203 if (cur
= nil) then break
;
1204 result
:= cur
.findLastFocus();
1205 if (result
<> nil) then exit
;
1207 result
:= findLastFocus();
1212 procedure THControl
.appendChild (ctl
: THControl
);
1214 if (ctl
= nil) then exit
;
1215 if (ctl
.mParent
<> nil) then exit
;
1216 SetLength(mChildren
, Length(mChildren
)+1);
1217 mChildren
[High(mChildren
)] := ctl
;
1218 ctl
.mParent
:= self
;
1219 Inc(ctl
.mX
, mFrameWidth
);
1220 Inc(ctl
.mY
, mFrameHeight
);
1221 if (ctl
.mWidth
> 0) and (ctl
.mHeight
> 0) and
1222 (ctl
.mX
+ctl
.mWidth
> mFrameWidth
) and (ctl
.mY
+ctl
.mHeight
> mFrameHeight
) then
1224 if (mWidth
+mFrameWidth
< ctl
.mX
+ctl
.mWidth
) then mWidth
:= ctl
.mX
+ctl
.mWidth
+mFrameWidth
;
1225 if (mHeight
+mFrameHeight
< ctl
.mY
+ctl
.mHeight
) then mHeight
:= ctl
.mY
+ctl
.mHeight
+mFrameHeight
;
1227 if (mFocused
= nil) and ctl
.mEnabled
and ctl
.mCanFocus
and (ctl
.mWidth
> 0) and (ctl
.mHeight
> 0) then mFocused
:= ctl
;
1231 // ////////////////////////////////////////////////////////////////////////// //
1232 procedure THControl
.setScissorGLInternal (x
, y
, w
, h
: Integer);
1234 if not scallowed
then exit
;
1235 x
:= trunc(x
*gh_ui_scale
);
1236 y
:= trunc(y
*gh_ui_scale
);
1237 w
:= trunc(w
*gh_ui_scale
);
1238 h
:= trunc(h
*gh_ui_scale
);
1239 scis
.combineRect(x
, y
, w
, h
);
1242 procedure THControl
.setScissor (lx
, ly
, lw
, lh
: Integer);
1245 //ox, oy, ow, oh: Integer;
1247 if not scallowed
then exit
;
1248 //ox := lx; oy := ly; ow := lw; oh := lh;
1249 if not intersectRect(lx
, ly
, lw
, lh
, 0, 0, mWidth
, mHeight
) then
1251 //writeln('oops: <', self.className, '>: old=(', ox, ',', oy, ')-[', ow, ',', oh, ']');
1252 glScissor(0, 0, 0, 0);
1255 toGlobal(lx
, ly
, gx
, gy
);
1256 setScissorGLInternal(gx
, gy
, lw
, lh
);
1259 procedure THControl
.resetScissor (fullArea
: Boolean); inline;
1261 if not scallowed
then exit
;
1264 setScissor(0, 0, mWidth
, mHeight
);
1268 setScissor(mFrameWidth
, mFrameHeight
, mWidth
-mFrameWidth
*2, mHeight
-mFrameHeight
*2);
1273 // ////////////////////////////////////////////////////////////////////////// //
1274 procedure THControl
.draw ();
1279 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1280 toGlobal(0, 0, gx
, gy
);
1281 //conwritefln('[%s]: (%d,%d)-(%d,%d) (%d,%d)', [ClassName, mX, mY, mWidth, mHeight, x, y]);
1283 scis
.save(true); // scissoring enabled
1286 resetScissor(true); // full area
1287 drawControl(gx
, gy
);
1288 resetScissor(false); // client area
1289 for f
:= 0 to High(mChildren
) do mChildren
[f
].draw();
1290 resetScissor(true); // full area
1291 drawControlPost(gx
, gy
);
1298 procedure THControl
.drawControl (gx
, gy
: Integer);
1300 if (mParent
= nil) then darkenRect(gx
, gy
, mWidth
, mHeight
, 64);
1303 procedure THControl
.drawControlPost (gx
, gy
: Integer);
1306 if mDrawShadow
and (mWidth
> 0) and (mHeight
> 0) then
1308 setScissorGLInternal(gx
+8, gy
+8, mWidth
, mHeight
);
1309 darkenRect(gx
+mWidth
, gy
+8, 8, mHeight
, 128);
1310 darkenRect(gx
+8, gy
+mHeight
, mWidth
-8, 8, 128);
1315 // ////////////////////////////////////////////////////////////////////////// //
1316 function THControl
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1321 if not mEnabled
then exit
;
1322 if (mParent
= nil) then
1324 if (mGrab
<> nil) then
1326 result
:= mGrab
.mouseEvent(ev
);
1327 if (ev
.release
) then mGrab
:= nil;
1331 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1332 ctl
:= controlAtXY(ev
.x
, ev
.y
);
1333 if (ctl
<> nil) and (ctl
<> self
) then
1335 if (ctl
<> topLevel
.mFocused
) then ctl
.setFocused(true);
1336 result
:= ctl
.mouseEvent(ev
);
1338 else if (ctl
= self
) and assigned(actionCB
) then
1345 function THControl
.keyEvent (var ev
: THKeyEvent
): Boolean;
1350 if not mEnabled
then exit
;
1351 if (topLevel
.mFocused
<> self
) and isMyChild(topLevel
.mFocused
) and topLevel
.mFocused
.mEnabled
then result
:= topLevel
.mFocused
.keyEvent(ev
);
1352 if (mParent
= nil) then
1354 if (ev
= 'S-Tab') then
1357 ctl
:= findPrevFocus(mFocused
);
1358 if (ctl
<> mFocused
) then
1365 if (ev
= 'Tab') then
1368 ctl
:= findNextFocus(mFocused
);
1369 if (ctl
<> mFocused
) then
1376 if mEscClose
and (ev
= 'Escape') then
1379 uiRemoveWindow(self
);
1383 if mEatKeys
then result
:= true;
1387 // ////////////////////////////////////////////////////////////////////////// //
1388 constructor THTopWindow
.Create (const atitle
: AnsiString; ax
, ay
: Integer; aw
: Integer=-1; ah
: Integer=-1);
1390 inherited Create(ax
, ay
, aw
, ah
);
1394 if (mWidth
< mFrameWidth
*2+3*8) then mWidth
:= mFrameWidth
*2+3*8;
1395 if (mHeight
< mFrameHeight
*2) then mHeight
:= mFrameHeight
*2;
1396 if (Length(mTitle
) > 0) then
1398 if (mWidth
< Length(mTitle
)*8+mFrameWidth
*2+3*8) then mWidth
:= Length(mTitle
)*8+mFrameWidth
*2+3*8;
1401 mDrawShadow
:= true;
1402 mWaitingClose
:= false;
1408 function THTopWindow
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1410 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
1412 mTitle
:= par
.expectStrOrId(true);
1416 if (strEquCI1251(prname
, 'children')) then
1422 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
1423 result
:= inherited parseProperty(prname
, par
);
1427 procedure THTopWindow
.centerInScreen ();
1429 if (mWidth
> 0) and (mHeight
> 0) then
1431 mX
:= trunc((gScrWidth
/gh_ui_scale
-mWidth
)/2);
1432 mY
:= trunc((gScrHeight
/gh_ui_scale
-mHeight
)/2);
1437 procedure THTopWindow
.drawControl (gx
, gy
: Integer);
1439 fillRect(gx
, gy
, mWidth
, mHeight
, TGxRGBA
.Create(0, 0, 128));
1443 procedure THTopWindow
.drawControlPost (gx
, gy
: Integer);
1452 drawRectUI(mX
+4, mY
+4, mWidth
-8, mHeight
-8, TGxRGBA
.Create(r
, g
, b
));
1456 drawRectUI(mX
+3, mY
+3, mWidth
-6, mHeight
-6, TGxRGBA
.Create(r
, g
, b
));
1457 drawRectUI(mX
+5, mY
+5, mWidth
-10, mHeight
-10, TGxRGBA
.Create(r
, g
, b
));
1458 setScissor(mFrameWidth
, 0, 3*8, 8);
1459 fillRect(mX
+mFrameWidth
, mY
, 3*8, 8, TGxRGBA
.Create(0, 0, 128));
1460 drawText8(mX
+mFrameWidth
, mY
, '[ ]', TGxRGBA
.Create(r
, g
, b
));
1461 if mInClose
then drawText8(mX
+mFrameWidth
+7, mY
, '#', TGxRGBA
.Create(0, 255, 0))
1462 else drawText8(mX
+mFrameWidth
+7, mY
, '*', TGxRGBA
.Create(0, 255, 0));
1464 if (Length(mTitle
) > 0) then
1466 setScissor(mFrameWidth
+3*8, 0, mWidth
-mFrameWidth
*2-3*8, 8);
1467 tx
:= (mX
+3*8)+((mWidth
-3*8)-Length(mTitle
)*8) div 2;
1468 fillRect(tx
-3, mY
, Length(mTitle
)*8+3+2, 8, TGxRGBA
.Create(0, 0, 128));
1469 drawText8(tx
, mY
, mTitle
, TGxRGBA
.Create(r
, g
, b
));
1471 inherited drawControlPost(gx
, gy
);
1475 procedure THTopWindow
.blurred ();
1478 mWaitingClose
:= false;
1484 function THTopWindow
.keyEvent (var ev
: THKeyEvent
): Boolean;
1486 result
:= inherited keyEvent(ev
);
1487 if not getFocused
then exit
;
1488 if (ev
= 'M-F3') then
1490 uiRemoveWindow(self
);
1497 function THTopWindow
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1502 if not mEnabled
then exit
;
1503 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1507 mX
+= ev
.x
-mDragStartX
;
1508 mY
+= ev
.y
-mDragStartY
;
1509 mDragStartX
:= ev
.x
;
1510 mDragStartY
:= ev
.y
;
1511 if (ev
.release
) then mDragging
:= false;
1516 if toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1522 if (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8) then
1524 //uiRemoveWindow(self);
1525 mWaitingClose
:= true;
1531 mDragStartX
:= ev
.x
;
1532 mDragStartY
:= ev
.y
;
1537 if (lx
< mFrameWidth
) or (lx
>= mWidth
-mFrameWidth
) or (ly
>= mHeight
-mFrameHeight
) then
1540 mDragStartX
:= ev
.x
;
1541 mDragStartY
:= ev
.y
;
1547 if (ev
.release
) then
1549 if mWaitingClose
and (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8) then
1551 uiRemoveWindow(self
);
1555 mWaitingClose
:= false;
1561 if mWaitingClose
then
1563 mInClose
:= (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8);
1572 if (not ev
.motion
) then mWaitingClose
:= false;
1575 result
:= inherited mouseEvent(ev
);
1579 // ////////////////////////////////////////////////////////////////////////// //
1580 constructor THCtlSimpleText
.Create (ax
, ay
: Integer);
1583 inherited Create(ax
, ay
, 4, 4);
1587 destructor THCtlSimpleText
.Destroy ();
1594 procedure THCtlSimpleText
.appendItem (const atext
: AnsiString; acentered
: Boolean=false; ahline
: Boolean=false);
1598 if (Length(atext
)*8+3*8+2 > mWidth
) then mWidth
:= Length(atext
)*8+3*8+2;
1599 SetLength(mItems
, Length(mItems
)+1);
1600 it
:= @mItems
[High(mItems
)];
1602 it
.centered
:= acentered
;
1604 if (Length(mItems
)*8 > mHeight
) then mHeight
:= Length(mItems
)*8;
1608 procedure THCtlSimpleText
.drawControl (gx
, gy
: Integer);
1614 for f
:= 0 to High(mItems
) do
1621 if it
.centered
then begin b
:= 255; tx
:= gx
+(mWidth
-Length(it
.title
)*8) div 2; end;
1625 if (Length(it
.title
) = 0) then
1627 drawHLine(gx
+4, gy
+3, mWidth
-8, TGxRGBA
.Create(r
, g
, b
));
1629 else if (tx
-3 > gx
+4) then
1631 drawHLine(gx
+4, gy
+3, tx
-3-(gx
+3), TGxRGBA
.Create(r
, g
, b
));
1632 drawHLine(tx
+Length(it
.title
)*8, gy
+3, mWidth
-4, TGxRGBA
.Create(r
, g
, b
));
1635 drawText8(tx
, gy
, it
.title
, TGxRGBA
.Create(r
, g
, b
));
1641 function THCtlSimpleText
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1645 result
:= inherited mouseEvent(ev
);
1646 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1653 function THCtlSimpleText
.keyEvent (var ev
: THKeyEvent
): Boolean;
1655 result
:= inherited keyEvent(ev
);
1659 // ////////////////////////////////////////////////////////////////////////// //
1660 constructor THCtlCBListBox
.Create (ax
, ay
: Integer);
1664 inherited Create(ax
, ay
, 4, 4);
1668 destructor THCtlCBListBox
.Destroy ();
1675 procedure THCtlCBListBox
.appendItem (const atext
: AnsiString; bv
: PBoolean; aaction
: TActionCB
=nil);
1679 if (Length(atext
)*8+3*8+2 > mWidth
) then mWidth
:= Length(atext
)*8+3*8+2;
1680 SetLength(mItems
, Length(mItems
)+1);
1681 it
:= @mItems
[High(mItems
)];
1684 it
.actionCB
:= aaction
;
1685 if (Length(mItems
)*8 > mHeight
) then mHeight
:= Length(mItems
)*8;
1686 if (mCurIndex
< 0) then mCurIndex
:= 0;
1690 procedure THCtlCBListBox
.drawControl (gx
, gy
: Integer);
1695 for f
:= 0 to High(mItems
) do
1698 if (mCurIndex
= f
) then fillRect(gx
, gy
, mWidth
, 8, TGxRGBA
.Create(0, 128, 0));
1699 if (it
.varp
<> nil) then
1701 if it
.varp
^ then drawText8(gx
, gy
, '[x]', TGxRGBA
.Create(255, 255, 255)) else drawText8(gx
, gy
, '[ ]', TGxRGBA
.Create(255, 255, 255));
1702 drawText8(gx
+3*8+2, gy
, it
.title
, TGxRGBA
.Create(255, 255, 0));
1704 else if (Length(it
.title
) > 0) then
1706 tx
:= gx
+(mWidth
-Length(it
.title
)*8) div 2;
1707 if (tx
-3 > gx
+4) then
1709 drawHLine(gx
+4, gy
+3, tx
-3-(gx
+3), TGxRGBA
.Create(255, 255, 255));
1710 drawHLine(tx
+Length(it
.title
)*8, gy
+3, mWidth
-4, TGxRGBA
.Create(255, 255, 255));
1712 drawText8(tx
, gy
, it
.title
, TGxRGBA
.Create(255, 255, 255));
1716 drawHLine(gx
+4, gy
+3, mWidth
-8, TGxRGBA
.Create(255, 255, 255));
1723 function THCtlCBListBox
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1728 result
:= inherited mouseEvent(ev
);
1729 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1732 if (ev
= 'lmb') then
1735 if (ly
>= 0) and (ly
< Length(mItems
)) then
1738 if (it
.varp
<> nil) then
1741 it
.varp
^ := not it
.varp
^;
1742 if assigned(it
.actionCB
) then it
.actionCB(self
, Integer(it
.varp
^));
1743 if assigned(actionCB
) then actionCB(self
, ly
);
1751 function THCtlCBListBox
.keyEvent (var ev
: THKeyEvent
): Boolean;
1755 result
:= inherited keyEvent(ev
);
1756 if not getFocused
then exit
;
1758 if (ev
= 'Home') or (ev
= 'PageUp') then
1763 if (ev
= 'End') or (ev
= 'PageDown') then
1766 mCurIndex
:= High(mItems
);
1771 if (Length(mItems
) > 0) then
1773 if (mCurIndex
< 0) then mCurIndex
:= Length(mItems
);
1774 while (mCurIndex
> 0) do
1777 if (mItems
[mCurIndex
].varp
<> nil) then break
;
1785 if (ev
= 'Down') then
1788 if (Length(mItems
) > 0) then
1790 if (mCurIndex
< 0) then mCurIndex
:= -1;
1791 while (mCurIndex
< High(mItems
)) do
1794 if (mItems
[mCurIndex
].varp
<> nil) then break
;
1802 if (ev
= 'Space') or (ev
= 'Enter') then
1805 if (mCurIndex
>= 0) and (mCurIndex
< Length(mItems
)) and (mItems
[mCurIndex
].varp
<> nil) then
1807 it
:= @mItems
[mCurIndex
];
1808 it
.varp
^ := not it
.varp
^;
1809 if assigned(it
.actionCB
) then it
.actionCB(self
, Integer(it
.varp
^));
1810 if assigned(actionCB
) then actionCB(self
, mCurIndex
);
1816 // ////////////////////////////////////////////////////////////////////////// //
1817 constructor THCtlBox
.Create (ahoriz
: Boolean);
1825 function THCtlBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1827 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
1828 if (strEquCI1251(prname
, 'frame')) then
1830 mHasFrame
:= parseBool(par
);
1831 if (mHasFrame
) then begin mFrameWidth
:= 8; mFrameHeight
:= 8; end else begin mFrameWidth
:= 0; mFrameHeight
:= 0; end;
1835 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
1837 mCaption
:= par
.expectStrOrId(true);
1838 mDefSize
:= TLaySize
.Create(Length(mCaption
)*8+3, 8);
1842 if (strEquCI1251(prname
, 'children')) then
1848 result
:= inherited parseProperty(prname
, par
);
1852 procedure THCtlBox
.drawControl (gx
, gy
: Integer);
1857 if focused
then begin r
:= 255; g
:= 255; b
:= 255; end else begin r
:= 255; g
:= 255; b
:= 0; end;
1861 drawRectUI(gx
+3, gy
+3, mWidth
-6, mHeight
-6, TGxRGBA
.Create(r
, g
, b
));
1864 if (Length(mCaption
) > 0) then
1866 setScissor(mFrameWidth
+1, 0, mWidth
-mFrameWidth
-2, 8);
1867 tx
:= gx
+((mWidth
-Length(mCaption
)*8) div 2);
1868 if mHasFrame
then fillRect(tx
-2, gy
, Length(mCaption
)*8+3, 8, TGxRGBA
.Create(0, 0, 128));
1869 drawText8(tx
, gy
, mCaption
, TGxRGBA
.Create(r
, g
, b
));
1874 function THCtlBox
.mouseEvent (var ev
: THMouseEvent
): Boolean;
1878 result
:= inherited mouseEvent(ev
);
1879 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
1886 //TODO: navigation with arrow keys, according to box orientation
1887 function THCtlBox
.keyEvent (var ev
: THKeyEvent
): Boolean;
1889 result
:= inherited keyEvent(ev
);
1893 // ////////////////////////////////////////////////////////////////////////// //
1894 procedure THCtlHBox
.AfterConstruction ();
1896 inherited AfterConstruction();
1901 // ////////////////////////////////////////////////////////////////////////// //
1902 procedure THCtlVBox
.AfterConstruction ();
1904 inherited AfterConstruction();
1910 // ////////////////////////////////////////////////////////////////////////// //
1911 procedure THCtlSpan
.AfterConstruction ();
1913 inherited AfterConstruction();
1919 function THCtlSpan
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1921 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
1922 result
:= inherited parseProperty(prname
, par
);
1926 procedure THCtlSpan
.drawControl (gx
, gy
: Integer);
1931 // ////////////////////////////////////////////////////////////////////// //
1932 function THCtlLine
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1934 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
1935 result
:= inherited parseProperty(prname
, par
);
1939 procedure THCtlLine
.drawControl (gx
, gy
: Integer);
1943 drawHLine(gx
, gy
+(mHeight
div 2), mWidth
, TGxRGBA
.Create(255, 255, 255));
1947 drawVLine(gx
+(mWidth
div 2), gy
, mHeight
, TGxRGBA
.Create(255, 255, 255));
1952 // ////////////////////////////////////////////////////////////////////////// //
1953 procedure THCtlHLine
.AfterConstruction ();
1961 // ////////////////////////////////////////////////////////////////////////// //
1962 procedure THCtlVLine
.AfterConstruction ();
1971 // ////////////////////////////////////////////////////////////////////////// //
1972 constructor THCtlTextLabel
.Create (const atext
: AnsiString);
1976 mDefSize
:= TLaySize
.Create(Length(atext
)*8, 8);
1980 procedure THCtlTextLabel
.AfterConstruction ();
1982 inherited AfterConstruction();
1986 if (mDefSize
.h
<= 0) then mDefSize
.h
:= 8;
1990 function THCtlTextLabel
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1992 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
1994 mText
:= par
.expectStrOrId(true);
1995 mDefSize
:= TLaySize
.Create(Length(mText
)*8, 8);
1999 if (strEquCI1251(prname
, 'textalign')) then
2001 parseTextAlign(par
, mHAlign
, mVAlign
);
2005 result
:= inherited parseProperty(prname
, par
);
2009 procedure THCtlTextLabel
.drawControl (gx
, gy
: Integer);
2011 xpos
, ypos
: Integer;
2014 fillRect(gx
, gy
, mWidth
, mHeight
, TGxRGBA
.Create(96, 96, 0));
2015 drawRectUI(gx
, gy
, mWidth
, mHeight
, TGxRGBA
.Create(96, 96, 96));
2017 if (Length(mText
) > 0) then
2019 if (mHAlign
< 0) then xpos
:= 0
2020 else if (mHAlign
> 0) then xpos
:= mWidth
-Length(mText
)*8
2021 else xpos
:= (mWidth
-Length(mText
)*8) div 2;
2023 if (mVAlign
< 0) then ypos
:= 0
2024 else if (mVAlign
> 0) then ypos
:= mHeight
-8
2025 else ypos
:= (mHeight
-8) div 2;
2027 drawText8(gx
+xpos
, gy
+ypos
, mText
, TGxRGBA
.Create(255, 255, 255));
2032 function THCtlTextLabel
.mouseEvent (var ev
: THMouseEvent
): Boolean;
2036 result
:= inherited mouseEvent(ev
);
2037 if not result
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
2044 function THCtlTextLabel
.keyEvent (var ev
: THKeyEvent
): Boolean;
2046 result
:= inherited keyEvent(ev
);
2051 registerCtlClass(THCtlHBox
, 'hbox');
2052 registerCtlClass(THCtlVBox
, 'vbox');
2053 registerCtlClass(THCtlSpan
, 'span');
2054 registerCtlClass(THCtlHLine
, 'hline');
2055 registerCtlClass(THCtlVLine
, 'vline');
2056 registerCtlClass(THCtlTextLabel
, 'label');