GNOME Bugzilla – Bug 735838
Provide a way to focus an entry without selecting its contents
Last modified: 2014-12-06 04:35:28 UTC
Right now, using gtk_widget_grab_focus on a GtkEntry also selects its contents. While for most usecase this is a valid thing to do, it becomes problematic when you implement a "search as you type" kind of thing in your app. You can't simply grab the focus on the entry, because then the text will be selected and the next keypress will replace the contents of the entry with your new keystrokes. Right now both Polari and gnome-software use this hack to grab the focus to a GtkEntry without selecting the text: g_object_set (entry, "editable", FALSE, NULL); gtk_widget_grab_focus (entry); g_object_set (entry, "editable", TRUE, NULL); It would make more sense if gtk itself could provide some sort of helper function to do it, something like gtk_entry_grab_focus(entry, select_contents) (first parameter is the entry, second parameter is a boolean indicating whether or not to select the contents of the entry).
A few details: I don't think we want the boolean argument. Your gtk_entry_grab_focus(entry, TRUE); is the same as gtk_widget_grab_focus(entry); and I don't think it ever makes sense to have the boolean not be constant when calling the function. I also would like the API to sound like the special case that it is. This API sounds like it's a 50/50 thing if the contents should be selected. I want this function to sound like "Of course when entries grab focus the contents get selected. Well unless you are very sure you don't want that, then you use this special function".
Created attachment 288607 [details] [review] Added gtk_entry_grab_focus_without_selection().
Created attachment 288608 [details] [review] Added gtk_entry_grab_focus_without_selection().
Created attachment 288609 [details] [review] Added gtk_entry_grab_focus_without_selection(). Corrected Spacing.
A few things: (1) A grammar issue: It should be "without_selecting" not "without_selection". Selection is a thing, selecting is an action, and what we want to avoid is the action of selecting. (2) The new function needs to be documented. And it needs to be added to docs/reference/gtk/gtk3-sections.txt so the docs build picks the documentation up. Also, in the documentation, please make sure to include a link to gtk_widget_grab_focus() and the fact that everybody should want to call that function. (3) Do we want to set the cursor in the new function? Or do we just keep it where it was?
Created attachment 288648 [details] [review] Added gtk_entry_grab_focus_without_selection(). Changed the method name and also added documentation text.
Review of attachment 288648 [details] [review]: ::: gtk/gtkentry.c @@ +5020,3 @@ + * which the user usually doesn't want to replace all text in, + * such as search-as-you-type entries. + */ Almost there. Just add a "Since: 3.16" in the doc comment here, and then this looks good to commit.
Created attachment 290097 [details] [review] Added Since: 3.16 in doc string
Review of attachment 290097 [details] [review]: looks ok now