how to programmatically select text (primary selection) in a gtk text buffer

1.4k views Asked by At

I am writing a text editor using gtk+-2.0 & gtksourceview-2.0. I am having trouble finding a way to programmatically select a block of text and add it to the OS (linux) primary selection clipboard. Just like if I hightlighted a block of text with the mouse or held down the shift key and selected the text with the arrow key.

I have found in devhelp under "gtk_text_buffer_get_selection_bound ()" the statement:

The currently-selected text in buffer is the region between the "selection_bound" and "insert" marks.

Edit: gtk_text_buffer_select_range (), sets the location of these two marks.

The following block of code copies, the region specified using the text iters start & end, to the primary selection clipboard (as desired):

            gtk_text_buffer_select_range (tbuffer, &start, &end); 
            GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
            gtk_text_buffer_copy_clipboard (tbuffer,cb);

Thanks for the ideas!!!

1

There are 1 answers

1
PALEN On BEST ANSWER

Perhaps you should try gtk_editable_copy_clipboard (). The documentation says that "copies the contents of the currently selected content in the editable and puts it on the clipboard". Then paste using gtk_editable_paste_clipboard () which "pastes the content of the clipboard to the current position of the cursor in the editable."