xref: /haiku/src/apps/deskbar/StatusView.cpp (revision cb6afcb1464e92f0134232e6179215295e17bc79)
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>
49*cb6afcb1SStephan Aßmus #include <Bitmap.h>
50*cb6afcb1SStephan 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 
64*cb6afcb1SStephan Aßmus #include "icons_logo.h"
65*cb6afcb1SStephan Aßmus #include "BarApp.h"
6641281cf3SAxel Dörfler #include "DeskBarUtils.h"
67*cb6afcb1SStephan 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 
89*cb6afcb1SStephan Aßmus float sMinimumWindowWidth = kGutter + kMinimumTrayWidth + kDragRegionWidth;
90*cb6afcb1SStephan 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),
130*cb6afcb1SStephan Aßmus 	fMinimumTrayWidth(kMinimumTrayWidth),
13141281cf3SAxel Dörfler 	fAlignmentSupport(false)
13241281cf3SAxel Dörfler {
133*cb6afcb1SStephan Aßmus 	// init the minimum window width according to the logo.
134*cb6afcb1SStephan Aßmus 	const BBitmap* logoBitmap = AppResSet()->FindBitmap(B_MESSAGE_TYPE,
135*cb6afcb1SStephan Aßmus 		R_BeLogoIcon);
136*cb6afcb1SStephan Aßmus 	if (logoBitmap != NULL) {
137*cb6afcb1SStephan Aßmus 		sMinimumWindowWidth = max_c(sMinimumWindowWidth,
138*cb6afcb1SStephan Aßmus 			2 * (logoBitmap->Bounds().Width() + 8));
139*cb6afcb1SStephan Aßmus 		fMinimumTrayWidth = sMinimumWindowWidth - kGutter - kDragRegionWidth;
140*cb6afcb1SStephan 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 
155*cb6afcb1SStephan Aßmus 	if (be_control_look != NULL) {
156*cb6afcb1SStephan Aßmus 		SetViewColor(Parent()->ViewColor());
157*cb6afcb1SStephan Aßmus 	} else {
15866eba86fSAxel Dörfler 		SetViewColor(tint_color(ui_color(B_MENU_BACKGROUND_COLOR),
15966eba86fSAxel Dörfler 			B_DARKEN_1_TINT));
160*cb6afcb1SStephan 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 
257*cb6afcb1SStephan 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);
299*cb6afcb1SStephan 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
312*cb6afcb1SStephan 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 {
346*cb6afcb1SStephan 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 
38141281cf3SAxel Dörfler 		case kMsgShowSeconds:
38241281cf3SAxel Dörfler 		case kMsgMilTime:
38341281cf3SAxel Dörfler 		case kMsgEuroDate:
38441281cf3SAxel Dörfler 		case kMsgFullDate:
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
47841281cf3SAxel Dörfler 			&& file.Read(&fDeskbarSecurityCode,
47941281cf3SAxel Dörfler 					sizeof(fDeskbarSecurityCode)) == 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
48741281cf3SAxel Dörfler 		// Deskbar at the exact same time into the bootsequence in order for their
48841281cf3SAxel Dörfler 		// 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);
49441281cf3SAxel Dörfler 			BFile file(path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
49541281cf3SAxel Dörfler 			if (file.InitCheck() == B_OK)
49641281cf3SAxel Dörfler 				file.Write(&fDeskbarSecurityCode, sizeof(fDeskbarSecurityCode));
49741281cf3SAxel Dörfler 		}
49841281cf3SAxel Dörfler 	}
49941281cf3SAxel Dörfler 
50041281cf3SAxel Dörfler 	// for each volume currently mounted
50141281cf3SAxel Dörfler 	//		index the volume with our indices
50241281cf3SAxel Dörfler 	BVolumeRoster roster;
50341281cf3SAxel Dörfler 	BVolume volume;
50441281cf3SAxel Dörfler 	while (roster.GetNextVolume(&volume) == B_OK) {
50541281cf3SAxel Dörfler 		fs_create_index(volume.Device(), kStatusPredicate, B_STRING_TYPE, 0);
50641281cf3SAxel Dörfler 		RunAddOnQuery(&volume, kEnabledPredicate);
50741281cf3SAxel Dörfler 	}
50841281cf3SAxel Dörfler 
50941281cf3SAxel Dörfler 	// we also watch for volumes mounted and unmounted
51041281cf3SAxel Dörfler 	watch_node(NULL, B_WATCH_MOUNT | B_WATCH_ATTR, this, Window());
51141281cf3SAxel Dörfler }
51241281cf3SAxel Dörfler 
51341281cf3SAxel Dörfler 
51441281cf3SAxel Dörfler void
51541281cf3SAxel Dörfler TReplicantTray::DeleteAddOnSupport()
51641281cf3SAxel Dörfler {
51741281cf3SAxel Dörfler 	for (int32 i = fItemList->CountItems(); i-- > 0 ;) {
51841281cf3SAxel Dörfler 		DeskbarItemInfo *item = (DeskbarItemInfo *)fItemList->RemoveItem(i);
51941281cf3SAxel Dörfler 		if (item) {
52041281cf3SAxel Dörfler 			if (item->isAddOn)
52141281cf3SAxel Dörfler 				watch_node(&(item->nodeRef), B_STOP_WATCHING, this, Window());
52241281cf3SAxel Dörfler 
52341281cf3SAxel Dörfler 			delete item;
52441281cf3SAxel Dörfler 		}
52541281cf3SAxel Dörfler 	}
52641281cf3SAxel Dörfler 	delete fItemList;
52741281cf3SAxel Dörfler 
52841281cf3SAxel Dörfler 	// stop the volume mount/unmount watch
52941281cf3SAxel Dörfler 	stop_watching(this, Window());
53041281cf3SAxel Dörfler }
53141281cf3SAxel Dörfler 
53241281cf3SAxel Dörfler 
53341281cf3SAxel Dörfler void
53441281cf3SAxel Dörfler TReplicantTray::RunAddOnQuery(BVolume *volume, const char *predicate)
53541281cf3SAxel Dörfler {
53641281cf3SAxel Dörfler 	// Since the new BFS supports querying for attributes without
53741281cf3SAxel Dörfler 	// an index, we only run the query if the index exists (for
53841281cf3SAxel Dörfler 	// newly mounted devices only - the Deskbar will automatically
53941281cf3SAxel Dörfler 	// create an index for every device mounted at startup).
54041281cf3SAxel Dörfler 	index_info info;
54166eba86fSAxel Dörfler 	if (!volume->KnowsQuery()
54266eba86fSAxel Dörfler 		|| fs_stat_index(volume->Device(), kStatusPredicate, &info) != 0)
54341281cf3SAxel Dörfler 		return;
54441281cf3SAxel Dörfler 
54541281cf3SAxel Dörfler 	// run a new query on a specific volume
54641281cf3SAxel Dörfler 	// make it live
54741281cf3SAxel Dörfler 	BQuery query;
54841281cf3SAxel Dörfler 	query.SetVolume(volume);
54941281cf3SAxel Dörfler 	query.SetPredicate(predicate);
55041281cf3SAxel Dörfler 	query.Fetch();
55141281cf3SAxel Dörfler 
55241281cf3SAxel Dörfler 	int32 id;
55341281cf3SAxel Dörfler 	BEntry entry;
5547da06231SAxel Dörfler 	while (query.GetNextEntry(&entry) == B_OK) {
55541281cf3SAxel Dörfler 		// scan any entries returned
55641281cf3SAxel Dörfler 		// attempt to load them as add-ons
55741281cf3SAxel Dörfler 		// collisions are handled in LoadAddOn
55841281cf3SAxel Dörfler 		LoadAddOn(&entry, &id);
55941281cf3SAxel Dörfler 	}
5607da06231SAxel Dörfler }
56141281cf3SAxel Dörfler 
56241281cf3SAxel Dörfler 
56341281cf3SAxel Dörfler bool
56441281cf3SAxel Dörfler TReplicantTray::IsAddOn(entry_ref &ref)
56541281cf3SAxel Dörfler {
56641281cf3SAxel Dörfler 	BFile file(&ref, B_READ_ONLY);
56741281cf3SAxel Dörfler 
56841281cf3SAxel Dörfler 	char status[64];
56966eba86fSAxel Dörfler 	ssize_t size = file.ReadAttr(kStatusPredicate, B_STRING_TYPE, 0, &status,
57066eba86fSAxel Dörfler 		sizeof(status));
57141281cf3SAxel Dörfler 	return size > 0;
57241281cf3SAxel Dörfler }
57341281cf3SAxel Dörfler 
57441281cf3SAxel Dörfler 
57541281cf3SAxel Dörfler DeskbarItemInfo *
57641281cf3SAxel Dörfler TReplicantTray::DeskbarItemFor(node_ref &nodeRef)
57741281cf3SAxel Dörfler {
57841281cf3SAxel Dörfler 	for (int32 i = fItemList->CountItems(); i-- > 0 ;) {
57966eba86fSAxel Dörfler 		DeskbarItemInfo *item = (DeskbarItemInfo *)fItemList->ItemAt(i);
58041281cf3SAxel Dörfler 		if (item == NULL)
58141281cf3SAxel Dörfler 			continue;
58241281cf3SAxel Dörfler 
58341281cf3SAxel Dörfler 		if (item->nodeRef == nodeRef)
58441281cf3SAxel Dörfler 			return item;
58541281cf3SAxel Dörfler 	}
58641281cf3SAxel Dörfler 
58741281cf3SAxel Dörfler 	return NULL;
58841281cf3SAxel Dörfler }
58941281cf3SAxel Dörfler 
59041281cf3SAxel Dörfler 
59141281cf3SAxel Dörfler DeskbarItemInfo *
59241281cf3SAxel Dörfler TReplicantTray::DeskbarItemFor(int32 id)
59341281cf3SAxel Dörfler {
59441281cf3SAxel Dörfler 	for (int32 i = fItemList->CountItems(); i-- > 0 ;) {
59566eba86fSAxel Dörfler 		DeskbarItemInfo *item = (DeskbarItemInfo *)fItemList->ItemAt(i);
59641281cf3SAxel Dörfler 		if (item == NULL)
59741281cf3SAxel Dörfler 			continue;
59841281cf3SAxel Dörfler 
59941281cf3SAxel Dörfler 		if (item->id == id)
60041281cf3SAxel Dörfler 			return item;
60141281cf3SAxel Dörfler 	}
60241281cf3SAxel Dörfler 
60341281cf3SAxel Dörfler 	return NULL;
60441281cf3SAxel Dörfler }
60541281cf3SAxel Dörfler 
60641281cf3SAxel Dörfler 
60741281cf3SAxel Dörfler bool
60841281cf3SAxel Dörfler TReplicantTray::NodeExists(node_ref &nodeRef)
60941281cf3SAxel Dörfler {
61041281cf3SAxel Dörfler 	return DeskbarItemFor(nodeRef) != NULL;
61141281cf3SAxel Dörfler }
61241281cf3SAxel Dörfler 
61341281cf3SAxel Dörfler 
61466eba86fSAxel Dörfler /*!	This handles B_NODE_MONITOR & B_QUERY_UPDATE messages received
61566eba86fSAxel Dörfler 	for the registered add-ons.
61641281cf3SAxel Dörfler */
61741281cf3SAxel Dörfler void
61841281cf3SAxel Dörfler TReplicantTray::HandleEntryUpdate(BMessage *message)
61941281cf3SAxel Dörfler {
62041281cf3SAxel Dörfler 	int32 opcode;
62141281cf3SAxel Dörfler 	if (message->FindInt32("opcode", &opcode) != B_OK)
62241281cf3SAxel Dörfler 		return;
62341281cf3SAxel Dörfler 
62441281cf3SAxel Dörfler 	BPath path;
62541281cf3SAxel Dörfler 	switch (opcode) {
62641281cf3SAxel Dörfler 		case B_ENTRY_CREATED:
62741281cf3SAxel Dörfler 		{
62866eba86fSAxel Dörfler 			// entry was just listed, matches live query
62941281cf3SAxel Dörfler 			const char *name;
63041281cf3SAxel Dörfler 			ino_t directory;
63141281cf3SAxel Dörfler 			dev_t device;
63241281cf3SAxel Dörfler 			// received when an app adds a ref to the
63341281cf3SAxel Dörfler 			// Deskbar add-ons folder
63441281cf3SAxel Dörfler 			if (message->FindString("name", &name) == B_OK
63541281cf3SAxel Dörfler 				&& message->FindInt64("directory", &directory) == B_OK
63641281cf3SAxel Dörfler 				&& message->FindInt32("device", &device) == B_OK) {
63741281cf3SAxel Dörfler 				entry_ref ref(device, directory, name);
63841281cf3SAxel Dörfler 				// see if this item has the attribute
63941281cf3SAxel Dörfler 				// that we expect
64041281cf3SAxel Dörfler 				if (IsAddOn(ref)) {
64141281cf3SAxel Dörfler 					int32 id;
64241281cf3SAxel Dörfler 					BEntry entry(&ref);
64341281cf3SAxel Dörfler 					LoadAddOn(&entry, &id);
64441281cf3SAxel Dörfler 				}
64541281cf3SAxel Dörfler 			}
64641281cf3SAxel Dörfler 			break;
64766eba86fSAxel Dörfler 		}
64841281cf3SAxel Dörfler 
64941281cf3SAxel Dörfler 		case B_ATTR_CHANGED:
65066eba86fSAxel Dörfler 		{
65141281cf3SAxel Dörfler 			// from node watch on individual items
65241281cf3SAxel Dörfler 			// (node_watch added in LoadAddOn)
65341281cf3SAxel Dörfler 			node_ref nodeRef;
65441281cf3SAxel Dörfler 			if (message->FindInt32("device", &(nodeRef.device)) == B_OK
65541281cf3SAxel Dörfler 				&& message->FindInt64("node", &(nodeRef.node)) == B_OK) {
65641281cf3SAxel Dörfler 				// get the add-on this is for
65741281cf3SAxel Dörfler 				DeskbarItemInfo *item = DeskbarItemFor(nodeRef);
65841281cf3SAxel Dörfler 				if (item == NULL)
65941281cf3SAxel Dörfler 					break;
66041281cf3SAxel Dörfler 
66141281cf3SAxel Dörfler 				BFile file(&item->entryRef, B_READ_ONLY);
66241281cf3SAxel Dörfler 
66341281cf3SAxel Dörfler 				char status[255];
66441281cf3SAxel Dörfler 				ssize_t size = file.ReadAttr(kStatusPredicate,
66541281cf3SAxel Dörfler 					B_STRING_TYPE, 0, status, sizeof(status) - 1);
66641281cf3SAxel Dörfler 				status[sizeof(status) - 1] = '\0';
66741281cf3SAxel Dörfler 
66841281cf3SAxel Dörfler 				// attribute was removed
66941281cf3SAxel Dörfler 				if (size == B_ENTRY_NOT_FOUND) {
67041281cf3SAxel Dörfler 					// cleans up and removes node_watch
67141281cf3SAxel Dörfler 					UnloadAddOn(&nodeRef, NULL, true, false);
67241281cf3SAxel Dörfler 				} else if (!strcmp(status, "enable")) {
67341281cf3SAxel Dörfler 					int32 id;
67441281cf3SAxel Dörfler 					BEntry entry(&item->entryRef, true);
67541281cf3SAxel Dörfler 					LoadAddOn(&entry, &id);
67641281cf3SAxel Dörfler 				}
67741281cf3SAxel Dörfler 			}
67841281cf3SAxel Dörfler 			break;
67966eba86fSAxel Dörfler 		}
68041281cf3SAxel Dörfler 
68141281cf3SAxel Dörfler 		case B_ENTRY_MOVED:
68241281cf3SAxel Dörfler 		{
68341281cf3SAxel Dörfler 			entry_ref ref;
68441281cf3SAxel Dörfler 			ino_t todirectory;
68541281cf3SAxel Dörfler 			ino_t node;
68641281cf3SAxel Dörfler 			const char *name;
68741281cf3SAxel Dörfler 			if (message->FindString("name", &name) == B_OK
68841281cf3SAxel Dörfler 				&& message->FindInt64("from directory", &(ref.directory)) == B_OK
68941281cf3SAxel Dörfler 				&& message->FindInt64("to directory", &todirectory) == B_OK
69041281cf3SAxel Dörfler 				&& message->FindInt32("device", &(ref.device)) == B_OK
69141281cf3SAxel Dörfler 				&& message->FindInt64("node", &node) == B_OK ) {
69241281cf3SAxel Dörfler 
69341281cf3SAxel Dörfler 				if (!name)
69441281cf3SAxel Dörfler 					break;
69541281cf3SAxel Dörfler 
69641281cf3SAxel Dörfler 				ref.set_name(name);
69741281cf3SAxel Dörfler 				// change the directory reference to
69841281cf3SAxel Dörfler 				// the new directory
69941281cf3SAxel Dörfler 				MoveItem(&ref, todirectory);
70041281cf3SAxel Dörfler 			}
70141281cf3SAxel Dörfler 			break;
70266eba86fSAxel Dörfler 		}
70341281cf3SAxel Dörfler 
70441281cf3SAxel Dörfler 		case B_ENTRY_REMOVED:
70541281cf3SAxel Dörfler 		{
70641281cf3SAxel Dörfler 			// entry was rm'd from the device
70741281cf3SAxel Dörfler 			node_ref nodeRef;
70841281cf3SAxel Dörfler 			if (message->FindInt32("device", &(nodeRef.device)) == B_OK
70941281cf3SAxel Dörfler 				&& message->FindInt64("node", &(nodeRef.node)) == B_OK) {
71041281cf3SAxel Dörfler 				DeskbarItemInfo *item = DeskbarItemFor(nodeRef);
71141281cf3SAxel Dörfler 				if (item == NULL)
71241281cf3SAxel Dörfler 					break;
71341281cf3SAxel Dörfler 
71441281cf3SAxel Dörfler 				// If there is a team running where the add-on comes from,
71541281cf3SAxel Dörfler 				// we don't want to remove the icon yet.
71641281cf3SAxel Dörfler 				if (be_roster->IsRunning(&item->entryRef))
71741281cf3SAxel Dörfler 					break;
71841281cf3SAxel Dörfler 
71941281cf3SAxel Dörfler 				UnloadAddOn(&nodeRef, NULL, true, false);
72041281cf3SAxel Dörfler 			}
72141281cf3SAxel Dörfler 			break;
72266eba86fSAxel Dörfler 		}
72341281cf3SAxel Dörfler 
72441281cf3SAxel Dörfler 		case B_DEVICE_MOUNTED:
72541281cf3SAxel Dörfler 		{
72641281cf3SAxel Dörfler 			// run a new query on the new device
72741281cf3SAxel Dörfler 			dev_t device;
72841281cf3SAxel Dörfler 			if (message->FindInt32("new device", &device) != B_OK)
72941281cf3SAxel Dörfler 				break;
73041281cf3SAxel Dörfler 
73141281cf3SAxel Dörfler 			RunAddOnQuery(new BVolume(device), kEnabledPredicate);
73241281cf3SAxel Dörfler 			break;
73366eba86fSAxel Dörfler 		}
73466eba86fSAxel Dörfler 
73541281cf3SAxel Dörfler 		case B_DEVICE_UNMOUNTED:
73641281cf3SAxel Dörfler 		{
73741281cf3SAxel Dörfler 			// remove all items associated with the device
73841281cf3SAxel Dörfler 			// unmounted
73941281cf3SAxel Dörfler 			// contrary to what the BeBook says, the item is called "device",
74041281cf3SAxel Dörfler 			// not "new device" like it is for B_DEVICE_MOUNTED
74141281cf3SAxel Dörfler 			dev_t device;
74241281cf3SAxel Dörfler 			if (message->FindInt32("device", &device) != B_OK)
74341281cf3SAxel Dörfler 				break;
74441281cf3SAxel Dörfler 
74541281cf3SAxel Dörfler 			UnloadAddOn(NULL, &device, false, true);
74641281cf3SAxel Dörfler 			break;
74741281cf3SAxel Dörfler 		}
74841281cf3SAxel Dörfler 	}
74966eba86fSAxel Dörfler }
75041281cf3SAxel Dörfler 
75141281cf3SAxel Dörfler 
75266eba86fSAxel Dörfler /*!
75366eba86fSAxel Dörfler 	The add-ons must support the exported C function API
75466eba86fSAxel Dörfler 	if they do, they will be loaded and added to deskbar
75566eba86fSAxel Dörfler 	primary function is the Instantiate function
7567da06231SAxel Dörfler */
75741281cf3SAxel Dörfler status_t
75841281cf3SAxel Dörfler TReplicantTray::LoadAddOn(BEntry *entry, int32 *id, bool force)
75941281cf3SAxel Dörfler {
76041281cf3SAxel Dörfler 	if (!entry)
76141281cf3SAxel Dörfler 		return B_ERROR;
76241281cf3SAxel Dörfler 
76341281cf3SAxel Dörfler 	node_ref nodeRef;
76441281cf3SAxel Dörfler 	entry->GetNodeRef(&nodeRef);
76541281cf3SAxel Dörfler 	// no duplicates
76641281cf3SAxel Dörfler 	if (NodeExists(nodeRef))
76741281cf3SAxel Dörfler 		return B_ERROR;
76841281cf3SAxel Dörfler 
76941281cf3SAxel Dörfler 	BNode node(entry);
77041281cf3SAxel Dörfler 	if (!force) {
77141281cf3SAxel Dörfler 		status_t error = node.InitCheck();
77241281cf3SAxel Dörfler 		if (error != B_OK)
77341281cf3SAxel Dörfler 			return error;
77441281cf3SAxel Dörfler 
77541281cf3SAxel Dörfler 		uint64 deskbarID;
77666eba86fSAxel Dörfler 		ssize_t size = node.ReadAttr(kDeskbarSecurityCodeAttr, B_UINT64_TYPE,
77766eba86fSAxel Dörfler 			0, &deskbarID, sizeof(fDeskbarSecurityCode));
77866eba86fSAxel Dörfler 		if (size != sizeof(fDeskbarSecurityCode)
77966eba86fSAxel Dörfler 			|| deskbarID != fDeskbarSecurityCode) {
78041281cf3SAxel Dörfler 			// no code or code doesn't match
78141281cf3SAxel Dörfler 			return B_ERROR;
78241281cf3SAxel Dörfler 		}
78341281cf3SAxel Dörfler 	}
78441281cf3SAxel Dörfler 
78541281cf3SAxel Dörfler 	BPath path;
78666eba86fSAxel Dörfler 	status_t status = entry->GetPath(&path);
78766eba86fSAxel Dörfler 	if (status < B_OK)
78866eba86fSAxel Dörfler 		return status;
78941281cf3SAxel Dörfler 
79041281cf3SAxel Dörfler 	// load the add-on
79141281cf3SAxel Dörfler 	image_id image = load_add_on(path.Path());
79266eba86fSAxel Dörfler 	if (image < B_OK)
79366eba86fSAxel Dörfler 		return image;
79441281cf3SAxel Dörfler 
79541281cf3SAxel Dörfler 	// get the view loading function symbol
79641281cf3SAxel Dörfler 	//    we first look for a symbol that takes an image_id
79741281cf3SAxel Dörfler 	//    and entry_ref pointer, if not found, go with normal
79841281cf3SAxel Dörfler 	//    instantiate function
79941281cf3SAxel Dörfler 	BView *(*entryFunction)(image_id, const entry_ref *);
80041281cf3SAxel Dörfler 	BView *(*itemFunction)(void);
80141281cf3SAxel Dörfler 	BView *view = NULL;
80241281cf3SAxel Dörfler 
80341281cf3SAxel Dörfler 	entry_ref ref;
80441281cf3SAxel Dörfler 	entry->GetRef(&ref);
80541281cf3SAxel Dörfler 
80641281cf3SAxel Dörfler 	if (get_image_symbol(image, kInstantiateEntryCFunctionName,
80766eba86fSAxel Dörfler 			B_SYMBOL_TYPE_TEXT, (void **)&entryFunction) >= B_OK) {
80841281cf3SAxel Dörfler 		view = (*entryFunction)(image, &ref);
80941281cf3SAxel Dörfler 	} else if (get_image_symbol(image, kInstantiateItemCFunctionName,
81066eba86fSAxel Dörfler 			B_SYMBOL_TYPE_TEXT, (void **)&itemFunction) >= B_OK) {
81141281cf3SAxel Dörfler 		view = (*itemFunction)();
81241281cf3SAxel Dörfler 	} else {
81341281cf3SAxel Dörfler 		unload_add_on(image);
81441281cf3SAxel Dörfler 		return B_ERROR;
81541281cf3SAxel Dörfler 	}
81641281cf3SAxel Dörfler 
81766eba86fSAxel Dörfler 	if (view == NULL || IconExists(view->Name())) {
81841281cf3SAxel Dörfler 		delete view;
81941281cf3SAxel Dörfler 		unload_add_on(image);
82041281cf3SAxel Dörfler 		return B_ERROR;
82141281cf3SAxel Dörfler 	}
82241281cf3SAxel Dörfler 
82341281cf3SAxel Dörfler 	BMessage *data = new BMessage;
82441281cf3SAxel Dörfler 	view->Archive(data);
82541281cf3SAxel Dörfler 	delete view;
82641281cf3SAxel Dörfler 
82741281cf3SAxel Dörfler 	AddIcon(data, id, &ref);
82841281cf3SAxel Dörfler 		// add the rep; adds info to list
82941281cf3SAxel Dörfler 
83041281cf3SAxel Dörfler 	node.WriteAttr(kDeskbarSecurityCodeAttr, B_UINT64_TYPE, 0,
83141281cf3SAxel Dörfler 		&fDeskbarSecurityCode, sizeof(fDeskbarSecurityCode));
83241281cf3SAxel Dörfler 
83341281cf3SAxel Dörfler 	return B_OK;
83441281cf3SAxel Dörfler }
83541281cf3SAxel Dörfler 
83641281cf3SAxel Dörfler 
83741281cf3SAxel Dörfler status_t
83841281cf3SAxel Dörfler TReplicantTray::AddItem(int32 id, node_ref nodeRef, BEntry &entry, bool isAddOn)
83941281cf3SAxel Dörfler {
84041281cf3SAxel Dörfler 	DeskbarItemInfo *item = new DeskbarItemInfo;
84141281cf3SAxel Dörfler 	if (item == NULL)
84241281cf3SAxel Dörfler 		return B_NO_MEMORY;
84341281cf3SAxel Dörfler 
84441281cf3SAxel Dörfler 	item->id = id;
84541281cf3SAxel Dörfler 	item->isAddOn = isAddOn;
84641281cf3SAxel Dörfler 
84741281cf3SAxel Dörfler 	if (entry.GetRef(&item->entryRef) < B_OK) {
84841281cf3SAxel Dörfler 		item->entryRef.device = -1;
84941281cf3SAxel Dörfler 		item->entryRef.directory = -1;
85041281cf3SAxel Dörfler 		item->entryRef.name = NULL;
85141281cf3SAxel Dörfler 	}
85241281cf3SAxel Dörfler 	item->nodeRef = nodeRef;
85341281cf3SAxel Dörfler 
85441281cf3SAxel Dörfler 	fItemList->AddItem(item);
85541281cf3SAxel Dörfler 
85641281cf3SAxel Dörfler 	if (isAddOn)
85741281cf3SAxel Dörfler 		watch_node(&nodeRef, B_WATCH_NAME | B_WATCH_ATTR, this, Window());
85841281cf3SAxel Dörfler 
85941281cf3SAxel Dörfler 	return B_OK;
86041281cf3SAxel Dörfler }
86141281cf3SAxel Dörfler 
86241281cf3SAxel Dörfler 
86341281cf3SAxel Dörfler /**	from entry_removed message, when attribute removed
86441281cf3SAxel Dörfler  *	or when a device is unmounted (use removeall, by device)
86541281cf3SAxel Dörfler  */
86641281cf3SAxel Dörfler 
86741281cf3SAxel Dörfler void
86841281cf3SAxel Dörfler TReplicantTray::UnloadAddOn(node_ref *nodeRef, dev_t *device,
86941281cf3SAxel Dörfler 	bool which, bool removeAll)
87041281cf3SAxel Dörfler {
87141281cf3SAxel Dörfler 	for (int32 i = fItemList->CountItems(); i-- > 0 ;) {
87241281cf3SAxel Dörfler 		DeskbarItemInfo *item = (DeskbarItemInfo*)fItemList->ItemAt(i);
87341281cf3SAxel Dörfler 		if (!item)
87441281cf3SAxel Dörfler 			continue;
87541281cf3SAxel Dörfler 
87641281cf3SAxel Dörfler 		if ((which && nodeRef && item->nodeRef == *nodeRef)
87741281cf3SAxel Dörfler 			|| (device && item->nodeRef.device == *device)) {
87841281cf3SAxel Dörfler 
87941281cf3SAxel Dörfler 			if (device && be_roster->IsRunning(&item->entryRef))
88041281cf3SAxel Dörfler 				continue;
88141281cf3SAxel Dörfler 
88241281cf3SAxel Dörfler 			RemoveIcon(item->id);
88341281cf3SAxel Dörfler 
88441281cf3SAxel Dörfler 			if (!removeAll)
88541281cf3SAxel Dörfler 				break;
88641281cf3SAxel Dörfler 		}
88741281cf3SAxel Dörfler 	}
88841281cf3SAxel Dörfler }
88941281cf3SAxel Dörfler 
89041281cf3SAxel Dörfler 
89141281cf3SAxel Dörfler void
89241281cf3SAxel Dörfler TReplicantTray::RemoveItem(int32 id)
89341281cf3SAxel Dörfler {
89441281cf3SAxel Dörfler 	DeskbarItemInfo *item = DeskbarItemFor(id);
89541281cf3SAxel Dörfler 	if (item == NULL)
89641281cf3SAxel Dörfler 		return;
89741281cf3SAxel Dörfler 
89841281cf3SAxel Dörfler 	// attribute was added via Deskbar API (AddItem(entry_ref *, int32 *)
89941281cf3SAxel Dörfler 	if (item->isAddOn) {
90041281cf3SAxel Dörfler 		BNode node(&item->entryRef);
90141281cf3SAxel Dörfler 		node.RemoveAttr(kStatusPredicate);
90241281cf3SAxel Dörfler 		watch_node(&item->nodeRef, B_STOP_WATCHING, this, Window());
90341281cf3SAxel Dörfler 	}
90441281cf3SAxel Dörfler 
90541281cf3SAxel Dörfler 	fItemList->RemoveItem(item);
90641281cf3SAxel Dörfler 	delete item;
90741281cf3SAxel Dörfler }
90841281cf3SAxel Dörfler 
90941281cf3SAxel Dörfler 
91041281cf3SAxel Dörfler /**	ENTRY_MOVED message, moving only occurs on a device
91141281cf3SAxel Dörfler  *	copying will occur (ENTRY_CREATED) between devices
91241281cf3SAxel Dörfler  */
91341281cf3SAxel Dörfler 
91441281cf3SAxel Dörfler void
91541281cf3SAxel Dörfler TReplicantTray::MoveItem(entry_ref *ref, ino_t toDirectory)
91641281cf3SAxel Dörfler {
91741281cf3SAxel Dörfler 	if (!ref)
91841281cf3SAxel Dörfler 		return;
91941281cf3SAxel Dörfler 
92041281cf3SAxel Dörfler 	// scan for a matching entry_ref and update it
92141281cf3SAxel Dörfler 	//
92241281cf3SAxel Dörfler 	// don't need to change node info as it does not change
92341281cf3SAxel Dörfler 
92441281cf3SAxel Dörfler 	for (int32 i = fItemList->CountItems(); i-- > 0 ;) {
92541281cf3SAxel Dörfler 		DeskbarItemInfo *item = (DeskbarItemInfo *)fItemList->ItemAt(i);
92641281cf3SAxel Dörfler 		if (!item)
92741281cf3SAxel Dörfler 			continue;
92841281cf3SAxel Dörfler 
92941281cf3SAxel Dörfler 		if (!strcmp(item->entryRef.name, ref->name)
93041281cf3SAxel Dörfler 			&& item->entryRef.device == ref->device
93141281cf3SAxel Dörfler 			&& item->entryRef.directory == ref->directory) {
93241281cf3SAxel Dörfler 			item->entryRef.directory = toDirectory;
93341281cf3SAxel Dörfler 			break;
93441281cf3SAxel Dörfler 		}
93541281cf3SAxel Dörfler 	}
93641281cf3SAxel Dörfler }
93741281cf3SAxel Dörfler 
93841281cf3SAxel Dörfler #endif	//	add-on support
93941281cf3SAxel Dörfler 
94041281cf3SAxel Dörfler //	external add-on API routines
94141281cf3SAxel Dörfler //	called using the new BDeskbar class
94241281cf3SAxel Dörfler 
94341281cf3SAxel Dörfler //	existence of icon/replicant by name or ID
94441281cf3SAxel Dörfler //	returns opposite
94541281cf3SAxel Dörfler //	note: name and id are semi-private limiting
94641281cf3SAxel Dörfler //		the ability of non-host apps to remove
94741281cf3SAxel Dörfler //		icons without a little bit of work
94841281cf3SAxel Dörfler 
94941281cf3SAxel Dörfler /**	for a specific id
95041281cf3SAxel Dörfler  *	return the name of the replicant (name of view)
95141281cf3SAxel Dörfler  */
95241281cf3SAxel Dörfler 
95341281cf3SAxel Dörfler status_t
95441281cf3SAxel Dörfler TReplicantTray::ItemInfo(int32 id, const char **name)
95541281cf3SAxel Dörfler {
95641281cf3SAxel Dörfler 	if (id < 0)
95741281cf3SAxel Dörfler 		return B_ERROR;
95841281cf3SAxel Dörfler 
95941281cf3SAxel Dörfler 	int32 index, temp;
96041281cf3SAxel Dörfler 	BView *view = ViewAt(&index, &temp, id, false);
96141281cf3SAxel Dörfler 	if (view) {
96241281cf3SAxel Dörfler 		*name = view->Name();
96341281cf3SAxel Dörfler 		return B_OK;
96441281cf3SAxel Dörfler 	}
96541281cf3SAxel Dörfler 
96641281cf3SAxel Dörfler 	return B_ERROR;
96741281cf3SAxel Dörfler }
96841281cf3SAxel Dörfler 
96941281cf3SAxel Dörfler 
9707da06231SAxel Dörfler /**	for a specific name
9717da06231SAxel Dörfler  *	return the id (internal to Deskbar)
9727da06231SAxel Dörfler  */
9737da06231SAxel Dörfler 
97441281cf3SAxel Dörfler status_t
97541281cf3SAxel Dörfler TReplicantTray::ItemInfo(const char *name, int32 *id)
97641281cf3SAxel Dörfler {
97741281cf3SAxel Dörfler 	if (!name || strlen(name) <= 0)
97841281cf3SAxel Dörfler 		return B_ERROR;
97941281cf3SAxel Dörfler 
98041281cf3SAxel Dörfler 	int32 index;
98141281cf3SAxel Dörfler 	BView *view = ViewAt(&index, id, name);
98241281cf3SAxel Dörfler 	if (view)
98341281cf3SAxel Dörfler 		return B_OK;
98441281cf3SAxel Dörfler 
98541281cf3SAxel Dörfler 	return B_ERROR;
98641281cf3SAxel Dörfler }
98741281cf3SAxel Dörfler 
98841281cf3SAxel Dörfler 
9897da06231SAxel Dörfler /**	at a specific index
9907da06231SAxel Dörfler  *	return both the name and the id of the replicant
9917da06231SAxel Dörfler  */
9927da06231SAxel Dörfler 
99341281cf3SAxel Dörfler status_t
99441281cf3SAxel Dörfler TReplicantTray::ItemInfo(int32 index, const char **name, int32 *id)
99541281cf3SAxel Dörfler {
99641281cf3SAxel Dörfler 	if (index < 0)
99741281cf3SAxel Dörfler 		return B_ERROR;
99841281cf3SAxel Dörfler 
99941281cf3SAxel Dörfler 	BView *view;
100041281cf3SAxel Dörfler 	fShelf->ReplicantAt(index, &view, (uint32 *)id, NULL);
100141281cf3SAxel Dörfler 	if (view) {
100241281cf3SAxel Dörfler 		*name = view->Name();
100341281cf3SAxel Dörfler 		return B_OK;
100441281cf3SAxel Dörfler 	}
100541281cf3SAxel Dörfler 
100641281cf3SAxel Dörfler 	return B_ERROR;
100741281cf3SAxel Dörfler }
100841281cf3SAxel Dörfler 
100941281cf3SAxel Dörfler 
10107da06231SAxel Dörfler /**	replicant exists, by id/index */
10117da06231SAxel Dörfler 
101241281cf3SAxel Dörfler bool
101341281cf3SAxel Dörfler TReplicantTray::IconExists(int32 target, bool byIndex)
101441281cf3SAxel Dörfler {
101541281cf3SAxel Dörfler 	int32 index, id;
101641281cf3SAxel Dörfler 	BView *view = ViewAt(&index, &id, target, byIndex);
101741281cf3SAxel Dörfler 
101841281cf3SAxel Dörfler 	return view && index >= 0;
101941281cf3SAxel Dörfler }
102041281cf3SAxel Dörfler 
102141281cf3SAxel Dörfler 
10227da06231SAxel Dörfler /**	replicant exists, by name */
10237da06231SAxel Dörfler 
102441281cf3SAxel Dörfler bool
102541281cf3SAxel Dörfler TReplicantTray::IconExists(const char *name)
102641281cf3SAxel Dörfler {
102741281cf3SAxel Dörfler 	if (!name || strlen(name) == 0)
102841281cf3SAxel Dörfler 		return false;
102941281cf3SAxel Dörfler 
103041281cf3SAxel Dörfler 	int32 index, id;
103141281cf3SAxel Dörfler 	BView *view = ViewAt(&index, &id, name);
103241281cf3SAxel Dörfler 
103341281cf3SAxel Dörfler 	return view && index >= 0;
103441281cf3SAxel Dörfler }
103541281cf3SAxel Dörfler 
103641281cf3SAxel Dörfler 
103741281cf3SAxel Dörfler int32
103841281cf3SAxel Dörfler TReplicantTray::IconCount() const
103941281cf3SAxel Dörfler {
104041281cf3SAxel Dörfler 	return fShelf->CountReplicants();
104141281cf3SAxel Dörfler }
104241281cf3SAxel Dörfler 
104341281cf3SAxel Dörfler 
1044ced3755cSAxel Dörfler /*!	Message must contain an archivable view for later rehydration.
1045ced3755cSAxel Dörfler 	This function takes over ownership of the provided message on success
1046ced3755cSAxel Dörfler 	only.
1047ced3755cSAxel Dörfler 	Returns the current replicant ID.
10487da06231SAxel Dörfler */
104941281cf3SAxel Dörfler status_t
1050ced3755cSAxel Dörfler TReplicantTray::AddIcon(BMessage *archive, int32 *id, const entry_ref *addOn)
105141281cf3SAxel Dörfler {
1052ced3755cSAxel Dörfler 	if (archive == NULL || id == NULL)
105341281cf3SAxel Dörfler 		return B_ERROR;
105441281cf3SAxel Dörfler 
10559c50f36eSAxel Dörfler 	// find entry_ref
10569c50f36eSAxel Dörfler 
10579c50f36eSAxel Dörfler 	entry_ref ref;
10589c50f36eSAxel Dörfler 	if (addOn) {
10599c50f36eSAxel Dörfler 		// Use it if we got it
10609c50f36eSAxel Dörfler 		ref = *addOn;
10619c50f36eSAxel Dörfler 	} else {
10629c50f36eSAxel Dörfler 		const char *signature;
1063ced3755cSAxel Dörfler 		status_t status = archive->FindString("add_on", &signature);
10649c50f36eSAxel Dörfler 		if (status == B_OK) {
10659c50f36eSAxel Dörfler 			BRoster roster;
10669c50f36eSAxel Dörfler 			status = roster.FindApp(signature, &ref);
10679c50f36eSAxel Dörfler 		}
10689c50f36eSAxel Dörfler 		if (status < B_OK)
10699c50f36eSAxel Dörfler 			return status;
10709c50f36eSAxel Dörfler 	}
10719c50f36eSAxel Dörfler 
10729c50f36eSAxel Dörfler 	BFile file;
10739c50f36eSAxel Dörfler 	status_t status = file.SetTo(&ref, B_READ_ONLY);
10749c50f36eSAxel Dörfler 	if (status < B_OK)
10759c50f36eSAxel Dörfler 		return status;
10769c50f36eSAxel Dörfler 
10779c50f36eSAxel Dörfler 	node_ref nodeRef;
10789c50f36eSAxel Dörfler 	status = file.GetNodeRef(&nodeRef);
10799c50f36eSAxel Dörfler 	if (status < B_OK)
10809c50f36eSAxel Dörfler 		return status;
10819c50f36eSAxel Dörfler 
10829c50f36eSAxel Dörfler 	BEntry entry(&ref, true);
10839c50f36eSAxel Dörfler 		// ToDo: this resolves an eventual link for the item
10849c50f36eSAxel Dörfler 		// being added - this is okay for now, but in multi-user
10859c50f36eSAxel Dörfler 		// environments, one might want to have links that
10869c50f36eSAxel Dörfler 		// carry the be:deskbar_item_status attribute
10879c50f36eSAxel Dörfler 	status = entry.InitCheck();
10889c50f36eSAxel Dörfler 	if (status != B_OK)
10899c50f36eSAxel Dörfler 		return status;
10909c50f36eSAxel Dörfler 
109141281cf3SAxel Dörfler 	*id = 999;
1092ced3755cSAxel Dörfler 	if (archive->what == B_ARCHIVED_OBJECT)
1093ced3755cSAxel Dörfler 		archive->what = 0;
109441281cf3SAxel Dörfler 
1095ced3755cSAxel Dörfler 	BRect originalBounds = archive->FindRect("_frame");
1096a5210ab1SAxel Dörfler 		// this is a work-around for buggy replicants that change their
1097a5210ab1SAxel Dörfler 		// size in AttachedToWindow() (such as "SVM")
1098a5210ab1SAxel Dörfler 
109941281cf3SAxel Dörfler 	// !! check for name collisions?
1100ced3755cSAxel Dörfler 	status = fShelf->AddReplicant(archive, BPoint(1, 1));
11019c50f36eSAxel Dörfler 	if (status != B_OK)
11029c50f36eSAxel Dörfler 		return status;
110341281cf3SAxel Dörfler 
1104a5210ab1SAxel Dörfler 	int32 count = fShelf->CountReplicants();
1105a5210ab1SAxel Dörfler 	BView *view;
1106a5210ab1SAxel Dörfler 	fShelf->ReplicantAt(count - 1, &view, (uint32 *)id, NULL);
1107a5210ab1SAxel Dörfler 
1108a5210ab1SAxel Dörfler 	if (originalBounds != view->Bounds()) {
1109a5210ab1SAxel Dörfler 		// The replicant changed its size when added to the window, so we need
1110a5210ab1SAxel Dörfler 		// to recompute all over again (it's already done once via BShelf::AddReplicant()
1111a5210ab1SAxel Dörfler 		// and TReplicantShelf::CanAcceptReplicantView())
1112a5210ab1SAxel Dörfler 		RealignReplicants();
1113a5210ab1SAxel Dörfler 	}
1114a5210ab1SAxel Dörfler 
111541281cf3SAxel Dörfler 	float oldWidth = Bounds().Width();
111641281cf3SAxel Dörfler 	float oldHeight = Bounds().Height();
111741281cf3SAxel Dörfler 	float width, height;
111841281cf3SAxel Dörfler 	GetPreferredSize(&width, &height);
111941281cf3SAxel Dörfler 	if (oldWidth != width || oldHeight != height)
112041281cf3SAxel Dörfler 		AdjustPlacement();
112141281cf3SAxel Dörfler 
112241281cf3SAxel Dörfler 	// add the item to the add-on list
112341281cf3SAxel Dörfler 
112441281cf3SAxel Dörfler 	AddItem(*id, nodeRef, entry, addOn != NULL);
112541281cf3SAxel Dörfler  	return B_OK;
112641281cf3SAxel Dörfler }
112741281cf3SAxel Dörfler 
112841281cf3SAxel Dörfler 
112941281cf3SAxel Dörfler void
113041281cf3SAxel Dörfler TReplicantTray::RemoveIcon(int32 target, bool byIndex)
113141281cf3SAxel Dörfler {
113241281cf3SAxel Dörfler 	if (target < 0)
113341281cf3SAxel Dörfler 		return;
113441281cf3SAxel Dörfler 
113541281cf3SAxel Dörfler 	int32 index, id;
113641281cf3SAxel Dörfler 	BView *view = ViewAt(&index, &id, target, byIndex);
113741281cf3SAxel Dörfler 	if (view && index >= 0) {
11387da06231SAxel Dörfler 		// remove the reference from the item list & the shelf
113941281cf3SAxel Dörfler 		RemoveItem(id);
114041281cf3SAxel Dörfler 		fShelf->DeleteReplicant(index);
11417da06231SAxel Dörfler 
114241281cf3SAxel Dörfler 		// force a placement update,  !! need to fix BShelf
114341281cf3SAxel Dörfler 		RealReplicantAdjustment(index);
114441281cf3SAxel Dörfler 	}
114541281cf3SAxel Dörfler }
114641281cf3SAxel Dörfler 
114741281cf3SAxel Dörfler 
114841281cf3SAxel Dörfler void
114941281cf3SAxel Dörfler TReplicantTray::RemoveIcon(const char *name)
115041281cf3SAxel Dörfler {
115141281cf3SAxel Dörfler 	if (!name || strlen(name) <= 0)
115241281cf3SAxel Dörfler 		return;
115341281cf3SAxel Dörfler 
115441281cf3SAxel Dörfler 	int32 id, index;
115541281cf3SAxel Dörfler 	BView *view = ViewAt(&index, &id, name);
115641281cf3SAxel Dörfler 	if (view && index >= 0) {
11577da06231SAxel Dörfler 		// remove the reference from the item list & shelf
115841281cf3SAxel Dörfler 		RemoveItem(id);
115941281cf3SAxel Dörfler 		fShelf->DeleteReplicant(index);
11607da06231SAxel Dörfler 
116141281cf3SAxel Dörfler 		// force a placement update,  !! need to fix BShelf
116241281cf3SAxel Dörfler 		RealReplicantAdjustment(index);
116341281cf3SAxel Dörfler 	}
116441281cf3SAxel Dörfler }
116541281cf3SAxel Dörfler 
116641281cf3SAxel Dörfler 
116741281cf3SAxel Dörfler void
11687da06231SAxel Dörfler TReplicantTray::RealReplicantAdjustment(int32 startIndex)
116941281cf3SAxel Dörfler {
11707da06231SAxel Dörfler 	if (startIndex < 0)
117141281cf3SAxel Dörfler 		return;
11727da06231SAxel Dörfler 
11737da06231SAxel Dörfler 	if (startIndex == fLastReplicant)
11747da06231SAxel Dörfler 		startIndex = 0;
11757da06231SAxel Dörfler 
117641281cf3SAxel Dörfler 	// reset the locations of all replicants after the one deleted
11777da06231SAxel Dörfler 	RealignReplicants(startIndex);
117841281cf3SAxel Dörfler 
117941281cf3SAxel Dörfler 	float oldWidth = Bounds().Width();
118041281cf3SAxel Dörfler 	float oldHeight = Bounds().Height();
118141281cf3SAxel Dörfler 	float width, height;
118241281cf3SAxel Dörfler 	GetPreferredSize(&width, &height);
118341281cf3SAxel Dörfler 	if (oldWidth != width || oldHeight != height) {
118441281cf3SAxel Dörfler 		// resize view to accomodate the replicants
118541281cf3SAxel Dörfler 		// redraw as necessary
118641281cf3SAxel Dörfler 		AdjustPlacement();
118741281cf3SAxel Dörfler 	}
118841281cf3SAxel Dörfler }
118941281cf3SAxel Dörfler 
119041281cf3SAxel Dörfler 
11917da06231SAxel Dörfler /**	looking for a replicant by id/index
11927da06231SAxel Dörfler  *	return the view and index
11937da06231SAxel Dörfler  */
11947da06231SAxel Dörfler 
119541281cf3SAxel Dörfler BView *
119641281cf3SAxel Dörfler TReplicantTray::ViewAt(int32 *index, int32 *id, int32 target, bool byIndex)
119741281cf3SAxel Dörfler {
119841281cf3SAxel Dörfler 	*index = -1;
119941281cf3SAxel Dörfler 
120041281cf3SAxel Dörfler 	BView *view;
120141281cf3SAxel Dörfler 	if (byIndex){
120241281cf3SAxel Dörfler 		if (fShelf->ReplicantAt(target, &view, (uint32 *)id)) {
120341281cf3SAxel Dörfler 			if (view) {
120441281cf3SAxel Dörfler 				*index = target;
120541281cf3SAxel Dörfler 				return view;
120641281cf3SAxel Dörfler 			}
120741281cf3SAxel Dörfler 		}
120841281cf3SAxel Dörfler 	} else {
120941281cf3SAxel Dörfler 		int32 count = fShelf->CountReplicants()-1;
121041281cf3SAxel Dörfler 		int32 localid;
121141281cf3SAxel Dörfler 		for (int32 repIndex = count ; repIndex >= 0 ; repIndex--) {
121241281cf3SAxel Dörfler 			fShelf->ReplicantAt(repIndex, &view, (uint32 *)&localid);
121341281cf3SAxel Dörfler 			if (localid == target && view) {
121441281cf3SAxel Dörfler 				*index = repIndex;
121541281cf3SAxel Dörfler 				*id = localid;
121641281cf3SAxel Dörfler 				return view;
121741281cf3SAxel Dörfler 			}
121841281cf3SAxel Dörfler 		}
121941281cf3SAxel Dörfler 	}
122041281cf3SAxel Dörfler 
122141281cf3SAxel Dörfler 	return NULL;
122241281cf3SAxel Dörfler }
122341281cf3SAxel Dörfler 
122441281cf3SAxel Dörfler 
12257da06231SAxel Dörfler /**	looking for a replicant with a view by name
12267da06231SAxel Dörfler  *	return the view, index and the id of the replicant
12277da06231SAxel Dörfler  */
12287da06231SAxel Dörfler 
122941281cf3SAxel Dörfler BView *
123041281cf3SAxel Dörfler TReplicantTray::ViewAt(int32 *index, int32 *id, const char *name)
123141281cf3SAxel Dörfler {
123241281cf3SAxel Dörfler 	*index = -1;
123341281cf3SAxel Dörfler 	*id = -1;
123441281cf3SAxel Dörfler 
123541281cf3SAxel Dörfler 	BView *view;
123641281cf3SAxel Dörfler 	int32 count = fShelf->CountReplicants()-1;
123741281cf3SAxel Dörfler 	for (int32 repIndex = count ; repIndex >= 0 ; repIndex--) {
123841281cf3SAxel Dörfler 		fShelf->ReplicantAt(repIndex, &view, (uint32 *)id);
123941281cf3SAxel Dörfler 		if (view && view->Name() && strcmp(name, view->Name()) == 0) {
124041281cf3SAxel Dörfler 			*index = repIndex;
124141281cf3SAxel Dörfler 			return view;
124241281cf3SAxel Dörfler 		}
124341281cf3SAxel Dörfler 	}
124441281cf3SAxel Dörfler 
124541281cf3SAxel Dörfler 	return NULL;
124641281cf3SAxel Dörfler }
124741281cf3SAxel Dörfler 
124841281cf3SAxel Dörfler 
12497da06231SAxel Dörfler /**	Shelf will call to determine where and if
12507da06231SAxel Dörfler  *	the replicant is to be added
12517da06231SAxel Dörfler  */
12527da06231SAxel Dörfler 
125341281cf3SAxel Dörfler bool
125441281cf3SAxel Dörfler TReplicantTray::AcceptAddon(BRect replicantFrame, BMessage *message)
125541281cf3SAxel Dörfler {
125641281cf3SAxel Dörfler 	if (!message)
125741281cf3SAxel Dörfler 		return false;
125841281cf3SAxel Dörfler 
125941281cf3SAxel Dörfler 	if (replicantFrame.Height() > kMaxReplicantHeight)
126041281cf3SAxel Dörfler 		return false;
126141281cf3SAxel Dörfler 
126241281cf3SAxel Dörfler 	alignment align = B_ALIGN_LEFT;
126341281cf3SAxel Dörfler 	if (fAlignmentSupport && message->HasBool("deskbar:dynamic_align")) {
126441281cf3SAxel Dörfler 		if (!fBarView->Vertical())
126541281cf3SAxel Dörfler 			align = B_ALIGN_RIGHT;
126641281cf3SAxel Dörfler 		else
126741281cf3SAxel Dörfler 			align = fBarView->Left() ? B_ALIGN_LEFT : B_ALIGN_RIGHT;
126841281cf3SAxel Dörfler 	} else if (message->HasInt32("deskbar:align"))
126941281cf3SAxel Dörfler 		message->FindInt32("deskbar:align", (int32 *)&align);
127041281cf3SAxel Dörfler 
127141281cf3SAxel Dörfler 	if (message->HasInt32("deskbar:private_align"))
127241281cf3SAxel Dörfler 		message->FindInt32("deskbar:private_align", (int32 *)&align);
127341281cf3SAxel Dörfler 	else
127441281cf3SAxel Dörfler 		align = B_ALIGN_LEFT;
127541281cf3SAxel Dörfler 
12767da06231SAxel Dörfler 	BPoint loc = LocationForReplicant(fShelf->CountReplicants(),
12777da06231SAxel Dörfler 		replicantFrame.Width());
127841281cf3SAxel Dörfler 
127941281cf3SAxel Dörfler 	message->AddPoint("_pjp_loc", loc);
128041281cf3SAxel Dörfler 	return true;
128141281cf3SAxel Dörfler }
128241281cf3SAxel Dörfler 
128341281cf3SAxel Dörfler 
128441281cf3SAxel Dörfler /**	based on the previous (index - 1) replicant in the list
128541281cf3SAxel Dörfler  *	calculate where the left point should be for this
128641281cf3SAxel Dörfler  *	replicant.  replicant will flow to the right on its own
128741281cf3SAxel Dörfler  */
128841281cf3SAxel Dörfler 
128941281cf3SAxel Dörfler BPoint
12907da06231SAxel Dörfler TReplicantTray::LocationForReplicant(int32 index, float width)
129141281cf3SAxel Dörfler {
129241281cf3SAxel Dörfler 	BPoint loc(kIconGap + 1, kGutter + 1);
129341281cf3SAxel Dörfler 
12947da06231SAxel Dörfler 	if (fMultiRowMode) {
12957da06231SAxel Dörfler 		// try to find free space in every row
12967da06231SAxel Dörfler 		for (int32 row = 0; ; loc.y += kMaxReplicantHeight + kIconGap, row++) {
12977da06231SAxel Dörfler 			// determine free space in this row
1298*cb6afcb1SStephan Aßmus 			BRect rect(loc.x, loc.y, loc.x + fMinimumTrayWidth - kIconGap - 2.0, loc.y + kMaxReplicantHeight);
12997da06231SAxel Dörfler 			if (row == 0 && fBarView->ShowingClock())
13007da06231SAxel Dörfler 				rect.right -= fClock->Frame().Width() + kIconGap;
13017da06231SAxel Dörfler 
13027da06231SAxel Dörfler 			for (int32 i = 0; i < index; i++) {
13037da06231SAxel Dörfler 				BView *view = NULL;
13047da06231SAxel Dörfler 				fShelf->ReplicantAt(i, &view);
13057da06231SAxel Dörfler 				if (view == NULL || view->Frame().top != rect.top)
13067da06231SAxel Dörfler 					continue;
13077da06231SAxel Dörfler 
13087da06231SAxel Dörfler 				rect.left = view->Frame().right + kIconGap + 1;
13097da06231SAxel Dörfler 			}
13107da06231SAxel Dörfler 
13117da06231SAxel Dörfler 			if (rect.Width() >= width) {
13127da06231SAxel Dörfler 				// the icon fits in this row
13137da06231SAxel Dörfler 				loc = rect.LeftTop();
13147da06231SAxel Dörfler 				break;
13157da06231SAxel Dörfler 			}
13167da06231SAxel Dörfler 		}
13177da06231SAxel Dörfler 	} else {
131841281cf3SAxel Dörfler 		if (index > 0) {
131941281cf3SAxel Dörfler 			// get the last replicant added for placement reference
132041281cf3SAxel Dörfler 			BView *view = NULL;
13217da06231SAxel Dörfler 			fShelf->ReplicantAt(index - 1, &view);
132241281cf3SAxel Dörfler 			if (view) {
132341281cf3SAxel Dörfler 				// push this rep placement past the last one
132441281cf3SAxel Dörfler 				loc.x = view->Frame().right + kIconGap + 1;
132541281cf3SAxel Dörfler 				loc.y = view->Frame().top;
132641281cf3SAxel Dörfler 			}
132741281cf3SAxel Dörfler 		}
13287da06231SAxel Dörfler 	}
132941281cf3SAxel Dörfler 
13307da06231SAxel Dörfler 	if ((loc.y == fRightBottomReplicant.top && loc.x > fRightBottomReplicant.left)
13317da06231SAxel Dörfler 		|| loc.y > fRightBottomReplicant.top) {
13327da06231SAxel Dörfler 		fRightBottomReplicant.Set(loc.x, loc.y, loc.x + width, loc.y + kMaxReplicantHeight);
13337da06231SAxel Dörfler 		fLastReplicant = index;
133441281cf3SAxel Dörfler 	}
13357da06231SAxel Dörfler 
133641281cf3SAxel Dörfler 	return loc;
133741281cf3SAxel Dörfler }
133841281cf3SAxel Dörfler 
133941281cf3SAxel Dörfler 
134041281cf3SAxel Dörfler BRect
134141281cf3SAxel Dörfler TReplicantTray::IconFrame(int32 target, bool byIndex)
134241281cf3SAxel Dörfler {
134341281cf3SAxel Dörfler 	int32 index, id;
134441281cf3SAxel Dörfler 	BView *view = ViewAt(&index, &id, target, byIndex);
134541281cf3SAxel Dörfler 	if (view)
134641281cf3SAxel Dörfler 		return view->Frame();
134741281cf3SAxel Dörfler 
134841281cf3SAxel Dörfler 	return BRect(0, 0, 0, 0);
134941281cf3SAxel Dörfler }
135041281cf3SAxel Dörfler 
135141281cf3SAxel Dörfler 
135241281cf3SAxel Dörfler BRect
135341281cf3SAxel Dörfler TReplicantTray::IconFrame(const char *name)
135441281cf3SAxel Dörfler {
135541281cf3SAxel Dörfler 	if (!name)
135641281cf3SAxel Dörfler 		return BRect(0, 0, 0, 0);
135741281cf3SAxel Dörfler 
135841281cf3SAxel Dörfler 	int32 id, index;
135941281cf3SAxel Dörfler 	BView *view = ViewAt(&index, &id, name);
136041281cf3SAxel Dörfler 	if (view)
136141281cf3SAxel Dörfler 		return view->Frame();
136241281cf3SAxel Dörfler 
136341281cf3SAxel Dörfler 	return BRect(0, 0, 0, 0);
136441281cf3SAxel Dörfler }
136541281cf3SAxel Dörfler 
136641281cf3SAxel Dörfler 
13677da06231SAxel Dörfler /**	Scan from the startIndex and reset the location
13687da06231SAxel Dörfler  *	as defined in LocationForReplicant()
136941281cf3SAxel Dörfler  */
137041281cf3SAxel Dörfler 
137141281cf3SAxel Dörfler void
137241281cf3SAxel Dörfler TReplicantTray::RealignReplicants(int32 startIndex)
137341281cf3SAxel Dörfler {
137441281cf3SAxel Dörfler 	if (startIndex < 0)
137541281cf3SAxel Dörfler 		startIndex = 0;
137641281cf3SAxel Dörfler 
137741281cf3SAxel Dörfler 	int32 count = fShelf->CountReplicants();
137841281cf3SAxel Dörfler 	if (count <= 0)
137941281cf3SAxel Dörfler 		return;
138041281cf3SAxel Dörfler 
13817da06231SAxel Dörfler 	if (startIndex == 0)
13827da06231SAxel Dörfler 		fRightBottomReplicant.Set(0, 0, 0, 0);
13837da06231SAxel Dörfler 
138441281cf3SAxel Dörfler 	BView *view = NULL;
138541281cf3SAxel Dörfler 	for (int32 i = startIndex ; i < count ; i++){
138641281cf3SAxel Dörfler 		fShelf->ReplicantAt(i, &view);
13870bec7100SStefano Ceccherini 		if (view != NULL) {
13887da06231SAxel Dörfler 			BPoint loc = LocationForReplicant(i, view->Frame().Width());
13890bec7100SStefano Ceccherini 			if (view->Frame().LeftTop() != loc)
139041281cf3SAxel Dörfler 				view->MoveTo(loc);
139141281cf3SAxel Dörfler 		}
139241281cf3SAxel Dörfler 	}
139341281cf3SAxel Dörfler }
139441281cf3SAxel Dörfler 
139541281cf3SAxel Dörfler 
139641281cf3SAxel Dörfler void
139741281cf3SAxel Dörfler TReplicantTray::SetMultiRow(bool state)
139841281cf3SAxel Dörfler {
139941281cf3SAxel Dörfler 	fMultiRowMode = state;
140041281cf3SAxel Dörfler 
140141281cf3SAxel Dörfler 	// in multi-row state, we only want the short date
140241281cf3SAxel Dörfler 
140341281cf3SAxel Dörfler 	if (fClock != NULL)
140441281cf3SAxel Dörfler 		fClock->AllowFullDate(!state);
140541281cf3SAxel Dörfler }
140641281cf3SAxel Dörfler 
140741281cf3SAxel Dörfler 
140841281cf3SAxel Dörfler //	#pragma mark -
140941281cf3SAxel Dörfler 
141041281cf3SAxel Dörfler 
14117da06231SAxel Dörfler /**	draggable region that is asynchronous so that
14127da06231SAxel Dörfler  *	dragging does not block other activities
14137da06231SAxel Dörfler  */
14147da06231SAxel Dörfler 
141541281cf3SAxel Dörfler TDragRegion::TDragRegion(TBarView *parent, BView *child)
141641281cf3SAxel Dörfler 	:	BControl(BRect(0, 0, 0, 0), "", "", NULL, B_FOLLOW_NONE,
141741281cf3SAxel Dörfler 			B_WILL_DRAW | B_FRAME_EVENTS),
141841281cf3SAxel Dörfler 		fBarView(parent),
141941281cf3SAxel Dörfler 		fChild(child),
142041281cf3SAxel Dörfler 		fDragLocation(kAutoPlaceDragRegion)
142141281cf3SAxel Dörfler {
142241281cf3SAxel Dörfler }
142341281cf3SAxel Dörfler 
142441281cf3SAxel Dörfler 
142541281cf3SAxel Dörfler void
142641281cf3SAxel Dörfler TDragRegion::AttachedToWindow()
142741281cf3SAxel Dörfler {
142841281cf3SAxel Dörfler 	BView::AttachedToWindow();
1429*cb6afcb1SStephan Aßmus 	if (be_control_look != NULL)
1430*cb6afcb1SStephan Aßmus 		SetViewColor(tint_color(ui_color(B_MENU_BACKGROUND_COLOR), 1.1));
1431*cb6afcb1SStephan Aßmus 	else
143241281cf3SAxel Dörfler 		SetViewColor(ui_color(B_MENU_BACKGROUND_COLOR));
143341281cf3SAxel Dörfler 	ResizeToPreferred();
143441281cf3SAxel Dörfler }
143541281cf3SAxel Dörfler 
143641281cf3SAxel Dörfler 
143741281cf3SAxel Dörfler void
143841281cf3SAxel Dörfler TDragRegion::GetPreferredSize(float *width, float *height)
143941281cf3SAxel Dörfler {
144041281cf3SAxel Dörfler 	fChild->ResizeToPreferred();
144141281cf3SAxel Dörfler 	*width = fChild->Bounds().Width();
144241281cf3SAxel Dörfler 	*height = fChild->Bounds().Height();
144341281cf3SAxel Dörfler 
144441281cf3SAxel Dörfler 	if (fDragLocation != kNoDragRegion)
144541281cf3SAxel Dörfler 		*width += 7;
144641281cf3SAxel Dörfler 	else
144741281cf3SAxel Dörfler 		*width += 6;
144841281cf3SAxel Dörfler 
144941281cf3SAxel Dörfler 	*height += 3;
145041281cf3SAxel Dörfler }
145141281cf3SAxel Dörfler 
145241281cf3SAxel Dörfler 
145341281cf3SAxel Dörfler void
145441281cf3SAxel Dörfler TDragRegion::FrameMoved(BPoint)
145541281cf3SAxel Dörfler {
145641281cf3SAxel Dörfler 	if (fBarView->Left() && fBarView->Vertical() && fDragLocation != kNoDragRegion)
145741281cf3SAxel Dörfler 		fChild->MoveTo(5,2);
145841281cf3SAxel Dörfler 	else
145941281cf3SAxel Dörfler 		fChild->MoveTo(2,2);
146041281cf3SAxel Dörfler }
146141281cf3SAxel Dörfler 
146241281cf3SAxel Dörfler 
146341281cf3SAxel Dörfler void
146441281cf3SAxel Dörfler TDragRegion::Draw(BRect)
146541281cf3SAxel Dörfler {
1466eaa9af99SStephan Aßmus 	rgb_color menuColor = ViewColor();
146741281cf3SAxel Dörfler 	rgb_color hilite = tint_color(menuColor, B_DARKEN_1_TINT);
1468*cb6afcb1SStephan Aßmus 	rgb_color ldark = tint_color(menuColor, 1.02);
1469*cb6afcb1SStephan Aßmus 	rgb_color dark = tint_color(menuColor, B_DARKEN_2_TINT);
147041281cf3SAxel Dörfler 	rgb_color vdark = tint_color(menuColor, B_DARKEN_3_TINT);
147141281cf3SAxel Dörfler 	rgb_color vvdark = tint_color(menuColor, B_DARKEN_4_TINT);
147241281cf3SAxel Dörfler 	rgb_color light = tint_color(menuColor, B_LIGHTEN_2_TINT);
147341281cf3SAxel Dörfler 
147441281cf3SAxel Dörfler 	BRect frame(Bounds());
147541281cf3SAxel Dörfler 	BeginLineArray(4);
147641281cf3SAxel Dörfler 
1477*cb6afcb1SStephan Aßmus 	if (be_control_look != NULL) {
1478*cb6afcb1SStephan Aßmus 		if (fBarView->Vertical()) {
1479*cb6afcb1SStephan Aßmus 			AddLine(frame.LeftTop(), frame.RightTop(), dark);
1480*cb6afcb1SStephan Aßmus 			AddLine(BPoint(frame.left, frame.top + 1),
1481*cb6afcb1SStephan Aßmus 				BPoint(frame.right, frame.top + 1), ldark);
1482*cb6afcb1SStephan Aßmus 			AddLine(frame.LeftBottom(), frame.RightBottom(), hilite);
1483*cb6afcb1SStephan Aßmus 		} else if (fBarView->AcrossTop() || fBarView->AcrossBottom()) {
1484*cb6afcb1SStephan Aßmus 			AddLine(frame.LeftTop(),
1485*cb6afcb1SStephan Aßmus 				BPoint(frame.left, frame.bottom), dark);
1486*cb6afcb1SStephan Aßmus 			AddLine(BPoint(frame.left + 1, frame.top + 1),
1487*cb6afcb1SStephan Aßmus 				BPoint(frame.right - 1, frame.top + 1), light);
1488*cb6afcb1SStephan Aßmus 			AddLine(BPoint(frame.right, frame.top + 2),
1489*cb6afcb1SStephan Aßmus 				BPoint(frame.right, frame.bottom), hilite);
1490*cb6afcb1SStephan Aßmus 			AddLine(BPoint(frame.left + 1, frame.bottom),
1491*cb6afcb1SStephan Aßmus 				BPoint(frame.right - 1, frame.bottom), hilite);
1492*cb6afcb1SStephan Aßmus 		}
1493*cb6afcb1SStephan Aßmus 	} else {
149441281cf3SAxel Dörfler 		if (fBarView->Vertical()) {
149541281cf3SAxel Dörfler 			AddLine(frame.LeftTop(), frame.RightTop(), light);
149641281cf3SAxel Dörfler 			AddLine(frame.LeftTop(), frame.LeftBottom(), light);
149741281cf3SAxel Dörfler 			AddLine(frame.RightBottom(), frame.RightTop(), hilite);
149841281cf3SAxel Dörfler 		} else if (fBarView->AcrossTop()) {
1499*cb6afcb1SStephan Aßmus 			AddLine(BPoint(frame.left, frame.top + 1),
1500*cb6afcb1SStephan Aßmus 				BPoint(frame.right - 1, frame.top + 1), light);
150141281cf3SAxel Dörfler 			AddLine(frame.RightTop(), frame.RightBottom(), vvdark);
1502*cb6afcb1SStephan Aßmus 			AddLine(BPoint(frame.right - 1, frame.top + 2),
1503*cb6afcb1SStephan Aßmus 				BPoint(frame.right - 1, frame.bottom - 1), hilite);
1504*cb6afcb1SStephan Aßmus 			AddLine(frame.LeftBottom(),
1505*cb6afcb1SStephan Aßmus 				BPoint(frame.right - 1, frame.bottom), hilite);
150641281cf3SAxel Dörfler 		} else if (fBarView->AcrossBottom()) {
1507*cb6afcb1SStephan Aßmus 			AddLine(BPoint(frame.left, frame.top + 1),
1508*cb6afcb1SStephan Aßmus 				BPoint(frame.right - 1, frame.top + 1), light);
150941281cf3SAxel Dörfler 			AddLine(frame.LeftBottom(), frame.RightBottom(), hilite);
151041281cf3SAxel Dörfler 			AddLine(frame.RightTop(), frame.RightBottom(), vvdark);
1511*cb6afcb1SStephan Aßmus 			AddLine(BPoint(frame.right - 1, frame.top + 1),
1512*cb6afcb1SStephan Aßmus 				BPoint(frame.right - 1, frame.bottom - 1), hilite);
1513*cb6afcb1SStephan Aßmus 		}
151441281cf3SAxel Dörfler 	}
151541281cf3SAxel Dörfler 
151641281cf3SAxel Dörfler 	EndLineArray();
151741281cf3SAxel Dörfler 
151841281cf3SAxel Dörfler 	if (fDragLocation != kDontDrawDragRegion || fDragLocation != kNoDragRegion)
151941281cf3SAxel Dörfler 		DrawDragRegion();
152041281cf3SAxel Dörfler }
152141281cf3SAxel Dörfler 
152241281cf3SAxel Dörfler 
152341281cf3SAxel Dörfler void
152441281cf3SAxel Dörfler TDragRegion::DrawDragRegion()
152541281cf3SAxel Dörfler {
152641281cf3SAxel Dörfler 	BRect dragRegion(DragRegion());
152741281cf3SAxel Dörfler 
1528*cb6afcb1SStephan Aßmus 	rgb_color menuColor = ViewColor();
1529*cb6afcb1SStephan Aßmus 	rgb_color menuHilite = menuColor;
153041281cf3SAxel Dörfler 	if (IsTracking()) {
1531*cb6afcb1SStephan Aßmus 		// Draw drag region highlighted if tracking mouse
1532*cb6afcb1SStephan Aßmus 		menuHilite = tint_color(menuColor, B_HIGHLIGHT_BACKGROUND_TINT);
153341281cf3SAxel Dörfler 		SetHighColor(menuHilite);
153441281cf3SAxel Dörfler 		FillRect(dragRegion);
153541281cf3SAxel Dörfler 	}
1536*cb6afcb1SStephan Aßmus 	rgb_color vdark = tint_color(menuHilite, B_DARKEN_3_TINT);
1537*cb6afcb1SStephan Aßmus 	rgb_color light = tint_color(menuHilite, B_LIGHTEN_2_TINT);
1538*cb6afcb1SStephan Aßmus 
1539*cb6afcb1SStephan Aßmus 	BeginLineArray(dragRegion.IntegerHeight());
1540*cb6afcb1SStephan Aßmus 	BPoint pt;
1541*cb6afcb1SStephan Aßmus 	pt.x = floorf((dragRegion.left + dragRegion.right) / 2 + 0.5) - 1;
1542*cb6afcb1SStephan Aßmus 	pt.y = dragRegion.top + 2;
1543*cb6afcb1SStephan Aßmus 
154441281cf3SAxel Dörfler 	while (pt.y + 1 <= dragRegion.bottom) {
154541281cf3SAxel Dörfler 		AddLine(pt, pt, vdark);
154641281cf3SAxel Dörfler 		AddLine(pt + BPoint(1, 1), pt + BPoint(1, 1), light);
154741281cf3SAxel Dörfler 
154841281cf3SAxel Dörfler 		pt.y += 3;
154941281cf3SAxel Dörfler 	}
155041281cf3SAxel Dörfler 	EndLineArray();
155141281cf3SAxel Dörfler }
155241281cf3SAxel Dörfler 
155341281cf3SAxel Dörfler 
155441281cf3SAxel Dörfler BRect
155541281cf3SAxel Dörfler TDragRegion::DragRegion() const
155641281cf3SAxel Dörfler {
1557*cb6afcb1SStephan Aßmus 	float kTopBottomInset = 2;
1558*cb6afcb1SStephan Aßmus 	float kLeftRightInset = 1;
1559*cb6afcb1SStephan Aßmus 	float kDragWidth = 3;
1560*cb6afcb1SStephan Aßmus 	if (be_control_look != NULL) {
1561*cb6afcb1SStephan Aßmus 		kTopBottomInset = 1;
1562*cb6afcb1SStephan Aßmus 		kLeftRightInset = 0;
1563*cb6afcb1SStephan Aßmus 		kDragWidth = 4;
1564*cb6afcb1SStephan Aßmus 	}
1565*cb6afcb1SStephan Aßmus 
156641281cf3SAxel Dörfler 	BRect dragRegion(Bounds());
1567*cb6afcb1SStephan Aßmus 	dragRegion.top += kTopBottomInset;
1568*cb6afcb1SStephan Aßmus 	dragRegion.bottom -= kTopBottomInset;
156941281cf3SAxel Dörfler 
157041281cf3SAxel Dörfler 	bool placeOnLeft=false;
157141281cf3SAxel Dörfler 	if (fDragLocation == kAutoPlaceDragRegion) {
157241281cf3SAxel Dörfler 		if (fBarView->Vertical() && fBarView->Left())
157341281cf3SAxel Dörfler 			placeOnLeft = true;
157441281cf3SAxel Dörfler 		else
157541281cf3SAxel Dörfler 			placeOnLeft = false;
157641281cf3SAxel Dörfler 	} else if (fDragLocation == kDragRegionLeft)
157741281cf3SAxel Dörfler 		placeOnLeft = true;
157841281cf3SAxel Dörfler 	else if (fDragLocation == kDragRegionRight)
157941281cf3SAxel Dörfler 		placeOnLeft = false;
158041281cf3SAxel Dörfler 
158141281cf3SAxel Dörfler 	if (placeOnLeft) {
1582*cb6afcb1SStephan Aßmus 		dragRegion.left += kLeftRightInset;
1583*cb6afcb1SStephan Aßmus 		dragRegion.right = dragRegion.left + kDragWidth;
158441281cf3SAxel Dörfler 	} else {
1585*cb6afcb1SStephan Aßmus 		dragRegion.right -= kLeftRightInset;
1586*cb6afcb1SStephan Aßmus 		dragRegion.left = dragRegion.right - kDragWidth;
158741281cf3SAxel Dörfler 	}
158841281cf3SAxel Dörfler 
158941281cf3SAxel Dörfler 	return dragRegion;
159041281cf3SAxel Dörfler }
159141281cf3SAxel Dörfler 
159241281cf3SAxel Dörfler 
159341281cf3SAxel Dörfler void
159441281cf3SAxel Dörfler TDragRegion::MouseDown(BPoint thePoint)
159541281cf3SAxel Dörfler {
159641281cf3SAxel Dörfler 	ulong buttons;
159741281cf3SAxel Dörfler 	BPoint where;
159841281cf3SAxel Dörfler 	BRect dragRegion(DragRegion());
159941281cf3SAxel Dörfler 
16007da06231SAxel Dörfler 	dragRegion.InsetBy(-2.0f, -2.0f);
16017da06231SAxel Dörfler 		// DragRegion() is designed for drawing, not clicking
160241281cf3SAxel Dörfler 
160341281cf3SAxel Dörfler 	if (!dragRegion.Contains(thePoint))
160441281cf3SAxel Dörfler 		return;
160541281cf3SAxel Dörfler 
160641281cf3SAxel Dörfler 	while(true) {
160741281cf3SAxel Dörfler 		GetMouse(&where, &buttons);
160841281cf3SAxel Dörfler 		if (!buttons)
160941281cf3SAxel Dörfler 			break;
161041281cf3SAxel Dörfler 
161141281cf3SAxel Dörfler 		if ((Window()->Flags() & B_ASYNCHRONOUS_CONTROLS) != 0) {
161241281cf3SAxel Dörfler 			fPreviousPosition = thePoint;
161341281cf3SAxel Dörfler 			SetTracking(true);
161441281cf3SAxel Dörfler 			SetMouseEventMask(B_POINTER_EVENTS,
161541281cf3SAxel Dörfler 				B_NO_POINTER_HISTORY | B_LOCK_WINDOW_FOCUS);
161641281cf3SAxel Dörfler 			Invalidate(DragRegion());
161741281cf3SAxel Dörfler 			break;
161841281cf3SAxel Dörfler 		}
161941281cf3SAxel Dörfler 
162041281cf3SAxel Dörfler 		snooze(25000);
162141281cf3SAxel Dörfler 	}
162241281cf3SAxel Dörfler }
162341281cf3SAxel Dörfler 
162441281cf3SAxel Dörfler 
162541281cf3SAxel Dörfler void
162641281cf3SAxel Dörfler TDragRegion::MouseUp(BPoint pt)
162741281cf3SAxel Dörfler {
162841281cf3SAxel Dörfler 	if (IsTracking()) {
162941281cf3SAxel Dörfler 		SetTracking(false);
163041281cf3SAxel Dörfler 		Invalidate(DragRegion());
163141281cf3SAxel Dörfler 	} else
163241281cf3SAxel Dörfler 		BControl::MouseUp(pt);
163341281cf3SAxel Dörfler }
163441281cf3SAxel Dörfler 
163541281cf3SAxel Dörfler 
163641281cf3SAxel Dörfler bool
163741281cf3SAxel Dörfler TDragRegion::SwitchModeForRect(BPoint mouse, BRect rect,
163841281cf3SAxel Dörfler 	bool newVertical, bool newLeft, bool newTop, int32 newState)
163941281cf3SAxel Dörfler {
164041281cf3SAxel Dörfler 	if (!rect.Contains(mouse))
164141281cf3SAxel Dörfler 		// not our rect
164241281cf3SAxel Dörfler 		return false;
164341281cf3SAxel Dörfler 
164441281cf3SAxel Dörfler 	if (newVertical == fBarView->Vertical()
164541281cf3SAxel Dörfler 		&& newLeft == fBarView->Left()
164641281cf3SAxel Dörfler 		&& newTop == fBarView->Top()
164741281cf3SAxel Dörfler 		&& newState == fBarView->State())
164841281cf3SAxel Dörfler 		// already in the correct mode
164941281cf3SAxel Dörfler 		return true;
165041281cf3SAxel Dörfler 
165141281cf3SAxel Dörfler 	fBarView->ChangeState(newState, newVertical, newLeft, newTop);
165241281cf3SAxel Dörfler 	return true;
165341281cf3SAxel Dörfler }
165441281cf3SAxel Dörfler 
165541281cf3SAxel Dörfler 
165641281cf3SAxel Dörfler void
165741281cf3SAxel Dörfler TDragRegion::MouseMoved(BPoint where, uint32 code, const BMessage *message)
165841281cf3SAxel Dörfler {
165941281cf3SAxel Dörfler 	if (IsTracking()) {
166041281cf3SAxel Dörfler 		BScreen screen;
166141281cf3SAxel Dörfler 		BRect frame = screen.Frame();
166241281cf3SAxel Dörfler 
166341281cf3SAxel Dörfler 		float hDivider = frame.Width() / 6;
1664*cb6afcb1SStephan Aßmus 		hDivider = (hDivider < sMinimumWindowWidth + 10.0f) ? sMinimumWindowWidth + 10.0f : hDivider;
166541281cf3SAxel Dörfler 		float miniDivider = frame.top + kMiniHeight + 10.0f;
166641281cf3SAxel Dörfler 		float vDivider = frame.Height() / 2;
166741281cf3SAxel Dörfler #ifdef FULL_MODE
166841281cf3SAxel Dörfler 		float thirdScreen = frame.Height() / 3;
166941281cf3SAxel Dörfler #endif
167041281cf3SAxel Dörfler 		BRect topLeft(frame.left, frame.top, frame.left + hDivider, miniDivider);
167141281cf3SAxel Dörfler 		BRect topMiddle(frame.left + hDivider, frame.top, frame.right - hDivider, vDivider);
167241281cf3SAxel Dörfler 		BRect topRight(frame.right - hDivider, frame.top, frame.right, miniDivider);
167341281cf3SAxel Dörfler 
167441281cf3SAxel Dörfler #ifdef FULL_MODE
167541281cf3SAxel Dörfler 		vDivider = miniDivider + thirdScreen;
167641281cf3SAxel Dörfler #endif
167741281cf3SAxel Dörfler 		BRect middleLeft(frame.left, miniDivider, frame.left + hDivider, vDivider);
167841281cf3SAxel Dörfler 		BRect middleRight(frame.right - hDivider, miniDivider, frame.right, vDivider);
167941281cf3SAxel Dörfler 
168041281cf3SAxel Dörfler #ifdef FULL_MODE
168141281cf3SAxel Dörfler 		BRect leftSide(frame.left, vDivider, frame.left + hDivider, frame.bottom - thirdScreen);
168241281cf3SAxel Dörfler 		BRect rightSide(frame.right - hDivider, vDivider, frame.right, frame.bottom - thirdScreen);
168341281cf3SAxel Dörfler 
168441281cf3SAxel Dörfler 		vDivider = frame.bottom - thirdScreen;
168541281cf3SAxel Dörfler #endif
168641281cf3SAxel Dörfler 		BRect bottomLeft(frame.left, vDivider, frame.left + hDivider, frame.bottom);
168741281cf3SAxel Dörfler 		BRect bottomMiddle(frame.left + hDivider, vDivider, frame.right - hDivider, frame.bottom);
168841281cf3SAxel Dörfler 		BRect bottomRight(frame.right - hDivider, vDivider, frame.right, frame.bottom);
168941281cf3SAxel Dörfler 
169041281cf3SAxel Dörfler 		if (where != fPreviousPosition) {
169141281cf3SAxel Dörfler 			fPreviousPosition = where;
169241281cf3SAxel Dörfler 			ConvertToScreen(&where);
169341281cf3SAxel Dörfler 
169441281cf3SAxel Dörfler 			// use short circuit evaluation for convenience
169541281cf3SAxel Dörfler 			if (SwitchModeForRect(where, topLeft, true, true, true, kMiniState)
169641281cf3SAxel Dörfler 				|| SwitchModeForRect(where, topMiddle, false, true, true, kExpandoState)
169741281cf3SAxel Dörfler 				|| SwitchModeForRect(where, topRight, true, false, true, kMiniState)
169841281cf3SAxel Dörfler 
169941281cf3SAxel Dörfler 				|| SwitchModeForRect(where, middleLeft, true, true, true, kExpandoState)
170041281cf3SAxel Dörfler 				|| SwitchModeForRect(where, middleRight, true, false, true, kExpandoState)
170141281cf3SAxel Dörfler 
170241281cf3SAxel Dörfler #ifdef FULL_MODE
170341281cf3SAxel Dörfler 				|| SwitchModeForRect(where, leftSide, true, true, true, kFullState)
170441281cf3SAxel Dörfler 				|| SwitchModeForRect(where, rightSide, true, false, true, kFullState)
170541281cf3SAxel Dörfler #endif
170641281cf3SAxel Dörfler 				|| SwitchModeForRect(where, bottomLeft, true, true, false, kMiniState)
170741281cf3SAxel Dörfler 				|| SwitchModeForRect(where, bottomMiddle, false, true, false, kExpandoState)
17087da06231SAxel Dörfler 				|| SwitchModeForRect(where, bottomRight, true, false, false, kMiniState))
170941281cf3SAxel Dörfler 				;
171041281cf3SAxel Dörfler 		}
171141281cf3SAxel Dörfler 	} else
171241281cf3SAxel Dörfler 		BControl::MouseMoved(where, code, message);
171341281cf3SAxel Dörfler }
171441281cf3SAxel Dörfler 
171541281cf3SAxel Dörfler 
171641281cf3SAxel Dörfler int32
171741281cf3SAxel Dörfler TDragRegion::DragRegionLocation() const
171841281cf3SAxel Dörfler {
171941281cf3SAxel Dörfler 	return fDragLocation;
172041281cf3SAxel Dörfler }
172141281cf3SAxel Dörfler 
172241281cf3SAxel Dörfler 
172341281cf3SAxel Dörfler void
172441281cf3SAxel Dörfler TDragRegion::SetDragRegionLocation(int32 location)
172541281cf3SAxel Dörfler {
172641281cf3SAxel Dörfler 	if (location == fDragLocation)
172741281cf3SAxel Dörfler 		return;
172841281cf3SAxel Dörfler 
172941281cf3SAxel Dörfler 	fDragLocation = location;
173041281cf3SAxel Dörfler 	Invalidate();
173141281cf3SAxel Dörfler }
173241281cf3SAxel Dörfler 
1733