Skip to content

Commit 56b783d

Browse files
committed
fix: add encoding selector to file-open dialog
Refs #2268
1 parent bac0d7a commit 56b783d

File tree

5 files changed

+109
-27
lines changed

5 files changed

+109
-27
lines changed

source/extfiledialog.lfm

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ object frmExtFileDialog: TfrmExtFileDialog
2828
AnchorSideLeft.Side = asrBottom
2929
AnchorSideTop.Control = pnlBottom
3030
AnchorSideRight.Control = comboFileType
31-
Left = 77
31+
Left = 73
3232
Height = 28
3333
Top = 0
34-
Width = 505
34+
Width = 549
3535
Anchors = [akTop, akLeft, akRight]
36-
BorderSpacing.Left = 10
3736
BorderSpacing.Right = 10
3837
TabOrder = 0
3938
Text = 'editFilename'
@@ -45,18 +44,19 @@ object frmExtFileDialog: TfrmExtFileDialog
4544
Left = 0
4645
Height = 20
4746
Top = 4
48-
Width = 67
49-
Caption = 'File name:'
47+
Width = 63
48+
BorderSpacing.Right = 10
49+
Caption = 'Filename:'
5050
end
5151
object comboFileType: TComboBox
5252
AnchorSideLeft.Control = btnOk
5353
AnchorSideTop.Control = pnlBottom
5454
AnchorSideRight.Control = pnlBottom
5555
AnchorSideRight.Side = asrBottom
56-
Left = 592
56+
Left = 632
5757
Height = 28
5858
Top = 0
59-
Width = 260
59+
Width = 220
6060
Anchors = [akTop, akLeft, akRight]
6161
BorderSpacing.Bottom = 10
6262
ItemHeight = 20
@@ -74,14 +74,15 @@ object frmExtFileDialog: TfrmExtFileDialog
7474
AnchorSideRight.Control = btnCancel
7575
AnchorSideBottom.Control = pnlBottom
7676
AnchorSideBottom.Side = asrBottom
77-
Left = 592
77+
Left = 632
7878
Height = 30
7979
Top = 38
80-
Width = 125
80+
Width = 105
8181
Anchors = [akTop, akRight, akBottom]
82+
AutoSize = True
8283
BorderSpacing.Right = 10
8384
Caption = 'OK'
84-
Constraints.MinWidth = 125
85+
Constraints.MinWidth = 105
8586
Default = True
8687
ModalResult = 1
8788
TabOrder = 2
@@ -91,18 +92,45 @@ object frmExtFileDialog: TfrmExtFileDialog
9192
AnchorSideTop.Side = asrBottom
9293
AnchorSideRight.Control = pnlBottom
9394
AnchorSideRight.Side = asrBottom
94-
Left = 727
95+
Left = 747
9596
Height = 30
9697
Top = 38
97-
Width = 125
98+
Width = 105
9899
Anchors = [akTop, akRight]
99100
AutoSize = True
100101
Cancel = True
101102
Caption = 'Cancel'
102-
Constraints.MinWidth = 125
103+
Constraints.MinWidth = 105
103104
ModalResult = 2
104105
TabOrder = 3
105106
end
107+
object comboEncoding: TComboBox
108+
AnchorSideTop.Control = comboFileType
109+
AnchorSideTop.Side = asrBottom
110+
AnchorSideRight.Control = btnOk
111+
Left = 402
112+
Height = 28
113+
Top = 38
114+
Width = 220
115+
Anchors = [akTop, akRight]
116+
BorderSpacing.Right = 10
117+
ItemHeight = 20
118+
Style = csDropDownList
119+
TabOrder = 4
120+
OnChange = comboEncodingChange
121+
end
122+
object lblEncoding: TLabel
123+
AnchorSideTop.Control = comboEncoding
124+
AnchorSideTop.Side = asrCenter
125+
AnchorSideRight.Control = comboEncoding
126+
Left = 327
127+
Height = 20
128+
Top = 42
129+
Width = 65
130+
Anchors = [akTop, akRight]
131+
BorderSpacing.Right = 10
132+
Caption = 'Encoding:'
133+
end
106134
end
107135
object ShellTreeView: TShellTreeView
108136
Left = 10

source/extfiledialog.pas

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,25 @@ interface
66

77
uses
88
Classes, SysUtils, LCLType, Forms, Controls, Graphics, Dialogs,
9-
ExtCtrls, StdCtrls, ShellCtrls, ComCtrls, apphelpers;
9+
ExtCtrls, StdCtrls, ShellCtrls, ComCtrls, apphelpers, extra_controls;
1010

1111
type
1212

1313
{ TfrmExtFileDialog }
1414

