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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>wxWidgets: Drag and Drop Overview</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="extra_stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="page_container">
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0" style="width: 100%;">
<tbody>
<tr>
<td id="projectlogo">
<a href="http://www.wxwidgets.org/" target="_new">
<img alt="wxWidgets" src="logo.png"/>
</a>
</td>
<td style="padding-left: 0.5em; text-align: right;">
<span id="projectnumber">Version: 3.0.2</span>
</td>
</tr>
</tbody>
</table>
</div>
<!-- Generated by Doxygen 1.8.2 -->
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main Page</span></a></li>
<li class="current"><a href="pages.html"><span>Related Pages</span></a></li>
<li><a href="modules.html"><span>Categories</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
</ul>
</div>
<div id="nav-path" class="navpath">
<ul>
<li class="navelem"><a class="el" href="index.html">Documentation</a></li><li class="navelem"><a class="el" href="page_topics.html">Programming Guides</a></li> </ul>
</div>
</div><!-- top -->
<div class="header">
<div class="headertitle">
<div class="title">Drag and Drop Overview </div> </div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul><li class="level1"><a href="#overview_dnd_dropsource">Drop Source Requirements</a></li>
<li class="level1"><a href="#overview_dnd_droptarget">Drop Target Requirements</a></li>
</ul>
</div>
<div class="textblock"><p>It may be noted that data transfer to and from the clipboard is quite similar to data transfer with drag and drop and the code to implement these two types is almost the same.</p>
<p>In particular, both data transfer mechanisms store data in some kind of <a class="el" href="classwx_data_object.html" title="A wxDataObject represents data that can be copied to or from the clipboard, or dragged and dropped...">wxDataObject</a> and identify its format(s) using the <a class="el" href="classwx_data_format.html" title="A wxDataFormat is an encapsulation of a platform-specific format handle which is used by the system f...">wxDataFormat</a> class.</p>
<p>Note that <code>wxUSE_DRAG_AND_DROP</code> must be defined in <code>setup.h</code> in order to use drag and drop in wxWidgets.</p>
<dl class="section see"><dt>See Also</dt><dd><a class="el" href="overview_dataobject.html">wxDataObject Overview</a>, <a class="el" href="group__group__class__dnd.html">Clipboard and Drag & Drop</a>, <a class="el" href="page_samples.html#page_samples_dnd">Drag & Drop Sample</a></dd></dl>
<h1><a class="anchor" id="overview_dnd_dropsource"></a>
Drop Source Requirements</h1>
<p>To be a <em>"drop source"</em>, i.e. to provide the data which may be dragged by the user elsewhere, you should implement the following steps:</p>
<ul>
<li><b>Preparation:</b> First of all, a data object must be created and initialized with the data you wish to drag. For example: <div class="fragment"><div class="line"><a class="code" href="classwx_text_data_object.html" title="wxTextDataObject is a specialization of wxDataObjectSimple for text data.">wxTextDataObject</a> my_data(<span class="stringliteral">"This text will be dragged."</span>);</div>
</div><!-- fragment --> </li>
<li><b>Drag start</b>: To start the dragging process (typically in response to a mouse click) you must call <a class="el" href="classwx_drop_source.html#a7053f3286614a78d0fd8c282c286778d" title="Starts the drag-and-drop operation which will terminate when the user releases the mouse...">wxDropSource::DoDragDrop</a> like this: <div class="fragment"><div class="line"><a class="code" href="classwx_drop_source.html" title="This class represents a source for a drag and drop operation.">wxDropSource</a> dragSource( <span class="keyword">this</span> );</div>
<div class="line">dragSource.SetData( my_data );</div>
<div class="line"><a class="code" href="interface_2wx_2dnd_8h.html#adbd4b292582eebbc9a6ce3dee31c2afb" title="Result returned from a wxDropSource::DoDragDrop() call.">wxDragResult</a> result = dragSource.DoDragDrop( <span class="keyword">true</span> );</div>
</div><!-- fragment --> </li>
<li><b>Dragging:</b> The call to DoDragDrop() blocks the program until the user releases the mouse button (unless you override the <a class="el" href="classwx_drop_source.html#a2b667de6f631839d4c4383f436726f7f" title="You may give some custom UI feedback during the drag and drop operation by overriding this function...">wxDropSource::GiveFeedback</a> function to do something special). When the mouse moves in a window of a program which understands the same drag-and-drop protocol (any program under Windows or any program supporting the XDnD protocol under X Windows), the corresponding <a class="el" href="classwx_drop_target.html" title="This class represents a target for a drag and drop operation.">wxDropTarget</a> methods are called - see below. </li>
<li><b>Processing the result</b>: DoDragDrop() returns an <em>effect</em> code which is one of the values of <code>wxDragResult</code> enum (explained in <a class="el" href="classwx_drop_target.html" title="This class represents a target for a drag and drop operation.">wxDropTarget</a> page): <div class="fragment"><div class="line"><span class="keywordflow">switch</span> (result)</div>
<div class="line">{</div>
<div class="line"> <span class="keywordflow">case</span> <a class="code" href="interface_2wx_2dnd_8h.html#adbd4b292582eebbc9a6ce3dee31c2afba736c04c59caf8154a096e0d350492fc1" title="The data was successfully copied.">wxDragCopy</a>:</div>
<div class="line"> <span class="comment">// copy the data</span></div>
<div class="line"> <span class="keywordflow">break</span>;</div>
<div class="line"> <span class="keywordflow">case</span> <a class="code" href="interface_2wx_2dnd_8h.html#adbd4b292582eebbc9a6ce3dee31c2afba8b2ba916ca49cc93ef34a5e20684d18e" title="The data was successfully moved (MSW only).">wxDragMove</a>:</div>
<div class="line"> <span class="comment">// move the data</span></div>
<div class="line"> <span class="keywordflow">break</span>;</div>
<div class="line"> <span class="keywordflow">default</span>:</div>
<div class="line"> <span class="comment">// do nothing</span></div>
<div class="line"> <span class="keywordflow">break</span>;</div>
<div class="line">}</div>
</div><!-- fragment --></li>
</ul>
<h1><a class="anchor" id="overview_dnd_droptarget"></a>
Drop Target Requirements</h1>
<p>To be a <em>"drop target"</em>, i.e. to receive the data dropped by the user you should follow the instructions below:</p>
<ul>
<li><b>Initialization:</b> For a window to be a drop target, it needs to have an associated <a class="el" href="classwx_drop_target.html" title="This class represents a target for a drag and drop operation.">wxDropTarget</a> object. Normally, you will call <a class="el" href="classwx_window.html#ae34b4d45433ca8287df0e47d46411e58" title="Associates a drop target with this window.">wxWindow::SetDropTarget</a> during window creation associating your drop target with it. You must derive a class from <a class="el" href="classwx_drop_target.html" title="This class represents a target for a drag and drop operation.">wxDropTarget</a> and override its pure virtual methods. Alternatively, you may derive from <a class="el" href="classwx_text_drop_target.html" title="A predefined drop target for dealing with text data.">wxTextDropTarget</a> or <a class="el" href="classwx_file_drop_target.html" title="This is a drop target which accepts files (dragged from File Manager or Explorer).">wxFileDropTarget</a> and override their OnDropText() or OnDropFiles() method. </li>
<li><b>Drop:</b> When the user releases the mouse over a window, wxWidgets asks the associated <a class="el" href="classwx_drop_target.html" title="This class represents a target for a drag and drop operation.">wxDropTarget</a> object if it accepts the data. For this, a <a class="el" href="classwx_data_object.html" title="A wxDataObject represents data that can be copied to or from the clipboard, or dragged and dropped...">wxDataObject</a> must be associated with the drop target and this data object will be responsible for the format negotiation between the drag source and the drop target. If all goes well, then <a class="el" href="classwx_drop_target.html#a127799d4bd9bb1bba28b17f3b161eae7" title="Called after OnDrop() returns true.">wxDropTarget::OnData</a> will get called and the <a class="el" href="classwx_data_object.html" title="A wxDataObject represents data that can be copied to or from the clipboard, or dragged and dropped...">wxDataObject</a> belonging to the drop target can get filled with data. </li>
<li><b>The end</b>: After processing the data, DoDragDrop() returns either wxDragCopy or wxDragMove depending on the state of the keys Ctrl, Shift and Alt at the moment of the drop. There is currently no way for the drop target to change this return code. </li>
</ul>
</div></div><!-- contents -->
<address class="footer">
<small>
Generated on Thu Nov 27 2014 13:46:42 for wxWidgets by <a href="http://www.doxygen.org/index.html" target="_new">Doxygen</a> 1.8.2
</small>
</address>
<script src="wxwidgets.js" type="text/javascript"></script>
</div><!-- #page_container -->
</body>
</html>
|