- cleanup: remove TQuestionDlg.ButtonKeyDown, simplify PromptDialogKeyDown - LCL do this already perfect itself
  - formatting

git-svn-id: trunk@28443 -
This commit is contained in:
paul 2010-11-24 07:56:46 +00:00
parent dd2575c2e0
commit 6ded956938

View File

@ -20,11 +20,9 @@ type
{ TPromptDialog }
TPromptDialog = class(TForm)
procedure PromptDialogKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure PromptDialogKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
private
function CreateButtons(AVerticalLayout: Boolean; ASpacing: Integer
): Integer;
function CreateButtons(AVerticalLayout: Boolean; ASpacing: Integer): Integer;
public
IsSmallDevice: Boolean;
@ -46,61 +44,10 @@ type
destructor Destroy; override;
end;
procedure TPromptDialog.PromptDialogKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
OldFocusControl, NewFocusControl: TWinControl;
i: integer;
procedure TPromptDialog.PromptDialogKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if (Key = VK_Escape) then
ModalResult := -1;
if (Key=VK_LEFT) or (Key=VK_RIGHT) then begin
// focus the next button to the left or right
// search old focused button
OldFocusControl:=FindOwnerControl(LCLIntf.GetFocus);
if (OldFocusControl=nil) or (GetParentForm(OldFocusControl)<>Self)
or (not (OldFocusControl is TCustomButton)) then
begin
OldFocusControl:=nil;
for i:=0 to ComponentCount-1 do
if (Components[i] is TCustomButton)
and (TCustomButton(Components[i]).Default) then
begin
OldFocusControl:=TCustomButton(Components[i]);
break;
end;
end;
// find next focused button
if (OldFocusControl<>nil) then begin
i:=ComponentCount-1;
while i>=0 do begin
if Components[i]=OldFocusControl then
break
else
dec(i);
end;
if i<0 then exit;
NewFocusControl:=nil;
repeat
if Key=VK_LEFT then begin
dec(i);
if i<0 then i:=ComponentCount-1;
end else begin
inc(i);
if i>=ComponentCount then i:=0;
end;
if Components[i] is TCustomButton then begin
NewFocusControl:=TWinControl(Components[i]);
break;
end;
until false;
ActiveControl:=NewFocusControl;
Key:=VK_UNKNOWN;
end;
end;
end;
function TPromptDialog.CreateButtons(AVerticalLayout: Boolean;
@ -115,7 +62,7 @@ begin
ButtonIndex := -1;
for curBtn := 0 to NumButtons - 1 do
begin
If (Buttons[curBtn] >= Low(DialogButtonKind)) and
if (Buttons[curBtn] >= Low(DialogButtonKind)) and
(Buttons[curBtn] <= High(DialogButtonKind))
then
begin
@ -177,7 +124,7 @@ begin
IsSmallDevice := (Screen.Width <= 300);
AutoScroll:=false;
AutoScroll := False;
OnKeyDown := @PromptDialogKeyDown;
//debugln('TPromptDialog.CreateMessageDialog A ButtonCount=',dbgs(ButtonCount));
@ -214,12 +161,10 @@ begin
NumButtons := ButtonCount;
Buttons := TheButtons;
if (DefaultIndex >= ButtonCount) or
(DefaultIndex < 0)
then
if (DefaultIndex >= ButtonCount) or (DefaultIndex < 0) then
TheDefaultIndex := 0
else
theDefaultIndex := DefaultIndex;
TheDefaultIndex := DefaultIndex;
// Assures a minimum text size
if MSG = '' then MSG := ' ';
@ -237,9 +182,9 @@ begin
end;
if IsSmallDevice then
LayoutDialogSmallDevice()
LayoutDialogSmallDevice
else
LayoutDialog();
LayoutDialog;
end;
destructor TPromptDialog.Destroy;
@ -249,7 +194,7 @@ begin
end;
procedure TPromptDialog.LayoutDialog;
Const
const
cBtnCalcWidth = 50;
cBtnCalcHeight = 13;
cBtnCalcSpace = 4;
@ -555,10 +500,9 @@ begin
end;
end;
function CreateMessageDialog(const Msg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons): TForm;
var PDlg: TPromptDialog;
function CreateMessageDialog(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons): TForm;
var
PDlg: TPromptDialog;
aCaption: String;
Btns: PLongInt;
CancelValue, DefaultIndex, ButtonCount: Longint;
@ -567,22 +511,17 @@ begin
aCaption := MsgDlgCaptions[DlgType]
else
aCaption := Application.Title;
Btns := GetPromptUserButtons(Buttons, CancelValue, DefaultIndex, ButtonCount,
False, mbOk);
Btns := GetPromptUserButtons(Buttons, CancelValue, DefaultIndex, ButtonCount, False, mbOk);
PDlg := TPromptDialog.CreateMessageDialog(aCaption, Msg, DialogIds[DlgType], Btns, ButtonCount, DefaultIndex);
Result := TForm(PDlg);
ReallocMem(Btns, 0);
end;
type
{ TQuestionDlg }
TQuestionDlg = class(TForm)
procedure ButtonKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
FButtons: TList;
FBitmap: TCustomBitmap;
@ -743,40 +682,22 @@ var
CurValue: TModalResult;
j: Integer;
begin
if FButtons=nil then begin
Result:=nil;
exit;
end;
for i:=Low(Order) to High(Order) do begin
CurValue:=Order[i];
for j:=0 to FButtons.Count-1 do begin
Result:=TBitBtn(FButtons[j]);
if Result.ModalResult=CurValue then exit;
end;
end;
Result:=nil;
end;
procedure TQuestionDlg.ButtonKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
Handled: Boolean;
if FButtons = nil then
begin
if Shift<>[] then exit;
Handled:=true;
if (Key=VK_ESCAPE) and (CancelControl<>nil) then
CancelControl.ExecuteCancelAction
else if (Key in [VK_RETURN,VK_SPACE]) and (Sender is TBitBtn) then
ModalResult:=TBitBtn(Sender).ModalResult
else if (Key=VK_RETURN) and (DefaultControl<>nil) then
DefaultControl.ExecuteDefaultAction
else if (Key=VK_LEFT) then
TWinControl(Sender).PerformTab(false)
else if (Key=VK_RIGHT) then
TWinControl(Sender).PerformTab(true)
else
Handled:=false;
if Handled then Key:=VK_UNKNOWN;
Result := nil;
Exit;
end;
for i := Low(Order) to High(Order) do
begin
CurValue := Order[i];
for j := 0 to FButtons.Count - 1 do
begin
Result := TBitBtn(FButtons[j]);
if Result.ModalResult = CurValue then
Exit;
end;
end;
Result := nil;
end;
constructor TQuestionDlg.CreateQuestionDlg(const aCaption, aMsg: string;
@ -800,7 +721,6 @@ begin
Position := poScreenCenter;
MessageTxt := ConvertLineEndings(aMsg);
HelpContext := HelpCtx;
OnKeyDown := @ButtonKeyDown;
// Initialize TextStyle
FillChar(TextStyle, SizeOf(TTextStyle), 0);
@ -817,11 +737,13 @@ begin
try
DefaultBtn:=nil;
i:=Low(Buttons);
while i<=High(Buttons) do begin
while i <= High(Buttons) do
begin
if Buttons[i].VType <> vtInteger then
raise Exception.Create('TQuestionDlg.CreateQuestionDlg integer expected at '
+IntToStr(i)+' but VType='+IntToStr(ord(Buttons[i].VType))+' found.');
if Buttons[i].VType=vtInteger then begin
if Buttons[i].VType = vtInteger then
begin
// get TModalResult
CurBtnValue := Buttons[i].VInteger;
//debugln('TQuestionDlg.CreateQuestionDlg i=',dbgs(i),' CurBtnValue=',dbgs(CurBtnValue));
@ -829,7 +751,8 @@ begin
// get button caption
CurBtnCaption := '';
if (i<=High(Buttons)) then begin
if (i <= High(Buttons)) then
begin
//debugln('TQuestionDlg.CreateQuestionDlg i=',dbgs(i),' Buttons[i].VType=',dbgs(Buttons[i].VType),' vtString=',dbgs(vtString));
case Buttons[i].VType of
vtString: CurBtnCaption := Buttons[i].VString^;
@ -847,10 +770,11 @@ begin
// get options
CurOptions := '';
IsDefault:=false;
if (i<=High(Buttons)) then begin
IsDefault := False;
if (i<=High(Buttons)) then
begin
//debugln('TQuestionDlg.CreateQuestionDlg i=',dbgs(i),' Buttons[i].VType=',dbgs(Buttons[i].VType),' vtString=',dbgs(vtString));
HasOptions:=true;
HasOptions := True;
case Buttons[i].VType of
vtString: CurOptions := Buttons[i].VString^;
vtAnsiString: CurOptions := AnsiString(Buttons[i].VAnsiString);
@ -860,7 +784,7 @@ begin
vtWideChar: CurOptions := Buttons[i].VWideChar;
vtWidestring: CurOptions := WideString(Buttons[i].VWideString);
else
HasOptions:=false;
HasOptions := False;
end;
if HasOptions then
begin
@ -869,13 +793,14 @@ begin
+IntToStr(i)+' but "'+CurOptions+'" found.');
if DefaultBtn<>nil then
raise Exception.Create('TQuestionDlg.CreateQuestionDlg only one button can be default');
IsDefault:=true;
IsDefault := True;
inc(i);
end;
end;
//DebugLn('TQuestionDlg.CreateQuestionDlg CurBtnCaption=',CurBtnCaption,' CurOptions="',CurOptions,'"');
if CurBtnCaption='' then begin
if CurBtnCaption = '' then
begin
// find default caption
case CurBtnValue of
mrOk : CurBtnCaption := rsmbOk;
@ -890,10 +815,8 @@ begin
mrNoToAll : CurBtnCaption := rsmbNoToAll;
end;
end;
if CurBtnCaption='' then begin
raise Exception.Create(
'TQuestionDlg.Create: missing Button caption '+dbgs(i-1));
end;
if CurBtnCaption = '' then
raise Exception.Create('TQuestionDlg.Create: missing Button caption '+dbgs(i-1));
// get button kind
case curBtnValue of
@ -907,14 +830,17 @@ begin
mrAll: NewKind := bkAll;
mrNoToAll: NewKind := bkNoToAll;
mrYesToAll: NewKind := bkYesToAll;
else NewKind:=bkCustom;
else
NewKind := bkCustom;
end;
// add button
if FButtons=nil then FButtons:=TList.Create;
if FButtons = nil then
FButtons := TList.Create;
NewButton := TBitBtn.Create(Self);
with NewButton do begin
AutoSize:=false;
with NewButton do
begin
AutoSize := False;
Anchors := [akLeft, akBottom];
ModalResult := curBtnValue;
Layout := blGlyphLeft;
@ -922,16 +848,15 @@ begin
Caption := curBtnCaption;
Parent := Self;
Default := IsDefault;
OnKeyDown:=@ButtonKeyDown;
end;
if IsDefault then
DefaultBtn := NewButton;
FButtons.Add(NewButton);
end else
raise Exception.Create(
'TQuestionDlg.Create: invalid Buttons parameter '+dbgs(i));
end
else
raise Exception.Create('TQuestionDlg.Create: invalid Buttons parameter '+dbgs(i));
end;
ok:=true;
ok := True;
finally
if not Ok then
FreeAndNil(FButtons);
@ -956,11 +881,9 @@ begin
// find default and cancel button
if DefaultBtn = nil then
DefaultBtn:=FindButton([mrYes,mrOk,mrYesToAll,mrAll,mrRetry,mrCancel,
mrNo,mrNoToAll,mrAbort,mrIgnore]);
DefaultBtn := FindButton([mrYes, mrOk, mrYesToAll, mrAll, mrRetry, mrCancel, mrNo, mrNoToAll, mrAbort, mrIgnore]);
DefaultControl := DefaultBtn;
CancelControl:=FindButton([mrAbort,mrCancel,mrNo,mrIgnore,mrNoToAll,mrYes,
mrOk,mrRetry,mrAll,mrYesToAll])
CancelControl := FindButton([mrAbort, mrCancel, mrNo, mrIgnore, mrNoToAll, mrYes, mrOk, mrRetry, mrAll, mrYesToAll])
end;
destructor TQuestionDlg.Destroy;
@ -993,8 +916,7 @@ function QuestionDlg(const aCaption, aMsg: string; DlgType: TMsgDlgType;
var
QuestionDialog: TQuestionDlg;
begin
QuestionDialog:=TQuestionDlg.CreateQuestionDlg(aCaption,aMsg,DlgType,Buttons,
HelpCtx);
QuestionDialog := TQuestionDlg.CreateQuestionDlg(aCaption, aMsg, DlgType, Buttons, HelpCtx);
try
Result := QuestionDialog.ShowModal;
finally