1 | /*
|
---|
2 | * trayicon.cpp - system-independent trayicon class (adapted from Qt example)
|
---|
3 | * Copyright (C) 2003 Justin Karneges
|
---|
4 | *
|
---|
5 | * This library is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU Lesser General Public
|
---|
7 | * License as published by the Free Software Foundation; either
|
---|
8 | * version 2.1 of the License, or (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This library is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | * Lesser General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU Lesser General Public
|
---|
16 | * License along with this library; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
18 | *
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "trayicon.h"
|
---|
22 | #include "qpopupmenu.h"
|
---|
23 |
|
---|
24 | /*!
|
---|
25 | \class TrayIcon qtrayicon.h
|
---|
26 | \brief The TrayIcon class implements an entry in the system tray.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /*!
|
---|
30 | Creates a TrayIcon object. \a parent and \a name are propagated
|
---|
31 | to the QObject constructor. The icon is initially invisible.
|
---|
32 |
|
---|
33 | \sa show
|
---|
34 | */
|
---|
35 | TrayIcon::TrayIcon( QObject *parent, const char *name )
|
---|
36 | : QObject(parent, name), pop(0), d(0)
|
---|
37 | {
|
---|
38 | v_isWMDock = FALSE;
|
---|
39 | }
|
---|
40 |
|
---|
41 | /*!
|
---|
42 | Creates a TrayIcon object displaying \a icon and \a tooltip, and opening
|
---|
43 | \a popup when clicked with the right mousebutton. \a parent and \a name are
|
---|
44 | propagated to the QObject constructor. The icon is initially invisible.
|
---|
45 |
|
---|
46 | \sa show
|
---|
47 | */
|
---|
48 | TrayIcon::TrayIcon( const QPixmap &icon, const QString &tooltip, QPopupMenu *popup, QObject *parent, const char *name )
|
---|
49 | : QObject(parent, name), pop(popup), pm(icon), tip(tooltip), d(0)
|
---|
50 | {
|
---|
51 | v_isWMDock = FALSE;
|
---|
52 |
|
---|
53 | if ( !pm.width() || !pm.height() )
|
---|
54 | pm = QPixmap( 16, 16 );
|
---|
55 | }
|
---|
56 |
|
---|
57 | /*!
|
---|
58 | Removes the icon from the system tray and frees all allocated resources.
|
---|
59 | */
|
---|
60 | TrayIcon::~TrayIcon()
|
---|
61 | {
|
---|
62 | sysRemove();
|
---|
63 | }
|
---|
64 |
|
---|
65 | /*!
|
---|
66 | Informs the trayicon that the notification owner has probably been changed;
|
---|
67 | and that it should attempt to register or re-register.
|
---|
68 | */
|
---|
69 | void TrayIcon::newTrayOwner()
|
---|
70 | {
|
---|
71 | // detach ourself from any existing notification area.
|
---|
72 | hide();
|
---|
73 | // show ourself on the new notification area
|
---|
74 | show();
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | /*!
|
---|
79 | Sets the context menu to \a popup. The context menu will pop up when the
|
---|
80 | user clicks the system tray entry with the right mouse button.
|
---|
81 | */
|
---|
82 | void TrayIcon::setPopup( QPopupMenu* popup )
|
---|
83 | {
|
---|
84 | pop = popup;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /*!
|
---|
88 | Returns the current popup menu.
|
---|
89 |
|
---|
90 | \sa setPopup
|
---|
91 | */
|
---|
92 | QPopupMenu* TrayIcon::popup() const
|
---|
93 | {
|
---|
94 | return pop;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /*!
|
---|
98 | \property TrayIcon::icon
|
---|
99 | \brief the system tray icon.
|
---|
100 | */
|
---|
101 | void TrayIcon::setIcon( const QPixmap &icon )
|
---|
102 | {
|
---|
103 | //if(!popup()) {
|
---|
104 | // tip = "";
|
---|
105 | //}
|
---|
106 |
|
---|
107 | pm = icon;
|
---|
108 | sysUpdateIcon();
|
---|
109 | }
|
---|
110 |
|
---|
111 | QPixmap TrayIcon::icon() const
|
---|
112 | {
|
---|
113 | return pm;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /*!
|
---|
117 | \property TrayIcon::toolTip
|
---|
118 | \brief the tooltip for the system tray entry
|
---|
119 |
|
---|
120 | On some systems, the tooltip's length is limited and will be truncated as necessary.
|
---|
121 | */
|
---|
122 | void TrayIcon::setToolTip( const QString &tooltip )
|
---|
123 | {
|
---|
124 | tip = tooltip;
|
---|
125 | sysUpdateToolTip();
|
---|
126 | }
|
---|
127 |
|
---|
128 | QString TrayIcon::toolTip() const
|
---|
129 | {
|
---|
130 | return tip;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /*!
|
---|
134 | Shows the icon in the system tray.
|
---|
135 |
|
---|
136 | \sa hide
|
---|
137 | */
|
---|
138 | void TrayIcon::show()
|
---|
139 | {
|
---|
140 | sysInstall();
|
---|
141 | }
|
---|
142 |
|
---|
143 | /*!
|
---|
144 | Hides the system tray entry.
|
---|
145 | */
|
---|
146 | void TrayIcon::hide()
|
---|
147 | {
|
---|
148 | sysRemove();
|
---|
149 | }
|
---|
150 |
|
---|
151 | /*!
|
---|
152 | \reimp
|
---|
153 | */
|
---|
154 | bool TrayIcon::event( QEvent *e )
|
---|
155 | {
|
---|
156 | switch ( e->type() ) {
|
---|
157 | case QEvent::MouseMove:
|
---|
158 | mouseMoveEvent( (QMouseEvent*)e );
|
---|
159 | break;
|
---|
160 |
|
---|
161 | case QEvent::MouseButtonPress:
|
---|
162 | mousePressEvent( (QMouseEvent*)e );
|
---|
163 | break;
|
---|
164 |
|
---|
165 | case QEvent::MouseButtonRelease:
|
---|
166 | mouseReleaseEvent( (QMouseEvent*)e );
|
---|
167 | break;
|
---|
168 |
|
---|
169 | case QEvent::MouseButtonDblClick:
|
---|
170 | mouseDoubleClickEvent( (QMouseEvent*)e );
|
---|
171 | break;
|
---|
172 | default:
|
---|
173 | return QObject::event( e );
|
---|
174 | }
|
---|
175 |
|
---|
176 | return TRUE;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /*!
|
---|
180 | This event handler can be reimplemented in a subclass to receive
|
---|
181 | mouse move events for the system tray entry.
|
---|
182 |
|
---|
183 | \sa mousePressEvent(), mouseReleaseEvent(), mouseDoubleClickEvent(), QMouseEvent
|
---|
184 | */
|
---|
185 | void TrayIcon::mouseMoveEvent( QMouseEvent *e )
|
---|
186 | {
|
---|
187 | e->ignore();
|
---|
188 | }
|
---|
189 |
|
---|
190 | /*!
|
---|
191 | This event handler can be reimplemented in a subclass to receive
|
---|
192 | mouse press events for the system tray entry.
|
---|
193 |
|
---|
194 | \sa mouseReleaseEvent(), mouseDoubleClickEvent(),
|
---|
195 | mouseMoveEvent(), QMouseEvent
|
---|
196 | */
|
---|
197 | void TrayIcon::mousePressEvent( QMouseEvent *e )
|
---|
198 | {
|
---|
199 | #ifndef Q_WS_WIN
|
---|
200 | // This is for X11, menus appear on mouse press
|
---|
201 | // I'm not sure whether Mac should be here or below.. Somebody check?
|
---|
202 | switch ( e->button() ) {
|
---|
203 | case RightButton:
|
---|
204 | if ( pop ) {
|
---|
205 | pop->popup( e->globalPos() );
|
---|
206 | e->accept();
|
---|
207 | }
|
---|
208 | break;
|
---|
209 | case LeftButton:
|
---|
210 | case MidButton:
|
---|
211 | emit clicked( e->globalPos(), e->button() );
|
---|
212 | break;
|
---|
213 | default:
|
---|
214 | break;
|
---|
215 | }
|
---|
216 | #endif
|
---|
217 | e->ignore();
|
---|
218 | }
|
---|
219 |
|
---|
220 | /*!
|
---|
221 | This event handler can be reimplemented in a subclass to receive
|
---|
222 | mouse release events for the system tray entry.
|
---|
223 |
|
---|
224 | The default implementations opens the context menu when the entry
|
---|
225 | has been clicked with the right mouse button.
|
---|
226 |
|
---|
227 | \sa setPopup(), mousePressEvent(), mouseDoubleClickEvent(),
|
---|
228 | mouseMoveEvent(), QMouseEvent
|
---|
229 | */
|
---|
230 | void TrayIcon::mouseReleaseEvent( QMouseEvent *e )
|
---|
231 | {
|
---|
232 | #ifdef Q_WS_WIN
|
---|
233 | // This is for Windows, where menus appear on mouse release
|
---|
234 | switch ( e->button() ) {
|
---|
235 | case RightButton:
|
---|
236 | if ( pop ) {
|
---|
237 | // Necessary to make keyboard focus
|
---|
238 | // and menu closing work on Windows.
|
---|
239 | pop->setActiveWindow();
|
---|
240 | pop->popup( e->globalPos() );
|
---|
241 | pop->setActiveWindow();
|
---|
242 | e->accept();
|
---|
243 | }
|
---|
244 | break;
|
---|
245 | case LeftButton:
|
---|
246 | case MidButton:
|
---|
247 | emit clicked( e->globalPos(), e->button() );
|
---|
248 | break;
|
---|
249 | default:
|
---|
250 | break;
|
---|
251 | }
|
---|
252 | #endif
|
---|
253 | e->ignore();
|
---|
254 | }
|
---|
255 |
|
---|
256 | /*!
|
---|
257 | This event handler can be reimplemented in a subclass to receive
|
---|
258 | mouse double click events for the system tray entry.
|
---|
259 |
|
---|
260 | Note that the system tray entry gets a mousePressEvent() and a
|
---|
261 | mouseReleaseEvent() before the mouseDoubleClickEvent().
|
---|
262 |
|
---|
263 | \sa mousePressEvent(), mouseReleaseEvent(),
|
---|
264 | mouseMoveEvent(), QMouseEvent
|
---|
265 | */
|
---|
266 | void TrayIcon::mouseDoubleClickEvent( QMouseEvent *e )
|
---|
267 | {
|
---|
268 | if ( e->button() == LeftButton )
|
---|
269 | emit doubleClicked( e->globalPos() );
|
---|
270 | e->accept();
|
---|
271 | }
|
---|
272 |
|
---|
273 | /*!
|
---|
274 | \fn void TrayIcon::clicked( const QPoint &p )
|
---|
275 |
|
---|
276 | This signal is emitted when the user clicks the system tray icon
|
---|
277 | with the left mouse button, with \a p being the global mouse position
|
---|
278 | at that moment.
|
---|
279 | */
|
---|
280 |
|
---|
281 | /*!
|
---|
282 | \fn void TrayIcon::doubleClicked( const QPoint &p )
|
---|
283 |
|
---|
284 | This signal is emitted when the user double clicks the system tray
|
---|
285 | icon with the left mouse button, with \a p being the global mouse position
|
---|
286 | at that moment.
|
---|
287 | */
|
---|
288 |
|
---|
289 | void TrayIcon::gotCloseEvent()
|
---|
290 | {
|
---|
291 | closed();
|
---|
292 | }
|
---|