15-
TfrmExtFileDialog = class(TForm)
15+
TfrmExtFileDialog = class(TExtForm)
1616
btnCancel: TButton;
1717
btnOk: TButton;
18+
comboEncoding: TComboBox;
1819
comboFileType: TComboBox;
1920
editFilename: TEdit;
21+
lblEncoding: TLabel;
2022
lblFilename: TLabel;
2123
pnlBottom: TPanel;
2224
ShellListView: TShellListView;
2325
ShellTreeView: TShellTreeView;
2426
splitterMain: TSplitter;
27+
procedure comboEncodingChange(Sender: TObject);
2528
procedure comboFileTypeChange(Sender: TObject);
2629
procedure FormCreate(Sender: TObject);
2730
procedure FormDestroy(Sender: TObject);
@@ -57,13 +60,16 @@ TfrmExtFileDialog = class(TForm)
5760
end;
5861

5962
// File-open-dialog with encoding selector
60-
TExtFileOpenDialog = class(TfrmExtFileDialog);
63+
TExtFileOpenDialog = class(TfrmExtFileDialog)
64+
procedure FormCreate(Sender: TObject); overload;
65+
end;
6166

6267

6368
implementation
6469

6570
{$R *.lfm}
6671

72+
6773
function TfrmExtFileDialog.Execute: Boolean;
6874
begin
6975
Result := ShowModal = mrOK;
@@ -78,12 +84,16 @@ procedure TfrmExtFileDialog.AddFileType(FileMask, DisplayName: String);
7884

7985
procedure TfrmExtFileDialog.FormCreate(Sender: TObject);
8086
begin
87+
if ClassType = TfrmExtFileDialog then
88+
raise Exception.CreateFmt('Constructor of base class %s called. Use one of its descendants instead.', [ClassName]);
8189
FFilterNames := TStringList.Create;
8290
FFilterMasks := TStringList.Create;
8391
FEncodings := TStringList.Create;
8492
FFiles := TStringList.Create;
8593
comboFileType.Items.Clear;
8694
editFilename.Text := '';
95+
lblEncoding.Enabled := False;
96+
comboEncoding.Enabled := False;
8797
end;
8898

8999
procedure TfrmExtFileDialog.FormDestroy(Sender: TObject);
@@ -106,8 +116,13 @@ procedure TfrmExtFileDialog.FormShow(Sender: TObject);
106116
else
107117
SetInitialDir(GetUserDir);
108118
end;
119+
109120
comboFileType.ItemIndex := 0;
110121
comboFileType.OnChange(Sender);
122+
123+
comboEncoding.Items.AddStrings(FEncodings, True);
124+
if (FEncodingIndex >=0) and (FEncodingIndex < comboEncoding.Items.Count) then
125+
comboEncoding.ItemIndex := FEncodingIndex;
111126
end;
112127

113128
procedure TfrmExtFileDialog.comboFileTypeChange(Sender: TObject);
@@ -121,6 +136,11 @@ procedure TfrmExtFileDialog.comboFileTypeChange(Sender: TObject);
121136
ShellListView.Mask := FileMask;
122137
end;
123138

139+
procedure TfrmExtFileDialog.comboEncodingChange(Sender: TObject);
140+
begin
141+
FEncodingIndex := comboEncoding.ItemIndex;
142+
end;
143+
124144
procedure TfrmExtFileDialog.ShellListViewClick(Sender: TObject);
125145
begin
126146
if ShellListView.Selected <> nil then
@@ -131,7 +151,8 @@ procedure TfrmExtFileDialog.ShellListViewClick(Sender: TObject);
131151

132152
procedure TfrmExtFileDialog.ShellListViewDblClick(Sender: TObject);
133153
begin
134-
ModalResult := mrOK;
154+
if ShellListView.Selected <> nil then
155+
ModalResult := mrOK;
135156
end;
136157

