/*
 ============================================================================
 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

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

#include "../BBApi.h" 
#include "MenuMaker.h" 
#include "PluginMenu.h" 
#include "MenuItem.h" 
#include "CommandItem.h" 
#include "FolderItem.h" 
#include "ConfigMenu.h" 
#include "../Slit.h" 
#include "../Settings.h" 
#include "../PluginManager.h" 

extern MenuMaker *pMenuMaker;
extern Slit *pSlit;
extern Settings *pSettings;
extern PluginManager *pPluginManager;

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

PluginMenu::PluginMenu(char* pszTitle, MenuMaker *pMenuMaker) : FolderItem(NULL, pszTitle)
{
        m_pMenuMaker = pMenuMaker;
}

PluginMenu::~PluginMenu()
{
        m_pParent->RemoveChild(m_pSubMenu);
        if (m_pSubMenu) delete m_pSubMenu;
        m_pSubMenu = NULL;
}

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

bool PluginMenu::Active(bool bActivate)
{
        if (bActivate && !m_bActive) UpdateFolder();
        return FolderItem::Active(bActivate);
}

void PluginMenu::Attached(Menu* pMenu)
{
        m_pParent = pMenu;
        m_pSubMenu = new Menu(m_pMenuMaker->hInst);
        m_pParent->AddChild(m_pSubMenu);
        m_pSubMenu->SetParent(m_pParent);
}

void PluginMenu::UpdateFolder()
{
        m_pSubMenu->DeleteMenuItems();
        m_pMenuMaker->AddHeaderItem(m_pSubMenu, m_pszTitle);

        m_pMenuMaker->CreateMenuItem(m_pSubMenu, "[loadplugin]", NULL, "Load plugin...", false);

        UnloadPluginMenu *UPMenu = new UnloadPluginMenu("Unload/Update plugin", m_pMenuMaker);
        UPMenu->SetActivePainter(m_pMenuMaker->m_pSelEntry);
        UPMenu->SetPainter(m_pMenuMaker->m_pEntry);
        UPMenu->SetHeight(m_pMenuMaker->m_nSubmenuHeight);
        m_pSubMenu->AddMenuItem(UPMenu);

        BroamMenu *BMenu = new BroamMenu("Bro@ms", m_pMenuMaker);
        BMenu->SetActivePainter(m_pMenuMaker->m_pSelEntry);
        BMenu->SetPainter(m_pMenuMaker->m_pEntry);
        BMenu->SetHeight(m_pMenuMaker->m_nSubmenuHeight);
        m_pSubMenu->AddMenuItem(BMenu);

        m_pMenuMaker->CreateMenuItem(m_pSubMenu, "[separator]", NULL, NULL, false);

        m_pMenuMaker->CreateMenuItem(m_pSubMenu, "[editplugins]", NULL, "Edit plugins.rc", false);
        m_pMenuMaker->CreateMenuItem(m_pSubMenu, "<slit_menu_item>", "CheckForPluginUpdates", "Check for updates...", false);
        m_pMenuMaker->CreateMenuItem(m_pSubMenu, "[aboutplugins]", NULL, "About loaded plugins...", false);

        m_pMenuMaker->AddBottomItem(m_pSubMenu);
        m_pSubMenu->Invalidate();
        m_pSubMenu->Validate();
}

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

UnloadPluginMenu::UnloadPluginMenu(char* pszTitle, MenuMaker *pMenuMaker) : FolderItem(NULL, pszTitle)
{
        m_pMenuMaker = pMenuMaker;
}

UnloadPluginMenu::~UnloadPluginMenu()
{
        m_pParent->RemoveChild(m_pSubMenu);
        if (m_pSubMenu) delete m_pSubMenu;
        m_pSubMenu = NULL;
}

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

bool UnloadPluginMenu::Active(bool bActivate)
{
        if (bActivate && !m_bActive) UpdateFolder();
        return FolderItem::Active(bActivate);
}

void UnloadPluginMenu::Attached(Menu* pMenu)
{
        m_pParent = pMenu;
        m_pSubMenu = new Menu(m_pMenuMaker->hInst);
        m_pParent->AddChild(m_pSubMenu);
        m_pSubMenu->SetParent(m_pParent);
}

void UnloadPluginMenu::UpdateFolder()
{
        m_pSubMenu->DeleteMenuItems();
        m_pMenuMaker->AddHeaderItem(m_pSubMenu, m_pszTitle);

        char pluginNumber[MAX_LINE_LENGTH], pluginName[MAX_LINE_LENGTH], tempArgument[MAX_LINE_LENGTH];

        for (int i=0; i < (int)pPluginManager->pluginList.size(); i++)
        {
                strcpy(tempArgument, "UnloadPlugin");
                strcat(tempArgument, itoa(i, pluginNumber, 10));
                strcpy(pluginName, pPluginManager->getPluginInfo(i, PLUGIN_NAME));

                m_pMenuMaker->CreateMenuItem(m_pSubMenu, "<slit_menu_item>", tempArgument, pluginName, pPluginManager->pluginList[i].UpdateAvailable);
        }

        m_pMenuMaker->AddBottomItem(m_pSubMenu);
        m_pSubMenu->Invalidate();
        m_pSubMenu->Validate();
}

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

BroamMenu::BroamMenu(char* pszTitle, MenuMaker *pMenuMaker) : FolderItem(NULL, pszTitle)
{
        m_pMenuMaker = pMenuMaker;
}

BroamMenu::~BroamMenu()
{
        m_pParent->RemoveChild(m_pSubMenu);
        if (m_pSubMenu) delete m_pSubMenu;
        m_pSubMenu = NULL;
}

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

bool BroamMenu::Active(bool bActivate)
{
        if (bActivate && !m_bActive) UpdateFolder();
        return FolderItem::Active(bActivate);
}

void BroamMenu::Attached(Menu* pMenu)
{
        m_pParent = pMenu;
        m_pSubMenu = new Menu(m_pMenuMaker->hInst);
        m_pParent->AddChild(m_pSubMenu);
        m_pSubMenu->SetParent(m_pParent);
}

void BroamMenu::UpdateFolder()
{
        m_pSubMenu->DeleteMenuItems();
        m_pMenuMaker->AddHeaderItem(m_pSubMenu, m_pszTitle);

        char pluginName[MAX_LINE_LENGTH], pluginBroams[MAX_LINE_LENGTH], pluginCheck[MAX_LINE_LENGTH];

        for (int i=0; i < (int)pPluginManager->pluginList.size(); i++)
        {
                // Check if the plugin supports the pluginInfo bro@m list parameter...
                strcpy(pluginBroams, pPluginManager->getPluginInfo(i, PLUGIN_BROAMS));
                strcpy(pluginCheck, pPluginManager->getPluginInfo(i, -1));
                if (strlen(pluginBroams) && stricmp(pluginBroams, pluginCheck))
                {
                        // If it does, create a bro@m subfolder for the plugin...
                        strcpy(pluginName, pPluginManager->getPluginInfo(i, PLUGIN_NAME));

                        PluginBroamList *PBList = new PluginBroamList(pluginName, m_pMenuMaker, i);
                        PBList->SetActivePainter(m_pMenuMaker->m_pSelEntry);
                        PBList->SetPainter(m_pMenuMaker->m_pEntry);
                        PBList->SetHeight(m_pMenuMaker->m_nSubmenuHeight);
                        m_pSubMenu->AddMenuItem(PBList);
                }
        }

        m_pMenuMaker->AddBottomItem(m_pSubMenu);
        m_pSubMenu->Invalidate();
        m_pSubMenu->Validate();
}

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

PluginBroamList::PluginBroamList(char* pszTitle, MenuMaker *pMenuMaker, int pluginNumber) : FolderItem(NULL, pszTitle)
{
        plugNumber = pluginNumber;
        m_pMenuMaker = pMenuMaker;
}

PluginBroamList::~PluginBroamList()
{
        m_pParent->RemoveChild(m_pSubMenu);
        if (m_pSubMenu) delete m_pSubMenu;
        m_pSubMenu = NULL;
}

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

bool PluginBroamList::Active(bool bActivate)
{
        if (bActivate && !m_bActive) UpdateFolder();
        return FolderItem::Active(bActivate);
}

void PluginBroamList::Attached(Menu* pMenu)
{
        m_pParent = pMenu;
        m_pSubMenu = new Menu(m_pMenuMaker->hInst);
        m_pParent->AddChild(m_pSubMenu);
        m_pSubMenu->SetParent(m_pParent);
}

void PluginBroamList::UpdateFolder()
{
        m_pSubMenu->DeleteMenuItems();
        m_pMenuMaker->AddHeaderItem(m_pSubMenu, m_pszTitle);

        // Fetch bro@m list for the plugin...
        char pluginBroamList[MAX_LINE_LENGTH], broam[MAX_LINE_LENGTH];
        strcpy(pluginBroamList, pPluginManager->getPluginInfo(plugNumber, PLUGIN_BROAMS));
        int nLen = strlen(pluginBroamList) - 1;

        while (nLen) // Loop as long as there are characters in the buffer...
        {
                // Start from the end and scan backwards for the start of a bro@m -> a @ sign...
                nLen = strlen(pluginBroamList) - 1;
                while (nLen >0 && pluginBroamList[nLen] != '@') nLen--;
                // When found, we copy the bro@m to the buffer...
                strcpy(broam, &pluginBroamList[nLen]);
                // ...and truncate the list string...
                pluginBroamList[nLen] = 0;

                // Finally, we create an [exec] menu item for it...
                m_pMenuMaker->CreateMenuItem(m_pSubMenu, "[exec]", broam, broam, false);
        }

        m_pMenuMaker->AddBottomItem(m_pSubMenu);
        m_pSubMenu->Invalidate();
        m_pSubMenu->Validate();
}

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





syntax highlighting by

w e b c p p
web c plus plus