unit Utils; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Graphics, Controls, StdCtrls, ComCtrls, Dialogs, Forms, Globals; type TToolbarPosition = (tpTop, tpLeft, tpRight); procedure InitForm(AForm: TForm); procedure AddButtonToToolbar(AToolButton: TToolButton; AToolBar: TToolBar); procedure InitToolbar(AToolbar: TToolbar; APosition: TToolbarPosition); function AnySelected(AListbox: TListBox): Boolean; procedure ErrorMsg(const AMsg: String); procedure ErrorMsg(const AMsg: String; const AParams: array of const); function CenterString(S: String; Width: Integer): String; function IndexOfString(L: StrDyneVec; s: String): Integer; implementation uses StrUtils, ToolWin; // https://stackoverflow.com/questions/4093595/create-ttoolbutton-runtime procedure AddButtonToToolbar(AToolButton: TToolButton; AToolBar: TToolBar); var lastBtnIdx: integer; begin lastBtnIdx := AToolBar.ButtonCount - 1; if lastBtnIdx > -1 then AToolButton.Left := AToolBar.Buttons[lastBtnIdx].Left + AToolBar.Buttons[lastBtnIdx].Width else AToolButton.Left := 0; AToolButton.Parent := AToolBar; end; procedure InitForm(AForm: TForm); begin AForm.Width := AForm.Scale96ToFont(DEFAULT_WIDTH); AForm.Height := AForm.Scale96ToFont(DEFAULT_HEIGHT); end; procedure InitToolbar(AToolbar: TToolbar; APosition: TToolbarPosition); begin // AToolbar.Transparent := false; // AToolbar.Color := clForm; case APosition of tpTop: begin AToolbar.Align := alTop; AToolbar.EdgeBorders := [ebBottom]; end; tpLeft: begin AToolbar.Align := alLeft; AToolbar.EdgeBorders := [ebRight]; end; tpRight: begin AToolbar.Align := alRight; AToolbar.EdgeBorders := [ebLeft]; end; end; end; function AnySelected(AListBox: TListBox): Boolean; var i: Integer; begin Result := false; for i := 0 to AListbox.Items.Count-1 do if AListbox.Selected[i] then begin Result := true; exit; end; end; procedure ErrorMsg(const AMsg: String); begin MessageDlg(AMsg, mtError, [mbOK], 0); end; procedure ErrorMsg(const AMsg: String; const AParams: array of const); begin ErrorMsg(Format(AMsg, AParams)); end; function CenterString(S: String; Width: Integer): String; var n1, n2: Integer; begin n1 := Width - Length(S); if n1 <= 0 then begin Result := S; exit; end; n1 := n1 div 2; if Length(S) + 2*n1 < Width then n2 := n1+1 else n2 := n1; Result := DupeString(' ', n1) + S + DupeString(' ', n2); end; function IndexOfString(L: StrDyneVec; s: String): Integer; var i: Integer; begin Result := -1; for i := 0 to High(L) do if L[i] = s then begin Result := i; exit; end; end; end.