Style cleanup, no functional change intended

This commit is contained in:
Stephan Aßmus
2012-05-05 17:08:15 +02:00
parent f30d463866
commit 34c3ca13b4
2 changed files with 208 additions and 181 deletions
+131 -104
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2006-2009, Haiku, Inc. All rights reserved. * Copyright 2006-2012, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@@ -47,13 +47,12 @@ static const float kMarkWidth = 14.0;
static const float kBorderOffset = 3.0; static const float kBorderOffset = 3.0;
static const float kTextOffset = 4.0; static const float kTextOffset = 4.0;
class PathListItem : public SimpleItem,
public Observer { class PathListItem : public SimpleItem, public Observer {
public: public:
PathListItem(VectorPath* p, PathListItem(VectorPath* p, PathListView* listView, bool markEnabled)
PathListView* listView, :
bool markEnabled) SimpleItem(""),
: SimpleItem(""),
path(NULL), path(NULL),
fListView(listView), fListView(listView),
fMarkEnabled(markEnabled), fMarkEnabled(markEnabled),
@@ -62,11 +61,13 @@ class PathListItem : public SimpleItem,
SetPath(p); SetPath(p);
} }
virtual ~PathListItem() virtual ~PathListItem()
{ {
SetPath(NULL); SetPath(NULL);
} }
// SimpleItem interface // SimpleItem interface
virtual void Draw(BView* owner, BRect itemFrame, uint32 flags) virtual void Draw(BView* owner, BRect itemFrame, uint32 flags)
{ {
@@ -78,18 +79,13 @@ class PathListItem : public SimpleItem,
owner->GetFontHeight(&fh); owner->GetFontHeight(&fh);
BString truncatedString(Text()); BString truncatedString(Text());
owner->TruncateString(&truncatedString, B_TRUNCATE_MIDDLE, owner->TruncateString(&truncatedString, B_TRUNCATE_MIDDLE,
itemFrame.Width() itemFrame.Width() - kBorderOffset - kMarkWidth - kTextOffset
- kBorderOffset
- kMarkWidth
- kTextOffset
- kBorderOffset); - kBorderOffset);
float height = itemFrame.Height(); float height = itemFrame.Height();
float textHeight = fh.ascent + fh.descent; float textHeight = fh.ascent + fh.descent;
BPoint pos; BPoint pos;
pos.x = itemFrame.left pos.x = itemFrame.left + kBorderOffset + kMarkWidth + kTextOffset;
+ kBorderOffset + kMarkWidth + kTextOffset; pos.y = itemFrame.top + ceilf((height - textHeight) / 2.0 + fh.ascent);
pos.y = itemFrame.top
+ ceilf((height - textHeight) / 2.0 + fh.ascent);
owner->DrawString(truncatedString.String(), pos); owner->DrawString(truncatedString.String(), pos);
if (!fMarkEnabled) if (!fMarkEnabled)
@@ -117,12 +113,14 @@ class PathListItem : public SimpleItem,
} }
} }
// Observer interface // Observer interface
virtual void ObjectChanged(const Observable* object) virtual void ObjectChanged(const Observable* object)
{ {
UpdateText(); UpdateText();
} }
// PathListItem // PathListItem
void SetPath(VectorPath* p) void SetPath(VectorPath* p)
{ {
@@ -142,12 +140,15 @@ class PathListItem : public SimpleItem,
UpdateText(); UpdateText();
} }
} }
void UpdateText() void UpdateText()
{ {
SetText(path->Name()); SetText(path->Name());
Invalidate(); Invalidate();
} }
void SetMarkEnabled(bool enabled) void SetMarkEnabled(bool enabled)
{ {
if (fMarkEnabled == enabled) if (fMarkEnabled == enabled)
@@ -155,6 +156,8 @@ class PathListItem : public SimpleItem,
fMarkEnabled = enabled; fMarkEnabled = enabled;
Invalidate(); Invalidate();
} }
void SetMarked(bool marked) void SetMarked(bool marked)
{ {
if (fMarked == marked) if (fMarked == marked)
@@ -163,9 +166,9 @@ class PathListItem : public SimpleItem,
Invalidate(); Invalidate();
} }
void Invalidate() void Invalidate()
{ {
// :-/
if (fListView->LockLooper()) { if (fListView->LockLooper()) {
fListView->InvalidateItem( fListView->InvalidateItem(
fListView->IndexOf(this)); fListView->IndexOf(this));
@@ -173,7 +176,9 @@ class PathListItem : public SimpleItem,
} }
} }
public:
VectorPath* path; VectorPath* path;
private: private:
PathListView* fListView; PathListView* fListView;
bool fMarkEnabled; bool fMarkEnabled;
@@ -185,32 +190,44 @@ class ShapePathListener : public PathContainerListener,
public ShapeContainerListener { public ShapeContainerListener {
public: public:
ShapePathListener(PathListView* listView) ShapePathListener(PathListView* listView)
: fListView(listView), :
fListView(listView),
fShape(NULL) fShape(NULL)
{ {
} }
virtual ~ShapePathListener() virtual ~ShapePathListener()
{ {
SetShape(NULL); SetShape(NULL);
} }
// PathContainerListener interface // PathContainerListener interface
virtual void PathAdded(VectorPath* path, int32 index) virtual void PathAdded(VectorPath* path, int32 index)
{ {
fListView->_SetPathMarked(path, true); fListView->_SetPathMarked(path, true);
} }
virtual void PathRemoved(VectorPath* path) virtual void PathRemoved(VectorPath* path)
{ {
fListView->_SetPathMarked(path, false); fListView->_SetPathMarked(path, false);
} }
// ShapeContainerListener interface // ShapeContainerListener interface
virtual void ShapeAdded(Shape* shape, int32 index) {} virtual void ShapeAdded(Shape* shape, int32 index)
{
}
virtual void ShapeRemoved(Shape* shape) virtual void ShapeRemoved(Shape* shape)
{ {
fListView->SetCurrentShape(NULL); fListView->SetCurrentShape(NULL);
} }
// ShapePathListener // ShapePathListener
void SetShape(Shape* shape) void SetShape(Shape* shape)
{ {
@@ -226,6 +243,7 @@ class ShapePathListener : public PathContainerListener,
fShape->Paths()->AddListener(this); fShape->Paths()->AddListener(this);
} }
Shape* CurrentShape() const Shape* CurrentShape() const
{ {
return fShape; return fShape;
@@ -236,8 +254,10 @@ class ShapePathListener : public PathContainerListener,
Shape* fShape; Shape* fShape;
}; };
// #pragma mark - // #pragma mark -
enum { enum {
MSG_ADD = 'addp', MSG_ADD = 'addp',
@@ -255,12 +275,11 @@ enum {
MSG_REMOVE = 'remp', MSG_REMOVE = 'remp',
}; };
// constructor
PathListView::PathListView(BRect frame, PathListView::PathListView(BRect frame, const char* name, BMessage* message,
const char* name, BHandler* target)
BMessage* message, BHandler* target) :
: SimpleListView(frame, name, SimpleListView(frame, name, NULL, B_SINGLE_SELECTION_LIST),
NULL, B_SINGLE_SELECTION_LIST),
fMessage(message), fMessage(message),
fMenu(NULL), fMenu(NULL),
@@ -274,22 +293,22 @@ PathListView::PathListView(BRect frame,
SetTarget(target); SetTarget(target);
} }
// destructor
PathListView::~PathListView() PathListView::~PathListView()
{ {
_MakeEmpty(); _MakeEmpty();
delete fMessage; delete fMessage;
if (fPathContainer) if (fPathContainer != NULL)
fPathContainer->RemoveListener(this); fPathContainer->RemoveListener(this);
if (fShapeContainer) if (fShapeContainer != NULL)
fShapeContainer->RemoveListener(fShapePathListener); fShapeContainer->RemoveListener(fShapePathListener);
delete fShapePathListener; delete fShapePathListener;
} }
// SelectionChanged
void void
PathListView::SelectionChanged() PathListView::SelectionChanged()
{ {
@@ -299,7 +318,7 @@ PathListView::SelectionChanged()
// NOTE: single selection list // NOTE: single selection list
PathListItem* item PathListItem* item
= dynamic_cast<PathListItem*>(ItemAt(CurrentSelection(0))); = dynamic_cast<PathListItem*>(ItemAt(CurrentSelection(0)));
if (fMessage) { if (fMessage != NULL) {
BMessage message(*fMessage); BMessage message(*fMessage);
message.AddPointer("path", item ? (void*)item->path : NULL); message.AddPointer("path", item ? (void*)item->path : NULL);
Invoke(&message); Invoke(&message);
@@ -309,11 +328,11 @@ PathListView::SelectionChanged()
_UpdateMenu(); _UpdateMenu();
} }
// MouseDown
void void
PathListView::MouseDown(BPoint where) PathListView::MouseDown(BPoint where)
{ {
if (!fCurrentShape) { if (fCurrentShape == NULL) {
SimpleListView::MouseDown(where); SimpleListView::MouseDown(where);
return; return;
} }
@@ -321,25 +340,22 @@ PathListView::MouseDown(BPoint where)
bool handled = false; bool handled = false;
int32 index = IndexOf(where); int32 index = IndexOf(where);
PathListItem* item = dynamic_cast<PathListItem*>(ItemAt(index)); PathListItem* item = dynamic_cast<PathListItem*>(ItemAt(index));
if (item) { if (item != NULL) {
BRect itemFrame(ItemFrame(index)); BRect itemFrame(ItemFrame(index));
itemFrame.right = itemFrame.left itemFrame.right = itemFrame.left + kBorderOffset + kMarkWidth
+ kBorderOffset + kMarkWidth
+ kTextOffset / 2.0; + kTextOffset / 2.0;
VectorPath* path = item->path; VectorPath* path = item->path;
if (itemFrame.Contains(where) && fCommandStack) { if (itemFrame.Contains(where) && fCommandStack) {
// add or remove the path to the shape // add or remove the path to the shape
::Command* command; ::Command* command;
if (fCurrentShape->Paths()->HasPath(path)) { if (fCurrentShape->Paths()->HasPath(path)) {
command = new UnassignPathCommand( command = new UnassignPathCommand(fCurrentShape, path);
fCurrentShape, path);
} else { } else {
VectorPath* paths[1]; VectorPath* paths[1];
paths[0] = path; paths[0] = path;
command = new AddPathsCommand( command = new AddPathsCommand(fCurrentShape->Paths(),
fCurrentShape->Paths(), paths, 1, false, fCurrentShape->Paths()->CountPaths());
paths, 1, false,
fCurrentShape->Paths()->CountPaths());
} }
fCommandStack->Perform(command); fCommandStack->Perform(command);
handled = true; handled = true;
@@ -350,7 +366,7 @@ PathListView::MouseDown(BPoint where)
SimpleListView::MouseDown(where); SimpleListView::MouseDown(where);
} }
// MessageReceived
void void
PathListView::MessageReceived(BMessage* message) PathListView::MessageReceived(BMessage* message)
{ {
@@ -482,7 +498,7 @@ PathListView::MessageReceived(BMessage* message)
} }
} }
// MakeDragMessage
void void
PathListView::MakeDragMessage(BMessage* message) const PathListView::MakeDragMessage(BMessage* message) const
{ {
@@ -492,37 +508,37 @@ PathListView::MakeDragMessage(BMessage* message) const
for (int32 i = 0; i < count; i++) { for (int32 i = 0; i < count; i++) {
PathListItem* item = dynamic_cast<PathListItem*>( PathListItem* item = dynamic_cast<PathListItem*>(
ItemAt(CurrentSelection(i))); ItemAt(CurrentSelection(i)));
if (item) if (item != NULL)
message->AddPointer("path", (void*)item->path); message->AddPointer("path", (void*)item->path);
else else
break; break;
} }
} }
// AcceptDragMessage
bool bool
PathListView::AcceptDragMessage(const BMessage* message) const PathListView::AcceptDragMessage(const BMessage* message) const
{ {
return SimpleListView::AcceptDragMessage(message); return SimpleListView::AcceptDragMessage(message);
} }
// SetDropTargetRect
void void
PathListView::SetDropTargetRect(const BMessage* message, BPoint where) PathListView::SetDropTargetRect(const BMessage* message, BPoint where)
{ {
SimpleListView::SetDropTargetRect(message, where); SimpleListView::SetDropTargetRect(message, where);
} }
// MoveItems
void void
PathListView::MoveItems(BList& items, int32 toIndex) PathListView::MoveItems(BList& items, int32 toIndex)
{ {
if (!fCommandStack || !fPathContainer) if (fCommandStack == NULL || fPathContainer == NULL)
return; return;
int32 count = items.CountItems(); int32 count = items.CountItems();
VectorPath** paths = new (nothrow) VectorPath*[count]; VectorPath** paths = new (nothrow) VectorPath*[count];
if (!paths) if (paths == NULL)
return; return;
for (int32 i = 0; i < count; i++) { for (int32 i = 0; i < count; i++) {
@@ -531,10 +547,9 @@ PathListView::MoveItems(BList& items, int32 toIndex)
paths[i] = item ? item->path : NULL; paths[i] = item ? item->path : NULL;
} }
MovePathsCommand* command MovePathsCommand* command = new (nothrow) MovePathsCommand(fPathContainer,
= new (nothrow) MovePathsCommand(fPathContainer,
paths, count, toIndex); paths, count, toIndex);
if (!command) { if (command == NULL) {
delete[] paths; delete[] paths;
return; return;
} }
@@ -542,11 +557,11 @@ PathListView::MoveItems(BList& items, int32 toIndex)
fCommandStack->Perform(command); fCommandStack->Perform(command);
} }
// CopyItems
void void
PathListView::CopyItems(BList& items, int32 toIndex) PathListView::CopyItems(BList& items, int32 toIndex)
{ {
if (!fCommandStack || !fPathContainer) if (fCommandStack == NULL || fPathContainer == NULL)
return; return;
int32 count = items.CountItems(); int32 count = items.CountItems();
@@ -558,10 +573,9 @@ PathListView::CopyItems(BList& items, int32 toIndex)
paths[i] = item ? new (nothrow) VectorPath(*item->path) : NULL; paths[i] = item ? new (nothrow) VectorPath(*item->path) : NULL;
} }
AddPathsCommand* command AddPathsCommand* command = new(nothrow) AddPathsCommand(fPathContainer,
= new (nothrow) AddPathsCommand(fPathContainer,
paths, count, true, toIndex); paths, count, true, toIndex);
if (!command) { if (command == NULL) {
for (int32 i = 0; i < count; i++) for (int32 i = 0; i < count; i++)
delete paths[i]; delete paths[i];
return; return;
@@ -570,11 +584,11 @@ PathListView::CopyItems(BList& items, int32 toIndex)
fCommandStack->Perform(command); fCommandStack->Perform(command);
} }
// RemoveItemList
void void
PathListView::RemoveItemList(BList& items) PathListView::RemoveItemList(BList& items)
{ {
if (!fCommandStack || !fPathContainer) if (fCommandStack == NULL || fPathContainer == NULL)
return; return;
int32 count = items.CountItems(); int32 count = items.CountItems();
@@ -582,61 +596,60 @@ PathListView::RemoveItemList(BList& items)
for (int32 i = 0; i < count; i++) { for (int32 i = 0; i < count; i++) {
PathListItem* item = dynamic_cast<PathListItem*>( PathListItem* item = dynamic_cast<PathListItem*>(
(BListItem*)items.ItemAtFast(i)); (BListItem*)items.ItemAtFast(i));
if (item) if (item != NULL)
paths[i] = item->path; paths[i] = item->path;
else else
paths[i] = NULL; paths[i] = NULL;
} }
RemovePathsCommand* command RemovePathsCommand* command = new (nothrow) RemovePathsCommand(
= new (nothrow) RemovePathsCommand(fPathContainer, fPathContainer, paths, count);
paths, count);
fCommandStack->Perform(command); fCommandStack->Perform(command);
} }
// CloneItem
BListItem* BListItem*
PathListView::CloneItem(int32 index) const PathListView::CloneItem(int32 index) const
{ {
if (PathListItem* item = dynamic_cast<PathListItem*>(ItemAt(index))) { if (PathListItem* item = dynamic_cast<PathListItem*>(ItemAt(index))) {
return new PathListItem(item->path, return new(nothrow) PathListItem(item->path,
const_cast<PathListView*>(this), const_cast<PathListView*>(this), fCurrentShape != NULL);
fCurrentShape != NULL);
} }
return NULL; return NULL;
} }
// IndexOfSelectable
int32 int32
PathListView::IndexOfSelectable(Selectable* selectable) const PathListView::IndexOfSelectable(Selectable* selectable) const
{ {
VectorPath* path = dynamic_cast<VectorPath*>(selectable); VectorPath* path = dynamic_cast<VectorPath*>(selectable);
if (!path) if (path == NULL)
return -1; return -1;
for (int32 i = 0; int32 count = CountItems();
PathListItem* item = dynamic_cast<PathListItem*>(ItemAt(i)); for (int32 i = 0; i < count; i++) {
i++) { if (SelectableFor(ItemAt(i)) == path)
if (item->path == path)
return i; return i;
} }
return -1; return -1;
} }
// SelectableFor
Selectable* Selectable*
PathListView::SelectableFor(BListItem* item) const PathListView::SelectableFor(BListItem* item) const
{ {
PathListItem* pathItem = dynamic_cast<PathListItem*>(item); PathListItem* pathItem = dynamic_cast<PathListItem*>(item);
if (pathItem) if (pathItem != NULL)
return pathItem->path; return pathItem->path;
return NULL; return NULL;
} }
// #pragma mark - // #pragma mark -
// PathAdded
void void
PathListView::PathAdded(VectorPath* path, int32 index) PathListView::PathAdded(VectorPath* path, int32 index)
{ {
@@ -653,7 +666,7 @@ PathListView::PathAdded(VectorPath* path, int32 index)
UnlockLooper(); UnlockLooper();
} }
// PathRemoved
void void
PathListView::PathRemoved(VectorPath* path) PathListView::PathRemoved(VectorPath* path)
{ {
@@ -670,9 +683,10 @@ PathListView::PathRemoved(VectorPath* path)
UnlockLooper(); UnlockLooper();
} }
// #pragma mark - // #pragma mark -
// SetPathContainer
void void
PathListView::SetPathContainer(PathContainer* container) PathListView::SetPathContainer(PathContainer* container)
{ {
@@ -680,14 +694,14 @@ PathListView::SetPathContainer(PathContainer* container)
return; return;
// detach from old container // detach from old container
if (fPathContainer) if (fPathContainer != NULL)
fPathContainer->RemoveListener(this); fPathContainer->RemoveListener(this);
_MakeEmpty(); _MakeEmpty();
fPathContainer = container; fPathContainer = container;
if (!fPathContainer) if (fPathContainer == NULL)
return; return;
fPathContainer->AddListener(this); fPathContainer->AddListener(this);
@@ -703,7 +717,7 @@ PathListView::SetPathContainer(PathContainer* container)
// fPathContainer->ReadUnlock(); // fPathContainer->ReadUnlock();
} }
// SetShapeContainer
void void
PathListView::SetShapeContainer(ShapeContainer* container) PathListView::SetShapeContainer(ShapeContainer* container)
{ {
@@ -711,23 +725,23 @@ PathListView::SetShapeContainer(ShapeContainer* container)
return; return;
// detach from old container // detach from old container
if (fShapeContainer) if (fShapeContainer != NULL)
fShapeContainer->RemoveListener(fShapePathListener); fShapeContainer->RemoveListener(fShapePathListener);
fShapeContainer = container; fShapeContainer = container;
if (fShapeContainer) if (fShapeContainer != NULL)
fShapeContainer->AddListener(fShapePathListener); fShapeContainer->AddListener(fShapePathListener);
} }
// SetCommandStack
void void
PathListView::SetCommandStack(CommandStack* stack) PathListView::SetCommandStack(CommandStack* stack)
{ {
fCommandStack = stack; fCommandStack = stack;
} }
// SetMenu
void void
PathListView::SetMenu(BMenu* menu) PathListView::SetMenu(BMenu* menu)
{ {
@@ -781,7 +795,7 @@ PathListView::SetMenu(BMenu* menu)
_UpdateMenu(); _UpdateMenu();
} }
// SetCurrentShape
void void
PathListView::SetCurrentShape(Shape* shape) PathListView::SetCurrentShape(Shape* shape)
{ {
@@ -794,57 +808,70 @@ PathListView::SetCurrentShape(Shape* shape)
_UpdateMarks(); _UpdateMarks();
} }
// #pragma mark - // #pragma mark -
// _AddPath
bool bool
PathListView::_AddPath(VectorPath* path, int32 index) PathListView::_AddPath(VectorPath* path, int32 index)
{ {
if (path) { if (path == NULL)
return AddItem( return false;
new PathListItem(path, this, fCurrentShape != NULL), index);
} PathListItem* item = new(nothrow) PathListItem(path, this,
fCurrentShape != NULL);
if (item == NULL)
return false;
if (!AddItem(item, index)) {
delete item;
return false; return false;
} }
// _RemovePath return true;
}
bool bool
PathListView::_RemovePath(VectorPath* path) PathListView::_RemovePath(VectorPath* path)
{ {
PathListItem* item = _ItemForPath(path); PathListItem* item = _ItemForPath(path);
if (item && RemoveItem(item)) { if (item != NULL && RemoveItem(item)) {
delete item; delete item;
return true; return true;
} }
return false; return false;
} }
// _ItemForPath
PathListItem* PathListItem*
PathListView::_ItemForPath(VectorPath* path) const PathListView::_ItemForPath(VectorPath* path) const
{ {
for (int32 i = 0; int32 count = CountItems();
for (int32 i = 0; i < count; i++) {
PathListItem* item = dynamic_cast<PathListItem*>(ItemAt(i)); PathListItem* item = dynamic_cast<PathListItem*>(ItemAt(i));
i++) { if (item == NULL)
continue;
if (item->path == path) if (item->path == path)
return item; return item;
} }
return NULL; return NULL;
} }
// #pragma mark - // #pragma mark -
// _UpdateMarks
void void
PathListView::_UpdateMarks() PathListView::_UpdateMarks()
{ {
int32 count = CountItems(); int32 count = CountItems();
if (fCurrentShape) { if (fCurrentShape != NULL) {
// enable display of marks and mark items whoes // enable display of marks and mark items whoes
// path is contained in fCurrentShape // path is contained in fCurrentShape
for (int32 i = 0; i < count; i++) { for (int32 i = 0; i < count; i++) {
PathListItem* item = dynamic_cast<PathListItem*>(ItemAt(i)); PathListItem* item = dynamic_cast<PathListItem*>(ItemAt(i));
if (!item) if (item == NULL)
continue; continue;
item->SetMarkEnabled(true); item->SetMarkEnabled(true);
item->SetMarked(fCurrentShape->Paths()->HasPath(item->path)); item->SetMarked(fCurrentShape->Paths()->HasPath(item->path));
@@ -853,7 +880,7 @@ PathListView::_UpdateMarks()
// disable display of marks // disable display of marks
for (int32 i = 0; i < count; i++) { for (int32 i = 0; i < count; i++) {
PathListItem* item = dynamic_cast<PathListItem*>(ItemAt(i)); PathListItem* item = dynamic_cast<PathListItem*>(ItemAt(i));
if (!item) if (item == NULL)
continue; continue;
item->SetMarkEnabled(false); item->SetMarkEnabled(false);
} }
@@ -862,20 +889,20 @@ PathListView::_UpdateMarks()
Invalidate(); Invalidate();
} }
// _SetPathMarked
void void
PathListView::_SetPathMarked(VectorPath* path, bool marked) PathListView::_SetPathMarked(VectorPath* path, bool marked)
{ {
if (PathListItem* item = _ItemForPath(path)) { PathListItem* item = _ItemForPath(path);
if (item != NULL)
item->SetMarked(marked); item->SetMarked(marked);
} }
}
// _UpdateMenu
void void
PathListView::_UpdateMenu() PathListView::_UpdateMenu()
{ {
if (!fMenu) if (fMenu == NULL)
return; return;
bool gotSelection = CurrentSelection(0) >= 0; bool gotSelection = CurrentSelection(0) >= 0;