Re: Best practice query: Entry field "user's done editing" handling?




On 16/12/13 21:27, Chris Angelico wrote:

I have a form with a whole pile of entry fields (GTK2.Entry), and I
need to do some processing (and save the edit) whenever the user's
edited a field and is now done editing. Is there a standard way to
recognize this? I'm thinking of something like the VX-REXX "Verify"
event, which fires on a changed entry field when focus moves to
another control on the same window, but not if focus moves to another
window altogether.

Currently, I'm hooking focus-out-event signals and checking the
current text to see if it differs from a saved copy of the text. I'd
rather not save every time a 'changed' signal comes through, as that
would be hopelessly inefficient most of the time.

In your focus-out handler you can call gtk_widget_is_focus on the entry, if it returns true then the focus has passed to another window and you don't need to do anything. If it returns false then it's possible that the user has popped up the context menu rather than moved to a different control in the same window. Keeping track of this is a bit more complicated. You need to connect to the "popup-menu" signal with the entry as the user_data and do something like

g_object_set_data (G_OBJECT(entry), "in-popup", G_INT_TO_POINTER(TRUE));
g_signal_connect (menu, "unmap", on_menu_unmap, entry);

in on_menu_unmap
g_object_set_data (G_OBJECT(entry), "in-popup", G_INT_TO_POINTER(FALSE));
g_object_set_data (G_OBJECT(entry), "popdown-id", G_UINT_TO_POINTER ("g_timeout_add (500, popdown_cb, entry)));

in popdown_cb
if (!gtk_widget_is_focus(entry))
  do_processing

In the focus out handler you can check in-popup for the entry, you should also remove the timeout function to avoid doing the processing twice. The timeout ensures that if the user transfers the focus from the context menu directly to another control you still save the result. Gnome shell seems to prevent the user from doing that but other window managers may not. This is the scheme that GtkCellRendererText uses to keep track of the context menu.

Hope this helps

Phillip



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]