DEADSOFTWARE

"hlm_ui_scale" convar; "--holmes_ui_scale" cli arg; (fgsfds request)
[d2df-sdl.git] / src / game / g_holmes_ui.inc
index aaf304db0ada4f121e5b291fbe856203cd7e6f74..d9909a2586a3540147eb716f3128443d75674226 100644 (file)
 // ////////////////////////////////////////////////////////////////////////// //
 type
   THControl = class
+  public
+    type TActionCB = procedure (me: THControl; uinfo: Integer);
+
   private
     mParent: THControl;
     mX, mY: Integer;
     mWidth, mHeight: Integer;
+    mFrameWidth, mFrameHeight: Integer;
     mEnabled: Boolean;
     mCanFocus: Boolean;
     mChildren: array of THControl;
@@ -27,6 +31,11 @@ type
     mGrab: THControl; // valid only for top-level controls
     mEscClose: Boolean; // valid only for top-level controls
     mEatKeys: Boolean;
+    mDrawShadow: Boolean;
+
+  private
+    scallowed: Boolean;
+    scxywh: array[0..3] of GLint;
 
   protected
     function getEnabled (): Boolean;
@@ -46,6 +55,23 @@ type
     procedure activated (); virtual;
     procedure blurred (); virtual;
 
+    //WARNING! do not call scissor functions outside `.draw*()` API!
+    // reset scissor to whole control
+    procedure resetScissor ();
+    // set scissor to this internal rect (in local coords)
+    procedure setScissor (lx, ly, lw, lh: Integer);
+
+    // DO NOT USE!
+    procedure setScissorGLInternal (x, y, w, h: Integer);
+
+  public
+    // return `false` if destination rect is empty
+    // modifies rect0
+    class function intersectRect (var x0, y0, w0, h0: Integer; const x1, y1, w1, h1: Integer): Boolean;
+
+  public
+    actionCB: TActionCB;
+
   public
     constructor Create (ax, ay, aw, ah: Integer; aparent: THControl=nil);
     destructor Destroy (); override;
@@ -75,6 +101,8 @@ type
     function firstChild (): THControl; inline;
     function lastChild (): THControl; inline;
 
+    procedure appendChild (ctl: THControl); virtual;
+
   public
     property x0: Integer read mX;
     property y0: Integer read mY;
@@ -93,34 +121,72 @@ type
     mTitle: AnsiString;
     mDragging: Boolean;
     mDragStartX, mDragStartY: Integer;
+    mWaitingClose: Boolean;
+    mInClose: Boolean;
 
   protected
     procedure blurred (); override;
 
+  public
+    closeCB: TActionCB; // called after window was removed from ui window list
+
   public
     constructor Create (const atitle: AnsiString; ax, ay: Integer; aw: Integer=-1; ah: Integer=-1);
 
