Many style fixes to ListView

This commit is contained in:
John Scipione
2013-04-09 00:17:37 -04:00
parent 29ceb649f8
commit 63f3755c5f
2 changed files with 68 additions and 56 deletions
+5 -5
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009, Haiku, Inc. All rights reserved. * Copyright 2002-2013 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#ifndef _LIST_VIEW_H #ifndef _LIST_VIEW_H
@@ -58,13 +58,13 @@ public:
virtual void MessageReceived(BMessage* message); virtual void MessageReceived(BMessage* message);
virtual void KeyDown(const char* bytes, int32 numBytes); virtual void KeyDown(const char* bytes, int32 numBytes);
virtual void MouseDown(BPoint where); virtual void MouseDown(BPoint where);
virtual void MouseUp(BPoint point); virtual void MouseUp(BPoint where);
virtual void MouseMoved(BPoint point, uint32 code, virtual void MouseMoved(BPoint where, uint32 code,
const BMessage* dragMessage); const BMessage* dragMessage);
virtual void ResizeToPreferred(); virtual void ResizeToPreferred();
virtual void GetPreferredSize(float* _width, virtual void GetPreferredSize(float *_width,
float* _height); float *_height);
virtual BSize MinSize(); virtual BSize MinSize();
virtual BSize MaxSize(); virtual BSize MaxSize();
+63 -51
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2001-2009, Haiku, Inc. All rights resrerved. * Copyright 2001-2013 Haiku, Inc. All rights resrerved.
* Distributed under the terms of the MIT license. * Distributed under the terms of the MIT license.
* *
* Authors: * Authors:
@@ -39,12 +39,15 @@ const float kDoubleClickTresh = 6;
static property_info sProperties[] = { static property_info sProperties[] = {
{ "Item", { B_COUNT_PROPERTIES, 0 }, { B_DIRECT_SPECIFIER, 0 }, { "Item", { B_COUNT_PROPERTIES, 0 }, { B_DIRECT_SPECIFIER, 0 },
"Returns the number of BListItems currently in the list.", 0, { B_INT32_TYPE } "Returns the number of BListItems currently in the list.", 0,
{ B_INT32_TYPE }
}, },
{ "Item", { B_EXECUTE_PROPERTY, 0 }, { B_INDEX_SPECIFIER, B_REVERSE_INDEX_SPECIFIER, { "Item", { B_EXECUTE_PROPERTY, 0 }, { B_INDEX_SPECIFIER,
B_RANGE_SPECIFIER, B_REVERSE_RANGE_SPECIFIER, 0 }, B_REVERSE_INDEX_SPECIFIER, B_RANGE_SPECIFIER,
"Select and invoke the specified items, first removing any existing selection." B_REVERSE_RANGE_SPECIFIER, 0 },
"Select and invoke the specified items, first removing any existing "
"selection."
}, },
{ "Selection", { B_COUNT_PROPERTIES, 0 }, { B_DIRECT_SPECIFIER, 0 }, { "Selection", { B_COUNT_PROPERTIES, 0 }, { B_DIRECT_SPECIFIER, 0 },
@@ -56,46 +59,52 @@ static property_info sProperties[] = {
}, },
{ "Selection", { B_GET_PROPERTY, 0 }, { B_DIRECT_SPECIFIER, 0 }, { "Selection", { B_GET_PROPERTY, 0 }, { B_DIRECT_SPECIFIER, 0 },
"Returns int32 indices of all items in the selection.", 0, { B_INT32_TYPE } "Returns int32 indices of all items in the selection.", 0,
{ B_INT32_TYPE }
}, },
{ "Selection", { B_SET_PROPERTY, 0 }, { B_INDEX_SPECIFIER, B_REVERSE_INDEX_SPECIFIER, { "Selection", { B_SET_PROPERTY, 0 }, { B_INDEX_SPECIFIER,
B_RANGE_SPECIFIER, B_REVERSE_RANGE_SPECIFIER, 0 }, B_REVERSE_INDEX_SPECIFIER, B_RANGE_SPECIFIER,
"Extends current selection or deselects specified items. Boolean field \"data\" " B_REVERSE_RANGE_SPECIFIER, 0 },
"chooses selection or deselection.", 0, { B_BOOL_TYPE } "Extends current selection or deselects specified items. Boolean field "
"\"data\" chooses selection or deselection.", 0, { B_BOOL_TYPE }
}, },
{ "Selection", { B_SET_PROPERTY, 0 }, { B_DIRECT_SPECIFIER, 0 }, { "Selection", { B_SET_PROPERTY, 0 }, { B_DIRECT_SPECIFIER, 0 },
"Select or deselect all items in the selection. Boolean field \"data\" chooses " "Select or deselect all items in the selection. Boolean field \"data\" "
"selection or deselection.", 0, { B_BOOL_TYPE } "chooses selection or deselection.", 0, { B_BOOL_TYPE }
}, },
}; };
BListView::BListView(BRect frame, const char* name, list_view_type type, BListView::BListView(BRect frame, const char* name, list_view_type type,
uint32 resizingMode, uint32 flags) uint32 resizingMode, uint32 flags)
: BView(frame, name, resizingMode, flags) :
BView(frame, name, resizingMode, flags)
{ {
_InitObject(type); _InitObject(type);
} }
BListView::BListView(const char* name, list_view_type type, uint32 flags) BListView::BListView(const char* name, list_view_type type, uint32 flags)
: BView(name, flags) :
BView(name, flags)
{ {
_InitObject(type); _InitObject(type);
} }
BListView::BListView(list_view_type type) BListView::BListView(list_view_type type)
: BView(NULL, B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE) :
BView(NULL, B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE)
{ {
_InitObject(type); _InitObject(type);
} }
BListView::BListView(BMessage* archive) BListView::BListView(BMessage* archive)
: BView(archive) :
BView(archive)
{ {
int32 listType; int32 listType;
archive->FindInt32("_lv_type", &listType); archive->FindInt32("_lv_type", &listType);
@@ -104,24 +113,24 @@ BListView::BListView(BMessage* archive)
int32 i = 0; int32 i = 0;
BMessage subData; BMessage subData;
while (archive->FindMessage("_l_items", i++, &subData) == B_OK) { while (archive->FindMessage("_l_items", i++, &subData) == B_OK) {
BArchivable *object = instantiate_object(&subData); BArchivable* object = instantiate_object(&subData);
if (!object) if (object == NULL)
continue; continue;
BListItem *item = dynamic_cast<BListItem*>(object); BListItem* item = dynamic_cast<BListItem*>(object);
if (item) if (item != NULL)
AddItem(item); AddItem(item);
} }
if (archive->HasMessage("_msg")) { if (archive->HasMessage("_msg")) {
BMessage *invokationMessage = new BMessage; BMessage* invokationMessage = new BMessage;
archive->FindMessage("_msg", invokationMessage); archive->FindMessage("_msg", invokationMessage);
SetInvocationMessage(invokationMessage); SetInvocationMessage(invokationMessage);
} }
if (archive->HasMessage("_2nd_msg")) { if (archive->HasMessage("_2nd_msg")) {
BMessage *selectionMessage = new BMessage; BMessage* selectionMessage = new BMessage;
archive->FindMessage("_2nd_msg", selectionMessage); archive->FindMessage("_2nd_msg", selectionMessage);
SetSelectionMessage(selectionMessage); SetSelectionMessage(selectionMessage);
@@ -159,7 +168,7 @@ BListView::Archive(BMessage* archive, bool deep) const
status = archive->AddInt32("_lv_type", fListType); status = archive->AddInt32("_lv_type", fListType);
if (status == B_OK && deep) { if (status == B_OK && deep) {
BListItem *item; BListItem* item;
int32 i = 0; int32 i = 0;
while ((item = ItemAt(i++))) { while ((item = ItemAt(i++))) {
@@ -379,11 +388,10 @@ BListView::MessageReceived(BMessage* msg)
void void
BListView::KeyDown(const char *bytes, int32 numBytes) BListView::KeyDown(const char* bytes, int32 numBytes)
{ {
bool extend bool extend = fListType == B_MULTIPLE_SELECTION_LIST
= fListType == B_MULTIPLE_SELECTION_LIST && (modifiers() & B_SHIFT_KEY) != 0;
&& (modifiers() & B_SHIFT_KEY) != 0;
switch (bytes[0]) { switch (bytes[0]) {
case B_UP_ARROW: case B_UP_ARROW:
@@ -427,6 +435,7 @@ BListView::KeyDown(const char *bytes, int32 numBytes)
fAnchorIndex = 0; fAnchorIndex = 0;
} else } else
Select(0, false); Select(0, false);
ScrollToSelection(); ScrollToSelection();
break; break;
case B_END: case B_END:
@@ -435,6 +444,7 @@ BListView::KeyDown(const char *bytes, int32 numBytes)
fAnchorIndex = CountItems() - 1; fAnchorIndex = CountItems() - 1;
} else } else
Select(CountItems() - 1, false); Select(CountItems() - 1, false);
ScrollToSelection(); ScrollToSelection();
break; break;
@@ -477,7 +487,7 @@ BListView::MouseDown(BPoint point)
Window()->UpdateIfNeeded(); Window()->UpdateIfNeeded();
} }
BMessage *message = Looper()->CurrentMessage(); BMessage* message = Looper()->CurrentMessage();
int32 index = IndexOf(point); int32 index = IndexOf(point);
// If the user double (or more) clicked within the current selection, // If the user double (or more) clicked within the current selection,
@@ -533,9 +543,8 @@ BListView::MouseDown(BPoint point)
Deselect(index); Deselect(index);
else else
Select(index, true); Select(index, true);
} else { } else
Select(index); Select(index);
}
} }
} else { } else {
// toggle selection state of clicked item // toggle selection state of clicked item
@@ -544,15 +553,13 @@ BListView::MouseDown(BPoint point)
else else
Select(index); Select(index);
} }
} else { } else if ((modifiers & B_COMMAND_KEY) == 0)
if (!(modifiers & B_COMMAND_KEY)) DeselectAll();
DeselectAll();
}
} }
void void
BListView::MouseUp(BPoint pt) BListView::MouseUp(BPoint where)
{ {
fTrack->try_drag = false; fTrack->try_drag = false;
} }
@@ -596,7 +603,7 @@ BListView::ResizeToPreferred()
void void
BListView::GetPreferredSize(float* _width, float* _height) BListView::GetPreferredSize(float *_width, float *_height)
{ {
int32 count = CountItems(); int32 count = CountItems();
@@ -612,9 +619,8 @@ BListView::GetPreferredSize(float* _width, float* _height)
*_width = maxWidth; *_width = maxWidth;
if (_height != NULL) if (_height != NULL)
*_height = ItemAt(count - 1)->Bottom(); *_height = ItemAt(count - 1)->Bottom();
} else { } else
BView::GetPreferredSize(_width, _height); BView::GetPreferredSize(_width, _height);
}
} }
@@ -774,7 +780,7 @@ BListView::AddList(BList* list)
BListItem* BListItem*
BListView::RemoveItem(int32 index) BListView::RemoveItem(int32 index)
{ {
BListItem *item = ItemAt(index); BListItem* item = ItemAt(index);
if (!item) if (!item)
return NULL; return NULL;
@@ -803,7 +809,7 @@ BListView::RemoveItem(int32 index)
bool bool
BListView::RemoveItem(BListItem *item) BListView::RemoveItem(BListItem* item)
{ {
return BListView::RemoveItem(IndexOf(item)) != NULL; return BListView::RemoveItem(IndexOf(item)) != NULL;
} }
@@ -824,6 +830,7 @@ BListView::RemoveItems(int32 index, int32 count)
fList.RemoveItems(index, count); fList.RemoveItems(index, count);
if (index < fList.CountItems()) if (index < fList.CountItems())
_RecalcItemTops(index); _RecalcItemTops(index);
Invalidate(); Invalidate();
return true; return true;
} }
@@ -879,8 +886,9 @@ void
BListView::SetListType(list_view_type type) BListView::SetListType(list_view_type type)
{ {
if (fListType == B_MULTIPLE_SELECTION_LIST && if (fListType == B_MULTIPLE_SELECTION_LIST &&
type == B_SINGLE_SELECTION_LIST) type == B_SINGLE_SELECTION_LIST) {
Select(CurrentSelection(0)); Select(CurrentSelection(0));
}
fListType = type; fListType = type;
} }
@@ -908,6 +916,7 @@ BListView::IndexOf(BListItem *item) const
int32 index = IndexOf(BPoint(0.0, item->Top())); int32 index = IndexOf(BPoint(0.0, item->Top()));
if (index >= 0 && fList.ItemAt(index) == item) if (index >= 0 && fList.ItemAt(index) == item)
return index; return index;
return -1; return -1;
} }
} }
@@ -923,6 +932,7 @@ BListView::IndexOf(BPoint point) const
int32 mid = -1; int32 mid = -1;
float frameTop = -1.0; float frameTop = -1.0;
float frameBottom = 1.0; float frameBottom = 1.0;
// binary search the list // binary search the list
while (high >= low) { while (high >= low) {
mid = (low + high) / 2; mid = (low + high) / 2;
@@ -1057,8 +1067,8 @@ BListView::Select(int32 start, int32 finish, bool extend)
bool bool
BListView::IsItemSelected(int32 index) const BListView::IsItemSelected(int32 index) const
{ {
BListItem *item = ItemAt(index); BListItem* item = ItemAt(index);
if (item) if (item != NULL)
return item->IsSelected(); return item->IsSelected();
return false; return false;
@@ -1211,7 +1221,7 @@ BListView::MoveItem(int32 from, int32 to)
bool bool
BListView::ReplaceItem(int32 index, BListItem *item) BListView::ReplaceItem(int32 index, BListItem* item)
{ {
MiscData data; MiscData data;
@@ -1504,7 +1514,8 @@ BListView::_Select(int32 index, bool extend)
} }
/*! Selects the items between \a from and \a to, and returns \c true in /*!
Selects the items between \a from and \a to, and returns \c true in
case the selection was changed because of this method. case the selection was changed because of this method.
If \a extend is \c false, all previously selected items are deselected. If \a extend is \c false, all previously selected items are deselected.
*/ */
@@ -1692,8 +1703,10 @@ BListView::_SwapItems(int32 a, int32 b)
int32 first = min_c(a, b); int32 first = min_c(a, b);
int32 last = max_c(a, b); int32 last = max_c(a, b);
if (ItemAt(a)->IsSelected() != ItemAt(b)->IsSelected()) { if (ItemAt(a)->IsSelected() != ItemAt(b)->IsSelected()) {
if (first < fFirstSelected || last > fLastSelected) if (first < fFirstSelected || last > fLastSelected) {
_RescanSelection(min_c(first, fFirstSelected), max_c(last, fLastSelected)); _RescanSelection(min_c(first, fFirstSelected),
max_c(last, fLastSelected));
}
// though the actually selected items stayed the // though the actually selected items stayed the
// same, the selection has still changed // same, the selection has still changed
SelectionChanged(); SelectionChanged();
@@ -1754,9 +1767,9 @@ BListView::_MoveItem(int32 from, int32 to)
bool bool
BListView::_ReplaceItem(int32 index, BListItem *item) BListView::_ReplaceItem(int32 index, BListItem* item)
{ {
if (!item) if (item == NULL)
return false; return false;
BListItem* old = ItemAt(index); BListItem* old = ItemAt(index);
@@ -1853,4 +1866,3 @@ BListView::_RecalcItemTops(int32 start, int32 end)
top += ceilf(item->Height()); top += ceilf(item->Height());
} }
} }