1/*
2 * Copyright 2009 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT license.
4 *
5 * Authors:
6 *		Pieter Panman
7 */
8
9
10#include "PropertyList.h"
11
12#include <Catalog.h>
13#include <Clipboard.h>
14#include <ColumnTypes.h>
15
16#undef B_TRANSLATION_CONTEXT
17#define B_TRANSLATION_CONTEXT "PropertyList"
18
19
20PropertyRow::PropertyRow(const char* name, const char* value)
21	: BRow(),
22	fName(name), fValue(value)
23{
24	SetField(new BStringField(name), kNameColumn);
25	SetField(new BStringField(value), kValueColumn);
26}
27
28
29PropertyRow::~PropertyRow()
30{
31}
32
33
34void
35PropertyRow::SetName(const char* name)
36{
37	fName = name;
38	SetField(new BStringField(name), kNameColumn);
39}
40
41
42void
43PropertyRow::SetValue(const char* value)
44{
45	fValue = value;
46	SetField(new BStringField(value), kValueColumn);
47}
48
49
50PropertyList::PropertyList(const char* name)
51	: BColumnListView(BRect(0.0, 0.0, 1.0, 1.0), name, B_FOLLOW_ALL, 0,
52		B_NO_BORDER, true)
53{
54	BStringColumn* nameColumn;
55	AddColumn(nameColumn = new BStringColumn(B_TRANSLATE("Name"), 150, 50, 500,
56			B_TRUNCATE_MIDDLE),
57		kNameColumn);
58	AddColumn(new BStringColumn(B_TRANSLATE("Value"), 300, 50, 500,
59		B_TRUNCATE_END), kValueColumn);
60	SetSortColumn(nameColumn, false, true);
61}
62
63
64PropertyList::~PropertyList()
65{
66	RemoveAll();
67}
68
69
70void
71PropertyList::AddAttributes(const Attributes& attributes)
72{
73	RemoveAll();
74	for (unsigned int i = 0; i < attributes.size(); i++) {
75		AddRow(new PropertyRow(attributes[i].fName, attributes[i].fValue));
76	}
77}
78
79
80void
81PropertyList::RemoveAll()
82{
83	BRow *row;
84	while ((row = RowAt((int32)0, NULL))!=NULL) {
85		RemoveRow(row);
86		delete row;
87	}
88}
89
90
91void
92PropertyList::SelectionChanged()
93{
94}
95
96
97void
98PropertyList::MessageReceived(BMessage* msg)
99{
100	switch (msg->what) {
101		case B_COPY:
102		{
103			BString strings;
104			uint32 rowsCount = CountRows();
105
106			for (uint32 i = 0; i < rowsCount; i++) {
107				PropertyRow* current = (PropertyRow*)RowAt(i);
108				if (current->IsSelected())
109					strings << current->Name()
110						<< "\t" << current->Value() << "\n";
111			}
112
113			if (!be_clipboard->Lock())
114				break;
115
116			be_clipboard->Clear();
117			BMessage* data = be_clipboard->Data();
118			if (data != NULL) {
119				data->AddData("text/plain", B_MIME_TYPE,
120						strings, strings.Length());
121				be_clipboard->Commit();
122			}
123
124			be_clipboard->Unlock();
125			break;
126		}
127		default:
128			BColumnListView::MessageReceived(msg);
129			break;
130	}
131}
132