trayicon.cpp

Go to the documentation of this file.
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 1563 2006-12-26 06:06:04Z edmanm $
00025  * \brief Places an icon with context menu in the system notification area
00026  */
00027 
00028 #include "trayicon.h"
00029 
00030 #if defined(Q_WS_MAC)
00031 /* Exported by Qt, but not declared in the header files. 
00032  * See http://doc.trolltech.com/exportedfunctions.html  */
00033 void qt_mac_set_dock_menu(QMenu *menu);
00034 #endif
00035 
00036 
00037 /** Default constructor. */
00038 TrayIcon::TrayIcon()
00039 {
00040   _contextMenu = 0;
00041 }
00042 
00043 /** Catches and handles mouse-related events. */
00044 bool
00045 TrayIcon::event(QEvent *event)
00046 {
00047   switch (event->type()) {
00048     case QEvent::MouseButtonPress:
00049       mouseButtonPress((QMouseEvent *)event);
00050       break;
00051 
00052     case QEvent::MouseButtonRelease:
00053       mouseButtonRelease((QMouseEvent *)event);
00054       break;
00055 
00056     case QEvent::MouseButtonDblClick:
00057       mouseButtonDblClick((QMouseEvent *)event);
00058       break;
00059 
00060     default:
00061       return TrayIconImpl::event(event);
00062   }
00063   event->accept();
00064   return true;
00065 }
00066 
00067 /** Responds to a single mouse button press. On X11, we display the menu when
00068  * a mouse button is pressed. */
00069 void
00070 TrayIcon::mouseButtonPress(QMouseEvent *event)
00071 {
00072 #if defined(Q_WS_X11)
00073   if (_contextMenu) {
00074     _contextMenu->popup(event->globalPos());
00075   }
00076 #else
00077   Q_UNUSED(event);
00078 #endif
00079 }
00080 
00081 /** Responds to a single mouse button release. On Windows, we display the menu
00082  * when a mouse button is released. */
00083 void
00084 TrayIcon::mouseButtonRelease(QMouseEvent *event)
00085 {
00086 #if defined(Q_WS_WIN)
00087   if (_contextMenu) {
00088     /* This little activateWindow() dance is necessary to make menu closing
00089      * work properly on Windows. */
00090     _contextMenu->activateWindow();
00091     _contextMenu->popup(event->globalPos());
00092     _contextMenu->activateWindow();
00093   }
00094 #else
00095   Q_UNUSED(event);
00096 #endif
00097 }
00098 
00099 /** Responds to a mouse button double-click. On all platforms, we just emit a
00100  * signal and let the owner of the tray icon decide if they want to do
00101  * anything. */
00102 void
00103 TrayIcon::mouseButtonDblClick(QMouseEvent *event)
00104 {
00105   if (event->button() == Qt::LeftButton) {
00106     emit doubleClicked();
00107   }
00108 }
00109 
00110 /** Update the tray icon's image and tooltip. */
00111 void
00112 TrayIcon::update(const QString &iconFile, const QString &toolTip)
00113 {
00114   setIcon(iconFile);
00115   setToolTip(toolTip);
00116 }
00117 
00118 /** Call the platform's tray icon implementation to show the tray icon. */
00119 void
00120 TrayIcon::show()
00121 {
00122   TrayIconImpl::show();
00123 }
00124 
00125 /** Call the platform's tray icon implementation to hide the tray icon. */
00126 void
00127 TrayIcon::hide()
00128 {
00129   TrayIconImpl::hide();
00130 }
00131 
00132 /** Call the platform's tray icon implementation to update the icon's tooltip.*/
00133 void
00134 TrayIcon::setToolTip(const QString &toolTip)
00135 {
00136   TrayIconImpl::setToolTip(toolTip);
00137 }
00138 
00139 /** Call the platform's tray icon implementation to update the icon image. */
00140 void
00141 TrayIcon::setIcon(const QString &iconFile)
00142 {
00143   TrayIconImpl::setIcon(iconFile);
00144 }
00145 
00146 /** Sets the context menu displayed when the tray icon is selected. On Mac,
00147  * the context menu is displayed when the dock icon is clicked. */
00148 void
00149 TrayIcon::setContextMenu(QMenu *menu)
00150 {
00151 #if defined(Q_WS_MAC)
00152   qt_mac_set_dock_menu(menu);
00153 #else
00154   _contextMenu = menu;
00155 #endif
00156 }
00157 

Generated on Wed Sep 5 15:49:28 2007 for Vidalia by  doxygen 1.5.3