Sync with trunk (r48545)
[reactos.git] / base / applications / taskmgr / applpage.c
index 3ef6ee9..88c5207 100644 (file)
@@ -38,7 +38,6 @@ HWND            hApplicationPageSwitchToButton; /* Application Switch To button
 HWND            hApplicationPageNewTaskButton;  /* Application New Task button */
 static int      nApplicationPageWidth;
 static int      nApplicationPageHeight;
-static HANDLE   hApplicationPageEvent = NULL;   /* When this event becomes signaled then we refresh the app list */
 static BOOL     bSortAscending = TRUE;
 DWORD WINAPI    ApplicationPageRefreshThread(void *lpParameter);
 BOOL            noApps;
@@ -49,7 +48,12 @@ void            ApplicationPageOnNotify(WPARAM wParam, LPARAM lParam);
 void            ApplicationPageShowContextMenu1(void);
 void            ApplicationPageShowContextMenu2(void);
 int CALLBACK    ApplicationPageCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);
-int             PerfGetIndexByProcessId(DWORD dwProcessId);
+int             ProcGetIndexByProcessId(DWORD dwProcessId);
+
+#ifdef RUN_APPS_PAGE
+static HANDLE   hApplicationThread = NULL;
+static DWORD    dwApplicationThread;
+#endif
 
 #if 0
 void SwitchToThisWindow (
@@ -92,7 +96,6 @@ ApplicationPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
     LV_COLUMN  column;
     WCHAR      szTemp[256];
     int        cx, cy;
-    HANDLE     hRefreshThread = NULL;
 
     switch (message) {
     case WM_INITDIALOG:
@@ -132,15 +135,16 @@ ApplicationPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
         UpdateApplicationListControlViewSetting();
 
         /* Start our refresh thread */
-        hRefreshThread = CreateThread(NULL, 0, ApplicationPageRefreshThread, NULL, 0, NULL);
-
+#ifdef RUN_APPS_PAGE
+        hApplicationThread = CreateThread(NULL, 0, ApplicationPageRefreshThread, NULL, 0, &dwApplicationThread);
+#endif
         return TRUE;
 
     case WM_DESTROY:
-        /* Close the event handle, this will make the */
-        /* refresh thread exit when the wait fails */
-        CloseHandle(hApplicationPageEvent);
-        CloseHandle(hRefreshThread);
+        /* Close refresh thread */
+#ifdef RUN_APPS_PAGE
+        EndLocalThread(&hApplicationThread, dwApplicationThread);
+#endif
         break;
 
     case WM_COMMAND:
@@ -213,9 +217,11 @@ ApplicationPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
 
 void RefreshApplicationPage(void)
 {
+#ifdef RUN_APPS_PAGE
     /* Signal the event so that our refresh thread */
     /* will wake up and refresh the application page */
-    SetEvent(hApplicationPageEvent);
+    PostThreadMessage(dwApplicationThread, WM_TIMER, 0, 0);
+#endif
 }
 
 void UpdateApplicationListControlViewSetting(void)
@@ -236,30 +242,23 @@ void UpdateApplicationListControlViewSetting(void)
 
 DWORD WINAPI ApplicationPageRefreshThread(void *lpParameter)
 {
-    /* Create the event */
-    hApplicationPageEvent = CreateEventW(NULL, TRUE, TRUE, NULL);
+    MSG msg;
+    INT i;
+    BOOL                            bItemRemoved = FALSE;
+    LV_ITEM                         item;
+    LPAPPLICATION_PAGE_LIST_ITEM    pAPLI = NULL;
+    HIMAGELIST                      hImageListLarge;
+    HIMAGELIST                      hImageListSmall;
 
     /* If we couldn't create the event then exit the thread */
-    if (!hApplicationPageEvent)
-        return 0;
-
     while (1)
     {
-        DWORD   dwWaitVal;
-
-        /* Wait on the event */
-        dwWaitVal = WaitForSingleObject(hApplicationPageEvent, INFINITE);
-
-        /* If the wait failed then the event object must have been */
-        /* closed and the task manager is exiting so exit this thread */
-        if (dwWaitVal == WAIT_FAILED)
+        /*  Wait for an the event or application close */
+        if (GetMessage(&msg, NULL, 0, 0) <= 0)
             return 0;
 
-        if (dwWaitVal == WAIT_OBJECT_0)
+        if (msg.message == WM_TIMER)
         {
-            /* Reset our event */
-            ResetEvent(hApplicationPageEvent);
-
             /*
              * FIXME:
              *
@@ -269,6 +268,55 @@ DWORD WINAPI ApplicationPageRefreshThread(void *lpParameter)
             EnumWindows(EnumWindowsProc, 0);
             if (noApps)
                 (void)ListView_DeleteAllItems(hApplicationPageListCtrl);
+
+            /* Get the image lists */
+            hImageListLarge = ListView_GetImageList(hApplicationPageListCtrl, LVSIL_NORMAL);
+            hImageListSmall = ListView_GetImageList(hApplicationPageListCtrl, LVSIL_SMALL);
+
+            /* Check to see if we need to remove any items from the list */
+            for (i=ListView_GetItemCount(hApplicationPageListCtrl)-1; i>=0; i--)
+            {
+                memset(&item, 0, sizeof(LV_ITEM));
+                item.mask = LVIF_IMAGE|LVIF_PARAM;
+                item.iItem = i;
+                (void)ListView_GetItem(hApplicationPageListCtrl, &item);
+
+                pAPLI = (LPAPPLICATION_PAGE_LIST_ITEM)item.lParam;
+                if (!IsWindow(pAPLI->hWnd)||
+                    (wcslen(pAPLI->szTitle) <= 0) ||
+                    !IsWindowVisible(pAPLI->hWnd) ||
+                    (GetParent(pAPLI->hWnd) != NULL) ||
+                    (GetWindow(pAPLI->hWnd, GW_OWNER) != NULL) ||
+                    (GetWindowLongPtr(pAPLI->hWnd, GWL_EXSTYLE) & WS_EX_TOOLWINDOW))
+                {
+                    ImageList_Remove(hImageListLarge, item.iItem);
+                    ImageList_Remove(hImageListSmall, item.iItem);
+
+                    (void)ListView_DeleteItem(hApplicationPageListCtrl, item.iItem);
+                    HeapFree(GetProcessHeap(), 0, pAPLI);
+                    bItemRemoved = TRUE;
+                }
+            }
+
+            /*
+             * If an item was removed from the list then
+             * we need to resync all the items with the
+             * image list
+             */
+            if (bItemRemoved)
+            {
+                for (i=0; i<ListView_GetItemCount(hApplicationPageListCtrl); i++)
+                {
+                    memset(&item, 0, sizeof(LV_ITEM));
+                    item.mask = LVIF_IMAGE;
+                    item.iItem = i;
+                    item.iImage = i;
+                    (void)ListView_SetItem(hApplicationPageListCtrl, &item);
+                }
+                bItemRemoved = FALSE;
+            }
+
+            ApplicationPageUpdate();
         }
     }
 }
@@ -333,13 +381,12 @@ BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
 
 void AddOrUpdateHwnd(HWND hWnd, WCHAR *szTitle, HICON hIcon, BOOL bHung)
 {
-    LPAPPLICATION_PAGE_LIST_ITEM  pAPLI = NULL;
-    HIMAGELIST                    hImageListLarge;
-    HIMAGELIST                    hImageListSmall;
-    LV_ITEM                       item;
-    int                           i;
-    BOOL                          bAlreadyInList = FALSE;
-    BOOL                          bItemRemoved = FALSE;
+    LPAPPLICATION_PAGE_LIST_ITEM    pAPLI = NULL;
+    HIMAGELIST                      hImageListLarge;
+    HIMAGELIST                      hImageListSmall;
+    LV_ITEM                         item;
+    int                             i;
+    BOOL                            bAlreadyInList = FALSE;
 
     memset(&item, 0, sizeof(LV_ITEM));
 
@@ -406,51 +453,7 @@ void AddOrUpdateHwnd(HWND hWnd, WCHAR *szTitle, HICON hIcon, BOOL bHung)
         item.lParam = (LPARAM)pAPLI;
         (void)ListView_InsertItem(hApplicationPageListCtrl, &item);
     }
-
-
-    /* Check to see if we need to remove any items from the list */
-    for (i=ListView_GetItemCount(hApplicationPageListCtrl)-1; i>=0; i--)
-    {
-        memset(&item, 0, sizeof(LV_ITEM));
-        item.mask = LVIF_IMAGE|LVIF_PARAM;
-        item.iItem = i;
-        (void)ListView_GetItem(hApplicationPageListCtrl, &item);
-
-        pAPLI = (LPAPPLICATION_PAGE_LIST_ITEM)item.lParam;
-        if (!IsWindow(pAPLI->hWnd)||
-            (wcslen(pAPLI->szTitle) <= 0) ||
-            !IsWindowVisible(pAPLI->hWnd) ||
-            (GetParent(pAPLI->hWnd) != NULL) ||
-            (GetWindow(pAPLI->hWnd, GW_OWNER) != NULL) ||
-            (GetWindowLongPtrW(hWnd, GWL_EXSTYLE) & WS_EX_TOOLWINDOW))
-        {
-            ImageList_Remove(hImageListLarge, item.iItem);
-            ImageList_Remove(hImageListSmall, item.iItem);
-
-            (void)ListView_DeleteItem(hApplicationPageListCtrl, item.iItem);
-            HeapFree(GetProcessHeap(), 0, pAPLI);
-            bItemRemoved = TRUE;
-        }
-    }
-
-    /*
-     * If an item was removed from the list then
-     * we need to resync all the items with the
-     * image list
-     */
-    if (bItemRemoved)
-    {
-        for (i=0; i<ListView_GetItemCount(hApplicationPageListCtrl); i++)
-        {
-            memset(&item, 0, sizeof(LV_ITEM));
-            item.mask = LVIF_IMAGE;
-            item.iItem = i;
-            item.iImage = i;
-            (void)ListView_SetItem(hApplicationPageListCtrl, &item);
-        }
-    }
-
-    ApplicationPageUpdate();
+    return;
 }
 
 void ApplicationPageUpdate(void)
@@ -878,7 +881,6 @@ void ApplicationPage_OnGotoProcess(void)
     LPAPPLICATION_PAGE_LIST_ITEM  pAPLI = NULL;
     LV_ITEM                       item;
     int                           i;
-    /* NMHDR nmhdr; */
 
     for (i=0; i<ListView_GetItemCount(hApplicationPageListCtrl); i++) {
         memset(&item, 0, sizeof(LV_ITEM));
@@ -902,7 +904,7 @@ void ApplicationPage_OnGotoProcess(void)
         /*
          * Select the process item in the list
          */
-        i = PerfGetIndexByProcessId(dwProcessId);
+        i = ProcGetIndexByProcessId(dwProcessId);
         if (i != -1)
         {
             ListView_SetItemState(hProcessPageListCtrl,