summaryrefslogtreecommitdiffstats
path: root/superkaramba/src/systemtray.cpp
blob: ea93889fc404ac7403829eea51a7c5e1fca0dc25 (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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/***************************************************************************
    copyright (C) 2003      Adam Geitgey <[email protected]>
                  2003      Sven Leiber <[email protected]>
                  2000-2001 Matthias Ettrich <[email protected]>
                  2000-2001 Matthias Elter   <[email protected]>
                  2001      Carsten Pfeiffer <[email protected]>
                  2001      Martijn Klingens <[email protected]>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "systemtray.h"


#include <tqobject.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <twinmodule.h>
#include <tdemessagebox.h>
#include <kdebug.h>
#include <twin.h>

#include <tqpopupmenu.h>
#include <tqdragobject.h>
#include <tqlayout.h>
#include <tqstringlist.h>
#include <tqpixmap.h>

#include <X11/Xlib.h>

Systemtray::Systemtray(TQWidget* parent)
  : TQWidget(parent,0,0)
{
  setBackgroundOrigin(ParentOrigin);
  setBackgroundMode(FixedPixmap);
  m_Wins.setAutoDelete(true);
}


Systemtray::~Systemtray()
{
  m_Wins.clear();
}

int Systemtray::getTraySize() {

	return (int) twin_module->systemTrayWindows().size();
}

void Systemtray::updateBackgroundPixmap ( const TQPixmap & pixmap) {
  QXEmbed *emb;
  setPaletteBackgroundPixmap (pixmap);
    for (emb = m_Wins.first(); emb != 0L; emb = m_Wins.next()) {

    //Stupid stupid stupid work around for annoying bug
    //QXEmbed ignores setBackgroundOrigin(AncestorOrigin)....
    TQPixmap bug = TQPixmap(emb->size());
    bitBlt(&bug, 0, 0, const_cast<TQPixmap*>(&pixmap), emb->parentWidget()->x()+emb->x(),  emb->parentWidget()->y()+emb->y(), emb->width(), emb->height(),TQt::CopyROP, false);
    emb->setPaletteBackgroundPixmap (bug);

  }

    TQPoint topPoint = mapToGlobal(TQPoint(0,0));
    Window hack = XCreateSimpleWindow(tqt_xdisplay(), winId(), 0,0, width(), height(), 0, 0, 0);
    XRaiseWindow(tqt_xdisplay(), hack);
    XMapWindow(tqt_xdisplay(), hack);
    XUnmapWindow(tqt_xdisplay(), hack);
    XDestroyWindow(tqt_xdisplay(), hack);
}

void Systemtray::initSystray( void )
{
  bool existing = false;
  //bool content = false;
  Display *display = tqt_xdisplay();
  no_of_systray_windows = 0;

  twin_module = new KWinModule();
  systemTrayWindows = twin_module->systemTrayWindows();
  TQValueList<WId>::ConstIterator end(systemTrayWindows.end());
  for (TQValueList<WId>::ConstIterator it = systemTrayWindows.begin(); it!=end; ++it)
  {
    no_of_systray_windows++;
    QXEmbed *emb;

    emb = new QXEmbed(this);
    emb->setBackgroundMode(FixedPixmap);

    emb->setAutoDelete(false);

    connect(emb, TQ_SIGNAL(embeddedWindowDestroyed()), TQ_SLOT(updateTrayWindows()));

    m_Wins.append(emb);

    emb->embed(*it);
    emb->resize(24, 24);
    emb->show();
    existing = true;
  }

  updateTrayWindows();

  connect(twin_module, TQ_SIGNAL(systemTrayWindowAdded(WId)), TQ_SLOT(systemTrayWindowAdded(WId)));
  connect(twin_module, TQ_SIGNAL(systemTrayWindowRemoved(WId)), TQ_SLOT(systemTrayWindowRemoved(WId)));

  TQCString screenstr;
  screenstr.setNum(tqt_xscreen());
  TQCString trayatom = "_NET_SYSTEM_TRAY_S" + screenstr;

  net_system_tray_selection = XInternAtom( display, trayatom, false );
  net_system_tray_opcode = XInternAtom( display, "_NET_SYSTEM_TRAY_OPCODE", false );

  // Acquire system tray
  XSetSelectionOwner( display,
    net_system_tray_selection,
    winId(),
    CurrentTime );

  WId root = tqt_xrootwin();

  if (XGetSelectionOwner(display, net_system_tray_selection) == winId())
  {
    XClientMessageEvent xev;

    xev.type = ClientMessage;
    xev.window = root;

    xev.message_type = XInternAtom(display, "MANAGER", false);
    xev.format = 32;

    xev.data.l[0] = CurrentTime;
    xev.data.l[1] = net_system_tray_selection;
    xev.data.l[2] = winId();
    xev.data.l[3] = 0;       /* Manager specific data */
    xev.data.l[4] = 0;       /* Manager specific data */

    XSendEvent( display, root, false, StructureNotifyMask, (XEvent *)&xev );
  }
}

void Systemtray::updateTrayWindows( void )
{
  QXEmbed *emb;

  emb = m_Wins.first();
  while ((emb = m_Wins.current()) != 0L)
  {
    WId wid = emb->embeddedWinId();
    if ((wid == 0) || !twin_module->systemTrayWindows().contains(wid) )
      m_Wins.remove(emb);
    else
      m_Wins.next();
  }
  layoutSystray();
}
void Systemtray::layoutSystray()
{
  int i = 0, a = 0;

  QXEmbed* emb;
  int x = 0;
  int count = 0;

  //How many systray icons can fit on a line?
  int aa = width() / 24;

  if(aa < 1)
  {
    /* The place is to small to display a icon we make than one line with
       icons that we display at the top */
    aa = 1;
  }

  for (emb = m_Wins.first(); emb != 0L; emb = m_Wins.next()) {
    x = 2+i*24;

    emb->move(a*24, x);
    a++;

    if(a+1 > aa) {
      a = 0;
      i++;
    }

    count++;
    emb->repaint();
  }
}

void Systemtray::systemTrayWindowAdded( WId w )
{
  //bool content = false;
  QXEmbed *emb;
  no_of_systray_windows++;
  emit updated();

  emb = new QXEmbed(this);

  emb->setAutoDelete(false);
  //emb->setBackgroundMode(X11ParentRelative);
  emb->setBackgroundMode(FixedPixmap);
  connect(emb, TQ_SIGNAL(embeddedWindowDestroyed()), TQ_SLOT(updateTrayWindows()));
  m_Wins.append(emb);

  emb->embed(w);
  emb->resize(24, 24);
  emb->show();

  layoutSystray();
}

void Systemtray::systemTrayWindowRemoved(WId)
{
  no_of_systray_windows--;
  emit updated();
  updateTrayWindows();
}

int Systemtray::getCurrentWindowCount()
{
  return no_of_systray_windows;
}

#include "systemtray.moc"