Lazarus

Miscellaneous => Suggestions => LCL => Topic started by: Avishai on November 23, 2013, 12:40:56 am

Title: Non-Visual TStringList Component
Post by: Avishai on November 23, 2013, 12:40:56 am
The subject of Non-Visual Components came up in the Forum earlier and afterwards I was coding a TStringList for something unrelated. It occurred to me that maybe we could have a Non-Visual TStringList Component with a design-time editor similar to the TListBox editor that would make it easy to add/edit strings at design-time.  It would also take care of the Create method and also have an Owner so freeing it would be taken care of as well.  Has anyone given this any thought?  It seems so obvious that there must be some good reason why it doesn't already exist.
Title: Re: Non-Visual TStringList Component
Post by: Leledumbo on November 23, 2013, 01:19:08 am
Quote
It seems so obvious that there must be some good reason why it doesn't already exist.
Because it's easy enough to create one by code? :-[
Title: Re: Non-Visual TStringList Component
Post by: Avishai on November 23, 2013, 01:30:27 am
Of course it's easy to create it.  It's also easy to forget to free it.  It's easy to do a lot of things with code.  But it's not always as fast, especially if you're trying to add/edit strings in a RightToLeft language with a code editor that barely supports it.  It's very nearly impossible.   But the design-time editor for TListBox does an excellent job.
Title: Re: Non-Visual TStringList Component
Post by: Edson on November 23, 2013, 05:35:44 am
I think we are used to create and destroy object by code, by it would be great (and secure) if FPC/Lazarus could do this for us.

I know we will say "It's easy to create one by code", but considering the new languages features, Pascal could seem an Old Fashioned language.

I consider necessary, probably not a Component, but some kind of object that we just need to do:

Code: [Select]
var MyList: OStringList;
begin
   //no need for create o destroy MyList
  ...
end;

FPC can manage Static Object, so I IMHO it shoud be easy to implement something like this.
Title: Re: Non-Visual TStringList Component
Post by: irfanbagus on November 23, 2013, 06:55:40 am
@avishai
you can load TstringList from file/stream/resource.

@edson
how about this scenario
Code: [Select]
var
  MyObj: TObj;
begin
  if SomeCondition then
  begin
    MyObj:= TMyObj.Create;
    ...
    MyObj.Free;
  end
  else
  begin
    // Not using MyObj at all
    ...
  end;
end;
or this one
Code: [Select]
var
  MyObj: TObjParent;
begin
  ...
  if UseObjChild1 then
    MyObj:= TMyObjChild1.Create
  else
    MyObj:= TMyObjChild2.Create;
  ...
  MyObj.Free;
end;
in first case, initialization code of TObj will be called even if TObj not used (SomeCondition=False). and for second case that would be not possible at all.
Title: Re: Non-Visual TStringList Component
Post by: Avishai on November 23, 2013, 07:33:30 am
Using the logic of "it's easy to create by code"...

ALabel:= TLabel.Create(self);
AGrid:= TStringGrid.Create(self);
...

I guess we don't have much need for the Component Palette.



If you want to see what it's like to try to edit RightToLeft strings in the code editor paste this into your code.

Label1.Caption:= 'הקבוצות שפתחו חזק במפתיע כמו פילדלפיה';

Then try to insert a char in the middle of a word and watch the new char show up someplace unexpected.
Title: Re: Non-Visual TStringList Component
Post by: Leledumbo on November 23, 2013, 02:46:35 pm
Quote
It's also easy to forget to free it.
You can make use of attached gc unit and example.

If you really need to create a non-visual TStringList that can store strings, feel free to do so. I think that would be a nice addition.
Title: Re: Non-Visual TStringList Component
Post by: Avishai on November 23, 2013, 03:11:15 pm
@Leledumbo

As usual some very interesting and clever code.  Thank you.

I wish there were a couple of programmers of your caliber from the RightToLeft world using Lazarus.  They would have solved all, or at least most of the problems related to R2L long ago.  I have the knowledge of what R2L apps need to be but I don't have the programming skills to make it happen.  The problem is that R2L people take one look at Lazarus and see that it doesn't truly support R2L (for MSWindows at least) and move on to something that does.
Title: Re: Non-Visual TStringList Component
Post by: engkin on November 23, 2013, 03:59:43 pm
Quote
If you want to see what it's like to try to edit RightToLeft strings in the code editor paste this into your code.

Label1.Caption:= 'הקבוצות שפתחו חזק במפתיע כמו פילדלפיה';

Then try to insert a char in the middle of a word and watch the new char show up someplace unexpected.

I just gave it a try. That was not pleasant at all!

Code: [Select]
//number+word
  Label1.Caption:= '1הקבוצות 2שפתחו 3חזק 4במפתיע 5כמו 6פילדלפיה';

