DEADSOFTWARE

FlexUI: button control; slightly changed event consuming logic
[d2df-sdl.git] / src / gx / gh_ui.pas
index c33a93fb6b3d72a67fbd38f604bcf5bf32522aef..b23362bdb35207813803c6fc5682483ac94fd6de 100644 (file)
@@ -24,6 +24,7 @@ uses
   SysUtils, Classes,
   GL, GLExt, SDL2,
   gh_ui_common,
+  gh_ui_style,
   sdlcarcass, glgfx,
   xparser;
 
@@ -35,10 +36,18 @@ type
   TUIControl = class
   public
     type TActionCB = procedure (me: TUIControl; uinfo: Integer);
+    type TCloseRequestCB = function (me: TUIControl): Boolean; // top-level windows will call this before closing with icon/keyboard
+
+  public
+    const ClrIdxActive = 0;
+    const ClrIdxDisabled = 1;
+    const ClrIdxInactive = 2;
+    const ClrIdxMax = 2;
 
   private
     mParent: TUIControl;
     mId: AnsiString;
+    mStyleId: AnsiString;
     mX, mY: Integer;
     mWidth, mHeight: Integer;
     mFrameWidth, mFrameHeight: Integer;
@@ -46,15 +55,29 @@ type
     mCanFocus: Boolean;
     mChildren: array of TUIControl;
     mFocused: TUIControl; // valid only for top-level controls
-    mGrab: TUIControl; // valid only for top-level controls
     mEscClose: Boolean; // valid only for top-level controls
     mEatKeys: Boolean;
     mDrawShadow: Boolean;
+    mCancel: Boolean;
+    mDefault: Boolean;
+    // colors
+    mCtl4Style: AnsiString;
+    mBackColor: array[0..ClrIdxMax] of TGxRGBA;
+    mTextColor: array[0..ClrIdxMax] of TGxRGBA;
+    mFrameColor: array[0..ClrIdxMax] of TGxRGBA;
+    mFrameTextColor: array[0..ClrIdxMax] of TGxRGBA;
+    mFrameIconColor: array[0..ClrIdxMax] of TGxRGBA;
+    mDarken: array[0..ClrIdxMax] of Integer; // -1: none
 
   private
     scis: TScissorSave;
     scallowed: Boolean;
 
+  protected
+    procedure updateStyle (); virtual;
+    procedure cacheStyle (root: TUIStyle); virtual;
+    function getColorIndex (): Integer; inline;
+
   protected
     function getEnabled (): Boolean;
     procedure setEnabled (v: Boolean); inline;
@@ -62,6 +85,8 @@ type
     function getFocused (): Boolean; inline;
     procedure setFocused (v: Boolean); inline;
 
+    function getCanFocus (): Boolean; inline;
+
     function isMyChild (ctl: TUIControl): Boolean;
 
     function findFirstFocus (): TUIControl;
@@ -70,6 +95,11 @@ type
     function findNextFocus (cur: TUIControl): TUIControl;
     function findPrevFocus (cur: TUIControl): TUIControl;
 
+    function findCancelControl (): TUIControl;
+    function findDefaulControl (): TUIControl;
+
+    function findControlById (const aid: AnsiString): TUIControl;
+
     procedure activated (); virtual;
     procedure blurred (); virtual;
 
@@ -85,6 +115,7 @@ type
 
   public
     actionCB: TActionCB;
+    closeRequestCB: TCloseRequestCB;
 
   private
     mDefSize: TLaySize; // default size
@@ -183,10 +214,12 @@ type
     procedure toGlobal (lx, ly: Integer; out x, y: Integer); inline;
 
     // x and y are global coords
-    function controlAtXY (x, y: Integer): TUIControl;
+    function controlAtXY (x, y: Integer; allowDisabled: Boolean=false): TUIControl;
 
-    function mouseEvent (var ev: THMouseEvent): Boolean; virtual; // returns `true` if event was eaten
-    function keyEvent (var ev: THKeyEvent): Boolean; virtual; // returns `true` if event was eaten
+    procedure doAction ();
+
+    procedure mouseEvent (var ev: THMouseEvent); virtual; // returns `true` if event was eaten
+    procedure keyEvent (var ev: THKeyEvent); virtual; // returns `true` if event was eaten
 
     function prevSibling (): TUIControl;
     function nextSibling (): TUIControl;
@@ -195,8 +228,11 @@ type
 
     procedure appendChild (ctl: TUIControl); virtual;
 
+    procedure close (); // this closes *top-level* control
+
   public
     property id: AnsiString read mId;
+    property styleId: AnsiString read mStyleId;
     property x0: Integer read mX;
     property y0: Integer read mY;
     property height: Integer read mHeight;
@@ -206,6 +242,10 @@ type
     property focused: Boolean read getFocused write setFocused;
     property escClose: Boolean read mEscClose write mEscClose;
     property eatKeys: Boolean read mEatKeys write mEatKeys;
+    property cancel: Boolean read mCancel write mCancel;
+    property defctl: Boolean read mDefault write mDefault;
+    property canFocus: Boolean read getCanFocus write mCanFocus;
+    property ctlById[const aid: AnsiString]: TUIControl read findControlById; default;
   end;
 
 
@@ -219,6 +259,9 @@ type
     mFreeOnClose: Boolean; // default: false
     mDoCenter: Boolean; // after layouting
 
+  protected
+    procedure cacheStyle (root: TUIStyle); override;
+
   protected
     procedure activated (); override;
     procedure blurred (); override;
@@ -229,6 +272,8 @@ type
   public
     constructor Create (const atitle: AnsiString; ax, ay: Integer; aw: Integer=-1; ah: Integer=-1);
 
+    procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
+
     function parseProperty (const prname: AnsiString; par: TTextParser): Boolean; override;
 
     procedure centerInScreen ();
@@ -237,8 +282,8 @@ type
     procedure drawControl (gx, gy: Integer); override;
     procedure drawControlPost (gx, gy: Integer); override;
 
-    function keyEvent (var ev: THKeyEvent): Boolean; override; // returns `true` if event was eaten
-    function mouseEvent (var ev: THMouseEvent): Boolean; override; // returns `true` if event was eaten
+    procedure keyEvent (var ev: THKeyEvent); override; // returns `true` if event was eaten
+    procedure mouseEvent (var ev: THMouseEvent); override; // returns `true` if event was eaten
 
   public
     property freeOnClose: Boolean read mFreeOnClose write mFreeOnClose;
@@ -265,8 +310,7 @@ type
 
     procedure drawControl (gx, gy: Integer); override;
 
-    function mouseEvent (var ev: THMouseEvent): Boolean; override;
-    function keyEvent (var ev: THKeyEvent): Boolean; override;
+    procedure mouseEvent (var ev: THMouseEvent); override;
   end;
 
 
@@ -291,8 +335,8 @@ type
 
     procedure drawControl (gx, gy: Integer); override;
 
-    function mouseEvent (var ev: THMouseEvent): Boolean; override;
-    function keyEvent (var ev: THKeyEvent): Boolean; override;
+    procedure mouseEvent (var ev: THMouseEvent); override;
+    procedure keyEvent (var ev: THKeyEvent); override;
   end;
 
   // ////////////////////////////////////////////////////////////////////// //
