Recent

Author Topic: [SOLVED] Loop Thru Components Setting Visibility And Enableability  (Read 12191 times)

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: [SOLVED] Loop Thru Components Setting Visibility And Enableability
« Reply #30 on: September 24, 2017, 08:43:13 pm »
Your text says
"...with the 64 bit controlling private and bit 32 the test."

and your code says:
Code: Pascal  [Select][+][-]
  1. const cPrivBit=128; cTestBit=64;

Following the code, here is my first try:
Code: Pascal  [Select][+][-]
  1. procedure SetVisibleByTags_bitwise(Parent: TWinControl; pPrivate, pTest:boolean);
  2. const cPrivBit=7 {128}; cTestBit=6 {64};
  3. type
  4.   TUseBits = bitpacked array[0..7] of boolean;
  5.   TBooleanTag = bitpacked record
  6.     case boolean of
  7.     true: (Val: PtrInt);
  8.     false: (Bit: TUseBits)
  9.   end;
  10. var
  11.   C: TComponent;
  12.   aTag: TBooleanTag;
  13. begin
  14.  for C in Parent do begin
  15.    if C.Tag<>0 then begin
  16.      aTag.Val := C.Tag;
  17.      if C is TMenuItem then TMenuItem(C).Visible := (aTag.Bit[cPrivBit] and pPrivate) or (aTag.Bit[cTestBit] and pTest)
  18.      else if C is TControl then TControl(C).Visible := (aTag.Bit[cPrivBit] and pPrivate) or (aTag.Bit[cTestBit] and pTest);
  19.    end;
  20.  end;
  21. end;

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: [SOLVED] Loop Thru Components Setting Visibility And Enableability
« Reply #31 on: September 24, 2017, 09:16:10 pm »
If you intend on adding more than two categories, in addition to Private and Test, then the following procedure is more logical:
Code: Pascal  [Select][+][-]
  1. type
  2.   TUseBits = bitpacked array[0..7] of boolean;
  3.   TBooleanTag = bitpacked record
  4.     case boolean of
  5.     true: (Val: PtrInt);
  6.     false: (Bit: TUseBits)
  7.   end;
  8.  
  9. procedure SetVisibleByTags_bitwise2(Parent: TWinControl; ControlTag: TBooleanTag);overload;
  10. const mask=128+64;
  11. var
  12.   C: TComponent;
  13.   aTag: TBooleanTag;
  14. begin
  15.  for C in Parent do begin
  16.    if C.Tag<>0 then begin
  17.      aTag.Val := C.Tag and mask;
  18.      if C is TMenuItem then TMenuItem(C).Visible := (aTag.Val and ControlTag.Val) <> 0
  19.      else if C is TControl then TControl(C).Visible := (aTag.Val and ControlTag.Val) <> 0;
  20.    end;
  21.  end;
  22. end;

But if you insist on using categories as parameters instead of one variable, then you need to use another overloaded version that has the signature you want:
Code: Pascal  [Select][+][-]
  1. procedure SetVisibleByTags_bitwise2(Parent: TWinControl; pPrivate, pTest:boolean);overload;
  2. var
  3.   ControlTag: TBooleanTag;
  4. begin
  5.  ControlTag.Val := 0; { Not needed, for 'mask' used in the next proc clears unused bits }
  6.  ControlTag.bit[7] := pPrivate; ControlTag.bit[6] := pTest;
  7.  SetVisibleByTags_bitwise2(Parent, ControlTag);
  8. end;

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: [SOLVED] Loop Thru Components Setting Visibility And Enableability
« Reply #32 on: September 25, 2017, 02:24:12 am »
Your text says
"...with the 64 bit controlling private and bit 32 the test."

and your code says:
Code: Pascal  [Select][+][-]
  1. const cPrivBit=128; cTestBit=64;

Oops. I kept changing the values during testing, as one should do before publishing  :D

Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: [SOLVED] Loop Thru Components Setting Visibility And Enableability
« Reply #33 on: September 25, 2017, 02:27:47 am »
If you intend on adding more than two categories, in addition to Private and Test, then the following procedure is more logical:
Code: Pascal  [Select][+][-]
  1. type
  2.   TUseBits = bitpacked array[0..7] of boolean;
  3.   TBooleanTag = bitpacked record
  4.     case boolean of
  5.     true: (Val: PtrInt);
  6.     false: (Bit: TUseBits)
  7.   end;
  8.  
  9. procedure SetVisibleByTags_bitwise2(Parent: TWinControl; ControlTag: TBooleanTag);overload;
  10. const mask=128+64;
  11. var
  12.   C: TComponent;
  13.   aTag: TBooleanTag;
  14. begin
  15.  for C in Parent do begin
  16.    if C.Tag<>0 then begin
  17.      aTag.Val := C.Tag and mask;
  18.      if C is TMenuItem then TMenuItem(C).Visible := (aTag.Val and ControlTag.Val) <> 0
  19.      else if C is TControl then TControl(C).Visible := (aTag.Val and ControlTag.Val) <> 0;
  20.    end;
  21.  end;
  22. end;

But if you insist on using categories as parameters instead of one variable, then you need to use another overloaded version that has the signature you want:
Code: Pascal  [Select][+][-]
  1. procedure SetVisibleByTags_bitwise2(Parent: TWinControl; pPrivate, pTest:boolean);overload;
  2. var
  3.   ControlTag: TBooleanTag;
  4. begin
  5.  ControlTag.Val := 0; { Not needed, for 'mask' used in the next proc clears unused bits }
  6.  ControlTag.bit[7] := pPrivate; ControlTag.bit[6] := pTest;
  7.  SetVisibleByTags_bitwise2(Parent, ControlTag);
  8. end;

Hey that's nice. I don't think there is a need for more than 2 categories (Private and Test covers a wide area), certainly not in the current project.

But I'll keep the code on file.

Many thanks
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

 

TinyPortal © 2005-2018