Code style cleanup

This commit is contained in:
Stephan Aßmus
2012-05-05 14:52:40 +02:00
parent d532c167d8
commit 01bcddc1bf
+110 -93
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2006-2009, 2011, Stephan Aßmus <[email protected]>. * Copyright 2006-2012, Stephan Aßmus <[email protected]>.
* All rights reserved. Distributed under the terms of the MIT License. * All rights reserved. Distributed under the terms of the MIT License.
*/ */
@@ -55,10 +55,9 @@ enum {
class StyleListItem : public SimpleItem, class StyleListItem : public SimpleItem,
public Observer { public Observer {
public: public:
StyleListItem(Style* s, StyleListItem(Style* s, StyleListView* listView, bool markEnabled)
StyleListView* listView, :
bool markEnabled) SimpleItem(""),
: SimpleItem(""),
style(NULL), style(NULL),
fListView(listView), fListView(listView),
fMarkEnabled(markEnabled), fMarkEnabled(markEnabled),
@@ -83,18 +82,13 @@ class StyleListItem : 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)
@@ -147,6 +141,7 @@ class StyleListItem : public SimpleItem,
UpdateText(); UpdateText();
} }
} }
void UpdateText() void UpdateText()
{ {
SetText(style->Name()); SetText(style->Name());
@@ -160,6 +155,7 @@ class StyleListItem : public SimpleItem,
fMarkEnabled = enabled; fMarkEnabled = enabled;
Invalidate(); Invalidate();
} }
void SetMarked(bool marked) void SetMarked(bool marked)
{ {
if (fMarked == marked) if (fMarked == marked)
@@ -170,15 +166,15 @@ class StyleListItem : public SimpleItem,
void Invalidate() void Invalidate()
{ {
// :-/
if (fListView->LockLooper()) { if (fListView->LockLooper()) {
fListView->InvalidateItem( fListView->InvalidateItem(fListView->IndexOf(this));
fListView->IndexOf(this));
fListView->UnlockLooper(); fListView->UnlockLooper();
} }
} }
public:
Style* style; Style* style;
private: private:
StyleListView* fListView; StyleListView* fListView;
bool fMarkEnabled; bool fMarkEnabled;
@@ -190,18 +186,25 @@ class ShapeStyleListener : public ShapeListener,
public ShapeContainerListener { public ShapeContainerListener {
public: public:
ShapeStyleListener(StyleListView* listView) ShapeStyleListener(StyleListView* listView)
: fListView(listView), :
fListView(listView),
fShape(NULL) fShape(NULL)
{ {
} }
virtual ~ShapeStyleListener() virtual ~ShapeStyleListener()
{ {
SetShape(NULL); SetShape(NULL);
} }
// ShapeListener interface // ShapeListener interface
virtual void TransformerAdded(Transformer* t, int32 index) {} virtual void TransformerAdded(Transformer* t, int32 index)
virtual void TransformerRemoved(Transformer* t) {} {
}
virtual void TransformerRemoved(Transformer* t)
{
}
virtual void StyleChanged(Style* oldStyle, Style* newStyle) virtual void StyleChanged(Style* oldStyle, Style* newStyle)
{ {
@@ -210,7 +213,10 @@ class ShapeStyleListener : public ShapeListener,
} }
// 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);
@@ -241,14 +247,14 @@ class ShapeStyleListener : public ShapeListener,
Shape* fShape; Shape* fShape;
}; };
// #pragma mark - // #pragma mark -
// constructor
StyleListView::StyleListView(BRect frame, StyleListView::StyleListView(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),
fStyleContainer(NULL), fStyleContainer(NULL),
fShapeContainer(NULL), fShapeContainer(NULL),
@@ -263,29 +269,31 @@ StyleListView::StyleListView(BRect frame,
SetTarget(target); SetTarget(target);
} }
// destructor
StyleListView::~StyleListView() StyleListView::~StyleListView()
{ {
_MakeEmpty(); _MakeEmpty();
delete fMessage; delete fMessage;
if (fStyleContainer) if (fStyleContainer != NULL)
fStyleContainer->RemoveListener(this); fStyleContainer->RemoveListener(this);
if (fShapeContainer) if (fShapeContainer != NULL)
fShapeContainer->RemoveListener(fShapeListener); fShapeContainer->RemoveListener(fShapeListener);
delete fShapeListener; delete fShapeListener;
} }
// #pragma mark - // #pragma mark -
// MessageReceived
void void
StyleListView::MessageReceived(BMessage* message) StyleListView::MessageReceived(BMessage* message)
{ {
switch (message->what) { switch (message->what) {
case MSG_ADD: { case MSG_ADD:
{
Style* style; Style* style;
AddStylesCommand* command; AddStylesCommand* command;
rgb_color color; rgb_color color;
@@ -301,10 +309,13 @@ StyleListView::MessageReceived(BMessage* message)
fCommandStack->Perform(command); fCommandStack->Perform(command);
break; break;
} }
case MSG_REMOVE: case MSG_REMOVE:
RemoveSelected(); RemoveSelected();
break; break;
case MSG_DUPLICATE: {
case MSG_DUPLICATE:
{
int32 count = CountSelectedItems(); int32 count = CountSelectedItems();
int32 index = 0; int32 index = 0;
BList items; BList items;
@@ -317,19 +328,21 @@ StyleListView::MessageReceived(BMessage* message)
CopyItems(items, index + 1); CopyItems(items, index + 1);
break; break;
} }
case MSG_RESET_TRANSFORMATION: {
case MSG_RESET_TRANSFORMATION:
{
int32 count = CountSelectedItems(); int32 count = CountSelectedItems();
BList gradients; BList gradients;
for (int32 i = 0; i < count; i++) { for (int32 i = 0; i < count; i++) {
StyleListItem* item = dynamic_cast<StyleListItem*>( StyleListItem* item = dynamic_cast<StyleListItem*>(
ItemAt(CurrentSelection(i))); ItemAt(CurrentSelection(i)));
if (item && item->style && item->style->Gradient()) if (item && item->style && item->style->Gradient()) {
if (!gradients.AddItem( if (!gradients.AddItem((void*)item->style->Gradient()))
(void*)item->style->Gradient()))
break; break;
} }
}
count = gradients.CountItems(); count = gradients.CountItems();
if (count < 0) if (count <= 0)
break; break;
Transformable* transformables[count]; Transformable* transformables[count];
@@ -338,19 +351,20 @@ StyleListView::MessageReceived(BMessage* message)
transformables[i] = gradient; transformables[i] = gradient;
} }
ResetTransformationCommand* command = ResetTransformationCommand* command
new ResetTransformationCommand(transformables, count); = new ResetTransformationCommand(transformables, count);
fCommandStack->Perform(command); fCommandStack->Perform(command);
break; break;
} }
default: default:
SimpleListView::MessageReceived(message); SimpleListView::MessageReceived(message);
break; break;
} }
} }
// SelectionChanged
void void
StyleListView::SelectionChanged() StyleListView::SelectionChanged()
{ {
@@ -370,11 +384,11 @@ StyleListView::SelectionChanged()
_UpdateMenu(); _UpdateMenu();
} }
// MouseDown
void void
StyleListView::MouseDown(BPoint where) StyleListView::MouseDown(BPoint where)
{ {
if (!fCurrentShape) { if (fCurrentShape == NULL) {
SimpleListView::MouseDown(where); SimpleListView::MouseDown(where);
return; return;
} }
@@ -382,10 +396,9 @@ StyleListView::MouseDown(BPoint where)
bool handled = false; bool handled = false;
int32 index = IndexOf(where); int32 index = IndexOf(where);
StyleListItem* item = dynamic_cast<StyleListItem*>(ItemAt(index)); StyleListItem* item = dynamic_cast<StyleListItem*>(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;
Style* style = item->style; Style* style = item->style;
if (itemFrame.Contains(where)) { if (itemFrame.Contains(where)) {
@@ -405,7 +418,7 @@ StyleListView::MouseDown(BPoint where)
SimpleListView::MouseDown(where); SimpleListView::MouseDown(where);
} }
// MakeDragMessage
void void
StyleListView::MakeDragMessage(BMessage* message) const StyleListView::MakeDragMessage(BMessage* message) const
{ {
@@ -415,37 +428,37 @@ StyleListView::MakeDragMessage(BMessage* message) const
for (int32 i = 0; i < count; i++) { for (int32 i = 0; i < count; i++) {
StyleListItem* item = dynamic_cast<StyleListItem*>( StyleListItem* item = dynamic_cast<StyleListItem*>(
ItemAt(CurrentSelection(i))); ItemAt(CurrentSelection(i)));
if (item) if (item != NULL)
message->AddPointer("style", (void*)item->style); message->AddPointer("style", (void*)item->style);
else else
break; break;
} }
} }
// AcceptDragMessage
bool bool
StyleListView::AcceptDragMessage(const BMessage* message) const StyleListView::AcceptDragMessage(const BMessage* message) const
{ {
return SimpleListView::AcceptDragMessage(message); return SimpleListView::AcceptDragMessage(message);
} }
// SetDropTargetRect
void void
StyleListView::SetDropTargetRect(const BMessage* message, BPoint where) StyleListView::SetDropTargetRect(const BMessage* message, BPoint where)
{ {
SimpleListView::SetDropTargetRect(message, where); SimpleListView::SetDropTargetRect(message, where);
} }
// MoveItems
void void
StyleListView::MoveItems(BList& items, int32 toIndex) StyleListView::MoveItems(BList& items, int32 toIndex)
{ {
if (!fCommandStack || !fStyleContainer) if (fCommandStack == NULL || fStyleContainer == NULL)
return; return;
int32 count = items.CountItems(); int32 count = items.CountItems();
Style** styles = new (nothrow) Style*[count]; Style** styles = new (nothrow) Style*[count];
if (!styles) if (styles == NULL)
return; return;
for (int32 i = 0; i < count; i++) { for (int32 i = 0; i < count; i++) {
@@ -454,10 +467,9 @@ StyleListView::MoveItems(BList& items, int32 toIndex)
styles[i] = item ? item->style : NULL; styles[i] = item ? item->style : NULL;
} }
MoveStylesCommand* command MoveStylesCommand* command = new (nothrow) MoveStylesCommand(
= new (nothrow) MoveStylesCommand(fStyleContainer, fStyleContainer, styles, count, toIndex);
styles, count, toIndex); if (command == NULL) {
if (!command) {
delete[] styles; delete[] styles;
return; return;
} }
@@ -465,11 +477,11 @@ StyleListView::MoveItems(BList& items, int32 toIndex)
fCommandStack->Perform(command); fCommandStack->Perform(command);
} }
// CopyItems
void void
StyleListView::CopyItems(BList& items, int32 toIndex) StyleListView::CopyItems(BList& items, int32 toIndex)
{ {
if (!fCommandStack || !fStyleContainer) if (fCommandStack == NULL || fStyleContainer == NULL)
return; return;
int32 count = items.CountItems(); int32 count = items.CountItems();
@@ -493,7 +505,7 @@ StyleListView::CopyItems(BList& items, int32 toIndex)
fCommandStack->Perform(command); fCommandStack->Perform(command);
} }
// RemoveItemList
void void
StyleListView::RemoveItemList(BList& items) StyleListView::RemoveItemList(BList& items)
{ {
@@ -517,7 +529,7 @@ StyleListView::RemoveItemList(BList& items)
fCommandStack->Perform(command); fCommandStack->Perform(command);
} }
// CloneItem
BListItem* BListItem*
StyleListView::CloneItem(int32 index) const StyleListView::CloneItem(int32 index) const
{ {
@@ -529,37 +541,37 @@ StyleListView::CloneItem(int32 index) const
return NULL; return NULL;
} }
// IndexOfSelectable
int32 int32
StyleListView::IndexOfSelectable(Selectable* selectable) const StyleListView::IndexOfSelectable(Selectable* selectable) const
{ {
Style* style = dynamic_cast<Style*>(selectable); Style* style = dynamic_cast<Style*>(selectable);
if (!style) if (style == NULL)
return -1; return -1;
for (int32 i = 0; int count = CountItems();
StyleListItem* item = dynamic_cast<StyleListItem*>(ItemAt(i)); for (int32 i = 0; i < count; i++) {
i++) { if (SelectableFor(ItemAt(i)) == style)
if (item->style == style)
return i; return i;
} }
return -1; return -1;
} }
// SelectableFor
Selectable* Selectable*
StyleListView::SelectableFor(BListItem* item) const StyleListView::SelectableFor(BListItem* item) const
{ {
StyleListItem* styleItem = dynamic_cast<StyleListItem*>(item); StyleListItem* styleItem = dynamic_cast<StyleListItem*>(item);
if (styleItem) if (styleItem != NULL)
return styleItem->style; return styleItem->style;
return NULL; return NULL;
} }
// #pragma mark - // #pragma mark -
// StyleAdded
void void
StyleListView::StyleAdded(Style* style, int32 index) StyleListView::StyleAdded(Style* style, int32 index)
{ {
@@ -576,7 +588,7 @@ StyleListView::StyleAdded(Style* style, int32 index)
UnlockLooper(); UnlockLooper();
} }
// StyleRemoved
void void
StyleListView::StyleRemoved(Style* style) StyleListView::StyleRemoved(Style* style)
{ {
@@ -593,9 +605,10 @@ StyleListView::StyleRemoved(Style* style)
UnlockLooper(); UnlockLooper();
} }
// #pragma mark - // #pragma mark -
// SetMenu
void void
StyleListView::SetMenu(BMenu* menu) StyleListView::SetMenu(BMenu* menu)
{ {
@@ -629,7 +642,7 @@ StyleListView::SetMenu(BMenu* menu)
_UpdateMenu(); _UpdateMenu();
} }
// SetStyleContainer
void void
StyleListView::SetStyleContainer(StyleContainer* container) StyleListView::SetStyleContainer(StyleContainer* container)
{ {
@@ -637,14 +650,14 @@ StyleListView::SetStyleContainer(StyleContainer* container)
return; return;
// detach from old container // detach from old container
if (fStyleContainer) if (fStyleContainer != NULL)
fStyleContainer->RemoveListener(this); fStyleContainer->RemoveListener(this);
_MakeEmpty(); _MakeEmpty();
fStyleContainer = container; fStyleContainer = container;
if (!fStyleContainer) if (fStyleContainer == NULL)
return; return;
fStyleContainer->AddListener(this); fStyleContainer->AddListener(this);
@@ -655,7 +668,7 @@ StyleListView::SetStyleContainer(StyleContainer* container)
_AddStyle(fStyleContainer->StyleAtFast(i), i); _AddStyle(fStyleContainer->StyleAtFast(i), i);
} }
// SetShapeContainer
void void
StyleListView::SetShapeContainer(ShapeContainer* container) StyleListView::SetShapeContainer(ShapeContainer* container)
{ {
@@ -672,21 +685,21 @@ StyleListView::SetShapeContainer(ShapeContainer* container)
fShapeContainer->AddListener(fShapeListener); fShapeContainer->AddListener(fShapeListener);
} }
// SetCommandStack
void void
StyleListView::SetCommandStack(CommandStack* stack) StyleListView::SetCommandStack(CommandStack* stack)
{ {
fCommandStack = stack; fCommandStack = stack;
} }
// SetCurrentColor
void void
StyleListView::SetCurrentColor(CurrentColor* color) StyleListView::SetCurrentColor(CurrentColor* color)
{ {
fCurrentColor = color; fCurrentColor = color;
} }
// SetCurrentShape
void void
StyleListView::SetCurrentShape(Shape* shape) StyleListView::SetCurrentShape(Shape* shape)
{ {
@@ -699,47 +712,51 @@ StyleListView::SetCurrentShape(Shape* shape)
_UpdateMarks(); _UpdateMarks();
} }
// #pragma mark - // #pragma mark -
// _AddStyle
bool bool
StyleListView::_AddStyle(Style* style, int32 index) StyleListView::_AddStyle(Style* style, int32 index)
{ {
if (style) { if (style != NULL) {
return AddItem(new StyleListItem( return AddItem(new StyleListItem(
style, this, fCurrentShape != NULL), index); style, this, fCurrentShape != NULL), index);
} }
return false; return false;
} }
// _RemoveStyle
bool bool
StyleListView::_RemoveStyle(Style* style) StyleListView::_RemoveStyle(Style* style)
{ {
StyleListItem* item = _ItemForStyle(style); StyleListItem* item = _ItemForStyle(style);
if (item && RemoveItem(item)) { if (item != NULL && RemoveItem(item)) {
delete item; delete item;
return true; return true;
} }
return false; return false;
} }
// _ItemForStyle
StyleListItem* StyleListItem*
StyleListView::_ItemForStyle(Style* style) const StyleListView::_ItemForStyle(Style* style) const
{ {
for (int32 i = 0; int count = CountItems();
for (int32 i = 0; i < count; i++) {
StyleListItem* item = dynamic_cast<StyleListItem*>(ItemAt(i)); StyleListItem* item = dynamic_cast<StyleListItem*>(ItemAt(i));
i++) { if (item == NULL)
continue;
if (item->style == style) if (item->style == style)
return item; return item;
} }
return NULL; return NULL;
} }
// #pragma mark - // #pragma mark -
// _UpdateMarks
void void
StyleListView::_UpdateMarks() StyleListView::_UpdateMarks()
{ {
@@ -749,7 +766,7 @@ StyleListView::_UpdateMarks()
// style is contained in fCurrentShape // style is contained in fCurrentShape
for (int32 i = 0; i < count; i++) { for (int32 i = 0; i < count; i++) {
StyleListItem* item = dynamic_cast<StyleListItem*>(ItemAt(i)); StyleListItem* item = dynamic_cast<StyleListItem*>(ItemAt(i));
if (!item) if (item == NULL)
continue; continue;
item->SetMarkEnabled(true); item->SetMarkEnabled(true);
item->SetMarked(fCurrentShape->Style() == item->style); item->SetMarked(fCurrentShape->Style() == item->style);
@@ -758,7 +775,7 @@ StyleListView::_UpdateMarks()
// disable display of marks // disable display of marks
for (int32 i = 0; i < count; i++) { for (int32 i = 0; i < count; i++) {
StyleListItem* item = dynamic_cast<StyleListItem*>(ItemAt(i)); StyleListItem* item = dynamic_cast<StyleListItem*>(ItemAt(i));
if (!item) if (item == NULL)
continue; continue;
item->SetMarkEnabled(false); item->SetMarkEnabled(false);
} }
@@ -767,20 +784,20 @@ StyleListView::_UpdateMarks()
Invalidate(); Invalidate();
} }
// _SetStyleMarked
void void
StyleListView::_SetStyleMarked(Style* style, bool marked) StyleListView::_SetStyleMarked(Style* style, bool marked)
{ {
if (StyleListItem* item = _ItemForStyle(style)) { StyleListItem* item = _ItemForStyle(style);
if (item != NULL)
item->SetMarked(marked); item->SetMarked(marked);
} }
}
// _UpdateMenu
void void
StyleListView::_UpdateMenu() StyleListView::_UpdateMenu()
{ {
if (!fMenu) if (fMenu == NULL)
return; return;
bool gotSelection = CurrentSelection(0) >= 0; bool gotSelection = CurrentSelection(0) >= 0;