gtkgtkdgtkentry

gtkentry focus behaviour


Is there any existing mechanism for a GtkEntry to simply position the cursor at the end of the text when focused, rather than selecting its contents to be overwritten by the next key? It seems odd to have to add a signal handler to do something this basic, but I can't find anything in the properties.

Edit: The signal handler doesn't work; whatever I do the default behaviour gets triggered after my handler runs. Here's my gtkd code; note that I am appending some text in the focus-in-event handler, and the appended text gets selected as well:

class NoteView : Entry
{
  this(string text) {
    if (text) {
      setText(text);
    }
    setEditable(true);
    setCanFocus(true);

    addOnFocusIn(delegate bool(GdkEventFocus* f, Widget w) {
        // clear selection
        selectRegion(0, 0);
        // test to see whether the appended text gets selected too
        appendText("hello");
        setPosition(-1);
        // don't let any other handlers run
        return 1;
      }, ConnectFlags.AFTER);
  }
}

The addOnFocusIn method is in the gtkd Gtk.Widget api; it calls g_signal_connect_data internally, which should in theory be honouring the G_CONNECT_AFTER flag I'm passing it, but doesn't seem to be.

Edit2: Solved - the grab-focus handler was doing the text selection, and being handled after focus-in-event


Solution

  • Turns out GtkEntry was selecting the text on the grab-focus signal, not focus-in-event. Working code:

    class NoteView : Entry
    {
      this(string text) {
        if (text) {
          setText(text);
        }
        setEditable(true);
        setCanFocus(true);
        setHasFrame(false);
    
        addOnGrabFocus(delegate void(Widget w) {
            selectRegion(0, 0);
            setPosition(-1);
          }, ConnectFlags.AFTER);   
      }
    }