1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
|
{%MainUnit ../dialogs.pp}
{******************************************************************************
MessageDialogs
******************************************************************************
*****************************************************************************
This file is part of the Lazarus Component Library (LCL)
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
current design flaws:
- ??? There has to be at least one :-)
Delphi compatibility:
- the interface is almost like in delphi 5
TODO:
- Help Context
- Help-button
- User ability to customize Button order
}
function ModalEscapeValue(Buttons: TMsgDlgButtons): TModalResult;
begin
Result := mrCancel;
end;
function ModalDefaultButton(Buttons : TMsgDlgButtons) : TMsgDlgbtn;
var
b: TMsgDlgBtn;
begin
Result := mbYes; // Some default return value.
If mbYes in Buttons then
Result := mbYes
else
If mbOk in Buttons then
Result := mbOk
else
If mbYesToAll in Buttons then
Result := mbYesToAll
else
If mbAll in Buttons then
Result := mbAll
else
If mbRetry in Buttons then
Result := mbRetry
else
If mbCancel in Buttons then
Result := mbCancel
else
If mbNo in Buttons then
Result := mbNo
else
If mbNoToAll in Buttons then
Result := mbNoToAll
else
If mbAbort in Buttons then
Result := mbAbort
else
If mbIgnore in Buttons then
Result := mbIgnore
else
begin
for b := Low(TMsgDlgBtn) to High(TMsgDlgBtn) do
if b in Buttons then
Result := b;
end;
end;
const
DialogIds : Array[mtWarning..mtCustom] of Longint = (idDialogWarning,
idDialogError, idDialogInfo, idDialogConfirm, idDialogBase);
ButtonIds : Array[TMsgDlgbtn] of Longint = (idButtonYes, idButtonNo,
idButtonOK, idButtonCancel, idButtonAbort, idButtonRetry, idButtonIgnore,
idButtonAll, idButtonNoToAll, idButtonYesToAll, idButtonHelp,
idButtonClose);
DialogResults : Array[idButtonOK..idButtonNoToAll] of TModalResult = (
mrOk, mrCancel,
mrNone{Help - when a mbHelp button is pressed the help system is started,
the dialog does not close },
mrYes, mrNo, mrClose, mrAbort, mrRetry,
mrIgnore, mrAll, mrYesToAll, mrNoToAll);
function GetPromptUserButtons(Buttons: TMsgDlgButtons; var CancelValue,
DefaultIndex, ButtonCount : Longint; UseDefButton: Boolean; DefButton: TMsgDlgBtn) : PLongint;
var
CurBtn : TMsgDlgBtn; // variable to loop through TMsgDlgButtons
DefaultButton : TMsgDlgBtn;
begin
if (Buttons = []) or (Buttons = [mbHelp]) then
Buttons := Buttons + [mbOk];
//Native PromptUser() dialog should return mrCancel on Escape or [X]-bordericon
//TPromptDialog.CreatMessageDialog responds to Escape in "old Delhpi" style,
//it will return mrCancel, mrNo, or mrOK if one of these buttons is present, else it will not respons to Escape key,
//TPromptDialog.CreatMessageDialog does not use the CancelValue variable
CancelValue := idButtonCancel;
if UseDefButton then
DefaultButton := DefButton
else
DefaultButton := ModalDefaultButton(Buttons);
DefaultIndex := 0;
ButtonCount := 0;
Result := nil;
for CurBtn := Low(TMsgDlgBtn) to High(TMsgDlgBtn) do
begin
if CurBtn in Buttons then
begin
ReallocMem(Result, (ButtonCount + 1) * SizeOf(Longint));
Result[ButtonCount] := ButtonIds[CurBtn];
if DefaultButton = CurBtn then
DefaultIndex := ButtonCount;
Inc(ButtonCount)
end;
end;
end;
function MessageDlg(const aMsg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint): TModalResult;
var
DefaultIndex,
CancelValue,
ButtonCount : Longint;
Btns : PLongint;
begin
Btns := GetPromptUserButtons(Buttons, CancelValue, DefaultIndex, ButtonCount,
False, mbYes);
Result := DialogResults[PromptUser(LineBreaksToSystemLineBreaks(aMsg),
DialogIds[DlgType], Btns, ButtonCount, DefaultIndex, CancelValue)];
ReallocMem(Btns, 0);
end;
function MessageDlg(const aCaption, aMsg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint): TModalResult;
var
DefaultIndex,
CancelValue,
ButtonCount : Longint;
Btns : PLongint;
begin
Btns := GetPromptUserButtons(Buttons, CancelValue, DefaultIndex, ButtonCount,
False, mbYes);
Result := DialogResults[PromptUser(aCaption, LineBreaksToSystemLineBreaks(aMsg),
DialogIds[DlgType], Btns, ButtonCount, DefaultIndex, CancelValue)];
ReallocMem(Btns, 0);
end;
function MessageDlg(const aCaption, aMsg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint; DefaultButton: TMsgDlgBtn): TModalResult;
var
DefaultIndex,
CancelValue,
ButtonCount : Longint;
Btns : PLongint;
begin
Btns := GetPromptUserButtons(Buttons, CancelValue, DefaultIndex, ButtonCount,
True, DefaultButton);
Result := DialogResults[PromptUser(aCaption, LineBreaksToSystemLineBreaks(aMsg),
DialogIds[DlgType], Btns, ButtonCount, DefaultIndex, CancelValue)];
ReallocMem(Btns, 0);
end;
function MessageDlg(const aMsg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint; DefaultButton: TMsgDlgBtn
): TModalResult;
begin
Result := MessageDlg('', aMsg, DlgType, Buttons, HelpCtx, DefaultButton);
end;
function MessageDlg(const aCaption, aMsg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; const HelpKeyword: string): TModalResult;
begin
// TODO: handle HelpKeyword
Result:=MessageDlg(aCaption, aMsg, DlgType, Buttons, 0);
end;
function MessageDlgPos(const aMsg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint; X, Y: Integer): TModalResult;
var
DefaultIndex,
CancelValue,
ButtonCount : Longint;
Btns : PLongint;
begin
Btns := GetPromptUserButtons(Buttons, CancelValue, DefaultIndex, ButtonCount,
False, mbYes);
Result := DialogResults[PromptUserAtXY(LineBreaksToSystemLineBreaks(aMsg),
DialogIds[DlgType], Btns, ButtonCount, DefaultIndex, CancelValue, X, Y)];
ReallocMem(Btns, 0);
end;
function MessageDlgPosHelp(const aMsg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint; X, Y: Integer;
const HelpFileName: string): TModalResult;
begin
DebugLn ('MessageDlgPosHelp ****** NOT YET FULLY IMPLEMENTED ********');
//TODO: set helpcontext and helpfile
result := MessageDlgPos(aMsg, DlgType, buttons, helpctx, X, Y);
end;
procedure ShowMessage(const aMsg: string);
begin
NotifyUser(LineBreaksToSystemLineBreaks(aMsg), idDialogBase);
end;
procedure ShowMessageFmt(const aMsg: string; Params: array of const);
begin
NotifyUser(LineBreaksToSystemLineBreaks(Format(aMsg, Params)), idDialogBase);
end;
procedure ShowMessagePos(const aMsg: string; X, Y: Integer);
begin
NotifyUserAtXY(LineBreaksToSystemLineBreaks(aMsg), idDialogBase, X, Y);
end;
//----------------------------------------------------------------------------//
//-----------------------Prompt User For Information--------------------------//
function InputBox(const ACaption, APrompt, ADefault : String) : String;
begin
Result := ADefault;
InputQuery(ACaption, APrompt, Result);
end;
function PasswordBox(const ACaption, APrompt : String) : String;
begin
Result := '';
InputQuery(ACaption, APrompt, True, Result);
end;
procedure DialogCopyToClipboard(Self, Sender: TObject; var Key: Word;
Shift: TShiftState);
var
S: string;
Dlg: TCustomForm;
Cnt, LastCnt: TControl;
begin
if not ((Key in [VK_C, VK_INSERT]) and (Shift = [ssModifier])) then
Exit;
Dlg := Self as TCustomForm;
S := Format('[%s]', [Dlg.Caption]) + sLineBreak;
LastCnt := nil;
if Dlg is TCustomCopyToClipboardDialog then
S := S + sLineBreak + TCustomCopyToClipboardDialog(Dlg).GetMessageText + sLineBreak;
for Cnt in Dlg.GetEnumeratorControls do
begin
if (Cnt is TCustomLabel) then
begin
S := S + sLineBreak + Cnt.Caption + sLineBreak;
LastCnt := nil;
end else
begin
if (LastCnt=nil) or (LastCnt.Top > Cnt.Top) then
S := S + sLineBreak+sLineBreak
else
S := S + ' ';
S := S + Format('[%s]', [StripHotKey(Cnt.Caption)]);
LastCnt := Cnt;
end;
end;
Clipboard.AsText := TrimRight(S);
end;
procedure RegisterDialogForCopyToClipboard(const ADlg: TCustomForm);
var
Mtd: TMethod;
begin
ADlg.KeyPreview := True;
Mtd.Code := @DialogCopyToClipboard;
Mtd.Data := ADlg;
ADlg.AddHandlerOnKeyDown(TKeyEvent(Mtd));
end;
function SelectDirectory(const Caption, InitialDirectory: string;
out Directory: string): boolean;
begin
Result:=SelectDirectory(Caption,InitialDirectory,Directory,false);
end;
function SelectDirectory(const Caption, InitialDirectory: string;
out Directory: string; ShowHidden: boolean; HelpCtx: Longint): boolean;
var
SelectDirectoryDialog: TSelectDirectoryDialog;
begin
SelectDirectoryDialog:=TSelectDirectoryDialog.Create(nil);
try
if ShowHidden then
SelectDirectoryDialog.Options:=SelectDirectoryDialog.Options
+[ofForceShowHidden];
SelectDirectoryDialog.InitialDir:=InitialDirectory;
SelectDirectoryDialog.Title:=Caption;
SelectDirectoryDialog.HelpContext:=HelpCtx;
Result:=SelectDirectoryDialog.Execute;
if Result then
Directory:=SelectDirectoryDialog.Filename
else
Directory:='';
finally
SelectDirectoryDialog.Free;
end;
end;
function SelectDirectory(out Directory: string;
Options: TSelectDirOpts; HelpCtx: Longint): Boolean;
var
SelectDirectoryDialog: TSelectDirectoryDialog;
begin
SelectDirectoryDialog:=TSelectDirectoryDialog.Create(nil);
// TODO: sdAllowCreate,
// TODO: sdPrompt
try
SelectDirectoryDialog.HelpContext:=HelpCtx;
Result:=SelectDirectoryDialog.Execute;
if Result then begin
Directory:=SelectDirectoryDialog.Filename;
if (sdPerformCreate in Options) and (not DirPathExists(Directory)) then
ForceDirectoriesUTF8(Directory);
end else
Directory:='';
finally
SelectDirectoryDialog.Free;
end;
end;
function InputQuery(const ACaption, APrompt : String; MaskInput : Boolean;
var Value : String) : Boolean;
begin
Result := LCLIntf.RequestInput(ACaption, LineBreaksToSystemLineBreaks(APrompt),
MaskInput, Value);
end;
function InputQuery(const ACaption, APrompt : String; var Value : String) : Boolean;
begin
Result := InputQuery(ACaption, APrompt, False, Value);
end;
{ TDummyForInput }
type
TDummyEditList = array of TEdit;
PDummyEditList = ^TDummyEditList;
TDummyForInput = class(TForm)
public
FEditsPtr: PDummyEditList;
FOnCloseEvent: TInputCloseQueryEvent;
procedure FOnClick(Sender: TObject);
end;
procedure TDummyForInput.FOnClick(Sender: TObject);
var
Cfm: boolean;
Str: array of string;
i: integer;
begin
Cfm:= true;
if Assigned(FOnCloseEvent) then
begin
SetLength(Str, Length(FEditsPtr^));
for i:= 0 to Length(Str)-1 do
Str[i]:= FEditsPtr^[i].Text;
FOnCloseEvent(nil, Str, Cfm);
end;
if Cfm then
ModalResult:= mrOk;
end;
function InputQuery(const ACaption: string; const APrompts: array of string;
var AValues: array of string; ACloseEvent: TInputCloseQueryEvent): boolean;
var
FPanels: array of TPanel;
FEdits: array of TEdit;
FLabels: array of TPanel;
FButtons: TButtonPanel;
FForm: TDummyForInput;
Len, NSpacing, NEditWidth, i: integer;
function GetPromptCaption(const APrompt: string): string;
begin
Result:= APrompt;
if (Result<>'') and (Result[1]<' ') then
Delete(Result, 1, 1);
end;
function GetPasswordChar(const APrompt: string): Char;
begin
if (APrompt<>'') and (APrompt[1]<' ') then
Result := '*'
else
Result := #0;
end;
begin
Result:= false;
if Length(APrompts)<1 then
raise EInvalidOperation.Create('InputQuery: prompt array cannot be empty');
if Length(APrompts)>Length(AValues) then
raise EInvalidOperation.Create('InputQuery: prompt array length must be <= value array length');
Len:= Length(AValues);
SetLength(FPanels, Len);
SetLength(FLabels, Len);
SetLength(FEdits, Len);
FForm:= TDummyForInput.CreateNew(nil);
try
FForm.Width:= FForm.Scale96ToForm(600);
FForm.Height:= FForm.Scale96ToForm(400);
FForm.BorderStyle:= bsDialog;
FForm.Position:= poScreenCenter;
FForm.Caption:= ACaption;
FForm.FOnCloseEvent:= ACloseEvent;
NSpacing:= FForm.Scale96ToForm(cInputQuerySpacingSize);
NEditWidth:= Max(
FForm.Scale96ToForm(cInputQueryEditSizePixels),
_InputQueryActiveMonitor.Width * cInputQueryEditSizePercents div 100);
FButtons:= TButtonPanel.Create(FForm);
FButtons.Parent:= FForm;
FButtons.ShowButtons:= [pbOK, pbCancel];
FButtons.ShowBevel:= false;
FButtons.OKButton.OnClick:= @FForm.FOnClick;
FButtons.OKButton.ModalResult:= mrNone;
for i:= 0 to Len-1 do
begin
FPanels[i]:= TPanel.Create(FForm);
FPanels[i].Parent:= FForm;
FPanels[i].Align:= alTop;
FPanels[i].BevelInner:= bvNone;
FPanels[i].BevelOuter:= bvNone;
FPanels[i].AutoSize:= true;
FPanels[i].BorderSpacing.Around:= NSpacing;
//fix order of panels
if i>0 then
FPanels[i].Top:= FPanels[i-1].Top+10;
FEdits[i]:= TEdit.Create(FForm);
FEdits[i].Parent:= FPanels[i];
FEdits[i].Align:= alRight;
FEdits[i].Width:= NEditWidth;
FEdits[i].Text:= AValues[i];
if i<Length(APrompts) then
FEdits[i].PasswordChar:= GetPasswordChar(APrompts[i]);
FLabels[i]:= TPanel.Create(FForm);
FLabels[i].Parent:= FPanels[i];
FLabels[i].Align:= alRight;
FLabels[i].BevelInner:= bvNone;
FLabels[i].BevelOuter:= bvNone;
if i<Length(APrompts) then
FLabels[i].Caption:= GetPromptCaption(APrompts[i]);
FLabels[i].BorderSpacing.Right:= NSpacing;
FLabels[i].Width:= FLabels[i].Canvas.TextWidth(FLabels[i].Caption);
FEdits[i].Left:= FForm.Width; // place edits to right
end;
FButtons.Align:= alTop;
FButtons.Top:= FPanels[Len-1].Top+10; // place buttons to bottom
FForm.AutoSize:= true;
FForm.ActiveControl:= FEdits[0];
FForm.FEditsPtr:= @FEdits;
Result:= FForm.ShowModal=mrOk;
if Result then
for i:= 0 to Len-1 do
AValues[i]:= FEdits[i].Text;
finally
FreeAndNil(FForm);
end;
end;
// included by dialogs.pp
|