-    procedure appendChild (ctl: THControl);
+    procedure centerInScreen ();
 
     // `sx` and `sy` are screen coordinates
     procedure drawControl (sx, sy: Integer); override;
     procedure drawControlPost (sx, sy: 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
   end;
 
 
+  THCtlSimpleText = class(THControl)
+  private
+    type
+      PItem = ^TItem;
+      TItem = record
+        title: AnsiString;
+        centered: Boolean;
+        hline: Boolean;
+      end;
+  private
+    mItems: array of TItem;
+
+  public
+    constructor Create (ax, ay: Integer; aparent: THControl=nil);
+    destructor Destroy (); override;
+
+    procedure appendItem (const atext: AnsiString; acentered: Boolean=false; ahline: Boolean=false);
+
+    procedure drawControl (sx, sy: Integer); override;
+
+    function mouseEvent (var ev: THMouseEvent): Boolean; override;
+    function keyEvent (var ev: THKeyEvent): Boolean; override;
+  end;
+
+
   THCtlCBListBox = class(THControl)
   private
-    mItems: array of AnsiString;
-    mChecks: array of PBoolean;
+    type
+      PItem = ^TItem;
+      TItem = record
+        title: AnsiString;
+        varp: PBoolean;
+        actionCB: TActionCB;
+      end;
+  private
+    mItems: array of TItem;
     mCurIndex: Integer;
 
   public
     constructor Create (ax, ay: Integer; aparent: THControl=nil);
     destructor Destroy (); override;
 
-    procedure appendItem (const atext: AnsiString; bv: PBoolean);
+    procedure appendItem (const atext: AnsiString; bv: PBoolean; aaction: TActionCB=nil);
 
     procedure drawControl (sx, sy: Integer); override;
 
@@ -128,6 +194,7 @@ type
     function keyEvent (var ev: THKeyEvent): Boolean; override;
   end;
 
+
 // ////////////////////////////////////////////////////////////////////////// //
 var
   uiTopList: array of THControl = nil;
@@ -228,6 +295,10 @@ begin
       ctl.blurred();
       for c := f+1 to High(uiTopList) do uiTopList[c-1] := uiTopList[c];
       SetLength(uiTopList, Length(uiTopList)-1);
+      if (ctl is THTopWindow) then
+      begin
+        if assigned(THTopWindow(ctl).closeCB) then THTopWindow(ctl).closeCB(ctl, 0);
+      end;
       exit;
     end;
   end;
@@ -256,6 +327,8 @@ begin
   mY := ay;
   mWidth := aw;
   mHeight := ah;
+  mFrameWidth := 0;
+  mFrameHeight := 0;
   mEnabled := true;
   mCanFocus := true;
   mChildren := nil;
@@ -263,6 +336,9 @@ begin
   mGrab := nil;
   mEscClose := false;
   mEatKeys := false;
+  scallowed := false;
+  mDrawShadow := false;
+  actionCB := nil;
 end;
 
 
@@ -543,36 +619,90 @@ begin
 end;
 
 
+procedure THControl.appendChild (ctl: THControl);
+begin
+  if (ctl = nil) then exit;
+  if (ctl.mParent <> nil) then exit;
+  SetLength(mChildren, Length(mChildren)+1);
+  mChildren[High(mChildren)] := ctl;
+  ctl.mParent := self;
+  Inc(ctl.mX, mFrameWidth);
+  Inc(ctl.mY, mFrameHeight);
+  if (ctl.mWidth > 0) and (ctl.mHeight > 0) and
+     (ctl.mX+ctl.mWidth > mFrameWidth) and (ctl.mY+ctl.mHeight > mFrameHeight) then
+  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;
+
+
+//TODO: overflow checks
+class function THControl.intersectRect (var x0, y0, w0, h0: Integer; const x1, y1, w1, h1: Integer): Boolean;
+var
+  ex0, ey0: Integer;
+begin
+  result := false;
+  if (w0 < 1) or (h0 < 1) or (w1 < 1) or (h1 < 1) then exit;
+  // check for intersection
+  if (x0+w0 <= x1) or (y0+h0 <= y1) or (x1+w1 <= x0) or (y1+h1 <= y0) then exit;
+  if (x0 >= x1+w1) or (y0 >= y1+h1) or (x1 >= x0+h0) or (y1 >= y0+h0) then exit;
+  // ok, intersects
+  ex0 := x0+w0;
+  ey0 := y0+h0;
+  if (x0 < x1) then x0 := x1;
+  if (y0 < y1) then y0 := y1;
+  if (ex0 > x1+w1) then ex0 := x1+w1;
+  if (ey0 > y1+h1) then ey0 := y1+h1;
+  w0 := ex0-x0;
+  h0 := ey0-y0;
+  result := (w0 > 0) and (h0 > 0);
+end;
+
+
+procedure THControl.setScissorGLInternal (x, y, w, h: Integer);
+begin
+  if not scallowed then exit;
+  x := trunc(x*g_holmes_ui_scale);
+  y := trunc(y*g_holmes_ui_scale);
+  w := trunc(w*g_holmes_ui_scale);
+  h := trunc(h*g_holmes_ui_scale);
+  y := gWinSizeY-(y+h);
+  if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then glScissor(0, 0, 0, 0) else glScissor(x, y, w, h);
+end;
+
+
+procedure THControl.resetScissor ();
+var
+  x, y: Integer;
+begin
+  if not scallowed then exit;
+  x := 0;
+  y := 0;
+  toGlobal(x, y);
+  setScissorGLInternal(x, y, mWidth, mHeight);
+end;
+
+
+procedure THControl.setScissor (lx, ly, lw, lh: Integer);
+var
+  x, y: Integer;
+begin
+  if not scallowed then exit;
+  if not intersectRect(lx, ly, lw, lh, 0, 0, mWidth, mHeight) then begin glScissor(0, 0, 0, 0); exit; end;
+  x := lx;
+  y := ly;
+  toGlobal(x, y);
+  setScissorGLInternal(x, y, lw, lh);
+end;
+
+
 procedure THControl.draw ();
 var
   f: Integer;
   x, y: Integer;
-  scxywh: array[0..3] of GLint;
   wassc: Boolean;
-
-  procedure setScissor (x, y, w, h: Integer);
-  var
-    x1, y1: Integer;
-    sx0, sy0: Integer;
-    sx1, sy1: Integer;
-  begin
-    if (w < 1) or (h < 1) or (scxywh[2] < 1) or (scxywh[3] < 1) then begin glScissor(0, 0, 0, 0); exit; end;
-    x1 := x+w-1;
-    y1 := y+h-1;
-    sx0 := scxywh[0];
-    sy0 := scxywh[1];
-    sx1 := sx0+scxywh[2]-1;
-    sy1 := sy0+scxywh[3]-1;
-    //conwritefln('0: (%d,%d)-(%d,%d)  (%d,%d)-(%d,%d)', [sx0, sy0, sx1, sy1, x, y, x1, y1]);
-    if (x1 < sx0) or (y1 < sy0) or (x > sx1) or (y > sy1) then begin glScissor(0, 0, 0, 0); exit; end;
-    if (x < sx0) then x := sx0;
-    if (y < sy0) then y := sy0;
-    if (x1 > sx1) then x1 := sx1;
-    if (y1 > sy1) then y1 := sy1;
-    //conwritefln('1: (%d,%d)-(%d,%d)  (%d,%d)-(%d,%d)', [sx0, sy0, sx1, sy1, x, y, x1, y1]);
-    glScissor(x, y, x1-x+1, y1-y+1);
-  end;
-
 begin
   if (mWidth < 1) or (mHeight < 1) then exit;
   x := 0;
@@ -586,38 +716,38 @@ begin
   scxywh[3] := 0;
 
   wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
-  if wassc then
-  begin
-    glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]);
-  end
-  else
-  begin
-    glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
-  end;
+  if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
   //conwritefln('(%d,%d)-(%d,%d)', [scxywh[0], scxywh[1], scxywh[2], scxywh[3]]);
