My application needs to listen to clipboard changes to record them.
(我的应用程序需要收听剪贴板的更改以进行记录。)
I can currently achieve that with a signal_owner_change
callback which then requests the contents of the clipboard. (目前,我可以使用signal_owner_change
回调实现该signal_owner_change
,然后该回调请求剪贴板的内容。)
However, I need to figure out which window the even originated from, so that I can ignore when a user copies text from the application itself (using Ctrl + C , not programmatically). (但是,我需要找出偶数来自哪个窗口,以便当用户从应用程序本身复制文本时可以忽略(使用Ctrl + C而不是通过编程)。)
The signal_owner_change
callback takes a GdkEventOwnerChange*
as a parameter, which contains the fields owner
and window
of type GdkWindow*
.
(所述signal_owner_change
回调采取GdkEventOwnerChange*
作为一个参数,它包含的字段owner
和window
类型的GdkWindow*
。)
Unfortunately, the GTK+ documentation is very unclear about what the "owner" and the "window" of a GdkEventOwnerChange is, or how to exploit the data.
(不幸的是,GTK +文档对于GdkEventOwnerChange的“所有者”和“窗口”是什么以及如何利用数据尚不清楚。)
My application window is a class inheriting from Gtk::ApplicationWindow
.
(我的应用程序窗口是一个继承自Gtk::ApplicationWindow
。)
I get the GdkWindow*
as such: (我得到这样的GdkWindow*
:)
GdkWindow* win = gtk_widget_get_window(GTK_WIDGET(gobj()));
Here is an extract of the clipboard owner change even handler: (in another class, which gets the window pointer above as parentWindow
)
(这是剪贴板所有者更改甚至处理程序的摘录:(在另一个类中,它将上面的窗口指针作为parentWindow
))
MyNamespace::MyClass::MyClass(GdkWindow* parentWindow)
: parentWindow(parentWindow) {
// setup UI, etc...
clipboard = Gtk::Clipboard::get();
clipboard->signal_owner_change().connect(
sigc::mem_fun(*this, &MyClass::on_clipboard_owner_change));
}
void MyNamespace::MyClass::on_clipboard_owner_change(GdkEventOwnerChange* event) {
std::cout << "Owner: " << event->owner
<< ", window: " << event->window
<< ", parent: " << parentWindow
<< std::endl;
// This does not work
if (event->owner == parentWindow)
return;
switch (get_clipboard_content_type()) {
case ClipboardDataType::Text: {
clipboard->request_text(
sigc::mem_fun(*this, &MyClass::on_clipboard_text_received));
break;
}
case ClipboardDataType::Image: {
std::cout << "Image is available, unimplemented handler" << std::endl;
break;
}
case ClipboardDataType::URIs: {
std::cout << "URIs is available, unimplemented handler" << std::endl;
break;
}
default: {
std::cout << "Unknown or unsupported clipboard data type" << std::endl;
}
}
}
Here is the console output:
(这是控制台输出:)
# Copy from another application
Owner: 0x5571ec44dc80, window: 0x5571ec08f330, parent: 0x5571ec08f4c0
# Copy from the application itself
Owner: 0x5571ec44de10, window: 0x5571ec08f330, parent: 0x5571ec08f4c0
I have tried comparing with the Gtk::Application
toplevel window, or with the window of the Gtk::Label
( label->get_window()->gobj()
) that gets copied but none of the pointers match.
(我尝试过与Gtk::Application
顶层窗口或与Gtk::Label
的窗口( label->get_window()->gobj()
)进行比较,该窗口被复制但没有指针匹配。)
ask by Guillaume Schlipak translate from so