TBXToolPals.pas 30 KB

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