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}
27 fui_common
, fui_events
, fui_style
,
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 mStyleLoaded
: Boolean;
68 mCtl4Style
: AnsiString;
69 mBackColor
: array[0..ClrIdxMax
] of TGxRGBA
;
70 mTextColor
: array[0..ClrIdxMax
] of TGxRGBA
;
71 mFrameColor
: array[0..ClrIdxMax
] of TGxRGBA
;
72 mFrameTextColor
: array[0..ClrIdxMax
] of TGxRGBA
;
73 mFrameIconColor
: array[0..ClrIdxMax
] of TGxRGBA
;
74 mDarken
: array[0..ClrIdxMax
] of Integer; // >255: none
77 procedure updateStyle (); virtual;
78 procedure cacheStyle (root
: TUIStyle
); virtual;
79 function getColorIndex (): Integer; inline;
82 function getEnabled (): Boolean;
83 procedure setEnabled (v
: Boolean); inline;
85 function getFocused (): Boolean; inline;
86 procedure setFocused (v
: Boolean); inline;
88 function getActive (): Boolean; inline;
90 function getCanFocus (): Boolean; inline;
92 function isMyChild (ctl
: TUIControl
): Boolean;
94 function findFirstFocus (): TUIControl
;
95 function findLastFocus (): TUIControl
;
97 function findNextFocus (cur
: TUIControl
; wrap
: Boolean): TUIControl
;
98 function findPrevFocus (cur
: TUIControl
; wrap
: Boolean): TUIControl
;
100 function findCancelControl (): TUIControl
;
101 function findDefaulControl (): TUIControl
;
103 function findControlById (const aid
: AnsiString): TUIControl
;
105 procedure activated (); virtual;
106 procedure blurred (); virtual;
108 procedure calcFullClientSize ();
110 procedure drawFrame (gx
, gy
, resx
, thalign
: Integer; const text: AnsiString; dbl
: Boolean);
113 var savedClip
: TGxRect
; // valid only in `draw*()` calls
114 //WARNING! do not call scissor functions outside `.draw*()` API!
115 // set scissor to this rect (in local coords)
116 procedure setScissor (lx
, ly
, lw
, lh
: Integer); // valid only in `draw*()` calls
120 closeRequestCB
: TCloseRequestCB
;
123 mDefSize
: TLaySize
; // default size
124 mMaxSize
: TLaySize
; // maximum size
131 mLayDefSize
: TLaySize
;
132 mLayMaxSize
: TLaySize
;
138 // layouter interface
139 function getDefSize (): TLaySize
; inline; // default size; <0: use max size
140 //procedure setDefSize (const sz: TLaySize); inline; // default size; <0: use max size
141 function getMargins (): TLayMargins
; inline;
142 function getPadding (): TLaySize
; inline; // children padding (each non-first child will get this on left/top)
143 function getMaxSize (): TLaySize
; inline; // max size; <0: set to some huge value
144 //procedure setMaxSize (const sz: TLaySize); inline; // max size; <0: set to some huge value
145 function getFlex (): Integer; inline; // <=0: not flexible
146 function isHorizBox (): Boolean; inline; // horizontal layout for children?
147 function noPad (): Boolean; inline; // ignore padding in box direction for this control
148 function getAlign (): Integer; inline; // aligning in non-main direction: <0: left/up; 0: center; >0: right/down
149 function getExpand (): Boolean; inline; // expanding in non-main direction: `true` will ignore align and eat all available space
150 function getHGroup (): AnsiString; inline; // empty: not grouped
151 function getVGroup (): AnsiString; inline; // empty: not grouped
153 procedure setActualSizePos (constref apos
: TLayPos
; constref asize
: TLaySize
); inline;
155 procedure layPrepare (); virtual; // called before registering control in layouter
158 property flex
: Integer read mFlex write mFlex
;
159 property flDefaultSize
: TLaySize read mDefSize write mDefSize
;
160 property flMaxSize
: TLaySize read mMaxSize write mMaxSize
;
161 property flPadding
: TLaySize read mPadding write mPadding
;
162 property flHoriz
: Boolean read mHoriz write mHoriz
;
163 property flAlign
: Integer read mAlign write mAlign
;
164 property flExpand
: Boolean read mExpand write mExpand
;
165 property flHGroup
: AnsiString read mHGroup write mHGroup
;
166 property flVGroup
: AnsiString read mVGroup write mVGroup
;
167 property flNoPad
: Boolean read mNoPad write mNoPad
;
168 property fullSize
: TLaySize read mFullSize
;
171 function parsePos (par
: TTextParser
): TLayPos
;
172 function parseSize (par
: TTextParser
): TLaySize
;
173 function parsePadding (par
: TTextParser
): TLaySize
;
174 function parseHPadding (par
: TTextParser
; def
: Integer): TLaySize
;
175 function parseVPadding (par
: TTextParser
; def
: Integer): TLaySize
;
176 function parseBool (par
: TTextParser
): Boolean;
177 function parseAnyAlign (par
: TTextParser
): Integer;
178 function parseHAlign (par
: TTextParser
): Integer;
179 function parseVAlign (par
: TTextParser
): Integer;
180 function parseOrientation (const prname
: AnsiString; par
: TTextParser
): Boolean;
181 procedure parseTextAlign (par
: TTextParser
; var h
, v
: Integer);
182 procedure parseChildren (par
: TTextParser
); // par should be on '{'; final '}' is eaten
185 // par is on property data
186 // there may be more data in text stream, don't eat it!
187 // return `true` if property name is valid and value was parsed
188 // return `false` if property name is invalid; don't advance parser in this case
189 // throw on property data errors
190 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; virtual;
192 // par should be on '{'; final '}' is eaten
193 procedure parseProperties (par
: TTextParser
);
196 constructor Create ();
197 destructor Destroy (); override;
199 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
201 // `sx` and `sy` are screen coordinates
202 procedure drawControl (gx
, gy
: Integer); virtual;
204 // called after all children drawn
205 procedure drawControlPost (gx
, gy
: Integer); virtual;
207 procedure draw (); virtual;
209 function topLevel (): TUIControl
; inline;
211 // returns `true` if global coords are inside this control
212 function toLocal (var x
, y
: Integer): Boolean;
213 function toLocal (gx
, gy
: Integer; out x
, y
: Integer): Boolean; inline;
214 procedure toGlobal (var x
, y
: Integer);
215 procedure toGlobal (lx
, ly
: Integer; out x
, y
: Integer); inline;
217 procedure getDrawRect (out gx
, gy
, wdt
, hgt
: Integer);
219 // x and y are global coords
220 function controlAtXY (x
, y
: Integer; allowDisabled
: Boolean=false): TUIControl
;
222 function parentScrollX (): Integer; inline;
223 function parentScrollY (): Integer; inline;
225 procedure makeVisibleInParent ();
227 procedure doAction (); virtual; // so user controls can override it
229 procedure mouseEvent (var ev
: THMouseEvent
); virtual; // returns `true` if event was eaten
230 procedure keyEvent (var ev
: THKeyEvent
); virtual; // returns `true` if event was eaten
231 procedure keyEventPre (var ev
: THKeyEvent
); virtual; // will be called before dispatching the event
232 procedure keyEventPost (var ev
: THKeyEvent
); virtual; // will be called after if nobody processed the event
234 function prevSibling (): TUIControl
;
235 function nextSibling (): TUIControl
;
236 function firstChild (): TUIControl
; inline;
237 function lastChild (): TUIControl
; inline;
239 procedure appendChild (ctl
: TUIControl
); virtual;
241 function setActionCBFor (const aid
: AnsiString; cb
: TActionCB
): TActionCB
; // returns previous cb
243 function forEachChildren (cb
: TCtlEnumCB
): TUIControl
; // doesn't recurse
244 function forEachControl (cb
: TCtlEnumCB
; includeSelf
: Boolean=true): TUIControl
;
246 procedure close (); // this closes *top-level* control
249 property id
: AnsiString read mId write mId
;
250 property styleId
: AnsiString read mStyleId
;
251 property scrollX
: Integer read mScrollX write mScrollX
;
252 property scrollY
: Integer read mScrollY write mScrollY
;
253 property x0
: Integer read mX write mX
;
254 property y0
: Integer read mY write mY
;
255 property width
: Integer read mWidth write mWidth
;
256 property height
: Integer read mHeight write mHeight
;
257 property enabled
: Boolean read getEnabled write setEnabled
;
258 property parent
: TUIControl read mParent
;
259 property focused
: Boolean read getFocused write setFocused
;
260 property active
: Boolean read getActive
;
261 property escClose
: Boolean read mEscClose write mEscClose
;
262 property cancel
: Boolean read mCancel write mCancel
;
263 property defctl
: Boolean read mDefault write mDefault
;
264 property canFocus
: Boolean read getCanFocus write mCanFocus
;
265 property ctlById
[const aid
: AnsiString]: TUIControl read findControlById
; default
;
269 TUITopWindow
= class(TUIControl
)
271 type TXMode
= (None
, Drag
, Scroll
);
276 mDragStartX
, mDragStartY
: Integer;
277 mWaitingClose
: Boolean;
279 mFreeOnClose
: Boolean; // default: false
280 mDoCenter
: Boolean; // after layouting
281 mFitToScreen
: Boolean;
284 procedure activated (); override;
285 procedure blurred (); override;
288 closeCB
: TActionCB
; // called after window was removed from ui window list
291 constructor Create (const atitle
: AnsiString);
293 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
295 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
297 procedure flFitToScreen (); // call this before layouting
299 procedure centerInScreen ();
301 // `sx` and `sy` are screen coordinates
302 procedure drawControl (gx
, gy
: Integer); override;
303 procedure drawControlPost (gx
, gy
: Integer); override;
305 procedure keyEvent (var ev
: THKeyEvent
); override; // returns `true` if event was eaten
306 procedure mouseEvent (var ev
: THMouseEvent
); override; // returns `true` if event was eaten
309 property freeOnClose
: Boolean read mFreeOnClose write mFreeOnClose
;
310 property fitToScreen
: Boolean read mFitToScreen write mFitToScreen
;
313 // ////////////////////////////////////////////////////////////////////// //
314 TUIBox
= class(TUIControl
)
317 mCaption
: AnsiString;
318 mHAlign
: Integer; // -1: left; 0: center; 1: right; default: left
321 procedure setCaption (const acap
: AnsiString);
322 procedure setHasFrame (v
: Boolean);
325 constructor Create (ahoriz
: Boolean);
327 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
329 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
331 procedure drawControl (gx
, gy
: Integer); override;
333 procedure mouseEvent (var ev
: THMouseEvent
); override;
334 procedure keyEvent (var ev
: THKeyEvent
); override;
337 property caption
: AnsiString read mCaption write setCaption
;
338 property hasFrame
: Boolean read mHasFrame write setHasFrame
;
339 property captionAlign
: Integer read mHAlign write mHAlign
;
342 TUIHBox
= class(TUIBox
)
344 constructor Create ();
346 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
349 TUIVBox
= class(TUIBox
)
351 constructor Create ();
353 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
356 // ////////////////////////////////////////////////////////////////////// //
357 TUISpan
= class(TUIControl
)
359 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
361 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
363 procedure drawControl (gx
, gy
: Integer); override;
366 // ////////////////////////////////////////////////////////////////////// //
367 TUILine
= class(TUIControl
)
369 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
371 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
373 procedure layPrepare (); override; // called before registering control in layouter
375 procedure drawControl (gx
, gy
: Integer); override;
378 // ////////////////////////////////////////////////////////////////////// //
379 TUIStaticText
= class(TUIControl
)
382 mHAlign
: Integer; // -1: left; 0: center; 1: right; default: left
383 mVAlign
: Integer; // -1: top; 0: center; 1: bottom; default: center
384 mHeader
: Boolean; // true: draw with frame text color
385 mLine
: Boolean; // true: draw horizontal line
388 procedure setText (const atext
: AnsiString);
391 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
393 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
395 procedure drawControl (gx
, gy
: Integer); override;
398 property text: AnsiString read mText write setText
;
399 property halign
: Integer read mHAlign write mHAlign
;
400 property valign
: Integer read mVAlign write mVAlign
;
401 property header
: Boolean read mHeader write mHeader
;
402 property line
: Boolean read mLine write mLine
;
405 // ////////////////////////////////////////////////////////////////////// //
406 TUITextLabel
= class(TUIControl
)
409 mHAlign
: Integer; // -1: left; 0: center; 1: right; default: left
410 mVAlign
: Integer; // -1: top; 0: center; 1: bottom; default: center
412 mHotOfs
: Integer; // from text start, in pixels
413 mHotColor
: array[0..ClrIdxMax
] of TGxRGBA
;
414 mLinkId
: AnsiString; // linked control
417 procedure cacheStyle (root
: TUIStyle
); override;
419 procedure setText (const s
: AnsiString); virtual;
422 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
424 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
426 procedure doAction (); override;
428 procedure drawControl (gx
, gy
: Integer); override;
430 procedure mouseEvent (var ev
: THMouseEvent
); override;
431 procedure keyEventPost (var ev
: THKeyEvent
); override;
434 property text: AnsiString read mText write setText
;
435 property halign
: Integer read mHAlign write mHAlign
;
436 property valign
: Integer read mVAlign write mVAlign
;
439 // ////////////////////////////////////////////////////////////////////// //
440 TUIButton
= class(TUITextLabel
)
442 mSkipLayPrepare
: Boolean;
443 mShadowSize
: Integer;
444 mAddMarkers
: Boolean;
445 mHideMarkers
: Boolean;
449 procedure setText (const s
: AnsiString); override;
451 procedure cacheStyle (root
: TUIStyle
); override;
454 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
456 procedure layPrepare (); override; // called before registering control in layouter
458 procedure drawControl (gx
, gy
: Integer); override;
460 procedure mouseEvent (var ev
: THMouseEvent
); override;
461 procedure keyEvent (var ev
: THKeyEvent
); override;
464 // ////////////////////////////////////////////////////////////////////// //
465 TUIButtonRound
= class(TUIButton
)
467 procedure setText (const s
: AnsiString); override;
470 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
472 procedure layPrepare (); override; // called before registering control in layouter
474 procedure drawControl (gx
, gy
: Integer); override;
477 // ////////////////////////////////////////////////////////////////////// //
478 TUISwitchBox
= class(TUITextLabel
)
482 mIcon
: TGxContext
.TMarkIcon
;
483 mSwitchColor
: array[0..ClrIdxMax
] of TGxRGBA
;
486 procedure cacheStyle (root
: TUIStyle
); override;
488 procedure setText (const s
: AnsiString); override;
490 function getChecked (): Boolean; virtual;
491 procedure setChecked (v
: Boolean); virtual; abstract;
494 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
496 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
498 procedure drawControl (gx
, gy
: Integer); override;
500 procedure mouseEvent (var ev
: THMouseEvent
); override;
501 procedure keyEvent (var ev
: THKeyEvent
); override;
503 procedure setVar (pvar
: PBoolean);
506 property checked
: Boolean read getChecked write setChecked
;
509 TUICheckBox
= class(TUISwitchBox
)
511 procedure setChecked (v
: Boolean); override;
514 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
516 procedure doAction (); override;
519 TUIRadioBox
= class(TUISwitchBox
)
521 mRadioGroup
: AnsiString;
524 procedure setChecked (v
: Boolean); override;
527 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
529 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
531 procedure doAction (); override;
534 property radioGroup
: AnsiString read mRadioGroup write mRadioGroup
; //FIXME
538 // ////////////////////////////////////////////////////////////////////////// //
539 procedure uiMouseEvent (var evt
: THMouseEvent
);
540 procedure uiKeyEvent (var evt
: THKeyEvent
);
544 // ////////////////////////////////////////////////////////////////////////// //
545 procedure uiAddWindow (ctl
: TUIControl
);
546 procedure uiRemoveWindow (ctl
: TUIControl
); // will free window if `mFreeOnClose` is `true`
547 function uiVisibleWindow (ctl
: TUIControl
): Boolean;
549 procedure uiUpdateStyles ();
552 // ////////////////////////////////////////////////////////////////////////// //
554 procedure uiLayoutCtl (ctl
: TUIControl
);
557 // ////////////////////////////////////////////////////////////////////////// //
559 fuiRenderScale
: Single = 1.0;
560 uiContext
: TGxContext
= nil;
570 // ////////////////////////////////////////////////////////////////////////// //
572 ctlsToKill
: array of TUIControl
= nil;
575 procedure scheduleKill (ctl
: TUIControl
);
579 if (ctl
= nil) then exit
;
581 for f
:= 0 to High(ctlsToKill
) do
583 if (ctlsToKill
[f
] = ctl
) then exit
;
584 if (ctlsToKill
[f
] = nil) then begin ctlsToKill
[f
] := ctl
; exit
; end;
586 SetLength(ctlsToKill
, Length(ctlsToKill
)+1);
587 ctlsToKill
[High(ctlsToKill
)] := ctl
;
591 procedure processKills ();
596 for f
:= 0 to High(ctlsToKill
) do
598 ctl
:= ctlsToKill
[f
];
599 if (ctl
= nil) then break
;
600 ctlsToKill
[f
] := nil;
603 if (Length(ctlsToKill
) > 0) then ctlsToKill
[0] := nil; // just in case
607 // ////////////////////////////////////////////////////////////////////////// //
609 knownCtlClasses
: array of record
610 klass
: TUIControlClass
;
615 procedure registerCtlClass (aklass
: TUIControlClass
; const aname
: AnsiString);
617 assert(aklass
<> nil);
618 assert(Length(aname
) > 0);
619 SetLength(knownCtlClasses
, Length(knownCtlClasses
)+1);
620 knownCtlClasses
[High(knownCtlClasses
)].klass
:= aklass
;
621 knownCtlClasses
[High(knownCtlClasses
)].name
:= aname
;
625 function findCtlClass (const aname
: AnsiString): TUIControlClass
;
629 for f
:= 0 to High(knownCtlClasses
) do
631 if (strEquCI1251(aname
, knownCtlClasses
[f
].name
)) then
633 result
:= knownCtlClasses
[f
].klass
;
641 // ////////////////////////////////////////////////////////////////////////// //
643 TFlexLayouter
= specialize TFlexLayouterBase
<TUIControl
>;
645 procedure uiLayoutCtl (ctl
: TUIControl
);
649 if (ctl
= nil) then exit
;
650 lay
:= TFlexLayouter
.Create();
652 if (not ctl
.mStyleLoaded
) then ctl
.updateStyle();
653 if (ctl
is TUITopWindow
) and (TUITopWindow(ctl
).fitToScreen
) then TUITopWindow(ctl
).flFitToScreen();
658 //writeln('============================'); lay.dumpFlat();
660 //writeln('=== initial ==='); lay.dump();
662 //lay.calcMaxSizeInternal(0);
665 writeln('=== after first pass ===');
669 writeln('=== after second pass ===');
674 //writeln('=== final ==='); lay.dump();
676 if (ctl
.mParent
= nil) and (ctl
is TUITopWindow
) and (TUITopWindow(ctl
).mDoCenter
) then
678 TUITopWindow(ctl
).centerInScreen();
681 // calculate full size
682 ctl
.calcFullClientSize();
685 if (ctl
.mParent
= nil) then
687 if (ctl
.mFocused
= nil) or (ctl
.mFocused
= ctl
) or (not ctl
.mFocused
.enabled
) then
689 ctl
.mFocused
:= ctl
.findFirstFocus();
699 // ////////////////////////////////////////////////////////////////////////// //
701 uiTopList
: array of TUIControl
= nil;
702 uiGrabCtl
: TUIControl
= nil;
705 procedure uiUpdateStyles ();
709 for ctl
in uiTopList
do ctl
.updateStyle();
713 procedure uiMouseEvent (var evt
: THMouseEvent
);
721 if (evt
.eaten
) or (evt
.cancelled
) then exit
;
723 ev
.x
:= trunc(ev
.x
/fuiRenderScale
);
724 ev
.y
:= trunc(ev
.y
/fuiRenderScale
);
725 ev
.dx
:= trunc(ev
.dx
/fuiRenderScale
); //FIXME
726 ev
.dy
:= trunc(ev
.dy
/fuiRenderScale
); //FIXME
728 if (uiGrabCtl
<> nil) then
730 uiGrabCtl
.mouseEvent(ev
);
731 if (ev
.release
) and ((ev
.bstate
and (not ev
.but
)) = 0) then uiGrabCtl
:= nil;
735 if (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)].enabled
) then uiTopList
[High(uiTopList
)].mouseEvent(ev
);
736 if (not ev
.eaten
) and (not ev
.cancelled
) and (ev
.press
) then
738 for f
:= High(uiTopList
) downto 0 do
740 if uiTopList
[f
].toLocal(ev
.x
, ev
.y
, lx
, ly
) then
742 if (uiTopList
[f
].enabled
) and (f
<> High(uiTopList
)) then
744 uiTopList
[High(uiTopList
)].blurred();
745 ctmp
:= uiTopList
[f
];
747 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
748 uiTopList
[High(uiTopList
)] := ctmp
;
758 if (ev
.eaten
) then evt
.eat();
759 if (ev
.cancelled
) then evt
.cancel();
764 procedure uiKeyEvent (var evt
: THKeyEvent
);
769 if (evt
.eaten
) or (evt
.cancelled
) then exit
;
771 ev
.x
:= trunc(ev
.x
/fuiRenderScale
);
772 ev
.y
:= trunc(ev
.y
/fuiRenderScale
);
774 if (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)].enabled
) then uiTopList
[High(uiTopList
)].keyEvent(ev
);
775 //if (ev.release) then begin ev.eat(); exit; end;
777 if (ev
.eaten
) then evt
.eat();
778 if (ev
.cancelled
) then evt
.cancel();
789 //if (uiContext = nil) then uiContext := TGxContext.Create();
790 gxSetContext(uiContext
, fuiRenderScale
);
791 uiContext
.resetClip();
793 for f
:= 0 to High(uiTopList
) do
797 if (f
<> High(uiTopList
)) then
799 cidx
:= ctl
.getColorIndex
;
800 uiContext
.darkenRect(ctl
.x0
, ctl
.y0
, ctl
.width
, ctl
.height
, ctl
.mDarken
[cidx
]);
809 procedure uiAddWindow (ctl
: TUIControl
);
813 if (ctl
= nil) then exit
;
815 if not (ctl
is TUITopWindow
) then exit
; // alas
816 for f
:= 0 to High(uiTopList
) do
818 if (uiTopList
[f
] = ctl
) then
820 if (f
<> High(uiTopList
)) then
822 uiTopList
[High(uiTopList
)].blurred();
823 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
824 uiTopList
[High(uiTopList
)] := ctl
;
830 if (Length(uiTopList
) > 0) then uiTopList
[High(uiTopList
)].blurred();
831 SetLength(uiTopList
, Length(uiTopList
)+1);
832 uiTopList
[High(uiTopList
)] := ctl
;
833 if (not ctl
.mStyleLoaded
) then ctl
.updateStyle();
838 procedure uiRemoveWindow (ctl
: TUIControl
);
842 if (ctl
= nil) then exit
;
844 if not (ctl
is TUITopWindow
) then exit
; // alas
845 for f
:= 0 to High(uiTopList
) do
847 if (uiTopList
[f
] = ctl
) then
850 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
851 SetLength(uiTopList
, Length(uiTopList
)-1);
852 if (ctl
is TUITopWindow
) then
855 if assigned(TUITopWindow(ctl
).closeCB
) then TUITopWindow(ctl
).closeCB(ctl
);
857 if (TUITopWindow(ctl
).mFreeOnClose
) then scheduleKill(ctl
);
866 function uiVisibleWindow (ctl
: TUIControl
): Boolean;
871 if (ctl
= nil) then exit
;
873 if not (ctl
is TUITopWindow
) then exit
; // alas
874 for f
:= 0 to High(uiTopList
) do
876 if (uiTopList
[f
] = ctl
) then begin result
:= true; exit
; end;
881 // ////////////////////////////////////////////////////////////////////////// //
882 constructor TUIControl
.Create ();
887 procedure TUIControl
.AfterConstruction ();
895 mHeight
:= uiContext
.charHeight(' ');
903 mDrawShadow
:= false;
905 // layouter interface
906 //mDefSize := TLaySize.Create(64, uiContext.charHeight(' ')); // default size
907 mDefSize
:= TLaySize
.Create(0, 0); // default size: hidden control
908 mMaxSize
:= TLaySize
.Create(-1, -1); // maximum size
909 mPadding
:= TLaySize
.Create(0, 0);
917 mAlign
:= -1; // left/top
919 mStyleLoaded
:= false;
923 destructor TUIControl
.Destroy ();
927 if (mParent
<> nil) then
930 for f
:= 0 to High(mParent
.mChildren
) do
932 if (mParent
.mChildren
[f
] = self
) then
934 for c
:= f
+1 to High(mParent
.mChildren
) do mParent
.mChildren
[c
-1] := mParent
.mChildren
[c
];
935 SetLength(mParent
.mChildren
, Length(mParent
.mChildren
)-1);
939 for f
:= 0 to High(mChildren
) do
941 mChildren
[f
].mParent
:= nil;
948 function TUIControl
.getColorIndex (): Integer; inline;
950 if (not enabled
) then begin result
:= ClrIdxDisabled
; exit
; end;
951 // top windows: no focus hack
952 if (self
is TUITopWindow
) then
954 if (getActive
) then begin result
:= ClrIdxActive
; exit
; end;
958 // if control cannot be focused, take "active" color scheme for it (it is easier this way)
959 if (not canFocus
) or (getActive
) then begin result
:= ClrIdxActive
; exit
; end;
961 result
:= ClrIdxInactive
;
964 procedure TUIControl
.updateStyle ();
970 while (ctl
<> nil) do
972 if (Length(ctl
.mStyleId
) <> 0) then begin stl
:= uiFindStyle(ctl
.mStyleId
); break
; end;
975 if (stl
= nil) then stl
:= uiFindStyle(''); // default
977 for ctl
in mChildren
do ctl
.updateStyle();
978 mStyleLoaded
:= true;
981 procedure TUIControl
.cacheStyle (root
: TUIStyle
);
985 //writeln('caching style for <', className, '> (', mCtl4Style, ')...');
988 mBackColor
[ClrIdxActive
] := root
.get('back-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(0, 0, 128));
989 mTextColor
[ClrIdxActive
] := root
.get('text-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
990 mFrameColor
[ClrIdxActive
] := root
.get('frame-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
991 mFrameTextColor
[ClrIdxActive
] := root
.get('frame-text-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
992 mFrameIconColor
[ClrIdxActive
] := root
.get('frame-icon-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(0, 255, 0));
993 mDarken
[ClrIdxActive
] := root
.get('darken', 'active', cst
).asInt(666);
995 mBackColor
[ClrIdxDisabled
] := root
.get('back-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(0, 0, 128));
996 mTextColor
[ClrIdxDisabled
] := root
.get('text-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(127, 127, 127));
997 mFrameColor
[ClrIdxDisabled
] := root
.get('frame-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(127, 127, 127));
998 mFrameTextColor
[ClrIdxDisabled
] := root
.get('frame-text-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(127, 127, 127));
999 mFrameIconColor
[ClrIdxDisabled
] := root
.get('frame-icon-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(0, 127, 0));
1000 mDarken
[ClrIdxDisabled
] := root
.get('darken', 'disabled', cst
).asInt(666);
1002 mBackColor
[ClrIdxInactive
] := root
.get('back-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(0, 0, 128));
1003 mTextColor
[ClrIdxInactive
] := root
.get('text-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
1004 mFrameColor
[ClrIdxInactive
] := root
.get('frame-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
1005 mFrameTextColor
[ClrIdxInactive
] := root
.get('frame-text-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
1006 mFrameIconColor
[ClrIdxInactive
] := root
.get('frame-icon-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(0, 255, 0));
1007 mDarken
[ClrIdxInactive
] := root
.get('darken', 'inactive', cst
).asInt(666);
1011 // ////////////////////////////////////////////////////////////////////////// //
1012 function TUIControl
.getDefSize (): TLaySize
; inline; begin result
:= mLayDefSize
; end;
1013 function TUIControl
.getMaxSize (): TLaySize
; inline; begin result
:= mLayMaxSize
; end;
1014 function TUIControl
.getPadding (): TLaySize
; inline; begin result
:= mPadding
; end;
1015 function TUIControl
.getFlex (): Integer; inline; begin result
:= mFlex
; end;
1016 function TUIControl
.isHorizBox (): Boolean; inline; begin result
:= mHoriz
; end;
1017 function TUIControl
.noPad (): Boolean; inline; begin result
:= mNoPad
; end;
1018 function TUIControl
.getAlign (): Integer; inline; begin result
:= mAlign
; end;
1019 function TUIControl
.getExpand (): Boolean; inline; begin result
:= mExpand
; end;
1020 function TUIControl
.getHGroup (): AnsiString; inline; begin result
:= mHGroup
; end;
1021 function TUIControl
.getVGroup (): AnsiString; inline; begin result
:= mVGroup
; end;
1022 function TUIControl
.getMargins (): TLayMargins
; inline; begin result
:= TLayMargins
.Create(mFrameHeight
, mFrameWidth
, mFrameHeight
, mFrameWidth
); end;
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
1034 if (mLayMaxSize
.w
>= 0) then mWidth
:= nmin(mWidth
, mLayMaxSize
.w
);
1035 if (mLayMaxSize
.h
>= 0) then mHeight
:= nmin(mHeight
, mLayMaxSize
.h
);
1038 procedure TUIControl
.layPrepare ();
1040 mLayDefSize
:= mDefSize
;
1041 if (mLayDefSize
.w
<> 0) or (mLayDefSize
.h
<> 0) then
1043 mLayMaxSize
:= mMaxSize
;
1044 if (mLayMaxSize
.w
>= 0) then begin mLayDefSize
.w
+= mFrameWidth
*2; mLayMaxSize
.w
+= mFrameWidth
*2; end;
1045 if (mLayMaxSize
.h
>= 0) then begin mLayDefSize
.h
+= mFrameHeight
*2; mLayMaxSize
.h
+= mFrameHeight
*2; end;
1049 mLayMaxSize
:= TLaySize
.Create(0, 0);
1054 // ////////////////////////////////////////////////////////////////////////// //
1055 function TUIControl
.parsePos (par
: TTextParser
): TLayPos
;
1057 ech
: AnsiChar = ')';
1059 if (par
.eatDelim('[')) then ech
:= ']' else par
.expectDelim('(');
1060 result
.x
:= par
.expectInt();
1061 par
.eatDelim(','); // optional comma
1062 result
.y
:= par
.expectInt();
1063 par
.eatDelim(','); // optional comma
1064 par
.expectDelim(ech
);
1067 function TUIControl
.parseSize (par
: TTextParser
): TLaySize
;
1069 ech
: AnsiChar = ')';
1071 if (par
.eatDelim('[')) then ech
:= ']' else par
.expectDelim('(');
1072 result
.w
:= par
.expectInt();
1073 par
.eatDelim(','); // optional comma
1074 result
.h
:= par
.expectInt();
1075 par
.eatDelim(','); // optional comma
1076 par
.expectDelim(ech
);
1079 function TUIControl
.parsePadding (par
: TTextParser
): TLaySize
;
1081 result
:= parseSize(par
);
1084 function TUIControl
.parseHPadding (par
: TTextParser
; def
: Integer): TLaySize
;
1089 result
.w
:= par
.expectInt();
1093 result
:= parsePadding(par
);
1097 function TUIControl
.parseVPadding (par
: TTextParser
; def
: Integer): TLaySize
;
1102 result
.h
:= par
.expectInt();
1106 result
:= parsePadding(par
);
1110 function TUIControl
.parseBool (par
: TTextParser
): Boolean;
1113 par
.eatIdOrStrCI('true') or
1114 par
.eatIdOrStrCI('yes') or
1115 par
.eatIdOrStrCI('tan');
1118 if (not par
.eatIdOrStrCI('false')) and (not par
.eatIdOrStrCI('no')) and (not par
.eatIdOrStrCI('ona')) then
1120 par
.error('boolean value expected');
1125 function TUIControl
.parseAnyAlign (par
: TTextParser
): Integer;
1127 if (par
.eatIdOrStrCI('left')) or (par
.eatIdOrStrCI('top')) then result
:= -1
1128 else if (par
.eatIdOrStrCI('right')) or (par
.eatIdOrStrCI('bottom')) then result
:= 1
1129 else if (par
.eatIdOrStrCI('center')) then result
:= 0
1130 else par
.error('invalid align value');
1133 function TUIControl
.parseHAlign (par
: TTextParser
): Integer;
1135 if (par
.eatIdOrStrCI('left')) then result
:= -1
1136 else if (par
.eatIdOrStrCI('right')) then result
:= 1
1137 else if (par
.eatIdOrStrCI('center')) then result
:= 0
1138 else par
.error('invalid horizontal align value');
1141 function TUIControl
.parseVAlign (par
: TTextParser
): Integer;
1143 if (par
.eatIdOrStrCI('top')) then result
:= -1
1144 else if (par
.eatIdOrStrCI('bottom')) then result
:= 1
1145 else if (par
.eatIdOrStrCI('center')) then result
:= 0
1146 else par
.error('invalid vertical align value');
1149 procedure TUIControl
.parseTextAlign (par
: TTextParser
; var h
, v
: Integer);
1151 wasH
: Boolean = false;
1152 wasV
: Boolean = false;
1156 if (par
.eatIdOrStrCI('left')) then
1158 if wasH
then par
.error('too many align directives');
1163 if (par
.eatIdOrStrCI('right')) then
1165 if wasH
then par
.error('too many align directives');
1170 if (par
.eatIdOrStrCI('hcenter')) then
1172 if wasH
then par
.error('too many align directives');
1177 if (par
.eatIdOrStrCI('top')) then
1179 if wasV
then par
.error('too many align directives');
1184 if (par
.eatIdOrStrCI('bottom')) then
1186 if wasV
then par
.error('too many align directives');
1191 if (par
.eatIdOrStrCI('vcenter')) then
1193 if wasV
then par
.error('too many align directives');
1198 if (par
.eatIdOrStrCI('center')) then
1200 if wasV
or wasH
then par
.error('too many align directives');
1209 if not wasV
and not wasH
then par
.error('invalid align value');
1212 function TUIControl
.parseOrientation (const prname
: AnsiString; par
: TTextParser
): Boolean;
1214 if (strEquCI1251(prname
, 'orientation')) or (strEquCI1251(prname
, 'orient')) then
1216 if (par
.eatIdOrStrCI('horizontal')) or (par
.eatIdOrStrCI('horiz')) then mHoriz
:= true
1217 else if (par
.eatIdOrStrCI('vertical')) or (par
.eatIdOrStrCI('vert')) then mHoriz
:= false
1218 else par
.error('`horizontal` or `vertical` expected');
1227 // par should be on '{'; final '}' is eaten
1228 procedure TUIControl
.parseProperties (par
: TTextParser
);
1232 if (not par
.eatDelim('{')) then exit
;
1233 while (not par
.eatDelim('}')) do
1235 if (not par
.isIdOrStr
) then par
.error('property name expected');
1238 par
.eatDelim(':'); // optional
1239 if not parseProperty(pn
, par
) then par
.errorfmt('invalid property name ''%s''', [pn
]);
1240 par
.eatDelim(','); // optional
1244 // par should be on '{'
1245 procedure TUIControl
.parseChildren (par
: TTextParser
);
1247 cc
: TUIControlClass
;
1250 par
.expectDelim('{');
1251 while (not par
.eatDelim('}')) do
1253 if (not par
.isIdOrStr
) then par
.error('control name expected');
1254 cc
:= findCtlClass(par
.tokStr
);
1255 if (cc
= nil) then par
.errorfmt('unknown control name: ''%s''', [par
.tokStr
]);
1256 //writeln('children for <', par.tokStr, '>: <', cc.className, '>');
1258 par
.eatDelim(':'); // optional
1260 //writeln(' mHoriz=', ctl.mHoriz);
1262 ctl
.parseProperties(par
);
1267 //writeln(': ', ctl.mDefSize.toString);
1269 par
.eatDelim(','); // optional
1274 function TUIControl
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1277 if (strEquCI1251(prname
, 'id')) then begin mId
:= par
.expectIdOrStr(true); exit
; end; // allow empty strings
1278 if (strEquCI1251(prname
, 'style')) then begin mStyleId
:= par
.expectIdOrStr(); exit
; end; // no empty strings
1279 if (strEquCI1251(prname
, 'flex')) then begin flex
:= par
.expectInt(); exit
; end;
1281 if (strEquCI1251(prname
, 'defsize')) or (strEquCI1251(prname
, 'size')) then begin mDefSize
:= parseSize(par
); exit
; end;
1282 if (strEquCI1251(prname
, 'maxsize')) then begin mMaxSize
:= parseSize(par
); exit
; end;
1283 if (strEquCI1251(prname
, 'defwidth')) or (strEquCI1251(prname
, 'width')) then begin mDefSize
.w
:= par
.expectInt(); exit
; end;
1284 if (strEquCI1251(prname
, 'defheight')) or (strEquCI1251(prname
, 'height')) then begin mDefSize
.h
:= par
.expectInt(); exit
; end;
1285 if (strEquCI1251(prname
, 'maxwidth')) then begin mMaxSize
.w
:= par
.expectInt(); exit
; end;
1286 if (strEquCI1251(prname
, 'maxheight')) then begin mMaxSize
.h
:= par
.expectInt(); exit
; end;
1288 if (strEquCI1251(prname
, 'padding')) then begin mPadding
:= parsePadding(par
); exit
; end;
1289 if (strEquCI1251(prname
, 'nopad')) then begin mNoPad
:= true; exit
; end;
1291 if (strEquCI1251(prname
, 'expand')) then begin mExpand
:= parseBool(par
); exit
; end;
1293 if (strEquCI1251(prname
, 'align')) then begin mAlign
:= parseAnyAlign(par
); exit
; end;
1294 if (strEquCI1251(prname
, 'hgroup')) then begin mHGroup
:= par
.expectIdOrStr(true); exit
; end; // allow empty strings
1295 if (strEquCI1251(prname
, 'vgroup')) then begin mVGroup
:= par
.expectIdOrStr(true); exit
; end; // allow empty strings
1297 if (strEquCI1251(prname
, 'canfocus')) then begin mCanFocus
:= true; exit
; end;
1298 if (strEquCI1251(prname
, 'nofocus')) then begin mCanFocus
:= false; exit
; end;
1299 if (strEquCI1251(prname
, 'disabled')) then begin mEnabled
:= false; exit
; end;
1300 if (strEquCI1251(prname
, 'enabled')) then begin mEnabled
:= true; exit
; end;
1301 if (strEquCI1251(prname
, 'escclose')) then begin mEscClose
:= not parseBool(par
); exit
; end;
1302 if (strEquCI1251(prname
, 'default')) then begin mDefault
:= true; exit
; end;
1303 if (strEquCI1251(prname
, 'cancel')) then begin mCancel
:= true; exit
; end;
1308 // ////////////////////////////////////////////////////////////////////////// //
1309 procedure TUIControl
.activated ();
1311 makeVisibleInParent();
1315 procedure TUIControl
.blurred ();
1317 if (uiGrabCtl
= self
) then uiGrabCtl
:= nil;
1321 procedure TUIControl
.calcFullClientSize ();
1325 mFullSize
:= TLaySize
.Create(0, 0);
1326 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1327 for ctl
in mChildren
do
1329 ctl
.calcFullClientSize();
1330 mFullSize
.w
:= nmax(mFullSize
.w
, ctl
.mX
-mFrameWidth
+ctl
.mFullSize
.w
);
1331 mFullSize
.h
:= nmax(mFullSize
.h
, ctl
.mY
-mFrameHeight
+ctl
.mFullSize
.h
);
1333 mFullSize
.w
:= nmax(mFullSize
.w
, mWidth
-mFrameWidth
*2);
1334 mFullSize
.h
:= nmax(mFullSize
.h
, mHeight
-mFrameHeight
*2);
1338 function TUIControl
.topLevel (): TUIControl
; inline;
1341 while (result
.mParent
<> nil) do result
:= result
.mParent
;
1345 function TUIControl
.getEnabled (): Boolean;
1350 if (not mEnabled
) then exit
;
1352 while (ctl
<> nil) do
1354 if (not ctl
.mEnabled
) then exit
;
1361 procedure TUIControl
.setEnabled (v
: Boolean); inline;
1363 if (mEnabled
= v
) then exit
;
1365 if (not v
) and focused
then setFocused(false);
1369 function TUIControl
.getFocused (): Boolean; inline;
1371 if (mParent
= nil) then
1373 result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = self
);
1377 result
:= (topLevel
.mFocused
= self
);
1378 if (result
) then result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = topLevel
);
1383 function TUIControl
.getActive (): Boolean; inline;
1387 if (mParent
= nil) then
1389 result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = self
);
1393 ctl
:= topLevel
.mFocused
;
1394 while (ctl
<> nil) and (ctl
<> self
) do ctl
:= ctl
.mParent
;
1395 result
:= (ctl
= self
);
1396 if (result
) then result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = topLevel
);
1401 procedure TUIControl
.setFocused (v
: Boolean); inline;
1408 if (tl
.mFocused
= self
) then
1410 blurred(); // this will reset grab, but still...
1411 if (uiGrabCtl
= self
) then uiGrabCtl
:= nil;
1412 tl
.mFocused
:= tl
.findNextFocus(self
, true);
1413 if (tl
.mFocused
= self
) then tl
.mFocused
:= nil;
1414 if (tl
.mFocused
<> nil) then tl
.mFocused
.activated();
1418 if (not canFocus
) then exit
;
1419 if (tl
.mFocused
<> self
) then
1421 if (tl
.mFocused
<> nil) then tl
.mFocused
.blurred();
1422 tl
.mFocused
:= self
;
1423 if (uiGrabCtl
<> self
) then uiGrabCtl
:= nil;
1429 function TUIControl
.getCanFocus (): Boolean; inline;
1431 result
:= (getEnabled
) and (mCanFocus
) and (mWidth
> 0) and (mHeight
> 0);
1435 function TUIControl
.isMyChild (ctl
: TUIControl
): Boolean;
1438 while (ctl
<> nil) do
1440 if (ctl
.mParent
= self
) then exit
;
1447 // returns `true` if global coords are inside this control
1448 function TUIControl
.toLocal (var x
, y
: Integer): Boolean;
1450 if (mParent
= nil) then
1454 result
:= true; // hack
1458 result
:= mParent
.toLocal(x
, y
);
1459 Inc(x
, mParent
.mScrollX
);
1460 Inc(y
, mParent
.mScrollY
);
1463 if result
then result
:= (x
>= 0) and (y
>= 0) and (x
< mParent
.mWidth
) and (y
< mParent
.mHeight
);
1465 if result
then result
:= (x
>= 0) and (y
>= 0) and (x
< mWidth
) and (y
< mHeight
);
1468 function TUIControl
.toLocal (gx
, gy
: Integer; out x
, y
: Integer): Boolean; inline;
1472 result
:= toLocal(x
, y
);
1476 procedure TUIControl
.toGlobal (var x
, y
: Integer);
1480 if (mParent
<> nil) then
1482 Dec(x
, mParent
.mScrollX
);
1483 Dec(y
, mParent
.mScrollY
);
1484 mParent
.toGlobal(x
, y
);
1488 procedure TUIControl
.toGlobal (lx
, ly
: Integer; out x
, y
: Integer); inline;
1495 procedure TUIControl
.getDrawRect (out gx
, gy
, wdt
, hgt
: Integer);
1499 if (mParent
= nil) then
1508 toGlobal(0, 0, cgx
, cgy
);
1509 mParent
.getDrawRect(gx
, gy
, wdt
, hgt
);
1510 if (wdt
> 0) and (hgt
> 0) then
1512 if (not intersectRect(gx
, gy
, wdt
, hgt
, cgx
, cgy
, mWidth
, mHeight
)) then
1522 // x and y are global coords
1523 function TUIControl
.controlAtXY (x
, y
: Integer; allowDisabled
: Boolean=false): TUIControl
;
1529 if (not allowDisabled
) and (not enabled
) then exit
;
1530 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1531 if not toLocal(x
, y
, lx
, ly
) then exit
;
1532 for f
:= High(mChildren
) downto 0 do
1534 result
:= mChildren
[f
].controlAtXY(x
, y
, allowDisabled
);
1535 if (result
<> nil) then exit
;
1541 function TUIControl
.parentScrollX (): Integer; inline; begin if (mParent
<> nil) then result
:= mParent
.mScrollX
else result
:= 0; end;
1542 function TUIControl
.parentScrollY (): Integer; inline; begin if (mParent
<> nil) then result
:= mParent
.mScrollY
else result
:= 0; end;
1545 procedure TUIControl
.makeVisibleInParent ();
1547 sy
, ey
, cy
: Integer;
1550 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1552 if (p
= nil) then exit
;
1553 if (p
.mFullSize
.w
< 1) or (p
.mFullSize
.h
< 1) then
1559 p
.makeVisibleInParent();
1560 cy
:= mY
-p
.mFrameHeight
;
1562 ey
:= sy
+(p
.mHeight
-p
.mFrameHeight
*2);
1565 p
.mScrollY
:= nmax(0, cy
);
1567 else if (cy
+mHeight
> ey
) then
1569 p
.mScrollY
:= nmax(0, cy
+mHeight
-(p
.mHeight
-p
.mFrameHeight
*2));
1574 // ////////////////////////////////////////////////////////////////////////// //
1575 function TUIControl
.prevSibling (): TUIControl
;
1579 if (mParent
<> nil) then
1581 for f
:= 1 to High(mParent
.mChildren
) do
1583 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
-1]; exit
; end;
1589 function TUIControl
.nextSibling (): TUIControl
;
1593 if (mParent
<> nil) then
1595 for f
:= 0 to High(mParent
.mChildren
)-1 do
1597 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
+1]; exit
; end;
1603 function TUIControl
.firstChild (): TUIControl
; inline;
1605 if (Length(mChildren
) <> 0) then result
:= mChildren
[0] else result
:= nil;
1608 function TUIControl
.lastChild (): TUIControl
; inline;
1610 if (Length(mChildren
) <> 0) then result
:= mChildren
[High(mChildren
)] else result
:= nil;
1614 function TUIControl
.findFirstFocus (): TUIControl
;
1621 for f
:= 0 to High(mChildren
) do
1623 result
:= mChildren
[f
].findFirstFocus();
1624 if (result
<> nil) then exit
;
1626 if (canFocus
) then result
:= self
;
1631 function TUIControl
.findLastFocus (): TUIControl
;
1638 for f
:= High(mChildren
) downto 0 do
1640 result
:= mChildren
[f
].findLastFocus();
1641 if (result
<> nil) then exit
;
1643 if (canFocus
) then result
:= self
;
1648 function TUIControl
.findNextFocus (cur
: TUIControl
; wrap
: Boolean): TUIControl
;
1650 curHit
: Boolean = false;
1652 function checkFocus (ctl
: TUIControl
): Boolean;
1656 result
:= (ctl
.canFocus
);
1660 curHit
:= (ctl
= cur
);
1661 result
:= false; // don't stop
1669 if not isMyChild(cur
) then
1671 result
:= findFirstFocus();
1675 result
:= forEachControl(checkFocus
);
1676 if (result
= nil) and (wrap
) then result
:= findFirstFocus();
1682 function TUIControl
.findPrevFocus (cur
: TUIControl
; wrap
: Boolean): TUIControl
;
1684 lastCtl
: TUIControl
= nil;
1686 function checkFocus (ctl
: TUIControl
): Boolean;
1695 if (ctl
.canFocus
) then lastCtl
:= ctl
;
1703 if not isMyChild(cur
) then
1705 result
:= findLastFocus();
1709 forEachControl(checkFocus
);
1710 if (lastCtl
= nil) and (wrap
) then lastCtl
:= findLastFocus();
1712 //if (lastCtl <> nil) then writeln('ctl<', lastCtl.className, '>: {', lastCtl.id, '}');
1718 function TUIControl
.findDefaulControl (): TUIControl
;
1724 if (mDefault
) then begin result
:= self
; exit
; end;
1725 for ctl
in mChildren
do
1727 result
:= ctl
.findDefaulControl();
1728 if (result
<> nil) then exit
;
1734 function TUIControl
.findCancelControl (): TUIControl
;
1740 if (mCancel
) then begin result
:= self
; exit
; end;
1741 for ctl
in mChildren
do
1743 result
:= ctl
.findCancelControl();
1744 if (result
<> nil) then exit
;
1751 function TUIControl
.findControlById (const aid
: AnsiString): TUIControl
;
1755 if (strEquCI1251(aid
, mId
)) then begin result
:= self
; exit
; end;
1756 for ctl
in mChildren
do
1758 result
:= ctl
.findControlById(aid
);
1759 if (result
<> nil) then exit
;
1765 procedure TUIControl
.appendChild (ctl
: TUIControl
);
1767 if (ctl
= nil) then exit
;
1768 if (ctl
.mParent
<> nil) then exit
;
1769 SetLength(mChildren
, Length(mChildren
)+1);
1770 mChildren
[High(mChildren
)] := ctl
;
1771 ctl
.mParent
:= self
;
1772 Inc(ctl
.mX
, mFrameWidth
);
1773 Inc(ctl
.mY
, mFrameHeight
);
1774 if (ctl
.mWidth
> 0) and (ctl
.mHeight
> 0) and
1775 (ctl
.mX
+ctl
.mWidth
> mFrameWidth
) and (ctl
.mY
+ctl
.mHeight
> mFrameHeight
) then
1777 if (mWidth
+mFrameWidth
< ctl
.mX
+ctl
.mWidth
) then mWidth
:= ctl
.mX
+ctl
.mWidth
+mFrameWidth
;
1778 if (mHeight
+mFrameHeight
< ctl
.mY
+ctl
.mHeight
) then mHeight
:= ctl
.mY
+ctl
.mHeight
+mFrameHeight
;
1783 function TUIControl
.setActionCBFor (const aid
: AnsiString; cb
: TActionCB
): TActionCB
;
1788 if (ctl
<> nil) then
1790 result
:= ctl
.actionCB
;
1800 function TUIControl
.forEachChildren (cb
: TCtlEnumCB
): TUIControl
;
1805 if (not assigned(cb
)) then exit
;
1806 for ctl
in mChildren
do
1808 if cb(ctl
) then begin result
:= ctl
; exit
; end;
1813 function TUIControl
.forEachControl (cb
: TCtlEnumCB
; includeSelf
: Boolean=true): TUIControl
;
1815 function forChildren (p
: TUIControl
; incSelf
: Boolean): TUIControl
;
1820 if (p
= nil) then exit
;
1821 if (incSelf
) and (cb(p
)) then begin result
:= p
; exit
; end;
1822 for ctl
in p
.mChildren
do
1824 result
:= forChildren(ctl
, true);
1825 if (result
<> nil) then break
;
1831 if (not assigned(cb
)) then exit
;
1832 result
:= forChildren(self
, includeSelf
);
1836 procedure TUIControl
.close (); // this closes *top-level* control
1841 uiRemoveWindow(ctl
);
1842 if (ctl
is TUITopWindow
) and (TUITopWindow(ctl
).mFreeOnClose
) then scheduleKill(ctl
); // just in case
1846 procedure TUIControl
.doAction ();
1848 if assigned(actionCB
) then actionCB(self
);
1852 // ////////////////////////////////////////////////////////////////////////// //
1853 procedure TUIControl
.setScissor (lx
, ly
, lw
, lh
: Integer);
1855 gx
, gy
, wdt
, hgt
, cgx
, cgy
: Integer;
1857 if (not intersectRect(lx
, ly
, lw
, lh
, 0, 0, mWidth
, mHeight
)) then
1859 uiContext
.clip
:= TGxRect
.Create(0, 0, 0, 0);
1863 getDrawRect(gx
, gy
, wdt
, hgt
);
1865 toGlobal(lx
, ly
, cgx
, cgy
);
1866 if (not intersectRect(gx
, gy
, wdt
, hgt
, cgx
, cgy
, lw
, lh
)) then
1868 uiContext
.clip
:= TGxRect
.Create(0, 0, 0, 0);
1872 uiContext
.clip
:= savedClip
;
1873 uiContext
.combineClip(TGxRect
.Create(gx
, gy
, wdt
, hgt
));
1874 //uiContext.clip := TGxRect.Create(gx, gy, wdt, hgt);
1879 // ////////////////////////////////////////////////////////////////////////// //
1880 procedure TUIControl
.drawFrame (gx
, gy
, resx
, thalign
: Integer; const text: AnsiString; dbl
: Boolean);
1882 cidx
, tx
, tw
: Integer;
1884 if (mFrameWidth
< 1) or (mFrameHeight
< 1) then exit
;
1885 cidx
:= getColorIndex
;
1886 uiContext
.color
:= mFrameColor
[cidx
];
1887 case mFrameHeight
of
1892 uiContext
.rect(gx
+3, gy
+3, mWidth
-6, mHeight
-6);
1893 uiContext
.rect(gx
+5, gy
+5, mWidth
-10, mHeight
-10);
1897 uiContext
.rect(gx
+4, gy
+4, mWidth
-8, mHeight
-8);
1904 uiContext
.rect(gx
+3, gy
+3+3, mWidth
-6, mHeight
-6-6);
1905 uiContext
.rect(gx
+5, gy
+5+3, mWidth
-10, mHeight
-10-6);
1909 uiContext
.rect(gx
+4, gy
+4+3, mWidth
-8, mHeight
-8-6);
1916 uiContext
.rect(gx
+3, gy
+3+4, mWidth
-6, mHeight
-6-8);
1917 uiContext
.rect(gx
+5, gy
+5+4, mWidth
-10, mHeight
-10-8);
1921 uiContext
.rect(gx
+4, gy
+4+4, mWidth
-8, mHeight
-8-8);
1937 if (Length(text) > 0) then
1939 if (resx
< 0) then resx
:= 0;
1940 tw
:= uiContext
.textWidth(text);
1941 setScissor(mFrameWidth
+resx
, 0, mWidth
-mFrameWidth
*2-resx
, mFrameHeight
);
1942 if (thalign
< 0) then tx
:= gx
+resx
+mFrameWidth
+2
1943 else if (thalign
> 0) then tx
:= gx
+mWidth
-mFrameWidth
-1-tw
1944 else tx
:= (gx
+resx
+mFrameWidth
)+(mWidth
-mFrameWidth
*2-resx
-tw
) div 2;
1945 uiContext
.color
:= mBackColor
[cidx
];
1946 uiContext
.fillRect(tx
-2, gy
, tw
+4, mFrameHeight
);
1947 uiContext
.color
:= mFrameTextColor
[cidx
];
1948 uiContext
.drawText(tx
, gy
, text);
1953 procedure TUIControl
.draw ();
1958 procedure resetScissor (fullArea
: Boolean); inline;
1960 uiContext
.clip
:= savedClip
;
1961 if (fullArea
) or ((mFrameWidth
= 0) and (mFrameHeight
= 0)) then
1963 setScissor(0, 0, mWidth
, mHeight
);
1967 //writeln('frm: (', mFrameWidth, 'x', mFrameHeight, ')');
1968 setScissor(mFrameWidth
, mFrameHeight
, mWidth
-mFrameWidth
*2, mHeight
-mFrameHeight
*2);
1973 if (mWidth
< 1) or (mHeight
< 1) or (uiContext
= nil) or (not uiContext
.active
) then exit
;
1974 toGlobal(0, 0, gx
, gy
);
1976 savedClip
:= uiContext
.clip
;
1978 resetScissor(true); // full area
1979 drawControl(gx
, gy
);
1980 resetScissor(false); // client area
1981 for f
:= 0 to High(mChildren
) do mChildren
[f
].draw();
1982 resetScissor(true); // full area
1983 if (self
is TUISwitchBox
) then
1985 uiContext
.color
:= TGxRGBA
.Create(255, 0, 0, 255);
1986 //uiContext.fillRect(gx, gy, mWidth, mHeight);
1987 //writeln('frm: (', mFrameWidth, 'x', mFrameHeight, '); sz=(', mWidth, 'x', mHeight, '); clip=', uiContext.clip.toString);
1989 if false and (mId
= 'cbtest') then
1991 uiContext
.color
:= TGxRGBA
.Create(255, 127, 0, 96);
1992 uiContext
.fillRect(gx
, gy
, mWidth
, mHeight
);
1993 if (mFrameWidth
> 0) and (mFrameHeight
> 0) then
1995 uiContext
.color
:= TGxRGBA
.Create(255, 255, 0, 96);
1996 uiContext
.fillRect(gx
+mFrameWidth
, gy
+mFrameHeight
, mWidth
-mFrameWidth
*2, mHeight
-mFrameHeight
*2);
1999 else if false and (self
is TUISwitchBox
) then
2001 uiContext
.color
:= TGxRGBA
.Create(255, 0, 0, 255);
2002 uiContext
.fillRect(gx
, gy
, mWidth
, mHeight
);
2003 //writeln('frm: (', mFrameWidth, 'x', mFrameHeight, ')');
2005 drawControlPost(gx
, gy
);
2007 uiContext
.clip
:= savedClip
;
2011 procedure TUIControl
.drawControl (gx
, gy
: Integer);
2013 //if (mParent = nil) then darkenRect(gx, gy, mWidth, mHeight, 64);
2016 procedure TUIControl
.drawControlPost (gx
, gy
: Integer);
2019 if (mParent
= nil) and (mDrawShadow
) and (mWidth
> 0) and (mHeight
> 0) then
2021 //setScissorGLInternal(gx+8, gy+8, mWidth, mHeight);
2022 uiContext
.resetClip();
2023 uiContext
.darkenRect(gx
+mWidth
, gy
+8, 8, mHeight
, 128);
2024 uiContext
.darkenRect(gx
+8, gy
+mHeight
, mWidth
-8, 8, 128);
2029 // ////////////////////////////////////////////////////////////////////////// //
2030 procedure TUIControl
.mouseEvent (var ev
: THMouseEvent
);
2034 if (not enabled
) then exit
;
2035 if (mWidth
< 1) or (mHeight
< 1) then exit
;
2036 ctl
:= controlAtXY(ev
.x
, ev
.y
);
2037 if (ctl
= nil) then exit
;
2038 if (ctl
.canFocus
) and (ev
.press
) then
2040 if (ctl
<> topLevel
.mFocused
) then ctl
.setFocused(true);
2043 if (ctl
<> self
) then ctl
.mouseEvent(ev
);
2048 procedure TUIControl
.keyEvent (var ev
: THKeyEvent
);
2050 function doPreKey (ctl
: TUIControl
): Boolean;
2052 if (not ctl
.enabled
) then begin result
:= false; exit
; end;
2053 ctl
.keyEventPre(ev
);
2054 result
:= (ev
.eaten
) or (ev
.cancelled
); // stop if event was consumed
2057 function doPostKey (ctl
: TUIControl
): Boolean;
2059 if (not ctl
.enabled
) then begin result
:= false; exit
; end;
2060 ctl
.keyEventPost(ev
);
2061 result
:= (ev
.eaten
) or (ev
.cancelled
); // stop if event was consumed
2067 if (not enabled
) then exit
;
2068 if (ev
.eaten
) or (ev
.cancelled
) then exit
;
2070 if (mParent
= nil) then
2072 forEachControl(doPreKey
);
2073 if (ev
.eaten
) or (ev
.cancelled
) then exit
;
2075 // focused control should process keyboard first
2076 if (topLevel
.mFocused
<> self
) and isMyChild(topLevel
.mFocused
) and (topLevel
.mFocused
.enabled
) then
2078 // bubble keyboard event
2079 ctl
:= topLevel
.mFocused
;
2080 while (ctl
<> nil) and (ctl
<> self
) do
2083 if (ev
.eaten
) or (ev
.cancelled
) then exit
;
2087 // for top-level controls
2088 if (mParent
= nil) then
2090 if (ev
= 'S-Tab') then
2092 ctl
:= findPrevFocus(mFocused
, true);
2093 if (ctl
<> nil) and (ctl
<> mFocused
) then ctl
.setFocused(true);
2097 if (ev
= 'Tab') then
2099 ctl
:= findNextFocus(mFocused
, true);
2100 if (ctl
<> nil) and (ctl
<> mFocused
) then ctl
.setFocused(true);
2104 if (ev
= 'Enter') or (ev
= 'C-Enter') then
2106 ctl
:= findDefaulControl();
2107 if (ctl
<> nil) then
2114 if (ev
= 'Escape') then
2116 ctl
:= findCancelControl();
2117 if (ctl
<> nil) then
2124 if mEscClose
and (ev
= 'Escape') then
2126 if (not assigned(closeRequestCB
)) or (closeRequestCB(self
)) then
2128 uiRemoveWindow(self
);
2134 if (ev
.eaten
) or (ev
.cancelled
) then exit
;
2135 forEachControl(doPostKey
);
2140 procedure TUIControl
.keyEventPre (var ev
: THKeyEvent
);
2145 procedure TUIControl
.keyEventPost (var ev
: THKeyEvent
);
2150 // ////////////////////////////////////////////////////////////////////////// //
2151 constructor TUITopWindow
.Create (const atitle
: AnsiString);
2158 procedure TUITopWindow
.AfterConstruction ();
2161 mFitToScreen
:= true;
2163 mFrameHeight
:= uiContext
.charHeight(#184);
2164 if (mWidth
< mFrameWidth
*2+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
)) then mWidth
:= mFrameWidth
*2+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
);
2165 if (mHeight
< mFrameHeight
*2) then mHeight
:= mFrameHeight
*2;
2166 if (Length(mTitle
) > 0) then
2168 if (mWidth
< uiContext
.textWidth(mTitle
)+mFrameWidth
*2+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
)) then
2170 mWidth
:= uiContext
.textWidth(mTitle
)+mFrameWidth
*2+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
);
2174 mDragScroll
:= TXMode
.None
;
2175 mDrawShadow
:= true;
2176 mWaitingClose
:= false;
2179 mCtl4Style
:= 'window';
2180 mDefSize
.w
:= nmax(1, mDefSize
.w
);
2181 mDefSize
.h
:= nmax(1, mDefSize
.h
);
2185 function TUITopWindow
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2187 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
2189 mTitle
:= par
.expectIdOrStr(true);
2193 if (strEquCI1251(prname
, 'children')) then
2199 if (strEquCI1251(prname
, 'position')) then
2201 if (par
.eatIdOrStrCI('default')) then mDoCenter
:= false
2202 else if (par
.eatIdOrStrCI('center')) then mDoCenter
:= true
2203 else par
.error('`center` or `default` expected');
2207 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
2208 result
:= inherited parseProperty(prname
, par
);
2212 procedure TUITopWindow
.flFitToScreen ();
2216 nsz
:= TLaySize
.Create(trunc(fuiScrWdt
/fuiRenderScale
)-mFrameWidth
*2-6, trunc(fuiScrHgt
/fuiRenderScale
)-mFrameHeight
*2-6);
2217 if (mMaxSize
.w
< 1) then mMaxSize
.w
:= nsz
.w
;
2218 if (mMaxSize
.h
< 1) then mMaxSize
.h
:= nsz
.h
;
2222 procedure TUITopWindow
.centerInScreen ();
2224 if (mWidth
> 0) and (mHeight
> 0) then
2226 mX
:= trunc((fuiScrWdt
/fuiRenderScale
-mWidth
)/2);
2227 mY
:= trunc((fuiScrHgt
/fuiRenderScale
-mHeight
)/2);
2232 procedure TUITopWindow
.drawControl (gx
, gy
: Integer);
2234 uiContext
.color
:= mBackColor
[getColorIndex
];
2235 uiContext
.fillRect(gx
, gy
, mWidth
, mHeight
);
2238 procedure TUITopWindow
.drawControlPost (gx
, gy
: Integer);
2241 hgt
, sbhgt
, iwdt
, ihgt
: Integer;
2243 cidx
:= getColorIndex
;
2244 iwdt
:= uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
);
2245 if (mDragScroll
= TXMode
.Drag
) then
2247 //uiContext.color := mFrameColor[cidx];
2248 drawFrame(gx
, gy
, iwdt
, 0, mTitle
, false);
2252 ihgt
:= uiContext
.iconWinHeight(TGxContext
.TWinIcon
.Close
);
2253 //uiContext.color := mFrameColor[cidx];
2254 drawFrame(gx
, gy
, iwdt
, 0, mTitle
, true);
2255 // vertical scroll bar
2256 hgt
:= mHeight
-mFrameHeight
*2;
2257 if (hgt
> 0) and (mFullSize
.h
> hgt
) then
2259 //writeln(mTitle, ': height=', mHeight-mFrameHeight*2, '; fullsize=', mFullSize.toString);
2260 sbhgt
:= mHeight
-mFrameHeight
*2+2;
2261 uiContext
.fillRect(gx
+mWidth
-mFrameWidth
+1, gy
+mFrameHeight
-1, mFrameWidth
-3, sbhgt
);
2263 if (hgt
> mFullSize
.h
) then hgt
:= mFullSize
.h
;
2264 hgt
:= sbhgt
*hgt
div mFullSize
.h
;
2267 setScissor(mWidth
-mFrameWidth
+1, mFrameHeight
-1, mFrameWidth
-3, sbhgt
);
2268 uiContext
.darkenRect(gx
+mWidth
-mFrameWidth
+1, gy
+mFrameHeight
-1+hgt
, mFrameWidth
-3, sbhgt
, 128);
2272 setScissor(mFrameWidth
, 0, iwdt
, ihgt
);
2273 uiContext
.color
:= mBackColor
[cidx
];
2274 uiContext
.fillRect(gx
+mFrameWidth
, gy
, iwdt
, ihgt
);
2275 uiContext
.color
:= mFrameIconColor
[cidx
];
2276 uiContext
.drawIconWin(TGxContext
.TWinIcon
.Close
, gx
+mFrameWidth
, gy
, mInClose
);
2279 inherited drawControlPost(gx
, gy
);
2283 procedure TUITopWindow
.activated ();
2285 if (mFocused
= nil) or (mFocused
= self
) then
2287 mFocused
:= findFirstFocus();
2289 if (mFocused
<> nil) and (mFocused
<> self
) then mFocused
.activated();
2294 procedure TUITopWindow
.blurred ();
2296 mDragScroll
:= TXMode
.None
;
2297 mWaitingClose
:= false;
2299 if (mFocused
<> nil) and (mFocused
<> self
) then mFocused
.blurred();
2304 procedure TUITopWindow
.keyEvent (var ev
: THKeyEvent
);
2306 inherited keyEvent(ev
);
2307 if (ev
.eaten
) or (ev
.cancelled
) or (not enabled
) {or (not getFocused)} then exit
;
2308 if (ev
= 'M-F3') then
2310 if (not assigned(closeRequestCB
)) or (closeRequestCB(self
)) then
2312 uiRemoveWindow(self
);
2320 procedure TUITopWindow
.mouseEvent (var ev
: THMouseEvent
);
2323 hgt
, sbhgt
: Integer;
2325 if (not enabled
) then exit
;
2326 if (mWidth
< 1) or (mHeight
< 1) then exit
;
2328 if (mDragScroll
= TXMode
.Drag
) then
2330 mX
+= ev
.x
-mDragStartX
;
2331 mY
+= ev
.y
-mDragStartY
;
2332 mDragStartX
:= ev
.x
;
2333 mDragStartY
:= ev
.y
;
2334 if (ev
.release
) and (ev
.but
= ev
.Left
) then mDragScroll
:= TXMode
.None
;
2339 if (mDragScroll
= TXMode
.Scroll
) then
2341 // check for vertical scrollbar
2349 sbhgt
:= mHeight
-mFrameHeight
*2+2;
2350 hgt
:= mHeight
-mFrameHeight
*2;
2351 if (hgt
> 0) and (mFullSize
.h
> hgt
) then
2353 hgt
:= (mFullSize
.h
*(ly
-7) div (sbhgt
-1))-(mHeight
-mFrameHeight
*2);
2354 mScrollY
:= nmax(0, hgt
);
2355 hgt
:= mHeight
-mFrameHeight
*2;
2356 if (mScrollY
+hgt
> mFullSize
.h
) then mScrollY
:= nmax(0, mFullSize
.h
-hgt
);
2359 if (ev
.release
) and (ev
.but
= ev
.Left
) then mDragScroll
:= TXMode
.None
;
2364 if toLocal(ev
.x
, ev
.y
, lx
, ly
) then
2368 if (ly
< mFrameHeight
) then
2371 if (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
)) then
2373 //uiRemoveWindow(self);
2374 mWaitingClose
:= true;
2379 mDragScroll
:= TXMode
.Drag
;
2380 mDragStartX
:= ev
.x
;
2381 mDragStartY
:= ev
.y
;
2386 // check for vertical scrollbar
2387 if (lx
>= mWidth
-mFrameWidth
+1) and (ly
>= 7) and (ly
< mHeight
-mFrameHeight
+1) then
2389 sbhgt
:= mHeight
-mFrameHeight
*2+2;
2390 hgt
:= mHeight
-mFrameHeight
*2;
2391 if (hgt
> 0) and (mFullSize
.h
> hgt
) then
2393 hgt
:= (mFullSize
.h
*(ly
-7) div (sbhgt
-1))-(mHeight
-mFrameHeight
*2);
2394 mScrollY
:= nmax(0, hgt
);
2396 mDragScroll
:= TXMode
.Scroll
;
2402 if (lx
< mFrameWidth
) or (lx
>= mWidth
-mFrameWidth
) or (ly
>= mHeight
-mFrameHeight
) then
2405 mDragScroll
:= TXMode
.Drag
;
2406 mDragStartX
:= ev
.x
;
2407 mDragStartY
:= ev
.y
;
2413 if (ev
.release
) then
2415 if mWaitingClose
then
2417 if (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
)) then
2419 if (not assigned(closeRequestCB
)) or (closeRequestCB(self
)) then
2421 uiRemoveWindow(self
);
2424 mWaitingClose
:= false;
2433 if mWaitingClose
then
2435 mInClose
:= (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
));
2441 inherited mouseEvent(ev
);
2446 if (not ev
.motion
) and (mWaitingClose
) then begin ev
.eat(); mWaitingClose
:= false; exit
; end;
2451 // ////////////////////////////////////////////////////////////////////////// //
2452 constructor TUIBox
.Create (ahoriz
: Boolean);
2459 procedure TUIBox
.AfterConstruction ();
2463 mHAlign
:= -1; // left
2464 mCtl4Style
:= 'box';
2465 mDefSize
:= TLaySize
.Create(-1, -1);
2469 procedure TUIBox
.setCaption (const acap
: AnsiString);
2472 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mCaption
)+3, uiContext
.textHeight(mCaption
));
2476 procedure TUIBox
.setHasFrame (v
: Boolean);
2479 if (mHasFrame
) then begin mFrameWidth
:= 8; mFrameHeight
:= uiContext
.charHeight(#184); end else begin mFrameWidth
:= 0; mFrameHeight
:= 0; end;
2480 if (mHasFrame
) then mNoPad
:= true;
2484 function TUIBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2486 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
2487 if (strEquCI1251(prname
, 'padding')) then
2489 if (mHoriz
) then mPadding
:= parseHPadding(par
, 0) else mPadding
:= parseVPadding(par
, 0);
2493 if (strEquCI1251(prname
, 'frame')) then
2495 setHasFrame(parseBool(par
));
2499 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
2501 setCaption(par
.expectIdOrStr(true));
2505 if (strEquCI1251(prname
, 'textalign')) or (strEquCI1251(prname
, 'text-align')) then
2507 mHAlign
:= parseHAlign(par
);
2511 if (strEquCI1251(prname
, 'children')) then
2517 result
:= inherited parseProperty(prname
, par
);
2521 procedure TUIBox
.drawControl (gx
, gy
: Integer);
2526 cidx
:= getColorIndex
;
2527 uiContext
.color
:= mBackColor
[cidx
];
2528 uiContext
.fillRect(gx
, gy
, mWidth
, mHeight
);
2532 drawFrame(gx
, gy
, 0, -1, mCaption
, false);
2533 //uiContext.color := mFrameColor[cidx];
2534 //uiContext.rect(gx+3, gy+3, mWidth-6, mHeight-6);
2536 else if (Length(mCaption
) > 0) then
2539 if (mHAlign
< 0) then xpos
:= 3
2540 else if (mHAlign
> 0) then xpos
:= mWidth
-mFrameWidth
*2-uiContext
.textWidth(mCaption
)
2541 else xpos
:= (mWidth
-mFrameWidth
*2-uiContext
.textWidth(mCaption
)) div 2;
2542 xpos
+= gx
+mFrameWidth
;
2544 setScissor(mFrameWidth
+1, 0, mWidth
-mFrameWidth
-2, uiContext
.textHeight(mCaption
));
2548 uiContext.color := mBackColor[cidx];
2549 uiContext.fillRect(xpos-3, gy, uiContext.textWidth(mCaption)+4, 8);
2552 uiContext
.color
:= mFrameTextColor
[cidx
];
2553 uiContext
.drawText(xpos
, gy
, mCaption
);
2558 procedure TUIBox
.mouseEvent (var ev
: THMouseEvent
);
2562 inherited mouseEvent(ev
);
2563 if (not ev
.eaten
) and (not ev
.cancelled
) and (enabled
) and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
2570 procedure TUIBox
.keyEvent (var ev
: THKeyEvent
);
2573 cur
, ctl
: TUIControl
;
2575 inherited keyEvent(ev
);
2576 if (ev
.eaten
) or (ev
.cancelled
) or (not ev
.press
) or (not enabled
) or (not getActive
) then exit
;
2577 if (Length(mChildren
) = 0) then exit
;
2578 if (mHoriz
) and (ev
= 'Left') then dir
:= -1
2579 else if (mHoriz
) and (ev
= 'Right') then dir
:= 1
2580 else if (not mHoriz
) and (ev
= 'Up') then dir
:= -1
2581 else if (not mHoriz
) and (ev
= 'Down') then dir
:= 1;
2582 if (dir
= 0) then exit
;
2584 cur
:= topLevel
.mFocused
;
2585 while (cur
<> nil) and (cur
.mParent
<> self
) do cur
:= cur
.mParent
;
2586 //if (cur = nil) then writeln('CUR: nil') else writeln('CUR: ', cur.className, '#', cur.id);
2587 if (dir
< 0) then ctl
:= findPrevFocus(cur
, true) else ctl
:= findNextFocus(cur
, true);
2588 //if (ctl = nil) then writeln('CTL: nil') else writeln('CTL: ', ctl.className, '#', ctl.id);
2589 if (ctl
<> nil) and (ctl
<> self
) then
2591 ctl
.focused
:= true;
2596 // ////////////////////////////////////////////////////////////////////////// //
2597 constructor TUIHBox
.Create ();
2602 procedure TUIHBox
.AfterConstruction ();
2609 // ////////////////////////////////////////////////////////////////////////// //
2610 constructor TUIVBox
.Create ();
2615 procedure TUIVBox
.AfterConstruction ();
2622 // ////////////////////////////////////////////////////////////////////////// //
2623 procedure TUISpan
.AfterConstruction ();
2629 mCtl4Style
:= 'span';
2630 mDefSize
:= TLaySize
.Create(-1, -1);
2634 function TUISpan
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2636 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
2637 result
:= inherited parseProperty(prname
, par
);
2641 procedure TUISpan
.drawControl (gx
, gy
: Integer);
2646 // ////////////////////////////////////////////////////////////////////// //
2647 procedure TUILine
.AfterConstruction ();
2653 mCtl4Style
:= 'line';
2654 mDefSize
:= TLaySize
.Create(-1, -1);
2658 function TUILine
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2660 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
2661 result
:= inherited parseProperty(prname
, par
);
2665 procedure TUILine
.layPrepare ();
2667 inherited layPrepare();
2668 if (mParent
<> nil) then mHoriz
:= not mParent
.mHoriz
;
2671 if (mLayDefSize
.w
< 0) then mLayDefSize
.w
:= 1;
2672 if (mLayDefSize
.h
< 0) then mLayDefSize
.h
:= 7;
2676 if (mLayDefSize
.w
< 0) then mLayDefSize
.w
:= 7;
2677 if (mLayDefSize
.h
< 0) then mLayDefSize
.h
:= 1;
2682 procedure TUILine
.drawControl (gx
, gy
: Integer);
2686 cidx
:= getColorIndex
;
2687 uiContext
.color
:= mTextColor
[cidx
];
2688 if mHoriz
then uiContext
.hline(gx
, gy
+(mHeight
div 2), mWidth
)
2689 else uiContext
.vline(gx
+(mWidth
div 2), gy
, mHeight
);
2693 // ////////////////////////////////////////////////////////////////////////// //
2694 procedure TUIStaticText
.AfterConstruction ();
2700 mHoriz
:= true; // nobody cares
2703 mCtl4Style
:= 'static';
2707 procedure TUIStaticText
.setText (const atext
: AnsiString);
2710 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
), uiContext
.textHeight(mText
));
2714 function TUIStaticText
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2716 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) or (strEquCI1251(prname
, 'text')) then
2718 setText(par
.expectIdOrStr(true));
2722 if (strEquCI1251(prname
, 'textalign')) or (strEquCI1251(prname
, 'text-align')) then
2724 parseTextAlign(par
, mHAlign
, mVAlign
);
2728 if (strEquCI1251(prname
, 'header')) then
2734 if (strEquCI1251(prname
, 'line')) then
2740 result
:= inherited parseProperty(prname
, par
);
2744 procedure TUIStaticText
.drawControl (gx
, gy
: Integer);
2746 xpos
, ypos
: Integer;
2749 cidx
:= getColorIndex
;
2750 uiContext
.color
:= mBackColor
[cidx
];
2751 uiContext
.fillRect(gx
, gy
, mWidth
, mHeight
);
2753 if (mHAlign
< 0) then xpos
:= 0
2754 else if (mHAlign
> 0) then xpos
:= mWidth
-uiContext
.textWidth(mText
)
2755 else xpos
:= (mWidth
-uiContext
.textWidth(mText
)) div 2;
2757 if (Length(mText
) > 0) then
2759 if (mHeader
) then uiContext
.color
:= mFrameTextColor
[cidx
] else uiContext
.color
:= mTextColor
[cidx
];
2761 if (mVAlign
< 0) then ypos
:= 0
2762 else if (mVAlign
> 0) then ypos
:= mHeight
-uiContext
.textHeight(mText
)
2763 else ypos
:= (mHeight
-uiContext
.textHeight(mText
)) div 2;
2765 uiContext
.drawText(gx
+xpos
, gy
+ypos
, mText
);
2770 if (mHeader
) then uiContext
.color
:= mFrameColor
[cidx
] else uiContext
.color
:= mTextColor
[cidx
];
2772 if (mVAlign
< 0) then ypos
:= 0
2773 else if (mVAlign
> 0) then ypos
:= mHeight
-1
2774 else ypos
:= (mHeight
div 2);
2777 if (Length(mText
) = 0) then
2779 uiContext
.hline(gx
, ypos
, mWidth
);
2783 uiContext
.hline(gx
, ypos
, xpos
-1);
2784 uiContext
.hline(gx
+xpos
+uiContext
.textWidth(mText
), ypos
, mWidth
);
2790 // ////////////////////////////////////////////////////////////////////////// //
2791 procedure TUITextLabel
.AfterConstruction ();
2797 mCtl4Style
:= 'label';
2802 procedure TUITextLabel
.cacheStyle (root
: TUIStyle
);
2804 inherited cacheStyle(root
);
2806 mHotColor
[ClrIdxActive
] := root
.get('hot-color', 'active', mCtl4Style
).asRGBADef(TGxRGBA
.Create(0, 128, 0));
2808 mHotColor
[ClrIdxDisabled
] := root
.get('hot-color', 'disabled', mCtl4Style
).asRGBADef(TGxRGBA
.Create(0, 64, 0));
2810 mHotColor
[ClrIdxInactive
] := root
.get('hot-color', 'inactive', mCtl4Style
).asRGBADef(TGxRGBA
.Create(0, 64, 0));
2814 procedure TUITextLabel
.setText (const s
: AnsiString);
2822 while (f
<= Length(s
)) do
2824 if (s
[f
] = '\\') then
2827 if (f
<= Length(s
)) then mText
+= s
[f
];
2830 else if (s
[f
] = '~') then
2833 if (f
<= Length(s
)) then
2835 if (mHotChar
= #0) then
2838 mHotOfs
:= Length(mText
);
2850 // fix hotchar offset
2851 if (mHotChar
<> #0) and (mHotOfs
> 0) then
2853 mHotOfs
:= uiContext
.textWidth(Copy(mText
, 1, mHotOfs
+1))-uiContext
.charWidth(mText
[mHotOfs
+1]);
2856 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
), uiContext
.textHeight(mText
));
2860 function TUITextLabel
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2862 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) or (strEquCI1251(prname
, 'text')) then
2864 setText(par
.expectIdOrStr(true));
2868 if (strEquCI1251(prname
, 'link')) then
2870 mLinkId
:= par
.expectIdOrStr(true);
2874 if (strEquCI1251(prname
, 'textalign')) or (strEquCI1251(prname
, 'text-align')) then
2876 parseTextAlign(par
, mHAlign
, mVAlign
);
2880 result
:= inherited parseProperty(prname
, par
);
2884 procedure TUITextLabel
.drawControl (gx
, gy
: Integer);
2886 xpos
, ypos
: Integer;
2889 cidx
:= getColorIndex
;
2890 uiContext
.color
:= mBackColor
[cidx
];
2891 uiContext
.fillRect(gx
, gy
, mWidth
, mHeight
);
2892 if (Length(mText
) > 0) then
2894 if (mHAlign
< 0) then xpos
:= 0
2895 else if (mHAlign
> 0) then xpos
:= mWidth
-uiContext
.textWidth(mText
)
2896 else xpos
:= (mWidth
-uiContext
.textWidth(mText
)) div 2;
2898 if (mVAlign
< 0) then ypos
:= 0
2899 else if (mVAlign
> 0) then ypos
:= mHeight
-uiContext
.textHeight(mText
)
2900 else ypos
:= (mHeight
-uiContext
.textHeight(mText
)) div 2;
2902 uiContext
.color
:= mTextColor
[cidx
];
2903 uiContext
.drawText(gx
+xpos
, gy
+ypos
, mText
);
2905 if (Length(mLinkId
) > 0) and (mHotChar
<> #0) and (mHotChar
<> ' ') then
2907 uiContext
.color
:= mHotColor
[cidx
];
2908 uiContext
.drawChar(gx
+xpos
+mHotOfs
, gy
+ypos
, mHotChar
);
2914 procedure TUITextLabel
.mouseEvent (var ev
: THMouseEvent
);
2918 inherited mouseEvent(ev
);
2919 if (not ev
.eaten
) and (not ev
.cancelled
) and (enabled
) and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
2926 procedure TUITextLabel
.doAction ();
2930 if (assigned(actionCB
)) then
2936 ctl
:= topLevel
[mLinkId
];
2937 if (ctl
<> nil) then
2939 if (ctl
.canFocus
) then ctl
.focused
:= true;
2945 procedure TUITextLabel
.keyEventPost (var ev
: THKeyEvent
);
2947 if (not enabled
) then exit
;
2948 if (mHotChar
= #0) then exit
;
2949 if (ev
.eaten
) or (ev
.cancelled
) or (not ev
.press
) then exit
;
2950 if (ev
.kstate
<> ev
.ModAlt
) then exit
;
2951 if (not ev
.isHot(mHotChar
)) then exit
;
2953 if (canFocus
) then focused
:= true;
2958 // ////////////////////////////////////////////////////////////////////////// //
2959 procedure TUIButton
.AfterConstruction ();
2966 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+uiContext
.textWidth('[ ]'), uiContext
.textHeight(mText
));
2967 mCtl4Style
:= 'button';
2968 mSkipLayPrepare
:= false;
2969 mAddMarkers
:= false;
2970 mHideMarkers
:= false;
2974 procedure TUIButton
.cacheStyle (root
: TUIStyle
);
2978 inherited cacheStyle(root
);
2980 sz
:= nmax(0, root
.get('shadow-size', 'active', mCtl4Style
).asInt(0));
2981 sz
:= nmax(sz
, root
.get('shadow-size', 'disabled', mCtl4Style
).asInt(0));
2982 sz
:= nmax(sz
, root
.get('shadow-size', 'inactive', mCtl4Style
).asInt(0));
2985 mAddMarkers
:= root
.get('add-markers', 'active', mCtl4Style
).asBool(false);
2986 mAddMarkers
:= mAddMarkers
or root
.get('add-markers', 'disabled', mCtl4Style
).asBool(false);
2987 mAddMarkers
:= mAddMarkers
or root
.get('add-markers', 'inactive', mCtl4Style
).asBool(false);
2989 mHideMarkers
:= root
.get('hide-markers', 'active', mCtl4Style
).asBool(false);
2990 mHideMarkers
:= mHideMarkers
or root
.get('hide-markers', 'disabled', mCtl4Style
).asBool(false);
2991 mHideMarkers
:= mHideMarkers
or root
.get('hide-markers', 'inactive', mCtl4Style
).asBool(false);
2995 procedure TUIButton
.setText (const s
: AnsiString);
2997 inherited setText(s
);
2998 if (mHideMarkers
) then
3000 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+10, uiContext
.textHeight(mText
));
3002 else if (mAddMarkers
) then
3004 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+uiContext
.textWidth('[<>]'), uiContext
.textHeight(mText
));
3008 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+uiContext
.textWidth('<>'), uiContext
.textHeight(mText
));
3013 procedure TUIButton
.layPrepare ();
3018 if (not mSkipLayPrepare
) then
3021 if (ods
.w
<> 0) or (ods
.h
<> 0) then
3023 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
), uiContext
.textHeight(mText
));
3024 if (mHideMarkers
) then
3028 else if (mAddMarkers
) then
3030 if (mDefault
) then ww
:= uiContext
.textWidth('[< >]')
3031 else if (mCancel
) then ww
:= uiContext
.textWidth('[{ }]')
3032 else ww
:= uiContext
.textWidth('[ ]');
3036 ww
:= nmax(0, uiContext
.textWidth('< >'));
3037 ww
:= nmax(ww
, uiContext
.textWidth('{ }'));
3038 ww
:= nmax(ww
, uiContext
.textWidth('[ ]'));
3040 mDefSize
.w
+= ww
+mShadowSize
;
3041 mDefSize
.h
+= mShadowSize
;
3046 ods
:= TLaySize
.Create(0, 0); // fpc is dumb!
3048 inherited layPrepare();
3049 if (not mSkipLayPrepare
) then mDefSize
:= ods
;
3053 procedure TUIButton
.drawControl (gx
, gy
: Integer);
3056 xpos
, ypos
, xofsl
, xofsr
{, sofs}: Integer;
3059 lstr
, rstr
: AnsiString;
3061 cidx
:= getColorIndex
;
3063 wdt
:= mWidth
-mShadowSize
;
3064 hgt
:= mHeight
-mShadowSize
;
3065 if (mPushed
) {or (cidx = ClrIdxActive)} then
3067 //sofs := mShadowSize;
3074 if (mShadowSize
> 0) then
3076 uiContext
.darkenRect(gx
+mShadowSize
, gy
+hgt
, wdt
, mShadowSize
, 96);
3077 uiContext
.darkenRect(gx
+wdt
, gy
+mShadowSize
, mShadowSize
, hgt
-mShadowSize
, 96);
3081 uiContext
.color
:= mBackColor
[cidx
];
3082 //setScissor(sofs, sofs, wdt, hgt);
3083 uiContext
.fillRect(gx
, gy
, wdt
, hgt
);
3085 if (mVAlign
< 0) then ypos
:= 0
3086 else if (mVAlign
> 0) then ypos
:= hgt
-uiContext
.textHeight(mText
)
3087 else ypos
:= (hgt
-uiContext
.textHeight(mText
)) div 2;
3090 uiContext
.color
:= mTextColor
[cidx
];
3092 if (mHideMarkers
) then
3099 if (mAddMarkers
) then
3101 if (mDefault
) then begin lstr
:= '[< '; rstr
:= ' >]'; end
3102 else if (mCancel
) then begin lstr
:= '[{ '; rstr
:= ' }]'; end
3103 else begin lstr
:= '[ '; rstr
:= ' ]'; end;
3104 xofsl
:= uiContext
.textWidth(lstr
);
3105 xofsr
:= uiContext
.textWidth(rstr
);
3106 uiContext
.drawText(gx
, ypos
, lstr
);
3107 uiContext
.drawText(gx
+wdt
-uiContext
.textWidth(rstr
), ypos
, rstr
);
3111 xofsl
:= nmax(0, uiContext
.textWidth('< '));
3112 xofsl
:= nmax(xofsl
, uiContext
.textWidth('{ '));
3113 xofsl
:= nmax(xofsl
, uiContext
.textWidth('[ '));
3114 xofsr
:= nmax(0, uiContext
.textWidth(' >'));
3115 xofsr
:= nmax(xofsr
, uiContext
.textWidth(' }'));
3116 xofsr
:= nmax(xofsr
, uiContext
.textWidth(' ]'));
3117 if (mDefault
) then begin lch
:= '<'; rch
:= '>'; end
3118 else if (mCancel
) then begin lch
:= '{'; rch
:= '}'; end
3119 else begin lch
:= '['; rch
:= ']'; end;
3120 uiContext
.drawChar(gx
, ypos
, lch
);
3121 uiContext
.drawChar(gx
+wdt
-uiContext
.charWidth(rch
), ypos
, rch
);
3125 if (Length(mText
) > 0) then
3127 if (mHAlign
< 0) then xpos
:= 0
3128 else begin xpos
:= wdt
-xofsl
-xofsr
-uiContext
.textWidth(mText
); if (mHAlign
= 0) then xpos
:= xpos
div 2; end;
3131 //setScissor(xofsl+sofs, sofs, wdt-xofsl-xofsr, hgt);
3132 uiContext
.drawText(gx
+xpos
, ypos
, mText
);
3134 if (mHotChar
<> #0) and (mHotChar
<> ' ') then
3136 uiContext
.color
:= mHotColor
[cidx
];
3137 uiContext
.drawChar(gx
+xpos
+mHotOfs
, ypos
, mHotChar
);
3143 procedure TUIButton
.mouseEvent (var ev
: THMouseEvent
);
3147 inherited mouseEvent(ev
);
3148 if (uiGrabCtl
= self
) then
3151 mPushed
:= toLocal(ev
.x
, ev
.y
, lx
, ly
);
3152 if (ev
= '-lmb') and focused
and mPushed
then
3159 if (ev
.eaten
) or (ev
.cancelled
) or (not enabled
) or not focused
then exit
;
3165 procedure TUIButton
.keyEvent (var ev
: THKeyEvent
);
3167 inherited keyEvent(ev
);
3168 if (not ev
.eaten
) and (not ev
.cancelled
) and (enabled
) then
3170 if (ev
= 'Enter') or (ev
= 'Space') then
3180 // ////////////////////////////////////////////////////////////////////////// //
3181 procedure TUIButtonRound
.AfterConstruction ();
3187 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+8*2, uiContext
.textHeight(mText
)+2);
3188 mCtl4Style
:= 'button-round';
3189 mSkipLayPrepare
:= true;
3193 procedure TUIButtonRound
.setText (const s
: AnsiString);
3195 inherited setText(s
);
3196 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+8*2, uiContext
.textHeight(mText
)+2);
3200 procedure TUIButtonRound
.layPrepare ();
3205 if (ods
.w
<> 0) or (ods
.h
<> 0) then
3207 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+8*2, uiContext
.textHeight(mText
)+2);
3209 inherited layPrepare();
3214 procedure TUIButtonRound
.drawControl (gx
, gy
: Integer);
3216 xpos
, ypos
: Integer;
3219 cidx
:= getColorIndex
;
3221 uiContext
.color
:= mBackColor
[cidx
];
3222 uiContext
.fillRect(gx
+1, gy
, mWidth
-2, mHeight
);
3223 uiContext
.fillRect(gx
, gy
+1, 1, mHeight
-2);
3224 uiContext
.fillRect(gx
+mWidth
-1, gy
+1, 1, mHeight
-2);
3226 if (Length(mText
) > 0) then
3228 if (mHAlign
< 0) then xpos
:= 0
3229 else if (mHAlign
> 0) then xpos
:= mWidth
-uiContext
.textWidth(mText
)
3230 else xpos
:= (mWidth
-uiContext
.textWidth(mText
)) div 2;
3232 if (mVAlign
< 0) then ypos
:= 0
3233 else if (mVAlign
> 0) then ypos
:= mHeight
-uiContext
.textHeight(mText
)
3234 else ypos
:= (mHeight
-uiContext
.textHeight(mText
)) div 2;
3236 setScissor(8, 0, mWidth
-16, mHeight
);
3237 uiContext
.color
:= mTextColor
[cidx
];
3238 uiContext
.drawText(gx
+xpos
+8, gy
+ypos
, mText
);
3240 if (mHotChar
<> #0) and (mHotChar
<> ' ') then
3242 uiContext
.color
:= mHotColor
[cidx
];
3243 uiContext
.drawChar(gx
+xpos
+8+mHotOfs
, gy
+ypos
, mHotChar
);
3249 // ////////////////////////////////////////////////////////////////////////// //
3250 procedure TUISwitchBox
.AfterConstruction ();
3256 mIcon
:= TGxContext
.TMarkIcon
.Checkbox
;
3257 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+3+uiContext
.iconMarkWidth(mIcon
), nmax(uiContext
.iconMarkHeight(mIcon
), uiContext
.textHeight(mText
)));
3258 mCtl4Style
:= 'switchbox';
3260 mBoolVar
:= @mChecked
;
3264 procedure TUISwitchBox
.cacheStyle (root
: TUIStyle
);
3266 inherited cacheStyle(root
);
3268 mSwitchColor
[ClrIdxActive
] := root
.get('switch-color', 'active', mCtl4Style
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
3270 mSwitchColor
[ClrIdxDisabled
] := root
.get('switch-color', 'disabled', mCtl4Style
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
3272 mSwitchColor
[ClrIdxInactive
] := root
.get('switch-color', 'inactive', mCtl4Style
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
3276 procedure TUISwitchBox
.setText (const s
: AnsiString);
3278 inherited setText(s
);
3279 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+3+uiContext
.iconMarkWidth(mIcon
), nmax(uiContext
.iconMarkHeight(mIcon
), uiContext
.textHeight(mText
)));
3283 function TUISwitchBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
3285 if (strEquCI1251(prname
, 'checked')) then
3291 result
:= inherited parseProperty(prname
, par
);
3295 function TUISwitchBox
.getChecked (): Boolean;
3297 if (mBoolVar
<> nil) then result
:= mBoolVar
^ else result
:= false;
3301 procedure TUISwitchBox
.setVar (pvar
: PBoolean);
3303 if (pvar
= nil) then pvar
:= @mChecked
;
3304 if (pvar
<> mBoolVar
) then
3307 setChecked(mBoolVar
^);
3312 procedure TUISwitchBox
.drawControl (gx
, gy
: Integer);
3314 xpos
, ypos
, iwdt
, dy
: Integer;
3317 cidx
:= getColorIndex
;
3319 iwdt
:= uiContext
.iconMarkWidth(mIcon
);
3320 if (mHAlign
< 0) then xpos
:= 0
3321 else if (mHAlign
> 0) then xpos
:= mWidth
-(uiContext
.textWidth(mText
)+3+iwdt
)
3322 else xpos
:= (mWidth
-(uiContext
.textWidth(mText
)+3+iwdt
)) div 2;
3324 if (mVAlign
< 0) then ypos
:= 0
3325 else if (mVAlign
> 0) then ypos
:= mHeight
-uiContext
.textHeight(mText
)
3326 else ypos
:= (mHeight
-uiContext
.textHeight(mText
)) div 2;
3328 uiContext
.color
:= mBackColor
[cidx
];
3329 uiContext
.fillRect(gx
, gy
, mWidth
, mHeight
);
3331 uiContext
.color
:= mSwitchColor
[cidx
];
3332 if (uiContext
.iconMarkHeight(mIcon
) < uiContext
.textHeight(mText
)) then
3334 case uiContext
.textHeight(mText
) of
3339 uiContext
.drawIconMark(mIcon
, gx
, gy
+ypos
+uiContext
.textHeight(mText
)-uiContext
.iconMarkHeight(mIcon
)-dy
, checked
);
3343 uiContext
.drawIconMark(mIcon
, gx
, gy
, checked
);
3346 uiContext
.color
:= mTextColor
[cidx
];
3347 uiContext
.drawText(gx
+xpos
+3+iwdt
, gy
+ypos
, mText
);
3349 if (mHotChar
<> #0) and (mHotChar
<> ' ') then
3351 uiContext
.color
:= mHotColor
[cidx
];
3352 uiContext
.drawChar(gx
+xpos
+3+iwdt
+mHotOfs
, gy
+ypos
, mHotChar
);
3357 procedure TUISwitchBox
.mouseEvent (var ev
: THMouseEvent
);
3361 inherited mouseEvent(ev
);
3362 if (uiGrabCtl
= self
) then
3365 if (ev
= '-lmb') and focused
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
3371 if (ev
.eaten
) or (ev
.cancelled
) or (not enabled
) or not focused
then exit
;
3376 procedure TUISwitchBox
.keyEvent (var ev
: THKeyEvent
);
3378 inherited keyEvent(ev
);
3379 if (not ev
.eaten
) and (not ev
.cancelled
) and (enabled
) then
3381 if (ev
= 'Space') then
3391 // ////////////////////////////////////////////////////////////////////////// //
3392 procedure TUICheckBox
.AfterConstruction ();
3396 mBoolVar
:= @mChecked
;
3397 mIcon
:= TGxContext
.TMarkIcon
.Checkbox
;
3402 procedure TUICheckBox
.setChecked (v
: Boolean);
3408 procedure TUICheckBox
.doAction ();
3410 if (assigned(actionCB
)) then
3416 setChecked(not getChecked
);
3421 // ////////////////////////////////////////////////////////////////////////// //
3422 procedure TUIRadioBox
.AfterConstruction ();
3426 mBoolVar
:= @mChecked
;
3428 mIcon
:= TGxContext
.TMarkIcon
.Radiobox
;
3433 function TUIRadioBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
3435 if (strEquCI1251(prname
, 'group')) then
3437 mRadioGroup
:= par
.expectIdOrStr(true);
3438 if (getChecked
) then setChecked(true);
3442 if (strEquCI1251(prname
, 'checked')) then
3448 result
:= inherited parseProperty(prname
, par
);
3452 procedure TUIRadioBox
.setChecked (v
: Boolean);
3454 function resetGroup (ctl
: TUIControl
): Boolean;
3457 if (ctl
<> self
) and (ctl
is TUIRadioBox
) and (TUIRadioBox(ctl
).mRadioGroup
= mRadioGroup
) then
3459 TUIRadioBox(ctl
).mBoolVar
^ := false;
3465 if v
then topLevel
.forEachControl(resetGroup
);
3469 procedure TUIRadioBox
.doAction ();
3471 if (assigned(actionCB
)) then
3482 // ////////////////////////////////////////////////////////////////////////// //
3484 registerCtlClass(TUIHBox
, 'hbox');
3485 registerCtlClass(TUIVBox
, 'vbox');
3486 registerCtlClass(TUISpan
, 'span');
3487 registerCtlClass(TUILine
, 'line');
3488 registerCtlClass(TUITextLabel
, 'label');
3489 registerCtlClass(TUIStaticText
, 'static');
3490 registerCtlClass(TUIButtonRound
, 'round-button');
3491 registerCtlClass(TUIButton
, 'button');
3492 registerCtlClass(TUICheckBox
, 'checkbox');
3493 registerCtlClass(TUIRadioBox
, 'radiobox');
3495 uiContext
:= TGxContext
.Create();