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 mSBarFullColor
: array[0..ClrIdxMax
] of TGxRGBA
;
75 mSBarEmptyColor
: array[0..ClrIdxMax
] of TGxRGBA
;
76 mDarken
: array[0..ClrIdxMax
] of Integer; // >255: none
79 procedure updateStyle (); virtual;
80 procedure cacheStyle (root
: TUIStyle
); virtual;
81 function getColorIndex (): Integer; inline;
84 function getEnabled (): Boolean;
85 procedure setEnabled (v
: Boolean); inline;
87 function getFocused (): Boolean; inline;
88 procedure setFocused (v
: Boolean); inline;
90 function getActive (): Boolean; inline;
92 function getCanFocus (): Boolean; inline;
94 function isMyChild (ctl
: TUIControl
): Boolean;
96 function findFirstFocus (): TUIControl
;
97 function findLastFocus (): TUIControl
;
99 function findNextFocus (cur
: TUIControl
; wrap
: Boolean): TUIControl
;
100 function findPrevFocus (cur
: TUIControl
; wrap
: Boolean): TUIControl
;
102 function findCancelControl (): TUIControl
;
103 function findDefaulControl (): TUIControl
;
105 function findControlById (const aid
: AnsiString): TUIControl
;
107 procedure activated (); virtual;
108 procedure blurred (); virtual;
110 procedure calcFullClientSize ();
112 procedure drawFrame (gx
, gy
, resx
, thalign
: Integer; const text: AnsiString; dbl
: Boolean);
115 var savedClip
: TGxRect
; // valid only in `draw*()` calls
116 //WARNING! do not call scissor functions outside `.draw*()` API!
117 // set scissor to this rect (in local coords)
118 procedure setScissor (lx
, ly
, lw
, lh
: Integer); // valid only in `draw*()` calls
119 procedure resetScissor (); inline; // only client area, w/o frame
120 procedure resetScissorNC (); inline; // full drawing area, with frame
124 closeRequestCB
: TCloseRequestCB
;
127 mDefSize
: TLaySize
; // default size
128 mMaxSize
: TLaySize
; // maximum size
135 mLayDefSize
: TLaySize
;
136 mLayMaxSize
: TLaySize
;
142 // layouter interface
143 function getDefSize (): TLaySize
; inline; // default size; <0: use max size
144 //procedure setDefSize (const sz: TLaySize); inline; // default size; <0: use max size
145 function getMargins (): TLayMargins
; inline;
146 function getPadding (): TLaySize
; inline; // children padding (each non-first child will get this on left/top)
147 function getMaxSize (): TLaySize
; inline; // max size; <0: set to some huge value
148 //procedure setMaxSize (const sz: TLaySize); inline; // max size; <0: set to some huge value
149 function getFlex (): Integer; inline; // <=0: not flexible
150 function isHorizBox (): Boolean; inline; // horizontal layout for children?
151 function noPad (): Boolean; inline; // ignore padding in box direction for this control
152 function getAlign (): Integer; inline; // aligning in non-main direction: <0: left/up; 0: center; >0: right/down
153 function getExpand (): Boolean; inline; // expanding in non-main direction: `true` will ignore align and eat all available space
154 function getHGroup (): AnsiString; inline; // empty: not grouped
155 function getVGroup (): AnsiString; inline; // empty: not grouped
157 procedure setActualSizePos (constref apos
: TLayPos
; constref asize
: TLaySize
); inline;
159 procedure layPrepare (); virtual; // called before registering control in layouter
162 property flex
: Integer read mFlex write mFlex
;
163 property flDefaultSize
: TLaySize read mDefSize write mDefSize
;
164 property flMaxSize
: TLaySize read mMaxSize write mMaxSize
;
165 property flPadding
: TLaySize read mPadding write mPadding
;
166 property flHoriz
: Boolean read mHoriz write mHoriz
;
167 property flAlign
: Integer read mAlign write mAlign
;
168 property flExpand
: Boolean read mExpand write mExpand
;
169 property flHGroup
: AnsiString read mHGroup write mHGroup
;
170 property flVGroup
: AnsiString read mVGroup write mVGroup
;
171 property flNoPad
: Boolean read mNoPad write mNoPad
;
172 property fullSize
: TLaySize read mFullSize
;
175 function parsePos (par
: TTextParser
): TLayPos
;
176 function parseSize (par
: TTextParser
): TLaySize
;
177 function parsePadding (par
: TTextParser
): TLaySize
;
178 function parseHPadding (par
: TTextParser
; def
: Integer): TLaySize
;
179 function parseVPadding (par
: TTextParser
; def
: Integer): TLaySize
;
180 function parseBool (par
: TTextParser
): Boolean;
181 function parseAnyAlign (par
: TTextParser
): Integer;
182 function parseHAlign (par
: TTextParser
): Integer;
183 function parseVAlign (par
: TTextParser
): Integer;
184 function parseOrientation (const prname
: AnsiString; par
: TTextParser
): Boolean;
185 procedure parseTextAlign (par
: TTextParser
; var h
, v
: Integer);
186 procedure parseChildren (par
: TTextParser
); // par should be on '{'; final '}' is eaten
189 // par is on property data
190 // there may be more data in text stream, don't eat it!
191 // return `true` if property name is valid and value was parsed
192 // return `false` if property name is invalid; don't advance parser in this case
193 // throw on property data errors
194 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; virtual;
196 // par should be on '{'; final '}' is eaten
197 procedure parseProperties (par
: TTextParser
);
200 constructor Create ();
201 destructor Destroy (); override;
203 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
205 // `sx` and `sy` are screen coordinates
206 procedure drawControl (gx
, gy
: Integer); virtual;
208 // called after all children drawn
209 procedure drawControlPost (gx
, gy
: Integer); virtual;
211 procedure draw (); virtual;
213 function topLevel (): TUIControl
; inline;
215 // returns `true` if global coords are inside this control
216 function toLocal (var x
, y
: Integer): Boolean;
217 function toLocal (gx
, gy
: Integer; out x
, y
: Integer): Boolean; inline;
218 procedure toGlobal (var x
, y
: Integer);
219 procedure toGlobal (lx
, ly
: Integer; out x
, y
: Integer); inline;
221 procedure getDrawRect (out gx
, gy
, wdt
, hgt
: Integer);
223 // x and y are global coords
224 function controlAtXY (x
, y
: Integer; allowDisabled
: Boolean=false): TUIControl
;
226 function parentScrollX (): Integer; inline;
227 function parentScrollY (): Integer; inline;
229 procedure makeVisibleInParent ();
231 procedure doAction (); virtual; // so user controls can override it
233 procedure onEvent (var ev
: TFUIEvent
); virtual; // general dispatcher
235 procedure mouseEvent (var ev
: TFUIEvent
); virtual;
236 procedure mouseEventSink (var ev
: TFUIEvent
); virtual;
237 procedure mouseEventBubble (var ev
: TFUIEvent
); virtual;
239 procedure keyEvent (var ev
: TFUIEvent
); virtual;
240 procedure keyEventSink (var ev
: TFUIEvent
); virtual;
241 procedure keyEventBubble (var ev
: TFUIEvent
); virtual;
243 function prevSibling (): TUIControl
;
244 function nextSibling (): TUIControl
;
245 function firstChild (): TUIControl
; inline;
246 function lastChild (): TUIControl
; inline;
248 procedure appendChild (ctl
: TUIControl
); virtual;
250 function setActionCBFor (const aid
: AnsiString; cb
: TActionCB
): TActionCB
; // returns previous cb
252 function forEachChildren (cb
: TCtlEnumCB
): TUIControl
; // doesn't recurse
253 function forEachControl (cb
: TCtlEnumCB
; includeSelf
: Boolean=true): TUIControl
;
255 procedure close (); // this closes *top-level* control
258 property id
: AnsiString read mId write mId
;
259 property styleId
: AnsiString read mStyleId
;
260 property scrollX
: Integer read mScrollX write mScrollX
;
261 property scrollY
: Integer read mScrollY write mScrollY
;
262 property x0
: Integer read mX write mX
;
263 property y0
: Integer read mY write mY
;
264 property width
: Integer read mWidth write mWidth
;
265 property height
: Integer read mHeight write mHeight
;
266 property enabled
: Boolean read getEnabled write setEnabled
;
267 property parent
: TUIControl read mParent
;
268 property focused
: Boolean read getFocused write setFocused
;
269 property active
: Boolean read getActive
;
270 property escClose
: Boolean read mEscClose write mEscClose
;
271 property cancel
: Boolean read mCancel write mCancel
;
272 property defctl
: Boolean read mDefault write mDefault
;
273 property canFocus
: Boolean read getCanFocus write mCanFocus
;
274 property ctlById
[const aid
: AnsiString]: TUIControl read findControlById
; default
;
278 TUITopWindow
= class(TUIControl
)
280 type TXMode
= (None
, Drag
, VScroll
, HScroll
);
285 mDragStartX
, mDragStartY
: Integer;
286 mWaitingClose
: Boolean;
288 mFreeOnClose
: Boolean; // default: false
289 mDoCenter
: Boolean; // after layouting
290 mFitToScreen
: Boolean;
293 procedure activated (); override;
294 procedure blurred (); override;
297 closeCB
: TActionCB
; // called after window was removed from ui window list
300 constructor Create (const atitle
: AnsiString);
302 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
304 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
306 procedure flFitToScreen (); // call this before layouting
308 procedure centerInScreen ();
310 // `sx` and `sy` are screen coordinates
311 procedure drawControl (gx
, gy
: Integer); override;
312 procedure drawControlPost (gx
, gy
: Integer); override;
314 procedure keyEventBubble (var ev
: TFUIEvent
); override; // returns `true` if event was eaten
315 procedure mouseEvent (var ev
: TFUIEvent
); override; // returns `true` if event was eaten
318 property freeOnClose
: Boolean read mFreeOnClose write mFreeOnClose
;
319 property fitToScreen
: Boolean read mFitToScreen write mFitToScreen
;
322 // ////////////////////////////////////////////////////////////////////// //
323 TUIBox
= class(TUIControl
)
326 mCaption
: AnsiString;
327 mHAlign
: Integer; // -1: left; 0: center; 1: right; default: left
330 procedure setCaption (const acap
: AnsiString);
331 procedure setHasFrame (v
: Boolean);
334 constructor Create (ahoriz
: Boolean);
336 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
338 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
340 procedure drawControl (gx
, gy
: Integer); override;
342 procedure mouseEvent (var ev
: TFUIEvent
); override;
343 procedure keyEvent (var ev
: TFUIEvent
); override;
346 property caption
: AnsiString read mCaption write setCaption
;
347 property hasFrame
: Boolean read mHasFrame write setHasFrame
;
348 property captionAlign
: Integer read mHAlign write mHAlign
;
351 TUIHBox
= class(TUIBox
)
353 constructor Create ();
355 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
358 TUIVBox
= class(TUIBox
)
360 constructor Create ();
362 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
365 // ////////////////////////////////////////////////////////////////////// //
366 TUISpan
= class(TUIControl
)
368 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
370 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
373 // ////////////////////////////////////////////////////////////////////// //
374 TUILine
= class(TUIControl
)
376 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
378 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
380 procedure layPrepare (); override; // called before registering control in layouter
382 procedure drawControl (gx
, gy
: Integer); override;
385 // ////////////////////////////////////////////////////////////////////// //
386 TUIStaticText
= class(TUIControl
)
389 mHAlign
: Integer; // -1: left; 0: center; 1: right; default: left
390 mVAlign
: Integer; // -1: top; 0: center; 1: bottom; default: center
391 mHeader
: Boolean; // true: draw with frame text color
392 mLine
: Boolean; // true: draw horizontal line
395 procedure setText (const atext
: AnsiString);
398 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
400 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
402 procedure drawControl (gx
, gy
: Integer); override;
405 property text: AnsiString read mText write setText
;
406 property halign
: Integer read mHAlign write mHAlign
;
407 property valign
: Integer read mVAlign write mVAlign
;
408 property header
: Boolean read mHeader write mHeader
;
409 property line
: Boolean read mLine write mLine
;
412 // ////////////////////////////////////////////////////////////////////// //
413 TUITextLabel
= class(TUIControl
)
416 mHAlign
: Integer; // -1: left; 0: center; 1: right; default: left
417 mVAlign
: Integer; // -1: top; 0: center; 1: bottom; default: center
419 mHotOfs
: Integer; // from text start, in pixels
420 mHotColor
: array[0..ClrIdxMax
] of TGxRGBA
;
421 mLinkId
: AnsiString; // linked control
424 procedure cacheStyle (root
: TUIStyle
); override;
426 procedure setText (const s
: AnsiString); virtual;
429 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
431 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
433 procedure doAction (); override;
435 procedure drawControl (gx
, gy
: Integer); override;
437 procedure mouseEvent (var ev
: TFUIEvent
); override;
438 procedure keyEventBubble (var ev
: TFUIEvent
); override;
441 property text: AnsiString read mText write setText
;
442 property halign
: Integer read mHAlign write mHAlign
;
443 property valign
: Integer read mVAlign write mVAlign
;
446 // ////////////////////////////////////////////////////////////////////// //
447 TUIButton
= class(TUITextLabel
)
449 mSkipLayPrepare
: Boolean;
450 mShadowSize
: Integer;
451 mAddMarkers
: Boolean;
452 mHideMarkers
: Boolean;
456 procedure setText (const s
: AnsiString); override;
458 procedure cacheStyle (root
: TUIStyle
); override;
460 procedure blurred (); override;
463 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
465 procedure layPrepare (); override; // called before registering control in layouter
467 procedure drawControl (gx
, gy
: Integer); override;
469 procedure mouseEvent (var ev
: TFUIEvent
); override;
470 procedure keyEvent (var ev
: TFUIEvent
); override;
473 // ////////////////////////////////////////////////////////////////////// //
474 TUIButtonRound
= class(TUIButton
)
476 procedure setText (const s
: AnsiString); override;
479 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
481 procedure layPrepare (); override; // called before registering control in layouter
483 procedure drawControl (gx
, gy
: Integer); override;
486 // ////////////////////////////////////////////////////////////////////// //
487 TUISwitchBox
= class(TUITextLabel
)
491 mIcon
: TGxContext
.TMarkIcon
;
492 mSwitchColor
: array[0..ClrIdxMax
] of TGxRGBA
;
495 procedure cacheStyle (root
: TUIStyle
); override;
497 procedure setText (const s
: AnsiString); override;
499 function getChecked (): Boolean; virtual;
500 procedure setChecked (v
: Boolean); virtual; abstract;
503 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
505 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
507 procedure drawControl (gx
, gy
: Integer); override;
509 procedure mouseEvent (var ev
: TFUIEvent
); override;
510 procedure keyEvent (var ev
: TFUIEvent
); override;
512 procedure setVar (pvar
: PBoolean);
515 property checked
: Boolean read getChecked write setChecked
;
518 TUICheckBox
= class(TUISwitchBox
)
520 procedure setChecked (v
: Boolean); override;
523 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
525 procedure doAction (); override;
528 TUIRadioBox
= class(TUISwitchBox
)
530 mRadioGroup
: AnsiString;
533 procedure setChecked (v
: Boolean); override;
536 procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
538 function parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean; override;
540 procedure doAction (); override;
543 property radioGroup
: AnsiString read mRadioGroup write mRadioGroup
; //FIXME
547 // ////////////////////////////////////////////////////////////////////////// //
548 procedure uiDispatchEvent (var evt
: TFUIEvent
);
551 procedure uiFocus ();
555 // ////////////////////////////////////////////////////////////////////////// //
556 procedure uiAddWindow (ctl
: TUIControl
);
557 procedure uiRemoveWindow (ctl
: TUIControl
); // will free window if `mFreeOnClose` is `true`
558 function uiVisibleWindow (ctl
: TUIControl
): Boolean;
560 // this can return `nil` or disabled control
561 function uiGetFocusedCtl (): TUIControl
;
563 procedure uiUpdateStyles ();
566 // ////////////////////////////////////////////////////////////////////////// //
568 procedure uiLayoutCtl (ctl
: TUIControl
);
571 // ////////////////////////////////////////////////////////////////////////// //
572 procedure uiInitialize ();
573 procedure uiDeinitialize ();
576 // ////////////////////////////////////////////////////////////////////////// //
578 fuiRenderScale
: Single = 1.0;
579 uiContext
: TGxContext
= nil;
590 uiInsideDispatcher
: Boolean = false;
591 uiTopList
: array of TUIControl
= nil;
592 uiGrabCtl
: TUIControl
= nil;
595 // ////////////////////////////////////////////////////////////////////////// //
596 procedure uiDeinitialize ();
598 FreeAndNil(uiContext
);
602 procedure uiInitialize ();
604 if (uiContext
<> nil) then raise Exception
.Create('FlexUI already initialized');
605 uiContext
:= TGxContext
.Create();
609 // ////////////////////////////////////////////////////////////////////////// //
611 ctlsToKill
: array of TUIControl
= nil;
614 procedure scheduleKill (ctl
: TUIControl
);
618 if (ctl
= nil) then exit
;
620 for f
:= 0 to High(ctlsToKill
) do
622 if (ctlsToKill
[f
] = ctl
) then exit
;
623 if (ctlsToKill
[f
] = nil) then begin ctlsToKill
[f
] := ctl
; exit
; end;
625 SetLength(ctlsToKill
, Length(ctlsToKill
)+1);
626 ctlsToKill
[High(ctlsToKill
)] := ctl
;
630 procedure processKills ();
635 for f
:= 0 to High(ctlsToKill
) do
637 ctl
:= ctlsToKill
[f
];
638 if (ctl
= nil) then break
;
639 if (uiGrabCtl
<> nil) and (ctl
.isMyChild(uiGrabCtl
)) then uiGrabCtl
:= nil; // just in case
640 ctlsToKill
[f
] := nil;
643 if (Length(ctlsToKill
) > 0) then ctlsToKill
[0] := nil; // just in case
647 // ////////////////////////////////////////////////////////////////////////// //
649 knownCtlClasses
: array of record
650 klass
: TUIControlClass
;
655 procedure registerCtlClass (aklass
: TUIControlClass
; const aname
: AnsiString);
657 assert(aklass
<> nil);
658 assert(Length(aname
) > 0);
659 SetLength(knownCtlClasses
, Length(knownCtlClasses
)+1);
660 knownCtlClasses
[High(knownCtlClasses
)].klass
:= aklass
;
661 knownCtlClasses
[High(knownCtlClasses
)].name
:= aname
;
665 function findCtlClass (const aname
: AnsiString): TUIControlClass
;
669 for f
:= 0 to High(knownCtlClasses
) do
671 if (strEquCI1251(aname
, knownCtlClasses
[f
].name
)) then
673 result
:= knownCtlClasses
[f
].klass
;
681 // ////////////////////////////////////////////////////////////////////////// //
683 TFlexLayouter
= specialize TFlexLayouterBase
<TUIControl
>;
685 procedure uiLayoutCtl (ctl
: TUIControl
);
689 if (ctl
= nil) then exit
;
690 lay
:= TFlexLayouter
.Create();
692 if (not ctl
.mStyleLoaded
) then ctl
.updateStyle();
693 if (ctl
is TUITopWindow
) and (TUITopWindow(ctl
).fitToScreen
) then TUITopWindow(ctl
).flFitToScreen();
698 //writeln('============================'); lay.dumpFlat();
700 //writeln('=== initial ==='); lay.dump();
702 //lay.calcMaxSizeInternal(0);
705 writeln('=== after first pass ===');
709 writeln('=== after second pass ===');
714 //writeln('=== final ==='); lay.dump();
716 if (ctl
.mParent
= nil) and (ctl
is TUITopWindow
) and (TUITopWindow(ctl
).mDoCenter
) then
718 TUITopWindow(ctl
).centerInScreen();
721 // calculate full size
722 ctl
.calcFullClientSize();
725 if (ctl
.mParent
= nil) then
727 if (ctl
.mFocused
= nil) or (ctl
.mFocused
= ctl
) or (not ctl
.mFocused
.enabled
) then
729 ctl
.mFocused
:= ctl
.findFirstFocus();
739 // ////////////////////////////////////////////////////////////////////////// //
740 procedure uiUpdateStyles ();
744 for ctl
in uiTopList
do ctl
.updateStyle();
748 procedure uiDispatchEvent (var evt
: TFUIEvent
);
753 procedure doSink (ctl
: TUIControl
);
755 if (ctl
= nil) or (not ev
.alive
) then exit
;
756 if (ctl
.mParent
<> nil) then
759 if (not ev
.alive
) then exit
;
761 //if (ctl = destCtl) then writeln(' SINK: MINE! <', ctl.className, '>');
764 if (ctl
= destCtl
) and (ev
.alive
) then
771 procedure dispatchTo (ctl
: TUIControl
);
773 if (ctl
= nil) then exit
;
778 //ctl := ctl.mParent; // 'cause "mine" is processed in `doSink()`
779 while (ctl
<> nil) and (ev
.alive
) do
787 procedure doMouseEvent ();
795 // pass mouse events to control with grab, if there is any
796 if (uiGrabCtl
<> nil) then
798 //writeln('GRABBED: ', uiGrabCtl.className);
799 doUngrab
:= (ev
.release
) and ((ev
.bstate
and (not ev
.but
)) = 0);
800 dispatchTo(uiGrabCtl
);
801 //FIXME: create API to get grabs, so control can regrab itself event on release
802 if (doUngrab
) and (uiGrabCtl
= destCtl
) then uiGrabCtl
:= nil;
807 if (Length(uiTopList
) > 0) then win
:= uiTopList
[High(uiTopList
)] else win
:= nil;
808 // check if we're still in top window
809 if (ev
.press
) and (win
<> nil) and (not win
.toLocal(0, 0, lx
, ly
)) then
811 // we have other windows too; check for window switching
812 for f
:= High(uiTopList
)-1 downto 0 do
814 if (uiTopList
[f
].enabled
) and (uiTopList
[f
].toLocal(ev
.x
, ev
.y
, lx
, ly
)) then
819 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
820 uiTopList
[High(uiTopList
)] := win
;
827 if (win
<> nil) and (win
.toLocal(ev
.x
, ev
.y
, lx
, ly
)) then
829 ctl
:= win
.controlAtXY(ev
.x
, ev
.y
); // don't allow disabled controls
830 if (ctl
= nil) or (not ctl
.canFocus
) or (not ctl
.enabled
) then ctl
:= win
;
831 // pass focus to another event and set grab, if necessary
834 // pass focus, if necessary
835 if (win
.mFocused
<> ctl
) then
837 if (win
.mFocused
<> nil) then win
.mFocused
.blurred();
840 if (ctl
<> win
) then ctl
.activated();
852 svx
, svy
, svdx
, svdy
: Integer;
857 if (not evt
.alive
) then exit
;
858 odp
:= uiInsideDispatcher
;
859 uiInsideDispatcher
:= true;
860 //writeln('ENTER: FUI DISPATCH');
862 // normalize mouse coordinates
863 svscale
:= fuiRenderScale
;
864 ev
.x
:= trunc(ev
.x
/svscale
);
865 ev
.y
:= trunc(ev
.y
/svscale
);
866 ev
.dx
:= trunc(ev
.dx
/svscale
); //FIXME
867 ev
.dy
:= trunc(ev
.dy
/svscale
); //FIXME
873 // "event grab" eats only mouse events
876 // we need to so some special processing here
881 // simply dispatch to focused control
882 dispatchTo(uiGetFocusedCtl
);
885 uiInsideDispatcher
:= odp
;
886 if (ev
.x
= svx
) and (ev
.y
= svy
) and (ev
.dx
= svdx
) and (ev
.dy
= svdy
) then
888 // due to possible precision loss
903 evt
.x
:= trunc(evt
.x
*svscale
);
904 evt
.y
:= trunc(evt
.y
*svscale
);
905 evt
.dx
:= trunc(evt
.dx
*svscale
);
906 evt
.dy
:= trunc(evt
.dy
*svscale
);
910 //writeln('EXIT: FUI DISPATCH');
913 procedure uiFocus ();
915 if (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)].enabled
) then uiTopList
[High(uiTopList
)].activated();
921 if (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)].enabled
) then uiTopList
[High(uiTopList
)].blurred();
931 //if (uiContext = nil) then uiContext := TGxContext.Create();
932 gxSetContext(uiContext
, fuiRenderScale
);
933 uiContext
.resetClip();
935 for f
:= 0 to High(uiTopList
) do
939 if (f
<> High(uiTopList
)) then
941 cidx
:= ctl
.getColorIndex
;
942 uiContext
.darkenRect(ctl
.x0
, ctl
.y0
, ctl
.width
, ctl
.height
, ctl
.mDarken
[cidx
]);
951 function uiGetFocusedCtl (): TUIControl
;
954 if (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)].enabled
) then
956 result
:= uiTopList
[High(uiTopList
)].mFocused
;
957 if (result
= nil) then result
:= uiTopList
[High(uiTopList
)];
962 procedure uiAddWindow (ctl
: TUIControl
);
966 if (ctl
= nil) then exit
;
968 if not (ctl
is TUITopWindow
) then exit
; // alas
969 for f
:= 0 to High(uiTopList
) do
971 if (uiTopList
[f
] = ctl
) then
973 if (f
<> High(uiTopList
)) then
975 if (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)].enabled
) then uiTopList
[High(uiTopList
)].blurred();
976 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
977 uiTopList
[High(uiTopList
)] := ctl
;
983 if (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)].enabled
) then uiTopList
[High(uiTopList
)].blurred();
984 SetLength(uiTopList
, Length(uiTopList
)+1);
985 uiTopList
[High(uiTopList
)] := ctl
;
986 if (not ctl
.mStyleLoaded
) then ctl
.updateStyle();
991 procedure uiRemoveWindow (ctl
: TUIControl
);
995 if (ctl
= nil) then exit
;
997 if not (ctl
is TUITopWindow
) then exit
; // alas
998 for f
:= 0 to High(uiTopList
) do
1000 if (uiTopList
[f
] = ctl
) then
1003 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
1004 SetLength(uiTopList
, Length(uiTopList
)-1);
1005 if (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)].enabled
) then uiTopList
[High(uiTopList
)].activated();
1006 if (ctl
is TUITopWindow
) then
1009 if assigned(TUITopWindow(ctl
).closeCB
) then TUITopWindow(ctl
).closeCB(ctl
);
1011 if (TUITopWindow(ctl
).mFreeOnClose
) then scheduleKill(ctl
);
1020 function uiVisibleWindow (ctl
: TUIControl
): Boolean;
1025 if (ctl
= nil) then exit
;
1026 ctl
:= ctl
.topLevel
;
1027 if not (ctl
is TUITopWindow
) then exit
; // alas
1028 for f
:= 0 to High(uiTopList
) do
1030 if (uiTopList
[f
] = ctl
) then begin result
:= true; exit
; end;
1035 // ////////////////////////////////////////////////////////////////////////// //
1036 constructor TUIControl
.Create ();
1041 procedure TUIControl
.AfterConstruction ();
1049 mHeight
:= uiContext
.charHeight(' ');
1057 mDrawShadow
:= false;
1059 // layouter interface
1060 //mDefSize := TLaySize.Create(64, uiContext.charHeight(' ')); // default size
1061 mDefSize
:= TLaySize
.Create(0, 0); // default size: hidden control
1062 mMaxSize
:= TLaySize
.Create(-1, -1); // maximum size
1063 mPadding
:= TLaySize
.Create(0, 0);
1071 mAlign
:= -1; // left/top
1073 mStyleLoaded
:= false;
1077 destructor TUIControl
.Destroy ();
1080 doActivateOtherWin
: Boolean = false;
1082 if (uiInsideDispatcher
) then raise Exception
.Create('FlexUI: cannot destroy objects in event dispatcher');
1083 if (uiGrabCtl
= self
) then uiGrabCtl
:= nil;
1084 // just in case, check if this is top-level shit
1085 for f
:= 0 to High(uiTopList
) do
1087 if (uiTopList
[f
] = self
) then
1089 if (uiGrabCtl
<> nil) and (isMyChild(uiGrabCtl
)) then uiGrabCtl
:= nil;
1090 for c
:= f
+1 to High(uiTopList
) do uiTopList
[c
-1] := uiTopList
[c
];
1091 SetLength(uiTopList
, Length(uiTopList
)-1);
1092 doActivateOtherWin
:= true;
1096 if (doActivateOtherWin
) and (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)].enabled
) then
1098 uiTopList
[High(uiTopList
)].activated();
1101 if (mParent
<> nil) then
1104 for f
:= 0 to High(mParent
.mChildren
) do
1106 if (mParent
.mChildren
[f
] = self
) then
1108 for c
:= f
+1 to High(mParent
.mChildren
) do mParent
.mChildren
[c
-1] := mParent
.mChildren
[c
];
1109 SetLength(mParent
.mChildren
, Length(mParent
.mChildren
)-1);
1113 for f
:= 0 to High(mChildren
) do
1115 mChildren
[f
].mParent
:= nil;
1116 mChildren
[f
].Free();
1122 function TUIControl
.getColorIndex (): Integer; inline;
1124 if (not enabled
) then begin result
:= ClrIdxDisabled
; exit
; end;
1125 // top windows: no focus hack
1126 if (self
is TUITopWindow
) then
1128 if (getActive
) then begin result
:= ClrIdxActive
; exit
; end;
1132 // if control cannot be focused, take "active" color scheme for it (it is easier this way)
1133 if (not canFocus
) or (getActive
) then begin result
:= ClrIdxActive
; exit
; end;
1135 result
:= ClrIdxInactive
;
1138 procedure TUIControl
.updateStyle ();
1140 stl
: TUIStyle
= nil;
1144 while (ctl
<> nil) do
1146 if (Length(ctl
.mStyleId
) <> 0) then begin stl
:= uiFindStyle(ctl
.mStyleId
); break
; end;
1149 if (stl
= nil) then stl
:= uiFindStyle(''); // default
1151 for ctl
in mChildren
do ctl
.updateStyle();
1152 mStyleLoaded
:= true;
1155 procedure TUIControl
.cacheStyle (root
: TUIStyle
);
1159 //writeln('caching style for <', className, '> (', mCtl4Style, ')...');
1162 mBackColor
[ClrIdxActive
] := root
.get('back-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(0, 0, 128));
1163 mTextColor
[ClrIdxActive
] := root
.get('text-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
1164 mFrameColor
[ClrIdxActive
] := root
.get('frame-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
1165 mFrameTextColor
[ClrIdxActive
] := root
.get('frame-text-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
1166 mFrameIconColor
[ClrIdxActive
] := root
.get('frame-icon-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(0, 255, 0));
1167 mSBarFullColor
[ClrIdxActive
] := root
.get('scrollbar-full-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
1168 mSBarEmptyColor
[ClrIdxActive
] := root
.get('scrollbar-empty-color', 'active', cst
).asRGBADef(TGxRGBA
.Create(128, 128, 128));
1169 mDarken
[ClrIdxActive
] := root
.get('darken', 'active', cst
).asInt(666);
1171 mBackColor
[ClrIdxDisabled
] := root
.get('back-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(0, 0, 128));
1172 mTextColor
[ClrIdxDisabled
] := root
.get('text-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(127, 127, 127));
1173 mFrameColor
[ClrIdxDisabled
] := root
.get('frame-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(127, 127, 127));
1174 mFrameTextColor
[ClrIdxDisabled
] := root
.get('frame-text-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(127, 127, 127));
1175 mFrameIconColor
[ClrIdxDisabled
] := root
.get('frame-icon-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(0, 127, 0));
1176 mSBarFullColor
[ClrIdxDisabled
] := root
.get('scrollbar-full-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(127, 127, 127));
1177 mSBarEmptyColor
[ClrIdxDisabled
] := root
.get('scrollbar-empty-color', 'disabled', cst
).asRGBADef(TGxRGBA
.Create(98, 98, 98));
1178 mDarken
[ClrIdxDisabled
] := root
.get('darken', 'disabled', cst
).asInt(666);
1180 mBackColor
[ClrIdxInactive
] := root
.get('back-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(0, 0, 128));
1181 mTextColor
[ClrIdxInactive
] := root
.get('text-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
1182 mFrameColor
[ClrIdxInactive
] := root
.get('frame-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
1183 mFrameTextColor
[ClrIdxInactive
] := root
.get('frame-text-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
1184 mFrameIconColor
[ClrIdxInactive
] := root
.get('frame-icon-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(0, 255, 0));
1185 mSBarFullColor
[ClrIdxInactive
] := root
.get('scrollbar-full-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
1186 mSBarEmptyColor
[ClrIdxInactive
] := root
.get('scrollbar-empty-color', 'inactive', cst
).asRGBADef(TGxRGBA
.Create(128, 128, 128));
1187 mDarken
[ClrIdxInactive
] := root
.get('darken', 'inactive', cst
).asInt(666);
1191 // ////////////////////////////////////////////////////////////////////////// //
1192 function TUIControl
.getDefSize (): TLaySize
; inline; begin result
:= mLayDefSize
; end;
1193 function TUIControl
.getMaxSize (): TLaySize
; inline; begin result
:= mLayMaxSize
; end;
1194 function TUIControl
.getPadding (): TLaySize
; inline; begin result
:= mPadding
; end;
1195 function TUIControl
.getFlex (): Integer; inline; begin result
:= mFlex
; end;
1196 function TUIControl
.isHorizBox (): Boolean; inline; begin result
:= mHoriz
; end;
1197 function TUIControl
.noPad (): Boolean; inline; begin result
:= mNoPad
; end;
1198 function TUIControl
.getAlign (): Integer; inline; begin result
:= mAlign
; end;
1199 function TUIControl
.getExpand (): Boolean; inline; begin result
:= mExpand
; end;
1200 function TUIControl
.getHGroup (): AnsiString; inline; begin result
:= mHGroup
; end;
1201 function TUIControl
.getVGroup (): AnsiString; inline; begin result
:= mVGroup
; end;
1202 function TUIControl
.getMargins (): TLayMargins
; inline; begin result
:= TLayMargins
.Create(mFrameHeight
, mFrameWidth
, mFrameHeight
, mFrameWidth
); end;
1204 procedure TUIControl
.setActualSizePos (constref apos
: TLayPos
; constref asize
: TLaySize
); inline;
1206 //writeln(self.className, '; pos=', apos.toString, '; size=', asize.toString);
1207 if (mParent
<> nil) then
1214 if (mLayMaxSize
.w
>= 0) then mWidth
:= nmin(mWidth
, mLayMaxSize
.w
);
1215 if (mLayMaxSize
.h
>= 0) then mHeight
:= nmin(mHeight
, mLayMaxSize
.h
);
1218 procedure TUIControl
.layPrepare ();
1220 mLayDefSize
:= mDefSize
;
1221 if (mLayDefSize
.w
<> 0) or (mLayDefSize
.h
<> 0) then
1223 mLayMaxSize
:= mMaxSize
;
1224 if (mLayMaxSize
.w
>= 0) then begin mLayDefSize
.w
+= mFrameWidth
*2; mLayMaxSize
.w
+= mFrameWidth
*2; end;
1225 if (mLayMaxSize
.h
>= 0) then begin mLayDefSize
.h
+= mFrameHeight
*2; mLayMaxSize
.h
+= mFrameHeight
*2; end;
1229 mLayMaxSize
:= TLaySize
.Create(0, 0);
1234 // ////////////////////////////////////////////////////////////////////////// //
1235 function TUIControl
.parsePos (par
: TTextParser
): TLayPos
;
1237 ech
: AnsiChar = ')';
1239 if (par
.eatDelim('[')) then ech
:= ']' else par
.expectDelim('(');
1240 result
.x
:= par
.expectInt();
1241 par
.eatDelim(','); // optional comma
1242 result
.y
:= par
.expectInt();
1243 par
.eatDelim(','); // optional comma
1244 par
.expectDelim(ech
);
1247 function TUIControl
.parseSize (par
: TTextParser
): TLaySize
;
1249 ech
: AnsiChar = ')';
1251 if (par
.eatDelim('[')) then ech
:= ']' else par
.expectDelim('(');
1252 result
.w
:= par
.expectInt();
1253 par
.eatDelim(','); // optional comma
1254 result
.h
:= par
.expectInt();
1255 par
.eatDelim(','); // optional comma
1256 par
.expectDelim(ech
);
1259 function TUIControl
.parsePadding (par
: TTextParser
): TLaySize
;
1261 result
:= parseSize(par
);
1264 function TUIControl
.parseHPadding (par
: TTextParser
; def
: Integer): TLaySize
;
1269 result
.w
:= par
.expectInt();
1273 result
:= parsePadding(par
);
1277 function TUIControl
.parseVPadding (par
: TTextParser
; def
: Integer): TLaySize
;
1282 result
.h
:= par
.expectInt();
1286 result
:= parsePadding(par
);
1290 function TUIControl
.parseBool (par
: TTextParser
): Boolean;
1293 par
.eatIdOrStrCI('true') or
1294 par
.eatIdOrStrCI('yes') or
1295 par
.eatIdOrStrCI('tan');
1298 if (not par
.eatIdOrStrCI('false')) and (not par
.eatIdOrStrCI('no')) and (not par
.eatIdOrStrCI('ona')) then
1300 par
.error('boolean value expected');
1305 function TUIControl
.parseAnyAlign (par
: TTextParser
): Integer;
1307 if (par
.eatIdOrStrCI('left')) or (par
.eatIdOrStrCI('top')) then result
:= -1
1308 else if (par
.eatIdOrStrCI('right')) or (par
.eatIdOrStrCI('bottom')) then result
:= 1
1309 else if (par
.eatIdOrStrCI('center')) then result
:= 0
1310 else par
.error('invalid align value');
1313 function TUIControl
.parseHAlign (par
: TTextParser
): Integer;
1315 if (par
.eatIdOrStrCI('left')) then result
:= -1
1316 else if (par
.eatIdOrStrCI('right')) then result
:= 1
1317 else if (par
.eatIdOrStrCI('center')) then result
:= 0
1318 else par
.error('invalid horizontal align value');
1321 function TUIControl
.parseVAlign (par
: TTextParser
): Integer;
1323 if (par
.eatIdOrStrCI('top')) then result
:= -1
1324 else if (par
.eatIdOrStrCI('bottom')) then result
:= 1
1325 else if (par
.eatIdOrStrCI('center')) then result
:= 0
1326 else par
.error('invalid vertical align value');
1329 procedure TUIControl
.parseTextAlign (par
: TTextParser
; var h
, v
: Integer);
1331 wasH
: Boolean = false;
1332 wasV
: Boolean = false;
1336 if (par
.eatIdOrStrCI('left')) then
1338 if wasH
then par
.error('too many align directives');
1343 if (par
.eatIdOrStrCI('right')) then
1345 if wasH
then par
.error('too many align directives');
1350 if (par
.eatIdOrStrCI('hcenter')) then
1352 if wasH
then par
.error('too many align directives');
1357 if (par
.eatIdOrStrCI('top')) then
1359 if wasV
then par
.error('too many align directives');
1364 if (par
.eatIdOrStrCI('bottom')) then
1366 if wasV
then par
.error('too many align directives');
1371 if (par
.eatIdOrStrCI('vcenter')) then
1373 if wasV
then par
.error('too many align directives');
1378 if (par
.eatIdOrStrCI('center')) then
1380 if wasV
or wasH
then par
.error('too many align directives');
1389 if not wasV
and not wasH
then par
.error('invalid align value');
1392 function TUIControl
.parseOrientation (const prname
: AnsiString; par
: TTextParser
): Boolean;
1394 if (strEquCI1251(prname
, 'orientation')) or (strEquCI1251(prname
, 'orient')) then
1396 if (par
.eatIdOrStrCI('horizontal')) or (par
.eatIdOrStrCI('horiz')) then mHoriz
:= true
1397 else if (par
.eatIdOrStrCI('vertical')) or (par
.eatIdOrStrCI('vert')) then mHoriz
:= false
1398 else par
.error('`horizontal` or `vertical` expected');
1407 // par should be on '{'; final '}' is eaten
1408 procedure TUIControl
.parseProperties (par
: TTextParser
);
1412 if (not par
.eatDelim('{')) then exit
;
1413 while (not par
.eatDelim('}')) do
1415 if (not par
.isIdOrStr
) then par
.error('property name expected');
1418 par
.eatDelim(':'); // optional
1419 if not parseProperty(pn
, par
) then par
.errorfmt('invalid property name ''%s''', [pn
]);
1420 par
.eatDelim(','); // optional
1424 // par should be on '{'
1425 procedure TUIControl
.parseChildren (par
: TTextParser
);
1427 cc
: TUIControlClass
;
1430 par
.expectDelim('{');
1431 while (not par
.eatDelim('}')) do
1433 if (not par
.isIdOrStr
) then par
.error('control name expected');
1434 cc
:= findCtlClass(par
.tokStr
);
1435 if (cc
= nil) then par
.errorfmt('unknown control name: ''%s''', [par
.tokStr
]);
1436 //writeln('children for <', par.tokStr, '>: <', cc.className, '>');
1438 par
.eatDelim(':'); // optional
1440 //writeln(' mHoriz=', ctl.mHoriz);
1442 ctl
.parseProperties(par
);
1447 //writeln(': ', ctl.mDefSize.toString);
1449 par
.eatDelim(','); // optional
1454 function TUIControl
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
1457 if (strEquCI1251(prname
, 'id')) then begin mId
:= par
.expectIdOrStr(true); exit
; end; // allow empty strings
1458 if (strEquCI1251(prname
, 'style')) then begin mStyleId
:= par
.expectIdOrStr(); exit
; end; // no empty strings
1459 if (strEquCI1251(prname
, 'flex')) then begin flex
:= par
.expectInt(); exit
; end;
1461 if (strEquCI1251(prname
, 'defsize')) or (strEquCI1251(prname
, 'size')) then begin mDefSize
:= parseSize(par
); exit
; end;
1462 if (strEquCI1251(prname
, 'maxsize')) then begin mMaxSize
:= parseSize(par
); exit
; end;
1463 if (strEquCI1251(prname
, 'defwidth')) or (strEquCI1251(prname
, 'width')) then begin mDefSize
.w
:= par
.expectInt(); exit
; end;
1464 if (strEquCI1251(prname
, 'defheight')) or (strEquCI1251(prname
, 'height')) then begin mDefSize
.h
:= par
.expectInt(); exit
; end;
1465 if (strEquCI1251(prname
, 'maxwidth')) then begin mMaxSize
.w
:= par
.expectInt(); exit
; end;
1466 if (strEquCI1251(prname
, 'maxheight')) then begin mMaxSize
.h
:= par
.expectInt(); exit
; end;
1468 if (strEquCI1251(prname
, 'padding')) then begin mPadding
:= parsePadding(par
); exit
; end;
1469 if (strEquCI1251(prname
, 'nopad')) then begin mNoPad
:= true; exit
; end;
1471 if (strEquCI1251(prname
, 'expand')) then begin mExpand
:= parseBool(par
); exit
; end;
1473 if (strEquCI1251(prname
, 'align')) then begin mAlign
:= parseAnyAlign(par
); exit
; end;
1474 if (strEquCI1251(prname
, 'hgroup')) then begin mHGroup
:= par
.expectIdOrStr(true); exit
; end; // allow empty strings
1475 if (strEquCI1251(prname
, 'vgroup')) then begin mVGroup
:= par
.expectIdOrStr(true); exit
; end; // allow empty strings
1477 if (strEquCI1251(prname
, 'canfocus')) then begin mCanFocus
:= true; exit
; end;
1478 if (strEquCI1251(prname
, 'nofocus')) then begin mCanFocus
:= false; exit
; end;
1479 if (strEquCI1251(prname
, 'disabled')) then begin mEnabled
:= false; exit
; end;
1480 if (strEquCI1251(prname
, 'enabled')) then begin mEnabled
:= true; exit
; end;
1481 if (strEquCI1251(prname
, 'escclose')) then begin mEscClose
:= not parseBool(par
); exit
; end;
1482 if (strEquCI1251(prname
, 'default')) then begin mDefault
:= true; exit
; end;
1483 if (strEquCI1251(prname
, 'cancel')) then begin mCancel
:= true; exit
; end;
1488 // ////////////////////////////////////////////////////////////////////////// //
1489 procedure TUIControl
.activated ();
1491 makeVisibleInParent();
1495 procedure TUIControl
.blurred ();
1497 if (uiGrabCtl
= self
) then uiGrabCtl
:= nil;
1501 procedure TUIControl
.calcFullClientSize ();
1505 mFullSize
:= TLaySize
.Create(0, 0);
1506 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1507 for ctl
in mChildren
do
1509 ctl
.calcFullClientSize();
1510 mFullSize
.w
:= nmax(mFullSize
.w
, ctl
.mX
-mFrameWidth
+ctl
.mFullSize
.w
);
1511 mFullSize
.h
:= nmax(mFullSize
.h
, ctl
.mY
-mFrameHeight
+ctl
.mFullSize
.h
);
1513 mFullSize
.w
:= nmax(mFullSize
.w
, mWidth
-mFrameWidth
*2);
1514 mFullSize
.h
:= nmax(mFullSize
.h
, mHeight
-mFrameHeight
*2);
1518 function TUIControl
.topLevel (): TUIControl
; inline;
1521 while (result
.mParent
<> nil) do result
:= result
.mParent
;
1525 function TUIControl
.getEnabled (): Boolean;
1530 if (not mEnabled
) then exit
;
1532 while (ctl
<> nil) do
1534 if (not ctl
.mEnabled
) then exit
;
1541 procedure TUIControl
.setEnabled (v
: Boolean); inline;
1543 if (mEnabled
= v
) then exit
;
1545 if (not v
) and focused
then setFocused(false);
1549 function TUIControl
.getFocused (): Boolean; inline;
1551 if (mParent
= nil) then
1553 result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = self
);
1557 result
:= (topLevel
.mFocused
= self
);
1558 if (result
) then result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = topLevel
);
1563 function TUIControl
.getActive (): Boolean; inline;
1567 if (mParent
= nil) then
1569 result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = self
);
1573 ctl
:= topLevel
.mFocused
;
1574 while (ctl
<> nil) and (ctl
<> self
) do ctl
:= ctl
.mParent
;
1575 result
:= (ctl
= self
);
1576 if (result
) then result
:= (Length(uiTopList
) > 0) and (uiTopList
[High(uiTopList
)] = topLevel
);
1581 procedure TUIControl
.setFocused (v
: Boolean); inline;
1588 if (tl
.mFocused
= self
) then
1590 blurred(); // this will reset grab, but still...
1591 if (uiGrabCtl
= self
) then uiGrabCtl
:= nil;
1592 tl
.mFocused
:= tl
.findNextFocus(self
, true);
1593 if (tl
.mFocused
= self
) then tl
.mFocused
:= nil;
1594 if (tl
.mFocused
<> nil) then tl
.mFocused
.activated();
1598 if (not canFocus
) then exit
;
1599 if (tl
.mFocused
<> self
) then
1601 if (tl
.mFocused
<> nil) then tl
.mFocused
.blurred();
1602 tl
.mFocused
:= self
;
1603 if (uiGrabCtl
<> self
) then uiGrabCtl
:= nil;
1609 function TUIControl
.getCanFocus (): Boolean; inline;
1611 result
:= (getEnabled
) and (mCanFocus
) and (mWidth
> 0) and (mHeight
> 0);
1615 function TUIControl
.isMyChild (ctl
: TUIControl
): Boolean;
1618 while (ctl
<> nil) do
1620 if (ctl
.mParent
= self
) then exit
;
1627 // returns `true` if global coords are inside this control
1628 function TUIControl
.toLocal (var x
, y
: Integer): Boolean;
1630 if (mParent
= nil) then
1634 result
:= true; // hack
1638 result
:= mParent
.toLocal(x
, y
);
1639 Inc(x
, mParent
.mScrollX
);
1640 Inc(y
, mParent
.mScrollY
);
1643 if result
then result
:= (x
>= 0) and (y
>= 0) and (x
< mParent
.mWidth
) and (y
< mParent
.mHeight
);
1645 if result
then result
:= (x
>= 0) and (y
>= 0) and (x
< mWidth
) and (y
< mHeight
);
1648 function TUIControl
.toLocal (gx
, gy
: Integer; out x
, y
: Integer): Boolean; inline;
1652 result
:= toLocal(x
, y
);
1656 procedure TUIControl
.toGlobal (var x
, y
: Integer);
1660 if (mParent
<> nil) then
1662 Dec(x
, mParent
.mScrollX
);
1663 Dec(y
, mParent
.mScrollY
);
1664 mParent
.toGlobal(x
, y
);
1668 procedure TUIControl
.toGlobal (lx
, ly
: Integer; out x
, y
: Integer); inline;
1675 procedure TUIControl
.getDrawRect (out gx
, gy
, wdt
, hgt
: Integer);
1679 if (mParent
= nil) then
1688 toGlobal(0, 0, cgx
, cgy
);
1689 mParent
.getDrawRect(gx
, gy
, wdt
, hgt
);
1690 if (wdt
> 0) and (hgt
> 0) then
1692 if (not intersectRect(gx
, gy
, wdt
, hgt
, cgx
, cgy
, mWidth
, mHeight
)) then
1702 // x and y are global coords
1703 function TUIControl
.controlAtXY (x
, y
: Integer; allowDisabled
: Boolean=false): TUIControl
;
1709 if (not allowDisabled
) and (not enabled
) then exit
;
1710 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1711 if not toLocal(x
, y
, lx
, ly
) then exit
;
1712 for f
:= High(mChildren
) downto 0 do
1714 result
:= mChildren
[f
].controlAtXY(x
, y
, allowDisabled
);
1715 if (result
<> nil) then exit
;
1721 function TUIControl
.parentScrollX (): Integer; inline; begin if (mParent
<> nil) then result
:= mParent
.mScrollX
else result
:= 0; end;
1722 function TUIControl
.parentScrollY (): Integer; inline; begin if (mParent
<> nil) then result
:= mParent
.mScrollY
else result
:= 0; end;
1725 procedure TUIControl
.makeVisibleInParent ();
1727 sy
, ey
, cy
: Integer;
1730 if (mWidth
< 1) or (mHeight
< 1) then exit
;
1732 if (p
= nil) then exit
;
1733 if (p
.mFullSize
.w
< 1) or (p
.mFullSize
.h
< 1) then
1739 p
.makeVisibleInParent();
1740 cy
:= mY
-p
.mFrameHeight
;
1742 ey
:= sy
+(p
.mHeight
-p
.mFrameHeight
*2);
1745 p
.mScrollY
:= nmax(0, cy
);
1747 else if (cy
+mHeight
> ey
) then
1749 p
.mScrollY
:= nmax(0, cy
+mHeight
-(p
.mHeight
-p
.mFrameHeight
*2));
1754 // ////////////////////////////////////////////////////////////////////////// //
1755 function TUIControl
.prevSibling (): TUIControl
;
1759 if (mParent
<> nil) then
1761 for f
:= 1 to High(mParent
.mChildren
) do
1763 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
-1]; exit
; end;
1769 function TUIControl
.nextSibling (): TUIControl
;
1773 if (mParent
<> nil) then
1775 for f
:= 0 to High(mParent
.mChildren
)-1 do
1777 if (mParent
.mChildren
[f
] = self
) then begin result
:= mParent
.mChildren
[f
+1]; exit
; end;
1783 function TUIControl
.firstChild (): TUIControl
; inline;
1785 if (Length(mChildren
) <> 0) then result
:= mChildren
[0] else result
:= nil;
1788 function TUIControl
.lastChild (): TUIControl
; inline;
1790 if (Length(mChildren
) <> 0) then result
:= mChildren
[High(mChildren
)] else result
:= nil;
1794 function TUIControl
.findFirstFocus (): TUIControl
;
1801 for f
:= 0 to High(mChildren
) do
1803 result
:= mChildren
[f
].findFirstFocus();
1804 if (result
<> nil) then exit
;
1806 if (canFocus
) then result
:= self
;
1811 function TUIControl
.findLastFocus (): TUIControl
;
1818 for f
:= High(mChildren
) downto 0 do
1820 result
:= mChildren
[f
].findLastFocus();
1821 if (result
<> nil) then exit
;
1823 if (canFocus
) then result
:= self
;
1828 function TUIControl
.findNextFocus (cur
: TUIControl
; wrap
: Boolean): TUIControl
;
1830 curHit
: Boolean = false;
1832 function checkFocus (ctl
: TUIControl
): Boolean;
1836 result
:= (ctl
.canFocus
);
1840 curHit
:= (ctl
= cur
);
1841 result
:= false; // don't stop
1849 if not isMyChild(cur
) then
1851 result
:= findFirstFocus();
1855 result
:= forEachControl(checkFocus
);
1856 if (result
= nil) and (wrap
) then result
:= findFirstFocus();
1862 function TUIControl
.findPrevFocus (cur
: TUIControl
; wrap
: Boolean): TUIControl
;
1864 lastCtl
: TUIControl
= nil;
1866 function checkFocus (ctl
: TUIControl
): Boolean;
1875 if (ctl
.canFocus
) then lastCtl
:= ctl
;
1883 if not isMyChild(cur
) then
1885 result
:= findLastFocus();
1889 forEachControl(checkFocus
);
1890 if (lastCtl
= nil) and (wrap
) then lastCtl
:= findLastFocus();
1892 //if (lastCtl <> nil) then writeln('ctl<', lastCtl.className, '>: {', lastCtl.id, '}');
1898 function TUIControl
.findDefaulControl (): TUIControl
;
1904 if (mDefault
) then begin result
:= self
; exit
; end;
1905 for ctl
in mChildren
do
1907 result
:= ctl
.findDefaulControl();
1908 if (result
<> nil) then exit
;
1914 function TUIControl
.findCancelControl (): TUIControl
;
1920 if (mCancel
) then begin result
:= self
; exit
; end;
1921 for ctl
in mChildren
do
1923 result
:= ctl
.findCancelControl();
1924 if (result
<> nil) then exit
;
1931 function TUIControl
.findControlById (const aid
: AnsiString): TUIControl
;
1935 if (strEquCI1251(aid
, mId
)) then begin result
:= self
; exit
; end;
1936 for ctl
in mChildren
do
1938 result
:= ctl
.findControlById(aid
);
1939 if (result
<> nil) then exit
;
1945 procedure TUIControl
.appendChild (ctl
: TUIControl
);
1947 if (ctl
= nil) then exit
;
1948 if (ctl
.mParent
<> nil) then exit
;
1949 SetLength(mChildren
, Length(mChildren
)+1);
1950 mChildren
[High(mChildren
)] := ctl
;
1951 ctl
.mParent
:= self
;
1952 Inc(ctl
.mX
, mFrameWidth
);
1953 Inc(ctl
.mY
, mFrameHeight
);
1954 if (ctl
.mWidth
> 0) and (ctl
.mHeight
> 0) and
1955 (ctl
.mX
+ctl
.mWidth
> mFrameWidth
) and (ctl
.mY
+ctl
.mHeight
> mFrameHeight
) then
1957 if (mWidth
+mFrameWidth
< ctl
.mX
+ctl
.mWidth
) then mWidth
:= ctl
.mX
+ctl
.mWidth
+mFrameWidth
;
1958 if (mHeight
+mFrameHeight
< ctl
.mY
+ctl
.mHeight
) then mHeight
:= ctl
.mY
+ctl
.mHeight
+mFrameHeight
;
1963 function TUIControl
.setActionCBFor (const aid
: AnsiString; cb
: TActionCB
): TActionCB
;
1968 if (ctl
<> nil) then
1970 result
:= ctl
.actionCB
;
1980 function TUIControl
.forEachChildren (cb
: TCtlEnumCB
): TUIControl
;
1985 if (not assigned(cb
)) then exit
;
1986 for ctl
in mChildren
do
1988 if cb(ctl
) then begin result
:= ctl
; exit
; end;
1993 function TUIControl
.forEachControl (cb
: TCtlEnumCB
; includeSelf
: Boolean=true): TUIControl
;
1995 function forChildren (p
: TUIControl
; incSelf
: Boolean): TUIControl
;
2000 if (p
= nil) then exit
;
2001 if (incSelf
) and (cb(p
)) then begin result
:= p
; exit
; end;
2002 for ctl
in p
.mChildren
do
2004 result
:= forChildren(ctl
, true);
2005 if (result
<> nil) then break
;
2011 if (not assigned(cb
)) then exit
;
2012 result
:= forChildren(self
, includeSelf
);
2016 procedure TUIControl
.close (); // this closes *top-level* control
2021 uiRemoveWindow(ctl
);
2022 if (ctl
is TUITopWindow
) and (TUITopWindow(ctl
).mFreeOnClose
) then scheduleKill(ctl
); // just in case
2026 procedure TUIControl
.doAction ();
2028 if assigned(actionCB
) then actionCB(self
);
2032 // ////////////////////////////////////////////////////////////////////////// //
2033 procedure TUIControl
.setScissor (lx
, ly
, lw
, lh
: Integer);
2035 gx
, gy
, wdt
, hgt
, cgx
, cgy
: Integer;
2037 if (not intersectRect(lx
, ly
, lw
, lh
, 0, 0, mWidth
, mHeight
)) then
2039 uiContext
.clip
:= TGxRect
.Create(0, 0, 0, 0);
2043 getDrawRect(gx
, gy
, wdt
, hgt
);
2045 toGlobal(lx
, ly
, cgx
, cgy
);
2046 if (not intersectRect(gx
, gy
, wdt
, hgt
, cgx
, cgy
, lw
, lh
)) then
2048 uiContext
.clip
:= TGxRect
.Create(0, 0, 0, 0);
2052 uiContext
.clip
:= savedClip
;
2053 uiContext
.combineClip(TGxRect
.Create(gx
, gy
, wdt
, hgt
));
2054 //uiContext.clip := TGxRect.Create(gx, gy, wdt, hgt);
2057 procedure TUIControl
.resetScissorNC (); inline;
2059 setScissor(0, 0, mWidth
, mHeight
);
2062 procedure TUIControl
.resetScissor (); inline;
2064 if ((mFrameWidth
<= 0) and (mFrameHeight
<= 0)) then
2070 setScissor(mFrameWidth
, mFrameHeight
, mWidth
-mFrameWidth
*2, mHeight
-mFrameHeight
*2);
2075 // ////////////////////////////////////////////////////////////////////////// //
2076 procedure TUIControl
.drawFrame (gx
, gy
, resx
, thalign
: Integer; const text: AnsiString; dbl
: Boolean);
2078 cidx
, tx
, tw
: Integer;
2080 if (mFrameWidth
< 1) or (mFrameHeight
< 1) then exit
;
2081 cidx
:= getColorIndex
;
2082 uiContext
.color
:= mFrameColor
[cidx
];
2083 case mFrameHeight
of
2088 uiContext
.rect(gx
+3, gy
+3, mWidth
-6, mHeight
-6);
2089 uiContext
.rect(gx
+5, gy
+5, mWidth
-10, mHeight
-10);
2093 uiContext
.rect(gx
+4, gy
+4, mWidth
-8, mHeight
-8);
2100 uiContext
.rect(gx
+3, gy
+3+3, mWidth
-6, mHeight
-6-6);
2101 uiContext
.rect(gx
+5, gy
+5+3, mWidth
-10, mHeight
-10-6);
2105 uiContext
.rect(gx
+4, gy
+4+3, mWidth
-8, mHeight
-8-6);
2112 uiContext
.rect(gx
+3, gy
+3+4, mWidth
-6, mHeight
-6-8);
2113 uiContext
.rect(gx
+5, gy
+5+4, mWidth
-10, mHeight
-10-8);
2117 uiContext
.rect(gx
+4, gy
+4+4, mWidth
-8, mHeight
-8-8);
2133 if (Length(text) > 0) then
2135 if (resx
< 0) then resx
:= 0;
2136 tw
:= uiContext
.textWidth(text);
2137 setScissor(mFrameWidth
+resx
, 0, mWidth
-mFrameWidth
*2-resx
, mFrameHeight
);
2138 if (thalign
< 0) then tx
:= gx
+resx
+mFrameWidth
+2
2139 else if (thalign
> 0) then tx
:= gx
+mWidth
-mFrameWidth
-1-tw
2140 else tx
:= (gx
+resx
+mFrameWidth
)+(mWidth
-mFrameWidth
*2-resx
-tw
) div 2;
2141 uiContext
.color
:= mBackColor
[cidx
];
2142 uiContext
.fillRect(tx
-2, gy
, tw
+4, mFrameHeight
);
2143 uiContext
.color
:= mFrameTextColor
[cidx
];
2144 uiContext
.drawText(tx
, gy
, text);
2149 procedure TUIControl
.draw ();
2155 if (mWidth
< 1) or (mHeight
< 1) or (uiContext
= nil) or (not uiContext
.active
) then exit
;
2156 toGlobal(0, 0, gx
, gy
);
2158 savedClip
:= uiContext
.clip
;
2161 drawControl(gx
, gy
);
2163 for f
:= 0 to High(mChildren
) do mChildren
[f
].draw();
2165 drawControlPost(gx
, gy
);
2167 uiContext
.clip
:= savedClip
;
2171 procedure TUIControl
.drawControl (gx
, gy
: Integer);
2175 procedure TUIControl
.drawControlPost (gx
, gy
: Integer);
2177 // shadow for top-level controls
2178 if (mParent
= nil) and (mDrawShadow
) and (mWidth
> 0) and (mHeight
> 0) then
2180 uiContext
.resetClip();
2181 uiContext
.darkenRect(gx
+mWidth
, gy
+8, 8, mHeight
, 128);
2182 uiContext
.darkenRect(gx
+8, gy
+mHeight
, mWidth
-8, 8, 128);
2187 // ////////////////////////////////////////////////////////////////////////// //
2188 procedure TUIControl
.onEvent (var ev
: TFUIEvent
);
2190 if (not ev
.alive
) or (not enabled
) then exit
;
2191 //if (ev.mine) then writeln(' MINE: <', className, '>');
2194 if (ev
.sinking
) then keyEventSink(ev
)
2195 else if (ev
.bubbling
) then keyEventBubble(ev
)
2196 else if (ev
.mine
) then keyEvent(ev
);
2198 else if (ev
.mouse
) then
2200 if (ev
.sinking
) then mouseEventSink(ev
)
2201 else if (ev
.bubbling
) then mouseEventBubble(ev
)
2202 else if (ev
.mine
) then mouseEvent(ev
);
2207 procedure TUIControl
.mouseEventSink (var ev
: TFUIEvent
);
2211 procedure TUIControl
.mouseEventBubble (var ev
: TFUIEvent
);
2215 procedure TUIControl
.mouseEvent (var ev
: TFUIEvent
);
2220 procedure TUIControl
.keyEventSink (var ev
: TFUIEvent
);
2224 if (not enabled
) then exit
;
2225 if (not ev
.alive
) then exit
;
2226 // for top-level controls
2227 if (mParent
<> nil) then exit
;
2228 if (mEscClose
) and (ev
= 'Escape') then
2230 if (not assigned(closeRequestCB
)) or (closeRequestCB(self
)) then
2232 uiRemoveWindow(self
);
2237 if (ev
= 'Enter') or (ev
= 'C-Enter') then
2239 ctl
:= findDefaulControl();
2240 if (ctl
<> nil) then
2247 if (ev
= 'Escape') then
2249 ctl
:= findCancelControl();
2250 if (ctl
<> nil) then
2259 procedure TUIControl
.keyEventBubble (var ev
: TFUIEvent
);
2263 if (not enabled
) then exit
;
2264 if (not ev
.alive
) then exit
;
2265 // for top-level controls
2266 if (mParent
<> nil) then exit
;
2267 if (ev
= 'S-Tab') then
2269 ctl
:= findPrevFocus(mFocused
, true);
2270 if (ctl
<> nil) and (ctl
<> mFocused
) then ctl
.setFocused(true);
2274 if (ev
= 'Tab') then
2276 ctl
:= findNextFocus(mFocused
, true);
2277 if (ctl
<> nil) and (ctl
<> mFocused
) then ctl
.setFocused(true);
2283 procedure TUIControl
.keyEvent (var ev
: TFUIEvent
);
2288 // ////////////////////////////////////////////////////////////////////////// //
2289 constructor TUITopWindow
.Create (const atitle
: AnsiString);
2296 procedure TUITopWindow
.AfterConstruction ();
2299 mFitToScreen
:= true;
2301 mFrameHeight
:= uiContext
.charHeight(#184);
2302 if (mWidth
< mFrameWidth
*2+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
)) then mWidth
:= mFrameWidth
*2+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
);
2303 if (mHeight
< mFrameHeight
*2) then mHeight
:= mFrameHeight
*2;
2304 if (Length(mTitle
) > 0) then
2306 if (mWidth
< uiContext
.textWidth(mTitle
)+mFrameWidth
*2+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
)) then
2308 mWidth
:= uiContext
.textWidth(mTitle
)+mFrameWidth
*2+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
);
2312 mDragScroll
:= TXMode
.None
;
2313 mDrawShadow
:= true;
2314 mWaitingClose
:= false;
2317 mCtl4Style
:= 'window';
2318 mDefSize
.w
:= nmax(1, mDefSize
.w
);
2319 mDefSize
.h
:= nmax(1, mDefSize
.h
);
2323 function TUITopWindow
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2325 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
2327 mTitle
:= par
.expectIdOrStr(true);
2331 if (strEquCI1251(prname
, 'children')) then
2337 if (strEquCI1251(prname
, 'position')) then
2339 if (par
.eatIdOrStrCI('default')) then mDoCenter
:= false
2340 else if (par
.eatIdOrStrCI('center')) then mDoCenter
:= true
2341 else par
.error('`center` or `default` expected');
2345 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
2346 result
:= inherited parseProperty(prname
, par
);
2350 procedure TUITopWindow
.flFitToScreen ();
2354 nsz
:= TLaySize
.Create(trunc(fuiScrWdt
/fuiRenderScale
)-mFrameWidth
*2-6, trunc(fuiScrHgt
/fuiRenderScale
)-mFrameHeight
*2-6);
2355 if (mMaxSize
.w
< 1) then mMaxSize
.w
:= nsz
.w
;
2356 if (mMaxSize
.h
< 1) then mMaxSize
.h
:= nsz
.h
;
2360 procedure TUITopWindow
.centerInScreen ();
2362 if (mWidth
> 0) and (mHeight
> 0) then
2364 mX
:= trunc((fuiScrWdt
/fuiRenderScale
-mWidth
)/2);
2365 mY
:= trunc((fuiScrHgt
/fuiRenderScale
-mHeight
)/2);
2370 // ////////////////////////////////////////////////////////////////////////// //
2371 procedure TUITopWindow
.drawControl (gx
, gy
: Integer);
2373 uiContext
.color
:= mBackColor
[getColorIndex
];
2374 uiContext
.fillRect(gx
, gy
, mWidth
, mHeight
);
2377 procedure TUITopWindow
.drawControlPost (gx
, gy
: Integer);
2379 cidx
, iwdt
, ihgt
: Integer;
2380 ybot
, xend
, vhgt
, vwdt
: Integer;
2382 cidx
:= getColorIndex
;
2383 iwdt
:= uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
);
2384 if (mDragScroll
= TXMode
.Drag
) then
2386 drawFrame(gx
, gy
, iwdt
, 0, mTitle
, false);
2390 ihgt
:= uiContext
.iconWinHeight(TGxContext
.TWinIcon
.Close
);
2391 drawFrame(gx
, gy
, iwdt
, 0, mTitle
, true);
2392 // vertical scroll bar
2393 vhgt
:= mHeight
-mFrameHeight
*2;
2394 if (mFullSize
.h
> vhgt
) then
2396 ybot
:= mScrollY
+vhgt
;
2398 uiContext
.drawVSBar(gx
+mWidth
-mFrameWidth
+1, gy
+mFrameHeight
-1, mFrameWidth
-3, vhgt
+2, ybot
, 0, mFullSize
.h
, mSBarFullColor
[cidx
], mSBarEmptyColor
[cidx
]);
2400 // horizontal scroll bar
2401 vwdt
:= mWidth
-mFrameWidth
*2;
2402 if (mFullSize
.w
> vwdt
) then
2404 xend
:= mScrollX
+vwdt
;
2406 uiContext
.drawHSBar(gx
+mFrameWidth
+1, gy
+mHeight
-mFrameHeight
+1, vwdt
-2, mFrameHeight
-3, xend
, 0, mFullSize
.w
, mSBarFullColor
[cidx
], mSBarEmptyColor
[cidx
]);
2409 setScissor(mFrameWidth
, 0, iwdt
, ihgt
);
2410 uiContext
.color
:= mBackColor
[cidx
];
2411 uiContext
.fillRect(gx
+mFrameWidth
, gy
, iwdt
, ihgt
);
2412 uiContext
.color
:= mFrameIconColor
[cidx
];
2413 uiContext
.drawIconWin(TGxContext
.TWinIcon
.Close
, gx
+mFrameWidth
, gy
, mInClose
);
2415 // shadow (no need to reset scissor, as draw should do it)
2416 inherited drawControlPost(gx
, gy
);
2420 // ////////////////////////////////////////////////////////////////////////// //
2421 procedure TUITopWindow
.activated ();
2423 if (mFocused
= nil) or (mFocused
= self
) then
2425 mFocused
:= findFirstFocus();
2427 if (mFocused
<> nil) and (mFocused
<> self
) then mFocused
.activated();
2432 procedure TUITopWindow
.blurred ();
2434 mDragScroll
:= TXMode
.None
;
2435 mWaitingClose
:= false;
2437 if (mFocused
<> nil) and (mFocused
<> self
) then mFocused
.blurred();
2442 procedure TUITopWindow
.keyEventBubble (var ev
: TFUIEvent
);
2444 inherited keyEvent(ev
);
2445 if (not ev
.alive
) or (not enabled
) {or (not getFocused)} then exit
;
2446 if (ev
= 'M-F3') then
2448 if (not assigned(closeRequestCB
)) or (closeRequestCB(self
)) then
2450 uiRemoveWindow(self
);
2458 procedure TUITopWindow
.mouseEvent (var ev
: TFUIEvent
);
2461 vhgt
, ytop
: Integer;
2462 vwdt
, xend
: Integer;
2464 if (not enabled
) then exit
;
2465 if (mWidth
< 1) or (mHeight
< 1) then exit
;
2467 if (mDragScroll
= TXMode
.Drag
) then
2469 mX
+= ev
.x
-mDragStartX
;
2470 mY
+= ev
.y
-mDragStartY
;
2471 mDragStartX
:= ev
.x
;
2472 mDragStartY
:= ev
.y
;
2473 if (ev
.release
) and (ev
.but
= ev
.Left
) then mDragScroll
:= TXMode
.None
;
2478 if (mDragScroll
= TXMode
.VScroll
) then
2481 vhgt
:= mHeight
-mFrameHeight
*2;
2482 ytop
:= uiContext
.sbarPos(ly
, mFrameHeight
-1, vhgt
+2, 0, mFullSize
.h
)-vhgt
;
2483 mScrollY
:= nmax(0, ytop
);
2484 if (ev
.release
) and (ev
.but
= ev
.Left
) then mDragScroll
:= TXMode
.None
;
2489 if (mDragScroll
= TXMode
.HScroll
) then
2492 vwdt
:= mWidth
-mFrameWidth
*2;
2493 xend
:= uiContext
.sbarPos(lx
, mFrameWidth
+1, vwdt
-2, 0, mFullSize
.w
)-vwdt
;
2494 mScrollX
:= nmax(0, xend
);
2495 if (ev
.release
) and (ev
.but
= ev
.Left
) then mDragScroll
:= TXMode
.None
;
2500 if toLocal(ev
.x
, ev
.y
, lx
, ly
) then
2504 if (ly
< mFrameHeight
) then
2507 if (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
)) then
2509 //uiRemoveWindow(self);
2510 mWaitingClose
:= true;
2515 mDragScroll
:= TXMode
.Drag
;
2516 mDragStartX
:= ev
.x
;
2517 mDragStartY
:= ev
.y
;
2522 // check for vertical scrollbar
2523 if (lx
>= mWidth
-mFrameWidth
+1) and (ly
>= mFrameHeight
-1) and (ly
< mHeight
-mFrameHeight
+2) then
2525 vhgt
:= mHeight
-mFrameHeight
*2;
2526 if (mFullSize
.h
> vhgt
) then
2529 mDragScroll
:= TXMode
.VScroll
;
2531 ytop
:= uiContext
.sbarPos(ly
, mFrameHeight
-1, vhgt
+2, 0, mFullSize
.h
)-vhgt
;
2532 mScrollY
:= nmax(0, ytop
);
2536 // check for horizontal scrollbar
2537 if (ly
>= mHeight
-mFrameHeight
+1) and (lx
>= mFrameWidth
+1) and (lx
< mWidth
-mFrameWidth
-1) then
2539 vwdt
:= mWidth
-mFrameWidth
*2;
2540 if (mFullSize
.w
> vwdt
) then
2543 mDragScroll
:= TXMode
.HScroll
;
2545 xend
:= uiContext
.sbarPos(lx
, mFrameWidth
+1, vwdt
-2, 0, mFullSize
.w
)-vwdt
;
2546 mScrollX
:= nmax(0, xend
);
2551 if (lx
< mFrameWidth
) or (lx
>= mWidth
-mFrameWidth
) or (ly
>= mHeight
-mFrameHeight
) then
2554 mDragScroll
:= TXMode
.Drag
;
2555 mDragStartX
:= ev
.x
;
2556 mDragStartY
:= ev
.y
;
2562 if (ev
.release
) then
2564 if mWaitingClose
then
2566 if (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
)) then
2568 if (not assigned(closeRequestCB
)) or (closeRequestCB(self
)) then
2570 uiRemoveWindow(self
);
2573 mWaitingClose
:= false;
2582 if mWaitingClose
then
2584 mInClose
:= (lx
>= mFrameWidth
) and (lx
< mFrameWidth
+uiContext
.iconWinWidth(TGxContext
.TWinIcon
.Close
));
2590 inherited mouseEvent(ev
);
2595 if (not ev
.motion
) and (mWaitingClose
) then begin ev
.eat(); mWaitingClose
:= false; exit
; end;
2600 // ////////////////////////////////////////////////////////////////////////// //
2601 constructor TUIBox
.Create (ahoriz
: Boolean);
2608 procedure TUIBox
.AfterConstruction ();
2612 mHAlign
:= -1; // left
2613 mCtl4Style
:= 'box';
2614 mDefSize
:= TLaySize
.Create(-1, -1);
2618 procedure TUIBox
.setCaption (const acap
: AnsiString);
2621 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mCaption
)+3, uiContext
.textHeight(mCaption
));
2625 procedure TUIBox
.setHasFrame (v
: Boolean);
2628 if (mHasFrame
) then begin mFrameWidth
:= 8; mFrameHeight
:= uiContext
.charHeight(#184); end else begin mFrameWidth
:= 0; mFrameHeight
:= 0; end;
2629 if (mHasFrame
) then mNoPad
:= true;
2633 function TUIBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2635 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
2636 if (strEquCI1251(prname
, 'padding')) then
2638 if (mHoriz
) then mPadding
:= parseHPadding(par
, 0) else mPadding
:= parseVPadding(par
, 0);
2642 if (strEquCI1251(prname
, 'frame')) then
2644 setHasFrame(parseBool(par
));
2648 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) then
2650 setCaption(par
.expectIdOrStr(true));
2654 if (strEquCI1251(prname
, 'textalign')) or (strEquCI1251(prname
, 'text-align')) then
2656 mHAlign
:= parseHAlign(par
);
2660 if (strEquCI1251(prname
, 'children')) then
2666 result
:= inherited parseProperty(prname
, par
);
2670 procedure TUIBox
.drawControl (gx
, gy
: Integer);
2675 cidx
:= getColorIndex
;
2676 uiContext
.color
:= mBackColor
[cidx
];
2677 uiContext
.fillRect(gx
, gy
, mWidth
, mHeight
);
2681 drawFrame(gx
, gy
, 0, mHAlign
, mCaption
, false);
2683 // no frame -- no caption
2685 else if (Length(mCaption) > 0) then
2688 if (mHAlign < 0) then xpos := 3
2689 else if (mHAlign > 0) then xpos := mWidth-mFrameWidth*2-uiContext.textWidth(mCaption)
2690 else xpos := (mWidth-mFrameWidth*2-uiContext.textWidth(mCaption)) div 2;
2691 xpos += gx+mFrameWidth;
2693 setScissor(mFrameWidth+1, 0, mWidth-mFrameWidth-2, uiContext.textHeight(mCaption));
2694 uiContext.color := mFrameTextColor[cidx];
2695 uiContext.drawText(xpos, gy, mCaption);
2701 procedure TUIBox
.mouseEvent (var ev
: TFUIEvent
);
2705 inherited mouseEvent(ev
);
2706 if (ev
.alive
) and (enabled
) and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
2713 procedure TUIBox
.keyEvent (var ev
: TFUIEvent
);
2716 cur
, ctl
: TUIControl
;
2718 inherited keyEvent(ev
);
2719 if (not ev
.alive
) or (not ev
.press
) or (not enabled
) or (not getActive
) then exit
;
2720 if (Length(mChildren
) = 0) then exit
;
2721 if (mHoriz
) and (ev
= 'Left') then dir
:= -1
2722 else if (mHoriz
) and (ev
= 'Right') then dir
:= 1
2723 else if (not mHoriz
) and (ev
= 'Up') then dir
:= -1
2724 else if (not mHoriz
) and (ev
= 'Down') then dir
:= 1;
2725 if (dir
= 0) then exit
;
2727 cur
:= topLevel
.mFocused
;
2728 while (cur
<> nil) and (cur
.mParent
<> self
) do cur
:= cur
.mParent
;
2729 //if (cur = nil) then writeln('CUR: nil') else writeln('CUR: ', cur.className, '#', cur.id);
2730 if (dir
< 0) then ctl
:= findPrevFocus(cur
, true) else ctl
:= findNextFocus(cur
, true);
2731 //if (ctl = nil) then writeln('CTL: nil') else writeln('CTL: ', ctl.className, '#', ctl.id);
2732 if (ctl
<> nil) and (ctl
<> self
) then
2734 ctl
.focused
:= true;
2739 // ////////////////////////////////////////////////////////////////////////// //
2740 constructor TUIHBox
.Create ();
2745 procedure TUIHBox
.AfterConstruction ();
2752 // ////////////////////////////////////////////////////////////////////////// //
2753 constructor TUIVBox
.Create ();
2758 procedure TUIVBox
.AfterConstruction ();
2765 // ////////////////////////////////////////////////////////////////////////// //
2766 procedure TUISpan
.AfterConstruction ();
2772 mCtl4Style
:= 'span';
2773 mDefSize
:= TLaySize
.Create(-1, -1);
2777 function TUISpan
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2779 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
2780 result
:= inherited parseProperty(prname
, par
);
2784 // ////////////////////////////////////////////////////////////////////// //
2785 procedure TUILine
.AfterConstruction ();
2791 mCtl4Style
:= 'line';
2792 mDefSize
:= TLaySize
.Create(-1, -1);
2796 function TUILine
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2798 if (parseOrientation(prname
, par
)) then begin result
:= true; exit
; end;
2799 result
:= inherited parseProperty(prname
, par
);
2803 procedure TUILine
.layPrepare ();
2805 inherited layPrepare();
2806 if (mParent
<> nil) then mHoriz
:= not mParent
.mHoriz
;
2809 if (mLayDefSize
.w
< 0) then mLayDefSize
.w
:= 1;
2810 if (mLayDefSize
.h
< 0) then mLayDefSize
.h
:= 7;
2814 if (mLayDefSize
.w
< 0) then mLayDefSize
.w
:= 7;
2815 if (mLayDefSize
.h
< 0) then mLayDefSize
.h
:= 1;
2820 procedure TUILine
.drawControl (gx
, gy
: Integer);
2824 cidx
:= getColorIndex
;
2825 uiContext
.color
:= mTextColor
[cidx
];
2826 if mHoriz
then uiContext
.hline(gx
, gy
+(mHeight
div 2), mWidth
)
2827 else uiContext
.vline(gx
+(mWidth
div 2), gy
, mHeight
);
2831 // ////////////////////////////////////////////////////////////////////////// //
2832 procedure TUIStaticText
.AfterConstruction ();
2838 mHoriz
:= true; // nobody cares
2841 mCtl4Style
:= 'static';
2845 procedure TUIStaticText
.setText (const atext
: AnsiString);
2848 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
), uiContext
.textHeight(mText
));
2852 function TUIStaticText
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
2854 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) or (strEquCI1251(prname
, 'text')) then
2856 setText(par
.expectIdOrStr(true));
2860 if (strEquCI1251(prname
, 'textalign')) or (strEquCI1251(prname
, 'text-align')) then
2862 parseTextAlign(par
, mHAlign
, mVAlign
);
2866 if (strEquCI1251(prname
, 'header')) then
2872 if (strEquCI1251(prname
, 'line')) then
2878 result
:= inherited parseProperty(prname
, par
);
2882 procedure TUIStaticText
.drawControl (gx
, gy
: Integer);
2884 xpos
, ypos
: Integer;
2887 cidx
:= getColorIndex
;
2888 uiContext
.color
:= mBackColor
[cidx
];
2889 uiContext
.fillRect(gx
, gy
, mWidth
, mHeight
);
2891 if (mHAlign
< 0) then xpos
:= 0
2892 else if (mHAlign
> 0) then xpos
:= mWidth
-uiContext
.textWidth(mText
)
2893 else xpos
:= (mWidth
-uiContext
.textWidth(mText
)) div 2;
2895 if (Length(mText
) > 0) then
2897 if (mHeader
) then uiContext
.color
:= mFrameTextColor
[cidx
] else uiContext
.color
:= mTextColor
[cidx
];
2899 if (mVAlign
< 0) then ypos
:= 0
2900 else if (mVAlign
> 0) then ypos
:= mHeight
-uiContext
.textHeight(mText
)
2901 else ypos
:= (mHeight
-uiContext
.textHeight(mText
)) div 2;
2903 uiContext
.drawText(gx
+xpos
, gy
+ypos
, mText
);
2908 if (mHeader
) then uiContext
.color
:= mFrameColor
[cidx
] else uiContext
.color
:= mTextColor
[cidx
];
2910 if (mVAlign
< 0) then ypos
:= 0
2911 else if (mVAlign
> 0) then ypos
:= mHeight
-1
2912 else ypos
:= (mHeight
div 2);
2915 if (Length(mText
) = 0) then
2917 uiContext
.hline(gx
, ypos
, mWidth
);
2921 uiContext
.hline(gx
, ypos
, xpos
-1);
2922 uiContext
.hline(gx
+xpos
+uiContext
.textWidth(mText
), ypos
, mWidth
);
2928 // ////////////////////////////////////////////////////////////////////////// //
2929 procedure TUITextLabel
.AfterConstruction ();
2935 mCtl4Style
:= 'label';
2940 procedure TUITextLabel
.cacheStyle (root
: TUIStyle
);
2942 inherited cacheStyle(root
);
2944 mHotColor
[ClrIdxActive
] := root
.get('hot-color', 'active', mCtl4Style
).asRGBADef(TGxRGBA
.Create(0, 128, 0));
2946 mHotColor
[ClrIdxDisabled
] := root
.get('hot-color', 'disabled', mCtl4Style
).asRGBADef(TGxRGBA
.Create(0, 64, 0));
2948 mHotColor
[ClrIdxInactive
] := root
.get('hot-color', 'inactive', mCtl4Style
).asRGBADef(TGxRGBA
.Create(0, 64, 0));
2952 procedure TUITextLabel
.setText (const s
: AnsiString);
2960 while (f
<= Length(s
)) do
2962 if (s
[f
] = '\\') then
2965 if (f
<= Length(s
)) then mText
+= s
[f
];
2968 else if (s
[f
] = '~') then
2971 if (f
<= Length(s
)) then
2973 if (mHotChar
= #0) then
2976 mHotOfs
:= Length(mText
);
2988 // fix hotchar offset
2989 if (mHotChar
<> #0) and (mHotOfs
> 0) then
2991 mHotOfs
:= uiContext
.textWidth(Copy(mText
, 1, mHotOfs
+1))-uiContext
.charWidth(mText
[mHotOfs
+1]);
2994 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
), uiContext
.textHeight(mText
));
2998 function TUITextLabel
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
3000 if (strEquCI1251(prname
, 'title')) or (strEquCI1251(prname
, 'caption')) or (strEquCI1251(prname
, 'text')) then
3002 setText(par
.expectIdOrStr(true));
3006 if (strEquCI1251(prname
, 'link')) then
3008 mLinkId
:= par
.expectIdOrStr(true);
3012 if (strEquCI1251(prname
, 'textalign')) or (strEquCI1251(prname
, 'text-align')) then
3014 parseTextAlign(par
, mHAlign
, mVAlign
);
3018 result
:= inherited parseProperty(prname
, par
);
3022 procedure TUITextLabel
.drawControl (gx
, gy
: Integer);
3024 xpos
, ypos
: Integer;
3027 cidx
:= getColorIndex
;
3028 uiContext
.color
:= mBackColor
[cidx
];
3029 uiContext
.fillRect(gx
, gy
, mWidth
, mHeight
);
3030 if (Length(mText
) > 0) then
3032 if (mHAlign
< 0) then xpos
:= 0
3033 else if (mHAlign
> 0) then xpos
:= mWidth
-uiContext
.textWidth(mText
)
3034 else xpos
:= (mWidth
-uiContext
.textWidth(mText
)) div 2;
3036 if (mVAlign
< 0) then ypos
:= 0
3037 else if (mVAlign
> 0) then ypos
:= mHeight
-uiContext
.textHeight(mText
)
3038 else ypos
:= (mHeight
-uiContext
.textHeight(mText
)) div 2;
3040 uiContext
.color
:= mTextColor
[cidx
];
3041 uiContext
.drawText(gx
+xpos
, gy
+ypos
, mText
);
3043 if (Length(mLinkId
) > 0) and (mHotChar
<> #0) and (mHotChar
<> ' ') then
3045 uiContext
.color
:= mHotColor
[cidx
];
3046 uiContext
.drawChar(gx
+xpos
+mHotOfs
, gy
+ypos
, mHotChar
);
3052 procedure TUITextLabel
.mouseEvent (var ev
: TFUIEvent
);
3056 inherited mouseEvent(ev
);
3057 if (ev
.alive
) and (enabled
) and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
3064 procedure TUITextLabel
.doAction ();
3068 if (assigned(actionCB
)) then
3074 ctl
:= topLevel
[mLinkId
];
3075 if (ctl
<> nil) then
3077 if (ctl
.canFocus
) then ctl
.focused
:= true;
3083 procedure TUITextLabel
.keyEventBubble (var ev
: TFUIEvent
);
3085 if (not enabled
) then exit
;
3086 if (mHotChar
= #0) then exit
;
3087 if (not ev
.alive
) or (not ev
.press
) then exit
;
3088 if (ev
.kstate
<> ev
.ModAlt
) then exit
;
3089 if (not ev
.isHot(mHotChar
)) then exit
;
3091 if (canFocus
) then focused
:= true;
3096 // ////////////////////////////////////////////////////////////////////////// //
3097 procedure TUIButton
.AfterConstruction ();
3104 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+uiContext
.textWidth('[ ]'), uiContext
.textHeight(mText
));
3105 mCtl4Style
:= 'button';
3106 mSkipLayPrepare
:= false;
3107 mAddMarkers
:= false;
3108 mHideMarkers
:= false;
3112 procedure TUIButton
.cacheStyle (root
: TUIStyle
);
3116 inherited cacheStyle(root
);
3118 sz
:= nmax(0, root
.get('shadow-size', 'active', mCtl4Style
).asInt(0));
3119 sz
:= nmax(sz
, root
.get('shadow-size', 'disabled', mCtl4Style
).asInt(0));
3120 sz
:= nmax(sz
, root
.get('shadow-size', 'inactive', mCtl4Style
).asInt(0));
3123 mAddMarkers
:= root
.get('add-markers', 'active', mCtl4Style
).asBool(false);
3124 mAddMarkers
:= mAddMarkers
or root
.get('add-markers', 'disabled', mCtl4Style
).asBool(false);
3125 mAddMarkers
:= mAddMarkers
or root
.get('add-markers', 'inactive', mCtl4Style
).asBool(false);
3127 mHideMarkers
:= root
.get('hide-markers', 'active', mCtl4Style
).asBool(false);
3128 mHideMarkers
:= mHideMarkers
or root
.get('hide-markers', 'disabled', mCtl4Style
).asBool(false);
3129 mHideMarkers
:= mHideMarkers
or root
.get('hide-markers', 'inactive', mCtl4Style
).asBool(false);
3133 procedure TUIButton
.setText (const s
: AnsiString);
3135 inherited setText(s
);
3136 if (mHideMarkers
) then
3138 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+10, uiContext
.textHeight(mText
));
3140 else if (mAddMarkers
) then
3142 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+uiContext
.textWidth('[<>]'), uiContext
.textHeight(mText
));
3146 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+uiContext
.textWidth('<>'), uiContext
.textHeight(mText
));
3151 procedure TUIButton
.layPrepare ();
3156 if (not mSkipLayPrepare
) then
3159 if (ods
.w
<> 0) or (ods
.h
<> 0) then
3161 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
), uiContext
.textHeight(mText
));
3162 if (mHideMarkers
) then
3166 else if (mAddMarkers
) then
3168 if (mDefault
) then ww
:= uiContext
.textWidth('[< >]')
3169 else if (mCancel
) then ww
:= uiContext
.textWidth('[{ }]')
3170 else ww
:= uiContext
.textWidth('[ ]');
3174 ww
:= nmax(0, uiContext
.textWidth('< >'));
3175 ww
:= nmax(ww
, uiContext
.textWidth('{ }'));
3176 ww
:= nmax(ww
, uiContext
.textWidth('[ ]'));
3178 mDefSize
.w
+= ww
+mShadowSize
;
3179 mDefSize
.h
+= mShadowSize
;
3184 ods
:= TLaySize
.Create(0, 0); // fpc is dumb!
3186 inherited layPrepare();
3187 if (not mSkipLayPrepare
) then mDefSize
:= ods
;
3191 procedure TUIButton
.blurred ();
3197 procedure TUIButton
.drawControl (gx
, gy
: Integer);
3200 xpos
, ypos
, xofsl
, xofsr
, sofs
: Integer;
3203 lstr
, rstr
: AnsiString;
3205 cidx
:= getColorIndex
;
3207 wdt
:= mWidth
-mShadowSize
;
3208 hgt
:= mHeight
-mShadowSize
;
3209 if (mPushed
) {or (cidx = ClrIdxActive)} then
3211 sofs
:= mShadowSize
;
3218 if (mShadowSize
> 0) then
3220 uiContext
.darkenRect(gx
+mShadowSize
, gy
+hgt
, wdt
, mShadowSize
, 96);
3221 uiContext
.darkenRect(gx
+wdt
, gy
+mShadowSize
, mShadowSize
, hgt
-mShadowSize
, 96);
3225 uiContext
.color
:= mBackColor
[cidx
];
3226 uiContext
.fillRect(gx
, gy
, wdt
, hgt
);
3228 if (mVAlign
< 0) then ypos
:= 0
3229 else if (mVAlign
> 0) then ypos
:= hgt
-uiContext
.textHeight(mText
)
3230 else ypos
:= (hgt
-uiContext
.textHeight(mText
)) div 2;
3233 uiContext
.color
:= mTextColor
[cidx
];
3235 if (mHideMarkers
) then
3242 if (mAddMarkers
) then
3244 if (mDefault
) then begin lstr
:= '[< '; rstr
:= ' >]'; end
3245 else if (mCancel
) then begin lstr
:= '[{ '; rstr
:= ' }]'; end
3246 else begin lstr
:= '[ '; rstr
:= ' ]'; end;
3247 xofsl
:= uiContext
.textWidth(lstr
);
3248 xofsr
:= uiContext
.textWidth(rstr
);
3249 uiContext
.drawText(gx
, ypos
, lstr
);
3250 uiContext
.drawText(gx
+wdt
-uiContext
.textWidth(rstr
), ypos
, rstr
);
3254 xofsl
:= nmax(0, uiContext
.textWidth('< '));
3255 xofsl
:= nmax(xofsl
, uiContext
.textWidth('{ '));
3256 xofsl
:= nmax(xofsl
, uiContext
.textWidth('[ '));
3257 xofsr
:= nmax(0, uiContext
.textWidth(' >'));
3258 xofsr
:= nmax(xofsr
, uiContext
.textWidth(' }'));
3259 xofsr
:= nmax(xofsr
, uiContext
.textWidth(' ]'));
3260 if (mDefault
) then begin lch
:= '<'; rch
:= '>'; end
3261 else if (mCancel
) then begin lch
:= '{'; rch
:= '}'; end
3262 else begin lch
:= '['; rch
:= ']'; end;
3263 uiContext
.drawChar(gx
, ypos
, lch
);
3264 uiContext
.drawChar(gx
+wdt
-uiContext
.charWidth(rch
), ypos
, rch
);
3268 if (Length(mText
) > 0) then
3270 if (mHAlign
< 0) then xpos
:= 0
3271 else begin xpos
:= wdt
-xofsl
-xofsr
-uiContext
.textWidth(mText
); if (mHAlign
= 0) then xpos
:= xpos
div 2; end;
3274 setScissor(sofs
+xofsl
, sofs
, wdt
-xofsl
-xofsr
, hgt
);
3275 uiContext
.drawText(gx
+xpos
, ypos
, mText
);
3277 if (mHotChar
<> #0) and (mHotChar
<> ' ') then
3279 uiContext
.color
:= mHotColor
[cidx
];
3280 uiContext
.drawChar(gx
+xpos
+mHotOfs
, ypos
, mHotChar
);
3286 procedure TUIButton
.mouseEvent (var ev
: TFUIEvent
);
3290 inherited mouseEvent(ev
);
3291 if (uiGrabCtl
= self
) then
3294 mPushed
:= toLocal(ev
.x
, ev
.y
, lx
, ly
);
3295 if (ev
= '-lmb') and (focused
) and (mPushed
) then
3302 if (not ev
.alive
) or (not enabled
) or (not focused
) then exit
;
3308 procedure TUIButton
.keyEvent (var ev
: TFUIEvent
);
3310 inherited keyEvent(ev
);
3311 if (ev
.alive
) and (enabled
) then
3313 if (ev
= '+Enter') or (ev
= '+Space') then
3320 if (focused
) and ((ev
= '-Enter') or (ev
= '-Space')) then
3338 // ////////////////////////////////////////////////////////////////////////// //
3339 procedure TUIButtonRound
.AfterConstruction ();
3345 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+8*2, uiContext
.textHeight(mText
)+2);
3346 mCtl4Style
:= 'button-round';
3347 mSkipLayPrepare
:= true;
3351 procedure TUIButtonRound
.setText (const s
: AnsiString);
3353 inherited setText(s
);
3354 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+8*2, uiContext
.textHeight(mText
)+2);
3358 procedure TUIButtonRound
.layPrepare ();
3363 if (ods
.w
<> 0) or (ods
.h
<> 0) then
3365 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+8*2, uiContext
.textHeight(mText
)+2);
3367 inherited layPrepare();
3372 procedure TUIButtonRound
.drawControl (gx
, gy
: Integer);
3374 xpos
, ypos
: Integer;
3377 cidx
:= getColorIndex
;
3379 uiContext
.color
:= mBackColor
[cidx
];
3380 uiContext
.fillRect(gx
+1, gy
, mWidth
-2, mHeight
);
3381 uiContext
.fillRect(gx
, gy
+1, 1, mHeight
-2);
3382 uiContext
.fillRect(gx
+mWidth
-1, gy
+1, 1, mHeight
-2);
3384 if (Length(mText
) > 0) then
3386 if (mHAlign
< 0) then xpos
:= 0
3387 else if (mHAlign
> 0) then xpos
:= mWidth
-uiContext
.textWidth(mText
)
3388 else xpos
:= (mWidth
-uiContext
.textWidth(mText
)) div 2;
3390 if (mVAlign
< 0) then ypos
:= 0
3391 else if (mVAlign
> 0) then ypos
:= mHeight
-uiContext
.textHeight(mText
)
3392 else ypos
:= (mHeight
-uiContext
.textHeight(mText
)) div 2;
3394 setScissor(8, 0, mWidth
-16, mHeight
);
3395 uiContext
.color
:= mTextColor
[cidx
];
3396 uiContext
.drawText(gx
+xpos
+8, gy
+ypos
, mText
);
3398 if (mHotChar
<> #0) and (mHotChar
<> ' ') then
3400 uiContext
.color
:= mHotColor
[cidx
];
3401 uiContext
.drawChar(gx
+xpos
+8+mHotOfs
, gy
+ypos
, mHotChar
);
3407 // ////////////////////////////////////////////////////////////////////////// //
3408 procedure TUISwitchBox
.AfterConstruction ();
3414 mIcon
:= TGxContext
.TMarkIcon
.Checkbox
;
3415 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+3+uiContext
.iconMarkWidth(mIcon
), nmax(uiContext
.iconMarkHeight(mIcon
), uiContext
.textHeight(mText
)));
3416 mCtl4Style
:= 'switchbox';
3418 mBoolVar
:= @mChecked
;
3422 procedure TUISwitchBox
.cacheStyle (root
: TUIStyle
);
3424 inherited cacheStyle(root
);
3426 mSwitchColor
[ClrIdxActive
] := root
.get('switch-color', 'active', mCtl4Style
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
3428 mSwitchColor
[ClrIdxDisabled
] := root
.get('switch-color', 'disabled', mCtl4Style
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
3430 mSwitchColor
[ClrIdxInactive
] := root
.get('switch-color', 'inactive', mCtl4Style
).asRGBADef(TGxRGBA
.Create(255, 255, 255));
3434 procedure TUISwitchBox
.setText (const s
: AnsiString);
3436 inherited setText(s
);
3437 mDefSize
:= TLaySize
.Create(uiContext
.textWidth(mText
)+3+uiContext
.iconMarkWidth(mIcon
), nmax(uiContext
.iconMarkHeight(mIcon
), uiContext
.textHeight(mText
)));
3441 function TUISwitchBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
3443 if (strEquCI1251(prname
, 'checked')) then
3449 result
:= inherited parseProperty(prname
, par
);
3453 function TUISwitchBox
.getChecked (): Boolean;
3455 if (mBoolVar
<> nil) then result
:= mBoolVar
^ else result
:= false;
3459 procedure TUISwitchBox
.setVar (pvar
: PBoolean);
3461 if (pvar
= nil) then pvar
:= @mChecked
;
3462 if (pvar
<> mBoolVar
) then
3465 setChecked(mBoolVar
^);
3470 procedure TUISwitchBox
.drawControl (gx
, gy
: Integer);
3472 xpos
, ypos
, iwdt
, dy
: Integer;
3475 cidx
:= getColorIndex
;
3477 iwdt
:= uiContext
.iconMarkWidth(mIcon
);
3478 if (mHAlign
< 0) then xpos
:= 0
3479 else if (mHAlign
> 0) then xpos
:= mWidth
-(uiContext
.textWidth(mText
)+3+iwdt
)
3480 else xpos
:= (mWidth
-(uiContext
.textWidth(mText
)+3+iwdt
)) div 2;
3482 if (mVAlign
< 0) then ypos
:= 0
3483 else if (mVAlign
> 0) then ypos
:= mHeight
-uiContext
.textHeight(mText
)
3484 else ypos
:= (mHeight
-uiContext
.textHeight(mText
)) div 2;
3486 uiContext
.color
:= mBackColor
[cidx
];
3487 uiContext
.fillRect(gx
, gy
, mWidth
, mHeight
);
3489 uiContext
.color
:= mSwitchColor
[cidx
];
3490 if (uiContext
.iconMarkHeight(mIcon
) < uiContext
.textHeight(mText
)) then
3492 case uiContext
.textHeight(mText
) of
3497 uiContext
.drawIconMark(mIcon
, gx
, gy
+ypos
+uiContext
.textHeight(mText
)-uiContext
.iconMarkHeight(mIcon
)-dy
, checked
);
3501 uiContext
.drawIconMark(mIcon
, gx
, gy
, checked
);
3504 uiContext
.color
:= mTextColor
[cidx
];
3505 uiContext
.drawText(gx
+xpos
+3+iwdt
, gy
+ypos
, mText
);
3507 if (mHotChar
<> #0) and (mHotChar
<> ' ') then
3509 uiContext
.color
:= mHotColor
[cidx
];
3510 uiContext
.drawChar(gx
+xpos
+3+iwdt
+mHotOfs
, gy
+ypos
, mHotChar
);
3515 procedure TUISwitchBox
.mouseEvent (var ev
: TFUIEvent
);
3519 inherited mouseEvent(ev
);
3520 if (uiGrabCtl
= self
) then
3523 if (ev
= '-lmb') and focused
and toLocal(ev
.x
, ev
.y
, lx
, ly
) then
3529 if (not ev
.alive
) or (not enabled
) or not focused
then exit
;
3534 procedure TUISwitchBox
.keyEvent (var ev
: TFUIEvent
);
3536 inherited keyEvent(ev
);
3537 if (ev
.alive
) and (enabled
) then
3539 if (ev
= 'Space') then
3549 // ////////////////////////////////////////////////////////////////////////// //
3550 procedure TUICheckBox
.AfterConstruction ();
3554 mBoolVar
:= @mChecked
;
3555 mIcon
:= TGxContext
.TMarkIcon
.Checkbox
;
3560 procedure TUICheckBox
.setChecked (v
: Boolean);
3566 procedure TUICheckBox
.doAction ();
3568 if (assigned(actionCB
)) then
3574 setChecked(not getChecked
);
3579 // ////////////////////////////////////////////////////////////////////////// //
3580 procedure TUIRadioBox
.AfterConstruction ();
3584 mBoolVar
:= @mChecked
;
3586 mIcon
:= TGxContext
.TMarkIcon
.Radiobox
;
3591 function TUIRadioBox
.parseProperty (const prname
: AnsiString; par
: TTextParser
): Boolean;
3593 if (strEquCI1251(prname
, 'group')) then
3595 mRadioGroup
:= par
.expectIdOrStr(true);
3596 if (getChecked
) then setChecked(true);
3600 if (strEquCI1251(prname
, 'checked')) then
3606 result
:= inherited parseProperty(prname
, par
);
3610 procedure TUIRadioBox
.setChecked (v
: Boolean);
3612 function resetGroup (ctl
: TUIControl
): Boolean;
3615 if (ctl
<> self
) and (ctl
is TUIRadioBox
) and (TUIRadioBox(ctl
).mRadioGroup
= mRadioGroup
) then
3617 TUIRadioBox(ctl
).mBoolVar
^ := false;
3623 if v
then topLevel
.forEachControl(resetGroup
);
3627 procedure TUIRadioBox
.doAction ();
3629 if (assigned(actionCB
)) then
3640 // ////////////////////////////////////////////////////////////////////////// //
3642 oldFocus
: procedure () = nil;
3643 oldBlur
: procedure () = nil;
3645 procedure onWinFocus (); begin uiFocus(); if (assigned(oldFocus
)) then oldFocus(); end;
3646 procedure onWinBlur (); begin fuiResetKMState(true); uiBlur(); if (assigned(oldBlur
)) then oldBlur(); end;
3649 registerCtlClass(TUIHBox
, 'hbox');
3650 registerCtlClass(TUIVBox
, 'vbox');
3651 registerCtlClass(TUISpan
, 'span');
3652 registerCtlClass(TUILine
, 'line');
3653 registerCtlClass(TUITextLabel
, 'label');
3654 registerCtlClass(TUIStaticText
, 'static');
3655 registerCtlClass(TUIButtonRound
, 'round-button');
3656 registerCtlClass(TUIButton
, 'button');
3657 registerCtlClass(TUICheckBox
, 'checkbox');
3658 registerCtlClass(TUIRadioBox
, 'radiobox');
3660 oldFocus
:= winFocusCB
;
3661 oldBlur
:= winBlurCB
;
3662 winFocusCB
:= onWinFocus
;
3663 winBlurCB
:= onWinBlur
;