source: trunk/src/gui/kernel/qdnd_x11.cpp@ 142

Last change on this file since 142 was 2, checked in by Dmitry A. Kuminov, 17 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 65.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qplatformdefs.h"
43
44#include "qapplication.h"
45
46#ifndef QT_NO_DRAGANDDROP
47
48#include "qwidget.h"
49#include "qpainter.h"
50#include "qpixmap.h"
51#include "qbitmap.h"
52#include "qdesktopwidget.h"
53#include "qevent.h"
54#include "qdatetime.h"
55#include "qiodevice.h"
56#include "qpointer.h"
57#include "qcursor.h"
58#include "qvariant.h"
59#include "qvector.h"
60#include "qurl.h"
61#include "qdebug.h"
62#include "qimagewriter.h"
63#include "qbuffer.h"
64#include "qtextcodec.h"
65
66#include "qdnd_p.h"
67#include "qt_x11_p.h"
68#include "qx11info_x11.h"
69
70#include "qwidget_p.h"
71#include "qcursor_p.h"
72
73QT_BEGIN_NAMESPACE
74
75// #define DND_DEBUG
76#ifdef DND_DEBUG
77#define DEBUG qDebug
78#else
79#define DEBUG if(0) qDebug
80#endif
81
82#ifdef DND_DEBUG
83#define DNDDEBUG qDebug()
84#else
85#define DNDDEBUG if(0) qDebug()
86#endif
87
88static int findXdndDropTransactionByWindow(Window window)
89{
90 int at = -1;
91 for (int i = 0; i < X11->dndDropTransactions.count(); ++i) {
92 const QXdndDropTransaction &t = X11->dndDropTransactions.at(i);
93 if (t.target == window || t.proxy_target == window) {
94 at = i;
95 break;
96 }
97 }
98 return at;
99}
100
101static int findXdndDropTransactionByTime(Time timestamp)
102{
103 int at = -1;
104 for (int i = 0; i < X11->dndDropTransactions.count(); ++i) {
105 const QXdndDropTransaction &t = X11->dndDropTransactions.at(i);
106 if (t.timestamp == timestamp) {
107 at = i;
108 break;
109 }
110 }
111 return at;
112}
113
114// timer used to discard old XdndDrop transactions
115static int transaction_expiry_timer = -1;
116enum { XdndDropTransactionTimeout = 5000 }; // 5 seconds
117
118static void restartXdndDropExpiryTimer()
119{
120 if (transaction_expiry_timer != -1)
121 QDragManager::self()->killTimer(transaction_expiry_timer);
122 transaction_expiry_timer = QDragManager::self()->startTimer(XdndDropTransactionTimeout);
123}
124
125
126// find an ancestor with XdndAware on it
127static Window findXdndAwareParent(Window window)
128{
129 Window target = 0;
130 forever {
131 // check if window has XdndAware
132 Atom type = 0;
133 int f;
134 unsigned long n, a;
135 unsigned char *data = 0;
136 if (XGetWindowProperty(X11->display, window, ATOM(XdndAware), 0, 0, False,
137 AnyPropertyType, &type, &f,&n,&a,&data) == Success) {
138 if (data)
139 XFree(data);
140 if (type) {
141 target = window;
142 break;
143 }
144 }
145
146 // try window's parent
147 Window root;
148 Window parent;
149 Window *children;
150 uint unused;
151 if (!XQueryTree(X11->display, window, &root, &parent, &children, &unused))
152 break;
153 if (children)
154 XFree(children);
155 if (window == root)
156 break;
157 window = parent;
158 }
159 return target;
160}
161
162
163
164
165// and all this stuff is copied -into- qapp_x11.cpp
166
167static void handle_xdnd_position(QWidget *, const XEvent *, bool);
168static void handle_xdnd_status(QWidget * w, const XEvent * xe, bool /*passive*/);
169
170const int xdnd_version = 5;
171
172static Qt::DropAction xdndaction_to_qtaction(Atom atom)
173{
174 if (atom == ATOM(XdndActionCopy) || atom == 0)
175 return Qt::CopyAction;
176 if (atom == ATOM(XdndActionLink))
177 return Qt::LinkAction;
178 if (atom == ATOM(XdndActionMove))
179 return Qt::MoveAction;
180 return Qt::CopyAction;
181}
182
183static int qtaction_to_xdndaction(Qt::DropAction a)
184{
185 switch (a) {
186 case Qt::CopyAction:
187 return ATOM(XdndActionCopy);
188 case Qt::LinkAction:
189 return ATOM(XdndActionLink);
190 case Qt::MoveAction:
191 case Qt::TargetMoveAction:
192 return ATOM(XdndActionMove);
193 case Qt::IgnoreAction: