After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 735838 - Provide a way to focus an entry without selecting its contents
Provide a way to focus an entry without selecting its contents
Status: RESOLVED FIXED
Product: gtk+
Classification: Platform
Component: Widget: GtkEntry
unspecified
Other Linux
: Normal enhancement
: ---
Assigned To: gtk-bugs
gtk-bugs
Depends on:
Blocks:
 
 
Reported: 2014-09-01 18:55 UTC by Elad Alfassa
Modified: 2014-12-06 04:35 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Added gtk_entry_grab_focus_without_selection(). (1.39 KB, patch)
2014-10-15 15:41 UTC, Ankita Patil
none Details | Review
Added gtk_entry_grab_focus_without_selection(). (1.39 KB, patch)
2014-10-15 15:48 UTC, Ankita Patil
none Details | Review
Added gtk_entry_grab_focus_without_selection(). (1.39 KB, patch)
2014-10-15 15:51 UTC, Ankita Patil
none Details | Review
Added gtk_entry_grab_focus_without_selection(). (2.26 KB, patch)
2014-10-16 07:25 UTC, Ankita Patil
reviewed Details | Review
Added Since: 3.16 in doc string (2.29 KB, patch)
2014-11-06 12:18 UTC, Ankita Patil
committed Details | Review

Description Elad Alfassa 2014-09-01 18:55:46 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).
Comment 1 Benjamin Otte (Company) 2014-10-15 13:48:53 UTC
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".
Comment 2 Ankita Patil 2014-10-15 15:41:32 UTC
Created attachment 288607 [details] [review]
Added gtk_entry_grab_focus_without_selection().
Comment 3 Ankita Patil 2014-10-15 15:48:06 UTC
Created attachment 288608 [details] [review]
Added gtk_entry_grab_focus_without_selection().
Comment 4 Ankita Patil 2014-10-15 15:51:53 UTC
Created attachment 288609 [details] [review]
Added gtk_entry_grab_focus_without_selection().

Corrected Spacing.
Comment 5 Benjamin Otte (Company) 2014-10-15 17:17:17 UTC
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?
Comment 6 Ankita Patil 2014-10-16 07:25:06 UTC
Created attachment 288648 [details] [review]
Added gtk_entry_grab_focus_without_selection().

Changed the method name and also added documentation text.
Comment 7 Matthias Clasen 2014-10-19 20:49:50 UTC
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.
Comment 8 Ankita Patil 2014-11-06 12:18:36 UTC
Created attachment 290097 [details] [review]
Added Since: 3.16 in doc string
Comment 9 Matthias Clasen 2014-12-06 04:15:36 UTC
Review of attachment 290097 [details] [review]:

looks ok now