-
   glEnable(GL_SCISSOR_TEST);
-  setScissor(x, gWinSizeY-(y+mHeight-1)-1, mWidth, mHeight);
+  scallowed := true;
 
+  resetScissor();
   drawControl(x, y);
-  if (mParent = nil) then setScissor(x+2, gWinSizeY-(y+mHeight-1-2)-1, mWidth-4, mHeight-14);
+  if (mFrameWidth <> 0) or (mFrameHeight <> 0) then setScissor(mFrameWidth, mFrameHeight, mWidth-mFrameWidth*2, mHeight-mFrameHeight*2);
   for f := 0 to High(mChildren) do mChildren[f].draw();
-  if (mParent = nil) then setScissor(x, gWinSizeY-(y+mHeight-1)-1, mWidth, mHeight);
+  if (mFrameWidth <> 0) or (mFrameHeight <> 0) then resetScissor();
   drawControlPost(x, y);
   glScissor(scxywh[0], scxywh[1], scxywh[2], scxywh[3]);
+
   if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
+  scallowed := false;
 end;
 
 
 procedure THControl.drawControl (sx, sy: Integer);
 begin
   if (mParent = nil) then darkenRect(sx, sy, mWidth, mHeight, 64);
-  //fillRect(sx, sy, mWidth, mHeight, 0, 0, 255, 120);
 end;
 
 
 procedure THControl.drawControlPost (sx, sy: Integer);
 begin
