Copy
| Chapter 19. The Clipboard |

## Copy

When the user asks to copy some data, you should tell the `Clipboard` what targets are available, and provide the callback methods that it can use to get the data. At this point you should store a copy of the data, to be provided when the clipboard calls your callback method in response to a paste.

For instance,

    Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();

    //Targets:
    std::vector<Gtk::TargetEntry> targets;
    targets.push_back( Gtk::TargetEntry("example_custom_target") );
    targets.push_back( Gtk::TargetEntry("UTF8_STRING") );

    refClipboard->set( targets,
        sigc::mem_fun(*this, &ExampleWindow::on_clipboard_get),
        sigc::mem_fun(*this, &ExampleWindow::on_clipboard_clear) );

Your callback will then provide the stored data when the user chooses to paste the data. For instance:

    void ExampleWindow::on_clipboard_get(
        Gtk::SelectionData& selection_data, guint /* info */)
    {
      const std::string target = selection_data.get_target();

      if(target == "example_custom_target")
        selection_data.set("example_custom_target", m_ClipboardStore);
    }

The `ideal` example below can supply more than one clipboard target.

The clear callback allows you to free the memory used by your stored data when the clipboard replaces its data with something else.

Chapter 19. The Clipboard |  | Paste