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 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
|
{
*****************************************************************************
* QtWSDialogs.pp *
* -------------- *
* *
* *
*****************************************************************************
*****************************************************************************
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.
*****************************************************************************
}
unit QtWSDialogs;
{$mode objfpc}{$H+}
interface
{$I qtdefines.inc}
uses
// Libs
qt6,
qtobjects, qtwidgets, qtproc,
// RTL + LCL
SysUtils, Classes, LCLType, LazUTF8, LazFileUtils, Dialogs, Controls, Forms, Graphics,
// Widgetset
WSDialogs, WSLCLClasses;
type
{ TQtWSCommonDialog }
TQtWSCommonDialog = class(TWSCommonDialog)
private
protected
class function GetDialogParent(const ACommonDialog: TCommonDialog): QWidgetH;
published
class function CreateHandle(const ACommonDialog: TCommonDialog): TLCLHandle; override;
class procedure DestroyHandle(const ACommonDialog: TCommonDialog); override;
class procedure ShowModal(const ACommonDialog: TCommonDialog); override;
end;
{ TQtWSFileDialog }
TQtWSFileDialog = class(TWSFileDialog)
private
protected
class function GetQtFilterString(const AFileDialog: TFileDialog;
var ASelectedFilter: WideString): WideString;
class procedure UpdateProperties(const AFileDialog: TFileDialog; QtFileDialog: TQtFileDialog);
published
class function CreateHandle(const ACommonDialog: TCommonDialog): TLCLHandle; override;
class procedure ShowModal(const ACommonDialog: TCommonDialog); override;
end;
{ TQtWSOpenDialog }
TQtWSOpenDialog = class(TWSOpenDialog)
published
class function CreateHandle(const ACommonDialog: TCommonDialog): TLCLHandle; override;
class function QueryWSEventCapabilities(const ACommonDialog: TCommonDialog): TCDWSEventCapabilities; override;
end;
{ TQtWSSaveDialog }
TQtWSSaveDialog = class(TWSSaveDialog)
published
class function QueryWSEventCapabilities(const ACommonDialog: TCommonDialog): TCDWSEventCapabilities; override;
end;
{ TQtWSSelectDirectoryDialog }
TQtWSSelectDirectoryDialog = class(TWSSelectDirectoryDialog)
protected
class procedure UpdateProperties(const AFileDialog: TSelectDirectoryDialog; QtFileDialog: TQtFileDialog);
published
class function CreateHandle(const ACommonDialog: TCommonDialog): TLCLHandle; override;
class procedure ShowModal(const ACommonDialog: TCommonDialog); override;
class function QueryWSEventCapabilities(const ACommonDialog: TCommonDialog): TCDWSEventCapabilities; override;
end;
{ TQtWSColorDialog }
TQtWSColorDialog = class(TWSColorDialog)
published
class function CreateHandle(const ACommonDialog: TCommonDialog): TLCLHandle; override;
class procedure ShowModal(const ACommonDialog: TCommonDialog); override;
class function QueryWSEventCapabilities(const ACommonDialog: TCommonDialog): TCDWSEventCapabilities; override;
end;
{ TQtWSColorButton }
TQtWSColorButton = class(TWSColorButton)
published
end;
{ TQtWSFontDialog }
TQtWSFontDialog = class(TWSFontDialog)
published
class function CreateHandle(const ACommonDialog: TCommonDialog): TLCLHandle; override;
class procedure ShowModal(const ACommonDialog: TCommonDialog); override;
class function QueryWSEventCapabilities(const ACommonDialog: TCommonDialog): TCDWSEventCapabilities; override;
end;
implementation
uses ExtDlgs, qtint;
{$ifndef QT_NATIVE_DIALOGS}
const
QtDialogCodeToModalResultMap: array[QDialogDialogCode] of TModalResult =
(
{QDialogRejected} mrCancel,
{QDialogAccepted} mrOk
);
{$endif}
{ TQtWSSaveDialog }
class function TQtWSSaveDialog.QueryWSEventCapabilities(
const ACommonDialog: TCommonDialog): TCDWSEventCapabilities;
begin
Result := [cdecWSNoCanCloseSupport];
end;
{ TQtWSCommonDialog }
class function TQtWSCommonDialog.GetDialogParent(const ACommonDialog: TCommonDialog): QWidgetH;
begin
if ACommonDialog.Owner is TWinControl then
Result := TQtWidget(TWinControl(ACommonDialog.Owner).Handle).Widget
else
if (QtWidgetSet.GetActiveWindow <> 0) then
Result := TQtWidget(QtWidgetSet.GetActiveWindow).Widget
else
if Assigned(Application.MainForm) and Application.MainForm.Visible then
Result := TQtWidget(Application.MainForm.Handle).Widget
else
Result := nil;
{$IFDEF MSWINDOWS}
if Assigned(Result) then
begin
if (QWidget_windowFlags(Result) and QtTool <> 0) or
(QWidget_windowFlags(Result) and QtDrawer <> 0) or
(QWidget_windowFlags(Result) and QtSheet <> 0) or
(QWidget_windowFlags(Result) and QtPopup <> 0) then
Result := QApplication_desktop;
end;
{$ENDIF}
end;
{------------------------------------------------------------------------------
Function: TQtWSCommonDialog.CreateHandle
Params: None
Returns: Nothing
Dummy handle creator. On Qt we don't need a Handle for common dialogs
------------------------------------------------------------------------------}
class function TQtWSCommonDialog.CreateHandle(const ACommonDialog: TCommonDialog): TLCLHandle;
begin
Result := TLCLHandle(TQtDialog.Create(ACommonDialog, GetDialogParent(ACommonDialog)));
TQtDialog(Result).AttachEvents;
end;
{------------------------------------------------------------------------------
Function: TQtWSCommonDialog.DestroyHandle
Params: None
Returns: Nothing
Dummy handle destructor. On Qt we don't need a Handle for common dialogs
------------------------------------------------------------------------------}
class procedure TQtWSCommonDialog.DestroyHandle(const ACommonDialog: TCommonDialog);
begin
if ACommonDialog.HandleAllocated then
TQtDialog(ACommonDialog.Handle).Release;
end;
class procedure TQtWSCommonDialog.ShowModal(const ACommonDialog: TCommonDialog);
begin
TQtDialog(ACommonDialog.Handle).exec;
end;
{ TQtWSFileDialog }
class function TQtWSFileDialog.GetQtFilterString(const AFileDialog: TFileDialog;
var ASelectedFilter: WideString): WideString;
const
FULLWIDTH_LEFT_PARENTHESIS_UTF8 = #$EF#$BC#$88;
FULLWIDTH_RIGHT_PARENTHESIS_UTF8 = #$EF#$BC#$89;
function ReplaceExtensionDelimiter(const ASource: String): String; inline;
begin
// replace *.ext1;*.ext2 by *.ext1 *.ext2
Result := StringReplace(ASource, ';', ' ', [rfReplaceAll]);
end;
function GetExtensionString(const ASource: String): String; inline;
begin
Result := '(' + ReplaceExtensionDelimiter(ASource) + ')';
end;
var
TmpFilter, strExtensions, DialogFilter, S, S1: string;
ParserState, Position, i, L: Integer;
List: TStrings;
begin
{------------------------------------------------------------------------------
This is a parser that converts LCL filter strings to Qt filter strings
The parses states are:
0 - Initial state, is reading a string to be displayed on the filter
1 - Is reading the extensions to be filtered
2 - Reached the end of extensions text, now it will write
A LCL filter string looks like this:
Text files (*.txt *.pas)|*.txt *.pas|Binaries (*.exe)|*.exe
And a Qt filter string looks like this
Text files (*.txt *.pas)
Binaries (*.exe)
The following LCL filter simply cannot be represented under Qt, because Qt
always appends a string with the extensions on the combo box
Text files|*.txt *.pas|Binaries|*.exe
To solve this this algorithm will try to find (*.txt) or similar on the display text
and will remove it. This algorithm is far from perfect and may cause trouble on some
special cases, but should work 99% of the time.
------------------------------------------------------------------------------}
ParserState := 0;
Position := 1;
TmpFilter := AFileDialog.Filter;
ASelectedFilter := '';
DialogFilter := TmpFilter;
TmpFilter := '';
List := TStringList.Create;
try
S1 := '';
L := Length(DialogFilter);
for i := 1 to L + 1 do
begin
if (i = L + 1) or (DialogFilter[i] = '|') then
begin
ParserState := ParserState + 1;
S := Copy(DialogFilter, Position, i - Position);
if ParserState = 1 then
begin
S := StringReplace(S, ' (', FULLWIDTH_LEFT_PARENTHESIS_UTF8, []);
S := StringReplace(S, '(', FULLWIDTH_LEFT_PARENTHESIS_UTF8, []);
S := StringReplace(S, ')', FULLWIDTH_RIGHT_PARENTHESIS_UTF8, []);
S1 := S;
List.Add(S1);
TmpFilter := TmpFilter + S1;
end else
//if ParserState = 2 then
begin
strExtensions := GetExtensionString(S);
List.Strings[List.Count - 1] := S1 + ' ' + strExtensions;
TmpFilter := TmpFilter + ' ' + strExtensions;
if i <> L + 1 then
TmpFilter := TmpFilter + ';;';
ParserState := 0;
end;
Position := i + 1;
end;
end;
// Remember that AFileDialog.FilterIndex is a 1-based index and that
// List has a zero-based index
if (AFileDialog.FilterIndex > 0) and (List.Count >= AFileDialog.FilterIndex) then
ASelectedFilter := List{%H-}.Strings[AFileDialog.FilterIndex - 1]
else
if (List.Count > 0) then
ASelectedFilter := List{%H-}.Strings[0];
finally
List.Free;
end;
if (AFileDialog is TSaveDialog) and (trim(TmpFilter)='()') then
Result := ''
else
Result := {%H-}TmpFilter;
end;
class procedure TQtWSFileDialog.UpdateProperties(
const AFileDialog: TFileDialog; QtFileDialog: TQtFileDialog);
var
ATitle: WideString;
{$ifndef QT_NATIVE_DIALOGS}
AInitDir: WideString;
{$ENDIF}
s: String;
begin
ATitle := AFileDialog{%H-}.Title;
QtFileDialog.setWindowTitle(@ATitle);
{$ifndef QT_NATIVE_DIALOGS}
s := AFileDialog.InitialDir;
if UTF8Pos('$HOME', S) > 0 then
begin
{$IFDEF MSWINDOWS}
AInitDir := GetEnvironmentVariableUTF8('HOMEDRIVE') +
GetEnvironmentVariableUTF8('HOMEPATH');
{$ELSE}
AInitDir := UTF8ToUTF16(GetEnvironmentVariableUTF8('HOME'));
{$ENDIF}
s := StringReplace(S,'$HOME', UTF8Encode(AInitDir),[rfReplaceAll]);
end else
if (S = '') then
S := GetCurrentDirUTF8;
if not DirectoryExistsUTF8(S) then
S := GetCurrentDirUTF8;
QtFileDialog.setDirectory(UTF8ToUTF16(S));
{$else}
s := AFileDialog.InitialDir;
if S = '' then
S := GetCurrentDirUTF8;
if DirectoryExistsUTF8(S) then
QtFileDialog.setDirectory(S);
{$endif}
QtFileDialog.setHistory(AFileDialog.HistoryList);
QtFileDialog.setFilter(GetQtFilterString(AFileDialog, ATitle));
QtFileDialog.setSelectedFilter(ATitle);
QtFileDialog.setConfirmOverwrite(ofOverwritePrompt in TOpenDialog(AFileDialog).Options);
QtFileDialog.setReadOnly(ofReadOnly in TOpenDialog(AFileDialog).Options);
QtFileDialog.setSizeGripEnabled(ofEnableSizing in TOpenDialog(AFileDialog).Options);
if ofViewDetail in TOpenDialog(AFileDialog).Options then
QtFileDialog.setViewMode(QFileDialogDetail)
else
QtFileDialog.setViewMode(QFileDialogList);
if ofAllowMultiSelect in TOpenDialog(AFileDialog).Options then
QtFileDialog.setFileMode(QFileDialogExistingFiles)
else
if ofFileMustExist in TOpenDialog(AFileDialog).Options then
QtFileDialog.setFileMode(QFileDialogExistingFile)
else
QtFileDialog.setFileMode(QFileDialogAnyFile);
if (AFileDialog.FileName <> '') and
not DirectoryExistsUTF8(AFileDialog.FileName) then
begin
ATitle := AFileDialog{%H-}.FileName;
if (AFileDialog is TSaveDialog) or FileExistsUTF8(AFileDialog.FileName) then
QFileDialog_selectFile(QFileDialogH(QtFileDialog.Widget), @ATitle);
{$ifndef QT_NATIVE_DIALOGS}
if (AFileDialog is TOpenPictureDialog) then
TQtFilePreviewDialog(QtFileDialog).CurrentChangedEvent(@ATitle);
{$ENDIF}
end;
{$ifndef QT_NATIVE_DIALOGS}
// set kbd shortcuts in case when we are not native dialog.
QtFileDialog.setShortcuts(AFileDialog is TOpenDialog);
{$endif}
end;
class function TQtWSFileDialog.CreateHandle(const ACommonDialog: TCommonDialog): TLCLHandle;
var
FileDialog: TQtFileDialog;
begin
FileDialog := TQtFileDialog.Create(ACommonDialog, TQtWSCommonDialog.GetDialogParent(ACommonDialog));
{$ifdef darwin}
QWidget_setWindowFlags(FileDialog.Widget, QtDialog or QtWindowSystemMenuHint or QtCustomizeWindowHint);
{$endif}
QFileDialog_setOption(QFileDialogH(FileDialog.Widget),
QFileDialogOptionDontUseNativeDialog, False);
FileDialog.AttachEvents;
Result := TLCLHandle(FileDialog);
end;
{------------------------------------------------------------------------------
Function: TQtWSFileDialog.ShowModal
Params: None
Returns: Nothing
------------------------------------------------------------------------------}
class procedure TQtWSFileDialog.ShowModal(const ACommonDialog: TCommonDialog);
var
ReturnText: WideString;
FileDialog: TFileDialog;
ReturnList: QStringListH;
i: integer;
QtFileDialog: TQtFileDialog;
{$ifdef QT_NATIVE_DIALOGS}
selectedFilter, saveFileName, saveFilter, saveTitle, sDir: WideString;
Flags: Cardinal;
{$endif}
ActiveWin: HWND;
s: string;
begin
{------------------------------------------------------------------------------
Initialization of variables
------------------------------------------------------------------------------}
ReturnText := '';
FileDialog := TFileDialog(ACommonDialog);
QtFileDialog := TQtFileDialog(FileDialog.Handle);
UpdateProperties(FileDialog, QtFileDialog);
{$ifdef QT_NATIVE_DIALOGS}
sDir := '';
if UTF8Pos('$HOME', FileDialog.InitialDir) > 0 then
begin
sDir := FileDialog.InitialDir;
{$IFDEF MSWINDOWS}
saveFileName := GetEnvironmentVariableUTF8('HOMEDRIVE') +
GetEnvironmentVariableUTF8('HOMEPATH');
{$ELSE}
saveFileName := GetEnvironmentVariableUTF8('HOME');
{$ENDIF}
sDir := StringReplace(sDir,'$HOME', UTF8Encode(saveFileName),[rfReplaceAll]);
saveFileName := GetUTF8String(SDir) + PathDelim + ExtractFileName(FileDialog.Filename);
end;
{$endif}
{------------------------------------------------------------------------------
Code to call the dialog
------------------------------------------------------------------------------}
if (FileDialog is TSaveDialog) or (FileDialog is TSavePictureDialog) then
QtFileDialog.setAcceptMode(QFileDialogAcceptSave)
else
if (FileDialog is TOpenDialog) or (FileDialog is TOpenPictureDialog) then
QtFileDialog.setAcceptMode(QFileDialogAcceptOpen)
else
if ACommonDialog is TSelectDirectoryDialog then
begin
QtFileDialog.setFileMode(QFileDialogDirectory);
QFileDialog_setOption(QFileDialogH(QtFileDialog.Widget), QFileDialogOptionShowDirsOnly, True);
end;
ActiveWin := QtWidgetSet.GetActiveWindow;
if ACommonDialog is TSaveDialog then
begin
{$ifdef QT_NATIVE_DIALOGS}
selectedFilter:='';
saveFilter := GetQtFilterString(TSaveDialog(ACommonDialog), selectedFilter);
if sDir = '' then
sDir := FileDialog.InitialDir;
if (SDir <> '') and (SDir[length(SDir)] <> PathDelim) then
SDir := SDir + PathDelim;
if (FileDialog.FileName <> '') and
(ExtractFileName(FileDialog.FileName) <> FileDialog.FileName) then
saveFileName := GetUtf8String(FileDialog.Filename)
else
saveFileName := GetUtf8String(SDir+FileDialog.Filename);
saveTitle := GetUTF8String(FileDialog.Title);
Flags := 0;
if not (ofOverwritePrompt in TSaveDialog(FileDialog).Options) then
Flags := Flags or QFileDialogOptionDontConfirmOverwrite;
{$IFDEF HASX11}
Clipboard.BeginX11SelectionLock;
try
{$ENDIF}
QFileDialog_getSaveFileName(@ReturnText,
QWidget_parentWidget(QtFileDialog.Widget), @SaveTitle, @saveFileName,
@saveFilter, @selectedFilter, Flags);
{$IFDEF HASX11}
finally
Clipboard.EndX11SelectionLock;
end;
{$ENDIF}
if ReturnText <> '' then
begin
{$ifdef MSWINDOWS}
s := UTF16ToUTF8(ReturnText);
s := StringReplace(s, '/','\', [rfReplaceAll]);
FileDialog.FileName := s;
{$else}
FileDialog.FileName := UTF16ToUTF8(ReturnText);
{$endif}
FileDialog.UserChoice := mrOK;
end else
FileDialog.UserChoice := mrCancel;
{$else}
{$IFDEF HASX11}
Clipboard.BeginX11SelectionLock;
{$ENDIF}
QFileDialog_setOption(QFileDialogH(QtFileDialog.Widget),
QFileDialogOptionDontConfirmOverwrite,
not (ofOverwritePrompt in TSaveDialog(FileDialog).Options));
FileDialog.UserChoice := QtDialogCodeToModalResultMap[QDialogDialogCode(QtFileDialog.exec)];
ReturnList := QStringList_create;
try
QtFileDialog.selectedFiles(ReturnList);
FileDialog.Files.Clear;
for i := 0 to QStringList_size(ReturnList) - 1 do
begin
QStringList_at(ReturnList, @ReturnText, i);
{$ifdef MSWINDOWS}
s := UTF16ToUTF8(ReturnText);
s := StringReplace(s, '/','\', [rfReplaceAll]);
FileDialog.Files.Add(s);
if i = 0 then
FileDialog.FileName := s;
{$else}
FileDialog.Files.Add(UTF16ToUTF8(ReturnText));
if i = 0 then
FileDialog.FileName := UTF16ToUTF8(ReturnText);
{$endif}
end;
// ReturnText := FileDialog.Files.Text;
finally
QStringList_destroy(ReturnList);
end;
{$IFDEF HASX11}
Clipboard.EndX11SelectionLock;
{$ENDIF}
{$endif}
end else
begin
{$ifdef QT_NATIVE_DIALOGS}
saveFilter := GetQtFilterString(TOpenDialog(ACommonDialog), selectedFilter);
if sDir = '' then
sDir := FileDialog.InitialDir;
if (SDir <> '') and (SDir[length(SDir)] <> PathDelim) then
SDir := SDir + PathDelim;
if (FileDialog.FileName <> '') and
(ExtractFileName(FileDialog.FileName) <> FileDialog.FileName) then
saveFileName := GetUtf8String(FileDialog.Filename)
else
saveFileName := GetUtf8String(SDir+FileDialog.Filename);
saveTitle := GetUTF8String(FileDialog.Title);
Flags := 0;
if (ofReadOnly in TOpenDialog(FileDialog).Options) then
Flags := Flags or QFileDialogOptionReadOnly;
if (ofAllowMultiSelect in TOpenDialog(FileDialog).Options) then
begin
ReturnText := '';
ReturnList := QStringList_create;
{$IFDEF HASX11}
Clipboard.BeginX11SelectionLock;
{$ENDIF}
try
QFileDialog_getOpenFileNames(ReturnList,
QWidget_parentWidget(QtFileDialog.Widget), @SaveTitle, @saveFileName,
@saveFilter, @selectedFilter, Flags);
FileDialog.Files.Clear;
for i := 0 to QStringList_size(ReturnList) - 1 do
begin
QStringList_at(ReturnList, @ReturnText, i);
{$ifdef MSWINDOWS}
s := UTF16ToUTF8(ReturnText);
s := StringReplace(s, '/','\', [rfReplaceAll]);
FileDialog.Files.Add(s);
if i = 0 then
FileDialog.FileName := s;
{$else}
FileDialog.Files.Add(UTF16ToUTF8(ReturnText));
if i = 0 then
FileDialog.FileName := UTF16ToUTF8(ReturnText);
{$endif}
end;
{assign to ReturnText first filename}
if QStringList_size(ReturnList) > 0 then
QStringList_at(ReturnList, @ReturnText, 0);
finally
QStringList_destroy(ReturnList);
{$IFDEF HASX11}
Clipboard.EndX11SelectionLock;
{$ENDIF}
end;
end else
begin
{$IFDEF HASX11}
Clipboard.BeginX11SelectionLock;
try
{$ENDIF}
QFileDialog_getOpenFileName(@ReturnText,
QWidget_parentWidget(QtFileDialog.Widget), @SaveTitle, @saveFileName,
@saveFilter, @selectedFilter, Flags);
{$IFDEF HASX11}
finally
Clipboard.EndX11SelectionLock;
end;
{$ENDIF}
end;
if ReturnText <> '' then
begin
{$ifdef MSWINDOWS}
s := UTF16ToUTF8(ReturnText);
s := StringReplace(s, '/','\', [rfReplaceAll]);
FileDialog.FileName := s;
{$else}
FileDialog.FileName := UTF16ToUTF8(ReturnText);
{$endif}
FileDialog.UserChoice := mrOK;
end else
FileDialog.UserChoice := mrCancel;
{$else}
{$IFDEF HASX11}
Clipboard.BeginX11SelectionLock;
{$ENDIF}
FileDialog.UserChoice := QtDialogCodeToModalResultMap[QDialogDialogCode(QtFileDialog.exec)];
ReturnList := QStringList_create;
try
QtFileDialog.selectedFiles(ReturnList);
FileDialog.Files.Clear;
for i := 0 to QStringList_size(ReturnList) - 1 do
begin
QStringList_at(ReturnList, @ReturnText, i);
{$ifdef MSWINDOWS}
s := UTF16ToUTF8(ReturnText);
s := StringReplace(s, '/','\', [rfReplaceAll]);
FileDialog.Files.Add(s);
if i = 0 then
FileDialog.FileName := s;
{$else}
FileDialog.Files.Add(UTF16ToUTF8(ReturnText));
if i = 0 then
FileDialog.FileName := UTF16ToUTF8(ReturnText);
{$endif}
end;
//ReturnText := FileDialog.Files.Text;
finally
QStringList_destroy(ReturnList);
end;
{$IFDEF HASX11}
Clipboard.EndX11SelectionLock;
{$ENDIF}
{$endif}
end;
if ActiveWin <> 0 then
begin
if QtWidgetSet.IsValidHandle(ActiveWin) then
QtWidgetSet.SetActiveWindow(ActiveWin);
end;
end;
{ TQtWSOpenDialog }
class function TQtWSOpenDialog.CreateHandle(const ACommonDialog: TCommonDialog
): TLCLHandle;
var
FileDialog: TQtFilePreviewDialog;
begin
if (ACommonDialog is TPreviewFileDialog) then
begin
FileDialog := TQtFilePreviewDialog.Create(ACommonDialog, TQtWSCommonDialog.GetDialogParent(ACommonDialog));
{$ifdef darwin}
QWidget_setWindowFlags(FileDialog.Widget, QtDialog or QtWindowSystemMenuHint or QtCustomizeWindowHint);
{$endif}
{$ifndef QT_NATIVE_DIALOGS}
QFileDialog_setOption(QFileDialogH(FileDialog.Widget),
QFileDialogOptionDontUseNativeDialog, True);
FileDialog.initializePreview(TPreviewFileDialog(ACommonDialog).PreviewFileControl);
{$endif}
FileDialog.AttachEvents;
Result := TLCLHandle(FileDialog);
end else
Result := TQtWSFileDialog.CreateHandle(ACommonDialog);
end;
class function TQtWSOpenDialog.QueryWSEventCapabilities(
const ACommonDialog: TCommonDialog): TCDWSEventCapabilities;
begin
Result := [cdecWSNoCanCloseSupport];
end;
{ TQtWSSelectDirectoryDialog }
class procedure TQtWSSelectDirectoryDialog.UpdateProperties(
const AFileDialog: TSelectDirectoryDialog; QtFileDialog: TQtFileDialog);
var
ATitle: WideString;
AInitDir: WideString;
s: String;
begin
ATitle := AFileDialog{%H-}.Title;
QtFileDialog.setWindowTitle(@ATitle);
{$ifndef QT_NATIVE_DIALOGS}
s := AFileDialog.InitialDir;
if UTF8Pos('$HOME', AFileDialog.InitialDir) > 0 then
begin
{$IFDEF MSWINDOWS}
AInitDir := GetEnvironmentVariableUTF8('HOMEDRIVE') +
GetEnvironmentVariableUTF8('HOMEPATH');
{$ELSE}
AInitDir := UTF8ToUTF16(GetEnvironmentVariableUTF8('HOME'));
{$ENDIF}
s := StringReplace(S,'$HOME', UTF8Encode(AInitDir),[rfReplaceAll]);
end;
if not DirectoryExistsUTF8(S) then
S := GetCurrentDirUTF8;
QtFileDialog.setDirectory(S{%H-});
{$else}
S := AFileDialog.InitialDir;
if not DirectoryExistsUTF8(S) then
S := GetCurrentDirUTF8;
QtFileDialog.setDirectory(S);
{$endif}
QtFileDialog.setSizeGripEnabled(ofEnableSizing in TSelectDirectoryDialog(AFileDialog).Options);
if ofViewDetail in TSelectDirectoryDialog(AFileDialog).Options then
QtFileDialog.setViewMode(QFileDialogDetail)
else
QtFileDialog.setViewMode(QFileDialogList);
{$ifndef QT_NATIVE_DIALOGS}
// set kbd shortcuts in case when we are not native dialog.
QtFileDialog.setShortcuts(False);
{$endif}
end;
class function TQtWSSelectDirectoryDialog.CreateHandle(const ACommonDialog: TCommonDialog): TLCLHandle;
var
FileDialog: TQtFileDialog;
begin
FileDialog := TQtFileDialog.Create(ACommonDialog, TQtWSCommonDialog.GetDialogParent(ACommonDialog));
{$ifdef darwin}
QWidget_setWindowFlags(FileDialog.Widget, QtDialog or
QtWindowSystemMenuHint or QtCustomizeWindowHint);
{$endif}
QFileDialog_setOption(QFileDialogH(FileDialog.Widget),
QFileDialogOptionDontUseNativeDialog, False);
FileDialog.setFileMode(QFileDialogDirectory);
QFileDialog_setOption(QFileDialogH(FileDialog.Widget), QFileDialogOptionShowDirsOnly, True);
FileDialog.AttachEvents;
Result := TLCLHandle(FileDialog);
end;
{------------------------------------------------------------------------------
Function: TQtWSSelectDirectoryDialog.ShowModal
Params: None
Returns: Nothing
------------------------------------------------------------------------------}
class procedure TQtWSSelectDirectoryDialog.ShowModal(const ACommonDialog: TCommonDialog);
var
ReturnText: WideString;
{$ifdef QT_NATIVE_DIALOGS}
saveFileName: WideString;
saveTitle: WideString;
{$endif}
FileDialog: TSelectDirectoryDialog;
QtFileDialog: TQtFileDialog;
{$ifndef QT_NATIVE_DIALOGS}
ReturnList: QStringListH;
i: Integer;
{$endif}
s: string;
begin
{------------------------------------------------------------------------------
Initialization of variables
------------------------------------------------------------------------------}
ReturnText := '';
FileDialog := TSelectDirectoryDialog(ACommonDialog);
QtFileDialog := TQtFileDialog(FileDialog.Handle);
UpdateProperties(FileDialog, QtFileDialog);
{------------------------------------------------------------------------------
Code to call the dialog
------------------------------------------------------------------------------}
{$ifdef QT_NATIVE_DIALOGS}
saveTitle := GetUTF8String(FileDialog.Title);
// saveFileName := GetUtf8String(FileDialog.InitialDir);
if UTF8Pos('$HOME', FileDialog.InitialDir) > 0 then
begin
s := FileDialog.InitialDir;
{$IFDEF MSWINDOWS}
saveFileName := GetEnvironmentVariableUTF8('HOMEDRIVE') +
GetEnvironmentVariableUTF8('HOMEPATH');
{$ELSE}
saveFileName := GetEnvironmentVariableUTF8('HOME');
{$ENDIF}
s := StringReplace(S,'$HOME', UTF8Encode(saveFileName),[rfReplaceAll]);
saveFileName := GetUTF8String(s);
end else
saveFileName := GetUtf8String(FileDialog.InitialDir);
QFileDialog_getExistingDirectory(@ReturnText,
QWidget_parentWidget(QtFileDialog.Widget), @SaveTitle, @saveFileName);
if ReturnText <> '' then
begin
{$ifdef MSWINDOWS}
s := UTF16ToUTF8(ReturnText);
s := StringReplace(s, '/','\', [rfReplaceAll]);
FileDialog.FileName := s;
{$else}
FileDialog.FileName := UTF16ToUTF8(ReturnText);
{$endif}
FileDialog.UserChoice := mrOK;
end else
FileDialog.UserChoice := mrCancel;
{$else}
{$IFDEF HASX11}
Clipboard.BeginX11SelectionLock;
{$ENDIF}
FileDialog.UserChoice := QtDialogCodeToModalResultMap[QDialogDialogCode(QtFileDialog.exec)];
ReturnList := QStringList_create;
try
QtFileDialog.selectedFiles(ReturnList);
FileDialog.Files.Clear;
for i := 0 to QStringList_size(ReturnList) - 1 do
begin
QStringList_at(ReturnList, @ReturnText, i);
{$ifdef MSWINDOWS}
s := UTF16ToUTF8(ReturnText);
s := StringReplace(s, '/','\', [rfReplaceAll]);
FileDialog.Files.Add(s);
if i = 0 then
FileDialog.FileName := s;
{$else}
FileDialog.Files.Add(UTF16ToUTF8(ReturnText));
if i = 0 then
FileDialog.FileName := UTF16ToUTF8(ReturnText);
{$endif}
end;
//ReturnText := FileDialog.Files.Text;
finally
QStringList_destroy(ReturnList);
end;
{$IFDEF HASX11}
Clipboard.EndX11SelectionLock;
{$ENDIF}
{$endif}
end;
class function TQtWSSelectDirectoryDialog.QueryWSEventCapabilities(
const ACommonDialog: TCommonDialog): TCDWSEventCapabilities;
begin
Result := [cdecWSNoCanCloseSupport];
end;
{ TQtWSColorDialog }
class function TQtWSColorDialog.CreateHandle(const ACommonDialog: TCommonDialog): TLCLHandle;
begin
Result := 0;
end;
{------------------------------------------------------------------------------
Function: TQtWSColorDialog.ShowModal
Params: None
Returns: Nothing
------------------------------------------------------------------------------}
class procedure TQtWSColorDialog.ShowModal(const ACommonDialog: TCommonDialog);
var
AColor: TColorRef;
AQColor, ARetColor: TQColor;
ReturnBool: Boolean;
ATitle: WideString;
ColorDialog: TColorDialog absolute ACommonDialog;
{$IFDEF HASX11}
AWND: HWND;
{$ENDIF}
procedure FillCustomColors;
var
i, AIndex, CustomColorCount: integer;
AColor: TColor;
AQColor: TQColor;
begin
CustomColorCount := QColorDialog_customCount();
AQColor := Default(TQColor);
for i := 0 to ColorDialog.CustomColors.Count - 1 do
if ExtractColorIndexAndColor(ColorDialog.CustomColors, i, AIndex, AColor) then
if AIndex < CustomColorCount then
begin
ColorRefToTQColor(AColor, AQColor);
QColorDialog_setCustomColor(AIndex, @AQColor{QRgb(AColor)});
end;
end;
begin
AColor := ColorToRgb(ColorDialog.Color);
AQColor.Alpha := $FFFF;
AQColor.ColorSpec := 1;
AQColor.Pad := 0;
ColorRefToTQColor(AColor, AQColor);
FillCustomColors;
{$IFDEF HASX11}
Clipboard.BeginX11SelectionLock;
{$ENDIF}
ATitle := UTF8ToUTF16(ACommonDialog.Title);
ARetColor := Default(TQColor);
ReturnBool := QColorDialog_getColor(@ARetColor, @AQColor, TQtWSCommonDialog.GetDialogParent(ACommonDialog), @ATitle, QColorDialogShowAlphaChannel);
if ReturnBool then
begin
TQColorToColorRef(ARetColor, AColor);
ColorDialog.Color := TColor(AColor);
end;
if ReturnBool then
ACommonDialog.UserChoice := mrOk
else
ACommonDialog.UserChoice := mrCancel;
{$IFDEF HASX11}
Clipboard.EndX11SelectionLock;
if (QtWidgetSet.WindowManagerName = 'xfwm4') and (QApplication_activeModalWidget() <> nil) then
begin
AWND := HwndFromWidgetH(QApplication_activeModalWidget());
if (AWND <> 0) and (X11GetActivewindow <> TQtWidget(AWND).Widget) then
X11Raise(QWidget_winID(TQtWidget(AWND).Widget));
end;
{$ENDIF}
//Since TQtWSColorDialog.CreateHandle returns 0, in TCommonDialog.Close DoClose will not be called,
//so call it from here
ACommonDialog.DoClose;
end;
class function TQtWSColorDialog.QueryWSEventCapabilities(
const ACommonDialog: TCommonDialog): TCDWSEventCapabilities;
begin
Result := [cdecWSPerformsDoClose, cdecWSNoCanCloseSupport];
end;
{ TQtWSFontDialog }
class function TQtWSFontDialog.CreateHandle(const ACommonDialog: TCommonDialog
): TLCLHandle;
begin
Result := 0;
end;
{------------------------------------------------------------------------------
Function: TQtWSFontDialog.ShowModal
Params: None
Returns: Nothing
------------------------------------------------------------------------------}
class procedure TQtWSFontDialog.ShowModal(const ACommonDialog: TCommonDialog);
var
ReturnFont, CurrentFont: QFontH;
ReturnBool: Boolean;
Str: WideString;
{$IFDEF HASX11}
AWND: HWND;
{$ENDIF}
begin
{------------------------------------------------------------------------------
Code to call the dialog
------------------------------------------------------------------------------}
CurrentFont := TQtFont(TFontDialog(ACommonDialog).Font.Reference.Handle).FHandle;
{$IFDEF HASX11}
Clipboard.BeginX11SelectionLock;
{$ENDIF}
ReturnFont := QFont_create;
try
QFontDialog_getFont(ReturnFont, @ReturnBool, CurrentFont,
TQtWSCommonDialog.GetDialogParent(ACommonDialog));
QFont_family(ReturnFont, @Str);
TFontDialog(ACommonDialog).Font.Name := UTF16ToUTF8(Str);
if QFont_pixelSize(ReturnFont) = -1 then
TFontDialog(ACommonDialog).Font.Size := QFont_pointSize(ReturnFont)
else
TFontDialog(ACommonDialog).Font.Height := QFont_pixelSize(ReturnFont);
TFontDialog(ACommonDialog).Font.Style := [];
if QFont_bold(ReturnFont) then
TFontDialog(ACommonDialog).Font.Style := TFontDialog(ACommonDialog).Font.Style + [fsBold];
if QFont_italic(ReturnFont) then
TFontDialog(ACommonDialog).Font.Style := TFontDialog(ACommonDialog).Font.Style + [fsItalic];
if QFont_strikeOut(ReturnFont) then
TFontDialog(ACommonDialog).Font.Style := TFontDialog(ACommonDialog).Font.Style + [fsStrikeOut];
if QFont_underline(ReturnFont) then
TFontDialog(ACommonDialog).Font.Style := TFontDialog(ACommonDialog).Font.Style + [fsUnderline];
if QFont_fixedPitch(ReturnFont) then
TFontDialog(ACommonDialog).Font.Pitch := fpFixed
else
TFontDialog(ACommonDialog).Font.Pitch := fpDefault;
finally
QFont_destroy(ReturnFont);
{$IFDEF HASX11}
Clipboard.EndX11SelectionLock;
{$ENDIF}
end;
if ReturnBool then
ACommonDialog.UserChoice := mrOk
else
ACommonDialog.UserChoice := mrCancel;
{$IFDEF HASX11}
if (QtWidgetSet.WindowManagerName = 'xfwm4') and (QApplication_activeModalWidget() <> nil) then
begin
AWND := HwndFromWidgetH(QApplication_activeModalWidget());
if (AWND <> 0) and (X11GetActivewindow <> TQtWidget(AWND).Widget) then
X11Raise(QWidget_winID(TQtWidget(AWND).Widget));
end;
{$ENDIF}
//Since TQtWSFontDialog.CreateHandle returns 0, in TCommonDialog.Close DoClose will not be called,
//so call it from here
ACommonDialog.DoClose;
end;
class function TQtWSFontDialog.QueryWSEventCapabilities(
const ACommonDialog: TCommonDialog): TCDWSEventCapabilities;
begin
Result := [cdecWSPerformsDoClose, cdecWSNoCanCloseSupport];
end;
end.
|