//Adding letter "A" in the middle
  Label1.Caption:= '1הקבוצות 2שפתחו 3חזק A 4במפתיע 5כמו 6פילדלפיה';


//word+number
  Label1.Caption:= 'הקבוצות1 שפתחו2 חזק3 במפתיע4 כמו5 פילדלפיה6';

The arrow keys did not work as visually expected.
Title: Re: Non-Visual TStringList Component
Post by: typo on November 23, 2013, 04:04:23 pm
I did the same with Insert and the chars was inserted from the left to the right.
Title: Re: Non-Visual TStringList Component
Post by: Avishai on November 23, 2013, 04:05:39 pm
Hahaha!  Welcome to my world  :D  As I said, editing R2L strings in a code editor is very nearly impossible.  That's not just Lazarus code editor either.  I've never seen a code editor that can do any better.

Honestly, I didn't expect anyone to try it.  Thanks.
Title: Re: Non-Visual TStringList Component
Post by: typo on November 23, 2013, 04:12:08 pm
Of couse a simple Length - X (supposing X the desired position) could do the job.
Title: Re: Non-Visual TStringList Component
Post by: Avishai on November 23, 2013, 04:51:39 pm
I guess I should have mentioned that if you edit a R2L string in the code editor and somehow you actually get it to "look right" in the code editor, when it's displayed in I.E. a TLabel it will be completely wrong.

Screwed up is 'Right' and 'Right' is screwed up.
Title: Re: Non-Visual TStringList Component
Post by: Edson on November 23, 2013, 05:24:21 pm
@irfanbagus

The
@edson
how about this scenario
...
in first case, initialization code of TObj will be called even if TObj not used (SomeCondition=False). and for second case that would be not possible at all.

The "static object" should behaves like currently the "Object Type" behave. It's storaged at the stack and not need for create or destroy.

Here, a simple example of how a static StringList could be implemented using the Object type:

Code: [Select]
program Project1; {$mode objfpc}{$H+}
uses Classes;

type OStringList = object
  item: array of string;
  function Count: integer;
  procedure Add(s: string);
end;

function OStringList.Count: integer;
begin
  Result := High(item);
end;

procedure OStringList.Add(s: string);
var n:integer;
begin
  n := High(item)+1;
  setlength(item, n+1);
  item[n] := s;
end;
////////////////// main ///////////////////
var MyList: OstringList;   //just declare
begin
  MyList.Add('Hi');       //and use
  Write(Mylist.item[0]);
  readln;                 //no need to free
end.

Unfortunately, Object Types, doesn't have the ability for execute automatically, a constructor, so it limits at what we can do with objects in this way.
Title: Re: Non-Visual TStringList Component
Post by: engkin on November 23, 2013, 05:33:46 pm
Avishai, are you familiar with Unicode control characters, like RLO and RS?
Title: Re: Non-Visual TStringList Component
Post by: Avishai on November 23, 2013, 05:59:25 pm
@engkin

Yes, but my solution is simply to use a text editor like NotePad to create/edit R2L text and the paste it into the code editor.  It isn't hard, it's just that it's one of many issues that I have to deal with that eats up time.
Title: Re: Non-Visual TStringList Component
Post by: engkin on November 23, 2013, 06:29:03 pm
Quote
..my solution is simply to use a text editor like NotePad to create/edit R2L text and the paste it into the code editor.

And when you need to modify it, take it back to NotePad, do the changes, then pasted again into the code editor.  Sounds fun!

What about commenting you code, same?

Either way the code editor does not support Unicode control characters.

