ComboEdit.pas 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. unit ComboEdit;
  2. {$J+}
  3. {$WARN UNIT_PLATFORM OFF}
  4. interface
  5. uses Windows, Classes, StdCtrls, Controls, Messages, Forms, Graphics,
  6. Menus, Buttons, Dialogs, Mask,
  7. { SysUtils must overload deprecated FileCtrl (implements SelectDirectory) }
  8. FileCtrl, SysUtils;
  9. const
  10. scAltDown = scAlt + vk_Down;
  11. scCtrlEnter = scCtrl + vk_Return;
  12. DefEditBtnWidth = 25;
  13. resourcestring
  14. SBrowse = 'Browse';
  15. SDefaultFilter = 'All files (*.*)|*.*';
  16. SInvalidFileName = 'Invalid file name - %s';
  17. type
  18. TFileExt = type string;
  19. { TCustomComboEdit }
  20. TCustomComboEdit = class(TCustomMaskEdit)
  21. private
  22. FButton: TButton;
  23. FBtnControl: TWinControl;
  24. FOnButtonClick: TNotifyEvent;
  25. FClickKey: TShortCut;
  26. FReadOnly: Boolean;
  27. FDirectInput: Boolean;
  28. FAlwaysEnable: Boolean;
  29. FAlignment: TAlignment;
  30. procedure SetEditRect;
  31. procedure UpdateBtnBounds;
  32. procedure EditButtonClick(Sender: TObject);
  33. function GetMinHeight: Integer;
  34. function GetTextHeight: Integer;
  35. procedure SetShowCaret;
  36. function GetButtonWidth: Integer;
  37. procedure SetButtonWidth(Value: Integer);
  38. function GetButtonHint: string;
  39. procedure SetButtonHint(const Value: string);
  40. procedure SetDirectInput(Value: Boolean);
  41. procedure SetReadOnly(Value: Boolean);
  42. procedure SetAlignment(Value: TAlignment);
  43. function BtnWidthStored: Boolean;
  44. procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
  45. procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
  46. procedure CMEnter(var Message: TMessage); message CM_ENTER;
  47. procedure CNCtlColor(var Message: TMessage); message CN_CTLCOLOREDIT;
  48. procedure WMSize(var Message: TWMSize); message WM_SIZE;
  49. procedure WMSetFocus(var Message: TMessage); message WM_SETFOCUS;
  50. procedure WMPaste(var Message: TWMPaste); message WM_PASTE;
  51. procedure WMCut(var Message: TWMCut); message WM_CUT;
  52. procedure CMCtl3DChanged(var Message: TMessage); message CM_CTL3DCHANGED;
  53. protected
  54. procedure CreateParams(var Params: TCreateParams); override;
  55. procedure CreateWnd; override;
  56. function EditCanModify: Boolean; override;
  57. function GetReadOnly: Boolean; virtual;
  58. procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  59. procedure KeyPress(var Key: Char); override;
  60. procedure ButtonClick; dynamic;
  61. property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
  62. property AlwaysEnable: Boolean read FAlwaysEnable write FAlwaysEnable default False;
  63. property Button: TButton read FButton;
  64. property ClickKey: TShortCut read FClickKey write FClickKey
  65. default scAltDown;
  66. property ButtonWidth: Integer read GetButtonWidth write SetButtonWidth
  67. stored BtnWidthStored;
  68. property ButtonHint: string read GetButtonHint write SetButtonHint;
  69. property DirectInput: Boolean read FDirectInput write SetDirectInput default True;
  70. property ReadOnly: Boolean read GetReadOnly write SetReadOnly default False;
  71. property OnButtonClick: TNotifyEvent read FOnButtonClick write FOnButtonClick;
  72. public
  73. constructor Create(AOwner: TComponent); override;
  74. destructor Destroy; override;
  75. procedure DoClick;
  76. procedure SelectAll;
  77. end;
  78. type
  79. TComboEdit = class(TCustomComboEdit)
  80. published
  81. property AutoSelect;
  82. property ButtonHint;
  83. property BorderStyle;
  84. property CharCase;
  85. property ClickKey;
  86. property Color;
  87. property Ctl3D;
  88. property DirectInput;
  89. property DragCursor;
  90. property DragMode;
  91. property EditMask;
  92. property Enabled;
  93. property Font;
  94. property ButtonWidth;
  95. property HideSelection;
  96. property Anchors;
  97. property BiDiMode;
  98. property Constraints;
  99. property DragKind;
  100. property ParentBiDiMode;
  101. property ImeMode;
  102. property ImeName;
  103. property ParentColor;
  104. property ParentCtl3D;
  105. property ParentFont;
  106. property ParentShowHint;
  107. property PopupMenu;
  108. property ReadOnly;
  109. property ShowHint;
  110. property TabOrder;
  111. property TabStop;
  112. property Text;
  113. property Visible;
  114. property OnButtonClick;
  115. property OnChange;
  116. property OnClick;
  117. property OnDblClick;
  118. property OnDragDrop;
  119. property OnDragOver;
  120. property OnEndDrag;
  121. property OnEnter;
  122. property OnExit;
  123. property OnKeyDown;
  124. property OnKeyPress;
  125. property OnKeyUp;
  126. property OnMouseDown;
  127. property OnMouseMove;
  128. property OnMouseUp;
  129. property OnStartDrag;
  130. property OnContextPopup;
  131. property OnEndDock;
  132. property OnStartDock;
  133. end;
  134. { TFileDirEdit }
  135. { The common parent of TFilenameEdit and TDirectoryEdit }
  136. { For internal use only; it's not intended to be used separately }
  137. const
  138. MaxFileLength = SizeOf(TFileName) - 1;
  139. type
  140. TExecOpenDialogEvent = procedure(Sender: TObject; var Name: string;
  141. var Action: Boolean) of object;
  142. TFileDirEdit = class(TCustomComboEdit)
  143. private
  144. FErrMode: Cardinal;
  145. FAcceptFiles: Boolean;
  146. FOnDropFiles: TNotifyEvent;
  147. FOnBeforeDialog: TExecOpenDialogEvent;
  148. FOnAfterDialog: TExecOpenDialogEvent;
  149. procedure SetDragAccept(Value: Boolean);
  150. procedure SetAcceptFiles(Value: Boolean);
  151. procedure WMDropFiles(var Msg: TWMDropFiles); message WM_DROPFILES;
  152. protected
  153. FMultipleDirs: Boolean;
  154. procedure CreateHandle; override;
  155. procedure DestroyWindowHandle; override;
  156. function GetLongName: string; virtual; abstract;
  157. function GetShortName: string; virtual; abstract;
  158. procedure DoAfterDialog(var FileName: string; var Action: Boolean); dynamic;
  159. procedure DoBeforeDialog(var FileName: string; var Action: Boolean); dynamic;
  160. procedure ReceptFileDir(const AFileName: string); virtual; abstract;
  161. procedure ClearFileList; virtual;
  162. procedure DisableSysErrors;
  163. procedure EnableSysErrors;
  164. property MaxLength;
  165. public
  166. property LongName: string read GetLongName;
  167. property ShortName: string read GetShortName;
  168. published
  169. property AcceptFiles: Boolean read FAcceptFiles write SetAcceptFiles default False;
  170. property OnBeforeDialog: TExecOpenDialogEvent read FOnBeforeDialog
  171. write FOnBeforeDialog;
  172. property OnAfterDialog: TExecOpenDialogEvent read FOnAfterDialog
  173. write FOnAfterDialog;
  174. property OnDropFiles: TNotifyEvent read FOnDropFiles write FOnDropFiles;
  175. property OnButtonClick;
  176. end;
  177. { TFilenameEdit }
  178. TFileDialogKind = (dkOpen, dkSave , dkOpenPicture,
  179. dkSavePicture);
  180. TCreateEditDialogEvent = procedure(Sender: TObject; DialogKind: TFileDialogKind;
  181. var Dialog: TOpenDialog) of object;
  182. TFilenameEdit = class(TFileDirEdit)
  183. private
  184. FDialog: TOpenDialog;
  185. FDialogKind: TFileDialogKind;
  186. FOnCreateEditDialog: TCreateEditDialogEvent;
  187. procedure CreateEditDialog;
  188. function GetFileName: string;
  189. function GetDefaultExt: TFileExt;
  190. function GetFileEditStyle: TFileEditStyle;
  191. function GetFilter: string;
  192. function GetFilterIndex: Integer;
  193. function GetInitialDir: string;
  194. function GetHistoryList: TStrings;
  195. function GetOptions: TOpenOptions;
  196. function GetDialogTitle: string;
  197. function GetDialogFiles: TStrings;
  198. procedure SetDialogKind(Value: TFileDialogKind);
  199. procedure SetFileName(const Value: string);
  200. procedure SetDefaultExt(Value: TFileExt);
  201. procedure SetFileEditStyle(Value: TFileEditStyle);
  202. procedure SetFilter(const Value: string);
  203. procedure SetFilterIndex(Value: Integer);
  204. procedure SetInitialDir(const Value: string);
  205. procedure SetHistoryList(Value: TStrings);
  206. procedure SetOptions(Value: TOpenOptions);
  207. procedure SetDialogTitle(const Value: string);
  208. procedure SetOnCreateEditDialog(Value: TCreateEditDialogEvent);
  209. function IsCustomTitle: Boolean;
  210. function IsCustomFilter: Boolean;
  211. protected
  212. procedure ButtonClick; override;
  213. procedure ReceptFileDir(const AFileName: string); override;
  214. procedure ClearFileList; override;
  215. function GetLongName: string; override;
  216. function GetShortName: string; override;
  217. public
  218. constructor Create(AOwner: TComponent); override;
  219. property Dialog: TOpenDialog read FDialog;
  220. property DialogFiles: TStrings read GetDialogFiles;
  221. published
  222. property DialogKind: TFileDialogKind read FDialogKind write SetDialogKind
  223. default dkOpen;
  224. property DefaultExt: TFileExt read GetDefaultExt write SetDefaultExt;
  225. property FileEditStyle: TFileEditStyle read GetFileEditStyle write SetFileEditStyle
  226. default fsEdit;
  227. property FileName: string read GetFileName write SetFileName stored False;
  228. property Filter: string read GetFilter write SetFilter stored IsCustomFilter;
  229. property FilterIndex: Integer read GetFilterIndex write SetFilterIndex default 1;
  230. property InitialDir: string read GetInitialDir write SetInitialDir;
  231. property HistoryList: TStrings read GetHistoryList write SetHistoryList;
  232. property DialogOptions: TOpenOptions read GetOptions write SetOptions
  233. default [ofHideReadOnly];
  234. property DialogTitle: string read GetDialogTitle write SetDialogTitle
  235. stored IsCustomTitle;
  236. property OnCreateEditDialog: TCreateEditDialogEvent read FOnCreateEditDialog
  237. write SetOnCreateEditDialog;
  238. property AutoSelect;
  239. property ButtonHint;
  240. property BorderStyle;
  241. property CharCase;
  242. property ClickKey;
  243. property Color;
  244. property Ctl3D;
  245. property DirectInput;
  246. property DragCursor;
  247. property DragMode;
  248. property EditMask;
  249. property Enabled;
  250. property Font;
  251. property ButtonWidth;
  252. property HideSelection;
  253. property Anchors;
  254. property BiDiMode;
  255. property Constraints;
  256. property DragKind;
  257. property ParentBiDiMode;
  258. property ImeMode;
  259. property ImeName;
  260. property ParentColor;
  261. property ParentCtl3D;
  262. property ParentFont;
  263. property ParentShowHint;
  264. property PopupMenu;
  265. property ReadOnly;
  266. property ShowHint;
  267. property TabOrder;
  268. property TabStop;
  269. property Text;
  270. property Visible;
  271. property OnChange;
  272. property OnClick;
  273. property OnDblClick;
  274. property OnDragDrop;
  275. property OnDragOver;
  276. property OnEndDrag;
  277. property OnEnter;
  278. property OnExit;
  279. property OnKeyDown;
  280. property OnKeyPress;
  281. property OnKeyUp;
  282. property OnMouseDown;
  283. property OnMouseMove;
  284. property OnMouseUp;
  285. property OnStartDrag;
  286. property OnContextPopup;
  287. property OnEndDock;
  288. property OnStartDock;
  289. end;
  290. { TDirectoryEdit }
  291. TDirectoryEdit = class(TFileDirEdit)
  292. private
  293. FInitialDir: string;
  294. FDialogText: string;
  295. protected
  296. procedure ButtonClick; override;
  297. procedure ReceptFileDir(const AFileName: string); override;
  298. function GetLongName: string; override;
  299. function GetShortName: string; override;
  300. public
  301. constructor Create(AOwner: TComponent); override;
  302. published
  303. property DialogText: string read FDialogText write FDialogText;
  304. property InitialDir: string read FInitialDir write FInitialDir;
  305. property MultipleDirs: Boolean read FMultipleDirs write FMultipleDirs default False;
  306. property AutoSelect;
  307. property ButtonHint;
  308. property BorderStyle;
  309. property CharCase;
  310. property ClickKey;
  311. property Color;
  312. property Ctl3D;
  313. property DirectInput;
  314. property DragCursor;
  315. property DragMode;
  316. property EditMask;
  317. property Enabled;
  318. property Font;
  319. property ButtonWidth;
  320. property HideSelection;
  321. property Anchors;
  322. property BiDiMode;
  323. property Constraints;
  324. property DragKind;
  325. property ParentBiDiMode;
  326. property ImeMode;
  327. property ImeName;
  328. property ParentColor;
  329. property ParentCtl3D;
  330. property ParentFont;
  331. property ParentShowHint;
  332. property PopupMenu;
  333. property ReadOnly;
  334. property ShowHint;
  335. property TabOrder;
  336. property TabStop;
  337. property Text;
  338. property Visible;
  339. property OnChange;
  340. property OnClick;
  341. property OnDblClick;
  342. property OnDragDrop;
  343. property OnDragOver;
  344. property OnEndDrag;
  345. property OnEnter;
  346. property OnExit;
  347. property OnKeyDown;
  348. property OnKeyPress;
  349. property OnKeyUp;
  350. property OnMouseDown;
  351. property OnMouseMove;
  352. property OnMouseUp;
  353. property OnStartDrag;
  354. property OnContextPopup;
  355. property OnEndDock;
  356. property OnStartDock;
  357. end;
  358. EComboEditError = class(Exception);
  359. procedure Register;
  360. implementation
  361. uses
  362. ShellAPI, Consts, ExtDlgs, Variants, PasTools, UITypes;
  363. procedure Register;
  364. begin
  365. RegisterComponents('Martin', [TComboEdit, TFilenameEdit, TDirectoryEdit]);
  366. end;
  367. { Utility functions }
  368. type
  369. TCharSet = TSysCharSet;
  370. function ExtractSubstr(const S: string; var Pos: Integer;
  371. const Delims: TCharSet): string;
  372. var
  373. I: Integer;
  374. begin
  375. I := Pos;
  376. while (I <= Length(S)) and not CharInSet(S[I], Delims) do Inc(I);
  377. Result := Copy(S, Pos, I - Pos);
  378. if (I <= Length(S)) and CharInSet(S[I], Delims) then Inc(I);
  379. Pos := I;
  380. end;
  381. function ValidFileName(const FileName: string): Boolean;
  382. function HasAny(const Str, Substr: string): Boolean;
  383. var
  384. I: Integer;
  385. begin
  386. Result := False;
  387. for I := 1 to Length(Substr) do begin
  388. if Pos(Substr[I], Str) > 0 then begin
  389. Result := True;
  390. Break;
  391. end;
  392. end;
  393. end;
  394. begin
  395. Result := (FileName <> '') and (not HasAny(FileName, '<>"[]|'));
  396. if Result then Result := Pos('\', ExtractFileName(FileName)) = 0;
  397. end;
  398. function DirExists(Name: string): Boolean;
  399. var
  400. Code: Integer;
  401. begin
  402. Code := GetFileAttributes(PChar(ApiPath(Name)));
  403. Result := (Code <> -1) and (FILE_ATTRIBUTE_DIRECTORY and Code <> 0);
  404. end;
  405. function ShortToLongFileName(const ShortName: string): string;
  406. var
  407. Temp: TWin32FindData;
  408. SearchHandle: THandle;
  409. begin
  410. SearchHandle := FindFirstFile(PChar(ShortName), Temp);
  411. if SearchHandle <> INVALID_HANDLE_VALUE then begin
  412. Result := string(Temp.cFileName);
  413. if Result = '' then Result := string(Temp.cAlternateFileName);
  414. end
  415. else Result := '';
  416. Windows.FindClose(SearchHandle);
  417. end;
  418. function LongToShortFileName(const LongName: string): string;
  419. var
  420. Temp: TWin32FindData;
  421. SearchHandle: THandle;
  422. begin
  423. SearchHandle := FindFirstFile(PChar(LongName), Temp);
  424. if SearchHandle <> INVALID_HANDLE_VALUE then begin
  425. Result := string(Temp.cAlternateFileName);
  426. if Result = '' then Result := string(Temp.cFileName);
  427. end
  428. else Result := '';
  429. Windows.FindClose(SearchHandle);
  430. end;
  431. function ShortToLongPath(const ShortName: string): string;
  432. var
  433. LastSlash: PChar;
  434. TempPathPtr: PChar;
  435. begin
  436. Result := '';
  437. TempPathPtr := PChar(ShortName);
  438. LastSlash := StrRScan(TempPathPtr, '\');
  439. while LastSlash <> nil do begin
  440. Result := '\' + ShortToLongFileName(TempPathPtr) + Result;
  441. if LastSlash <> nil then begin
  442. LastSlash^ := char(0);
  443. LastSlash := StrRScan(TempPathPtr, '\');
  444. end;
  445. end;
  446. Result := TempPathPtr + Result;
  447. end;
  448. function LongToShortPath(const LongName: string): string;
  449. var
  450. LastSlash: PChar;
  451. TempPathPtr: PChar;
  452. begin
  453. Result := '';
  454. TempPathPtr := PChar(LongName);
  455. LastSlash := StrRScan(TempPathPtr, '\');
  456. while LastSlash <> nil do begin
  457. Result := '\' + LongToShortFileName(TempPathPtr) + Result;
  458. if LastSlash <> nil then begin
  459. LastSlash^ := char(0);
  460. LastSlash := StrRScan(TempPathPtr, '\');
  461. end;
  462. end;
  463. Result := TempPathPtr + Result;
  464. end;
  465. { TCustomComboEdit }
  466. constructor TCustomComboEdit.Create(AOwner: TComponent);
  467. begin
  468. inherited Create(AOwner);
  469. ControlStyle := ControlStyle + [csCaptureMouse];
  470. AutoSize := False;
  471. FDirectInput := True;
  472. FClickKey := scCtrlEnter;
  473. FBtnControl := TWinControl.Create(Self);
  474. with FBtnControl do
  475. begin
  476. ControlStyle := ControlStyle + [csReplicatable];
  477. Width := DefEditBtnWidth;
  478. Height := 17;
  479. Visible := True;
  480. Parent := Self;
  481. end;
  482. FButton := TButton.Create(Self);
  483. with FButton do
  484. begin
  485. SetBounds(0, 0, FBtnControl.Width, FBtnControl.Height);
  486. ControlStyle := ControlStyle + [csReplicatable];
  487. ParentShowHint := True;
  488. Caption := '...';
  489. Visible := True;
  490. Parent := FBtnControl;
  491. OnClick := EditButtonClick;
  492. end;
  493. Height := 21;
  494. end;
  495. destructor TCustomComboEdit.Destroy;
  496. begin
  497. FButton.OnClick := nil;
  498. inherited Destroy;
  499. end;
  500. procedure TCustomComboEdit.CreateParams(var Params: TCreateParams);
  501. const
  502. Alignments: array[TAlignment] of Longword = (ES_LEFT, ES_RIGHT, ES_CENTER);
  503. begin
  504. inherited CreateParams(Params);
  505. Params.Style := Params.Style or ES_MULTILINE or WS_CLIPCHILDREN
  506. or Alignments[FAlignment];
  507. end;
  508. procedure TCustomComboEdit.CreateWnd;
  509. begin
  510. inherited CreateWnd;
  511. SetEditRect;
  512. end;
  513. function TCustomComboEdit.EditCanModify: Boolean;
  514. begin
  515. Result := not FReadOnly;
  516. end;
  517. procedure TCustomComboEdit.KeyDown(var Key: Word; Shift: TShiftState);
  518. begin
  519. inherited KeyDown(Key, Shift);
  520. if (FClickKey = ShortCut(Key, Shift)) and (ButtonWidth > 0) then
  521. begin
  522. EditButtonClick(Self);
  523. Key := 0;
  524. end;
  525. end;
  526. procedure TCustomComboEdit.KeyPress(var Key: Char);
  527. var
  528. OrigKey: Char;
  529. begin
  530. if (Key = Char(VK_RETURN)) or (Key = Char(VK_ESCAPE)) or (Key = #10) then
  531. begin
  532. OrigKey := Key;
  533. { must catch and remove this, since is actually multi-line }
  534. GetParentForm(Self).Perform(CM_DIALOGKEY, Byte(Key), 0);
  535. if Key = OrigKey then
  536. begin
  537. inherited KeyPress(Key);
  538. Key := #0;
  539. Exit;
  540. end;
  541. end;
  542. inherited KeyPress(Key);
  543. end;
  544. function TCustomComboEdit.GetButtonWidth: Integer;
  545. begin
  546. Result := FButton.Width;
  547. end;
  548. procedure TCustomComboEdit.SetButtonWidth(Value: Integer);
  549. begin
  550. if ButtonWidth <> Value then
  551. begin
  552. FBtnControl.Visible := Value > 1;
  553. if (csCreating in ControlState) then
  554. begin
  555. FBtnControl.Width := Value;
  556. FButton.Width := Value;
  557. with FButton do
  558. ControlStyle := ControlStyle - [csFixedWidth];
  559. end
  560. else if (Value <> ButtonWidth) and (Value < ClientWidth) then
  561. begin
  562. FButton.Width := Value;
  563. with FButton do
  564. ControlStyle := ControlStyle - [csFixedWidth];
  565. if HandleAllocated then RecreateWnd;
  566. end;
  567. end;
  568. end;
  569. function TCustomComboEdit.GetButtonHint: string;
  570. begin
  571. Result := FButton.Hint;
  572. end;
  573. procedure TCustomComboEdit.SetButtonHint(const Value: string);
  574. begin
  575. FButton.Hint := Value;
  576. end;
  577. procedure TCustomComboEdit.SetEditRect;
  578. var
  579. Loc: TRect;
  580. begin
  581. SetRect(Loc, 0, 0, ClientWidth - FBtnControl.Width - 2, ClientHeight + 1);
  582. SendMessage(Handle, EM_SETRECTNP, 0, LongInt(@Loc));
  583. end;
  584. procedure TCustomComboEdit.UpdateBtnBounds;
  585. var
  586. BtnRect: TRect;
  587. begin
  588. if NewStyleControls then begin
  589. if Ctl3D and (BorderStyle = bsSingle) then
  590. BtnRect := Bounds(Width - FButton.Width - 4, 0,
  591. FButton.Width, Height - 4)
  592. else begin
  593. if BorderStyle = bsSingle then
  594. BtnRect := Bounds(Width - FButton.Width - 2, 2,
  595. FButton.Width, Height - 4)
  596. else
  597. BtnRect := Bounds(Width - FButton.Width, 0,
  598. FButton.Width, Height);
  599. end;
  600. end
  601. else
  602. BtnRect := Bounds(Width - FButton.Width, 0, FButton.Width, Height);
  603. with BtnRect do
  604. FBtnControl.SetBounds(Left, Top, Right - Left, Bottom - Top);
  605. FButton.Height := FBtnControl.Height;
  606. SetEditRect;
  607. end;
  608. procedure TCustomComboEdit.CMCtl3DChanged(var Message: TMessage);
  609. begin
  610. inherited;
  611. UpdateBtnBounds;
  612. end;
  613. procedure TCustomComboEdit.WMSize(var Message: TWMSize);
  614. var
  615. MinHeight: Integer;
  616. begin
  617. inherited;
  618. if not (csLoading in ComponentState) then
  619. begin
  620. MinHeight := GetMinHeight;
  621. { text edit bug: if size to less than MinHeight, then edit ctrl does
  622. not display the text }
  623. if Height < MinHeight then
  624. begin
  625. Height := MinHeight;
  626. Exit;
  627. end;
  628. end;
  629. UpdateBtnBounds;
  630. end;
  631. function TCustomComboEdit.GetTextHeight: Integer;
  632. var
  633. DC: HDC;
  634. SaveFont: HFont;
  635. SysMetrics, Metrics: TTextMetric;
  636. begin
  637. DC := GetDC(0);
  638. try
  639. GetTextMetrics(DC, SysMetrics);
  640. SaveFont := SelectObject(DC, Font.Handle);
  641. GetTextMetrics(DC, Metrics);
  642. SelectObject(DC, SaveFont);
  643. finally
  644. ReleaseDC(0, DC);
  645. end;
  646. if SysMetrics.tmHeight < Metrics.tmHeight then Result := SysMetrics.tmHeight
  647. else Result := Metrics.tmHeight;
  648. end;
  649. function TCustomComboEdit.GetMinHeight: Integer;
  650. var
  651. I: Integer;
  652. begin
  653. I := GetTextHeight;
  654. Result := I + GetSystemMetrics(SM_CYBORDER) * 4 + 1;
  655. end;
  656. procedure TCustomComboEdit.CMFontChanged(var Message: TMessage);
  657. begin
  658. inherited;
  659. if HandleAllocated then SetEditRect;
  660. end;
  661. procedure TCustomComboEdit.CMEnabledChanged(var Message: TMessage);
  662. begin
  663. inherited;
  664. FButton.Enabled := Enabled;
  665. end;
  666. procedure TCustomComboEdit.CMEnter(var Message: TMessage);
  667. begin
  668. if AutoSelect and not (csLButtonDown in ControlState) then SelectAll;
  669. inherited;
  670. end;
  671. procedure TCustomComboEdit.CNCtlColor(var Message: TMessage);
  672. var
  673. TextColor: Longint;
  674. begin
  675. inherited;
  676. if NewStyleControls then begin
  677. TextColor := ColorToRGB(Font.Color);
  678. if not Enabled and (ColorToRGB(Color) <> ColorToRGB(clGrayText)) then
  679. TextColor := ColorToRGB(clGrayText);
  680. SetTextColor(Message.WParam, TextColor);
  681. end;
  682. end;
  683. procedure TCustomComboEdit.WMSetFocus(var Message: TMessage);
  684. begin
  685. inherited;
  686. SetShowCaret;
  687. end;
  688. procedure TCustomComboEdit.SetShowCaret;
  689. const
  690. CaretWidth: array[Boolean] of Byte = (1, 2);
  691. begin
  692. CreateCaret(Handle, 0, CaretWidth[fsBold in Font.Style], GetTextHeight);
  693. ShowCaret(Handle);
  694. end;
  695. procedure TCustomComboEdit.EditButtonClick(Sender: TObject);
  696. begin
  697. if (not FReadOnly) or AlwaysEnable then ButtonClick;
  698. end;
  699. procedure TCustomComboEdit.DoClick;
  700. begin
  701. EditButtonClick(Self);
  702. end;
  703. procedure TCustomComboEdit.ButtonClick;
  704. begin
  705. if Assigned(FOnButtonClick) then FOnButtonClick(Self);
  706. end;
  707. procedure TCustomComboEdit.SelectAll;
  708. begin
  709. if DirectInput then inherited SelectAll;
  710. end;
  711. procedure TCustomComboEdit.SetDirectInput(Value: Boolean);
  712. begin
  713. inherited ReadOnly := not Value or FReadOnly;
  714. FDirectInput := Value;
  715. end;
  716. procedure TCustomComboEdit.WMPaste(var Message: TWMPaste);
  717. begin
  718. if not FDirectInput or ReadOnly then Exit;
  719. inherited;
  720. end;
  721. procedure TCustomComboEdit.WMCut(var Message: TWMCut);
  722. begin
  723. if not FDirectInput or ReadOnly then Exit;
  724. inherited;
  725. end;
  726. function TCustomComboEdit.GetReadOnly: Boolean;
  727. begin
  728. Result := FReadOnly;
  729. end;
  730. procedure TCustomComboEdit.SetReadOnly(Value: Boolean);
  731. begin
  732. if Value <> FReadOnly then begin
  733. FReadOnly := Value;
  734. inherited ReadOnly := Value or not FDirectInput;
  735. end;
  736. end;
  737. procedure TCustomComboEdit.SetAlignment(Value: TAlignment);
  738. begin
  739. if FAlignment <> Value then begin
  740. FAlignment := Value;
  741. RecreateWnd;
  742. end;
  743. end;
  744. function TCustomComboEdit.BtnWidthStored: Boolean;
  745. begin
  746. Result := ButtonWidth <> DefEditBtnWidth;
  747. end;
  748. { TFileDirEdit }
  749. procedure TFileDirEdit.DoBeforeDialog(var FileName: string;
  750. var Action: Boolean);
  751. begin
  752. if Assigned(FOnBeforeDialog) then FOnBeforeDialog(Self, FileName, Action);
  753. end;
  754. procedure TFileDirEdit.DoAfterDialog(var FileName: string;
  755. var Action: Boolean);
  756. begin
  757. if Assigned(FOnAfterDialog) then FOnAfterDialog(Self, FileName, Action);
  758. end;
  759. procedure TFileDirEdit.CreateHandle;
  760. begin
  761. inherited CreateHandle;
  762. if FAcceptFiles then SetDragAccept(True);
  763. end;
  764. procedure TFileDirEdit.DestroyWindowHandle;
  765. begin
  766. SetDragAccept(False);
  767. inherited DestroyWindowHandle;
  768. end;
  769. procedure TFileDirEdit.SetDragAccept(Value: Boolean);
  770. begin
  771. if not (csDesigning in ComponentState) and (Handle <> 0) then
  772. DragAcceptFiles(Handle, Value);
  773. end;
  774. procedure TFileDirEdit.SetAcceptFiles(Value: Boolean);
  775. begin
  776. if FAcceptFiles <> Value then begin
  777. SetDragAccept(Value);
  778. FAcceptFiles := Value;
  779. end;
  780. end;
  781. procedure TFileDirEdit.DisableSysErrors;
  782. begin
  783. FErrMode := SetErrorMode(SEM_NOOPENFILEERRORBOX or SEM_FAILCRITICALERRORS);
  784. end;
  785. procedure TFileDirEdit.EnableSysErrors;
  786. begin
  787. SetErrorMode(FErrMode);
  788. FErrMode := 0;
  789. end;
  790. procedure TFileDirEdit.WMDropFiles(var Msg: TWMDropFiles);
  791. var
  792. AFileName: array[0..255] of Char;
  793. I, Num: Cardinal;
  794. begin
  795. Msg.Result := 0;
  796. try
  797. Num := DragQueryFile(Msg.Drop, $FFFFFFFF, nil, 0);
  798. if Num > 0 then begin
  799. ClearFileList;
  800. for I := 0 to Num - 1 do begin
  801. DragQueryFile(Msg.Drop, I, PChar(@AFileName), Pred(SizeOf(AFileName)));
  802. ReceptFileDir(StrPas(AFileName));
  803. if not FMultipleDirs then Break;
  804. end;
  805. if Assigned(FOnDropFiles) then FOnDropFiles(Self);
  806. end;
  807. finally
  808. DragFinish(Msg.Drop);
  809. end;
  810. end;
  811. procedure TFileDirEdit.ClearFileList;
  812. begin
  813. end;
  814. { TFilenameEdit }
  815. function StrPAlloc(const S: string): PChar;
  816. begin
  817. Result := StrPCopy(StrAlloc(Length(S) + 1), S);
  818. end;
  819. function GetParamStr(P: PChar; var Param: string): PChar;
  820. var
  821. Len: Integer;
  822. Buffer: array[Byte] of Char;
  823. begin
  824. while True do
  825. begin
  826. while (P[0] <> #0) and (P[0] <= ' ') do Inc(P);
  827. if (P[0] = '"') and (P[1] = '"') then Inc(P, 2) else Break;
  828. end;
  829. Len := 0;
  830. while P[0] > ' ' do
  831. if P[0] = '"' then
  832. begin
  833. Inc(P);
  834. while (P[0] <> #0) and (P[0] <> '"') do
  835. begin
  836. Buffer[Len] := P[0];
  837. Inc(Len);
  838. Inc(P);
  839. end;
  840. if P[0] <> #0 then Inc(P);
  841. end else
  842. begin
  843. Buffer[Len] := P[0];
  844. Inc(Len);
  845. Inc(P);
  846. end;
  847. SetString(Param, Buffer, Len);
  848. Result := P;
  849. end;
  850. function ParamCountFromCommandLine(CmdLine: PChar): Integer;
  851. var
  852. S: string;
  853. P: PChar;
  854. begin
  855. P := CmdLine;
  856. Result := 0;
  857. while True do
  858. begin
  859. P := GetParamStr(P, S);
  860. if S = '' then Break;
  861. Inc(Result);
  862. end;
  863. end;
  864. function ParamStrFromCommandLine(CmdLine: PChar; Index: Integer): string;
  865. var
  866. P: PChar;
  867. begin
  868. P := CmdLine;
  869. while True do
  870. begin
  871. P := GetParamStr(P, Result);
  872. if (Index = 0) or (Result = '') then Break;
  873. Dec(Index);
  874. end;
  875. end;
  876. procedure SplitCommandLine(const CmdLine: string; var ExeName,
  877. Params: string);
  878. var
  879. Buffer: PChar;
  880. Cnt, I: Integer;
  881. S: string;
  882. begin
  883. ExeName := '';
  884. Params := '';
  885. Buffer := StrPAlloc(CmdLine);
  886. try
  887. Cnt := ParamCountFromCommandLine(Buffer);
  888. if Cnt > 0 then begin
  889. ExeName := ParamStrFromCommandLine(Buffer, 0);
  890. for I := 1 to Cnt - 1 do begin
  891. S := ParamStrFromCommandLine(Buffer, I);
  892. if Pos(' ', S) > 0 then S := '"' + S + '"';
  893. Params := Params + S;
  894. if I < Cnt - 1 then Params := Params + ' ';
  895. end;
  896. end;
  897. finally
  898. StrDispose(Buffer);
  899. end;
  900. end;
  901. function ClipFilename(const FileName: string): string;
  902. var
  903. Params: string;
  904. begin
  905. if FileExists(ApiPath(FileName)) then Result := FileName
  906. else SplitCommandLine(FileName, Result, Params);
  907. end;
  908. function ExtFilename(const FileName: string): string;
  909. begin
  910. if (Pos(' ', FileName) > 0) and (FileName[1] <> '"') then
  911. Result := Format('"%s"', [FileName])
  912. else Result := FileName;
  913. end;
  914. constructor TFilenameEdit.Create(AOwner: TComponent);
  915. begin
  916. inherited Create(AOwner);
  917. CreateEditDialog;
  918. end;
  919. procedure TFilenameEdit.CreateEditDialog;
  920. var
  921. NewDialog: TOpenDialog;
  922. begin
  923. NewDialog := nil;
  924. if Assigned(FOnCreateEditDialog) then
  925. FOnCreateEditDialog(Self, FDialogKind, NewDialog);
  926. if not Assigned(NewDialog) then
  927. case FDialogKind of
  928. dkOpen: NewDialog := TOpenDialog.Create(Self);
  929. dkOpenPicture: NewDialog := TOpenPictureDialog.Create(Self);
  930. dkSavePicture: NewDialog := TSavePictureDialog.Create(Self);
  931. else {dkSave} NewDialog := TSaveDialog.Create(Self);
  932. end;
  933. try
  934. if FDialog <> nil then begin
  935. with NewDialog do begin
  936. DefaultExt := FDialog.DefaultExt;
  937. FileEditStyle := FDialog.FileEditStyle;
  938. FileName := FDialog.FileName;
  939. Filter := FDialog.Filter;
  940. FilterIndex := FDialog.FilterIndex;
  941. InitialDir := FDialog.InitialDir;
  942. HistoryList := FDialog.HistoryList;
  943. Files.Assign(FDialog.Files);
  944. Options := FDialog.Options;
  945. Title := FDialog.Title;
  946. end;
  947. FDialog.Free;
  948. end
  949. else begin
  950. NewDialog.Title := SBrowse;
  951. NewDialog.Filter := SDefaultFilter;
  952. NewDialog.Options := [ofHideReadOnly];
  953. end;
  954. finally
  955. FDialog := NewDialog;
  956. end;
  957. end;
  958. function TFilenameEdit.IsCustomTitle: Boolean;
  959. begin
  960. Result := CompareStr(SBrowse, FDialog.Title) <> 0;
  961. end;
  962. function TFilenameEdit.IsCustomFilter: Boolean;
  963. begin
  964. Result := CompareStr(SDefaultFilter, FDialog.Filter) <> 0;
  965. end;
  966. procedure TFilenameEdit.ButtonClick;
  967. var
  968. Temp: string;
  969. Action: Boolean;
  970. begin
  971. inherited ButtonClick;
  972. Temp := inherited Text;
  973. Action := True;
  974. Temp := ClipFilename(Temp);
  975. DoBeforeDialog(Temp, Action);
  976. if not Action then Exit;
  977. if ValidFileName(Temp) then
  978. try
  979. if DirExists(ExtractFilePath(Temp)) then
  980. SetInitialDir(ExtractFilePath(Temp));
  981. if (ExtractFileName(Temp) = '') or
  982. not ValidFileName(ExtractFileName(Temp)) then Temp := '';
  983. FDialog.FileName := Temp;
  984. except
  985. { ignore any exceptions }
  986. end;
  987. FDialog.HelpContext := Self.HelpContext;
  988. DisableSysErrors;
  989. try
  990. Action := FDialog.Execute;
  991. finally
  992. EnableSysErrors;
  993. end;
  994. if Action then Temp := FDialog.FileName;
  995. if CanFocus then SetFocus;
  996. DoAfterDialog(Temp, Action);
  997. if Action then begin
  998. inherited Text := ExtFilename(Temp);
  999. SetInitialDir(ExtractFilePath(FDialog.FileName));
  1000. end;
  1001. end;
  1002. function TFilenameEdit.GetFileName: string;
  1003. begin
  1004. Result := ClipFilename(inherited Text);
  1005. end;
  1006. procedure TFilenameEdit.SetFileName(const Value: string);
  1007. begin
  1008. if (Value = '') or ValidFileName(ClipFilename(Value)) then begin
  1009. inherited Text := ExtFilename(Value);
  1010. ClearFileList;
  1011. end
  1012. else raise EComboEditError.CreateFmt(SInvalidFilename, [Value]);
  1013. end;
  1014. function TFilenameEdit.GetLongName: string;
  1015. begin
  1016. Result := ShortToLongFileName(FileName);
  1017. end;
  1018. function TFilenameEdit.GetShortName: string;
  1019. begin
  1020. Result := LongToShortFileName(FileName);
  1021. end;
  1022. procedure TFilenameEdit.ClearFileList;
  1023. begin
  1024. FDialog.Files.Clear;
  1025. end;
  1026. procedure TFilenameEdit.ReceptFileDir(const AFileName: string);
  1027. begin
  1028. if FMultipleDirs then begin
  1029. if FDialog.Files.Count = 0 then SetFileName(AFileName);
  1030. FDialog.Files.Add(AFileName);
  1031. end
  1032. else SetFileName(AFileName);
  1033. end;
  1034. function TFilenameEdit.GetDialogFiles: TStrings;
  1035. begin
  1036. Result := FDialog.Files;
  1037. end;
  1038. function TFilenameEdit.GetDefaultExt: TFileExt;
  1039. begin
  1040. Result := FDialog.DefaultExt;
  1041. end;
  1042. function TFilenameEdit.GetFileEditStyle: TFileEditStyle;
  1043. begin
  1044. Result := FDialog.FileEditStyle;
  1045. end;
  1046. function TFilenameEdit.GetFilter: string;
  1047. begin
  1048. Result := FDialog.Filter;
  1049. end;
  1050. function TFilenameEdit.GetFilterIndex: Integer;
  1051. begin
  1052. Result := FDialog.FilterIndex;
  1053. end;
  1054. function TFilenameEdit.GetInitialDir: string;
  1055. begin
  1056. Result := FDialog.InitialDir;
  1057. end;
  1058. function TFilenameEdit.GetHistoryList: TStrings;
  1059. begin
  1060. Result := FDialog.HistoryList;
  1061. end;
  1062. function TFilenameEdit.GetOptions: TOpenOptions;
  1063. begin
  1064. Result := FDialog.Options;
  1065. end;
  1066. function TFilenameEdit.GetDialogTitle: string;
  1067. begin
  1068. Result := FDialog.Title;
  1069. end;
  1070. procedure TFilenameEdit.SetDialogKind(Value: TFileDialogKind);
  1071. begin
  1072. if FDialogKind <> Value then begin
  1073. FDialogKind := Value;
  1074. CreateEditDialog;
  1075. end;
  1076. end;
  1077. procedure TFilenameEdit.SetOnCreateEditDialog(Value: TCreateEditDialogEvent);
  1078. begin
  1079. if @FOnCreateEditDialog <> @Value then
  1080. begin
  1081. FOnCreateEditDialog := Value;
  1082. // to recreate a dialog with every change is stupid way,
  1083. // but its done only once, so it is acceptable
  1084. CreateEditDialog;
  1085. end;
  1086. end;
  1087. procedure TFilenameEdit.SetDefaultExt(Value: TFileExt);
  1088. begin
  1089. FDialog.DefaultExt := Value;
  1090. end;
  1091. procedure TFilenameEdit.SetFileEditStyle(Value: TFileEditStyle);
  1092. begin
  1093. FDialog.FileEditStyle := Value;
  1094. end;
  1095. procedure TFilenameEdit.SetFilter(const Value: string);
  1096. begin
  1097. FDialog.Filter := Value;
  1098. end;
  1099. procedure TFilenameEdit.SetFilterIndex(Value: Integer);
  1100. begin
  1101. FDialog.FilterIndex := Value;
  1102. end;
  1103. procedure TFilenameEdit.SetInitialDir(const Value: string);
  1104. begin
  1105. FDialog.InitialDir := Value;
  1106. end;
  1107. procedure TFilenameEdit.SetHistoryList(Value: TStrings);
  1108. begin
  1109. FDialog.HistoryList := Value;
  1110. end;
  1111. procedure TFilenameEdit.SetOptions(Value: TOpenOptions);
  1112. begin
  1113. if Value <> FDialog.Options then begin
  1114. FDialog.Options := Value;
  1115. FMultipleDirs := ofAllowMultiSelect in FDialog.Options;
  1116. if not FMultipleDirs then ClearFileList;
  1117. end;
  1118. end;
  1119. procedure TFilenameEdit.SetDialogTitle(const Value: string);
  1120. begin
  1121. FDialog.Title := Value;
  1122. end;
  1123. { TDirectoryEdit }
  1124. constructor TDirectoryEdit.Create(AOwner: TComponent);
  1125. begin
  1126. inherited Create(AOwner);
  1127. end;
  1128. procedure TDirectoryEdit.ButtonClick;
  1129. var
  1130. Temp: string;
  1131. Action: Boolean;
  1132. begin
  1133. inherited ButtonClick;
  1134. Temp := Text;
  1135. Action := True;
  1136. DoBeforeDialog(Temp, Action);
  1137. if not Action then Exit;
  1138. if (Temp = '') then begin
  1139. if (InitialDir <> '') then Temp := InitialDir
  1140. else Temp := '\';
  1141. end;
  1142. if not DirExists(Temp) then Temp := '\';
  1143. DisableSysErrors;
  1144. try
  1145. Action := SelectDirectory(FDialogText, '', Temp);
  1146. finally
  1147. EnableSysErrors;
  1148. end;
  1149. if CanFocus then SetFocus;
  1150. DoAfterDialog(Temp, Action);
  1151. if Action then begin
  1152. SelText := '';
  1153. if (Text = '') or not MultipleDirs then Text := Temp
  1154. else Text := Text + ';' + Temp;
  1155. if (Temp <> '') and DirExists(Temp) then InitialDir := Temp;
  1156. end;
  1157. end;
  1158. procedure TDirectoryEdit.ReceptFileDir(const AFileName: string);
  1159. var
  1160. Temp: string;
  1161. begin
  1162. if FileExists(ApiPath(AFileName)) then Temp := ExtractFilePath(AFileName)
  1163. else Temp := AFileName;
  1164. if (Text = '') or not MultipleDirs then Text := Temp
  1165. else Text := Text + ';' + Temp;
  1166. end;
  1167. function TDirectoryEdit.GetLongName: string;
  1168. var
  1169. Temp: string;
  1170. Pos: Integer;
  1171. begin
  1172. if not MultipleDirs then Result := ShortToLongPath(Text)
  1173. else begin
  1174. Result := '';
  1175. Pos := 1;
  1176. while Pos <= Length(Text) do begin
  1177. Temp := ShortToLongPath(ExtractSubstr(Text, Pos, [';']));
  1178. if (Result <> '') and (Temp <> '') then Result := Result + ';';
  1179. Result := Result + Temp;
  1180. end;
  1181. end;
  1182. end;
  1183. function TDirectoryEdit.GetShortName: string;
  1184. var
  1185. Temp: string;
  1186. Pos: Integer;
  1187. begin
  1188. if not MultipleDirs then Result := LongToShortPath(Text)
  1189. else begin
  1190. Result := '';
  1191. Pos := 1;
  1192. while Pos <= Length(Text) do begin
  1193. Temp := LongToShortPath(ExtractSubstr(Text, Pos, [';']));
  1194. if (Result <> '') and (Temp <> '') then Result := Result + ';';
  1195. Result := Result + Temp;
  1196. end;
  1197. end;
  1198. end;
  1199. initialization
  1200. end.