Now the systrayicon change it's color when a download is in progress. I simply change...
[kdenetwork.git] / kget / droptarget.cpp
blob97bb01e2518b49937c687a26187c5500780c84e0
1 /***************************************************************************
2 * droptarget.cpp
3 * -------------------
5 * Revision : $Id$
6 * begin : Tue Jan 29 2002
7 * copyright : (C) 2002 by Patrick Charbonnier
8 * : Based On Caitoo v.0.7.3 (c) 1998 - 2000, Matej Koss
9 * email : pch@freeshell.org
11 ****************************************************************************/
13 /***************************************************************************
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 ***************************************************************************/
27 #include <qpainter.h>
29 #include <kaction.h>
30 #include <kapplication.h>
31 #include <kconfig.h>
32 #include <kiconloader.h>
33 #include <kglobalsettings.h>
34 #include <kmainwindow.h>
35 #include <kwin.h>
36 #include <klocale.h>
37 #include <kpopupmenu.h>
38 #include <kurldrag.h>
40 #include "kmainwidget.h"
41 #include <qcursor.h>
42 #ifdef Q_WS_X11
43 #include <X11/Xlib.h>
44 #include <X11/Xutil.h>
45 #include <X11/Xos.h>
46 #include <X11/extensions/shape.h>
47 #undef Bool
48 #undef Status
49 #endif
50 #include "settings.h"
51 #include "droptarget.h"
53 DropTarget::DropTarget(KMainWindow * mainWin):QWidget()
55 int x = ksettings.dropPosition.x();
56 int y = ksettings.dropPosition.y();
58 QRect desk = KGlobalSettings::desktopGeometry(this);
59 QPixmap bgnd = UserIcon( "target" );
61 if (x != -1 &&
62 x >= desk.left() && y >= desk.top() &&
63 (x + bgnd.width()) <= desk.right() &&
64 (y + bgnd.height()) <= desk.bottom() )
66 move(ksettings.dropPosition);
67 resize(bgnd.width(), bgnd.height());
68 KWin::setState(winId(), ksettings.dropState);
70 else
72 setGeometry(desk.x()+200, desk.y()+200, bgnd.width(), bgnd.height());
73 KWin::setState(winId(), NET::SkipTaskbar | NET::StaysOnTop);
76 b_sticky = ksettings.dropState & NET::Sticky;
78 // setup pixmaps
80 if (!bgnd.mask())
81 kdError(5001) << "Drop target pixmap has no mask!\n";
82 else
83 mask = *bgnd.mask();
85 setBackgroundPixmap( bgnd );
87 // popup menu for right mouse button
88 popupMenu = new KPopupMenu();
89 popupMenu->insertTitle(kapp->caption());
90 popupMenu->setCheckable(true);
92 pop_Max = popupMenu->insertItem(i18n("Maximize"), this, SLOT(toggleMinimizeRestore()));
93 pop_Min = popupMenu->insertItem(i18n("Minimize"), this, SLOT(toggleMinimizeRestore()));
95 pop_sticky = popupMenu->insertItem(i18n("Sticky"), this, SLOT(toggleSticky()));
96 popupMenu->insertSeparator();
97 mainWin->action("drop_target")->plug(popupMenu);
98 popupMenu->insertSeparator();
100 popupMenu->setItemChecked(pop_sticky, b_sticky);
101 kmain->m_paPreferences->plug(popupMenu);
102 popupMenu->insertSeparator();
103 kmain->m_paQuit->plug(popupMenu);
105 isdragging = false;
107 // Enable dropping
108 setAcceptDrops(true);
112 DropTarget::~DropTarget()
114 delete popupMenu;
118 void
119 DropTarget::mousePressEvent(QMouseEvent * e)
121 if (e->button() == LeftButton)
123 // toggleMinimizeRestore ();
124 // oldX = 0;
125 // oldY = 0;
126 isdragging = true;
127 dx = QCursor::pos().x() - pos().x();
128 dy = QCursor::pos().y() - pos().y();
130 else if (e->button() == RightButton)
132 popupMenu->setItemEnabled(pop_Min, kmain->isVisible());
133 popupMenu->setItemEnabled(pop_Max, kmain->isHidden());
135 popupMenu->popup(QCursor::pos());
137 else if (e->button() == MidButton)
139 kmain->slotPasteTransfer();
144 void DropTarget::resizeEvent(QResizeEvent *)
146 #ifdef Q_WS_X11
147 XShapeCombineMask(x11Display(), winId(), ShapeBounding, 0, 0, mask.handle(), ShapeSet);
148 #endif
152 void DropTarget::dragEnterEvent(QDragEnterEvent * event)
154 event->accept(KURLDrag::canDecode(event)
155 || QTextDrag::canDecode(event));
159 void DropTarget::dropEvent(QDropEvent * event)
161 KURL::List list;
162 QString str;
164 if (KURLDrag::decode(event, list))
166 kmain->addTransfers(list);
168 else if (QTextDrag::decode(event, str))
170 kmain->addTransfer(str);
175 void DropTarget::toggleSticky()
177 b_sticky = !b_sticky;
178 popupMenu->setItemChecked(pop_sticky, b_sticky);
179 updateStickyState();
182 void DropTarget::updateStickyState()
184 if (b_sticky)
186 KWin::setState(winId(), NET::SkipTaskbar | NET::StaysOnTop | NET::Sticky);
188 else
190 KWin::clearState(winId(), NET::Sticky);
194 void DropTarget::toggleMinimizeRestore()
196 if (kmain->isVisible())
197 kmain->hide();
198 else
199 kmain->show();
202 /** No descriptions */
203 void DropTarget::mouseMoveEvent(QMouseEvent * e)
206 if (oldX == 0)
208 oldX = e->x();
209 oldY = e->y();
210 return;
213 if (isdragging)
214 move( QCursor::pos().x() - dx, QCursor::pos().y() - dy );
216 // move(x() + (e->x() - oldX), y() + (e->y() - oldY)); // <<--
219 void DropTarget::mouseReleaseEvent(QMouseEvent *)
221 isdragging = false;
224 /** No descriptions */
225 void DropTarget::mouseDoubleClickEvent(QMouseEvent * e)
227 if (e->button() == LeftButton)
228 toggleMinimizeRestore();
231 #include "droptarget.moc"