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 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
|
unit LazDialogs;
{$mode objfpc}{$H+}
interface
uses
// RTL
Classes, SysUtils, math,
// LCL
Graphics, Forms, ShellCtrls, Buttons, StdCtrls, ExtCtrls, FileCtrl, ComCtrls,
Dialogs, ButtonPanel, LCLStrConsts, FileUtil, Controls;
type
TLazFileDialogKind = (
ldkOpenDesktop, ldkSaveDesktop, ldkOpenPDA, ldkSavePDA, ldkSelectDirectory);
{ TLazarusFileDialogForm }
TLazarusFileDialogForm = class(TForm)
private
FKind: TLazFileDialogKind;
procedure SetFilter(AFilter: string);
public
// User interface
ButtonPanel: TButtonPanel;
ShellTreeView: TShellTreeView;
ShellListView: TShellListView;
SaveEdit: TEdit;
FilterComboBox: TFilterComboBox;
// input/output
FileName: string;
Filter: string;
InitialDir: string;
Title: string;
//
constructor CreateNew(AOwner: TComponent; Num: Integer = 0); override;
procedure Initialize(AKind: TLazFileDialogKind);
procedure HandleOkClick(ASender: TObject);
procedure HandleCancelClick(ASender: TObject);
procedure HandleCloseQuery(Sender : TObject; var CanClose : boolean);
procedure HandleEditChange(ASender: TObject);
procedure HandleSelectItem(Sender: TObject;
Item: TListItem; Selected: Boolean);
procedure HandleTreeViewSelectionChanged(ASender: TObject);
end;
{ TLazOpenDialog }
TLazOpenDialog = class(TOpenDialog)
protected
FForm: TLazarusFileDialogForm;
class procedure WSRegisterClass; override;
function DoExecute: boolean; override;
procedure DoInitialize; virtual;
public
constructor Create(TheOwner: TComponent); override;
end;
{ TLazSaveDialog }
TLazSaveDialog = class(TLazOpenDialog)
protected
procedure DoInitialize; override;
end;
{ TLazSelectDirectoryDialog }
TLazSelectDirectoryDialog = class(TLazOpenDialog)
protected
procedure DoInitialize; override;
end;
{ TLazMessageDialog }
TLazMessageDialog = class(TForm)
private
Image1: TImage;
Label1: TLabel; // we need a TLabel to be able to resize it properly
btnList: array [0..11] of TBitBtn;
NumButtons: Integer;
public
constructor CreateNew(TheOwner: TComponent; Num: Integer = 0); override;
end;
function LazMessageDlg(const aCaption, aMsg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint): Integer;
function LazMessageDlg(const aMsg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint): Integer;
implementation
var
{ Declared here for the time being to make it possibly work with LCLCustodrawn}
LazMessageDialog: TLazMessageDialog;
{ TLazarusFileDialogForm }
procedure TLazarusFileDialogForm.SetFilter(AFilter: string);
begin
if AFilter = '' then
FilterComboBox.Filter := Format(rsAllFiles,[GetAllFilesMask, GetAllFilesMask,''])
else
FilterComboBox.Filter := AFilter;
end;
{
The size of the window is determined only when creating the
handle, so any reference to TForm.Width and TForm.Height
here doesnt correspond to the final value.
}
constructor TLazarusFileDialogForm.CreateNew(AOwner: TComponent; Num: Integer = 0);
begin
inherited CreateNew(AOwner, Num);
Self.Position := poScreenCenter;
end;
procedure TLazarusFileDialogForm.Initialize(AKind: TLazFileDialogKind);
begin
FKind := AKind;
ButtonPanel := TButtonPanel.Create(Self);
ButtonPanel.Parent := Self;
ButtonPanel.Left := 0;
ButtonPanel.Height := 20;
ButtonPanel.Top := Height - ButtonPanel.Height;
ButtonPanel.Width := Width;
ButtonPanel.Align := alBottom;
ButtonPanel.ShowButtons := [pbOK, pbCancel];
ButtonPanel.OKButton.OnClick := @HandleOkClick;
ButtonPanel.CancelButton.OnClick := @HandleCancelClick;
if AKind in [ldkOpenDesktop, ldkSaveDesktop, ldkOpenPDA, ldkSavePDA] then
begin
// Add the ShellTreeView to the dialog
ShellTreeView := TShellTreeView.Create(Self);
ShellTreeView.Parent := Self;
ShellTreeView.Left := 0;
ShellTreeView.Top := 0;
ShellTreeView.Width := Width;
ShellTreeView.Height := 100;
ShellTreeView.Align := alTop;
// Add the ShellListView to the dialog
ShellListView := TShellListView.Create(Self);
ShellListView.Parent := Self;
ShellListView.Left := 0;
ShellListView.Top := ShellTreeView.Height;
ShellListView.Width := Width;
ShellListView.Height := Height - ShellTreeView.Height - ButtonPanel.Height;
ShellListView.Align := alClient;
ShellListView.ShellTreeView := ShellTreeView;
ShellListView.ScrollBars := ssVertical;
ShellListView.OnSelectItem := @HandleSelectItem;
// TEdit for save dialog
if AKind in [ldkSaveDesktop, ldkSavePDA] then
begin
SaveEdit := TEdit.Create(Self);
SaveEdit.Parent := Self;
SaveEdit.Left := 0;
SaveEdit.Height := 20;
SaveEdit.Top := Height - ButtonPanel.Height - SaveEdit.Height;
SaveEdit.Width := Width;
SaveEdit.Align := alBottom;
SaveEdit.Text := SysUtils.ExtractFileName(FileName);
SaveEdit.OnChange := @HandleEditChange;
end;
// TFilterComboBox
FilterComboBox := TFilterComboBox.Create(Self);
FilterComboBox.Parent := Self;
FilterComboBox.Left := 0;
FilterComboBox.Height := 20;
FilterComboBox.Top := Height - ButtonPanel.Height - FilterComboBox.Height;
if SaveEdit <> nil then
FilterComboBox.Top := FilterComboBox.Top - SaveEdit.Height;
FilterComboBox.Width := Width;
FilterComboBox.Align := alBottom;
SetFilter(Filter);
FilterComboBox.ShellListView := ShellListView;
// In the save dialog it is enabled when there is a text in the TEdit
if AKind in [ldkSaveDesktop, ldkSavePDA] then
ButtonPanel.OKButton.Enabled := SaveEdit.Text <> ''
// In a TOpenDialog the Ok button is only enabled when a file is selected
else
ButtonPanel.OkButton.Enabled := False;
end
else if FKind = ldkSelectDirectory then
begin
// Add the ShellTreeView to the dialog
ShellTreeView := TShellTreeView.Create(Self);
ShellTreeView.Parent := Self;
ShellTreeView.Left := 0;
ShellTreeView.Top := 0;
ShellTreeView.Align := alClient;
ShellTreeView.OnSelectionChanged := @HandleTreeViewSelectionChanged;
ButtonPanel.OKButton.Enabled := False;
end;
// Form events
OnCloseQuery := @HandleCloseQuery;
end;
// The Ok button code should be only a simple mrOk,
// because there is the dialog Ok button, which will
// always be active and will set the ModalResult to mrOk
// so the code needs to affect it too, and this can be
// done in CloseQuery
procedure TLazarusFileDialogForm.HandleOkClick(ASender: TObject);
begin
ModalResult := mrOk;
end;
procedure TLazarusFileDialogForm.HandleCancelClick(ASender: TObject);
begin
ModalResult := mrCancel;
end;
procedure TLazarusFileDialogForm.HandleCloseQuery(Sender: TObject;
var CanClose: boolean);
begin
if ModalResult = mrCancel then
begin
CanClose := True;
Exit;
end;
CanClose := False;
if FKind in [ldkSaveDesktop, ldkSavePDA] then
begin
if SaveEdit.Text = '' then Exit;
FileName := ShellTreeView.GetPathFromNode(ShellTreeView.Selected);
FileName := IncludeTrailingPathDelimiter(FileName);
FileName := FileName + SaveEdit.Text;
CanClose := True;
end
else if FKind in [ldkOpenDesktop, ldkOpenPDA] then
begin
if ShellListView.Selected = nil then Exit;
FileName := ShellListView.GetPathFromItem(ShellListView.Selected);
CanClose := True;
end
else
begin
if ShellTreeView.Selected = nil then Exit;
FileName := ShellTreeView.GetPathFromNode(ShellTreeView.Selected);
CanClose := True;
end;
end;
procedure TLazarusFileDialogForm.HandleEditChange(ASender: TObject);
begin
ButtonPanel.OkButton.Enabled := SaveEdit.Text <> '';
end;
procedure TLazarusFileDialogForm.HandleSelectItem(Sender: TObject;
Item: TListItem; Selected: Boolean);
begin
// Selecting an item changes the filename in the TEdit
// in save dialogs
if (FKind in [ldkSaveDesktop, ldkSavePDA]) and Selected then
begin
SaveEdit.Text := Item.Caption;
end
// In the OpenDialog the state of the Ok button is dependent
// on the selection of an item
else
begin
ButtonPanel.OkButton.Enabled := Selected;
end;
end;
// Used only in the TLazSelectDirectoryDialog
procedure TLazarusFileDialogForm.HandleTreeViewSelectionChanged(ASender: TObject);
begin
ButtonPanel.OKButton.Enabled := True;
end;
{ TLazOpenDialog }
class procedure TLazOpenDialog.WSRegisterClass;
begin
// Do nothing, because this dialog doesn't require a WS implementation
end;
function TLazOpenDialog.DoExecute: boolean;
begin
Result := FForm.ShowModal <> mrCancel;
FileName := FForm.FileName;
end;
procedure TLazOpenDialog.DoInitialize;
begin
FForm.Initialize(ldkOpenDesktop);
end;
constructor TLazOpenDialog.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
FForm := TLazarusFileDialogForm.CreateNew(Self);
FForm.FileName := FileName;
FForm.Filter := Filter;
FForm.Title := Title;
DoInitialize;
FForm.Hide;
end;
{ TLazSaveDialog }
procedure TLazSaveDialog.DoInitialize;
begin
FForm.Initialize(ldkSaveDesktop);
end;
{ TLazSelectDirectoryDialog }
procedure TLazSelectDirectoryDialog.DoInitialize;
begin
FForm.Initialize(ldkSelectDirectory);
end;
{ Dialog Functions }
function LazMessageDlg(const aCaption, aMsg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint): Integer;
Var
I: Integer;
textWidth: Integer;
ButtonPos: Integer;
RequiredWidth: Integer;
BlankRight: Integer; // blank space at the right of last button
begin
{$ifdef LCLCustomdrawn} if not assigned(LazMessageDialog) then {$endif}
LazMessageDialog:= TLazMessageDialog.CreateNew(Application);
with LazMessageDialog do begin
Label1.Caption:= aMsg;
Label1.Parent:= LazMessageDialog;
{Select Image (and Caption) from DlgType}
case DlgType of
mtWarning: begin
Caption:= rsMtWarning;
image1.Picture.LoadFromResourceName(hInstance, 'dialog_warning', TPortableNetworkGraphic);
end;
mtError: begin
Caption:= rsMtError;
image1.Picture.LoadFromResourceName(hInstance, 'dialog_error', TPortableNetworkGraphic);
end;
mtConfirmation: begin
Caption:= rsMtConfirmation;
image1.Picture.LoadFromResourceName(hInstance, 'dialog_confirmation', TPortableNetworkGraphic);
end;
mtInformation: begin
Caption:= rsMtInformation;
image1.Picture.LoadFromResourceName(hInstance, 'dialog_information', TPortableNetworkGraphic);
end;
mtCustom: begin
Caption:= ApplicationName;
Image1.Width:= 8;
Image1.Hide;
end;
end;
Image1.Parent := LazMessageDialog;
if aCaption <> '' then //A custom dialog caption has been required
Caption:= aCaption;
Label1.Left:= Image1.Left + Image1.Width + 8;
{Select Buttons from Buttons}
if (Buttons = []) or (Buttons = [mbHelp]) then
Buttons:= Buttons + [mbOK]; // the dialog must provide a modal result
NumButtons:= 0;
{ The order of Buttons is the same as in Qt - Totally different from GTK2}
if mbHelp in Buttons then begin
btnList[NumButtons] := TBitBtn.Create(LazMessageDialog);
btnList[NumButtons].Parent := LazMessageDialog;
btnList[NumButtons].Kind:= bkHelp;
inc(NumButtons);
end;
if mbYes in Buttons then begin
btnList[NumButtons] := TBitBtn.Create(LazMessageDialog);
btnList[NumButtons].Parent := LazMessageDialog;
btnList[NumButtons].Kind:= bkYes;
inc(NumButtons);
end;
if mbYesToAll in Buttons then begin
btnList[NumButtons] := TBitBtn.Create(LazMessageDialog);
btnList[NumButtons].Parent := LazMessageDialog;
btnList[NumButtons].Kind:= bkYesToAll;
inc(NumButtons);
end;
if mbNo in Buttons then begin
btnList[NumButtons] := TBitBtn.Create(LazMessageDialog);
btnList[NumButtons].Parent := LazMessageDialog;
btnList[NumButtons].Kind:= bkNo;
inc(NumButtons);
end;
if mbNoToAll in Buttons then begin
btnList[NumButtons] := TBitBtn.Create(LazMessageDialog);
btnList[NumButtons].Parent := LazMessageDialog;
btnList[NumButtons].Kind:= bkNoToAll;
inc(NumButtons);
end;
if mbAll in Buttons then begin
btnList[NumButtons] := TBitBtn.Create(LazMessageDialog);
btnList[NumButtons].Parent := LazMessageDialog;
btnList[NumButtons].Kind:= bkAll;
inc(NumButtons);
end;
if mbOK in Buttons then begin
btnList[NumButtons] := TBitBtn.Create(LazMessageDialog);
btnList[NumButtons].Parent := LazMessageDialog;
btnList[NumButtons].Kind:= bkOK;
inc(NumButtons);
end;
if mbRetry in Buttons then begin
btnList[NumButtons] := TBitBtn.Create(LazMessageDialog);
btnList[NumButtons].Parent := LazMessageDialog;
btnList[NumButtons].Kind:= bkRetry;
inc(NumButtons);
end;
if mbIgnore in Buttons then begin
btnList[NumButtons] := TBitBtn.Create(LazMessageDialog);
btnList[NumButtons].Parent := LazMessageDialog;
btnList[NumButtons].Kind:= bkIgnore;
inc(NumButtons);
end;
if mbCancel in Buttons then begin
btnList[NumButtons] := TBitBtn.Create(LazMessageDialog);
btnList[NumButtons].Parent := LazMessageDialog;
btnList[NumButtons].Kind:= bkCancel;
inc(NumButtons);
end;
if mbAbort in Buttons then begin
btnList[NumButtons] := TBitBtn.Create(LazMessageDialog);
btnList[NumButtons].Parent := LazMessageDialog;
btnList[NumButtons].Kind:= bkAbort;
inc(NumButtons);
end;
if mbClose in Buttons then begin
btnList[NumButtons] := TBitBtn.Create(LazMessageDialog);
btnList[NumButtons].Parent := LazMessageDialog;
btnList[NumButtons].Kind:= bkClose;
inc(NumButtons);
end;
ButtonPos:= Image1.Left;
for I:= 0 to NumButtons -1 do begin
btnList[I].Constraints.MinHeight:= 25;
btnList[I].Constraints.MinWidth:= 75;
btnList[I].Left:= ButtonPos;
btnList[I].Top:= Image1.Top + Image1.Height + 10;
{next line is required because even if Auyosize is true,
width property is changed only when the button is
painted. Either we wait (but Application.ProcessMessages is not enough)
or we force the actual width. We may safely use form canvas, because our
components inherit the font from the form (ParentFont = true by default)}
btnList[I].Width:= LazMessageDialog.Canvas.TextExtent(btnList[I].Caption).cx
+ btnList[I].Glyph.Width + 20;
btnList[I].Visible:= True;
ButtonPos:= ButtonPos + btnList[I].Width + 8;
end;
{ See comment above for width property. Static Text apparently doesn't behave
properly when Text is changed at run time. The width is set as appropriate, but
the text is written only up to the previous width. Therefore we must use a TLabel}
textWidth:= LazMessageDialog.Canvas.TextExtent(Label1.Caption).cx;
Label1.Width:= textWidth;
textWidth:= label1.Left + label1.Width;
RequiredWidth:= Max(textWidth,ButtonPos);
Width := RequiredWidth + 10;
Height:= btnList[0].Top + btnList[0].Height + 10;
// now let's move our buttons to the right side of dialog, if appropriate
BlankRight:= Width - ButtonPos;
if BlankRight > 10 then
for I:= 0 to NumButtons-1 do btnList[I].Left:= btnList[I].Left+BlankRight ;
end;
result := LazMessageDialog.ShowModal;
{$ifndef LCLCustomdrawn}LazMessageDialog.Release;{$endif}
end;
function LazMessageDlg(const aMsg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint): Integer;
begin
result := LazMessageDlg('',aMsg,DlgType,Buttons,HelpCtx);
end;
{ TLazMessageDialog }
constructor TLazMessageDialog.CreateNew(TheOwner: TComponent; Num: Integer = 0);
begin
inherited CreateNew(TheOwner);
FormStyle:= fsStayOnTop;
Position:= poMainFormCenter;
Image1 := TImage.Create(Self);
Image1.Top:= 10;
Image1.Left:= 10;
Image1.Width:= 48;
Image1.Height:= 48;
Label1 := TLabel.Create(Self);
Label1.Top:= Image1.Top;
Label1.Left:= Image1.Left + Image1.Width + 10;
Label1.Caption:= 'Label1';
Width:= Image1.Width + Label1.Width + 20;
Height:= Image1.Height + 20;
end;
end.
|