trayicon_mac.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_mac.cpp
00024  * \version $Id: trayicon_mac.cpp 1238 2006-09-25 17:50:57Z edmanm $
00025  * \brief Tray icon implementation on OS X (Dock icon)
00026  */
00027 
00028 #include <QApplication>
00029 
00030 #include "trayicon_mac.h"
00031 
00032 
00033 /** Constructor. */
00034 TrayIconImpl::TrayIconImpl(const QString &iconFile, const QString &toolTip)
00035 {
00036   setObjectName("trayiconimpl");
00037   _imageRef = 0;
00038   _shown    = false;
00039 
00040   /* Add the tool tip to the structure */
00041   setIcon(iconFile);
00042 
00043   /* And give it an icon */
00044   setToolTip(toolTip);
00045 }
00046 
00047 /** Destructor */
00048 TrayIconImpl::~TrayIconImpl()
00049 {
00050   if (_shown) {
00051     hide();
00052   }
00053   if (_imageRef) {
00054     CGImageRelease(_imageRef);
00055   }
00056 }
00057 
00058 /** Callback used by CGDataProviderCreateWithData(). */
00059 void
00060 TrayIconImpl::releaseCallback(void *info, const void *data, size_t size)
00061 {
00062   Q_UNUSED(info);
00063   Q_UNUSED(size);
00064   free((void*)data);
00065 }
00066 
00067 /** Load icon data from the given file and create a CGImageRef. */
00068 CGImageRef
00069 TrayIconImpl::createIconFromFile(FSSpec fileSpec)
00070 {
00071   CGDataProviderRef provider = NULL;
00072   CGImageRef image = NULL;
00073   IconFamilyHandle iconFamily;
00074   
00075   /* Load the icon from the resource bundle */
00076   if (ReadIconFile(&fileSpec, &iconFamily) == noErr) {
00077     int size = 128 * 128 * 4;
00078     Handle rawBitmapdata = NewHandle(size);
00079     GetIconFamilyData(iconFamily, kThumbnail32BitData, rawBitmapdata);
00080     
00081     Handle rawMaskdata = NewHandle(128 * 128);
00082     GetIconFamilyData(iconFamily, kThumbnail8BitMask, rawMaskdata);
00083     
00084     char *data = (char *)malloc(size);
00085     HLock(rawBitmapdata);
00086     HLock(rawMaskdata);
00087     
00088     /* copy mask data into alpha channel */
00089     const char *mask = (const char*) *rawMaskdata;
00090     const char *from = (const char*) *rawBitmapdata;
00091     char *to = data;
00092     
00093     for (int i= 0; i < 128*128; i++) {
00094       from++;
00095       *to++ = *mask++;
00096       *to++ = *from++;
00097       *to++ = *from++;
00098       *to++ = *from++;
00099     }
00100     HUnlock(rawBitmapdata);
00101     HUnlock(rawMaskdata);
00102     
00103     DisposeHandle(rawBitmapdata);
00104     DisposeHandle(rawMaskdata);
00105     
00106     provider = CGDataProviderCreateWithData(NULL, data, size, releaseCallback);
00107     CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
00108     
00109     image = CGImageCreate(128,  // width
00110                           128,  // height
00111                           8,    // Bits per component
00112                           32,   // Bits per pixel
00113                           4 * 128,  // Bytes per row
00114                           cs,
00115                           kCGImageAlphaFirst,
00116                           provider,
00117                           NULL,
00118                           0,
00119                           kCGRenderingIntentDefault);
00120     
00121     CGColorSpaceRelease(cs);
00122     CGDataProviderRelease(provider);
00123   }
00124   return image;
00125 }
00126 
00127 /** Create an icon from the given filename in the application bundle. */
00128 CGImageRef
00129 TrayIconImpl::createIcon(const QString &iconFile)
00130 {
00131   FSRef ref;
00132   CGImageRef image = NULL;
00133   
00134   /* Create a CFStringRef that we can use to build the resource URL */
00135   CFStringRef iconFileRef = CFStringCreateWithCString(NULL, qPrintable(iconFile), 
00136                                                       kCFStringEncodingASCII);
00137   
00138   /* Build a URL to the requested .icns in our resource bundle */
00139   CFURLRef url = CFBundleCopyResourceURL(CFBundleGetMainBundle(), 
00140                                          iconFileRef, CFSTR("icns"), NULL);
00141   
00142   /* Try to find the resource in the bundle */
00143   if (CFURLGetFSRef(url, &ref)) {
00144     FSSpec fileSpec;
00145     if (FSGetCatalogInfo(&ref, kFSCatInfoNone, NULL, 
00146                          NULL, &fileSpec, NULL) == noErr) {
00147       /* Found it, now create the icon from it */
00148       image = createIconFromFile(fileSpec);
00149     }
00150   }
00151   CFRelease(iconFileRef);
00152   CFRelease(url);
00153   return image;  
00154 }
00155 
00156 /** Show the tray icon image. */
00157 void
00158 TrayIconImpl::show()
00159 {
00160   if (_imageRef) {
00161     CGContextRef ctxRef = BeginCGContextForApplicationDockTile();
00162     if (!ctxRef) {
00163       return;
00164     }
00165     SetApplicationDockTileImage(_imageRef);
00166     EndCGContextForApplicationDockTile(ctxRef);
00167     
00168     _shown = true;
00169   }  
00170 }
00171 
00172 /** Hide the tray icon image. */
00173 void
00174 TrayIconImpl::hide()
00175 {
00176   _shown = false;
00177   
00178   CGContextRef ctxRef = BeginCGContextForApplicationDockTile();
00179   if (!ctxRef) {
00180     return;
00181   }
00182   RestoreApplicationDockTileImage();
00183   EndCGContextForApplicationDockTile(ctxRef);
00184 }
00185 
00186 /** Set the tray icon's tooltip. */
00187 void
00188 TrayIconImpl::setToolTip(const QString &toolTip)
00189 {
00190   /* The dock icon doesn't have a tooltip. */
00191   Q_UNUSED(toolTip);
00192 }
00193 
00194 /** Set the tray icon's image. */
00195 void
00196 TrayIconImpl::setIcon(const QString &iconFile)
00197 {
00198   /* If there was an image set previously, free it before getting the new one */
00199   if (_imageRef) {
00200     CGImageRelease(_imageRef);
00201     _imageRef = 0;
00202   }
00203   
00204   /* Load the icon data from the application bundle */
00205   _imageRef = createIcon(iconFile);
00206   
00207   /* If the icon is to be shown, put it in the dock now. */
00208   if (_imageRef && _shown) {
00209     show();
00210   }
00211 }
00212 

Generated on Mon Oct 23 20:08:16 2006 for Vidalia by  doxygen 1.5.0