1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="highlight.min.css">
<script src="highlight.min.js"></script><script>
hljs.configure({languages: ['cpp']});
hljs.highlightAll();
</script><title>Example</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Programming with gtkmm 4">
<link rel="up" href="chapter-draganddrop.html" title="Chapter 19. Drag and Drop">
<link rel="prev" href="sec-dnd-signals.html" title="Signals">
<link rel="next" href="chapter-clipboard.html" title="Chapter 20. The Clipboard">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Example</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-dnd-signals.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 19. Drag and Drop</th>
<td width="20%" align="right"> <a accesskey="n" href="chapter-clipboard.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-dnd-example"></a>Example</h2></div></div></div>
<p>Here is a very simple example, demonstrating a drag and drop <code class="literal">Copy</code> operation:</p>
<div class="figure">
<a name="figure-drag-and-drop"></a><p class="title"><b>Figure 19.1. Drag and Drop</b></p>
<div class="figure-contents">
<div class="screenshot">
<div class="mediaobject"><img src="figures/drag_and_drop.png" alt="Drag and Drop"></div>
</div>
</div>
</div>
<br class="figure-break">
<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/drag_and_drop" target="_top">Source Code</a></p>
<p>File: <code class="filename">dndwindow.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLE_DNDWINDOW_H
#define GTKMM_EXAMPLE_DNDWINDOW_H
#include <gdkmm/drop.h>
#include <gtkmm/box.h>
#include <gtkmm/label.h>
#include <gtkmm/window.h>
#include <gtkmm/button.h>
class DnDWindow : public Gtk::Window
{
public:
DnDWindow();
virtual ~DnDWindow();
protected:
//Signal handlers:
Glib::RefPtr<Gdk::ContentProvider> on_label_drag_prepare_data(double x, double y);
bool on_button_drop_drop_data(const Glib::ValueBase& value, double x, double y);
//Member widgets:
Gtk::Box m_HBox;
Gtk::Label m_Label_Drag;
Gtk::Button m_Button_Drop;
};
#endif // GTKMM_EXAMPLE_DNDWINDOW_H
</code></pre>
<p>File: <code class="filename">dndwindow.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "dndwindow.h"
#include <gdkmm/contentprovider.h>
#include <gtkmm/dragsource.h>
#include <gtkmm/droptarget.h>
#include <iostream>
DnDWindow::DnDWindow()
: m_Label_Drag("Drag Here\n"),
m_Button_Drop("Drop here\n")
{
set_title("DnD example");
set_child(m_HBox);
//Drag site:
//Make m_Label_Drag a DnD drag source:
auto source = Gtk::DragSource::create();
source->set_actions(Gdk::DragAction::COPY);
source->signal_prepare().connect(
sigc::mem_fun(*this, &DnDWindow::on_label_drag_prepare_data), false);
m_Label_Drag.add_controller(source);
m_HBox.append(m_Label_Drag);
m_Label_Drag.set_expand(true);
//Drop site:
//Make m_Button_Drop a DnD drop destination:
const GType ustring_type = Glib::Value<Glib::ustring>::value_type();
auto target = Gtk::DropTarget::create(ustring_type, Gdk::DragAction::COPY);
target->signal_drop().connect(
sigc::mem_fun(*this, &DnDWindow::on_button_drop_drop_data), false);
m_Button_Drop.add_controller(target);
m_HBox.append(m_Button_Drop);
m_Button_Drop.set_expand(true);
}
DnDWindow::~DnDWindow()
{
}
// In this simple example where just a small amount of data is copied,
// it would be reasonable to store the ContentProvider in the DragSource.
// Then this signal handler would be unnecessary.
Glib::RefPtr<Gdk::ContentProvider> DnDWindow::on_label_drag_prepare_data(double, double)
{
Glib::Value<Glib::ustring> ustring_value;
ustring_value.init(ustring_value.value_type());
ustring_value.set("I'm Data!");
return Gdk::ContentProvider::create(ustring_value);
}
bool DnDWindow::on_button_drop_drop_data(const Glib::ValueBase& value, double, double)
{
if (G_VALUE_HOLDS(value.gobj(), Glib::Value<Glib::ustring>::value_type()))
{
// We got the value type that we expected.
Glib::Value<Glib::ustring> ustring_value;
ustring_value.init(value.gobj());
const Glib::ustring dropped_string = ustring_value.get();
std::cout << "Received \"" << dropped_string << "\" in button " << std::endl;
return true;
}
else
{
std::cout << "Received unexpected data type \""
<< G_VALUE_TYPE_NAME(value.gobj()) << "\" in button " << std::endl;
return false;
}
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "dndwindow.h"
#include <gtkmm/application.h>
int main(int argc, char* argv[])
{
auto app = Gtk::Application::create("org.gtkmm.example");
//Shows the window and returns when it is closed.
return app->make_window_and_run<DnDWindow>(argc, argv);
}
</code></pre>
<p>
There is a more complex example in examples/others/dnd.
</p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-dnd-signals.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-draganddrop.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="chapter-clipboard.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Signals </td>
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td>
<td width="40%" align="right" valign="top"> Chapter 20. The Clipboard</td>
</tr>
</table>
</div>
</body>
</html>
|