00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <QApplication>
00029
00030 #include "trayicon_mac.h"
00031
00032
00033
00034 TrayIconImpl::TrayIconImpl(const QString &iconFile, const QString &toolTip)
00035 {
00036 setObjectName("trayiconimpl");
00037 _imageRef = 0;
00038 _shown = false;
00039
00040
00041 setIcon(iconFile);
00042
00043
00044 setToolTip(toolTip);
00045 }
00046
00047
00048 TrayIconImpl::~TrayIconImpl()
00049 {
00050 if (_shown) {
00051 hide();
00052 }
00053 if (_imageRef) {
00054 CGImageRelease(_imageRef);
00055 }
00056 }
00057
00058
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
00068 CGImageRef
00069 TrayIconImpl::createIconFromFile(FSSpec fileSpec)
00070 {
00071 CGDataProviderRef provider = NULL;
00072 CGImageRef image = NULL;
00073 IconFamilyHandle iconFamily;
00074
00075
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
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,
00110 128,
00111 8,
00112 32,
00113 4 * 128,
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
00128 CGImageRef
00129 TrayIconImpl::createIcon(const QString &iconFile)
00130 {
00131 FSRef ref;
00132 CGImageRef image = NULL;
00133
00134
00135 CFStringRef iconFileRef = CFStringCreateWithCString(NULL, qPrintable(iconFile),
00136 kCFStringEncodingASCII);
00137
00138
00139 CFURLRef url = CFBundleCopyResourceURL(CFBundleGetMainBundle(),
00140 iconFileRef, CFSTR("icns"), NULL);
00141
00142
00143 if (CFURLGetFSRef(url, &ref)) {
00144 FSSpec fileSpec;
00145 if (FSGetCatalogInfo(&ref, kFSCatInfoNone, NULL,
00146 NULL, &fileSpec, NULL) == noErr) {
00147
00148 image = createIconFromFile(fileSpec);
00149 }
00150 }
00151 CFRelease(iconFileRef);
00152 CFRelease(url);
00153 return image;
00154 }
00155
00156
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
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
00187 void
00188 TrayIconImpl::setToolTip(const QString &toolTip)
00189 {
00190
00191 Q_UNUSED(toolTip);
00192 }
00193
00194
00195 void
00196 TrayIconImpl::setIcon(const QString &iconFile)
00197 {
00198
00199 if (_imageRef) {
00200 CGImageRelease(_imageRef);
00201 _imageRef = 0;
00202 }
00203
00204
00205 _imageRef = createIcon(iconFile);
00206
00207
00208 if (_imageRef && _shown) {
00209 show();
00210 }
00211 }
00212