Lazarus

Programming => Widgetset => Cocoa => Topic started by: trev on June 12, 2019, 03:55:57 am

Title: [SOLVED] Tedit issues
Post by: trev on June 12, 2019, 03:55:57 am
I'm in the process of converting an application I wrote in FMX in Delphi 10.2.1 to Lazarus. This is just my second day using Lazarus (although I used it a decade ago to convert a Delphi VCL and a Kylix program to tun on OS X).

I've noticed some odd behaviour of the Tedit control. The Tedit control is on a visible, active TTabSheet. Tested so far only on macOS 10.14.5.

1) Using Tedit.SetFocus in code does not seem to work at all (clicking on the control does set focus).

So, I thought I'd work around the issue by:

2) In the OnMouseEnter handler I set the focus to the TEdit control. This only works if the mouse pointer goes over the far right of the control. Otherwise it looks like the event never triggers.

Any ideas? A bug maybe? Or an ID10T error?
Title: Re: Tedit issues
Post by: skalogryz on June 12, 2019, 05:36:36 am
for issue #1 it's either a bug or a missing functionality.
SetFocus is rarely used, so it's possible nobody noticed the misbehavior before

for issue #2 what Lazarus version are you using?
Title: Re: Tedit issues
Post by: trev on June 13, 2019, 06:15:16 am
I'm using Lazarus 2.02 (32 bit Carbon) to produce macOS 64 bit binaries.
Title: Re: Tedit issues
Post by: wittbo on June 13, 2019, 10:16:39 am
Not a direct answer, but more some kind of hint. Nearly 20 years ago there was a set of very nice and effective edit components for Delphi (TEditN, TTMEditN, TDBEditN) as a replacement of the Standard Delphi components. I loved them because of their additional features and when being on Delphi, I used them every time. Jose Maria Gias; his homepage isn't longer online, use this one: http://delphi.icm.edu.pl/newl/midxd30f.htm
Since about 3 years I'm on Lazarus (MacOS/Cocoa and Win10). Actually I'm porting his package to Lazarus, it's nearly ready to use. I will send a message upon finalizing.
Title: Re: Tedit issues
Post by: skalogryz on June 13, 2019, 03:17:02 pm
I'm using Lazarus 2.02 (32 bit Carbon) to produce macOS 64 bit binaries.
it should be working for 2.0.2
could you please provide a sample project?
Title: Re: Tedit issues
Post by: trev on June 14, 2019, 12:28:29 pm
Sample project attached.

I include three attempts to edit1.SetFocus:

 - Procedure TForm1.TabSheet2Show(Sender: TObject);
 - Procedure TForm1.PageControl1Change(Sender: TObject);
 - Procedure TForm1.Button1Click(Sender: TObject);

All fail. Thanks for looking!
Title: Re: Tedit issues
Post by: lucamar on June 14, 2019, 03:14:18 pm
I had a similar problem in Linux and now I always use SetFocusedControl(), Also the TabSheet must be visible so I set the focusing in a handler for the TPageControl.OnChange event.

It usually works--not always, Gtk being quite temperamental, but most of the time it does.

A small example:

Code: Pascal  [Select][+][-]
  1. procedure TMainForm.EditPagesChange(Sender: TObject);
  2. { EditPages is a TPageControl }
  3. begin
  4.   UpdateStatus;
  5.   { CurrentEditor returns the TMemo linside the current TabSheet }
  6.   if Assigned(CurrentEditor) then
  7.     SetFocusedControl(CurrentEditor);
  8. end;
  9.  
  10. function TMainForm.MemoInTab(ATab: TTabSheet): TMemo;
  11. { This returns the memo inside a tabsheet,
  12.   and is called from GetCurrentEditor }
  13. var
  14.   i: Integer;
  15. begin
  16.   Result := Nil;
  17.   if Assigned(ATab) then begin
  18.     for i := 0 to ATab.ControlCount-1 do
  19.       if ATab.Controls[i].InheritsFrom(TMemo) then begin
  20.         Result := ATab.Controls[i] as TMemo;
  21.         Break;
  22.       end;
  23.   end;
  24. end;
Title: Re: Tedit issues
Post by: trev on June 14, 2019, 04:03:38 pm
I had a similar problem in Linux and now I always use SetFocusedControl(), Also the TabSheet must be visible so I set the focusing in a handler for the TPageControl.OnChange event.

