1 (* Copyright (C) DooM 2D:Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ////////////////////////////////////////////////////////////////////////// //
20 type TActionCB = procedure (me: THControl; uinfo: Integer);
25 mWidth, mHeight: Integer;
26 mFrameWidth, mFrameHeight: Integer;
29 mChildren: array of THControl;
30 mFocused: THControl; // valid only for top-level controls
31 mGrab: THControl; // valid only for top-level controls
32 mEscClose: Boolean; // valid only for top-level controls
38 scxywh: array[0..3] of GLint;
41 function getEnabled (): Boolean;
42 procedure setEnabled (v: Boolean); inline;
44 function getFocused (): Boolean; inline;
45 procedure setFocused (v: Boolean); inline;
47 function isMyChild (ctl: THControl): Boolean;
49 function findFirstFocus (): THControl;
50 function findLastFocus (): THControl;
52 function findNextFocus (cur: THControl): THControl;
53 function findPrevFocus (cur: THControl): THControl;
55 procedure activated (); virtual;
56 procedure blurred (); virtual;
58 //WARNING! do not call scissor functions outside `.draw*()` API!
59 // reset scissor to whole control
60 procedure resetScissor ();
61 // set scissor to this internal rect (in local coords)
62 procedure setScissor (lx, ly, lw, lh: Integer);
65 procedure setScissorGLInternal (x, y, w, h: Integer);
68 // return `false` if destination rect is empty
70 class function intersectRect (var x0, y0, w0, h0: Integer; const x1, y1, w1, h1: Integer): Boolean;
76 constructor Create (ax, ay, aw, ah: Integer; aparent: THControl=nil);
77 destructor Destroy (); override;
79 // `sx` and `sy` are screen coordinates
80 procedure drawControl (sx, sy: Integer); virtual;
82 // called after all children drawn
83 procedure drawControlPost (sx, sy: Integer); virtual;
85 procedure draw (); virtual;
87 function topLevel (): THControl; inline;
89 // returns `true` if global coords are inside this control
90 function toLocal (var x, y: Integer): Boolean;
91 procedure toGlobal (var x, y: Integer);
93 // x and y are global coords
94 function controlAtXY (x, y: Integer): THControl;
96 function mouseEvent (var ev: THMouseEvent): Boolean; virtual; // returns `true` if event was eaten
97 function keyEvent (var ev: THKeyEvent): Boolean; virtual; // returns `true` if event was eaten
99 function prevSibling (): THControl;
100 function nextSibling (): THControl;
101 function firstChild (): THControl; inline;
102 function lastChild (): THControl; inline;
104 procedure appendChild (ctl: THControl); virtual;
107 property x0: Integer read mX;
108 property y0: Integer read mY;
109 property height: Integer read mHeight;
110 property width: Integer read mWidth;
111 property enabled: Boolean read getEnabled write setEnabled;
112 property parent: THControl read mParent;
113 property focused: Boolean read getFocused write setFocused;
114 property escClose: Boolean read mEscClose write mEscClose;
115 property eatKeys: Boolean read mEatKeys write mEatKeys;
119 THTopWindow = class(THControl)
123 mDragStartX, mDragStartY: Integer;
124 mWaitingClose: Boolean;
128 procedure blurred (); override;
131 closeCB: TActionCB; // called after window was removed from ui window list
134 constructor Create (const atitle: AnsiString; ax, ay: Integer; aw: Integer=-1; ah: Integer=-1);
136 procedure centerInScreen ();
138 // `sx` and `sy` are screen coordinates
139 procedure drawControl (sx, sy: Integer); override;
140 procedure drawControlPost (sx, sy: Integer); override;
142 function keyEvent (var ev: THKeyEvent): Boolean; override; // returns `true` if event was eaten
143 function mouseEvent (var ev: THMouseEvent): Boolean; override; // returns `true` if event was eaten
147 THCtlSimpleText = class(THControl)
157 mItems: array of TItem;
160 constructor Create (ax, ay: Integer; aparent: THControl=nil);
161 destructor Destroy (); override;
163 procedure appendItem (const atext: AnsiString; acentered: Boolean=false; ahline: Boolean=false);
165 procedure drawControl (sx, sy: Integer); override;
167 function mouseEvent (var ev: THMouseEvent): Boolean; override;
168 function keyEvent (var ev: THKeyEvent): Boolean; override;
172 THCtlCBListBox = class(THControl)
182 mItems: array of TItem;
186 constructor Create (ax, ay: Integer; aparent: THControl=nil);
187 destructor Destroy (); override;
189 procedure appendItem (const atext: AnsiString; bv: PBoolean; aaction: TActionCB=nil);
191 procedure drawControl (sx, sy: Integer); override;
193 function mouseEvent (var ev: THMouseEvent): Boolean; override;
194 function keyEvent (var ev: THKeyEvent): Boolean; override;
198 // ////////////////////////////////////////////////////////////////////////// //
200 uiTopList: array of THControl = nil;
203 function uiMouseEvent (var ev: THMouseEvent): Boolean;
209 if (Length(uiTopList) = 0) then result := false else result := uiTopList[High(uiTopList)].mouseEvent(ev);
210 if not result and (ev.kind = ev.Press) then
212 for f := High(uiTopList) downto 0 do
216 if uiTopList[f].toLocal(lx, ly) then
219 if uiTopList[f].mEnabled and (f <> High(uiTopList)) then
221 uiTopList[High(uiTopList)].blurred();
222 ctmp := uiTopList[f];
224 for c := f+1 to High(uiTopList) do uiTopList[c-1] := uiTopList[c];
225 uiTopList[High(uiTopList)] := ctmp;
227 result := ctmp.mouseEvent(ev);
236 function uiKeyEvent (var ev: THKeyEvent): Boolean;
238 if (Length(uiTopList) = 0) then result := false else result := uiTopList[High(uiTopList)].keyEvent(ev);
239 if (ev.kind = ev.Release) then begin result := true; exit; end;
248 for f := 0 to High(uiTopList) do
252 if (f <> High(uiTopList)) then darkenRect(ctl.x0, ctl.y0, ctl.width, ctl.height, 128);
257 procedure uiAddWindow (ctl: THControl);
261 if (ctl = nil) then exit;
263 for f := 0 to High(uiTopList) do
265 if (uiTopList[f] = ctl) then
267 if (f <> High(uiTopList)) then
269 uiTopList[High(uiTopList)].blurred();
270 for c := f+1 to High(uiTopList) do uiTopList[c-1] := uiTopList[c];
271 uiTopList[High(uiTopList)] := ctl;
277 if (Length(uiTopList) > 0) then uiTopList[High(uiTopList)].blurred();
278 SetLength(uiTopList, Length(uiTopList)+1);
279 uiTopList[High(uiTopList)] := ctl;
285 procedure uiRemoveWindow (ctl: THControl);
289 if (ctl = nil) then exit;
291 for f := 0 to High(uiTopList) do
293 if (uiTopList[f] = ctl) then
296 for c := f+1 to High(uiTopList) do uiTopList[c-1] := uiTopList[c];
297 SetLength(uiTopList, Length(uiTopList)-1);
298 if (ctl is THTopWindow) then
300 if assigned(THTopWindow(ctl).closeCB) then THTopWindow(ctl).closeCB(ctl, 0);
308 function uiVisibleWindow (ctl: THControl): Boolean;
313 if (ctl = nil) then exit;
315 for f := 0 to High(uiTopList) do
317 if (uiTopList[f] = ctl) then begin result := true; exit; end;
322 // ////////////////////////////////////////////////////////////////////////// //
323 constructor THControl.Create (ax, ay, aw, ah: Integer; aparent: THControl=nil);
340 mDrawShadow := false;
345 destructor THControl.Destroy ();
349 if (mParent <> nil) then
352 for f := 0 to High(mParent.mChildren) do
354 if (mParent.mChildren[f] = self) then
356 for c := f+1 to High(mParent.mChildren) do mParent.mChildren[c-1] := mParent.mChildren[c];
357 SetLength(mParent.mChildren, Length(mParent.mChildren)-1);
361 for f := 0 to High(mChildren) do
363 mChildren[f].mParent := nil;
370 procedure THControl.activated ();
375 procedure THControl.blurred ();
381 function THControl.topLevel (): THControl; inline;
384 while (result.mParent <> nil) do result := result.mParent;
388 function THControl.getEnabled (): Boolean;
393 if (not mEnabled) or (mWidth < 1) or (mHeight < 1) then exit;
395 while (ctl <> nil) do
397 if (not ctl.mEnabled) or (ctl.mWidth < 1) or (ctl.mHeight < 1) then exit;
404 procedure THControl.setEnabled (v: Boolean); inline;
406 if (mEnabled = v) then exit;
408 if not v and focused then setFocused(false);
412 function THControl.getFocused (): Boolean; inline;
414 if (mParent = nil) then result := (Length(uiTopList) > 0) and (uiTopList[High(uiTopList)] = self) else result := (topLevel.mFocused = self);
418 procedure THControl.setFocused (v: Boolean); inline;
425 if (tl.mFocused = self) then
428 tl.mFocused := tl.findNextFocus(self);
429 if (tl.mFocused = self) then tl.mFocused := nil;
433 if (not mEnabled) or (not mCanFocus) then exit;
434 if (tl.mFocused <> self) then
436 tl.mFocused.blurred();
438 if (tl.mGrab <> self) then tl.mGrab := nil;
444 function THControl.isMyChild (ctl: THControl): Boolean;
447 while (ctl <> nil) do
449 if (ctl.mParent = self) then exit;
456 // returns `true` if global coords are inside this control
457 function THControl.toLocal (var x, y: Integer): Boolean;
462 while (ctl <> nil) do
468 result := (x >= 0) and (y >= 0) and (x < mWidth) and (y < mHeight);
472 procedure THControl.toGlobal (var x, y: Integer);
477 while (ctl <> nil) do
486 // x and y are global coords
487 function THControl.controlAtXY (x, y: Integer): THControl;
493 if (not mEnabled) or (mWidth < 1) or (mHeight < 1) then exit;
496 if not toLocal(lx, ly) then exit;
497 for f := High(mChildren) downto 0 do
499 result := mChildren[f].controlAtXY(x, y);
500 if (result <> nil) then exit;
506 function THControl.prevSibling (): THControl;
510 if (mParent <> nil) then
512 for f := 1 to High(mParent.mChildren) do
514 if (mParent.mChildren[f] = self) then begin result := mParent.mChildren[f-1]; exit; end;
520 function THControl.nextSibling (): THControl;
524 if (mParent <> nil) then
526 for f := 0 to High(mParent.mChildren)-1 do
528 if (mParent.mChildren[f] = self) then begin result := mParent.mChildren[f+1]; exit; end;
534 function THControl.firstChild (): THControl; inline;
536 if (Length(mChildren) <> 0) then result := mChildren[0] else result := nil;
539 function THControl.lastChild (): THControl; inline;
541 if (Length(mChildren) <> 0) then result := mChildren[High(mChildren)] else result := nil;
545 function THControl.findFirstFocus (): THControl;
552 for f := 0 to High(mChildren) do
554 result := mChildren[f].findFirstFocus();
555 if (result <> nil) then exit;
557 if mCanFocus then result := self;
562 function THControl.findLastFocus (): THControl;
569 for f := High(mChildren) downto 0 do
571 result := mChildren[f].findLastFocus();
572 if (result <> nil) then exit;
574 if mCanFocus then result := self;
579 function THControl.findNextFocus (cur: THControl): THControl;
584 if not isMyChild(cur) then cur := nil;
585 if (cur = nil) then begin result := findFirstFocus(); exit; end;
586 result := cur.findFirstFocus();
587 if (result <> nil) and (result <> cur) then exit;
590 cur := cur.nextSibling;
591 if (cur = nil) then break;
592 result := cur.findFirstFocus();
593 if (result <> nil) then exit;
595 result := findFirstFocus();
600 function THControl.findPrevFocus (cur: THControl): THControl;
605 if not isMyChild(cur) then cur := nil;
606 if (cur = nil) then begin result := findLastFocus(); exit; end;
608 result := cur.findLastFocus();
609 if (result <> nil) and (result <> cur) then exit;
612 cur := cur.prevSibling;
613 if (cur = nil) then break;
614 result := cur.findLastFocus();
615 if (result <> nil) then exit;
617 result := findLastFocus();
622 procedure THControl.appendChild (ctl: THControl);
624 if (ctl = nil) then exit;
625 if (ctl.mParent <> nil) then exit;
626 SetLength(mChildren, Length(mChildren)+1);
627 mChildren[High(mChildren)] := ctl;
629 Inc(ctl.mX, mFrameWidth);
630 Inc(ctl.mY, mFrameHeight);
631 if (ctl.mWidth > 0) and (ctl.mHeight > 0) and
632 (ctl.mX+ctl.mWidth > mFrameWidth) and (ctl.mY+ctl.mHeight > mFrameHeight) then
634 if (mWidth+mFrameWidth < ctl.mX+ctl.mWidth) then mWidth := ctl.mX+ctl.mWidth+mFrameWidth;
635 if (mHeight+mFrameHeight < ctl.mY+ctl.mHeight) then mHeight := ctl.mY+ctl.mHeight+mFrameHeight;
637 if (mFocused = nil) and ctl.mEnabled and ctl.mCanFocus and (ctl.mWidth > 0) and (ctl.mHeight > 0) then mFocused := ctl;
641 //TODO: overflow checks
642 class function THControl.intersectRect (var x0, y0, w0, h0: Integer; const x1, y1, w1, h1: Integer): Boolean;
647 if (w0 < 1) or (h0 < 1) or (w1 < 1) or (h1 < 1) then exit;
648 // check for intersection
649 if (x0+w0 <= x1) or (y0+h0 <= y1) or (x1+w1 <= x0) or (y1+h1 <= y0) then exit;
650 if (x0 >= x1+w1) or (y0 >= y1+h1) or (x1 >= x0+h0) or (y1 >= y0+h0) then exit;
654 if (x0 < x1) then x0 := x1;
655 if (y0 < y1) then y0 := y1;
656 if (ex0 > x1+w1) then ex0 := x1+w1;
657 if (ey0 > y1+h1) then ey0 := y1+h1;
660 result := (w0 > 0) and (h0 > 0);
664 procedure THControl.setScissorGLInternal (x, y, w, h: Integer);
666 if not scallowed then exit;
667 y := gWinSizeY-(y+h);
668 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);
672 procedure THControl.resetScissor ();
676 if not scallowed then exit;
680 setScissorGLInternal(x, y, mWidth, mHeight);
684 procedure THControl.setScissor (lx, ly, lw, lh: Integer);
688 if not scallowed then exit;
689 if not intersectRect(lx, ly, lw, lh, 0, 0, mWidth, mHeight) then begin glScissor(0, 0, 0, 0); exit; end;
693 setScissorGLInternal(x, y, lw, lh);
697 procedure THControl.draw ();
703 if (mWidth < 1) or (mHeight < 1) then exit;
707 //conwritefln('[%s]: (%d,%d)-(%d,%d) (%d,%d)', [ClassName, mX, mY, mWidth, mHeight, x, y]);
714 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
715 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
716 //conwritefln('(%d,%d)-(%d,%d)', [scxywh[0], scxywh[1], scxywh[2], scxywh[3]]);
717 glEnable(GL_SCISSOR_TEST);
722 if (mFrameWidth <> 0) or (mFrameHeight <> 0) then setScissor(mFrameWidth, mFrameHeight, mWidth-mFrameWidth*2, mHeight-mFrameHeight*2);
723 for f := 0 to High(mChildren) do mChildren[f].draw();
724 if (mFrameWidth <> 0) or (mFrameHeight <> 0) then resetScissor();
725 drawControlPost(x, y);
726 glScissor(scxywh[0], scxywh[1], scxywh[2], scxywh[3]);
728 if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
733 procedure THControl.drawControl (sx, sy: Integer);
735 if (mParent = nil) then darkenRect(sx, sy, mWidth, mHeight, 64);
739 procedure THControl.drawControlPost (sx, sy: Integer);
741 if mDrawShadow and (mWidth > 0) and (mHeight > 0) then
743 setScissorGLInternal(sx+8, sy+8, mWidth, mHeight);
744 darkenRect(sx+mWidth, sy+8, 8, mHeight, 128);
745 darkenRect(sx+8, sy+mHeight, mWidth-8, 8, 128);
750 function THControl.mouseEvent (var ev: THMouseEvent): Boolean;
755 if not mEnabled then exit;
756 if (mParent = nil) then
758 if (mGrab <> nil) then
760 result := mGrab.mouseEvent(ev);
761 if (ev.kind = ev.Release) then mGrab := nil;
765 if (mWidth < 1) or (mHeight < 1) then exit;
766 ctl := controlAtXY(ev.x, ev.y);
767 if (ctl <> nil) and (ctl <> self) then
769 if (ctl <> topLevel.mFocused) then ctl.setFocused(true);
770 result := ctl.mouseEvent(ev);
772 else if (ctl = self) and assigned(actionCB) then
779 function THControl.keyEvent (var ev: THKeyEvent): Boolean;
784 if not mEnabled then exit;
785 if (topLevel.mFocused <> self) and isMyChild(topLevel.mFocused) and topLevel.mFocused.mEnabled then result := topLevel.mFocused.keyEvent(ev);
786 if (mParent = nil) then
788 if (ev.kind = ev.Press) and (ev = 'S-Tab') then
791 if (ev.kind = ev.Press) then
793 ctl := findPrevFocus(mFocused);
794 if (ctl <> mFocused) then
802 if (ev.kind = ev.Press) and (ev = 'Tab') then
805 if (ev.kind = ev.Press) then
807 ctl := findNextFocus(mFocused);
808 if (ctl <> mFocused) then
816 if mEscClose and (ev.kind = ev.Press) and (ev = 'Escape') then
819 uiRemoveWindow(self);
823 if mEatKeys then result := true;
827 // ////////////////////////////////////////////////////////////////////////// //
828 constructor THTopWindow.Create (const atitle: AnsiString; ax, ay: Integer; aw: Integer=-1; ah: Integer=-1);
830 inherited Create(ax, ay, aw, ah, nil);
834 if (mWidth < mFrameWidth*2+3*8) then mWidth := mFrameWidth*2+3*8;
835 if (mHeight < mFrameHeight*2) then mHeight := mFrameHeight*2;
836 if (Length(mTitle) > 0) then
838 if (mWidth < Length(mTitle)*8+mFrameWidth*2+3*8) then mWidth := Length(mTitle)*8+mFrameWidth*2+3*8;
842 mWaitingClose := false;
848 procedure THTopWindow.centerInScreen ();
850 if (mWidth > 0) and (mHeight > 0) then
852 mX := (gWinSizeX-mWidth) div 2;
853 mY := (gWinSizeY-mHeight) div 2;
858 procedure THTopWindow.drawControl (sx, sy: Integer);
860 fillRect(sx, sy, mWidth, mHeight, 0, 0, 128);
864 procedure THTopWindow.drawControlPost (sx, sy: Integer);
873 drawRect(mX+4, mY+4, mWidth-8, mHeight-8, r, g, b);
877 drawRect(mX+3, mY+3, mWidth-6, mHeight-6, r, g, b);
878 drawRect(mX+5, mY+5, mWidth-10, mHeight-10, r, g, b);
879 setScissor(mFrameWidth, 0, 3*8, 8);
880 fillRect(mX+mFrameWidth, mY, 3*8, 8, 0, 0, 128);
881 drawText8(mX+mFrameWidth, mY, '[ ]', r, g, b);
882 if mInClose then drawText8(mX+mFrameWidth+7, mY, '#', 0, 255, 0)
883 else drawText8(mX+mFrameWidth+7, mY, '*', 0, 255, 0);
885 if (Length(mTitle) > 0) then
887 setScissor(mFrameWidth+3*8, 0, mWidth-mFrameWidth*2-3*8, 8);
888 tx := (mX+3*8)+((mWidth-3*8)-Length(mTitle)*8) div 2;
889 fillRect(tx-3, mY, Length(mTitle)*8+3+2, 8, 0, 0, 128);
890 drawText8(tx, mY, mTitle, r, g, b);
892 inherited drawControlPost(sx, sy);
896 procedure THTopWindow.blurred ();
899 mWaitingClose := false;
905 function THTopWindow.keyEvent (var ev: THKeyEvent): Boolean;
907 result := inherited keyEvent(ev);
908 if not getFocused then exit;
909 if (ev.kind = ev.Press) and (ev = 'M-F3') then
911 uiRemoveWindow(self);
918 function THTopWindow.mouseEvent (var ev: THMouseEvent): Boolean;
923 if not mEnabled then exit;
924 if (mWidth < 1) or (mHeight < 1) then exit;
928 mX += ev.x-mDragStartX;
929 mY += ev.y-mDragStartY;
932 if (ev.kind = ev.Release) then mDragging := false;
939 if toLocal(lx, ly) then
941 if (ev.kind = ev.Press) then
945 if (lx >= mFrameWidth) and (lx < mFrameWidth+3*8) then
947 //uiRemoveWindow(self);
948 mWaitingClose := true;
960 if (lx < mFrameWidth) or (lx >= mWidth-mFrameWidth) or (ly >= mHeight-mFrameHeight) then
970 if (ev.kind = ev.Release) then
972 if mWaitingClose and (lx >= mFrameWidth) and (lx < mFrameWidth+3*8) then
974 uiRemoveWindow(self);
978 mWaitingClose := false;
982 if (ev.kind = ev.Motion) then
984 if mWaitingClose then
986 mInClose := (lx >= mFrameWidth) and (lx < mFrameWidth+3*8);
995 if (ev.kind <> ev.Motion) then mWaitingClose := false;
998 result := inherited mouseEvent(ev);
1002 // ////////////////////////////////////////////////////////////////////////// //
1003 constructor THCtlSimpleText.Create (ax, ay: Integer; aparent: THControl=nil);
1006 inherited Create(ax, ay, 4, 4);
1010 destructor THCtlSimpleText.Destroy ();
1017 procedure THCtlSimpleText.appendItem (const atext: AnsiString; acentered: Boolean=false; ahline: Boolean=false);
1021 if (Length(atext)*8+3*8+2 > mWidth) then mWidth := Length(atext)*8+3*8+2;
1022 SetLength(mItems, Length(mItems)+1);
1023 it := @mItems[High(mItems)];
1025 it.centered := acentered;
1027 if (Length(mItems)*8 > mHeight) then mHeight := Length(mItems)*8;
1031 procedure THCtlSimpleText.drawControl (sx, sy: Integer);
1037 for f := 0 to High(mItems) do
1044 if it.centered then begin b := 255; tx := sx+(mWidth-Length(it.title)*8) div 2; end;
1048 if (Length(it.title) = 0) then
1050 drawLine(sx+4, sy+3, sx+mWidth-8, sy+3, r, g, b);
1052 else if (tx-3 > sx+4) then
1054 drawLine(sx+4, sy+3, tx-3, sy+3, r, g, b);
1055 drawLine(tx+Length(it.title)*8, sy+3, sx+mWidth-4, sy+3, r, g, b);
1058 drawText8(tx, sy, it.title, r, g, b);
1064 function THCtlSimpleText.mouseEvent (var ev: THMouseEvent): Boolean;
1068 result := inherited mouseEvent(ev);
1071 if not result and toLocal(lx, ly) then
1078 function THCtlSimpleText.keyEvent (var ev: THKeyEvent): Boolean;
1080 result := inherited keyEvent(ev);
1084 // ////////////////////////////////////////////////////////////////////////// //
1085 constructor THCtlCBListBox.Create (ax, ay: Integer; aparent: THControl=nil);
1089 inherited Create(ax, ay, 4, 4);
1093 destructor THCtlCBListBox.Destroy ();
1100 procedure THCtlCBListBox.appendItem (const atext: AnsiString; bv: PBoolean; aaction: TActionCB=nil);
1104 if (Length(atext)*8+3*8+2 > mWidth) then mWidth := Length(atext)*8+3*8+2;
1105 SetLength(mItems, Length(mItems)+1);
1106 it := @mItems[High(mItems)];
1109 it.actionCB := aaction;
1110 if (Length(mItems)*8 > mHeight) then mHeight := Length(mItems)*8;
1111 if (mCurIndex < 0) then mCurIndex := 0;
1115 procedure THCtlCBListBox.drawControl (sx, sy: Integer);
1120 for f := 0 to High(mItems) do
1123 if (mCurIndex = f) then fillRect(sx, sy, mWidth, 8, 0, 128, 0);
1124 if (it.varp <> nil) then
1126 if it.varp^ then drawText8(sx, sy, '[x]', 255, 255, 255) else drawText8(sx, sy, '[ ]', 255, 255, 255);
1127 drawText8(sx+3*8+2, sy, it.title, 255, 255, 0);
1129 else if (Length(it.title) > 0) then
1131 tx := sx+(mWidth-Length(it.title)*8) div 2;
1132 if (tx-3 > sx+4) then
1134 drawLine(sx+4, sy+3, tx-3, sy+3, 255, 255, 255);
1135 drawLine(tx+Length(it.title)*8, sy+3, sx+mWidth-4, sy+3, 255, 255, 255);
1137 drawText8(tx, sy, it.title, 255, 255, 255);
1141 drawLine(sx+4, sy+3, sx+mWidth-8, sy+3, 255, 255, 255);
1148 function THCtlCBListBox.mouseEvent (var ev: THMouseEvent): Boolean;
1153 result := inherited mouseEvent(ev);
1156 if not result and toLocal(lx, ly) then
1159 if (ev.kind = ev.Press) and (ev = 'lmb') then
1162 if (ly >= 0) and (ly < Length(mItems)) then
1165 if (it.varp <> nil) then
1168 it.varp^ := not it.varp^;
1169 if assigned(it.actionCB) then it.actionCB(self, Integer(it.varp^));
1170 if assigned(actionCB) then actionCB(self, ly);
1178 function THCtlCBListBox.keyEvent (var ev: THKeyEvent): Boolean;
1182 result := inherited keyEvent(ev);
1183 if not getFocused then exit;
1185 if (ev.kind = ev.Press) then
1187 if (ev = 'Home') or (ev = 'PageUp') then
1192 if (ev = 'End') or (ev = 'PageDown') then
1195 mCurIndex := High(mItems);
1200 if (Length(mItems) > 0) then
1202 if (mCurIndex < 0) then mCurIndex := Length(mItems);
1203 while (mCurIndex > 0) do
1206 if (mItems[mCurIndex].varp <> nil) then break;
1214 if (ev = 'Down') then
1217 if (Length(mItems) > 0) then
1219 if (mCurIndex < 0) then mCurIndex := -1;
1220 while (mCurIndex < High(mItems)) do
1223 if (mItems[mCurIndex].varp <> nil) then break;
1231 if (ev = 'Space') or (ev = 'Return') then
1234 if (mCurIndex >= 0) and (mCurIndex < Length(mItems)) and (mItems[mCurIndex].varp <> nil) then
1236 it := @mItems[mCurIndex];
1237 it.varp^ := not it.varp^;
1238 if assigned(it.actionCB) then it.actionCB(self, Integer(it.varp^));
1239 if assigned(actionCB) then actionCB(self, mCurIndex);