1 (* coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
2 * Understanding is not required. Only obedience.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 {$INCLUDE ../shared/a_modes.inc}
32 // ////////////////////////////////////////////////////////////////////////// //
34 TUIControlClass
= class of TUIControl
;
38 type TActionCB
= procedure (me
: TUIControl
);
39 type TCloseRequestCB
= function (me
: TUIControl
): Boolean; // top-level windows will call this before closing with icon/keyboard
41 // return `true` to stop
42 type TCtlEnumCB
= function (ctl
: TUIControl
): Boolean is nested
;
45 const ClrIdxActive
= 0;
46 const ClrIdxDisabled
= 1;
47 const ClrIdxInactive
= 2;
55 mWidth
, mHeight
: Integer;
56 mFrameWidth
, mFrameHeight
: Integer;
57 mScrollX
, mScrollY
: Integer;
60 mChildren
: array of TUIControl
;
61 mFocused
: TUIControl
; // valid only for top-level controls
62 mEscClose
: Boolean; // valid only for top-level controls
67 mCtl4Style
: AnsiString;
68 mBackColor
: array[0..ClrIdxMax
] of TGxRGBA
;
69 mTextColor
: array[0..ClrIdxMax
] of TGxRGBA
;
70 mFrameColor
: array[0..ClrIdxMax
] of TGxRGBA
;
71 mFrameTextColor
: array[0..ClrIdxMax
] of TGxRGBA
;
72 mFrameIconColor
: array[0..ClrIdxMax
] of TGxRGBA
;
73 mDarken
: array[0..ClrIdxMax
] of Integer; // -1: none
80 procedure updateStyle (); virtual;
81 procedure cacheStyle (root
: TUIStyle
); virtual;
82 function getColorIndex (): Integer; inline;
85 function getEnabled (): Boolean;
86 procedure setEnabled (v
: Boolean); inline;
88 function getFocused (): Boolean; inline;
89 procedure setFocused (v
: Boolean); inline;
91 function getActive (): Boolean; inline;
93 function getCanFocus (): Boolean; inline;
95 function isMyChild (ctl
: TUIControl
): Boolean;
97 function findFirstFocus (): TUIControl
;
98 function findLastFocus (): TUIControl
;
100 function findNextFocus (cur
: TUIControl
; wrap
: Boolean): TUIControl
;
101 function findPrevFocus (cur
: TUIControl
; wrap
: Boolean): TUIControl
;
103 function findCancelControl (): TUIControl
;
104 function findDefaulControl (): TUIControl
;
106 function findControlById (const aid
: AnsiString): TUIControl
;
108 procedure activated (); virtual;
109 procedure blurred (); virtual;
111 procedure calcFullClientSize ();
113 //WARNING! do not call scissor functions outside `.draw*()` API!
114 // set scissor to this rect (in local coords)
115 procedure setScissor (lx
, ly
, lw
, lh
: Integer);
116 // reset scissor to whole control
117 procedure resetScissor (fullArea
: Boolean); inline; // "full area" means "with frame"
120 // set scissor to this rect (in global coords)
121 procedure setScissorGLInternal (x
, y
, w
, h
: Integer);
125 closeRequestCB
: TCloseRequestCB
;
128 mDefSize
: TLaySize
; // default size
129 mMaxSize
: TLaySize
; // maximum size
138 mLayDefSize
: TLaySize
;
139 mLayMaxSize
: TLaySize
;
143 // layouter interface
144 function getDefSize (): TLaySize
; inline; // default size; <0: use max size
145 //procedure setDefSize (const sz: TLaySize); inline; // default size; <0: use max size
146 function getMargins (): TLayMargins
; inline;
147 function getMaxSize (): TLaySize
; inline; // max size; <0: set to some huge value
148 //procedure setMaxSize (const sz: TLaySize); inline; // max size; <0: set to some huge value
149 function getFlex (): Integer; inline; // <=0: not flexible
150 function isHorizBox (): Boolean; inline; // horizontal layout for children?
151 procedure setHorizBox (v
: Boolean); inline; // horizontal layout for children?
152 function canWrap (): Boolean; inline; // for horizontal boxes: can wrap children? for child: `false` means 'nonbreakable at *next* ctl'
153 procedure setCanWrap (v
: Boolean); inline; // for horizontal boxes: can wrap children? for child: `false` means 'nonbreakable at *next* ctl'
154 function isLineStart (): Boolean; inline; // `true` if this ctl should start a new line; ignored for vertical boxes
155 procedure setLineStart (v
: Boolean); inline; // `true` if this ctl should start a new line; ignored for vertical boxes
156 function getAlign (): Integer; inline; // aligning in non-main direction: <0: left/up; 0: center; >0: right/down
157 procedure setAlign (v
: Integer); inline; // aligning in non-main direction: <0: left/up; 0: center; >0: right/down
158 function getExpand (): Boolean; inline; // expanding in non-main direction: `true` will ignore align and eat all available space
159 procedure setExpand (v
: Boolean); inline; // expanding in non-main direction: `true` will ignore align and eat all available space
160 function getHGroup (): AnsiString; inline; // empty: not grouped
161 procedure setHGroup (const v
: AnsiString); inline; // empty: not grouped
162 function getVGroup (): AnsiString; inline; // empty: not grouped
163 procedure setVGroup (const v
: AnsiString); inline; // empty: not grouped
165 procedure setActualSizePos (constref apos
: TLayPos
; constref asize
: TLaySize
); inline;
167 procedure layPrepare (); virtual; // called before registering control in layouter
170 property flex
: Integer read mFlex write mFlex
;
171 property flDefaultSize
: TLaySize read mDefSize write mDefSize
;
172 property flMaxSize
: TLaySize read mMaxSize write mMaxSize
;
173 property flHoriz
: Boolean read isHorizBox write setHorizBox
;
174 property flCanWrap
: Boolean read canWrap write setCanWrap
;
175 property flLineStart
: Boolean read isLineStart write setLineStart
;
176 property flAlign
: Integer read getAlign write setAlign
;
177 property flExpand
: Boolean read getExpand write setExpand
;
178 property flHGroup
: AnsiString read getHGroup write setHGroup
;
179 property flVGroup
: AnsiString read getVGroup write setVGroup
;
180 property fullSize
: TLaySize read mFullSize
;
183 function parsePos (par
: TTextParser
): TLayPos
;
184 function parseSize (par
: TTextParser
): TLaySize
;
185 function parseBool (par
: TTextParser
): Boolean;
186 function parseAnyAlign (par
: TTextParser
): Integer;
187 function parseHAlign (par
: TTextParser
): Integer;
188 function parseVAlign (par
: TTextParser
): Integer;
189 function parseOrientation (const prname
: AnsiString; par
: TTextParser
): Boolean;
190 procedure parseTextAlign (par
: TTextParser
; var h
, v
: Integer);
191 procedure parseChildren (par
: TTextParser
); // par should be on '{'; final '}' is eaten
194 // par is on property data
195 // there may be more data in text stream, don't eat it!
196 // return `true` if property name is valid and value was parsed
197 // return `false` if property name is invalid; don't advance parser in this case
198 // throw on property data errors
199 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; virtual;
201 // par should be on '{'; final '}' is eaten
202 procedure parseProperties (par
: TTextParser
);
205 constructor Create ();
206 destructor Destroy (); override;
208 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
210 // `sx` and `sy` are screen coordinates
211 procedure drawControl (gx
, gy
: Integer); virtual;
213 // called after all children drawn
214 procedure drawControlPost (gx
, gy
: Integer); virtual;
216 procedure draw (); virtual;
218 function topLevel (): TUIControl
; inline;
220 // returns `true` if global coords are inside this control
221 function toLocal (var x
, y
: Integer): Boolean;
222 function toLocal (gx
, gy
: Integer; out x
, y
: Integer): Boolean; inline;
223 procedure toGlobal (var x
, y
: Integer);
224 procedure toGlobal (lx
, ly
: Integer; out x
, y
: Integer); inline;
226 procedure getDrawRect (out gx
, gy
, wdt
, hgt
: Integer);
228 // x and y are global coords
229 function controlAtXY (x
, y
: Integer; allowDisabled
: Boolean=false): TUIControl
;
231 function parentScrollX (): Integer; inline;
232 function parentScrollY (): Integer; inline;
234 procedure makeVisibleInParent ();
236 procedure doAction (); virtual; // so user controls can override it
238 procedure mouseEvent (var ev
: THMouseEvent
); virtual; // returns `true` if event was eaten
239 procedure keyEvent (var ev
: THKeyEvent
); virtual; // returns `true` if event was eaten
240 procedure keyEventPre (var ev
: THKeyEvent
); virtual; // will be called before dispatching the event
241 procedure keyEventPost (var ev
: THKeyEvent
); virtual; // will be called after if nobody processed the event
243 function prevSibling (): TUIControl
;
244 function nextSibling (): TUIControl
;
245 function firstChild (): TUIControl
; inline;
246 function lastChild (): TUIControl
; inline;
248 procedure appendChild (ctl
: TUIControl
); virtual;
250 function setActionCBFor (const aid
: AnsiString; cb
: TActionCB
): TActionCB
; // returns previous cb
252 function forEachChildren (cb
: TCtlEnumCB
): TUIControl
; // doesn't recurse
253 function forEachControl (cb
: TCtlEnumCB
; includeSelf
: Boolean=true): TUIControl
;
255 procedure close (); // this closes *top-level* control
258 property id
: AnsiString read mId
;
259 property styleId
: AnsiString read mStyleId
;
260 property scrollX
: Integer read mScrollX write mScrollX
;
261 property scrollY
: Integer read mScrollY write mScrollY
;
262 property x0
: Integer read mX write mX
;
263 property y0
: Integer read mY write mY
;
264 property width
: Integer read mWidth write mWidth
;
265 property height
: Integer read mHeight write mHeight
;
266 property enabled
: Boolean read getEnabled write setEnabled
;
267 property parent
: TUIControl read mParent
;
268 property focused
: Boolean read getFocused write setFocused
;
269 property active
: Boolean read getActive
;
270 property escClose
: Boolean read mEscClose write mEscClose
;
271 property cancel
: Boolean read mCancel write mCancel
;
272 property defctl
: Boolean read mDefault write mDefault
;
273 property canFocus
: Boolean read getCanFocus write mCanFocus
;
274 property ctlById
[const aid
: AnsiString]: TUIControl read findControlById
; default
;
278 TUITopWindow
= class(TUIControl
)
280 type TXMode
= (None
, Drag
, Scroll
);
285 mDragStartX
, mDragStartY
: Integer;
286 mWaitingClose
: Boolean;
288 mFreeOnClose
: Boolean; // default: false
289 mDoCenter
: Boolean; // after layouting
290 mFitToScreen
: Boolean;
293 procedure activated (); override;
294 procedure blurred (); override;
297 closeCB
: TActionCB
; // called after window was removed from ui window list
300 constructor Create (const atitle
: AnsiString);
302 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
304 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
306 procedure flFitToScreen (); // call this before layouting
308 procedure centerInScreen ();
310 // `sx` and `sy` are screen coordinates
311 procedure drawControl (gx
, gy
: Integer); override;
312 procedure drawControlPost (gx
, gy
: Integer); override;
314 procedure keyEvent (var ev
: THKeyEvent
); override; // returns `true` if event was eaten
315 procedure mouseEvent (var ev
: THMouseEvent
); override; // returns `true` if event was eaten
318 property freeOnClose
: Boolean read mFreeOnClose write mFreeOnClose
;
319 property fitToScreen
: Boolean read mFitToScreen write mFitToScreen
;
322 // ////////////////////////////////////////////////////////////////////// //
323 TUIBox
= class(TUIControl
)
326 mCaption
: AnsiString;
327 mHAlign
: Integer; // -1: left; 0: center; 1: right; default: left
330 procedure setCaption (const acap
: AnsiString);
331 procedure setHasFrame (v
: Boolean);
334 constructor Create (ahoriz
: Boolean);
336 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
338 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
340 procedure drawControl (gx
, gy
: Integer); override;
342 procedure mouseEvent (var ev
: THMouseEvent
); override;
343 procedure keyEvent (var ev
: THKeyEvent
); override;
346 property caption
: AnsiString read mCaption write setCaption
;
347 property hasFrame
: Boolean read mHasFrame write setHasFrame
;
348 property captionAlign
: Integer read mHAlign write mHAlign
;
351 TUIHBox
= class(TUIBox
)
353 constructor Create ();
355 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
358 TUIVBox
= class(TUIBox
)
360 constructor Create ();
362 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
365 // ////////////////////////////////////////////////////////////////////// //
366 TUISpan
= class(TUIControl
)
368 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
370 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
372 procedure drawControl (gx
, gy
: Integer); override;
375 // ////////////////////////////////////////////////////////////////////// //
376 TUILine
= class(TUIControl
)
378 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
380 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
382 procedure drawControl (gx
, gy
: Integer); override;
385 TUIHLine
= class(TUILine
)
387 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
390 TUIVLine
= class(TUILine
)
392 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
395 // ////////////////////////////////////////////////////////////////////// //
396 TUIStaticText
= class(TUIControl
)
399 mHAlign
: Integer; // -1: left; 0: center; 1: right; default: left
400 mVAlign
: Integer; // -1: top; 0: center; 1: bottom; default: center
401 mHeader
: Boolean; // true: draw with frame text color
402 mLine
: Boolean; // true: draw horizontal line
405 procedure setText (const atext
: AnsiString);
408 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
410 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
412 procedure drawControl (gx
, gy
: Integer); override;
415 property text: AnsiString read mText write setText
;
416 property halign
: Integer read mHAlign write mHAlign
;
417 property valign
: Integer read mVAlign write mVAlign
;
418 property header
: Boolean read mHeader write mHeader
;
419 property line
: Boolean read mLine write mLine
;
422 // ////////////////////////////////////////////////////////////////////// //
423 TUITextLabel
= class(TUIControl
)
426 mHAlign
: Integer; // -1: left; 0: center; 1: right; default: left
427 mVAlign
: Integer; // -1: top; 0: center; 1: bottom; default: center
429 mHotOfs
: Integer; // from text start, in pixels
430 mHotColor
: array[0..ClrIdxMax
] of TGxRGBA
;
431 mLinkId
: AnsiString; // linked control
434 procedure cacheStyle (root
: TUIStyle
); override;
436 procedure setText (const s
: AnsiString); virtual;
439 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
441 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
443 procedure doAction (); override;
445 procedure drawControl (gx
, gy
: Integer); override;
447 procedure mouseEvent (var ev
: THMouseEvent
); override;
448 procedure keyEventPost (var ev
: THKeyEvent
); override;
451 property text: AnsiString read mText write setText
;
452 property halign
: Integer read mHAlign write mHAlign
;
453 property valign
: Integer read mVAlign write mVAlign
;
456 // ////////////////////////////////////////////////////////////////////// //
457 TUIButton
= class(TUITextLabel
)
459 procedure setText (const s
: AnsiString); override;
462 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
464 procedure drawControl (gx
, gy
: Integer); override;
466 procedure mouseEvent (var ev
: THMouseEvent
); override;
467 procedure keyEvent (var ev
: THKeyEvent
); override;
470 // ////////////////////////////////////////////////////////////////////// //
471 TUISwitchBox
= class(TUITextLabel
)
475 mCheckedStr
: AnsiString;
476 mUncheckedStr
: AnsiString;
477 mSwitchColor
: array[0..ClrIdxMax
] of TGxRGBA
;
480 procedure cacheStyle (root
: TUIStyle
); override;
482 procedure setText (const s
: AnsiString); override;
484 function getChecked (): Boolean; virtual;
485 procedure setChecked (v
: Boolean); virtual; abstract;
488 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
490 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
492 procedure drawControl (gx
, gy
: Integer); override;
494 procedure mouseEvent (var ev
: THMouseEvent
); override;
495 procedure keyEvent (var ev
: THKeyEvent
); override;
497 procedure setVar (pvar
: PBoolean);
500 property checked
: Boolean read getChecked write setChecked
;
503 TUICheckBox
= class(TUISwitchBox
)
505 procedure setChecked (v
: Boolean); override;
508 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
510 procedure doAction (); override;
513 TUIRadioBox
= class(TUISwitchBox
)
515 mRadioGroup
: AnsiString;
518 procedure setChecked (v
: Boolean); override;
521 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
523 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
525 procedure doAction (); override;
528 property radioGroup
: AnsiString read mRadioGroup write mRadioGroup
; //FIXME
532 // ////////////////////////////////////////////////////////////////////////// //
533 procedure uiMouseEvent (var evt
: THMouseEvent
);
534 procedure uiKeyEvent (var evt
: THKeyEvent
);
538 // ////////////////////////////////////////////////////////////////////////// //
539 procedure uiAddWindow (ctl
: TUIControl
);
540 procedure uiRemoveWindow (ctl
: TUIControl
); // will free window if `mFreeOnClose` is `true`
541 function uiVisibleWindow (ctl
: TUIControl
): Boolean;
543 procedure uiUpdateStyles ();
546 // ////////////////////////////////////////////////////////////////////////// //
548 procedure uiLayoutCtl (ctl
: TUIControl
);
551 // ////////////////////////////////////////////////////////////////////////// //
553 gh_ui_scale
: Single = 1.0;
563 // ////////////////////////////////////////////////////////////////////////// //
565 ctlsToKill
: array of TUIControl
= nil;
568 procedure scheduleKill (ctl
: TUIControl
);
572 if (ctl
= nil) then exit
;
574 for f
:= 0 to High(ctlsToKill
) do
576 if (ctlsToKill
[f
] = ctl
) then exit
;
577 if (ctlsToKill
[f
] = nil) then begin ctlsToKill
[f
] := ctl
; exit
; end;
579 SetLength(ctlsToKill
, Length(ctlsToKill
)+1);
580 ctlsToKill
[High(ctlsToKill
)] := ctl
;
584 procedure processKills ();
589 for f
:= 0 to High(ctlsToKill
) do
591 ctl
:= ctlsToKill
[f
];
592 if (ctl
= nil) then break
;
593 ctlsToKill
[f
] := nil;
596 if (Length(ctlsToKill
) > 0) then ctlsToKill
[0] := nil; // just in case
600 // ////////////////////////////////////////////////////////////////////////// //
602 knownCtlClasses
: array of record
603 klass
: TUIControlClass
;
608 procedure registerCtlClass (aklass
: TUIControlClass
; const aname
: AnsiString);
610 assert(aklass
<> nil);
611 assert(Length(aname
) > 0);
612 SetLength(knownCtlClasses
, Length(knownCtlClasses
)+1);
613 knownCtlClasses
[High(knownCtlClasses
)].klass
:= aklass
;
614 knownCtlClasses
[High(knownCtlClasses
)].name
:= aname
;
618 function findCtlClass (const aname
: AnsiString): TUIControlClass
;
622 for f
:= 0 to High(knownCtlClasses
) do
624 if (strEquCI1251(aname
, knownCtlClasses
[f
].name
)) then
626 result
:= knownCtlClasses
[f
].klass
;
634 // ////////////////////////////////////////////////////////////////////////// //
636 TFlexLayouter
= specialize TFlexLayouterBase
<TUIControl
>;
638 procedure uiLayoutCtl (ctl
: TUIControl
);
642 if (ctl
= nil) then exit
;
643 lay
:= TFlexLayouter
.Create();
645 if (ctl
is TUITopWindow
) and (TUITopWindow(ctl
).fitToScreen
) then TUITopWindow(ctl
).flFitToScreen();
650 //writeln('============================'); lay.dumpFlat();
652 //writeln('=== initial ==='); lay.dump();
654 //lay.calcMaxSizeInternal(0);
657 writeln('=== after first pass ===');
661 writeln('=== after second pass ===');
666 //writeln('=== final ==='); lay.dump();
668 if (ctl
.mParent
= nil) and (ctl
is TUITopWindow
) and (TUITopWindow(ctl
).mDoCenter
) then
670 TUITopWindow(ctl
).centerInScreen();
673 // calculate full size
674 ctl
.calcFullClientSize();
677 if (ctl
.mParent
= nil) then
679 if (ctl
.mFocused
= nil) or (ctl
.mFocused
= ctl
) or (not ctl
.mFocused
.enabled
) then
681 ctl
.mFocused
:= ctl
.findFirstFocus();
691 // ////////////////////////////////////////////////////////////////////////// //
693 uiTopList
: array of TUIControl
= nil;
694 uiGrabCtl
: TUIControl
= nil;
697 procedure uiUpdateStyles ();
701 for ctl
in uiTopList
do ctl
.updateStyle();
705 procedure uiMouseEvent (var evt
: THMouseEvent
);
713 if (evt
.eaten
) or (evt
.cancelled
) then exit
;
715 ev
.x
:= trunc(ev
.x
/gh_ui_scale
);
716 ev
.y
:= trunc(ev
.y
/gh_ui_scale
);
717 ev
.dx
:= trunc(ev
.dx
/gh_ui_scale
); //FIXME
718 ev
.dy
:= trunc(ev
.dy
/gh_ui_scale
); //FIXME
720 if (uiGrabCtl
<> nil) then
722 uiGrabCtl
.mouseEvent(ev
);
723 if (ev
.release
) and ((ev
.bstate
and (not ev
.but
)) = 0) then uiGrabCtl
:= nil;
727 if (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)].enabled
) then uiTopList
[High(uiTopList
)].mouseEvent(ev
);
728 if (not ev
.eaten
) and (not ev
.cancelled
) and (ev
.press
) then
730 for f
:= High(uiTopList
) downto 0 do
732 if uiTopList
[f
].toLocal(ev
.x
, ev
.y
, lx
, ly
) then
734 if (uiTopList
[f
].enabled
) and (f
<> High(uiTopList
)) then
736 uiTopList
[High(uiTopList
)].blurred();
737 ctmp
:= uiTopList
[f
];
739 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
740 uiTopList
[High(uiTopList
)] := ctmp
;
750 if (ev
.eaten
) then evt
.eat();
751 if (ev
.cancelled
) then evt
.cancel();
756 procedure uiKeyEvent (var evt
: THKeyEvent
);
761 if (evt
.eaten
) or (evt
.cancelled
) then exit
;
763 ev
.x
:= trunc(ev
.x
/gh_ui_scale
);
764 ev
.y
:= trunc(ev
.y
/gh_ui_scale
);
766 if (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)].enabled
) then uiTopList
[High(uiTopList
)].keyEvent(ev
);
767 //if (ev.release) then begin ev.eat(); exit; end;
769 if (ev
.eaten
) then evt
.eat();
770 if (ev
.cancelled
) then evt
.cancel();
781 gxBeginUIDraw(gh_ui_scale
);
783 for f
:= 0 to High(uiTopList
) do
787 if (f
<> High(uiTopList
)) then
789 cidx
:= ctl
.getColorIndex
;
790 if (ctl
.mDarken
[cidx
] > 0) then darkenRect(ctl
.x0
, ctl
.y0
, ctl
.width
, ctl
.height
, ctl
.mDarken
[cidx
]);
799 procedure uiAddWindow (ctl
: TUIControl
);
803 if (ctl
= nil) then exit
;
805 if not (ctl
is TUITopWindow
) then exit
; // alas
806 for f
:= 0 to High(uiTopList
) do
808 if (uiTopList
[f
] = ctl
) then
810 if (f
<> High(uiTopList
)) then
812 uiTopList
[High(uiTopList
)].blurred();
813 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
814 uiTopList
[High(uiTopList
)] := ctl
;
820 if (Length(uiTopList
) > 0) then uiTopList
[High(uiTopList
)].blurred();
821 SetLength(uiTopList
, Length(uiTopList
)+1);
822 uiTopList
[High(uiTopList
)] := ctl
;
828 procedure uiRemoveWindow (ctl
: TUIControl
);
832 if (ctl
= nil) then exit
;
834 if not (ctl
is TUITopWindow
) then exit
; // alas
835 for f
:= 0 to High(uiTopList
) do
837 if (uiTopList
[f
] = ctl
) then
840 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
841 SetLength(uiTopList
, Length(uiTopList
)-1);
842 if (ctl
is TUITopWindow
) then
845 if assigned(TUITopWindow(ctl
).closeCB
) then TUITopWindow(ctl
).closeCB(ctl
);
847 if (TUITopWindow(ctl
).mFreeOnClose
) then scheduleKill(ctl
);
856 function uiVisibleWindow (ctl
: TUIControl
): Boolean;
861 if (ctl
= nil) then exit
;
863 if not (ctl
is TUITopWindow
) then exit
; // alas
864 for f
:= 0 to High(uiTopList
) do
866 if (uiTopList
[f
] = ctl
) then begin result
:= true; exit
; end;
871 // ////////////////////////////////////////////////////////////////////////// //
872 constructor TUIControl
.Create ();
877 procedure TUIControl
.AfterConstruction ();
894 mDrawShadow
:= false;
896 // layouter interface
897 //mDefSize := TLaySize.Create(64, 8); // default size
898 mDefSize
:= TLaySize
.Create(0, 0); // default size
899 mMaxSize
:= TLaySize
.Create(-1, -1); // maximum size
908 mAlign
:= -1; // left/top
913 destructor TUIControl
.Destroy ();
917 if (mParent
<> nil) then
920 for f
:= 0 to High(mParent
.mChildren
) do
922 if (mParent
.mChildren
[f
] = self
) then
924 for c
:= f
+1 to High(mParent
.mChildren
) do mParent
.mChildren
[c
-1] := mParent
.mChildren
[c
];
925 SetLength(mParent
.mChildren
, Length(mParent
.mChildren
)-1);
929 for f
:= 0 to High(mChildren
) do
931 mChildren
[f
].mParent
:= nil;
938 function TUIControl
.getColorIndex (): Integer; inline;
940 if (not enabled
) then begin result
:= ClrIdxDisabled
; exit
; end;
941 // top windows: no focus hack
942 if (self
is TUITopWindow
) then
944 if (getActive
) then begin result
:= ClrIdxActive
; exit
; end;
948 // if control cannot be focused, take "active" color scheme for it (it is easier this way)
949 if (not canFocus
) or (getActive
) then begin result
:= ClrIdxActive
; exit
; end;
951 result
:= ClrIdxInactive
;
954 procedure TUIControl
.updateStyle ();
960 while (ctl
<> nil) do
962 if (Length(ctl
.mStyleId
) <> 0) then begin stl
:= uiFindStyle(ctl
.mStyleId
); break
; end;
965 if (stl
= nil) then stl
:= uiFindStyle(''); // default
967 for ctl
in mChildren
do ctl
.updateStyle();
970 procedure TUIControl
.cacheStyle (root
: TUIStyle
);
974 //writeln('caching style for <', className, '> (', mCtl4Style, ')...');
977 mBackColor
[ClrIdxActive
] := root
.get('back-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(0, 0, 128));
978 mTextColor
[ClrIdxActive
] := root
.get('text-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
979 mFrameColor
[ClrIdxActive
] := root
.get('frame-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
980 mFrameTextColor
[ClrIdxActive
] := root
.get('frame-text-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
981 mFrameIconColor
[ClrIdxActive
] := root
.get('frame-icon-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(0, 255, 0));
982 mDarken
[ClrIdxActive
] := root
.get('darken', 'active', cst
).asInt(-1);
984 mBackColor
[ClrIdxDisabled
] := root
.get('back-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(0, 0, 128));
985 mTextColor
[ClrIdxDisabled
] := root
.get('text-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(127, 127, 127));
986 mFrameColor
[ClrIdxDisabled
] := root
.get('frame-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(127, 127, 127));
987 mFrameTextColor
[ClrIdxDisabled
] := root
.get('frame-text-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(127, 127, 127));
988 mFrameIconColor
[ClrIdxDisabled
] := root
.get('frame-icon-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(0, 127, 0));
989 mDarken
[ClrIdxDisabled
] := root
.get('darken', 'disabled', cst
).asInt(-1);
991 mBackColor
[ClrIdxInactive
] := root
.get('back-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(0, 0, 128));
992 mTextColor
[ClrIdxInactive
] := root
.get('text-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
993 mFrameColor
[ClrIdxInactive
] := root
.get('frame-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
994 mFrameTextColor
[ClrIdxInactive
] := root
.get('frame-text-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
995 mFrameIconColor
[ClrIdxInactive
] := root
.get('frame-icon-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(0, 255, 0));
996 mDarken
[ClrIdxInactive
] := root
.get('darken', 'inactive', cst
).asInt(-1);
1000 // ////////////////////////////////////////////////////////////////////////// //
1001 function TUIControl
.getDefSize (): TLaySize
; inline; begin result
:= mLayDefSize
; end;
1002 function TUIControl
.getMaxSize (): TLaySize
; inline; begin result
:= mLayMaxSize
; end;
1003 function TUIControl
.getFlex (): Integer; inline; begin result
:= mFlex
; end;
1004 function TUIControl
.isHorizBox (): Boolean; inline; begin result
:= mHoriz
; end;
1005 procedure TUIControl
.setHorizBox (v
: Boolean); inline; begin mHoriz
:= v
; end;
1006 function TUIControl
.canWrap (): Boolean; inline; begin result
:= mCanWrap
; end;
1007 procedure TUIControl
.setCanWrap (v
: Boolean); inline; begin mCanWrap
:= v
; end;
1008 function TUIControl
.isLineStart (): Boolean; inline; begin result
:= mLineStart
; end;
1009 procedure TUIControl
.setLineStart (v
: Boolean); inline; begin mLineStart
:= v
; end;
1010 function TUIControl
.getAlign (): Integer; inline; begin result
:= mAlign
; end;
1011 procedure TUIControl
.setAlign (v
: Integer); inline; begin mAlign
:= v
; end;
1012 function TUIControl
.getExpand (): Boolean; inline; begin result
:= mExpand
; end;
1013 procedure TUIControl
.setExpand (v
: Boolean); inline; begin mExpand
:= v
; end;
1014 function TUIControl
.getHGroup (): AnsiString; inline; begin result
:= mHGroup
; end;
1015 procedure TUIControl
.setHGroup (const v
: AnsiString); inline; begin mHGroup
:= v
; end;
1016 function TUIControl
.getVGroup (): AnsiString; inline; begin result
:= mVGroup
; end;
1017 procedure TUIControl
.setVGroup (const v
: AnsiString); inline; begin mVGroup
:= v
; end;
1019 function TUIControl
.getMargins (): TLayMargins
; inline;
1021 result
:= TLayMargins
.Create(mFrameHeight
, mFrameWidth
, mFrameHeight
, mFrameWidth
);
1024 procedure TUIControl
.setActualSizePos (constref apos
: TLayPos
; constref asize
: TLaySize
); inline;
1026 //writeln(self.className, '; pos=', apos.toString, '; size=', asize.toString);
1027 if (mParent
<> nil) then
1036 procedure TUIControl
.layPrepare ();
1038 mLayDefSize
:= mDefSize
;
1039 mLayMaxSize
:= mMaxSize
;
1040 if (mLayMaxSize
.w
>= 0) then mLayMaxSize
.w
+= mFrameWidth
*2;
1041 if (mLayMaxSize
.h
>= 0) then mLayMaxSize
.h
+= mFrameHeight
*2;
1045 // ////////////////////////////////////////////////////////////////////////// //
1046 function TUIControl
.parsePos (par
: TTextParser
): TLayPos
;
1048 ech
: AnsiChar = ')';
1050 if (par
.eatDelim('[')) then ech
:= ']' else par
.expectDelim('(');
1051 result
.x
:= par
.expectInt();
1052 par
.eatDelim(','); // optional comma
1053 result
.y
:= par
.expectInt();
1054 par
.eatDelim(','); // optional comma
1055 par
.expectDelim(ech
);
1058 function TUIControl
.parseSize (par
: TTextParser
): TLaySize
;
1060 ech
: AnsiChar = ')';
1062 if (par
.eatDelim('[')) then ech
:= ']' else par
.expectDelim('(');
1063 result
.w
:= par
.expectInt();
1064 par
.eatDelim(','); // optional comma
1065 result
.h
:= par
.expectInt();
1066 par
.eatDelim(','); // optional comma
1067 par
.expectDelim(ech
);
1070 function TUIControl
.parseBool (par
: TTextParser
): Boolean;
1073 par
.eatIdOrStrCI('true') or
1074 par
.eatIdOrStrCI('yes') or
1075 par
.eatIdOrStrCI('tan');
1078 if (not par
.eatIdOrStrCI('false')) and (not par
.eatIdOrStrCI('no')) and (not par
.eatIdOrStrCI('ona')) then
1080 par
.error('boolean value expected');
1085 function TUIControl
.parseAnyAlign (par
: TTextParser
): Integer;
1087 if (par
.eatIdOrStrCI('left')) or (par
.eatIdOrStrCI('top')) then result
:= -1
1088 else if (par
.eatIdOrStrCI('right')) or (par
.eatIdOrStrCI('bottom')) then result
:= 1
1089 else if (par
.eatIdOrStrCI('center')) then result
:= 0
1090 else par
.error('invalid align value');
1093 function TUIControl
.parseHAlign (par
: TTextParser
): Integer;
1095 if (par
.eatIdOrStrCI('left')) then result
:= -1
1096 else if (par
.eatIdOrStrCI('right')) then result
:= 1
1097 else if (par
.eatIdOrStrCI('center')) then result
:= 0
1098 else par
.error('invalid horizontal align value');
1101 function TUIControl
.parseVAlign (par
: TTextParser
): Integer;
1103 if (par
.eatIdOrStrCI('top')) then result
:= -1
1104 else if (par
.eatIdOrStrCI('bottom')) then result
:= 1
1105 else if (par
.eatIdOrStrCI('center')) then result
:= 0
1106 else par
.error('invalid vertical align value');
1109 procedure TUIControl
.parseTextAlign (par
: TTextParser
; var h
, v
: Integer);
1111 wasH
: Boolean = false;
1112 wasV
: Boolean = false;
1116 if (par
.eatIdOrStrCI('left')) then
1118 if wasH
then par
.error('too many align directives');
1123 if (par
.eatIdOrStrCI('right')) then
1125 if wasH
then par
.error('too many align directives');
1130 if (par
.eatIdOrStrCI('hcenter')) then
1132 if wasH
then par
.error('too many align directives');
1137 if (par
.eatIdOrStrCI('top')) then
1139 if wasV
then par
.error('too many align directives');
1144 if (par
.eatIdOrStrCI('bottom')) then
1146 if wasV
then par
.error('too many align directives');
1151 if (par
.eatIdOrStrCI('vcenter')) then
1153 if wasV
then par
.error('too many align directives');
1158 if (par
.eatIdOrStrCI('center')) then
1160 if wasV
or wasH
then par
.error('too many align directives');
1169 if not wasV
and not wasH
then par
.error('invalid align value');
1172 function TUIControl
.parseOrientation (const prname
: AnsiString; par
: TTextParser
): Boolean;
1174 if (strEquCI1251(prname
, 'orientation')) or (strEquCI1251(prname
, 'orient')) then
1176 if (par
.eatIdOrStrCI('horizontal')) or (par
.eatIdOrStrCI('horiz')) then mHoriz
:= true
1177 else if (par
.eatIdOrStrCI('vertical')) or (par
.eatIdOrStrCI('vert')) then mHoriz
:= false
1178 else par
.error('`horizontal` or `vertical` expected');
1187 // par should be on '{'; final '}' is eaten
1188 procedure TUIControl
.parseProperties (par
: TTextParser
);
1192 if (not par
.eatDelim('{')) then exit
;
1193 while (not par
.eatDelim('}')) do
1195 if (not par
.isIdOrStr
) then par
.error('property name expected');
1198 par
.eatDelim(':'); // optional
1199 if not parseProperty(pn
, par
) then par
.errorfmt('invalid property name ''%s''', [pn
]);
1200 par
.eatDelim(','); // optional
1204 // par should be on '{'
1205 procedure TUIControl
.parseChildren (par
: TTextParser
);
1207 cc
: TUIControlClass
;
1210 par
.expectDelim('{');
1211 while (not par
.eatDelim('}')) do
1213 if (not par
.isIdOrStr
) then par
.error('control name expected');
1214 cc
:= findCtlClass(par
.tokStr
);
1215 if (cc
= nil) then par
.errorfmt('unknown control name: ''%s''', [par
.tokStr
]);
1216 //writeln('children for <', par.tokStr, '>: <', cc.className, '>');
1218 par
.eatDelim(':'); // optional
1220 //writeln(' mHoriz=', ctl.mHoriz);
1222 ctl
.parseProperties(par
);
1227 //writeln(': ', ctl.mDefSize.toString);
1229 par
.eatDelim(','); // optional
1234 function TUIControl
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1237 if (strEquCI1251(prname
, 'id')) then begin mId
:= par
.expectIdOrStr(true); exit
; end; // allow empty strings
1238 if (strEquCI1251(prname
, 'style')) then begin mStyleId
:= par
.expectIdOrStr(); exit
; end; // no empty strings
1239 if (strEquCI1251(prname
, 'flex')) then begin flex
:= par
.expectInt(); exit
; end;
1241 if (strEquCI1251(prname
, 'defsize')) or (strEquCI1251(prname
, 'size')) then begin mDefSize
:= parseSize(par
); exit
; end;
1242 if (strEquCI1251(prname
, 'maxsize')) then begin mMaxSize
:= parseSize(par
); exit
; end;
1243 if (strEquCI1251(prname
, 'defwidth')) or (strEquCI1251(prname
, 'width')) then begin mDefSize
.w
:= par
.expectInt(); exit
; end;
1244 if (strEquCI1251(prname
, 'defheight')) or (strEquCI1251(prname
, 'height')) then begin mDefSize
.h
:= par
.expectInt(); exit
; end;
1245 if (strEquCI1251(prname
, 'maxwidth')) then begin mMaxSize
.w
:= par
.expectInt(); exit
; end;
1246 if (strEquCI1251(prname
, 'maxheight')) then begin mMaxSize
.h
:= par
.expectInt(); exit
; end;
1248 if (strEquCI1251(prname
, 'wrap')) then begin mCanWrap
:= parseBool(par
); exit
; end;
1249 if (strEquCI1251(prname
, 'linestart')) then begin mLineStart
:= parseBool(par
); exit
; end;
1250 if (strEquCI1251(prname
, 'expand')) then begin mExpand
:= parseBool(par
); exit
; end;
1252 if (strEquCI1251(prname
, 'align')) then begin mAlign
:= parseAnyAlign(par
); exit
; end;
1253 if (strEquCI1251(prname
, 'hgroup')) then begin mHGroup
:= par
.expectIdOrStr(true); exit
; end; // allow empty strings
1254 if (strEquCI1251(prname
, 'vgroup')) then begin mVGroup
:= par
.expectIdOrStr(true); exit
; end; // allow empty strings
1256 if (strEquCI1251(prname
, 'canfocus')) then begin mCanFocus
:= true; exit
; end;
1257 if (strEquCI1251(prname
, 'nofocus')) then begin mCanFocus
:= false; exit
; end;
1258 if (strEquCI1251(prname
, 'disabled')) then begin mEnabled
:= false; exit
; end;
1259 if (strEquCI1251(prname
, 'enabled')) then begin mEnabled
:= true; exit
; end;
1260 if (strEquCI1251(prname
, 'escclose')) then begin mEscClose
:= not parseBool(par
); exit
; end;
1261 if (strEquCI1251(prname
, 'default')) then begin mDefault
:= true; exit
; end;
1262 if (strEquCI1251(prname
, 'cancel')) then begin mCancel
:= true; exit
; end;
1267 // ////////////////////////////////////////////////////////////////////////// //
1268 procedure TUIControl
.activated ();
1270 makeVisibleInParent();
1274 procedure TUIControl
.blurred ();
1276 if (uiGrabCtl
= self
) then uiGrabCtl
:= nil;
1280 procedure TUIControl
.calcFullClientSize ();
1284 mFullSize
:= TLaySize
.Create(0, 0);
1285 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1286 for ctl
in mChildren
do
1288 ctl
.calcFullClientSize();
1289 mFullSize
.w
:= nmax(mFullSize
.w
, ctl
.mX
-mFrameWidth
+ctl
.mFullSize
.w
);
1290 mFullSize
.h
:= nmax(mFullSize
.h
, ctl
.mY
-mFrameHeight
+ctl
.mFullSize
.h
);
1292 mFullSize
.w
:= nmax(mFullSize
.w
, mWidth
-mFrameWidth
*2);
1293 mFullSize
.h
:= nmax(mFullSize
.h
, mHeight
-mFrameHeight
*2);
1297 function TUIControl
.topLevel (): TUIControl
; inline;
1300 while (result
.mParent
<> nil) do result
:= result
.mParent
;
1304 function TUIControl
.getEnabled (): Boolean;
1309 if (not mEnabled
) then exit
;
1311 while (ctl
<> nil) do
1313 if (not ctl
.mEnabled
) then exit
;
1320 procedure TUIControl
.setEnabled (v
: Boolean); inline;
1322 if (mEnabled
= v
) then exit
;
1324 if (not v
) and focused
then setFocused(false);
1328 function TUIControl
.getFocused (): Boolean; inline;
1330 if (mParent
= nil) then
1332 result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = self
);
1336 result
:= (topLevel
.mFocused
= self
);
1337 if (result
) then result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = topLevel
);
1342 function TUIControl
.getActive (): Boolean; inline;
1346 if (mParent
= nil) then
1348 result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = self
);
1352 ctl
:= topLevel
.mFocused
;
1353 while (ctl
<> nil) and (ctl
<> self
) do ctl
:= ctl
.mParent
;
1354 result
:= (ctl
= self
);
1355 if (result
) then result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = topLevel
);
1360 procedure TUIControl
.setFocused (v
: Boolean); inline;
1367 if (tl
.mFocused
= self
) then
1369 blurred(); // this will reset grab, but still...
1370 if (uiGrabCtl
= self
) then uiGrabCtl
:= nil;
1371 tl
.mFocused
:= tl
.findNextFocus(self
, true);
1372 if (tl
.mFocused
= self
) then tl
.mFocused
:= nil;
1373 if (tl
.mFocused
<> nil) then tl
.mFocused
.activated();
1377 if (not canFocus
) then exit
;
1378 if (tl
.mFocused
<> self
) then
1380 if (tl
.mFocused
<> nil) then tl
.mFocused
.blurred();
1381 tl
.mFocused
:= self
;
1382 if (uiGrabCtl
<> self
) then uiGrabCtl
:= nil;
1388 function TUIControl
.getCanFocus (): Boolean; inline;
1390 result
:= (getEnabled
) and (mCanFocus
) and (mWidth
> 0) and (mHeight
> 0);
1394 function TUIControl
.isMyChild (ctl
: TUIControl
): Boolean;
1397 while (ctl
<> nil) do
1399 if (ctl
.mParent
= self
) then exit
;
1406 // returns `true` if global coords are inside this control
1407 function TUIControl
.toLocal (var x
, y
: Integer): Boolean;
1409 if (mParent
= nil) then
1413 result
:= true; // hack
1417 result
:= mParent
.toLocal(x
, y
);
1418 Inc(x
, mParent
.mScrollX
);
1419 Inc(y
, mParent
.mScrollY
);
1422 if result
then result
:= (x
>= 0) and (y
>= 0) and (x
< mParent
.mWidth
) and (y
< mParent
.mHeight
);
1424 if result
then result
:= (x
>= 0) and (y
>= 0) and (x
< mWidth
) and (y
< mHeight
);
1427 function TUIControl
.toLocal (gx
, gy
: Integer; out x
, y
: Integer): Boolean; inline;
1431 result
:= toLocal(x
, y
);
1435 procedure TUIControl
.toGlobal (var x
, y
: Integer);
1439 if (mParent
<> nil) then
1441 Dec(x
, mParent
.mScrollX
);
1442 Dec(y
, mParent
.mScrollY
);
1443 mParent
.toGlobal(x
, y
);
1447 procedure TUIControl
.toGlobal (lx
, ly
: Integer; out x
, y
: Integer); inline;
1454 procedure TUIControl
.getDrawRect (out gx
, gy
, wdt
, hgt
: Integer);
1458 if (mParent
= nil) then
1467 toGlobal(0, 0, cgx
, cgy
);
1468 mParent
.getDrawRect(gx
, gy
, wdt
, hgt
);
1469 if (wdt
> 0) and (hgt
> 0) then
1471 if not intersectRect(gx
, gy
, wdt
, hgt
, cgx
, cgy
, mWidth
, mHeight
) then
1481 // x and y are global coords
1482 function TUIControl
.controlAtXY (x
, y
: Integer; allowDisabled
: Boolean=false): TUIControl
;
1488 if (not allowDisabled
) and (not enabled
) then exit
;
1489 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1490 if not toLocal(x
, y
, lx
, ly
) then exit
;
1491 for f
:= High(mChildren
) downto 0 do
1493 result
:= mChildren
[f
].controlAtXY(x
, y
, allowDisabled
);
1494 if (result
<> nil) then exit
;
1500 function TUIControl
.parentScrollX (): Integer; inline; begin if (mParent
<> nil) then result
:= mParent
.mScrollX
else result
:= 0; end;
1501 function TUIControl
.parentScrollY (): Integer; inline; begin if (mParent
<> nil) then result
:= mParent
.mScrollY
else result
:= 0; end;
1504 procedure TUIControl
.makeVisibleInParent ();
1506 sy
, ey
, cy
: Integer;
1509 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1511 if (p
= nil) then exit
;
1512 if (p
.mFullSize
.w
< 1) or (p
.mFullSize
.h
< 1) then
1518 p
.makeVisibleInParent();
1519 cy
:= mY
-p
.mFrameHeight
;
1521 ey
:= sy
+(p
.mHeight
-p
.mFrameHeight
*2);
1524 p
.mScrollY
:= nmax(0, cy
);
1526 else if (cy
+mHeight
> ey
) then
1528 p
.mScrollY
:= nmax(0, cy
+mHeight
-(p
.mHeight
-p
.mFrameHeight
*2));
1533 // ////////////////////////////////////////////////////////////////////////// //
1534 function TUIControl
.prevSibling (): TUIControl
;
1538 if (mParent
<> nil) then
1540 for f
:= 1 to High(mParent
.mChildren
) do
1542 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
-1]; exit
; end;
1548 function TUIControl
.nextSibling (): TUIControl
;
1552 if (mParent
<> nil) then
1554 for f
:= 0 to High(mParent
.mChildren
)-1 do
1556 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
+1]; exit
; end;
1562 function TUIControl
.firstChild (): TUIControl
; inline;
1564 if (Length(mChildren
) <> 0) then result
:= mChildren
[0] else result
:= nil;
1567 function TUIControl
.lastChild (): TUIControl
; inline;
1569 if (Length(mChildren
) <> 0) then result
:= mChildren
[High(mChildren
)] else result
:= nil;
1573 function TUIControl
.findFirstFocus (): TUIControl
;
1580 for f
:= 0 to High(mChildren
) do
1582 result
:= mChildren
[f
].findFirstFocus();
1583 if (result
<> nil) then exit
;
1585 if (canFocus
) then result
:= self
;
1590 function TUIControl
.findLastFocus (): TUIControl
;
1597 for f
:= High(mChildren
) downto 0 do
1599 result
:= mChildren
[f
].findLastFocus();
1600 if (result
<> nil) then exit
;
1602 if (canFocus
) then result
:= self
;
1607 function TUIControl
.findNextFocus (cur
: TUIControl
; wrap
: Boolean): TUIControl
;
1609 curHit
: Boolean = false;
1611 function checkFocus (ctl
: TUIControl
): Boolean;
1615 result
:= (ctl
.canFocus
);
1619 curHit
:= (ctl
= cur
);
1620 result
:= false; // don't stop
1628 if not isMyChild(cur
) then
1630 result
:= findFirstFocus();
1634 result
:= forEachControl(checkFocus
);
1635 if (result
= nil) and (wrap
) then result
:= findFirstFocus();
1641 function TUIControl
.findPrevFocus (cur
: TUIControl
; wrap
: Boolean): TUIControl
;
1643 lastCtl
: TUIControl
= nil;
1645 function checkFocus (ctl
: TUIControl
): Boolean;
1654 if (ctl
.canFocus
) then lastCtl
:= ctl
;
1662 if not isMyChild(cur
) then
1664 result
:= findLastFocus();
1668 forEachControl(checkFocus
);
1669 if (lastCtl
= nil) and (wrap
) then lastCtl
:= findLastFocus();
1671 //if (lastCtl <> nil) then writeln('ctl<', lastCtl.className, '>: {', lastCtl.id, '}');
1677 function TUIControl
.findDefaulControl (): TUIControl
;
1683 if (mDefault
) then begin result
:= self
; exit
; end;
1684 for ctl
in mChildren
do
1686 result
:= ctl
.findDefaulControl();
1687 if (result
<> nil) then exit
;
1693 function TUIControl
.findCancelControl (): TUIControl
;
1699 if (mCancel
) then begin result
:= self
; exit
; end;
1700 for ctl
in mChildren
do
1702 result
:= ctl
.findCancelControl();
1703 if (result
<> nil) then exit
;
1710 function TUIControl
.findControlById (const aid
: AnsiString): TUIControl
;
1714 if (strEquCI1251(aid
, mId
)) then begin result
:= self
; exit
; end;
1715 for ctl
in mChildren
do
1717 result
:= ctl
.findControlById(aid
);
1718 if (result
<> nil) then exit
;
1724 procedure TUIControl
.appendChild (ctl
: TUIControl
);
1726 if (ctl
= nil) then exit
;
1727 if (ctl
.mParent
<> nil) then exit
;
1728 SetLength(mChildren
, Length(mChildren
)+1);
1729 mChildren
[High(mChildren
)] := ctl
;
1730 ctl
.mParent
:= self
;
1731 Inc(ctl
.mX
, mFrameWidth
);
1732 Inc(ctl
.mY
, mFrameHeight
);
1733 if (ctl
.mWidth
> 0) and (ctl
.mHeight
> 0) and
1734 (ctl
.mX
+ctl
.mWidth
> mFrameWidth
) and (ctl
.mY
+ctl
.mHeight
> mFrameHeight
) then
1736 if (mWidth
+mFrameWidth
< ctl
.mX
+ctl
.mWidth
) then mWidth
:= ctl
.mX
+ctl
.mWidth
+mFrameWidth
;
1737 if (mHeight
+mFrameHeight
< ctl
.mY
+ctl
.mHeight
) then mHeight
:= ctl
.mY
+ctl
.mHeight
+mFrameHeight
;
1742 function TUIControl
.setActionCBFor (const aid
: AnsiString; cb
: TActionCB
): TActionCB
;
1747 if (ctl
<> nil) then
1749 result
:= ctl
.actionCB
;
1759 function TUIControl
.forEachChildren (cb
: TCtlEnumCB
): TUIControl
;
1764 if (not assigned(cb
)) then exit
;
1765 for ctl
in mChildren
do
1767 if cb(ctl
) then begin result
:= ctl
; exit
; end;
1772 function TUIControl
.forEachControl (cb
: TCtlEnumCB
; includeSelf
: Boolean=true): TUIControl
;
1774 function forChildren (p
: TUIControl
; incSelf
: Boolean): TUIControl
;
1779 if (p
= nil) then exit
;
1780 if (incSelf
) and (cb(p
)) then begin result
:= p
; exit
; end;
1781 for ctl
in p
.mChildren
do
1783 result
:= forChildren(ctl
, true);
1784 if (result
<> nil) then break
;
1790 if (not assigned(cb
)) then exit
;
1791 result
:= forChildren(self
, includeSelf
);
1795 procedure TUIControl
.close (); // this closes *top-level* control
1800 uiRemoveWindow(ctl
);
1801 if (ctl
is TUITopWindow
) and (TUITopWindow(ctl
).mFreeOnClose
) then scheduleKill(ctl
); // just in case
1805 procedure TUIControl
.doAction ();
1807 if assigned(actionCB
) then actionCB(self
);
1811 // ////////////////////////////////////////////////////////////////////////// //
1812 procedure TUIControl
.setScissorGLInternal (x
, y
, w
, h
: Integer);
1814 if not scallowed
then exit
;
1815 x
:= trunc(x
*gh_ui_scale
);
1816 y
:= trunc(y
*gh_ui_scale
);
1817 w
:= trunc(w
*gh_ui_scale
);
1818 h
:= trunc(h
*gh_ui_scale
);
1819 scis
.combineRect(x
, y
, w
, h
);
1822 procedure TUIControl
.setScissor (lx
, ly
, lw
, lh
: Integer);
1824 gx
, gy
, wdt
, hgt
, cgx
, cgy
: Integer;
1826 if not scallowed
then exit
;
1828 if not intersectRect(lx
, ly
, lw
, lh
, 0, 0, mWidth
, mHeight
) then
1830 scis
.combineRect(0, 0, 0, 0);
1834 getDrawRect(gx
, gy
, wdt
, hgt
);
1835 toGlobal(lx
, ly
, cgx
, cgy
);
1836 if not intersectRect(gx
, gy
, wdt
, hgt
, cgx
, cgy
, lw
, lh
) then
1838 scis
.combineRect(0, 0, 0, 0);
1842 setScissorGLInternal(gx
, gy
, wdt
, hgt
);
1845 procedure TUIControl
.resetScissor (fullArea
: Boolean); inline;
1847 if not scallowed
then exit
;
1850 setScissor(0, 0, mWidth
, mHeight
);
1854 setScissor(mFrameWidth
, mFrameHeight
, mWidth
-mFrameWidth
*2, mHeight
-mFrameHeight
*2);
1859 // ////////////////////////////////////////////////////////////////////////// //
1860 procedure TUIControl
.draw ();
1865 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1866 toGlobal(0, 0, gx
, gy
);
1868 scis
.save(true); // scissoring enabled
1871 resetScissor(true); // full area
1872 drawControl(gx
, gy
);
1873 resetScissor(false); // client area
1874 for f
:= 0 to High(mChildren
) do mChildren
[f
].draw();
1875 resetScissor(true); // full area
1876 drawControlPost(gx
, gy
);
1883 procedure TUIControl
.drawControl (gx
, gy
: Integer);
1885 //if (mParent = nil) then darkenRect(gx, gy, mWidth, mHeight, 64);
1888 procedure TUIControl
.drawControlPost (gx
, gy
: Integer);
1891 if mDrawShadow
and (mWidth
> 0) and (mHeight
> 0) then
1893 setScissorGLInternal(gx
+8, gy
+8, mWidth
, mHeight
);
1894 darkenRect(gx
+mWidth
, gy
+8, 8, mHeight
, 128);
1895 darkenRect(gx
+8, gy
+mHeight
, mWidth
-8, 8, 128);
1900 // ////////////////////////////////////////////////////////////////////////// //
1901 procedure TUIControl
.mouseEvent (var ev
: THMouseEvent
);
1905 if (not enabled
) then exit
;
1906 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1907 ctl
:= controlAtXY(ev
.x
, ev
.y
);
1908 if (ctl
= nil) then exit
;
1909 if (ctl
.canFocus
) and (ev
.press
) then
1911 if (ctl
<> topLevel
.mFocused
) then ctl
.setFocused(true);
1914 if (ctl
<> self
) then ctl
.mouseEvent(ev
);
1919 procedure TUIControl
.keyEvent (var ev
: THKeyEvent
);
1921 function doPreKey (ctl
: TUIControl
): Boolean;
1923 if (not ctl
.enabled
) then begin result
:= false; exit
; end;
1924 ctl
.keyEventPre(ev
);
1925 result
:= (ev
.eaten
) or (ev
.cancelled
); // stop if event was consumed
1928 function doPostKey (ctl
: TUIControl
): Boolean;
1930 if (not ctl
.enabled
) then begin result
:= false; exit
; end;
1931 ctl
.keyEventPost(ev
);
1932 result
:= (ev
.eaten
) or (ev
.cancelled
); // stop if event was consumed
1938 if (not enabled
) then exit
;
1939 if (ev
.eaten
) or (ev
.cancelled
) then exit
;
1941 if (mParent
= nil) then
1943 forEachControl(doPreKey
);
1944 if (ev
.eaten
) or (ev
.cancelled
) then exit
;
1946 // focused control should process keyboard first
1947 if (topLevel
.mFocused
<> self
) and isMyChild(topLevel
.mFocused
) and (topLevel
.mFocused
.enabled
) then
1949 // bubble keyboard event
1950 ctl
:= topLevel
.mFocused
;
1951 while (ctl
<> nil) and (ctl
<> self
) do
1954 if (ev
.eaten
) or (ev
.cancelled
) then exit
;
1958 // for top-level controls
1959 if (mParent
= nil) then
1961 if (ev
= 'S-Tab') then
1963 ctl
:= findPrevFocus(mFocused
, true);
1964 if (ctl
<> nil) and (ctl
<> mFocused
) then ctl
.setFocused(true);
1968 if (ev
= 'Tab') then
1970 ctl
:= findNextFocus(mFocused
, true);
1971 if (ctl
<> nil) and (ctl
<> mFocused
) then ctl
.setFocused(true);
1975 if (ev
= 'Enter') or (ev
= 'C-Enter') then
1977 ctl
:= findDefaulControl();
1978 if (ctl
<> nil) then
1985 if (ev
= 'Escape') then
1987 ctl
:= findCancelControl();
1988 if (ctl
<> nil) then
1995 if mEscClose
and (ev
= 'Escape') then
1997 if (not assigned(closeRequestCB
)) or (closeRequestCB(self
)) then
1999 uiRemoveWindow(self
);
2005 if (ev
.eaten
) or (ev
.cancelled
) then exit
;
2006 forEachControl(doPostKey
);
2011 procedure TUIControl
.keyEventPre (var ev
: THKeyEvent
);
2016 procedure TUIControl
.keyEventPost (var ev
: THKeyEvent
);
2021 // ////////////////////////////////////////////////////////////////////////// //
2022 constructor TUITopWindow
.Create (const atitle
: AnsiString);
2029 procedure TUITopWindow
.AfterConstruction ();
2032 mFitToScreen
:= true;
2035 if (mWidth
< mFrameWidth
*2+3*8) then mWidth
:= mFrameWidth
*2+3*8;
2036 if (mHeight
< mFrameHeight
*2) then mHeight
:= mFrameHeight
*2;
2037 if (Length(mTitle
) > 0) then
2039 if (mWidth
< Length(mTitle
)*8+mFrameWidth
*2+3*8) then mWidth
:= Length(mTitle
)*8+mFrameWidth
*2+3*8;
2042 mDragScroll
:= TXMode
.None
;
2043 mDrawShadow
:= true;
2044 mWaitingClose
:= false;
2047 mCtl4Style
:= 'window';
2051 function TUITopWindow
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2053 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
2055 mTitle
:= par
.expectIdOrStr(true);
2059 if (strEquCI1251(prname
, 'children')) then
2065 if (strEquCI1251(prname
, 'position')) then
2067 if (par
.eatIdOrStrCI('default')) then mDoCenter
:= false
2068 else if (par
.eatIdOrStrCI('center')) then mDoCenter
:= true
2069 else par
.error('`center` or `default` expected');
2073 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
2074 result
:= inherited parseProperty(prname
, par
);
2078 procedure TUITopWindow
.flFitToScreen ();
2080 flMaxSize
:= TLaySize
.Create(trunc(getScrWdt
/gh_ui_scale
)-mFrameWidth
*2-6, trunc(getScrHgt
/gh_ui_scale
)-mFrameHeight
*2-6);
2084 procedure TUITopWindow
.centerInScreen ();
2086 if (mWidth
> 0) and (mHeight
> 0) then
2088 mX
:= trunc((gScrWidth
/gh_ui_scale
-mWidth
)/2);
2089 mY
:= trunc((gScrHeight
/gh_ui_scale
-mHeight
)/2);
2094 procedure TUITopWindow
.drawControl (gx
, gy
: Integer);
2096 fillRect(gx
, gy
, mWidth
, mHeight
, mBackColor
[getColorIndex
]);
2100 procedure TUITopWindow
.drawControlPost (gx
, gy
: Integer);
2103 tx
, hgt
, sbhgt
: Integer;
2105 cidx
:= getColorIndex
;
2106 if (mDragScroll
= TXMode
.Drag
) then
2108 drawRectUI(gx
+4, gy
+4, mWidth
-8, mHeight
-8, mFrameColor
[cidx
]);
2112 drawRectUI(gx
+3, gy
+3, mWidth
-6, mHeight
-6, mFrameColor
[cidx
]);
2113 drawRectUI(gx
+5, gy
+5, mWidth
-10, mHeight
-10, mFrameColor
[cidx
]);
2114 // vertical scroll bar
2115 hgt
:= mHeight
-mFrameHeight
*2;
2116 if (hgt
> 0) and (mFullSize
.h
> hgt
) then
2118 //writeln(mTitle, ': height=', mHeight-mFrameHeight*2, '; fullsize=', mFullSize.toString);
2119 sbhgt
:= mHeight
-mFrameHeight
*2+2;
2120 fillRect(gx
+mWidth
-mFrameWidth
+1, gy
+7, mFrameWidth
-3, sbhgt
, mFrameColor
[cidx
]);
2122 if (hgt
> mFullSize
.h
) then hgt
:= mFullSize
.h
;
2123 hgt
:= sbhgt
*hgt
div mFullSize
.h
;
2126 setScissor(mWidth
-mFrameWidth
+1, 7, mFrameWidth
-3, sbhgt
);
2127 darkenRect(gx
+mWidth
-mFrameWidth
+1, gy
+7+hgt
, mFrameWidth
-3, sbhgt
, 128);
2131 setScissor(mFrameWidth
, 0, 3*8, 8);
2132 fillRect(gx
+mFrameWidth
, gy
, 3*8, 8, mBackColor
[cidx
]);
2133 drawText8(gx
+mFrameWidth
, gy
, '[ ]', mFrameColor
[cidx
]);
2134 if mInClose
then drawText8(gx
+mFrameWidth
+7, gy
, '#', mFrameIconColor
[cidx
])
2135 else drawText8(gx
+mFrameWidth
+7, gy
, '*', mFrameIconColor
[cidx
]);
2138 if (Length(mTitle
) > 0) then
2140 setScissor(mFrameWidth
+3*8, 0, mWidth
-mFrameWidth
*2-3*8, 8);
2141 tx
:= (gx
+3*8)+((mWidth
-3*8)-Length(mTitle
)*8) div 2;
2142 fillRect(tx
-3, gy
, Length(mTitle
)*8+3+2, 8, mBackColor
[cidx
]);
2143 drawText8(tx
, gy
, mTitle
, mFrameTextColor
[cidx
]);
2146 inherited drawControlPost(gx
, gy
);
2150 procedure TUITopWindow
.activated ();
2152 if (mFocused
= nil) or (mFocused
= self
) then
2154 mFocused
:= findFirstFocus();
2156 if (mFocused
<> nil) and (mFocused
<> self
) then mFocused
.activated();
2161 procedure TUITopWindow
.blurred ();
2163 mDragScroll
:= TXMode
.None
;
2164 mWaitingClose
:= false;
2166 if (mFocused
<> nil) and (mFocused
<> self
) then mFocused
.blurred();
2171 procedure TUITopWindow
.keyEvent (var ev
: THKeyEvent
);
2173 inherited keyEvent(ev
);
2174 if (ev
.eaten
) or (ev
.cancelled
) or (not enabled
) {or (not getFocused)} then exit
;
2175 if (ev
= 'M-F3') then
2177 if (not assigned(closeRequestCB
)) or (closeRequestCB(self
)) then
2179 uiRemoveWindow(self
);
2187 procedure TUITopWindow
.mouseEvent (var ev
: THMouseEvent
);
2190 hgt
, sbhgt
: Integer;
2192 if (not enabled
) then exit
;
2193 if (mWidth
< 1) or (mHeight
< 1) then exit
;
2195 if (mDragScroll
= TXMode
.Drag
) then
2197 mX
+= ev
.x
-mDragStartX
;
2198 mY
+= ev
.y
-mDragStartY
;
2199 mDragStartX
:= ev
.x
;
2200 mDragStartY
:= ev
.y
;
2201 if (ev
.release
) and (ev
.but
= ev
.Left
) then mDragScroll
:= TXMode
.None
;
2206 if (mDragScroll
= TXMode
.Scroll
) then
2208 // check for vertical scrollbar
2216 sbhgt
:= mHeight
-mFrameHeight
*2+2;
2217 hgt
:= mHeight
-mFrameHeight
*2;
2218 if (hgt
> 0) and (mFullSize
.h
> hgt
) then
2220 hgt
:= (mFullSize
.h
*(ly
-7) div (sbhgt
-1))-(mHeight
-mFrameHeight
*2);
2221 mScrollY
:= nmax(0, hgt
);
2222 hgt
:= mHeight
-mFrameHeight
*2;
2223 if (mScrollY
+hgt
> mFullSize
.h
) then mScrollY
:= nmax(0, mFullSize
.h
-hgt
);
2226 if (ev
.release
) and (ev
.but
= ev
.Left
) then mDragScroll
:= TXMode
.None
;
2231 if toLocal(ev
.x
, ev
.y
, lx
, ly
) then
2238 if (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8) then
2240 //uiRemoveWindow(self);
2241 mWaitingClose
:= true;
2246 mDragScroll
:= TXMode
.Drag
;
2247 mDragStartX
:= ev
.x
;
2248 mDragStartY
:= ev
.y
;
2253 // check for vertical scrollbar
2254 if (lx
>= mWidth
-mFrameWidth
+1) and (ly
>= 7) and (ly
< mHeight
-mFrameHeight
+1) then
2256 sbhgt
:= mHeight
-mFrameHeight
*2+2;
2257 hgt
:= mHeight
-mFrameHeight
*2;
2258 if (hgt
> 0) and (mFullSize
.h
> hgt
) then
2260 hgt
:= (mFullSize
.h
*(ly
-7) div (sbhgt
-1))-(mHeight
-mFrameHeight
*2);
2261 mScrollY
:= nmax(0, hgt
);
2263 mDragScroll
:= TXMode
.Scroll
;
2269 if (lx
< mFrameWidth
) or (lx
>= mWidth
-mFrameWidth
) or (ly
>= mHeight
-mFrameHeight
) then
2272 mDragScroll
:= TXMode
.Drag
;
2273 mDragStartX
:= ev
.x
;
2274 mDragStartY
:= ev
.y
;
2280 if (ev
.release
) then
2282 if mWaitingClose
then
2284 if (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8) then
2286 if (not assigned(closeRequestCB
)) or (closeRequestCB(self
)) then
2288 uiRemoveWindow(self
);
2291 mWaitingClose
:= false;
2300 if mWaitingClose
then
2302 mInClose
:= (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8);
2308 inherited mouseEvent(ev
);
2313 if (not ev
.motion
) and (mWaitingClose
) then begin ev
.eat(); mWaitingClose
:= false; exit
; end;
2318 // ////////////////////////////////////////////////////////////////////////// //
2319 constructor TUIBox
.Create (ahoriz
: Boolean);
2326 procedure TUIBox
.AfterConstruction ();
2330 mHAlign
:= -1; // left
2331 mCtl4Style
:= 'box';
2335 procedure TUIBox
.setCaption (const acap
: AnsiString);
2338 mDefSize
:= TLaySize
.Create(Length(mCaption
)*8+3, 8);
2342 procedure TUIBox
.setHasFrame (v
: Boolean);
2345 if (mHasFrame
) then begin mFrameWidth
:= 8; mFrameHeight
:= 8; end else begin mFrameWidth
:= 0; mFrameHeight
:= 0; end;
2349 function TUIBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2351 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
2352 if (strEquCI1251(prname
, 'frame')) then
2354 setHasFrame(parseBool(par
));
2358 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
2360 setCaption(par
.expectIdOrStr(true));
2364 if (strEquCI1251(prname
, 'textalign')) or (strEquCI1251(prname
, 'text-align')) then
2366 mHAlign
:= parseHAlign(par
);
2370 if (strEquCI1251(prname
, 'children')) then
2376 result
:= inherited parseProperty(prname
, par
);
2380 procedure TUIBox
.drawControl (gx
, gy
: Integer);
2385 cidx
:= getColorIndex
;
2386 fillRect(gx
, gy
, mWidth
, mHeight
, mBackColor
[cidx
]);
2390 drawRectUI(gx
+3, gy
+3, mWidth
-6, mHeight
-6, mFrameColor
[cidx
]);
2393 if (Length(mCaption
) > 0) then
2395 if (mHAlign
< 0) then xpos
:= 3
2396 else if (mHAlign
> 0) then xpos
:= mWidth
-mFrameWidth
*2-Length(mCaption
)*8
2397 else xpos
:= (mWidth
-mFrameWidth
*2-Length(mCaption
)*8) div 2;
2398 xpos
+= gx
+mFrameWidth
;
2400 setScissor(mFrameWidth
+1, 0, mWidth
-mFrameWidth
-2, 8);
2401 if mHasFrame
then fillRect(xpos
-3, gy
, Length(mCaption
)*8+4, 8, mBackColor
[cidx
]);
2402 drawText8(xpos
, gy
, mCaption
, mFrameTextColor
[cidx
]);
2407 procedure TUIBox
.mouseEvent (var ev
: THMouseEvent
);
2411 inherited mouseEvent(ev
);
2412 if (not ev
.eaten
) and (not ev
.cancelled
) and (enabled
) and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
2419 procedure TUIBox
.keyEvent (var ev
: THKeyEvent
);
2422 cur
, ctl
: TUIControl
;
2424 inherited keyEvent(ev
);
2425 if (ev
.eaten
) or (ev
.cancelled
) or (not ev
.press
) or (not enabled
) or (not getActive
) then exit
;
2426 if (Length(mChildren
) = 0) then exit
;
2427 if (mHoriz
) and (ev
= 'Left') then dir
:= -1
2428 else if (mHoriz
) and (ev
= 'Right') then dir
:= 1
2429 else if (not mHoriz
) and (ev
= 'Up') then dir
:= -1
2430 else if (not mHoriz
) and (ev
= 'Down') then dir
:= 1;
2431 if (dir
= 0) then exit
;
2433 cur
:= topLevel
.mFocused
;
2434 while (cur
<> nil) and (cur
.mParent
<> self
) do cur
:= cur
.mParent
;
2435 //if (cur = nil) then writeln('CUR: nil') else writeln('CUR: ', cur.className, '#', cur.id);
2436 if (dir
< 0) then ctl
:= findPrevFocus(cur
, true) else ctl
:= findNextFocus(cur
, true);
2437 //if (ctl = nil) then writeln('CTL: nil') else writeln('CTL: ', ctl.className, '#', ctl.id);
2438 if (ctl
<> nil) and (ctl
<> self
) then
2440 ctl
.focused
:= true;
2445 // ////////////////////////////////////////////////////////////////////////// //
2446 constructor TUIHBox
.Create ();
2451 procedure TUIHBox
.AfterConstruction ();
2458 // ////////////////////////////////////////////////////////////////////////// //
2459 constructor TUIVBox
.Create ();
2464 procedure TUIVBox
.AfterConstruction ();
2468 writeln('VBOX: ', canFocus
, ':', enabled
);
2472 // ////////////////////////////////////////////////////////////////////////// //
2473 procedure TUISpan
.AfterConstruction ();
2478 mCtl4Style
:= 'span';
2482 function TUISpan
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2484 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
2485 result
:= inherited parseProperty(prname
, par
);
2489 procedure TUISpan
.drawControl (gx
, gy
: Integer);
2494 // ////////////////////////////////////////////////////////////////////// //
2495 procedure TUILine
.AfterConstruction ();
2501 mCtl4Style
:= 'line';
2505 function TUILine
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2507 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
2508 result
:= inherited parseProperty(prname
, par
);
2512 procedure TUILine
.drawControl (gx
, gy
: Integer);
2516 cidx
:= getColorIndex
;
2519 drawHLine(gx
, gy
+(mHeight
div 2), mWidth
, mTextColor
[cidx
]);
2523 drawVLine(gx
+(mWidth
div 2), gy
, mHeight
, mTextColor
[cidx
]);
2528 // ////////////////////////////////////////////////////////////////////////// //
2529 procedure TUIHLine
.AfterConstruction ();
2537 // ////////////////////////////////////////////////////////////////////////// //
2538 procedure TUIVLine
.AfterConstruction ();
2546 // ////////////////////////////////////////////////////////////////////////// //
2547 procedure TUIStaticText
.AfterConstruction ();
2553 mHoriz
:= true; // nobody cares
2557 mCtl4Style
:= 'static';
2561 procedure TUIStaticText
.setText (const atext
: AnsiString);
2564 mDefSize
:= TLaySize
.Create(Length(mText
)*8, 8);
2568 function TUIStaticText
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2570 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) or (strEquCI1251(prname
, 'text')) then
2572 setText(par
.expectIdOrStr(true));
2576 if (strEquCI1251(prname
, 'textalign')) or (strEquCI1251(prname
, 'text-align')) then
2578 parseTextAlign(par
, mHAlign
, mVAlign
);
2582 if (strEquCI1251(prname
, 'header')) then
2588 if (strEquCI1251(prname
, 'line')) then
2594 result
:= inherited parseProperty(prname
, par
);
2598 procedure TUIStaticText
.drawControl (gx
, gy
: Integer);
2600 xpos
, ypos
: Integer;
2604 cidx
:= getColorIndex
;
2605 fillRect(gx
, gy
, mWidth
, mHeight
, mBackColor
[cidx
]);
2607 if (mHAlign
< 0) then xpos
:= 0
2608 else if (mHAlign
> 0) then xpos
:= mWidth
-Length(mText
)*8
2609 else xpos
:= (mWidth
-Length(mText
)*8) div 2;
2611 if (Length(mText
) > 0) then
2613 if (mHeader
) then clr
:= mFrameTextColor
[cidx
] else clr
:= mTextColor
[cidx
];
2615 if (mVAlign
< 0) then ypos
:= 0
2616 else if (mVAlign
> 0) then ypos
:= mHeight
-8
2617 else ypos
:= (mHeight
-8) div 2;
2619 drawText8(gx
+xpos
, gy
+ypos
, mText
, clr
);
2624 if (mHeader
) then clr
:= mFrameColor
[cidx
] else clr
:= mTextColor
[cidx
];
2626 if (mVAlign
< 0) then ypos
:= 0
2627 else if (mVAlign
> 0) then ypos
:= mHeight
-1
2628 else ypos
:= (mHeight
div 2);
2631 if (Length(mText
) = 0) then
2633 drawHLine(gx
, ypos
, mWidth
, clr
);
2637 drawHLine(gx
, ypos
, xpos
-1, clr
);
2638 drawHLine(gx
+xpos
+Length(mText
)*8, ypos
, mWidth
, clr
);
2644 // ////////////////////////////////////////////////////////////////////////// //
2645 procedure TUITextLabel
.AfterConstruction ();
2651 mDefSize
:= TLaySize
.Create(Length(mText
)*8, 8);
2652 mCtl4Style
:= 'label';
2657 procedure TUITextLabel
.cacheStyle (root
: TUIStyle
);
2659 inherited cacheStyle(root
);
2661 mHotColor
[ClrIdxActive
] := root
.get('hot-color', 'active', mCtl4Style
).asRGBADef(TGxRGBA
.Create(0, 128, 0));
2663 mHotColor
[ClrIdxDisabled
] := root
.get('hot-color', 'disabled', mCtl4Style
).asRGBADef(TGxRGBA
.Create(0, 64, 0));
2665 mHotColor
[ClrIdxInactive
] := root
.get('hot-color', 'inactive', mCtl4Style
).asRGBADef(TGxRGBA
.Create(0, 64, 0));
2669 procedure TUITextLabel
.setText (const s
: AnsiString);
2677 while (f
<= Length(s
)) do
2679 if (s
[f
] = '\\') then
2682 if (f
<= Length(s
)) then mText
+= s
[f
];
2685 else if (s
[f
] = '~') then
2688 if (f
<= Length(s
)) then
2690 if (mHotChar
= #0) then
2693 mHotOfs
:= Length(mText
)*8;
2705 mDefSize
:= TLaySize
.Create(Length(mText
)*8, 8);
2709 function TUITextLabel
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2711 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) or (strEquCI1251(prname
, 'text')) then
2713 setText(par
.expectIdOrStr(true));
2717 if (strEquCI1251(prname
, 'link')) then
2719 mLinkId
:= par
.expectIdOrStr(true);
2723 if (strEquCI1251(prname
, 'textalign')) or (strEquCI1251(prname
, 'text-align')) then
2725 parseTextAlign(par
, mHAlign
, mVAlign
);
2729 result
:= inherited parseProperty(prname
, par
);
2733 procedure TUITextLabel
.drawControl (gx
, gy
: Integer);
2735 xpos
, ypos
: Integer;
2738 cidx
:= getColorIndex
;
2739 fillRect(gx
, gy
, mWidth
, mHeight
, mBackColor
[cidx
]);
2740 if (Length(mText
) > 0) then
2742 if (mHAlign
< 0) then xpos
:= 0
2743 else if (mHAlign
> 0) then xpos
:= mWidth
-Length(mText
)*8
2744 else xpos
:= (mWidth
-Length(mText
)*8) div 2;
2746 if (mVAlign
< 0) then ypos
:= 0
2747 else if (mVAlign
> 0) then ypos
:= mHeight
-8
2748 else ypos
:= (mHeight
-8) div 2;
2750 drawText8(gx
+xpos
, gy
+ypos
, mText
, mTextColor
[cidx
]);
2752 if (Length(mLinkId
) > 0) and (mHotChar
<> #0) and (mHotChar
<> ' ') then
2754 drawText8(gx
+xpos
+8+mHotOfs
, gy
+ypos
, mHotChar
, mHotColor
[cidx
]);
2760 procedure TUITextLabel
.mouseEvent (var ev
: THMouseEvent
);
2764 inherited mouseEvent(ev
);
2765 if (not ev
.eaten
) and (not ev
.cancelled
) and (enabled
) and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
2772 procedure TUITextLabel
.doAction ();
2776 if (assigned(actionCB
)) then
2782 ctl
:= topLevel
[mLinkId
];
2783 if (ctl
<> nil) then
2785 if (ctl
.canFocus
) then ctl
.focused
:= true;
2791 procedure TUITextLabel
.keyEventPost (var ev
: THKeyEvent
);
2793 if (not enabled
) then exit
;
2794 if (mHotChar
= #0) then exit
;
2795 if (ev
.eaten
) or (ev
.cancelled
) or (not ev
.press
) then exit
;
2796 if (ev
.kstate
<> ev
.ModAlt
) then exit
;
2797 if (not ev
.isHot(mHotChar
)) then exit
;
2799 if (canFocus
) then focused
:= true;
2804 // ////////////////////////////////////////////////////////////////////////// //
2805 procedure TUIButton
.AfterConstruction ();
2811 mDefSize
:= TLaySize
.Create(Length(mText
)*8+8, 10);
2812 mCtl4Style
:= 'button';
2816 procedure TUIButton
.setText (const s
: AnsiString);
2818 inherited setText(s
);
2819 mDefSize
:= TLaySize
.Create(Length(mText
)*8+8*2, 10);
2823 procedure TUIButton
.drawControl (gx
, gy
: Integer);
2825 xpos
, ypos
: Integer;
2828 cidx
:= getColorIndex
;
2830 fillRect(gx
+1, gy
, mWidth
-2, mHeight
, mBackColor
[cidx
]);
2831 fillRect(gx
, gy
+1, 1, mHeight
-2, mBackColor
[cidx
]);
2832 fillRect(gx
+mWidth
-1, gy
+1, 1, mHeight
-2, mBackColor
[cidx
]);
2834 if (Length(mText
) > 0) then
2836 if (mHAlign
< 0) then xpos
:= 0
2837 else if (mHAlign
> 0) then xpos
:= mWidth
-Length(mText
)*8
2838 else xpos
:= (mWidth
-Length(mText
)*8) div 2;
2840 if (mVAlign
< 0) then ypos
:= 0
2841 else if (mVAlign
> 0) then ypos
:= mHeight
-8
2842 else ypos
:= (mHeight
-8) div 2;
2844 setScissor(8, 0, mWidth
-16, mHeight
);
2845 drawText8(gx
+xpos
+8, gy
+ypos
, mText
, mTextColor
[cidx
]);
2847 if (mHotChar
<> #0) and (mHotChar
<> ' ') then drawText8(gx
+xpos
+8+mHotOfs
, gy
+ypos
, mHotChar
, mHotColor
[cidx
]);
2852 procedure TUIButton
.mouseEvent (var ev
: THMouseEvent
);
2856 inherited mouseEvent(ev
);
2857 if (uiGrabCtl
= self
) then
2860 if (ev
= '-lmb') and focused
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
2866 if (ev
.eaten
) or (ev
.cancelled
) or (not enabled
) or not focused
then exit
;
2871 procedure TUIButton
.keyEvent (var ev
: THKeyEvent
);
2873 inherited keyEvent(ev
);
2874 if (not ev
.eaten
) and (not ev
.cancelled
) and (enabled
) then
2876 if (ev
= 'Enter') or (ev
= 'Space') then
2886 // ////////////////////////////////////////////////////////////////////////// //
2887 procedure TUISwitchBox
.AfterConstruction ();
2893 mDefSize
:= TLaySize
.Create(Length(mText
)*8+8*3, 8);
2894 mCtl4Style
:= 'switchbox';
2896 mBoolVar
:= @mChecked
;
2900 procedure TUISwitchBox
.cacheStyle (root
: TUIStyle
);
2902 inherited cacheStyle(root
);
2904 mSwitchColor
[ClrIdxActive
] := root
.get('switch-color', 'active', mCtl4Style
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
2906 mSwitchColor
[ClrIdxDisabled
] := root
.get('switch-color', 'disabled', mCtl4Style
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
2908 mSwitchColor
[ClrIdxInactive
] := root
.get('switch-color', 'inactive', mCtl4Style
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
2912 procedure TUISwitchBox
.setText (const s
: AnsiString);
2914 inherited setText(s
);
2915 mDefSize
:= TLaySize
.Create(Length(mText
)*8+8*3, 8);
2919 function TUISwitchBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2921 if (strEquCI1251(prname
, 'checked')) then
2927 result
:= inherited parseProperty(prname
, par
);
2931 function TUISwitchBox
.getChecked (): Boolean;
2933 if (mBoolVar
<> nil) then result
:= mBoolVar
^ else result
:= false;
2937 procedure TUISwitchBox
.setVar (pvar
: PBoolean);
2939 if (pvar
= nil) then pvar
:= @mChecked
;
2940 if (pvar
<> mBoolVar
) then
2943 setChecked(mBoolVar
^);
2948 procedure TUISwitchBox
.drawControl (gx
, gy
: Integer);
2950 xpos
, ypos
: Integer;
2953 cidx
:= getColorIndex
;
2955 if (mHAlign
< 0) then xpos
:= 0
2956 else if (mHAlign
> 0) then xpos
:= mWidth
-(Length(mText
)+4)*8
2957 else xpos
:= (mWidth
-(Length(mText
)+4)*8) div 2;
2959 if (mVAlign
< 0) then ypos
:= 0
2960 else if (mVAlign
> 0) then ypos
:= mHeight
-8
2961 else ypos
:= (mHeight
-8) div 2;
2964 fillRect(gx
, gy
, mWidth
, mHeight
, mBackColor
[cidx
]);
2968 if (Length(mCheckedStr
) <> 3) or (mCheckedStr
[2] <> '*') then
2970 drawText8(gx
+xpos
, gy
+ypos
, mCheckedStr
, mSwitchColor
[cidx
]);
2974 drawText8(gx
+xpos
, gy
+ypos
, mCheckedStr
[1], mSwitchColor
[cidx
]);
2975 drawText8(gx
+xpos
+2*8, gy
+ypos
, mCheckedStr
[3], mSwitchColor
[cidx
]);
2976 drawText8(gx
+xpos
+7, gy
+ypos
, '*', mSwitchColor
[cidx
]);
2981 drawText8(gx
+xpos
, gy
+ypos
, mUncheckedStr
, mSwitchColor
[cidx
]);
2984 drawText8(gx
+xpos
+8*3, gy
+ypos
, mText
, mTextColor
[cidx
]);
2986 if (mHotChar
<> #0) and (mHotChar
<> ' ') then drawText8(gx
+xpos
+8*3+mHotOfs
, gy
+ypos
, mHotChar
, mHotColor
[cidx
]);
2990 procedure TUISwitchBox
.mouseEvent (var ev
: THMouseEvent
);
2994 inherited mouseEvent(ev
);
2995 if (uiGrabCtl
= self
) then
2998 if (ev
= '-lmb') and focused
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
3004 if (ev
.eaten
) or (ev
.cancelled
) or (not enabled
) or not focused
then exit
;
3009 procedure TUISwitchBox
.keyEvent (var ev
: THKeyEvent
);
3011 inherited keyEvent(ev
);
3012 if (not ev
.eaten
) and (not ev
.cancelled
) and (enabled
) then
3014 if (ev
= 'Space') then
3024 // ////////////////////////////////////////////////////////////////////////// //
3025 procedure TUICheckBox
.AfterConstruction ();
3029 mBoolVar
:= @mChecked
;
3030 mCheckedStr
:= '[x]';
3031 mUncheckedStr
:= '[ ]';
3035 procedure TUICheckBox
.setChecked (v
: Boolean);
3041 procedure TUICheckBox
.doAction ();
3043 if (assigned(actionCB
)) then
3049 setChecked(not getChecked
);
3054 // ////////////////////////////////////////////////////////////////////////// //
3055 procedure TUIRadioBox
.AfterConstruction ();
3059 mBoolVar
:= @mChecked
;
3060 mCheckedStr
:= '(*)';
3061 mUncheckedStr
:= '( )';
3066 function TUIRadioBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
3068 if (strEquCI1251(prname
, 'group')) then
3070 mRadioGroup
:= par
.expectIdOrStr(true);
3071 if (getChecked
) then setChecked(true);
3075 if (strEquCI1251(prname
, 'checked')) then
3081 result
:= inherited parseProperty(prname
, par
);
3085 procedure TUIRadioBox
.setChecked (v
: Boolean);
3087 function resetGroup (ctl
: TUIControl
): Boolean;
3090 if (ctl
<> self
) and (ctl
is TUIRadioBox
) and (TUIRadioBox(ctl
).mRadioGroup
= mRadioGroup
) then
3092 TUIRadioBox(ctl
).mBoolVar
^ := false;
3098 if v
then topLevel
.forEachControl(resetGroup
);
3102 procedure TUIRadioBox
.doAction ();
3104 if (assigned(actionCB
)) then
3115 // ////////////////////////////////////////////////////////////////////////// //
3117 registerCtlClass(TUIHBox
, 'hbox');
3118 registerCtlClass(TUIVBox
, 'vbox');
3119 registerCtlClass(TUISpan
, 'span');
3120 registerCtlClass(TUIHLine
, 'hline');
3121 registerCtlClass(TUIVLine
, 'vline');
3122 registerCtlClass(TUITextLabel
, 'label');
3123 registerCtlClass(TUIStaticText
, 'static');
3124 registerCtlClass(TUIButton
, 'button');
3125 registerCtlClass(TUICheckBox
, 'checkbox');
3126 registerCtlClass(TUIRadioBox
, 'radiobox');