trayicon_win.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  *  The createIcon() method in this class is derived from code in
00022  *  trayicon_win.cpp by Justin Karneges, licensed as follows:
00023  *
00024  *  Copyright (C) 2003  Justin Karneges
00025  *
00026  *  This library is free software; you can redistribute it and/or
00027  *  modify it under the terms of the GNU Lesser General Public
00028  *  License as published by the Free Software Foundation; either
00029  *  version 2.1 of the License, or (at your option) any later version.
00030  * 
00031  *  This library is distributed in the hope that it will be useful,
00032  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00033  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00034  *  Lesser General Public License for more details.
00035  * 
00036  *  You should have received a copy of the GNU Lesser General Public
00037  *  License along with this library; if not, write to the Free Software
00038  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00039  *  02111-1307  USA
00040  ****************************************************************/
00041 
00042 /** 
00043  * \file trayicon_win.cpp
00044  * \version $Id: trayicon_win.cpp 1465 2006-11-21 04:45:15Z edmanm $
00045  * \brief Tray icon implementation on Win32 
00046  */
00047 
00048 #include <QApplication>
00049 
00050 #include "trayicon_win.h"
00051 
00052 #define WM_NOTIFYICON   WM_APP /**< Message sent for events related to our icon.*/
00053 #define TRAY_ICON_ID    0   /**< ID for our tray icon. */
00054 UINT    WM_TASKBARCREATED;  /**< Message sent when taskbar is created. */
00055 
00056 
00057 /** Default constructor. */
00058 TrayIconImpl::TrayIconImpl()
00059 {
00060   setObjectName("trayiconimpl");
00061  
00062   /* We want to know when Explorer crashes and recreates the taskbar. */
00063   WM_TASKBARCREATED = RegisterWindowMessage(TEXT("TaskbarCreated"));
00064 }
00065 
00066 /** Updates the tray icon. */
00067 void
00068 TrayIconImpl::updateTrayIcon(DWORD msg)
00069 {
00070 QT_WA(  
00071   { /* UNICODE */
00072     NOTIFYICONDATAW nfd;
00073     nfd.cbSize = sizeof(NOTIFYICONDATAW);
00074     nfd.hWnd = winId();
00075     nfd.uID = TRAY_ICON_ID;
00076     nfd.uCallbackMessage = WM_NOTIFYICON;
00077     nfd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
00078     nfd.hIcon = _icon;
00079     lstrcpynW(nfd.szTip, (TCHAR*)_toolTip.unicode(), 
00080               qMin(_toolTip.length()+1, 64));
00081     Shell_NotifyIconW(msg, &nfd);
00082   },
00083   { /* non-UNICODE */
00084     NOTIFYICONDATAA nfd;
00085     nfd.cbSize = sizeof(NOTIFYICONDATAA);
00086     nfd.hWnd = winId();
00087     nfd.uID = TRAY_ICON_ID;
00088     nfd.uCallbackMessage = WM_NOTIFYICON;
00089     nfd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
00090     nfd.hIcon = _icon;
00091     lstrcpynA(nfd.szTip, (const char*)_toolTip.toLocal8Bit(), 
00092               qMin(_toolTip.length()+1, 64));
00093     Shell_NotifyIconA(msg, &nfd);
00094   })
00095 }
00096 
00097 /** Catches and handles mouse-related native Windows event messages. */
00098 bool
00099 TrayIconImpl::winEvent(MSG *msg, long *result)
00100 {
00101   if (msg->message == WM_NOTIFYICON) {
00102     switch (msg->lParam) {
00103       case WM_LBUTTONUP:
00104         return sendMouseEvent(QEvent::MouseButtonRelease, Qt::LeftButton);
00105 
00106       case WM_RBUTTONUP:
00107         return sendMouseEvent(QEvent::MouseButtonRelease, Qt::RightButton);
00108 
00109       case WM_LBUTTONDBLCLK:
00110         return sendMouseEvent(QEvent::MouseButtonDblClick, Qt::LeftButton);
00111 
00112       default:
00113         break;
00114     }
00115   } else if (msg->message == WM_TASKBARCREATED) {
00116     /* We handle WM_TASKBARCREATED because it's possible that Explorer
00117      * crashed and was recreated, in which case we need to re-add our icon. */
00118     updateTrayIcon(NIM_ADD);
00119   }
00120   return QWidget::winEvent(msg, result);
00121 }
00122 
00123 /** Sends a mouse-related event to the main TrayIcon class. */
00124 bool
00125 TrayIconImpl::sendMouseEvent(QEvent::Type type, Qt::MouseButton button)
00126 {
00127   QPoint pos = QCursor::pos();
00128   QMouseEvent event(type, mapFromGlobal(pos), pos, button, button, Qt::NoModifier);
00129   return QApplication::sendEvent(this, &event);
00130 }
00131 
00132 /** Create an icon for the tray image from a resource identifier. */
00133 HICON
00134 TrayIconImpl::createIcon(const int resourceId)
00135 {
00136   return LoadIconA((HINSTANCE)GetModuleHandleA(NULL),
00137                    MAKEINTRESOURCEA(resourceId));
00138 }
00139 
00140 /** Show the tray icon image. */
00141 void
00142 TrayIconImpl::show()
00143 {
00144   updateTrayIcon(NIM_ADD);
00145 }
00146 
00147 /** Hide the tray icon image. */
00148 void
00149 TrayIconImpl::hide()
00150 {
00151   updateTrayIcon(NIM_DELETE);
00152 }
00153 
00154 /** Set the tray icon's tooltip. */
00155 void
00156 TrayIconImpl::setToolTip(const QString &toolTip)
00157 {
00158   _toolTip = toolTip;
00159   updateTrayIcon(NIM_MODIFY);
00160 }
00161 
00162 /** Set the tray icon's image. */
00163 void
00164 TrayIconImpl::setIcon(const QString &iconFile)
00165 {
00166   _icon = createIcon(iconFile.toInt());
00167   updateTrayIcon(NIM_MODIFY);
00168 }
00169 

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