+  if mDrawShadow and (mWidth > 0) and (mHeight > 0) then
+  begin
+    setScissorGLInternal(sx+8, sy+8, mWidth, mHeight);
+    darkenRect(sx+mWidth, sy+8, 8, mHeight, 128);
+    darkenRect(sx+8, sy+mHeight, mWidth-8, 8, 128);
+  end;
 end;
 
 
@@ -642,6 +772,10 @@ begin
   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);
   end;
 end;
 
@@ -655,7 +789,7 @@ begin
   if (topLevel.mFocused <> self) and isMyChild(topLevel.mFocused) and topLevel.mFocused.mEnabled then result := topLevel.mFocused.keyEvent(ev);
   if (mParent = nil) then
   begin
-    if (ev.kstate = THKeyEvent.ModShift) and (ev.scan = SDL_SCANCODE_TAB) then
+    if (ev.kind = ev.Press) and (ev = 'S-Tab') then
     begin
       result := true;
       if (ev.kind = ev.Press) then
@@ -669,7 +803,7 @@ begin
       end;
       exit;
     end;
-    if (ev.kstate = 0) and (ev.scan = SDL_SCANCODE_TAB) then
+    if (ev.kind = ev.Press) and (ev = 'Tab') then
     begin
       result := true;
       if (ev.kind = ev.Press) then
@@ -683,7 +817,7 @@ begin
       end;
       exit;
     end;
-    if mEscClose and (ev.kind = ev.Press) and (ev.kstate = 0) and (ev.scan = SDL_SCANCODE_ESCAPE) then
+    if mEscClose and (ev.kind = ev.Press) and (ev = 'Escape') then
     begin
       result := true;
       uiRemoveWindow(self);
@@ -697,88 +831,94 @@ end;
 // ////////////////////////////////////////////////////////////////////////// //
 constructor THTopWindow.Create (const atitle: AnsiString; ax, ay: Integer; aw: Integer=-1; ah: Integer=-1);
 begin
+  inherited Create(ax, ay, aw, ah, nil);
+  mFrameWidth := 8;
+  mFrameHeight := 8;
   mTitle := atitle;
+  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
   begin
-    if (ah < 14) then ah := 14;
-    if (aw < Length(mTitle)*8+4) then aw := Length(mTitle)*8+4;
-  end
-  else
-  begin
-    if (ah < 4) then ah := 4;
-    if (aw < 4) then aw := 4;
+    if (mWidth < Length(mTitle)*8+mFrameWidth*2+3*8) then mWidth := Length(mTitle)*8+mFrameWidth*2+3*8;
   end;
   mDragging := false;
-  inherited Create(ax, ay, aw, ah, nil);
+  mDrawShadow := true;
+  mWaitingClose := false;
+  mInClose := false;
+  closeCB := nil;
 end;
 
 
-procedure THTopWindow.appendChild (ctl: THControl);
-var
-  myofs: Integer;
+procedure THTopWindow.centerInScreen ();
 begin
-  if (ctl = nil) then exit;
-  if (ctl.mParent <> nil) then exit;
-  if (Length(mTitle) > 0) then myofs := 12 else myofs := 2;
-  SetLength(mChildren, Length(mChildren)+1);
-  mChildren[High(mChildren)] := ctl;
-  ctl.mParent := self;
-  Inc(ctl.mX, 2);
-  Inc(ctl.mY, myofs);
-  if (ctl.mWidth > 0) and (ctl.mHeight > 0) and
-     (ctl.mX+ctl.mWidth > 2) and (ctl.mY+ctl.mHeight > myofs-2) then
+  if (mWidth > 0) and (mHeight > 0) then
   begin
-    if (mWidth+2 < ctl.mX+ctl.mWidth) then mWidth := ctl.mX+ctl.mWidth+2;
-    if (mHeight+2 < ctl.mY+ctl.mHeight) then mHeight := ctl.mY+ctl.mHeight+2;
+    mX := trunc((gWinSizeX/g_holmes_ui_scale-mWidth)/2);
+    mY := trunc((gWinSizeY/g_holmes_ui_scale-mHeight)/2);
   end;
