I have a GtkEntry
with inline autocompletion enabled (gtk_entry_completion_set_inline_completion() called with true
). Everything works well, but I would like that by pressing the TAB
key the following happens:
- if an inline completion is being shown: use it for completing the
GtkEntry
; do not propagate thekey-pressed
event - if no inline completion is being shown: propagate the event (move to the next field in this case, since that is the normal meaning of
TAB
)
The same behavior is used when one presses Ctrl
+ l
in Nautilus and a GtkEntry
for the path is shown.
What strategy do I have to follow?
EDIT
I have found the solution. I will post it here in case anyone wants to do the same. All I had to do was to check if a text selection was present and then decide to propagate the TAB
key-press event or simply confirm the completion.
static gboolean gtk_entry_forward_controller_key_pressed (
GtkEventControllerKey * const controller,
guint const keyval,
guint const keycode,
GdkModifierType const state,
gpointer const v_entry
) {
int start_pos, end_pos;
if (
keyval == GDK_KEY_Tab &&
gtk_editable_get_selection_bounds(
GTK_EDITABLE(v_entry),
&start_pos,
&end_pos
)
) {
gtk_editable_set_position(
GTK_EDITABLE(v_entry),
end_pos
);
return true;
}
return false;
}
static void activate (
GtkApplication * const app,
gpointer const user_data
) {
/* ... */
GtkWidget * const entry = gtk_entry_new();
/* ... */
GtkEventController * controller = gtk_event_controller_key_new();
gtk_widget_add_controller(entry, controller);
g_signal_connect(
G_OBJECT(controller),
"key-pressed",
G_CALLBACK(gtk_entry_forward_controller_key_pressed),
entry
);
/* ... */
}