xref: /haiku/src/apps/text_search/GrepListView.cpp (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
1 /*
2  * Copyright (c) 1998-2007 Matthijs Hollemans
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "GrepListView.h"
24 
25 #include <Path.h>
26 
27 ResultItem::ResultItem(const entry_ref& ref)
28 	: BStringItem("", 0, false),
29 	  ref(ref)
30 {
31 	BEntry entry(&ref);
32 	BPath path(&entry);
33 	SetText(path.Path());
34 }
35 
36 
37 GrepListView::GrepListView()
38 	: BOutlineListView(BRect(0, 0, 40, 80), "SearchResults",
39 		B_MULTIPLE_SELECTION_LIST, B_FOLLOW_ALL_SIDES,
40 		B_WILL_DRAW | B_NAVIGABLE)
41 {
42 }
43 
44 
45 ResultItem*
46 GrepListView::FindItem(const entry_ref& ref, int32* _index) const
47 {
48 	int32 count = FullListCountItems();
49 	for (int32 i = 0; i < count; i++) {
50 		ResultItem* item = dynamic_cast<ResultItem*>(FullListItemAt(i));
51 		if (item == NULL)
52 			continue;
53 		if (item->ref == ref) {
54 			*_index = i;
55 			return item;
56 		}
57 	}
58 	*_index = -1;
59 	return NULL;
60 }
61 
62 
63 ResultItem*
64 GrepListView::RemoveResults(const entry_ref& ref, bool completeItem)
65 {
66 	int32 index;
67 	ResultItem* item = FindItem(ref, &index);
68 	if (item == NULL)
69 		return NULL;
70 
71 	// remove all the sub items
72 	while (true) {
73 		BListItem* subItem = FullListItemAt(index + 1);
74 		if (subItem && subItem->OutlineLevel() > 0)
75 			delete RemoveItem(index + 1);
76 		else
77 			break;
78 	}
79 
80 	if (completeItem) {
81 		// remove file item itself
82 		delete RemoveItem(index);
83 		item = NULL;
84 	}
85 
86 	return item;
87 }
88 
89