Recent

Author Topic: Characters for Widgetset  (Read 13378 times)

watte

  • New Member
  • *
  • Posts: 13
Characters for Widgetset
« on: March 29, 2011, 04:02:36 pm »
hello

I have to send charakters to a TEdit-/TComboBox- Field. It seems to me, new characters (from keyboard) will be handled by the widgetset-object?.
How can I send a character without the keyboard to a text-field for showing in the field??


Quote
field.Text:=field.Text + character;
doesn't work, because of the cursorposition.

Quote
field.SelText:=character;
doesn't work too, because of mismatches of SelStart and SelLength...

     example:  *     Text in Field "123|45678" ( | is the cursor between 3 and 4  )
                  ->    field.SelText:='z';           overwrites 3 characters  ( maybe it is a BUG ?!  ... SelStart = 3  )
             result:    Text in Field "123z|78" ( | is the cursor )

                   is the cursor between 4 and 5, -> 4 characters will be overriten ( SelStart=4 )
                   is the cursor between 2 and 3, -> 2 characters will be overriten ( SelStart=2 )
  BUG ?!


Is there an other way to send a character to the Text-field?


I use Ubuntu 10.10  64bit and  Lazarus 0.9.30, fpc 2.4.2 -> from SourceForge
« Last Edit: March 29, 2011, 07:40:03 pm by watte »

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Charakters for Widgetset
« Reply #1 on: March 29, 2011, 04:59:14 pm »
Set SelStart to the location you want to insert,SelLength to zero. And set SelText to the Char you want to insert.

  Edit1.SelStart:= 3;
  Edit1.SelLength:= 0;
  Edit1.SelText:= 'A';
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Charakters for Widgetset
« Reply #2 on: March 29, 2011, 05:09:42 pm »
If you want to overwrite one or more chars, set SelStart to the laocation to start, SelLength to the number of chars to overwrite and SelText to what you want to replace it with.

  Edit1.Text:= '123456789';
  Edit1.SelStart:= 3;
  Edit1.SelLength:= 1;
  Edit1.SelText:= 'A';
Lazarus Trunk / fpc 2.6.2 / Win32

watte

  • New Member
  • *
  • Posts: 13
Re: Charakters for Widgetset
« Reply #3 on: March 29, 2011, 05:19:35 pm »
thank you!  but....

I don't know where the user set the cursor... I can't set the SelStart somewhere, because the user could set the cursor by mouse.

I want to write a Touch-Screen-Keyboard. There is no real keyboard on the pc. There are input fields, and a virtual keyboard ( a form with keys qwert... [my application] ).
On KeyClick (maybe "z"), I find out where is the cursor. no problem
BUT ... I can't send the key to this field.... I can't append ... text:=text+charakter, because I don't know, if the user had select something, or where is the cursor....
when I set the SelLength:=0 and the user had select something, he will wonder what happens

You can try, it doesn't work. ...use my example above

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Charakters for Widgetset
« Reply #4 on: March 29, 2011, 05:27:50 pm »
I haven't tried it under the conditions you are talking about, but I think if you just set SelLength to zero and SelText to what you want to insert, it will insert.  But if the user has selected something, I think that the "normal" behavior should be to overwrite what was selected.  But I don't know for sure what you are doing so maybe it isn't what you want.
Lazarus Trunk / fpc 2.6.2 / Win32

watte

  • New Member
  • *
  • Posts: 13
Re: Charakters for Widgetset
« Reply #5 on: March 29, 2011, 05:31:23 pm »
Oh sorry,

the error is just on a TComboBox.

TEdit is ok. Sorry, my fault.

watte

  • New Member
  • *
  • Posts: 13
Re: Charakters for Widgetset
« Reply #6 on: March 29, 2011, 06:00:11 pm »
you can use my code...  (new project and then use my unit1.pas)

now  click A,B,C,D,E,F,G,H
 - combobox shows "ABCDEFGH" ok
then set the cursor by mouse between C and D (nothing mark, a single cursor) and click an other key (maybe U)

 - my result in combobox: "ABCUGH" not ok, because nothing was selected. 3 charakters will be overwritten


Quote
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { private declarations }
    Combo: TComboBox;
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Sender is TButton then
  begin
    Combo.SelText:=chr( TButton(Sender).Tag );
    Combo.SelStart:=Length( Combo.Text );
    Combo.SelLength:=0;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  x,y: integer;
  btn: TButton;
  b: Byte;
