A key-based mechanism to manage sub windows.
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@30402 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2009, Ingo Weinhold, [email protected].
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
#include "SubWindow.h"
|
||||
|
||||
#include <AutoLocker.h>
|
||||
|
||||
#include "SubWindowManager.h"
|
||||
|
||||
|
||||
// #pragma mark - SubWindowKey
|
||||
|
||||
|
||||
SubWindowKey::~SubWindowKey()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
// #pragma mark - ObjectSubWindowKey
|
||||
|
||||
|
||||
ObjectSubWindowKey::ObjectSubWindowKey(void* object)
|
||||
:
|
||||
fObject(object)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
ObjectSubWindowKey::Equals(const SubWindowKey* other) const
|
||||
{
|
||||
if (this == other)
|
||||
return true;
|
||||
|
||||
const ObjectSubWindowKey* key
|
||||
= dynamic_cast<const ObjectSubWindowKey*>(other);
|
||||
return key != NULL && fObject == key->fObject;
|
||||
}
|
||||
|
||||
|
||||
size_t
|
||||
ObjectSubWindowKey::HashCode() const
|
||||
{
|
||||
return (size_t)(addr_t)fObject;
|
||||
}
|
||||
|
||||
|
||||
// #pragma mark - SubWindow
|
||||
|
||||
|
||||
SubWindow::SubWindow(SubWindowManager* manager, BRect frame, const char* title,
|
||||
window_type type, uint32 flags, uint32 workspace)
|
||||
:
|
||||
BWindow(frame, title, type, flags, workspace),
|
||||
fSubWindowManager(manager),
|
||||
fSubWindowKey(NULL)
|
||||
|
||||
{
|
||||
fSubWindowManager->AddReference();
|
||||
}
|
||||
|
||||
|
||||
SubWindow::~SubWindow()
|
||||
{
|
||||
RemoveFromSubWindowManager();
|
||||
fSubWindowManager->RemoveReference();
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
SubWindow::AddToSubWindowManager(SubWindowKey* key)
|
||||
{
|
||||
AutoLocker<SubWindowManager> locker(fSubWindowManager);
|
||||
|
||||
fSubWindowKey = key;
|
||||
|
||||
if (!fSubWindowManager->AddSubWindow(this)) {
|
||||
fSubWindowKey = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
SubWindow::RemoveFromSubWindowManager()
|
||||
{
|
||||
if (fSubWindowKey == NULL)
|
||||
return false;
|
||||
|
||||
fSubWindowManager->RemoveSubWindow(this);
|
||||
delete fSubWindowKey;
|
||||
fSubWindowKey = NULL;
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2009, Ingo Weinhold, [email protected].
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
#ifndef SUB_WINDOW_H
|
||||
#define SUB_WINDOW_H
|
||||
|
||||
#include <Window.h>
|
||||
|
||||
#include <util/OpenHashTable.h>
|
||||
|
||||
|
||||
class SubWindowManager;
|
||||
|
||||
|
||||
class SubWindowKey {
|
||||
public:
|
||||
virtual ~SubWindowKey();
|
||||
|
||||
virtual bool Equals(const SubWindowKey* other) const = 0;
|
||||
virtual size_t HashCode() const = 0;
|
||||
};
|
||||
|
||||
|
||||
class ObjectSubWindowKey : public SubWindowKey {
|
||||
public:
|
||||
ObjectSubWindowKey(void* object);
|
||||
|
||||
virtual bool Equals(const SubWindowKey* other) const;
|
||||
virtual size_t HashCode() const;
|
||||
|
||||
private:
|
||||
void* fObject;
|
||||
};
|
||||
|
||||
|
||||
class SubWindow : public BWindow, public HashTableLink<SubWindow> {
|
||||
public:
|
||||
SubWindow(SubWindowManager* manager,
|
||||
BRect frame, const char* title,
|
||||
window_type type, uint32 flags,
|
||||
uint32 workspace = B_CURRENT_WORKSPACE);
|
||||
virtual ~SubWindow();
|
||||
|
||||
inline SubWindowKey* GetSubWindowKey() const;
|
||||
|
||||
bool AddToSubWindowManager(SubWindowKey* key);
|
||||
bool RemoveFromSubWindowManager();
|
||||
|
||||
protected:
|
||||
SubWindowManager* fSubWindowManager;
|
||||
SubWindowKey* fSubWindowKey;
|
||||
};
|
||||
|
||||
|
||||
SubWindowKey*
|
||||
SubWindow::GetSubWindowKey() const
|
||||
{
|
||||
return fSubWindowKey;
|
||||
}
|
||||
|
||||
|
||||
#endif // SUB_WINDOW_H
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2009, Ingo Weinhold, [email protected].
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
#include "SubWindowManager.h"
|
||||
|
||||
#include <new>
|
||||
|
||||
#include <Message.h>
|
||||
|
||||
#include <AutoLocker.h>
|
||||
|
||||
|
||||
SubWindowManager::SubWindowManager(BLooper* parent)
|
||||
:
|
||||
fParent(parent),
|
||||
fSubWindows()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
SubWindowManager::~SubWindowManager()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
SubWindowManager::Init()
|
||||
{
|
||||
return fSubWindows.Init();
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
SubWindowManager::AddSubWindow(SubWindow* window)
|
||||
{
|
||||
AutoLocker<SubWindowManager> locker(this);
|
||||
|
||||
if (fSubWindows.Lookup(*window->GetSubWindowKey()) != NULL)
|
||||
return false;
|
||||
|
||||
fSubWindows.Insert(window);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
SubWindowManager::RemoveSubWindow(SubWindow* window)
|
||||
{
|
||||
AutoLocker<SubWindowManager> locker(this);
|
||||
|
||||
return fSubWindows.Remove(window);
|
||||
}
|
||||
|
||||
|
||||
SubWindow*
|
||||
SubWindowManager::LookupSubWindow(const SubWindowKey& key) const
|
||||
{
|
||||
return fSubWindows.Lookup(key);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SubWindowManager::Broadcast(uint32 messageCode)
|
||||
{
|
||||
BMessage message(messageCode);
|
||||
Broadcast(&message);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SubWindowManager::Broadcast(BMessage* message)
|
||||
{
|
||||
AutoLocker<SubWindowManager> locker(this);
|
||||
|
||||
SubWindowTable::Iterator it = fSubWindows.GetIterator();
|
||||
while (SubWindow* window = it.Next())
|
||||
window->PostMessage(message);
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2009, Ingo Weinhold, [email protected].
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
#ifndef SUB_WINDOW_MANAGER_H
|
||||
#define SUB_WINDOW_MANAGER_H
|
||||
|
||||
#include <Locker.h>
|
||||
|
||||
#include <Referenceable.h>
|
||||
|
||||
#include <util/OpenHashTable.h>
|
||||
|
||||
#include "SubWindow.h"
|
||||
|
||||
|
||||
class SubWindowManager : public Referenceable, public BLocker {
|
||||
public:
|
||||
SubWindowManager(BLooper* parent);
|
||||
virtual ~SubWindowManager();
|
||||
|
||||
status_t Init();
|
||||
|
||||
bool AddSubWindow(SubWindow* window);
|
||||
bool RemoveSubWindow(SubWindow* window);
|
||||
SubWindow* LookupSubWindow(const SubWindowKey& key) const;
|
||||
|
||||
void Broadcast(uint32 messageCode);
|
||||
void Broadcast(BMessage* message);
|
||||
|
||||
private:
|
||||
struct HashDefinition {
|
||||
typedef SubWindowKey KeyType;
|
||||
typedef SubWindow ValueType;
|
||||
|
||||
size_t HashKey(const SubWindowKey& key) const
|
||||
{
|
||||
return key.HashCode();
|
||||
}
|
||||
|
||||
size_t Hash(const SubWindow* value) const
|
||||
{
|
||||
return value->GetSubWindowKey()->HashCode();
|
||||
}
|
||||
|
||||
bool Compare(const SubWindowKey& key,
|
||||
const SubWindow* value) const
|
||||
{
|
||||
return key.Equals(value->GetSubWindowKey());
|
||||
}
|
||||
|
||||
HashTableLink<SubWindow>* GetLink(SubWindow* value) const
|
||||
{
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
typedef OpenHashTable<HashDefinition> SubWindowTable;
|
||||
|
||||
private:
|
||||
BLooper* fParent;
|
||||
SubWindowTable fSubWindows;
|
||||
};
|
||||
|
||||
|
||||
#endif // SUB_WINDOW_MANAGER_H
|
||||
Reference in New Issue
Block a user