ComboEdit.pas 34 KB

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