Recent

Author Topic: [SOLVED] Themed checkboxes for TreeView  (Read 3343 times)

apeoperaio

  • Full Member
  • ***
  • Posts: 158
[SOLVED] Themed checkboxes for TreeView
« on: July 16, 2018, 11:22:48 pm »
I have an application using TTreeView with checkboxes, in order to do that I fill an ImageList with CheckBox images using ThemeService. It works fine on Win and Linux but I have issues on OSX.
Using Cocoa I get only two images (attached).
Any hint to have my TTreeView working on OSX?

Code: Pascal  [Select]
  1. procedure TForm1.fillimagelist;
  2. var
  3.   aSize: TSize;
  4.   aBMP: TBitmap;
  5.   aDetails: TThemedElementDetails;
  6.   aRect: TRect;
  7. begin
  8.   aDetails:= ThemeServices.GetElementDetails(tbCheckBoxCheckedNormal);
  9.   aSize:= ThemeServices.GetDetailSize(aDetails);
  10.   ilTreeView.Width:= aSize.cx;
  11.   ilTreeView.Height:= aSize.cy;
  12.   aBMP:= TBitmap.Create;
  13.   with aBMP do
  14.     begin
  15.       SetSize(aSize.cx, aSize.cy);
  16.       Transparent:= True;
  17.       TransparentColor:= clForm;
  18.       Brush.Color:= TransparentColor;
  19.       Canvas.FillRect(0,0, Width,Height);
  20.     end;
  21.   aRect:=Rect(0, 0, aSize.cx, aSize.cy);
  22.   // 0 - tbCheckBoxUncheckedNormal - none selected
  23.   ThemeServices.DrawElement(aBMP.Canvas.Handle, aDetails, aRect, nil);
  24.   ilTreeView.Add(aBMP, nil);
  25.  
  26.   // 1 - tbCheckBoxCheckedNormal - all selected
  27.   aBMP.Canvas.FillRect(0,0, Width,Height);
  28.   aDetails:=ThemeServices.GetElementDetails(tbCheckBoxCheckedNormal);
  29.   ThemeServices.DrawElement(aBMP.Canvas.Handle, aDetails, aRect, nil);
  30.   ilTreeView.Add(aBMP, nil);
  31.  
  32.   // 2 - tbCheckBoxMixedNormal -  mixed selection
  33.   aBMP.Canvas.FillRect(0,0, Width,Height);
  34.   aDetails:=ThemeServices.GetElementDetails(tbCheckBoxMixedNormal);
  35.   ThemeServices.DrawElement(aBMP.Canvas.Handle, aDetails, aRect, nil);
  36.   ilTreeView.Add(aBMP, nil);
  37.  
  38.   // 3 - tbCheckBoxUncheckedDisabled - unchecked disabled
  39.   aBMP.Canvas.FillRect(0,0, Width,Height);
  40.   aDetails:=ThemeServices.GetElementDetails(tbCheckBoxUncheckedDisabled);
  41.   ThemeServices.DrawElement(aBMP.Canvas.Handle, aDetails, aRect, nil);
  42.   ilTreeView.Add(aBMP, nil);
  43.  
  44.   // 4 - tbCheckBoxCheckedDisabled - checked disabled (e.g. 3d descriptor when tbOnly2D checked)
  45.   aBMP.Canvas.FillRect(0,0, Width,Height);
  46.   aDetails:=ThemeServices.GetElementDetails(tbCheckBoxCheckedDisabled);
  47.   ThemeServices.DrawElement(aBMP.Canvas.Handle, aDetails, aRect, nil);
  48.   ilTreeView.Add(aBMP, nil);
  49.  
  50.   // 5 - tbCheckBoxMixedDisabled -  mixed selection disabled
  51.   aBMP.Canvas.FillRect(0,0, Width,Height);
  52.   aDetails:=ThemeServices.GetElementDetails(tbCheckBoxMixedDisabled);
  53.   ThemeServices.DrawElement(aBMP.Canvas.Handle, aDetails, aRect, nil);
  54.   ilTreeView.Add(aBMP, nil);
  55.  
  56.   FreeAndNil(aBMP);
  57. end;              
« Last Edit: August 28, 2018, 10:21:05 am by apeoperaio »

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2284
    • havefunsoft.com
Re: Themed checkboxes for TreeView
« Reply #1 on: July 18, 2018, 05:49:27 am »
please try trunk.

trunk is using theme-drawn checkboxes for the Object Inspector in IDE.
And they have been working so far
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

apeoperaio

  • Full Member
  • ***
  • Posts: 158
