Recent

Author Topic: Better code for Lazarus  (Read 389 times)

edgarrod71

  • Jr. Member
  • **
  • Posts: 68
Better code for Lazarus
« on: March 08, 2023, 04:45:03 pm »
I'm not the best programmer (probably), but when I see I can improve something, I do it.  Since I couldn't work with last lazarus because of "^ñáéíóúÁÉÍÓÚÜüÑ" issue, I downgraded to v2.0.12 and linking my programs to 64 bits with it.  I like dark interfaces and I was trying to modify IDE v2.0.12.  When I selected new units or forms, the dialog is mostly "white" and sometimes a little slow when I go back and forth, so I changed the Form color and Font color and put ListBox and Splitter to inherit them, Splitter changed normally but not ListBox, so I took a look (lol) inside the DrawItem and found that there is a little recalculations on every item (slow) and improved the code to make my goal, so I put an onShow to calculate those "once" and redefined DrawItem.  Here's the code.

this block goes on the class definition
Code: Pascal  [Select][+][-]
  1.     procedure FormShow(Sender: TObject);
  2.        ·
  3.        ·
  4.        ·
  5.     procedure HelpButtonClick(Sender: TObject);
  6.     procedure CancelButtonClick(Sender :TObject);
  7.     procedure MultiselectCheckBoxClick(Sender :TObject);
  8.   private
  9.     img_W: byte;  // IDEImages.Images_16.Width
  10.     S96F: byte;  // Scale96ToFont(4)
  11.     aR: TRect;  // Global Rect instead of Local
  12.     FIdleConnected: boolean;
  13.     FItemType: TIDEProjectItem;
  14.     FSortAlphabetically: boolean;
  15.     FImageIndex: Integer;
  16.        ·
  17.        ·
  18.        ·  

Implementation section
Code: Pascal  [Select][+][-]
  1. procedure TViewUnitDialog.ListboxDrawItem(Control: TWinControl; Index: Integer;
  2.   ARect: TRect; State: TOwnerDrawState);
  3. begin
  4.   if Index < 0 then Exit;
  5.   with ListBox do begin
  6.     if odSelected in state then begin
  7.       Canvas.Brush.Color := Parent.Font.Color;
  8.       Canvas.Font.Color := Parent.Color;
  9.       Canvas.Font.Style := [fsBold]
  10.     end
  11.     else begin
  12.       Canvas.Brush.Color := Parent.Color;
  13.       Canvas.Font.Color := Parent.Font.Color;
  14.       Canvas.Font.Style := [];  // normal font
  15.     end;
  16.     Canvas.FillRect(aRect);
  17.     IDEImages.Images_16.Draw(Canvas, 1, aRect.Top, FImageIndex);
  18.     Canvas.TextOut(ARect.Left + img_W + S96F, ARect.Top, Items[Index]); // Less Calculations
  19.  
  20.     { This avoids blank from bottom of last item to ClientHeight }
  21.     if (ClientHeight > aRect.Bottom) and
  22.        (index = pred(count)) then begin
  23.         Canvas.Brush.Color := Parent.Color;
  24.         aR.Top := aRect.Bottom; // (aRect.Bottom - aRect.Top) * Count;
  25.         aR.Bottom := ClientHeight;
  26.         Canvas.FillRect(aR);
  27.     end;
  28.   end;
  29. end;

onShow
Code: Pascal  [Select][+][-]
  1. procedure TViewUnitDialog.FormShow(Sender: TObject);
  2. begin
  3.   aR := ListBox.ClientRect; // must be initialized here
  4.   img_W := IDEImages.Images_16.Width;
  5.   S96F := Scale96ToFont(4);
  6. end;

 

TinyPortal © 2005-2018