begin
  Combo:=TComboBox.Create(Self);
  Combo.Left:=4;
  Combo.Top:=4;
  Combo.Width:=200;
  Self.InsertControl( Combo );
  //
  for y:=0 to 2 do
    for x:=0 to 7 do
    begin
      b:=65+(y*8)+x;
      btn:=TButton.Create(Self);
      btn.Tag:=b;
      btn.Caption:=chr(b);
      btn.OnClick:=@Button1Click;
      btn.Width:=24;
      btn.Height:=24;
      btn.Left:=(x*28)+4;
      btn.Top:=(y*28)+50;
      Self.InsertControl(btn);
    end;

end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  ctl: TControl;
  l: integer;
begin
  for l:=0 to ControlCount-1 do
  begin
    ctl:=Controls[l];
    if (ctl<>nil) then ctl.Free;
  end;
end;

end.
« Last Edit: March 29, 2011, 06:02:36 pm by watte »

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Charakters for Widgetset
« Reply #7 on: March 29, 2011, 06:06:30 pm »
 tried this and I think it's doing what you want, but I have no way to test it on touch screen.

var
  Loc: Integer;

procedure TForm1.Button1Click(Sender: TObject);
begin
  cbx1.SelStart:= Loc;
  cbx1.SelLength:= 0;  //Maybe you don't want this line?
  cbx1.SelText:= 'A';
end;

procedure TForm1.cbx1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  Loc:= cbx1.SelStart;
end;

procedure TForm1.cbx1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Loc:= cbx1.SelStart;
end;  
« Last Edit: March 29, 2011, 06:09:30 pm by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Charakters for Widgetset
« Reply #8 on: March 29, 2011, 06:35:22 pm »
I have "improved" it some. I hope it helps :)

var
  Loc: Integer;
  Len: Integer;

procedure TForm1.Button1Click(Sender: TObject);
begin
  cbx1.SelStart:= Loc;
  cbx1.SelLength:= Len;
  cbx1.SelText:= Button1.Caption;
  inc(Loc);
  cbx1.SetFocus;
  cbx1.SelStart:= Loc;
  cbx1.SelLength:= 0;
end;

procedure TForm1.cbx1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  Loc:= cbx1.SelStart;
  Len:= cbx1.SelLength;
end;

procedure TForm1.cbx1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Loc:= cbx1.SelStart;
  Len:= cbx1.SelLength;
end;
« Last Edit: March 29, 2011, 06:38:21 pm by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

watte

  • New Member
  • *
  • Posts: 13
Re: Characters for Widgetset
« Reply #9 on: March 29, 2011, 07:38:16 pm »
thank you very much!

but it still not work...

in TForm1.cbx1MouseUp, the cbx1.SelLenght returns the same value like cbx1.SelStart
when I click on the 4th position, both functions return 4
when I click on the 3rd position, both functions return 3
and so on

maybe that is the reason?!

do it work on your pc ?

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Characters for Widgetset
« Reply #10 on: March 29, 2011, 07:47:01 pm »
It works on my PC.  What you say sounds very strange and I have no answer for it.  I don't see how, but maybe AutoSelect is set to true for the ComboBox and that is causing it?  It shouldn't do that, but maybe?  For simple insert you "should" be able to set SelLength to zero.  What is SelLength if you select something?  Does it give the correct number?
Lazarus Trunk / fpc 2.6.2 / Win32

watte

  • New Member
  • *
  • Posts: 13
Re: Characters for Widgetset
« Reply #11 on: March 29, 2011, 09:44:37 pm »
When I select something, the values are correct.

I debugged a little bit, and I found a strange reaction. I don't know the reason...

in Gtk2WSStdCtrls is a function

Quote
class function TGtk2WSCustomComboBox.GetSelLength(
  const ACustomComboBox: TCustomComboBox): integer;
var
  WidgetInfo: PWidgetInfo;
  Entry: PGtkEntry;
  AStart, AEnd: gint;
begin
  Result := 0;
  WidgetInfo := GetWidgetInfo(Pointer(ACustomComboBox.Handle));

  // if the combo is an editable ...
  Entry := GetComboBoxEntry(WidgetInfo^.CoreWidget);
  if Entry<>nil then begin
    if gtk_editable_get_selection_bounds(PGtkEditable(Entry), @AStart, @AEnd) = False then
      Exit(gtk_editable_get_position(PGtkEditable(Entry)));
    Result := ABS(AStart - AEnd);
  end;
end;

first, result will set to 0 an stay with this value.
if gtk_editable_get_selection_bounds~ terminates with false and so line Exit(gtk_editable_get_position(PGtkEditable(Entry))); will be call.
after this line, suddenly result has value 3  :o   I don't know why....

Why must SelLength terminate with the position (gtk_editable_get_position) ???
« Last Edit: March 30, 2011, 09:34:36 am by watte »