xref: /haiku/src/apps/deskbar/StatusView.cpp (revision 71bd3ba59c06b130115c3ddd44457d8f9dd78997)
141281cf3SAxel Dörfler /*
241281cf3SAxel Dörfler Open Tracker License
341281cf3SAxel Dörfler 
441281cf3SAxel Dörfler Terms and Conditions
541281cf3SAxel Dörfler 
641281cf3SAxel Dörfler Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
741281cf3SAxel Dörfler 
841281cf3SAxel Dörfler Permission is hereby granted, free of charge, to any person obtaining a copy of
941281cf3SAxel Dörfler this software and associated documentation files (the "Software"), to deal in
1041281cf3SAxel Dörfler the Software without restriction, including without limitation the rights to
1141281cf3SAxel Dörfler use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
1241281cf3SAxel Dörfler of the Software, and to permit persons to whom the Software is furnished to do
1341281cf3SAxel Dörfler so, subject to the following conditions:
1441281cf3SAxel Dörfler 
1541281cf3SAxel Dörfler The above copyright notice and this permission notice applies to all licensees
1641281cf3SAxel Dörfler and shall be included in all copies or substantial portions of the Software.
1741281cf3SAxel Dörfler 
1841281cf3SAxel Dörfler THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1941281cf3SAxel Dörfler IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
2041281cf3SAxel Dörfler FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2141281cf3SAxel Dörfler BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2241281cf3SAxel Dörfler AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
2341281cf3SAxel Dörfler WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2441281cf3SAxel Dörfler 
2541281cf3SAxel Dörfler Except as contained in this notice, the name of Be Incorporated shall not be
2641281cf3SAxel Dörfler used in advertising or otherwise to promote the sale, use or other dealings in
2741281cf3SAxel Dörfler this Software without prior written authorization from Be Incorporated.
2841281cf3SAxel Dörfler 
2941281cf3SAxel Dörfler Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
3041281cf3SAxel Dörfler of Be Incorporated in the United States and other countries. Other brand product
3141281cf3SAxel Dörfler names are registered trademarks or trademarks of their respective holders.
3241281cf3SAxel Dörfler All rights reserved.
3341281cf3SAxel Dörfler */
3441281cf3SAxel Dörfler 
3541281cf3SAxel Dörfler #include <Debug.h>
3641281cf3SAxel Dörfler 
3741281cf3SAxel Dörfler #include <errno.h>
3841281cf3SAxel Dörfler #include <stdio.h>
3941281cf3SAxel Dörfler #include <string.h>
4041281cf3SAxel Dörfler #include <time.h>
4141281cf3SAxel Dörfler #include <unistd.h>
4241281cf3SAxel Dörfler #include <algorithm>
4341281cf3SAxel Dörfler 
4441281cf3SAxel Dörfler #include <fs_index.h>
4541281cf3SAxel Dörfler #include <fs_info.h>
4641281cf3SAxel Dörfler 
4741281cf3SAxel Dörfler #include <Application.h>
4841281cf3SAxel Dörfler #include <Beep.h>
49cb6afcb1SStephan Aßmus #include <Bitmap.h>
50cb6afcb1SStephan Aßmus #include <ControlLook.h>
5141281cf3SAxel Dörfler #include <Directory.h>
5241281cf3SAxel Dörfler #include <FindDirectory.h>
5341281cf3SAxel Dörfler #include <MenuItem.h>
5441281cf3SAxel Dörfler #include <NodeInfo.h>
5541281cf3SAxel Dörfler #include <NodeMonitor.h>
5641281cf3SAxel Dörfler #include <Path.h>
5741281cf3SAxel Dörfler #include <PopUpMenu.h>
5841281cf3SAxel Dörfler #include <Roster.h>
5941281cf3SAxel Dörfler #include <Screen.h>
6041281cf3SAxel Dörfler #include <Volume.h>
6141281cf3SAxel Dörfler #include <VolumeRoster.h>
6241281cf3SAxel Dörfler #include <Window.h>
6341281cf3SAxel Dörfler 
64cb6afcb1SStephan Aßmus #include "icons_logo.h"
65cb6afcb1SStephan Aßmus #include "BarApp.h"
6641281cf3SAxel Dörfler #include "DeskBarUtils.h"
67cb6afcb1SStephan Aßmus #include "ResourceSet.h"
6841281cf3SAxel Dörfler #include "StatusView.h"
6941281cf3SAxel Dörfler #include "StatusViewShelf.h"
7041281cf3SAxel Dörfler #include "TimeView.h"
7141281cf3SAxel Dörfler 
726d01b66dSAxel Dörfler using std::max;
7341281cf3SAxel Dörfler 
7441281cf3SAxel Dörfler #ifdef DB_ADDONS
7541281cf3SAxel Dörfler //	Add-on support
7641281cf3SAxel Dörfler //
7741281cf3SAxel Dörfler //	Item - internal item list (node, eref, etc)
7841281cf3SAxel Dörfler //	Icon - physical replicant handed to the DeskbarClass class
7941281cf3SAxel Dörfler //	AddOn - attribute based add-on
8041281cf3SAxel Dörfler 
8141281cf3SAxel Dörfler const char* const kInstantiateItemCFunctionName = "instantiate_deskbar_item";
8241281cf3SAxel Dörfler const char* const kInstantiateEntryCFunctionName = "instantiate_deskbar_entry";
8341281cf3SAxel Dörfler const char* const kDeskbarSecurityCodeFile = "Deskbar_security_code";
8441281cf3SAxel Dörfler const char* const kDeskbarSecurityCodeAttr = "be:deskbar_security_code";
8541281cf3SAxel Dörfler const char* const kStatusPredicate = "be:deskbar_item_status";
8641281cf3SAxel Dörfler const char* const kEnabledPredicate = "be:deskbar_item_status=enabled";
8741281cf3SAxel Dörfler const char* const kDisabledPredicate = "be:deskbar_item_status=disabled";
8841281cf3SAxel Dörfler 
89cb6afcb1SStephan Aßmus float sMinimumWindowWidth = kGutter + kMinimumTrayWidth + kDragRegionWidth;
90cb6afcb1SStephan Aßmus 
9141281cf3SAxel Dörfler 
9241281cf3SAxel Dörfler static void
9341281cf3SAxel Dörfler DumpItem(DeskbarItemInfo* item)
9441281cf3SAxel Dörfler {
9541281cf3SAxel Dörfler 	printf("is addon: %i, id: %li\n", item->isAddOn, item->id);
9666eba86fSAxel Dörfler 	printf("entry_ref:  %ld, %Ld, %s\n", item->entryRef.device,
9766eba86fSAxel Dörfler 		item->entryRef.directory, item->entryRef.name);
9841281cf3SAxel Dörfler 	printf("node_ref:  %ld, %Ld\n", item->nodeRef.device, item->nodeRef.node);
9941281cf3SAxel Dörfler }
10041281cf3SAxel Dörfler 
10141281cf3SAxel Dörfler 
10241281cf3SAxel Dörfler static void
10341281cf3SAxel Dörfler DumpList(BList* itemlist)
10441281cf3SAxel Dörfler {
10541281cf3SAxel Dörfler 	int32 count = itemlist->CountItems() - 1;
10641281cf3SAxel Dörfler 	if (count < 0) {
10741281cf3SAxel Dörfler 		printf("no items in list\n");
10841281cf3SAxel Dörfler 		return;
10941281cf3SAxel Dörfler 	}
11041281cf3SAxel Dörfler 	for (int32 i = count ; i >= 0 ; i--) {
11141281cf3SAxel Dörfler 		DeskbarItemInfo* item = (DeskbarItemInfo*)itemlist->ItemAt(i);
11241281cf3SAxel Dörfler 		if (!item)
11341281cf3SAxel Dörfler 			continue;
11441281cf3SAxel Dörfler 
11541281cf3SAxel Dörfler 		DumpItem(item);
11641281cf3SAxel Dörfler 	}
11741281cf3SAxel Dörfler }
11841281cf3SAxel Dörfler #endif	/* DB_ADDONS */
11941281cf3SAxel Dörfler 
12041281cf3SAxel Dörfler 
1217da06231SAxel Dörfler // don't change the name of this view to anything other than "Status"!
1227da06231SAxel Dörfler 
12341281cf3SAxel Dörfler TReplicantTray::TReplicantTray(TBarView* parent, bool vertical)
12441281cf3SAxel Dörfler 	: BView(BRect(0, 0, 1, 1), "Status", B_FOLLOW_LEFT | B_FOLLOW_TOP,
12541281cf3SAxel Dörfler 			B_WILL_DRAW | B_FRAME_EVENTS),
12641281cf3SAxel Dörfler 	fClock(NULL),
12741281cf3SAxel Dörfler 	fBarView(parent),
12841281cf3SAxel Dörfler 	fShelf(new TReplicantShelf(this)),
12941281cf3SAxel Dörfler 	fMultiRowMode(vertical),
130cb6afcb1SStephan Aßmus 	fMinimumTrayWidth(kMinimumTrayWidth),
13141281cf3SAxel Dörfler 	fAlignmentSupport(false)
13241281cf3SAxel Dörfler {
133cb6afcb1SStephan Aßmus 	// init the minimum window width according to the logo.
134cb6afcb1SStephan Aßmus 	const BBitmap* logoBitmap = AppResSet()->FindBitmap(B_MESSAGE_TYPE,
135cb6afcb1SStephan Aßmus 		R_BeLogoIcon);
136cb6afcb1SStephan Aßmus 	if (logoBitmap != NULL) {
137cb6afcb1SStephan Aßmus 		sMinimumWindowWidth = max_c(sMinimumWindowWidth,
138cb6afcb1SStephan Aßmus 			2 * (logoBitmap->Bounds().Width() + 8));
139cb6afcb1SStephan Aßmus 		fMinimumTrayWidth = sMinimumWindowWidth - kGutter - kDragRegionWidth;
140cb6afcb1SStephan Aßmus 	}
14141281cf3SAxel Dörfler }
14241281cf3SAxel Dörfler 
14341281cf3SAxel Dörfler 
14441281cf3SAxel Dörfler TReplicantTray::~TReplicantTray()
14541281cf3SAxel Dörfler {
14641281cf3SAxel Dörfler 	delete fShelf;
14741281cf3SAxel Dörfler }
14841281cf3SAxel Dörfler 
14941281cf3SAxel Dörfler 
15041281cf3SAxel Dörfler void
15141281cf3SAxel Dörfler TReplicantTray::AttachedToWindow()
15241281cf3SAxel Dörfler {
15341281cf3SAxel Dörfler 	BView::AttachedToWindow();
15441281cf3SAxel Dörfler 
155cb6afcb1SStephan Aßmus 	if (be_control_look != NULL) {
156cb6afcb1SStephan Aßmus 		SetViewColor(Parent()->ViewColor());
157cb6afcb1SStephan Aßmus 	} else {
15866eba86fSAxel Dörfler 		SetViewColor(tint_color(ui_color(B_MENU_BACKGROUND_COLOR),
15966eba86fSAxel Dörfler 			B_DARKEN_1_TINT));
160cb6afcb1SStephan Aßmus 	}
16141281cf3SAxel Dörfler 	SetDrawingMode(B_OP_COPY);
16266eba86fSAxel Dörfler 
16341281cf3SAxel Dörfler 	Window()->SetPulseRate(1000000);
16441281cf3SAxel Dörfler 	DealWithClock(fBarView->ShowingClock());
16541281cf3SAxel Dörfler 
16641281cf3SAxel Dörfler #ifdef DB_ADDONS
16741281cf3SAxel Dörfler 	// load addons and rehydrate archives
1683a6add49SAxel Dörfler #if !defined(HAIKU_TARGET_PLATFORM_LIBBE_TEST)
16941281cf3SAxel Dörfler 	InitAddOnSupport();
17041281cf3SAxel Dörfler #endif
1719c910f9eSStephan Aßmus #endif
17241281cf3SAxel Dörfler 	ResizeToPreferred();
17341281cf3SAxel Dörfler }
17441281cf3SAxel Dörfler 
17541281cf3SAxel Dörfler 
17641281cf3SAxel Dörfler void
17741281cf3SAxel Dörfler TReplicantTray::DetachedFromWindow()
17841281cf3SAxel Dörfler {
17941281cf3SAxel Dörfler #ifdef DB_ADDONS
18041281cf3SAxel Dörfler 	// clean up add-on support
181465b54cdSAxel Dörfler #if !defined(HAIKU_TARGET_PLATFORM_LIBBE_TEST)
18241281cf3SAxel Dörfler 	DeleteAddOnSupport();
18341281cf3SAxel Dörfler #endif
184465b54cdSAxel Dörfler #endif
18541281cf3SAxel Dörfler 	BView::DetachedFromWindow();
18641281cf3SAxel Dörfler }
18741281cf3SAxel Dörfler 
18841281cf3SAxel Dörfler 
18941281cf3SAxel Dörfler void
19041281cf3SAxel Dörfler TReplicantTray::RememberClockSettings()
19141281cf3SAxel Dörfler {
19241281cf3SAxel Dörfler 	if (fClock)	{
19341281cf3SAxel Dörfler 		desk_settings* settings = ((TBarApp*)be_app)->Settings();
19441281cf3SAxel Dörfler 
19541281cf3SAxel Dörfler 		settings->timeShowSeconds = fClock->ShowingSeconds();
19641281cf3SAxel Dörfler 		settings->timeShowMil = fClock->ShowingMilTime();
19741281cf3SAxel Dörfler 		settings->timeShowEuro = fClock->ShowingEuroDate();
19841281cf3SAxel Dörfler 		settings->timeFullDate = fClock->ShowingFullDate();
19941281cf3SAxel Dörfler 	}
20041281cf3SAxel Dörfler }
20141281cf3SAxel Dörfler 
20241281cf3SAxel Dörfler 
20341281cf3SAxel Dörfler bool
20441281cf3SAxel Dörfler TReplicantTray::ShowingSeconds()
20541281cf3SAxel Dörfler {
20641281cf3SAxel Dörfler 	if (fClock)
20741281cf3SAxel Dörfler 		return fClock->ShowingSeconds();
20841281cf3SAxel Dörfler 	return false;
20941281cf3SAxel Dörfler }
21041281cf3SAxel Dörfler 
21141281cf3SAxel Dörfler 
21241281cf3SAxel Dörfler bool
21341281cf3SAxel Dörfler TReplicantTray::ShowingMiltime()
21441281cf3SAxel Dörfler {
21541281cf3SAxel Dörfler 	if (fClock)
21641281cf3SAxel Dörfler 		return fClock->ShowingMilTime();
21741281cf3SAxel Dörfler 	return false;
21841281cf3SAxel Dörfler }
21941281cf3SAxel Dörfler 
22041281cf3SAxel Dörfler 
22141281cf3SAxel Dörfler bool
22241281cf3SAxel Dörfler TReplicantTray::ShowingEuroDate()
22341281cf3SAxel Dörfler {
22441281cf3SAxel Dörfler 	if (fClock)
22541281cf3SAxel Dörfler 		return fClock->ShowingEuroDate();
22641281cf3SAxel Dörfler 	return false;
22741281cf3SAxel Dörfler }
22841281cf3SAxel Dörfler 
22941281cf3SAxel Dörfler 
23041281cf3SAxel Dörfler bool
23141281cf3SAxel Dörfler TReplicantTray::ShowingFullDate()
23241281cf3SAxel Dörfler {
23341281cf3SAxel Dörfler 	if (fClock && CanShowFullDate())
23441281cf3SAxel Dörfler 		return fClock->ShowingFullDate();
23541281cf3SAxel Dörfler 	return false;
23641281cf3SAxel Dörfler }
23741281cf3SAxel Dörfler 
23841281cf3SAxel Dörfler 
23941281cf3SAxel Dörfler bool
24041281cf3SAxel Dörfler TReplicantTray::CanShowFullDate()
24141281cf3SAxel Dörfler {
24241281cf3SAxel Dörfler 	if (fClock)
24341281cf3SAxel Dörfler 		return fClock->CanShowFullDate();
24441281cf3SAxel Dörfler 	return false;
24541281cf3SAxel Dörfler }
24641281cf3SAxel Dörfler 
24741281cf3SAxel Dörfler 
24841281cf3SAxel Dörfler void
24941281cf3SAxel Dörfler TReplicantTray::DealWithClock(bool showClock)
25041281cf3SAxel Dörfler {
25141281cf3SAxel Dörfler 	fBarView->ShowClock(showClock);
25241281cf3SAxel Dörfler 
25341281cf3SAxel Dörfler 	if (showClock) {
25441281cf3SAxel Dörfler 		if (!fClock) {
25541281cf3SAxel Dörfler 			desk_settings* settings = ((TBarApp*)be_app)->Settings();
25641281cf3SAxel Dörfler 
257cb6afcb1SStephan Aßmus 			fClock = new TTimeView(fMinimumTrayWidth, kMaxReplicantHeight - 1.0,
258157cd891SAlexandre Deckner 				settings->timeShowSeconds, settings->timeShowMil,
259157cd891SAlexandre Deckner 				settings->timeFullDate,	settings->timeShowEuro, false);
26041281cf3SAxel Dörfler 			AddChild(fClock);
26141281cf3SAxel Dörfler 
26241281cf3SAxel Dörfler 			fClock->MoveTo(Bounds().right - fClock->Bounds().Width() - 1, 2);
26341281cf3SAxel Dörfler 			fClock->AllowFullDate(!IsMultiRow());
26441281cf3SAxel Dörfler 		}
26541281cf3SAxel Dörfler 	} else {
26641281cf3SAxel Dörfler 		if (fClock) {
26741281cf3SAxel Dörfler 			RememberClockSettings();
26841281cf3SAxel Dörfler 
26941281cf3SAxel Dörfler 			fClock->RemoveSelf();
27041281cf3SAxel Dörfler 			delete fClock;
27141281cf3SAxel Dörfler 			fClock = NULL;
27241281cf3SAxel Dörfler 		}
27341281cf3SAxel Dörfler 	}
27441281cf3SAxel Dörfler }
27541281cf3SAxel Dörfler 
27641281cf3SAxel Dörfler 
27766eba86fSAxel Dörfler /*!
27866eba86fSAxel Dörfler 	Width is set to a minimum of kMinimumReplicantCount by kMaxReplicantWidth
27966eba86fSAxel Dörfler 	if not in multirowmode and greater than kMinimumReplicantCount
28066eba86fSAxel Dörfler 	the width should be calculated based on the actual
28166eba86fSAxel Dörfler 	replicant widths
28241281cf3SAxel Dörfler */
28341281cf3SAxel Dörfler void
28441281cf3SAxel Dörfler TReplicantTray::GetPreferredSize(float* preferredWidth, float* preferredHeight)
28541281cf3SAxel Dörfler {
28641281cf3SAxel Dörfler 	float width = 0, height = kMinimumTrayHeight;
28741281cf3SAxel Dörfler 
28841281cf3SAxel Dörfler 	if (fMultiRowMode) {
2897da06231SAxel Dörfler 		if (fShelf->CountReplicants() > 0)
2907da06231SAxel Dörfler 			height = fRightBottomReplicant.bottom;
29141281cf3SAxel Dörfler 
29241281cf3SAxel Dörfler 		// the height will be uniform for the number of rows
29341281cf3SAxel Dörfler 		// necessary to show all the reps + any gutters
29441281cf3SAxel Dörfler 		// necessary for spacing
29541281cf3SAxel Dörfler 		int32 rowCount = (int32)(height / kMaxReplicantHeight);
29641281cf3SAxel Dörfler 		height = kGutter + (rowCount * kMaxReplicantHeight)
29741281cf3SAxel Dörfler 			+ ((rowCount - 1) * kIconGap) + kGutter;
29841281cf3SAxel Dörfler 		height = max(kMinimumTrayHeight, height);
299cb6afcb1SStephan Aßmus 		width = fMinimumTrayWidth;
30041281cf3SAxel Dörfler 	} else {
30141281cf3SAxel Dörfler 		// if last replicant overruns clock then
30241281cf3SAxel Dörfler 		// resize to accomodate
3037da06231SAxel Dörfler 		if (fShelf->CountReplicants() > 0) {
30441281cf3SAxel Dörfler 			if (fBarView->ShowingClock()
3057da06231SAxel Dörfler 				&& fRightBottomReplicant.right + 6 >= fClock->Frame().left) {
30666eba86fSAxel Dörfler 				width = fRightBottomReplicant.right + 6
30766eba86fSAxel Dörfler 					+ fClock->Frame().Width();
30841281cf3SAxel Dörfler 			} else
3097da06231SAxel Dörfler 				width = fRightBottomReplicant.right + 3;
31041281cf3SAxel Dörfler 		}
31141281cf3SAxel Dörfler 		// this view has a fixed minimum width
312cb6afcb1SStephan Aßmus 		width = max(fMinimumTrayWidth, width);
31341281cf3SAxel Dörfler 	}
31441281cf3SAxel Dörfler 
31541281cf3SAxel Dörfler 	*preferredWidth = width;
31641281cf3SAxel Dörfler 	// add 2 for the border
31741281cf3SAxel Dörfler 	*preferredHeight = height + 1;
31841281cf3SAxel Dörfler }
31941281cf3SAxel Dörfler 
32041281cf3SAxel Dörfler 
32141281cf3SAxel Dörfler void
32241281cf3SAxel Dörfler TReplicantTray::AdjustPlacement()
32341281cf3SAxel Dörfler {
32441281cf3SAxel Dörfler 	// called when an add-on has been added or removed
32541281cf3SAxel Dörfler 	// need to resize the parent of this accordingly
326a5210ab1SAxel Dörfler 
32741281cf3SAxel Dörfler 	BRect bounds = Bounds();
32841281cf3SAxel Dörfler 	float width, height;
32941281cf3SAxel Dörfler 	GetPreferredSize(&width, &height);
33041281cf3SAxel Dörfler 
33141281cf3SAxel Dörfler 	if (width == bounds.Width() && height == bounds.Height()) {
33241281cf3SAxel Dörfler 		// no need to change anything
33341281cf3SAxel Dörfler 		return;
33441281cf3SAxel Dörfler 	}
33541281cf3SAxel Dörfler 
33641281cf3SAxel Dörfler 	Parent()->ResizeToPreferred();
33741281cf3SAxel Dörfler 	fBarView->UpdatePlacement();
33841281cf3SAxel Dörfler 	Parent()->Invalidate();
33941281cf3SAxel Dörfler 	Invalidate();
34041281cf3SAxel Dörfler }
34141281cf3SAxel Dörfler 
34241281cf3SAxel Dörfler 
34341281cf3SAxel Dörfler void
34441281cf3SAxel Dörfler TReplicantTray::Draw(BRect)
34541281cf3SAxel Dörfler {
346cb6afcb1SStephan Aßmus return;
347eaa9af99SStephan Aßmus 	rgb_color menuColor = ViewColor();
34841281cf3SAxel Dörfler 	rgb_color vdark = tint_color(menuColor, B_DARKEN_3_TINT);
34941281cf3SAxel Dörfler 	rgb_color light = tint_color(menuColor, B_LIGHTEN_2_TINT);
35041281cf3SAxel Dörfler 
35141281cf3SAxel Dörfler 	BRect frame(Bounds());
35241281cf3SAxel Dörfler 
35341281cf3SAxel Dörfler 	SetHighColor(light);
35441281cf3SAxel Dörfler 	StrokeLine(frame.LeftBottom(), frame.RightBottom());
35541281cf3SAxel Dörfler 	StrokeLine(frame.RightBottom(), frame.RightTop());
35641281cf3SAxel Dörfler 
35741281cf3SAxel Dörfler 	SetHighColor(vdark);
35841281cf3SAxel Dörfler 	StrokeLine(frame.RightTop(), frame.LeftTop());
35941281cf3SAxel Dörfler 	StrokeLine(frame.LeftTop(), frame.LeftBottom());
36041281cf3SAxel Dörfler }
36141281cf3SAxel Dörfler 
36241281cf3SAxel Dörfler 
36341281cf3SAxel Dörfler void
36441281cf3SAxel Dörfler TReplicantTray::MessageReceived(BMessage* message)
36541281cf3SAxel Dörfler {
36641281cf3SAxel Dörfler 	switch (message->what) {
36741281cf3SAxel Dörfler 		case 'time':
36841281cf3SAxel Dörfler 			// from context menu in clock and in this view
36941281cf3SAxel Dörfler 			DealWithClock(!fBarView->ShowingClock());
37041281cf3SAxel Dörfler 			RealignReplicants();
37141281cf3SAxel Dörfler 			AdjustPlacement();
37241281cf3SAxel Dörfler 			break;
37341281cf3SAxel Dörfler 
374a5210ab1SAxel Dörfler 		case 'Trfm':
37541281cf3SAxel Dörfler 			// time string reformat -> realign
37641281cf3SAxel Dörfler 			DealWithClock(fBarView->ShowingClock());
37741281cf3SAxel Dörfler 			RealignReplicants();
37841281cf3SAxel Dörfler 			AdjustPlacement();
37941281cf3SAxel Dörfler 			break;
38041281cf3SAxel Dörfler 
381*71bd3ba5SJonas Sundström 		case kShowSeconds:
382*71bd3ba5SJonas Sundström 		case kMilTime:
383*71bd3ba5SJonas Sundström 		case kEuroDate:
384*71bd3ba5SJonas Sundström 		case kFullDate:
38541281cf3SAxel Dörfler 			if (fClock != NULL)
38641281cf3SAxel Dörfler 				Window()->PostMessage(message, fClock);
38741281cf3SAxel Dörfler 			break;
38841281cf3SAxel Dörfler 
38941281cf3SAxel Dörfler #ifdef DB_ADDONS
39041281cf3SAxel Dörfler 		case B_NODE_MONITOR:
39141281cf3SAxel Dörfler 		case B_QUERY_UPDATE:
39241281cf3SAxel Dörfler 			HandleEntryUpdate(message);
39341281cf3SAxel Dörfler 			break;
39441281cf3SAxel Dörfler #endif
39541281cf3SAxel Dörfler 
39641281cf3SAxel Dörfler 		default:
39741281cf3SAxel Dörfler 			BView::MessageReceived(message);
39841281cf3SAxel Dörfler 			break;
39941281cf3SAxel Dörfler 	}
40041281cf3SAxel Dörfler }
40141281cf3SAxel Dörfler 
40241281cf3SAxel Dörfler 
40341281cf3SAxel Dörfler void
40441281cf3SAxel Dörfler TReplicantTray::ShowReplicantMenu(BPoint point)
40541281cf3SAxel Dörfler {
40641281cf3SAxel Dörfler 	BPopUpMenu* menu = new BPopUpMenu("", false, false);
40741281cf3SAxel Dörfler 	menu->SetFont(be_plain_font);
40841281cf3SAxel Dörfler 
40941281cf3SAxel Dörfler 	// If the clock is visible, show the extended menu
41041281cf3SAxel Dörfler 	// otheriwse, show "Show Time".
41141281cf3SAxel Dörfler 
41241281cf3SAxel Dörfler 	if (fBarView->ShowingClock())
41341281cf3SAxel Dörfler 		fClock->ShowClockOptions(ConvertToScreen(point));
41441281cf3SAxel Dörfler 	else {
41541281cf3SAxel Dörfler 		BMenuItem* item = new BMenuItem("Show Time", new BMessage('time'));
41641281cf3SAxel Dörfler 		menu->AddItem(item);
41741281cf3SAxel Dörfler 		menu->SetTargetForItems(this);
41841281cf3SAxel Dörfler 		BPoint where = ConvertToScreen(point);
41941281cf3SAxel Dörfler 		menu->Go(where, true, true, BRect(where - BPoint(4, 4),
42041281cf3SAxel Dörfler 			where + BPoint(4, 4)), true);
42141281cf3SAxel Dörfler 	}
42241281cf3SAxel Dörfler }
42341281cf3SAxel Dörfler 
42441281cf3SAxel Dörfler 
42541281cf3SAxel Dörfler void
42641281cf3SAxel Dörfler TReplicantTray::MouseDown(BPoint where)
42741281cf3SAxel Dörfler {
42841281cf3SAxel Dörfler #ifdef DB_ADDONS
42941281cf3SAxel Dörfler 	if (modifiers() & B_CONTROL_KEY)
43041281cf3SAxel Dörfler 		DumpList(fItemList);
43141281cf3SAxel Dörfler #endif
43241281cf3SAxel Dörfler 
43341281cf3SAxel Dörfler 	uint32	buttons;
43441281cf3SAxel Dörfler 
43541281cf3SAxel Dörfler 	Window()->CurrentMessage()->FindInt32("buttons", (int32*)&buttons);
43641281cf3SAxel Dörfler 	if (buttons == B_SECONDARY_MOUSE_BUTTON) {
43741281cf3SAxel Dörfler 		ShowReplicantMenu(where);
43841281cf3SAxel Dörfler 	} else {
43941281cf3SAxel Dörfler 		BPoint save = where;
44041281cf3SAxel Dörfler 		bigtime_t doubleClickSpeed;
44141281cf3SAxel Dörfler 		bigtime_t start = system_time();
44241281cf3SAxel Dörfler 		uint32 buttons;
44341281cf3SAxel Dörfler 
44441281cf3SAxel Dörfler 		get_click_speed(&doubleClickSpeed);
44541281cf3SAxel Dörfler 
44641281cf3SAxel Dörfler 		do {
44741281cf3SAxel Dörfler 			if (fabs(where.x - save.x) > 4 || fabs(where.y - save.y) > 4)
44841281cf3SAxel Dörfler 				// user moved out of bounds of click area
44941281cf3SAxel Dörfler 				break;
45041281cf3SAxel Dörfler 
45141281cf3SAxel Dörfler 			if ((system_time() - start) > (2 * doubleClickSpeed)) {
45241281cf3SAxel Dörfler 				ShowReplicantMenu(where);
45341281cf3SAxel Dörfler 				break;
45441281cf3SAxel Dörfler 			}
45541281cf3SAxel Dörfler 
45641281cf3SAxel Dörfler 			snooze(50000);
45741281cf3SAxel Dörfler 			GetMouse(&where, &buttons);
45841281cf3SAxel Dörfler 		} while (buttons);
45941281cf3SAxel Dörfler 	}
46041281cf3SAxel Dörfler 	BView::MouseDown(where);
46141281cf3SAxel Dörfler }
46241281cf3SAxel Dörfler 
46341281cf3SAxel Dörfler #ifdef DB_ADDONS
46441281cf3SAxel Dörfler 
46541281cf3SAxel Dörfler void
46641281cf3SAxel Dörfler TReplicantTray::InitAddOnSupport()
46741281cf3SAxel Dörfler {
46841281cf3SAxel Dörfler 	// list to maintain refs to each rep added/deleted
46941281cf3SAxel Dörfler 	fItemList = new BList();
47041281cf3SAxel Dörfler 
47141281cf3SAxel Dörfler 	bool haveKey = false;
47241281cf3SAxel Dörfler  	BPath path;
47341281cf3SAxel Dörfler 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path, true) == B_OK) {
47441281cf3SAxel Dörfler 		path.Append(kDeskbarSecurityCodeFile);
47541281cf3SAxel Dörfler 
47641281cf3SAxel Dörfler 		BFile file(path.Path(),B_READ_ONLY);
47741281cf3SAxel Dörfler 		if (file.InitCheck() == B_OK
478*71bd3ba5SJonas Sundström 			&& file.Read(&fDeskbarSecurityCode,	sizeof(fDeskbarSecurityCode))
479*71bd3ba5SJonas Sundström 				== sizeof(fDeskbarSecurityCode))
48041281cf3SAxel Dörfler 			haveKey = true;
48141281cf3SAxel Dörfler 	}
48241281cf3SAxel Dörfler 	if (!haveKey) {
48341281cf3SAxel Dörfler 		// create the security code
48441281cf3SAxel Dörfler 		bigtime_t real = real_time_clock_usecs();
48541281cf3SAxel Dörfler 		bigtime_t boot = system_time();
48641281cf3SAxel Dörfler 		// two computers would have to have exactly matching clocks, and launch
487*71bd3ba5SJonas Sundström 		// Deskbar at the exact same time into the bootsequence in order for
488*71bd3ba5SJonas Sundström 		// their security-ID to be identical
48966eba86fSAxel Dörfler 		fDeskbarSecurityCode = ((real & 0xffffffffULL) << 32)
49066eba86fSAxel Dörfler 			| (boot & 0xffffffffULL);
49141281cf3SAxel Dörfler 
49241281cf3SAxel Dörfler 	if (find_directory (B_USER_SETTINGS_DIRECTORY, &path, true) == B_OK) {
49341281cf3SAxel Dörfler 			path.Append(kDeskbarSecurityCodeFile);
494*71bd3ba5SJonas Sundström 			BFile file(path.Path(), B_WRITE_ONLY | B_CREATE_FILE
495*71bd3ba5SJonas Sundström 				| B_ERASE_FILE);
49641281cf3SAxel Dörfler 			if (file.InitCheck() == B_OK)
49741281cf3SAxel Dörfler 				file.Write(&fDeskbarSecurityCode, sizeof(fDeskbarSecurityCode));
49841281cf3SAxel Dörfler 		}
49941281cf3SAxel Dörfler 	}
50041281cf3SAxel Dörfler 
50141281cf3SAxel Dörfler 	// for each volume currently mounted
50241281cf3SAxel Dörfler 	//		index the volume with our indices
50341281cf3SAxel Dörfler 	BVolumeRoster roster;
50441281cf3SAxel Dörfler 	BVolume volume;
50541281cf3SAxel Dörfler 	while (roster.GetNextVolume(&volume) == B_OK) {
50641281cf3SAxel Dörfler 		fs_create_index(volume.Device(), kStatusPredicate, B_STRING_TYPE, 0);
50741281cf3SAxel Dörfler 		RunAddOnQuery(&volume, kEnabledPredicate);
50841281cf3SAxel Dörfler 	}
50941281cf3SAxel Dörfler 
51041281cf3SAxel Dörfler 	// we also watch for volumes mounted and unmounted
51141281cf3SAxel Dörfler 	watch_node(NULL, B_WATCH_MOUNT | B_WATCH_ATTR, this, Window());
51241281cf3SAxel Dörfler }
51341281cf3SAxel Dörfler 
51441281cf3SAxel Dörfler 
51541281cf3SAxel Dörfler void
51641281cf3SAxel Dörfler TReplicantTray::DeleteAddOnSupport()
51741281cf3SAxel Dörfler {
51841281cf3SAxel Dörfler 	for (int32 i = fItemList->CountItems(); i-- > 0 ;) {
51941281cf3SAxel Dörfler 		DeskbarItemInfo* item = (DeskbarItemInfo*)fItemList->RemoveItem(i);
52041281cf3SAxel Dörfler 		if (item) {
52141281cf3SAxel Dörfler 			if (item->isAddOn)
52241281cf3SAxel Dörfler 				watch_node(&(item->nodeRef), B_STOP_WATCHING, this, Window());
52341281cf3SAxel Dörfler 
52441281cf3SAxel Dörfler 			delete item;
52541281cf3SAxel Dörfler 		}
52641281cf3SAxel Dörfler 	}
52741281cf3SAxel Dörfler 	delete fItemList;
52841281cf3SAxel Dörfler 
52941281cf3SAxel Dörfler 	// stop the volume mount/unmount watch
53041281cf3SAxel Dörfler 	stop_watching(this, Window());
53141281cf3SAxel Dörfler }
53241281cf3SAxel Dörfler 
53341281cf3SAxel Dörfler 
53441281cf3SAxel Dörfler void
53541281cf3SAxel Dörfler TReplicantTray::RunAddOnQuery(BVolume* volume, const char* predicate)
53641281cf3SAxel Dörfler {
53741281cf3SAxel Dörfler 	// Since the new BFS supports querying for attributes without
53841281cf3SAxel Dörfler 	// an index, we only run the query if the index exists (for
53941281cf3SAxel Dörfler 	// newly mounted devices only - the Deskbar will automatically
54041281cf3SAxel Dörfler 	// create an index for every device mounted at startup).
54141281cf3SAxel Dörfler 	index_info info;
54266eba86fSAxel Dörfler 	if (!volume->KnowsQuery()
54366eba86fSAxel Dörfler 		|| fs_stat_index(volume->Device(), kStatusPredicate, &info) != 0)
54441281cf3SAxel Dörfler 		return;
54541281cf3SAxel Dörfler 
54641281cf3SAxel Dörfler 	// run a new query on a specific volume
54741281cf3SAxel Dörfler 	// make it live
54841281cf3SAxel Dörfler 	BQuery query;
54941281cf3SAxel Dörfler 	query.SetVolume(volume);
55041281cf3SAxel Dörfler 	query.SetPredicate(predicate);
55141281cf3SAxel Dörfler 	query.Fetch();
55241281cf3SAxel Dörfler 
55341281cf3SAxel Dörfler 	int32 id;
55441281cf3SAxel Dörfler 	BEntry entry;
5557da06231SAxel Dörfler 	while (query.GetNextEntry(&entry) == B_OK) {
55641281cf3SAxel Dörfler 		// scan any entries returned
55741281cf3SAxel Dörfler 		// attempt to load them as add-ons
55841281cf3SAxel Dörfler 		// collisions are handled in LoadAddOn
55941281cf3SAxel Dörfler 		LoadAddOn(&entry, &id);
56041281cf3SAxel Dörfler 	}
5617da06231SAxel Dörfler }
56241281cf3SAxel Dörfler 
56341281cf3SAxel Dörfler 
56441281cf3SAxel Dörfler bool
56541281cf3SAxel Dörfler TReplicantTray::IsAddOn(entry_ref &ref)
56641281cf3SAxel Dörfler {
56741281cf3SAxel Dörfler 	BFile file(&ref, B_READ_ONLY);
56841281cf3SAxel Dörfler 
56941281cf3SAxel Dörfler 	char status[64];
57066eba86fSAxel Dörfler 	ssize_t size = file.ReadAttr(kStatusPredicate, B_STRING_TYPE, 0, &status,
57166eba86fSAxel Dörfler 		sizeof(status));
57241281cf3SAxel Dörfler 	return size > 0;
57341281cf3SAxel Dörfler }
57441281cf3SAxel Dörfler 
57541281cf3SAxel Dörfler 
57641281cf3SAxel Dörfler DeskbarItemInfo*
57741281cf3SAxel Dörfler TReplicantTray::DeskbarItemFor(node_ref &nodeRef)
57841281cf3SAxel Dörfler {
57941281cf3SAxel Dörfler 	for (int32 i = fItemList->CountItems(); i-- > 0 ;) {
58066eba86fSAxel Dörfler 		DeskbarItemInfo* item = (DeskbarItemInfo*)fItemList->ItemAt(i);
58141281cf3SAxel Dörfler 		if (item == NULL)
58241281cf3SAxel Dörfler 			continue;
58341281cf3SAxel Dörfler 
58441281cf3SAxel Dörfler 		if (item->nodeRef == nodeRef)
58541281cf3SAxel Dörfler 			return item;
58641281cf3SAxel Dörfler 	}
58741281cf3SAxel Dörfler 
58841281cf3SAxel Dörfler 	return NULL;
58941281cf3SAxel Dörfler }
59041281cf3SAxel Dörfler 
59141281cf3SAxel Dörfler 
59241281cf3SAxel Dörfler DeskbarItemInfo*
59341281cf3SAxel Dörfler TReplicantTray::DeskbarItemFor(int32 id)
59441281cf3SAxel Dörfler {
59541281cf3SAxel Dörfler 	for (int32 i = fItemList->CountItems(); i-- > 0 ;) {
59666eba86fSAxel Dörfler 		DeskbarItemInfo* item = (DeskbarItemInfo*)fItemList->ItemAt(i);
59741281cf3SAxel Dörfler 		if (item == NULL)
59841281cf3SAxel Dörfler 			continue;
59941281cf3SAxel Dörfler 
60041281cf3SAxel Dörfler 		if (item->id == id)
60141281cf3SAxel Dörfler 			return item;
60241281cf3SAxel Dörfler 	}
60341281cf3SAxel Dörfler 
60441281cf3SAxel Dörfler 	return NULL;
60541281cf3SAxel Dörfler }
60641281cf3SAxel Dörfler 
60741281cf3SAxel Dörfler 
60841281cf3SAxel Dörfler bool
60941281cf3SAxel Dörfler TReplicantTray::NodeExists(node_ref &nodeRef)
61041281cf3SAxel Dörfler {
61141281cf3SAxel Dörfler 	return DeskbarItemFor(nodeRef) != NULL;
61241281cf3SAxel Dörfler }
61341281cf3SAxel Dörfler 
61441281cf3SAxel Dörfler 
61566eba86fSAxel Dörfler /*!	This handles B_NODE_MONITOR & B_QUERY_UPDATE messages received
61666eba86fSAxel Dörfler 	for the registered add-ons.
61741281cf3SAxel Dörfler */
61841281cf3SAxel Dörfler void
61941281cf3SAxel Dörfler TReplicantTray::HandleEntryUpdate(BMessage* message)
62041281cf3SAxel Dörfler {
62141281cf3SAxel Dörfler 	int32 opcode;
62241281cf3SAxel Dörfler 	if (message->FindInt32("opcode", &opcode) != B_OK)
62341281cf3SAxel Dörfler 		return;
62441281cf3SAxel Dörfler 
62541281cf3SAxel Dörfler 	BPath path;
62641281cf3SAxel Dörfler 	switch (opcode) {
62741281cf3SAxel Dörfler 		case B_ENTRY_CREATED:
62841281cf3SAxel Dörfler 		{
62966eba86fSAxel Dörfler 			// entry was just listed, matches live query
63041281cf3SAxel Dörfler 			const char* name;
63141281cf3SAxel Dörfler 			ino_t directory;
63241281cf3SAxel Dörfler 			dev_t device;
63341281cf3SAxel Dörfler 			// received when an app adds a ref to the
63441281cf3SAxel Dörfler 			// Deskbar add-ons folder
63541281cf3SAxel Dörfler 			if (message->FindString("name", &name) == B_OK
63641281cf3SAxel Dörfler 				&& message->FindInt64("directory", &directory) == B_OK
63741281cf3SAxel Dörfler 				&& message->FindInt32("device", &device) == B_OK) {
63841281cf3SAxel Dörfler 				entry_ref ref(device, directory, name);
63941281cf3SAxel Dörfler 				// see if this item has the attribute
64041281cf3SAxel Dörfler 				// that we expect
64141281cf3SAxel Dörfler 				if (IsAddOn(ref)) {
64241281cf3SAxel Dörfler 					int32 id;
64341281cf3SAxel Dörfler 					BEntry entry(&ref);
64441281cf3SAxel Dörfler 					LoadAddOn(&entry, &id);
64541281cf3SAxel Dörfler 				}
64641281cf3SAxel Dörfler 			}
64741281cf3SAxel Dörfler 			break;
64866eba86fSAxel Dörfler 		}
64941281cf3SAxel Dörfler 
65041281cf3SAxel Dörfler 		case B_ATTR_CHANGED:
65166eba86fSAxel Dörfler 		{
65241281cf3SAxel Dörfler 			// from node watch on individual items
65341281cf3SAxel Dörfler 			// (node_watch added in LoadAddOn)
65441281cf3SAxel Dörfler 			node_ref nodeRef;
65541281cf3SAxel Dörfler 			if (message->FindInt32("device", &(nodeRef.device)) == B_OK
65641281cf3SAxel Dörfler 				&& message->FindInt64("node", &(nodeRef.node)) == B_OK) {
65741281cf3SAxel Dörfler 				// get the add-on this is for
65841281cf3SAxel Dörfler 				DeskbarItemInfo* item = DeskbarItemFor(nodeRef);
65941281cf3SAxel Dörfler 				if (item == NULL)
66041281cf3SAxel Dörfler 					break;
66141281cf3SAxel Dörfler 
66241281cf3SAxel Dörfler 				BFile file(&item->entryRef, B_READ_ONLY);
66341281cf3SAxel Dörfler 
66441281cf3SAxel Dörfler 				char status[255];
66541281cf3SAxel Dörfler 				ssize_t size = file.ReadAttr(kStatusPredicate,
66641281cf3SAxel Dörfler 					B_STRING_TYPE, 0, status, sizeof(status) - 1);
66741281cf3SAxel Dörfler 				status[sizeof(status) - 1] = '\0';
66841281cf3SAxel Dörfler 
66941281cf3SAxel Dörfler 				// attribute was removed
67041281cf3SAxel Dörfler 				if (size == B_ENTRY_NOT_FOUND) {
67141281cf3SAxel Dörfler 					// cleans up and removes node_watch
67241281cf3SAxel Dörfler 					UnloadAddOn(&nodeRef, NULL, true, false);
67341281cf3SAxel Dörfler 				} else if (!strcmp(status, "enable")) {
67441281cf3SAxel Dörfler 					int32 id;
67541281cf3SAxel Dörfler 					BEntry entry(&item->entryRef, true);
67641281cf3SAxel Dörfler 					LoadAddOn(&entry, &id);
67741281cf3SAxel Dörfler 				}
67841281cf3SAxel Dörfler 			}
67941281cf3SAxel Dörfler 			break;
68066eba86fSAxel Dörfler 		}
68141281cf3SAxel Dörfler 
68241281cf3SAxel Dörfler 		case B_ENTRY_MOVED:
68341281cf3SAxel Dörfler 		{
68441281cf3SAxel Dörfler 			entry_ref ref;
68541281cf3SAxel Dörfler 			ino_t todirectory;
68641281cf3SAxel Dörfler 			ino_t node;
68741281cf3SAxel Dörfler 			const char* name;
68841281cf3SAxel Dörfler 			if (message->FindString("name", &name) == B_OK
689*71bd3ba5SJonas Sundström 				&& message->FindInt64("from directory", &(ref.directory))
690*71bd3ba5SJonas Sundström 				== B_OK
69141281cf3SAxel Dörfler 				&& message->FindInt64("to directory", &todirectory) == B_OK
69241281cf3SAxel Dörfler 				&& message->FindInt32("device", &(ref.device)) == B_OK
69341281cf3SAxel Dörfler 				&& message->FindInt64("node", &node) == B_OK ) {
69441281cf3SAxel Dörfler 
69541281cf3SAxel Dörfler 				if (!name)
69641281cf3SAxel Dörfler 					break;
69741281cf3SAxel Dörfler 
69841281cf3SAxel Dörfler 				ref.set_name(name);
69941281cf3SAxel Dörfler 				// change the directory reference to
70041281cf3SAxel Dörfler 				// the new directory
70141281cf3SAxel Dörfler 				MoveItem(&ref, todirectory);
70241281cf3SAxel Dörfler 			}
70341281cf3SAxel Dörfler 			break;
70466eba86fSAxel Dörfler 		}
70541281cf3SAxel Dörfler 
70641281cf3SAxel Dörfler 		case B_ENTRY_REMOVED:
70741281cf3SAxel Dörfler 		{
70841281cf3SAxel Dörfler 			// entry was rm'd from the device
70941281cf3SAxel Dörfler 			node_ref nodeRef;
71041281cf3SAxel Dörfler 			if (message->FindInt32("device", &(nodeRef.device)) == B_OK
71141281cf3SAxel Dörfler 				&& message->FindInt64("node", &(nodeRef.node)) == B_OK) {
71241281cf3SAxel Dörfler 				DeskbarItemInfo* item = DeskbarItemFor(nodeRef);
71341281cf3SAxel Dörfler 				if (item == NULL)
71441281cf3SAxel Dörfler 					break;
71541281cf3SAxel Dörfler 
71641281cf3SAxel Dörfler 				// If there is a team running where the add-on comes from,
71741281cf3SAxel Dörfler 				// we don't want to remove the icon yet.
71841281cf3SAxel Dörfler 				if (be_roster->IsRunning(&item->entryRef))
71941281cf3SAxel Dörfler 					break;
72041281cf3SAxel Dörfler 
72141281cf3SAxel Dörfler 				UnloadAddOn(&nodeRef, NULL, true, false);
72241281cf3SAxel Dörfler 			}
72341281cf3SAxel Dörfler 			break;
72466eba86fSAxel Dörfler 		}
72541281cf3SAxel Dörfler 
72641281cf3SAxel Dörfler 		case B_DEVICE_MOUNTED:
72741281cf3SAxel Dörfler 		{
72841281cf3SAxel Dörfler 			// run a new query on the new device
72941281cf3SAxel Dörfler 			dev_t device;
73041281cf3SAxel Dörfler 			if (message->FindInt32("new device", &device) != B_OK)
73141281cf3SAxel Dörfler 				break;
73241281cf3SAxel Dörfler 
73341281cf3SAxel Dörfler 			RunAddOnQuery(new BVolume(device), kEnabledPredicate);
73441281cf3SAxel Dörfler 			break;
73566eba86fSAxel Dörfler 		}
73666eba86fSAxel Dörfler 
73741281cf3SAxel Dörfler 		case B_DEVICE_UNMOUNTED:
73841281cf3SAxel Dörfler 		{
73941281cf3SAxel Dörfler 			// remove all items associated with the device
74041281cf3SAxel Dörfler 			// unmounted
74141281cf3SAxel Dörfler 			// contrary to what the BeBook says, the item is called "device",
74241281cf3SAxel Dörfler 			// not "new device" like it is for B_DEVICE_MOUNTED
74341281cf3SAxel Dörfler 			dev_t device;
74441281cf3SAxel Dörfler 			if (message->FindInt32("device", &device) != B_OK)
74541281cf3SAxel Dörfler 				break;
74641281cf3SAxel Dörfler 
74741281cf3SAxel Dörfler 			UnloadAddOn(NULL, &device, false, true);
74841281cf3SAxel Dörfler 			break;
74941281cf3SAxel Dörfler 		}
75041281cf3SAxel Dörfler 	}
75166eba86fSAxel Dörfler }
75241281cf3SAxel Dörfler 
75341281cf3SAxel Dörfler 
75466eba86fSAxel Dörfler /*!
75566eba86fSAxel Dörfler 	The add-ons must support the exported C function API
75666eba86fSAxel Dörfler 	if they do, they will be loaded and added to deskbar
75766eba86fSAxel Dörfler 	primary function is the Instantiate function
7587da06231SAxel Dörfler */
75941281cf3SAxel Dörfler status_t
76041281cf3SAxel Dörfler TReplicantTray::LoadAddOn(BEntry* entry, int32* id, bool force)
76141281cf3SAxel Dörfler {
76241281cf3SAxel Dörfler 	if (!entry)
76341281cf3SAxel Dörfler 		return B_ERROR;
76441281cf3SAxel Dörfler 
76541281cf3SAxel Dörfler 	node_ref nodeRef;
76641281cf3SAxel Dörfler 	entry->GetNodeRef(&nodeRef);
76741281cf3SAxel Dörfler 	// no duplicates
76841281cf3SAxel Dörfler 	if (NodeExists(nodeRef))
76941281cf3SAxel Dörfler 		return B_ERROR;
77041281cf3SAxel Dörfler 
77141281cf3SAxel Dörfler 	BNode node(entry);
77241281cf3SAxel Dörfler 	if (!force) {
77341281cf3SAxel Dörfler 		status_t error = node.InitCheck();
77441281cf3SAxel Dörfler 		if (error != B_OK)
77541281cf3SAxel Dörfler 			return error;
77641281cf3SAxel Dörfler 
77741281cf3SAxel Dörfler 		uint64 deskbarID;
77866eba86fSAxel Dörfler 		ssize_t size = node.ReadAttr(kDeskbarSecurityCodeAttr, B_UINT64_TYPE,
77966eba86fSAxel Dörfler 			0, &deskbarID, sizeof(fDeskbarSecurityCode));
78066eba86fSAxel Dörfler 		if (size != sizeof(fDeskbarSecurityCode)
78166eba86fSAxel Dörfler 			|| deskbarID != fDeskbarSecurityCode) {
78241281cf3SAxel Dörfler 			// no code or code doesn't match
78341281cf3SAxel Dörfler 			return B_ERROR;
78441281cf3SAxel Dörfler 		}
78541281cf3SAxel Dörfler 	}
78641281cf3SAxel Dörfler 
78741281cf3SAxel Dörfler 	BPath path;
78866eba86fSAxel Dörfler 	status_t status = entry->GetPath(&path);
78966eba86fSAxel Dörfler 	if (status < B_OK)
79066eba86fSAxel Dörfler 		return status;
79141281cf3SAxel Dörfler 
79241281cf3SAxel Dörfler 	// load the add-on
79341281cf3SAxel Dörfler 	image_id image = load_add_on(path.Path());
79466eba86fSAxel Dörfler 	if (image < B_OK)
79566eba86fSAxel Dörfler 		return image;
79641281cf3SAxel Dörfler 
79741281cf3SAxel Dörfler 	// get the view loading function symbol
79841281cf3SAxel Dörfler 	//    we first look for a symbol that takes an image_id
79941281cf3SAxel Dörfler 	//    and entry_ref pointer, if not found, go with normal
80041281cf3SAxel Dörfler 	//    instantiate function
80141281cf3SAxel Dörfler 	BView* (*entryFunction)(image_id, const entry_ref*);
80241281cf3SAxel Dörfler 	BView* (*itemFunction)(void);
80341281cf3SAxel Dörfler 	BView* view = NULL;
80441281cf3SAxel Dörfler 
80541281cf3SAxel Dörfler 	entry_ref ref;
80641281cf3SAxel Dörfler 	entry->GetRef(&ref);
80741281cf3SAxel Dörfler 
80841281cf3SAxel Dörfler 	if (get_image_symbol(image, kInstantiateEntryCFunctionName,
80966eba86fSAxel Dörfler 			B_SYMBOL_TYPE_TEXT, (void**)&entryFunction) >= B_OK) {
81041281cf3SAxel Dörfler 		view = (*entryFunction)(image, &ref);
81141281cf3SAxel Dörfler 	} else if (get_image_symbol(image, kInstantiateItemCFunctionName,
81266eba86fSAxel Dörfler 			B_SYMBOL_TYPE_TEXT, (void**)&itemFunction) >= B_OK) {
81341281cf3SAxel Dörfler 		view = (*itemFunction)();
81441281cf3SAxel Dörfler 	} else {
81541281cf3SAxel Dörfler 		unload_add_on(image);
81641281cf3SAxel Dörfler 		return B_ERROR;
81741281cf3SAxel Dörfler 	}
81841281cf3SAxel Dörfler 
81966eba86fSAxel Dörfler 	if (view == NULL || IconExists(view->Name())) {
82041281cf3SAxel Dörfler 		delete view;
82141281cf3SAxel Dörfler 		unload_add_on(image);
82241281cf3SAxel Dörfler 		return B_ERROR;
82341281cf3SAxel Dörfler 	}
82441281cf3SAxel Dörfler 
82541281cf3SAxel Dörfler 	BMessage* data = new BMessage;
82641281cf3SAxel Dörfler 	view->Archive(data);
82741281cf3SAxel Dörfler 	delete view;
82841281cf3SAxel Dörfler 
82941281cf3SAxel Dörfler 	AddIcon(data, id, &ref);
83041281cf3SAxel Dörfler 		// add the rep; adds info to list
83141281cf3SAxel Dörfler 
83241281cf3SAxel Dörfler 	node.WriteAttr(kDeskbarSecurityCodeAttr, B_UINT64_TYPE, 0,
83341281cf3SAxel Dörfler 		&fDeskbarSecurityCode, sizeof(fDeskbarSecurityCode));
83441281cf3SAxel Dörfler 
83541281cf3SAxel Dörfler 	return B_OK;
83641281cf3SAxel Dörfler }
83741281cf3SAxel Dörfler 
83841281cf3SAxel Dörfler 
83941281cf3SAxel Dörfler status_t
84041281cf3SAxel Dörfler TReplicantTray::AddItem(int32 id, node_ref nodeRef, BEntry &entry, bool isAddOn)
84141281cf3SAxel Dörfler {
84241281cf3SAxel Dörfler 	DeskbarItemInfo* item = new DeskbarItemInfo;
84341281cf3SAxel Dörfler 	if (item == NULL)
84441281cf3SAxel Dörfler 		return B_NO_MEMORY;
84541281cf3SAxel Dörfler 
84641281cf3SAxel Dörfler 	item->id = id;
84741281cf3SAxel Dörfler 	item->isAddOn = isAddOn;
84841281cf3SAxel Dörfler 
84941281cf3SAxel Dörfler 	if (entry.GetRef(&item->entryRef) < B_OK) {
85041281cf3SAxel Dörfler 		item->entryRef.device = -1;
85141281cf3SAxel Dörfler 		item->entryRef.directory = -1;
85241281cf3SAxel Dörfler 		item->entryRef.name = NULL;
85341281cf3SAxel Dörfler 	}
85441281cf3SAxel Dörfler 	item->nodeRef = nodeRef;
85541281cf3SAxel Dörfler 
85641281cf3SAxel Dörfler 	fItemList->AddItem(item);
85741281cf3SAxel Dörfler 
85841281cf3SAxel Dörfler 	if (isAddOn)
85941281cf3SAxel Dörfler 		watch_node(&nodeRef, B_WATCH_NAME | B_WATCH_ATTR, this, Window());
86041281cf3SAxel Dörfler 
86141281cf3SAxel Dörfler 	return B_OK;
86241281cf3SAxel Dörfler }
86341281cf3SAxel Dörfler 
86441281cf3SAxel Dörfler 
86541281cf3SAxel Dörfler /**	from entry_removed message, when attribute removed
86641281cf3SAxel Dörfler  *	or when a device is unmounted (use removeall, by device)
86741281cf3SAxel Dörfler  */
86841281cf3SAxel Dörfler 
86941281cf3SAxel Dörfler void
87041281cf3SAxel Dörfler TReplicantTray::UnloadAddOn(node_ref* nodeRef, dev_t* device,
87141281cf3SAxel Dörfler 	bool which, bool removeAll)
87241281cf3SAxel Dörfler {
87341281cf3SAxel Dörfler 	for (int32 i = fItemList->CountItems(); i-- > 0 ;) {
87441281cf3SAxel Dörfler 		DeskbarItemInfo* item = (DeskbarItemInfo*)fItemList->ItemAt(i);
87541281cf3SAxel Dörfler 		if (!item)
87641281cf3SAxel Dörfler 			continue;
87741281cf3SAxel Dörfler 
87841281cf3SAxel Dörfler 		if ((which && nodeRef && item->nodeRef == *nodeRef)
87941281cf3SAxel Dörfler 			|| (device && item->nodeRef.device == *device)) {
88041281cf3SAxel Dörfler 
88141281cf3SAxel Dörfler 			if (device && be_roster->IsRunning(&item->entryRef))
88241281cf3SAxel Dörfler 				continue;
88341281cf3SAxel Dörfler 
88441281cf3SAxel Dörfler 			RemoveIcon(item->id);
88541281cf3SAxel Dörfler 
88641281cf3SAxel Dörfler 			if (!removeAll)
88741281cf3SAxel Dörfler 				break;
88841281cf3SAxel Dörfler 		}
88941281cf3SAxel Dörfler 	}
89041281cf3SAxel Dörfler }
89141281cf3SAxel Dörfler 
89241281cf3SAxel Dörfler 
89341281cf3SAxel Dörfler void
89441281cf3SAxel Dörfler TReplicantTray::RemoveItem(int32 id)
89541281cf3SAxel Dörfler {
89641281cf3SAxel Dörfler 	DeskbarItemInfo* item = DeskbarItemFor(id);
89741281cf3SAxel Dörfler 	if (item == NULL)
89841281cf3SAxel Dörfler 		return;
89941281cf3SAxel Dörfler 
90041281cf3SAxel Dörfler 	// attribute was added via Deskbar API (AddItem(entry_ref*, int32*)
90141281cf3SAxel Dörfler 	if (item->isAddOn) {
90241281cf3SAxel Dörfler 		BNode node(&item->entryRef);
90341281cf3SAxel Dörfler 		node.RemoveAttr(kStatusPredicate);
90441281cf3SAxel Dörfler 		watch_node(&item->nodeRef, B_STOP_WATCHING, this, Window());
90541281cf3SAxel Dörfler 	}
90641281cf3SAxel Dörfler 
90741281cf3SAxel Dörfler 	fItemList->RemoveItem(item);
90841281cf3SAxel Dörfler 	delete item;
90941281cf3SAxel Dörfler }
91041281cf3SAxel Dörfler 
91141281cf3SAxel Dörfler 
91241281cf3SAxel Dörfler /**	ENTRY_MOVED message, moving only occurs on a device
91341281cf3SAxel Dörfler  *	copying will occur (ENTRY_CREATED) between devices
91441281cf3SAxel Dörfler  */
91541281cf3SAxel Dörfler 
91641281cf3SAxel Dörfler void
91741281cf3SAxel Dörfler TReplicantTray::MoveItem(entry_ref* ref, ino_t toDirectory)
91841281cf3SAxel Dörfler {
91941281cf3SAxel Dörfler 	if (!ref)
92041281cf3SAxel Dörfler 		return;
92141281cf3SAxel Dörfler 
92241281cf3SAxel Dörfler 	// scan for a matching entry_ref and update it
92341281cf3SAxel Dörfler 	//
92441281cf3SAxel Dörfler 	// don't need to change node info as it does not change
92541281cf3SAxel Dörfler 
92641281cf3SAxel Dörfler 	for (int32 i = fItemList->CountItems(); i-- > 0 ;) {
92741281cf3SAxel Dörfler 		DeskbarItemInfo* item = (DeskbarItemInfo*)fItemList->ItemAt(i);
92841281cf3SAxel Dörfler 		if (!item)
92941281cf3SAxel Dörfler 			continue;
93041281cf3SAxel Dörfler 
93141281cf3SAxel Dörfler 		if (!strcmp(item->entryRef.name, ref->name)
93241281cf3SAxel Dörfler 			&& item->entryRef.device == ref->device
93341281cf3SAxel Dörfler 			&& item->entryRef.directory == ref->directory) {
93441281cf3SAxel Dörfler 			item->entryRef.directory = toDirectory;
93541281cf3SAxel Dörfler 			break;
93641281cf3SAxel Dörfler 		}
93741281cf3SAxel Dörfler 	}
93841281cf3SAxel Dörfler }
93941281cf3SAxel Dörfler 
94041281cf3SAxel Dörfler #endif	//	add-on support
94141281cf3SAxel Dörfler 
94241281cf3SAxel Dörfler //	external add-on API routines
94341281cf3SAxel Dörfler //	called using the new BDeskbar class
94441281cf3SAxel Dörfler 
94541281cf3SAxel Dörfler //	existence of icon/replicant by name or ID
94641281cf3SAxel Dörfler //	returns opposite
94741281cf3SAxel Dörfler //	note: name and id are semi-private limiting
94841281cf3SAxel Dörfler //		the ability of non-host apps to remove
94941281cf3SAxel Dörfler //		icons without a little bit of work
95041281cf3SAxel Dörfler 
95141281cf3SAxel Dörfler /**	for a specific id
95241281cf3SAxel Dörfler  *	return the name of the replicant (name of view)
95341281cf3SAxel Dörfler  */
95441281cf3SAxel Dörfler 
95541281cf3SAxel Dörfler status_t
95641281cf3SAxel Dörfler TReplicantTray::ItemInfo(int32 id, const char** name)
95741281cf3SAxel Dörfler {
95841281cf3SAxel Dörfler 	if (id < 0)
95941281cf3SAxel Dörfler 		return B_ERROR;
96041281cf3SAxel Dörfler 
96141281cf3SAxel Dörfler 	int32 index, temp;
96241281cf3SAxel Dörfler 	BView* view = ViewAt(&index, &temp, id, false);
96341281cf3SAxel Dörfler 	if (view) {
96441281cf3SAxel Dörfler 		*name = view->Name();
96541281cf3SAxel Dörfler 		return B_OK;
96641281cf3SAxel Dörfler 	}
96741281cf3SAxel Dörfler 
96841281cf3SAxel Dörfler 	return B_ERROR;
96941281cf3SAxel Dörfler }
97041281cf3SAxel Dörfler 
97141281cf3SAxel Dörfler 
9727da06231SAxel Dörfler /**	for a specific name
9737da06231SAxel Dörfler  *	return the id (internal to Deskbar)
9747da06231SAxel Dörfler  */
9757da06231SAxel Dörfler 
97641281cf3SAxel Dörfler status_t
97741281cf3SAxel Dörfler TReplicantTray::ItemInfo(const char* name, int32* id)
97841281cf3SAxel Dörfler {
97941281cf3SAxel Dörfler 	if (!name || strlen(name) <= 0)
98041281cf3SAxel Dörfler 		return B_ERROR;
98141281cf3SAxel Dörfler 
98241281cf3SAxel Dörfler 	int32 index;
98341281cf3SAxel Dörfler 	BView* view = ViewAt(&index, id, name);
98441281cf3SAxel Dörfler 	if (view)
98541281cf3SAxel Dörfler 		return B_OK;
98641281cf3SAxel Dörfler 
98741281cf3SAxel Dörfler 	return B_ERROR;
98841281cf3SAxel Dörfler }
98941281cf3SAxel Dörfler 
99041281cf3SAxel Dörfler 
9917da06231SAxel Dörfler /**	at a specific index
9927da06231SAxel Dörfler  *	return both the name and the id of the replicant
9937da06231SAxel Dörfler  */
9947da06231SAxel Dörfler 
99541281cf3SAxel Dörfler status_t
99641281cf3SAxel Dörfler TReplicantTray::ItemInfo(int32 index, const char** name, int32* id)
99741281cf3SAxel Dörfler {
99841281cf3SAxel Dörfler 	if (index < 0)
99941281cf3SAxel Dörfler 		return B_ERROR;
100041281cf3SAxel Dörfler 
100141281cf3SAxel Dörfler 	BView* view;
100241281cf3SAxel Dörfler 	fShelf->ReplicantAt(index, &view, (uint32*)id, NULL);
100341281cf3SAxel Dörfler 	if (view) {
100441281cf3SAxel Dörfler 		*name = view->Name();
100541281cf3SAxel Dörfler 		return B_OK;
100641281cf3SAxel Dörfler 	}
100741281cf3SAxel Dörfler 
100841281cf3SAxel Dörfler 	return B_ERROR;
100941281cf3SAxel Dörfler }
101041281cf3SAxel Dörfler 
101141281cf3SAxel Dörfler 
10127da06231SAxel Dörfler /**	replicant exists, by id/index */
10137da06231SAxel Dörfler 
101441281cf3SAxel Dörfler bool
101541281cf3SAxel Dörfler TReplicantTray::IconExists(int32 target, bool byIndex)
101641281cf3SAxel Dörfler {
101741281cf3SAxel Dörfler 	int32 index, id;
101841281cf3SAxel Dörfler 	BView* view = ViewAt(&index, &id, target, byIndex);
101941281cf3SAxel Dörfler 
102041281cf3SAxel Dörfler 	return view && index >= 0;
102141281cf3SAxel Dörfler }
102241281cf3SAxel Dörfler 
102341281cf3SAxel Dörfler 
10247da06231SAxel Dörfler /**	replicant exists, by name */
10257da06231SAxel Dörfler 
102641281cf3SAxel Dörfler bool
102741281cf3SAxel Dörfler TReplicantTray::IconExists(const char* name)
102841281cf3SAxel Dörfler {
102941281cf3SAxel Dörfler 	if (!name || strlen(name) == 0)
103041281cf3SAxel Dörfler 		return false;
103141281cf3SAxel Dörfler 
103241281cf3SAxel Dörfler 	int32 index, id;
103341281cf3SAxel Dörfler 	BView* view = ViewAt(&index, &id, name);
103441281cf3SAxel Dörfler 
103541281cf3SAxel Dörfler 	return view && index >= 0;
103641281cf3SAxel Dörfler }
103741281cf3SAxel Dörfler 
103841281cf3SAxel Dörfler 
103941281cf3SAxel Dörfler int32
104041281cf3SAxel Dörfler TReplicantTray::IconCount() const
104141281cf3SAxel Dörfler {
104241281cf3SAxel Dörfler 	return fShelf->CountReplicants();
104341281cf3SAxel Dörfler }
104441281cf3SAxel Dörfler 
104541281cf3SAxel Dörfler 
1046ced3755cSAxel Dörfler /*!	Message must contain an archivable view for later rehydration.
1047ced3755cSAxel Dörfler 	This function takes over ownership of the provided message on success
1048ced3755cSAxel Dörfler 	only.
1049ced3755cSAxel Dörfler 	Returns the current replicant ID.
10507da06231SAxel Dörfler */
105141281cf3SAxel Dörfler status_t
1052ced3755cSAxel Dörfler TReplicantTray::AddIcon(BMessage* archive, int32* id, const entry_ref* addOn)
105341281cf3SAxel Dörfler {
1054ced3755cSAxel Dörfler 	if (archive == NULL || id == NULL)
105541281cf3SAxel Dörfler 		return B_ERROR;
105641281cf3SAxel Dörfler 
10579c50f36eSAxel Dörfler 	// find entry_ref
10589c50f36eSAxel Dörfler 
10599c50f36eSAxel Dörfler 	entry_ref ref;
10609c50f36eSAxel Dörfler 	if (addOn) {
10619c50f36eSAxel Dörfler 		// Use it if we got it
10629c50f36eSAxel Dörfler 		ref = *addOn;
10639c50f36eSAxel Dörfler 	} else {
10649c50f36eSAxel Dörfler 		const char* signature;
1065ced3755cSAxel Dörfler 		status_t status = archive->FindString("add_on", &signature);
10669c50f36eSAxel Dörfler 		if (status == B_OK) {
10679c50f36eSAxel Dörfler 			BRoster roster;
10689c50f36eSAxel Dörfler 			status = roster.FindApp(signature, &ref);
10699c50f36eSAxel Dörfler 		}
10709c50f36eSAxel Dörfler 		if (status < B_OK)
10719c50f36eSAxel Dörfler 			return status;
10729c50f36eSAxel Dörfler 	}
10739c50f36eSAxel Dörfler 
10749c50f36eSAxel Dörfler 	BFile file;
10759c50f36eSAxel Dörfler 	status_t status = file.SetTo(&ref, B_READ_ONLY);
10769c50f36eSAxel Dörfler 	if (status < B_OK)
10779c50f36eSAxel Dörfler 		return status;
10789c50f36eSAxel Dörfler 
10799c50f36eSAxel Dörfler 	node_ref nodeRef;
10809c50f36eSAxel Dörfler 	status = file.GetNodeRef(&nodeRef);
10819c50f36eSAxel Dörfler 	if (status < B_OK)
10829c50f36eSAxel Dörfler 		return status;
10839c50f36eSAxel Dörfler 
10849c50f36eSAxel Dörfler 	BEntry entry(&ref, true);
10859c50f36eSAxel Dörfler 		// ToDo: this resolves an eventual link for the item
10869c50f36eSAxel Dörfler 		// being added - this is okay for now, but in multi-user
10879c50f36eSAxel Dörfler 		// environments, one might want to have links that
10889c50f36eSAxel Dörfler 		// carry the be:deskbar_item_status attribute
10899c50f36eSAxel Dörfler 	status = entry.InitCheck();
10909c50f36eSAxel Dörfler 	if (status != B_OK)
10919c50f36eSAxel Dörfler 		return status;
10929c50f36eSAxel Dörfler 
109341281cf3SAxel Dörfler 	*id = 999;
1094ced3755cSAxel Dörfler 	if (archive->what == B_ARCHIVED_OBJECT)
1095ced3755cSAxel Dörfler 		archive->what = 0;
109641281cf3SAxel Dörfler 
1097ced3755cSAxel Dörfler 	BRect originalBounds = archive->FindRect("_frame");
1098a5210ab1SAxel Dörfler 		// this is a work-around for buggy replicants that change their
1099a5210ab1SAxel Dörfler 		// size in AttachedToWindow() (such as "SVM")
1100a5210ab1SAxel Dörfler 
110141281cf3SAxel Dörfler 	// !! check for name collisions?
1102ced3755cSAxel Dörfler 	status = fShelf->AddReplicant(archive, BPoint(1, 1));
11039c50f36eSAxel Dörfler 	if (status != B_OK)
11049c50f36eSAxel Dörfler 		return status;
110541281cf3SAxel Dörfler 
1106a5210ab1SAxel Dörfler 	int32 count = fShelf->CountReplicants();
1107a5210ab1SAxel Dörfler 	BView* view;
1108a5210ab1SAxel Dörfler 	fShelf->ReplicantAt(count - 1, &view, (uint32*)id, NULL);
1109a5210ab1SAxel Dörfler 
1110a5210ab1SAxel Dörfler 	if (originalBounds != view->Bounds()) {
1111a5210ab1SAxel Dörfler 		// The replicant changed its size when added to the window, so we need
1112*71bd3ba5SJonas Sundström 		// to recompute all over again (it's already done once via
1113*71bd3ba5SJonas Sundström 		// BShelf::AddReplicant() and TReplicantShelf::CanAcceptReplicantView())
1114a5210ab1SAxel Dörfler 		RealignReplicants();
1115a5210ab1SAxel Dörfler 	}
1116a5210ab1SAxel Dörfler 
111741281cf3SAxel Dörfler 	float oldWidth = Bounds().Width();
111841281cf3SAxel Dörfler 	float oldHeight = Bounds().Height();
111941281cf3SAxel Dörfler 	float width, height;
112041281cf3SAxel Dörfler 	GetPreferredSize(&width, &height);
112141281cf3SAxel Dörfler 	if (oldWidth != width || oldHeight != height)
112241281cf3SAxel Dörfler 		AdjustPlacement();
112341281cf3SAxel Dörfler 
112441281cf3SAxel Dörfler 	// add the item to the add-on list
112541281cf3SAxel Dörfler 
112641281cf3SAxel Dörfler 	AddItem(*id, nodeRef, entry, addOn != NULL);
112741281cf3SAxel Dörfler  	return B_OK;
112841281cf3SAxel Dörfler }
112941281cf3SAxel Dörfler 
113041281cf3SAxel Dörfler 
113141281cf3SAxel Dörfler void
113241281cf3SAxel Dörfler TReplicantTray::RemoveIcon(int32 target, bool byIndex)
113341281cf3SAxel Dörfler {
113441281cf3SAxel Dörfler 	if (target < 0)
113541281cf3SAxel Dörfler 		return;
113641281cf3SAxel Dörfler 
113741281cf3SAxel Dörfler 	int32 index, id;
113841281cf3SAxel Dörfler 	BView* view = ViewAt(&index, &id, target, byIndex);
113941281cf3SAxel Dörfler 	if (view && index >= 0) {
11407da06231SAxel Dörfler 		// remove the reference from the item list & the shelf
114141281cf3SAxel Dörfler 		RemoveItem(id);
114241281cf3SAxel Dörfler 		fShelf->DeleteReplicant(index);
11437da06231SAxel Dörfler 
114441281cf3SAxel Dörfler 		// force a placement update,  !! need to fix BShelf
114541281cf3SAxel Dörfler 		RealReplicantAdjustment(index);
114641281cf3SAxel Dörfler 	}
114741281cf3SAxel Dörfler }
114841281cf3SAxel Dörfler 
114941281cf3SAxel Dörfler 
115041281cf3SAxel Dörfler void
115141281cf3SAxel Dörfler TReplicantTray::RemoveIcon(const char* name)
115241281cf3SAxel Dörfler {
115341281cf3SAxel Dörfler 	if (!name || strlen(name) <= 0)
115441281cf3SAxel Dörfler 		return;
115541281cf3SAxel Dörfler 
115641281cf3SAxel Dörfler 	int32 id, index;
115741281cf3SAxel Dörfler 	BView* view = ViewAt(&index, &id, name);
115841281cf3SAxel Dörfler 	if (view && index >= 0) {
11597da06231SAxel Dörfler 		// remove the reference from the item list & shelf
116041281cf3SAxel Dörfler 		RemoveItem(id);
116141281cf3SAxel Dörfler 		fShelf->DeleteReplicant(index);
11627da06231SAxel Dörfler 
116341281cf3SAxel Dörfler 		// force a placement update,  !! need to fix BShelf
116441281cf3SAxel Dörfler 		RealReplicantAdjustment(index);
116541281cf3SAxel Dörfler 	}
116641281cf3SAxel Dörfler }
116741281cf3SAxel Dörfler 
116841281cf3SAxel Dörfler 
116941281cf3SAxel Dörfler void
11707da06231SAxel Dörfler TReplicantTray::RealReplicantAdjustment(int32 startIndex)
117141281cf3SAxel Dörfler {
11727da06231SAxel Dörfler 	if (startIndex < 0)
117341281cf3SAxel Dörfler 		return;
11747da06231SAxel Dörfler 
11757da06231SAxel Dörfler 	if (startIndex == fLastReplicant)
11767da06231SAxel Dörfler 		startIndex = 0;
11777da06231SAxel Dörfler 
117841281cf3SAxel Dörfler 	// reset the locations of all replicants after the one deleted
11797da06231SAxel Dörfler 	RealignReplicants(startIndex);
118041281cf3SAxel Dörfler 
118141281cf3SAxel Dörfler 	float oldWidth = Bounds().Width();
118241281cf3SAxel Dörfler 	float oldHeight = Bounds().Height();
118341281cf3SAxel Dörfler 	float width, height;
118441281cf3SAxel Dörfler 	GetPreferredSize(&width, &height);
118541281cf3SAxel Dörfler 	if (oldWidth != width || oldHeight != height) {
118641281cf3SAxel Dörfler 		// resize view to accomodate the replicants
118741281cf3SAxel Dörfler 		// redraw as necessary
118841281cf3SAxel Dörfler 		AdjustPlacement();
118941281cf3SAxel Dörfler 	}
119041281cf3SAxel Dörfler }
119141281cf3SAxel Dörfler 
119241281cf3SAxel Dörfler 
11937da06231SAxel Dörfler /**	looking for a replicant by id/index
11947da06231SAxel Dörfler  *	return the view and index
11957da06231SAxel Dörfler  */
11967da06231SAxel Dörfler 
119741281cf3SAxel Dörfler BView*
119841281cf3SAxel Dörfler TReplicantTray::ViewAt(int32* index, int32* id, int32 target, bool byIndex)
119941281cf3SAxel Dörfler {
120041281cf3SAxel Dörfler 	*index = -1;
120141281cf3SAxel Dörfler 
120241281cf3SAxel Dörfler 	BView* view;
120341281cf3SAxel Dörfler 	if (byIndex){
120441281cf3SAxel Dörfler 		if (fShelf->ReplicantAt(target, &view, (uint32*)id)) {
120541281cf3SAxel Dörfler 			if (view) {
120641281cf3SAxel Dörfler 				*index = target;
120741281cf3SAxel Dörfler 				return view;
120841281cf3SAxel Dörfler 			}
120941281cf3SAxel Dörfler 		}
121041281cf3SAxel Dörfler 	} else {
121141281cf3SAxel Dörfler 		int32 count = fShelf->CountReplicants()-1;
121241281cf3SAxel Dörfler 		int32 localid;
121341281cf3SAxel Dörfler 		for (int32 repIndex = count ; repIndex >= 0 ; repIndex--) {
121441281cf3SAxel Dörfler 			fShelf->ReplicantAt(repIndex, &view, (uint32*)&localid);
121541281cf3SAxel Dörfler 			if (localid == target && view) {
121641281cf3SAxel Dörfler 				*index = repIndex;
121741281cf3SAxel Dörfler 				*id = localid;
121841281cf3SAxel Dörfler 				return view;
121941281cf3SAxel Dörfler 			}
122041281cf3SAxel Dörfler 		}
122141281cf3SAxel Dörfler 	}
122241281cf3SAxel Dörfler 
122341281cf3SAxel Dörfler 	return NULL;
122441281cf3SAxel Dörfler }
122541281cf3SAxel Dörfler 
122641281cf3SAxel Dörfler 
12277da06231SAxel Dörfler /**	looking for a replicant with a view by name
12287da06231SAxel Dörfler  *	return the view, index and the id of the replicant
12297da06231SAxel Dörfler  */
12307da06231SAxel Dörfler 
123141281cf3SAxel Dörfler BView*
123241281cf3SAxel Dörfler TReplicantTray::ViewAt(int32* index, int32* id, const char* name)
123341281cf3SAxel Dörfler {
123441281cf3SAxel Dörfler 	*index = -1;
123541281cf3SAxel Dörfler 	*id = -1;
123641281cf3SAxel Dörfler 
123741281cf3SAxel Dörfler 	BView* view;
123841281cf3SAxel Dörfler 	int32 count = fShelf->CountReplicants()-1;
123941281cf3SAxel Dörfler 	for (int32 repIndex = count ; repIndex >= 0 ; repIndex--) {
124041281cf3SAxel Dörfler 		fShelf->ReplicantAt(repIndex, &view, (uint32*)id);
124141281cf3SAxel Dörfler 		if (view && view->Name() && strcmp(name, view->Name()) == 0) {
124241281cf3SAxel Dörfler 			*index = repIndex;
124341281cf3SAxel Dörfler 			return view;
124441281cf3SAxel Dörfler 		}
124541281cf3SAxel Dörfler 	}
124641281cf3SAxel Dörfler 
124741281cf3SAxel Dörfler 	return NULL;
124841281cf3SAxel Dörfler }
124941281cf3SAxel Dörfler 
125041281cf3SAxel Dörfler 
12517da06231SAxel Dörfler /**	Shelf will call to determine where and if
12527da06231SAxel Dörfler  *	the replicant is to be added
12537da06231SAxel Dörfler  */
12547da06231SAxel Dörfler 
125541281cf3SAxel Dörfler bool
125641281cf3SAxel Dörfler TReplicantTray::AcceptAddon(BRect replicantFrame, BMessage* message)
125741281cf3SAxel Dörfler {
125841281cf3SAxel Dörfler 	if (!message)
125941281cf3SAxel Dörfler 		return false;
126041281cf3SAxel Dörfler 
126141281cf3SAxel Dörfler 	if (replicantFrame.Height() > kMaxReplicantHeight)
126241281cf3SAxel Dörfler 		return false;
126341281cf3SAxel Dörfler 
126441281cf3SAxel Dörfler 	alignment align = B_ALIGN_LEFT;
126541281cf3SAxel Dörfler 	if (fAlignmentSupport && message->HasBool("deskbar:dynamic_align")) {
126641281cf3SAxel Dörfler 		if (!fBarView->Vertical())
126741281cf3SAxel Dörfler 			align = B_ALIGN_RIGHT;
126841281cf3SAxel Dörfler 		else
126941281cf3SAxel Dörfler 			align = fBarView->Left() ? B_ALIGN_LEFT : B_ALIGN_RIGHT;
127041281cf3SAxel Dörfler 	} else if (message->HasInt32("deskbar:align"))
127141281cf3SAxel Dörfler 		message->FindInt32("deskbar:align", (int32*)&align);
127241281cf3SAxel Dörfler 
127341281cf3SAxel Dörfler 	if (message->HasInt32("deskbar:private_align"))
127441281cf3SAxel Dörfler 		message->FindInt32("deskbar:private_align", (int32*)&align);
127541281cf3SAxel Dörfler 	else
127641281cf3SAxel Dörfler 		align = B_ALIGN_LEFT;
127741281cf3SAxel Dörfler 
12787da06231SAxel Dörfler 	BPoint loc = LocationForReplicant(fShelf->CountReplicants(),
12797da06231SAxel Dörfler 		replicantFrame.Width());
128041281cf3SAxel Dörfler 
128141281cf3SAxel Dörfler 	message->AddPoint("_pjp_loc", loc);
128241281cf3SAxel Dörfler 	return true;
128341281cf3SAxel Dörfler }
128441281cf3SAxel Dörfler 
128541281cf3SAxel Dörfler 
128641281cf3SAxel Dörfler /**	based on the previous (index - 1) replicant in the list
128741281cf3SAxel Dörfler  *	calculate where the left point should be for this
128841281cf3SAxel Dörfler  *	replicant.  replicant will flow to the right on its own
128941281cf3SAxel Dörfler  */
129041281cf3SAxel Dörfler 
129141281cf3SAxel Dörfler BPoint
12927da06231SAxel Dörfler TReplicantTray::LocationForReplicant(int32 index, float width)
129341281cf3SAxel Dörfler {
129441281cf3SAxel Dörfler 	BPoint loc(kIconGap + 1, kGutter + 1);
129541281cf3SAxel Dörfler 
12967da06231SAxel Dörfler 	if (fMultiRowMode) {
12977da06231SAxel Dörfler 		// try to find free space in every row
12987da06231SAxel Dörfler 		for (int32 row = 0; ; loc.y += kMaxReplicantHeight + kIconGap, row++) {
12997da06231SAxel Dörfler 			// determine free space in this row
1300*71bd3ba5SJonas Sundström 			BRect rect(loc.x, loc.y, loc.x + fMinimumTrayWidth - kIconGap - 2.0,
1301*71bd3ba5SJonas Sundström 				loc.y + kMaxReplicantHeight);
13027da06231SAxel Dörfler 			if (row == 0 && fBarView->ShowingClock())
13037da06231SAxel Dörfler 				rect.right -= fClock->Frame().Width() + kIconGap;
13047da06231SAxel Dörfler 
13057da06231SAxel Dörfler 			for (int32 i = 0; i < index; i++) {
13067da06231SAxel Dörfler 				BView* view = NULL;
13077da06231SAxel Dörfler 				fShelf->ReplicantAt(i, &view);
13087da06231SAxel Dörfler 				if (view == NULL || view->Frame().top != rect.top)
13097da06231SAxel Dörfler 					continue;
13107da06231SAxel Dörfler 
13117da06231SAxel Dörfler 				rect.left = view->Frame().right + kIconGap + 1;
13127da06231SAxel Dörfler 			}
13137da06231SAxel Dörfler 
13147da06231SAxel Dörfler 			if (rect.Width() >= width) {
13157da06231SAxel Dörfler 				// the icon fits in this row
13167da06231SAxel Dörfler 				loc = rect.LeftTop();
13177da06231SAxel Dörfler 				break;
13187da06231SAxel Dörfler 			}
13197da06231SAxel Dörfler 		}
13207da06231SAxel Dörfler 	} else {
132141281cf3SAxel Dörfler 		if (index > 0) {
132241281cf3SAxel Dörfler 			// get the last replicant added for placement reference
132341281cf3SAxel Dörfler 			BView* view = NULL;
13247da06231SAxel Dörfler 			fShelf->ReplicantAt(index - 1, &view);
132541281cf3SAxel Dörfler 			if (view) {
132641281cf3SAxel Dörfler 				// push this rep placement past the last one
132741281cf3SAxel Dörfler 				loc.x = view->Frame().right + kIconGap + 1;
132841281cf3SAxel Dörfler 				loc.y = view->Frame().top;
132941281cf3SAxel Dörfler 			}
133041281cf3SAxel Dörfler 		}
13317da06231SAxel Dörfler 	}
133241281cf3SAxel Dörfler 
1333*71bd3ba5SJonas Sundström 	if ((loc.y == fRightBottomReplicant.top && loc.x
1334*71bd3ba5SJonas Sundström 		> fRightBottomReplicant.left) || loc.y > fRightBottomReplicant.top) {
1335*71bd3ba5SJonas Sundström 		fRightBottomReplicant.Set(loc.x, loc.y, loc.x + width, loc.y
1336*71bd3ba5SJonas Sundström 		+ kMaxReplicantHeight);
13377da06231SAxel Dörfler 		fLastReplicant = index;
133841281cf3SAxel Dörfler 	}
13397da06231SAxel Dörfler 
134041281cf3SAxel Dörfler 	return loc;
134141281cf3SAxel Dörfler }
134241281cf3SAxel Dörfler 
134341281cf3SAxel Dörfler 
134441281cf3SAxel Dörfler BRect
134541281cf3SAxel Dörfler TReplicantTray::IconFrame(int32 target, bool byIndex)
134641281cf3SAxel Dörfler {
134741281cf3SAxel Dörfler 	int32 index, id;
134841281cf3SAxel Dörfler 	BView* view = ViewAt(&index, &id, target, byIndex);
134941281cf3SAxel Dörfler 	if (view)
135041281cf3SAxel Dörfler 		return view->Frame();
135141281cf3SAxel Dörfler 
135241281cf3SAxel Dörfler 	return BRect(0, 0, 0, 0);
135341281cf3SAxel Dörfler }
135441281cf3SAxel Dörfler 
135541281cf3SAxel Dörfler 
135641281cf3SAxel Dörfler BRect
135741281cf3SAxel Dörfler TReplicantTray::IconFrame(const char* name)
135841281cf3SAxel Dörfler {
135941281cf3SAxel Dörfler 	if (!name)
136041281cf3SAxel Dörfler 		return BRect(0, 0, 0, 0);
136141281cf3SAxel Dörfler 
136241281cf3SAxel Dörfler 	int32 id, index;
136341281cf3SAxel Dörfler 	BView* view = ViewAt(&index, &id, name);
136441281cf3SAxel Dörfler 	if (view)
136541281cf3SAxel Dörfler 		return view->Frame();
136641281cf3SAxel Dörfler 
136741281cf3SAxel Dörfler 	return BRect(0, 0, 0, 0);
136841281cf3SAxel Dörfler }
136941281cf3SAxel Dörfler 
137041281cf3SAxel Dörfler 
13717da06231SAxel Dörfler /**	Scan from the startIndex and reset the location
13727da06231SAxel Dörfler  *	as defined in LocationForReplicant()
137341281cf3SAxel Dörfler  */
137441281cf3SAxel Dörfler 
137541281cf3SAxel Dörfler void
137641281cf3SAxel Dörfler TReplicantTray::RealignReplicants(int32 startIndex)
137741281cf3SAxel Dörfler {
137841281cf3SAxel Dörfler 	if (startIndex < 0)
137941281cf3SAxel Dörfler 		startIndex = 0;
138041281cf3SAxel Dörfler 
138141281cf3SAxel Dörfler 	int32 count = fShelf->CountReplicants();
138241281cf3SAxel Dörfler 	if (count <= 0)
138341281cf3SAxel Dörfler 		return;
138441281cf3SAxel Dörfler 
13857da06231SAxel Dörfler 	if (startIndex == 0)
13867da06231SAxel Dörfler 		fRightBottomReplicant.Set(0, 0, 0, 0);
13877da06231SAxel Dörfler 
138841281cf3SAxel Dörfler 	BView* view = NULL;
138941281cf3SAxel Dörfler 	for (int32 i = startIndex ; i < count ; i++){
139041281cf3SAxel Dörfler 		fShelf->ReplicantAt(i, &view);
13910bec7100SStefano Ceccherini 		if (view != NULL) {
13927da06231SAxel Dörfler 			BPoint loc = LocationForReplicant(i, view->Frame().Width());
13930bec7100SStefano Ceccherini 			if (view->Frame().LeftTop() != loc)
139441281cf3SAxel Dörfler 				view->MoveTo(loc);
139541281cf3SAxel Dörfler 		}
139641281cf3SAxel Dörfler 	}
139741281cf3SAxel Dörfler }
139841281cf3SAxel Dörfler 
139941281cf3SAxel Dörfler 
140041281cf3SAxel Dörfler void
140141281cf3SAxel Dörfler TReplicantTray::SetMultiRow(bool state)
140241281cf3SAxel Dörfler {
140341281cf3SAxel Dörfler 	fMultiRowMode = state;
140441281cf3SAxel Dörfler 
140541281cf3SAxel Dörfler 	// in multi-row state, we only want the short date
140641281cf3SAxel Dörfler 
140741281cf3SAxel Dörfler 	if (fClock != NULL)
140841281cf3SAxel Dörfler 		fClock->AllowFullDate(!state);
140941281cf3SAxel Dörfler }
141041281cf3SAxel Dörfler 
141141281cf3SAxel Dörfler 
141241281cf3SAxel Dörfler //	#pragma mark -
141341281cf3SAxel Dörfler 
141441281cf3SAxel Dörfler 
14157da06231SAxel Dörfler /**	draggable region that is asynchronous so that
14167da06231SAxel Dörfler  *	dragging does not block other activities
14177da06231SAxel Dörfler  */
14187da06231SAxel Dörfler 
141941281cf3SAxel Dörfler TDragRegion::TDragRegion(TBarView* parent, BView* child)
142041281cf3SAxel Dörfler 	:	BControl(BRect(0, 0, 0, 0), "", "", NULL, B_FOLLOW_NONE,
142141281cf3SAxel Dörfler 			B_WILL_DRAW | B_FRAME_EVENTS),
142241281cf3SAxel Dörfler 		fBarView(parent),
142341281cf3SAxel Dörfler 		fChild(child),
142441281cf3SAxel Dörfler 		fDragLocation(kAutoPlaceDragRegion)
142541281cf3SAxel Dörfler {
142641281cf3SAxel Dörfler }
142741281cf3SAxel Dörfler 
142841281cf3SAxel Dörfler 
142941281cf3SAxel Dörfler void
143041281cf3SAxel Dörfler TDragRegion::AttachedToWindow()
143141281cf3SAxel Dörfler {
143241281cf3SAxel Dörfler 	BView::AttachedToWindow();
1433cb6afcb1SStephan Aßmus 	if (be_control_look != NULL)
1434cb6afcb1SStephan Aßmus 		SetViewColor(tint_color(ui_color(B_MENU_BACKGROUND_COLOR), 1.1));
1435cb6afcb1SStephan Aßmus 	else
143641281cf3SAxel Dörfler 		SetViewColor(ui_color(B_MENU_BACKGROUND_COLOR));
143741281cf3SAxel Dörfler 	ResizeToPreferred();
143841281cf3SAxel Dörfler }
143941281cf3SAxel Dörfler 
144041281cf3SAxel Dörfler 
144141281cf3SAxel Dörfler void
144241281cf3SAxel Dörfler TDragRegion::GetPreferredSize(float* width, float* height)
144341281cf3SAxel Dörfler {
144441281cf3SAxel Dörfler 	fChild->ResizeToPreferred();
144541281cf3SAxel Dörfler 	*width = fChild->Bounds().Width();
144641281cf3SAxel Dörfler 	*height = fChild->Bounds().Height();
144741281cf3SAxel Dörfler 
144841281cf3SAxel Dörfler 	if (fDragLocation != kNoDragRegion)
144941281cf3SAxel Dörfler 		*width += 7;
145041281cf3SAxel Dörfler 	else
145141281cf3SAxel Dörfler 		*width += 6;
145241281cf3SAxel Dörfler 
145341281cf3SAxel Dörfler 	*height += 3;
145441281cf3SAxel Dörfler }
145541281cf3SAxel Dörfler 
145641281cf3SAxel Dörfler 
145741281cf3SAxel Dörfler void
145841281cf3SAxel Dörfler TDragRegion::FrameMoved(BPoint)
145941281cf3SAxel Dörfler {
1460*71bd3ba5SJonas Sundström 	if (fBarView->Left() && fBarView->Vertical() && fDragLocation
1461*71bd3ba5SJonas Sundström 		!= kNoDragRegion)
146241281cf3SAxel Dörfler 		fChild->MoveTo(5,2);
146341281cf3SAxel Dörfler 	else
146441281cf3SAxel Dörfler 		fChild->MoveTo(2,2);
146541281cf3SAxel Dörfler }
146641281cf3SAxel Dörfler 
146741281cf3SAxel Dörfler 
146841281cf3SAxel Dörfler void
146941281cf3SAxel Dörfler TDragRegion::Draw(BRect)
147041281cf3SAxel Dörfler {
1471eaa9af99SStephan Aßmus 	rgb_color menuColor = ViewColor();
147241281cf3SAxel Dörfler 	rgb_color hilite = tint_color(menuColor, B_DARKEN_1_TINT);
1473cb6afcb1SStephan Aßmus 	rgb_color ldark = tint_color(menuColor, 1.02);
1474cb6afcb1SStephan Aßmus 	rgb_color dark = tint_color(menuColor, B_DARKEN_2_TINT);
147541281cf3SAxel Dörfler 	rgb_color vdark = tint_color(menuColor, B_DARKEN_3_TINT);
147641281cf3SAxel Dörfler 	rgb_color vvdark = tint_color(menuColor, B_DARKEN_4_TINT);
147741281cf3SAxel Dörfler 	rgb_color light = tint_color(menuColor, B_LIGHTEN_2_TINT);
147841281cf3SAxel Dörfler 
147941281cf3SAxel Dörfler 	BRect frame(Bounds());
148041281cf3SAxel Dörfler 	BeginLineArray(4);
148141281cf3SAxel Dörfler 
1482cb6afcb1SStephan Aßmus 	if (be_control_look != NULL) {
1483cb6afcb1SStephan Aßmus 		if (fBarView->Vertical()) {
1484cb6afcb1SStephan Aßmus 			AddLine(frame.LeftTop(), frame.RightTop(), dark);
1485cb6afcb1SStephan Aßmus 			AddLine(BPoint(frame.left, frame.top + 1),
1486cb6afcb1SStephan Aßmus 				BPoint(frame.right, frame.top + 1), ldark);
1487cb6afcb1SStephan Aßmus 			AddLine(frame.LeftBottom(), frame.RightBottom(), hilite);
1488cb6afcb1SStephan Aßmus 		} else if (fBarView->AcrossTop() || fBarView->AcrossBottom()) {
1489cb6afcb1SStephan Aßmus 			AddLine(frame.LeftTop(),
1490cb6afcb1SStephan Aßmus 				BPoint(frame.left, frame.bottom), dark);
1491cb6afcb1SStephan Aßmus 			AddLine(BPoint(frame.left + 1, frame.top + 1),
1492cb6afcb1SStephan Aßmus 				BPoint(frame.right - 1, frame.top + 1), light);
1493cb6afcb1SStephan Aßmus 			AddLine(BPoint(frame.right, frame.top + 2),
1494cb6afcb1SStephan Aßmus 				BPoint(frame.right, frame.bottom), hilite);
1495cb6afcb1SStephan Aßmus 			AddLine(BPoint(frame.left + 1, frame.bottom),
1496cb6afcb1SStephan Aßmus 				BPoint(frame.right - 1, frame.bottom), hilite);
1497cb6afcb1SStephan Aßmus 		}
1498cb6afcb1SStephan Aßmus 	} else {
149941281cf3SAxel Dörfler 		if (fBarView->Vertical()) {
150041281cf3SAxel Dörfler 			AddLine(frame.LeftTop(), frame.RightTop(), light);
150141281cf3SAxel Dörfler 			AddLine(frame.LeftTop(), frame.LeftBottom(), light);
150241281cf3SAxel Dörfler 			AddLine(frame.RightBottom(), frame.RightTop(), hilite);
150341281cf3SAxel Dörfler 		} else if (fBarView->AcrossTop()) {
1504cb6afcb1SStephan Aßmus 			AddLine(BPoint(frame.left, frame.top + 1),
1505cb6afcb1SStephan Aßmus 				BPoint(frame.right - 1, frame.top + 1), light);
150641281cf3SAxel Dörfler 			AddLine(frame.RightTop(), frame.RightBottom(), vvdark);
1507cb6afcb1SStephan Aßmus 			AddLine(BPoint(frame.right - 1, frame.top + 2),
1508cb6afcb1SStephan Aßmus 				BPoint(frame.right - 1, frame.bottom - 1), hilite);
1509cb6afcb1SStephan Aßmus 			AddLine(frame.LeftBottom(),
1510cb6afcb1SStephan Aßmus 				BPoint(frame.right - 1, frame.bottom), hilite);
151141281cf3SAxel Dörfler 		} else if (fBarView->AcrossBottom()) {
1512cb6afcb1SStephan Aßmus 			AddLine(BPoint(frame.left, frame.top + 1),
1513cb6afcb1SStephan Aßmus 				BPoint(frame.right - 1, frame.top + 1), light);
151441281cf3SAxel Dörfler 			AddLine(frame.LeftBottom(), frame.RightBottom(), hilite);
151541281cf3SAxel Dörfler 			AddLine(frame.RightTop(), frame.RightBottom(), vvdark);
1516cb6afcb1SStephan Aßmus 			AddLine(BPoint(frame.right - 1, frame.top + 1),
1517cb6afcb1SStephan Aßmus 				BPoint(frame.right - 1, frame.bottom - 1), hilite);
1518cb6afcb1SStephan Aßmus 		}
151941281cf3SAxel Dörfler 	}
152041281cf3SAxel Dörfler 
152141281cf3SAxel Dörfler 	EndLineArray();
152241281cf3SAxel Dörfler 
152341281cf3SAxel Dörfler 	if (fDragLocation != kDontDrawDragRegion || fDragLocation != kNoDragRegion)
152441281cf3SAxel Dörfler 		DrawDragRegion();
152541281cf3SAxel Dörfler }
152641281cf3SAxel Dörfler 
152741281cf3SAxel Dörfler 
152841281cf3SAxel Dörfler void
152941281cf3SAxel Dörfler TDragRegion::DrawDragRegion()
153041281cf3SAxel Dörfler {
153141281cf3SAxel Dörfler 	BRect dragRegion(DragRegion());
153241281cf3SAxel Dörfler 
1533cb6afcb1SStephan Aßmus 	rgb_color menuColor = ViewColor();
1534cb6afcb1SStephan Aßmus 	rgb_color menuHilite = menuColor;
153541281cf3SAxel Dörfler 	if (IsTracking()) {
1536cb6afcb1SStephan Aßmus 		// Draw drag region highlighted if tracking mouse
1537cb6afcb1SStephan Aßmus 		menuHilite = tint_color(menuColor, B_HIGHLIGHT_BACKGROUND_TINT);
153841281cf3SAxel Dörfler 		SetHighColor(menuHilite);
153941281cf3SAxel Dörfler 		FillRect(dragRegion);
154041281cf3SAxel Dörfler 	}
1541cb6afcb1SStephan Aßmus 	rgb_color vdark = tint_color(menuHilite, B_DARKEN_3_TINT);
1542cb6afcb1SStephan Aßmus 	rgb_color light = tint_color(menuHilite, B_LIGHTEN_2_TINT);
1543cb6afcb1SStephan Aßmus 
1544cb6afcb1SStephan Aßmus 	BeginLineArray(dragRegion.IntegerHeight());
1545cb6afcb1SStephan Aßmus 	BPoint pt;
1546cb6afcb1SStephan Aßmus 	pt.x = floorf((dragRegion.left + dragRegion.right) / 2 + 0.5) - 1;
1547cb6afcb1SStephan Aßmus 	pt.y = dragRegion.top + 2;
1548cb6afcb1SStephan Aßmus 
154941281cf3SAxel Dörfler 	while (pt.y + 1 <= dragRegion.bottom) {
155041281cf3SAxel Dörfler 		AddLine(pt, pt, vdark);
155141281cf3SAxel Dörfler 		AddLine(pt + BPoint(1, 1), pt + BPoint(1, 1), light);
155241281cf3SAxel Dörfler 
155341281cf3SAxel Dörfler 		pt.y += 3;
155441281cf3SAxel Dörfler 	}
155541281cf3SAxel Dörfler 	EndLineArray();
155641281cf3SAxel Dörfler }
155741281cf3SAxel Dörfler 
155841281cf3SAxel Dörfler 
155941281cf3SAxel Dörfler BRect
156041281cf3SAxel Dörfler TDragRegion::DragRegion() const
156141281cf3SAxel Dörfler {
1562cb6afcb1SStephan Aßmus 	float kTopBottomInset = 2;
1563cb6afcb1SStephan Aßmus 	float kLeftRightInset = 1;
1564cb6afcb1SStephan Aßmus 	float kDragWidth = 3;
1565cb6afcb1SStephan Aßmus 	if (be_control_look != NULL) {
1566cb6afcb1SStephan Aßmus 		kTopBottomInset = 1;
1567cb6afcb1SStephan Aßmus 		kLeftRightInset = 0;
1568cb6afcb1SStephan Aßmus 		kDragWidth = 4;
1569cb6afcb1SStephan Aßmus 	}
1570cb6afcb1SStephan Aßmus 
157141281cf3SAxel Dörfler 	BRect dragRegion(Bounds());
1572cb6afcb1SStephan Aßmus 	dragRegion.top += kTopBottomInset;
1573cb6afcb1SStephan Aßmus 	dragRegion.bottom -= kTopBottomInset;
157441281cf3SAxel Dörfler 
157541281cf3SAxel Dörfler 	bool placeOnLeft=false;
157641281cf3SAxel Dörfler 	if (fDragLocation == kAutoPlaceDragRegion) {
157741281cf3SAxel Dörfler 		if (fBarView->Vertical() && fBarView->Left())
157841281cf3SAxel Dörfler 			placeOnLeft = true;
157941281cf3SAxel Dörfler 		else
158041281cf3SAxel Dörfler 			placeOnLeft = false;
158141281cf3SAxel Dörfler 	} else if (fDragLocation == kDragRegionLeft)
158241281cf3SAxel Dörfler 		placeOnLeft = true;
158341281cf3SAxel Dörfler 	else if (fDragLocation == kDragRegionRight)
158441281cf3SAxel Dörfler 		placeOnLeft = false;
158541281cf3SAxel Dörfler 
158641281cf3SAxel Dörfler 	if (placeOnLeft) {
1587cb6afcb1SStephan Aßmus 		dragRegion.left += kLeftRightInset;
1588cb6afcb1SStephan Aßmus 		dragRegion.right = dragRegion.left + kDragWidth;
158941281cf3SAxel Dörfler 	} else {
1590cb6afcb1SStephan Aßmus 		dragRegion.right -= kLeftRightInset;
1591cb6afcb1SStephan Aßmus 		dragRegion.left = dragRegion.right - kDragWidth;
159241281cf3SAxel Dörfler 	}
159341281cf3SAxel Dörfler 
159441281cf3SAxel Dörfler 	return dragRegion;
159541281cf3SAxel Dörfler }
159641281cf3SAxel Dörfler 
159741281cf3SAxel Dörfler 
159841281cf3SAxel Dörfler void
159941281cf3SAxel Dörfler TDragRegion::MouseDown(BPoint thePoint)
160041281cf3SAxel Dörfler {
160141281cf3SAxel Dörfler 	ulong buttons;
160241281cf3SAxel Dörfler 	BPoint where;
160341281cf3SAxel Dörfler 	BRect dragRegion(DragRegion());
160441281cf3SAxel Dörfler 
16057da06231SAxel Dörfler 	dragRegion.InsetBy(-2.0f, -2.0f);
16067da06231SAxel Dörfler 		// DragRegion() is designed for drawing, not clicking
160741281cf3SAxel Dörfler 
160841281cf3SAxel Dörfler 	if (!dragRegion.Contains(thePoint))
160941281cf3SAxel Dörfler 		return;
161041281cf3SAxel Dörfler 
161141281cf3SAxel Dörfler 	while(true) {
161241281cf3SAxel Dörfler 		GetMouse(&where, &buttons);
161341281cf3SAxel Dörfler 		if (!buttons)
161441281cf3SAxel Dörfler 			break;
161541281cf3SAxel Dörfler 
161641281cf3SAxel Dörfler 		if ((Window()->Flags() & B_ASYNCHRONOUS_CONTROLS) != 0) {
161741281cf3SAxel Dörfler 			fPreviousPosition = thePoint;
161841281cf3SAxel Dörfler 			SetTracking(true);
161941281cf3SAxel Dörfler 			SetMouseEventMask(B_POINTER_EVENTS,
162041281cf3SAxel Dörfler 				B_NO_POINTER_HISTORY | B_LOCK_WINDOW_FOCUS);
162141281cf3SAxel Dörfler 			Invalidate(DragRegion());
162241281cf3SAxel Dörfler 			break;
162341281cf3SAxel Dörfler 		}
162441281cf3SAxel Dörfler 
162541281cf3SAxel Dörfler 		snooze(25000);
162641281cf3SAxel Dörfler 	}
162741281cf3SAxel Dörfler }
162841281cf3SAxel Dörfler 
162941281cf3SAxel Dörfler 
163041281cf3SAxel Dörfler void
163141281cf3SAxel Dörfler TDragRegion::MouseUp(BPoint pt)
163241281cf3SAxel Dörfler {
163341281cf3SAxel Dörfler 	if (IsTracking()) {
163441281cf3SAxel Dörfler 		SetTracking(false);
163541281cf3SAxel Dörfler 		Invalidate(DragRegion());
163641281cf3SAxel Dörfler 	} else
163741281cf3SAxel Dörfler 		BControl::MouseUp(pt);
163841281cf3SAxel Dörfler }
163941281cf3SAxel Dörfler 
164041281cf3SAxel Dörfler 
164141281cf3SAxel Dörfler bool
164241281cf3SAxel Dörfler TDragRegion::SwitchModeForRect(BPoint mouse, BRect rect,
164341281cf3SAxel Dörfler 	bool newVertical, bool newLeft, bool newTop, int32 newState)
164441281cf3SAxel Dörfler {
164541281cf3SAxel Dörfler 	if (!rect.Contains(mouse))
164641281cf3SAxel Dörfler 		// not our rect
164741281cf3SAxel Dörfler 		return false;
164841281cf3SAxel Dörfler 
164941281cf3SAxel Dörfler 	if (newVertical == fBarView->Vertical()
165041281cf3SAxel Dörfler 		&& newLeft == fBarView->Left()
165141281cf3SAxel Dörfler 		&& newTop == fBarView->Top()
165241281cf3SAxel Dörfler 		&& newState == fBarView->State())
165341281cf3SAxel Dörfler 		// already in the correct mode
165441281cf3SAxel Dörfler 		return true;
165541281cf3SAxel Dörfler 
165641281cf3SAxel Dörfler 	fBarView->ChangeState(newState, newVertical, newLeft, newTop);
165741281cf3SAxel Dörfler 	return true;
165841281cf3SAxel Dörfler }
165941281cf3SAxel Dörfler 
166041281cf3SAxel Dörfler 
166141281cf3SAxel Dörfler void
166241281cf3SAxel Dörfler TDragRegion::MouseMoved(BPoint where, uint32 code, const BMessage* message)
166341281cf3SAxel Dörfler {
166441281cf3SAxel Dörfler 	if (IsTracking()) {
166541281cf3SAxel Dörfler 		BScreen screen;
166641281cf3SAxel Dörfler 		BRect frame = screen.Frame();
166741281cf3SAxel Dörfler 
166841281cf3SAxel Dörfler 		float hDivider = frame.Width() / 6;
1669*71bd3ba5SJonas Sundström 		hDivider = (hDivider < sMinimumWindowWidth + 10.0f)
1670*71bd3ba5SJonas Sundström 			? sMinimumWindowWidth + 10.0f : hDivider;
167141281cf3SAxel Dörfler 		float miniDivider = frame.top + kMiniHeight + 10.0f;
167241281cf3SAxel Dörfler 		float vDivider = frame.Height() / 2;
167341281cf3SAxel Dörfler #ifdef FULL_MODE
167441281cf3SAxel Dörfler 		float thirdScreen = frame.Height() / 3;
167541281cf3SAxel Dörfler #endif
1676*71bd3ba5SJonas Sundström 		BRect topLeft(frame.left, frame.top, frame.left + hDivider,
1677*71bd3ba5SJonas Sundström 			miniDivider);
1678*71bd3ba5SJonas Sundström 		BRect topMiddle(frame.left + hDivider, frame.top, frame.right
1679*71bd3ba5SJonas Sundström 			- hDivider, vDivider);
1680*71bd3ba5SJonas Sundström 		BRect topRight(frame.right - hDivider, frame.top, frame.right,
1681*71bd3ba5SJonas Sundström 			miniDivider);
168241281cf3SAxel Dörfler 
168341281cf3SAxel Dörfler #ifdef FULL_MODE
168441281cf3SAxel Dörfler 		vDivider = miniDivider + thirdScreen;
168541281cf3SAxel Dörfler #endif
1686*71bd3ba5SJonas Sundström 		BRect middleLeft(frame.left, miniDivider, frame.left + hDivider,
1687*71bd3ba5SJonas Sundström 			vDivider);
1688*71bd3ba5SJonas Sundström 		BRect middleRight(frame.right - hDivider, miniDivider, frame.right,
1689*71bd3ba5SJonas Sundström 			vDivider);
169041281cf3SAxel Dörfler 
169141281cf3SAxel Dörfler #ifdef FULL_MODE
1692*71bd3ba5SJonas Sundström 		BRect leftSide(frame.left, vDivider, frame.left + hDivider,
1693*71bd3ba5SJonas Sundström 			frame.bottom - thirdScreen);
1694*71bd3ba5SJonas Sundström 		BRect rightSide(frame.right - hDivider, vDivider, frame.right,
1695*71bd3ba5SJonas Sundström 			frame.bottom - thirdScreen);
169641281cf3SAxel Dörfler 
169741281cf3SAxel Dörfler 		vDivider = frame.bottom - thirdScreen;
169841281cf3SAxel Dörfler #endif
1699*71bd3ba5SJonas Sundström 		BRect bottomLeft(frame.left, vDivider, frame.left + hDivider,
1700*71bd3ba5SJonas Sundström 			frame.bottom);
1701*71bd3ba5SJonas Sundström 		BRect bottomMiddle(frame.left + hDivider, vDivider, frame.right
1702*71bd3ba5SJonas Sundström 			- hDivider, frame.bottom);
1703*71bd3ba5SJonas Sundström 		BRect bottomRight(frame.right - hDivider, vDivider, frame.right,
1704*71bd3ba5SJonas Sundström 			frame.bottom);
170541281cf3SAxel Dörfler 
170641281cf3SAxel Dörfler 		if (where != fPreviousPosition) {
170741281cf3SAxel Dörfler 			fPreviousPosition = where;
170841281cf3SAxel Dörfler 			ConvertToScreen(&where);
170941281cf3SAxel Dörfler 
171041281cf3SAxel Dörfler 			// use short circuit evaluation for convenience
171141281cf3SAxel Dörfler 			if (SwitchModeForRect(where, topLeft, true, true, true, kMiniState)
1712*71bd3ba5SJonas Sundström 				|| SwitchModeForRect(where, topMiddle, false, true, true,
1713*71bd3ba5SJonas Sundström 					kExpandoState)
1714*71bd3ba5SJonas Sundström 				|| SwitchModeForRect(where, topRight, true, false, true,
1715*71bd3ba5SJonas Sundström 					kMiniState)
1716*71bd3ba5SJonas Sundström 				|| SwitchModeForRect(where, middleLeft, true, true, true,
1717*71bd3ba5SJonas Sundström 					kExpandoState)
1718*71bd3ba5SJonas Sundström 				|| SwitchModeForRect(where, middleRight, true, false, true,
1719*71bd3ba5SJonas Sundström 					kExpandoState)
172041281cf3SAxel Dörfler 
172141281cf3SAxel Dörfler #ifdef FULL_MODE
1722*71bd3ba5SJonas Sundström 				|| SwitchModeForRect(where, leftSide, true, true, true,
1723*71bd3ba5SJonas Sundström 					kFullState)
1724*71bd3ba5SJonas Sundström 				|| SwitchModeForRect(where, rightSide, true, false, true,
1725*71bd3ba5SJonas Sundström 					kFullState)
172641281cf3SAxel Dörfler #endif
1727*71bd3ba5SJonas Sundström 				|| SwitchModeForRect(where, bottomLeft, true, true, false,
1728*71bd3ba5SJonas Sundström 					kMiniState)
1729*71bd3ba5SJonas Sundström 				|| SwitchModeForRect(where, bottomMiddle, false, true, false,
1730*71bd3ba5SJonas Sundström 					kExpandoState)
1731*71bd3ba5SJonas Sundström 				|| SwitchModeForRect(where, bottomRight, true, false, false,
1732*71bd3ba5SJonas Sundström 					kMiniState))
173341281cf3SAxel Dörfler 				;
173441281cf3SAxel Dörfler 		}
173541281cf3SAxel Dörfler 	} else
173641281cf3SAxel Dörfler 		BControl::MouseMoved(where, code, message);
173741281cf3SAxel Dörfler }
173841281cf3SAxel Dörfler 
173941281cf3SAxel Dörfler 
174041281cf3SAxel Dörfler int32
174141281cf3SAxel Dörfler TDragRegion::DragRegionLocation() const
174241281cf3SAxel Dörfler {
174341281cf3SAxel Dörfler 	return fDragLocation;
174441281cf3SAxel Dörfler }
174541281cf3SAxel Dörfler 
174641281cf3SAxel Dörfler 
174741281cf3SAxel Dörfler void
174841281cf3SAxel Dörfler TDragRegion::SetDragRegionLocation(int32 location)
174941281cf3SAxel Dörfler {
175041281cf3SAxel Dörfler 	if (location == fDragLocation)
175141281cf3SAxel Dörfler 		return;
175241281cf3SAxel Dörfler 
175341281cf3SAxel Dörfler 	fDragLocation = location;
175441281cf3SAxel Dörfler 	Invalidate();
175541281cf3SAxel Dörfler }
175641281cf3SAxel Dörfler 
1757