//  Beta release only.
//
//  Copyright(c) Ivyware Pty Ltd 2002-4  (all rights reserved)
//               MELBOURNE, VICTORIA, AUSTRALIA, 3130
//
//  Provided no fee is charged (either by the distributer or user)
//  permission to use, copy, modify, and distribute this software and
//  its documentation for any purpose and without fee is hereby granted,
//  provided that the above copyright notice appear in all copies and
//  that both that copyright notice and this permission notice appear
//  in supporting documentation.
//
//  No waranty or suitablity for purpose is implied.
//
//
//  Implementation for CWnd extensions
//

#include "StdAfx.h"
#include "CWnd_Ext.h"

///////////////////////////////////////////////////////////////////////
//  CWnd message broadcasting
//  NOTES: Static functions are just simpler
//       : Facilitates message broadcasting to registered clients
typedef struct
{
    CWnd *pCWnd;
    UINT  nID;
} BCastRegister;                       // Registration management
static CList<BCastRegister*,BCastRegister*>
               s_oCListBCastRegister;  // Registered clients list

//
//  Manages client interest in broadcasted  message
//
//
//  Parameters:  CWnd *pCWnd
//               Client
//
//               UINT nID
//               Message identification
//
//               BOOL bRegister
//               Registration mode
//                 TRUE... Register for broadcasts
//                 FALSE.. Deregister
//
//  Returns:     BOOL
//               Registered clients summary
BOOL
CWnd_BCastRegister( CWnd *pCWnd, UINT nID, BOOL bRegister )
{
    // Introduce locals
    BCastRegister *pBCastRegister = 0;

    // Loop through all registered clients
    for ( int k = 0; k < s_oCListBCastRegister.GetCount(); k++ )
    {
      pBCastRegister = s_oCListBCastRegister.RemoveHead();
      if ( pBCastRegister->nID   == nID   &&
           pBCastRegister->pCWnd == pCWnd    )
        break;
      s_oCListBCastRegister.AddTail ( pBCastRegister );
      pBCastRegister = 0;
    }

    // List addition
    if ( bRegister )
    {
      if ( pBCastRegister == 0 )
        pBCastRegister = new BCastRegister;
      pBCastRegister -> nID   = nID;
      pBCastRegister -> pCWnd = pCWnd;
      s_oCListBCastRegister.AddTail ( pBCastRegister );
      pBCastRegister = 0;
    }

    // List removal
    else
      delete pBCastRegister;

    // Tidy up and
    return TRUE;
}

//
//  Broadcasts passed message to all registered clients
//
//
//  Parameters:  UINT nID
//               Message identification
//
//               WPARAM wParam
//               Message dependant
//
//               LPARAM lParam
//               Message dependant
//
//  Returns:     BOOL
//               Registered clients summary
BOOL
CWnd_BCastMessage ( UINT nID, WPARAM wParam, LPARAM lParam )
{
    // Introduce locals
    BCastRegister *pBCastRegister;
    BOOL           bSummary = FALSE;

    // Loop through all registered clients
    POSITION pos = s_oCListBCastRegister.GetHeadPosition();
    while ( pos )
    {
      pBCastRegister = s_oCListBCastRegister.GetNext ( pos );
      if ( pBCastRegister->nID != nID )
        continue;
      pBCastRegister -> pCWnd->PostMessage ( pBCastRegister->nID 
                                           , wParam, lParam );
      bSummary = TRUE;
    }

    // Tidy up and
    return bSummary;
}

//
//  Cancels all message registrations for window
//
//
//  Parameters:  CWnd *pCWnd
//               Client for which all message registrations are
//               to be cancelled
//
//  Returns:     BOOL
//               Registered clients summary
BOOL
CWnd_BCastCancel ( CWnd *pCWnd )
{
    // Introduce locals
    BCastRegister *pBCastRegister = 0;
    BOOL           bSummary = FALSE;

    // Loop through all registered clients
    for ( int k = 0; k < s_oCListBCastRegister.GetCount(); k++ )
    {
      pBCastRegister = s_oCListBCastRegister.RemoveHead();
      if ( pBCastRegister->pCWnd == pCWnd )
      {
        delete pBCastRegister;
        bSummary = TRUE;
        k--;
      }
      else
        s_oCListBCastRegister.AddTail ( pBCastRegister );
    }

    // Tidy up and
    return bSummary;
}
