home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / primcuts.zip / AutoResizeClientControls / AutoResizeClientControls.cpp next >
Text File  |  1998-05-03  |  5KB  |  183 lines

  1. #define INCL_WINWINDOWMGR
  2. #define INCL_DOSPROCESS
  3.  
  4. #include <os2.h>
  5.  
  6. #include <string.h>                      // memcpy()
  7.  
  8. #include "AutoResizeClientControls.h"
  9.  
  10.  
  11. // doublelinkedlist structure
  12. typedef struct _WNDLIST
  13. {
  14.    HWND           hWnd;
  15.    RELWNDPOS      pos;
  16.    struct _WNDLIST *next;
  17.    struct _WNDLIST *prev;
  18. }WNDLIST, *PWNDLIST;
  19.  
  20. // extra windowdata
  21. #define QWP_WNDLIST                      0
  22. #define QWP_CURRENT                      QWP_WNDLIST+sizeof(PWNDLIST)
  23. #define QW_EXTRA                         QWP_CURRENT+sizeof(PWNDLIST)
  24.  
  25. // function prototypes
  26. MRESULT EXPENTRY AutoSizeClientProc(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2);
  27. HWND AddControl(HWND hWnd, MPARAM mp1);
  28. void ascpSize(HWND hWnd);
  29. void ascpDestroy(HWND hWnd);
  30.  
  31.  
  32. BOOL ascRegisterClass(HAB hAB)
  33. {
  34.    return WinRegisterClass(hAB, WC_AUTOSIZECLIENT, AutoSizeClientProc, CS_SIZEREDRAW, QW_EXTRA);
  35. }
  36.  
  37. MRESULT EXPENTRY AutoSizeClientProc(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  38. {
  39.    MRESULT  mReturn = 0;
  40.    BOOL     fHandled = TRUE;
  41.  
  42.    switch(msg)
  43.    {
  44.       case WM_CREATE:
  45.          WinSetWindowPtr(hWnd, QWP_WNDLIST, 0);
  46.          WinSetWindowPtr(hWnd, QWP_CURRENT, 0);
  47.          break;
  48.  
  49.       case WM_PAINT:
  50.          {
  51.             RECTL rect;
  52.             HPS   hPS = WinBeginPaint(hWnd, NULLHANDLE, &rect);
  53.             WinFillRect(hPS, &rect, CLR_PALEGRAY);
  54.             WinEndPaint(hPS);
  55.          }
  56.          break;
  57.  
  58.       case WM_SIZE:
  59.          ascpSize(hWnd);
  60.          break;
  61.  
  62.       case WM_CLOSE:
  63.          ascpDestroy(hWnd);
  64.          WinPostMsg(hWnd, WM_QUIT, NULL, NULL);
  65.          break;
  66.  
  67.       case WM_DESTROY:
  68.          ascpDestroy(hWnd);
  69.          break;
  70.  
  71.       case WMU_ARCC_ADD:
  72.          return (MRESULT)AddControl(hWnd, mp1);
  73.  
  74.       default:
  75.          fHandled = FALSE;
  76.          break;
  77.    }
  78.    if(!fHandled)
  79.       mReturn = WinDefWindowProc(hWnd, msg, mp1, mp2);
  80.  
  81.    return mReturn;
  82. }
  83.  
  84. HWND AddControl(HWND hWnd, MPARAM mp1)
  85. {
  86.    HWND        hCntrl = NULLHANDLE;
  87.    PWNDLIST    pList = NULL;
  88.    PWNDLIST    pCurrent = NULL;
  89.    PCREATEWND  pCreate = (PCREATEWND)mp1;
  90.    RECTL       rect;
  91.  
  92.    pList = (PWNDLIST)WinQueryWindowPtr(hWnd, QWP_WNDLIST);
  93.    pCurrent = (PWNDLIST)WinQueryWindowPtr(hWnd, QWP_CURRENT);
  94.  
  95.    WinQueryWindowRect(hWnd, &rect);
  96.  
  97.    ULONG x = ULONG(float(rect.xRight)*pCreate->pos.x);
  98.    ULONG y = ULONG(float(rect.yTop)*pCreate->pos.y);
  99.    ULONG x2 = ULONG(float(rect.xRight)*pCreate->pos.xx);
  100.    ULONG y2 = ULONG(float(rect.yTop)*pCreate->pos.yy);
  101.  
  102.    hCntrl = WinCreateWindow(pCreate->hwndParent, pCreate->pszClass, pCreate->pszTitle, pCreate->flStyle, x+pCreate->pos.xAdd, y+pCreate->pos.yAdd, ((x2-x)+pCreate->pos.xxAdd)-pCreate->pos.xAdd, ((y2-y)+pCreate->pos.yyAdd)-pCreate->pos.yAdd, pCreate->hwndOwner, pCreate->hwndInsertBehind, pCreate->id, pCreate->pCtlData, pCreate->pPresParams);
  103.    if(hCntrl == NULLHANDLE)
  104.       return hCntrl;
  105.  
  106.    if(pList == NULL)
  107.    {
  108.       pCurrent = new WNDLIST;
  109.       pCurrent->next = pCurrent;
  110.       pCurrent->prev = pCurrent;
  111.       WinSetWindowPtr(hWnd, QWP_WNDLIST, (PVOID)pCurrent);
  112.    }
  113.    else
  114.    {
  115.       pCurrent->next = new WNDLIST;
  116.       pCurrent->next->prev = pCurrent;
  117.       pCurrent->next->next = pList;
  118.       pCurrent = pCurrent->next;
  119.       pList->prev = pCurrent;
  120.    }
  121.    pCurrent->hWnd = hCntrl;
  122.    memcpy(&pCurrent->pos, &pCreate->pos, sizeof(RELWNDPOS));
  123.    WinSetWindowPtr(hWnd, QWP_CURRENT, (PVOID)pCurrent);
  124.  
  125.    return hCntrl;
  126. }
  127.  
  128.  
  129. void ascpSize(HWND hWnd)
  130. {
  131.    RECTL rect;
  132.    ULONG x;
  133.    ULONG y;
  134.    ULONG x2;
  135.    ULONG y2;
  136.  
  137.    PWNDLIST pList = (PWNDLIST)WinQueryWindowPtr(hWnd, QWP_WNDLIST);
  138.    PWNDLIST pTmp  = pList;
  139.  
  140.    if(pList == NULL)
  141.       return;
  142.  
  143.    WinQueryWindowRect(hWnd, &rect);
  144.  
  145.    do
  146.    {
  147.       x = ULONG(float(rect.xRight)*pTmp->pos.x);
  148.       y = ULONG(float(rect.yTop)*pTmp->pos.y);
  149.       x2 = ULONG(float(rect.xRight)*pTmp->pos.xx);
  150.       y2 = ULONG(float(rect.yTop)*pTmp->pos.yy);
  151.  
  152.       WinSetWindowPos(pTmp->hWnd, HWND_TOP, x+pTmp->pos.xAdd, y+pTmp->pos.yAdd, ((x2-x)+pTmp->pos.xxAdd)-pTmp->pos.xAdd, ((y2-y)+pTmp->pos.yyAdd)-pTmp->pos.yAdd, SWP_SIZE | SWP_MOVE);
  153.       pTmp = pTmp->next;
  154.    }while(pTmp != pList);
  155. }
  156.  
  157.  
  158. void ascpDestroy(HWND hWnd)
  159. {
  160.    PWNDLIST pFirst = (PWNDLIST)WinQueryWindowPtr(hWnd, QWP_WNDLIST);
  161.    PWNDLIST pEntry = pFirst;
  162.    PWNDLIST pTmp = pEntry;
  163.  
  164.    if(pFirst == NULL)
  165.       return;
  166.  
  167.    do
  168.    {
  169.       pEntry = pEntry->next;
  170.  
  171.       WinDestroyWindow(pTmp->hWnd);
  172.       delete pTmp;
  173.  
  174.       pTmp = pEntry;
  175.  
  176.       DosSleep(0);
  177.    }while(pTmp != pFirst);
  178.  
  179.    // reset playlist info
  180.    WinSetWindowPtr(hWnd, QWP_WNDLIST, (PVOID)NULL);
  181.    WinSetWindowPtr(hWnd, QWP_CURRENT, (PVOID)NULL);
  182. }
  183.