home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol11n16.zip / OBJWND.C < prev    next >
C/C++ Source or Header  |  1992-08-14  |  3KB  |  150 lines

  1. /* 
  2. OBJWND.C -- "object windows" from WMHANDLER
  3.  
  4. Copyright (c) 1992 Ziff Davis Communications
  5. PC Magazine * Andrew Schulman (June 1992)
  6. */
  7.  
  8.  
  9. #include "windows.h"
  10. #include "objwnd.h"
  11.  
  12. static WMHANDLER wmhandler[WM_USER] = {0};
  13.  
  14. #define MAX_EXTRA    32
  15.  
  16. typedef struct {
  17.     WORD message;
  18.     WMHANDLER handler;
  19.     } EXTRAHANDLER;
  20.  
  21. static EXTRAHANDLER extrahandler[MAX_EXTRA] = {0};
  22. static int num_extra = 0;
  23.  
  24. static int isextramsg(WORD message)
  25. {
  26.     EXTRAHANDLER *pex;
  27.     int i;
  28.     for (i=0, pex=extrahandler; i<MAX_EXTRA; i++, pex++)
  29.         if (pex->message == message)
  30.             return i;
  31.     return -1;
  32. }
  33.  
  34. static long defwmhandler(HWND hwnd, WORD msg, WORD wParam, LONG lParam)
  35. {   
  36.     return DefWindowProc(hwnd, msg, wParam, lParam);
  37. }
  38.  
  39. static BOOL did_init = FALSE;
  40. void objwnd_init(void)
  41. {
  42.     EXTRAHANDLER *pex;
  43.     WMHANDLER *pwm;
  44.     int i;
  45.     for (i=0, pwm=wmhandler; i < WM_USER; i++, pwm++) 
  46.         *pwm = defwmhandler;
  47.     for (i=0, pex=extrahandler; i < MAX_EXTRA; i++, pex++)
  48.     {
  49.         pex->message = 0;
  50.         pex->handler = defwmhandler;
  51.     }
  52.     did_init = TRUE;
  53. }
  54.  
  55. long FAR PASCAL _export WndProc(HWND hWnd, WORD message, 
  56.     WORD wParam, LONG lParam)
  57. {
  58.     int iExtraMsg;
  59.     
  60.     if (message < WM_USER)
  61.         return (*wmhandler[message])(hWnd, message, wParam, lParam);
  62.     else if ((iExtraMsg = isextramsg(message)) != -1)
  63.         return (*extrahandler[iExtraMsg].handler)(hWnd, 
  64.             message, wParam, lParam);
  65.     else
  66.         return DefWindowProc(hWnd, message, wParam, lParam);
  67. }
  68.  
  69. WMHANDLER on(unsigned message, WMHANDLER f)
  70. {
  71.     WMHANDLER oldf;
  72.     int iExtraMsg;
  73.     
  74.     if (! did_init)    
  75.         objwnd_init();
  76.         
  77.     if (message < WM_USER)
  78.     {
  79.         oldf = wmhandler[message];
  80.         wmhandler[message] = f ? f : defwmhandler;
  81.         return (oldf) ? oldf : defwmhandler;
  82.     }
  83.     else if ((iExtraMsg = isextramsg(message)) != -1)
  84.     {
  85.         oldf = extrahandler[iExtraMsg].handler;
  86.         extrahandler[iExtraMsg].handler = f ? f : defwmhandler;
  87.         return (oldf) ? oldf : defwmhandler;
  88.     }
  89.     else if (num_extra < MAX_EXTRA)
  90.     {
  91.         extrahandler[num_extra].message = message;
  92.         extrahandler[num_extra].handler = f;
  93.         num_extra++;
  94.         return defwmhandler;
  95.     }
  96.     else
  97.         return -1;    // couldn't set!
  98. }
  99.  
  100. HWND objwnd(HANDLE hPrevInst, HANDLE hInst, char *name)
  101. {
  102.     static HWND hwnd = 0;
  103.     
  104.     if (hwnd)        
  105.         return hwnd;
  106.     
  107.     if (! did_init)    
  108.         objwnd_init();
  109.         
  110.     if (! hPrevInst)
  111.     {
  112.         WNDCLASS wndclass;
  113.         memset(&wndclass, 0, sizeof(wndclass));
  114.         wndclass.lpfnWndProc = WndProc;
  115.         wndclass.hInstance = hInst;
  116.         wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  117.         wndclass.lpszClassName = "OBJECTWND";
  118.         if (! RegisterClass(&wndclass))
  119.             return 0;
  120.     }
  121.     
  122.     hwnd = CreateWindow("OBJECTWND", name, 
  123.         WS_OVERLAPPEDWINDOW,
  124.         CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 
  125.         NULL, NULL, hInst, NULL);
  126.     return hwnd;
  127. }
  128.  
  129. int mainloop(void)
  130. {
  131.     MSG msg;
  132.     while (GetMessage(&msg, NULL, 0, 0))
  133.     {
  134.         TranslateMessage(&msg);
  135.         DispatchMessage(&msg);
  136.     }
  137.     return msg.wParam;
  138. }
  139.  
  140. void yield(void)
  141. {
  142.     MSG msg;
  143.     if (GetMessage(&msg, NULL, 0, 0))
  144.     {
  145.         TranslateMessage(&msg);
  146.         DispatchMessage(&msg);
  147.     }
  148. }
  149.  
  150.