home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / WINTRE.ZIP / WINTREE.C next >
Text File  |  1990-04-20  |  10KB  |  325 lines

  1. /* ----------------------------------------------------------------
  2.    WINLIST -- List the Titles of all the active windows
  3.    ----------------------------------------------------------------*/
  4.  
  5. #define INCL_WIN
  6. #define INCL_GPI
  7. #include <os2.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11.  
  12. HAB hab;         /* Handle to an anchor block */
  13.  
  14. MRESULT EXPENTRY ClientWndProc(HWND, USHORT, MPARAM, MPARAM);
  15. void TraverseWindows(HWND hwnd, char **list, USHORT *index, USHORT level);
  16.  
  17. int main(void)
  18.    {
  19.    static CHAR   szClientClass[] = "Winlist";
  20.    static ULONG  flFrameFlags = FCF_TITLEBAR      |
  21.                                 FCF_SYSMENU       |
  22.                                 FCF_SIZEBORDER    |
  23.                                 FCF_MINMAX        |
  24.                                 FCF_SHELLPOSITION |
  25.                                 FCF_TASKLIST      |
  26.                                 FCF_VERTSCROLL    |
  27.                                 FCF_HORZSCROLL    ;
  28.  
  29.    HMQ           hmq;         /* Handle to a message queue */
  30.    HWND          hwndFrame,   /* Handle to the fram window */
  31.                  hwndClient;  /* Handle to the client window */
  32.    QMSG          qmsg;        /* A queued message */
  33.  
  34.    hab = WinInitialize(0);
  35.    hmq = WinCreateMsgQueue(hab,0);
  36.  
  37.    WinRegisterClass(hab,
  38.                     szClientClass,
  39.                     ClientWndProc,
  40.                     CS_SIZEREDRAW,
  41.                     0);
  42.  
  43.    hwndFrame = WinCreateStdWindow(HWND_DESKTOP,
  44.                                   WS_VISIBLE,
  45.                                   &flFrameFlags,
  46.                                   szClientClass,
  47.                                   NULL,
  48.                                   0L,
  49.                                   NULL,
  50.                                   0,
  51.                                   &hwndClient);
  52.  
  53.    WinSendMsg(hwndFrame,
  54.               WM_SETICON,
  55.               WinQuerySysPointer(HWND_DESKTOP, SPTR_APPICON, FALSE),
  56.               NULL);
  57.  
  58.    while(WinGetMsg(hab, &qmsg, NULL, 0, 0))
  59.       WinDispatchMsg(hab,&qmsg);
  60.  
  61.    WinDestroyWindow(hwndFrame);
  62.    WinDestroyMsgQueue(hmq);
  63.    WinTerminate(hab);
  64.    return 0;
  65.    } /* Main */
  66.  
  67.  
  68.  
  69. MRESULT EXPENTRY ClientWndProc(HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  70.    {
  71.    static HWND   hwndHscroll,  /* Handle to a horizontal scroll window */
  72.                  hwndVscroll;  /* Handle to a vertical scroll window */
  73.    static SHORT  sHscrollMax,  /* Horizontal scroll max */
  74.                  sVscrollMax,  /* Vertical Scroll Max */
  75.                  sHscrollPos,  /* Horizontal Scroll position */
  76.                  sVscrollPos,  /* Vertical Scroll position */
  77.                  cxChar,
  78.                  cxCaps,
  79.                  cyChar,
  80.                  cyDesc,
  81.                  cxClient,
  82.                  cyClient,
  83.                  MaxLines = 0,
  84.                  cxTextTotal;
  85.    static CHAR   *szBuffer[250];
  86.  
  87.    FONTMETRICS   fm;
  88.    HPS           hps;          /* Handle to a presentation space */
  89.    POINTL        ptl;
  90.    SHORT         sLine,
  91.                  sPaintBeg,
  92.                  sPaintEnd,
  93.                  sHscrollInc,
  94.                  sVscrollInc;
  95.    RECTL         rclInvalid;
  96.  
  97.    switch(msg)
  98.       {
  99.       case WM_CREATE:
  100.          hps = WinGetPS(hwnd);
  101.  
  102.          GpiQueryFontMetrics(hps,(LONG) sizeof fm, &fm);
  103.  
  104.          cxChar = (SHORT) fm.lAveCharWidth;
  105.          cxCaps = (SHORT) fm.lEmInc;
  106.          cyChar = (SHORT) fm.lMaxBaselineExt;
  107.          cyDesc = (SHORT) fm.lMaxDescender;
  108.  
  109.          WinReleasePS(hps);
  110.  
  111.          cxTextTotal = 80 * cxCaps;
  112.  
  113.          hwndHscroll = WinWindowFromID(
  114.                           WinQueryWindow(hwnd, QW_PARENT, FALSE),
  115.                           FID_HORZSCROLL);
  116.  
  117.          hwndVscroll = WinWindowFromID(
  118.                           WinQueryWindow(hwnd, QW_PARENT, FALSE),
  119.                           FID_VERTSCROLL);
  120.  
  121.          TraverseWindows(HWND_DESKTOP, szBuffer, &MaxLines, 0);
  122.          MaxLines--;
  123.  
  124.          return 0;
  125.  
  126.       case WM_SIZE:
  127.          cxClient = SHORT1FROMMP(mp2);
  128.          cyClient = SHORT2FROMMP(mp2);
  129.  
  130.          sHscrollMax = max(0, cxTextTotal - cxClient);
  131.          sHscrollPos = min(sHscrollPos, sHscrollMax);
  132.  
  133.          WinSendMsg(hwndHscroll,
  134.                     SBM_SETSCROLLBAR,
  135.                     MPFROM2SHORT(sHscrollPos,0),
  136.                     MPFROM2SHORT(0,sHscrollMax));
  137.  
  138.          WinEnableWindow(hwndHscroll, sHscrollMax ? TRUE : FALSE);
  139.  
  140.          sVscrollMax = max(0, MaxLines - cyClient / cyChar);
  141.          sVscrollPos = min(sVscrollPos, sVscrollMax);
  142.  
  143.          WinSendMsg(hwndVscroll,
  144.                     SBM_SETSCROLLBAR,
  145.                     MPFROM2SHORT(sVscrollPos,0),
  146.                     MPFROM2SHORT(0,sVscrollMax));
  147.  
  148.          WinEnableWindow(hwndVscroll, sVscrollMax ? TRUE : FALSE);
  149.          return 0;
  150.  
  151.       case WM_HSCROLL:
  152.          switch(SHORT2FROMMP(mp2))
  153.             {
  154.             case SB_LINELEFT:
  155.                sHscrollInc = -cxCaps;
  156.                break;
  157.             case SB_LINERIGHT:
  158.                sHscrollInc = cxCaps;
  159.                break;
  160.             case SB_PAGELEFT:
  161.                sHscrollInc = -8 * cxCaps;
  162.                break;
  163.             case SB_PAGERIGHT:
  164.                sHscrollInc = 8 * cxCaps;
  165.                break;
  166.             case SB_SLIDERPOSITION:
  167.                sHscrollInc = SHORT1FROMMP(mp2) - sHscrollPos;
  168.                break;
  169.             default:
  170.                sHscrollInc = 0;
  171.                break;
  172.             } /* switch */
  173.  
  174.          sHscrollInc = max(-sHscrollPos,
  175.                            min(sHscrollInc, sHscrollMax - sHscrollPos));
  176.  
  177.          if (sHscrollInc != 0)
  178.             {
  179.             sHscrollPos += sHscrollInc;
  180.             WinScrollWindow(hwnd, -sHscrollInc, 0, NULL, NULL, NULL, NULL,
  181.                             SW_INVALIDATERGN);
  182.  
  183.             WinSendMsg(hwndHscroll, SBM_SETPOS,
  184.                        MPFROMSHORT(sHscrollPos), NULL);
  185.             }
  186.  
  187.          return 0;
  188.  
  189.       case WM_VSCROLL:
  190.          switch(SHORT2FROMMP(mp2))
  191.             {
  192.             case SB_LINEUP:
  193.                sVscrollInc = -1;
  194.                break;
  195.             case SB_LINEDOWN:
  196.                sVscrollInc = 1;
  197.                break;
  198.             case SB_PAGEUP:
  199.                sVscrollInc = min(-1,-cyClient / cyChar);
  200.                break;
  201.             case SB_PAGEDOWN:
  202.                sVscrollInc = max(1, cyClient / cyChar);
  203.                break;
  204.             case SB_SLIDERTRACK:
  205.                sVscrollInc = SHORT1FROMMP(mp2) - sVscrollPos;
  206.                break;
  207.             default:
  208.                sVscrollInc = 0;
  209.                break;
  210.             } /* switch */
  211.  
  212.          sVscrollInc = max(-sVscrollPos,
  213.                            min(sVscrollInc, sVscrollMax - sVscrollPos));
  214.  
  215.          if (sVscrollInc != 0)
  216.             {
  217.             sVscrollPos += sVscrollInc;
  218.             WinScrollWindow(hwnd, 0, cyChar * sVscrollInc, NULL, NULL, NULL,
  219.                             NULL, SW_INVALIDATERGN);
  220.             WinSendMsg(hwndVscroll, SBM_SETPOS,
  221.                        MPFROMSHORT(sVscrollPos), NULL);
  222.  
  223.             WinUpdateWindow(hwnd);
  224.             }
  225.  
  226.          return 0;
  227.  
  228.       case WM_CHAR:
  229.          switch(CHARMSG(&msg)->vkey)
  230.             {
  231.             case VK_LEFT:
  232.             case VK_RIGHT:
  233.                return WinSendMsg(hwndHscroll, msg, mp1, mp2);
  234.             case VK_UP:
  235.             case VK_DOWN:
  236.             case VK_PAGEUP:
  237.             case VK_PAGEDOWN:
  238.                return WinSendMsg(hwndVscroll, msg, mp1, mp2);
  239.             }
  240.          break;
  241.  
  242.       case WM_PAINT:
  243.          hps = WinBeginPaint(hwnd, NULL, &rclInvalid);
  244.          GpiErase(hps);
  245.          sPaintBeg = max(0, sVscrollPos +
  246.                             (cyClient - (SHORT) rclInvalid.yTop) / cyChar);
  247.          sPaintEnd = min(MaxLines, sVscrollPos +
  248.                             (cyClient - (SHORT) rclInvalid.yBottom) /
  249.                              cyChar + 1);
  250.  
  251.          for(sLine = sPaintBeg; sLine < sPaintEnd; sLine++)
  252.             {
  253.             ptl.x = cxCaps - sHscrollPos;
  254.             ptl.y = cyClient - cyChar * (sLine + 1 - sVscrollPos) + cyDesc;
  255.  
  256.             GpiCharStringAt(hps, &ptl,
  257.                             (LONG) strlen(szBuffer[sLine]),
  258.                             szBuffer[sLine]);
  259.             }
  260.  
  261.          WinEndPaint(hps);
  262.          return 0;
  263.       }
  264.    return WinDefWindowProc(hwnd, msg, mp1, mp2);
  265.    }
  266.  
  267.  
  268.  
  269. void TraverseWindows(HWND hwnd, char **list, USHORT *index, USHORT level)
  270.    {
  271.    HWND   hwndNext;     /* Handle to a window frame */
  272.    HENUM  hEnum;        /* Handle to an enumeration list */
  273.    CHAR   szTemp[80];   /* Temp char buffer */
  274.    USHORT i;
  275.  
  276.    /* Enumerate the Windows on the desktop */
  277.  
  278.    hEnum = WinBeginEnumWindows(hwnd);
  279.    while ((hwndNext = WinGetNextWindow(hEnum)) != NULL)
  280.       {
  281.  
  282.       /* Unlock the Window */
  283.  
  284.       WinLockWindow(hwndNext,FALSE);
  285.  
  286.       /* Get the name of the window */
  287.  
  288.       if ((list[*index] = calloc(1,80)) == NULL)
  289.          WinMessageBox(HWND_DESKTOP, hwnd,
  290.                        "Unable to allocate buffer",
  291.                        "Window List", 0, MB_OK | MB_ICONEXCLAMATION);
  292.  
  293.       /* Get the title of the window */
  294.  
  295.       for (i = 0; i < level; i++)
  296.          strcat(list[*index],"─");
  297.  
  298.       WinQueryWindowText(hwndNext,80,szTemp);
  299.       if (!strlen(szTemp))
  300.          strcpy(szTemp,"[No Title]");
  301.  
  302.       strcat(list[*index],szTemp);
  303.  
  304.       /* Append the handle to the window */
  305.  
  306.       sprintf(szTemp,"....%x:%x",SELECTOROF(hwndNext),OFFSETOF(hwndNext));
  307.       strcat(list[*index],szTemp);
  308.  
  309.       /* Append the class name of the window */
  310.  
  311.       WinQueryClassName(hwndNext,80,szTemp);
  312.       strcat(list[*index],"....");
  313.       strcat(list[*index],szTemp);
  314.  
  315.       /* Go find the children */
  316.  
  317.       (*index)++;
  318.       level += 3;
  319.       TraverseWindows(hwndNext, list, index, level);
  320.       level -= 3;
  321.  
  322.       } /* end While */
  323.    WinEndEnumWindows(hEnum);
  324.    }
  325.