Question

It's my first times with GTK library, I wrote this code:

GtkWidget *dialog, *vbox;
/* create dialog */
dialog = gtk_dialog_new_with_buttons("Go to File...", GTK_WINDOW(parent),
GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);

gtk_widget_set_name(dialog, "GotoFile");
vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));

GtkWidget *label = gtk_label_new("Enter the file you want to open:");

gtk_container_add(GTK_CONTAINER(vbox), label);

/* create entry */
GtkWidget *entry = gtk_entry_new();
gtk_container_add(GTK_CONTAINER(vbox), entry);
gtk_entry_set_text(GTK_ENTRY(entry), "this_is_a_test");
gtk_entry_set_max_length(GTK_ENTRY(entry), MAX_FILENAME_LENGTH);
gtk_entry_set_width_chars(GTK_ENTRY(entry), 30);
gtk_editable_select_region(GTK_EDITABLE(entry), 2, 5);

I expect it creates a dialog with a label and textbox (and this is working) and selects characters "is_" into the entry, but it's not working (all text is selected).

I tried also:

gint a,b;
gtk_editable_get_selection_bounds(GTK_EDITABLE(entry), &a, &b);

printf("SELECTION: %i - %i\n",a,b);

And it prints correctly 2 and 5.

What's wrong?

Was it helpful?

Solution

Solved!

gtk_widget_grab_focus(entry)

must be called on entry before trying to select the text, otherwise it replaces the selection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top