00001 /**************************************************************** 00002 * Vidalia is distributed under the following license: 00003 * 00004 * Copyright (C) 2006, Matt Edman, Justin Hipple 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 ****************************************************************/ 00021 00022 /** 00023 * \file trayicon.cpp 00024 * \version $Id: trayicon.cpp 1238 2006-09-25 17:50:57Z edmanm $ 00025 * \brief Places an icon with context menu in the system notification area 00026 */ 00027 00028 #include "trayicon.h" 00029 00030 /** Constructor. */ 00031 TrayIcon::TrayIcon(const QString &iconFile, const QString &toolTip, QMenu *popupMenu) 00032 : TrayIconImpl(iconFile, toolTip) 00033 { 00034 _popupMenu = popupMenu; 00035 } 00036 00037 /** Catches and handles mouse-related events. */ 00038 bool 00039 TrayIcon::event(QEvent *event) 00040 { 00041 switch (event->type()) { 00042 case QEvent::MouseButtonPress: 00043 mouseButtonPress((QMouseEvent *)event); 00044 break; 00045 00046 case QEvent::MouseButtonRelease: 00047 mouseButtonRelease((QMouseEvent *)event); 00048 break; 00049 00050 case QEvent::MouseButtonDblClick: 00051 mouseButtonDblClick((QMouseEvent *)event); 00052 break; 00053 00054 default: 00055 return TrayIconImpl::event(event); 00056 } 00057 event->accept(); 00058 return true; 00059 } 00060 00061 /** Responds to a single mouse button press. On X11, we display the menu when 00062 * a mouse button is pressed. */ 00063 void 00064 TrayIcon::mouseButtonPress(QMouseEvent *event) 00065 { 00066 #if defined(Q_WS_X11) 00067 if (_popupMenu) { 00068 _popupMenu->popup(event->globalPos()); 00069 } 00070 #else 00071 Q_UNUSED(event); 00072 #endif 00073 } 00074 00075 /** Responds to a single mouse button release. On Windows, we display the menu 00076 * when a mouse button is released. */ 00077 void 00078 TrayIcon::mouseButtonRelease(QMouseEvent *event) 00079 { 00080 #if defined(Q_WS_WIN) 00081 if (_popupMenu) { 00082 /* This little activateWindow() dance is necessary to make menu closing 00083 * work properly on Windows. */ 00084 _popupMenu->activateWindow(); 00085 _popupMenu->popup(event->globalPos()); 00086 _popupMenu->activateWindow(); 00087 } 00088 #else 00089 Q_UNUSED(event); 00090 #endif 00091 } 00092 00093 /** Responds to a mouse button double-click. On all platforms, we just emit a 00094 * signal and let the owner of the tray icon decide if they want to do 00095 * anything. */ 00096 void 00097 TrayIcon::mouseButtonDblClick(QMouseEvent *event) 00098 { 00099 if (event->button() == Qt::LeftButton) { 00100 emit doubleClicked(); 00101 } 00102 } 00103 00104 /** Update the tray icon's image and tooltip. */ 00105 void 00106 TrayIcon::update(const QString &iconFile, const QString &toolTip) 00107 { 00108 setIcon(iconFile); 00109 setToolTip(toolTip); 00110 } 00111 00112 /** Call the platform's tray icon implementation to show the tray icon. */ 00113 void 00114 TrayIcon::show() 00115 { 00116 TrayIconImpl::show(); 00117 } 00118 00119 /** Call the platform's tray icon implementation to hide the tray icon. */ 00120 void 00121 TrayIcon::hide() 00122 { 00123 TrayIconImpl::hide(); 00124 } 00125 00126 /** Call the platform's tray icon implementation to update the icon's tooltip.*/ 00127 void 00128 TrayIcon::setToolTip(const QString &toolTip) 00129 { 00130 TrayIconImpl::setToolTip(toolTip); 00131 } 00132 00133 /** Call the platform's tray icon implementation to update the icon image. */ 00134 void 00135 TrayIcon::setIcon(const QString &iconFile) 00136 { 00137 TrayIconImpl::setIcon(iconFile); 00138 } 00139