/*
 ============================================================================
 xoblite -> an alternative shell based on Blackbox for Windows
 Copyright  2002-2005 Karl-Henrik Henriksson [qwilk]
 Copyright  2001-2004 The Blackbox for Windows Development Team
 http://xoblite.net/ - #bb4win on irc.freenode.net
 ============================================================================

  Blackbox for Windows is free software, released under the
  GNU General Public License (GPL version 2 or later), with an extension
  that allows linking of proprietary modules under a controlled interface.
  What this means is that plugins etc. are allowed to be released
  under any license the author wishes. Please note, however, that the
  original Blackbox gradient math code used in Blackbox for Windows
  is available under the BSD license.

  http://www.fsf.org/licenses/gpl.html
  http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface
  http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  For additional license information, please read the included license.html

 ============================================================================
*/

//#pragma warning(disable: 4786) // STL naming warnings

#include "MessageManager.h" 

//===========================================================================

MessageManager::MessageManager()
{
}

MessageManager::~MessageManager()
{
        ClearMessages();
}

//===========================================================================

void MessageManager::AddMessage(UINT message, HWND window)
{
        WindowSet *winSet;
        MsgMap::iterator iter = msgs.find(message);
        if (iter == msgs.end())
        {
                winSet = new WindowSet();
                msgs[message] = winSet;
        }
        else
                winSet = iter->second;

        winSet->insert(window);
}

//====================

void MessageManager::RemoveMessage(UINT message, HWND window)
{
        MsgMap::iterator iter = msgs.find(message);

        if (iter == msgs.end()) return;

        iter->second->erase(window);

        if (iter->second->size() == 0)
        {
                delete iter->second;
                msgs.erase(iter);
        }
}

//====================

void MessageManager::ClearMessages()
{
        MsgMap::iterator iter = msgs.begin();

        while (iter != msgs.end())
        {
                if (iter->second)
                        delete iter->second;
                iter++;
        }

        msgs.clear();
}

//====================

bool MessageManager::SendMessage(UINT message, WPARAM wParam, LPARAM lParam, LRESULT *result)
{
        WindowSet *winSet;
        MsgMap::iterator iter1 = msgs.find(message);

        if (result)     *result = 0;

        if (iter1 == msgs.end()) return false;

        winSet = iter1->second;

        WindowSet::iterator iter2 = winSet->begin();

        // Use the first one for the result...
        if (iter2 != winSet->end())
        {
                if (result)
                {
                        *result = ::SendMessage((HWND)*iter2, message, wParam, lParam);
                        iter2++;
                }
        }

        while (iter2 != winSet->end())
        {
                ::SendMessage((HWND)*iter2, message, wParam, lParam);
                iter2++;
        }

        return true;
}

//====================

void MessageManager::PostMessage(UINT message, WPARAM wParam, LPARAM lParam)
{
        WindowSet *winSet;
        MsgMap::iterator iter1;

        iter1 = msgs.find(message);

        if (iter1 == msgs.end()) return;

        winSet = iter1->second;

        WindowSet::iterator iter2 = winSet->begin();

        while (iter2 != winSet->end())
        {
                ::PostMessage((HWND)*iter2, message, wParam, lParam);
                iter2++;
        }
}

//====================

bool MessageManager::HandlerExists(UINT message)
{
        if (msgs.find(message) != msgs.end())
                return true;
        return false;
}

//===========================================================================





syntax highlighting by

w e b c p p
web c plus plus