Search code examples
clinuxgtk3

how to clear the entry box when it's active(clicked)


I wanted to clear my entrybox in order to when I want to enter something again it's clear. I implemented a Simple Webbrowser. In the entrybox is always the url but what I was looking for is that if i click (activate) the entrybox, the entrybox should be cleared.

I hopped I could do something like this:

if(gtk_window_get_focus(GTK_WINDOW(w->window)) == w->entry)
          gtk_entry_set_text (GTK_ENTRY(w->entry), "");

But I don't realy know where to do it and how this wokrs that it can detect that the entrybock was clicked.


Solution

  • Complementing Alexander's answer ...

    In case you don't know to associate callbacks with signals, you can use g_signal_connect(). This takes four parameters:

    1. the object we are interested in - we need to use G_OBJECT() to cast this to the relevant type
    2. the signal we want to watch - in this case grab-focus
    3. the callback to invoke when the signal is raised
    4. arbitrary user data (probably not needed in this case).

    In this case you could call it like

    g_signal_connect(G_OBJECT(w->entry), "grab-focus", G_CALLBACK(on_input_focus), NULL);
    

    If you check the signature of the the callback for the grab-focus signal you will see it accepts two arguments and returns nothing. The last argument is the arbitrary user data which was the last parameter in the g_signal_connect() function; we did not set it and can ignore it. The first argument is the widget on which the signal was triggered - which we can cast to a GtkEntry.

    void on_input_focus(GtkWidget *w, gpointer data) {
        gtk_entry_set_text(GTK_ENTRY(w), "");
    }