@@ -304,12 +348,14 @@ type
   public
     constructor Create (ahoriz: Boolean);
 
+    procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
+
     function parseProperty (const prname: AnsiString; par: TTextParser): Boolean; override;
 
     procedure drawControl (gx, gy: Integer); override;
 
-    function mouseEvent (var ev: THMouseEvent): Boolean; override;
-    function keyEvent (var ev: THKeyEvent): Boolean; override;
+    procedure mouseEvent (var ev: THMouseEvent); override;
+    procedure keyEvent (var ev: THKeyEvent); override;
   end;
 
   TUIHBox = class(TUIBox)
@@ -335,6 +381,8 @@ type
   // ////////////////////////////////////////////////////////////////////// //
   TUILine = class(TUIControl)
   public
+    procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
+
     function parseProperty (const prname: AnsiString; par: TTextParser): Boolean; override;
 
     procedure drawControl (gx, gy: Integer); override;
@@ -366,14 +414,28 @@ type
 
     procedure drawControl (gx, gy: Integer); override;
 
-    function mouseEvent (var ev: THMouseEvent): Boolean; override;
-    function keyEvent (var ev: THKeyEvent): Boolean; override;
+    procedure mouseEvent (var ev: THMouseEvent); override;
+  end;
+
+  // ////////////////////////////////////////////////////////////////////// //
+  TUIButton = class(TUITextLabel)
+  public
+    constructor Create (const atext: AnsiString);
+
+    procedure AfterConstruction (); override; // so it will be correctly initialized when created from parser
+
+    function parseProperty (const prname: AnsiString; par: TTextParser): Boolean; override;
+
+    procedure drawControl (gx, gy: Integer); override;
+
+    procedure mouseEvent (var ev: THMouseEvent); override;
+    procedure keyEvent (var ev: THKeyEvent); override;
   end;
 
 
 // ////////////////////////////////////////////////////////////////////////// //
-function uiMouseEvent (ev: THMouseEvent): Boolean;
-function uiKeyEvent (ev: THKeyEvent): Boolean;
+procedure uiMouseEvent (var evt: THMouseEvent);
+procedure uiKeyEvent (var evt: THKeyEvent);
 procedure uiDraw ();
 
 
@@ -382,6 +444,8 @@ procedure uiAddWindow (ctl: TUIControl);
 procedure uiRemoveWindow (ctl: TUIControl); // will free window if `mFreeOnClose` is `true`
 function uiVisibleWindow (ctl: TUIControl): Boolean;
 
+procedure uiUpdateStyles ();
+
 
 // ////////////////////////////////////////////////////////////////////////// //
 // do layouting
@@ -400,6 +464,43 @@ uses
   utils;
 
 
+// ////////////////////////////////////////////////////////////////////////// //
+var
+  ctlsToKill: array of TUIControl = nil;
+
+
+procedure scheduleKill (ctl: TUIControl);
+var
+  f: Integer;
+begin
+  if (ctl = nil) then exit;
+  ctl := ctl.topLevel;
+  for f := 0 to High(ctlsToKill) do
+  begin
+    if (ctlsToKill[f] = ctl) then exit;
+    if (ctlsToKill[f] = nil) then begin ctlsToKill[f] := ctl; exit; end;
+  end;
+  SetLength(ctlsToKill, Length(ctlsToKill)+1);
+  ctlsToKill[High(ctlsToKill)] := ctl;
+end;
+
+
+procedure processKills ();
+var
+  f: Integer;
+  ctl: TUIControl;
+begin
+  for f := 0 to High(ctlsToKill) do
+  begin
+    ctl := ctlsToKill[f];
+    if (ctl = nil) then break;
+    ctlsToKill[f] := nil;
+    FreeAndNil(ctl);
+  end;
+  if (Length(ctlsToKill) > 0) then ctlsToKill[0] := nil; // just in case
+end;
+
+
 // ////////////////////////////////////////////////////////////////////////// //
 var
   knownCtlClasses: array of record
@@ -480,57 +581,93 @@ end;
 // ////////////////////////////////////////////////////////////////////////// //
 var
   uiTopList: array of TUIControl = nil;
+  uiGrabCtl: TUIControl = nil;
 
 
-function uiMouseEvent (ev: THMouseEvent): Boolean;
+procedure uiUpdateStyles ();
 var
+  ctl: TUIControl;
+begin
+  for ctl in uiTopList do ctl.updateStyle();
+end;
+
+
+procedure uiMouseEvent (var evt: THMouseEvent);
+var
+  ev: THMouseEvent;
   f, c: Integer;
   lx, ly: Integer;
   ctmp: TUIControl;
 begin
+  processKills();
+  if (evt.eaten) or (evt.cancelled) then exit;
+  ev := evt;
   ev.x := trunc(ev.x/gh_ui_scale);
   ev.y := trunc(ev.y/gh_ui_scale);
   ev.dx := trunc(ev.dx/gh_ui_scale); //FIXME
   ev.dy := trunc(ev.dy/gh_ui_scale); //FIXME
-  if (Length(uiTopList) = 0) then result := false else result := uiTopList[High(uiTopList)].mouseEvent(ev);
-  if not result and (ev.press) then
-  begin
-    for f := High(uiTopList) downto 0 do
+  try
+    if (uiGrabCtl <> nil) then
     begin
-      if uiTopList[f].toLocal(ev.x, ev.y, lx, ly) then
+      uiGrabCtl.mouseEvent(ev);
+      if (ev.release) then uiGrabCtl := nil;
+      ev.eat();
+      exit;
+    end;
+    if (Length(uiTopList) > 0) and (uiTopList[High(uiTopList)].mEnabled) then uiTopList[High(uiTopList)].mouseEvent(ev);
+    if (not ev.eaten) and (not ev.cancelled) and (ev.press) then
+    begin
+      for f := High(uiTopList) downto 0 do
       begin
-        result := true;
-        if uiTopList[f].mEnabled and (f <> High(uiTopList)) then
+        if uiTopList[f].toLocal(ev.x, ev.y, lx, ly) then
         begin
-          uiTopList[High(uiTopList)].blurred();
-          ctmp := uiTopList[f];
-          ctmp.mGrab := nil;
-          for c := f+1 to High(uiTopList) do uiTopList[c-1] := uiTopList[c];
-          uiTopList[High(uiTopList)] := ctmp;
-          ctmp.activated();
-          result := ctmp.mouseEvent(ev);
+          if (uiTopList[f].mEnabled) and (f <> High(uiTopList)) then
+          begin
+            uiTopList[High(uiTopList)].blurred();
+            ctmp := uiTopList[f];
+            uiGrabCtl := nil;
+            for c := f+1 to High(uiTopList) do uiTopList[c-1] := uiTopList[c];
+            uiTopList[High(uiTopList)] := ctmp;
+            ctmp.activated();
+            ctmp.mouseEvent(ev);
+          end;
+          ev.eat();
+          exit;
         end;
-        exit;
       end;
     end;
+  finally
+    if (ev.eaten) then evt.eat();
+    if (ev.cancelled) then evt.cancel();
   end;
 end;
 
 
