src/gui/util/qsystemtrayicon_win.cpp
changeset 25 e24348a560a6
parent 18 2f34d5167611
child 30 5dc02b23752f
equal deleted inserted replaced
23:89e065397ea6 25:e24348a560a6
    59 #include <QApplication>
    59 #include <QApplication>
    60 #include <QToolTip>
    60 #include <QToolTip>
    61 #include <QDesktopWidget>
    61 #include <QDesktopWidget>
    62 #include <QSettings>
    62 #include <QSettings>
    63 
    63 
    64 #if defined(Q_WS_WINCE) && !defined(STANDARDSHELL_UI_MODEL)
       
    65 #   include <streams.h>
       
    66 #endif
       
    67 
       
    68 QT_BEGIN_NAMESPACE
    64 QT_BEGIN_NAMESPACE
    69 
    65 
    70 #if defined(Q_WS_WINCE)
       
    71 static const UINT q_uNOTIFYICONID = 13;     // IDs from 0 to 12 are reserved on WinCE.
       
    72 #else
       
    73 static const UINT q_uNOTIFYICONID = 0;
    66 static const UINT q_uNOTIFYICONID = 0;
    74 #endif
       
    75 
    67 
    76 static uint MYWM_TASKBARCREATED = 0;
    68 static uint MYWM_TASKBARCREATED = 0;
    77 #define MYWM_NOTIFYICON (WM_APP+101)
    69 #define MYWM_NOTIFYICON (WM_APP+101)
    78 
    70 
    79 struct Q_NOTIFYICONIDENTIFIER {
    71 struct Q_NOTIFYICONIDENTIFIER {
   123 #endif
   115 #endif
   124 }
   116 }
   125 
   117 
   126 bool QSystemTrayIconSys::supportsMessages()
   118 bool QSystemTrayIconSys::supportsMessages()
   127 {
   119 {
   128 #ifndef Q_OS_WINCE
       
   129     return allowsMessages();
   120     return allowsMessages();
   130 #endif
       
   131     return false;
       
   132 }
   121 }
   133 
   122 
   134 QSystemTrayIconSys::QSystemTrayIconSys(QSystemTrayIcon *object)
   123 QSystemTrayIconSys::QSystemTrayIconSys(QSystemTrayIcon *object)
   135     : hIcon(0), q(object), ignoreNextMouseRelease(false)
   124     : hIcon(0), q(object), ignoreNextMouseRelease(false)
   136 
   125 
   137 {
   126 {
   138 #ifndef Q_OS_WINCE
       
   139     notifyIconSize = FIELD_OFFSET(NOTIFYICONDATA, guidItem); // NOTIFYICONDATAW_V2_SIZE;
   127     notifyIconSize = FIELD_OFFSET(NOTIFYICONDATA, guidItem); // NOTIFYICONDATAW_V2_SIZE;
   140     maxTipLength = 128;
   128     maxTipLength = 128;
   141 #else
       
   142     notifyIconSize = FIELD_OFFSET(NOTIFYICONDATA, szTip[64]); // NOTIFYICONDATAW_V1_SIZE;
       
   143     maxTipLength = 64;
       
   144 #endif
       
   145 
   129 
   146     // For restoring the tray icon after explorer crashes
   130     // For restoring the tray icon after explorer crashes
   147     if (!MYWM_TASKBARCREATED) {
   131     if (!MYWM_TASKBARCREATED) {
   148         MYWM_TASKBARCREATED = RegisterWindowMessage(L"TaskbarCreated");
   132         MYWM_TASKBARCREATED = RegisterWindowMessage(L"TaskbarCreated");
   149     }
   133     }
   315                 break;
   299                 break;
   316 
   300 
   317             case WM_RBUTTONUP:
   301             case WM_RBUTTONUP:
   318                 if (q->contextMenu()) {
   302                 if (q->contextMenu()) {
   319                     q->contextMenu()->popup(gpos);
   303                     q->contextMenu()->popup(gpos);
   320 #if defined(Q_WS_WINCE)
       
   321                     // We must ensure that the popup menu doesn't show up behind the task bar.
       
   322                     QRect desktopRect = qApp->desktop()->availableGeometry();
       
   323                     int maxY = desktopRect.y() + desktopRect.height() - q->contextMenu()->height();
       
   324                     if (gpos.y() > maxY) {
       
   325                         gpos.ry() = maxY;
       
   326                         q->contextMenu()->move(gpos);
       
   327                     }
       
   328 #endif
       
   329                     q->contextMenu()->activateWindow();
   304                     q->contextMenu()->activateWindow();
   330                     //Must be activated for proper keyboardfocus and menu closing on windows:
   305                     //Must be activated for proper keyboardfocus and menu closing on windows:
   331                 }
   306                 }
   332                 emit q->activated(QSystemTrayIcon::Context);
   307                 emit q->activated(QSystemTrayIcon::Context);
   333                 break;
   308                 break;
   334 
   309 
   335 #if !defined(Q_WS_WINCE)
       
   336             case NIN_BALLOONUSERCLICK:
   310             case NIN_BALLOONUSERCLICK:
   337                 emit q->messageClicked();
   311                 emit q->messageClicked();
   338                 break;
   312                 break;
   339 #endif
       
   340 
   313 
   341             case WM_MBUTTONUP:
   314             case WM_MBUTTONUP:
   342                 emit q->activated(QSystemTrayIcon::MiddleClick);
   315                 emit q->activated(QSystemTrayIcon::MiddleClick);
   343                 break;
   316                 break;
   344             default:
   317             default:
   401     DWORD processID = 0;
   374     DWORD processID = 0;
   402     HWND trayHandle = FindWindow(L"Shell_TrayWnd", NULL);
   375     HWND trayHandle = FindWindow(L"Shell_TrayWnd", NULL);
   403 
   376 
   404     //find the toolbar used in the notification area
   377     //find the toolbar used in the notification area
   405     if (trayHandle) {
   378     if (trayHandle) {
   406 #if defined(Q_OS_WINCE)
       
   407         trayHandle = FindWindow(L"TrayNotifyWnd", NULL);
       
   408 #else
       
   409         trayHandle = FindWindowEx(trayHandle, NULL, L"TrayNotifyWnd", NULL);
   379         trayHandle = FindWindowEx(trayHandle, NULL, L"TrayNotifyWnd", NULL);
   410 #endif
       
   411         if (trayHandle) {
   380         if (trayHandle) {
   412 #if defined(Q_OS_WINCE)
       
   413             HWND hwnd = FindWindow(L"SysPager", NULL);
       
   414 #else
       
   415             HWND hwnd = FindWindowEx(trayHandle, NULL, L"SysPager", NULL);
   381             HWND hwnd = FindWindowEx(trayHandle, NULL, L"SysPager", NULL);
   416 #endif
       
   417             if (hwnd) {
   382             if (hwnd) {
   418 #if defined(Q_OS_WINCE)
       
   419                 hwnd = FindWindow(L"ToolbarWindow32", NULL);
       
   420 #else
       
   421                 hwnd = FindWindowEx(hwnd, NULL, L"ToolbarWindow32", NULL);
   383                 hwnd = FindWindowEx(hwnd, NULL, L"ToolbarWindow32", NULL);
   422 #endif
       
   423                 if (hwnd)
   384                 if (hwnd)
   424                     trayHandle = hwnd;
   385                     trayHandle = hwnd;
   425             }
   386             }
   426         }
   387         }
   427     }
   388     }
   436     HANDLE trayProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ, 0, processID);
   397     HANDLE trayProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ, 0, processID);
   437     if (!trayProcess)
   398     if (!trayProcess)
   438         return ret;
   399         return ret;
   439 
   400 
   440     int buttonCount = SendMessage(trayHandle, TB_BUTTONCOUNT, 0, 0);
   401     int buttonCount = SendMessage(trayHandle, TB_BUTTONCOUNT, 0, 0);
   441 #if defined(Q_OS_WINCE)
       
   442     LPVOID data = VirtualAlloc(NULL, sizeof(TBBUTTON), MEM_COMMIT, PAGE_READWRITE);
       
   443 #else
       
   444     LPVOID data = VirtualAllocEx(trayProcess, NULL, sizeof(TBBUTTON), MEM_COMMIT, PAGE_READWRITE);
   402     LPVOID data = VirtualAllocEx(trayProcess, NULL, sizeof(TBBUTTON), MEM_COMMIT, PAGE_READWRITE);
   445 #endif
       
   446 
   403 
   447     if ( buttonCount < 1 || !data ) {
   404     if ( buttonCount < 1 || !data ) {
   448         CloseHandle(trayProcess);
   405         CloseHandle(trayProcess);
   449         return ret;
   406         return ret;
   450     }
   407     }
   478                     ret = geometry;
   435                     ret = geometry;
   479                 break;
   436                 break;
   480             }
   437             }
   481         }
   438         }
   482     }
   439     }
   483 #if defined(Q_OS_WINCE)
       
   484     VirtualFree(data, 0, MEM_RELEASE);
       
   485 #else
       
   486     VirtualFreeEx(trayProcess, data, 0, MEM_RELEASE);
   440     VirtualFreeEx(trayProcess, data, 0, MEM_RELEASE);
   487 #endif
       
   488     CloseHandle(trayProcess);
   441     CloseHandle(trayProcess);
   489     return ret;
   442     return ret;
   490 }
   443 }
   491 
   444 
   492 void QSystemTrayIconPrivate::showMessage_sys(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon type, int timeOut)
   445 void QSystemTrayIconPrivate::showMessage_sys(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon type, int timeOut)
   556 
   509 
   557 }
   510 }
   558 
   511 
   559 void QSystemTrayIconPrivate::updateToolTip_sys()
   512 void QSystemTrayIconPrivate::updateToolTip_sys()
   560 {
   513 {
   561 #ifdef Q_WS_WINCE
       
   562     // Calling sys->trayMessage(NIM_MODIFY) on an existing icon is broken on Windows CE.
       
   563     // So we need to call updateIcon_sys() which creates a new icon handle.
       
   564     updateIcon_sys();
       
   565 #else
       
   566     if (!sys)
   514     if (!sys)
   567         return;
   515         return;
   568 
   516 
   569     sys->trayMessage(NIM_MODIFY);
   517     sys->trayMessage(NIM_MODIFY);
   570 #endif
       
   571 }
   518 }
   572 
   519 
   573 bool QSystemTrayIconPrivate::isSystemTrayAvailable_sys()
   520 bool QSystemTrayIconPrivate::isSystemTrayAvailable_sys()
   574 {
   521 {
   575     return true;
   522     return true;