djjd47130

JD Left Menu

Aug 19th, 2016
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 13.59 KB | None | 0 0
  1. unit JD.Ctrls.LeftToolbar;
  2.  
  3. (*
  4.   JD Left Menu (to change name from "Toolbar" to "Menu")
  5.   Designed to display a dynamically drawn menu along left with many customizable options
  6.  
  7.   NOTE: Extremely raw code, in beginning stages - Can compile, but in fresh development so not actually working yet.
  8.  
  9. *)
  10.  
  11. interface
  12.  
  13. uses
  14.   System.Classes, System.SysUtils, System.Types,
  15.   Winapi.Windows, Winapi.Messages,
  16.   Vcl.Controls, Vcl.Graphics;
  17.  
  18. type
  19.   TLeftButtonStateProps = class;
  20.   TLeftToolbar = class;
  21.   TLeftToolbarItem = class;
  22.   TLeftToolbarItems = class;
  23.  
  24.   TLeftToolbarBorder = (lbNone, lbRect, lbElipse, lbRoundRect,
  25.     lbBtnDown, lbBtnUp, lbBtnHover); //NOTE: "Btn" refers to Windows/VCL styled drawing
  26.  
  27.   TLeftToolbarState = (lsNormal, lsHover, lsDown, lsClick);
  28.  
  29.   TLeftToolbarItemEvent = procedure(Sender: TObject; const Item: Integer) of object;
  30.  
  31.   TLeftButtonStateProps = class(TPersistent)
  32.   private
  33.     FBackColor: TColor;
  34.     FForeColor: TColor;
  35.     FBorderColor: TColor;
  36.     FBorder: TLeftToolbarBorder;
  37.     FOverlay: TPicture;
  38.     procedure SetBackColor(const Value: TColor);
  39.     procedure SetBorderColor(const Value: TColor);
  40.     procedure SetForeColor(const Value: TColor);
  41.     procedure SetOverlay(const Value: TPicture);
  42.     procedure SetBorder(const Value: TLeftToolbarBorder);
  43.   public
  44.     constructor Create;
  45.     destructor Destroy; override;
  46.     procedure Invalidate;
  47.   published
  48.     property Border: TLeftToolbarBorder read FBorder write SetBorder;
  49.     property BackColor: TColor read FBackColor write SetBackColor;
  50.     property ForeColor: TColor read FForeColor write SetForeColor;
  51.     property BorderColor: TColor read FBorderColor write SetBorderColor;
  52.     property Overlay: TPicture read FOverlay write SetOverlay;
  53.   end;
  54.  
  55.   TLeftToolbarItem = class(TCollectionItem)
  56.   private
  57.     FToolbar: TLeftToolbar;
  58.     FPicture: TPicture;
  59.     FCaption: TCaption;
  60.     FGroupIndex: Integer;
  61.     procedure SetCaption(const Value: TCaption);
  62.     procedure SetPicture(const Value: TPicture);
  63.     procedure SetGroupIndex(const Value: Integer);
  64.   protected
  65.     function GetDisplayName: String; override;
  66.   public
  67.     constructor Create(Collection: TCollection); override;
  68.     destructor Destroy; override;
  69.     function BoundsRect: TRect;
  70.     function State: TLeftToolbarState;
  71.   published
  72.     property Caption: TCaption read FCaption write SetCaption;
  73.     property GroupIndex: Integer read FGroupIndex write SetGroupIndex;
  74.     property Picture: TPicture read FPicture write SetPicture;
  75.   end;
  76.  
  77.   TLeftToolbarItems = class(TOwnedCollection)
  78.   private
  79.     FToolbar: TLeftToolbar;
  80.     function GetItem(Index: Integer): TLeftToolbarItem;
  81.     procedure SetItem(Index: Integer; const Value: TLeftToolbarItem);
  82.   public
  83.     constructor Create(AOwner: TPersistent; AToolbar: TLeftToolbar); reintroduce;
  84.     property Items[Index: Integer]: TLeftToolbarItem read GetItem write SetItem; default;
  85.   end;
  86.  
  87.   TLeftToolbarGroup = class(TCollectionItem)
  88.   private
  89.     FToolbar: TLeftToolbar;
  90.     FCaption: TCaption;
  91.     procedure SetCaption(const Value: TCaption);
  92.   protected
  93.     function GetDisplayName: String; override;
  94.   public
  95.     constructor Create(Collection: TCollection); override;
  96.     destructor Destroy; override;
  97.     function BoundsRect: TRect;
  98.   published
  99.     property Caption: TCaption read FCaption write SetCaption;
  100.  
  101.   end;
  102.  
  103.   TLeftToolbarGroups = class(TOwnedCollection)
  104.   private
  105.     FToolbar: TLeftToolbar;
  106.     function GetItem(Index: Integer): TLeftToolbarGroup;
  107.     procedure SetItem(Index: Integer; const Value: TLeftToolbarGroup);
  108.   public
  109.     constructor Create(AOwner: TPersistent; AToolbar: TLeftToolbar); reintroduce;
  110.     property Items[Index: Integer]: TLeftToolbarGroup read GetItem write SetItem; default;
  111.   end;
  112.  
  113.   TLeftToolbar = class(TCustomControl)
  114.   private
  115.     FItems: TLeftToolbarItems;
  116.     FGroups: TLeftToolbarGroups;
  117.     FItemIndex: Integer;
  118.     FItemHeight: Integer;
  119.     FHover: Boolean;
  120.     FStyleDown: TLeftButtonStateProps;
  121.     FStyleNormal: TLeftButtonStateProps;
  122.     FStyleClick: TLeftButtonStateProps;
  123.     FStyleHover: TLeftButtonStateProps;
  124.     procedure SetItems(const Value: TLeftToolbarItems);
  125.     procedure SetItemIndex(const Value: Integer);
  126.     procedure SetItemHeight(const Value: Integer);
  127.     procedure SetStyleClick(const Value: TLeftButtonStateProps);
  128.     procedure SetStyleDown(const Value: TLeftButtonStateProps);
  129.     procedure SetStyleHover(const Value: TLeftButtonStateProps);
  130.     procedure SetStyleNormal(const Value: TLeftButtonStateProps);
  131.     procedure SetGroups(const Value: TLeftToolbarGroups);
  132.   protected
  133.     procedure Paint; override;
  134.     procedure CMHitTest(var Msg: TWMNCHitTest); message WM_NCHITTEST;
  135.   public
  136.     constructor Create(AOwner: TComponent); override;
  137.     destructor Destroy; override;
  138.   published
  139.     property Align;
  140.     property Color;
  141.     property Font;
  142.     property Groups: TLeftToolbarGroups read FGroups write SetGroups;
  143.     property Height;
  144.     property Items: TLeftToolbarItems read FItems write SetItems;
  145.     property ItemIndex: Integer read FItemIndex write SetItemIndex;
  146.     property ItemHeight: Integer read FItemHeight write SetItemHeight;
  147.     property ParentColor;
  148.     property ParentFont;
  149.     property StyleNormal: TLeftButtonStateProps read FStyleNormal write SetStyleNormal;
  150.     property StyleHover: TLeftButtonStateProps read FStyleHover write SetStyleHover;
  151.     property StyleClick: TLeftButtonStateProps read FStyleClick write SetStyleClick;
  152.     property StyleDown: TLeftButtonStateProps read FStyleDown write SetStyleDown;
  153.     property Visible;
  154.     property Width;
  155.   end;
  156.  
  157. implementation
  158.  
  159. { TLeftButtonStateProps }
  160.  
  161. constructor TLeftButtonStateProps.Create;
  162. begin
  163.   FOverlay:= TPicture.Create;
  164. end;
  165.  
  166. destructor TLeftButtonStateProps.Destroy;
  167. begin
  168.   FOverlay.Free;
  169.   inherited;
  170. end;
  171.  
  172. procedure TLeftButtonStateProps.Invalidate;
  173. begin
  174.   //TODO
  175. end;
  176.  
  177. procedure TLeftButtonStateProps.SetBackColor(const Value: TColor);
  178. begin
  179.   FBackColor := Value;
  180.   Invalidate;
  181. end;
  182.  
  183. procedure TLeftButtonStateProps.SetBorder(const Value: TLeftToolbarBorder);
  184. begin
  185.   FBorder := Value;
  186.   Invalidate;
  187. end;
  188.  
  189. procedure TLeftButtonStateProps.SetBorderColor(const Value: TColor);
  190. begin
  191.   FBorderColor := Value;
  192.   Invalidate;
  193. end;
  194.  
  195. procedure TLeftButtonStateProps.SetForeColor(const Value: TColor);
  196. begin
  197.   FForeColor := Value;
  198.   Invalidate;
  199. end;
  200.  
  201. procedure TLeftButtonStateProps.SetOverlay(const Value: TPicture);
  202. begin
  203.   FOverlay := Value;
  204.   Invalidate;
  205. end;
  206.  
  207. { TLeftToolbarItem }
  208.  
  209. constructor TLeftToolbarItem.Create(Collection: TCollection);
  210. begin
  211.   inherited;
  212.   FPicture:= TPicture.Create;
  213.   FToolbar:= TLeftToolbarItems(Collection).FToolbar;
  214. end;
  215.  
  216. destructor TLeftToolbarItem.Destroy;
  217. begin
  218.  
  219.   FPicture.Free;
  220.   inherited;
  221. end;
  222.  
  223. function TLeftToolbarItem.GetDisplayName: String;
  224. begin
  225.   if Trim(FCaption) <> '' then
  226.     Result:= FCaption
  227.   else
  228.     inherited;
  229. end;
  230.  
  231. procedure TLeftToolbarItem.SetCaption(const Value: TCaption);
  232. begin
  233.   FCaption := Value;
  234.   FToolbar.Invalidate;
  235. end;
  236.  
  237. procedure TLeftToolbarItem.SetGroupIndex(const Value: Integer);
  238. begin
  239.   FGroupIndex := Value;
  240.   FToolbar.Invalidate;
  241. end;
  242.  
  243. procedure TLeftToolbarItem.SetPicture(const Value: TPicture);
  244. begin
  245.   FPicture.Assign(Value);
  246.   FToolbar.Invalidate;
  247. end;
  248.  
  249. function TLeftToolbarItem.State: TLeftToolbarState;
  250. begin
  251.   Result:= TLeftToolbarState.lsNormal;
  252.   if FToolbar.FItemIndex = Self.Index then
  253.     Result:= TLeftToolbarState.lsDown;
  254.   //TODO: Detect if mouse is currently over or clicked on
  255.  
  256. end;
  257.  
  258. function TLeftToolbarItem.BoundsRect: TRect;
  259. var
  260.   T: Integer;
  261. begin
  262.   //TODO: Return rectangle of full possible drawing area of item
  263.   T:= Self.Index * FToolbar.ItemHeight;
  264.   Result:= Rect(0, T, FToolbar.ClientWidth, T + FToolbar.ItemHeight);
  265. end;
  266.  
  267. { TLeftToolbarItems }
  268.  
  269. constructor TLeftToolbarItems.Create(AOwner: TPersistent; AToolbar: TLeftToolbar);
  270. begin
  271.   inherited Create(AOwner, TLeftToolbarItem);
  272.   FToolbar:= AToolbar;
  273. end;
  274.  
  275. function TLeftToolbarItems.GetItem(Index: Integer): TLeftToolbarItem;
  276. begin
  277.   Result:= TLeftToolbarItem(inherited Items[Index]);
  278. end;
  279.  
  280. procedure TLeftToolbarItems.SetItem(Index: Integer;
  281.   const Value: TLeftToolbarItem);
  282. begin
  283.   inherited Items[Index]:= Value;
  284. end;
  285.  
  286. { TLeftToolbarGroup }
  287.  
  288. constructor TLeftToolbarGroup.Create(Collection: TCollection);
  289. begin
  290.   inherited;
  291.   FToolbar:= TLeftToolbarGroups(Collection).FToolbar;
  292. end;
  293.  
  294. destructor TLeftToolbarGroup.Destroy;
  295. begin
  296.  
  297.   inherited;
  298. end;
  299.  
  300. function TLeftToolbarGroup.GetDisplayName: String;
  301. begin
  302.   if Trim(FCaption) <> '' then
  303.     Result:= FCaption
  304.   else
  305.     inherited;
  306. end;
  307.  
  308. procedure TLeftToolbarGroup.SetCaption(const Value: TCaption);
  309. begin
  310.   FCaption := Value;
  311.   FToolbar.Invalidate;
  312. end;
  313.  
  314. function TLeftToolbarGroup.BoundsRect: TRect;
  315. begin
  316.   //TODO: Return rect representing group header display
  317.  
  318. end;
  319.  
  320. { TLeftToolbarGroups }
  321.  
  322. constructor TLeftToolbarGroups.Create(AOwner: TPersistent;
  323.   AToolbar: TLeftToolbar);
  324. begin
  325.   inherited Create(AOwner, TLeftToolbarGroup);
  326.   FToolbar:= AToolbar;
  327. end;
  328.  
  329. function TLeftToolbarGroups.GetItem(Index: Integer): TLeftToolbarGroup;
  330. begin
  331.   Result:= TLeftToolbarGroup(inherited Items[Index]);
  332. end;
  333.  
  334. procedure TLeftToolbarGroups.SetItem(Index: Integer;
  335.   const Value: TLeftToolbarGroup);
  336. begin
  337.   inherited Items[Index]:= Value;
  338. end;
  339.  
  340. { TLeftToolbar }
  341.  
  342. constructor TLeftToolbar.Create(AOwner: TComponent);
  343. begin
  344.   inherited;
  345.   FItems:= TLeftToolbarItems.Create(Self, Self);
  346.   FGroups:= TLeftToolbarGroups.Create(Self, Self);
  347.  
  348.   ParentColor:= False;
  349.   Color:= clBtnFace;
  350.  
  351.   FHover:= False;
  352.  
  353.   FStyleDown:= TLeftButtonStateProps.Create;
  354.   FStyleDown.FBackColor:= Color;
  355.   FStyleDown.FForeColor:= Color;
  356.   FStyleDown.FBorderColor:= clRed;
  357.   FStyleDown.Border:= lbRect;
  358.  
  359.   FStyleNormal:= TLeftButtonStateProps.Create;
  360.   FStyleNormal.FBackColor:= Color;
  361.   FStyleNormal.FForeColor:= Color;
  362.   FStyleNormal.FBorderColor:= Color;
  363.   FStyleNormal.Border:= lbNone;
  364.  
  365.   FStyleClick:= TLeftButtonStateProps.Create;
  366.   FStyleClick.FBackColor:= Color;
  367.   FStyleClick.FForeColor:= Color;
  368.   FStyleClick.FBorderColor:= clBlue;
  369.   FStyleClick.Border:= lbRect;
  370.  
  371.   FStyleHover:= TLeftButtonStateProps.Create;
  372.   FStyleHover.FBackColor:= Color;
  373.   FStyleHover.FForeColor:= Color;
  374.   FStyleHover.FBorderColor:= clSkyBlue;
  375.   FStyleHover.Border:= lbRect;
  376.  
  377.   Align:= alLeft;
  378.   Width:= 200;
  379.   FItemHeight:= 42;
  380.   FItemIndex:= -1;
  381.  
  382. end;
  383.  
  384. destructor TLeftToolbar.Destroy;
  385. begin
  386.  
  387.   FGroups.Free;
  388.   FItems.Free;
  389.   inherited;
  390. end;
  391.  
  392. procedure TLeftToolbar.SetGroups(const Value: TLeftToolbarGroups);
  393. begin
  394.   FGroups := Value;
  395.   Invalidate;
  396. end;
  397.  
  398. procedure TLeftToolbar.SetItemHeight(const Value: Integer);
  399. begin
  400.   FItemHeight := Value;
  401.   Invalidate;
  402. end;
  403.  
  404. procedure TLeftToolbar.SetItemIndex(const Value: Integer);
  405. begin
  406.   FItemIndex := Value;
  407.   Invalidate;
  408. end;
  409.  
  410. procedure TLeftToolbar.SetItems(const Value: TLeftToolbarItems);
  411. begin
  412.   FItems.Assign(Value);
  413.   Invalidate;
  414. end;
  415.  
  416. procedure TLeftToolbar.SetStyleClick(const Value: TLeftButtonStateProps);
  417. begin
  418.   FStyleClick.Assign(Value);
  419.   Invalidate;
  420. end;
  421.  
  422. procedure TLeftToolbar.SetStyleDown(const Value: TLeftButtonStateProps);
  423. begin
  424.   FStyleDown.Assign(Value);
  425.   Invalidate;
  426. end;
  427.  
  428. procedure TLeftToolbar.SetStyleHover(const Value: TLeftButtonStateProps);
  429. begin
  430.   FStyleHover.Assign(Value);
  431.   Invalidate;
  432. end;
  433.  
  434. procedure TLeftToolbar.SetStyleNormal(const Value: TLeftButtonStateProps);
  435. begin
  436.   FStyleNormal.Assign(Value);
  437.   Invalidate;
  438. end;
  439.  
  440. procedure TLeftToolbar.CMHitTest(var Msg: TWMNCHitTest);
  441. var
  442.   X: Integer;
  443.   P: TPoint;
  444.   I: TLeftToolbarItem;
  445.   G: TLeftToolbarGroup;
  446.   H: Boolean;
  447. begin
  448.   //TODO: Detect if point is within a certain item
  449.   P:= Msg.Pos;
  450.   Msg.Result:= HTCLIENT;
  451.   H:= False;
  452.  
  453.   //HTOBJECT
  454.   for X := 0 to FItems.Count-1 do begin
  455.     I:= FItems[X];
  456.     if PtInRect(I.BoundsRect, P) then begin
  457.       Msg.Result:= HTOBJECT;
  458.       //TODO: Make hover effect
  459.       H:= True;
  460.  
  461.     end;
  462.   end;
  463.   for X := 0 to FGroups.Count-1 do begin
  464.     G:= FGroups[X];
  465.     if PtInRect(G.BoundsRect, P) then begin
  466.       Msg.Result:= HTOBJECT;
  467.       //TODO: Make hover effect
  468.       H:= True;
  469.  
  470.     end;
  471.   end;
  472.  
  473.   if FHover <> H then begin
  474.     FHover:= H;
  475.     Invalidate;
  476.   end;
  477.  
  478.   //TODO: This aproach won't work because each item needs to know
  479.   //  its current state by itself.
  480.  
  481. end;
  482.  
  483. procedure TLeftToolbar.Paint;
  484. var
  485.   C: TCanvas;
  486.   B: TBrush;
  487.   P: TPen;
  488.   I: TLeftToolbarItem;
  489.   X: Integer;
  490.   R, IR, CR: TRect; //TODO: Move IR and CR to item class
  491.  
  492.   procedure DrawBackground;
  493.   begin
  494.     B.Style:= bsSolid;
  495.     P.Style:= psClear;
  496.     B.Color:= Self.Color; //TODO: Change color to preferences
  497.  
  498.     C.FillRect(R);
  499.   end;
  500.  
  501.   procedure DrawImage;
  502.   begin
  503.     IR:= Rect(R.Left, R.Top, R.Height, R.Bottom);
  504.     //TODO: Draw image inside image rect
  505.  
  506.     //TODO: Check whether current state has overlay image assigned
  507.  
  508.   end;
  509.  
  510.   procedure DrawCaption;
  511.   begin
  512.     CR:= Rect(R.Height, R.Top, R.Right, R.Bottom);
  513.     C.TextOut(CR.Left, CR.Top, I.Caption);
  514.     //TODO: Change to DrawText and implement alignment options
  515.  
  516.   end;
  517.  
  518.   procedure DrawBorder;
  519.   begin
  520.     //TODO: Draw border around full item
  521.  
  522.   end;
  523.  
  524. begin
  525.   inherited;
  526.   C:= Canvas;
  527.   B:= C.Brush;
  528.   P:= C.Pen;
  529.   C.Font.Assign(Font);
  530.  
  531.   //Draw overall background
  532.   B.Style:= bsSolid;
  533.   P.Style:= psClear;
  534.   B.Color:= Color;
  535.   C.FillRect(C.ClipRect);
  536.  
  537.   for X := 0 to FItems.Count-1 do begin
  538.     I:= FItems[X];
  539.     R:= I.BoundsRect;
  540.     DrawBackground;
  541.     DrawImage;
  542.     DrawCaption;
  543.     DrawBorder;
  544.   end;
  545.  
  546. end;
  547.  
  548. end.
Add Comment
Please, Sign In to add comment