xref: /haiku/src/preferences/datatranslations/TranslatorListView.cpp (revision 4c07199d8201fcf267e90be0d24b76799d03cea6)
1 /*
2  * Copyright 2002-2006, Haiku, Inc.
3  * Distributed under the terms of the MIT license.
4  *
5  * Authors:
6  *		Oliver Siebenmarck
7  *		Andrew McCall, mccall@digitalparadise.co.uk
8  *		Michael Wilber
9  */
10 
11 
12 #include "TranslatorListView.h"
13 
14 #include <string.h>
15 
16 #include <Application.h>
17 #include <String.h>
18 #include <TranslatorRoster.h>
19 
20 
21 static int
22 compare_items(const void* a, const void* b)
23 {
24 	const TranslatorItem* itemA = *(const TranslatorItem**)a;
25 	const TranslatorItem* itemB = *(const TranslatorItem**)b;
26 
27 	// Compare by supertype, then by name
28 	int typeDiff = itemA->Supertype().Compare(itemB->Supertype());
29 	if (typeDiff != 0)
30 		return typeDiff;
31 
32 	return strcmp(itemA->Text(), itemB->Text());
33 }
34 
35 
36 //	#pragma mark -
37 
38 
39 TranslatorItem::TranslatorItem(translator_id id, const char* name)
40 	:
41 	BStringItem(name),
42 	fID(id)
43 {
44 	static BTranslatorRoster* roster = BTranslatorRoster::Default();
45 
46 	const translation_format* format;
47 	int32 count;
48 	roster->GetOutputFormats(id, &format, &count);
49 
50 	// Find a supertype to categorize the item in ("application" is too generic,
51 	// so exclude it unless it's the only one available)
52 	do {
53 		fSupertype = format->MIME;
54 		int32 slash = fSupertype.FindFirst('/');
55 		fSupertype.Truncate(slash);
56 	} while (fSupertype == "application" && --count != 0);
57 }
58 
59 
60 TranslatorItem::~TranslatorItem()
61 {
62 }
63 
64 
65 //	#pragma mark -
66 
67 
68 TranslatorListView::TranslatorListView(const char* name, list_view_type type)
69 	:
70 	BListView(name, B_SINGLE_SELECTION_LIST)
71 {
72 }
73 
74 
75 TranslatorListView::~TranslatorListView()
76 {
77 }
78 
79 
80 TranslatorItem*
81 TranslatorListView::TranslatorAt(int32 index) const
82 {
83 	return dynamic_cast<TranslatorItem*>(ItemAt(index));
84 }
85 
86 
87 void
88 TranslatorListView::MessageReceived(BMessage* message)
89 {
90 	uint32 type;
91 	int32 count;
92 
93 	switch (message->what) {
94 		case B_SIMPLE_DATA:
95 			// Tell the application object that a
96 			// file has been dropped on this view
97 			message->GetInfo("refs", &type, &count);
98 			if (count > 0 && type == B_REF_TYPE) {
99 				message->what = B_REFS_RECEIVED;
100 				be_app->PostMessage(message);
101 				Invalidate();
102 			}
103 			break;
104 
105 		default:
106 			BListView::MessageReceived(message);
107 			break;
108 	}
109 }
110 
111 
112 void
113 TranslatorListView::MouseMoved(BPoint point, uint32 transit,
114 	const BMessage* dragMessage)
115 {
116 	if (dragMessage != NULL && transit == B_ENTERED_VIEW) {
117 		// Draw a red box around the inside of the view
118 		// to tell the user that this view accepts drops
119 		SetHighColor(220, 0, 0);
120 		SetPenSize(4);
121 		StrokeRect(Bounds());
122 		SetHighColor(0, 0, 0);
123 	} else if (dragMessage != NULL && transit == B_EXITED_VIEW)
124 		Invalidate();
125 
126 	BListView::MouseMoved(point, transit, dragMessage);
127 }
128 
129 
130 void
131 TranslatorListView::SortItems()
132 {
133 	BListView::SortItems(&compare_items);
134 }
135 
136