#include "WindowMenu.h"
#include <Catalog.h>
#include <Locale.h>
#include <Window.h>
#include "BarApp.h"
#include "BarView.h"
#include "ShowHideMenuItem.h"
#include "TeamMenu.h"
#include "TeamMenuItem.h"
#include "tracker_private.h"
#include "WindowMenuItem.h"
#include "WindowPrivate.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "WindowMenu"
bool
TWindowMenu::WindowShouldBeListed(client_window_info* info)
{
return ((info->feel == B_NORMAL_WINDOW_FEEL || info->feel == kWindowScreenFeel)
&& info->show_hide_level <= 0);
}
TWindowMenu::TWindowMenu(const BList* team, const char* signature)
:
BMenu("Deskbar Team Menu"),
fTeam(team),
fApplicationSignature(signature),
fExpanded(false),
fExpandedIndex(0)
{
SetItemMargins(0.0f, 0.0f, 0.0f, 0.0f);
}
void
TWindowMenu::AttachedToWindow()
{
RemoveItems(0, CountItems(), true);
int32 miniCount = 0;
bool dragging = false;
TBarView* barview =(static_cast<TBarApp*>(be_app))->BarView();
if (barview != NULL && barview->LockLooper()) {
dragging = barview->Dragging();
if (dragging) {
Window()->Hide();
if (barview->ExpandoState()) {
SetTrackingHook(barview->MenuTrackingHook,
barview->GetTrackingHookData());
}
barview->DragStart();
}
barview->UnlockLooper();
}
int32 parentMenuItems = 0;
int32 teamCount = fTeam->CountItems();
for (int32 i = 0; i < teamCount; i++) {
team_id theTeam = (addr_t)fTeam->ItemAt(i);
int32 tokenCount = 0;
int32* tokens = get_token_list(theTeam, &tokenCount);
for (int32 j = 0; j < tokenCount; j++) {
client_window_info* wInfo = get_window_info(tokens[j]);
if (wInfo == NULL)
continue;
if (WindowShouldBeListed(wInfo)) {
int32 numItems = CountItems();
for (int32 addIndex = 0; addIndex < numItems; addIndex++) {
TWindowMenuItem* item
= static_cast<TWindowMenuItem*>(ItemAt(addIndex));
if (item != NULL
&& strcasecmp(item->Label(), wInfo->name) > 0) {
break;
}
}
if (!fExpanded) {
TWindowMenuItem* item = new TWindowMenuItem(wInfo->name,
wInfo->server_token, wInfo->is_mini,
((1 << current_workspace()) & wInfo->workspaces) != 0,
dragging);
if (dragging)
item->SetEnabled(false);
AddItem(item,
TWindowMenuItem::InsertIndexFor(this, 0, item));
} else {
TTeamMenuItem* parentItem
= static_cast<TTeamMenuItem*>(Superitem());
if (parentItem->ExpandedWindowItem(wInfo->server_token)) {
TWindowMenuItem* item = parentItem->ExpandedWindowItem(
wInfo->server_token);
if (item == NULL)
continue;
item->SetTo(wInfo->name, wInfo->server_token,
wInfo->is_mini,
((1 << current_workspace()) & wInfo->workspaces)
!= 0, dragging);
parentMenuItems++;
}
}
if (wInfo->is_mini)
miniCount++;
}
free(wInfo);
}
free(tokens);
}
int32 itemCount = CountItems() + parentMenuItems;
if (itemCount < 1) {
TWindowMenuItem* noWindowsItem
= new TWindowMenuItem(B_TRANSLATE("No windows"), -1, false, false);
noWindowsItem->SetEnabled(false);
AddItem(noWindowsItem);
if (fApplicationSignature.ICompare(kTrackerSignature) != 0) {
AddSeparatorItem();
AddItem(new TShowHideMenuItem(B_TRANSLATE("Quit application"),
fTeam, B_QUIT_REQUESTED));
}
} else {
if (!dragging) {
TShowHideMenuItem* hide
= new TShowHideMenuItem(B_TRANSLATE("Hide all"), fTeam,
B_MINIMIZE_WINDOW);
TShowHideMenuItem* show
= new TShowHideMenuItem(B_TRANSLATE("Show all"), fTeam,
B_BRING_TO_FRONT);
TShowHideMenuItem* close
= new TShowHideMenuItem(B_TRANSLATE("Close all"), fTeam,
B_QUIT_REQUESTED);
if (miniCount == itemCount)
hide->SetEnabled(false);
else if (miniCount == 0)
show->SetEnabled(false);
if (!parentMenuItems)
AddSeparatorItem();
AddItem(hide);
AddItem(show);
AddItem(close);
}
}
BMenu::AttachedToWindow();
}
void
TWindowMenu::DetachedFromWindow()
{
TBarView* barview = (dynamic_cast<TBarApp*>(be_app))->BarView();
if (barview && barview->ExpandoState() && barview->Dragging()
&& barview->LockLooper()) {
Window()->Show();
barview->DragStop();
barview->UnlockLooper();
}
BMenu::DetachedFromWindow();
}
BPoint
TWindowMenu::ScreenLocation()
{
return BMenu::ScreenLocation();
}
void
TWindowMenu::SetExpanded(bool status, int lastIndex)
{
fExpanded = status;
fExpandedIndex = lastIndex;
}