-function uiKeyEvent (ev: THKeyEvent): Boolean;
+procedure uiKeyEvent (var evt: THKeyEvent);
+var
+  ev: THKeyEvent;
 begin
+  processKills();
+  if (evt.eaten) or (evt.cancelled) then exit;
+  ev := evt;
   ev.x := trunc(ev.x/gh_ui_scale);
   ev.y := trunc(ev.y/gh_ui_scale);
-  if (Length(uiTopList) = 0) then result := false else result := uiTopList[High(uiTopList)].keyEvent(ev);
-  if (ev.release) then begin result := true; exit; end;
+  try
+    if (Length(uiTopList) > 0) and (uiTopList[High(uiTopList)].mEnabled) then uiTopList[High(uiTopList)].keyEvent(ev);
+    //if (ev.release) then begin ev.eat(); exit; end;
+  finally
+    if (ev.eaten) then evt.eat();
+    if (ev.cancelled) then evt.cancel();
+  end;
 end;
 
 
 procedure uiDraw ();
 var
-  f: Integer;
+  f, cidx: Integer;
   ctl: TUIControl;
 begin
+  processKills();
   glMatrixMode(GL_MODELVIEW);
   glPushMatrix();
   try
@@ -540,7 +677,9 @@ begin
     begin
       ctl := uiTopList[f];
       ctl.draw();
-      if (f <> High(uiTopList)) then darkenRect(ctl.x0, ctl.y0, ctl.width, ctl.height, 128);
+      cidx := ctl.getColorIndex;
+      //if (f <> High(uiTopList)) then darkenRect(ctl.x0, ctl.y0, ctl.width, ctl.height, 128);
+      if (ctl.mDarken[cidx] > 0) then darkenRect(ctl.x0, ctl.y0, ctl.width, ctl.height, ctl.mDarken[cidx]);
     end;
   finally
     glMatrixMode(GL_MODELVIEW);
@@ -573,6 +712,7 @@ begin
   if (Length(uiTopList) > 0) then uiTopList[High(uiTopList)].blurred();
   SetLength(uiTopList, Length(uiTopList)+1);
   uiTopList[High(uiTopList)] := ctl;
+  ctl.updateStyle();
   ctl.activated();
 end;
 
@@ -596,7 +736,7 @@ begin
         try
           if assigned(TUITopWindow(ctl).closeCB) then TUITopWindow(ctl).closeCB(ctl, 0);
         finally
-          if (TUITopWindow(ctl).mFreeOnClose) then FreeAndNil(ctl);
+          if (TUITopWindow(ctl).mFreeOnClose) then scheduleKill(ctl);
         end;
       end;
       exit;
@@ -624,6 +764,7 @@ end;
 constructor TUIControl.Create ();
 begin
   mParent := nil;
+  mId := '';
   mX := 0;
   mY := 0;
   mWidth := 64;
@@ -634,7 +775,6 @@ begin
   mCanFocus := true;
   mChildren := nil;
   mFocused := nil;
-  mGrab := nil;
   mEscClose := false;
   mEatKeys := false;
   scallowed := false;
@@ -650,6 +790,8 @@ begin
   mLineStart := false;
   mHGroup := '';
   mVGroup := '';
+  mStyleId := '';
+  mCtl4Style := '';
   mAlign := -1; // left/top
   mExpand := false;
 end;
@@ -690,6 +832,63 @@ begin
 end;
 
 
+function TUIControl.getColorIndex (): Integer; inline;
+begin
+  if (not mEnabled) then begin result := ClrIdxDisabled; exit; end;
+  if (getFocused) then begin result := ClrIdxActive; exit; end;
+  result := ClrIdxInactive;
+end;
+
+procedure TUIControl.updateStyle ();
+var
+  stl: TUIStyle = nil;
+  ctl: TUIControl;
+begin
+  ctl := self;
+  while (ctl <> nil) do
+  begin
+    if (Length(ctl.mStyleId) <> 0) then begin stl := uiFindStyle(ctl.mStyleId); break; end;
+    ctl := ctl.mParent;
+  end;
+  if (stl = nil) then stl := uiFindStyle(''); // default
+  cacheStyle(stl);
+  for ctl in mChildren do ctl.updateStyle();
+end;
+
+procedure TUIControl.cacheStyle (root: TUIStyle);
+var
+  cst: AnsiString = '';
+begin
+  //writeln('caching style for <', className, '> (', mCtl4Style, ')...');
+  if (Length(mCtl4Style) > 0) then
+  begin
+    cst := mCtl4Style;
+    if (cst[1] <> '@') then cst := '@'+cst;
+  end;
+  // active
+  mBackColor[ClrIdxActive] := root['back-color'+cst].asRGBADef(TGxRGBA.Create(0, 0, 128));
+  mTextColor[ClrIdxActive] := root['text-color'+cst].asRGBADef(TGxRGBA.Create(255, 255, 255));
+  mFrameColor[ClrIdxActive] := root['frame-color'+cst].asRGBADef(TGxRGBA.Create(255, 255, 255));
+  mFrameTextColor[ClrIdxActive] := root['frame-text-color'+cst].asRGBADef(TGxRGBA.Create(255, 255, 255));
+  mFrameIconColor[ClrIdxActive] := root['frame-icon-color'+cst].asRGBADef(TGxRGBA.Create(0, 255, 0));
+  mDarken[ClrIdxActive] := root['darken'+cst].asIntDef(-1);
+  // disabled
+  mBackColor[ClrIdxDisabled] := root['back-color#disabled'+cst].asRGBADef(TGxRGBA.Create(0, 0, 128));
+  mTextColor[ClrIdxDisabled] := root['text-color#disabled'+cst].asRGBADef(TGxRGBA.Create(127, 127, 127));
+  mFrameColor[ClrIdxDisabled] := root['frame-color#disabled'+cst].asRGBADef(TGxRGBA.Create(127, 127, 127));
+  mFrameTextColor[ClrIdxDisabled] := root['frame-text-color#disabled'+cst].asRGBADef(TGxRGBA.Create(127, 127, 127));
+  mFrameIconColor[ClrIdxDisabled] := root['frame-icon-color#disabled'+cst].asRGBADef(TGxRGBA.Create(0, 127, 0));
+  mDarken[ClrIdxDisabled] := root['darken#disabled'+cst].asIntDef(128);
+  // inactive
+  mBackColor[ClrIdxInactive] := root['back-color#inactive'+cst].asRGBADef(TGxRGBA.Create(0, 0, 128));
+  mTextColor[ClrIdxInactive] := root['text-color#inactive'+cst].asRGBADef(TGxRGBA.Create(255, 255, 255));
+  mFrameColor[ClrIdxInactive] := root['frame-color#inactive'+cst].asRGBADef(TGxRGBA.Create(255, 255, 255));
+  mFrameTextColor[ClrIdxInactive] := root['frame-text-color#inactive'+cst].asRGBADef(TGxRGBA.Create(255, 255, 255));
+  mFrameIconColor[ClrIdxInactive] := root['frame-icon-color#inactive'+cst].asRGBADef(TGxRGBA.Create(0, 255, 0));
+  mDarken[ClrIdxInactive] := root['darken#inactive'+cst].asIntDef(128);
+end;
+
+
 // ////////////////////////////////////////////////////////////////////////// //
 function TUIControl.getDefSize (): TLaySize; inline; begin result := mLayDefSize; end;
 function TUIControl.getMaxSize (): TLaySize; inline; begin result := mLayMaxSize; end;