-  if (mFocused = nil) and ctl.mEnabled and ctl.mCanFocus and (ctl.mWidth > 0) and (ctl.mHeight > 0) then mFocused := ctl;
 end;
 
 
 procedure THTopWindow.drawControl (sx, sy: Integer);
 begin
-  //if (mParent = nil) then darkenRect(sx, sy, mWidth, mHeight, 200);
   fillRect(sx, sy, mWidth, mHeight, 0, 0, 128);
 end;
 
 
 procedure THTopWindow.drawControlPost (sx, sy: Integer);
+const r = 255;
+const g = 255;
+const b = 255;
 var
-  r, g, b: Integer;
+  tx: Integer;
 begin
-  {
-  if getFocused then
+  if mDragging then
   begin
-    r := 255;
-    g := 255;
-    b := 255;
+    drawRect(mX+4, mY+4, mWidth-8, mHeight-8, r, g, b);
   end
   else
   begin
-    r := 127;
-    g := 127;
-    b := 127;
+    drawRect(mX+3, mY+3, mWidth-6, mHeight-6, r, g, b);
+    drawRect(mX+5, mY+5, mWidth-10, mHeight-10, r, g, b);
+    setScissor(mFrameWidth, 0, 3*8, 8);
+    fillRect(mX+mFrameWidth, mY, 3*8, 8, 0, 0, 128);
+    drawText8(mX+mFrameWidth, mY, '[ ]', r, g, b);
+    if mInClose then drawText8(mX+mFrameWidth+7, mY, '#', 0, 255, 0)
+    else drawText8(mX+mFrameWidth+7, mY, '*', 0, 255, 0);
   end;
-  }
-  r := 255;
-  g := 255;
-  b := 255;
-  drawRect(mX, mY, mWidth, mHeight, r, g, b);
   if (Length(mTitle) > 0) then
   begin
-    fillRect(mX+1, mY+1, mWidth-2, 9, r, g, b);
-    drawText8(mX+2, mY+1, mTitle, 0, 0, 0);
+    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, 0, 0, 128);
+    drawText8(tx, mY, mTitle, r, g, b);
   end;
+  inherited drawControlPost(sx, sy);
 end;
 
 
 procedure THTopWindow.blurred ();
 begin
   mDragging := false;
+  mWaitingClose := false;
+  mInClose := false;
   inherited;
 end;
 
 
+function THTopWindow.keyEvent (var ev: THKeyEvent): Boolean;
+begin
+  result := inherited keyEvent(ev);
+  if not getFocused then exit;
+  if (ev.kind = ev.Press) and (ev = 'M-F3') then
+  begin
+    uiRemoveWindow(self);
+    result := true;
+    exit;
+  end;
+end;
+
+
 function THTopWindow.mouseEvent (var ev: THMouseEvent): Boolean;
 var
   lx, ly: Integer;
@@ -798,13 +938,30 @@ begin
     exit;
   end;
 
-  if (ev.kind = ev.Press) and (ev.but = ev.Left) then
+  lx := ev.x;
+  ly := ev.y;
+  if toLocal(lx, ly) then
   begin
-    lx := ev.x;
-    ly := ev.y;
-    if toLocal(lx, ly) then
+    if (ev.kind = ev.Press) then
     begin
-      if ((Length(mTitle) > 0) and (ly < 12)) or ((Length(mTitle) = 0) and (ly < 2)) then
+      if (ly < 8) then
+      begin
+        if (lx >= mFrameWidth) and (lx < mFrameWidth+3*8) then
+        begin
+          //uiRemoveWindow(self);
+          mWaitingClose := true;
+          mInClose := true;
+        end
+        else
+        begin
+          mDragging := true;
+          mDragStartX := ev.x;
+          mDragStartY := ev.y;
+        end;
+        result := true;
+        exit;
+      end;
+      if (lx < mFrameWidth) or (lx >= mWidth-mFrameWidth) or (ly >= mHeight-mFrameHeight) then
       begin
         mDragging := true;
         mDragStartX := ev.x;
