1/*
2 * Copyright 2011, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Clemens Zeidler <haiku@clemens-zeidler.de>
7 */
8#ifndef MUSIC_FILE_LIST_VIEW_H
9#define MUSIC_FILE_LIST_VIEW_H
10
11
12#include <Bitmap.h>
13#include <ListItem.h>
14#include <OutlineListView.h>
15#include <Roster.h>
16
17#include "QueryMonitor.h"
18
19
20class FileListItem : public BStringItem {
21public:
22	FileListItem(const char* text, WatchedFile* file = NULL)
23		:
24		BStringItem(text),
25		fFile(file)
26	{
27	}
28
29
30	WatchedFile*
31	File()
32	{
33		return fFile;
34	}
35
36private:
37			WatchedFile*		fFile;
38};
39
40
41class MusicFileListView : public BOutlineListView {
42public:
43
44	MusicFileListView(const char *name)
45		:
46		BOutlineListView(name)
47	{
48	}
49
50
51	bool
52	InitiateDrag(BPoint where, int32 index, bool wasSelected)
53	{
54		int32 itemIndex = IndexOf(where);
55		FileListItem* item = (FileListItem*)ItemAt(itemIndex);
56		if (item == NULL)
57			return false;
58
59		const char* text = item->Text();
60
61		BRect rect(0, 0, 200, 50);
62		BBitmap* bitmap = new BBitmap(rect, B_RGB32, true);
63		BView* bitmapView = new BView(rect, "bitmap", B_FOLLOW_NONE,
64			B_WILL_DRAW);
65
66		bitmap->Lock();
67		bitmap->AddChild(bitmapView);
68
69		bitmapView->SetLowColor(255, 255, 255, 0);	//	transparent
70		bitmapView->SetHighColor(0, 0, 0, 100);
71		bitmapView->SetDrawingMode(B_OP_COPY);
72		bitmapView->FillRect(bitmapView->Bounds(), B_SOLID_LOW);
73
74		bitmapView->SetDrawingMode(B_OP_OVER);
75		font_height height;
76		bitmapView->GetFontHeight(&height);
77		float fontHeight = height.ascent + height.descent;
78		BRect latchRect = LatchRect(BRect(0, 0, item->Width(), item->Height()),
79			item->OutlineLevel());
80		bitmapView->DrawString(text, BPoint(latchRect.Width(), fontHeight));
81
82		bitmapView->Sync();
83		bitmap->Unlock();
84
85		BMessage dragMessage(B_SIMPLE_DATA);
86		dragMessage.AddPoint("click_location", where);
87
88		_RecursiveAddRefs(dragMessage, item);
89
90		BRect itemFrame(ItemFrame(itemIndex));
91		BPoint pt(where.x + itemFrame.left, where.y - itemFrame.top);
92		DragMessage(&dragMessage, bitmap, B_OP_ALPHA, pt, this);
93
94		return true;
95	}
96
97
98	void
99	Launch(BMessage* message)
100	{
101		int32 index;
102		for (int32 i = 0; ; i++) {
103			if (message->FindInt32("index", i, &index) != B_OK)
104				break;
105			FileListItem* item = (FileListItem*)ItemAt(index);
106
107			BMessage refs(B_REFS_RECEIVED);
108			_RecursiveAddRefs(refs, item);
109			be_roster->Launch("application/x-vnd.Haiku-MediaPlayer", &refs);
110		}
111	};
112
113private:
114
115	void
116	_RecursiveAddRefs(BMessage& message, FileListItem* item)
117	{
118		WatchedFile* file = item->File();
119		if (file != NULL) {
120			message.AddRef("refs", &(file->entry));
121		} else {
122			for (int32 i = 0; i < CountItemsUnder(item, true); i++) {
123				_RecursiveAddRefs(message, (FileListItem*)ItemUnderAt(
124					item, true, i));
125			}
126		}
127	}
128};
129
130#endif // MUSIC_FILE_LIST_VIEW_H
131