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
292 procedure activated (); override;
293 procedure blurred (); override;
296 closeCB
: TActionCB
; // called after window was removed from ui window list
299 constructor Create (const atitle
: AnsiString);
301 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
303 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
305 procedure centerInScreen ();
307 // `sx` and `sy` are screen coordinates
308 procedure drawControl (gx
, gy
: Integer); override;
309 procedure drawControlPost (gx
, gy
: Integer); override;
311 procedure keyEvent (var ev
: THKeyEvent
); override; // returns `true` if event was eaten
312 procedure mouseEvent (var ev
: THMouseEvent
); override; // returns `true` if event was eaten
315 property freeOnClose
: Boolean read mFreeOnClose write mFreeOnClose
;
318 // ////////////////////////////////////////////////////////////////////// //
319 TUIBox
= class(TUIControl
)
322 mCaption
: AnsiString;
323 mHAlign
: Integer; // -1: left; 0: center; 1: right; default: left
326 procedure setCaption (const acap
: AnsiString);
327 procedure setHasFrame (v
: Boolean);
330 constructor Create (ahoriz
: Boolean);
332 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
334 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
336 procedure drawControl (gx
, gy
: Integer); override;
338 procedure mouseEvent (var ev
: THMouseEvent
); override;
339 procedure keyEvent (var ev
: THKeyEvent
); override;
342 property caption
: AnsiString read mCaption write setCaption
;
343 property hasFrame
: Boolean read mHasFrame write setHasFrame
;
346 TUIHBox
= class(TUIBox
)
348 constructor Create ();
350 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
353 TUIVBox
= class(TUIBox
)
355 constructor Create ();
357 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
360 // ////////////////////////////////////////////////////////////////////// //
361 TUISpan
= class(TUIControl
)
363 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
365 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
367 procedure drawControl (gx
, gy
: Integer); override;
370 // ////////////////////////////////////////////////////////////////////// //
371 TUILine
= class(TUIControl
)
373 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
375 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
377 procedure drawControl (gx
, gy
: Integer); override;
380 TUIHLine
= class(TUILine
)
382 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
385 TUIVLine
= class(TUILine
)
387 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
390 // ////////////////////////////////////////////////////////////////////// //
391 TUIStaticText
= class(TUIControl
)
394 mHAlign
: Integer; // -1: left; 0: center; 1: right; default: left
395 mVAlign
: Integer; // -1: top; 0: center; 1: bottom; default: center
396 mHeader
: Boolean; // true: draw with frame text color
397 mLine
: Boolean; // true: draw horizontal line
400 procedure setText (const atext
: AnsiString);
403 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
405 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
407 procedure drawControl (gx
, gy
: Integer); override;
410 property text: AnsiString read mText write setText
;
411 property halign
: Integer read mHAlign write mHAlign
;
412 property valign
: Integer read mVAlign write mVAlign
;
413 property header
: Boolean read mHeader write mHeader
;
414 property line
: Boolean read mLine write mLine
;
417 // ////////////////////////////////////////////////////////////////////// //
418 TUITextLabel
= class(TUIControl
)
421 mHAlign
: Integer; // -1: left; 0: center; 1: right; default: left
422 mVAlign
: Integer; // -1: top; 0: center; 1: bottom; default: center
424 mHotOfs
: Integer; // from text start, in pixels
425 mHotColor
: array[0..ClrIdxMax
] of TGxRGBA
;
426 mLinkId
: AnsiString; // linked control
429 procedure cacheStyle (root
: TUIStyle
); override;
431 procedure setText (const s
: AnsiString); virtual;
434 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
436 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
438 procedure drawControl (gx
, gy
: Integer); override;
440 procedure mouseEvent (var ev
: THMouseEvent
); override;
441 procedure keyEventPost (var ev
: THKeyEvent
); override;
444 property text: AnsiString read mText write setText
;
445 property halign
: Integer read mHAlign write mHAlign
;
446 property valign
: Integer read mVAlign write mVAlign
;
449 // ////////////////////////////////////////////////////////////////////// //
450 TUIButton
= class(TUITextLabel
)
452 procedure setText (const s
: AnsiString); override;
455 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
457 procedure drawControl (gx
, gy
: Integer); override;
459 procedure mouseEvent (var ev
: THMouseEvent
); override;
460 procedure keyEvent (var ev
: THKeyEvent
); override;
461 procedure keyEventPost (var ev
: THKeyEvent
); override;
464 // ////////////////////////////////////////////////////////////////////// //
465 TUISwitchBox
= class(TUITextLabel
)
469 mCheckedStr
: AnsiString;
470 mUncheckedStr
: AnsiString;
471 mSwitchColor
: array[0..ClrIdxMax
] of TGxRGBA
;
474 procedure cacheStyle (root
: TUIStyle
); override;
476 procedure setText (const s
: AnsiString); override;
478 function getChecked (): Boolean; virtual;
479 procedure setChecked (v
: Boolean); virtual; abstract;
482 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
484 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
486 procedure drawControl (gx
, gy
: Integer); override;
488 procedure mouseEvent (var ev
: THMouseEvent
); override;
489 procedure keyEvent (var ev
: THKeyEvent
); override;
490 procedure keyEventPost (var ev
: THKeyEvent
); override;
492 procedure setVar (pvar
: PBoolean);
495 property checked
: Boolean read getChecked write setChecked
;
498 TUICheckBox
= class(TUISwitchBox
)
500 procedure setChecked (v
: Boolean); override;
503 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
505 procedure doAction (); override;
508 TUIRadioBox
= class(TUISwitchBox
)
510 mRadioGroup
: AnsiString;
513 procedure setChecked (v
: Boolean); override;
516 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
518 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
520 procedure doAction (); override;
523 property radioGroup
: AnsiString read mRadioGroup write mRadioGroup
; //FIXME
527 // ////////////////////////////////////////////////////////////////////////// //
528 procedure uiMouseEvent (var evt
: THMouseEvent
);
529 procedure uiKeyEvent (var evt
: THKeyEvent
);
533 // ////////////////////////////////////////////////////////////////////////// //
534 procedure uiAddWindow (ctl
: TUIControl
);
535 procedure uiRemoveWindow (ctl
: TUIControl
); // will free window if `mFreeOnClose` is `true`
536 function uiVisibleWindow (ctl
: TUIControl
): Boolean;
538 procedure uiUpdateStyles ();
541 // ////////////////////////////////////////////////////////////////////////// //
543 procedure uiLayoutCtl (ctl
: TUIControl
);
546 // ////////////////////////////////////////////////////////////////////////// //
548 gh_ui_scale
: Single = 1.0;
558 // ////////////////////////////////////////////////////////////////////////// //
560 ctlsToKill
: array of TUIControl
= nil;
563 procedure scheduleKill (ctl
: TUIControl
);
567 if (ctl
= nil) then exit
;
569 for f
:= 0 to High(ctlsToKill
) do
571 if (ctlsToKill
[f
] = ctl
) then exit
;
572 if (ctlsToKill
[f
] = nil) then begin ctlsToKill
[f
] := ctl
; exit
; end;
574 SetLength(ctlsToKill
, Length(ctlsToKill
)+1);
575 ctlsToKill
[High(ctlsToKill
)] := ctl
;
579 procedure processKills ();
584 for f
:= 0 to High(ctlsToKill
) do
586 ctl
:= ctlsToKill
[f
];
587 if (ctl
= nil) then break
;
588 ctlsToKill
[f
] := nil;
591 if (Length(ctlsToKill
) > 0) then ctlsToKill
[0] := nil; // just in case
595 // ////////////////////////////////////////////////////////////////////////// //
597 knownCtlClasses
: array of record
598 klass
: TUIControlClass
;
603 procedure registerCtlClass (aklass
: TUIControlClass
; const aname
: AnsiString);
605 assert(aklass
<> nil);
606 assert(Length(aname
) > 0);
607 SetLength(knownCtlClasses
, Length(knownCtlClasses
)+1);
608 knownCtlClasses
[High(knownCtlClasses
)].klass
:= aklass
;
609 knownCtlClasses
[High(knownCtlClasses
)].name
:= aname
;
613 function findCtlClass (const aname
: AnsiString): TUIControlClass
;
617 for f
:= 0 to High(knownCtlClasses
) do
619 if (strEquCI1251(aname
, knownCtlClasses
[f
].name
)) then
621 result
:= knownCtlClasses
[f
].klass
;
629 // ////////////////////////////////////////////////////////////////////////// //
631 TFlexLayouter
= specialize TFlexLayouterBase
<TUIControl
>;
633 procedure uiLayoutCtl (ctl
: TUIControl
);
637 if (ctl
= nil) then exit
;
638 lay
:= TFlexLayouter
.Create();
643 //writeln('============================'); lay.dumpFlat();
645 //writeln('=== initial ==='); lay.dump();
647 //lay.calcMaxSizeInternal(0);
650 writeln('=== after first pass ===');
654 writeln('=== after second pass ===');
659 //writeln('=== final ==='); lay.dump();
661 if (ctl
.mParent
= nil) and (ctl
is TUITopWindow
) and (TUITopWindow(ctl
).mDoCenter
) then
663 TUITopWindow(ctl
).centerInScreen();
666 // calculate full size
667 ctl
.calcFullClientSize();
670 if (ctl
.mParent
= nil) then
672 if (ctl
.mFocused
= nil) or (ctl
.mFocused
= ctl
) or (not ctl
.mFocused
.enabled
) then
674 ctl
.mFocused
:= ctl
.findFirstFocus();
684 // ////////////////////////////////////////////////////////////////////////// //
686 uiTopList
: array of TUIControl
= nil;
687 uiGrabCtl
: TUIControl
= nil;
690 procedure uiUpdateStyles ();
694 for ctl
in uiTopList
do ctl
.updateStyle();
698 procedure uiMouseEvent (var evt
: THMouseEvent
);
706 if (evt
.eaten
) or (evt
.cancelled
) then exit
;
708 ev
.x
:= trunc(ev
.x
/gh_ui_scale
);
709 ev
.y
:= trunc(ev
.y
/gh_ui_scale
);
710 ev
.dx
:= trunc(ev
.dx
/gh_ui_scale
); //FIXME
711 ev
.dy
:= trunc(ev
.dy
/gh_ui_scale
); //FIXME
713 if (uiGrabCtl
<> nil) then
715 uiGrabCtl
.mouseEvent(ev
);
716 if (ev
.release
) and ((ev
.bstate
and (not ev
.but
)) = 0) then uiGrabCtl
:= nil;
720 if (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)].enabled
) then uiTopList
[High(uiTopList
)].mouseEvent(ev
);
721 if (not ev
.eaten
) and (not ev
.cancelled
) and (ev
.press
) then
723 for f
:= High(uiTopList
) downto 0 do
725 if uiTopList
[f
].toLocal(ev
.x
, ev
.y
, lx
, ly
) then
727 if (uiTopList
[f
].enabled
) and (f
<> High(uiTopList
)) then
729 uiTopList
[High(uiTopList
)].blurred();
730 ctmp
:= uiTopList
[f
];
732 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
733 uiTopList
[High(uiTopList
)] := ctmp
;
743 if (ev
.eaten
) then evt
.eat();
744 if (ev
.cancelled
) then evt
.cancel();
749 procedure uiKeyEvent (var evt
: THKeyEvent
);
754 if (evt
.eaten
) or (evt
.cancelled
) then exit
;
756 ev
.x
:= trunc(ev
.x
/gh_ui_scale
);
757 ev
.y
:= trunc(ev
.y
/gh_ui_scale
);
759 if (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)].enabled
) then uiTopList
[High(uiTopList
)].keyEvent(ev
);
760 //if (ev.release) then begin ev.eat(); exit; end;
762 if (ev
.eaten
) then evt
.eat();
763 if (ev
.cancelled
) then evt
.cancel();
774 gxBeginUIDraw(gh_ui_scale
);
776 for f
:= 0 to High(uiTopList
) do
780 if (f
<> High(uiTopList
)) then
782 cidx
:= ctl
.getColorIndex
;
783 if (ctl
.mDarken
[cidx
] > 0) then darkenRect(ctl
.x0
, ctl
.y0
, ctl
.width
, ctl
.height
, ctl
.mDarken
[cidx
]);
792 procedure uiAddWindow (ctl
: TUIControl
);
796 if (ctl
= nil) then exit
;
798 if not (ctl
is TUITopWindow
) then exit
; // alas
799 for f
:= 0 to High(uiTopList
) do
801 if (uiTopList
[f
] = ctl
) then
803 if (f
<> High(uiTopList
)) then
805 uiTopList
[High(uiTopList
)].blurred();
806 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
807 uiTopList
[High(uiTopList
)] := ctl
;
813 if (Length(uiTopList
) > 0) then uiTopList
[High(uiTopList
)].blurred();
814 SetLength(uiTopList
, Length(uiTopList
)+1);
815 uiTopList
[High(uiTopList
)] := ctl
;
821 procedure uiRemoveWindow (ctl
: TUIControl
);
825 if (ctl
= nil) then exit
;
827 if not (ctl
is TUITopWindow
) then exit
; // alas
828 for f
:= 0 to High(uiTopList
) do
830 if (uiTopList
[f
] = ctl
) then
833 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
834 SetLength(uiTopList
, Length(uiTopList
)-1);
835 if (ctl
is TUITopWindow
) then
838 if assigned(TUITopWindow(ctl
).closeCB
) then TUITopWindow(ctl
).closeCB(ctl
);
840 if (TUITopWindow(ctl
).mFreeOnClose
) then scheduleKill(ctl
);
849 function uiVisibleWindow (ctl
: TUIControl
): Boolean;
854 if (ctl
= nil) then exit
;
856 if not (ctl
is TUITopWindow
) then exit
; // alas
857 for f
:= 0 to High(uiTopList
) do
859 if (uiTopList
[f
] = ctl
) then begin result
:= true; exit
; end;
864 // ////////////////////////////////////////////////////////////////////////// //
865 constructor TUIControl
.Create ();
870 procedure TUIControl
.AfterConstruction ();
887 mDrawShadow
:= false;
889 // layouter interface
890 //mDefSize := TLaySize.Create(64, 8); // default size
891 mDefSize
:= TLaySize
.Create(0, 0); // default size
892 mMaxSize
:= TLaySize
.Create(-1, -1); // maximum size
901 mAlign
:= -1; // left/top
906 destructor TUIControl
.Destroy ();
910 if (mParent
<> nil) then
913 for f
:= 0 to High(mParent
.mChildren
) do
915 if (mParent
.mChildren
[f
] = self
) then
917 for c
:= f
+1 to High(mParent
.mChildren
) do mParent
.mChildren
[c
-1] := mParent
.mChildren
[c
];
918 SetLength(mParent
.mChildren
, Length(mParent
.mChildren
)-1);
922 for f
:= 0 to High(mChildren
) do
924 mChildren
[f
].mParent
:= nil;
931 function TUIControl
.getColorIndex (): Integer; inline;
933 if (not enabled
) then begin result
:= ClrIdxDisabled
; exit
; end;
934 // top windows: no focus hack
935 if (self
is TUITopWindow
) then
937 if (getActive
) then begin result
:= ClrIdxActive
; exit
; end;
941 // if control cannot be focused, take "active" color scheme for it (it is easier this way)
942 if (not canFocus
) or (getActive
) then begin result
:= ClrIdxActive
; exit
; end;
944 result
:= ClrIdxInactive
;
947 procedure TUIControl
.updateStyle ();
953 while (ctl
<> nil) do
955 if (Length(ctl
.mStyleId
) <> 0) then begin stl
:= uiFindStyle(ctl
.mStyleId
); break
; end;
958 if (stl
= nil) then stl
:= uiFindStyle(''); // default
960 for ctl
in mChildren
do ctl
.updateStyle();
963 procedure TUIControl
.cacheStyle (root
: TUIStyle
);
967 //writeln('caching style for <', className, '> (', mCtl4Style, ')...');
970 mBackColor
[ClrIdxActive
] := root
.get('back-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(0, 0, 128));
971 mTextColor
[ClrIdxActive
] := root
.get('text-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
972 mFrameColor
[ClrIdxActive
] := root
.get('frame-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
973 mFrameTextColor
[ClrIdxActive
] := root
.get('frame-text-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
974 mFrameIconColor
[ClrIdxActive
] := root
.get('frame-icon-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(0, 255, 0));
975 mDarken
[ClrIdxActive
] := root
.get('darken', 'active', cst
).asInt(-1);
977 mBackColor
[ClrIdxDisabled
] := root
.get('back-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(0, 0, 128));
978 mTextColor
[ClrIdxDisabled
] := root
.get('text-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(127, 127, 127));
979 mFrameColor
[ClrIdxDisabled
] := root
.get('frame-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(127, 127, 127));
980 mFrameTextColor
[ClrIdxDisabled
] := root
.get('frame-text-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(127, 127, 127));
981 mFrameIconColor
[ClrIdxDisabled
] := root
.get('frame-icon-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(0, 127, 0));
982 mDarken
[ClrIdxDisabled
] := root
.get('darken', 'disabled', cst
).asInt(-1);
984 mBackColor
[ClrIdxInactive
] := root
.get('back-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(0, 0, 128));
985 mTextColor
[ClrIdxInactive
] := root
.get('text-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
986 mFrameColor
[ClrIdxInactive
] := root
.get('frame-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
987 mFrameTextColor
[ClrIdxInactive
] := root
.get('frame-text-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
988 mFrameIconColor
[ClrIdxInactive
] := root
.get('frame-icon-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(0, 255, 0));
989 mDarken
[ClrIdxInactive
] := root
.get('darken', 'inactive', cst
).asInt(-1);
993 // ////////////////////////////////////////////////////////////////////////// //
994 function TUIControl
.getDefSize (): TLaySize
; inline; begin result
:= mLayDefSize
; end;
995 function TUIControl
.getMaxSize (): TLaySize
; inline; begin result
:= mLayMaxSize
; end;
996 function TUIControl
.getFlex (): Integer; inline; begin result
:= mFlex
; end;
997 function TUIControl
.isHorizBox (): Boolean; inline; begin result
:= mHoriz
; end;
998 procedure TUIControl
.setHorizBox (v
: Boolean); inline; begin mHoriz
:= v
; end;
999 function TUIControl
.canWrap (): Boolean; inline; begin result
:= mCanWrap
; end;
1000 procedure TUIControl
.setCanWrap (v
: Boolean); inline; begin mCanWrap
:= v
; end;
1001 function TUIControl
.isLineStart (): Boolean; inline; begin result
:= mLineStart
; end;
1002 procedure TUIControl
.setLineStart (v
: Boolean); inline; begin mLineStart
:= v
; end;
1003 function TUIControl
.getAlign (): Integer; inline; begin result
:= mAlign
; end;
1004 procedure TUIControl
.setAlign (v
: Integer); inline; begin mAlign
:= v
; end;
1005 function TUIControl
.getExpand (): Boolean; inline; begin result
:= mExpand
; end;
1006 procedure TUIControl
.setExpand (v
: Boolean); inline; begin mExpand
:= v
; end;
1007 function TUIControl
.getHGroup (): AnsiString; inline; begin result
:= mHGroup
; end;
1008 procedure TUIControl
.setHGroup (const v
: AnsiString); inline; begin mHGroup
:= v
; end;
1009 function TUIControl
.getVGroup (): AnsiString; inline; begin result
:= mVGroup
; end;
1010 procedure TUIControl
.setVGroup (const v
: AnsiString); inline; begin mVGroup
:= v
; end;
1012 function TUIControl
.getMargins (): TLayMargins
; inline;
1014 result
:= TLayMargins
.Create(mFrameHeight
, mFrameWidth
, mFrameHeight
, mFrameWidth
);
1017 procedure TUIControl
.setActualSizePos (constref apos
: TLayPos
; constref asize
: TLaySize
); inline;
1019 //writeln(self.className, '; pos=', apos.toString, '; size=', asize.toString);
1020 if (mParent
<> nil) then
1029 procedure TUIControl
.layPrepare ();
1031 mLayDefSize
:= mDefSize
;
1032 mLayMaxSize
:= mMaxSize
;
1033 if (mLayMaxSize
.w
>= 0) then mLayMaxSize
.w
+= mFrameWidth
*2;
1034 if (mLayMaxSize
.h
>= 0) then mLayMaxSize
.h
+= mFrameHeight
*2;
1038 // ////////////////////////////////////////////////////////////////////////// //
1039 function TUIControl
.parsePos (par
: TTextParser
): TLayPos
;
1041 ech
: AnsiChar = ')';
1043 if (par
.eatDelim('[')) then ech
:= ']' else par
.expectDelim('(');
1044 result
.x
:= par
.expectInt();
1045 par
.eatDelim(','); // optional comma
1046 result
.y
:= par
.expectInt();
1047 par
.eatDelim(','); // optional comma
1048 par
.expectDelim(ech
);
1051 function TUIControl
.parseSize (par
: TTextParser
): TLaySize
;
1053 ech
: AnsiChar = ')';
1055 if (par
.eatDelim('[')) then ech
:= ']' else par
.expectDelim('(');
1056 result
.w
:= par
.expectInt();
1057 par
.eatDelim(','); // optional comma
1058 result
.h
:= par
.expectInt();
1059 par
.eatDelim(','); // optional comma
1060 par
.expectDelim(ech
);
1063 function TUIControl
.parseBool (par
: TTextParser
): Boolean;
1066 par
.eatIdOrStrCI('true') or
1067 par
.eatIdOrStrCI('yes') or
1068 par
.eatIdOrStrCI('tan');
1071 if (not par
.eatIdOrStrCI('false')) and (not par
.eatIdOrStrCI('no')) and (not par
.eatIdOrStrCI('ona')) then
1073 par
.error('boolean value expected');
1078 function TUIControl
.parseAnyAlign (par
: TTextParser
): Integer;
1080 if (par
.eatIdOrStrCI('left')) or (par
.eatIdOrStrCI('top')) then result
:= -1
1081 else if (par
.eatIdOrStrCI('right')) or (par
.eatIdOrStrCI('bottom')) then result
:= 1
1082 else if (par
.eatIdOrStrCI('center')) then result
:= 0
1083 else par
.error('invalid align value');
1086 function TUIControl
.parseHAlign (par
: TTextParser
): Integer;
1088 if (par
.eatIdOrStrCI('left')) then result
:= -1
1089 else if (par
.eatIdOrStrCI('right')) then result
:= 1
1090 else if (par
.eatIdOrStrCI('center')) then result
:= 0
1091 else par
.error('invalid horizontal align value');
1094 function TUIControl
.parseVAlign (par
: TTextParser
): Integer;
1096 if (par
.eatIdOrStrCI('top')) then result
:= -1
1097 else if (par
.eatIdOrStrCI('bottom')) then result
:= 1
1098 else if (par
.eatIdOrStrCI('center')) then result
:= 0
1099 else par
.error('invalid vertical align value');
1102 procedure TUIControl
.parseTextAlign (par
: TTextParser
; var h
, v
: Integer);
1104 wasH
: Boolean = false;
1105 wasV
: Boolean = false;
1109 if (par
.eatIdOrStrCI('left')) then
1111 if wasH
then par
.error('too many align directives');
1116 if (par
.eatIdOrStrCI('right')) then
1118 if wasH
then par
.error('too many align directives');
1123 if (par
.eatIdOrStrCI('hcenter')) then
1125 if wasH
then par
.error('too many align directives');
1130 if (par
.eatIdOrStrCI('top')) then
1132 if wasV
then par
.error('too many align directives');
1137 if (par
.eatIdOrStrCI('bottom')) then
1139 if wasV
then par
.error('too many align directives');
1144 if (par
.eatIdOrStrCI('vcenter')) then
1146 if wasV
then par
.error('too many align directives');
1151 if (par
.eatIdOrStrCI('center')) then
1153 if wasV
or wasH
then par
.error('too many align directives');
1162 if not wasV
and not wasH
then par
.error('invalid align value');
1165 function TUIControl
.parseOrientation (const prname
: AnsiString; par
: TTextParser
): Boolean;
1167 if (strEquCI1251(prname
, 'orientation')) or (strEquCI1251(prname
, 'orient')) then
1169 if (par
.eatIdOrStrCI('horizontal')) or (par
.eatIdOrStrCI('horiz')) then mHoriz
:= true
1170 else if (par
.eatIdOrStrCI('vertical')) or (par
.eatIdOrStrCI('vert')) then mHoriz
:= false
1171 else par
.error('`horizontal` or `vertical` expected');
1180 // par should be on '{'; final '}' is eaten
1181 procedure TUIControl
.parseProperties (par
: TTextParser
);
1185 if (not par
.eatDelim('{')) then exit
;
1186 while (not par
.eatDelim('}')) do
1188 if (not par
.isIdOrStr
) then par
.error('property name expected');
1191 par
.eatDelim(':'); // optional
1192 if not parseProperty(pn
, par
) then par
.errorfmt('invalid property name ''%s''', [pn
]);
1193 par
.eatDelim(','); // optional
1197 // par should be on '{'
1198 procedure TUIControl
.parseChildren (par
: TTextParser
);
1200 cc
: TUIControlClass
;
1203 par
.expectDelim('{');
1204 while (not par
.eatDelim('}')) do
1206 if (not par
.isIdOrStr
) then par
.error('control name expected');
1207 cc
:= findCtlClass(par
.tokStr
);
1208 if (cc
= nil) then par
.errorfmt('unknown control name: ''%s''', [par
.tokStr
]);
1209 //writeln('children for <', par.tokStr, '>: <', cc.className, '>');
1211 par
.eatDelim(':'); // optional
1213 //writeln(' mHoriz=', ctl.mHoriz);
1215 ctl
.parseProperties(par
);
1220 //writeln(': ', ctl.mDefSize.toString);
1222 par
.eatDelim(','); // optional
1227 function TUIControl
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1230 if (strEquCI1251(prname
, 'id')) then begin mId
:= par
.expectIdOrStr(true); exit
; end; // allow empty strings
1231 if (strEquCI1251(prname
, 'style')) then begin mStyleId
:= par
.expectIdOrStr(); exit
; end; // no empty strings
1232 if (strEquCI1251(prname
, 'flex')) then begin flex
:= par
.expectInt(); exit
; end;
1234 if (strEquCI1251(prname
, 'defsize')) or (strEquCI1251(prname
, 'size')) then begin mDefSize
:= parseSize(par
); exit
; end;
1235 if (strEquCI1251(prname
, 'maxsize')) then begin mMaxSize
:= parseSize(par
); exit
; end;
1236 if (strEquCI1251(prname
, 'defwidth')) or (strEquCI1251(prname
, 'width')) then begin mDefSize
.w
:= par
.expectInt(); exit
; end;
1237 if (strEquCI1251(prname
, 'defheight')) or (strEquCI1251(prname
, 'height')) then begin mDefSize
.h
:= par
.expectInt(); exit
; end;
1238 if (strEquCI1251(prname
, 'maxwidth')) then begin mMaxSize
.w
:= par
.expectInt(); exit
; end;
1239 if (strEquCI1251(prname
, 'maxheight')) then begin mMaxSize
.h
:= par
.expectInt(); exit
; end;
1241 if (strEquCI1251(prname
, 'wrap')) then begin mCanWrap
:= parseBool(par
); exit
; end;
1242 if (strEquCI1251(prname
, 'linestart')) then begin mLineStart
:= parseBool(par
); exit
; end;
1243 if (strEquCI1251(prname
, 'expand')) then begin mExpand
:= parseBool(par
); exit
; end;
1245 if (strEquCI1251(prname
, 'align')) then begin mAlign
:= parseAnyAlign(par
); exit
; end;
1246 if (strEquCI1251(prname
, 'hgroup')) then begin mHGroup
:= par
.expectIdOrStr(true); exit
; end; // allow empty strings
1247 if (strEquCI1251(prname
, 'vgroup')) then begin mVGroup
:= par
.expectIdOrStr(true); exit
; end; // allow empty strings
1249 if (strEquCI1251(prname
, 'canfocus')) then begin mCanFocus
:= true; exit
; end;
1250 if (strEquCI1251(prname
, 'nofocus')) then begin mCanFocus
:= false; exit
; end;
1251 if (strEquCI1251(prname
, 'disabled')) then begin mEnabled
:= false; exit
; end;
1252 if (strEquCI1251(prname
, 'enabled')) then begin mEnabled
:= true; exit
; end;
1253 if (strEquCI1251(prname
, 'escclose')) then begin mEscClose
:= not parseBool(par
); exit
; end;
1254 if (strEquCI1251(prname
, 'default')) then begin mDefault
:= true; exit
; end;
1255 if (strEquCI1251(prname
, 'cancel')) then begin mCancel
:= true; exit
; end;
1260 // ////////////////////////////////////////////////////////////////////////// //
1261 procedure TUIControl
.activated ();
1263 makeVisibleInParent();
1267 procedure TUIControl
.blurred ();
1269 if (uiGrabCtl
= self
) then uiGrabCtl
:= nil;
1273 procedure TUIControl
.calcFullClientSize ();
1277 mFullSize
:= TLaySize
.Create(0, 0);
1278 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1279 for ctl
in mChildren
do
1281 ctl
.calcFullClientSize();
1282 mFullSize
.w
:= nmax(mFullSize
.w
, ctl
.mX
-mFrameWidth
+ctl
.mFullSize
.w
);
1283 mFullSize
.h
:= nmax(mFullSize
.h
, ctl
.mY
-mFrameHeight
+ctl
.mFullSize
.h
);
1285 mFullSize
.w
:= nmax(mFullSize
.w
, mWidth
-mFrameWidth
*2);
1286 mFullSize
.h
:= nmax(mFullSize
.h
, mHeight
-mFrameHeight
*2);
1290 function TUIControl
.topLevel (): TUIControl
; inline;
1293 while (result
.mParent
<> nil) do result
:= result
.mParent
;
1297 function TUIControl
.getEnabled (): Boolean;
1302 if (not mEnabled
) then exit
;
1304 while (ctl
<> nil) do
1306 if (not ctl
.mEnabled
) then exit
;
1313 procedure TUIControl
.setEnabled (v
: Boolean); inline;
1315 if (mEnabled
= v
) then exit
;
1317 if (not v
) and focused
then setFocused(false);
1321 function TUIControl
.getFocused (): Boolean; inline;
1323 if (mParent
= nil) then
1325 result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = self
);
1329 result
:= (topLevel
.mFocused
= self
);
1330 if (result
) then result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = topLevel
);
1335 function TUIControl
.getActive (): Boolean; inline;
1339 if (mParent
= nil) then
1341 result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = self
);
1345 ctl
:= topLevel
.mFocused
;
1346 while (ctl
<> nil) and (ctl
<> self
) do ctl
:= ctl
.mParent
;
1347 result
:= (ctl
= self
);
1348 if (result
) then result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = topLevel
);
1353 procedure TUIControl
.setFocused (v
: Boolean); inline;
1360 if (tl
.mFocused
= self
) then
1362 blurred(); // this will reset grab, but still...
1363 if (uiGrabCtl
= self
) then uiGrabCtl
:= nil;
1364 tl
.mFocused
:= tl
.findNextFocus(self
, true);
1365 if (tl
.mFocused
= self
) then tl
.mFocused
:= nil;
1366 if (tl
.mFocused
<> nil) then tl
.mFocused
.activated();
1370 if (not canFocus
) then exit
;
1371 if (tl
.mFocused
<> self
) then
1373 if (tl
.mFocused
<> nil) then tl
.mFocused
.blurred();
1374 tl
.mFocused
:= self
;
1375 if (uiGrabCtl
<> self
) then uiGrabCtl
:= nil;
1381 function TUIControl
.getCanFocus (): Boolean; inline;
1383 result
:= (getEnabled
) and (mCanFocus
) and (mWidth
> 0) and (mHeight
> 0);
1387 function TUIControl
.isMyChild (ctl
: TUIControl
): Boolean;
1390 while (ctl
<> nil) do
1392 if (ctl
.mParent
= self
) then exit
;
1399 // returns `true` if global coords are inside this control
1400 function TUIControl
.toLocal (var x
, y
: Integer): Boolean;
1402 if (mParent
= nil) then
1406 result
:= true; // hack
1410 result
:= mParent
.toLocal(x
, y
);
1411 Inc(x
, mParent
.mScrollX
);
1412 Inc(y
, mParent
.mScrollY
);
1415 if result
then result
:= (x
>= 0) and (y
>= 0) and (x
< mParent
.mWidth
) and (y
< mParent
.mHeight
);
1417 if result
then result
:= (x
>= 0) and (y
>= 0) and (x
< mWidth
) and (y
< mHeight
);
1420 function TUIControl
.toLocal (gx
, gy
: Integer; out x
, y
: Integer): Boolean; inline;
1424 result
:= toLocal(x
, y
);
1428 procedure TUIControl
.toGlobal (var x
, y
: Integer);
1432 if (mParent
<> nil) then
1434 Dec(x
, mParent
.mScrollX
);
1435 Dec(y
, mParent
.mScrollY
);
1436 mParent
.toGlobal(x
, y
);
1440 procedure TUIControl
.toGlobal (lx
, ly
: Integer; out x
, y
: Integer); inline;
1447 procedure TUIControl
.getDrawRect (out gx
, gy
, wdt
, hgt
: Integer);
1451 if (mParent
= nil) then
1460 toGlobal(0, 0, cgx
, cgy
);
1461 mParent
.getDrawRect(gx
, gy
, wdt
, hgt
);
1462 if (wdt
> 0) and (hgt
> 0) then
1464 if not intersectRect(gx
, gy
, wdt
, hgt
, cgx
, cgy
, mWidth
, mHeight
) then
1474 // x and y are global coords
1475 function TUIControl
.controlAtXY (x
, y
: Integer; allowDisabled
: Boolean=false): TUIControl
;
1481 if (not allowDisabled
) and (not enabled
) then exit
;
1482 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1483 if not toLocal(x
, y
, lx
, ly
) then exit
;
1484 for f
:= High(mChildren
) downto 0 do
1486 result
:= mChildren
[f
].controlAtXY(x
, y
, allowDisabled
);
1487 if (result
<> nil) then exit
;
1493 function TUIControl
.parentScrollX (): Integer; inline; begin if (mParent
<> nil) then result
:= mParent
.mScrollX
else result
:= 0; end;
1494 function TUIControl
.parentScrollY (): Integer; inline; begin if (mParent
<> nil) then result
:= mParent
.mScrollY
else result
:= 0; end;
1497 procedure TUIControl
.makeVisibleInParent ();
1499 sy
, ey
, cy
: Integer;
1502 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1504 if (p
= nil) then exit
;
1505 if (p
.mFullSize
.w
< 1) or (p
.mFullSize
.h
< 1) then
1511 p
.makeVisibleInParent();
1512 cy
:= mY
-p
.mFrameHeight
;
1514 ey
:= sy
+(p
.mHeight
-p
.mFrameHeight
*2);
1517 p
.mScrollY
:= nmax(0, cy
);
1519 else if (cy
+mHeight
> ey
) then
1521 p
.mScrollY
:= nmax(0, cy
+mHeight
-(p
.mHeight
-p
.mFrameHeight
*2));
1526 // ////////////////////////////////////////////////////////////////////////// //
1527 function TUIControl
.prevSibling (): TUIControl
;
1531 if (mParent
<> nil) then
1533 for f
:= 1 to High(mParent
.mChildren
) do
1535 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
-1]; exit
; end;
1541 function TUIControl
.nextSibling (): TUIControl
;
1545 if (mParent
<> nil) then
1547 for f
:= 0 to High(mParent
.mChildren
)-1 do
1549 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
+1]; exit
; end;
1555 function TUIControl
.firstChild (): TUIControl
; inline;
1557 if (Length(mChildren
) <> 0) then result
:= mChildren
[0] else result
:= nil;
1560 function TUIControl
.lastChild (): TUIControl
; inline;
1562 if (Length(mChildren
) <> 0) then result
:= mChildren
[High(mChildren
)] else result
:= nil;
1566 function TUIControl
.findFirstFocus (): TUIControl
;
1573 for f
:= 0 to High(mChildren
) do
1575 result
:= mChildren
[f
].findFirstFocus();
1576 if (result
<> nil) then exit
;
1578 if (canFocus
) then result
:= self
;
1583 function TUIControl
.findLastFocus (): TUIControl
;
1590 for f
:= High(mChildren
) downto 0 do
1592 result
:= mChildren
[f
].findLastFocus();
1593 if (result
<> nil) then exit
;
1595 if (canFocus
) then result
:= self
;
1600 function TUIControl
.findNextFocus (cur
: TUIControl
; wrap
: Boolean): TUIControl
;
1602 curHit
: Boolean = false;
1604 function checkFocus (ctl
: TUIControl
): Boolean;
1608 result
:= (ctl
.canFocus
);
1612 curHit
:= (ctl
= cur
);
1613 result
:= false; // don't stop
1621 if not isMyChild(cur
) then
1623 result
:= findFirstFocus();
1627 result
:= forEachControl(checkFocus
);
1628 if (result
= nil) and (wrap
) then result
:= findFirstFocus();
1634 function TUIControl
.findPrevFocus (cur
: TUIControl
; wrap
: Boolean): TUIControl
;
1636 lastCtl
: TUIControl
= nil;
1638 function checkFocus (ctl
: TUIControl
): Boolean;
1647 if (ctl
.canFocus
) then lastCtl
:= ctl
;
1655 if not isMyChild(cur
) then
1657 result
:= findLastFocus();
1661 forEachControl(checkFocus
);
1662 if (lastCtl
= nil) and (wrap
) then lastCtl
:= findLastFocus();
1664 //if (lastCtl <> nil) then writeln('ctl<', lastCtl.className, '>: {', lastCtl.id, '}');
1670 function TUIControl
.findDefaulControl (): TUIControl
;
1676 if (mDefault
) then begin result
:= self
; exit
; end;
1677 for ctl
in mChildren
do
1679 result
:= ctl
.findDefaulControl();
1680 if (result
<> nil) then exit
;
1686 function TUIControl
.findCancelControl (): TUIControl
;
1692 if (mCancel
) then begin result
:= self
; exit
; end;
1693 for ctl
in mChildren
do
1695 result
:= ctl
.findCancelControl();
1696 if (result
<> nil) then exit
;
1703 function TUIControl
.findControlById (const aid
: AnsiString): TUIControl
;
1707 if (strEquCI1251(aid
, mId
)) then begin result
:= self
; exit
; end;
1708 for ctl
in mChildren
do
1710 result
:= ctl
.findControlById(aid
);
1711 if (result
<> nil) then exit
;
1717 procedure TUIControl
.appendChild (ctl
: TUIControl
);
1719 if (ctl
= nil) then exit
;
1720 if (ctl
.mParent
<> nil) then exit
;
1721 SetLength(mChildren
, Length(mChildren
)+1);
1722 mChildren
[High(mChildren
)] := ctl
;
1723 ctl
.mParent
:= self
;
1724 Inc(ctl
.mX
, mFrameWidth
);
1725 Inc(ctl
.mY
, mFrameHeight
);
1726 if (ctl
.mWidth
> 0) and (ctl
.mHeight
> 0) and
1727 (ctl
.mX
+ctl
.mWidth
> mFrameWidth
) and (ctl
.mY
+ctl
.mHeight
> mFrameHeight
) then
1729 if (mWidth
+mFrameWidth
< ctl
.mX
+ctl
.mWidth
) then mWidth
:= ctl
.mX
+ctl
.mWidth
+mFrameWidth
;
1730 if (mHeight
+mFrameHeight
< ctl
.mY
+ctl
.mHeight
) then mHeight
:= ctl
.mY
+ctl
.mHeight
+mFrameHeight
;
1735 function TUIControl
.setActionCBFor (const aid
: AnsiString; cb
: TActionCB
): TActionCB
;
1740 if (ctl
<> nil) then
1742 result
:= ctl
.actionCB
;
1752 function TUIControl
.forEachChildren (cb
: TCtlEnumCB
): TUIControl
;
1757 if (not assigned(cb
)) then exit
;
1758 for ctl
in mChildren
do
1760 if cb(ctl
) then begin result
:= ctl
; exit
; end;
1765 function TUIControl
.forEachControl (cb
: TCtlEnumCB
; includeSelf
: Boolean=true): TUIControl
;
1767 function forChildren (p
: TUIControl
; incSelf
: Boolean): TUIControl
;
1772 if (p
= nil) then exit
;
1773 if (incSelf
) and (cb(p
)) then begin result
:= p
; exit
; end;
1774 for ctl
in p
.mChildren
do
1776 result
:= forChildren(ctl
, true);
1777 if (result
<> nil) then break
;
1783 if (not assigned(cb
)) then exit
;
1784 result
:= forChildren(self
, includeSelf
);
1788 procedure TUIControl
.close (); // this closes *top-level* control
1793 uiRemoveWindow(ctl
);
1794 if (ctl
is TUITopWindow
) and (TUITopWindow(ctl
).mFreeOnClose
) then scheduleKill(ctl
); // just in case
1798 procedure TUIControl
.doAction ();
1800 if assigned(actionCB
) then actionCB(self
);
1804 // ////////////////////////////////////////////////////////////////////////// //
1805 procedure TUIControl
.setScissorGLInternal (x
, y
, w
, h
: Integer);
1807 if not scallowed
then exit
;
1808 x
:= trunc(x
*gh_ui_scale
);
1809 y
:= trunc(y
*gh_ui_scale
);
1810 w
:= trunc(w
*gh_ui_scale
);
1811 h
:= trunc(h
*gh_ui_scale
);
1812 scis
.combineRect(x
, y
, w
, h
);
1815 procedure TUIControl
.setScissor (lx
, ly
, lw
, lh
: Integer);
1817 gx
, gy
, wdt
, hgt
, cgx
, cgy
: Integer;
1819 if not scallowed
then exit
;
1821 if not intersectRect(lx
, ly
, lw
, lh
, 0, 0, mWidth
, mHeight
) then
1823 scis
.combineRect(0, 0, 0, 0);
1827 getDrawRect(gx
, gy
, wdt
, hgt
);
1828 toGlobal(lx
, ly
, cgx
, cgy
);
1829 if not intersectRect(gx
, gy
, wdt
, hgt
, cgx
, cgy
, lw
, lh
) then
1831 scis
.combineRect(0, 0, 0, 0);
1835 setScissorGLInternal(gx
, gy
, wdt
, hgt
);
1838 procedure TUIControl
.resetScissor (fullArea
: Boolean); inline;
1840 if not scallowed
then exit
;
1843 setScissor(0, 0, mWidth
, mHeight
);
1847 setScissor(mFrameWidth
, mFrameHeight
, mWidth
-mFrameWidth
*2, mHeight
-mFrameHeight
*2);
1852 // ////////////////////////////////////////////////////////////////////////// //
1853 procedure TUIControl
.draw ();
1858 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1859 toGlobal(0, 0, gx
, gy
);
1861 scis
.save(true); // scissoring enabled
1864 resetScissor(true); // full area
1865 drawControl(gx
, gy
);
1866 resetScissor(false); // client area
1867 for f
:= 0 to High(mChildren
) do mChildren
[f
].draw();
1868 resetScissor(true); // full area
1869 drawControlPost(gx
, gy
);
1876 procedure TUIControl
.drawControl (gx
, gy
: Integer);
1878 //if (mParent = nil) then darkenRect(gx, gy, mWidth, mHeight, 64);
1881 procedure TUIControl
.drawControlPost (gx
, gy
: Integer);
1884 if mDrawShadow
and (mWidth
> 0) and (mHeight
> 0) then
1886 setScissorGLInternal(gx
+8, gy
+8, mWidth
, mHeight
);
1887 darkenRect(gx
+mWidth
, gy
+8, 8, mHeight
, 128);
1888 darkenRect(gx
+8, gy
+mHeight
, mWidth
-8, 8, 128);
1893 // ////////////////////////////////////////////////////////////////////////// //
1894 procedure TUIControl
.mouseEvent (var ev
: THMouseEvent
);
1898 if (not enabled
) then exit
;
1899 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1900 ctl
:= controlAtXY(ev
.x
, ev
.y
);
1901 if (ctl
= nil) then exit
;
1902 if (ctl
.canFocus
) and (ev
.press
) then
1904 if (ctl
<> topLevel
.mFocused
) then ctl
.setFocused(true);
1907 if (ctl
<> self
) then ctl
.mouseEvent(ev
);
1912 procedure TUIControl
.keyEvent (var ev
: THKeyEvent
);
1914 function doPreKey (ctl
: TUIControl
): Boolean;
1916 if (not ctl
.enabled
) then begin result
:= false; exit
; end;
1917 ctl
.keyEventPre(ev
);
1918 result
:= (ev
.eaten
) or (ev
.cancelled
); // stop if event was consumed
1921 function doPostKey (ctl
: TUIControl
): Boolean;
1923 if (not ctl
.enabled
) then begin result
:= false; exit
; end;
1924 ctl
.keyEventPost(ev
);
1925 result
:= (ev
.eaten
) or (ev
.cancelled
); // stop if event was consumed
1931 if (not enabled
) then exit
;
1932 if (ev
.eaten
) or (ev
.cancelled
) then exit
;
1934 if (mParent
= nil) then
1936 forEachControl(doPreKey
);
1937 if (ev
.eaten
) or (ev
.cancelled
) then exit
;
1939 // focused control should process keyboard first
1940 if (topLevel
.mFocused
<> self
) and isMyChild(topLevel
.mFocused
) and (topLevel
.mFocused
.enabled
) then
1942 // bubble keyboard event
1943 ctl
:= topLevel
.mFocused
;
1944 while (ctl
<> nil) and (ctl
<> self
) do
1947 if (ev
.eaten
) or (ev
.cancelled
) then exit
;
1951 // for top-level controls
1952 if (mParent
= nil) then
1954 if (ev
= 'S-Tab') then
1956 ctl
:= findPrevFocus(mFocused
, true);
1957 if (ctl
<> nil) and (ctl
<> mFocused
) then ctl
.setFocused(true);
1961 if (ev
= 'Tab') then
1963 ctl
:= findNextFocus(mFocused
, true);
1964 if (ctl
<> nil) and (ctl
<> mFocused
) then ctl
.setFocused(true);
1968 if (ev
= 'Enter') or (ev
= 'C-Enter') then
1970 ctl
:= findDefaulControl();
1971 if (ctl
<> nil) then
1978 if (ev
= 'Escape') then
1980 ctl
:= findCancelControl();
1981 if (ctl
<> nil) then
1988 if mEscClose
and (ev
= 'Escape') then
1990 if (not assigned(closeRequestCB
)) or (closeRequestCB(self
)) then
1992 uiRemoveWindow(self
);
1998 if (ev
.eaten
) or (ev
.cancelled
) then exit
;
1999 forEachControl(doPostKey
);
2004 procedure TUIControl
.keyEventPre (var ev
: THKeyEvent
);
2009 procedure TUIControl
.keyEventPost (var ev
: THKeyEvent
);
2014 // ////////////////////////////////////////////////////////////////////////// //
2015 constructor TUITopWindow
.Create (const atitle
: AnsiString);
2022 procedure TUITopWindow
.AfterConstruction ();
2027 if (mWidth
< mFrameWidth
*2+3*8) then mWidth
:= mFrameWidth
*2+3*8;
2028 if (mHeight
< mFrameHeight
*2) then mHeight
:= mFrameHeight
*2;
2029 if (Length(mTitle
) > 0) then
2031 if (mWidth
< Length(mTitle
)*8+mFrameWidth
*2+3*8) then mWidth
:= Length(mTitle
)*8+mFrameWidth
*2+3*8;
2034 mDragScroll
:= TXMode
.None
;
2035 mDrawShadow
:= true;
2036 mWaitingClose
:= false;
2039 mCtl4Style
:= 'window';
2043 function TUITopWindow
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2045 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
2047 mTitle
:= par
.expectIdOrStr(true);
2051 if (strEquCI1251(prname
, 'children')) then
2057 if (strEquCI1251(prname
, 'position')) then
2059 if (par
.eatIdOrStrCI('default')) then mDoCenter
:= false
2060 else if (par
.eatIdOrStrCI('center')) then mDoCenter
:= true
2061 else par
.error('`center` or `default` expected');
2065 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
2066 result
:= inherited parseProperty(prname
, par
);
2070 procedure TUITopWindow
.centerInScreen ();
2072 if (mWidth
> 0) and (mHeight
> 0) then
2074 mX
:= trunc((gScrWidth
/gh_ui_scale
-mWidth
)/2);
2075 mY
:= trunc((gScrHeight
/gh_ui_scale
-mHeight
)/2);
2080 procedure TUITopWindow
.drawControl (gx
, gy
: Integer);
2082 fillRect(gx
, gy
, mWidth
, mHeight
, mBackColor
[getColorIndex
]);
2086 procedure TUITopWindow
.drawControlPost (gx
, gy
: Integer);
2089 tx
, hgt
, sbhgt
: Integer;
2091 cidx
:= getColorIndex
;
2092 if (mDragScroll
= TXMode
.Drag
) then
2094 drawRectUI(gx
+4, gy
+4, mWidth
-8, mHeight
-8, mFrameColor
[cidx
]);
2098 drawRectUI(gx
+3, gy
+3, mWidth
-6, mHeight
-6, mFrameColor
[cidx
]);
2099 drawRectUI(gx
+5, gy
+5, mWidth
-10, mHeight
-10, mFrameColor
[cidx
]);
2100 // vertical scroll bar
2101 hgt
:= mHeight
-mFrameHeight
*2;
2102 if (hgt
> 0) and (mFullSize
.h
> hgt
) then
2104 //writeln(mTitle, ': height=', mHeight-mFrameHeight*2, '; fullsize=', mFullSize.toString);
2105 sbhgt
:= mHeight
-mFrameHeight
*2+2;
2106 fillRect(gx
+mWidth
-mFrameWidth
+1, gy
+7, mFrameWidth
-3, sbhgt
, mFrameColor
[cidx
]);
2108 if (hgt
> mFullSize
.h
) then hgt
:= mFullSize
.h
;
2109 hgt
:= sbhgt
*hgt
div mFullSize
.h
;
2112 setScissor(mWidth
-mFrameWidth
+1, 7, mFrameWidth
-3, sbhgt
);
2113 darkenRect(gx
+mWidth
-mFrameWidth
+1, gy
+7+hgt
, mFrameWidth
-3, sbhgt
, 128);
2117 setScissor(mFrameWidth
, 0, 3*8, 8);
2118 fillRect(gx
+mFrameWidth
, gy
, 3*8, 8, mBackColor
[cidx
]);
2119 drawText8(gx
+mFrameWidth
, gy
, '[ ]', mFrameColor
[cidx
]);
2120 if mInClose
then drawText8(gx
+mFrameWidth
+7, gy
, '#', mFrameIconColor
[cidx
])
2121 else drawText8(gx
+mFrameWidth
+7, gy
, '*', mFrameIconColor
[cidx
]);
2124 if (Length(mTitle
) > 0) then
2126 setScissor(mFrameWidth
+3*8, 0, mWidth
-mFrameWidth
*2-3*8, 8);
2127 tx
:= (gx
+3*8)+((mWidth
-3*8)-Length(mTitle
)*8) div 2;
2128 fillRect(tx
-3, gy
, Length(mTitle
)*8+3+2, 8, mBackColor
[cidx
]);
2129 drawText8(tx
, gy
, mTitle
, mFrameTextColor
[cidx
]);
2132 inherited drawControlPost(gx
, gy
);
2136 procedure TUITopWindow
.activated ();
2138 if (mFocused
= nil) or (mFocused
= self
) then
2140 mFocused
:= findFirstFocus();
2142 if (mFocused
<> nil) and (mFocused
<> self
) then mFocused
.activated();
2147 procedure TUITopWindow
.blurred ();
2149 mDragScroll
:= TXMode
.None
;
2150 mWaitingClose
:= false;
2152 if (mFocused
<> nil) and (mFocused
<> self
) then mFocused
.blurred();
2157 procedure TUITopWindow
.keyEvent (var ev
: THKeyEvent
);
2159 inherited keyEvent(ev
);
2160 if (ev
.eaten
) or (ev
.cancelled
) or (not enabled
) {or (not getFocused)} then exit
;
2161 if (ev
= 'M-F3') then
2163 if (not assigned(closeRequestCB
)) or (closeRequestCB(self
)) then
2165 uiRemoveWindow(self
);
2173 procedure TUITopWindow
.mouseEvent (var ev
: THMouseEvent
);
2176 hgt
, sbhgt
: Integer;
2178 if (not enabled
) then exit
;
2179 if (mWidth
< 1) or (mHeight
< 1) then exit
;
2181 if (mDragScroll
= TXMode
.Drag
) then
2183 mX
+= ev
.x
-mDragStartX
;
2184 mY
+= ev
.y
-mDragStartY
;
2185 mDragStartX
:= ev
.x
;
2186 mDragStartY
:= ev
.y
;
2187 if (ev
.release
) and (ev
.but
= ev
.Left
) then mDragScroll
:= TXMode
.None
;
2192 if (mDragScroll
= TXMode
.Scroll
) then
2194 // check for vertical scrollbar
2202 sbhgt
:= mHeight
-mFrameHeight
*2+2;
2203 hgt
:= mHeight
-mFrameHeight
*2;
2204 if (hgt
> 0) and (mFullSize
.h
> hgt
) then
2206 hgt
:= (mFullSize
.h
*(ly
-7) div (sbhgt
-1))-(mHeight
-mFrameHeight
*2);
2207 mScrollY
:= nmax(0, hgt
);
2208 hgt
:= mHeight
-mFrameHeight
*2;
2209 if (mScrollY
+hgt
> mFullSize
.h
) then mScrollY
:= nmax(0, mFullSize
.h
-hgt
);
2212 if (ev
.release
) and (ev
.but
= ev
.Left
) then mDragScroll
:= TXMode
.None
;
2217 if toLocal(ev
.x
, ev
.y
, lx
, ly
) then
2224 if (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8) then
2226 //uiRemoveWindow(self);
2227 mWaitingClose
:= true;
2232 mDragScroll
:= TXMode
.Drag
;
2233 mDragStartX
:= ev
.x
;
2234 mDragStartY
:= ev
.y
;
2239 // check for vertical scrollbar
2240 if (lx
>= mWidth
-mFrameWidth
+1) and (ly
>= 7) and (ly
< mHeight
-mFrameHeight
+1) then
2242 sbhgt
:= mHeight
-mFrameHeight
*2+2;
2243 hgt
:= mHeight
-mFrameHeight
*2;
2244 if (hgt
> 0) and (mFullSize
.h
> hgt
) then
2246 hgt
:= (mFullSize
.h
*(ly
-7) div (sbhgt
-1))-(mHeight
-mFrameHeight
*2);
2247 mScrollY
:= nmax(0, hgt
);
2249 mDragScroll
:= TXMode
.Scroll
;
2255 if (lx
< mFrameWidth
) or (lx
>= mWidth
-mFrameWidth
) or (ly
>= mHeight
-mFrameHeight
) then
2258 mDragScroll
:= TXMode
.Drag
;
2259 mDragStartX
:= ev
.x
;
2260 mDragStartY
:= ev
.y
;
2266 if (ev
.release
) then
2268 if mWaitingClose
then
2270 if (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8) then
2272 if (not assigned(closeRequestCB
)) or (closeRequestCB(self
)) then
2274 uiRemoveWindow(self
);
2277 mWaitingClose
:= false;
2286 if mWaitingClose
then
2288 mInClose
:= (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+3*8);
2294 inherited mouseEvent(ev
);
2299 if (not ev
.motion
) and (mWaitingClose
) then begin ev
.eat(); mWaitingClose
:= false; exit
; end;
2304 // ////////////////////////////////////////////////////////////////////////// //
2305 constructor TUIBox
.Create (ahoriz
: Boolean);
2312 procedure TUIBox
.AfterConstruction ();
2316 mHAlign
:= -1; // left
2317 mCtl4Style
:= 'box';
2321 procedure TUIBox
.setCaption (const acap
: AnsiString);
2324 mDefSize
:= TLaySize
.Create(Length(mCaption
)*8+3, 8);
2328 procedure TUIBox
.setHasFrame (v
: Boolean);
2331 if (mHasFrame
) then begin mFrameWidth
:= 8; mFrameHeight
:= 8; end else begin mFrameWidth
:= 0; mFrameHeight
:= 0; end;
2335 function TUIBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2337 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
2338 if (strEquCI1251(prname
, 'frame')) then
2340 setHasFrame(parseBool(par
));
2344 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
2346 setCaption(par
.expectIdOrStr(true));
2350 if (strEquCI1251(prname
, 'textalign')) or (strEquCI1251(prname
, 'text-align')) then
2352 mHAlign
:= parseHAlign(par
);
2356 if (strEquCI1251(prname
, 'children')) then
2362 result
:= inherited parseProperty(prname
, par
);
2366 procedure TUIBox
.drawControl (gx
, gy
: Integer);
2371 cidx
:= getColorIndex
;
2372 fillRect(gx
, gy
, mWidth
, mHeight
, mBackColor
[cidx
]);
2376 drawRectUI(gx
+3, gy
+3, mWidth
-6, mHeight
-6, mFrameColor
[cidx
]);
2379 if (Length(mCaption
) > 0) then
2381 if (mHAlign
< 0) then xpos
:= 3
2382 else if (mHAlign
> 0) then xpos
:= mWidth
-mFrameWidth
*2-Length(mCaption
)*8
2383 else xpos
:= (mWidth
-mFrameWidth
*2-Length(mCaption
)*8) div 2;
2384 xpos
+= gx
+mFrameWidth
;
2386 setScissor(mFrameWidth
+1, 0, mWidth
-mFrameWidth
-2, 8);
2387 if mHasFrame
then fillRect(xpos
-3, gy
, Length(mCaption
)*8+4, 8, mBackColor
[cidx
]);
2388 drawText8(xpos
, gy
, mCaption
, mFrameTextColor
[cidx
]);
2393 procedure TUIBox
.mouseEvent (var ev
: THMouseEvent
);
2397 inherited mouseEvent(ev
);
2398 if (not ev
.eaten
) and (not ev
.cancelled
) and (enabled
) and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
2405 procedure TUIBox
.keyEvent (var ev
: THKeyEvent
);
2408 cur
, ctl
: TUIControl
;
2410 inherited keyEvent(ev
);
2411 if (ev
.eaten
) or (ev
.cancelled
) or (not ev
.press
) or (not enabled
) or (not getActive
) then exit
;
2412 if (Length(mChildren
) = 0) then exit
;
2413 if (mHoriz
) and (ev
= 'Left') then dir
:= -1
2414 else if (mHoriz
) and (ev
= 'Right') then dir
:= 1
2415 else if (not mHoriz
) and (ev
= 'Up') then dir
:= -1
2416 else if (not mHoriz
) and (ev
= 'Down') then dir
:= 1;
2417 if (dir
= 0) then exit
;
2419 cur
:= topLevel
.mFocused
;
2420 while (cur
<> nil) and (cur
.mParent
<> self
) do cur
:= cur
.mParent
;
2421 //if (cur = nil) then writeln('CUR: nil') else writeln('CUR: ', cur.className, '#', cur.id);
2422 if (dir
< 0) then ctl
:= findPrevFocus(cur
, true) else ctl
:= findNextFocus(cur
, true);
2423 //if (ctl = nil) then writeln('CTL: nil') else writeln('CTL: ', ctl.className, '#', ctl.id);
2424 if (ctl
<> nil) and (ctl
<> self
) then
2426 ctl
.focused
:= true;
2431 // ////////////////////////////////////////////////////////////////////////// //
2432 constructor TUIHBox
.Create ();
2437 procedure TUIHBox
.AfterConstruction ();
2444 // ////////////////////////////////////////////////////////////////////////// //
2445 constructor TUIVBox
.Create ();
2450 procedure TUIVBox
.AfterConstruction ();
2454 writeln('VBOX: ', canFocus
, ':', enabled
);
2458 // ////////////////////////////////////////////////////////////////////////// //
2459 procedure TUISpan
.AfterConstruction ();
2464 mCtl4Style
:= 'span';
2468 function TUISpan
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2470 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
2471 result
:= inherited parseProperty(prname
, par
);
2475 procedure TUISpan
.drawControl (gx
, gy
: Integer);
2480 // ////////////////////////////////////////////////////////////////////// //
2481 procedure TUILine
.AfterConstruction ();
2487 mCtl4Style
:= 'line';
2491 function TUILine
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2493 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
2494 result
:= inherited parseProperty(prname
, par
);
2498 procedure TUILine
.drawControl (gx
, gy
: Integer);
2502 cidx
:= getColorIndex
;
2505 drawHLine(gx
, gy
+(mHeight
div 2), mWidth
, mTextColor
[cidx
]);
2509 drawVLine(gx
+(mWidth
div 2), gy
, mHeight
, mTextColor
[cidx
]);
2514 // ////////////////////////////////////////////////////////////////////////// //
2515 procedure TUIHLine
.AfterConstruction ();
2523 // ////////////////////////////////////////////////////////////////////////// //
2524 procedure TUIVLine
.AfterConstruction ();
2532 // ////////////////////////////////////////////////////////////////////////// //
2533 procedure TUIStaticText
.AfterConstruction ();
2539 mHoriz
:= true; // nobody cares
2543 mCtl4Style
:= 'static';
2547 procedure TUIStaticText
.setText (const atext
: AnsiString);
2550 mDefSize
:= TLaySize
.Create(Length(mText
)*8, 8);
2554 function TUIStaticText
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2556 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) or (strEquCI1251(prname
, 'text')) then
2558 setText(par
.expectIdOrStr(true));
2562 if (strEquCI1251(prname
, 'textalign')) or (strEquCI1251(prname
, 'text-align')) then
2564 parseTextAlign(par
, mHAlign
, mVAlign
);
2568 if (strEquCI1251(prname
, 'header')) then
2574 if (strEquCI1251(prname
, 'line')) then
2580 result
:= inherited parseProperty(prname
, par
);
2584 procedure TUIStaticText
.drawControl (gx
, gy
: Integer);
2586 xpos
, ypos
: Integer;
2590 cidx
:= getColorIndex
;
2591 fillRect(gx
, gy
, mWidth
, mHeight
, mBackColor
[cidx
]);
2593 if (mHAlign
< 0) then xpos
:= 0
2594 else if (mHAlign
> 0) then xpos
:= mWidth
-Length(mText
)*8
2595 else xpos
:= (mWidth
-Length(mText
)*8) div 2;
2597 if (Length(mText
) > 0) then
2599 if (mHeader
) then clr
:= mFrameTextColor
[cidx
] else clr
:= mTextColor
[cidx
];
2601 if (mVAlign
< 0) then ypos
:= 0
2602 else if (mVAlign
> 0) then ypos
:= mHeight
-8
2603 else ypos
:= (mHeight
-8) div 2;
2605 drawText8(gx
+xpos
, gy
+ypos
, mText
, clr
);
2610 if (mHeader
) then clr
:= mFrameColor
[cidx
] else clr
:= mTextColor
[cidx
];
2612 if (mVAlign
< 0) then ypos
:= 0
2613 else if (mVAlign
> 0) then ypos
:= mHeight
-1
2614 else ypos
:= (mHeight
div 2);
2617 if (Length(mText
) = 0) then
2619 drawHLine(gx
, ypos
, mWidth
, clr
);
2623 drawHLine(gx
, ypos
, xpos
-1, clr
);
2624 drawHLine(gx
+xpos
+Length(mText
)*8, ypos
, mWidth
, clr
);
2630 // ////////////////////////////////////////////////////////////////////////// //
2631 procedure TUITextLabel
.AfterConstruction ();
2637 mDefSize
:= TLaySize
.Create(Length(mText
)*8, 8);
2638 mCtl4Style
:= 'label';
2643 procedure TUITextLabel
.cacheStyle (root
: TUIStyle
);
2645 inherited cacheStyle(root
);
2647 mHotColor
[ClrIdxActive
] := root
.get('hot-color', 'active', mCtl4Style
).asRGBADef(TGxRGBA
.Create(0, 128, 0));
2649 mHotColor
[ClrIdxDisabled
] := root
.get('hot-color', 'disabled', mCtl4Style
).asRGBADef(TGxRGBA
.Create(0, 64, 0));
2651 mHotColor
[ClrIdxInactive
] := root
.get('hot-color', 'inactive', mCtl4Style
).asRGBADef(TGxRGBA
.Create(0, 64, 0));
2655 procedure TUITextLabel
.setText (const s
: AnsiString);
2663 while (f
<= Length(s
)) do
2665 if (s
[f
] = '\\') then
2668 if (f
<= Length(s
)) then mText
+= s
[f
];
2671 else if (s
[f
] = '~') then
2674 if (f
<= Length(s
)) then
2676 if (mHotChar
= #0) then
2679 mHotOfs
:= Length(mText
)*8;
2691 mDefSize
:= TLaySize
.Create(Length(mText
)*8, 8);
2695 function TUITextLabel
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2697 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) or (strEquCI1251(prname
, 'text')) then
2699 setText(par
.expectIdOrStr(true));
2703 if (strEquCI1251(prname
, 'link')) then
2705 mLinkId
:= par
.expectIdOrStr(true);
2709 if (strEquCI1251(prname
, 'textalign')) or (strEquCI1251(prname
, 'text-align')) then
2711 parseTextAlign(par
, mHAlign
, mVAlign
);
2715 result
:= inherited parseProperty(prname
, par
);
2719 procedure TUITextLabel
.drawControl (gx
, gy
: Integer);
2721 xpos
, ypos
: Integer;
2724 cidx
:= getColorIndex
;
2725 fillRect(gx
, gy
, mWidth
, mHeight
, mBackColor
[cidx
]);
2726 if (Length(mText
) > 0) then
2728 if (mHAlign
< 0) then xpos
:= 0
2729 else if (mHAlign
> 0) then xpos
:= mWidth
-Length(mText
)*8
2730 else xpos
:= (mWidth
-Length(mText
)*8) div 2;
2732 if (mVAlign
< 0) then ypos
:= 0
2733 else if (mVAlign
> 0) then ypos
:= mHeight
-8
2734 else ypos
:= (mHeight
-8) div 2;
2736 drawText8(gx
+xpos
, gy
+ypos
, mText
, mTextColor
[cidx
]);
2738 if (Length(mLinkId
) > 0) and (mHotChar
<> #0) and (mHotChar
<> ' ') then
2740 drawText8(gx
+xpos
+8+mHotOfs
, gy
+ypos
, mHotChar
, mHotColor
[cidx
]);
2746 procedure TUITextLabel
.mouseEvent (var ev
: THMouseEvent
);
2750 inherited mouseEvent(ev
);
2751 if (not ev
.eaten
) and (not ev
.cancelled
) and (enabled
) and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
2758 procedure TUITextLabel
.keyEventPost (var ev
: THKeyEvent
);
2762 if (not enabled
) then exit
;
2763 if (mHotChar
= #0) or (Length(mLinkId
) = 0) then exit
;
2764 if (ev
.eaten
) or (ev
.cancelled
) or (not ev
.press
) then exit
;
2765 if (not ev
.isHot(mHotChar
)) then exit
;
2766 ctl
:= topLevel
[mLinkId
];
2767 if (ctl
<> nil) then
2770 if (ctl
.canFocus
) then ctl
.focused
:= true;
2775 // ////////////////////////////////////////////////////////////////////////// //
2776 procedure TUIButton
.AfterConstruction ();
2782 mDefSize
:= TLaySize
.Create(Length(mText
)*8+8, 10);
2783 mCtl4Style
:= 'button';
2787 procedure TUIButton
.setText (const s
: AnsiString);
2789 inherited setText(s
);
2790 mDefSize
:= TLaySize
.Create(Length(mText
)*8+8*2, 10);
2794 procedure TUIButton
.drawControl (gx
, gy
: Integer);
2796 xpos
, ypos
: Integer;
2799 cidx
:= getColorIndex
;
2801 fillRect(gx
+1, gy
, mWidth
-2, mHeight
, mBackColor
[cidx
]);
2802 fillRect(gx
, gy
+1, 1, mHeight
-2, mBackColor
[cidx
]);
2803 fillRect(gx
+mWidth
-1, gy
+1, 1, mHeight
-2, mBackColor
[cidx
]);
2805 if (Length(mText
) > 0) then
2807 if (mHAlign
< 0) then xpos
:= 0
2808 else if (mHAlign
> 0) then xpos
:= mWidth
-Length(mText
)*8
2809 else xpos
:= (mWidth
-Length(mText
)*8) div 2;
2811 if (mVAlign
< 0) then ypos
:= 0
2812 else if (mVAlign
> 0) then ypos
:= mHeight
-8
2813 else ypos
:= (mHeight
-8) div 2;
2815 setScissor(8, 0, mWidth
-16, mHeight
);
2816 drawText8(gx
+xpos
+8, gy
+ypos
, mText
, mTextColor
[cidx
]);
2818 if (mHotChar
<> #0) and (mHotChar
<> ' ') then drawText8(gx
+xpos
+8+mHotOfs
, gy
+ypos
, mHotChar
, mHotColor
[cidx
]);
2823 procedure TUIButton
.mouseEvent (var ev
: THMouseEvent
);
2827 inherited mouseEvent(ev
);
2828 if (uiGrabCtl
= self
) then
2831 if (ev
= '-lmb') and focused
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
2837 if (ev
.eaten
) or (ev
.cancelled
) or (not enabled
) or not focused
then exit
;
2842 procedure TUIButton
.keyEvent (var ev
: THKeyEvent
);
2844 inherited keyEvent(ev
);
2845 if (not ev
.eaten
) and (not ev
.cancelled
) and (enabled
) then
2847 if (ev
= 'Enter') or (ev
= 'Space') then
2857 procedure TUIButton
.keyEventPost (var ev
: THKeyEvent
);
2859 if (not enabled
) then exit
;
2860 if (mHotChar
= #0) then exit
;
2861 if (ev
.eaten
) or (ev
.cancelled
) or (not ev
.press
) then exit
;
2862 if (not ev
.isHot(mHotChar
)) then exit
;
2863 if (not canFocus
) then exit
;
2870 // ////////////////////////////////////////////////////////////////////////// //
2871 procedure TUISwitchBox
.AfterConstruction ();
2877 mDefSize
:= TLaySize
.Create(Length(mText
)*8+8*3, 8);
2878 mCtl4Style
:= 'switchbox';
2880 mBoolVar
:= @mChecked
;
2884 procedure TUISwitchBox
.cacheStyle (root
: TUIStyle
);
2886 inherited cacheStyle(root
);
2888 mSwitchColor
[ClrIdxActive
] := root
.get('switch-color', 'active', mCtl4Style
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
2890 mSwitchColor
[ClrIdxDisabled
] := root
.get('switch-color', 'disabled', mCtl4Style
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
2892 mSwitchColor
[ClrIdxInactive
] := root
.get('switch-color', 'inactive', mCtl4Style
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
2896 procedure TUISwitchBox
.setText (const s
: AnsiString);
2898 inherited setText(s
);
2899 mDefSize
:= TLaySize
.Create(Length(mText
)*8+8*3, 8);
2903 function TUISwitchBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2905 if (strEquCI1251(prname
, 'checked')) then
2911 result
:= inherited parseProperty(prname
, par
);
2915 function TUISwitchBox
.getChecked (): Boolean;
2917 if (mBoolVar
<> nil) then result
:= mBoolVar
^ else result
:= false;
2921 procedure TUISwitchBox
.setVar (pvar
: PBoolean);
2923 if (pvar
= nil) then pvar
:= @mChecked
;
2924 if (pvar
<> mBoolVar
) then
2927 setChecked(mBoolVar
^);
2932 procedure TUISwitchBox
.drawControl (gx
, gy
: Integer);
2934 xpos
, ypos
: Integer;
2937 cidx
:= getColorIndex
;
2939 if (mHAlign
< 0) then xpos
:= 0
2940 else if (mHAlign
> 0) then xpos
:= mWidth
-(Length(mText
)+4)*8
2941 else xpos
:= (mWidth
-(Length(mText
)+4)*8) div 2;
2943 if (mVAlign
< 0) then ypos
:= 0
2944 else if (mVAlign
> 0) then ypos
:= mHeight
-8
2945 else ypos
:= (mHeight
-8) div 2;
2948 fillRect(gx
, gy
, mWidth
, mHeight
, mBackColor
[cidx
]);
2952 if (Length(mCheckedStr
) <> 3) or (mCheckedStr
[2] <> '*') then
2954 drawText8(gx
+xpos
, gy
+ypos
, mCheckedStr
, mSwitchColor
[cidx
]);
2958 drawText8(gx
+xpos
, gy
+ypos
, mCheckedStr
[1], mSwitchColor
[cidx
]);
2959 drawText8(gx
+xpos
+2*8, gy
+ypos
, mCheckedStr
[3], mSwitchColor
[cidx
]);
2960 drawText8(gx
+xpos
+7, gy
+ypos
, '*', mSwitchColor
[cidx
]);
2965 drawText8(gx
+xpos
, gy
+ypos
, mUncheckedStr
, mSwitchColor
[cidx
]);
2968 drawText8(gx
+xpos
+8*3, gy
+ypos
, mText
, mTextColor
[cidx
]);
2970 if (mHotChar
<> #0) and (mHotChar
<> ' ') then drawText8(gx
+xpos
+8*3+mHotOfs
, gy
+ypos
, mHotChar
, mHotColor
[cidx
]);
2974 procedure TUISwitchBox
.mouseEvent (var ev
: THMouseEvent
);
2978 inherited mouseEvent(ev
);
2979 if (uiGrabCtl
= self
) then
2982 if (ev
= '-lmb') and focused
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
2988 if (ev
.eaten
) or (ev
.cancelled
) or (not enabled
) or not focused
then exit
;
2993 procedure TUISwitchBox
.keyEvent (var ev
: THKeyEvent
);
2995 inherited keyEvent(ev
);
2996 if (not ev
.eaten
) and (not ev
.cancelled
) and (enabled
) then
2998 if (ev
= 'Space') then
3008 procedure TUISwitchBox
.keyEventPost (var ev
: THKeyEvent
);
3010 if (not enabled
) then exit
;
3011 if (mHotChar
= #0) then exit
;
3012 if (ev
.eaten
) or (ev
.cancelled
) or (not ev
.press
) then exit
;
3013 if (not ev
.isHot(mHotChar
)) then exit
;
3014 if (not canFocus
) then exit
;
3021 // ////////////////////////////////////////////////////////////////////////// //
3022 procedure TUICheckBox
.AfterConstruction ();
3026 mBoolVar
:= @mChecked
;
3027 mCheckedStr
:= '[x]';
3028 mUncheckedStr
:= '[ ]';
3032 procedure TUICheckBox
.setChecked (v
: Boolean);
3038 procedure TUICheckBox
.doAction ();
3040 if (assigned(actionCB
)) then
3046 setChecked(not getChecked
);
3051 // ////////////////////////////////////////////////////////////////////////// //
3052 procedure TUIRadioBox
.AfterConstruction ();
3056 mBoolVar
:= @mChecked
;
3057 mCheckedStr
:= '(*)';
3058 mUncheckedStr
:= '( )';
3063 function TUIRadioBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
3065 if (strEquCI1251(prname
, 'group')) then
3067 mRadioGroup
:= par
.expectIdOrStr(true);
3068 if (getChecked
) then setChecked(true);
3072 if (strEquCI1251(prname
, 'checked')) then
3078 result
:= inherited parseProperty(prname
, par
);
3082 procedure TUIRadioBox
.setChecked (v
: Boolean);
3084 function resetGroup (ctl
: TUIControl
): Boolean;
3087 if (ctl
<> self
) and (ctl
is TUIRadioBox
) and (TUIRadioBox(ctl
).mRadioGroup
= mRadioGroup
) then
3089 TUIRadioBox(ctl
).mBoolVar
^ := false;
3095 if v
then topLevel
.forEachControl(resetGroup
);
3099 procedure TUIRadioBox
.doAction ();
3101 if (assigned(actionCB
)) then
3112 // ////////////////////////////////////////////////////////////////////////// //
3114 registerCtlClass(TUIHBox
, 'hbox');
3115 registerCtlClass(TUIVBox
, 'vbox');
3116 registerCtlClass(TUISpan
, 'span');
3117 registerCtlClass(TUIHLine
, 'hline');
3118 registerCtlClass(TUIVLine
, 'vline');
3119 registerCtlClass(TUITextLabel
, 'label');
3120 registerCtlClass(TUIStaticText
, 'static');
3121 registerCtlClass(TUIButton
, 'button');
3122 registerCtlClass(TUICheckBox
, 'checkbox');
3123 registerCtlClass(TUIRadioBox
, 'radiobox');