unit SelctGui; (************************************************) (* SELCTGUI.PAS *) (* Author: Daniel Marschall *) (* Revision: 2020-09-07 *) (* License: Apache 2.0 *) (* This file contains: *) (* - A list box control GUI *) (************************************************) interface uses StrList; function DoScreenSelectionList(X, Y, ListWidth, ListHeight: integer; items: PStringList): integer; implementation uses Crt; (* MISC FUNCTIONS *) function FillRight(str: string; len: integer; c: char): string; var s: string; i: integer; begin s := str; for i := Length(str) to len-1 do begin s := s + c; end; FillRight := s; end; (* GRAPHICAL USER INTERFACE (GUI) *) function DoScreenSelectionList(X, Y, ListWidth, ListHeight: integer; items: PStringList): integer; var i: integer; itemIndex: integer; sc: char; iStartScope, iEndScope: integer; label doAgain; begin ClrScr; itemIndex := 0; iStartScope := itemIndex; iEndScope := itemIndex + ListHeight; doAgain: if itemIndex < 0 then itemIndex := 0; if itemIndex > ListCount(items)-1 then itemIndex := ListCount(items)-1; if itemIndex < iStartScope then begin Dec(iEndScope); Dec(iStartScope); end; if itemIndex > iEndScope then begin Inc(iEndScope); Inc(iStartScope); end; for i := iStartScope to iEndScope-1 do begin if itemIndex = i then begin TextColor(Black); TextBackground(White); end else begin TextColor(White); TextBackground(Black); end; GotoXY(x,y+i-iStartScope); if i > ListCount(items)-1 then Write(FillRight('', ListWidth, ' ')) else Write(FillRight(ListGetElement(items, i), ListWidth, ' ')); TextColor(White); TextBackground(Black); end; repeat sc := ReadKey; if sc = #0 then begin sc := ReadKey; if sc = #72 then begin dec(itemIndex); goto doAgain; end; if sc = #80 then begin inc(itemIndex); goto doAgain; end; end; if sc = #13(*Return*) then begin DoScreenSelectionList := itemIndex; exit; end; if sc = #27(*ESC*) then begin DoScreenSelectionList := -1; exit; end; until false; end; end.