Menu

[r411]: / trunk / Src / UOpenDialogHelper.pas  Maximize  Restore  History

Download this file

231 lines (207 with data), 7.9 kB

  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
{
* UOpenDialogHelper.pas
*
* Helper routines for use when working with standard windows open and save file
* dialog boxes.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is UOpenDialogHelper.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2008-2009 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UOpenDialogHelper;
interface
uses
// Delphi
Dialogs;
function FileOpenEditedFileName(const Dlg: TOpenDialog): string;
{Gets full path to the file that is currently entered in a file open dialog
box.
@param Dlg [in] Dialog box for which file name is required.
@return Required file path.
}
function FileOpenFileNameWithExt(Dlg: TOpenDialog): string;
{Ensures a file name entered in a dialog box has a default extension if none
provided.
@param Dlg [in] Dialog box containing required file name and extension info.
@return File name, with default extension if (1) file name has no extension,
(2) current filter is not *.* (3) dialog has a filter string.
}
function FilterIndexToExt(const Dlg: TOpenDialog): string;
{Extracts extension associated with a standard file open dialog's selected
file type from its "|" delimited Filter property.
@param Dlg [in] Dialog box for which current extension is to be extracted.
@return Extension specified by dialog's FilterIndex property with a
prepended '.'.
}
function ExtToFilterIndex(const FilterStr, Ext: string;
const DefValue: Integer): Integer;
{Calculates index of a file extension in a "|" delimited file filter string as
used in standard file dialog boxes.
@param FilterStr [in] List of file types and extensions. Has format
"file desc 1|ext 1|file desc 2|ext 2 etc...".
@param Ext [in] Extension to be found.
@param DefValue [in] Default 1 based index to use if Ext is not in
FilterStr.
@return 1 based index of extension in filter string or -1 if extension not
in list.
}
implementation
uses
// Delphi
SysUtils, Classes, Windows, Dlgs, CommDlg,
// Project
UUtils;
function FilterIndexToExt(const Dlg: TOpenDialog): string;
{Extracts extension associated with a standard file open dialog's selected
file type from its "|" delimited Filter property.
@param Dlg [in] Dialog box for which current extension is to be extracted.
@return Extension specified by dialog's FilterIndex property with a
prepended '.'.
}
var
FilterParts: TStringList; // stores filter split into component parts
begin
FilterParts := TStringList.Create;
try
// Split filter string into parts (divided by | chars):
// even number indexes are descriptions and odd indexes are extensions
ExplodeStr(Dlg.Filter, '|', FilterParts);
Result := ExtractFileExt(FilterParts[2 * (Dlg.FilterIndex - 1) + 1]);
finally
FreeAndNil(FilterParts);
end;
end;
function ExtToFilterIndex(const FilterStr, Ext: string;
const DefValue: Integer): Integer;
{Calculates index of a file extension in a "|" delimited file filter string as
used in standard file dialog boxes.
@param FilterStr [in] List of file types and extensions. Has format
"file desc 1|ext 1|file desc 2|ext 2 etc...".
@param Ext [in] Extension to be found.
@param DefValue [in] Default 1 based index to use if Ext is not in
FilterStr.
@return 1 based index of extension in filter string or -1 if extension not
in list.
}
var
FilterParts: TStringList; // stores filter split into component parts
Extensions: TStringList; // list of extensions in filter string
Idx: Integer; // loops thru extensions in filter string
begin
Extensions := nil;
FilterParts := TStringList.Create;
try
// Split filter string into parts (divided by | chars):
// even number indexes are descriptions and odd indexes are extensions
ExplodeStr(FilterStr, '|', FilterParts);
// Record only extensions (every 2nd entry starting at index 1)
Extensions := TStringList.Create;
Idx := 1;
while Idx < FilterParts.Count do
begin
Extensions.Add(ExtractFileExt(FilterParts[Idx]));
Inc(Idx, 2);
end;
// Check if required extension in list
Result := Extensions.IndexOf(Ext);
if Result >= 0 then
// extension in list, increment by 1 since filter indexes are 1 based
Inc(Result)
else
Result := DefValue;
finally
FreeAndNil(Extensions);
FreeAndNil(FilterParts);
end;
end;
function FileOpenFileNameWithExt(Dlg: TOpenDialog): string;
{Ensures a file name entered in a dialog box has a default extension if none
provided.
@param Dlg [in] Dialog box containing required file name and extension info.
@return File name, with default extension if (1) file name has no extension,
(2) current filter is not *.* (3) dialog has a filter string.
}
var
DefaultExt: string; // default extension for current filter
begin
Result := Dlg.FileName;
if (ExtractFileExt(Dlg.FileName) = '') and (Dlg.Filter <> '') then
begin
DefaultExt := FilterIndexToExt(Dlg);
if AnsiPos('*', DefaultExt) = 0 then
Result := Result + DefaultExt;
end;
end;
function FileOpenEditText(const Dlg: TOpenDialog): string;
{Gets text from file edit control in a file open dialog box.
@param Dlg [in] Reference to dialog box for which text is required.
@return Required text.
}
var
DlgItemWnd: HWND; // handle of edit control window in dlg box
Text: array[0..MAX_PATH] of Char; // buffer to receive edit control text
begin
Result := '';
// File name entered by user or selected in dialog is displayed either in
// edit control edt1 or in combo box cmb13. We try to get text first from
// edt1 and if that fails, from cmb13. If we fail to find both we bail out.
DlgItemWnd := GetDlgItem(GetParent(Dlg.Handle), edt1);
if DlgItemWnd = 0 then
DlgItemWnd := GetDlgItem(GetParent(Dlg.Handle), cmb13);
if DlgItemWnd = 0 then
Exit;
// Get text from found control
GetWindowText(DlgItemWnd, Text, SizeOf(Text));
Result := Text;
end;
function FileOpenFolderPath(const Dlg: TOpenDialog): string;
{Gets name of currently selected folder in a file open dialog box.
@param Dlg [in] Reference to dialog box for which folder is required.
@return Path to folder.
}
var
Folder: array[0..MAX_PATH] of Char; // receives path to folder
begin
SendMessage(
GetParent(Dlg.Handle), CDM_GETFOLDERPATH, SizeOf(Folder), Integer(@Folder)
);
Result := Folder;
end;
function FileOpenEditedFileName(const Dlg: TOpenDialog): string;
{Gets full path to the file that is currently entered in a file open dialog
box.
@param Dlg [in] Dialog box for which file name is required.
@return Required file path.
}
begin
Result := FileOpenEditText(Dlg);
if Result = '' then
Exit;
if IsBaseFileName(Result) then
Result := IncludeTrailingPathDelimiter(FileOpenFolderPath(Dlg)) + Result;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.