@@ -926,7 +1125,8 @@ end;
 function TUIControl.parseProperty (const prname: AnsiString; par: TTextParser): Boolean;
 begin
   result := true;
-  if (strEquCI1251(prname, 'id')) then begin mId := par.expectStrOrId(true); exit; end; // allow empty strings
+  if (strEquCI1251(prname, 'id')) then begin mId := par.expectIdOrStr(true); exit; end; // allow empty strings
+  if (strEquCI1251(prname, 'style')) then begin mStyleId := par.expectIdOrStr(); exit; end; // no empty strings
   if (strEquCI1251(prname, 'flex')) then begin flex := par.expectInt(); exit; end;
   // sizes
   if (strEquCI1251(prname, 'defsize')) or (strEquCI1251(prname, 'size')) then begin mDefSize := parseSize(par); exit; end;
@@ -941,14 +1141,16 @@ begin
   if (strEquCI1251(prname, 'expand')) then begin mExpand := parseBool(par); exit; end;
   // align
   if (strEquCI1251(prname, 'align')) then begin mAlign := parseAnyAlign(par); exit; end;
-  if (strEquCI1251(prname, 'hgroup')) then begin mHGroup := par.expectStrOrId(true); exit; end; // allow empty strings
-  if (strEquCI1251(prname, 'vgroup')) then begin mVGroup := par.expectStrOrId(true); exit; end; // allow empty strings
+  if (strEquCI1251(prname, 'hgroup')) then begin mHGroup := par.expectIdOrStr(true); exit; end; // allow empty strings
+  if (strEquCI1251(prname, 'vgroup')) then begin mVGroup := par.expectIdOrStr(true); exit; end; // allow empty strings
   // other
   if (strEquCI1251(prname, 'canfocus')) then begin mCanFocus := parseBool(par); exit; end;
   if (strEquCI1251(prname, 'enabled')) then begin mEnabled := parseBool(par); exit; end;
   if (strEquCI1251(prname, 'disabled')) then begin mEnabled := not parseBool(par); exit; end;
   if (strEquCI1251(prname, 'escclose')) then begin mEscClose := not parseBool(par); exit; end;
   if (strEquCI1251(prname, 'eatkeys')) then begin mEatKeys := not parseBool(par); exit; end;
+  if (strEquCI1251(prname, 'default')) then begin mDefault := true; exit; end;
+  if (strEquCI1251(prname, 'cancel')) then begin mCancel := true; exit; end;
   result := false;
 end;
 
@@ -961,7 +1163,7 @@ end;
 
 procedure TUIControl.blurred ();
 begin
-  mGrab := nil;
+  if (uiGrabCtl = self) then uiGrabCtl := nil;
 end;
 
 
@@ -977,11 +1179,11 @@ var
   ctl: TUIControl;
 begin
   result := false;
-  if (not mEnabled) or (mWidth < 1) or (mHeight < 1) then exit;
+  if (not mEnabled) then exit;
   ctl := mParent;
   while (ctl <> nil) do
   begin
-    if (not ctl.mEnabled) or (ctl.mWidth < 1) or (ctl.mHeight < 1) then exit;
+    if (not ctl.mEnabled) then exit;
     ctl := ctl.mParent;
   end;
   result := true;
@@ -992,13 +1194,21 @@ procedure TUIControl.setEnabled (v: Boolean); inline;
 begin
   if (mEnabled = v) then exit;
   mEnabled := v;
-  if not v and focused then setFocused(false);
+  if (not v) and focused then setFocused(false);
 end;
 
 
 function TUIControl.getFocused (): Boolean; inline;
 begin
-  if (mParent = nil) then result := (Length(uiTopList) > 0) and (uiTopList[High(uiTopList)] = self) else result := (topLevel.mFocused = self);
+  if (mParent = nil) then
+  begin
+    result := (Length(uiTopList) > 0) and (uiTopList[High(uiTopList)] = self);
+  end
+  else
+  begin
+    result := (topLevel.mFocused = self);
+    if (result) then result := (Length(uiTopList) > 0) and (uiTopList[High(uiTopList)] = topLevel);
+  end;
 end;
 
 
@@ -1017,17 +1227,23 @@ begin
     end;
     exit;
   end;
-  if (not mEnabled) or (not mCanFocus) then exit;
+  if (not mEnabled) or (not canFocus) then exit;
   if (tl.mFocused <> self) then
   begin
-    if (tl.mFocused <> nil) then tl.mFocused.blurred();
+    if (tl.mFocused <> nil) and (tl.mFocused <> nil) then tl.mFocused.blurred();
     tl.mFocused := self;
-    if (tl.mGrab <> self) then tl.mGrab := nil;
+    if (uiGrabCtl <> self) then uiGrabCtl := nil;
     activated();
   end;
 end;
 
 
+function TUIControl.getCanFocus (): Boolean; inline;
+begin
+  result := (mCanFocus) and (mWidth > 0) and (mHeight > 0);
+end;
+
+
 function TUIControl.isMyChild (ctl: TUIControl): Boolean;
 begin
   result := true;
@@ -1084,17 +1300,18 @@ end;
 
 
 // x and y are global coords
-function TUIControl.controlAtXY (x, y: Integer): TUIControl;
+function TUIControl.controlAtXY (x, y: Integer; allowDisabled: Boolean=false): TUIControl;
 var
   lx, ly: Integer;
   f: Integer;
 begin
   result := nil;
-  if (not mEnabled) or (mWidth < 1) or (mHeight < 1) then exit;
+  if (not allowDisabled) and (not mEnabled) then exit;
+  if (mWidth < 1) or (mHeight < 1) then exit;
   if not toLocal(x, y, lx, ly) then exit;
   for f := High(mChildren) downto 0 do
   begin
-    result := mChildren[f].controlAtXY(x, y);
+    result := mChildren[f].controlAtXY(x, y, allowDisabled);
     if (result <> nil) then exit;
   end;
   result := self;
@@ -1152,7 +1369,7 @@ begin
       result := mChildren[f].findFirstFocus();
       if (result <> nil) then exit;
     end;
-    if mCanFocus then result := self;
+    if canFocus then result := self;
   end;
 end;
 
@@ -1169,7 +1386,7 @@ begin
       result := mChildren[f].findLastFocus();
       if (result <> nil) then exit;
     end;
-    if mCanFocus then result := self;
+    if canFocus then result := self;
   end;
 end;
 
@@ -1217,6 +1434,47 @@ begin
 end;
 
 