Title: Re: Non-Visual TStringList Component
Post by: Avishai on November 23, 2013, 06:36:41 pm
Yes, working with R2L requires a lot of extra steps.  Copy/Paste, figuring out workarounds, experimenting...  It's just part of the game that you have to get used to.  But it add a lot to the time it takes to create a R2L app.  And the truth is that for MSWindows, Lazarus doesn't really support R2L.  That has been my goal from the beginning.  To add full R2L support, although I don't have the skills needed to do that.  But that doesn't keep me from trying. :)
Title: Re: Non-Visual TStringList Component
Post by: engkin on November 24, 2013, 12:28:25 am
Avishai, check this attachment. Drawing the icon took longer time than writing the code. It's not a beautiful icon even!  :-[
Title: Re: Non-Visual TStringList Component
Post by: Avishai on November 24, 2013, 02:20:20 am
Thanks engkin, you beat me to it.  I was almost finished, but after reading your code I realize I was trying to over complicate things quite a bit.  It works like a charm and editing R2L strings is so easy now.  It will save me a huge amount of time and aggravation.
Title: Re: Non-Visual TStringList Component
Post by: engkin on November 24, 2013, 05:26:03 am
 :-[ I can't claim credit for it. Most of the code was auto completed or generated by the component wizard. The property editor is already implemented.
Title: Re: Non-Visual TStringList Component
Post by: Leledumbo on November 24, 2013, 05:29:05 am
@engkin:
You might want to add it to Lazarus-CCR so everybody can download and test.
Title: Re: Non-Visual TStringList Component
Post by: Avishai on November 24, 2013, 09:22:31 am
I just found a great and unexpected side effect to using TSCStringList.

TStringList.Sort does not work for Hebrew.  In fact, I use it to create a 'randomized' list.  But the List Editor has a 'Sort' button and it does a fair job of sorting Hebrew. :)  I wonder how well it would work for Arabic.
Title: Re: Non-Visual TStringList Component
Post by: avra on November 24, 2013, 01:58:11 pm
You can make use of attached gc unit and example.
Leledumbo, this is very similar to how I wanted to exploit interfaces but you have done it in a simpler and more elegant way. How free is your code for use? Can it be used even in commercial applications? What about changes? If I rename Guard to GC do I have to publish changes? I know that code is short, and some of these questions are dumb, but please give it some license. There are people that don't care about licenses, but there are also people that will not put anything into their work unless it has a proper license that fits. Something like PD, BSD or MPL is desirable  :D, but it's up to you.
Title: Re: Non-Visual TStringList Component
Post by: Leledumbo on November 24, 2013, 03:10:54 pm
Quote
How free is your code for use? Can it be used even in commercial applications? What about changes? If I rename Guard to GC do I have to publish changes?
First, it's not my code. I found it a couple of years ago when looking for garbage collection in object pascal. I can't seem to find the original article anymore...
Title: Re: Non-Visual TStringList Component
Post by: avra on November 25, 2013, 11:04:34 am
Quote
How free is your code for use? Can it be used even in commercial applications? What about changes? If I rename Guard to GC do I have to publish changes?
First, it's not my code. I found it a couple of years ago when looking for garbage collection in object pascal. I can't seem to find the original article anymore...
OK, thanks. I found it here: http://delphi.cjcsoft.net/viewthread.php?tid=48730
I have also bumped into this more complex idea: http://edn.embarcadero.com/article/28217
Title: Re: Non-Visual TStringList Component
Post by: marcov on November 25, 2013, 03:03:36 pm
Isn't simply
Code: [Select]
Type   
   TMyStringList = class(TComponent)
                               fstrlst : TStrings;
                            public
                               constructor create(parent:tsomething); override;  //check for exact paramter
                               destructor  destroy; override;
                           published
                              property strings: tstringlist read fstrlst write fstrlst;
                               end;
                           
                               constructor TMyStringList.create(parent:tsomething);  //check for exact paramter
                              begin
                                 fstrlst:=tstringlist.create;
                              end;
                               destructor  TMyStringList.destroy;
                                begin
                                   fstrlst.free;
                                end;
Title: Re: Non-Visual TStringList Component
Post by: engkin on November 26, 2013, 05:38:43 am
@engkin:
You might want to add it to Lazarus-CCR so everybody can download and test.

Leledumbo, :) thank you for the encouragement.


Avishai, I don't know if you noticed, but double clicking the component does nothing. I thought it might be a good idea to open the "String Editor Dialog". I attached it here with this post. Again, most of the code was already there, I just needed to rearrange it.


Marcov, yes, it is!
Title: Re: Non-Visual TStringList Component
Post by: Avishai on November 26, 2013, 08:37:57 am
engkin, THANKS!  I can't understand why it took so long for me to realize that I needed such a component or just how valuable It would be.  It has taken one of my nightmares and turned it into fun.

My approach for writing it was so wrong.  I would have eventually ended up with something that worked, but your code is so clean.
Title: Re: Non-Visual TStringList Component
Post by: Windsurfer on November 26, 2013, 01:56:56 pm
#Avishai,

My apologies for this being off topic.

I am writing a small example program to show how the defaulttranslations unit works, and ask for a favour.

I want to include a right to left language example, but need some strings translated accurately into arabic or hebrew, whichever you prefer, or both.

If you are happy to translate them, they are in the project1.po file in the attachment. The msgstr "" is the part that needs to be completed.

Thanks in anticipation.
Title: Re: Non-Visual TStringList Component
Post by: Avishai on November 26, 2013, 02:17:11 pm
Windsurfer, I'm not the best person to ask.  I'm a new immigrant to Israel and I'm still learning the language.  I think as any language, there is 'common/street language' and 'high language' and then there's 'computer jargon'.  What I have tried to learn is 'common language' first.  Anyway, I think there are more Arabic speaking people using Lazarus.  You might be better off asking one of them.  Zaher Dirkey might be able to help you, but he lives in Syria so I don't know if he's able to get to a computer because of the fighting.

I wish more people would add their country to their profile.  I think it would help for things like this.
Title: Re: Non-Visual TStringList Component
Post by: Windsurfer on November 26, 2013, 07:10:28 pm
Thanks Aishai,

I'll ask around. I'll also add my country to my profile.