TBXToolPals.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. unit TBXToolPals;
  2. // TBX Package
  3. // Copyright 2001-2004 Alex A. Denisov. All Rights Reserved
  4. // See TBX.chm for license and installation instructions
  5. //
  6. // Id: TBXToolPals.pas 7 2004-02-21 06:07:53Z $
  7. interface
  8. uses
  9. Windows, Messages, Classes, SysUtils, Controls, Forms, Graphics, TB2Item, TBX,
  10. TBXThemes;
  11. {$I TB2Ver.inc}
  12. type
  13. TRowColCount = 1..100;
  14. TTBXCustomToolPalette = class;
  15. TTPCalcSize = procedure(Sender: TTBXCustomToolPalette; Canvas: TCanvas;
  16. var AWidth, AHeight: Integer) of object;
  17. TTPGetCellVisible = procedure(Sender: TTBXCustomToolPalette;
  18. ACol, ARow: Integer; var Visible: Boolean) of object;
  19. TTPGetCellHint = procedure(Sender: TTBXCustomToolPalette;
  20. ACol, ARow: Integer; var HintText: string) of object;
  21. TTPDrawCellImage = procedure(Sender: TTBXCustomToolPalette; Canvas: TCanvas;
  22. ARect: TRect; ACol, ARow: Integer; Selected, Hot, Enabled: Boolean) of object;
  23. TTPCellClick = procedure(Sender: TTBXCustomToolPalette;
  24. var ACol, ARow: Integer; var AllowChange: Boolean) of object;
  25. TTBXToolPaletteOptions = set of (tpoCustomImages, tpoNoAutoSelect);
  26. TTBXCustomToolPalette = class(TTBXCustomItem)
  27. private
  28. FColCount: TRowColCount;
  29. FPaletteOptions: TTBXToolPaletteOptions;
  30. FRowCount: TRowColCount;
  31. FSelectedCell: TPoint;
  32. FOnCalcImageSize: TTPCalcSize;
  33. FOnChange: TNotifyEvent;
  34. FOnCellClick: TTPCellClick;
  35. FOnDrawCellImage: TTPDrawCellImage;
  36. FOnGetCellVisible: TTPGetCellVisible;
  37. FOnGetCellHint: TTPGetCellHint;
  38. procedure SetColCount(Value: TRowColCount);
  39. procedure SetPaletteOptions(Value: TTBXToolPaletteOptions);
  40. procedure SetRowCount(Value: TRowColCount);
  41. procedure SetSelectedCell(Value: TPoint);
  42. protected
  43. procedure DoCalcCellSize(Canvas: TCanvas; var AWidth, AHeight: Integer); virtual;
  44. procedure DoCalcImageSize(Canvas: TCanvas; var AWidth, AHeight: Integer); virtual;
  45. function DoCellClick(var ACol, ARow: Integer): Boolean; virtual;
  46. procedure DoChange; virtual;
  47. procedure DoDrawCellImage(Canvas: TCanvas; const ARect: TRect; ACol, ARow: Integer; ItemInfo: TTBXItemInfo); virtual;
  48. procedure DoGetCellVisible(ACol, ARow: Integer; var Visible: Boolean); virtual;
  49. procedure DoGetHint(ACell: TPoint; var HintText: string); virtual;
  50. function GetItemViewerClass(AView: TTBView): TTBItemViewerClass; override;
  51. procedure HandleClickCell(ACol, ARow: Integer); virtual;
  52. property ColCount: TRowColCount read FColCount write SetColCount default 1;
  53. property PaletteOptions: TTBXToolPaletteOptions read FPaletteOptions write SetPaletteOptions;
  54. property RowCount: TRowColCount read FRowCount write SetRowCount default 1;
  55. property SelectedCell: TPoint read FSelectedCell write SetSelectedCell;
  56. property OnChange: TNotifyEvent read FOnChange write FOnChange;
  57. property OnCalcImageSize: TTPCalcSize read FOnCalcImageSize write FOnCalcImageSize;
  58. property OnCellClick: TTPCellClick read FOnCellClick write FOnCellClick;
  59. property OnDrawCellImage: TTPDrawCellImage read FOnDrawCellImage write FOnDrawCellImage;
  60. property OnGetCellVisible: TTPGetCellVisible read FOnGetCellVisible write FOnGetCellVisible;
  61. property OnGetCellHint: TTPGetCellHint read FOnGetCellHint write FOnGetCellHint;
  62. public
  63. constructor Create(AOwner: TComponent); override;
  64. end;
  65. TTBXToolPalette = class(TTBXCustomToolPalette)
  66. public
  67. property SelectedCell;
  68. published
  69. property ColCount;
  70. property HelpContext;
  71. property Images;
  72. property Options;
  73. property PaletteOptions;
  74. property RowCount;
  75. property Stretch;
  76. property Visible;
  77. property OnChange;
  78. property OnCalcImageSize;
  79. property OnCellClick;
  80. property OnDrawCellImage;
  81. property OnGetCellHint;
  82. property OnGetCellVisible;
  83. end;
  84. TTBXToolViewer = class(TTBXItemViewer)
  85. private
  86. FCellHeight: Integer;
  87. FCellWidth: Integer;
  88. FColCount: Integer;
  89. FRowCount: Integer;
  90. FHotCell: TPoint;
  91. protected
  92. Indent: Integer;
  93. MouseIsDown: Boolean;
  94. procedure CalcCellSize(Canvas: TCanvas; var AWidth, AHeight: Integer); virtual;
  95. procedure CalcImageSize(Canvas: TCanvas; var AWidth, AHeight: Integer); virtual;
  96. procedure CalcSize(const Canvas: TCanvas; var AWidth, AHeight: Integer); override;
  97. function GetImageIndex(Col, Row: Integer): Integer;
  98. function GetCellAt(X, Y: Integer; out Col, Row: Integer): Boolean;
  99. function GetCellRect(ClientAreaRect: TRect; Col, Row: Integer): TRect; virtual;
  100. function GetHint(Col, Row: Integer): string;
  101. procedure CMHintShow(var Message: TCMHintShow); message CM_HINTSHOW;
  102. procedure DrawCell(Canvas: TCanvas; const CellRect: TRect; Col, Row: Integer; var ItemInfo: TTBXItemInfo);
  103. procedure DrawCellImage(Canvas: TCanvas; const ARect: TRect; Col, Row: Integer; ItemInfo: TTBXItemInfo); virtual;
  104. procedure Entering(OldSelected: TTBItemViewer); override;
  105. procedure InvalidateCell(ACol, ARow: Integer);
  106. function IsCellVisible(Cell: TPoint): Boolean; virtual;
  107. procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  108. procedure MouseDown(Shift: TShiftState; X, Y: Integer;var MouseDownOnMenu: Boolean); override;
  109. procedure MouseMove(X, Y: Integer); override;
  110. procedure MouseUp(X, Y: Integer; MouseWasDownOnMenu: Boolean); override;
  111. procedure Paint(const Canvas: TCanvas; const ClientAreaRect: TRect; IsHoverItem, IsPushed, UseDisabledShadow: Boolean); override;
  112. property CellHeight: Integer read FCellHeight;
  113. property CellWidth: Integer read FCellWidth;
  114. property ColCount: Integer read FColCount;
  115. property HotCell: TPoint read FHotCell;
  116. property RowCount: Integer read FRowCount;
  117. public
  118. constructor Create(AView: TTBView; AItem: TTBCustomItem; AGroupLevel: Integer); override;
  119. end;
  120. { TTBXCustomColorSet }
  121. TTBXCustomColorSet = class;
  122. TCSGetColorInfo = procedure(Sender: TTBXCustomColorSet; Col, Row: Integer;
  123. var Color: TColor; var Name: string) of object;
  124. TTBXCustomColorSet = class(TComponent)
  125. private
  126. FPalettes: TList;
  127. FColCount: Integer;
  128. FRowCount: Integer;
  129. FOnGetColorInfo: TCSGetColorInfo;
  130. procedure SetColCount(Value: Integer);
  131. procedure SetRowCount(Value: Integer);
  132. protected
  133. procedure UpdateSize(NewColCount, NewRowCount: Integer); virtual;
  134. function ColorToString(Color: TColor): string; virtual;
  135. procedure GetColorInfo(Col, Row: Integer; out Color: TColor; out ColorName: string); virtual;
  136. public
  137. constructor Create(AOwner: TComponent); override;
  138. destructor Destroy; override;
  139. function GetColor(Col, Row: Integer): TColor;
  140. function GetName(Col, Row: Integer): string;
  141. property ColCount: Integer read FColCount write SetColCount;
  142. property RowCount: Integer read FRowCount write SetRowCount;
  143. property OnGetColorInfo: TCSGetColorInfo read FOnGetColorInfo write FOnGetColorInfo;
  144. end;
  145. TTBXColorSet = class(TTBXCustomColorSet)
  146. published
  147. property ColCount;
  148. property RowCount;
  149. property OnGetColorInfo;
  150. end;
  151. TTBXColorPalette = class(TTBXCustomToolPalette)
  152. private
  153. FColor: TColor;
  154. FColorSet: TTBXCustomColorSet;
  155. FImageSize: Integer;
  156. procedure SetColorSet(Value: TTBXCustomColorSet);
  157. procedure SetColor(Value: TColor);
  158. protected
  159. procedure DoCalcImageSize(Canvas: TCanvas; var AWidth, AHeight: Integer); override;
  160. procedure DoChange; override;
  161. procedure DoGetCellVisible(ACol, ARow: Integer; var Visible: Boolean); override;
  162. procedure DoGetHint(ACell: TPoint; var HintText: string); override;
  163. procedure DoDrawCellImage(Canvas: TCanvas; const ARect: TRect; ACol, ARow: Integer; ItemInfo: TTBXItemInfo); override;
  164. function GetColorSet: TTBXCustomColorSet;
  165. function GetCellColor(ACol, ARow: Integer): TColor; virtual;
  166. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  167. public
  168. constructor Create(AOwner: TComponent); override;
  169. function FindCell(AColor: TColor): TPoint;
  170. function ColorToString(AColor: TColor): string;
  171. published
  172. property Color: TColor read FColor write SetColor default clNone;
  173. property ColorSet: TTBXCustomColorSet read FColorSet write SetColorSet;
  174. property HelpContext;
  175. property InheritOptions;
  176. property MaskOptions;
  177. property Options default [tboShowHint];
  178. property PaletteOptions;
  179. property Stretch;
  180. property Visible;
  181. property OnChange;
  182. property OnCellClick;
  183. property OnGetCellHint;
  184. end;
  185. implementation
  186. uses ImgList, UxTheme, Types, TBXUtils;
  187. var
  188. DefaultColorSet: TTBXCustomColorSet;
  189. type
  190. TTBViewAccess = class(TTBView);
  191. { TTBXCustomToolPalette }
  192. constructor TTBXCustomToolPalette.Create(AOwner: TComponent);
  193. begin
  194. inherited;
  195. FColCount := 1;
  196. FRowCount := 1;
  197. FSelectedCell.X := -1;
  198. // Options := Options + [tboToolbarStyle];
  199. end;
  200. procedure TTBXCustomToolPalette.DoCalcCellSize(Canvas: TCanvas; var AWidth, AHeight: Integer);
  201. begin
  202. end;
  203. procedure TTBXCustomToolPalette.DoCalcImageSize(Canvas: TCanvas; var AWidth, AHeight: Integer);
  204. begin
  205. if Assigned(FOnCalcImageSize) then FOnCalcImageSize(Self, Canvas, AWidth, AHeight);
  206. end;
  207. function TTBXCustomToolPalette.DoCellClick(var ACol, ARow: Integer): Boolean;
  208. begin
  209. Result := True;
  210. if Assigned(FOnCellClick) then FOnCellClick(Self, ACol, ARow, Result);
  211. end;
  212. procedure TTBXCustomToolPalette.DoChange;
  213. begin
  214. if Assigned(FOnChange) then FOnChange(Self);
  215. end;
  216. procedure TTBXCustomToolPalette.DoDrawCellImage(Canvas: TCanvas;
  217. const ARect: TRect; ACol, ARow: Integer; ItemInfo: TTBXItemInfo);
  218. begin
  219. if Assigned(FOnDrawCellImage) then
  220. begin
  221. FOnDrawCellImage(Self, Canvas, ARect, ACol, ARow, ItemInfo.Selected,
  222. ItemInfo.HoverKind <> hkNone, ItemInfo.Enabled);
  223. end;
  224. end;
  225. procedure TTBXCustomToolPalette.DoGetCellVisible(ACol, ARow: Integer; var Visible: Boolean);
  226. begin
  227. if Assigned(FOnGetCellVisible) then FOnGetCellVisible(Self, ACol, ARow, Visible);
  228. end;
  229. procedure TTBXCustomToolPalette.DoGetHint(ACell: TPoint; var HintText: string);
  230. begin
  231. if Assigned(FOnGetCellHint) then FOnGetCellHint(Self, ACell.X, ACell.Y, HintText);
  232. end;
  233. function TTBXCustomToolPalette.GetItemViewerClass(AView: TTBView): TTBItemViewerClass;
  234. begin
  235. Result := TTBXToolViewer;
  236. end;
  237. procedure TTBXCustomToolPalette.HandleClickCell(ACol, ARow: Integer);
  238. begin
  239. if DoCellClick(ACol, ARow) and not (tpoNoAutoSelect in PaletteOptions) then
  240. SelectedCell := Point(ACol, ARow);
  241. end;
  242. procedure TTBXCustomToolPalette.SetColCount(Value: TRowColCount);
  243. begin
  244. if FColCount <> Value then
  245. begin
  246. FColCount := Value;
  247. Change(True);
  248. end;
  249. end;
  250. procedure TTBXCustomToolPalette.SetPaletteOptions(Value: TTBXToolPaletteOptions);
  251. begin
  252. if FPaletteOptions <> Value then
  253. begin
  254. FPaletteOptions := Value;
  255. Change(True);
  256. end;
  257. end;
  258. procedure TTBXCustomToolPalette.SetRowCount(Value: TRowColCount);
  259. begin
  260. if FRowCount <> Value then
  261. begin
  262. FRowCount := Value;
  263. Change(True);
  264. end;
  265. end;
  266. procedure TTBXCustomToolPalette.SetSelectedCell(Value: TPoint);
  267. begin
  268. FSelectedCell := Value;
  269. Change(True);
  270. DoChange;
  271. end;
  272. { TTBXToolViewer }
  273. procedure TTBXToolViewer.CalcCellSize(Canvas: TCanvas; var AWidth, AHeight: Integer);
  274. begin
  275. CalcImageSize(Canvas, AWidth, AHeight);
  276. TTBXCustomToolPalette(Item).DoCalcCellSize(Canvas, AWidth, AHeight);
  277. Inc(AWidth, 6);
  278. Inc(AHeight, 6);
  279. end;
  280. procedure TTBXToolViewer.CalcImageSize(Canvas: TCanvas; var AWidth, AHeight: Integer);
  281. var
  282. ImgList: TCustomImageList;
  283. begin
  284. ImgList := GetImageList;
  285. if ImgList <> nil then
  286. begin
  287. AWidth := ImgList.Width;
  288. AHeight := ImgList.Height;
  289. end
  290. else
  291. begin
  292. AWidth := 16;
  293. AHeight := 16;
  294. end;
  295. TTBXCustomToolPalette(Item).DoCalcImageSize(Canvas, AWidth, AHeight);
  296. end;
  297. procedure TTBXToolViewer.CalcSize(const Canvas: TCanvas; var AWidth, AHeight: Integer);
  298. var
  299. CellWidth, CellHeight: Integer;
  300. begin
  301. if not IsToolbarStyle then with CurrentTheme do
  302. begin
  303. Indent :=
  304. GetPopupMargin(Self) + GetIntegerMetrics(Self, TMI_MENU_IMGTEXTSPACE) +
  305. GetIntegerMetrics(Self, TMI_MENU_LCAPTIONMARGIN) - 3;
  306. end
  307. else
  308. begin
  309. Indent := 0;
  310. end;
  311. FColCount := TTBXCustomToolPalette(Item).ColCount;
  312. FRowCount := TTBXCustomToolPalette(Item).RowCount;
  313. CalcCellSize(Canvas, CellWidth, CellHeight);
  314. AWidth := Indent + CellWidth * ColCount;
  315. if not IsToolbarStyle then Inc(AWidth, CurrentTheme.GetIntegerMetrics(Self, TMI_MENU_RCAPTIONMARGIN));
  316. AHeight := CellHeight * RowCount;
  317. if AWidth < 8 then AWidth := 8;
  318. if AHeight < 8 then AHeight := 8;
  319. end;
  320. procedure TTBXToolViewer.CMHintShow(var Message: TCMHintShow);
  321. var
  322. Col, Row: Integer;
  323. begin
  324. with Message.HintInfo^ do
  325. begin
  326. if GetCellAt(CursorPos.X - BoundsRect.Left, CursorPos.Y - BoundsRect.Top, Col, Row) then
  327. begin
  328. CursorRect := GetCellRect(CursorRect, Col, Row);
  329. HintStr := GetHint(Col, Row);
  330. end
  331. else HintStr := '';
  332. end;
  333. end;
  334. constructor TTBXToolViewer.Create(AView: TTBView; AItem: TTBCustomItem; AGroupLevel: Integer);
  335. begin
  336. inherited;
  337. FColCount := TTBXCustomToolPalette(AItem).ColCount;
  338. FRowCount := TTBXCustomToolPalette(AItem).RowCount;
  339. end;
  340. procedure TTBXToolViewer.DrawCell(Canvas: TCanvas; const CellRect: TRect;
  341. Col, Row: Integer; var ItemInfo: TTBXItemInfo);
  342. var
  343. ImageWidth, ImageHeight: Integer;
  344. R: TRect;
  345. begin
  346. CurrentTheme.PaintButton(Canvas, CellRect, ItemInfo);
  347. CalcImageSize(Canvas, ImageWidth, ImageHeight);
  348. R := Bounds((CellRect.Right + CellRect.Left - ImageWidth) div 2,
  349. (CellRect.Top + CellRect.Bottom - ImageHeight) div 2, ImageWidth, ImageHeight);
  350. DrawCellImage(Canvas, R, Col, Row, ItemInfo);
  351. end;
  352. procedure TTBXToolViewer.DrawCellImage(Canvas: TCanvas; const ARect: TRect;
  353. Col, Row: Integer; ItemInfo: TTBXItemInfo);
  354. var
  355. ImgIndex: Integer;
  356. ImgList: TCustomImageList;
  357. begin
  358. if not (tpoCustomImages in TTBXCustomToolPalette(Item).PaletteOptions) then
  359. begin
  360. ImgIndex := GetImageIndex(Col, Row);
  361. ImgList := GetImageList;
  362. if (ImgList <> nil) and (ImgIndex >= 0) and (ImgIndex < ImgList.Count) then
  363. CurrentTheme.PaintImage(Canvas, ARect, ItemInfo, ImgList, ImgIndex);
  364. end;
  365. TTBXCustomToolPalette(Item).DoDrawCellImage(Canvas, ARect, Col, Row, ItemInfo);
  366. end;
  367. procedure TTBXToolViewer.Entering(OldSelected: TTBItemViewer);
  368. begin
  369. FHotCell := Point(-1, 0);
  370. if (View is TTBXPopupView) and (OldSelected <> nil) then
  371. begin
  372. if OldSelected.Index > Index then
  373. begin
  374. FHotCell := Point(ColCount - 1, RowCount - 1);
  375. while (FHotCell.X > 0) and not IsCellVisible(FHotCell) do Dec(FHotCell.X);
  376. end
  377. else if OldSelected.Index < Index then
  378. FHotCell := Point(0, 0);
  379. end;
  380. inherited Entering(OldSelected);
  381. end;
  382. function TTBXToolViewer.GetCellAt(X, Y: Integer; out Col, Row: Integer): Boolean;
  383. begin
  384. { Returns true if there is a cell at (X,Y) point }
  385. if (CellWidth = 0) or (CellHeight = 0) then
  386. begin
  387. Col := 0;
  388. Row := 0;
  389. end
  390. else if not TTBXCustomToolPalette(Item).Stretch then
  391. begin
  392. Col := (X - Indent) div CellWidth;
  393. Row := Y div CellHeight;
  394. end
  395. else
  396. begin
  397. Col := (X - Indent) * ColCount div (BoundsRect.Right - BoundsRect.Left);
  398. Row := Y * RowCount div (BoundsRect.Bottom - BoundsRect.Top);
  399. end;
  400. Result := IsCellVisible(Point(Col, Row));
  401. end;
  402. function TTBXToolViewer.GetCellRect(ClientAreaRect: TRect; Col, Row: Integer): TRect;
  403. var
  404. W, H: Integer;
  405. begin
  406. with ClientAreaRect do
  407. if not TTBXCustomToolPalette(Item).Stretch then
  408. begin
  409. Result := Bounds(Left + Indent + Col * CellWidth, Top + Row * CellHeight, CellWidth, CellHeight)
  410. end
  411. else
  412. begin
  413. W := Right - Left;
  414. H := Bottom - Top;
  415. Result.Left := Left + Indent + W * Col div ColCount;
  416. Result.Top := Top + H * Row div RowCount;
  417. Result.Right := Left + W * (Col + 1) div ColCount;
  418. Result.Bottom := Top + H * (Row + 1) div RowCount;
  419. end;
  420. end;
  421. function TTBXToolViewer.GetHint(Col, Row: Integer): string;
  422. begin
  423. Result := '';
  424. TTBXCustomToolPalette(Item).DoGetHint(Point(Col, Row), Result);
  425. end;
  426. function TTBXToolViewer.GetImageIndex(Col, Row: Integer): Integer;
  427. begin
  428. Result := Col + Row * ColCount;
  429. end;
  430. procedure TTBXToolViewer.InvalidateCell(ACol, ARow: Integer);
  431. var
  432. R: TRect;
  433. begin
  434. R := GetCellRect(BoundsRect, ACol, ARow);
  435. InvalidateRect(View.Window.Handle, @R, False);
  436. end;
  437. function TTBXToolViewer.IsCellVisible(Cell: TPoint): Boolean;
  438. var
  439. ImgList: TCustomImageList;
  440. begin
  441. Result := (Cell.X >= 0) and (Cell.Y >= 0) and (Cell.X < ColCount) and (Cell.Y < RowCount);
  442. if Result then
  443. begin
  444. if not (tpoCustomImages in TTBXCustomToolPalette(Item).PaletteOptions) then
  445. begin
  446. ImgList := GetImageList;
  447. if ImgList <> nil then Result := (Cell.X + Cell.Y * ColCount) < ImgList.Count;
  448. end;
  449. TTBXCustomToolPalette(Item).DoGetCellVisible(Cell.X, Cell.Y, Result);
  450. end;
  451. end;
  452. procedure TTBXToolViewer.KeyDown(var Key: Word; Shift: TShiftState);
  453. var
  454. OldPos, Pos: TPoint;
  455. begin
  456. if IsCellVisible(HotCell) then OldPos := HotCell
  457. else if IsCellVisible(TTBXCustomToolPalette(Item).SelectedCell) then
  458. OldPos := TTBXCustomToolPalette(Item).SelectedCell
  459. else OldPos.X := -1;
  460. if OldPos.X >= 0 then
  461. begin
  462. Pos := OldPos;
  463. case Key of
  464. VK_LEFT:
  465. begin
  466. Dec(Pos.X);
  467. if Pos.X < 0 then
  468. begin
  469. Pos.X := ColCount - 1;
  470. Dec(Pos.Y);
  471. end;
  472. end;
  473. VK_UP: Dec(Pos.Y);
  474. VK_RIGHT:
  475. begin
  476. Inc(Pos.X);
  477. if Pos.X >= ColCount then
  478. begin
  479. Pos.X := 0;
  480. Inc(Pos.Y);
  481. end;
  482. end;
  483. VK_DOWN: Inc(Pos.Y);
  484. VK_PRIOR: Pos.Y := 0;
  485. VK_NEXT: Pos.Y := RowCount - 1;
  486. VK_HOME: Pos.X := 0;
  487. VK_END: Pos.Y := ColCount - 1;
  488. VK_RETURN:
  489. if IsCellVisible(HotCell) then
  490. begin
  491. TTBXCustomToolPalette(Item).HandleClickCell(HotCell.X, HotCell.Y);
  492. Exit;
  493. end;
  494. else
  495. inherited;
  496. Exit;
  497. end;
  498. end
  499. else
  500. begin
  501. OldPos := Point(-1, 0);
  502. Pos := Point(0, 0);
  503. end;
  504. if ((OldPos.X <> Pos.X) or (OldPos.Y <> Pos.Y)) and IsCellVisible(Pos) then
  505. begin
  506. Key := 0;
  507. FHotCell := Pos;
  508. TTBXCustomToolPalette(Item).Change(False);
  509. end;
  510. end;
  511. procedure TTBXToolViewer.MouseDown(Shift: TShiftState; X, Y: Integer; var MouseDownOnMenu: Boolean);
  512. begin
  513. MouseIsDown := True;
  514. MouseMove(X, Y);
  515. inherited;
  516. View.SetCapture;
  517. end;
  518. procedure TTBXToolViewer.MouseMove(X, Y: Integer);
  519. var
  520. OldHotCell: TPoint;
  521. begin
  522. OldHotCell := HotCell;
  523. if not GetCellAt(X, Y, FHotCell.X, FHotCell.Y) then FHotCell := Point(-1, 0);
  524. if (HotCell.X <> OldHotCell.X) or (HotCell.Y <> OldHotCell.Y) then
  525. begin
  526. with TTBXCustomToolPalette(Item) do
  527. begin
  528. if Show and not IsRectEmpty(BoundsRect) and
  529. not (Item is TTBControlItem) then
  530. begin
  531. Include(State, tbisInvalidated);
  532. InvalidateCell(OldHotCell.X, OldHotCell.Y);
  533. InvalidateCell(HotCell.X, HotCell.Y);
  534. end;
  535. end;
  536. end;
  537. end;
  538. procedure TTBXToolViewer.MouseUp(X, Y: Integer; MouseWasDownOnMenu: Boolean);
  539. var
  540. Col, Row: Integer;
  541. DAD: TTBDoneActionData;
  542. begin
  543. MouseIsDown := False;
  544. if GetCellAt(X, Y, Col, Row) then
  545. TTBXCustomToolPalette(Item).HandleClickCell(Col, Row);
  546. DAD := TTBViewAccess(TTBViewAccess(View).GetRootView).DoneActionData;
  547. DAD.ClickItem := Item;
  548. DAD.DoneAction := tbdaClickItem;
  549. DAD.Sound := True;
  550. TTBViewAccess(TTBViewAccess(View).GetRootView).DoneActionData := DAD;
  551. inherited;
  552. end;
  553. procedure TTBXToolViewer.Paint(const Canvas: TCanvas; const ClientAreaRect: TRect;
  554. IsHoverItem, IsPushed, UseDisabledShadow: Boolean);
  555. const
  556. CDesigning: array [Boolean] of Integer = (0, IO_DESIGNING);
  557. var
  558. I, J: Integer;
  559. ItemInfo: TTBXItemInfo;
  560. Hover: TTBXHoverKind;
  561. R, CellRect: TRect;
  562. begin
  563. FillChar(ItemInfo, SizeOf(TTBXItemInfo), 0);
  564. ItemInfo.ViewType := GetViewType(View);
  565. ItemInfo.ItemOptions := CDesigning[csDesigning in Item.ComponentState];
  566. ItemInfo.Enabled := Item.Enabled or View.Customizing;
  567. ItemInfo.Pushed := False;
  568. ItemInfo.Selected := False;
  569. ItemInfo.ImageShown := True;
  570. with ItemInfo do CalcImageSize(Canvas, ImageWidth, ImageHeight);
  571. ItemInfo.HoverKind := hkNone;
  572. if not IsToolbarStyle then ItemInfo.PopupMargin := GetPopupMargin(Self);
  573. if not IsToolbarStyle then with CurrentTheme do
  574. begin
  575. R := ClientAreaRect;
  576. CurrentTheme.PaintMenuItemFrame(Canvas, R, ItemInfo);
  577. end;
  578. CalcCellSize(Canvas, FCellWidth, FCellHeight);
  579. if IsHoverItem then
  580. begin
  581. if not ItemInfo.Enabled and not View.MouseOverSelected then Hover := hkKeyboardHover
  582. else if ItemInfo.Enabled then Hover := hkMouseHover
  583. else Hover := hkNone;
  584. end
  585. else
  586. Hover := hkNone;
  587. for J := 0 to RowCount - 1 do
  588. for I := 0 to ColCount - 1 do
  589. begin
  590. if IsCellVisible(Point(I, J)) then
  591. begin
  592. if (Hover <> hkNone) and (HotCell.X = I) and (HotCell.Y = J) then
  593. begin
  594. ItemInfo.HoverKind := Hover;
  595. if IsPushed then ItemInfo.Pushed := True
  596. end
  597. else
  598. begin
  599. ItemInfo.HoverKind := hkNone;
  600. ItemInfo.Pushed := False;
  601. end;
  602. with TTBXCustomToolPalette(Item) do
  603. if (SelectedCell.X = I) and (SelectedCell.Y = J) then
  604. ItemInfo.Selected := True
  605. else
  606. ItemInfo.Selected := False;
  607. CellRect := GetCellRect(ClientAreaRect, I, J);
  608. DrawCell(Canvas, CellRect, I, J, ItemInfo);
  609. end;
  610. end;
  611. end;
  612. //----------------------------------------------------------------------------//
  613. { TTBXCustomColorSet }
  614. constructor TTBXCustomColorSet.Create(AOwner: TComponent);
  615. begin
  616. inherited;
  617. FPalettes := TList.Create;
  618. end;
  619. destructor TTBXCustomColorSet.Destroy;
  620. begin
  621. FPalettes.Free;
  622. inherited;
  623. end;
  624. function TTBXCustomColorSet.GetColor(Col, Row: Integer): TColor;
  625. var
  626. Dummy: string;
  627. begin
  628. GetColorInfo(Col, Row, Result, Dummy);
  629. end;
  630. procedure TTBXCustomColorSet.GetColorInfo(Col, Row: Integer; out Color: TColor; out ColorName: string);
  631. begin
  632. Color := clNone;
  633. SetLength(ColorName, 0);
  634. if Assigned(FOnGetColorInfo) then FOnGetColorInfo(Self, Col, Row, Color, ColorName);
  635. end;
  636. function TTBXCustomColorSet.ColorToString(Color: TColor): string;
  637. var
  638. I, J: Integer;
  639. C: TColor;
  640. N: string;
  641. function GetRGB(C: TColor): TColor;
  642. begin
  643. Result := (C and $FF00) + C shr 16 + (C and $FF shl 16);
  644. end;
  645. begin
  646. if Color = clNone then Result := 'None'
  647. else
  648. begin
  649. if Color < 0 then Color := GetSysColor(Color and $000000FF);
  650. Color := Color and $00FFFFFF;
  651. for J := 0 to RowCount - 1 do
  652. for I := 0 to ColCount - 1 do
  653. begin
  654. GetColorInfo(I, J, C, N);
  655. if C <> clNone then
  656. begin
  657. if C < 0 then C := GetSysColor(C and $000000FF);
  658. C := C and $00FFFFFF;
  659. if C = Color then
  660. begin
  661. Result := N;
  662. if Length(N) = 0 then Result := '#' + IntToHex(GetRGB(Color), 6);
  663. Exit;
  664. end
  665. end;
  666. end;
  667. Result := '#' + IntToHex(GetRGB(Color), 6);
  668. end;
  669. end;
  670. function TTBXCustomColorSet.GetName(Col, Row: Integer): string;
  671. var
  672. Dummy: TColor;
  673. begin
  674. GetColorInfo(Col, Row, Dummy, Result);
  675. end;
  676. procedure TTBXCustomColorSet.SetColCount(Value: Integer);
  677. begin
  678. UpdateSize(Value, RowCount);
  679. end;
  680. procedure TTBXCustomColorSet.SetRowCount(Value: Integer);
  681. begin
  682. UpdateSize(ColCount, Value);
  683. end;
  684. procedure TTBXCustomColorSet.UpdateSize(NewColCount, NewRowCount: Integer);
  685. var
  686. I: Integer;
  687. begin
  688. FColCount := NewColCount;
  689. FRowCount := NewRowCount;
  690. for I := 0 to FPalettes.Count - 1 do
  691. with TTBXColorPalette(FPalettes[I]) do
  692. begin
  693. ColCount := Self.ColCount;
  694. RowCount := Self.RowCount;
  695. end;
  696. end;
  697. //----------------------------------------------------------------------------//
  698. { TTBXColorPalette }
  699. function TTBXColorPalette.ColorToString(AColor: TColor): string;
  700. begin
  701. Result := GetColorSet.ColorToString(AColor);
  702. end;
  703. constructor TTBXColorPalette.Create(AOwner: TComponent);
  704. begin
  705. inherited;
  706. ColCount := DefaultColorSet.ColCount;
  707. RowCount := DefaultColorSet.RowCount;
  708. Options := Options + [tboShowHint];
  709. FColor := clNone;
  710. PaletteOptions := PaletteOptions + [tpoCustomImages];
  711. FImageSize := -1;
  712. end;
  713. procedure TTBXColorPalette.DoCalcImageSize(Canvas: TCanvas; var AWidth, AHeight: Integer);
  714. begin
  715. if FImageSize < 0 then
  716. begin
  717. FImageSize := TBXScaleByTextHeightRunTime(Canvas, 12);
  718. end;
  719. AWidth := FImageSize;
  720. AHeight := FImageSize;
  721. end;
  722. procedure TTBXColorPalette.DoChange;
  723. begin
  724. if SelectedCell.X >= 0 then
  725. FColor := GetCellColor(SelectedCell.X, SelectedCell.Y);
  726. inherited;
  727. end;
  728. procedure TTBXColorPalette.DoDrawCellImage(Canvas: TCanvas;
  729. const ARect: TRect; ACol, ARow: Integer; ItemInfo: TTBXItemInfo);
  730. var
  731. R: TRect;
  732. begin
  733. R := ARect;
  734. Canvas.Brush.Color := clBtnShadow;
  735. Canvas.FrameRect(R);
  736. InflateRect(R, -1, -1);
  737. if ItemInfo.Enabled then
  738. begin
  739. Canvas.Brush.Color := GetCellColor(ACol, ARow);
  740. Canvas.FillRect(R);
  741. end;
  742. end;
  743. procedure TTBXColorPalette.DoGetCellVisible(ACol, ARow: Integer; var Visible: Boolean);
  744. begin
  745. Visible := GetCellColor(ACol, ARow) <> clNone;
  746. end;
  747. procedure TTBXColorPalette.DoGetHint(ACell: TPoint; var HintText: string);
  748. begin
  749. HintText := GetColorSet.GetName(ACell.X, ACell.Y);
  750. end;
  751. function TTBXColorPalette.FindCell(AColor: TColor): TPoint;
  752. var
  753. I, J: Integer;
  754. C: TColor;
  755. begin
  756. if AColor <> clNone then AColor := ColorToRGB(AColor);
  757. for J := 0 to RowCount - 1 do
  758. for I := 0 to ColCount - 1 do
  759. begin
  760. C := GetCellColor(I, J);
  761. if C <> clNone then C := ColorToRGB(C);
  762. if C = AColor then
  763. begin
  764. Result.X := I;
  765. Result.Y := J;
  766. Exit;
  767. end;
  768. end;
  769. Result.X := -1;
  770. Result.Y := 0;
  771. end;
  772. function TTBXColorPalette.GetCellColor(ACol, ARow: Integer): TColor;
  773. begin
  774. Result := GetColorSet.GetColor(ACol, ARow);
  775. end;
  776. function TTBXColorPalette.GetColorSet: TTBXCustomColorSet;
  777. begin
  778. if FColorSet = nil then Result := DefaultColorSet
  779. else Result := FColorSet;
  780. end;
  781. procedure TTBXColorPalette.Notification(AComponent: TComponent; Operation: TOperation);
  782. begin
  783. inherited;
  784. if (AComponent = FColorSet) and (Operation = opRemove) then ColorSet := nil;
  785. end;
  786. procedure TTBXColorPalette.SetColor(Value: TColor);
  787. begin
  788. FColor := Value;
  789. SelectedCell := FindCell(Value);
  790. end;
  791. procedure TTBXColorPalette.SetColorSet(Value: TTBXCustomColorSet);
  792. begin
  793. if FColorSet <> Value then
  794. begin
  795. if Assigned(FColorSet) then FColorSet.FPalettes.Remove(Self);
  796. FColorSet := Value;
  797. if Assigned(Value) then
  798. begin
  799. Value.FreeNotification(Self);
  800. Value.FPalettes.Add(Self);
  801. ColCount := Value.ColCount;
  802. RowCount := Value.RowCount;
  803. end
  804. else
  805. begin
  806. ColCount := DefaultColorSet.ColCount;
  807. RowCount := DefaultColorSet.RowCount;
  808. end;
  809. Change(True);
  810. end;
  811. end;
  812. { TTBXDefaultColorSet }
  813. type
  814. TTBXDefaultColorSet = class (TTBXCustomColorSet)
  815. protected
  816. procedure GetColorInfo(Col, Row: Integer; out Color: TColor; out ColorName: string); override;
  817. public
  818. constructor Create(AOwner: TComponent); override;
  819. end;
  820. procedure TTBXDefaultColorSet.GetColorInfo(Col, Row: Integer; out Color: TColor; out ColorName: string);
  821. procedure Clr(const AName: string; AColor: TColor);
  822. begin
  823. Color := AColor;
  824. ColorName := AName;
  825. end;
  826. begin
  827. Color := clNone;
  828. Name := '';
  829. case Row of
  830. 0:
  831. case Col of
  832. 0: Clr('Black', $000000);
  833. 1: Clr('Brown', $003399);
  834. 2: Clr('Olive Green', $003333);
  835. 3: Clr('Dark Green', $003300);
  836. 4: Clr('Dark Teal', $663300);
  837. 5: Clr('Dark blue', $800000);
  838. 6: Clr('Indigo', $993333);
  839. 7: Clr('Gray-80%', $333333);
  840. end;
  841. 1:
  842. case Col of
  843. 0: Clr('Dark Red', $000080);
  844. 1: Clr('Orange', $0066FF);
  845. 2: Clr('Dark Yellow', $008080);
  846. 3: Clr('Green', $008000);
  847. 4: Clr('Teal', $808000);
  848. 5: Clr('Blue', $FF0000);
  849. 6: Clr('Blue-Gray', $996666);
  850. 7: Clr('Gray-50%', $808080);
  851. end;
  852. 2:
  853. case Col of
  854. 0: Clr('Red', $0000FF);
  855. 1: Clr('Light Orange', $0099FF);
  856. 2: Clr('Lime', $00CC99);
  857. 3: Clr('Sea Green', $669933);
  858. 4: Clr('Aqua', $CCCC33);
  859. 5: Clr('Light Blue', $FF6633);
  860. 6: Clr('Violet', $800080);
  861. 7: Clr('Gray-40%', $969696);
  862. end;
  863. 3:
  864. case Col of
  865. 0: Clr('Pink', $FF00FF);
  866. 1: Clr('Gold', $00CCFF);
  867. 2: Clr('Yellow', $00FFFF);
  868. 3: Clr('Bright Green', $00FF00);
  869. 4: Clr('Turquoise', $FFFF00);
  870. 5: Clr('Sky Blue', $FFCC00);
  871. 6: Clr('Plum', $663399);
  872. 7: Clr('Gray-25%', $C0C0C0);
  873. end;
  874. 4:
  875. case Col of
  876. 0: Clr('Rose', $CC99FF);
  877. 1: Clr('Tan', $99CCFF);
  878. 2: Clr('Light Yellow', $99FFFF);
  879. 3: Clr('Light Green', $CCFFCC);
  880. 4: Clr('Light Turquoise', $FFFFCC);
  881. 5: Clr('Pale Blue', $FFCC99);
  882. 6: Clr('Lavender', $FF99CC);
  883. 7: Clr('White', $FFFFFF);
  884. end;
  885. end;
  886. end;
  887. constructor TTBXDefaultColorSet.Create(AOwner: TComponent);
  888. begin
  889. inherited;
  890. FColCount := 8;
  891. FRowCount := 5;
  892. end;
  893. initialization
  894. DefaultColorSet := TTBXDefaultColorSet.Create(nil);
  895. finalization
  896. DefaultColorSet.Free;
  897. end.