+function TUIControl.findDefaulControl (): TUIControl;
+var
+  ctl: TUIControl;
+begin
+  if mDefault then begin result := self; exit; end;
+  for ctl in mChildren do
+  begin
+    result := ctl.findDefaulControl();
+    if (result <> nil) then exit;
+  end;
+  result := nil;
+end;
+
+function TUIControl.findCancelControl (): TUIControl;
+var
+  ctl: TUIControl;
+begin
+  if mCancel then begin result := self; exit; end;
+  for ctl in mChildren do
+  begin
+    result := ctl.findCancelControl();
+    if (result <> nil) then exit;
+  end;
+  result := nil;
+end;
+
+
+function TUIControl.findControlById (const aid: AnsiString): TUIControl;
+var
+  ctl: TUIControl;
+begin
+  if (strEquCI1251(aid, mId)) then begin result := self; exit; end;
+  for ctl in mChildren do
+  begin
+    result := ctl.findControlById(aid);
+    if (result <> nil) then exit;
+  end;
+  result := nil;
+end;
+
+
 procedure TUIControl.appendChild (ctl: TUIControl);
 begin
   if (ctl = nil) then exit;
@@ -1232,7 +1490,22 @@ begin
     if (mWidth+mFrameWidth < ctl.mX+ctl.mWidth) then mWidth := ctl.mX+ctl.mWidth+mFrameWidth;
     if (mHeight+mFrameHeight < ctl.mY+ctl.mHeight) then mHeight := ctl.mY+ctl.mHeight+mFrameHeight;
   end;
-  //if (mFocused = nil) and ctl.mEnabled and ctl.mCanFocus and (ctl.mWidth > 0) and (ctl.mHeight > 0) then mFocused := ctl;
+end;
+
+
+procedure TUIControl.close (); // this closes *top-level* control
+var
+  ctl: TUIControl;
+begin
+  ctl := topLevel;
+  uiRemoveWindow(ctl);
+  if (ctl is TUITopWindow) and (TUITopWindow(ctl).mFreeOnClose) then scheduleKill(ctl); // just in case
+end;
+
+
+procedure TUIControl.doAction ();
+begin
+  if assigned(actionCB) then actionCB(self, 0);
 end;
 
 
@@ -1305,7 +1578,7 @@ end;
 
 procedure TUIControl.drawControl (gx, gy: Integer);
 begin
-  if (mParent = nil) then darkenRect(gx, gy, mWidth, mHeight, 64);
+  //if (mParent = nil) then darkenRect(gx, gy, mWidth, mHeight, 64);
 end;
 
 procedure TUIControl.drawControlPost (gx, gy: Integer);
@@ -1321,74 +1594,82 @@ end;
 
 
 // ////////////////////////////////////////////////////////////////////////// //
-function TUIControl.mouseEvent (var ev: THMouseEvent): Boolean;
+procedure TUIControl.mouseEvent (var ev: THMouseEvent);
 var
   ctl: TUIControl;
 begin
-  result := false;
-  if not mEnabled then exit;
-  if (mParent = nil) then
-  begin
-    if (mGrab <> nil) then
-    begin
-      result := mGrab.mouseEvent(ev);
-      if (ev.release) then mGrab := nil;
-      exit;
-    end;
-  end;
+  if (not mEnabled) then exit;
   if (mWidth < 1) or (mHeight < 1) then exit;
   ctl := controlAtXY(ev.x, ev.y);
-  if (ctl <> nil) and (ctl <> self) then
+  if (ctl = nil) then exit;
+  if (ctl.canFocus) and (ev.press) then
   begin
     if (ctl <> topLevel.mFocused) then ctl.setFocused(true);
-    result := ctl.mouseEvent(ev);
-  end
-  else if (ctl = self) and assigned(actionCB) then
-  begin
-    actionCB(self, 0);
+    uiGrabCtl := ctl;
   end;
+  if (ctl <> self) then ctl.mouseEvent(ev);
+  //ev.eat();
 end;
 
 
-function TUIControl.keyEvent (var ev: THKeyEvent): Boolean;
+procedure TUIControl.keyEvent (var ev: THKeyEvent);
 var
   ctl: TUIControl;
 begin
-  result := false;
-  if not mEnabled then exit;
-  if (topLevel.mFocused <> self) and isMyChild(topLevel.mFocused) and topLevel.mFocused.mEnabled then result := topLevel.mFocused.keyEvent(ev);
-  if (mParent = nil) then
+  if (not mEnabled) then exit;
+  // focused control should process keyboard first
+  if (topLevel.mFocused <> self) and isMyChild(topLevel.mFocused) and (topLevel.mFocused.mEnabled) then
+  begin
+    topLevel.mFocused.keyEvent(ev);
+  end;
+  // for top-level controls
+  if (mParent = nil) and (not ev.eaten) and (not ev.cancelled) then
   begin
     if (ev = 'S-Tab') then
     begin
-      result := true;
       ctl := findPrevFocus(mFocused);
-      if (ctl <> mFocused) then
-      begin
-        mGrab := nil;
-        mFocused := ctl;
-      end;
+      if (ctl <> mFocused) then ctl.setFocused(true);
+      ev.eat();
       exit;
     end;
     if (ev = 'Tab') then
     begin
-      result := true;
       ctl := findNextFocus(mFocused);
-      if (ctl <> mFocused) then
+      if (ctl <> mFocused) then ctl.setFocused(true);
+      ev.eat();
+      exit;
+    end;
+    if (ev = 'Enter') or (ev = 'C-Enter') then
+    begin
+      ctl := findDefaulControl();
+      if (ctl <> nil) then
       begin
-        mGrab := nil;
-        mFocused := ctl;
+        ev.eat();
+        ctl.doAction();
+        exit;
+      end;
+    end;
+    if (ev = 'Escape') then
+    begin
+      ctl := findCancelControl();
+      if (ctl <> nil) then
+      begin
+        ev.eat();
+        ctl.doAction();
+        exit;
       end;
-      exit;
     end;
     if mEscClose and (ev = 'Escape') then
     begin
-      result := true;
-      uiRemoveWindow(self);
+      if (not assigned(closeRequestCB)) or (closeRequestCB(self)) then
+      begin
+        uiRemoveWindow(self);
+      end;
+      ev.eat();
       exit;
     end;
   end;
-  if mEatKeys then result := true;
+  if mEatKeys then ev.eat();
 end;
 
 
@@ -1399,6 +1680,11 @@ begin
   mFrameWidth := 8;
   mFrameHeight := 8;
   mTitle := atitle;
+end;
+
+procedure TUITopWindow.AfterConstruction ();
+begin
+  inherited AfterConstruction();
   if (mWidth < mFrameWidth*2+3*8) then mWidth := mFrameWidth*2+3*8;
   if (mHeight < mFrameHeight*2) then mHeight := mFrameHeight*2;
   if (Length(mTitle) > 0) then
@@ -1410,6 +1696,7 @@ begin
   mWaitingClose := false;
   mInClose := false;
   closeCB := nil;
+  mCtl4Style := '';
 end;
 
 
@@ -1417,7 +1704,7 @@ function TUITopWindow.parseProperty (const prname: AnsiString; par: TTextParser)
 begin
   if (strEquCI1251(prname, 'title')) or (strEquCI1251(prname, 'caption')) then
   begin
-    mTitle := par.expectStrOrId(true);
+    mTitle := par.expectIdOrStr(true);
     result := true;
     exit;
   end;
@@ -1440,6 +1727,12 @@ begin
 end;
 
 
