简体   繁体   English

如何以编程方式在gtk文本缓冲区中选择文本(主要选择)

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

I am writing a text editor using gtk+-2.0 & gtksourceview-2.0. 我正在使用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. 我在寻找一种方法来编程选择文本块并将其添加到OS(linux)主选择剪贴板时遇到麻烦。 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. 就像我用鼠标突出显示一个文本块或按住Shift键并使用箭头键选择文本一样。

I have found in devhelp under "gtk_text_buffer_get_selection_bound ()" the statement: 我在devhelp中的“ gtk_text_buffer_get_selection_bound()”下找到了以下语句:

The currently-selected text in buffer is the region between the "selection_bound" and "insert" marks. 缓冲区中当前选择的文本是“ selection_bound”和“ insert”标记之间的区域。

Edit: gtk_text_buffer_select_range (), sets the location of these two marks. 编辑: gtk_text_buffer_select_range(),设置这两个标记的位置。

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!!! 谢谢你的想法!!!

Perhaps you should try gtk_editable_copy_clipboard () . 也许您应该尝试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." 然后使用gtk_editable_paste_clipboard()粘贴,它“将剪贴板的内容粘贴到可编辑区域中光标的当前位置”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM