home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / slavewnd.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.2 KB  |  198 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "stdafx.h"
  20. #include "slavewnd.h"
  21.  
  22. //  Global must be present first, as other globals may want
  23. //      to register early on.
  24. #pragma warning(disable:4074)
  25. #pragma init_seg(compiler)
  26. CSlaveWindow slavewnd;
  27. #pragma warning(default:4074)
  28.  
  29. struct SlaveStruct  {
  30.     UINT m_msg;
  31.     SlaveWindowCallback m_swc;
  32. };
  33.  
  34. const char *pSlaveWindowClass = "NetscapeSlaveClass";
  35. const char *pSlaveWindowTitle = "NetscapeSlaveWindow";
  36.  
  37. CSlaveWindow::CSlaveWindow()
  38. {
  39.     m_hWnd = NULL;
  40.     m_hInstance = NULL;
  41.  
  42.     m_pHandlers = XP_ListNew();
  43. }
  44.  
  45. CSlaveWindow::~CSlaveWindow()
  46. {
  47.     if(m_hWnd)  {
  48.         VERIFY(::DestroyWindow(m_hWnd));
  49.         m_hWnd = NULL;
  50.     }
  51.  
  52.     if(m_hInstance) {
  53.         VERIFY(::UnregisterClass(pSlaveWindowClass, m_hInstance));
  54.         m_hInstance = NULL;
  55.     }
  56.  
  57.     if(m_pHandlers) {
  58.         XP_List *pTraverse = m_pHandlers;
  59.         SlaveStruct *pSlave = NULL;
  60.         while(pSlave = (SlaveStruct *)XP_ListNextObject(pTraverse))    {
  61.             delete pSlave;
  62.         }
  63.  
  64.         XP_ListDestroy(m_pHandlers);
  65.         m_pHandlers = NULL;
  66.     }
  67. }
  68.  
  69. LRESULT
  70. CALLBACK
  71. #ifndef _WIN32
  72. _export
  73. #endif
  74. SlaveWindowProc(HWND hWnd,
  75.                 UINT uMsg,
  76.                 WPARAM wParam,
  77.                 LPARAM lParam)
  78. {
  79.     LRESULT lRetval = NULL;
  80.  
  81.     slavewnd.WindowProc(uMsg, wParam, lParam);
  82.  
  83.     lRetval = DefWindowProc(hWnd, uMsg, wParam, lParam);
  84.  
  85.     return(lRetval);
  86. }
  87.  
  88. void CSlaveWindow::WindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  89. {
  90.     if(m_pHandlers) {
  91.         //  Go through list of handlers, see if anyone wanted this
  92.         //      message.
  93.         //  Keep a seperate list, so callbacks can add and remove
  94.         //      themselves via the registration and not cause a
  95.         //      crash.
  96.         //  This also has the side effect of calling the callbacks
  97.         //      in the order of registration, instead of in the
  98.         //      reverse order.
  99.         XP_List *pTraverse = m_pHandlers;
  100.         XP_List *pTemp = NULL;
  101.         SlaveStruct *pSlave = NULL;
  102.         while(pSlave = (SlaveStruct *)XP_ListNextObject(pTraverse)) {
  103.             if(pSlave->m_msg == uMsg)   {
  104.                 if(NULL == pTemp) {
  105.                     pTemp = XP_ListNew();
  106.                 }
  107.                 if(pTemp) {
  108.                     XP_ListAddObject(pTemp, (void *)pSlave);
  109.                 }
  110.             }
  111.         }
  112.         if(pTemp) {
  113.             pTraverse = pTemp;
  114.             while(pSlave = (SlaveStruct *)XP_ListNextObject(pTraverse)) {
  115.                 //  Fire.
  116.                 pSlave->m_swc(uMsg, wParam, lParam);
  117.             }
  118.             XP_ListDestroy(pTemp);
  119.             pTemp = NULL;
  120.         }
  121.     }
  122. }
  123.  
  124. void CSlaveWindow::InitInstance(HINSTANCE hInst)
  125. {
  126.     //  Only do once.
  127.     if(hInst != NULL && m_hInstance == NULL) {
  128.         m_hInstance = hInst;
  129.  
  130.         //  Register the window class.
  131.         WNDCLASS wc;
  132.         memset(&wc, 0, sizeof(wc));
  133.         wc.lpfnWndProc = SlaveWindowProc;
  134.         wc.hInstance = m_hInstance;
  135.         wc.lpszClassName = pSlaveWindowClass;
  136.         ATOM aWndClass = ::RegisterClass(&wc);
  137.         if(aWndClass != NULL)   {
  138.             //  Instantiate the window.
  139.             m_hWnd = ::CreateWindow(
  140.                 pSlaveWindowClass,
  141.                 pSlaveWindowTitle,
  142.                 WS_DISABLED,
  143.                 10,
  144.                 10,
  145.                 10,
  146.                 10,
  147.                 NULL,
  148.                 NULL,
  149.                 m_hInstance,
  150.                 NULL);
  151.         }
  152.         ASSERT(m_hWnd);
  153.     }
  154. }
  155.  
  156. HWND CSlaveWindow::GetWindow()
  157. {
  158.     //  Null until initted.
  159.     return(m_hWnd);
  160. }
  161.  
  162. void *CSlaveWindow::Register(UINT msg, SlaveWindowCallback func)
  163. {
  164.     void *pRetval = NULL;
  165.  
  166.     //  Can't call into nothing.
  167.     //  Must have list.
  168.     if(func && m_pHandlers)    {
  169.         SlaveStruct *pNew = new SlaveStruct;
  170.         if(pNew)    {
  171.             pNew->m_msg = msg;
  172.             pNew->m_swc = func;
  173.  
  174.             XP_ListAddObject(m_pHandlers, (void *)pNew);
  175.  
  176.             pRetval = (void *)pNew;
  177.         }
  178.     }
  179.  
  180.     return(pRetval);
  181. }
  182.  
  183. BOOL CSlaveWindow::UnRegister(void *pCookie)
  184. {
  185.     BOOL bRetval = FALSE;
  186.  
  187.     //  Must have cookie, must have list.
  188.     if(pCookie && m_pHandlers) {
  189.         bRetval = XP_ListRemoveObject(m_pHandlers, pCookie);
  190.         if(bRetval) {
  191.             SlaveStruct *pDel = (SlaveStruct *)pCookie;
  192.             delete pCookie;
  193.         }
  194.     }
  195.  
  196.     return(bRetval);
  197. }
  198.