diff --git a/src/apps/icon-o-matic/gui/ShapeListView.cpp b/src/apps/icon-o-matic/gui/ShapeListView.cpp index 6dd90cc0d5..04fa6ea90d 100644 --- a/src/apps/icon-o-matic/gui/ShapeListView.cpp +++ b/src/apps/icon-o-matic/gui/ShapeListView.cpp @@ -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. * * Authors: @@ -41,64 +41,68 @@ using std::nothrow; -class ShapeListItem : public SimpleItem, - public Observer { - public: - ShapeListItem(Shape* s, - ShapeListView* listView) - : SimpleItem(""), - shape(NULL), - fListView(listView) - { - SetClip(s); - } +class ShapeListItem : public SimpleItem, public Observer { +public: + ShapeListItem(Shape* s, ShapeListView* listView) + : + SimpleItem(""), + shape(NULL), + fListView(listView) + { + SetShape(s); + } - virtual ~ShapeListItem() - { - SetClip(NULL); - } - virtual void ObjectChanged(const Observable* object) - { - UpdateText(); - } + virtual ~ShapeListItem() + { + SetShape(NULL); + } - void SetClip(Shape* s) - { - if (s == shape) - return; - if (shape) { - shape->RemoveObserver(this); - shape->Release(); - } + virtual void ObjectChanged(const Observable* object) + { + UpdateText(); + } - shape = s; + void SetShape(Shape* s) + { + if (s == shape) + return; - if (shape) { - shape->Acquire(); - shape->AddObserver(this); - UpdateText(); - } - } - void UpdateText() - { - SetText(shape->Name()); - // :-/ - if (fListView->LockLooper()) { - fListView->InvalidateItem( - fListView->IndexOf(this)); - fListView->UnlockLooper(); - } - } + if (shape) { + shape->RemoveObserver(this); + shape->Release(); + } + shape = s; + + if (shape) { + shape->Acquire(); + shape->AddObserver(this); + UpdateText(); + } + } + + void UpdateText() + { + SetText(shape->Name()); + if (fListView->LockLooper()) { + fListView->InvalidateItem(fListView->IndexOf(this)); + fListView->UnlockLooper(); + } + } + +public: Shape* shape; - private: + +private: ShapeListView* fListView; }; + // #pragma mark - + enum { MSG_REMOVE = 'rmsh', MSG_DUPLICATE = 'dpsh', @@ -108,31 +112,30 @@ enum { MSG_DRAG_SHAPE = 'drgs', }; -// constructor -ShapeListView::ShapeListView(BRect frame, - const char* name, - BMessage* message, BHandler* target) - : SimpleListView(frame, name, - NULL, B_MULTIPLE_SELECTION_LIST), - fMessage(message), - fShapeContainer(NULL), - fCommandStack(NULL) + +ShapeListView::ShapeListView(BRect frame, const char* name, BMessage* message, + BHandler* target) + : + SimpleListView(frame, name, NULL, B_MULTIPLE_SELECTION_LIST), + fMessage(message), + fShapeContainer(NULL), + fCommandStack(NULL) { SetDragCommand(MSG_DRAG_SHAPE); SetTarget(target); } -// destructor + ShapeListView::~ShapeListView() { _MakeEmpty(); delete fMessage; - if (fShapeContainer) + if (fShapeContainer != NULL) fShapeContainer->RemoveListener(this); } -// SelectionChanged + void ShapeListView::SelectionChanged() { @@ -151,7 +154,7 @@ ShapeListView::SelectionChanged() _UpdateMenu(); } -// MessageReceived + void ShapeListView::MessageReceived(BMessage* message) { @@ -159,7 +162,9 @@ ShapeListView::MessageReceived(BMessage* message) case MSG_REMOVE: RemoveSelected(); break; - case MSG_DUPLICATE: { + + case MSG_DUPLICATE: + { int32 count = CountSelectedItems(); int32 index = 0; BList items; @@ -172,7 +177,9 @@ ShapeListView::MessageReceived(BMessage* message) CopyItems(items, index + 1); break; } - case MSG_RESET_TRANSFORMATION: { + + case MSG_RESET_TRANSFORMATION: + { BList shapes; _GetSelectedShapes(shapes); int32 count = shapes.CountItems(); @@ -191,27 +198,30 @@ ShapeListView::MessageReceived(BMessage* message) fCommandStack->Perform(command); break; } - case MSG_FREEZE_TRANSFORMATION: { + + case MSG_FREEZE_TRANSFORMATION: + { BList shapes; _GetSelectedShapes(shapes); int32 count = shapes.CountItems(); if (count < 0) break; - FreezeTransformationCommand* command = - new FreezeTransformationCommand((Shape**)shapes.Items(), + FreezeTransformationCommand* command + = new FreezeTransformationCommand((Shape**)shapes.Items(), count); fCommandStack->Perform(command); break; } + default: SimpleListView::MessageReceived(message); break; } } -// MakeDragMessage + void ShapeListView::MakeDragMessage(BMessage* message) const { @@ -221,51 +231,40 @@ ShapeListView::MakeDragMessage(BMessage* message) const for (int32 i = 0; i < count; i++) { ShapeListItem* item = dynamic_cast( ItemAt(CurrentSelection(i))); - if (item) + if (item != NULL) message->AddPointer("shape", (void*)item->shape); else break; } - -// message->AddInt32("be:actions", B_COPY_TARGET); -// message->AddInt32("be:actions", B_TRASH_TARGET); -// -// message->AddString("be:types", B_FILE_MIME_TYPE); -//// message->AddString("be:filetypes", ""); -//// message->AddString("be:type_descriptions", ""); -// -// message->AddString("be:clip_name", item->shape->Name()); -// -// message->AddString("be:originator", "Icon-O-Matic"); -// message->AddPointer("be:originator_data", (void*)item->shape); } -// AcceptDragMessage + bool ShapeListView::AcceptDragMessage(const BMessage* message) const { return SimpleListView::AcceptDragMessage(message); } -// SetDropTargetRect + void ShapeListView::SetDropTargetRect(const BMessage* message, BPoint where) { SimpleListView::SetDropTargetRect(message, where); } + // #pragma mark - -// MoveItems + void ShapeListView::MoveItems(BList& items, int32 toIndex) { - if (!fCommandStack || !fShapeContainer) + if (fCommandStack == NULL || fShapeContainer == NULL) return; int32 count = items.CountItems(); - Shape** shapes = new (nothrow) Shape*[count]; - if (!shapes) + Shape** shapes = new(nothrow) Shape*[count]; + if (shapes == NULL) return; for (int32 i = 0; i < count; i++) { @@ -274,10 +273,9 @@ ShapeListView::MoveItems(BList& items, int32 toIndex) shapes[i] = item ? item->shape : NULL; } - MoveShapesCommand* command - = new (nothrow) MoveShapesCommand(fShapeContainer, - shapes, count, toIndex); - if (!command) { + MoveShapesCommand* command = new (nothrow) MoveShapesCommand( + fShapeContainer, shapes, count, toIndex); + if (command == NULL) { delete[] shapes; return; } @@ -289,7 +287,7 @@ ShapeListView::MoveItems(BList& items, int32 toIndex) void ShapeListView::CopyItems(BList& items, int32 toIndex) { - if (!fCommandStack || !fShapeContainer) + if (fCommandStack == NULL || fShapeContainer == NULL) return; int32 count = items.CountItems(); @@ -298,14 +296,12 @@ ShapeListView::CopyItems(BList& items, int32 toIndex) for (int32 i = 0; i < count; i++) { ShapeListItem* item = dynamic_cast((BListItem*)items.ItemAtFast(i)); - shapes[i] = item ? new (nothrow) Shape(*item->shape) : NULL; + shapes[i] = item ? new(nothrow) Shape(*item->shape) : NULL; } - AddShapesCommand* command - = new (nothrow) AddShapesCommand(fShapeContainer, - shapes, count, toIndex, - fSelection); - if (!command) { + AddShapesCommand* command = new(nothrow) AddShapesCommand(fShapeContainer, + shapes, count, toIndex, fSelection); + if (command == NULL) { for (int32 i = 0; i < count; i++) delete shapes[i]; return; @@ -314,11 +310,11 @@ ShapeListView::CopyItems(BList& items, int32 toIndex) fCommandStack->Perform(command); } -// RemoveItemList + void ShapeListView::RemoveItemList(BList& items) { - if (!fCommandStack || !fShapeContainer) + if (fCommandStack == NULL || fShapeContainer == NULL) return; int32 count = items.CountItems(); @@ -326,43 +322,44 @@ ShapeListView::RemoveItemList(BList& items) for (int32 i = 0; i < count; i++) indices[i] = IndexOf((BListItem*)items.ItemAtFast(i)); - RemoveShapesCommand* command - = new (nothrow) RemoveShapesCommand(fShapeContainer, - indices, count); + RemoveShapesCommand* command = new(nothrow) RemoveShapesCommand( + fShapeContainer, indices, count); + fCommandStack->Perform(command); } -// CloneItem + BListItem* ShapeListView::CloneItem(int32 index) const { - if (ShapeListItem* item = dynamic_cast(ItemAt(index))) { + ShapeListItem* item = dynamic_cast(ItemAt(index)); + if (item != NULL) { return new ShapeListItem(item->shape, - const_cast(this)); + const_cast(this)); } return NULL; } -// IndexOfSelectable + int32 ShapeListView::IndexOfSelectable(Selectable* selectable) const { Shape* shape = dynamic_cast(selectable); - if (!shape) { + if (shape == NULL) { Transformer* transformer = dynamic_cast(selectable); - if (!transformer) + if (transformer == NULL) return -1; - for (int32 i = 0; - ShapeListItem* item = dynamic_cast(ItemAt(i)); - i++) { - if (item->shape->HasTransformer(transformer)) + int32 count = CountItems(); + for (int32 i = 0; i < count; i++) { + ShapeListItem* item = dynamic_cast(ItemAt(i)); + if (item != NULL && item->shape->HasTransformer(transformer)) return i; } } else { - for (int32 i = 0; - ShapeListItem* item = dynamic_cast(ItemAt(i)); - i++) { - if (item->shape == shape) + int32 count = CountItems(); + for (int32 i = 0; i < count; i++) { + ShapeListItem* item = dynamic_cast(ItemAt(i)); + if (item != NULL && item->shape == shape) return i; } } @@ -370,19 +367,20 @@ ShapeListView::IndexOfSelectable(Selectable* selectable) const return -1; } -// SelectableFor + Selectable* ShapeListView::SelectableFor(BListItem* item) const { ShapeListItem* shapeItem = dynamic_cast(item); - if (shapeItem) + if (shapeItem != NULL) return shapeItem->shape; return NULL; } + // #pragma mark - -// ShapeAdded + void ShapeListView::ShapeAdded(Shape* shape, int32 index) { @@ -399,7 +397,7 @@ ShapeListView::ShapeAdded(Shape* shape, int32 index) UnlockLooper(); } -// ShapeRemoved + void ShapeListView::ShapeRemoved(Shape* shape) { @@ -416,9 +414,10 @@ ShapeListView::ShapeRemoved(Shape* shape) UnlockLooper(); } + // #pragma mark - -// SetMenu + void ShapeListView::SetMenu(BMenu* menu) { @@ -426,6 +425,7 @@ ShapeListView::SetMenu(BMenu* menu) return; fMenu = menu; + if (fMenu == NULL) return; @@ -480,7 +480,7 @@ ShapeListView::SetMenu(BMenu* menu) _UpdateMenu(); } -// SetShapeContainer + void ShapeListView::SetShapeContainer(ShapeContainer* container) { @@ -488,14 +488,14 @@ ShapeListView::SetShapeContainer(ShapeContainer* container) return; // detach from old container - if (fShapeContainer) + if (fShapeContainer != NULL) fShapeContainer->RemoveListener(this); _MakeEmpty(); fShapeContainer = container; - if (!fShapeContainer) + if (fShapeContainer == NULL) return; fShapeContainer->AddListener(this); @@ -506,54 +506,65 @@ ShapeListView::SetShapeContainer(ShapeContainer* container) _AddShape(fShapeContainer->ShapeAtFast(i), i); } -// SetCommandStack + void ShapeListView::SetCommandStack(CommandStack* stack) { fCommandStack = stack; } + // #pragma mark - -// _AddShape + bool ShapeListView::_AddShape(Shape* shape, int32 index) { - if (shape) - return AddItem(new ShapeListItem(shape, this), index); - return false; + if (shape == NULL) + return false; + + ShapeListItem* item = new(std::nothrow) ShapeListItem(shape, this); + if (item == NULL) + return false; + + if (!AddItem(item, index)) { + delete item; + return false; + } + + return true; } -// _RemoveShape + bool ShapeListView::_RemoveShape(Shape* shape) { ShapeListItem* item = _ItemForShape(shape); - if (item && RemoveItem(item)) { + if (item != NULL && RemoveItem(item)) { delete item; return true; } return false; } -// _ItemForShape + ShapeListItem* ShapeListView::_ItemForShape(Shape* shape) const { - for (int32 i = 0; - ShapeListItem* item = dynamic_cast(ItemAt(i)); - i++) { - if (item->shape == shape) + int32 count = CountItems(); + for (int32 i = 0; i < count; i++) { + ShapeListItem* item = dynamic_cast(ItemAt(i)); + if (item != NULL && item->shape == shape) return item; } return NULL; } -// _UpdateMenu + void ShapeListView::_UpdateMenu() { - if (!fMenu) + if (fMenu == NULL) return; bool gotSelection = CurrentSelection(0) >= 0; @@ -564,7 +575,7 @@ ShapeListView::_UpdateMenu() fRemoveMI->SetEnabled(gotSelection); } -// _GetSelectedShapes + void ShapeListView::_GetSelectedShapes(BList& shapes) const { @@ -572,7 +583,7 @@ ShapeListView::_GetSelectedShapes(BList& shapes) const for (int32 i = 0; i < count; i++) { ShapeListItem* item = dynamic_cast( ItemAt(CurrentSelection(i))); - if (item && item->shape) { + if (item != NULL && item->shape != NULL) { if (!shapes.AddItem((void*)item->shape)) break; }