1
0

ComboEdit.pas 30 KB

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