title: Get the GTK Window from which a Clipboard signal_owner_change originated (Gtkmm)
body: <p>My application needs to listen to clipboard changes to record them. I can currently achieve that with a <code>signal_owner_change</code> callback which then requests the contents of the clipboard. However, I need to figure out which window the event originated from, so that I can ignore when a user copies text from the application itself (using <kbd>Ctrl</kbd>+<kbd>C</kbd>, not programmatically).</p>
  
  <p>The <code>signal_owner_change</code> callback takes a <code>GdkEventOwnerChange*</code> as a parameter, which contains the fields <code>owner</code> and <code>window</code> of type <code>GdkWindow*</code>.</p>
  
  <ul>
  <li>The field <code>owner</code> changes for each different application / window I copy from, but never corresponds to the window pointer of my application.</li>
  <li>The field <code>window</code> is always the same, but also does not match my application window.</li>
  </ul>
  
  <p>Unfortunately, the GTK+ documentation is very unclear about what the "owner" and the "window" of a GdkEventOwnerChange is, or how to exploit the data.</p>
  
  <p>My application window is a class inheriting from <code>Gtk::ApplicationWindow</code>. I get the <code>GdkWindow*</code> as such:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>GdkWindow* win = gtk_widget_get_window(GTK_WIDGET(gobj()));
  </code></pre>
  
  <p>Here is an extract of the clipboard owner change even handler: (in another class, which gets the window pointer above as <code>parentWindow</code>)</p>
  
  <pre class="lang-cpp prettyprint-override"><code>MyNamespace::MyClass::MyClass(GdkWindow* parentWindow)
      : parentWindow(parentWindow) {
    // setup UI, etc...
  
    clipboard = Gtk::Clipboard::get();
    clipboard-&gt;signal_owner_change().connect(
        sigc::mem_fun(*this, &amp;MyClass::on_clipboard_owner_change));
  }
  
  void MyNamespace::MyClass::on_clipboard_owner_change(GdkEventOwnerChange* event) {
    std::cout &lt;&lt; "Owner: " &lt;&lt; event-&gt;owner
              &lt;&lt; ", window: " &lt;&lt; event-&gt;window
              &lt;&lt; ", parent: " &lt;&lt; parentWindow
              &lt;&lt; std::endl;
  
    // This does not work
    if (event-&gt;owner == parentWindow)
      return;
  
    switch (get_clipboard_content_type()) {
      case ClipboardDataType::Text: {
        clipboard-&gt;request_text(
            sigc::mem_fun(*this, &amp;MyClass::on_clipboard_text_received));
        break;
      }
      case ClipboardDataType::Image: {
        std::cout &lt;&lt; "Image is available, unimplemented handler" &lt;&lt; std::endl;
        break;
      }
      case ClipboardDataType::URIs: {
        std::cout &lt;&lt; "URIs is available, unimplemented handler" &lt;&lt; std::endl;
        break;
      }
      default: {
        std::cout &lt;&lt; "Unknown or unsupported clipboard data type" &lt;&lt; std::endl;
      }
    }
  }
  </code></pre>
  
  <p>Here is the console output:</p>
  
  <pre><code># Copy from another application
  Owner: 0x5571ec44dc80, window: 0x5571ec08f330, parent: 0x5571ec08f4c0
  
  # Copy from the application itself
  Owner: 0x5571ec44de10, window: 0x5571ec08f330, parent: 0x5571ec08f4c0
  </code></pre>
  
  <p>I have tried comparing with the <code>Gtk::Application</code> toplevel window, or with the window of the <code>Gtk::Label</code> (<code>label-&gt;get_window()-&gt;gobj()</code>) that gets copied but none of the pointers match.</p>