xref: /haiku/src/apps/deskbar/StatusView.cpp (revision a5210ab17da5a6784a814bb69c59ba6bf5401432)
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 <Debug.h>
4841281cf3SAxel Dörfler #include <Application.h>
4941281cf3SAxel Dörfler #include <Beep.h>
5041281cf3SAxel Dörfler #include <Directory.h>
5141281cf3SAxel Dörfler #include <FindDirectory.h>
5241281cf3SAxel Dörfler #include <MenuItem.h>
5341281cf3SAxel Dörfler #include <NodeInfo.h>
5441281cf3SAxel Dörfler #include <NodeMonitor.h>
5541281cf3SAxel Dörfler #include <Path.h>
5641281cf3SAxel Dörfler #include <PopUpMenu.h>
5741281cf3SAxel Dörfler #include <Roster.h>
5841281cf3SAxel Dörfler #include <Screen.h>
5941281cf3SAxel Dörfler #include <Volume.h>
6041281cf3SAxel Dörfler #include <VolumeRoster.h>
6141281cf3SAxel Dörfler #include <Window.h>
6241281cf3SAxel Dörfler 
6341281cf3SAxel Dörfler #include "DeskBarUtils.h"
6441281cf3SAxel Dörfler #include "StatusView.h"
6541281cf3SAxel Dörfler #include "StatusViewShelf.h"
6641281cf3SAxel Dörfler #include "TimeView.h"
6741281cf3SAxel Dörfler #include "BarApp.h"
6841281cf3SAxel Dörfler 
696d01b66dSAxel Dörfler using std::max;
7041281cf3SAxel Dörfler 
7141281cf3SAxel Dörfler #ifdef DB_ADDONS
7241281cf3SAxel Dörfler //	Add-on support
7341281cf3SAxel Dörfler //
7441281cf3SAxel Dörfler //	Item - internal item list (node, eref, etc)
7541281cf3SAxel Dörfler //	Icon - physical replicant handed to the DeskbarClass class
7641281cf3SAxel Dörfler //	AddOn - attribute based add-on
7741281cf3SAxel Dörfler 
7841281cf3SAxel Dörfler const char *const kInstantiateItemCFunctionName = "instantiate_deskbar_item";
7941281cf3SAxel Dörfler const char *const kInstantiateEntryCFunctionName = "instantiate_deskbar_entry";
8041281cf3SAxel Dörfler const char *const kDeskbarSecurityCodeFile = "Deskbar_security_code";
8141281cf3SAxel Dörfler const char *const kDeskbarSecurityCodeAttr = "be:deskbar_security_code";
8241281cf3SAxel Dörfler const char *const kStatusPredicate = "be:deskbar_item_status";
8341281cf3SAxel Dörfler const char *const kEnabledPredicate = "be:deskbar_item_status=enabled";
8441281cf3SAxel Dörfler const char *const kDisabledPredicate = "be:deskbar_item_status=disabled";
8541281cf3SAxel Dörfler 
8641281cf3SAxel Dörfler 
8741281cf3SAxel Dörfler static void
8841281cf3SAxel Dörfler DumpItem(DeskbarItemInfo *item)
8941281cf3SAxel Dörfler {
9041281cf3SAxel Dörfler 	printf("is addon: %i, id: %li\n", item->isAddOn, item->id);
9141281cf3SAxel Dörfler 	printf("entry_ref:  %ld, %Ld, %s\n", item->entryRef.device, item->entryRef.directory, item->entryRef.name);
9241281cf3SAxel Dörfler 	printf("node_ref:  %ld, %Ld\n", item->nodeRef.device, item->nodeRef.node);
9341281cf3SAxel Dörfler }
9441281cf3SAxel Dörfler 
9541281cf3SAxel Dörfler 
9641281cf3SAxel Dörfler static void
9741281cf3SAxel Dörfler DumpList(BList *itemlist)
9841281cf3SAxel Dörfler {
9941281cf3SAxel Dörfler 	int32 count = itemlist->CountItems() - 1;
10041281cf3SAxel Dörfler 	if (count < 0) {
10141281cf3SAxel Dörfler 		printf("no items in list\n");
10241281cf3SAxel Dörfler 		return;
10341281cf3SAxel Dörfler 	}
10441281cf3SAxel Dörfler 	for (int32 i = count ; i >= 0 ; i--) {
10541281cf3SAxel Dörfler 		DeskbarItemInfo *item = (DeskbarItemInfo*)itemlist->ItemAt(i);
10641281cf3SAxel Dörfler 		if (!item)
10741281cf3SAxel Dörfler 			continue;
10841281cf3SAxel Dörfler 
10941281cf3SAxel Dörfler 		DumpItem(item);
11041281cf3SAxel Dörfler 	}
11141281cf3SAxel Dörfler }
11241281cf3SAxel Dörfler #endif	/* DB_ADDONS */
11341281cf3SAxel Dörfler 
11441281cf3SAxel Dörfler 
1157da06231SAxel Dörfler // don't change the name of this view to anything other than "Status"!
1167da06231SAxel Dörfler 
11741281cf3SAxel Dörfler TReplicantTray::TReplicantTray(TBarView *parent, bool vertical)
11841281cf3SAxel Dörfler 	:	BView(BRect(0, 0, 1, 1), "Status", B_FOLLOW_LEFT | B_FOLLOW_TOP,
11941281cf3SAxel Dörfler 			B_WILL_DRAW | B_FRAME_EVENTS),
12041281cf3SAxel Dörfler 	fClock(NULL),
12141281cf3SAxel Dörfler 	fBarView(parent),
12241281cf3SAxel Dörfler 	fShelf(new TReplicantShelf(this)),
12341281cf3SAxel Dörfler 	fMultiRowMode(vertical),
12441281cf3SAxel Dörfler 	fAlignmentSupport(false)
12541281cf3SAxel Dörfler {
12641281cf3SAxel Dörfler }
12741281cf3SAxel Dörfler 
12841281cf3SAxel Dörfler 
12941281cf3SAxel Dörfler TReplicantTray::~TReplicantTray()
13041281cf3SAxel Dörfler {
13141281cf3SAxel Dörfler 	delete fShelf;
13241281cf3SAxel Dörfler }
13341281cf3SAxel Dörfler 
13441281cf3SAxel Dörfler 
13541281cf3SAxel Dörfler void
13641281cf3SAxel Dörfler TReplicantTray::AttachedToWindow()
13741281cf3SAxel Dörfler {
13841281cf3SAxel Dörfler 	BView::AttachedToWindow();
13941281cf3SAxel Dörfler 
14041281cf3SAxel Dörfler 	SetViewColor(tint_color(ui_color(B_MENU_BACKGROUND_COLOR), B_DARKEN_1_TINT));
14141281cf3SAxel Dörfler 	SetDrawingMode(B_OP_COPY);
14241281cf3SAxel Dörfler 	Window()->SetPulseRate(1000000);
14341281cf3SAxel Dörfler 	DealWithClock(fBarView->ShowingClock());
14441281cf3SAxel Dörfler 
14541281cf3SAxel Dörfler #ifdef DB_ADDONS
14641281cf3SAxel Dörfler 	// load addons and rehydrate archives
1473a6add49SAxel Dörfler #if !defined(HAIKU_TARGET_PLATFORM_LIBBE_TEST)
14841281cf3SAxel Dörfler 	InitAddOnSupport();
14941281cf3SAxel Dörfler #endif
1509c910f9eSStephan Aßmus #endif
15141281cf3SAxel Dörfler 	ResizeToPreferred();
15241281cf3SAxel Dörfler }
15341281cf3SAxel Dörfler 
15441281cf3SAxel Dörfler 
15541281cf3SAxel Dörfler void
15641281cf3SAxel Dörfler TReplicantTray::DetachedFromWindow()
15741281cf3SAxel Dörfler {
15841281cf3SAxel Dörfler #ifdef DB_ADDONS
15941281cf3SAxel Dörfler 	// clean up add-on support
160465b54cdSAxel Dörfler #if !defined(HAIKU_TARGET_PLATFORM_LIBBE_TEST)
16141281cf3SAxel Dörfler 	DeleteAddOnSupport();
16241281cf3SAxel Dörfler #endif
163465b54cdSAxel Dörfler #endif
16441281cf3SAxel Dörfler 	BView::DetachedFromWindow();
16541281cf3SAxel Dörfler }
16641281cf3SAxel Dörfler 
16741281cf3SAxel Dörfler 
16841281cf3SAxel Dörfler void
16941281cf3SAxel Dörfler TReplicantTray::RememberClockSettings()
17041281cf3SAxel Dörfler {
17141281cf3SAxel Dörfler 	if (fClock)	{
17241281cf3SAxel Dörfler 		desk_settings *settings = ((TBarApp *)be_app)->Settings();
17341281cf3SAxel Dörfler 
17441281cf3SAxel Dörfler 		settings->timeShowSeconds = fClock->ShowingSeconds();
17541281cf3SAxel Dörfler 		settings->timeShowMil = fClock->ShowingMilTime();
17641281cf3SAxel Dörfler 		settings->timeShowEuro = fClock->ShowingEuroDate();
17741281cf3SAxel Dörfler 		settings->timeFullDate = fClock->ShowingFullDate();
17841281cf3SAxel Dörfler 	}
17941281cf3SAxel Dörfler }
18041281cf3SAxel Dörfler 
18141281cf3SAxel Dörfler 
18241281cf3SAxel Dörfler bool
18341281cf3SAxel Dörfler TReplicantTray::ShowingSeconds()
18441281cf3SAxel Dörfler {
18541281cf3SAxel Dörfler 	if (fClock)
18641281cf3SAxel Dörfler 		return fClock->ShowingSeconds();
18741281cf3SAxel Dörfler 	return false;
18841281cf3SAxel Dörfler }
18941281cf3SAxel Dörfler 
19041281cf3SAxel Dörfler 
19141281cf3SAxel Dörfler bool
19241281cf3SAxel Dörfler TReplicantTray::ShowingMiltime()
19341281cf3SAxel Dörfler {
19441281cf3SAxel Dörfler 	if (fClock)
19541281cf3SAxel Dörfler 		return fClock->ShowingMilTime();
19641281cf3SAxel Dörfler 	return false;
19741281cf3SAxel Dörfler }
19841281cf3SAxel Dörfler 
19941281cf3SAxel Dörfler 
20041281cf3SAxel Dörfler bool
20141281cf3SAxel Dörfler TReplicantTray::ShowingEuroDate()
20241281cf3SAxel Dörfler {
20341281cf3SAxel Dörfler 	if (fClock)
20441281cf3SAxel Dörfler 		return fClock->ShowingEuroDate();
20541281cf3SAxel Dörfler 	return false;
20641281cf3SAxel Dörfler }
20741281cf3SAxel Dörfler 
20841281cf3SAxel Dörfler 
20941281cf3SAxel Dörfler bool
21041281cf3SAxel Dörfler TReplicantTray::ShowingFullDate()
21141281cf3SAxel Dörfler {
21241281cf3SAxel Dörfler 	if (fClock && CanShowFullDate())
21341281cf3SAxel Dörfler 		return fClock->ShowingFullDate();
21441281cf3SAxel Dörfler 	return false;
21541281cf3SAxel Dörfler }
21641281cf3SAxel Dörfler 
21741281cf3SAxel Dörfler 
21841281cf3SAxel Dörfler bool
21941281cf3SAxel Dörfler TReplicantTray::CanShowFullDate()
22041281cf3SAxel Dörfler {
22141281cf3SAxel Dörfler 	if (fClock)
22241281cf3SAxel Dörfler 		return fClock->CanShowFullDate();
22341281cf3SAxel Dörfler 	return false;
22441281cf3SAxel Dörfler }
22541281cf3SAxel Dörfler 
22641281cf3SAxel Dörfler 
22741281cf3SAxel Dörfler void
22841281cf3SAxel Dörfler TReplicantTray::DealWithClock(bool showClock)
22941281cf3SAxel Dörfler {
23041281cf3SAxel Dörfler 	fBarView->ShowClock(showClock);
23141281cf3SAxel Dörfler 
23241281cf3SAxel Dörfler 	if (showClock) {
23341281cf3SAxel Dörfler 		if (!fClock) {
23441281cf3SAxel Dörfler 			desk_settings *settings = ((TBarApp *)be_app)->Settings();
23541281cf3SAxel Dörfler 
23641281cf3SAxel Dörfler 			fClock = new TTimeView(settings->timeShowSeconds, settings->timeShowMil,
23741281cf3SAxel Dörfler 				settings->timeFullDate, settings->timeShowEuro, false);
23841281cf3SAxel Dörfler 			AddChild(fClock);
23941281cf3SAxel Dörfler 
24041281cf3SAxel Dörfler 			fClock->MoveTo(Bounds().right - fClock->Bounds().Width() - 1, 2);
24141281cf3SAxel Dörfler 			fClock->AllowFullDate(!IsMultiRow());
24241281cf3SAxel Dörfler 		}
24341281cf3SAxel Dörfler 	} else {
24441281cf3SAxel Dörfler 		if (fClock) {
24541281cf3SAxel Dörfler 			RememberClockSettings();
24641281cf3SAxel Dörfler 
24741281cf3SAxel Dörfler 			fClock->RemoveSelf();
24841281cf3SAxel Dörfler 			delete fClock;
24941281cf3SAxel Dörfler 			fClock = NULL;
25041281cf3SAxel Dörfler 		}
25141281cf3SAxel Dörfler 	}
25241281cf3SAxel Dörfler }
25341281cf3SAxel Dörfler 
25441281cf3SAxel Dörfler 
25541281cf3SAxel Dörfler /**	width is set to a minimum of kMinimumReplicantCount by kMaxReplicantWidth
25641281cf3SAxel Dörfler  *	if not in multirowmode and greater than kMinimumReplicantCount
25741281cf3SAxel Dörfler  *	the width should be calculated based on the actual
25841281cf3SAxel Dörfler  *	replicant widths
25941281cf3SAxel Dörfler  */
26041281cf3SAxel Dörfler 
26141281cf3SAxel Dörfler void
26241281cf3SAxel Dörfler TReplicantTray::GetPreferredSize(float *preferredWidth, float *preferredHeight)
26341281cf3SAxel Dörfler {
26441281cf3SAxel Dörfler 	float width = 0, height = kMinimumTrayHeight;
26541281cf3SAxel Dörfler 
26641281cf3SAxel Dörfler 	if (fMultiRowMode) {
2677da06231SAxel Dörfler 		if (fShelf->CountReplicants() > 0)
2687da06231SAxel Dörfler 			height = fRightBottomReplicant.bottom;
26941281cf3SAxel Dörfler 
27041281cf3SAxel Dörfler 		// the height will be uniform for the number of rows
27141281cf3SAxel Dörfler 		// necessary to show all the reps + any gutters
27241281cf3SAxel Dörfler 		// necessary for spacing
27341281cf3SAxel Dörfler 		int32 rowCount = (int32)(height / kMaxReplicantHeight);
27441281cf3SAxel Dörfler 		height = kGutter + (rowCount * kMaxReplicantHeight)
27541281cf3SAxel Dörfler 			+ ((rowCount - 1) * kIconGap) + kGutter;
27641281cf3SAxel Dörfler 		height = max(kMinimumTrayHeight, height);
27741281cf3SAxel Dörfler 		width = kMinimumTrayWidth;
27841281cf3SAxel Dörfler 	} else {
27941281cf3SAxel Dörfler 		// if last replicant overruns clock then
28041281cf3SAxel Dörfler 		// resize to accomodate
2817da06231SAxel Dörfler 		if (fShelf->CountReplicants() > 0) {
28241281cf3SAxel Dörfler 			if (fBarView->ShowingClock()
2837da06231SAxel Dörfler 				&& fRightBottomReplicant.right + 6 >= fClock->Frame().left) {
2847da06231SAxel Dörfler 				width = fRightBottomReplicant.right + 6 + fClock->Frame().Width();
28541281cf3SAxel Dörfler 			} else
2867da06231SAxel Dörfler 				width = fRightBottomReplicant.right + 3;
28741281cf3SAxel Dörfler 		}
28841281cf3SAxel Dörfler 		// this view has a fixed minimum width
28941281cf3SAxel Dörfler 		width = max(kMinimumTrayWidth, width);
29041281cf3SAxel Dörfler 	}
29141281cf3SAxel Dörfler 
29241281cf3SAxel Dörfler 	*preferredWidth = width;
29341281cf3SAxel Dörfler 	// add 2 for the border
29441281cf3SAxel Dörfler 	*preferredHeight = height + 1;
29541281cf3SAxel Dörfler }
29641281cf3SAxel Dörfler 
29741281cf3SAxel Dörfler 
29841281cf3SAxel Dörfler void
29941281cf3SAxel Dörfler TReplicantTray::AdjustPlacement()
30041281cf3SAxel Dörfler {
30141281cf3SAxel Dörfler 	// called when an add-on has been added or removed
30241281cf3SAxel Dörfler 	// need to resize the parent of this accordingly
303*a5210ab1SAxel Dörfler 
30441281cf3SAxel Dörfler 	BRect bounds = Bounds();
30541281cf3SAxel Dörfler 	float width, height;
30641281cf3SAxel Dörfler 	GetPreferredSize(&width, &height);
30741281cf3SAxel Dörfler 
30841281cf3SAxel Dörfler 	if (width == bounds.Width() && height == bounds.Height()) {
30941281cf3SAxel Dörfler 		// no need to change anything
31041281cf3SAxel Dörfler 		return;
31141281cf3SAxel Dörfler 	}
31241281cf3SAxel Dörfler 
31341281cf3SAxel Dörfler 	Parent()->ResizeToPreferred();
31441281cf3SAxel Dörfler 	fBarView->UpdatePlacement();
31541281cf3SAxel Dörfler 	Parent()->Invalidate();
31641281cf3SAxel Dörfler 	Invalidate();
31741281cf3SAxel Dörfler }
31841281cf3SAxel Dörfler 
31941281cf3SAxel Dörfler 
32041281cf3SAxel Dörfler void
32141281cf3SAxel Dörfler TReplicantTray::Draw(BRect)
32241281cf3SAxel Dörfler {
32341281cf3SAxel Dörfler 	rgb_color menuColor = ui_color(B_MENU_BACKGROUND_COLOR);
32441281cf3SAxel Dörfler 	rgb_color vdark = tint_color(menuColor, B_DARKEN_3_TINT);
32541281cf3SAxel Dörfler 	rgb_color light = tint_color(menuColor, B_LIGHTEN_2_TINT);
32641281cf3SAxel Dörfler 
32741281cf3SAxel Dörfler 	BRect frame(Bounds());
32841281cf3SAxel Dörfler 
32941281cf3SAxel Dörfler 	SetHighColor(light);
33041281cf3SAxel Dörfler 	StrokeLine(frame.LeftBottom(), frame.RightBottom());
33141281cf3SAxel Dörfler 	StrokeLine(frame.RightBottom(), frame.RightTop());
33241281cf3SAxel Dörfler 
33341281cf3SAxel Dörfler 	SetHighColor(vdark);
33441281cf3SAxel Dörfler 	StrokeLine(frame.RightTop(), frame.LeftTop());
33541281cf3SAxel Dörfler 	StrokeLine(frame.LeftTop(), frame.LeftBottom());
33641281cf3SAxel Dörfler }
33741281cf3SAxel Dörfler 
33841281cf3SAxel Dörfler 
33941281cf3SAxel Dörfler void
34041281cf3SAxel Dörfler TReplicantTray::MessageReceived(BMessage *message)
34141281cf3SAxel Dörfler {
34241281cf3SAxel Dörfler 	switch (message->what) {
34341281cf3SAxel Dörfler 		case 'time':
34441281cf3SAxel Dörfler 			// from context menu in clock and in this view
34541281cf3SAxel Dörfler 			DealWithClock(!fBarView->ShowingClock());
34641281cf3SAxel Dörfler 			RealignReplicants();
34741281cf3SAxel Dörfler 			AdjustPlacement();
34841281cf3SAxel Dörfler 			break;
34941281cf3SAxel Dörfler 
350*a5210ab1SAxel Dörfler 		case 'Trfm':
35141281cf3SAxel Dörfler 			// time string reformat -> realign
35241281cf3SAxel Dörfler 			DealWithClock(fBarView->ShowingClock());
35341281cf3SAxel Dörfler 			RealignReplicants();
35441281cf3SAxel Dörfler 			AdjustPlacement();
35541281cf3SAxel Dörfler 			break;
35641281cf3SAxel Dörfler 
35741281cf3SAxel Dörfler 		case kMsgShowSeconds:
35841281cf3SAxel Dörfler 		case kMsgMilTime:
35941281cf3SAxel Dörfler 		case kMsgEuroDate:
36041281cf3SAxel Dörfler 		case kMsgFullDate:
36141281cf3SAxel Dörfler 			if (fClock != NULL)
36241281cf3SAxel Dörfler 				Window()->PostMessage(message, fClock);
36341281cf3SAxel Dörfler 			break;
36441281cf3SAxel Dörfler 
36541281cf3SAxel Dörfler #ifdef DB_ADDONS
36641281cf3SAxel Dörfler 		case B_NODE_MONITOR:
36741281cf3SAxel Dörfler 		case B_QUERY_UPDATE:
36841281cf3SAxel Dörfler 			HandleEntryUpdate(message);
36941281cf3SAxel Dörfler 			break;
37041281cf3SAxel Dörfler #endif
37141281cf3SAxel Dörfler 
37241281cf3SAxel Dörfler 		default:
37341281cf3SAxel Dörfler 			BView::MessageReceived(message);
37441281cf3SAxel Dörfler 			break;
37541281cf3SAxel Dörfler 	}
37641281cf3SAxel Dörfler }
37741281cf3SAxel Dörfler 
37841281cf3SAxel Dörfler 
37941281cf3SAxel Dörfler void
38041281cf3SAxel Dörfler TReplicantTray::ShowReplicantMenu(BPoint point)
38141281cf3SAxel Dörfler {
38241281cf3SAxel Dörfler 	BPopUpMenu *menu = new BPopUpMenu("", false, false);
38341281cf3SAxel Dörfler 	menu->SetFont(be_plain_font);
38441281cf3SAxel Dörfler 
38541281cf3SAxel Dörfler 	// If the clock is visible, show the extended menu
38641281cf3SAxel Dörfler 	// otheriwse, show "Show Time".
38741281cf3SAxel Dörfler 
38841281cf3SAxel Dörfler 	if (fBarView->ShowingClock())
38941281cf3SAxel Dörfler 		fClock->ShowClockOptions(ConvertToScreen(point));
39041281cf3SAxel Dörfler 	else {
39141281cf3SAxel Dörfler 		BMenuItem *item = new BMenuItem("Show Time", new BMessage('time'));
39241281cf3SAxel Dörfler 		menu->AddItem(item);
39341281cf3SAxel Dörfler 		menu->SetTargetForItems(this);
39441281cf3SAxel Dörfler 		BPoint where = ConvertToScreen(point);
39541281cf3SAxel Dörfler 		menu->Go(where, true, true, BRect(where - BPoint(4, 4),
39641281cf3SAxel Dörfler 			where + BPoint(4, 4)), true);
39741281cf3SAxel Dörfler 	}
39841281cf3SAxel Dörfler }
39941281cf3SAxel Dörfler 
40041281cf3SAxel Dörfler 
40141281cf3SAxel Dörfler void
40241281cf3SAxel Dörfler TReplicantTray::MouseDown(BPoint where)
40341281cf3SAxel Dörfler {
40441281cf3SAxel Dörfler #ifdef DB_ADDONS
40541281cf3SAxel Dörfler 	if (modifiers() & B_CONTROL_KEY)
40641281cf3SAxel Dörfler 		DumpList(fItemList);
40741281cf3SAxel Dörfler #endif
40841281cf3SAxel Dörfler 
40941281cf3SAxel Dörfler 	uint32	buttons;
41041281cf3SAxel Dörfler 
41141281cf3SAxel Dörfler 	Window()->CurrentMessage()->FindInt32("buttons", (int32 *)&buttons);
41241281cf3SAxel Dörfler 	if (buttons == B_SECONDARY_MOUSE_BUTTON) {
41341281cf3SAxel Dörfler 		ShowReplicantMenu(where);
41441281cf3SAxel Dörfler 	} else {
41541281cf3SAxel Dörfler 		BPoint save = where;
41641281cf3SAxel Dörfler 		bigtime_t doubleClickSpeed;
41741281cf3SAxel Dörfler 		bigtime_t start = system_time();
41841281cf3SAxel Dörfler 		uint32 buttons;
41941281cf3SAxel Dörfler 
42041281cf3SAxel Dörfler 		get_click_speed(&doubleClickSpeed);
42141281cf3SAxel Dörfler 
42241281cf3SAxel Dörfler 		do {
42341281cf3SAxel Dörfler 			if (fabs(where.x - save.x) > 4 || fabs(where.y - save.y) > 4)
42441281cf3SAxel Dörfler 				// user moved out of bounds of click area
42541281cf3SAxel Dörfler 				break;
42641281cf3SAxel Dörfler 
42741281cf3SAxel Dörfler 			if ((system_time() - start) > (2 * doubleClickSpeed)) {
42841281cf3SAxel Dörfler 				ShowReplicantMenu(where);
42941281cf3SAxel Dörfler 				break;
43041281cf3SAxel Dörfler 			}
43141281cf3SAxel Dörfler 
43241281cf3SAxel Dörfler 			snooze(50000);
43341281cf3SAxel Dörfler 			GetMouse(&where, &buttons);
43441281cf3SAxel Dörfler 		} while (buttons);
43541281cf3SAxel Dörfler 	}
43641281cf3SAxel Dörfler 	BView::MouseDown(where);
43741281cf3SAxel Dörfler }
43841281cf3SAxel Dörfler 
43941281cf3SAxel Dörfler #ifdef DB_ADDONS
44041281cf3SAxel Dörfler 
44141281cf3SAxel Dörfler void
44241281cf3SAxel Dörfler TReplicantTray::InitAddOnSupport()
44341281cf3SAxel Dörfler {
44441281cf3SAxel Dörfler 	// list to maintain refs to each rep added/deleted
44541281cf3SAxel Dörfler 	fItemList = new BList();
44641281cf3SAxel Dörfler 
44741281cf3SAxel Dörfler 	bool haveKey = false;
44841281cf3SAxel Dörfler  	BPath path;
44941281cf3SAxel Dörfler     if (find_directory(B_USER_SETTINGS_DIRECTORY, &path, true) == B_OK) {
45041281cf3SAxel Dörfler 		path.Append(kDeskbarSecurityCodeFile);
45141281cf3SAxel Dörfler 
45241281cf3SAxel Dörfler 		BFile file(path.Path(),B_READ_ONLY);
45341281cf3SAxel Dörfler 		if (file.InitCheck() == B_OK
45441281cf3SAxel Dörfler 			&& file.Read(&fDeskbarSecurityCode,
45541281cf3SAxel Dörfler 					sizeof(fDeskbarSecurityCode)) == sizeof(fDeskbarSecurityCode))
45641281cf3SAxel Dörfler 			haveKey = true;
45741281cf3SAxel Dörfler 	}
45841281cf3SAxel Dörfler 	if (!haveKey) {
45941281cf3SAxel Dörfler 		// create the security code
46041281cf3SAxel Dörfler 		bigtime_t real = real_time_clock_usecs();
46141281cf3SAxel Dörfler 		bigtime_t boot = system_time();
46241281cf3SAxel Dörfler 		// two computers would have to have exactly matching clocks, and launch
46341281cf3SAxel Dörfler 		// Deskbar at the exact same time into the bootsequence in order for their
46441281cf3SAxel Dörfler 		// security-ID to be identical
46541281cf3SAxel Dörfler 		fDeskbarSecurityCode = ((real&0xffffffffULL)<<32)|(boot&0xffffffffULL);
46641281cf3SAxel Dörfler 
46741281cf3SAxel Dörfler     	if (find_directory (B_USER_SETTINGS_DIRECTORY, &path, true) == B_OK) {
46841281cf3SAxel Dörfler 			path.Append(kDeskbarSecurityCodeFile);
46941281cf3SAxel Dörfler 			BFile file(path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
47041281cf3SAxel Dörfler 			if (file.InitCheck() == B_OK)
47141281cf3SAxel Dörfler 				file.Write(&fDeskbarSecurityCode, sizeof(fDeskbarSecurityCode));
47241281cf3SAxel Dörfler 		}
47341281cf3SAxel Dörfler 	}
47441281cf3SAxel Dörfler 
47541281cf3SAxel Dörfler 	// for each volume currently mounted
47641281cf3SAxel Dörfler 	//		index the volume with our indices
47741281cf3SAxel Dörfler 	BVolumeRoster roster;
47841281cf3SAxel Dörfler 	BVolume volume;
47941281cf3SAxel Dörfler 	while (roster.GetNextVolume(&volume) == B_OK) {
48041281cf3SAxel Dörfler 		fs_create_index(volume.Device(), kStatusPredicate, B_STRING_TYPE, 0);
48141281cf3SAxel Dörfler 		RunAddOnQuery(&volume, kEnabledPredicate);
48241281cf3SAxel Dörfler 	}
48341281cf3SAxel Dörfler 
48441281cf3SAxel Dörfler 	// we also watch for volumes mounted and unmounted
48541281cf3SAxel Dörfler 	watch_node(NULL, B_WATCH_MOUNT | B_WATCH_ATTR, this, Window());
48641281cf3SAxel Dörfler }
48741281cf3SAxel Dörfler 
48841281cf3SAxel Dörfler 
48941281cf3SAxel Dörfler void
49041281cf3SAxel Dörfler TReplicantTray::DeleteAddOnSupport()
49141281cf3SAxel Dörfler {
49241281cf3SAxel Dörfler 	for (int32 i = fItemList->CountItems(); i-- > 0 ;) {
49341281cf3SAxel Dörfler 		DeskbarItemInfo *item = (DeskbarItemInfo *)fItemList->RemoveItem(i);
49441281cf3SAxel Dörfler 		if (item) {
49541281cf3SAxel Dörfler 			if (item->isAddOn)
49641281cf3SAxel Dörfler 				watch_node(&(item->nodeRef), B_STOP_WATCHING, this, Window());
49741281cf3SAxel Dörfler 
49841281cf3SAxel Dörfler 			delete item;
49941281cf3SAxel Dörfler 		}
50041281cf3SAxel Dörfler 	}
50141281cf3SAxel Dörfler 	delete fItemList;
50241281cf3SAxel Dörfler 
50341281cf3SAxel Dörfler 	// stop the volume mount/unmount watch
50441281cf3SAxel Dörfler 	stop_watching(this, Window());
50541281cf3SAxel Dörfler }
50641281cf3SAxel Dörfler 
50741281cf3SAxel Dörfler 
50841281cf3SAxel Dörfler void
50941281cf3SAxel Dörfler TReplicantTray::RunAddOnQuery(BVolume *volume, const char *predicate)
51041281cf3SAxel Dörfler {
51141281cf3SAxel Dörfler 	// Since the new BFS supports querying for attributes without
51241281cf3SAxel Dörfler 	// an index, we only run the query if the index exists (for
51341281cf3SAxel Dörfler 	// newly mounted devices only - the Deskbar will automatically
51441281cf3SAxel Dörfler 	// create an index for every device mounted at startup).
51541281cf3SAxel Dörfler 	index_info info;
51641281cf3SAxel Dörfler 	if (!volume->KnowsQuery() || fs_stat_index(volume->Device(),kStatusPredicate,&info) != 0)
51741281cf3SAxel Dörfler 		return;
51841281cf3SAxel Dörfler 
51941281cf3SAxel Dörfler 	// run a new query on a specific volume
52041281cf3SAxel Dörfler 	// make it live
52141281cf3SAxel Dörfler 	BQuery query;
52241281cf3SAxel Dörfler 	query.SetVolume(volume);
52341281cf3SAxel Dörfler 	query.SetPredicate(predicate);
52441281cf3SAxel Dörfler 	query.Fetch();
52541281cf3SAxel Dörfler 
52641281cf3SAxel Dörfler 	int32 id;
52741281cf3SAxel Dörfler 	BEntry entry;
5287da06231SAxel Dörfler 	while (query.GetNextEntry(&entry) == B_OK) {
52941281cf3SAxel Dörfler 		// scan any entries returned
53041281cf3SAxel Dörfler 		// attempt to load them as add-ons
53141281cf3SAxel Dörfler 		// collisions are handled in LoadAddOn
53241281cf3SAxel Dörfler 		LoadAddOn(&entry, &id);
53341281cf3SAxel Dörfler 	}
5347da06231SAxel Dörfler }
53541281cf3SAxel Dörfler 
53641281cf3SAxel Dörfler 
53741281cf3SAxel Dörfler bool
53841281cf3SAxel Dörfler TReplicantTray::IsAddOn(entry_ref &ref)
53941281cf3SAxel Dörfler {
54041281cf3SAxel Dörfler 	BFile file(&ref, B_READ_ONLY);
54141281cf3SAxel Dörfler 
54241281cf3SAxel Dörfler 	char status[64];
54341281cf3SAxel Dörfler 	ssize_t size = file.ReadAttr(kStatusPredicate, B_STRING_TYPE, 0, &status, 64);
54441281cf3SAxel Dörfler 	return size > 0;
54541281cf3SAxel Dörfler }
54641281cf3SAxel Dörfler 
54741281cf3SAxel Dörfler 
54841281cf3SAxel Dörfler DeskbarItemInfo *
54941281cf3SAxel Dörfler TReplicantTray::DeskbarItemFor(node_ref &nodeRef)
55041281cf3SAxel Dörfler {
55141281cf3SAxel Dörfler 	for (int32 i = fItemList->CountItems(); i-- > 0 ;) {
55241281cf3SAxel Dörfler 		DeskbarItemInfo *item = static_cast<DeskbarItemInfo *>(fItemList->ItemAt(i));
55341281cf3SAxel Dörfler 		if (item == NULL)
55441281cf3SAxel Dörfler 			continue;
55541281cf3SAxel Dörfler 
55641281cf3SAxel Dörfler 		if (item->nodeRef == nodeRef)
55741281cf3SAxel Dörfler 			return item;
55841281cf3SAxel Dörfler 	}
55941281cf3SAxel Dörfler 
56041281cf3SAxel Dörfler 	return NULL;
56141281cf3SAxel Dörfler }
56241281cf3SAxel Dörfler 
56341281cf3SAxel Dörfler 
56441281cf3SAxel Dörfler DeskbarItemInfo *
56541281cf3SAxel Dörfler TReplicantTray::DeskbarItemFor(int32 id)
56641281cf3SAxel Dörfler {
56741281cf3SAxel Dörfler 	for (int32 i = fItemList->CountItems(); i-- > 0 ;) {
56841281cf3SAxel Dörfler 		DeskbarItemInfo *item = static_cast<DeskbarItemInfo *>(fItemList->ItemAt(i));
56941281cf3SAxel Dörfler 		if (item == NULL)
57041281cf3SAxel Dörfler 			continue;
57141281cf3SAxel Dörfler 
57241281cf3SAxel Dörfler 		if (item->id == id)
57341281cf3SAxel Dörfler 			return item;
57441281cf3SAxel Dörfler 	}
57541281cf3SAxel Dörfler 
57641281cf3SAxel Dörfler 	return NULL;
57741281cf3SAxel Dörfler }
57841281cf3SAxel Dörfler 
57941281cf3SAxel Dörfler 
58041281cf3SAxel Dörfler bool
58141281cf3SAxel Dörfler TReplicantTray::NodeExists(node_ref &nodeRef)
58241281cf3SAxel Dörfler {
58341281cf3SAxel Dörfler 	return DeskbarItemFor(nodeRef) != NULL;
58441281cf3SAxel Dörfler }
58541281cf3SAxel Dörfler 
58641281cf3SAxel Dörfler 
58741281cf3SAxel Dörfler /**	This handles B_NODE_MONITOR & B_QUERY_UPDATE messages received
58841281cf3SAxel Dörfler  *	for the registered add-ons.
58941281cf3SAxel Dörfler  */
59041281cf3SAxel Dörfler 
59141281cf3SAxel Dörfler void
59241281cf3SAxel Dörfler TReplicantTray::HandleEntryUpdate(BMessage *message)
59341281cf3SAxel Dörfler {
59441281cf3SAxel Dörfler 	int32 opcode;
59541281cf3SAxel Dörfler 	if (message->FindInt32("opcode", &opcode) != B_OK)
59641281cf3SAxel Dörfler 		return;
59741281cf3SAxel Dörfler 
59841281cf3SAxel Dörfler 	BPath path;
59941281cf3SAxel Dörfler 	switch (opcode) {
60041281cf3SAxel Dörfler 		case B_ENTRY_CREATED:
60141281cf3SAxel Dörfler 			// entry was just listed, matches live query
60241281cf3SAxel Dörfler 			{
60341281cf3SAxel Dörfler 				const char *name;
60441281cf3SAxel Dörfler 				ino_t directory;
60541281cf3SAxel Dörfler 				dev_t device;
60641281cf3SAxel Dörfler 				// received when an app adds a ref to the
60741281cf3SAxel Dörfler 				// Deskbar add-ons folder
60841281cf3SAxel Dörfler 				if (message->FindString("name", &name) == B_OK
60941281cf3SAxel Dörfler 					&& message->FindInt64("directory", &directory) == B_OK
61041281cf3SAxel Dörfler 					&& message->FindInt32("device", &device) == B_OK) {
61141281cf3SAxel Dörfler 					entry_ref ref(device, directory, name);
61241281cf3SAxel Dörfler 					// see if this item has the attribute
61341281cf3SAxel Dörfler 					// that we expect
61441281cf3SAxel Dörfler 					if (IsAddOn(ref)) {
61541281cf3SAxel Dörfler 						int32 id;
61641281cf3SAxel Dörfler 						BEntry entry(&ref);
61741281cf3SAxel Dörfler 						LoadAddOn(&entry, &id);
61841281cf3SAxel Dörfler 					}
61941281cf3SAxel Dörfler 				}
62041281cf3SAxel Dörfler 			}
62141281cf3SAxel Dörfler 			break;
62241281cf3SAxel Dörfler 
62341281cf3SAxel Dörfler 		case B_ATTR_CHANGED:
62441281cf3SAxel Dörfler 			// from node watch on individual items
62541281cf3SAxel Dörfler 			// (node_watch added in LoadAddOn)
62641281cf3SAxel Dörfler 			{
62741281cf3SAxel Dörfler 				node_ref nodeRef;
62841281cf3SAxel Dörfler 				if (message->FindInt32("device", &(nodeRef.device)) == B_OK
62941281cf3SAxel Dörfler 					&& message->FindInt64("node", &(nodeRef.node)) == B_OK) {
63041281cf3SAxel Dörfler 					// get the add-on this is for
63141281cf3SAxel Dörfler 					DeskbarItemInfo *item = DeskbarItemFor(nodeRef);
63241281cf3SAxel Dörfler 					if (item == NULL)
63341281cf3SAxel Dörfler 						break;
63441281cf3SAxel Dörfler 
63541281cf3SAxel Dörfler 					BFile file(&item->entryRef, B_READ_ONLY);
63641281cf3SAxel Dörfler 
63741281cf3SAxel Dörfler 					char status[255];
63841281cf3SAxel Dörfler 					ssize_t size = file.ReadAttr(kStatusPredicate,
63941281cf3SAxel Dörfler 						B_STRING_TYPE, 0, status, sizeof(status) - 1);
64041281cf3SAxel Dörfler 					status[sizeof(status) - 1] = '\0';
64141281cf3SAxel Dörfler 
64241281cf3SAxel Dörfler 					// attribute was removed
64341281cf3SAxel Dörfler 					if (size == B_ENTRY_NOT_FOUND) {
64441281cf3SAxel Dörfler 						// cleans up and removes node_watch
64541281cf3SAxel Dörfler 						UnloadAddOn(&nodeRef, NULL, true, false);
64641281cf3SAxel Dörfler 					} else if (!strcmp(status, "enable")) {
64741281cf3SAxel Dörfler 						int32 id;
64841281cf3SAxel Dörfler 						BEntry entry(&item->entryRef, true);
64941281cf3SAxel Dörfler 						LoadAddOn(&entry, &id);
65041281cf3SAxel Dörfler 					}
65141281cf3SAxel Dörfler 				}
65241281cf3SAxel Dörfler 			}
65341281cf3SAxel Dörfler 			break;
65441281cf3SAxel Dörfler 
65541281cf3SAxel Dörfler 		case B_ENTRY_MOVED:
65641281cf3SAxel Dörfler 			{
65741281cf3SAxel Dörfler 				entry_ref ref;
65841281cf3SAxel Dörfler 				ino_t todirectory;
65941281cf3SAxel Dörfler 				ino_t node;
66041281cf3SAxel Dörfler 				const char *name;
66141281cf3SAxel Dörfler 				if (message->FindString("name", &name) == B_OK
66241281cf3SAxel Dörfler 					&& message->FindInt64("from directory", &(ref.directory)) == B_OK
66341281cf3SAxel Dörfler 					&& message->FindInt64("to directory", &todirectory) == B_OK
66441281cf3SAxel Dörfler 					&& message->FindInt32("device", &(ref.device)) == B_OK
66541281cf3SAxel Dörfler 					&& message->FindInt64("node", &node) == B_OK ) {
66641281cf3SAxel Dörfler 
66741281cf3SAxel Dörfler 					if (!name)
66841281cf3SAxel Dörfler 						break;
66941281cf3SAxel Dörfler 
67041281cf3SAxel Dörfler 					ref.set_name(name);
67141281cf3SAxel Dörfler 					// change the directory reference to
67241281cf3SAxel Dörfler 					// the new directory
67341281cf3SAxel Dörfler 					MoveItem(&ref, todirectory);
67441281cf3SAxel Dörfler 				}
67541281cf3SAxel Dörfler 			}
67641281cf3SAxel Dörfler 			break;
67741281cf3SAxel Dörfler 
67841281cf3SAxel Dörfler 		case B_ENTRY_REMOVED:
67941281cf3SAxel Dörfler 		{
68041281cf3SAxel Dörfler 			// entry was rm'd from the device
68141281cf3SAxel Dörfler 			node_ref nodeRef;
68241281cf3SAxel Dörfler 			if (message->FindInt32("device", &(nodeRef.device)) == B_OK
68341281cf3SAxel Dörfler 				&& message->FindInt64("node", &(nodeRef.node)) == B_OK) {
68441281cf3SAxel Dörfler 				DeskbarItemInfo *item = DeskbarItemFor(nodeRef);
68541281cf3SAxel Dörfler 				if (item == NULL)
68641281cf3SAxel Dörfler 					break;
68741281cf3SAxel Dörfler 
68841281cf3SAxel Dörfler 				// If there is a team running where the add-on comes from,
68941281cf3SAxel Dörfler 				// we don't want to remove the icon yet.
69041281cf3SAxel Dörfler 				if (be_roster->IsRunning(&item->entryRef))
69141281cf3SAxel Dörfler 					break;
69241281cf3SAxel Dörfler 
69341281cf3SAxel Dörfler 				UnloadAddOn(&nodeRef, NULL, true, false);
69441281cf3SAxel Dörfler 			}
69541281cf3SAxel Dörfler 		}
69641281cf3SAxel Dörfler 		break;
69741281cf3SAxel Dörfler 
69841281cf3SAxel Dörfler 		case B_DEVICE_MOUNTED:
69941281cf3SAxel Dörfler 			{
70041281cf3SAxel Dörfler 				// run a new query on the new device
70141281cf3SAxel Dörfler 				dev_t device;
70241281cf3SAxel Dörfler 				if (message->FindInt32("new device", &device) != B_OK)
70341281cf3SAxel Dörfler 					break;
70441281cf3SAxel Dörfler 
70541281cf3SAxel Dörfler 				RunAddOnQuery(new BVolume(device), kEnabledPredicate);
70641281cf3SAxel Dörfler 			}
70741281cf3SAxel Dörfler 			break;
70841281cf3SAxel Dörfler 		case B_DEVICE_UNMOUNTED:
70941281cf3SAxel Dörfler 			{
71041281cf3SAxel Dörfler 				// remove all items associated with the device
71141281cf3SAxel Dörfler 				// unmounted
71241281cf3SAxel Dörfler 				// contrary to what the BeBook says, the item is called "device",
71341281cf3SAxel Dörfler 				// not "new device" like it is for B_DEVICE_MOUNTED
71441281cf3SAxel Dörfler 				dev_t device;
71541281cf3SAxel Dörfler 				if (message->FindInt32("device", &device) != B_OK)
71641281cf3SAxel Dörfler 					break;
71741281cf3SAxel Dörfler 
71841281cf3SAxel Dörfler 				UnloadAddOn(NULL, &device, false, true);
71941281cf3SAxel Dörfler 			}
72041281cf3SAxel Dörfler 			break;
72141281cf3SAxel Dörfler 	}
72241281cf3SAxel Dörfler }
72341281cf3SAxel Dörfler 
72441281cf3SAxel Dörfler 
7257da06231SAxel Dörfler /** The add-ons must support the exported C function API
7267da06231SAxel Dörfler  *	if they do, they will be loaded and added to deskbar
7277da06231SAxel Dörfler  *	primary function is the Instantiate function
7287da06231SAxel Dörfler  */
72941281cf3SAxel Dörfler 
73041281cf3SAxel Dörfler status_t
73141281cf3SAxel Dörfler TReplicantTray::LoadAddOn(BEntry *entry, int32 *id, bool force)
73241281cf3SAxel Dörfler {
73341281cf3SAxel Dörfler 	if (!entry)
73441281cf3SAxel Dörfler 		return B_ERROR;
73541281cf3SAxel Dörfler 
73641281cf3SAxel Dörfler 	node_ref nodeRef;
73741281cf3SAxel Dörfler 	entry->GetNodeRef(&nodeRef);
73841281cf3SAxel Dörfler 	// no duplicates
73941281cf3SAxel Dörfler 	if (NodeExists(nodeRef))
74041281cf3SAxel Dörfler 		return B_ERROR;
74141281cf3SAxel Dörfler 
74241281cf3SAxel Dörfler 	BNode node(entry);
74341281cf3SAxel Dörfler 	if (!force) {
74441281cf3SAxel Dörfler 		status_t error = node.InitCheck();
74541281cf3SAxel Dörfler 		if (error != B_OK)
74641281cf3SAxel Dörfler 			return error;
74741281cf3SAxel Dörfler 
74841281cf3SAxel Dörfler 		uint64 deskbarID;
74941281cf3SAxel Dörfler 		ssize_t size = node.ReadAttr(kDeskbarSecurityCodeAttr, B_UINT64_TYPE, 0,
75041281cf3SAxel Dörfler 			&deskbarID, sizeof(fDeskbarSecurityCode));
75141281cf3SAxel Dörfler 		if (size != sizeof(fDeskbarSecurityCode) || deskbarID != fDeskbarSecurityCode) {
75241281cf3SAxel Dörfler 			// no code or code doesn't match
75341281cf3SAxel Dörfler 			return B_ERROR;
75441281cf3SAxel Dörfler 		}
75541281cf3SAxel Dörfler 	}
75641281cf3SAxel Dörfler 
75741281cf3SAxel Dörfler 	BPath path;
75841281cf3SAxel Dörfler 	entry->GetPath(&path);
75941281cf3SAxel Dörfler 
76041281cf3SAxel Dörfler 	// load the add-on
76141281cf3SAxel Dörfler 	image_id image = load_add_on(path.Path());
76241281cf3SAxel Dörfler 	if (image < 0)
76341281cf3SAxel Dörfler 		return (status_t)image;
76441281cf3SAxel Dörfler 
76541281cf3SAxel Dörfler 	// get the view loading function symbol
76641281cf3SAxel Dörfler 	//    we first look for a symbol that takes an image_id
76741281cf3SAxel Dörfler 	//    and entry_ref pointer, if not found, go with normal
76841281cf3SAxel Dörfler 	//    instantiate function
76941281cf3SAxel Dörfler 	BView *(*entryFunction)(image_id, const entry_ref *);
77041281cf3SAxel Dörfler 	BView *(*itemFunction)(void);
77141281cf3SAxel Dörfler 	BView *view = NULL;
77241281cf3SAxel Dörfler 
77341281cf3SAxel Dörfler 	entry_ref ref;
77441281cf3SAxel Dörfler 	entry->GetRef(&ref);
77541281cf3SAxel Dörfler 
77641281cf3SAxel Dörfler 	if (get_image_symbol(image, kInstantiateEntryCFunctionName,
77741281cf3SAxel Dörfler 		B_SYMBOL_TYPE_TEXT, (void **)&entryFunction) >= 0) {
77841281cf3SAxel Dörfler 
77941281cf3SAxel Dörfler 		view = (*entryFunction)(image, &ref);
78041281cf3SAxel Dörfler 	} else if (get_image_symbol(image, kInstantiateItemCFunctionName,
78141281cf3SAxel Dörfler 		B_SYMBOL_TYPE_TEXT, (void **)&itemFunction) >= 0) {
78241281cf3SAxel Dörfler 
78341281cf3SAxel Dörfler 		view = (*itemFunction)();
78441281cf3SAxel Dörfler 	} else {
78541281cf3SAxel Dörfler 		unload_add_on(image);
78641281cf3SAxel Dörfler 		return B_ERROR;
78741281cf3SAxel Dörfler 	}
78841281cf3SAxel Dörfler 
78941281cf3SAxel Dörfler 	if (!view || IconExists(view->Name())) {
79041281cf3SAxel Dörfler 		delete view;
79141281cf3SAxel Dörfler 		unload_add_on(image);
79241281cf3SAxel Dörfler 		return B_ERROR;
79341281cf3SAxel Dörfler 	}
79441281cf3SAxel Dörfler 
79541281cf3SAxel Dörfler 	BMessage *data = new BMessage;
79641281cf3SAxel Dörfler 	view->Archive(data);
79741281cf3SAxel Dörfler 	delete view;
79841281cf3SAxel Dörfler 
79941281cf3SAxel Dörfler 	AddIcon(data, id, &ref);
80041281cf3SAxel Dörfler 		// add the rep; adds info to list
80141281cf3SAxel Dörfler 
80241281cf3SAxel Dörfler 	node.WriteAttr(kDeskbarSecurityCodeAttr, B_UINT64_TYPE, 0,
80341281cf3SAxel Dörfler 		&fDeskbarSecurityCode, sizeof(fDeskbarSecurityCode));
80441281cf3SAxel Dörfler 
80541281cf3SAxel Dörfler 	return B_OK;
80641281cf3SAxel Dörfler }
80741281cf3SAxel Dörfler 
80841281cf3SAxel Dörfler 
80941281cf3SAxel Dörfler status_t
81041281cf3SAxel Dörfler TReplicantTray::AddItem(int32 id, node_ref nodeRef, BEntry &entry, bool isAddOn)
81141281cf3SAxel Dörfler {
81241281cf3SAxel Dörfler 	DeskbarItemInfo *item = new DeskbarItemInfo;
81341281cf3SAxel Dörfler 	if (item == NULL)
81441281cf3SAxel Dörfler 		return B_NO_MEMORY;
81541281cf3SAxel Dörfler 
81641281cf3SAxel Dörfler 	item->id = id;
81741281cf3SAxel Dörfler 	item->isAddOn = isAddOn;
81841281cf3SAxel Dörfler 
81941281cf3SAxel Dörfler 	if (entry.GetRef(&item->entryRef) < B_OK) {
82041281cf3SAxel Dörfler 		item->entryRef.device = -1;
82141281cf3SAxel Dörfler 		item->entryRef.directory = -1;
82241281cf3SAxel Dörfler 		item->entryRef.name = NULL;
82341281cf3SAxel Dörfler 	}
82441281cf3SAxel Dörfler 	item->nodeRef = nodeRef;
82541281cf3SAxel Dörfler 
82641281cf3SAxel Dörfler 	fItemList->AddItem(item);
82741281cf3SAxel Dörfler 
82841281cf3SAxel Dörfler 	if (isAddOn)
82941281cf3SAxel Dörfler 		watch_node(&nodeRef, B_WATCH_NAME | B_WATCH_ATTR, this, Window());
83041281cf3SAxel Dörfler 
83141281cf3SAxel Dörfler 	return B_OK;
83241281cf3SAxel Dörfler }
83341281cf3SAxel Dörfler 
83441281cf3SAxel Dörfler 
83541281cf3SAxel Dörfler /**	from entry_removed message, when attribute removed
83641281cf3SAxel Dörfler  *	or when a device is unmounted (use removeall, by device)
83741281cf3SAxel Dörfler  */
83841281cf3SAxel Dörfler 
83941281cf3SAxel Dörfler void
84041281cf3SAxel Dörfler TReplicantTray::UnloadAddOn(node_ref *nodeRef, dev_t *device,
84141281cf3SAxel Dörfler 	bool which, bool removeAll)
84241281cf3SAxel Dörfler {
84341281cf3SAxel Dörfler 	for (int32 i = fItemList->CountItems(); i-- > 0 ;) {
84441281cf3SAxel Dörfler 		DeskbarItemInfo *item = (DeskbarItemInfo*)fItemList->ItemAt(i);
84541281cf3SAxel Dörfler 		if (!item)
84641281cf3SAxel Dörfler 			continue;
84741281cf3SAxel Dörfler 
84841281cf3SAxel Dörfler 		if ((which && nodeRef && item->nodeRef == *nodeRef)
84941281cf3SAxel Dörfler 			|| (device && item->nodeRef.device == *device)) {
85041281cf3SAxel Dörfler 
85141281cf3SAxel Dörfler 			if (device && be_roster->IsRunning(&item->entryRef))
85241281cf3SAxel Dörfler 				continue;
85341281cf3SAxel Dörfler 
85441281cf3SAxel Dörfler 			RemoveIcon(item->id);
85541281cf3SAxel Dörfler 
85641281cf3SAxel Dörfler 			if (!removeAll)
85741281cf3SAxel Dörfler 				break;
85841281cf3SAxel Dörfler 		}
85941281cf3SAxel Dörfler 	}
86041281cf3SAxel Dörfler }
86141281cf3SAxel Dörfler 
86241281cf3SAxel Dörfler 
86341281cf3SAxel Dörfler void
86441281cf3SAxel Dörfler TReplicantTray::RemoveItem(int32 id)
86541281cf3SAxel Dörfler {
86641281cf3SAxel Dörfler 	DeskbarItemInfo *item = DeskbarItemFor(id);
86741281cf3SAxel Dörfler 	if (item == NULL)
86841281cf3SAxel Dörfler 		return;
86941281cf3SAxel Dörfler 
87041281cf3SAxel Dörfler 	// attribute was added via Deskbar API (AddItem(entry_ref *, int32 *)
87141281cf3SAxel Dörfler 	if (item->isAddOn) {
87241281cf3SAxel Dörfler 		BNode node(&item->entryRef);
87341281cf3SAxel Dörfler 		node.RemoveAttr(kStatusPredicate);
87441281cf3SAxel Dörfler 		watch_node(&item->nodeRef, B_STOP_WATCHING, this, Window());
87541281cf3SAxel Dörfler 	}
87641281cf3SAxel Dörfler 
87741281cf3SAxel Dörfler 	fItemList->RemoveItem(item);
87841281cf3SAxel Dörfler 	delete item;
87941281cf3SAxel Dörfler }
88041281cf3SAxel Dörfler 
88141281cf3SAxel Dörfler 
88241281cf3SAxel Dörfler /**	ENTRY_MOVED message, moving only occurs on a device
88341281cf3SAxel Dörfler  *	copying will occur (ENTRY_CREATED) between devices
88441281cf3SAxel Dörfler  */
88541281cf3SAxel Dörfler 
88641281cf3SAxel Dörfler void
88741281cf3SAxel Dörfler TReplicantTray::MoveItem(entry_ref *ref, ino_t toDirectory)
88841281cf3SAxel Dörfler {
88941281cf3SAxel Dörfler 	if (!ref)
89041281cf3SAxel Dörfler 		return;
89141281cf3SAxel Dörfler 
89241281cf3SAxel Dörfler 	// scan for a matching entry_ref and update it
89341281cf3SAxel Dörfler 	//
89441281cf3SAxel Dörfler 	// don't need to change node info as it does not change
89541281cf3SAxel Dörfler 
89641281cf3SAxel Dörfler 	for (int32 i = fItemList->CountItems(); i-- > 0 ;) {
89741281cf3SAxel Dörfler 		DeskbarItemInfo *item = (DeskbarItemInfo *)fItemList->ItemAt(i);
89841281cf3SAxel Dörfler 		if (!item)
89941281cf3SAxel Dörfler 			continue;
90041281cf3SAxel Dörfler 
90141281cf3SAxel Dörfler 		if (!strcmp(item->entryRef.name, ref->name)
90241281cf3SAxel Dörfler 			&& item->entryRef.device == ref->device
90341281cf3SAxel Dörfler 			&& item->entryRef.directory == ref->directory) {
90441281cf3SAxel Dörfler 			item->entryRef.directory = toDirectory;
90541281cf3SAxel Dörfler 			break;
90641281cf3SAxel Dörfler 		}
90741281cf3SAxel Dörfler 	}
90841281cf3SAxel Dörfler }
90941281cf3SAxel Dörfler 
91041281cf3SAxel Dörfler #endif	//	add-on support
91141281cf3SAxel Dörfler 
91241281cf3SAxel Dörfler //	external add-on API routines
91341281cf3SAxel Dörfler //	called using the new BDeskbar class
91441281cf3SAxel Dörfler 
91541281cf3SAxel Dörfler //	existence of icon/replicant by name or ID
91641281cf3SAxel Dörfler //	returns opposite
91741281cf3SAxel Dörfler //	note: name and id are semi-private limiting
91841281cf3SAxel Dörfler //		the ability of non-host apps to remove
91941281cf3SAxel Dörfler //		icons without a little bit of work
92041281cf3SAxel Dörfler 
92141281cf3SAxel Dörfler /**	for a specific id
92241281cf3SAxel Dörfler  *	return the name of the replicant (name of view)
92341281cf3SAxel Dörfler  */
92441281cf3SAxel Dörfler 
92541281cf3SAxel Dörfler status_t
92641281cf3SAxel Dörfler TReplicantTray::ItemInfo(int32 id, const char **name)
92741281cf3SAxel Dörfler {
92841281cf3SAxel Dörfler 	if (id < 0)
92941281cf3SAxel Dörfler 		return B_ERROR;
93041281cf3SAxel Dörfler 
93141281cf3SAxel Dörfler 	int32 index, temp;
93241281cf3SAxel Dörfler 	BView *view = ViewAt(&index, &temp, id, false);
93341281cf3SAxel Dörfler 	if (view) {
93441281cf3SAxel Dörfler 		*name = view->Name();
93541281cf3SAxel Dörfler 		return B_OK;
93641281cf3SAxel Dörfler 	}
93741281cf3SAxel Dörfler 
93841281cf3SAxel Dörfler 	return B_ERROR;
93941281cf3SAxel Dörfler }
94041281cf3SAxel Dörfler 
94141281cf3SAxel Dörfler 
9427da06231SAxel Dörfler /**	for a specific name
9437da06231SAxel Dörfler  *	return the id (internal to Deskbar)
9447da06231SAxel Dörfler  */
9457da06231SAxel Dörfler 
94641281cf3SAxel Dörfler status_t
94741281cf3SAxel Dörfler TReplicantTray::ItemInfo(const char *name, int32 *id)
94841281cf3SAxel Dörfler {
94941281cf3SAxel Dörfler 	if (!name || strlen(name) <= 0)
95041281cf3SAxel Dörfler 		return B_ERROR;
95141281cf3SAxel Dörfler 
95241281cf3SAxel Dörfler 	int32 index;
95341281cf3SAxel Dörfler 	BView *view = ViewAt(&index, id, name);
95441281cf3SAxel Dörfler 	if (view)
95541281cf3SAxel Dörfler 		return B_OK;
95641281cf3SAxel Dörfler 
95741281cf3SAxel Dörfler 	return B_ERROR;
95841281cf3SAxel Dörfler }
95941281cf3SAxel Dörfler 
96041281cf3SAxel Dörfler 
9617da06231SAxel Dörfler /**	at a specific index
9627da06231SAxel Dörfler  *	return both the name and the id of the replicant
9637da06231SAxel Dörfler  */
9647da06231SAxel Dörfler 
96541281cf3SAxel Dörfler status_t
96641281cf3SAxel Dörfler TReplicantTray::ItemInfo(int32 index, const char **name, int32 *id)
96741281cf3SAxel Dörfler {
96841281cf3SAxel Dörfler 	if (index < 0)
96941281cf3SAxel Dörfler 		return B_ERROR;
97041281cf3SAxel Dörfler 
97141281cf3SAxel Dörfler 	BView *view;
97241281cf3SAxel Dörfler 	fShelf->ReplicantAt(index, &view, (uint32 *)id, NULL);
97341281cf3SAxel Dörfler 	if (view) {
97441281cf3SAxel Dörfler 		*name = view->Name();
97541281cf3SAxel Dörfler 		return B_OK;
97641281cf3SAxel Dörfler 	}
97741281cf3SAxel Dörfler 
97841281cf3SAxel Dörfler 	return B_ERROR;
97941281cf3SAxel Dörfler }
98041281cf3SAxel Dörfler 
98141281cf3SAxel Dörfler 
9827da06231SAxel Dörfler /**	replicant exists, by id/index */
9837da06231SAxel Dörfler 
98441281cf3SAxel Dörfler bool
98541281cf3SAxel Dörfler TReplicantTray::IconExists(int32 target, bool byIndex)
98641281cf3SAxel Dörfler {
98741281cf3SAxel Dörfler 	int32 index, id;
98841281cf3SAxel Dörfler 	BView *view = ViewAt(&index, &id, target, byIndex);
98941281cf3SAxel Dörfler 
99041281cf3SAxel Dörfler 	return view && index >= 0;
99141281cf3SAxel Dörfler }
99241281cf3SAxel Dörfler 
99341281cf3SAxel Dörfler 
9947da06231SAxel Dörfler /**	replicant exists, by name */
9957da06231SAxel Dörfler 
99641281cf3SAxel Dörfler bool
99741281cf3SAxel Dörfler TReplicantTray::IconExists(const char *name)
99841281cf3SAxel Dörfler {
99941281cf3SAxel Dörfler 	if (!name || strlen(name) == 0)
100041281cf3SAxel Dörfler 		return false;
100141281cf3SAxel Dörfler 
100241281cf3SAxel Dörfler 	int32 index, id;
100341281cf3SAxel Dörfler 	BView *view = ViewAt(&index, &id, name);
100441281cf3SAxel Dörfler 
100541281cf3SAxel Dörfler 	return view && index >= 0;
100641281cf3SAxel Dörfler }
100741281cf3SAxel Dörfler 
100841281cf3SAxel Dörfler 
100941281cf3SAxel Dörfler int32
101041281cf3SAxel Dörfler TReplicantTray::IconCount() const
101141281cf3SAxel Dörfler {
101241281cf3SAxel Dörfler 	return fShelf->CountReplicants();
101341281cf3SAxel Dörfler }
101441281cf3SAxel Dörfler 
101541281cf3SAxel Dörfler 
10169c50f36eSAxel Dörfler /**	Message must contain an archivable view
10177da06231SAxel Dörfler  *	in the Archives folder for later rehydration
10187da06231SAxel Dörfler  *	returns the current boot id
10197da06231SAxel Dörfler  */
10207da06231SAxel Dörfler 
102141281cf3SAxel Dörfler status_t
102241281cf3SAxel Dörfler TReplicantTray::AddIcon(BMessage *icon, int32 *id, const entry_ref *addOn)
102341281cf3SAxel Dörfler {
102441281cf3SAxel Dörfler 	if (!icon || !id)
102541281cf3SAxel Dörfler 		return B_ERROR;
102641281cf3SAxel Dörfler 
10279c50f36eSAxel Dörfler 	// find entry_ref
10289c50f36eSAxel Dörfler 
10299c50f36eSAxel Dörfler 	entry_ref ref;
10309c50f36eSAxel Dörfler 	if (addOn) {
10319c50f36eSAxel Dörfler 		// Use it if we got it
10329c50f36eSAxel Dörfler 		ref = *addOn;
10339c50f36eSAxel Dörfler 	} else {
10349c50f36eSAxel Dörfler 		const char *signature;
10359c50f36eSAxel Dörfler 		status_t status = icon->FindString("add_on", &signature);
10369c50f36eSAxel Dörfler 		if (status == B_OK) {
10379c50f36eSAxel Dörfler 			BRoster roster;
10389c50f36eSAxel Dörfler 			status = roster.FindApp(signature, &ref);
10399c50f36eSAxel Dörfler 		}
10409c50f36eSAxel Dörfler 		if (status < B_OK)
10419c50f36eSAxel Dörfler 			return status;
10429c50f36eSAxel Dörfler 	}
10439c50f36eSAxel Dörfler 
10449c50f36eSAxel Dörfler 	BFile file;
10459c50f36eSAxel Dörfler 	status_t status = file.SetTo(&ref, B_READ_ONLY);
10469c50f36eSAxel Dörfler 	if (status < B_OK)
10479c50f36eSAxel Dörfler 		return status;
10489c50f36eSAxel Dörfler 
10499c50f36eSAxel Dörfler 	node_ref nodeRef;
10509c50f36eSAxel Dörfler 	status = file.GetNodeRef(&nodeRef);
10519c50f36eSAxel Dörfler 	if (status < B_OK)
10529c50f36eSAxel Dörfler 		return status;
10539c50f36eSAxel Dörfler 
10549c50f36eSAxel Dörfler 	BEntry entry(&ref, true);
10559c50f36eSAxel Dörfler 		// ToDo: this resolves an eventual link for the item
10569c50f36eSAxel Dörfler 		// being added - this is okay for now, but in multi-user
10579c50f36eSAxel Dörfler 		// environments, one might want to have links that
10589c50f36eSAxel Dörfler 		// carry the be:deskbar_item_status attribute
10599c50f36eSAxel Dörfler 	status = entry.InitCheck();
10609c50f36eSAxel Dörfler 	if (status != B_OK)
10619c50f36eSAxel Dörfler 		return status;
10629c50f36eSAxel Dörfler 
106341281cf3SAxel Dörfler 	*id = 999;
106441281cf3SAxel Dörfler 	if (icon->what == B_ARCHIVED_OBJECT)
106541281cf3SAxel Dörfler 		icon->what = 0;
106641281cf3SAxel Dörfler 
1067*a5210ab1SAxel Dörfler 	BRect originalBounds = icon->FindRect("_frame");
1068*a5210ab1SAxel Dörfler 		// this is a work-around for buggy replicants that change their
1069*a5210ab1SAxel Dörfler 		// size in AttachedToWindow() (such as "SVM")
1070*a5210ab1SAxel Dörfler 
107141281cf3SAxel Dörfler 	// !! check for name collisions?
10729c50f36eSAxel Dörfler 	status = fShelf->AddReplicant(icon, BPoint(1, 1));
10739c50f36eSAxel Dörfler 	if (status != B_OK)
10749c50f36eSAxel Dörfler 		return status;
107541281cf3SAxel Dörfler 
1076*a5210ab1SAxel Dörfler 	int32 count = fShelf->CountReplicants();
1077*a5210ab1SAxel Dörfler 	BView *view;
1078*a5210ab1SAxel Dörfler 	fShelf->ReplicantAt(count - 1, &view, (uint32 *)id, NULL);
1079*a5210ab1SAxel Dörfler 
1080*a5210ab1SAxel Dörfler 	if (originalBounds != view->Bounds()) {
1081*a5210ab1SAxel Dörfler 		// The replicant changed its size when added to the window, so we need
1082*a5210ab1SAxel Dörfler 		// to recompute all over again (it's already done once via BShelf::AddReplicant()
1083*a5210ab1SAxel Dörfler 		// and TReplicantShelf::CanAcceptReplicantView())
1084*a5210ab1SAxel Dörfler 		RealignReplicants();
1085*a5210ab1SAxel Dörfler 	}
1086*a5210ab1SAxel Dörfler 
108741281cf3SAxel Dörfler 	float oldWidth = Bounds().Width();
108841281cf3SAxel Dörfler 	float oldHeight = Bounds().Height();
108941281cf3SAxel Dörfler 	float width, height;
109041281cf3SAxel Dörfler 	GetPreferredSize(&width, &height);
109141281cf3SAxel Dörfler 	if (oldWidth != width || oldHeight != height)
109241281cf3SAxel Dörfler 		AdjustPlacement();
109341281cf3SAxel Dörfler 
109441281cf3SAxel Dörfler 	// add the item to the add-on list
109541281cf3SAxel Dörfler 
109641281cf3SAxel Dörfler 	AddItem(*id, nodeRef, entry, addOn != NULL);
109741281cf3SAxel Dörfler  	return B_OK;
109841281cf3SAxel Dörfler }
109941281cf3SAxel Dörfler 
110041281cf3SAxel Dörfler 
110141281cf3SAxel Dörfler void
110241281cf3SAxel Dörfler TReplicantTray::RemoveIcon(int32 target, bool byIndex)
110341281cf3SAxel Dörfler {
110441281cf3SAxel Dörfler 	if (target < 0)
110541281cf3SAxel Dörfler 		return;
110641281cf3SAxel Dörfler 
110741281cf3SAxel Dörfler 	int32 index, id;
110841281cf3SAxel Dörfler 	BView *view = ViewAt(&index, &id, target, byIndex);
110941281cf3SAxel Dörfler 	if (view && index >= 0) {
11107da06231SAxel Dörfler 		// remove the reference from the item list & the shelf
111141281cf3SAxel Dörfler 		RemoveItem(id);
111241281cf3SAxel Dörfler 		fShelf->DeleteReplicant(index);
11137da06231SAxel Dörfler 
111441281cf3SAxel Dörfler 		// force a placement update,  !! need to fix BShelf
111541281cf3SAxel Dörfler 		RealReplicantAdjustment(index);
111641281cf3SAxel Dörfler 	}
111741281cf3SAxel Dörfler }
111841281cf3SAxel Dörfler 
111941281cf3SAxel Dörfler 
112041281cf3SAxel Dörfler void
112141281cf3SAxel Dörfler TReplicantTray::RemoveIcon(const char *name)
112241281cf3SAxel Dörfler {
112341281cf3SAxel Dörfler 	if (!name || strlen(name) <= 0)
112441281cf3SAxel Dörfler 		return;
112541281cf3SAxel Dörfler 
112641281cf3SAxel Dörfler 	int32 id, index;
112741281cf3SAxel Dörfler 	BView *view = ViewAt(&index, &id, name);
112841281cf3SAxel Dörfler 	if (view && index >= 0) {
11297da06231SAxel Dörfler 		// remove the reference from the item list & shelf
113041281cf3SAxel Dörfler 		RemoveItem(id);
113141281cf3SAxel Dörfler 		fShelf->DeleteReplicant(index);
11327da06231SAxel Dörfler 
113341281cf3SAxel Dörfler 		// force a placement update,  !! need to fix BShelf
113441281cf3SAxel Dörfler 		RealReplicantAdjustment(index);
113541281cf3SAxel Dörfler 	}
113641281cf3SAxel Dörfler }
113741281cf3SAxel Dörfler 
113841281cf3SAxel Dörfler 
113941281cf3SAxel Dörfler void
11407da06231SAxel Dörfler TReplicantTray::RealReplicantAdjustment(int32 startIndex)
114141281cf3SAxel Dörfler {
11427da06231SAxel Dörfler 	if (startIndex < 0)
114341281cf3SAxel Dörfler 		return;
11447da06231SAxel Dörfler 
11457da06231SAxel Dörfler 	if (startIndex == fLastReplicant)
11467da06231SAxel Dörfler 		startIndex = 0;
11477da06231SAxel Dörfler 
114841281cf3SAxel Dörfler 	// reset the locations of all replicants after the one deleted
11497da06231SAxel Dörfler 	RealignReplicants(startIndex);
115041281cf3SAxel Dörfler 
115141281cf3SAxel Dörfler 	float oldWidth = Bounds().Width();
115241281cf3SAxel Dörfler 	float oldHeight = Bounds().Height();
115341281cf3SAxel Dörfler 	float width, height;
115441281cf3SAxel Dörfler 	GetPreferredSize(&width, &height);
115541281cf3SAxel Dörfler 	if (oldWidth != width || oldHeight != height) {
115641281cf3SAxel Dörfler 		// resize view to accomodate the replicants
115741281cf3SAxel Dörfler 		// redraw as necessary
115841281cf3SAxel Dörfler 		AdjustPlacement();
115941281cf3SAxel Dörfler 	}
116041281cf3SAxel Dörfler }
116141281cf3SAxel Dörfler 
116241281cf3SAxel Dörfler 
11637da06231SAxel Dörfler /**	looking for a replicant by id/index
11647da06231SAxel Dörfler  *	return the view and index
11657da06231SAxel Dörfler  */
11667da06231SAxel Dörfler 
116741281cf3SAxel Dörfler BView *
116841281cf3SAxel Dörfler TReplicantTray::ViewAt(int32 *index, int32 *id, int32 target, bool byIndex)
116941281cf3SAxel Dörfler {
117041281cf3SAxel Dörfler 	*index = -1;
117141281cf3SAxel Dörfler 
117241281cf3SAxel Dörfler 	BView *view;
117341281cf3SAxel Dörfler 	if (byIndex){
117441281cf3SAxel Dörfler 		if (fShelf->ReplicantAt(target, &view, (uint32 *)id)) {
117541281cf3SAxel Dörfler 			if (view) {
117641281cf3SAxel Dörfler 				*index = target;
117741281cf3SAxel Dörfler 				return view;
117841281cf3SAxel Dörfler 			}
117941281cf3SAxel Dörfler 		}
118041281cf3SAxel Dörfler 	} else {
118141281cf3SAxel Dörfler 		int32 count = fShelf->CountReplicants()-1;
118241281cf3SAxel Dörfler 		int32 localid;
118341281cf3SAxel Dörfler 		for (int32 repIndex = count ; repIndex >= 0 ; repIndex--) {
118441281cf3SAxel Dörfler 			fShelf->ReplicantAt(repIndex, &view, (uint32 *)&localid);
118541281cf3SAxel Dörfler 			if (localid == target && view) {
118641281cf3SAxel Dörfler 				*index = repIndex;
118741281cf3SAxel Dörfler 				*id = localid;
118841281cf3SAxel Dörfler 				return view;
118941281cf3SAxel Dörfler 			}
119041281cf3SAxel Dörfler 		}
119141281cf3SAxel Dörfler 	}
119241281cf3SAxel Dörfler 
119341281cf3SAxel Dörfler 	return NULL;
119441281cf3SAxel Dörfler }
119541281cf3SAxel Dörfler 
119641281cf3SAxel Dörfler 
11977da06231SAxel Dörfler /**	looking for a replicant with a view by name
11987da06231SAxel Dörfler  *	return the view, index and the id of the replicant
11997da06231SAxel Dörfler  */
12007da06231SAxel Dörfler 
120141281cf3SAxel Dörfler BView *
120241281cf3SAxel Dörfler TReplicantTray::ViewAt(int32 *index, int32 *id, const char *name)
120341281cf3SAxel Dörfler {
120441281cf3SAxel Dörfler 	*index = -1;
120541281cf3SAxel Dörfler 	*id = -1;
120641281cf3SAxel Dörfler 
120741281cf3SAxel Dörfler 	BView *view;
120841281cf3SAxel Dörfler 	int32 count = fShelf->CountReplicants()-1;
120941281cf3SAxel Dörfler 	for (int32 repIndex = count ; repIndex >= 0 ; repIndex--) {
121041281cf3SAxel Dörfler 		fShelf->ReplicantAt(repIndex, &view, (uint32 *)id);
121141281cf3SAxel Dörfler 		if (view && view->Name() && strcmp(name, view->Name()) == 0) {
121241281cf3SAxel Dörfler 			*index = repIndex;
121341281cf3SAxel Dörfler 			return view;
121441281cf3SAxel Dörfler 		}
121541281cf3SAxel Dörfler 	}
121641281cf3SAxel Dörfler 
121741281cf3SAxel Dörfler 	return NULL;
121841281cf3SAxel Dörfler }
121941281cf3SAxel Dörfler 
122041281cf3SAxel Dörfler 
12217da06231SAxel Dörfler /**	Shelf will call to determine where and if
12227da06231SAxel Dörfler  *	the replicant is to be added
12237da06231SAxel Dörfler  */
12247da06231SAxel Dörfler 
122541281cf3SAxel Dörfler bool
122641281cf3SAxel Dörfler TReplicantTray::AcceptAddon(BRect replicantFrame, BMessage *message)
122741281cf3SAxel Dörfler {
122841281cf3SAxel Dörfler 	if (!message)
122941281cf3SAxel Dörfler 		return false;
123041281cf3SAxel Dörfler 
123141281cf3SAxel Dörfler 	if (replicantFrame.Height() > kMaxReplicantHeight)
123241281cf3SAxel Dörfler 		return false;
123341281cf3SAxel Dörfler 
123441281cf3SAxel Dörfler 	alignment align = B_ALIGN_LEFT;
123541281cf3SAxel Dörfler 	if (fAlignmentSupport && message->HasBool("deskbar:dynamic_align")) {
123641281cf3SAxel Dörfler 		if (!fBarView->Vertical())
123741281cf3SAxel Dörfler 			align = B_ALIGN_RIGHT;
123841281cf3SAxel Dörfler 		else
123941281cf3SAxel Dörfler 			align = fBarView->Left() ? B_ALIGN_LEFT : B_ALIGN_RIGHT;
124041281cf3SAxel Dörfler 	} else if (message->HasInt32("deskbar:align"))
124141281cf3SAxel Dörfler 		message->FindInt32("deskbar:align", (int32 *)&align);
124241281cf3SAxel Dörfler 
124341281cf3SAxel Dörfler 	if (message->HasInt32("deskbar:private_align"))
124441281cf3SAxel Dörfler 		message->FindInt32("deskbar:private_align", (int32 *)&align);
124541281cf3SAxel Dörfler 	else
124641281cf3SAxel Dörfler 		align = B_ALIGN_LEFT;
124741281cf3SAxel Dörfler 
12487da06231SAxel Dörfler 	BPoint loc = LocationForReplicant(fShelf->CountReplicants(),
12497da06231SAxel Dörfler 		replicantFrame.Width());
125041281cf3SAxel Dörfler 
125141281cf3SAxel Dörfler 	message->AddPoint("_pjp_loc", loc);
125241281cf3SAxel Dörfler 	return true;
125341281cf3SAxel Dörfler }
125441281cf3SAxel Dörfler 
125541281cf3SAxel Dörfler 
125641281cf3SAxel Dörfler /**	based on the previous (index - 1) replicant in the list
125741281cf3SAxel Dörfler  *	calculate where the left point should be for this
125841281cf3SAxel Dörfler  *	replicant.  replicant will flow to the right on its own
125941281cf3SAxel Dörfler  */
126041281cf3SAxel Dörfler 
126141281cf3SAxel Dörfler BPoint
12627da06231SAxel Dörfler TReplicantTray::LocationForReplicant(int32 index, float width)
126341281cf3SAxel Dörfler {
126441281cf3SAxel Dörfler 	BPoint loc(kIconGap + 1, kGutter + 1);
126541281cf3SAxel Dörfler 
12667da06231SAxel Dörfler 	if (fMultiRowMode) {
12677da06231SAxel Dörfler 		// try to find free space in every row
12687da06231SAxel Dörfler 		for (int32 row = 0; ; loc.y += kMaxReplicantHeight + kIconGap, row++) {
12697da06231SAxel Dörfler 			// determine free space in this row
12707da06231SAxel Dörfler 			BRect rect(loc.x, loc.y, loc.x + kMinimumTrayWidth + 2, kMaxReplicantHeight);
12717da06231SAxel Dörfler 			if (row == 0 && fBarView->ShowingClock())
12727da06231SAxel Dörfler 				rect.right -= fClock->Frame().Width() + kIconGap;
12737da06231SAxel Dörfler 
12747da06231SAxel Dörfler 			for (int32 i = 0; i < index; i++) {
12757da06231SAxel Dörfler 				BView *view = NULL;
12767da06231SAxel Dörfler 				fShelf->ReplicantAt(i, &view);
12777da06231SAxel Dörfler 				if (view == NULL || view->Frame().top != rect.top)
12787da06231SAxel Dörfler 					continue;
12797da06231SAxel Dörfler 
12807da06231SAxel Dörfler 				rect.left = view->Frame().right + kIconGap + 1;
12817da06231SAxel Dörfler 			}
12827da06231SAxel Dörfler 
12837da06231SAxel Dörfler 			if (rect.Width() >= width) {
12847da06231SAxel Dörfler 				// the icon fits in this row
12857da06231SAxel Dörfler 				loc = rect.LeftTop();
12867da06231SAxel Dörfler 				break;
12877da06231SAxel Dörfler 			}
12887da06231SAxel Dörfler 		}
12897da06231SAxel Dörfler 	} else {
129041281cf3SAxel Dörfler 		if (index > 0) {
129141281cf3SAxel Dörfler 			// get the last replicant added for placement reference
129241281cf3SAxel Dörfler 			BView *view = NULL;
12937da06231SAxel Dörfler 			fShelf->ReplicantAt(index - 1, &view);
129441281cf3SAxel Dörfler 			if (view) {
129541281cf3SAxel Dörfler 				// push this rep placement past the last one
129641281cf3SAxel Dörfler 				loc.x = view->Frame().right + kIconGap + 1;
129741281cf3SAxel Dörfler 				loc.y = view->Frame().top;
129841281cf3SAxel Dörfler 			}
129941281cf3SAxel Dörfler 		}
13007da06231SAxel Dörfler 	}
130141281cf3SAxel Dörfler 
13027da06231SAxel Dörfler 	if ((loc.y == fRightBottomReplicant.top && loc.x > fRightBottomReplicant.left)
13037da06231SAxel Dörfler 		|| loc.y > fRightBottomReplicant.top) {
13047da06231SAxel Dörfler 		fRightBottomReplicant.Set(loc.x, loc.y, loc.x + width, loc.y + kMaxReplicantHeight);
13057da06231SAxel Dörfler 		fLastReplicant = index;
130641281cf3SAxel Dörfler 	}
13077da06231SAxel Dörfler 
130841281cf3SAxel Dörfler 	return loc;
130941281cf3SAxel Dörfler }
131041281cf3SAxel Dörfler 
131141281cf3SAxel Dörfler 
131241281cf3SAxel Dörfler BRect
131341281cf3SAxel Dörfler TReplicantTray::IconFrame(int32 target, bool byIndex)
131441281cf3SAxel Dörfler {
131541281cf3SAxel Dörfler 	int32 index, id;
131641281cf3SAxel Dörfler 	BView *view = ViewAt(&index, &id, target, byIndex);
131741281cf3SAxel Dörfler 	if (view)
131841281cf3SAxel Dörfler 		return view->Frame();
131941281cf3SAxel Dörfler 
132041281cf3SAxel Dörfler 	return BRect(0, 0, 0, 0);
132141281cf3SAxel Dörfler }
132241281cf3SAxel Dörfler 
132341281cf3SAxel Dörfler 
132441281cf3SAxel Dörfler BRect
132541281cf3SAxel Dörfler TReplicantTray::IconFrame(const char *name)
132641281cf3SAxel Dörfler {
132741281cf3SAxel Dörfler 	if (!name)
132841281cf3SAxel Dörfler 		return BRect(0, 0, 0, 0);
132941281cf3SAxel Dörfler 
133041281cf3SAxel Dörfler 	int32 id, index;
133141281cf3SAxel Dörfler 	BView *view = ViewAt(&index, &id, name);
133241281cf3SAxel Dörfler 	if (view)
133341281cf3SAxel Dörfler 		return view->Frame();
133441281cf3SAxel Dörfler 
133541281cf3SAxel Dörfler 	return BRect(0, 0, 0, 0);
133641281cf3SAxel Dörfler }
133741281cf3SAxel Dörfler 
133841281cf3SAxel Dörfler 
13397da06231SAxel Dörfler /**	Scan from the startIndex and reset the location
13407da06231SAxel Dörfler  *	as defined in LocationForReplicant()
134141281cf3SAxel Dörfler  */
134241281cf3SAxel Dörfler 
134341281cf3SAxel Dörfler void
134441281cf3SAxel Dörfler TReplicantTray::RealignReplicants(int32 startIndex)
134541281cf3SAxel Dörfler {
134641281cf3SAxel Dörfler 	if (startIndex < 0)
134741281cf3SAxel Dörfler 		startIndex = 0;
134841281cf3SAxel Dörfler 
134941281cf3SAxel Dörfler 	int32 count = fShelf->CountReplicants();
135041281cf3SAxel Dörfler 	if (count <= 0)
135141281cf3SAxel Dörfler 		return;
135241281cf3SAxel Dörfler 
13537da06231SAxel Dörfler 	if (startIndex == 0)
13547da06231SAxel Dörfler 		fRightBottomReplicant.Set(0, 0, 0, 0);
13557da06231SAxel Dörfler 
135641281cf3SAxel Dörfler 	BView *view = NULL;
135741281cf3SAxel Dörfler 	for (int32 i = startIndex ; i < count ; i++){
135841281cf3SAxel Dörfler 		fShelf->ReplicantAt(i, &view);
13597da06231SAxel Dörfler 		BPoint loc = LocationForReplicant(i, view->Frame().Width());
136041281cf3SAxel Dörfler 		if (view && (view->Frame().LeftTop() != loc)) {
136141281cf3SAxel Dörfler 			view->MoveTo(loc);
136241281cf3SAxel Dörfler 		}
136341281cf3SAxel Dörfler 	}
136441281cf3SAxel Dörfler }
136541281cf3SAxel Dörfler 
136641281cf3SAxel Dörfler 
136741281cf3SAxel Dörfler void
136841281cf3SAxel Dörfler TReplicantTray::SetMultiRow(bool state)
136941281cf3SAxel Dörfler {
137041281cf3SAxel Dörfler 	fMultiRowMode = state;
137141281cf3SAxel Dörfler 
137241281cf3SAxel Dörfler 	// in multi-row state, we only want the short date
137341281cf3SAxel Dörfler 
137441281cf3SAxel Dörfler 	if (fClock != NULL)
137541281cf3SAxel Dörfler 		fClock->AllowFullDate(!state);
137641281cf3SAxel Dörfler }
137741281cf3SAxel Dörfler 
137841281cf3SAxel Dörfler 
137941281cf3SAxel Dörfler //	#pragma mark -
138041281cf3SAxel Dörfler 
138141281cf3SAxel Dörfler 
13827da06231SAxel Dörfler /**	draggable region that is asynchronous so that
13837da06231SAxel Dörfler  *	dragging does not block other activities
13847da06231SAxel Dörfler  */
13857da06231SAxel Dörfler 
138641281cf3SAxel Dörfler TDragRegion::TDragRegion(TBarView *parent, BView *child)
138741281cf3SAxel Dörfler 	:	BControl(BRect(0, 0, 0, 0), "", "", NULL, B_FOLLOW_NONE,
138841281cf3SAxel Dörfler 			B_WILL_DRAW | B_FRAME_EVENTS),
138941281cf3SAxel Dörfler 		fBarView(parent),
139041281cf3SAxel Dörfler 		fChild(child),
139141281cf3SAxel Dörfler 		fDragLocation(kAutoPlaceDragRegion)
139241281cf3SAxel Dörfler {
139341281cf3SAxel Dörfler }
139441281cf3SAxel Dörfler 
139541281cf3SAxel Dörfler 
139641281cf3SAxel Dörfler void
139741281cf3SAxel Dörfler TDragRegion::AttachedToWindow()
139841281cf3SAxel Dörfler {
139941281cf3SAxel Dörfler 	BView::AttachedToWindow();
140041281cf3SAxel Dörfler 	SetViewColor(ui_color(B_MENU_BACKGROUND_COLOR));
140141281cf3SAxel Dörfler 	ResizeToPreferred();
140241281cf3SAxel Dörfler }
140341281cf3SAxel Dörfler 
140441281cf3SAxel Dörfler 
140541281cf3SAxel Dörfler void
140641281cf3SAxel Dörfler TDragRegion::GetPreferredSize(float *width, float *height)
140741281cf3SAxel Dörfler {
140841281cf3SAxel Dörfler 	fChild->ResizeToPreferred();
140941281cf3SAxel Dörfler 	*width = fChild->Bounds().Width();
141041281cf3SAxel Dörfler 	*height = fChild->Bounds().Height();
141141281cf3SAxel Dörfler 
141241281cf3SAxel Dörfler 	if (fDragLocation != kNoDragRegion)
141341281cf3SAxel Dörfler 		*width += 7;
141441281cf3SAxel Dörfler 	else
141541281cf3SAxel Dörfler 		*width += 6;
141641281cf3SAxel Dörfler 
141741281cf3SAxel Dörfler 	*height += 3;
141841281cf3SAxel Dörfler }
141941281cf3SAxel Dörfler 
142041281cf3SAxel Dörfler 
142141281cf3SAxel Dörfler void
142241281cf3SAxel Dörfler TDragRegion::FrameMoved(BPoint)
142341281cf3SAxel Dörfler {
142441281cf3SAxel Dörfler 	if (fBarView->Left() && fBarView->Vertical() && fDragLocation != kNoDragRegion)
142541281cf3SAxel Dörfler 		fChild->MoveTo(5,2);
142641281cf3SAxel Dörfler 	else
142741281cf3SAxel Dörfler 		fChild->MoveTo(2,2);
142841281cf3SAxel Dörfler }
142941281cf3SAxel Dörfler 
143041281cf3SAxel Dörfler 
143141281cf3SAxel Dörfler void
143241281cf3SAxel Dörfler TDragRegion::Draw(BRect)
143341281cf3SAxel Dörfler {
143441281cf3SAxel Dörfler 	rgb_color menuColor = ui_color(B_MENU_BACKGROUND_COLOR);
143541281cf3SAxel Dörfler 	rgb_color hilite = tint_color(menuColor, B_DARKEN_1_TINT);
143641281cf3SAxel Dörfler 	rgb_color vdark = tint_color(menuColor, B_DARKEN_3_TINT);
143741281cf3SAxel Dörfler 	rgb_color vvdark = tint_color(menuColor, B_DARKEN_4_TINT);
143841281cf3SAxel Dörfler 	rgb_color light = tint_color(menuColor, B_LIGHTEN_2_TINT);
143941281cf3SAxel Dörfler 
144041281cf3SAxel Dörfler 	BRect frame(Bounds());
144141281cf3SAxel Dörfler 	BeginLineArray(4);
144241281cf3SAxel Dörfler 
144341281cf3SAxel Dörfler 	if (fBarView->Vertical()) {
144441281cf3SAxel Dörfler 		AddLine(frame.LeftTop(), frame.RightTop(), light);
144541281cf3SAxel Dörfler 		AddLine(frame.LeftTop(), frame.LeftBottom(), light);
144641281cf3SAxel Dörfler 		AddLine(frame.RightBottom(), frame.RightTop(), hilite);
144741281cf3SAxel Dörfler 	} else if (fBarView->AcrossTop()) {
144841281cf3SAxel Dörfler 		AddLine(frame.LeftTop()+BPoint(0, 1), frame.RightTop()+BPoint(-1, 1),
144941281cf3SAxel Dörfler 			light);
145041281cf3SAxel Dörfler 		AddLine(frame.RightTop(), frame.RightBottom(), vvdark);
145141281cf3SAxel Dörfler 		AddLine(frame.RightTop()+BPoint(-1, 2),frame.RightBottom()+BPoint(-1, -1),
145241281cf3SAxel Dörfler 			hilite);
145341281cf3SAxel Dörfler 		AddLine(frame.LeftBottom(), frame.RightBottom()+BPoint(-1, 0), hilite);
145441281cf3SAxel Dörfler 	} else if (fBarView->AcrossBottom()) {
145541281cf3SAxel Dörfler 		AddLine(frame.LeftTop()+BPoint(0, 1), frame.RightTop()+BPoint(-1, 1), light);
145641281cf3SAxel Dörfler 		AddLine(frame.LeftBottom(), frame.RightBottom(), hilite);
145741281cf3SAxel Dörfler 		AddLine(frame.RightTop(), frame.RightBottom(), vvdark);
145841281cf3SAxel Dörfler 		AddLine(frame.RightTop()+BPoint(-1, 1),frame.RightBottom()+BPoint(-1, -1),
145941281cf3SAxel Dörfler 			hilite);
146041281cf3SAxel Dörfler 	}
146141281cf3SAxel Dörfler 
146241281cf3SAxel Dörfler 	EndLineArray();
146341281cf3SAxel Dörfler 
146441281cf3SAxel Dörfler 	if (fDragLocation != kDontDrawDragRegion || fDragLocation != kNoDragRegion)
146541281cf3SAxel Dörfler 		DrawDragRegion();
146641281cf3SAxel Dörfler }
146741281cf3SAxel Dörfler 
146841281cf3SAxel Dörfler 
146941281cf3SAxel Dörfler void
147041281cf3SAxel Dörfler TDragRegion::DrawDragRegion()
147141281cf3SAxel Dörfler {
147241281cf3SAxel Dörfler 	rgb_color menuColor = ui_color(B_MENU_BACKGROUND_COLOR);
147341281cf3SAxel Dörfler 	rgb_color menuHilite = tint_color(menuColor, B_HIGHLIGHT_BACKGROUND_TINT);
147441281cf3SAxel Dörfler 	rgb_color vdark = tint_color(menuColor, B_DARKEN_3_TINT);
147541281cf3SAxel Dörfler 	rgb_color light = tint_color(menuColor, B_LIGHTEN_2_TINT);
147641281cf3SAxel Dörfler 
147741281cf3SAxel Dörfler 	BRect dragRegion(DragRegion());
147841281cf3SAxel Dörfler 
147941281cf3SAxel Dörfler 	BeginLineArray(dragRegion.IntegerHeight());
148041281cf3SAxel Dörfler 	BPoint pt = dragRegion.LeftTop() + BPoint(1,1);
148141281cf3SAxel Dörfler 
148241281cf3SAxel Dörfler 	// Draw drag region highlighted if tracking mouse
148341281cf3SAxel Dörfler 	if (IsTracking()) {
148441281cf3SAxel Dörfler 		SetHighColor(menuHilite);
148541281cf3SAxel Dörfler 		FillRect(dragRegion);
148641281cf3SAxel Dörfler 		while (pt.y + 1 <= dragRegion.bottom) {
148741281cf3SAxel Dörfler 			AddLine(pt, pt, light);
148841281cf3SAxel Dörfler 			AddLine(pt+BPoint(1,1), pt+BPoint(1,1), vdark);
148941281cf3SAxel Dörfler 
149041281cf3SAxel Dörfler 			pt.y += 3;
149141281cf3SAxel Dörfler 		}
149241281cf3SAxel Dörfler 	} else {
149341281cf3SAxel Dörfler 		while (pt.y + 1 <= dragRegion.bottom) {
149441281cf3SAxel Dörfler 			AddLine(pt, pt, vdark);
149541281cf3SAxel Dörfler 			AddLine(pt+BPoint(1,1), pt+BPoint(1,1), light);
149641281cf3SAxel Dörfler 
149741281cf3SAxel Dörfler 			pt.y += 3;
149841281cf3SAxel Dörfler 		}
149941281cf3SAxel Dörfler 	}
150041281cf3SAxel Dörfler 	EndLineArray();
150141281cf3SAxel Dörfler }
150241281cf3SAxel Dörfler 
150341281cf3SAxel Dörfler 
150441281cf3SAxel Dörfler BRect
150541281cf3SAxel Dörfler TDragRegion::DragRegion() const
150641281cf3SAxel Dörfler {
150741281cf3SAxel Dörfler 	BRect dragRegion(Bounds());
150841281cf3SAxel Dörfler 	dragRegion.top += 2;
150941281cf3SAxel Dörfler 	dragRegion.bottom -= 2;
151041281cf3SAxel Dörfler 
151141281cf3SAxel Dörfler 	bool placeOnLeft=false;
151241281cf3SAxel Dörfler 	if (fDragLocation == kAutoPlaceDragRegion) {
151341281cf3SAxel Dörfler 		if (fBarView->Vertical() && fBarView->Left())
151441281cf3SAxel Dörfler 			placeOnLeft = true;
151541281cf3SAxel Dörfler 		else
151641281cf3SAxel Dörfler 			placeOnLeft = false;
151741281cf3SAxel Dörfler 	} else if (fDragLocation == kDragRegionLeft)
151841281cf3SAxel Dörfler 		placeOnLeft = true;
151941281cf3SAxel Dörfler 	else if (fDragLocation == kDragRegionRight)
152041281cf3SAxel Dörfler 		placeOnLeft = false;
152141281cf3SAxel Dörfler 
152241281cf3SAxel Dörfler 	if (placeOnLeft) {
152341281cf3SAxel Dörfler 		dragRegion.left += 1;
152441281cf3SAxel Dörfler 		dragRegion.right = dragRegion.left + 3;
152541281cf3SAxel Dörfler 	} else {
152641281cf3SAxel Dörfler 		dragRegion.right -= 1;
152741281cf3SAxel Dörfler 		dragRegion.left = dragRegion.right - 3;
152841281cf3SAxel Dörfler 	}
152941281cf3SAxel Dörfler 
153041281cf3SAxel Dörfler 	return dragRegion;
153141281cf3SAxel Dörfler }
153241281cf3SAxel Dörfler 
153341281cf3SAxel Dörfler 
153441281cf3SAxel Dörfler void
153541281cf3SAxel Dörfler TDragRegion::MouseDown(BPoint thePoint)
153641281cf3SAxel Dörfler {
153741281cf3SAxel Dörfler 	ulong buttons;
153841281cf3SAxel Dörfler 	BPoint where;
153941281cf3SAxel Dörfler 	BRect dragRegion(DragRegion());
154041281cf3SAxel Dörfler 
15417da06231SAxel Dörfler 	dragRegion.InsetBy(-2.0f, -2.0f);
15427da06231SAxel Dörfler 		// DragRegion() is designed for drawing, not clicking
154341281cf3SAxel Dörfler 
154441281cf3SAxel Dörfler 	if (!dragRegion.Contains(thePoint))
154541281cf3SAxel Dörfler 		return;
154641281cf3SAxel Dörfler 
154741281cf3SAxel Dörfler 	while(true) {
154841281cf3SAxel Dörfler 		GetMouse(&where, &buttons);
154941281cf3SAxel Dörfler 		if (!buttons)
155041281cf3SAxel Dörfler 			break;
155141281cf3SAxel Dörfler 
155241281cf3SAxel Dörfler 		if ((Window()->Flags() & B_ASYNCHRONOUS_CONTROLS) != 0) {
155341281cf3SAxel Dörfler 			fPreviousPosition = thePoint;
155441281cf3SAxel Dörfler 			SetTracking(true);
155541281cf3SAxel Dörfler 			SetMouseEventMask(B_POINTER_EVENTS,
155641281cf3SAxel Dörfler 				B_NO_POINTER_HISTORY | B_LOCK_WINDOW_FOCUS);
155741281cf3SAxel Dörfler 			Invalidate(DragRegion());
155841281cf3SAxel Dörfler 			break;
155941281cf3SAxel Dörfler 		}
156041281cf3SAxel Dörfler 
156141281cf3SAxel Dörfler 		snooze(25000);
156241281cf3SAxel Dörfler 	}
156341281cf3SAxel Dörfler }
156441281cf3SAxel Dörfler 
156541281cf3SAxel Dörfler 
156641281cf3SAxel Dörfler void
156741281cf3SAxel Dörfler TDragRegion::MouseUp(BPoint pt)
156841281cf3SAxel Dörfler {
156941281cf3SAxel Dörfler 	if (IsTracking()) {
157041281cf3SAxel Dörfler 		SetTracking(false);
157141281cf3SAxel Dörfler 		Invalidate(DragRegion());
157241281cf3SAxel Dörfler 	} else
157341281cf3SAxel Dörfler 		BControl::MouseUp(pt);
157441281cf3SAxel Dörfler }
157541281cf3SAxel Dörfler 
157641281cf3SAxel Dörfler 
157741281cf3SAxel Dörfler bool
157841281cf3SAxel Dörfler TDragRegion::SwitchModeForRect(BPoint mouse, BRect rect,
157941281cf3SAxel Dörfler 	bool newVertical, bool newLeft, bool newTop, int32 newState)
158041281cf3SAxel Dörfler {
158141281cf3SAxel Dörfler 	if (!rect.Contains(mouse))
158241281cf3SAxel Dörfler 		// not our rect
158341281cf3SAxel Dörfler 		return false;
158441281cf3SAxel Dörfler 
158541281cf3SAxel Dörfler 	if (newVertical == fBarView->Vertical()
158641281cf3SAxel Dörfler 		&& newLeft == fBarView->Left()
158741281cf3SAxel Dörfler 		&& newTop == fBarView->Top()
158841281cf3SAxel Dörfler 		&& newState == fBarView->State())
158941281cf3SAxel Dörfler 		// already in the correct mode
159041281cf3SAxel Dörfler 		return true;
159141281cf3SAxel Dörfler 
159241281cf3SAxel Dörfler 	fBarView->ChangeState(newState, newVertical, newLeft, newTop);
159341281cf3SAxel Dörfler 	return true;
159441281cf3SAxel Dörfler }
159541281cf3SAxel Dörfler 
159641281cf3SAxel Dörfler 
159741281cf3SAxel Dörfler void
159841281cf3SAxel Dörfler TDragRegion::MouseMoved(BPoint where, uint32 code, const BMessage *message)
159941281cf3SAxel Dörfler {
160041281cf3SAxel Dörfler 	if (IsTracking()) {
160141281cf3SAxel Dörfler 		BScreen screen;
160241281cf3SAxel Dörfler 		BRect frame = screen.Frame();
160341281cf3SAxel Dörfler 
160441281cf3SAxel Dörfler 		float hDivider = frame.Width() / 6;
160541281cf3SAxel Dörfler 		hDivider = (hDivider < kMinimumWindowWidth + 10.0f) ? kMinimumWindowWidth + 10.0f : hDivider;
160641281cf3SAxel Dörfler 		float miniDivider = frame.top + kMiniHeight + 10.0f;
160741281cf3SAxel Dörfler 		float vDivider = frame.Height() / 2;
160841281cf3SAxel Dörfler #ifdef FULL_MODE
160941281cf3SAxel Dörfler 		float thirdScreen = frame.Height() / 3;
161041281cf3SAxel Dörfler #endif
161141281cf3SAxel Dörfler 		BRect topLeft(frame.left, frame.top, frame.left + hDivider, miniDivider);
161241281cf3SAxel Dörfler 		BRect topMiddle(frame.left + hDivider, frame.top, frame.right - hDivider, vDivider);
161341281cf3SAxel Dörfler 		BRect topRight(frame.right - hDivider, frame.top, frame.right, miniDivider);
161441281cf3SAxel Dörfler 
161541281cf3SAxel Dörfler #ifdef FULL_MODE
161641281cf3SAxel Dörfler 		vDivider = miniDivider + thirdScreen;
161741281cf3SAxel Dörfler #endif
161841281cf3SAxel Dörfler 		BRect middleLeft(frame.left, miniDivider, frame.left + hDivider, vDivider);
161941281cf3SAxel Dörfler 		BRect middleRight(frame.right - hDivider, miniDivider, frame.right, vDivider);
162041281cf3SAxel Dörfler 
162141281cf3SAxel Dörfler #ifdef FULL_MODE
162241281cf3SAxel Dörfler 		BRect leftSide(frame.left, vDivider, frame.left + hDivider, frame.bottom - thirdScreen);
162341281cf3SAxel Dörfler 		BRect rightSide(frame.right - hDivider, vDivider, frame.right, frame.bottom - thirdScreen);
162441281cf3SAxel Dörfler 
162541281cf3SAxel Dörfler 		vDivider = frame.bottom - thirdScreen;
162641281cf3SAxel Dörfler #endif
162741281cf3SAxel Dörfler 		BRect bottomLeft(frame.left, vDivider, frame.left + hDivider, frame.bottom);
162841281cf3SAxel Dörfler 		BRect bottomMiddle(frame.left + hDivider, vDivider, frame.right - hDivider, frame.bottom);
162941281cf3SAxel Dörfler 		BRect bottomRight(frame.right - hDivider, vDivider, frame.right, frame.bottom);
163041281cf3SAxel Dörfler 
163141281cf3SAxel Dörfler 		if (where != fPreviousPosition) {
163241281cf3SAxel Dörfler 			fPreviousPosition = where;
163341281cf3SAxel Dörfler 			ConvertToScreen(&where);
163441281cf3SAxel Dörfler 
163541281cf3SAxel Dörfler 			// use short circuit evaluation for convenience
163641281cf3SAxel Dörfler 			if (SwitchModeForRect(where, topLeft, true, true, true, kMiniState)
163741281cf3SAxel Dörfler 				|| SwitchModeForRect(where, topMiddle, false, true, true, kExpandoState)
163841281cf3SAxel Dörfler 				|| SwitchModeForRect(where, topRight, true, false, true, kMiniState)
163941281cf3SAxel Dörfler 
164041281cf3SAxel Dörfler 				|| SwitchModeForRect(where, middleLeft, true, true, true, kExpandoState)
164141281cf3SAxel Dörfler 				|| SwitchModeForRect(where, middleRight, true, false, true, kExpandoState)
164241281cf3SAxel Dörfler 
164341281cf3SAxel Dörfler #ifdef FULL_MODE
164441281cf3SAxel Dörfler 				|| SwitchModeForRect(where, leftSide, true, true, true, kFullState)
164541281cf3SAxel Dörfler 				|| SwitchModeForRect(where, rightSide, true, false, true, kFullState)
164641281cf3SAxel Dörfler #endif
164741281cf3SAxel Dörfler 				|| SwitchModeForRect(where, bottomLeft, true, true, false, kMiniState)
164841281cf3SAxel Dörfler 				|| SwitchModeForRect(where, bottomMiddle, false, true, false, kExpandoState)
16497da06231SAxel Dörfler 				|| SwitchModeForRect(where, bottomRight, true, false, false, kMiniState))
165041281cf3SAxel Dörfler 				;
165141281cf3SAxel Dörfler 		}
165241281cf3SAxel Dörfler 	} else
165341281cf3SAxel Dörfler 		BControl::MouseMoved(where, code, message);
165441281cf3SAxel Dörfler }
165541281cf3SAxel Dörfler 
165641281cf3SAxel Dörfler 
165741281cf3SAxel Dörfler int32
165841281cf3SAxel Dörfler TDragRegion::DragRegionLocation() const
165941281cf3SAxel Dörfler {
166041281cf3SAxel Dörfler 	return fDragLocation;
166141281cf3SAxel Dörfler }
166241281cf3SAxel Dörfler 
166341281cf3SAxel Dörfler 
166441281cf3SAxel Dörfler void
166541281cf3SAxel Dörfler TDragRegion::SetDragRegionLocation(int32 location)
166641281cf3SAxel Dörfler {
166741281cf3SAxel Dörfler 	if (location == fDragLocation)
166841281cf3SAxel Dörfler 		return;
166941281cf3SAxel Dörfler 
167041281cf3SAxel Dörfler 	fDragLocation = location;
167141281cf3SAxel Dörfler 	Invalidate();
167241281cf3SAxel Dörfler }
167341281cf3SAxel Dörfler 
1674