UnixPathComboBox.pas 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. unit UnixPathComboBox;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  5. StdCtrls;
  6. type
  7. TUnixPathComboBox = class(TCustomComboBox)
  8. private
  9. { Private declarations }
  10. FUnixPath: string;
  11. FRootName: string;
  12. FImageList: TImageList;
  13. FNotifyChange: boolean;
  14. F_NotifyChange: boolean;
  15. procedure SetUnixPath(AUnixPath: string);
  16. procedure SetRootName(ARootName: string);
  17. procedure UpdateItems;
  18. procedure CNDrawItem(var Message: TWMDrawItem); message CN_DrawItem;
  19. procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
  20. procedure WMCHAR(var Message: TMessage); message WM_CHAR;
  21. protected
  22. { Protected declarations }
  23. procedure CreateWnd; override;
  24. procedure Change; override;
  25. procedure Click; override;
  26. public
  27. { Public declarations }
  28. constructor Create(AOwner: TComponent); override;
  29. destructor Destroy; override;
  30. property NotifyChange: Boolean read F_NotifyChange write F_NotifyChange;
  31. published
  32. { Published declarations }
  33. property Align;
  34. property Color;
  35. property Ctl3D;
  36. property DragMode;
  37. property DragCursor;
  38. property DropDownCount;
  39. property Anchors;
  40. property Enabled;
  41. property Font;
  42. property ParentColor;
  43. property ParentCtl3D;
  44. property ParentFont;
  45. property ParentShowHint;
  46. property PopupMenu;
  47. property ShowHint;
  48. property TabOrder;
  49. property TabStop;
  50. property Visible;
  51. property OnClick;
  52. property OnChange;
  53. property OnDblClick;
  54. property OnDragDrop;
  55. property OnDragOver;
  56. property OnDropDown;
  57. property OnEndDrag;
  58. property OnEnter;
  59. property OnExit;
  60. property OnKeyDown;
  61. property OnKeyPress;
  62. property OnKeyUp;
  63. property OnStartDrag;
  64. property UnixPath: string read FUnixPath write SetUnixPath;
  65. property RootName: string read FRootName write SetRootName;
  66. end;
  67. procedure Register;
  68. implementation
  69. uses
  70. ShellAPI, ImgList;
  71. procedure Register;
  72. begin
  73. RegisterComponents('My', [TUnixPathComboBox]);
  74. end;
  75. { TUnixPathComboBox }
  76. constructor TUnixPathComboBox.Create(AOwner: TComponent);
  77. var
  78. sfi: TSHFileInfo;
  79. begin
  80. inherited Create(AOwner);
  81. Style := csOwnerDrawFixed;
  82. FImageList := TImageList.create(self);
  83. FImageList.handle :=
  84. SHGetFileInfo('',0,sfi,sizeof(tshfileinfo), shgfi_sysiconindex or shgfi_smallicon);
  85. FImageList.shareimages := true;
  86. FImageList.BlendColor := clHighlight;
  87. FRootName := '/ <root>';
  88. F_NotifyChange := True;
  89. end;
  90. procedure TUnixPathComboBox.SetUnixPath(AUnixPath: string);
  91. begin
  92. if FUnixPath <> AUnixPath then
  93. begin
  94. FUnixPath := AUnixPath;
  95. UpdateItems;
  96. FNotifyChange := true;
  97. Change; {The only way to notify OnChange event}
  98. FNotifyChange := false;
  99. end;
  100. end;
  101. procedure TUnixPathComboBox.UpdateItems;
  102. var
  103. Temp: string;
  104. P: Integer;
  105. begin
  106. with Items do
  107. begin
  108. BeginUpdate;
  109. Clear;
  110. try
  111. Temp := FUnixPath;
  112. while Temp <> '' do
  113. begin
  114. P := LastDelimiter('/', Temp);
  115. if P < Length(Temp) then Insert(0, Copy(Temp, P+1, Length(Temp)-(P+1)+1));
  116. SetLength(Temp, P-1);
  117. if Length(Temp) = 0 then
  118. Insert(0, FRootName);
  119. end;
  120. finally
  121. ItemIndex := Count-1;
  122. EndUpdate;
  123. end;
  124. end;
  125. end;
  126. procedure TUnixPathComboBox.SetRootName(ARootName: string);
  127. begin
  128. if FRootName <> ARootName then
  129. begin
  130. FRootName := ARootName;
  131. if Items.Count > 0 then Items[0] := FRootName
  132. else UpdateItems;
  133. end;
  134. end;
  135. function GetIconIndex(AFile: string; Attrs, Attrs2: Cardinal): Integer;
  136. var
  137. SFI: TSHFileInfo;
  138. begin
  139. SHGetFileInfo(PChar(AFile), Attrs, SFI, SizeOf(TSHFileInfo),
  140. SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES or Attrs2);
  141. Result := SFI.iIcon;
  142. end;
  143. procedure TUnixPathComboBox.CNDrawItem(var Message: TWMDrawItem);
  144. var
  145. Icon: Integer;
  146. begin
  147. with Message.DrawItemStruct^ do
  148. begin
  149. Canvas.Handle := hDC;
  150. Canvas.Font := Font;
  151. Canvas.Brush := Brush;
  152. if Integer(ItemID) < 0 then Canvas.FillRect(rcItem)
  153. else
  154. with Canvas do
  155. begin
  156. // This next line prevents 'edit selection' text from being indented as well.
  157. if (rcItem.top <> 3) then rcItem.left := rcItem.left + 10*Integer(ItemID);
  158. if Integer(ItemID) < Items.Count-1 then
  159. Icon := GetIconIndex('dummy', FILE_ATTRIBUTE_NORMAL or FILE_ATTRIBUTE_DIRECTORY, 0)
  160. else
  161. Icon := GetIconIndex('dummy', FILE_ATTRIBUTE_NORMAL or FILE_ATTRIBUTE_DIRECTORY, shgfi_openicon);
  162. if (Integer(itemID) >= 0) and (ods_Selected and ItemState <> 0) then
  163. FimageList.DrawingStyle := dsFocus
  164. else
  165. FimageList.DrawingStyle := dsNormal;
  166. FillRect(rcItem);
  167. FImageList.draw(canvas,rcItem.left+2,rcItem.top, Icon);
  168. rcItem.left := rcItem.left+FImageList.width+4;
  169. if (Integer(itemID) >= 0) and (ods_Selected and ItemState <> 0) then
  170. begin
  171. rcItem.right := rcItem.left+textwidth(items[ItemID])+4;
  172. Canvas.Brush.Color := clHighlight;
  173. Canvas.Font.Color := clHighlightText;
  174. FillRect(rcItem);
  175. end;
  176. TextOut(rcItem.left+2, rcItem.top, items[ItemID]);
  177. end; {with canvas}
  178. if (ods_Focus and ItemState <> 0) then DrawFocusRect(hDC, rcItem);
  179. Canvas.Handle := 0;
  180. end;
  181. end;
  182. destructor TUnixPathComboBox.Destroy;
  183. begin
  184. FImageList.handle := 0;
  185. FImageList.free;
  186. inherited Destroy;
  187. end;
  188. procedure TUnixPathComboBox.CreateWnd;
  189. begin
  190. inherited CreateWnd;
  191. UpdateItems;
  192. end;
  193. procedure TUnixPathComboBox.CMFontChanged(var Message: TMessage);
  194. function GetItemHeight(Font: TFont): Integer;
  195. var
  196. DC: HDC;
  197. SaveFont: HFont;
  198. Metrics: TTextMetric;
  199. begin
  200. DC := GetDC(0);
  201. SaveFont := SelectObject(DC, Font.Handle);
  202. GetTextMetrics(DC, Metrics);
  203. SelectObject(DC, SaveFont);
  204. ReleaseDC(0, DC);
  205. Result := Metrics.tmHeight;
  206. end;
  207. var
  208. IHeight: integer;
  209. begin
  210. inherited;
  211. IHeight := GetItemHeight(Font)+2;
  212. if IHeight < FImageList.height then IHeight := FImageList.height;
  213. ItemHeight := IHeight;
  214. RecreateWnd;
  215. end;
  216. procedure TUnixPathComboBox.Change;
  217. begin
  218. if FNotifyChange and F_NotifyChange then inherited Change;
  219. end;
  220. procedure TUnixPathComboBox.Click;
  221. var
  222. NewPath: string;
  223. i: Integer;
  224. begin
  225. inherited Click;
  226. if (ItemIndex >= 0) and (ItemIndex < Items.Count-1) and
  227. (sendmessage(handle,CB_GETDROPPEDSTATE,0,0)=0) then
  228. begin
  229. NewPath := '/';
  230. for i := 1 to ItemIndex do
  231. begin
  232. NewPath := NewPath + Items[i];
  233. if i < ItemIndex then NewPath := NewPath + '/';
  234. end;
  235. UnixPath := NewPath;
  236. end;
  237. end;
  238. procedure TUnixPathComboBox.WMCHAR(var Message: TMessage);
  239. begin
  240. inherited;
  241. if (TWMKey(Message).CharCode = VK_RETURN) or
  242. (TWMKey(Message).CharCode = VK_ESCAPE) then Click;
  243. end;
  244. end.