unit Utils; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Graphics, Controls, StdCtrls, ExtCtrls, ComCtrls, Dialogs, Forms, Globals; type TToolbarPosition = (tpTop, tpLeft, tpRight); procedure InitForm(AForm: TForm); procedure AddButtonToToolbar(AToolButton: TToolButton; AToolBar: TToolBar); procedure AddComboboxToToolbar(AToolBar: TToolbar; ACaption: String; out ACombobox: TCombobox); 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; function MaxValueI(const AData: array of Integer): 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 AddComboboxToToolbar(AToolbar: TToolbar; ACaption: String; out AComboBox: TComboBox); var panel: TPanel; lbl: TLabel = nil; begin panel := TPanel.Create(AToolbar); if ACaption <> '' then begin lbl := TLabel.Create(panel); lbl.Parent := panel; lbl.Caption := ACaption; end; ACombobox := TCombobox.Create(panel); ACombobox.Parent := panel; ACombobox.Style := csDropdownList; ACombobox.DropdownCount := 24; ACombobox.Items.Clear; ACombobox.Constraints.MinWidth := 300; if lbl <> nil then begin lbl.AnchorSideLeft.Side := asrTop; lbl.AnchorSideLeft.Control := panel; lbl.AnchorSideTop.Side := asrCenter; lbl.AnchorSideTop.Control := ACombobox; end; ACombobox.AnchorSideLeft.Control := lbl; ACombobox.AnchorSideLeft.Side := asrBottom; ACombobox.BorderSpacing.Left := 6; ACombobox.BorderSpacing.Around := 2; panel.Parent := AToolbar; panel.AutoSize := true; panel.BevelInner := bvNone; panel.BevelOuter := bvNone; panel.Left := 0; 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; // Reimplements MaxValue of unit Math which cannot be compiled on 64 bit due // to "Can't determine which overloaded function to call". function MaxValueI(const AData: array of Integer): Integer; var i: Integer; begin Result := -MaxInt; for i := 0 to High(AData) do if Result < AData[i] then Result := AData[i]; end; end.