Moved the static stuff into a singleton.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34376 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold
2009-11-30 14:43:35 +00:00
parent d0805bf47e
commit 1cbfe2ef6e
2 changed files with 90 additions and 38 deletions
-5
View File
@@ -98,11 +98,6 @@ private:
void _BuildDefaultPopUp(); void _BuildDefaultPopUp();
void _ShowPopUp(BView* target, BPoint where); void _ShowPopUp(BView* target, BPoint where);
static bool sVisible;
static bool sVisibleInitialized;
static BLocker sLock;
static BList sList;
enum relation { enum relation {
TARGET_UNKNOWN, TARGET_UNKNOWN,
TARGET_IS_CHILD, TARGET_IS_CHILD,
+90 -33
View File
@@ -11,11 +11,11 @@
//! BDragger represents a replicant "handle". //! BDragger represents a replicant "handle".
#include <pthread.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <Alert.h> #include <Alert.h>
#include <Autolock.h>
#include <Beep.h> #include <Beep.h>
#include <Bitmap.h> #include <Bitmap.h>
#include <Dragger.h> #include <Dragger.h>
@@ -25,6 +25,8 @@
#include <Shelf.h> #include <Shelf.h>
#include <Window.h> #include <Window.h>
#include <AutoLocker.h>
#include <AppServerLink.h> #include <AppServerLink.h>
#include <DragTrackingFilter.h> #include <DragTrackingFilter.h>
#include <binary_compatibility/Interface.h> #include <binary_compatibility/Interface.h>
@@ -34,11 +36,6 @@
#include "ZombieReplicantView.h" #include "ZombieReplicantView.h"
bool BDragger::sVisible;
bool BDragger::sVisibleInitialized;
BLocker BDragger::sLock("BDragger static");
BList BDragger::sList;
const uint32 kMsgDragStarted = 'Drgs'; const uint32 kMsgDragStarted = 'Drgs';
const unsigned char const unsigned char
@@ -54,6 +51,58 @@ kHandBitmap[] = {
}; };
namespace {
struct DraggerManager {
bool visible;
bool visibleInitialized;
BList list;
DraggerManager()
:
visible(false),
visibleInitialized(false),
fLock("BDragger static")
{
}
bool Lock()
{
return fLock.Lock();
}
void Unlock()
{
fLock.Unlock();
}
static DraggerManager* Default()
{
if (sDefaultInstance == NULL)
pthread_once(&sDefaultInitOnce, &_InitSingleton);
return sDefaultInstance;
}
private:
static void _InitSingleton()
{
sDefaultInstance = new DraggerManager;
}
private:
BLocker fLock;
static pthread_once_t sDefaultInitOnce;
static DraggerManager* sDefaultInstance;
};
pthread_once_t DraggerManager::sDefaultInitOnce = PTHREAD_ONCE_INIT;
DraggerManager* DraggerManager::sDefaultInstance = NULL;
} // unnamed namespace
BDragger::BDragger(BRect bounds, BView *target, uint32 rmask, uint32 flags) BDragger::BDragger(BRect bounds, BView *target, uint32 rmask, uint32 flags)
: BView(bounds, "_dragger_", rmask, flags), : BView(bounds, "_dragger_", rmask, flags),
fTarget(target), fTarget(target),
@@ -114,7 +163,7 @@ BDragger::Instantiate(BMessage *data)
} }
status_t status_t
BDragger::Archive(BMessage *data, bool deep) const BDragger::Archive(BMessage *data, bool deep) const
{ {
status_t ret = BView::Archive(data, deep); status_t ret = BView::Archive(data, deep);
@@ -122,7 +171,7 @@ BDragger::Archive(BMessage *data, bool deep) const
return ret; return ret;
BMessage popupMsg; BMessage popupMsg;
if (fPopUp && fPopUpIsCustom) { if (fPopUp && fPopUpIsCustom) {
bool windowLocked = fPopUp->Window()->Lock(); bool windowLocked = fPopUp->Window()->Lock();
@@ -144,7 +193,7 @@ BDragger::Archive(BMessage *data, bool deep) const
void void
BDragger::AttachedToWindow() BDragger::AttachedToWindow()
{ {
if (fIsZombie) { if (fIsZombie) {
SetLowColor(kZombieColor); SetLowColor(kZombieColor);
@@ -329,8 +378,10 @@ BDragger::ShowAllDraggers()
status_t status = link.Flush(); status_t status = link.Flush();
if (status == B_OK) { if (status == B_OK) {
sVisible = true; DraggerManager* manager = DraggerManager::Default();
sVisibleInitialized = true; AutoLocker<DraggerManager> locker(manager);
manager->visible = true;
manager->visibleInitialized = true;
} }
return status; return status;
@@ -346,8 +397,10 @@ BDragger::HideAllDraggers()
status_t status = link.Flush(); status_t status = link.Flush();
if (status == B_OK) { if (status == B_OK) {
sVisible = false; DraggerManager* manager = DraggerManager::Default();
sVisibleInitialized = true; AutoLocker<DraggerManager> locker(manager);
manager->visible = false;
manager->visibleInitialized = true;
} }
return status; return status;
@@ -357,21 +410,22 @@ BDragger::HideAllDraggers()
bool bool
BDragger::AreDraggersDrawn() BDragger::AreDraggersDrawn()
{ {
BAutolock _(sLock); DraggerManager* manager = DraggerManager::Default();
AutoLocker<DraggerManager> locker(manager);
if (!sVisibleInitialized) { if (!manager->visibleInitialized) {
BPrivate::AppServerLink link; BPrivate::AppServerLink link;
link.StartMessage(AS_GET_SHOW_ALL_DRAGGERS); link.StartMessage(AS_GET_SHOW_ALL_DRAGGERS);
status_t status; status_t status;
if (link.FlushWithReply(status) == B_OK && status == B_OK) { if (link.FlushWithReply(status) == B_OK && status == B_OK) {
link.Read<bool>(&sVisible); link.Read<bool>(&manager->visible);
sVisibleInitialized = true; manager->visibleInitialized = true;
} else } else
return false; return false;
} }
return sVisible; return manager->visible;
} }
@@ -547,24 +601,26 @@ BDragger::operator=(const BDragger &)
/*static*/ void /*static*/ void
BDragger::_UpdateShowAllDraggers(bool visible) BDragger::_UpdateShowAllDraggers(bool visible)
{ {
BAutolock _(sLock); DraggerManager* manager = DraggerManager::Default();
AutoLocker<DraggerManager> locker(manager);
sVisibleInitialized = true; manager->visibleInitialized = true;
sVisible = visible; manager->visible = visible;
for (int32 i = sList.CountItems(); i-- > 0;) { for (int32 i = manager->list.CountItems(); i-- > 0;) {
BDragger* dragger = (BDragger*)sList.ItemAt(i); BDragger* dragger = (BDragger*)manager->list.ItemAt(i);
BMessenger target(dragger); BMessenger target(dragger);
target.SendMessage(_SHOW_DRAG_HANDLES_); target.SendMessage(_SHOW_DRAG_HANDLES_);
} }
} }
void void
BDragger::_AddToList() BDragger::_AddToList()
{ {
BAutolock _(sLock); DraggerManager* manager = DraggerManager::Default();
sList.AddItem(this); AutoLocker<DraggerManager> locker(manager);
manager->list.AddItem(this);
bool allowsDragging = true; bool allowsDragging = true;
if (fShelf) if (fShelf)
@@ -583,8 +639,9 @@ BDragger::_AddToList()
void void
BDragger::_RemoveFromList() BDragger::_RemoveFromList()
{ {
BAutolock _(sLock); DraggerManager* manager = DraggerManager::Default();
sList.RemoveItem(this); AutoLocker<DraggerManager> locker(manager);
manager->list.RemoveItem(this);
} }
@@ -610,10 +667,10 @@ BDragger::_DetermineRelationship()
if (fRelation == TARGET_IS_PARENT) { if (fRelation == TARGET_IS_PARENT) {
BRect bounds (Frame()); BRect bounds (Frame());
BRect parentBounds (Parent()->Bounds()); BRect parentBounds (Parent()->Bounds());
if (!parentBounds.Contains(bounds)) if (!parentBounds.Contains(bounds))
MoveTo(parentBounds.right - bounds.Width(), MoveTo(parentBounds.right - bounds.Width(),
parentBounds.bottom - bounds.Height()); parentBounds.bottom - bounds.Height());
} }
return B_OK; return B_OK;
} }
@@ -667,7 +724,7 @@ BDragger::_BuildDefaultPopUp()
char about[B_OS_NAME_LENGTH]; char about[B_OS_NAME_LENGTH];
snprintf(about, B_OS_NAME_LENGTH, "About %s" B_UTF8_ELLIPSIS, name); snprintf(about, B_OS_NAME_LENGTH, "About %s" B_UTF8_ELLIPSIS, name);
fPopUp->AddItem(new BMenuItem(about, msg)); fPopUp->AddItem(new BMenuItem(about, msg));
fPopUp->AddSeparatorItem(); fPopUp->AddSeparatorItem();
fPopUp->AddItem(new BMenuItem("Remove Replicant", fPopUp->AddItem(new BMenuItem("Remove Replicant",
@@ -690,7 +747,7 @@ BDragger::_ShowPopUp(BView *target, BPoint where)
BRect rect(0, 0, menuWidth, menuHeight); BRect rect(0, 0, menuWidth, menuHeight);
rect.InsetBy(-0.5, -0.5); rect.InsetBy(-0.5, -0.5);
rect.OffsetTo(point); rect.OffsetTo(point);
fPopUp->Go(point, true, false, rect, true); fPopUp->Go(point, true, false, rect, true);
} }