#include "../BBApi.h"
#include "Menu.h"
#include "MenuMaker.h"
#include "DesktopMenu.h"
#include "MenuItem.h"
#include "FolderItem.h"
#include "CommandItem.h"
#include "../Settings.h"
#include "../Workspaces.h"
extern MenuMaker *pMenuMaker;
extern Settings *pSettings;
extern Workspaces *pWorkspaces;
#define DESKTOP_FOLDER_TIMER 4434
DesktopMenu::DesktopMenu(const char *pszTitle, MenuMaker *pMenuMaker) : Menu(pMenuMaker->hInst)
{
pMenuMaker = pMenuMaker;
m_pszTitle = pszTitle ? strdup(pszTitle) : NULL;
}
DesktopMenu::~DesktopMenu()
{
if(m_pszTitle) free(m_pszTitle);
m_pszTitle = NULL;
}
void DesktopMenu::OnShow(bool fShow)
{
if(fShow) UpdateFolder();
}
bool DesktopMenu::OnUser(int nMessage, WPARAM wParam, LPARAM lParam, LRESULT &lResult)
{
if(nMessage == BB_DESKTOPINFO)
{
DesktopInfo *pDesktopInfo = (DesktopInfo *) lParam;
DesktopMenuItem *item = new DesktopMenuItem(pDesktopInfo->name, pMenuMaker, pDesktopInfo->number);
item->SetActivePainter(pMenuMaker->m_pSelEntry);
item->SetPainter(pMenuMaker->m_pEntry);
item->SetHeight(pMenuMaker->m_nSubmenuHeight);
AddMenuItem(item);
return true;
}
return Menu::OnUser(nMessage, wParam, lParam, lResult);
}
void DesktopMenu::UpdateFolder()
{
DeleteMenuItems();
pMenuMaker->AddHeaderItem(this, m_pszTitle);
DesktopMenuItem *item = new DesktopMenuItem("Minimized windows", pMenuMaker, 255);
item->SetActivePainter(pMenuMaker->m_pSelEntry);
item->SetPainter(pMenuMaker->m_pEntry);
item->SetHeight(pMenuMaker->m_nSubmenuHeight);
AddMenuItem(item);
pMenuMaker->CreateMenuItem(this, "[separator]", NULL, NULL, false);
SendMessage(GetBBWnd(), BB_LISTDESKTOPS, (WPARAM)GetWindow(), 0);
pMenuMaker->CreateMenuItem(this, "[separator]", NULL, NULL, false);
pMenuMaker->CreateMenuItem(this, "[add]", NULL, "New workspace", false);
pMenuMaker->CreateMenuItem(this, "[del]", NULL, "Remove last workspace", false);
pMenuMaker->AddBottomItem(this);
Invalidate();
Validate();
}
DesktopMenuItem::DesktopMenuItem(char *pszTitle, MenuMaker *pMenuMaker, int nDesktop) : FolderItem(NULL, pszTitle)
{
m_pMenuMaker = pMenuMaker;
m_nDesktop = nDesktop;
m_nTimer = 0;
}
DesktopMenuItem::~DesktopMenuItem()
{
m_pParent->RemoveChild(m_pSubMenu);
if (m_pSubMenu) delete m_pSubMenu;
m_pSubMenu = NULL;
}
void DesktopMenuItem::Invoke()
{
m_pParent->Hide(HIDE_CHILDREN);
m_pParent->Hide(HIDE_PARENTS);
pWorkspaces->DeskSwitch(m_nDesktop);
}
bool DesktopMenuItem::Active(bool bActivate)
{
if(bActivate && !m_bActive)
UpdateFolder();
return FolderItem::Active(bActivate);
}
void DesktopMenuItem::UpdateFolder()
{
m_pSubMenu->DeleteMenuItems();
m_pMenuMaker->AddHeaderItem(m_pSubMenu, m_pszTitle);
EnumWindows(CheckTaskEnumProc, (LPARAM)this);
m_pMenuMaker->AddBottomItem(m_pSubMenu);
m_pSubMenu->Sort(0,0);
m_pSubMenu->Invalidate();
m_pSubMenu->Validate();
}
void DesktopMenuItem::AddWindow(HWND hWnd)
{
char pszWindowText[1024], wndstring[10];
GetWindowText(hWnd, pszWindowText, sizeof(pszWindowText));
_ultoa((unsigned long)hWnd, wndstring, 16);
m_pMenuMaker->CreateMenuItem(m_pSubMenu, "<task_item>", wndstring, pszWindowText, false);
}
void DesktopMenuItem::Attached(Menu* pMenu)
{
m_pParent = pMenu;
m_pSubMenu = new Menu(m_pMenuMaker->hInst);
m_pParent->AddChild(m_pSubMenu);
m_pSubMenu->SetParent(m_pParent);
SetTimer(GetWindow(), DESKTOP_FOLDER_TIMER, 2000, NULL);
}
void DesktopMenuItem::Timer(int nTimer)
{
FolderItem::Timer(nTimer);
if(nTimer == DESKTOP_FOLDER_TIMER && IsWindowVisible(m_pSubMenu->GetWindow()))
UpdateFolder();
}
BOOL CALLBACK CheckTaskEnumProc(HWND hwnd, LPARAM lParam)
{
DesktopMenuItem* pD = (DesktopMenuItem*)lParam;
if (pD->m_nDesktop == 255)
{
if (IsAppWindow(hwnd) && IsIconic(hwnd)) pD->AddWindow(hwnd);
}
else if (pD->m_nDesktop == 254)
{
if (IsAppWindow(hwnd)) pD->AddWindow(hwnd);
}
else
{
if (IsAppWindow(hwnd) && !IsIconic(hwnd))
{
if (pWorkspaces->getDesktop(hwnd) == pD->m_nDesktop) pD->AddWindow(hwnd);
}
}
return true;
}
syntax highlighting by