Lazarus

Programming => Graphics and Multimedia => Graphics => Topic started by: lainz on April 22, 2021, 06:03:02 pm

Title: [SOLVED] TextRect and ListBox issues
Post by: lainz on April 22, 2021, 06:03:02 pm
Hi, I have this code, that draws the listbox rows.

The problem is that the numbers are moved to the Right (I'm using Lazarus and FPC trunk).

See attached screenshot. The product in the Object of TProductos says:

Quote
1 KG HELADO

and when drawn it shows

Quote
KG HELADO 1

Any ideas?

Code: Pascal  [Select][+][-]
  1. procedure TPOSBerryLiteSearch.ListBoxDraw(Control: TWinControl;
  2.   Index: integer; ARect: TRect; State: TOwnerDrawState);
  3.  
  4.   procedure DrawListItemBackground();
  5.   begin
  6.     if (Index mod 2 = 0) then
  7.     begin
  8.       FListBox.Canvas.Pen.Color := TPOSBerryTheme.getTheme.rowEvenColor;
  9.       FListBox.Canvas.Brush.Color := TPOSBerryTheme.getTheme.rowEvenColor;
  10.     end
  11.     else
  12.     begin
  13.       FListBox.Canvas.Pen.Color := TPOSBerryTheme.getTheme.rowOddColor;
  14.       FListBox.Canvas.Brush.Color := TPOSBerryTheme.getTheme.rowOddColor;
  15.     end;
  16.     FListBox.Canvas.Rectangle(ARect);
  17.   end;
  18.  
  19.   procedure DrawListItemSelection();
  20.   begin
  21.     FListBox.Canvas.Pen.Color := TPOSBerryTheme.getTheme.rowSelectedBorderColor;
  22.     FListBox.Canvas.Brush.Color := TPOSBerryTheme.getTheme.rowSelectedColor;
  23.     FListBox.Canvas.Rectangle(ARect);
  24.   end;
  25.  
  26.   procedure DrawListItemProductos();
  27.   var
  28.     p: TProductos;
  29.     ts: TTextStyle;
  30.   begin
  31.     p := TProductos(FListBox.Items.Objects[Index]);
  32.     if (p.bfavorito.IsNull) or (p.bfavorito.Value = 'F') then
  33.       FIcons.Draw(0, 1, FListBox.Canvas, 5, ARect.Top, Scale96ToForm(50),
  34.         ARect.Height, False, 255)
  35.     else
  36.       FIcons.Draw(1, 1, FListBox.Canvas, 5, ARect.Top, Scale96ToForm(50),
  37.         ARect.Height, False, 255);
  38.     ts.Alignment := taLeftJustify;
  39.     ts.Layout := tlCenter;
  40.     ts.SystemFont := False;
  41.     ts.Clipping := True;
  42.     ts.SingleLine := True;
  43.     ts.Wordbreak := False;
  44.     ts.EndEllipsis := False;
  45.  
  46.     FListBox.Canvas.TextRect(Rect(ARect.Left + 50, ARect.Top,
  47.       Scale96ToForm(250), ARect.Bottom), Scale96ToForm(60),
  48.       0, p.scodproducto.Value, ts);
  49.     FListBox.Canvas.TextRect(Rect(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom),
  50.       Scale96ToForm(260), 0, p.snombre.Value, ts);
  51.   end;
  52.  
  53.   procedure DrawListItemClientes();
  54.   var
  55.     p: TClientes;
  56.     ts: TTextStyle;
  57.   begin
  58.     p := TClientes(FListBox.Items.Objects[Index]);
  59.     if (p.bfavorito.IsNull) or (p.bfavorito.Value = 'F') then
  60.       FIcons.Draw(2, 1, FListBox.Canvas, 5, ARect.Top, Scale96ToForm(50),
  61.         ARect.Height, False, 255)
  62.     else
  63.       FIcons.Draw(1, 1, FListBox.Canvas, 5, ARect.Top, Scale96ToForm(50),
  64.         ARect.Height, False, 255);
  65.     ts.Alignment := taLeftJustify;
  66.     ts.Layout := tlCenter;
  67.     ts.SystemFont := False;
  68.     ts.Clipping := True;
  69.     ts.SingleLine := True;
  70.     ts.Wordbreak := False;
  71.     ts.EndEllipsis := False;
  72.  
  73.     FListBox.Canvas.TextRect(Rect(ARect.Left + 50, ARect.Top,
  74.       Scale96ToForm(250), ARect.Bottom), Scale96ToForm(60),
  75.       0, p.scodcliente.Value, ts);
  76.     FListBox.Canvas.TextRect(Rect(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom),
  77.       Scale96ToForm(260), 0, p.srazonsocial.Value, ts);
  78.   end;
  79.  
  80. begin
  81.   FListBox.Canvas.Font := Parent.Font;
  82.   if (odSelected in State) then
  83.     DrawListItemSelection()
  84.   else
  85.     DrawListItemBackground();
  86.  
  87.   case SearchKind of
  88.     skProductos: DrawListItemProductos();
  89.     skClientes: DrawListItemClientes();
  90.   end;
  91. end;  
Title: Re: TextRect and ListBox issues
Post by: howardpc on April 22, 2021, 08:02:36 pm
You don't explicitly set
Code: Pascal  [Select][+][-]
  1. ts.RightToLeft
so it takes a value you may not expect?
Title: Re: TextRect and ListBox issues
Post by: lainz on April 22, 2021, 10:18:46 pm
You don't explicitly set
Code: Pascal  [Select][+][-]
  1. ts.RightToLeft
so it takes a value you may not expect?

Thanks!!

Setting it to False does the trick.  :)
Title: Re: [SOLVED] TextRect and ListBox issues
Post by: furious programming on April 23, 2021, 06:44:36 pm
The easiest way is just to initialize the variable, and then, set only those properties that should have non-default values.

Code: Pascal  [Select][+][-]
  1. ts := Default(TTextStyle);
  2.  
  3. ts.Layout := tlCenter;
  4. ts.Clipping := True;
  5. ts.SingleLine := True;

Full confidence, less code.
Title: Re: [SOLVED] TextRect and ListBox issues
Post by: wp on April 24, 2021, 11:19:00 pm
The easiest way is just to initialize the variable, and then, set only those properties that should have non-default values.

Code: Pascal  [Select][+][-]
  1. ts := Default(TTextStyle);
  2.  
  3. ts.Layout := tlCenter;
  4. ts.Clipping := True;
  5. ts.SingleLine := True;
But this overwrites TextStyle elements that may have been set before. To avoid this I usually initialize the record as ts := Canvas.TextStyle and then apply my changes.
TinyPortal © 2005-2018