+procedure TUITopWindow.cacheStyle (root: TUIStyle);
+begin
+  inherited cacheStyle(root);
+end;
+
+
 procedure TUITopWindow.centerInScreen ();
 begin
   if (mWidth > 0) and (mHeight > 0) then
@@ -1452,37 +1745,36 @@ end;
 
 procedure TUITopWindow.drawControl (gx, gy: Integer);
 begin
-  fillRect(gx, gy, mWidth, mHeight, TGxRGBA.Create(0, 0, 128));
+  fillRect(gx, gy, mWidth, mHeight, mBackColor[getColorIndex]);
 end;
 
 
 procedure TUITopWindow.drawControlPost (gx, gy: Integer);
-const r = 255;
-const g = 255;
-const b = 255;
 var
+  cidx: Integer;
   tx: Integer;
 begin
+  cidx := getColorIndex;
   if mDragging then
   begin
-    drawRectUI(mX+4, mY+4, mWidth-8, mHeight-8, TGxRGBA.Create(r, g, b));
+    drawRectUI(mX+4, mY+4, mWidth-8, mHeight-8, mFrameColor[cidx]);
   end
   else
   begin
-    drawRectUI(mX+3, mY+3, mWidth-6, mHeight-6, TGxRGBA.Create(r, g, b));
-    drawRectUI(mX+5, mY+5, mWidth-10, mHeight-10, TGxRGBA.Create(r, g, b));
+    drawRectUI(mX+3, mY+3, mWidth-6, mHeight-6, mFrameColor[cidx]);
+    drawRectUI(mX+5, mY+5, mWidth-10, mHeight-10, mFrameColor[cidx]);
     setScissor(mFrameWidth, 0, 3*8, 8);
-    fillRect(mX+mFrameWidth, mY, 3*8, 8, TGxRGBA.Create(0, 0, 128));
-    drawText8(mX+mFrameWidth, mY, '[ ]', TGxRGBA.Create(r, g, b));
-    if mInClose then drawText8(mX+mFrameWidth+7, mY, '#', TGxRGBA.Create(0, 255, 0))
-    else drawText8(mX+mFrameWidth+7, mY, '*', TGxRGBA.Create(0, 255, 0));
+    fillRect(mX+mFrameWidth, mY, 3*8, 8, mBackColor[cidx]);
+    drawText8(mX+mFrameWidth, mY, '[ ]', mFrameColor[cidx]);
+    if mInClose then drawText8(mX+mFrameWidth+7, mY, '#', mFrameIconColor[cidx])
+    else drawText8(mX+mFrameWidth+7, mY, '*', mFrameIconColor[cidx]);
   end;
   if (Length(mTitle) > 0) then
   begin
     setScissor(mFrameWidth+3*8, 0, mWidth-mFrameWidth*2-3*8, 8);
     tx := (mX+3*8)+((mWidth-3*8)-Length(mTitle)*8) div 2;
-    fillRect(tx-3, mY, Length(mTitle)*8+3+2, 8, TGxRGBA.Create(0, 0, 128));
-    drawText8(tx, mY, mTitle, TGxRGBA.Create(r, g, b));
+    fillRect(tx-3, mY, Length(mTitle)*8+3+2, 8, mBackColor[cidx]);
+    drawText8(tx, mY, mTitle, mFrameTextColor[cidx]);
   end;
   inherited drawControlPost(gx, gy);
 end;
@@ -1508,25 +1800,27 @@ begin
 end;
 
 
-function TUITopWindow.keyEvent (var ev: THKeyEvent): Boolean;
+procedure TUITopWindow.keyEvent (var ev: THKeyEvent);
 begin
-  result := inherited keyEvent(ev);
-  if not getFocused then exit;
+  inherited keyEvent(ev);
+  if (ev.eaten) or (ev.cancelled) or (not mEnabled) or (not getFocused) then exit;
   if (ev = 'M-F3') then
   begin
-    uiRemoveWindow(self);
-    result := true;
+    if (not assigned(closeRequestCB)) or (closeRequestCB(self)) then
+    begin
+      uiRemoveWindow(self);
+    end;
+    ev.eat();
     exit;
   end;
 end;
 
 
-function TUITopWindow.mouseEvent (var ev: THMouseEvent): Boolean;
+procedure TUITopWindow.mouseEvent (var ev: THMouseEvent);
 var
   lx, ly: Integer;
 begin
-  result := false;
-  if not mEnabled then exit;
+  if (not mEnabled) then exit;
   if (mWidth < 1) or (mHeight < 1) then exit;
 
   if mDragging then
@@ -1536,7 +1830,7 @@ begin
     mDragStartX := ev.x;
     mDragStartY := ev.y;
     if (ev.release) then mDragging := false;
-    result := true;
+    ev.eat();
     exit;
   end;
 
@@ -1546,6 +1840,7 @@ begin
     begin
       if (ly < 8) then
       begin
+        uiGrabCtl := self;
         if (lx >= mFrameWidth) and (lx < mFrameWidth+3*8) then
         begin
           //uiRemoveWindow(self);
@@ -1558,29 +1853,36 @@ begin
           mDragStartX := ev.x;
           mDragStartY := ev.y;
         end;
-        result := true;
+        ev.eat();
         exit;
       end;
       if (lx < mFrameWidth) or (lx >= mWidth-mFrameWidth) or (ly >= mHeight-mFrameHeight) then
       begin
+        uiGrabCtl := self;
         mDragging := true;
         mDragStartX := ev.x;
         mDragStartY := ev.y;
-        result := true;
+        ev.eat();
         exit;
       end;
     end;
 
     if (ev.release) then
     begin
-      if mWaitingClose and (lx >= mFrameWidth) and (lx < mFrameWidth+3*8) then
+      if mWaitingClose then
       begin
-        uiRemoveWindow(self);
-        result := true;
+        if (lx >= mFrameWidth) and (lx < mFrameWidth+3*8) then
+        begin
+          if (not assigned(closeRequestCB)) or (closeRequestCB(self)) then
+          begin
+            uiRemoveWindow(self);
+          end;
+        end;
+        mWaitingClose := false;
+        mInClose := false;
+        ev.eat();
         exit;
       end;
-      mWaitingClose := false;
-      mInClose := false;
     end;
 
     if (ev.motion) then
@@ -1588,18 +1890,18 @@ begin
       if mWaitingClose then
       begin
         mInClose := (lx >= mFrameWidth) and (lx < mFrameWidth+3*8);
-        result := true;
+        ev.eat();
         exit;
       end;
     end;
+
+    inherited mouseEvent(ev);
   end
   else
   begin
     mInClose := false;
-    if (not ev.motion) then mWaitingClose := false;
+    if (not ev.motion) and (mWaitingClose) then begin ev.eat(); mWaitingClose := false; exit; end;
   end;
-
-  result := inherited mouseEvent(ev);
 end;
 
 
@@ -1665,24 +1967,18 @@ begin
 end;
 
 
-function TUISimpleText.mouseEvent (var ev: THMouseEvent): Boolean;
+procedure TUISimpleText.mouseEvent (var ev: THMouseEvent);
 var
   lx, ly: Integer;
 begin
