Paste
Prev | Chapter 16. The Clipboard | Next

## Paste

When the user asks to paste data from the clipboard, you should request a specific format and provide a callback method which will be called with the actual data. For instance:

    refClipboard->request_contents("example_custom_target",  sigc::mem_fun(*this, &ExampleWindow::on_clipboard_received) );

Here is an example callback method:

    void ExampleWindow::on_clipboard_received(const Gtk::SelectionData& selection_data)
    {
      Glib::ustring clipboard_data = selection_data.get_data_as_text();
      //Do something with the pasted data.
    }

### Discovering the available targets

To find out what targets are currently available on the clipboard for pasting, call the `request_methods()` method, specifying a method to be called with the information. For instance:

    refClipboard->request_targets( sigc::mem_fun(*this, &ExampleWindow::on_clipboard_received_targets) );

In your callback, compare the list of available targets with those that your application supports for pasting. You could enable or disable a Paste menu item, depending on whether pasting is currently possible. For instance:

    void ExampleWindow::on_clipboard_received_targets(const Gtk::SelectionData& selection_data)
    {
      bool bPasteIsPossible = false;

      //Get the list of available clipboard targets:
      typedef std::list<Glib::ustring> type_listTargets;
      type_listTargets targets = selection_data.get_targets();

      //and see if one is suitable:
      for(type_listTargets::const_iterator iter = targets.begin(); iter != targets.end(); ++iter)
      {
        if(*iter == "example_custom_target")
          bPasteIsPossible = true;
      }

      //Do something, depending on whether bPasteIsPossible is true.
    }

Prev | Up | Next
Copy | Home | Examples