TBXToolPals.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. begin
  304. Indent :=
  305. GetPopupMargin(Self) + GetIntegerMetrics(Self, TMI_MENU_IMGTEXTSPACE) +
  306. GetIntegerMetrics(Self, TMI_MENU_LCAPTIONMARGIN) - 3;
  307. end
  308. else
  309. begin
  310. Indent := 0;
  311. end;
  312. FColCount := TTBXCustomToolPalette(Item).ColCount;
  313. FRowCount := TTBXCustomToolPalette(Item).RowCount;
  314. CalcCellSize(Canvas, CellWidth, CellHeight);
  315. AWidth := Indent + CellWidth * ColCount;
  316. if not IsToolbarStyle then Inc(AWidth, CurrentTheme.GetIntegerMetrics(Self, TMI_MENU_RCAPTIONMARGIN));
  317. AHeight := CellHeight * RowCount;
  318. if AWidth < 8 then AWidth := 8;
  319. if AHeight < 8 then AHeight := 8;
  320. end;
  321. procedure TTBXToolViewer.CMHintShow(var Message: TCMHintShow);
  322. var
  323. Col, Row: Integer;
  324. begin
  325. with Message.HintInfo^ do
  326. begin
  327. if GetCellAt(CursorPos.X - BoundsRect.Left, CursorPos.Y - BoundsRect.Top, Col, Row) then
  328. begin
  329. CursorRect := GetCellRect(CursorRect, Col, Row);
  330. HintStr := GetHint(Col, Row);
  331. end
  332. else HintStr := '';
  333. end;
  334. end;
  335. constructor TTBXToolViewer.Create(AView: TTBView; AItem: TTBCustomItem; AGroupLevel: Integer);
  336. begin
  337. inherited;
  338. FColCount := TTBXCustomToolPalette(AItem).ColCount;
  339. FRowCount := TTBXCustomToolPalette(AItem).RowCount;
  340. end;
  341. procedure TTBXToolViewer.DrawCell(Canvas: TCanvas; const CellRect: TRect;
  342. Col, Row: Integer; var ItemInfo: TTBXItemInfo);
  343. var
  344. ImageWidth, ImageHeight: Integer;
  345. R: TRect;
  346. begin
  347. CurrentTheme.PaintButton(Canvas, CellRect, ItemInfo);
  348. CalcImageSize(Canvas, ImageWidth, ImageHeight);
  349. R := Bounds((CellRect.Right + CellRect.Left - ImageWidth) div 2,
  350. (CellRect.Top + CellRect.Bottom - ImageHeight) div 2, ImageWidth, ImageHeight);
  351. DrawCellImage(Canvas, R, Col, Row, ItemInfo);
  352. end;
  353. procedure TTBXToolViewer.DrawCellImage(Canvas: TCanvas; const ARect: TRect;
  354. Col, Row: Integer; ItemInfo: TTBXItemInfo);
  355. var
  356. ImgIndex: Integer;
  357. ImgList: TCustomImageList;
  358. begin
  359. if not (tpoCustomImages in TTBXCustomToolPalette(Item).PaletteOptions) then
  360. begin
  361. ImgIndex := GetImageIndex(Col, Row);
  362. ImgList := GetImageList;
  363. if (ImgList <> nil) and (ImgIndex >= 0) and (ImgIndex < ImgList.Count) then
  364. CurrentTheme.PaintImage(Canvas, ARect, ItemInfo, ImgList, ImgIndex);
  365. end;
  366. TTBXCustomToolPalette(Item).DoDrawCellImage(Canvas, ARect, Col, Row, ItemInfo);
  367. end;
  368. procedure TTBXToolViewer.Entering(OldSelected: TTBItemViewer);
  369. begin
  370. FHotCell := Point(-1, 0);
  371. if (View is TTBXPopupView) and (OldSelected <> nil) then
  372. begin
  373. if OldSelected.Index > Index then
  374. begin
  375. FHotCell := Point(ColCount - 1, RowCount - 1);
  376. while (FHotCell.X > 0) and not IsCellVisible(FHotCell) do Dec(FHotCell.X);
  377. end
  378. else if OldSelected.Index < Index then
  379. FHotCell := Point(0, 0);
  380. end;
  381. inherited Entering(OldSelected);
  382. end;
  383. function TTBXToolViewer.GetCellAt(X, Y: Integer; out Col, Row: Integer): Boolean;
  384. begin
  385. { Returns true if there is a cell at (X,Y) point }
  386. if (CellWidth = 0) or (CellHeight = 0) then
  387. begin
  388. Col := 0;
  389. Row := 0;
  390. end
  391. else if not TTBXCustomToolPalette(Item).Stretch then
  392. begin
  393. Col := (X - Indent) div CellWidth;
  394. Row := Y div CellHeight;
  395. end
  396. else
  397. begin
  398. Col := (X - Indent) * ColCount div (BoundsRect.Right - BoundsRect.Left);
  399. Row := Y * RowCount div (BoundsRect.Bottom - BoundsRect.Top);
  400. end;
  401. Result := IsCellVisible(Point(Col, Row));
  402. end;
  403. function TTBXToolViewer.GetCellRect(ClientAreaRect: TRect; Col, Row: Integer): TRect;
  404. var
  405. W, H: Integer;
  406. begin
  407. with ClientAreaRect do
  408. if not TTBXCustomToolPalette(Item).Stretch then
  409. begin
  410. Result := Bounds(Left + Indent + Col * CellWidth, Top + Row * CellHeight, CellWidth, CellHeight)
  411. end
  412. else
  413. begin
  414. W := Right - Left;
  415. H := Bottom - Top;
  416. Result.Left := Left + Indent + W * Col div ColCount;
  417. Result.Top := Top + H * Row div RowCount;
  418. Result.Right := Left + W * (Col + 1) div ColCount;
  419. Result.Bottom := Top + H * (Row + 1) div RowCount;
  420. end;
  421. end;
  422. function TTBXToolViewer.GetHint(Col, Row: Integer): string;
  423. begin
  424. Result := '';
  425. TTBXCustomToolPalette(Item).DoGetHint(Point(Col, Row), Result);
  426. end;
  427. function TTBXToolViewer.GetImageIndex(Col, Row: Integer): Integer;
  428. begin
  429. Result := Col + Row * ColCount;
  430. end;
  431. procedure TTBXToolViewer.InvalidateCell(ACol, ARow: Integer);
  432. var
  433. R: TRect;
  434. begin
  435. R := GetCellRect(BoundsRect, ACol, ARow);
  436. InvalidateRect(View.Window.Handle, @R, False);
  437. end;
  438. function TTBXToolViewer.IsCellVisible(Cell: TPoint): Boolean;
  439. var
  440. ImgList: TCustomImageList;
  441. begin
  442. Result := (Cell.X >= 0) and (Cell.Y >= 0) and (Cell.X < ColCount) and (Cell.Y < RowCount);
  443. if Result then
  444. begin
  445. if not (tpoCustomImages in TTBXCustomToolPalette(Item).PaletteOptions) then
  446. begin
  447. ImgList := GetImageList;
  448. if ImgList <> nil then Result := (Cell.X + Cell.Y * ColCount) < ImgList.Count;
  449. end;
  450. TTBXCustomToolPalette(Item).DoGetCellVisible(Cell.X, Cell.Y, Result);
  451. end;
  452. end;
  453. procedure TTBXToolViewer.KeyDown(var Key: Word; Shift: TShiftState);
  454. var
  455. OldPos, Pos: TPoint;
  456. begin
  457. if IsCellVisible(HotCell) then OldPos := HotCell
  458. else if IsCellVisible(TTBXCustomToolPalette(Item).SelectedCell) then
  459. OldPos := TTBXCustomToolPalette(Item).SelectedCell
  460. else OldPos.X := -1;
  461. if OldPos.X >= 0 then
  462. begin
  463. Pos := OldPos;
  464. case Key of
  465. VK_LEFT:
  466. begin
  467. Dec(Pos.X);
  468. if Pos.X < 0 then
  469. begin
  470. Pos.X := ColCount - 1;
  471. Dec(Pos.Y);
  472. end;
  473. end;
  474. VK_UP: Dec(Pos.Y);
  475. VK_RIGHT:
  476. begin
  477. Inc(Pos.X);
  478. if Pos.X >= ColCount then
  479. begin
  480. Pos.X := 0;
  481. Inc(Pos.Y);
  482. end;
  483. end;
  484. VK_DOWN: Inc(Pos.Y);
  485. VK_PRIOR: Pos.Y := 0;
  486. VK_NEXT: Pos.Y := RowCount - 1;
  487. VK_HOME: Pos.X := 0;
  488. VK_END: Pos.Y := ColCount - 1;
  489. VK_RETURN:
  490. if IsCellVisible(HotCell) then
  491. begin
  492. TTBXCustomToolPalette(Item).HandleClickCell(HotCell.X, HotCell.Y);
  493. Exit;
  494. end;
  495. else
  496. inherited;
  497. Exit;
  498. end;
  499. end
  500. else
  501. begin
  502. OldPos := Point(-1, 0);
  503. Pos := Point(0, 0);
  504. end;
  505. if ((OldPos.X <> Pos.X) or (OldPos.Y <> Pos.Y)) and IsCellVisible(Pos) then
  506. begin
  507. Key := 0;
  508. FHotCell := Pos;
  509. TTBXCustomToolPalette(Item).Change(False);
  510. end;
  511. end;
  512. procedure TTBXToolViewer.MouseDown(Shift: TShiftState; X, Y: Integer; var MouseDownOnMenu: Boolean);
  513. begin
  514. MouseIsDown := True;
  515. MouseMove(X, Y);
  516. inherited;
  517. View.SetCapture;
  518. end;
  519. procedure TTBXToolViewer.MouseMove(X, Y: Integer);
  520. var
  521. OldHotCell: TPoint;
  522. begin
  523. OldHotCell := HotCell;
  524. if not GetCellAt(X, Y, FHotCell.X, FHotCell.Y) then FHotCell := Point(-1, 0);
  525. if (HotCell.X <> OldHotCell.X) or (HotCell.Y <> OldHotCell.Y) then
  526. begin
  527. with TTBXCustomToolPalette(Item) do
  528. begin
  529. if Show and not IsRectEmpty(BoundsRect) and
  530. not (Item is TTBControlItem) then
  531. begin
  532. Include(State, tbisInvalidated);
  533. InvalidateCell(OldHotCell.X, OldHotCell.Y);
  534. InvalidateCell(HotCell.X, HotCell.Y);
  535. end;
  536. end;
  537. end;
  538. end;
  539. procedure TTBXToolViewer.MouseUp(X, Y: Integer; MouseWasDownOnMenu: Boolean);
  540. var
  541. Col, Row: Integer;
  542. DAD: TTBDoneActionData;
  543. begin
  544. MouseIsDown := False;
  545. if GetCellAt(X, Y, Col, Row) then
  546. TTBXCustomToolPalette(Item).HandleClickCell(Col, Row);
  547. DAD := TTBViewAccess(TTBViewAccess(View).GetRootView).DoneActionData;
  548. DAD.ClickItem := Item;
  549. DAD.DoneAction := tbdaClickItem;
  550. DAD.Sound := True;
  551. TTBViewAccess(TTBViewAccess(View).GetRootView).DoneActionData := DAD;
  552. inherited;
  553. end;
  554. procedure TTBXToolViewer.Paint(const Canvas: TCanvas; const ClientAreaRect: TRect;
  555. IsHoverItem, IsPushed, UseDisabledShadow: Boolean);
  556. const
  557. CDesigning: array [Boolean] of Integer = (0, IO_DESIGNING);
  558. var
  559. I, J: Integer;
  560. ItemInfo: TTBXItemInfo;
  561. Hover: TTBXHoverKind;
  562. R, CellRect: TRect;
  563. begin
  564. FillChar(ItemInfo, SizeOf(TTBXItemInfo), 0);
  565. ItemInfo.ViewType := GetViewType(View);
  566. ItemInfo.ItemOptions := CDesigning[csDesigning in Item.ComponentState];
  567. ItemInfo.Enabled := Item.Enabled or View.Customizing;
  568. ItemInfo.Pushed := False;
  569. ItemInfo.Selected := False;
  570. ItemInfo.ImageShown := True;
  571. with ItemInfo do CalcImageSize(Canvas, ImageWidth, ImageHeight);
  572. ItemInfo.HoverKind := hkNone;
  573. if not IsToolbarStyle then ItemInfo.PopupMargin := GetPopupMargin(Self);
  574. if not IsToolbarStyle then with CurrentTheme do
  575. begin
  576. R := ClientAreaRect;
  577. CurrentTheme.PaintMenuItemFrame(Canvas, R, ItemInfo);
  578. end;
  579. CalcCellSize(Canvas, FCellWidth, FCellHeight);
  580. if IsHoverItem then
  581. begin
  582. if not ItemInfo.Enabled and not View.MouseOverSelected then Hover := hkKeyboardHover
  583. else if ItemInfo.Enabled then Hover := hkMouseHover
  584. else Hover := hkNone;
  585. end
  586. else
  587. Hover := hkNone;
  588. for J := 0 to RowCount - 1 do
  589. for I := 0 to ColCount - 1 do
  590. begin
  591. if IsCellVisible(Point(I, J)) then
  592. begin
  593. if (Hover <> hkNone) and (HotCell.X = I) and (HotCell.Y = J) then
  594. begin
  595. ItemInfo.HoverKind := Hover;
  596. if IsPushed then ItemInfo.Pushed := True
  597. end
  598. else
  599. begin
  600. ItemInfo.HoverKind := hkNone;
  601. ItemInfo.Pushed := False;
  602. end;
  603. with TTBXCustomToolPalette(Item) do
  604. if (SelectedCell.X = I) and (SelectedCell.Y = J) then
  605. ItemInfo.Selected := True
  606. else
  607. ItemInfo.Selected := False;
  608. CellRect := GetCellRect(ClientAreaRect, I, J);
  609. DrawCell(Canvas, CellRect, I, J, ItemInfo);
  610. end;
  611. end;
  612. end;
  613. //----------------------------------------------------------------------------//
  614. { TTBXCustomColorSet }
  615. constructor TTBXCustomColorSet.Create(AOwner: TComponent);
  616. begin
  617. inherited;
  618. FPalettes := TList.Create;
  619. end;
  620. destructor TTBXCustomColorSet.Destroy;
  621. begin
  622. FPalettes.Free;
  623. inherited;
  624. end;
  625. function TTBXCustomColorSet.GetColor(Col, Row: Integer): TColor;
  626. var
  627. Dummy: string;
  628. begin
  629. GetColorInfo(Col, Row, Result, Dummy);
  630. end;
  631. procedure TTBXCustomColorSet.GetColorInfo(Col, Row: Integer; out Color: TColor; out ColorName: string);
  632. begin
  633. Color := clNone;
  634. SetLength(ColorName, 0);
  635. if Assigned(FOnGetColorInfo) then FOnGetColorInfo(Self, Col, Row, Color, ColorName);
  636. end;
  637. function TTBXCustomColorSet.ColorToString(Color: TColor): string;
  638. var
  639. I, J: Integer;
  640. C: TColor;
  641. N: string;
  642. function GetRGB(C: TColor): TColor;
  643. begin
  644. Result := (C and $FF00) + C shr 16 + (C and $FF shl 16);
  645. end;
  646. begin
  647. if Color = clNone then Result := 'None'
  648. else
  649. begin
  650. if Color < 0 then Color := GetSysColor(Color and $000000FF);
  651. Color := Color and $00FFFFFF;
  652. for J := 0 to RowCount - 1 do
  653. for I := 0 to ColCount - 1 do
  654. begin
  655. GetColorInfo(I, J, C, N);
  656. if C <> clNone then
  657. begin
  658. if C < 0 then C := GetSysColor(C and $000000FF);
  659. C := C and $00FFFFFF;
  660. if C = Color then
  661. begin
  662. Result := N;
  663. if Length(N) = 0 then Result := '#' + IntToHex(GetRGB(Color), 6);
  664. Exit;
  665. end
  666. end;
  667. end;
  668. Result := '#' + IntToHex(GetRGB(Color), 6);
  669. end;
  670. end;
  671. function TTBXCustomColorSet.GetName(Col, Row: Integer): string;
  672. var
  673. Dummy: TColor;
  674. begin
  675. GetColorInfo(Col, Row, Dummy, Result);
  676. end;
  677. procedure TTBXCustomColorSet.SetColCount(Value: Integer);
  678. begin
  679. UpdateSize(Value, RowCount);
  680. end;
  681. procedure TTBXCustomColorSet.SetRowCount(Value: Integer);
  682. begin
  683. UpdateSize(ColCount, Value);
  684. end;
  685. procedure TTBXCustomColorSet.UpdateSize(NewColCount, NewRowCount: Integer);
  686. var
  687. I: Integer;
  688. begin
  689. FColCount := NewColCount;
  690. FRowCount := NewRowCount;
  691. for I := 0 to FPalettes.Count - 1 do
  692. with TTBXColorPalette(FPalettes[I]) do
  693. begin
  694. ColCount := Self.ColCount;
  695. RowCount := Self.RowCount;
  696. end;
  697. end;
  698. //----------------------------------------------------------------------------//
  699. { TTBXColorPalette }
  700. function TTBXColorPalette.ColorToString(AColor: TColor): string;
  701. begin
  702. Result := GetColorSet.ColorToString(AColor);
  703. end;
  704. constructor TTBXColorPalette.Create(AOwner: TComponent);
  705. begin
  706. inherited;
  707. ColCount := DefaultColorSet.ColCount;
  708. RowCount := DefaultColorSet.RowCount;
  709. Options := Options + [tboShowHint];
  710. FColor := clNone;
  711. PaletteOptions := PaletteOptions + [tpoCustomImages];
  712. FImageSize := -1;
  713. end;
  714. procedure TTBXColorPalette.DoCalcImageSize(Canvas: TCanvas; var AWidth, AHeight: Integer);
  715. begin
  716. if FImageSize < 0 then
  717. begin
  718. FImageSize := TBXScaleByTextHeightRunTime(Canvas, 12);
  719. end;
  720. AWidth := FImageSize;
  721. AHeight := FImageSize;
  722. end;
  723. procedure TTBXColorPalette.DoChange;
  724. begin
  725. if SelectedCell.X >= 0 then
  726. FColor := GetCellColor(SelectedCell.X, SelectedCell.Y);
  727. inherited;
  728. end;
  729. procedure TTBXColorPalette.DoDrawCellImage(Canvas: TCanvas;
  730. const ARect: TRect; ACol, ARow: Integer; ItemInfo: TTBXItemInfo);
  731. var
  732. R: TRect;
  733. begin
  734. R := ARect;
  735. Canvas.Brush.Color := clBtnShadow;
  736. Canvas.FrameRect(R);
  737. InflateRect(R, -1, -1);
  738. if ItemInfo.Enabled then
  739. begin
  740. Canvas.Brush.Color := GetCellColor(ACol, ARow);
  741. Canvas.FillRect(R);
  742. end;
  743. end;
  744. procedure TTBXColorPalette.DoGetCellVisible(ACol, ARow: Integer; var Visible: Boolean);
  745. begin
  746. Visible := GetCellColor(ACol, ARow) <> clNone;
  747. end;
  748. procedure TTBXColorPalette.DoGetHint(ACell: TPoint; var HintText: string);
  749. begin
  750. HintText := GetColorSet.GetName(ACell.X, ACell.Y);
  751. end;
  752. function TTBXColorPalette.FindCell(AColor: TColor): TPoint;
  753. var
  754. I, J: Integer;
  755. C: TColor;
  756. begin
  757. if AColor <> clNone then AColor := ColorToRGB(AColor);
  758. for J := 0 to RowCount - 1 do
  759. for I := 0 to ColCount - 1 do
  760. begin
  761. C := GetCellColor(I, J);
  762. if C <> clNone then C := ColorToRGB(C);
  763. if C = AColor then
  764. begin
  765. Result.X := I;
  766. Result.Y := J;
  767. Exit;
  768. end;
  769. end;
  770. Result.X := -1;
  771. Result.Y := 0;
  772. end;
  773. function TTBXColorPalette.GetCellColor(ACol, ARow: Integer): TColor;
  774. begin
  775. Result := GetColorSet.GetColor(ACol, ARow);
  776. end;
  777. function TTBXColorPalette.GetColorSet: TTBXCustomColorSet;
  778. begin
  779. if FColorSet = nil then Result := DefaultColorSet
  780. else Result := FColorSet;
  781. end;
  782. procedure TTBXColorPalette.Notification(AComponent: TComponent; Operation: TOperation);
  783. begin
  784. inherited;
  785. if (AComponent = FColorSet) and (Operation = opRemove) then ColorSet := nil;
  786. end;
  787. procedure TTBXColorPalette.SetColor(Value: TColor);
  788. begin
  789. FColor := Value;
  790. SelectedCell := FindCell(Value);
  791. end;
  792. procedure TTBXColorPalette.SetColorSet(Value: TTBXCustomColorSet);
  793. begin
  794. if FColorSet <> Value then
  795. begin
  796. if Assigned(FColorSet) then FColorSet.FPalettes.Remove(Self);
  797. FColorSet := Value;
  798. if Assigned(Value) then
  799. begin
  800. Value.FreeNotification(Self);
  801. Value.FPalettes.Add(Self);
  802. ColCount := Value.ColCount;
  803. RowCount := Value.RowCount;
  804. end
  805. else
  806. begin
  807. ColCount := DefaultColorSet.ColCount;
  808. RowCount := DefaultColorSet.RowCount;
  809. end;
  810. Change(True);
  811. end;
  812. end;
  813. { TTBXDefaultColorSet }
  814. type
  815. TTBXDefaultColorSet = class (TTBXCustomColorSet)
  816. protected
  817. procedure GetColorInfo(Col, Row: Integer; out Color: TColor; out ColorName: string); override;
  818. public
  819. constructor Create(AOwner: TComponent); override;
  820. end;
  821. procedure TTBXDefaultColorSet.GetColorInfo(Col, Row: Integer; out Color: TColor; out ColorName: string);
  822. procedure Clr(const AName: string; AColor: TColor);
  823. begin
  824. Color := AColor;
  825. ColorName := AName;
  826. end;
  827. begin
  828. Color := clNone;
  829. Name := '';
  830. case Row of
  831. 0:
  832. case Col of
  833. 0: Clr('Black', $000000);
  834. 1: Clr('Brown', $003399);
  835. 2: Clr('Olive Green', $003333);
  836. 3: Clr('Dark Green', $003300);
  837. 4: Clr('Dark Teal', $663300);
  838. 5: Clr('Dark blue', $800000);
  839. 6: Clr('Indigo', $993333);
  840. 7: Clr('Gray-80%', $333333);
  841. end;
  842. 1:
  843. case Col of
  844. 0: Clr('Dark Red', $000080);
  845. 1: Clr('Orange', $0066FF);
  846. 2: Clr('Dark Yellow', $008080);
  847. 3: Clr('Green', $008000);
  848. 4: Clr('Teal', $808000);
  849. 5: Clr('Blue', $FF0000);
  850. 6: Clr('Blue-Gray', $996666);
  851. 7: Clr('Gray-50%', $808080);
  852. end;
  853. 2:
  854. case Col of
  855. 0: Clr('Red', $0000FF);
  856. 1: Clr('Light Orange', $0099FF);
  857. 2: Clr('Lime', $00CC99);
  858. 3: Clr('Sea Green', $669933);
  859. 4: Clr('Aqua', $CCCC33);
  860. 5: Clr('Light Blue', $FF6633);
  861. 6: Clr('Violet', $800080);
  862. 7: Clr('Gray-40%', $969696);
  863. end;
  864. 3:
  865. case Col of
  866. 0: Clr('Pink', $FF00FF);
  867. 1: Clr('Gold', $00CCFF);
  868. 2: Clr('Yellow', $00FFFF);
  869. 3: Clr('Bright Green', $00FF00);
  870. 4: Clr('Turquoise', $FFFF00);
  871. 5: Clr('Sky Blue', $FFCC00);
  872. 6: Clr('Plum', $663399);
  873. 7: Clr('Gray-25%', $C0C0C0);
  874. end;
  875. 4:
  876. case Col of
  877. 0: Clr('Rose', $CC99FF);
  878. 1: Clr('Tan', $99CCFF);
  879. 2: Clr('Light Yellow', $99FFFF);
  880. 3: Clr('Light Green', $CCFFCC);
  881. 4: Clr('Light Turquoise', $FFFFCC);
  882. 5: Clr('Pale Blue', $FFCC99);
  883. 6: Clr('Lavender', $FF99CC);
  884. 7: Clr('White', $FFFFFF);
  885. end;
  886. end;
  887. end;
  888. constructor TTBXDefaultColorSet.Create(AOwner: TComponent);
  889. begin
  890. inherited;
  891. FColCount := 8;
  892. FRowCount := 5;
  893. end;
  894. initialization
  895. DefaultColorSet := TTBXDefaultColorSet.Create(nil);
  896. finalization
  897. DefaultColorSet.Free;
  898. end.