TListFilterEdit additional conditions lazarus / 31-LCL ---------------------------------------------------------------------- staratel20 · 2016-03-06 08:21 Good day to all. At this time TListFilterEdit verify - is exists property Filter in Items - and filter it. But what if I want more complicated conditions? For example - if in Filter exists numbers - keep this string. I look to events and found: 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 OnCheckItem(Item: TObject; out Done: Boolean): Boolean; , but it operates with Objects, associated with strings. Think, that event I need should be 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 OnCheckItemString(ItemStr: string; out Done: Boolean): Boolean;    Am I right or exists another approach to resolve my problem? ---------------------------------------------------------------------- JuhaManninen · 2016-03-06 15:08 You are right, extra rules for filtering by Caption are not possible now. I would like to add a Caption parameter to the existing event although it can potentially break existing handlers. 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 TFilterItemEvent = function (Caption: String; Item: TObject; out Done: Boolean): Boolean of object;The change must be documented then. Another choice is a separate event as you suggested. ---------------------------------------------------------------------- staratel20 · 2016-03-06 16:16 Especially  I more like what you suggest, and don't wrote the same because of Quoteit can potentially break existing handlers, but in my own code I always do that hard work and restore code working by adding what necessary to it works again with new function/method parameters. So if another developers agree, I would like to realize patch of your suggest and then document it. Or if not - realize my own suggestion. Because I have not ability to write to Mailing list, I please you to ask and tell me what they sad about it. Thank for your answer. ---------------------------------------------------------------------- Bart · 2016-03-06 16:29 Quote from: JuhaManninen on March 06, 2016, 04:08:52 pmI would like to add a Caption parameter to the existing event although it can potentially break existing handlers. 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 OnCheckItem(Caption: String; Item: TObject; out Done: Boolean): Boolean; It'll break existing programs for sure. Quote from: JuhaManninen on March 06, 2016, 04:08:52 pmAnother choice is a separate event as you suggested. IMO the better solution. Bart ---------------------------------------------------------------------- staratel20 · 2016-03-06 16:43 Thanks Bart, I'll realize it as patch and post on bug tracker. ---------------------------------------------------------------------- staratel20 · 2016-03-06 20:56 Bart: Whatever, I think Juha right about parameter list. Because user may want some condition, based both on properties of string and Object , related to it. So I think it would looks 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 OnCheckItemString(AString: String; AItem: TObject; out Done: Boolean): Boolean;  or (maybe more beauty name): 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 OnCheckStringItem(AString: String; AItem: TObject; out Done: Boolean): Boolean; ---------------------------------------------------------------------- howardpc · 2016-03-06 22:40 If TFilterItemEvent were to be redefined 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 TFilterItemEvent = function (Item: TObject; out Done: Boolean; const aCaption: string=''): Boolean of object; would that necessarily break existing code? ---------------------------------------------------------------------- Bart · 2016-03-06 22:51 Yes, because it alters the signature of the event. And because, if you set the event in the OI (not by code) there will be no compiler error, but just unexplained crashes of your program. See also IDE does not warn if eventhandler has wrong signature, especially note 0090408 and the references to  issue 0024999 there. It's sort of acceptable if an alteration prevents your program from compiling, but making it crash, without any clue to the cause will make people very angry. Bart ---------------------------------------------------------------------- JuhaManninen · 2016-03-07 01:30 Quote from: Bart on March 06, 2016, 11:51:38 pmIt's sort of acceptable if an alteration prevents your program from compiling, but making it crash, without any clue to the cause will make people very angry. True. I was thinking the event is not used in many applications but we should not create hidden bugs even to those few. An idea: A new event type with Caption and Object would be TItemFilterEvent. A new event property would be OnItemFilter. The original property OnFilterItem would be deprecated and removed later. ---------------------------------------------------------------------- staratel20 · 2016-03-07 04:54 Juha: I think idea is good. Also 'Check' - it's my wrong, of cause everywhere I mean 'Filter'. Events with 'Check' can be symmetry modified or can be kept(I think better (1) ) What another thinking? ---------------------------------------------------------------------- Bart · 2016-03-07 10:45 Quote from: JuhaManninen on March 07, 2016, 02:30:33 amTrue. I was thinking the event is not used in many applications but we should not create hidden bugs even to those few. An idea: A new event type with Caption and Object would be TItemFilterEvent. A new event property would be OnItemFilter. The original property OnFilterItem would be deprecated and removed later. Maybe: OnFilterItemEx? In DoFilterItem (if a method like that exists, if not it should), simply do something like: Code: [Select]  if Assigned(FOnFilterEx) then     FOnFilterEx(...)   else     if Assigned(FOnFilter) then FOnFilter(...); end; OnFilterEx in such a scenario must carry all the info that OnFilter has now. Bart ---------------------------------------------------------------------- JuhaManninen · 2016-03-07 11:53 Quote from: Bart on March 07, 2016, 11:45:05 amMaybe: OnFilterItemEx? Fine with me. ---------------------------------------------------------------------- staratel20 · 2016-03-07 12:03 Think, Juha's idea greater. Caption and Object must be passed into a event by default, here is nothing Ex. At second, implementation code only at first look - good idea. What if in development process involved many users and assigning event are go not through IDE, but dynamically. So one of user handle event OnFilterItem, and another user debug application and see - OnFilterItemEx=nil. "Ou, that's what I need to handle my situation" - think another user and handle this event. And program will broken, because of OnFilterItem already not handled. So to avoid this situation we need more mosterous construction if goes this way, something 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     Pass1:=False;Pass2:=False;    Done1:=False;Done2:=False;       if Assigned(OnFilterItemEx) then Pass1:=OnFilterItemEx(fOriginalData[Origi], Done1);    if Assigned(OnFilterItem) then Pass2:=OnFilterItem(fOriginalData.Objects[Origi], Done2);     Pass:=Pass1 or Pass2;    // will be True if some of hander handle event    Done:=Done1 or Done2;   And if go Juha's way - code will save beauty and projects with obsolete event will simply to modify, because tokens to replace are not equal. ---------------------------------------------------------------------- Bart · 2016-03-07 12:34 Programmer should be wise enough to either use OnFilter or OnFilterEx. However we "fix" this, there will always be a possibility for one writing the actual program to screw it up. IMO the better scenario is: Implement a new eventDeprecate the old event (and remove it in the second major release after it was deprecated)Call the new event and the old event (the latter maybe conditionally) The end user (you as a programmer) must be considered sane enough to not handle a given situation in both event handlers. The situation is much like OnKeyPress and OnUtf8KeyPress. If you change something in OnUtf8KeyPress you're not supposed to do that again in OnKeyPress. (IIRC if you change Key in OnUtf8KeyPress, then OnKeyPress will not be callled. I'm too lazy to test that now, so pretty plese don't slap me in the face if I'm wrong.) Bart ---------------------------------------------------------------------- staratel20 · 2016-03-07 12:47 Ok, at least we have a plan. I'll post the patch, but actually you are already post almost all code of patch)) ---------------------------------------------------------------------- Bart · 2016-03-07 13:18 Another thought. In TListFilterEdit.SortAndFilter, after calling FOnFilterEx only call FOnFilter if Done = False? Code: [Select]var   TheCaption: String; begin ....   TheCaption := ... ....     if Assigned(FOnFilterItemEx) then       Pass:=OnFilterItemEx(TheCaption, fOriginalData.Objects[Origi], Done)     else       Pass:=False;     if ((not Pass) or (Pass and not Done)) and Assigned(FOnFilterItem) then        Pass:=OnFilterItem(fOriginalData.Objects[Origi], Done) ... end; Bart ---------------------------------------------------------------------- staratel20 · 2016-03-07 13:39 already submit a patch, there 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     // Filter with event handler if there is one.    if Assigned(OnFilterItemEx) then       Pass:=OnFilterItemEx(s,fOriginalData.Objects[Origi], Done)    else if Assigned(OnFilterItem) then       Pass:=OnFilterItem(fOriginalData.Objects[Origi], Done)    else       Pass:=False;  patch is here: http://bugs.freepascal.org/view.php?id=29793 P/s: if first parameter is Caption is conflicted with Form.Caption. PP/s: now on rev.51820 ---------------------------------------------------------------------- Bart · 2016-03-07 14:01 You forgot to attach the patch  Bart ---------------------------------------------------------------------- staratel20 · 2016-03-07 14:09 My bad). Carefully look to another fields but missing the main)). Already upload. ---------------------------------------------------------------------- howardpc · 2016-03-07 14:24 Quote from: howardpc on March 06, 2016, 11:40:24 pmIf TFilterItemEvent were to be redefined 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 TFilterItemEvent = function (Item: TObject; out Done: Boolean; const aCaption: string=''): Boolean of object; would that necessarily break existing code? Quote from: Bart on March 06, 2016, 11:51:38 pmYes, because it alters the signature of the event. And because, if you set the event in the OI (not by code) there will be no compiler error, but just unexplained crashes of your program. See also IDE does not warn if eventhandler has wrong signature, especially note 0090408 and the references to  issue 0024999 there. It's sort of acceptable if an alteration prevents your program from compiling, but making it crash, without any clue to the cause will make people very angry. In the case of changing the function signature with the addition of a parameter which is given a default value, I am not convinced you are correct. I just changed my svn copy of Lazarus trunk with that single alteration to TFilterItemEvent I proposed. The IDE compiles and runs well. Filteredit functionality works well. I have no unexplained crashes (admittedly after only 5 mins of testing). Like you, I have no desire to make people angry with unexplained crashes due to changes in source that are not backwards compatible. But I think this change is backwards compatible. ---------------------------------------------------------------------- Bart · 2016-03-07 16:24 You change the signature of the callback. This means there is now a different memory layout. This will lead to unexpected behaviour. If I am wrong (the signature does not alter) then it must be possible to assign a method with old signature to the event with the new signature in code: Foo.OnFilterItem := @MyOldOnfilterMethod. I would expect that won''t compile after you changed the signature of TFilterItemEvent. Bart ---------------------------------------------------------------------- howardpc · 2016-03-07 22:12 Well of course the compiler quite correctly prevents you trying to assign an incompatibly typed event, as it should if you try to mix the old and the new. This is exactly what you want when developing new code. If the function signature were to be changed in this way, you would need to flag up the change, and the need for it, and document the change fully and  the process of deprecating the older usage. My point was not about the changes needed in developing brand new code if the LCL signature of that event were to change, but the fact that this particular change does not break existing code (code written for the old signature) which as far as I can see will still run without problems whether you use the old or the new signature. ---------------------------------------------------------------------- Bart · 2016-03-07 22:59 And here we disagree. I will not repeat my arguments here. Bart ---------------------------------------------------------------------- JuhaManninen · 2016-03-08 21:22 I am back at my development machine and applied the patch with minor changes. I am still not happy with the "Ex" suffix. It will stay there as a historical remain for a long time. Anyway, no big deal. Please test. ---------------------------------------------------------------------- staratel20 · 2016-03-09 21:06 >>I am still not happy with the "Ex" suffix That's what I'm talking about a bit above. There nothing Ex - this set of params absolutely must be default.