Thanks, but even a Button click event on the tab page fails to set the focus with edit.SetFocus and/or SetFocusedControl(edit).
Title: Re: Tedit issues
Post by: skalogryz on June 14, 2019, 06:09:53 pm
Sample project attached.
Thank you. I can see the problem (the issue is with tab change events actually within Cocoa-Widgetset. Not much you can do about it.).

If you comment out code of
 - Procedure TForm1.TabSheet2Show(Sender: TObject);
 - Procedure TForm1.PageControl1Change(Sender: TObject);

You'll see that
 - Procedure TForm1.Button1Click(Sender: TObject);
would work.

Which is only a little relief

But I'd think this is fixable. Stay tuned.
Title: Re: Tedit issues
Post by: skalogryz on June 14, 2019, 09:19:13 pm
if you update your trunk to r61393.
you should have it working now.
Title: Re: Tedit issues
Post by: trev on June 15, 2019, 03:26:46 am
Thanks! That was quick. I've doubled my Patreon support :)
Title: Re: [Solved] Tedit issues
Post by: trev on June 15, 2019, 08:30:21 am
Small unwanted side-effect  :-[

Code: [Select]
procedure TForm1.deviceEditKeyUp(Sender: TObject);
var
  i: integer;
  tempStr: String;
begin
  try
    for i := 0 to picListBox.Items.Count - 1 do
      if(AnsiStartsText(deviceEdit.Text, picListBox.Items[i]) = True) then
        begin
          picListBox.ItemIndex := i;
          picListBox.TopIndex := picListBox.ItemIndex;
          break;
        end;
   finally
    if(i = picListBox.Items.Count - 1) then
      begin
        MakeBeep;                             // Alert user
        tempStr := deviceEdit.Text;
        delete(tempStr, Length(tempStr), 1);  // remove unmatching char
        deviceEdit.Text := tempStr;
      end;
   end;
end;

The procedure above checks what the user has typed in the edit box against the list box entries and either breaks out of the for loop when there is a partial match at the start of an entry or falls through to beep and remove the character that does not match any entries.

However, when deviceEdit.SetFocus has been executed in code (as opposed to clicking the edit box), the procedure fails because AnsiStartsText never matches for some reason.
Title: Re: Tedit issues
Post by: skalogryz on June 15, 2019, 08:17:55 pm
what is deviceEditKeyUp?
it looks like an event handler. But keyup event handler comes with additional parameters (key code and shift state)
If it's an event handler, what event does it process?
as usual - sample projects help.
Title: Re: Tedit issues
Post by: trev on June 16, 2019, 01:40:06 am
what is deviceEditKeyUp?
it looks like an event handler. But keyup event handler comes with additional parameters (key code and shift state)

The compiler kept complaining (hinting) about the unused key and shift args, removing them shut it up and didn't seem to cause any issues.

Quote
If it's an event handler, what event does it process?
as usual - sample projects help.

It processes key strokes in the deviceEdit and highlights the corresponding matching entry in the ListBox.

I'll come up with a sample project to show the behaviour.
Title: Re: Tedit issues
Post by: skalogryz on June 16, 2019, 01:51:48 am
The compiler kept complaining (hinting) about the unused key and shift args, removing them shut it up and didn't seem to cause any issues.
I've a bad feeling about this!

please either restore the parameters, or reassign the event. Just to be sure they're there.
In order to satisfy the compiler, you can right click on the message (in Messages) window and select any option you'd like to ignore the hint.
(I typically use "Hide with project option")
Title: Re: Tedit issues
Post by: trev on June 16, 2019, 08:52:26 am
I restored all the missing args - no change.

The issue appears to be that when accessing the deviceEdit.Text in the procedure it has no content if it has been focused by deviceEdit.SetFocus! If I click another control, it loses focus as expected, and then once focused by a click deviceEdit.text has content again.

No luck creating a sample project yet :(
Title: Re: Tedit issues
Post by: trev on June 16, 2019, 09:53:14 am
Sample project :)

As setup, Edit1.SetFocus is called from Form1.Activate handler. Typing in the edit control does not scroll the selection in ListBox1. Click another control, return to Edit1 and then it works.
Title: Re: Tedit issues
Post by: skalogryz on June 16, 2019, 01:54:17 pm
I can see it works just fine for all options in the given List.
Except for the last one.
(i.e. if one types "18" the last "8" would be removed, even though the option is in the list.)
In general it affects the selection of LAST item. (no matter what the text of it is)
However, it's caused by the code as well.


Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState
  2.   );
  3. var
  4.   i: integer;
  5.   tempStr: String;
  6.   found : Boolean;
  7. begin
  8.   try
  9.     found := false;
  10.     for i := 0 to ListBox1.Items.Count - 1 do
  11.       begin
  12.       //ShowMessage(picListBox.Items[i]);
  13.       if(AnsiStartsText(Edit1.Text, ListBox1.Items[i]) = True) then
  14.         begin
  15.           ListBox1.ItemIndex := i;
  16.           ListBox1.TopIndex := ListBox1.ItemIndex;
  17.           found := true;
  18.           break;
  19.         end;
  20.       end;
  21.    finally
  22.     if (not found) then
  23.       begin
  24.         //MakeBeep;                           // Alert user
  25.         tempStr := Edit1.Text;
  26.         delete(tempStr, Length(tempStr), 1);  // remove unmatching char
  27.         Edit1.Text := tempStr;
  28.       end;
  29.    end;
  30. end;  
  31.  

