Paste
| Chapter 18. The Clipboard |

## 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_string();
      //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_targets()` 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 Glib::StringArrayHandle& targets_array)
    {
      // Get the list of available clipboard targets:
      std::list<std::string> targets = targets_array;

      const bool bPasteIsPossible =
        std::find(targets.begin(), targets.end(),
          example_target_custom) != targets.end();

      // Enable/Disable the Paste button appropriately:
      m_Button_Paste.set_sensitive(bPasteIsPossible);
    }

Copy |  | Examples