@@ -813,9 +970,118 @@ begin
         exit;
       end;
     end;
+
+    if (ev.kind = ev.Release) then
+    begin
+      if mWaitingClose and (lx >= mFrameWidth) and (lx < mFrameWidth+3*8) then
+      begin
+        uiRemoveWindow(self);
+        result := true;
+        exit;
+      end;
+      mWaitingClose := false;
+      mInClose := false;
+    end;
+
+    if (ev.kind = ev.Motion) then
+    begin
+      if mWaitingClose then
+      begin
+        mInClose := (lx >= mFrameWidth) and (lx < mFrameWidth+3*8);
+        result := true;
+        exit;
+      end;
+    end;
+  end
+  else
+  begin
+    mInClose := false;
+    if (ev.kind <> ev.Motion) then mWaitingClose := false;
+  end;
+
+  result := inherited mouseEvent(ev);
+end;
+
+
+// ////////////////////////////////////////////////////////////////////////// //
+constructor THCtlSimpleText.Create (ax, ay: Integer; aparent: THControl=nil);
+begin
+  mItems := nil;
+  inherited Create(ax, ay, 4, 4);
+end;
+
+
+destructor THCtlSimpleText.Destroy ();
+begin
+  mItems := nil;
+  inherited;
+end;
+
+
+procedure THCtlSimpleText.appendItem (const atext: AnsiString; acentered: Boolean=false; ahline: Boolean=false);
+var
+  it: PItem;
+begin
+  if (Length(atext)*8+3*8+2 > mWidth) then mWidth := Length(atext)*8+3*8+2;
+  SetLength(mItems, Length(mItems)+1);
+  it := @mItems[High(mItems)];
+  it.title := atext;
+  it.centered := acentered;
+  it.hline := ahline;
+  if (Length(mItems)*8 > mHeight) then mHeight := Length(mItems)*8;
+end;
+
+
+procedure THCtlSimpleText.drawControl (sx, sy: Integer);
+var
+  f, tx: Integer;
+  it: PItem;
+  r, g, b: Integer;
+begin
+  for f := 0 to High(mItems) do
+  begin
+    it := @mItems[f];
+    tx := sx;
+    r := 255;
+    g := 255;
+    b := 0;
+    if it.centered then begin b := 255; tx := sx+(mWidth-Length(it.title)*8) div 2; end;
+    if it.hline then
+    begin
+      b := 255;
+      if (Length(it.title) = 0) then
+      begin
+        drawLine(sx+4, sy+3, sx+mWidth-8, sy+3, r, g, b);
+      end
+      else if (tx-3 > sx+4) then
+      begin
+        drawLine(sx+4, sy+3, tx-3, sy+3, r, g, b);
+        drawLine(tx+Length(it.title)*8, sy+3, sx+mWidth-4, sy+3, r, g, b);
+      end;
+    end;
+    drawText8(tx, sy, it.title, r, g, b);
+    Inc(sy, 8);
   end;
+end;
+
 
+function THCtlSimpleText.mouseEvent (var ev: THMouseEvent): Boolean;
+var
+  lx, ly: Integer;
+begin
   result := inherited mouseEvent(ev);
+  lx := ev.x;
+  ly := ev.y;
+  if not result and toLocal(lx, ly) then
+  begin
+    result := true;
+  end;
+end;
+
+
+function THCtlSimpleText.keyEvent (var ev: THKeyEvent): Boolean;
+begin
+  result := inherited keyEvent(ev);
 end;
 
 
@@ -823,7 +1089,6 @@ end;
 constructor THCtlCBListBox.Create (ax, ay: Integer; aparent: THControl=nil);
 begin
   mItems := nil;
-  mChecks := nil;
   mCurIndex := -1;
   inherited Create(ax, ay, 4, 4);
 end;
@@ -832,38 +1097,52 @@ end;
 destructor THCtlCBListBox.Destroy ();
 begin
   mItems := nil;
-  mChecks := nil;
   inherited;
 end;
 
 