Re: Themed checkboxes for TreeView
« Reply #2 on: July 18, 2018, 10:49:25 am »
I tested on trunk with the following code (form with an imageless called ilTreeView and a TTreeView).
On lazarus trunk with cocoa none image is shown. See two images, one for carbon (1.8.4) and one for cocoa trunk.

Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
  9.   themes, Types;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     ilTreeView: TImageList;
  17.     TreeView1: TTreeView;
  18.     procedure FormCreate(Sender: TObject);
  19.   private
  20.     procedure fillimagelist;
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.FormCreate(Sender: TObject);
  35. var
  36.   node: TTreeNode;
  37.   i: Integer;
  38. begin
  39.   fillimagelist;
  40.   TreeView1.StateImages:= ilTreeView;
  41.   node:= TreeView1.Items.Add(nil, 'item1');
  42.   node.StateIndex:= 0;
  43.   for i:= 1 to ilTreeView.Count - 1 do begin
  44.     node:= TreeView1.Items.Add(nil, Format('item%d', [i+ 1]));
  45.     node.StateIndex:= i
  46.   end;
  47. end;
  48.  
  49. procedure TForm1.fillimagelist;
  50. var
  51.   aSize: TSize;
  52.   aBMP: TBitmap;
  53.   aDetails: TThemedElementDetails;
  54.   aRect: TRect;
  55. begin
  56.   aDetails:= ThemeServices.GetElementDetails(tbCheckBoxCheckedNormal);
  57.   aSize:= ThemeServices.GetDetailSize(aDetails);
  58.   ilTreeView.Width:= aSize.cx;
  59.   ilTreeView.Height:= aSize.cy;
  60.   aBMP:= TBitmap.Create;
  61.   with aBMP do
  62.     begin
  63.       SetSize(aSize.cx, aSize.cy);
  64.       Transparent:= True;
  65.       TransparentColor:= clForm;
  66.       Brush.Color:= TransparentColor;
  67.       Canvas.FillRect(0,0, Width,Height);
  68.     end;
  69.   aRect:=Rect(0, 0, aSize.cx, aSize.cy);
  70.   // 0 - tbCheckBoxUncheckedNormal - none selected
  71.   ThemeServices.DrawElement(aBMP.Canvas.Handle, aDetails, aRect, nil);
  72.   ilTreeView.Add(aBMP, nil);
  73.  
  74.   // 1 - tbCheckBoxCheckedNormal - all selected
  75.   aBMP.Canvas.FillRect(0,0, Width,Height);
  76.   aDetails:=ThemeServices.GetElementDetails(tbCheckBoxCheckedNormal);
  77.   ThemeServices.DrawElement(aBMP.Canvas.Handle, aDetails, aRect, nil);
  78.   ilTreeView.Add(aBMP, nil);
  79.  
  80.   // 2 - tbCheckBoxMixedNormal -  mixed selection
  81.   aBMP.Canvas.FillRect(0,0, Width,Height);
  82.   aDetails:=ThemeServices.GetElementDetails(tbCheckBoxMixedNormal);
  83.   ThemeServices.DrawElement(aBMP.Canvas.Handle, aDetails, aRect, nil);
  84.   ilTreeView.Add(aBMP, nil);
  85.  
  86.   // 3 - tbCheckBoxUncheckedDisabled - unchecked disabled
  87.   aBMP.Canvas.FillRect(0,0, Width,Height);
  88.   aDetails:=ThemeServices.GetElementDetails(tbCheckBoxUncheckedDisabled);
  89.   ThemeServices.DrawElement(aBMP.Canvas.Handle, aDetails, aRect, nil);
  90.   ilTreeView.Add(aBMP, nil);
  91.  
  92.   // 4 - tbCheckBoxCheckedDisabled - checked disabled (e.g. 3d descriptor when tbOnly2D checked)
  93.   aBMP.Canvas.FillRect(0,0, Width,Height);
  94.   aDetails:=ThemeServices.GetElementDetails(tbCheckBoxCheckedDisabled);
  95.   ThemeServices.DrawElement(aBMP.Canvas.Handle, aDetails, aRect, nil);
  96.   ilTreeView.Add(aBMP, nil);
  97.  
  98.   // 5 - tbCheckBoxMixedDisabled -  mixed selection disabled
  99.   aBMP.Canvas.FillRect(0,0, Width,Height);
  100.   aDetails:=ThemeServices.GetElementDetails(tbCheckBoxMixedDisabled);
  101.   ThemeServices.DrawElement(aBMP.Canvas.Handle, aDetails, aRect, nil);
  102.   ilTreeView.Add(aBMP, nil);
  103.  
  104.   FreeAndNil(aBMP);
  105. end;
  106.  
  107. end.
  108.  

apeoperaio

  • Full Member
  • ***
  • Posts: 158
Re: Themed checkboxes for TreeView
« Reply #3 on: July 18, 2018, 10:51:39 am »
Here the image using cocoa with lazarus 1.8.4

apeoperaio

  • Full Member
  • ***
  • Posts: 158
Re: Themed checkboxes for TreeView
« Reply #4 on: July 19, 2018, 07:07:36 pm »
Anyone can test it? Any suggestion?

MISV

  • Hero Member
  • *****
  • Posts: 633
Re: Themed checkboxes for TreeView
« Reply #5 on: August 21, 2018, 01:29:20 pm »
I also simulate using StateImages - I may try using your method to get better state images and report back
« Last Edit: August 22, 2018, 02:24:30 pm by MISV »

apeoperaio

  • Full Member
  • ***
  • Posts: 158
Re: Themed checkboxes for TreeView
« Reply #6 on: August 22, 2018, 03:31:29 pm »
Good to know. The method I am using works perfectly on windows and linux. Not on Mac. Did you manage to make it working?

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2284
    • havefunsoft.com
Re: Themed checkboxes for TreeView
« Reply #7 on: August 24, 2018, 05:05:36 am »
try trunk on or after revision 58766
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

apeoperaio

  • Full Member
  • ***
  • Posts: 158
Re: Themed checkboxes for TreeView
« Reply #8 on: August 24, 2018, 10:37:01 pm »
Tested and now it works better!
tbCheckBoxCheckedNormal and tbCheckBoxUncheckedNormal are drawn properly while tbCheckBoxMixedNormal is drawn as tbCheckBoxUncheckedNormal.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2284
    • havefunsoft.com
Re: Themed checkboxes for TreeView
« Reply #9 on: August 25, 2018, 05:07:16 am »
try r58774 then
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

apeoperaio

  • Full Member
  • ***
  • Posts: 158
Re: Themed checkboxes for TreeView
« Reply #10 on: August 25, 2018, 09:13:10 am »
Yes, it works!
Great!