Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

c++ - 获取GTK窗口,剪贴板信号源自该GTK窗口(Gtkmm)(Get the GTK Window from which a Clipboard signal_owner_change originated (Gtkmm))

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*作为一个参数,它包含的字段ownerwindow类型的GdkWindow* 。)

  • The field owner changes for each different application / window I copy from, but never corresponds to the window pointer of my application.

    (字段owner针对我从中复制的每个不同的应用程序/窗口进行更改,但从未与我的应用程序的窗口指针相对应。)

  • The field window is always the same, but also does not match my application window.

    (字段window始终相同,但也不匹配我的应用程序窗口。)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...