-procedure THCtlCBListBox.appendItem (const atext: AnsiString; bv: PBoolean);
+procedure THCtlCBListBox.appendItem (const atext: AnsiString; bv: PBoolean; aaction: TActionCB=nil);
+var
+  it: PItem;
 begin
-  if (Length(atext)*8+4+3*8+2 > mWidth) then mWidth := Length(atext)*8+4+3*8+2;
+  if (Length(atext)*8+3*8+2 > mWidth) then mWidth := Length(atext)*8+3*8+2;
   SetLength(mItems, Length(mItems)+1);
-  mItems[High(mItems)] := atext;
-  SetLength(mChecks, Length(mChecks)+1);
-  mChecks[High(mChecks)] := bv;
-  if (Length(mItems)*8+4 > mHeight) then mHeight := Length(mItems)*8+4;
+  it := @mItems[High(mItems)];
+  it.title := atext;
+  it.varp := bv;
+  it.actionCB := aaction;
+  if (Length(mItems)*8 > mHeight) then mHeight := Length(mItems)*8;
   if (mCurIndex < 0) then mCurIndex := 0;
 end;
 
 
 procedure THCtlCBListBox.drawControl (sx, sy: Integer);
 var
-  f: Integer;
+  f, tx: Integer;
+  it: PItem;
 begin
-  //fillRect(sx, sy, mWidth, mHeight, 0, 128, 0);
-  Inc(sx, 2);
-  Inc(sy, 2);
   for f := 0 to High(mItems) do
   begin
-    if (mCurIndex = f) then fillRect(sx-2, sy, mWidth, 8, 0, 128, 0);
-    if (mChecks[f] <> nil) then
+    it := @mItems[f];
+    if (mCurIndex = f) then fillRect(sx, sy, mWidth, 8, 0, 128, 0);
+    if (it.varp <> nil) then
+    begin
+      if it.varp^ then drawText8(sx, sy, '[x]', 255, 255, 255) else drawText8(sx, sy, '[ ]', 255, 255, 255);
+      drawText8(sx+3*8+2, sy, it.title, 255, 255, 0);
+    end
+    else if (Length(it.title) > 0) then
+    begin
+      tx := sx+(mWidth-Length(it.title)*8) div 2;
+      if (tx-3 > sx+4) then
+      begin
+        drawLine(sx+4, sy+3, tx-3, sy+3, 255, 255, 255);
+        drawLine(tx+Length(it.title)*8, sy+3, sx+mWidth-4, sy+3, 255, 255, 255);
+      end;
+      drawText8(tx, sy, it.title, 255, 255, 255);
+    end
+    else
     begin
-      drawText8(sx, sy, '[ ]', 255, 255, 255);
-      if mChecks[f]^ then drawText8(sx+6, sy, 'x', 255, 255, 255);
-      drawText8(sx+3*8+2, sy, mItems[f], 255, 255, 0);
+      drawLine(sx+4, sy+3, sx+mWidth-8, sy+3, 255, 255, 255);
     end;
     Inc(sy, 8);
   end;
@@ -873,84 +1152,96 @@ end;
 function THCtlCBListBox.mouseEvent (var ev: THMouseEvent): Boolean;
 var
   lx, ly: Integer;
+  it: PItem;
 begin
   result := inherited mouseEvent(ev);
-  if not result and (Length(mItems) > 0) and (ev.kind = ev.Press) then
+  lx := ev.x;
+  ly := ev.y;
+  if not result and toLocal(lx, ly) then
   begin
-    lx := ev.x;
-    ly := ev.y;
-    if toLocal(lx, ly) then
+    result := true;
+    if (ev.kind = ev.Press) and (ev = 'lmb') then
     begin
-      if (ly < 2) then ly := 2;
       ly := ly div 8;
