Recent

Author Topic: Non-Visual TStringList Component  (Read 28925 times)

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Non-Visual TStringList Component
« 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.
Lazarus Trunk / fpc 2.6.2 / Win32

Leledumbo

  • Hero Member
  • *****
  • Posts: 8744
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Non-Visual TStringList Component
« Reply #1 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? :-[

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Non-Visual TStringList Component
« Reply #2 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.
Lazarus Trunk / fpc 2.6.2 / Win32

Edson

  • Hero Member
  • *****
  • Posts: 1296
Re: Non-Visual TStringList Component
« Reply #3 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.
« Last Edit: November 23, 2013, 05:41:04 am by Edson »
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

irfanbagus

  • Jr. Member
  • **
  • Posts: 73
Re: Non-Visual TStringList Component
« Reply #4 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.

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Non-Visual TStringList Component
« Reply #5 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.
« Last Edit: November 23, 2013, 08:09:27 am by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

Leledumbo

  • Hero Member
  • *****
  • Posts: 8744
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Non-Visual TStringList Component
« Reply #6 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.

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Non-Visual TStringList Component
« Reply #7 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.
Lazarus Trunk / fpc 2.6.2 / Win32

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Non-Visual TStringList Component
« Reply #8 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.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Non-Visual TStringList Component
« Reply #9 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.

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Non-Visual TStringList Component
« Reply #10 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.
Lazarus Trunk / fpc 2.6.2 / Win32

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Non-Visual TStringList Component
« Reply #11 on: November 23, 2013, 04:12:08 pm »
Of couse a simple Length - X (supposing X the desired position) could do the job.

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Non-Visual TStringList Component
« Reply #12 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.
« Last Edit: November 30, 2013, 03:57:07 pm by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

Edson

  • Hero Member
  • *****
  • Posts: 1296
Re: Non-Visual TStringList Component
« Reply #13 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.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Non-Visual TStringList Component
« Reply #14 on: November 23, 2013, 05:33:46 pm »
Avishai, are you familiar with Unicode control characters, like RLO and RS?

 

TinyPortal © 2005-2018