Overloading the onpaint event lazarus / 21-General ---------------------------------------------------------------------- seany · 2023-07-15 01:48 Assume, Please, that I have a TBCPanel, I can do : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i myTBCPanel.OnPaint := @myFunc  where myFunc contains the code to draw lines and text and whatnot on my BCpanel. I want to have a chain of functions like : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure myFunc1(Sender: TObject)procedure myFunc2(Sender: TObject)... procedure myFuncn(Sender: TObject)  None of these functions are aware of any other. But want the onpaint event to be able to call an arbitrary list of these one. That is, I want something like : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  someTypedVar := [myFunc4, myFunc61, myFunc42] ; // this array i want to fill dynamicallymyTBCPanel.OnPaint := @ some magic here ; // so that, the code calls in sequence everything inside someTypedvar in the given sequence   I am not sure this is even possible, because i think everything needs to happen inside the onpaint function. But in the off chance it is possible, how can i do it? Thank you. ---------------------------------------------------------------------- TRon · 2023-07-15 02:54 Note: hot, bothered, sleepy, from memory and untested. Quote from: seany on July 15, 2023, 03:48:34 amI am not sure this is even possible, because i think everything needs to happen inside the onpaint function. Depends. Where or what determines the contents of/for SomeTypedVar ? Is it done before the onpaint event is called (for the first time) ? How many times do the contents change or do you have an (static) amount of predefined arrays ? QuoteBut in the off chance it is possible, how can i do it? Also depends. If the order of the array can not be represented by an enum (because the order might differ) then I would probably opt for something like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TMyArray = array of TNotifyEvent; var  SomeArray : TMyArray = (func1, func30, func40); procedure TMyBCPanel.DoPaint(Sender: TObject);var  x: integer;begin  for x := low(SomeArray) to High(SomeArray)     do SomeArray[x](Sender);end;  In case you can not use a 'global' variable like that, e.g. it has to be tight to the BCpanel then you can abuse the tag property of BCPanel to point to the array of functions. which can then be 'obtained' with: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure xxxx.DoPaint(Sender: TObject);var  currentArray : TMyArray;  x: integer;begin  CurrentArray := TMyarray((Sender as TBCPanel).Tag);    for x := low(CurrentArray) to High(CurrentArray)     do CurrentArray[x](Sender);end; procedure xxx.SetArrayToUse;begin  MyTBPanel.Tag := PtrInt(SomeArray);  // after having set this the paint procedure should use this   // arrayend;  If you can use an enum (size wise and order wise) then you could opt for storing an enum in the tag property. The enum would the have to correspond (index/array wise) with your function-list. ---------------------------------------------------------------------- seany · 2023-07-15 03:09 Quote from: TRon on July 15, 2023, 04:54:24 am Depends. Where or what determines the contents of/for SomeTypedVar ? Some condition. All the functions are for drawing things on renderPanel.canvas, where renderPanel is TBCPanel. So, roughly Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if condition1 then someArray : myArray = (func1,func2, func684);if condition2 then someArray : myArray = (func334,func450, func6); // here I want the magic  etc. The magic should now be: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i onPaint_execute(passed_TBCPanel, myArray);  // ideally packed in a function, but not a must.  Quote from: TRon on July 15, 2023, 04:54:24 amIs it done before the onpaint event is called (for the first time) ? How many times do the contents change or do you have an (static) amount of predefined arrays ? 'Throughout the program's lifetime. There are too many combinations. Including, infuture, there will be a need to pass parameters to each such funcN, to slightly change their behaviors. (But can ignore for now, will open a new topic in future). Quote from: TRon on July 15, 2023, 04:54:24 am If the order of the array can not be represented by an enum (because the order might differ) then I would probably opt for something like: There will be way too many. I wanted to try like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure xxxx.DoPaint(Sender: TObject ; (*here is the strange part*) passedArray: TMyArray);var   x: integer;begin  CurrentArray := passedArray;   for x := low(CurrentArray) to High(CurrentArray)    do CurrentArray[x](Sender);end; procedure xxx.SetArrayToUse;begin  MyTBPanel.Tag := PtrInt(SomeArray);  // after having set this the paint procedure should use this  // arrayend;  But the onpaint event does not accept the additional parameter. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Nachrichten, Warnungen: 2Warning: other unit files search path (aka unit path) of "KControlsLaz 1.7.3" contains "/home/monsoon/.lazarus/on linepackagemanager/packages/kcontrols/source", which belongs to package "KControlsBase"Warning: other unit files search path (aka unit path) of "KControlsBase 1.7.3" contains "/home/monsoon/.lazarus/online packagemanager/packages/kcontrols/source", which belongs to package "KControlsLaz"Projekt kompilieren, Ziel: project1: Exit code 1, Fehler: 2, Warnungen: 4, Hinweise: 11trenderengine.pas(340,3) Note: Local variable "sideLength" not usedtrenderengine.pas(354,3) Note: Local variable "P" not usedtrenderengine.pas(355,3) Note: Local variable "i" not usedtrenderengine.pas(356,3) Note: Local variable "phi" not usedtrenderengine.pas(239,48) Hint: Parameter "a" not usedtrenderengine.pas(240,26) Hint: Parameter "pageNum" not usedtrenderengine.pas(8,55) Hint: Unit "StdCtrls" not used in TRenderEnginetrenderengine.pas(8,75) Hint: Unit "menu_dataTypes" not used in TRenderEnginetrenderengine.pas(8,90) Hint: Unit "BCLabel" not used in TRenderEngineunit1.pas(89,55) Hint: Parameter "NewOffset" not usedunit1.pas(90,11) Hint: Parameter "Accept" not usedunit1.pas(316,22) Error: Incompatible types: got "" expected "" ---------------------------------------------------------------------- TRon · 2023-07-15 03:21 If you know that the event is only invoked for the BCPanel then the sender is the BCPanel. Abuse the tag of BCPanel to pass any information. If a single PtrInt is not enough then let it point to a record/class/object/list that contains any amount of entries that you wish for. edit: Like I showed in the mockup example Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   CurrentArray := TMyarray((Sender as TBCPanel).Tag); You have to make sure that when you retrieve the tag property that it is properly casted to whatever the PtrInt points to (record/class/object/list/etc). Having said that I probably should have passed and cast it as PMyArray (Pointer to TMyArray) ---------------------------------------------------------------------- TRon · 2023-07-15 03:39 Let me simplify and adapt to your example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMyForm.onPaint_execute(passed_TBCPanel: TBCPanel; var myArray: TMyArray);begin  passed_TBCPanel.Tag := @myArray;  MyForm.DoPaintBCPanel(passed_TBCPanel);end; procedure TMyForm.DoPaintBCPanel(Sender: TObject);var  currentArray : PMyArray;  x: integer;begin  CurrentArray := PMyarray((Sender as TBCPanel).Tag);   for x := low(CurrentArray^) to High(CurrentArray^)  do CurrentArray^.[x](Sender);end; Except in my example I did it in a single step (as I did not know where you wanted to determine which array should be used). Does that explain it more clear for you ? edit: made a naming error and did not pass myarray as var (both fixed now) edit2: actually, the following is more correct. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMyForm.onPaint_execute(passed_TBCPanel: TBCPanel; var myArray: TMyArray);begin  passed_TBCPanel.Tag := @myArray;  passed_TBCPanel.OnPaint := @MyForm.DoPaintBCPanel;  passed_TBCPanel.Paint; // assuming TBCPanel has a Paint method, otherwise use refresh or whatever invokes the OnPaint event handler for TBCPanel. Only here for demonstration purpose, if your GUI does invoke the OnPaint handler somewhere else then that is ok.end; procedure TMyForm.DoPaintBCPanel(Sender: TObject);var  currentArray : PMyArray;  x: integer;begin  CurrentArray := PMyarray((Sender as TBCPanel).Tag);   for x := low(CurrentArray^) to High(CurrentArray^)    do CurrentArray^.[x](Sender);end;  edit: tried and tested (well, using writeln's but replace with your actual drawing code): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type   { TForm1 }   TForm1 = class(TForm)    MyBCPanel: TBCPanel;   public    procedure MyFunc1(Sender: TObject);    procedure MyFunc2(Sender: TObject);    procedure MyFunc25(Sender: TObject);    procedure MyFunc46(Sender: TObject);    procedure DoPaintBCPanel(Sender: TObject);    procedure SomeButtonClick(Sender: TObject);  end; var  Form1: TForm1; implementation {$R *.lfm} type  PMyArray= ^TMyArray;  TMyArray= array of TNotifyEvent; function Condition(ACondition: boolean): Boolean;begin  result:= ACondition;end;  { TForm1 } procedure TForm1.MyFunc1(Sender: TObject);begin   writeln('One')end; procedure TForm1.MyFunc2(Sender: TObject);begin  writeln('Two')end; procedure TForm1.MyFunc25(Sender: TObject);begin  writeln('Twenty-Five')end; procedure TForm1.MyFunc46(Sender: TObject);begin  writeln('Fourty-Six')end; procedure TForm1.DoPaintBCPanel(Sender: TObject);var  currentArray: PMyArray;  x: integer;begin  CurrentArray:= PMyarray((Sender as TBCPanel).Tag);   for x:= low(CurrentArray^) to High(CurrentArray^)    do CurrentArray^[x](Sender);end; procedure TForm1.SomeButtonClick(Sender: TObject);var  x: integer= 1;  SomeArray: TMyArray= nil;begin  if condition(x = 0)    then SomeArray:= [@MyFunc1, @MyFunc2, @MyFunc25, @MyFunc46];   if condition(x = 1)    then SomeArray:= [@MyFunc46, @MyFunc2, @MyFunc25];   MyBCPanel.OnPaint:= @DoPaintBCPanel;  MyBCPanel.Tag:= PtrInt(@SomeArray);  MyBCPanel.Paint;   SomeArray:= nil;  // SetLength(SomeArray, 0);end; As said before, the only thing I am not sure about is if TBCPanel has a paint method or uses other means to invoke the OnPaint handler (I don't have Lazarus around atm so had to go with what is present in github BGRA repository and was not able to locate a paint method so quickly).