summaryrefslogtreecommitdiffstats
path: root/src/gui/freedesksystray.cpp
blob: 6812cbe62b27836d99f6fbc0853f2dd7e93827bc (plain)
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
/***************************************************************************
 *   Copyright (C) 2004 by Emil Stoyanov                                   *
 *   emosto@users.sourceforge.net                                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

// 2006 Modified by Michel de Boer

#include "freedesksystray.h"
//Added by qt3to4:
#include <q3mimefactory.h>
#include <QPixmap>
#include <QLabel>
#include <QMouseEvent>
#include <Q3PopupMenu>

#include <QtGui/QX11Info>

FreeDeskSysTray::FreeDeskSysTray ( QWidget *pParent , const char *pszName )
    : QLabel(pParent, pszName, Qt::WMouseNoMask | Qt::WRepaintNoErase | Qt::WType_TopLevel |
                               Qt::WStyle_Customize | Qt::WStyle_NoBorder | Qt::WStyle_StaysOnTop)
{
  mainWindow = pParent;
  trayMenu = new Q3PopupMenu(this);
}

void FreeDeskSysTray::dock ()
{
  trayMenu->insertSeparator();
  trayMenu->insertItem(tr("Show/Hide"), this, SLOT(slotMenuItemShow())) ;
  
  QIcon quitIcon(qPixmapFromMimeSource("exit.png"));
  trayMenu->insertItem(quitIcon, tr("Quit"), this, SLOT(slotMenuItemQuit())) ;
  
  Display *dpy = QPaintDevice::x11AppDisplay();
  WId trayWin  = winId();

  // System Tray Protocol Specification from freedesktop.org
  Screen *screen = XDefaultScreenOfDisplay(dpy);
  int iScreen = XScreenNumberOfScreen(screen);
  char szAtom[32];
  snprintf(szAtom, sizeof(szAtom), "_NET_SYSTEM_TRAY_S%d", iScreen);
  Atom selectionAtom = XInternAtom(dpy, szAtom, false);
  XGrabServer(dpy);
  Window managerWin = XGetSelectionOwner(dpy, selectionAtom);
  XSelectInput(dpy, managerWin, StructureNotifyMask);
  XUngrabServer(dpy);
  XFlush(dpy);
  XEvent ev;
  memset(&ev, 0, sizeof(ev));
  ev.xclient.type = ClientMessage;
  ev.xclient.window = managerWin;
  ev.xclient.message_type = XInternAtom(dpy, "_NET_SYSTEM_TRAY_OPCODE", true);
  ev.xclient.format = 32;
  ev.xclient.data.l[0] = CurrentTime;
  ev.xclient.data.l[1] = SYSTEM_TRAY_REQUEST_DOCK;
  ev.xclient.data.l[2] = trayWin;
  ev.xclient.data.l[3] = 0;
  ev.xclient.data.l[4] = 0;
  XSendEvent(dpy, managerWin, false, NoEventMask, &ev);
  XSync(dpy, false);

  Atom trayAtom;
  WId forWin = mainWindow ? mainWindow->topLevelWidget()->winId() : QX11Info::appRootWindow();
  trayAtom = XInternAtom(dpy, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", false);
  XChangeProperty(dpy, trayWin, trayAtom, XA_WINDOW, 32, PropModeReplace, (unsigned char *) &forWin, 1);
  
  setMinimumSize(22, 22);
  setBackgroundMode(Qt::X11ParentRelative);
  
  // because of GNOME - needs a wait of at least 50-100 ms, otherwise width=1
  // KDocker solves the problem so (bug?)
  QTimer::singleShot(500, this, SLOT(show()));
  
}

void FreeDeskSysTray::undock ()
{
  XUnmapWindow(QPaintDevice::x11AppDisplay(), winId());
  hide();
}

FreeDeskSysTray::~FreeDeskSysTray ()
{}

void FreeDeskSysTray::mousePressEvent ( QMouseEvent *pMouseEvent )
{
  if (!QLabel::rect().contains(pMouseEvent->pos()))
    return;

  switch (pMouseEvent->button())
  {

  case Qt::LeftButton:
    slotMenuItemShow();
    break;

  case Qt::RightButton:
    showContextMenu(pMouseEvent->globalPos());
    break;

  default:
    break;
  }
}

Q3PopupMenu *FreeDeskSysTray::contextMenu()
{
	return trayMenu;
}

 void FreeDeskSysTray::setPixmap(const QPixmap& pixmap)
{
	 QLabel::setPixmap(pixmap);
	 repaint(true);
}

void FreeDeskSysTray::showContextMenu(const QPoint& position)
{
   trayMenu->popup(position,0);
}

void FreeDeskSysTray::slotMenuItemShow() {
   
//     mainWindowGeometry = mainWindow->geometry();
//     windowPos = mainWindow->frameGeometry().topLeft();
   
   if (mainWindow->isVisible()) {
     //mainWindow->setGeometry(mainWindowGeometry);
     mainWindow->close();
   } else {
//     mainWindow->move( windowPos );          // restore position
     mainWindow->show();
   }
       
}
  
void FreeDeskSysTray::slotMenuItemQuit() {
   emit quitSelected();
}