-  result := inherited mouseEvent(ev);
-  if not result and toLocal(ev.x, ev.y, lx, ly) then
+  inherited mouseEvent(ev);
+  if (not ev.eaten) and (not ev.cancelled) and (mEnabled) and toLocal(ev.x, ev.y, lx, ly) then
   begin
-    result := true;
+    ev.eat();
   end;
 end;
 
 
-function TUISimpleText.keyEvent (var ev: THKeyEvent): Boolean;
-begin
-  result := inherited keyEvent(ev);
-end;
-
-
 // ////////////////////////////////////////////////////////////////////////// //
 constructor TUICBListBox.Create (ax, ay: Integer);
 begin
@@ -1747,15 +2043,15 @@ begin
 end;
 
 
-function TUICBListBox.mouseEvent (var ev: THMouseEvent): Boolean;
+procedure TUICBListBox.mouseEvent (var ev: THMouseEvent);
 var
   lx, ly: Integer;
   it: PItem;
 begin
-  result := inherited mouseEvent(ev);
-  if not result and toLocal(ev.x, ev.y, lx, ly) then
+  inherited mouseEvent(ev);
+  if (not ev.eaten) and (not ev.cancelled) and (mEnabled) and toLocal(ev.x, ev.y, lx, ly) then
   begin
-    result := true;
+    ev.eat();
     if (ev = 'lmb') then
     begin
       ly := ly div 8;
@@ -1775,26 +2071,26 @@ begin
 end;
 
 
-function TUICBListBox.keyEvent (var ev: THKeyEvent): Boolean;
+procedure TUICBListBox.keyEvent (var ev: THKeyEvent);
 var
   it: PItem;
 begin
-  result := inherited keyEvent(ev);
-  if not getFocused then exit;
+  inherited keyEvent(ev);
+  if (ev.eaten) or (ev.cancelled) or (not mEnabled) or (not getFocused) then exit;
   //result := true;
   if (ev = 'Home') or (ev = 'PageUp') then
   begin
-    result := true;
+    ev.eat();
     mCurIndex := 0;
   end;
   if (ev = 'End') or (ev = 'PageDown') then
   begin
-    result := true;
+    ev.eat();
     mCurIndex := High(mItems);
   end;
   if (ev = 'Up') then
   begin
-    result := true;
+    ev.eat();
     if (Length(mItems) > 0) then
     begin
       if (mCurIndex < 0) then mCurIndex := Length(mItems);
@@ -1811,7 +2107,7 @@ begin
   end;
   if (ev = 'Down') then
   begin
-    result := true;
+    ev.eat();
     if (Length(mItems) > 0) then
     begin
       if (mCurIndex < 0) then mCurIndex := -1;
@@ -1828,7 +2124,7 @@ begin
   end;
   if (ev = 'Space') or (ev = 'Enter') then
   begin
-    result := true;
+    ev.eat();
     if (mCurIndex >= 0) and (mCurIndex < Length(mItems)) and (mItems[mCurIndex].varp <> nil) then
     begin
       it := @mItems[mCurIndex];
@@ -1845,7 +2141,14 @@ constructor TUIBox.Create (ahoriz: Boolean);
 begin
   inherited Create();
   mHoriz := ahoriz;
+end;
+
+
+procedure TUIBox.AfterConstruction ();
+begin
+  inherited AfterConstruction();
   mCanFocus := false;
+  mCtl4Style := 'box';
 end;
 
 
@@ -1861,7 +2164,7 @@ begin
   end;
   if (strEquCI1251(prname, 'title')) or (strEquCI1251(prname, 'caption')) then
   begin
-    mCaption := par.expectStrOrId(true);
+    mCaption := par.expectIdOrStr(true);
     mDefSize := TLaySize.Create(Length(mCaption)*8+3, 8);
     result := true;
     exit;
@@ -1878,42 +2181,44 @@ end;
 
 procedure TUIBox.drawControl (gx, gy: Integer);
 var
-  r, g, b: Integer;
+  cidx: Integer;
   tx: Integer;
 begin
-  if focused then begin r := 255; g := 255; b := 255; end else begin r := 255; g := 255; b := 0; end;
+  cidx := getColorIndex;
+  fillRect(gx, gy, mWidth, mHeight, mBackColor[cidx]);
   if mHasFrame then
   begin
     // draw frame
-    drawRectUI(gx+3, gy+3, mWidth-6, mHeight-6, TGxRGBA.Create(r, g, b));
+    drawRectUI(gx+3, gy+3, mWidth-6, mHeight-6, mFrameColor[cidx]);
   end;
   // draw caption
   if (Length(mCaption) > 0) then
   begin
     setScissor(mFrameWidth+1, 0, mWidth-mFrameWidth-2, 8);
     tx := gx+((mWidth-Length(mCaption)*8) div 2);
-    if mHasFrame then fillRect(tx-2, gy, Length(mCaption)*8+3, 8, TGxRGBA.Create(0, 0, 128));
-    drawText8(tx, gy, mCaption, TGxRGBA.Create(r, g, b));
+    if mHasFrame then fillRect(tx-2, gy, Length(mCaption)*8+3, 8, mBackColor[cidx]);
+    drawText8(tx, gy, mCaption, mFrameTextColor[cidx]);
   end;
 end;
 
 
-function TUIBox.mouseEvent (var ev: THMouseEvent): Boolean;
+procedure TUIBox.mouseEvent (var ev: THMouseEvent);
 var
   lx, ly: Integer;
 begin
-  result := inherited mouseEvent(ev);
-  if not result and toLocal(ev.x, ev.y, lx, ly) then
+  inherited mouseEvent(ev);
+  if (not ev.eaten) and (not ev.cancelled) and (mEnabled) and toLocal(ev.x, ev.y, lx, ly) then
   begin
-    result := true;
+    ev.eat();
   end;
 end;
 
 
 //TODO: navigation with arrow keys, according to box orientation
-function TUIBox.keyEvent (var ev: THKeyEvent): Boolean;
+procedure TUIBox.keyEvent (var ev: THKeyEvent);
 begin
-  result := inherited keyEvent(ev);
+  inherited keyEvent(ev);
+  if (ev.eaten) or (ev.cancelled) or (not mEnabled) or (not getFocused) then exit;
 end;
 
 
@@ -1930,7 +2235,6 @@ procedure TUIVBox.AfterConstruction ();
 begin
   inherited AfterConstruction();
   mHoriz := false;
-  mCanFocus := false;
 end;
 
 
@@ -1940,6 +2244,7 @@ begin
   inherited AfterConstruction();
   mExpand := true;
   mCanFocus := false;
+  mCtl4Style := 'span';
 end;
 
 
@@ -1956,6 +2261,15 @@ end;
 
 
 // ////////////////////////////////////////////////////////////////////// //
+procedure TUILine.AfterConstruction ();
+begin
+  inherited AfterConstruction();
+  mExpand := true;
+  mCanFocus := false;
+  mCtl4Style := 'line';
+end;
+
+
 function TUILine.parseProperty (const prname: AnsiString; par: TTextParser): Boolean;
 begin
   if (parseOrientation(prname, par)) then begin result := true; exit; end;
