btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / apps / switcher / GroupListView.cpp
blob5aa26533c96dee767ab4b32ecd209402e502c38d
1 /*
2 * Copyright 2011, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "GroupListView.h"
9 #include <map>
11 #include <Window.h>
14 class RendererLayoutItem : public BAbstractLayout {
15 public:
16 RendererLayoutItem(BView* owner, int32 index, void* item,
17 ListItemRenderer*& renderer)
19 fOwner(owner),
20 fIndex(index),
21 fItem(item),
22 fRenderer(renderer)
26 ListItemRenderer* Renderer() const
28 fRenderer->SetTo(fOwner, fItem);
29 return fRenderer;
32 int32 Index() const
34 return fIndex;
37 void* Item() const
39 return fItem;
42 virtual BSize BaseMinSize()
44 fRenderer->SetTo(fOwner, fItem);
45 return fRenderer->MinSize();
48 virtual BSize BasePreferredSize()
50 fRenderer->SetTo(fOwner, fItem);
51 return fRenderer->PreferredSize();
54 protected:
55 virtual void DoLayout()
57 // shouldn't ever be called?
60 private:
61 BView* fOwner;
62 int32 fIndex;
63 void* fItem;
64 ListItemRenderer*& fRenderer;
68 GroupListView::GroupListView(const char* name, GroupListModel* model,
69 enum orientation orientation, float spacing)
71 BView(NULL, B_WILL_DRAW, new BGroupLayout(orientation, spacing)),
72 fModel(NULL),
73 fItemRenderer(NULL),
74 fGroupRenderer(NULL),
75 fSelectionMessage(NULL)
77 SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
78 SetModel(model);
82 GroupListView::~GroupListView()
84 delete fModel;
85 delete fItemRenderer;
86 delete fGroupRenderer;
87 delete fSelectionMessage;
91 void
92 GroupListView::SetModel(GroupListModel* model)
94 // TODO: remove all previous
95 // TODO: add change mechanism
96 // TODO: use a "virtual" BGroupLayout (ie. one that create its layout items
97 // on the fly).
98 fModel = model;
100 std::map<addr_t, BGroupLayout*> groupMap;
102 int32 groupCount = model->CountGroups();
103 for (int groupIndex = 0; groupIndex < groupCount; groupIndex++) {
104 BGroupLayout* groupItem = new BGroupLayout(B_VERTICAL, 0);
105 groupItem->SetVisible(false);
106 AddChild(groupItem);
108 void* group = model->GroupAt(groupIndex);
109 groupMap[(addr_t)group] = groupItem;
110 groupItem->AddItem(new RendererLayoutItem(this, groupIndex, group,
111 fGroupRenderer));
114 int32 itemCount = model->CountItems();
115 for (int itemIndex = 0; itemIndex < itemCount; itemIndex++) {
116 void* group = model->GroupForItemAt(itemIndex);
117 if (group == NULL)
118 continue;
120 BGroupLayout* groupItem = groupMap[(addr_t)group];
121 if (groupItem == NULL)
122 continue;
124 groupItem->SetVisible(true);
126 RendererLayoutItem* rendererItem = new RendererLayoutItem(this,
127 itemIndex, model->ItemAt(itemIndex), fItemRenderer);
128 groupItem->AddItem(rendererItem);
133 void
134 GroupListView::SetItemRenderer(ListItemRenderer* renderer)
136 fItemRenderer = renderer;
137 InvalidateLayout();
141 void
142 GroupListView::SetGroupRenderer(ListItemRenderer* renderer)
144 fGroupRenderer = renderer;
145 InvalidateLayout();
149 void
150 GroupListView::SetSelectionMessage(BMessage* message, BMessenger target)
152 fSelectionMessage = message;
153 fSelectionTarget = target;
157 void
158 GroupListView::AttachedToWindow()
163 void
164 GroupListView::MessageReceived(BMessage* message)
166 switch (message->what) {
167 default:
168 BView::MessageReceived(message);
169 break;
174 void
175 GroupListView::MouseDown(BPoint point)
177 if (fSelectionMessage == NULL)
178 return;
180 BLayoutItem* item = _ItemAt(GetLayout(), point);
182 if (RendererLayoutItem* rendererItem
183 = dynamic_cast<RendererLayoutItem*>(item)) {
184 BMessage message(*fSelectionMessage);
186 int32 buttons = 0;
187 if (Window()->CurrentMessage() != NULL)
188 buttons = Window()->CurrentMessage()->FindInt32("buttons");
189 if (buttons != 0)
190 message.AddInt32("buttons", buttons);
191 message.AddInt32("index", rendererItem->Index());
192 message.AddPointer(rendererItem->Renderer() == fGroupRenderer
193 ? "group" : "item", rendererItem->Item());
195 fSelectionTarget.SendMessage(&message);
200 void
201 GroupListView::Draw(BRect updateRect)
203 _Draw(GetLayout(), updateRect);
207 void
208 GroupListView::_Draw(BLayoutItem* item, BRect updateRect)
210 if (RendererLayoutItem* rendererItem
211 = dynamic_cast<RendererLayoutItem*>(item)) {
212 ListItemRenderer* renderer = rendererItem->Renderer();
213 renderer->Draw(this, rendererItem->Frame(), rendererItem->Index(),
214 false);
215 } else if (BLayout* layout = dynamic_cast<BLayout*>(item)) {
216 for (int i = 0; i < layout->CountItems(); i++) {
217 item = layout->ItemAt(i);
218 if (!item->IsVisible() || !item->Frame().Intersects(updateRect))
219 continue;
221 _Draw(item, updateRect);
227 BLayoutItem*
228 GroupListView::_ItemAt(BLayoutItem* item, BPoint point)
230 if (RendererLayoutItem* rendererItem
231 = dynamic_cast<RendererLayoutItem*>(item))
232 return rendererItem;
234 if (BLayout* layout = dynamic_cast<BLayout*>(item)) {
235 for (int i = 0; i < layout->CountItems(); i++) {
236 item = layout->ItemAt(i);
237 if (!item->IsVisible() || !item->Frame().Contains(point))
238 continue;
240 return _ItemAt(item, point);
244 return item;