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, version 3 of the License ONLY.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE ../shared/a_modes.inc}
26 fui_common
, fui_events
, fui_style
,
31 // ////////////////////////////////////////////////////////////////////////// //
33 TUIControlClass
= class of TUIControl
;
37 type TActionCB
= procedure (me
: TUIControl
);
38 type TCloseRequestCB
= function (me
: TUIControl
): Boolean; // top-level windows will call this before closing with icon/keyboard
40 // return `true` to stop
41 type TCtlEnumCB
= function (ctl
: TUIControl
): Boolean is nested
;
44 const ClrIdxActive
= 0;
45 const ClrIdxDisabled
= 1;
46 const ClrIdxInactive
= 2;
54 mWidth
, mHeight
: Integer;
55 mFrameWidth
, mFrameHeight
: Integer;
56 mScrollX
, mScrollY
: Integer;
59 mChildren
: array of TUIControl
;
60 mFocused
: TUIControl
; // valid only for top-level controls
61 mEscClose
: Boolean; // valid only for top-level controls
66 mStyleLoaded
: Boolean;
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 mSBarFullColor
: array[0..ClrIdxMax
] of TGxRGBA
;
74 mSBarEmptyColor
: array[0..ClrIdxMax
] of TGxRGBA
;
75 mDarken
: array[0..ClrIdxMax
] of Integer; // >255: none
78 procedure updateStyle (); virtual;
79 procedure cacheStyle (root
: TUIStyle
); virtual;
80 function getColorIndex (): Integer; inline;
83 function getEnabled (): Boolean;
84 procedure setEnabled (v
: Boolean); inline;
86 function getFocused (): Boolean; inline;
87 procedure setFocused (v
: Boolean); inline;
89 function getActive (): Boolean; inline;
91 function getCanFocus (): Boolean; inline;
93 function isMyChild (ctl
: TUIControl
): Boolean;
95 function findFirstFocus (): TUIControl
;
96 function findLastFocus (): TUIControl
;
98 function findNextFocus (cur
: TUIControl
; wrap
: Boolean): TUIControl
;
99 function findPrevFocus (cur
: TUIControl
; wrap
: Boolean): TUIControl
;
101 function findCancelControl (): TUIControl
;
102 function findDefaulControl (): TUIControl
;
104 function findControlById (const aid
: AnsiString): TUIControl
;
106 procedure activated (); virtual;
107 procedure blurred (); virtual;
109 procedure calcFullClientSize ();
111 procedure drawFrame (gx
, gy
, resx
, thalign
: Integer; const text: AnsiString; dbl
: Boolean);
114 var savedClip
: TGxRect
; // valid only in `draw*()` calls
115 //WARNING! do not call scissor functions outside `.draw*()` API!
116 // set scissor to this rect (in local coords)
117 procedure setScissor (lx
, ly
, lw
, lh
: Integer); // valid only in `draw*()` calls
118 procedure resetScissor (); inline; // only client area, w/o frame
119 procedure resetScissorNC (); inline; // full drawing area, with frame
123 closeRequestCB
: TCloseRequestCB
;
126 mDefSize
: TLaySize
; // default size
127 mMaxSize
: TLaySize
; // maximum size
134 mLayDefSize
: TLaySize
;
135 mLayMaxSize
: TLaySize
;
141 // layouter interface
142 function getDefSize (): TLaySize
; inline; // default size; <0: use max size
143 //procedure setDefSize (const sz: TLaySize); inline; // default size; <0: use max size
144 function getMargins (): TLayMargins
; inline;
145 function getPadding (): TLaySize
; inline; // children padding (each non-first child will get this on left/top)
146 function getMaxSize (): TLaySize
; inline; // max size; <0: set to some huge value
147 //procedure setMaxSize (const sz: TLaySize); inline; // max size; <0: set to some huge value
148 function getFlex (): Integer; inline; // <=0: not flexible
149 function isHorizBox (): Boolean; inline; // horizontal layout for children?
150 function noPad (): Boolean; inline; // ignore padding in box direction for this control
151 function getAlign (): Integer; inline; // aligning in non-main direction: <0: left/up; 0: center; >0: right/down
152 function getExpand (): Boolean; inline; // expanding in non-main direction: `true` will ignore align and eat all available space
153 function getHGroup (): AnsiString; inline; // empty: not grouped
154 function getVGroup (): AnsiString; inline; // empty: not grouped
156 procedure setActualSizePos (constref apos
: TLayPos
; constref asize
: TLaySize
); inline;
158 procedure layPrepare (); virtual; // called before registering control in layouter
161 property flex
: Integer read mFlex write mFlex
;
162 property flDefaultSize
: TLaySize read mDefSize write mDefSize
;
163 property flMaxSize
: TLaySize read mMaxSize write mMaxSize
;
164 property flPadding
: TLaySize read mPadding write mPadding
;
165 property flHoriz
: Boolean read mHoriz write mHoriz
;
166 property flAlign
: Integer read mAlign write mAlign
;
167 property flExpand
: Boolean read mExpand write mExpand
;
168 property flHGroup
: AnsiString read mHGroup write mHGroup
;
169 property flVGroup
: AnsiString read mVGroup write mVGroup
;
170 property flNoPad
: Boolean read mNoPad write mNoPad
;
171 property fullSize
: TLaySize read mFullSize
;
174 function parsePos (par
: TTextParser
): TLayPos
;
175 function parseSize (par
: TTextParser
): TLaySize
;
176 function parsePadding (par
: TTextParser
): TLaySize
;
177 function parseHPadding (par
: TTextParser
; def
: Integer): TLaySize
;
178 function parseVPadding (par
: TTextParser
; def
: Integer): TLaySize
;
179 function parseBool (par
: TTextParser
): Boolean;
180 function parseAnyAlign (par
: TTextParser
): Integer;
181 function parseHAlign (par
: TTextParser
): Integer;
182 function parseVAlign (par
: TTextParser
): Integer;
183 function parseOrientation (const prname
: AnsiString; par
: TTextParser
): Boolean;
184 procedure parseTextAlign (par
: TTextParser
; var h
, v
: Integer);
185 procedure parseChildren (par
: TTextParser
); // par should be on '{'; final '}' is eaten
188 // par is on property data
189 // there may be more data in text stream, don't eat it!
190 // return `true` if property name is valid and value was parsed
191 // return `false` if property name is invalid; don't advance parser in this case
192 // throw on property data errors
193 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; virtual;
195 // par should be on '{'; final '}' is eaten
196 procedure parseProperties (par
: TTextParser
);
199 constructor Create ();
200 destructor Destroy (); override;
202 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
204 // `sx` and `sy` are screen coordinates
205 procedure drawControl (gx
, gy
: Integer); virtual;
207 // called after all children drawn
208 procedure drawControlPost (gx
, gy
: Integer); virtual;
210 procedure draw (); virtual;
212 function topLevel (): TUIControl
; inline;
214 // returns `true` if global coords are inside this control
215 function toLocal (var x
, y
: Integer): Boolean;
216 function toLocal (gx
, gy
: Integer; out x
, y
: Integer): Boolean; inline;
217 procedure toGlobal (var x
, y
: Integer);
218 procedure toGlobal (lx
, ly
: Integer; out x
, y
: Integer); inline;
220 procedure getDrawRect (out gx
, gy
, wdt
, hgt
: Integer);
222 // x and y are global coords
223 function controlAtXY (x
, y
: Integer; allowDisabled
: Boolean=false): TUIControl
;
225 function parentScrollX (): Integer; inline;
226 function parentScrollY (): Integer; inline;
228 procedure makeVisibleInParent ();
230 procedure doAction (); virtual; // so user controls can override it
232 procedure onEvent (var ev
: TFUIEvent
); virtual; // general dispatcher
234 procedure mouseEvent (var ev
: TFUIEvent
); virtual;
235 procedure mouseEventSink (var ev
: TFUIEvent
); virtual;
236 procedure mouseEventBubble (var ev
: TFUIEvent
); virtual;
238 procedure keyEvent (var ev
: TFUIEvent
); virtual;
239 procedure keyEventSink (var ev
: TFUIEvent
); virtual;
240 procedure keyEventBubble (var ev
: TFUIEvent
); virtual;
242 function prevSibling (): TUIControl
;
243 function nextSibling (): TUIControl
;
244 function firstChild (): TUIControl
; inline;
245 function lastChild (): TUIControl
; inline;
247 procedure appendChild (ctl
: TUIControl
); virtual;
249 function setActionCBFor (const aid
: AnsiString; cb
: TActionCB
): TActionCB
; // returns previous cb
251 function forEachChildren (cb
: TCtlEnumCB
): TUIControl
; // doesn't recurse
252 function forEachControl (cb
: TCtlEnumCB
; includeSelf
: Boolean=true): TUIControl
;
254 procedure close (); // this closes *top-level* control
257 property id
: AnsiString read mId write mId
;
258 property styleId
: AnsiString read mStyleId
;
259 property scrollX
: Integer read mScrollX write mScrollX
;
260 property scrollY
: Integer read mScrollY write mScrollY
;
261 property x0
: Integer read mX write mX
;
262 property y0
: Integer read mY write mY
;
263 property width
: Integer read mWidth write mWidth
;
264 property height
: Integer read mHeight write mHeight
;
265 property enabled
: Boolean read getEnabled write setEnabled
;
266 property parent
: TUIControl read mParent
;
267 property focused
: Boolean read getFocused write setFocused
;
268 property active
: Boolean read getActive
;
269 property escClose
: Boolean read mEscClose write mEscClose
;
270 property cancel
: Boolean read mCancel write mCancel
;
271 property defctl
: Boolean read mDefault write mDefault
;
272 property canFocus
: Boolean read getCanFocus write mCanFocus
;
273 property ctlById
[const aid
: AnsiString]: TUIControl read findControlById
; default
;
277 TUITopWindow
= class(TUIControl
)
279 type TXMode
= (None
, Drag
, VScroll
, HScroll
);
284 mDragStartX
, mDragStartY
: Integer;
285 mWaitingClose
: Boolean;
287 mFreeOnClose
: Boolean; // default: false
288 mDoCenter
: Boolean; // after layouting
289 mFitToScreen
: Boolean;
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 flFitToScreen (); // call this before layouting
307 procedure centerInScreen ();
309 // `sx` and `sy` are screen coordinates
310 procedure drawControl (gx
, gy
: Integer); override;
311 procedure drawControlPost (gx
, gy
: Integer); override;
313 procedure keyEventBubble (var ev
: TFUIEvent
); override; // returns `true` if event was eaten
314 procedure mouseEvent (var ev
: TFUIEvent
); override; // returns `true` if event was eaten
317 property freeOnClose
: Boolean read mFreeOnClose write mFreeOnClose
;
318 property fitToScreen
: Boolean read mFitToScreen write mFitToScreen
;
321 // ////////////////////////////////////////////////////////////////////// //
322 TUIBox
= class(TUIControl
)
325 mCaption
: AnsiString;
326 mHAlign
: Integer; // -1: left; 0: center; 1: right; default: left
329 procedure setCaption (const acap
: AnsiString);
330 procedure setHasFrame (v
: Boolean);
333 constructor Create (ahoriz
: Boolean);
335 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
337 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
339 procedure drawControl (gx
, gy
: Integer); override;
341 procedure mouseEvent (var ev
: TFUIEvent
); override;
342 procedure keyEvent (var ev
: TFUIEvent
); override;
345 property caption
: AnsiString read mCaption write setCaption
;
346 property hasFrame
: Boolean read mHasFrame write setHasFrame
;
347 property captionAlign
: Integer read mHAlign write mHAlign
;
350 TUIHBox
= class(TUIBox
)
352 constructor Create ();
354 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
357 TUIVBox
= class(TUIBox
)
359 constructor Create ();
361 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
364 // ////////////////////////////////////////////////////////////////////// //
365 TUISpan
= class(TUIControl
)
367 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
369 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
372 // ////////////////////////////////////////////////////////////////////// //
373 TUILine
= class(TUIControl
)
375 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
377 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
379 procedure layPrepare (); override; // called before registering control in layouter
381 procedure drawControl (gx
, gy
: Integer); override;
384 // ////////////////////////////////////////////////////////////////////// //
385 TUIStaticText
= class(TUIControl
)
388 mHAlign
: Integer; // -1: left; 0: center; 1: right; default: left
389 mVAlign
: Integer; // -1: top; 0: center; 1: bottom; default: center
390 mHeader
: Boolean; // true: draw with frame text color
391 mLine
: Boolean; // true: draw horizontal line
394 procedure setText (const atext
: AnsiString);
397 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
399 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
401 procedure drawControl (gx
, gy
: Integer); override;
404 property text: AnsiString read mText write setText
;
405 property halign
: Integer read mHAlign write mHAlign
;
406 property valign
: Integer read mVAlign write mVAlign
;
407 property header
: Boolean read mHeader write mHeader
;
408 property line
: Boolean read mLine write mLine
;
411 // ////////////////////////////////////////////////////////////////////// //
412 TUITextLabel
= class(TUIControl
)
415 mHAlign
: Integer; // -1: left; 0: center; 1: right; default: left
416 mVAlign
: Integer; // -1: top; 0: center; 1: bottom; default: center
418 mHotOfs
: Integer; // from text start, in pixels
419 mHotColor
: array[0..ClrIdxMax
] of TGxRGBA
;
420 mLinkId
: AnsiString; // linked control
423 procedure cacheStyle (root
: TUIStyle
); override;
425 procedure setText (const s
: AnsiString); virtual;
428 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
430 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
432 procedure doAction (); override;
434 procedure drawControl (gx
, gy
: Integer); override;
436 procedure mouseEvent (var ev
: TFUIEvent
); override;
437 procedure keyEventBubble (var ev
: TFUIEvent
); override;
440 property text: AnsiString read mText write setText
;
441 property halign
: Integer read mHAlign write mHAlign
;
442 property valign
: Integer read mVAlign write mVAlign
;
445 // ////////////////////////////////////////////////////////////////////// //
446 TUIButton
= class(TUITextLabel
)
448 mSkipLayPrepare
: Boolean;
449 mShadowSize
: Integer;
450 mAddMarkers
: Boolean;
451 mHideMarkers
: Boolean;
455 procedure setText (const s
: AnsiString); override;
457 procedure cacheStyle (root
: TUIStyle
); override;
459 procedure blurred (); override;
462 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
464 procedure layPrepare (); override; // called before registering control in layouter
466 procedure drawControl (gx
, gy
: Integer); override;
468 procedure mouseEvent (var ev
: TFUIEvent
); override;
469 procedure keyEvent (var ev
: TFUIEvent
); override;
472 // ////////////////////////////////////////////////////////////////////// //
473 TUIButtonRound
= class(TUIButton
)
475 procedure setText (const s
: AnsiString); override;
478 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
480 procedure layPrepare (); override; // called before registering control in layouter
482 procedure drawControl (gx
, gy
: Integer); override;
485 // ////////////////////////////////////////////////////////////////////// //
486 TUISwitchBox
= class(TUITextLabel
)
490 mIcon
: TGxContext
.TMarkIcon
;
491 mSwitchColor
: array[0..ClrIdxMax
] of TGxRGBA
;
494 procedure cacheStyle (root
: TUIStyle
); override;
496 procedure setText (const s
: AnsiString); override;
498 function getChecked (): Boolean; virtual;
499 procedure setChecked (v
: Boolean); virtual; abstract;
502 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
504 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
506 procedure drawControl (gx
, gy
: Integer); override;
508 procedure mouseEvent (var ev
: TFUIEvent
); override;
509 procedure keyEvent (var ev
: TFUIEvent
); override;
511 procedure setVar (pvar
: PBoolean);
514 property checked
: Boolean read getChecked write setChecked
;
517 TUICheckBox
= class(TUISwitchBox
)
519 procedure setChecked (v
: Boolean); override;
522 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
524 procedure doAction (); override;
527 TUIRadioBox
= class(TUISwitchBox
)
529 mRadioGroup
: AnsiString;
532 procedure setChecked (v
: Boolean); override;
535 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
537 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
539 procedure doAction (); override;
542 property radioGroup
: AnsiString read mRadioGroup write mRadioGroup
; //FIXME
546 // ////////////////////////////////////////////////////////////////////////// //
547 procedure uiDispatchEvent (var evt
: TFUIEvent
);
550 procedure uiFocus ();
554 // ////////////////////////////////////////////////////////////////////////// //
555 procedure uiAddWindow (ctl
: TUIControl
);
556 procedure uiRemoveWindow (ctl
: TUIControl
); // will free window if `mFreeOnClose` is `true`
557 function uiVisibleWindow (ctl
: TUIControl
): Boolean;
559 // this can return `nil` or disabled control
560 function uiGetFocusedCtl (): TUIControl
;
562 procedure uiUpdateStyles ();
565 // ////////////////////////////////////////////////////////////////////////// //
567 procedure uiLayoutCtl (ctl
: TUIControl
);
570 // ////////////////////////////////////////////////////////////////////////// //
571 procedure uiInitialize ();
572 procedure uiDeinitialize ();
575 // ////////////////////////////////////////////////////////////////////////// //
577 fuiRenderScale
: Single = 1.0;
578 uiContext
: TGxContext
= nil;
589 uiInsideDispatcher
: Boolean = false;
590 uiTopList
: array of TUIControl
= nil;
591 uiGrabCtl
: TUIControl
= nil;
594 // ////////////////////////////////////////////////////////////////////////// //
595 procedure uiDeinitialize ();
597 FreeAndNil(uiContext
);
601 procedure uiInitialize ();
603 if (uiContext
<> nil) then raise Exception
.Create('FlexUI already initialized');
604 uiContext
:= TGxContext
.Create();
608 // ////////////////////////////////////////////////////////////////////////// //
610 ctlsToKill
: array of TUIControl
= nil;
613 procedure scheduleKill (ctl
: TUIControl
);
617 if (ctl
= nil) then exit
;
619 for f
:= 0 to High(ctlsToKill
) do
621 if (ctlsToKill
[f
] = ctl
) then exit
;
622 if (ctlsToKill
[f
] = nil) then begin ctlsToKill
[f
] := ctl
; exit
; end;
624 SetLength(ctlsToKill
, Length(ctlsToKill
)+1);
625 ctlsToKill
[High(ctlsToKill
)] := ctl
;
629 procedure processKills ();
634 for f
:= 0 to High(ctlsToKill
) do
636 ctl
:= ctlsToKill
[f
];
637 if (ctl
= nil) then break
;
638 if (uiGrabCtl
<> nil) and (ctl
.isMyChild(uiGrabCtl
)) then uiGrabCtl
:= nil; // just in case
639 ctlsToKill
[f
] := nil;
642 if (Length(ctlsToKill
) > 0) then ctlsToKill
[0] := nil; // just in case
646 // ////////////////////////////////////////////////////////////////////////// //
648 knownCtlClasses
: array of record
649 klass
: TUIControlClass
;
654 procedure registerCtlClass (aklass
: TUIControlClass
; const aname
: AnsiString);
656 assert(aklass
<> nil);
657 assert(Length(aname
) > 0);
658 SetLength(knownCtlClasses
, Length(knownCtlClasses
)+1);
659 knownCtlClasses
[High(knownCtlClasses
)].klass
:= aklass
;
660 knownCtlClasses
[High(knownCtlClasses
)].name
:= aname
;
664 function findCtlClass (const aname
: AnsiString): TUIControlClass
;
668 for f
:= 0 to High(knownCtlClasses
) do
670 if (strEquCI1251(aname
, knownCtlClasses
[f
].name
)) then
672 result
:= knownCtlClasses
[f
].klass
;
680 // ////////////////////////////////////////////////////////////////////////// //
682 TFlexLayouter
= specialize TFlexLayouterBase
<TUIControl
>;
684 procedure uiLayoutCtl (ctl
: TUIControl
);
688 if (ctl
= nil) then exit
;
689 lay
:= TFlexLayouter
.Create();
691 if (not ctl
.mStyleLoaded
) then ctl
.updateStyle();
692 if (ctl
is TUITopWindow
) and (TUITopWindow(ctl
).fitToScreen
) then TUITopWindow(ctl
).flFitToScreen();
697 //writeln('============================'); lay.dumpFlat();
699 //writeln('=== initial ==='); lay.dump();
701 //lay.calcMaxSizeInternal(0);
704 writeln('=== after first pass ===');
708 writeln('=== after second pass ===');
713 //writeln('=== final ==='); lay.dump();
715 if (ctl
.mParent
= nil) and (ctl
is TUITopWindow
) and (TUITopWindow(ctl
).mDoCenter
) then
717 TUITopWindow(ctl
).centerInScreen();
720 // calculate full size
721 ctl
.calcFullClientSize();
724 if (ctl
.mParent
= nil) then
726 if (ctl
.mFocused
= nil) or (ctl
.mFocused
= ctl
) or (not ctl
.mFocused
.enabled
) then
728 ctl
.mFocused
:= ctl
.findFirstFocus();
738 // ////////////////////////////////////////////////////////////////////////// //
739 procedure uiUpdateStyles ();
743 for ctl
in uiTopList
do ctl
.updateStyle();
747 procedure uiDispatchEvent (var evt
: TFUIEvent
);
752 procedure doSink (ctl
: TUIControl
);
754 if (ctl
= nil) or (not ev
.alive
) then exit
;
755 if (ctl
.mParent
<> nil) then
758 if (not ev
.alive
) then exit
;
760 //if (ctl = destCtl) then writeln(' SINK: MINE! <', ctl.className, '>');
763 if (ctl
= destCtl
) and (ev
.alive
) then
770 procedure dispatchTo (ctl
: TUIControl
);
772 if (ctl
= nil) then exit
;
777 //ctl := ctl.mParent; // 'cause "mine" is processed in `doSink()`
778 while (ctl
<> nil) and (ev
.alive
) do
786 procedure doMouseEvent ();
794 // pass mouse events to control with grab, if there is any
795 if (uiGrabCtl
<> nil) then
797 //writeln('GRABBED: ', uiGrabCtl.className);
798 doUngrab
:= (ev
.release
) and ((ev
.bstate
and (not ev
.but
)) = 0);
799 dispatchTo(uiGrabCtl
);
800 //FIXME: create API to get grabs, so control can regrab itself event on release
801 if (doUngrab
) and (uiGrabCtl
= destCtl
) then uiGrabCtl
:= nil;
806 if (Length(uiTopList
) > 0) then win
:= uiTopList
[High(uiTopList
)] else win
:= nil;
807 // check if we're still in top window
808 if (ev
.press
) and (win
<> nil) and (not win
.toLocal(0, 0, lx
, ly
)) then
810 // we have other windows too; check for window switching
811 for f
:= High(uiTopList
)-1 downto 0 do
813 if (uiTopList
[f
].enabled
) and (uiTopList
[f
].toLocal(ev
.x
, ev
.y
, lx
, ly
)) then
818 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
819 uiTopList
[High(uiTopList
)] := win
;
826 if (win
<> nil) and (win
.toLocal(ev
.x
, ev
.y
, lx
, ly
)) then
828 ctl
:= win
.controlAtXY(ev
.x
, ev
.y
); // don't allow disabled controls
829 if (ctl
= nil) or (not ctl
.canFocus
) or (not ctl
.enabled
) then ctl
:= win
;
830 // pass focus to another event and set grab, if necessary
833 // pass focus, if necessary
834 if (win
.mFocused
<> ctl
) then
836 if (win
.mFocused
<> nil) then win
.mFocused
.blurred();
839 if (ctl
<> win
) then ctl
.activated();
851 svx
, svy
, svdx
, svdy
: Integer;
856 if (not evt
.alive
) then exit
;
857 odp
:= uiInsideDispatcher
;
858 uiInsideDispatcher
:= true;
859 //writeln('ENTER: FUI DISPATCH');
861 // normalize mouse coordinates
862 svscale
:= fuiRenderScale
;
863 ev
.x
:= trunc(ev
.x
/svscale
);
864 ev
.y
:= trunc(ev
.y
/svscale
);
865 ev
.dx
:= trunc(ev
.dx
/svscale
); //FIXME
866 ev
.dy
:= trunc(ev
.dy
/svscale
); //FIXME
872 // "event grab" eats only mouse events
875 // we need to so some special processing here
880 // simply dispatch to focused control
881 dispatchTo(uiGetFocusedCtl
);
884 uiInsideDispatcher
:= odp
;
885 if (ev
.x
= svx
) and (ev
.y
= svy
) and (ev
.dx
= svdx
) and (ev
.dy
= svdy
) then
887 // due to possible precision loss
902 evt
.x
:= trunc(evt
.x
*svscale
);
903 evt
.y
:= trunc(evt
.y
*svscale
);
904 evt
.dx
:= trunc(evt
.dx
*svscale
);
905 evt
.dy
:= trunc(evt
.dy
*svscale
);
909 //writeln('EXIT: FUI DISPATCH');
912 procedure uiFocus ();
914 if (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)].enabled
) then uiTopList
[High(uiTopList
)].activated();
920 if (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)].enabled
) then uiTopList
[High(uiTopList
)].blurred();
930 //if (uiContext = nil) then uiContext := TGxContext.Create();
931 gxSetContext(uiContext
, fuiRenderScale
);
932 uiContext
.resetClip();
934 for f
:= 0 to High(uiTopList
) do
938 if (f
<> High(uiTopList
)) then
940 cidx
:= ctl
.getColorIndex
;
941 uiContext
.darkenRect(ctl
.x0
, ctl
.y0
, ctl
.width
, ctl
.height
, ctl
.mDarken
[cidx
]);
950 function uiGetFocusedCtl (): TUIControl
;
953 if (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)].enabled
) then
955 result
:= uiTopList
[High(uiTopList
)].mFocused
;
956 if (result
= nil) then result
:= uiTopList
[High(uiTopList
)];
961 procedure uiAddWindow (ctl
: TUIControl
);
965 if (ctl
= nil) then exit
;
967 if not (ctl
is TUITopWindow
) then exit
; // alas
968 for f
:= 0 to High(uiTopList
) do
970 if (uiTopList
[f
] = ctl
) then
972 if (f
<> High(uiTopList
)) then
974 if (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)].enabled
) then uiTopList
[High(uiTopList
)].blurred();
975 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
976 uiTopList
[High(uiTopList
)] := ctl
;
982 if (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)].enabled
) then uiTopList
[High(uiTopList
)].blurred();
983 SetLength(uiTopList
, Length(uiTopList
)+1);
984 uiTopList
[High(uiTopList
)] := ctl
;
985 if (not ctl
.mStyleLoaded
) then ctl
.updateStyle();
990 procedure uiRemoveWindow (ctl
: TUIControl
);
994 if (ctl
= nil) then exit
;
996 if not (ctl
is TUITopWindow
) then exit
; // alas
997 for f
:= 0 to High(uiTopList
) do
999 if (uiTopList
[f
] = ctl
) then
1002 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
1003 SetLength(uiTopList
, Length(uiTopList
)-1);
1004 if (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)].enabled
) then uiTopList
[High(uiTopList
)].activated();
1005 if (ctl
is TUITopWindow
) then
1008 if assigned(TUITopWindow(ctl
).closeCB
) then TUITopWindow(ctl
).closeCB(ctl
);
1010 if (TUITopWindow(ctl
).mFreeOnClose
) then scheduleKill(ctl
);
1019 function uiVisibleWindow (ctl
: TUIControl
): Boolean;
1024 if (ctl
= nil) then exit
;
1025 ctl
:= ctl
.topLevel
;
1026 if not (ctl
is TUITopWindow
) then exit
; // alas
1027 for f
:= 0 to High(uiTopList
) do
1029 if (uiTopList
[f
] = ctl
) then begin result
:= true; exit
; end;
1034 // ////////////////////////////////////////////////////////////////////////// //
1035 constructor TUIControl
.Create ();
1040 procedure TUIControl
.AfterConstruction ();
1048 mHeight
:= uiContext
.charHeight(' ');
1056 mDrawShadow
:= false;
1058 // layouter interface
1059 //mDefSize := TLaySize.Create(64, uiContext.charHeight(' ')); // default size
1060 mDefSize
:= TLaySize
.Create(0, 0); // default size: hidden control
1061 mMaxSize
:= TLaySize
.Create(-1, -1); // maximum size
1062 mPadding
:= TLaySize
.Create(0, 0);
1070 mAlign
:= -1; // left/top
1072 mStyleLoaded
:= false;
1076 destructor TUIControl
.Destroy ();
1079 doActivateOtherWin
: Boolean = false;
1081 if (uiInsideDispatcher
) then raise Exception
.Create('FlexUI: cannot destroy objects in event dispatcher');
1082 if (uiGrabCtl
= self
) then uiGrabCtl
:= nil;
1083 // just in case, check if this is top-level shit
1084 for f
:= 0 to High(uiTopList
) do
1086 if (uiTopList
[f
] = self
) then
1088 if (uiGrabCtl
<> nil) and (isMyChild(uiGrabCtl
)) then uiGrabCtl
:= nil;
1089 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
1090 SetLength(uiTopList
, Length(uiTopList
)-1);
1091 doActivateOtherWin
:= true;
1095 if (doActivateOtherWin
) and (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)].enabled
) then
1097 uiTopList
[High(uiTopList
)].activated();
1100 if (mParent
<> nil) then
1103 for f
:= 0 to High(mParent
.mChildren
) do
1105 if (mParent
.mChildren
[f
] = self
) then
1107 for c
:= f
+1 to High(mParent
.mChildren
) do mParent
.mChildren
[c
-1] := mParent
.mChildren
[c
];
1108 SetLength(mParent
.mChildren
, Length(mParent
.mChildren
)-1);
1112 for f
:= 0 to High(mChildren
) do
1114 mChildren
[f
].mParent
:= nil;
1115 mChildren
[f
].Free();
1121 function TUIControl
.getColorIndex (): Integer; inline;
1123 if (not enabled
) then begin result
:= ClrIdxDisabled
; exit
; end;
1124 // top windows: no focus hack
1125 if (self
is TUITopWindow
) then
1127 if (getActive
) then begin result
:= ClrIdxActive
; exit
; end;
1131 // if control cannot be focused, take "active" color scheme for it (it is easier this way)
1132 if (not canFocus
) or (getActive
) then begin result
:= ClrIdxActive
; exit
; end;
1134 result
:= ClrIdxInactive
;
1137 procedure TUIControl
.updateStyle ();
1139 stl
: TUIStyle
= nil;
1143 while (ctl
<> nil) do
1145 if (Length(ctl
.mStyleId
) <> 0) then begin stl
:= uiFindStyle(ctl
.mStyleId
); break
; end;
1148 if (stl
= nil) then stl
:= uiFindStyle(''); // default
1150 for ctl
in mChildren
do ctl
.updateStyle();
1151 mStyleLoaded
:= true;
1154 procedure TUIControl
.cacheStyle (root
: TUIStyle
);
1158 //writeln('caching style for <', className, '> (', mCtl4Style, ')...');
1161 mBackColor
[ClrIdxActive
] := root
.get('back-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(0, 0, 128));
1162 mTextColor
[ClrIdxActive
] := root
.get('text-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
1163 mFrameColor
[ClrIdxActive
] := root
.get('frame-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
1164 mFrameTextColor
[ClrIdxActive
] := root
.get('frame-text-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
1165 mFrameIconColor
[ClrIdxActive
] := root
.get('frame-icon-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(0, 255, 0));
1166 mSBarFullColor
[ClrIdxActive
] := root
.get('scrollbar-full-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
1167 mSBarEmptyColor
[ClrIdxActive
] := root
.get('scrollbar-empty-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(128, 128, 128));
1168 mDarken
[ClrIdxActive
] := root
.get('darken', 'active', cst
).asInt(666);
1170 mBackColor
[ClrIdxDisabled
] := root
.get('back-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(0, 0, 128));
1171 mTextColor
[ClrIdxDisabled
] := root
.get('text-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(127, 127, 127));
1172 mFrameColor
[ClrIdxDisabled
] := root
.get('frame-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(127, 127, 127));
1173 mFrameTextColor
[ClrIdxDisabled
] := root
.get('frame-text-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(127, 127, 127));
1174 mFrameIconColor
[ClrIdxDisabled
] := root
.get('frame-icon-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(0, 127, 0));
1175 mSBarFullColor
[ClrIdxDisabled
] := root
.get('scrollbar-full-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(127, 127, 127));
1176 mSBarEmptyColor
[ClrIdxDisabled
] := root
.get('scrollbar-empty-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(98, 98, 98));
1177 mDarken
[ClrIdxDisabled
] := root
.get('darken', 'disabled', cst
).asInt(666);
1179 mBackColor
[ClrIdxInactive
] := root
.get('back-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(0, 0, 128));
1180 mTextColor
[ClrIdxInactive
] := root
.get('text-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
1181 mFrameColor
[ClrIdxInactive
] := root
.get('frame-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
1182 mFrameTextColor
[ClrIdxInactive
] := root
.get('frame-text-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
1183 mFrameIconColor
[ClrIdxInactive
] := root
.get('frame-icon-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(0, 255, 0));
1184 mSBarFullColor
[ClrIdxInactive
] := root
.get('scrollbar-full-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
1185 mSBarEmptyColor
[ClrIdxInactive
] := root
.get('scrollbar-empty-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(128, 128, 128));
1186 mDarken
[ClrIdxInactive
] := root
.get('darken', 'inactive', cst
).asInt(666);
1190 // ////////////////////////////////////////////////////////////////////////// //
1191 function TUIControl
.getDefSize (): TLaySize
; inline; begin result
:= mLayDefSize
; end;
1192 function TUIControl
.getMaxSize (): TLaySize
; inline; begin result
:= mLayMaxSize
; end;
1193 function TUIControl
.getPadding (): TLaySize
; inline; begin result
:= mPadding
; end;
1194 function TUIControl
.getFlex (): Integer; inline; begin result
:= mFlex
; end;
1195 function TUIControl
.isHorizBox (): Boolean; inline; begin result
:= mHoriz
; end;
1196 function TUIControl
.noPad (): Boolean; inline; begin result
:= mNoPad
; end;
1197 function TUIControl
.getAlign (): Integer; inline; begin result
:= mAlign
; end;
1198 function TUIControl
.getExpand (): Boolean; inline; begin result
:= mExpand
; end;
1199 function TUIControl
.getHGroup (): AnsiString; inline; begin result
:= mHGroup
; end;
1200 function TUIControl
.getVGroup (): AnsiString; inline; begin result
:= mVGroup
; end;
1201 function TUIControl
.getMargins (): TLayMargins
; inline; begin result
:= TLayMargins
.Create(mFrameHeight
, mFrameWidth
, mFrameHeight
, mFrameWidth
); end;
1203 procedure TUIControl
.setActualSizePos (constref apos
: TLayPos
; constref asize
: TLaySize
); inline;
1205 //writeln(self.className, '; pos=', apos.toString, '; size=', asize.toString);
1206 if (mParent
<> nil) then
1213 if (mLayMaxSize
.w
>= 0) then mWidth
:= nmin(mWidth
, mLayMaxSize
.w
);
1214 if (mLayMaxSize
.h
>= 0) then mHeight
:= nmin(mHeight
, mLayMaxSize
.h
);
1217 procedure TUIControl
.layPrepare ();
1219 mLayDefSize
:= mDefSize
;
1220 if (mLayDefSize
.w
<> 0) or (mLayDefSize
.h
<> 0) then
1222 mLayMaxSize
:= mMaxSize
;
1223 if (mLayMaxSize
.w
>= 0) then begin mLayDefSize
.w
+= mFrameWidth
*2; mLayMaxSize
.w
+= mFrameWidth
*2; end;
1224 if (mLayMaxSize
.h
>= 0) then begin mLayDefSize
.h
+= mFrameHeight
*2; mLayMaxSize
.h
+= mFrameHeight
*2; end;
1228 mLayMaxSize
:= TLaySize
.Create(0, 0);
1233 // ////////////////////////////////////////////////////////////////////////// //
1234 function TUIControl
.parsePos (par
: TTextParser
): TLayPos
;
1236 ech
: AnsiChar = ')';
1238 if (par
.eatDelim('[')) then ech
:= ']' else par
.expectDelim('(');
1239 result
.x
:= par
.expectInt();
1240 par
.eatDelim(','); // optional comma
1241 result
.y
:= par
.expectInt();
1242 par
.eatDelim(','); // optional comma
1243 par
.expectDelim(ech
);
1246 function TUIControl
.parseSize (par
: TTextParser
): TLaySize
;
1248 ech
: AnsiChar = ')';
1250 if (par
.eatDelim('[')) then ech
:= ']' else par
.expectDelim('(');
1251 result
.w
:= par
.expectInt();
1252 par
.eatDelim(','); // optional comma
1253 result
.h
:= par
.expectInt();
1254 par
.eatDelim(','); // optional comma
1255 par
.expectDelim(ech
);
1258 function TUIControl
.parsePadding (par
: TTextParser
): TLaySize
;
1260 result
:= parseSize(par
);
1263 function TUIControl
.parseHPadding (par
: TTextParser
; def
: Integer): TLaySize
;
1268 result
.w
:= par
.expectInt();
1272 result
:= parsePadding(par
);
1276 function TUIControl
.parseVPadding (par
: TTextParser
; def
: Integer): TLaySize
;
1281 result
.h
:= par
.expectInt();
1285 result
:= parsePadding(par
);
1289 function TUIControl
.parseBool (par
: TTextParser
): Boolean;
1292 par
.eatIdOrStrCI('true') or
1293 par
.eatIdOrStrCI('yes') or
1294 par
.eatIdOrStrCI('tan');
1297 if (not par
.eatIdOrStrCI('false')) and (not par
.eatIdOrStrCI('no')) and (not par
.eatIdOrStrCI('ona')) then
1299 par
.error('boolean value expected');
1304 function TUIControl
.parseAnyAlign (par
: TTextParser
): Integer;
1306 if (par
.eatIdOrStrCI('left')) or (par
.eatIdOrStrCI('top')) then result
:= -1
1307 else if (par
.eatIdOrStrCI('right')) or (par
.eatIdOrStrCI('bottom')) then result
:= 1
1308 else if (par
.eatIdOrStrCI('center')) then result
:= 0
1309 else par
.error('invalid align value');
1312 function TUIControl
.parseHAlign (par
: TTextParser
): Integer;
1314 if (par
.eatIdOrStrCI('left')) then result
:= -1
1315 else if (par
.eatIdOrStrCI('right')) then result
:= 1
1316 else if (par
.eatIdOrStrCI('center')) then result
:= 0
1317 else par
.error('invalid horizontal align value');
1320 function TUIControl
.parseVAlign (par
: TTextParser
): Integer;
1322 if (par
.eatIdOrStrCI('top')) then result
:= -1
1323 else if (par
.eatIdOrStrCI('bottom')) then result
:= 1
1324 else if (par
.eatIdOrStrCI('center')) then result
:= 0
1325 else par
.error('invalid vertical align value');
1328 procedure TUIControl
.parseTextAlign (par
: TTextParser
; var h
, v
: Integer);
1330 wasH
: Boolean = false;
1331 wasV
: Boolean = false;
1335 if (par
.eatIdOrStrCI('left')) then
1337 if wasH
then par
.error('too many align directives');
1342 if (par
.eatIdOrStrCI('right')) then
1344 if wasH
then par
.error('too many align directives');
1349 if (par
.eatIdOrStrCI('hcenter')) then
1351 if wasH
then par
.error('too many align directives');
1356 if (par
.eatIdOrStrCI('top')) then
1358 if wasV
then par
.error('too many align directives');
1363 if (par
.eatIdOrStrCI('bottom')) then
1365 if wasV
then par
.error('too many align directives');
1370 if (par
.eatIdOrStrCI('vcenter')) then
1372 if wasV
then par
.error('too many align directives');
1377 if (par
.eatIdOrStrCI('center')) then
1379 if wasV
or wasH
then par
.error('too many align directives');
1388 if not wasV
and not wasH
then par
.error('invalid align value');
1391 function TUIControl
.parseOrientation (const prname
: AnsiString; par
: TTextParser
): Boolean;
1393 if (strEquCI1251(prname
, 'orientation')) or (strEquCI1251(prname
, 'orient')) then
1395 if (par
.eatIdOrStrCI('horizontal')) or (par
.eatIdOrStrCI('horiz')) then mHoriz
:= true
1396 else if (par
.eatIdOrStrCI('vertical')) or (par
.eatIdOrStrCI('vert')) then mHoriz
:= false
1397 else par
.error('`horizontal` or `vertical` expected');
1406 // par should be on '{'; final '}' is eaten
1407 procedure TUIControl
.parseProperties (par
: TTextParser
);
1411 if (not par
.eatDelim('{')) then exit
;
1412 while (not par
.eatDelim('}')) do
1414 if (not par
.isIdOrStr
) then par
.error('property name expected');
1417 par
.eatDelim(':'); // optional
1418 if not parseProperty(pn
, par
) then par
.errorfmt('invalid property name ''%s''', [pn
]);
1419 par
.eatDelim(','); // optional
1423 // par should be on '{'
1424 procedure TUIControl
.parseChildren (par
: TTextParser
);
1426 cc
: TUIControlClass
;
1429 par
.expectDelim('{');
1430 while (not par
.eatDelim('}')) do
1432 if (not par
.isIdOrStr
) then par
.error('control name expected');
1433 cc
:= findCtlClass(par
.tokStr
);
1434 if (cc
= nil) then par
.errorfmt('unknown control name: ''%s''', [par
.tokStr
]);
1435 //writeln('children for <', par.tokStr, '>: <', cc.className, '>');
1437 par
.eatDelim(':'); // optional
1439 //writeln(' mHoriz=', ctl.mHoriz);
1441 ctl
.parseProperties(par
);
1446 //writeln(': ', ctl.mDefSize.toString);
1448 par
.eatDelim(','); // optional
1453 function TUIControl
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1456 if (strEquCI1251(prname
, 'id')) then begin mId
:= par
.expectIdOrStr(true); exit
; end; // allow empty strings
1457 if (strEquCI1251(prname
, 'style')) then begin mStyleId
:= par
.expectIdOrStr(); exit
; end; // no empty strings
1458 if (strEquCI1251(prname
, 'flex')) then begin flex
:= par
.expectInt(); exit
; end;
1460 if (strEquCI1251(prname
, 'defsize')) or (strEquCI1251(prname
, 'size')) then begin mDefSize
:= parseSize(par
); exit
; end;
1461 if (strEquCI1251(prname
, 'maxsize')) then begin mMaxSize
:= parseSize(par
); exit
; end;
1462 if (strEquCI1251(prname
, 'defwidth')) or (strEquCI1251(prname
, 'width')) then begin mDefSize
.w
:= par
.expectInt(); exit
; end;
1463 if (strEquCI1251(prname
, 'defheight')) or (strEquCI1251(prname
, 'height')) then begin mDefSize
.h
:= par
.expectInt(); exit
; end;
1464 if (strEquCI1251(prname
, 'maxwidth')) then begin mMaxSize
.w
:= par
.expectInt(); exit
; end;
1465 if (strEquCI1251(prname
, 'maxheight')) then begin mMaxSize
.h
:= par
.expectInt(); exit
; end;
1467 if (strEquCI1251(prname
, 'padding')) then begin mPadding
:= parsePadding(par
); exit
; end;
1468 if (strEquCI1251(prname
, 'nopad')) then begin mNoPad
:= true; exit
; end;
1470 if (strEquCI1251(prname
, 'expand')) then begin mExpand
:= parseBool(par
); exit
; end;
1472 if (strEquCI1251(prname
, 'align')) then begin mAlign
:= parseAnyAlign(par
); exit
; end;
1473 if (strEquCI1251(prname
, 'hgroup')) then begin mHGroup
:= par
.expectIdOrStr(true); exit
; end; // allow empty strings
1474 if (strEquCI1251(prname
, 'vgroup')) then begin mVGroup
:= par
.expectIdOrStr(true); exit
; end; // allow empty strings
1476 if (strEquCI1251(prname
, 'canfocus')) then begin mCanFocus
:= true; exit
; end;
1477 if (strEquCI1251(prname
, 'nofocus')) then begin mCanFocus
:= false; exit
; end;
1478 if (strEquCI1251(prname
, 'disabled')) then begin mEnabled
:= false; exit
; end;
1479 if (strEquCI1251(prname
, 'enabled')) then begin mEnabled
:= true; exit
; end;
1480 if (strEquCI1251(prname
, 'escclose')) then begin mEscClose
:= not parseBool(par
); exit
; end;
1481 if (strEquCI1251(prname
, 'default')) then begin mDefault
:= true; exit
; end;
1482 if (strEquCI1251(prname
, 'cancel')) then begin mCancel
:= true; exit
; end;
1487 // ////////////////////////////////////////////////////////////////////////// //
1488 procedure TUIControl
.activated ();
1490 makeVisibleInParent();
1494 procedure TUIControl
.blurred ();
1496 if (uiGrabCtl
= self
) then uiGrabCtl
:= nil;
1500 procedure TUIControl
.calcFullClientSize ();
1504 mFullSize
:= TLaySize
.Create(0, 0);
1505 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1506 for ctl
in mChildren
do
1508 ctl
.calcFullClientSize();
1509 mFullSize
.w
:= nmax(mFullSize
.w
, ctl
.mX
-mFrameWidth
+ctl
.mFullSize
.w
);
1510 mFullSize
.h
:= nmax(mFullSize
.h
, ctl
.mY
-mFrameHeight
+ctl
.mFullSize
.h
);
1512 mFullSize
.w
:= nmax(mFullSize
.w
, mWidth
-mFrameWidth
*2);
1513 mFullSize
.h
:= nmax(mFullSize
.h
, mHeight
-mFrameHeight
*2);
1517 function TUIControl
.topLevel (): TUIControl
; inline;
1520 while (result
.mParent
<> nil) do result
:= result
.mParent
;
1524 function TUIControl
.getEnabled (): Boolean;
1529 if (not mEnabled
) then exit
;
1531 while (ctl
<> nil) do
1533 if (not ctl
.mEnabled
) then exit
;
1540 procedure TUIControl
.setEnabled (v
: Boolean); inline;
1542 if (mEnabled
= v
) then exit
;
1544 if (not v
) and focused
then setFocused(false);
1548 function TUIControl
.getFocused (): Boolean; inline;
1550 if (mParent
= nil) then
1552 result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = self
);
1556 result
:= (topLevel
.mFocused
= self
);
1557 if (result
) then result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = topLevel
);
1562 function TUIControl
.getActive (): Boolean; inline;
1566 if (mParent
= nil) then
1568 result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = self
);
1572 ctl
:= topLevel
.mFocused
;
1573 while (ctl
<> nil) and (ctl
<> self
) do ctl
:= ctl
.mParent
;
1574 result
:= (ctl
= self
);
1575 if (result
) then result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = topLevel
);
1580 procedure TUIControl
.setFocused (v
: Boolean); inline;
1587 if (tl
.mFocused
= self
) then
1589 blurred(); // this will reset grab, but still...
1590 if (uiGrabCtl
= self
) then uiGrabCtl
:= nil;
1591 tl
.mFocused
:= tl
.findNextFocus(self
, true);
1592 if (tl
.mFocused
= self
) then tl
.mFocused
:= nil;
1593 if (tl
.mFocused
<> nil) then tl
.mFocused
.activated();
1597 if (not canFocus
) then exit
;
1598 if (tl
.mFocused
<> self
) then
1600 if (tl
.mFocused
<> nil) then tl
.mFocused
.blurred();
1601 tl
.mFocused
:= self
;
1602 if (uiGrabCtl
<> self
) then uiGrabCtl
:= nil;
1608 function TUIControl
.getCanFocus (): Boolean; inline;
1610 result
:= (getEnabled
) and (mCanFocus
) and (mWidth
> 0) and (mHeight
> 0);
1614 function TUIControl
.isMyChild (ctl
: TUIControl
): Boolean;
1617 while (ctl
<> nil) do
1619 if (ctl
.mParent
= self
) then exit
;
1626 // returns `true` if global coords are inside this control
1627 function TUIControl
.toLocal (var x
, y
: Integer): Boolean;
1629 if (mParent
= nil) then
1633 result
:= true; // hack
1637 result
:= mParent
.toLocal(x
, y
);
1638 Inc(x
, mParent
.mScrollX
);
1639 Inc(y
, mParent
.mScrollY
);
1642 if result
then result
:= (x
>= 0) and (y
>= 0) and (x
< mParent
.mWidth
) and (y
< mParent
.mHeight
);
1644 if result
then result
:= (x
>= 0) and (y
>= 0) and (x
< mWidth
) and (y
< mHeight
);
1647 function TUIControl
.toLocal (gx
, gy
: Integer; out x
, y
: Integer): Boolean; inline;
1651 result
:= toLocal(x
, y
);
1655 procedure TUIControl
.toGlobal (var x
, y
: Integer);
1659 if (mParent
<> nil) then
1661 Dec(x
, mParent
.mScrollX
);
1662 Dec(y
, mParent
.mScrollY
);
1663 mParent
.toGlobal(x
, y
);
1667 procedure TUIControl
.toGlobal (lx
, ly
: Integer; out x
, y
: Integer); inline;
1674 procedure TUIControl
.getDrawRect (out gx
, gy
, wdt
, hgt
: Integer);
1678 if (mParent
= nil) then
1687 toGlobal(0, 0, cgx
, cgy
);
1688 mParent
.getDrawRect(gx
, gy
, wdt
, hgt
);
1689 if (wdt
> 0) and (hgt
> 0) then
1691 if (not intersectRect(gx
, gy
, wdt
, hgt
, cgx
, cgy
, mWidth
, mHeight
)) then
1701 // x and y are global coords
1702 function TUIControl
.controlAtXY (x
, y
: Integer; allowDisabled
: Boolean=false): TUIControl
;
1708 if (not allowDisabled
) and (not enabled
) then exit
;
1709 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1710 if not toLocal(x
, y
, lx
, ly
) then exit
;
1711 for f
:= High(mChildren
) downto 0 do
1713 result
:= mChildren
[f
].controlAtXY(x
, y
, allowDisabled
);
1714 if (result
<> nil) then exit
;
1720 function TUIControl
.parentScrollX (): Integer; inline; begin if (mParent
<> nil) then result
:= mParent
.mScrollX
else result
:= 0; end;
1721 function TUIControl
.parentScrollY (): Integer; inline; begin if (mParent
<> nil) then result
:= mParent
.mScrollY
else result
:= 0; end;
1724 procedure TUIControl
.makeVisibleInParent ();
1726 sy
, ey
, cy
: Integer;
1729 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1731 if (p
= nil) then exit
;
1732 if (p
.mFullSize
.w
< 1) or (p
.mFullSize
.h
< 1) then
1738 p
.makeVisibleInParent();
1739 cy
:= mY
-p
.mFrameHeight
;
1741 ey
:= sy
+(p
.mHeight
-p
.mFrameHeight
*2);
1744 p
.mScrollY
:= nmax(0, cy
);
1746 else if (cy
+mHeight
> ey
) then
1748 p
.mScrollY
:= nmax(0, cy
+mHeight
-(p
.mHeight
-p
.mFrameHeight
*2));
1753 // ////////////////////////////////////////////////////////////////////////// //
1754 function TUIControl
.prevSibling (): TUIControl
;
1758 if (mParent
<> nil) then
1760 for f
:= 1 to High(mParent
.mChildren
) do
1762 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
-1]; exit
; end;
1768 function TUIControl
.nextSibling (): TUIControl
;
1772 if (mParent
<> nil) then
1774 for f
:= 0 to High(mParent
.mChildren
)-1 do
1776 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
+1]; exit
; end;
1782 function TUIControl
.firstChild (): TUIControl
; inline;
1784 if (Length(mChildren
) <> 0) then result
:= mChildren
[0] else result
:= nil;
1787 function TUIControl
.lastChild (): TUIControl
; inline;
1789 if (Length(mChildren
) <> 0) then result
:= mChildren
[High(mChildren
)] else result
:= nil;
1793 function TUIControl
.findFirstFocus (): TUIControl
;
1800 for f
:= 0 to High(mChildren
) do
1802 result
:= mChildren
[f
].findFirstFocus();
1803 if (result
<> nil) then exit
;
1805 if (canFocus
) then result
:= self
;
1810 function TUIControl
.findLastFocus (): TUIControl
;
1817 for f
:= High(mChildren
) downto 0 do
1819 result
:= mChildren
[f
].findLastFocus();
1820 if (result
<> nil) then exit
;
1822 if (canFocus
) then result
:= self
;
1827 function TUIControl
.findNextFocus (cur
: TUIControl
; wrap
: Boolean): TUIControl
;
1829 curHit
: Boolean = false;
1831 function checkFocus (ctl
: TUIControl
): Boolean;
1835 result
:= (ctl
.canFocus
);
1839 curHit
:= (ctl
= cur
);
1840 result
:= false; // don't stop
1848 if not isMyChild(cur
) then
1850 result
:= findFirstFocus();
1854 result
:= forEachControl(checkFocus
);
1855 if (result
= nil) and (wrap
) then result
:= findFirstFocus();
1861 function TUIControl
.findPrevFocus (cur
: TUIControl
; wrap
: Boolean): TUIControl
;
1863 lastCtl
: TUIControl
= nil;
1865 function checkFocus (ctl
: TUIControl
): Boolean;
1874 if (ctl
.canFocus
) then lastCtl
:= ctl
;
1882 if not isMyChild(cur
) then
1884 result
:= findLastFocus();
1888 forEachControl(checkFocus
);
1889 if (lastCtl
= nil) and (wrap
) then lastCtl
:= findLastFocus();
1891 //if (lastCtl <> nil) then writeln('ctl<', lastCtl.className, '>: {', lastCtl.id, '}');
1897 function TUIControl
.findDefaulControl (): TUIControl
;
1903 if (mDefault
) then begin result
:= self
; exit
; end;
1904 for ctl
in mChildren
do
1906 result
:= ctl
.findDefaulControl();
1907 if (result
<> nil) then exit
;
1913 function TUIControl
.findCancelControl (): TUIControl
;
1919 if (mCancel
) then begin result
:= self
; exit
; end;
1920 for ctl
in mChildren
do
1922 result
:= ctl
.findCancelControl();
1923 if (result
<> nil) then exit
;
1930 function TUIControl
.findControlById (const aid
: AnsiString): TUIControl
;
1934 if (strEquCI1251(aid
, mId
)) then begin result
:= self
; exit
; end;
1935 for ctl
in mChildren
do
1937 result
:= ctl
.findControlById(aid
);
1938 if (result
<> nil) then exit
;
1944 procedure TUIControl
.appendChild (ctl
: TUIControl
);
1946 if (ctl
= nil) then exit
;
1947 if (ctl
.mParent
<> nil) then exit
;
1948 SetLength(mChildren
, Length(mChildren
)+1);
1949 mChildren
[High(mChildren
)] := ctl
;
1950 ctl
.mParent
:= self
;
1951 Inc(ctl
.mX
, mFrameWidth
);
1952 Inc(ctl
.mY
, mFrameHeight
);
1953 if (ctl
.mWidth
> 0) and (ctl
.mHeight
> 0) and
1954 (ctl
.mX
+ctl
.mWidth
> mFrameWidth
) and (ctl
.mY
+ctl
.mHeight
> mFrameHeight
) then
1956 if (mWidth
+mFrameWidth
< ctl
.mX
+ctl
.mWidth
) then mWidth
:= ctl
.mX
+ctl
.mWidth
+mFrameWidth
;
1957 if (mHeight
+mFrameHeight
< ctl
.mY
+ctl
.mHeight
) then mHeight
:= ctl
.mY
+ctl
.mHeight
+mFrameHeight
;
1962 function TUIControl
.setActionCBFor (const aid
: AnsiString; cb
: TActionCB
): TActionCB
;
1967 if (ctl
<> nil) then
1969 result
:= ctl
.actionCB
;
1979 function TUIControl
.forEachChildren (cb
: TCtlEnumCB
): TUIControl
;
1984 if (not assigned(cb
)) then exit
;
1985 for ctl
in mChildren
do
1987 if cb(ctl
) then begin result
:= ctl
; exit
; end;
1992 function TUIControl
.forEachControl (cb
: TCtlEnumCB
; includeSelf
: Boolean=true): TUIControl
;
1994 function forChildren (p
: TUIControl
; incSelf
: Boolean): TUIControl
;
1999 if (p
= nil) then exit
;
2000 if (incSelf
) and (cb(p
)) then begin result
:= p
; exit
; end;
2001 for ctl
in p
.mChildren
do
2003 result
:= forChildren(ctl
, true);
2004 if (result
<> nil) then break
;
2010 if (not assigned(cb
)) then exit
;
2011 result
:= forChildren(self
, includeSelf
);
2015 procedure TUIControl
.close (); // this closes *top-level* control
2020 uiRemoveWindow(ctl
);
2021 if (ctl
is TUITopWindow
) and (TUITopWindow(ctl
).mFreeOnClose
) then scheduleKill(ctl
); // just in case
2025 procedure TUIControl
.doAction ();
2027 if assigned(actionCB
) then actionCB(self
);
2031 // ////////////////////////////////////////////////////////////////////////// //
2032 procedure TUIControl
.setScissor (lx
, ly
, lw
, lh
: Integer);
2034 gx
, gy
, wdt
, hgt
, cgx
, cgy
: Integer;
2036 if (not intersectRect(lx
, ly
, lw
, lh
, 0, 0, mWidth
, mHeight
)) then
2038 uiContext
.clip
:= TGxRect
.Create(0, 0, 0, 0);
2042 getDrawRect(gx
, gy
, wdt
, hgt
);
2044 toGlobal(lx
, ly
, cgx
, cgy
);
2045 if (not intersectRect(gx
, gy
, wdt
, hgt
, cgx
, cgy
, lw
, lh
)) then
2047 uiContext
.clip
:= TGxRect
.Create(0, 0, 0, 0);
2051 uiContext
.clip
:= savedClip
;
2052 uiContext
.combineClip(TGxRect
.Create(gx
, gy
, wdt
, hgt
));
2053 //uiContext.clip := TGxRect.Create(gx, gy, wdt, hgt);
2056 procedure TUIControl
.resetScissorNC (); inline;
2058 setScissor(0, 0, mWidth
, mHeight
);
2061 procedure TUIControl
.resetScissor (); inline;
2063 if ((mFrameWidth
<= 0) and (mFrameHeight
<= 0)) then
2069 setScissor(mFrameWidth
, mFrameHeight
, mWidth
-mFrameWidth
*2, mHeight
-mFrameHeight
*2);
2074 // ////////////////////////////////////////////////////////////////////////// //
2075 procedure TUIControl
.drawFrame (gx
, gy
, resx
, thalign
: Integer; const text: AnsiString; dbl
: Boolean);
2077 cidx
, tx
, tw
: Integer;
2079 if (mFrameWidth
< 1) or (mFrameHeight
< 1) then exit
;
2080 cidx
:= getColorIndex
;
2081 uiContext
.color
:= mFrameColor
[cidx
];
2082 case mFrameHeight
of
2087 uiContext
.rect(gx
+3, gy
+3, mWidth
-6, mHeight
-6);
2088 uiContext
.rect(gx
+5, gy
+5, mWidth
-10, mHeight
-10);
2092 uiContext
.rect(gx
+4, gy
+4, mWidth
-8, mHeight
-8);
2099 uiContext
.rect(gx
+3, gy
+3+3, mWidth
-6, mHeight
-6-6);
2100 uiContext
.rect(gx
+5, gy
+5+3, mWidth
-10, mHeight
-10-6);
2104 uiContext
.rect(gx
+4, gy
+4+3, mWidth
-8, mHeight
-8-6);
2111 uiContext
.rect(gx
+3, gy
+3+4, mWidth
-6, mHeight
-6-8);
2112 uiContext
.rect(gx
+5, gy
+5+4, mWidth
-10, mHeight
-10-8);
2116 uiContext
.rect(gx
+4, gy
+4+4, mWidth
-8, mHeight
-8-8);
2132 if (Length(text) > 0) then
2134 if (resx
< 0) then resx
:= 0;
2135 tw
:= uiContext
.textWidth(text);
2136 setScissor(mFrameWidth
+resx
, 0, mWidth
-mFrameWidth
*2-resx
, mFrameHeight
);
2137 if (thalign
< 0) then tx
:= gx
+resx
+mFrameWidth
+2
2138 else if (thalign
> 0) then tx
:= gx
+mWidth
-mFrameWidth
-1-tw
2139 else tx
:= (gx
+resx
+mFrameWidth
)+(mWidth
-mFrameWidth
*2-resx
-tw
) div 2;
2140 uiContext
.color
:= mBackColor
[cidx
];
2141 uiContext
.fillRect(tx
-2, gy
, tw
+4, mFrameHeight
);
2142 uiContext
.color
:= mFrameTextColor
[cidx
];
2143 uiContext
.drawText(tx
, gy
, text);
2148 procedure TUIControl
.draw ();
2154 if (mWidth
< 1) or (mHeight
< 1) or (uiContext
= nil) or (not uiContext
.active
) then exit
;
2155 toGlobal(0, 0, gx
, gy
);
2157 savedClip
:= uiContext
.clip
;
2160 drawControl(gx
, gy
);
2162 for f
:= 0 to High(mChildren
) do mChildren
[f
].draw();
2164 drawControlPost(gx
, gy
);
2166 uiContext
.clip
:= savedClip
;
2170 procedure TUIControl
.drawControl (gx
, gy
: Integer);
2174 procedure TUIControl
.drawControlPost (gx
, gy
: Integer);
2176 // shadow for top-level controls
2177 if (mParent
= nil) and (mDrawShadow
) and (mWidth
> 0) and (mHeight
> 0) then
2179 uiContext
.resetClip();
2180 uiContext
.darkenRect(gx
+mWidth
, gy
+8, 8, mHeight
, 128);
2181 uiContext
.darkenRect(gx
+8, gy
+mHeight
, mWidth
-8, 8, 128);
2186 // ////////////////////////////////////////////////////////////////////////// //
2187 procedure TUIControl
.onEvent (var ev
: TFUIEvent
);
2189 if (not ev
.alive
) or (not enabled
) then exit
;
2190 //if (ev.mine) then writeln(' MINE: <', className, '>');
2193 if (ev
.sinking
) then keyEventSink(ev
)
2194 else if (ev
.bubbling
) then keyEventBubble(ev
)
2195 else if (ev
.mine
) then keyEvent(ev
);
2197 else if (ev
.mouse
) then
2199 if (ev
.sinking
) then mouseEventSink(ev
)
2200 else if (ev
.bubbling
) then mouseEventBubble(ev
)
2201 else if (ev
.mine
) then mouseEvent(ev
);
2206 procedure TUIControl
.mouseEventSink (var ev
: TFUIEvent
);
2210 procedure TUIControl
.mouseEventBubble (var ev
: TFUIEvent
);
2214 procedure TUIControl
.mouseEvent (var ev
: TFUIEvent
);
2219 procedure TUIControl
.keyEventSink (var ev
: TFUIEvent
);
2223 if (not enabled
) then exit
;
2224 if (not ev
.alive
) then exit
;
2225 // for top-level controls
2226 if (mParent
<> nil) then exit
;
2227 if (mEscClose
) and (ev
= 'Escape') then
2229 if (not assigned(closeRequestCB
)) or (closeRequestCB(self
)) then
2231 uiRemoveWindow(self
);
2236 if (ev
= 'Enter') or (ev
= 'C-Enter') then
2238 ctl
:= findDefaulControl();
2239 if (ctl
<> nil) then
2246 if (ev
= 'Escape') then
2248 ctl
:= findCancelControl();
2249 if (ctl
<> nil) then
2258 procedure TUIControl
.keyEventBubble (var ev
: TFUIEvent
);
2262 if (not enabled
) then exit
;
2263 if (not ev
.alive
) then exit
;
2264 // for top-level controls
2265 if (mParent
<> nil) then exit
;
2266 if (ev
= 'S-Tab') then
2268 ctl
:= findPrevFocus(mFocused
, true);
2269 if (ctl
<> nil) and (ctl
<> mFocused
) then ctl
.setFocused(true);
2273 if (ev
= 'Tab') then
2275 ctl
:= findNextFocus(mFocused
, true);
2276 if (ctl
<> nil) and (ctl
<> mFocused
) then ctl
.setFocused(true);
2282 procedure TUIControl
.keyEvent (var ev
: TFUIEvent
);
2287 // ////////////////////////////////////////////////////////////////////////// //
2288 constructor TUITopWindow
.Create (const atitle
: AnsiString);
2295 procedure TUITopWindow
.AfterConstruction ();
2298 mFitToScreen
:= true;
2300 mFrameHeight
:= uiContext
.charHeight(#184);
2301 if (mWidth
< mFrameWidth
*2+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
)) then mWidth
:= mFrameWidth
*2+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
);
2302 if (mHeight
< mFrameHeight
*2) then mHeight
:= mFrameHeight
*2;
2303 if (Length(mTitle
) > 0) then
2305 if (mWidth
< uiContext
.textWidth(mTitle
)+mFrameWidth
*2+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
)) then
2307 mWidth
:= uiContext
.textWidth(mTitle
)+mFrameWidth
*2+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
);
2311 mDragScroll
:= TXMode
.None
;
2312 mDrawShadow
:= true;
2313 mWaitingClose
:= false;
2316 mCtl4Style
:= 'window';
2317 mDefSize
.w
:= nmax(1, mDefSize
.w
);
2318 mDefSize
.h
:= nmax(1, mDefSize
.h
);
2322 function TUITopWindow
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2324 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
2326 mTitle
:= par
.expectIdOrStr(true);
2330 if (strEquCI1251(prname
, 'children')) then
2336 if (strEquCI1251(prname
, 'position')) then
2338 if (par
.eatIdOrStrCI('default')) then mDoCenter
:= false
2339 else if (par
.eatIdOrStrCI('center')) then mDoCenter
:= true
2340 else par
.error('`center` or `default` expected');
2344 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
2345 result
:= inherited parseProperty(prname
, par
);
2349 procedure TUITopWindow
.flFitToScreen ();
2353 nsz
:= TLaySize
.Create(trunc(fuiScrWdt
/fuiRenderScale
)-mFrameWidth
*2-6, trunc(fuiScrHgt
/fuiRenderScale
)-mFrameHeight
*2-6);
2354 if (mMaxSize
.w
< 1) then mMaxSize
.w
:= nsz
.w
;
2355 if (mMaxSize
.h
< 1) then mMaxSize
.h
:= nsz
.h
;
2359 procedure TUITopWindow
.centerInScreen ();
2361 if (mWidth
> 0) and (mHeight
> 0) then
2363 mX
:= trunc((fuiScrWdt
/fuiRenderScale
-mWidth
)/2);
2364 mY
:= trunc((fuiScrHgt
/fuiRenderScale
-mHeight
)/2);
2369 // ////////////////////////////////////////////////////////////////////////// //
2370 procedure TUITopWindow
.drawControl (gx
, gy
: Integer);
2372 uiContext
.color
:= mBackColor
[getColorIndex
];
2373 uiContext
.fillRect(gx
, gy
, mWidth
, mHeight
);
2376 procedure TUITopWindow
.drawControlPost (gx
, gy
: Integer);
2378 cidx
, iwdt
, ihgt
: Integer;
2379 ybot
, xend
, vhgt
, vwdt
: Integer;
2381 cidx
:= getColorIndex
;
2382 iwdt
:= uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
);
2383 if (mDragScroll
= TXMode
.Drag
) then
2385 drawFrame(gx
, gy
, iwdt
, 0, mTitle
, false);
2389 ihgt
:= uiContext
.iconWinHeight(TGxContext
.TWinIcon
.Close
);
2390 drawFrame(gx
, gy
, iwdt
, 0, mTitle
, true);
2391 // vertical scroll bar
2392 vhgt
:= mHeight
-mFrameHeight
*2;
2393 if (mFullSize
.h
> vhgt
) then
2395 ybot
:= mScrollY
+vhgt
;
2397 uiContext
.drawVSBar(gx
+mWidth
-mFrameWidth
+1, gy
+mFrameHeight
-1, mFrameWidth
-3, vhgt
+2, ybot
, 0, mFullSize
.h
, mSBarFullColor
[cidx
], mSBarEmptyColor
[cidx
]);
2399 // horizontal scroll bar
2400 vwdt
:= mWidth
-mFrameWidth
*2;
2401 if (mFullSize
.w
> vwdt
) then
2403 xend
:= mScrollX
+vwdt
;
2405 uiContext
.drawHSBar(gx
+mFrameWidth
+1, gy
+mHeight
-mFrameHeight
+1, vwdt
-2, mFrameHeight
-3, xend
, 0, mFullSize
.w
, mSBarFullColor
[cidx
], mSBarEmptyColor
[cidx
]);
2408 setScissor(mFrameWidth
, 0, iwdt
, ihgt
);
2409 uiContext
.color
:= mBackColor
[cidx
];
2410 uiContext
.fillRect(gx
+mFrameWidth
, gy
, iwdt
, ihgt
);
2411 uiContext
.color
:= mFrameIconColor
[cidx
];
2412 uiContext
.drawIconWin(TGxContext
.TWinIcon
.Close
, gx
+mFrameWidth
, gy
, mInClose
);
2414 // shadow (no need to reset scissor, as draw should do it)
2415 inherited drawControlPost(gx
, gy
);
2419 // ////////////////////////////////////////////////////////////////////////// //
2420 procedure TUITopWindow
.activated ();
2422 if (mFocused
= nil) or (mFocused
= self
) then
2424 mFocused
:= findFirstFocus();
2426 if (mFocused
<> nil) and (mFocused
<> self
) then mFocused
.activated();
2431 procedure TUITopWindow
.blurred ();
2433 mDragScroll
:= TXMode
.None
;
2434 mWaitingClose
:= false;
2436 if (mFocused
<> nil) and (mFocused
<> self
) then mFocused
.blurred();
2441 procedure TUITopWindow
.keyEventBubble (var ev
: TFUIEvent
);
2443 inherited keyEvent(ev
);
2444 if (not ev
.alive
) or (not enabled
) {or (not getFocused)} then exit
;
2445 if (ev
= 'M-F3') then
2447 if (not assigned(closeRequestCB
)) or (closeRequestCB(self
)) then
2449 uiRemoveWindow(self
);
2457 procedure TUITopWindow
.mouseEvent (var ev
: TFUIEvent
);
2460 vhgt
, ytop
: Integer;
2461 vwdt
, xend
: Integer;
2463 if (not enabled
) then exit
;
2464 if (mWidth
< 1) or (mHeight
< 1) then exit
;
2466 if (mDragScroll
= TXMode
.Drag
) then
2468 mX
+= ev
.x
-mDragStartX
;
2469 mY
+= ev
.y
-mDragStartY
;
2470 mDragStartX
:= ev
.x
;
2471 mDragStartY
:= ev
.y
;
2472 if (ev
.release
) and (ev
.but
= ev
.Left
) then mDragScroll
:= TXMode
.None
;
2477 if (mDragScroll
= TXMode
.VScroll
) then
2480 vhgt
:= mHeight
-mFrameHeight
*2;
2481 ytop
:= uiContext
.sbarPos(ly
, mFrameHeight
-1, vhgt
+2, 0, mFullSize
.h
)-vhgt
;
2482 mScrollY
:= nmax(0, ytop
);
2483 if (ev
.release
) and (ev
.but
= ev
.Left
) then mDragScroll
:= TXMode
.None
;
2488 if (mDragScroll
= TXMode
.HScroll
) then
2491 vwdt
:= mWidth
-mFrameWidth
*2;
2492 xend
:= uiContext
.sbarPos(lx
, mFrameWidth
+1, vwdt
-2, 0, mFullSize
.w
)-vwdt
;
2493 mScrollX
:= nmax(0, xend
);
2494 if (ev
.release
) and (ev
.but
= ev
.Left
) then mDragScroll
:= TXMode
.None
;
2499 if toLocal(ev
.x
, ev
.y
, lx
, ly
) then
2503 if (ly
< mFrameHeight
) then
2506 if (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
)) then
2508 //uiRemoveWindow(self);
2509 mWaitingClose
:= true;
2514 mDragScroll
:= TXMode
.Drag
;
2515 mDragStartX
:= ev
.x
;
2516 mDragStartY
:= ev
.y
;
2521 // check for vertical scrollbar
2522 if (lx
>= mWidth
-mFrameWidth
+1) and (ly
>= mFrameHeight
-1) and (ly
< mHeight
-mFrameHeight
+2) then
2524 vhgt
:= mHeight
-mFrameHeight
*2;
2525 if (mFullSize
.h
> vhgt
) then
2528 mDragScroll
:= TXMode
.VScroll
;
2530 ytop
:= uiContext
.sbarPos(ly
, mFrameHeight
-1, vhgt
+2, 0, mFullSize
.h
)-vhgt
;
2531 mScrollY
:= nmax(0, ytop
);
2535 // check for horizontal scrollbar
2536 if (ly
>= mHeight
-mFrameHeight
+1) and (lx
>= mFrameWidth
+1) and (lx
< mWidth
-mFrameWidth
-1) then
2538 vwdt
:= mWidth
-mFrameWidth
*2;
2539 if (mFullSize
.w
> vwdt
) then
2542 mDragScroll
:= TXMode
.HScroll
;
2544 xend
:= uiContext
.sbarPos(lx
, mFrameWidth
+1, vwdt
-2, 0, mFullSize
.w
)-vwdt
;
2545 mScrollX
:= nmax(0, xend
);
2550 if (lx
< mFrameWidth
) or (lx
>= mWidth
-mFrameWidth
) or (ly
>= mHeight
-mFrameHeight
) then
2553 mDragScroll
:= TXMode
.Drag
;
2554 mDragStartX
:= ev
.x
;
2555 mDragStartY
:= ev
.y
;
2561 if (ev
.release
) then
2563 if mWaitingClose
then
2565 if (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
)) then
2567 if (not assigned(closeRequestCB
)) or (closeRequestCB(self
)) then
2569 uiRemoveWindow(self
);
2572 mWaitingClose
:= false;
2581 if mWaitingClose
then
2583 mInClose
:= (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
));
2589 inherited mouseEvent(ev
);
2594 if (not ev
.motion
) and (mWaitingClose
) then begin ev
.eat(); mWaitingClose
:= false; exit
; end;
2599 // ////////////////////////////////////////////////////////////////////////// //
2600 constructor TUIBox
.Create (ahoriz
: Boolean);
2607 procedure TUIBox
.AfterConstruction ();
2611 mHAlign
:= -1; // left
2612 mCtl4Style
:= 'box';
2613 mDefSize
:= TLaySize
.Create(-1, -1);
2617 procedure TUIBox
.setCaption (const acap
: AnsiString);
2620 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mCaption
)+3, uiContext
.textHeight(mCaption
));
2624 procedure TUIBox
.setHasFrame (v
: Boolean);
2627 if (mHasFrame
) then begin mFrameWidth
:= 8; mFrameHeight
:= uiContext
.charHeight(#184); end else begin mFrameWidth
:= 0; mFrameHeight
:= 0; end;
2628 if (mHasFrame
) then mNoPad
:= true;
2632 function TUIBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2634 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
2635 if (strEquCI1251(prname
, 'padding')) then
2637 if (mHoriz
) then mPadding
:= parseHPadding(par
, 0) else mPadding
:= parseVPadding(par
, 0);
2641 if (strEquCI1251(prname
, 'frame')) then
2643 setHasFrame(parseBool(par
));
2647 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
2649 setCaption(par
.expectIdOrStr(true));
2653 if (strEquCI1251(prname
, 'textalign')) or (strEquCI1251(prname
, 'text-align')) then
2655 mHAlign
:= parseHAlign(par
);
2659 if (strEquCI1251(prname
, 'children')) then
2665 result
:= inherited parseProperty(prname
, par
);
2669 procedure TUIBox
.drawControl (gx
, gy
: Integer);
2674 cidx
:= getColorIndex
;
2675 uiContext
.color
:= mBackColor
[cidx
];
2676 uiContext
.fillRect(gx
, gy
, mWidth
, mHeight
);
2680 drawFrame(gx
, gy
, 0, mHAlign
, mCaption
, false);
2682 // no frame -- no caption
2684 else if (Length(mCaption) > 0) then
2687 if (mHAlign < 0) then xpos := 3
2688 else if (mHAlign > 0) then xpos := mWidth-mFrameWidth*2-uiContext.textWidth(mCaption)
2689 else xpos := (mWidth-mFrameWidth*2-uiContext.textWidth(mCaption)) div 2;
2690 xpos += gx+mFrameWidth;
2692 setScissor(mFrameWidth+1, 0, mWidth-mFrameWidth-2, uiContext.textHeight(mCaption));
2693 uiContext.color := mFrameTextColor[cidx];
2694 uiContext.drawText(xpos, gy, mCaption);
2700 procedure TUIBox
.mouseEvent (var ev
: TFUIEvent
);
2704 inherited mouseEvent(ev
);
2705 if (ev
.alive
) and (enabled
) and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
2712 procedure TUIBox
.keyEvent (var ev
: TFUIEvent
);
2715 cur
, ctl
: TUIControl
;
2717 inherited keyEvent(ev
);
2718 if (not ev
.alive
) or (not ev
.press
) or (not enabled
) or (not getActive
) then exit
;
2719 if (Length(mChildren
) = 0) then exit
;
2720 if (mHoriz
) and (ev
= 'Left') then dir
:= -1
2721 else if (mHoriz
) and (ev
= 'Right') then dir
:= 1
2722 else if (not mHoriz
) and (ev
= 'Up') then dir
:= -1
2723 else if (not mHoriz
) and (ev
= 'Down') then dir
:= 1;
2724 if (dir
= 0) then exit
;
2726 cur
:= topLevel
.mFocused
;
2727 while (cur
<> nil) and (cur
.mParent
<> self
) do cur
:= cur
.mParent
;
2728 //if (cur = nil) then writeln('CUR: nil') else writeln('CUR: ', cur.className, '#', cur.id);
2729 if (dir
< 0) then ctl
:= findPrevFocus(cur
, true) else ctl
:= findNextFocus(cur
, true);
2730 //if (ctl = nil) then writeln('CTL: nil') else writeln('CTL: ', ctl.className, '#', ctl.id);
2731 if (ctl
<> nil) and (ctl
<> self
) then
2733 ctl
.focused
:= true;
2738 // ////////////////////////////////////////////////////////////////////////// //
2739 constructor TUIHBox
.Create ();
2744 procedure TUIHBox
.AfterConstruction ();
2751 // ////////////////////////////////////////////////////////////////////////// //
2752 constructor TUIVBox
.Create ();
2757 procedure TUIVBox
.AfterConstruction ();
2764 // ////////////////////////////////////////////////////////////////////////// //
2765 procedure TUISpan
.AfterConstruction ();
2771 mCtl4Style
:= 'span';
2772 mDefSize
:= TLaySize
.Create(-1, -1);
2776 function TUISpan
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2778 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
2779 result
:= inherited parseProperty(prname
, par
);
2783 // ////////////////////////////////////////////////////////////////////// //
2784 procedure TUILine
.AfterConstruction ();
2790 mCtl4Style
:= 'line';
2791 mDefSize
:= TLaySize
.Create(-1, -1);
2795 function TUILine
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2797 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
2798 result
:= inherited parseProperty(prname
, par
);
2802 procedure TUILine
.layPrepare ();
2804 inherited layPrepare();
2805 if (mParent
<> nil) then mHoriz
:= not mParent
.mHoriz
;
2808 if (mLayDefSize
.w
< 0) then mLayDefSize
.w
:= 1;
2809 if (mLayDefSize
.h
< 0) then mLayDefSize
.h
:= 7;
2813 if (mLayDefSize
.w
< 0) then mLayDefSize
.w
:= 7;
2814 if (mLayDefSize
.h
< 0) then mLayDefSize
.h
:= 1;
2819 procedure TUILine
.drawControl (gx
, gy
: Integer);
2823 cidx
:= getColorIndex
;
2824 uiContext
.color
:= mTextColor
[cidx
];
2825 if mHoriz
then uiContext
.hline(gx
, gy
+(mHeight
div 2), mWidth
)
2826 else uiContext
.vline(gx
+(mWidth
div 2), gy
, mHeight
);
2830 // ////////////////////////////////////////////////////////////////////////// //
2831 procedure TUIStaticText
.AfterConstruction ();
2837 mHoriz
:= true; // nobody cares
2840 mCtl4Style
:= 'static';
2844 procedure TUIStaticText
.setText (const atext
: AnsiString);
2847 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
), uiContext
.textHeight(mText
));
2851 function TUIStaticText
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2853 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) or (strEquCI1251(prname
, 'text')) then
2855 setText(par
.expectIdOrStr(true));
2859 if (strEquCI1251(prname
, 'textalign')) or (strEquCI1251(prname
, 'text-align')) then
2861 parseTextAlign(par
, mHAlign
, mVAlign
);
2865 if (strEquCI1251(prname
, 'header')) then
2871 if (strEquCI1251(prname
, 'line')) then
2877 result
:= inherited parseProperty(prname
, par
);
2881 procedure TUIStaticText
.drawControl (gx
, gy
: Integer);
2883 xpos
, ypos
: Integer;
2886 cidx
:= getColorIndex
;
2887 uiContext
.color
:= mBackColor
[cidx
];
2888 uiContext
.fillRect(gx
, gy
, mWidth
, mHeight
);
2890 if (mHAlign
< 0) then xpos
:= 0
2891 else if (mHAlign
> 0) then xpos
:= mWidth
-uiContext
.textWidth(mText
)
2892 else xpos
:= (mWidth
-uiContext
.textWidth(mText
)) div 2;
2894 if (Length(mText
) > 0) then
2896 if (mHeader
) then uiContext
.color
:= mFrameTextColor
[cidx
] else uiContext
.color
:= mTextColor
[cidx
];
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
.drawText(gx
+xpos
, gy
+ypos
, mText
);
2907 if (mHeader
) then uiContext
.color
:= mFrameColor
[cidx
] else uiContext
.color
:= mTextColor
[cidx
];
2909 if (mVAlign
< 0) then ypos
:= 0
2910 else if (mVAlign
> 0) then ypos
:= mHeight
-1
2911 else ypos
:= (mHeight
div 2);
2914 if (Length(mText
) = 0) then
2916 uiContext
.hline(gx
, ypos
, mWidth
);
2920 uiContext
.hline(gx
, ypos
, xpos
-1);
2921 uiContext
.hline(gx
+xpos
+uiContext
.textWidth(mText
), ypos
, mWidth
);
2927 // ////////////////////////////////////////////////////////////////////////// //
2928 procedure TUITextLabel
.AfterConstruction ();
2934 mCtl4Style
:= 'label';
2939 procedure TUITextLabel
.cacheStyle (root
: TUIStyle
);
2941 inherited cacheStyle(root
);
2943 mHotColor
[ClrIdxActive
] := root
.get('hot-color', 'active', mCtl4Style
).asRGBADef(TGxRGBA
.Create(0, 128, 0));
2945 mHotColor
[ClrIdxDisabled
] := root
.get('hot-color', 'disabled', mCtl4Style
).asRGBADef(TGxRGBA
.Create(0, 64, 0));
2947 mHotColor
[ClrIdxInactive
] := root
.get('hot-color', 'inactive', mCtl4Style
).asRGBADef(TGxRGBA
.Create(0, 64, 0));
2951 procedure TUITextLabel
.setText (const s
: AnsiString);
2959 while (f
<= Length(s
)) do
2961 if (s
[f
] = '\\') then
2964 if (f
<= Length(s
)) then mText
+= s
[f
];
2967 else if (s
[f
] = '~') then
2970 if (f
<= Length(s
)) then
2972 if (mHotChar
= #0) then
2975 mHotOfs
:= Length(mText
);
2987 // fix hotchar offset
2988 if (mHotChar
<> #0) and (mHotOfs
> 0) then
2990 mHotOfs
:= uiContext
.textWidth(Copy(mText
, 1, mHotOfs
+1))-uiContext
.charWidth(mText
[mHotOfs
+1]);
2993 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
), uiContext
.textHeight(mText
));
2997 function TUITextLabel
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2999 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) or (strEquCI1251(prname
, 'text')) then
3001 setText(par
.expectIdOrStr(true));
3005 if (strEquCI1251(prname
, 'link')) then
3007 mLinkId
:= par
.expectIdOrStr(true);
3011 if (strEquCI1251(prname
, 'textalign')) or (strEquCI1251(prname
, 'text-align')) then
3013 parseTextAlign(par
, mHAlign
, mVAlign
);
3017 result
:= inherited parseProperty(prname
, par
);
3021 procedure TUITextLabel
.drawControl (gx
, gy
: Integer);
3023 xpos
, ypos
: Integer;
3026 cidx
:= getColorIndex
;
3027 uiContext
.color
:= mBackColor
[cidx
];
3028 uiContext
.fillRect(gx
, gy
, mWidth
, mHeight
);
3029 if (Length(mText
) > 0) then
3031 if (mHAlign
< 0) then xpos
:= 0
3032 else if (mHAlign
> 0) then xpos
:= mWidth
-uiContext
.textWidth(mText
)
3033 else xpos
:= (mWidth
-uiContext
.textWidth(mText
)) div 2;
3035 if (mVAlign
< 0) then ypos
:= 0
3036 else if (mVAlign
> 0) then ypos
:= mHeight
-uiContext
.textHeight(mText
)
3037 else ypos
:= (mHeight
-uiContext
.textHeight(mText
)) div 2;
3039 uiContext
.color
:= mTextColor
[cidx
];
3040 uiContext
.drawText(gx
+xpos
, gy
+ypos
, mText
);
3042 if (Length(mLinkId
) > 0) and (mHotChar
<> #0) and (mHotChar
<> ' ') then
3044 uiContext
.color
:= mHotColor
[cidx
];
3045 uiContext
.drawChar(gx
+xpos
+mHotOfs
, gy
+ypos
, mHotChar
);
3051 procedure TUITextLabel
.mouseEvent (var ev
: TFUIEvent
);
3055 inherited mouseEvent(ev
);
3056 if (ev
.alive
) and (enabled
) and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
3063 procedure TUITextLabel
.doAction ();
3067 if (assigned(actionCB
)) then
3073 ctl
:= topLevel
[mLinkId
];
3074 if (ctl
<> nil) then
3076 if (ctl
.canFocus
) then ctl
.focused
:= true;
3082 procedure TUITextLabel
.keyEventBubble (var ev
: TFUIEvent
);
3084 if (not enabled
) then exit
;
3085 if (mHotChar
= #0) then exit
;
3086 if (not ev
.alive
) or (not ev
.press
) then exit
;
3087 if (ev
.kstate
<> ev
.ModAlt
) then exit
;
3088 if (not ev
.isHot(mHotChar
)) then exit
;
3090 if (canFocus
) then focused
:= true;
3095 // ////////////////////////////////////////////////////////////////////////// //
3096 procedure TUIButton
.AfterConstruction ();
3103 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+uiContext
.textWidth('[ ]'), uiContext
.textHeight(mText
));
3104 mCtl4Style
:= 'button';
3105 mSkipLayPrepare
:= false;
3106 mAddMarkers
:= false;
3107 mHideMarkers
:= false;
3111 procedure TUIButton
.cacheStyle (root
: TUIStyle
);
3115 inherited cacheStyle(root
);
3117 sz
:= nmax(0, root
.get('shadow-size', 'active', mCtl4Style
).asInt(0));
3118 sz
:= nmax(sz
, root
.get('shadow-size', 'disabled', mCtl4Style
).asInt(0));
3119 sz
:= nmax(sz
, root
.get('shadow-size', 'inactive', mCtl4Style
).asInt(0));
3122 mAddMarkers
:= root
.get('add-markers', 'active', mCtl4Style
).asBool(false);
3123 mAddMarkers
:= mAddMarkers
or root
.get('add-markers', 'disabled', mCtl4Style
).asBool(false);
3124 mAddMarkers
:= mAddMarkers
or root
.get('add-markers', 'inactive', mCtl4Style
).asBool(false);
3126 mHideMarkers
:= root
.get('hide-markers', 'active', mCtl4Style
).asBool(false);
3127 mHideMarkers
:= mHideMarkers
or root
.get('hide-markers', 'disabled', mCtl4Style
).asBool(false);
3128 mHideMarkers
:= mHideMarkers
or root
.get('hide-markers', 'inactive', mCtl4Style
).asBool(false);
3132 procedure TUIButton
.setText (const s
: AnsiString);
3134 inherited setText(s
);
3135 if (mHideMarkers
) then
3137 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+10, uiContext
.textHeight(mText
));
3139 else if (mAddMarkers
) then
3141 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+uiContext
.textWidth('[<>]'), uiContext
.textHeight(mText
));
3145 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+uiContext
.textWidth('<>'), uiContext
.textHeight(mText
));
3150 procedure TUIButton
.layPrepare ();
3155 if (not mSkipLayPrepare
) then
3158 if (ods
.w
<> 0) or (ods
.h
<> 0) then
3160 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
), uiContext
.textHeight(mText
));
3161 if (mHideMarkers
) then
3165 else if (mAddMarkers
) then
3167 if (mDefault
) then ww
:= uiContext
.textWidth('[< >]')
3168 else if (mCancel
) then ww
:= uiContext
.textWidth('[{ }]')
3169 else ww
:= uiContext
.textWidth('[ ]');
3173 ww
:= nmax(0, uiContext
.textWidth('< >'));
3174 ww
:= nmax(ww
, uiContext
.textWidth('{ }'));
3175 ww
:= nmax(ww
, uiContext
.textWidth('[ ]'));
3177 mDefSize
.w
+= ww
+mShadowSize
;
3178 mDefSize
.h
+= mShadowSize
;
3183 ods
:= TLaySize
.Create(0, 0); // fpc is dumb!
3185 inherited layPrepare();
3186 if (not mSkipLayPrepare
) then mDefSize
:= ods
;
3190 procedure TUIButton
.blurred ();
3196 procedure TUIButton
.drawControl (gx
, gy
: Integer);
3199 xpos
, ypos
, xofsl
, xofsr
, sofs
: Integer;
3202 lstr
, rstr
: AnsiString;
3204 cidx
:= getColorIndex
;
3206 wdt
:= mWidth
-mShadowSize
;
3207 hgt
:= mHeight
-mShadowSize
;
3208 if (mPushed
) {or (cidx = ClrIdxActive)} then
3210 sofs
:= mShadowSize
;
3217 if (mShadowSize
> 0) then
3219 uiContext
.darkenRect(gx
+mShadowSize
, gy
+hgt
, wdt
, mShadowSize
, 96);
3220 uiContext
.darkenRect(gx
+wdt
, gy
+mShadowSize
, mShadowSize
, hgt
-mShadowSize
, 96);
3224 uiContext
.color
:= mBackColor
[cidx
];
3225 uiContext
.fillRect(gx
, gy
, wdt
, hgt
);
3227 if (mVAlign
< 0) then ypos
:= 0
3228 else if (mVAlign
> 0) then ypos
:= hgt
-uiContext
.textHeight(mText
)
3229 else ypos
:= (hgt
-uiContext
.textHeight(mText
)) div 2;
3232 uiContext
.color
:= mTextColor
[cidx
];
3234 if (mHideMarkers
) then
3241 if (mAddMarkers
) then
3243 if (mDefault
) then begin lstr
:= '[< '; rstr
:= ' >]'; end
3244 else if (mCancel
) then begin lstr
:= '[{ '; rstr
:= ' }]'; end
3245 else begin lstr
:= '[ '; rstr
:= ' ]'; end;
3246 xofsl
:= uiContext
.textWidth(lstr
);
3247 xofsr
:= uiContext
.textWidth(rstr
);
3248 uiContext
.drawText(gx
, ypos
, lstr
);
3249 uiContext
.drawText(gx
+wdt
-uiContext
.textWidth(rstr
), ypos
, rstr
);
3253 xofsl
:= nmax(0, uiContext
.textWidth('< '));
3254 xofsl
:= nmax(xofsl
, uiContext
.textWidth('{ '));
3255 xofsl
:= nmax(xofsl
, uiContext
.textWidth('[ '));
3256 xofsr
:= nmax(0, uiContext
.textWidth(' >'));
3257 xofsr
:= nmax(xofsr
, uiContext
.textWidth(' }'));
3258 xofsr
:= nmax(xofsr
, uiContext
.textWidth(' ]'));
3259 if (mDefault
) then begin lch
:= '<'; rch
:= '>'; end
3260 else if (mCancel
) then begin lch
:= '{'; rch
:= '}'; end
3261 else begin lch
:= '['; rch
:= ']'; end;
3262 uiContext
.drawChar(gx
, ypos
, lch
);
3263 uiContext
.drawChar(gx
+wdt
-uiContext
.charWidth(rch
), ypos
, rch
);
3267 if (Length(mText
) > 0) then
3269 if (mHAlign
< 0) then xpos
:= 0
3270 else begin xpos
:= wdt
-xofsl
-xofsr
-uiContext
.textWidth(mText
); if (mHAlign
= 0) then xpos
:= xpos
div 2; end;
3273 setScissor(sofs
+xofsl
, sofs
, wdt
-xofsl
-xofsr
, hgt
);
3274 uiContext
.drawText(gx
+xpos
, ypos
, mText
);
3276 if (mHotChar
<> #0) and (mHotChar
<> ' ') then
3278 uiContext
.color
:= mHotColor
[cidx
];
3279 uiContext
.drawChar(gx
+xpos
+mHotOfs
, ypos
, mHotChar
);
3285 procedure TUIButton
.mouseEvent (var ev
: TFUIEvent
);
3289 inherited mouseEvent(ev
);
3290 if (uiGrabCtl
= self
) then
3293 mPushed
:= toLocal(ev
.x
, ev
.y
, lx
, ly
);
3294 if (ev
= '-lmb') and (focused
) and (mPushed
) then
3301 if (not ev
.alive
) or (not enabled
) or (not focused
) then exit
;
3307 procedure TUIButton
.keyEvent (var ev
: TFUIEvent
);
3309 inherited keyEvent(ev
);
3310 if (ev
.alive
) and (enabled
) then
3312 if (ev
= '+Enter') or (ev
= '+Space') then
3319 if (focused
) and ((ev
= '-Enter') or (ev
= '-Space')) then
3337 // ////////////////////////////////////////////////////////////////////////// //
3338 procedure TUIButtonRound
.AfterConstruction ();
3344 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+8*2, uiContext
.textHeight(mText
)+2);
3345 mCtl4Style
:= 'button-round';
3346 mSkipLayPrepare
:= true;
3350 procedure TUIButtonRound
.setText (const s
: AnsiString);
3352 inherited setText(s
);
3353 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+8*2, uiContext
.textHeight(mText
)+2);
3357 procedure TUIButtonRound
.layPrepare ();
3362 if (ods
.w
<> 0) or (ods
.h
<> 0) then
3364 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+8*2, uiContext
.textHeight(mText
)+2);
3366 inherited layPrepare();
3371 procedure TUIButtonRound
.drawControl (gx
, gy
: Integer);
3373 xpos
, ypos
: Integer;
3376 cidx
:= getColorIndex
;
3378 uiContext
.color
:= mBackColor
[cidx
];
3379 uiContext
.fillRect(gx
+1, gy
, mWidth
-2, mHeight
);
3380 uiContext
.fillRect(gx
, gy
+1, 1, mHeight
-2);
3381 uiContext
.fillRect(gx
+mWidth
-1, gy
+1, 1, mHeight
-2);
3383 if (Length(mText
) > 0) then
3385 if (mHAlign
< 0) then xpos
:= 0
3386 else if (mHAlign
> 0) then xpos
:= mWidth
-uiContext
.textWidth(mText
)
3387 else xpos
:= (mWidth
-uiContext
.textWidth(mText
)) div 2;
3389 if (mVAlign
< 0) then ypos
:= 0
3390 else if (mVAlign
> 0) then ypos
:= mHeight
-uiContext
.textHeight(mText
)
3391 else ypos
:= (mHeight
-uiContext
.textHeight(mText
)) div 2;
3393 setScissor(8, 0, mWidth
-16, mHeight
);
3394 uiContext
.color
:= mTextColor
[cidx
];
3395 uiContext
.drawText(gx
+xpos
+8, gy
+ypos
, mText
);
3397 if (mHotChar
<> #0) and (mHotChar
<> ' ') then
3399 uiContext
.color
:= mHotColor
[cidx
];
3400 uiContext
.drawChar(gx
+xpos
+8+mHotOfs
, gy
+ypos
, mHotChar
);
3406 // ////////////////////////////////////////////////////////////////////////// //
3407 procedure TUISwitchBox
.AfterConstruction ();
3413 mIcon
:= TGxContext
.TMarkIcon
.Checkbox
;
3414 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+3+uiContext
.iconMarkWidth(mIcon
), nmax(uiContext
.iconMarkHeight(mIcon
), uiContext
.textHeight(mText
)));
3415 mCtl4Style
:= 'switchbox';
3417 mBoolVar
:= @mChecked
;
3421 procedure TUISwitchBox
.cacheStyle (root
: TUIStyle
);
3423 inherited cacheStyle(root
);
3425 mSwitchColor
[ClrIdxActive
] := root
.get('switch-color', 'active', mCtl4Style
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
3427 mSwitchColor
[ClrIdxDisabled
] := root
.get('switch-color', 'disabled', mCtl4Style
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
3429 mSwitchColor
[ClrIdxInactive
] := root
.get('switch-color', 'inactive', mCtl4Style
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
3433 procedure TUISwitchBox
.setText (const s
: AnsiString);
3435 inherited setText(s
);
3436 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+3+uiContext
.iconMarkWidth(mIcon
), nmax(uiContext
.iconMarkHeight(mIcon
), uiContext
.textHeight(mText
)));
3440 function TUISwitchBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
3442 if (strEquCI1251(prname
, 'checked')) then
3448 result
:= inherited parseProperty(prname
, par
);
3452 function TUISwitchBox
.getChecked (): Boolean;
3454 if (mBoolVar
<> nil) then result
:= mBoolVar
^ else result
:= false;
3458 procedure TUISwitchBox
.setVar (pvar
: PBoolean);
3460 if (pvar
= nil) then pvar
:= @mChecked
;
3461 if (pvar
<> mBoolVar
) then
3464 setChecked(mBoolVar
^);
3469 procedure TUISwitchBox
.drawControl (gx
, gy
: Integer);
3471 xpos
, ypos
, iwdt
, dy
: Integer;
3474 cidx
:= getColorIndex
;
3476 iwdt
:= uiContext
.iconMarkWidth(mIcon
);
3477 if (mHAlign
< 0) then xpos
:= 0
3478 else if (mHAlign
> 0) then xpos
:= mWidth
-(uiContext
.textWidth(mText
)+3+iwdt
)
3479 else xpos
:= (mWidth
-(uiContext
.textWidth(mText
)+3+iwdt
)) div 2;
3481 if (mVAlign
< 0) then ypos
:= 0
3482 else if (mVAlign
> 0) then ypos
:= mHeight
-uiContext
.textHeight(mText
)
3483 else ypos
:= (mHeight
-uiContext
.textHeight(mText
)) div 2;
3485 uiContext
.color
:= mBackColor
[cidx
];
3486 uiContext
.fillRect(gx
, gy
, mWidth
, mHeight
);
3488 uiContext
.color
:= mSwitchColor
[cidx
];
3489 if (uiContext
.iconMarkHeight(mIcon
) < uiContext
.textHeight(mText
)) then
3491 case uiContext
.textHeight(mText
) of
3496 uiContext
.drawIconMark(mIcon
, gx
, gy
+ypos
+uiContext
.textHeight(mText
)-uiContext
.iconMarkHeight(mIcon
)-dy
, checked
);
3500 uiContext
.drawIconMark(mIcon
, gx
, gy
, checked
);
3503 uiContext
.color
:= mTextColor
[cidx
];
3504 uiContext
.drawText(gx
+xpos
+3+iwdt
, gy
+ypos
, mText
);
3506 if (mHotChar
<> #0) and (mHotChar
<> ' ') then
3508 uiContext
.color
:= mHotColor
[cidx
];
3509 uiContext
.drawChar(gx
+xpos
+3+iwdt
+mHotOfs
, gy
+ypos
, mHotChar
);
3514 procedure TUISwitchBox
.mouseEvent (var ev
: TFUIEvent
);
3518 inherited mouseEvent(ev
);
3519 if (uiGrabCtl
= self
) then
3522 if (ev
= '-lmb') and focused
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
3528 if (not ev
.alive
) or (not enabled
) or not focused
then exit
;
3533 procedure TUISwitchBox
.keyEvent (var ev
: TFUIEvent
);
3535 inherited keyEvent(ev
);
3536 if (ev
.alive
) and (enabled
) then
3538 if (ev
= 'Space') then
3548 // ////////////////////////////////////////////////////////////////////////// //
3549 procedure TUICheckBox
.AfterConstruction ();
3553 mBoolVar
:= @mChecked
;
3554 mIcon
:= TGxContext
.TMarkIcon
.Checkbox
;
3559 procedure TUICheckBox
.setChecked (v
: Boolean);
3565 procedure TUICheckBox
.doAction ();
3567 if (assigned(actionCB
)) then
3573 setChecked(not getChecked
);
3578 // ////////////////////////////////////////////////////////////////////////// //
3579 procedure TUIRadioBox
.AfterConstruction ();
3583 mBoolVar
:= @mChecked
;
3585 mIcon
:= TGxContext
.TMarkIcon
.Radiobox
;
3590 function TUIRadioBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
3592 if (strEquCI1251(prname
, 'group')) then
3594 mRadioGroup
:= par
.expectIdOrStr(true);
3595 if (getChecked
) then setChecked(true);
3599 if (strEquCI1251(prname
, 'checked')) then
3605 result
:= inherited parseProperty(prname
, par
);
3609 procedure TUIRadioBox
.setChecked (v
: Boolean);
3611 function resetGroup (ctl
: TUIControl
): Boolean;
3614 if (ctl
<> self
) and (ctl
is TUIRadioBox
) and (TUIRadioBox(ctl
).mRadioGroup
= mRadioGroup
) then
3616 TUIRadioBox(ctl
).mBoolVar
^ := false;
3622 if v
then topLevel
.forEachControl(resetGroup
);
3626 procedure TUIRadioBox
.doAction ();
3628 if (assigned(actionCB
)) then
3639 // ////////////////////////////////////////////////////////////////////////// //
3641 oldFocus
: procedure () = nil;
3642 oldBlur
: procedure () = nil;
3644 procedure onWinFocus (); begin uiFocus(); if (assigned(oldFocus
)) then oldFocus(); end;
3645 procedure onWinBlur (); begin fuiResetKMState(true); uiBlur(); if (assigned(oldBlur
)) then oldBlur(); end;
3648 registerCtlClass(TUIHBox
, 'hbox');
3649 registerCtlClass(TUIVBox
, 'vbox');
3650 registerCtlClass(TUISpan
, 'span');
3651 registerCtlClass(TUILine
, 'line');
3652 registerCtlClass(TUITextLabel
, 'label');
3653 registerCtlClass(TUIStaticText
, 'static');
3654 registerCtlClass(TUIButtonRound
, 'round-button');
3655 registerCtlClass(TUIButton
, 'button');
3656 registerCtlClass(TUICheckBox
, 'checkbox');
3657 registerCtlClass(TUIRadioBox
, 'radiobox');
3659 oldFocus
:= winFocusCB
;
3660 oldBlur
:= winBlurCB
;
3661 winFocusCB
:= onWinFocus
;
3662 winBlurCB
:= onWinBlur
;