-      if (ly < 0) then ly := 0 else if (ly > High(mItems)) then ly := High(mItems);
-      mCurIndex := ly;
-      if (mChecks[ly] <> nil) then mChecks[ly]^ := not mChecks[ly]^;
+      if (ly >= 0) and (ly < Length(mItems)) then
+      begin
+        it := @mItems[ly];
+        if (it.varp <> nil) then
+        begin
+          mCurIndex := ly;
+          it.varp^ := not it.varp^;
+          if assigned(it.actionCB) then it.actionCB(self, Integer(it.varp^));
+          if assigned(actionCB) then actionCB(self, ly);
+        end;
+      end;
     end;
   end;
 end;
 
 
 function THCtlCBListBox.keyEvent (var ev: THKeyEvent): Boolean;
+var
+  it: PItem;
 begin
   result := inherited keyEvent(ev);
   if not getFocused then exit;
   //result := true;
-  if (ev.kstate = 0) and (ev.kind = ev.Press) then
+  if (ev.kind = ev.Press) then
   begin
-    case ev.scan of
-      SDL_SCANCODE_HOME,
-      SDL_SCANCODE_PAGEUP:
-        begin
-          result := true;
-          mCurIndex := 0;
-        end;
-      SDL_SCANCODE_END,
-      SDL_SCANCODE_PAGEDOWN:
-        begin
-          result := true;
-          mCurIndex := High(mItems);
-        end;
-      SDL_SCANCODE_UP:
-        begin
-          result := true;
-          if (Length(mItems) > 0) then
-          begin
-            if (mCurIndex < 0) then mCurIndex := Length(mItems);
-            while (mCurIndex > 0) do
-            begin
-              Dec(mCurIndex);
-              if (mChecks[mCurIndex] <> nil) then break;
-            end;
-          end
-          else
-          begin
-            mCurIndex := -1;
-          end;
-        end;
-      SDL_SCANCODE_DOWN:
+    if (ev = 'Home') or (ev = 'PageUp') then
+    begin
+      result := true;
+      mCurIndex := 0;
+    end;
+    if (ev = 'End') or (ev = 'PageDown') then
+    begin
+      result := true;
+      mCurIndex := High(mItems);
+    end;
+    if (ev = 'Up') then
+    begin
+      result := true;
+      if (Length(mItems) > 0) then
+      begin
+        if (mCurIndex < 0) then mCurIndex := Length(mItems);
+        while (mCurIndex > 0) do
         begin
-          result := true;
-          if (Length(mItems) > 0) then
-          begin
-            if (mCurIndex < 0) then mCurIndex := -1;
-            while (mCurIndex < High(mItems)) do
-            begin
-              Inc(mCurIndex);
-              if (mChecks[mCurIndex] <> nil) then break;
-            end;
-          end
-          else
-          begin
-            mCurIndex := -1;
-          end;
+          Dec(mCurIndex);
+          if (mItems[mCurIndex].varp <> nil) then break;
         end;
-      SDL_SCANCODE_SPACE,
-      SDL_SCANCODE_RETURN:
+      end
+      else
+      begin
+        mCurIndex := -1;
+      end;
+    end;
+    if (ev = 'Down') then
+    begin
+      result := true;
+      if (Length(mItems) > 0) then
+      begin
+        if (mCurIndex < 0) then mCurIndex := -1;
+        while (mCurIndex < High(mItems)) do
         begin
-          result := true;
-          if (mCurIndex >= 0) and (mCurIndex < Length(mChecks)) and (mChecks[mCurIndex] <> nil) then mChecks[mCurIndex]^ := not mChecks[mCurIndex]^;
+          Inc(mCurIndex);
+          if (mItems[mCurIndex].varp <> nil) then break;
         end;
+      end
+      else
+      begin
+        mCurIndex := -1;
+      end;
+    end;
+    if (ev = 'Space') or (ev = 'Return') then
+    begin
+      result := true;
+      if (mCurIndex >= 0) and (mCurIndex < Length(mItems)) and (mItems[mCurIndex].varp <> nil) then
+      begin
+        it := @mItems[mCurIndex];
+        it.varp^ := not it.varp^;
+        if assigned(it.actionCB) then it.actionCB(self, Integer(it.varp^));
+        if assigned(actionCB) then actionCB(self, mCurIndex);
+      end;
     end;
   end;
 end;