137158
procedure TfrmExtFileDialog.ShellListViewSelectItem(Sender: TObject;
@@ -170,5 +191,17 @@ procedure TfrmExtFileDialog.SetInitialDir(const AValue: String);
170191
ShellTreeView.Path := AValue;
171192
end;
172193

194+
195+
196+
{ TExtFileOpenDialog }
197+
198+
procedure TExtFileOpenDialog.FormCreate(Sender: TObject);
199+
begin
200+
inherited;
201+
lblEncoding.Enabled := True;
202+
comboEncoding.Enabled := True;
203+
end;
204+
205+
173206
end.
174207

source/loaddata.pas

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ procedure Tloaddataform.ClientParse(Sender: TObject);
667667
procedure Tloaddataform.btnOpenFileClick(Sender: TObject);
668668
var
669669
Dialog: TExtFileOpenDialog;
670+
TestStream: TFileStream;
670671
begin
671672
AppSettings.ResetPath;
672673
Dialog := TExtFileOpenDialog.Create(Self);
@@ -680,7 +681,16 @@ procedure Tloaddataform.btnOpenFileClick(Sender: TObject);
680681
Dialog.EncodingIndex := AppSettings.ReadInt(asFileDialogEncoding, Self.Name);
681682
if Dialog.Execute then begin
682683
editfilename.Text := Dialog.FileName;
683-
FFileEncoding := TEncoding.UTF8;
684+
FFileEncoding := Mainform.GetEncodingByName(Dialog.Encodings[Dialog.EncodingIndex]);
685+
if FFileEncoding = nil then begin
686+
MessageDialog(_('Auto detecting the encoding of a file is highly discouraged. You may experience data loss if the detection fails.') +
687+
SLineBreak + SLineBreak +
688+
_('To avoid this message select the correct encoding before pressing Open.'),
689+
mtWarning, [mbOK]);
690+
TestStream := TFileStream.Create(Dialog.Filename, fmOpenRead or fmShareDenyNone);
691+
FFileEncoding := DetectEncoding(TestStream);
692+
TestStream.Free;
693+
end;
684694
grpParseMethod.OnClick(Sender);
685695
AppSettings.WriteInt(asFileDialogEncoding, Dialog.EncodingIndex, Self.Name);
686696
end;

source/main.pas

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3943,7 +3943,7 @@ procedure TMainForm.actLaunchCommandlineExecute(Sender: TObject);
39433943
// Load SQL-file, make sure that SheetQuery is activated
39443944
procedure TMainForm.actLoadSQLExecute(Sender: TObject);
39453945
var
3946-
i: Integer;
3946+
i, ProceedResult: Integer;
39473947
Dialog: TExtFileOpenDialog;
39483948
Encoding: TEncoding;
39493949
Tab: TQueryTab;
@@ -3957,13 +3957,24 @@ procedure TMainForm.actLoadSQLExecute(Sender: TObject);
39573957
Dialog.Encodings.Assign(FileEncodings);
39583958
Dialog.EncodingIndex := AppSettings.ReadInt(asFileDialogEncoding, Self.Name);
39593959
if Dialog.Execute then begin
3960-
Encoding := TEncoding.UTF8;
3961-
if not RunQueryFiles(Dialog.Files, Encoding, Sender=actRunSQL) then begin
3962-
for i:=0 to Dialog.Files.Count-1 do begin
3963-
Tab := GetOrCreateEmptyQueryTab(False);
3964-
Tab.LoadContents(Dialog.Files[i], True, Encoding);
3965-
if i = Dialog.Files.Count-1 then
3966-
SetMainTab(Tab.TabSheet);
3960+
Encoding := GetEncodingByName(Dialog.Encodings[Dialog.EncodingIndex]);
3961+
if Encoding = nil then begin
3962+
ProceedResult := MessageDialog(_('Really auto-detect file encoding?') + SLineBreak + SLineBreak +
3963+
_('Auto detecting the encoding of a file is highly discouraged. You may experience data loss if the detection fails.') + SLineBreak + SLineBreak +
3964+
_('To avoid this message select the correct encoding before pressing Open.'),
3965+
mtConfirmation, [mbYes, mbCancel]);
3966+
end else begin
3967+
ProceedResult := mrYes;
3968+
end;
3969+
3970+
if ProceedResult = mrYes then begin
3971+
if not RunQueryFiles(Dialog.Files, Encoding, Sender=actRunSQL) then begin
3972+
for i:=0 to Dialog.Files.Count-1 do begin
3973+
Tab := GetOrCreateEmptyQueryTab(False);
3974+
Tab.LoadContents(Dialog.Files[i], True, Encoding);
3975+
if i = Dialog.Files.Count-1 then
3976+
SetMainTab(Tab.TabSheet);
3977+
end;
39673978
end;
39683979
end;
39693980
AppSettings.WriteInt(asFileDialogEncoding, Dialog.EncodingIndex, Self.Name);

source/texteditor.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ procedure TfrmTextEditor.btnLoadTextClick(Sender: TObject);
421421
d.EncodingIndex := AppSettings.ReadInt(asFileDialogEncoding, Self.Name);
422422
if d.Execute then try
423423
Screen.Cursor := crHourglass;
424-
MemoText.Text := ReadTextFile(d.FileName, TEncoding.UTF8);
424+
MemoText.Text := ReadTextFile(d.FileName, MainForm.GetEncodingByName(d.Encodings[d.EncodingIndex]));
425425
if (FMaxLength > 0) and (Length(MemoText.Text) > FMaxLength) then
426426
MemoText.Text := copy(MemoText.Text, 0, FMaxLength);
427427
AppSettings.WriteInt(asFileDialogEncoding, d.EncodingIndex, Self.Name);

0 commit comments

Comments
 (0)