@@ -1964,14 +2278,17 @@ end;
 
 
 procedure TUILine.drawControl (gx, gy: Integer);
+var
+  cidx: Integer;
 begin
+  cidx := getColorIndex;
   if mHoriz then
   begin
-    drawHLine(gx, gy+(mHeight div 2), mWidth, TGxRGBA.Create(255, 255, 255));
+    drawHLine(gx, gy+(mHeight div 2), mWidth, mTextColor[cidx]);
   end
   else
   begin
-    drawVLine(gx+(mWidth div 2), gy, mHeight, TGxRGBA.Create(255, 255, 255));
+    drawVLine(gx+(mWidth div 2), gy, mHeight, mTextColor[cidx]);
   end;
 end;
 
@@ -1979,8 +2296,8 @@ end;
 // ////////////////////////////////////////////////////////////////////////// //
 procedure TUIHLine.AfterConstruction ();
 begin
+  inherited AfterConstruction();
   mHoriz := true;
-  mExpand := true;
   mDefSize.h := 1;
 end;
 
@@ -1988,10 +2305,9 @@ end;
 // ////////////////////////////////////////////////////////////////////////// //
 procedure TUIVLine.AfterConstruction ();
 begin
+  inherited AfterConstruction();
   mHoriz := false;
-  mExpand := true;
   mDefSize.w := 1;
-  //mDefSize.h := 8;
 end;
 
 
@@ -2011,6 +2327,7 @@ begin
   mVAlign := 0;
   mCanFocus := false;
   if (mDefSize.h <= 0) then mDefSize.h := 8;
+  mCtl4Style := 'label';
 end;
 
 
@@ -2018,7 +2335,7 @@ function TUITextLabel.parseProperty (const prname: AnsiString; par: TTextParser)
 begin
   if (strEquCI1251(prname, 'title')) or (strEquCI1251(prname, 'caption')) then
   begin
-    mText := par.expectStrOrId(true);
+    mText := par.expectIdOrStr(true);
     mDefSize := TLaySize.Create(Length(mText)*8, 8);
     result := true;
     exit;
@@ -2036,11 +2353,10 @@ end;
 procedure TUITextLabel.drawControl (gx, gy: Integer);
 var
   xpos, ypos: Integer;
+  cidx: Integer;
 begin
-  // debug
-  fillRect(gx, gy, mWidth, mHeight, TGxRGBA.Create(96, 96, 0));
-  drawRectUI(gx, gy, mWidth, mHeight, TGxRGBA.Create(96, 96, 96));
-
+  cidx := getColorIndex;
+  fillRect(gx, gy, mWidth, mHeight, mBackColor[cidx]);
   if (Length(mText) > 0) then
   begin
          if (mHAlign < 0) then xpos := 0
@@ -2051,26 +2367,114 @@ begin
     else if (mVAlign > 0) then ypos := mHeight-8
     else ypos := (mHeight-8) div 2;
 
-    drawText8(gx+xpos, gy+ypos, mText, TGxRGBA.Create(255, 255, 255));
+    drawText8(gx+xpos, gy+ypos, mText, mTextColor[cidx]);
   end;
 end;
 
 
-function TUITextLabel.mouseEvent (var ev: THMouseEvent): Boolean;
+procedure TUITextLabel.mouseEvent (var ev: THMouseEvent);
 var
   lx, ly: Integer;
 begin
-  result := inherited mouseEvent(ev);
-  if not result and toLocal(ev.x, ev.y, lx, ly) then
+  inherited mouseEvent(ev);
+  if (not ev.eaten) and (not ev.cancelled) and (mEnabled) and toLocal(ev.x, ev.y, lx, ly) then
   begin
-    result := true;
+    ev.eat();
+  end;
+end;
+
+
+// ////////////////////////////////////////////////////////////////////////// //
+constructor TUIButton.Create (const atext: AnsiString);
+begin
+  inherited Create(atext);
+end;
+
+
+procedure TUIButton.AfterConstruction ();
+begin
+  inherited AfterConstruction();
+  mHAlign := -1;
+  mVAlign := 0;
+  mCanFocus := true;
+  mDefSize := TLaySize.Create(Length(mText)*8+8, 8);
+  mCtl4Style := 'button';
+end;
+
+
+function TUIButton.parseProperty (const prname: AnsiString; par: TTextParser): Boolean;
+begin
+  result := inherited parseProperty(prname, par);
+  if result then
+  begin
+    mDefSize := TLaySize.Create(Length(mText)*8+8*2, 8);
+  end;
+end;
+
+
+procedure TUIButton.drawControl (gx, gy: Integer);
+var
+  xpos, ypos: Integer;
+  cidx: Integer;
+  lch, rch: AnsiChar;
+begin
+  cidx := getColorIndex;
+  fillRect(gx, gy, mWidth, mHeight, mBackColor[cidx]);
+
+       if (mDefault) then begin lch := '<'; rch := '>'; end
+  else if (mCancel) then begin lch := '{'; rch := '}'; end
+  else begin lch := '['; rch := ']'; end;
+
+       if (mVAlign < 0) then ypos := 0
+  else if (mVAlign > 0) then ypos := mHeight-8
+  else ypos := (mHeight-8) div 2;
+
+  drawText8(gx, gy+ypos, lch, mTextColor[cidx]);
+  drawText8(gx+mWidth-8, gy+ypos, rch, mTextColor[cidx]);
+
+  if (Length(mText) > 0) then
+  begin
+         if (mHAlign < 0) then xpos := 0
+    else if (mHAlign > 0) then xpos := mWidth-Length(mText)*8
+    else xpos := (mWidth-Length(mText)*8) div 2;
+
+    setScissor(8, 0, mWidth-16, mHeight);
+    drawText8(gx+xpos+8, gy+ypos, mText, mTextColor[cidx]);
   end;
 end;
 
 
-function TUITextLabel.keyEvent (var ev: THKeyEvent): Boolean;
+procedure TUIButton.mouseEvent (var ev: THMouseEvent);
+var
+  lx, ly: Integer;
 begin
-  result := inherited keyEvent(ev);
+  inherited mouseEvent(ev);
+  if (uiGrabCtl = self) then
+  begin
+    ev.eat();
+    if (ev = '-lmb') and focused and toLocal(ev.x, ev.y, lx, ly) then
+    begin
+      doAction();
+    end;
+    exit;
+  end;
+  if (ev.eaten) or (ev.cancelled) or (not mEnabled) or not focused then exit;
+  ev.eat();
+end;
+
+
+procedure TUIButton.keyEvent (var ev: THKeyEvent);
+begin
+  inherited keyEvent(ev);
+  if (not ev.eaten) and (not ev.cancelled) and (mEnabled) then
+  begin
+    if (ev = 'Enter') or (ev = 'Space') then
+    begin
+      ev.eat();
+      doAction();
+      exit;
+    end;
+  end;
 end;
 
 
@@ -2081,4 +2485,5 @@ initialization
   registerCtlClass(TUIHLine, 'hline');
   registerCtlClass(TUIVLine, 'vline');
   registerCtlClass(TUITextLabel, 'label');
+  registerCtlClass(TUIButton, 'button');
 end.