{*******************************************************} { } { Responsive Software http://www.responsive.co.nz } { } { Copyright (c) 2003-2006 Responsive Software Limited } { } {*******************************************************} unit PromptPaymentType; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Base, Grids, StdCtrls, Buttons, DatabaseObjects; type TPromptPaymentTypeForm = class(TBaseForm) CancelBitBtn: TBitBtn; procedure FormShow(Sender: TObject); procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure ButtonClick(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } FButtons : TList; FPaymentTypes : TDatabaseObjectCollection; public { Public declarations } PaymentTypeId : int64; function Prompt : boolean; end; var PromptPaymentTypeForm: TPromptPaymentTypeForm; implementation {$R *.dfm} uses GeneralUtilities, Globals; function ComparePaymentTypes (Item1, Item2 : pointer) : integer; var PaymentType1, PaymentType2 : TPaymentType; begin PaymentType1 := TPaymentType(Item1); PaymentType2 := TPaymentType(Item2); if (PaymentType1 = nil) and (PaymentType2 = nil) then Result := 0 else if (PaymentType1 = nil) and (PaymentType2 <> nil) then Result := -1 else if (PaymentType1 <> nil) and (PaymentType2 = nil) then Result := 1 else begin // compare on keyboard shortcut Result := CompareText(PaymentType1.KeyboardShortcut,PaymentType2.KeyboardShortcut); // if same then compare on name if Result = 0 then Result := CompareText(PaymentType1.Name,PaymentType2.Name); end; end; function TPromptPaymentTypeForm.Prompt : boolean; begin Result := (ShowModal = mrOk); end; procedure TPromptPaymentTypeForm.FormShow(Sender: TObject); var i : integer; Button : TButton; Index : integer; PaymentType : TPaymentType; begin // create a sorted copy of the active payment types FPaymentTypes.Free; FPaymentTypes := TDatabaseObjectCollection.Create; FPaymentTypes.Owned := false; for i := 0 to Globals.PaymentTypes.Count - 1 do if TPaymentType(PaymentTypes[i]).Active then FPaymentTypes.Add(PaymentTypes[i]); FPaymentTypes.Sort(ComparePaymentTypes); PaymentTypeId := 0; ClearList(FButtons); Index := 0; for i := 0 to FPaymentTypes.Count - 1 do begin PaymentType := TPaymentType(FPaymentTypes[i]); Button := TButton.Create(nil); Button.Parent := Self; Button.Left := 32; Button.Top := 16 + Index * 32; Button.Width := 153; Button.Height := 25; if PaymentType.KeyboardShortcut <> ' ' then Button.Caption := '&' + PaymentType.KeyboardShortcut + ' - ' + PaymentType.Name else Button.Caption := PaymentType.Name; Button.TabOrder := Index + 1; Button.OnClick := ButtonClick; Inc(Index); FButtons.Add(Button); end; CancelBitBtn.Top := 25 + Index * 32; Height := 92 + Index * 32; if FButtons.Count > 0 then TButton(FButtons[0]).SetFocus; end; procedure TPromptPaymentTypeForm.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = VK_ESCAPE then begin Close; Key := 0; end; end; procedure TPromptPaymentTypeForm.ButtonClick(Sender: TObject); begin PaymentTypeId := FPaymentTypes[FButtons.IndexOf(Sender)].Id; ModalResult := mrOk; end; procedure TPromptPaymentTypeForm.FormCreate(Sender: TObject); begin FButtons := TList.Create; end; end.