5

我有一组GtkEntry小部件,其中一些是可编辑和可聚焦的,而有些则不是。我想找出其中哪些(如果有的话)当前选择了文本,以实现 Edit->Copy 菜单项。除了遍历所有小部件直到gtk_editable_get_selection_bounds返回 true 之外,还有什么方法可以做到这一点?

我目前正在通过调用来回避这个问题,gtk_clipboard_get(GDK_SELECTION_PRIMARY)但根据文档所说,这是不可移植的(并且还会在当前显示的任何位置选择文本,而不仅仅是从我的应用程序中)。

4

1 回答 1

3

Have you tried gtk_window_get_focus ()? Users are frequently interacting with entries, so it may work for you. The documentation says that it "retrieves the current focused widget within the window." You can look it by yourself here. Then, compare if the widget retrieved is one of your entries.

Once you get the focused entry, perhaps you would like to get its text using gtk_entry_get_text () , though, it will get all the text in the entry. If this does not fit your purposes, the solution might be using gtk_editable_copy_clipboard () which copies the contents of the currently selected content in the editable (of course, cast the entry to editable) and puts it on the clipboard. Then if it applies, paste what was copied using gtk_editable_paste_clipboard ().

于 2012-05-09T05:03:10.023 回答