https://www.dropbox.com/s/zico28gvlfhl47r/works.mov?dl=0
Title: Re: Tedit issues
Post by: trev on June 16, 2019, 02:18:02 pm
I can see it works just fine for all options in the given List.
Except for the last one.

Weird. None of the options work for me. See pic eg.

(It was a sample to show the issue; the last item lost its 8 but that seemed not so important)
Title: Re: Tedit issues
Post by: trev on June 16, 2019, 02:21:45 pm
If I remove the Edit.SetFocus in the code OR if I click on another control then we have this pic.
Title: Re: Tedit issues
Post by: skalogryz on June 16, 2019, 02:26:28 pm
try this code
Code: [Select]
procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState
  );
var
  i: integer;
  tempStr: String;
  found: Boolean;
begin
  Caption := Edit1.Text;
  try
    found := false;
    for i := 0 to ListBox1.Items.Count - 1 do
      begin
      //ShowMessage(picListBox.Items[i]);
      if(AnsiStartsText(Edit1.Text, ListBox1.Items[i]) = True) then
        begin
          ListBox1.ItemIndex := i;
          ListBox1.TopIndex := ListBox1.ItemIndex;
          found := true;
          Caption := Caption +' '+IntToStr(i);
          break;
        end;
      end;
   finally
    if (not found) then
      begin
        //MakeBeep;                           // Alert user
        tempStr := Edit1.Text;
        delete(tempStr, Length(tempStr), 1);  // remove unmatching char
        Edit1.Text := tempStr;
      end;
   end;
end;
Title: Re: Tedit issues
Post by: trev on June 16, 2019, 02:39:13 pm
Same problem - no matches unless I comment out the Edit1.SetFocus OR click on another control and return to typing in the Edit.
Title: Re: Tedit issues
Post by: skalogryz on June 16, 2019, 02:55:34 pm
It's not about matches, it's about the form caption.
What is in the form caption when you try to type for the first time.
Title: Re: Tedit issues
Post by: trev on June 16, 2019, 02:59:28 pm
Please see attached 7 second movie of the issue (using the new code). Rename file from .mpg to .mov (forum requirement)
Title: Re: Tedit issues
Post by: trev on June 16, 2019, 03:12:22 pm
What is in the form caption when you try to type for the first time.

Nothing as per movie.
Title: Re: Tedit issues
Post by: skalogryz on June 16, 2019, 03:18:17 pm
what's you macOS version?

hmm is it the original sample project? or is this dialog - meaning it's a different form that opens?
Title: Re: Tedit issues
Post by: trev on June 16, 2019, 03:19:52 pm
Mojave 10.14.5

It's the modified sample project as per your code (see caption after I click the second listbox)
Title: Re: Tedit issues
Post by: skalogryz on June 16, 2019, 03:23:33 pm
try the latest trunk - r61401

i'm running Mojave 10.14.4 actually. But I don't think this is the problem.

Do you've any idea on why the form caption is blank in your video? It should be updated only on the first KeyUp event. It should be "Form1" from the start.
Title: Re: Tedit issues
Post by: trev on June 17, 2019, 02:10:51 am
> try the latest trunk - r61401

Yes! It works! Thankyou.

> Do you've any idea on why the form caption is blank in your video?

No - the Object Inspector shows 'Form1' in the caption as does the design time form.

In my real application, it does show the form caption as it should.

I just changed the caption in the sample project from 'Form1' to 'Form 1' and it now shows up.
TinyPortal © 2005-2018