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 1563 2006-12-26 06:06:04Z 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 /** Default constructor */
00034 TrayIconImpl::TrayIconImpl()
00035 {
00036   setObjectName("trayiconimpl");
00037   _imageRef = 0;
00038   _shown    = false;
00039 }
00040 
00041 /** Destructor */
00042 TrayIconImpl::~TrayIconImpl()
00043 {
00044   if (_shown) {
00045     hide();
00046   }
00047   if (_imageRef) {
00048     CGImageRelease(_imageRef);
00049   }
00050 }
00051 
00052 /** Callback used by CGDataProviderCreateWithData(). */
00053 void
00054 TrayIconImpl::releaseCallback(void *info, const void *data, size_t size)
00055 {
00056   Q_UNUSED(info);
00057   Q_UNUSED(size);
00058   free((void*)data);
00059 }
00060 
00061 /** Load icon data from the given file and create a CGImageRef. */
00062 CGImageRef
00063 TrayIconImpl::createIconFromFile(FSSpec fileSpec)
00064 {
00065   CGDataProviderRef provider = NULL;
00066   CGImageRef image = NULL;
00067   IconFamilyHandle iconFamily;
00068   
00069   /* Load the icon from the resource bundle */
00070   if (ReadIconFile(&fileSpec, &iconFamily) == noErr) {
00071     int size = 128 * 128 * 4;
00072     Handle rawBitmapdata = NewHandle(size);
00073     GetIconFamilyData(iconFamily, kThumbnail32BitData, rawBitmapdata);
00074     
00075     Handle rawMaskdata = NewHandle(128 * 128);
00076     GetIconFamilyData(iconFamily, kThumbnail8BitMask, rawMaskdata);
00077     
00078     char *data = (char *)malloc(size);
00079     HLock(rawBitmapdata);
00080     HLock(rawMaskdata);
00081     
00082     /* copy mask data into alpha channel */
00083     const char *mask = (const char*) *rawMaskdata;
00084     const char *from = (const char*) *rawBitmapdata;
00085     char *to = data;
00086     
00087     for (int i= 0; i < 128*128; i++) {
00088       from++;
00089       *to++ = *mask++;
00090       *to++ = *from++;
00091       *to++ = *from++;
00092       *to++ = *from++;
00093     }
00094     HUnlock(rawBitmapdata);
00095     HUnlock(rawMaskdata);
00096     
00097     DisposeHandle(rawBitmapdata);
00098     DisposeHandle(rawMaskdata);
00099     
00100     provider = CGDataProviderCreateWithData(NULL, data, size, releaseCallback);
00101     CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
00102     
00103     image = CGImageCreate(128,  // width
00104                           128,  // height
00105                           8,    // Bits per component
00106                           32,   // Bits per pixel
00107                           4 * 128,  // Bytes per row
00108                           cs,
00109                           kCGImageAlphaFirst,
00110                           provider,
00111                           NULL,
00112                           0,
00113                           kCGRenderingIntentDefault);
00114     
00115     CGColorSpaceRelease(cs);
00116     CGDataProviderRelease(provider);
00117   }
00118   return image;
00119 }
00120 
00121 /** Create an icon from the given filename in the application bundle. */
00122 CGImageRef
00123 TrayIconImpl::createIcon(const QString &iconFile)
00124 {
00125   FSRef ref;
00126   CGImageRef image = NULL;
00127   
00128   /* Create a CFStringRef that we can use to build the resource URL */
00129   CFStringRef iconFileRef = CFStringCreateWithCString(NULL, qPrintable(iconFile), 
00130                                                       kCFStringEncodingASCII);
00131   if (!iconFileRef) {
00132     return NULL;
00133   }
00134   
00135   /* Build a URL to the requested .icns in our resource bundle */
00136   CFURLRef url = CFBundleCopyResourceURL(CFBundleGetMainBundle(), 
00137                                          iconFileRef, CFSTR("icns"), NULL);
00138   if (!url) {
00139     return NULL;
00140   }
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 Wed Sep 5 15:49:28 2007 for Vidalia by  doxygen 1.5.3