找出哪个gtk小部件有当前选择 [英] find out which gtk widget has the current selection

查看:132
本文介绍了找出哪个gtk小部件有当前选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 GtkEntry 小部件的集合,其中一些是可编辑和可聚焦的,其中一些不是。我想知道他们当前选择了哪些文本(如果有的话),以便实现编辑 - >复制菜单项。除了迭代所有窗口部件之外,是否有任何其他方式来执行此操作,直到 gtk_editable_get_selection_bounds 返回true?



我目前通过调用 gtk_clipboard_get(GDK_SELECTION_PRIMARY)来回避这个问题,但是从文档的说明来看,这不是可移植的(并且还会选取当前显示内选中的任何文本,而不仅仅是从内部我的应用程序)。

解决方案

您是否试过 gtk_window_get_focus()?用户经常与条目交互,因此它可能适用于您。该文档说,它检索窗口内的当前焦点小部件。您可以自己此处。然后,比较检索到的小部件是否是您的条目之一。



一旦获得焦点入口,您可能希望使用 gtk_entry_get_text(),但它会获取条目中的所有文本。如果这不符合您的目的,解决方案可能会使用 gtk_editable_copy_clipboard(),它复制当前选定内容的可编辑内容(当然,将该条目转换为可编辑)并将其放在剪贴板上。然后,如果适用,请粘贴使用 gtk_editable_paste_clipboard()复制的内容

I have a collection of GtkEntry widgets, some of which are editable and focusable, and some of which are not. I would like to find out which, if any, of them currently has text selected, in order to implement an Edit->Copy menu item. Is there any way to do this other than iterating over all the widgets until gtk_editable_get_selection_bounds returns true?

I am currently sidestepping the issue by calling gtk_clipboard_get(GDK_SELECTION_PRIMARY) but from what the docs say, that's not portable (and will also pick up text selected anywhere within the current display, not just from within my application).

解决方案

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 ().

这篇关于找出哪个gtk小部件有当前选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