xref: /haiku/src/preferences/datatranslations/TranslatorListView.cpp (revision dfc8a217db488098641462dfc334dcc0f7d62456)
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 <Application.h>
15 
16 #include <string.h>
17 
18 
19 static int
20 compare_items(const void* a, const void* b)
21 {
22 	const BStringItem* stringA = *(const BStringItem**)a;
23 	const BStringItem* stringB = *(const BStringItem**)b;
24 
25 	return strcmp(stringA->Text(), stringB->Text());
26 }
27 
28 
29 //	#pragma mark -
30 
31 
32 TranslatorItem::TranslatorItem(translator_id id, const char* name)
33 	:
34 	BStringItem(name),
35 	fID(id)
36 {
37 }
38 
39 
40 TranslatorItem::~TranslatorItem()
41 {
42 }
43 
44 
45 //	#pragma mark -
46 
47 
48 TranslatorListView::TranslatorListView(const char *name, list_view_type type)
49 	:
50 	BListView(name, B_SINGLE_SELECTION_LIST)
51 {
52 }
53 
54 
55 TranslatorListView::~TranslatorListView()
56 {
57 }
58 
59 
60 TranslatorItem*
61 TranslatorListView::TranslatorAt(int32 index) const
62 {
63 	return dynamic_cast<TranslatorItem*>(ItemAt(index));
64 }
65 
66 
67 void
68 TranslatorListView::MessageReceived(BMessage *message)
69 {
70 	uint32 type;
71 	int32 count;
72 
73 	switch (message->what) {
74 		case B_SIMPLE_DATA:
75 			// Tell the application object that a
76 			// file has been dropped on this view
77 			message->GetInfo("refs", &type, &count);
78 			if (count > 0 && type == B_REF_TYPE) {
79 				message->what = B_REFS_RECEIVED;
80 				be_app->PostMessage(message);
81 				Invalidate();
82 			}
83 			break;
84 
85 		default:
86 			BListView::MessageReceived(message);
87 			break;
88 	}
89 }
90 
91 
92 void
93 TranslatorListView::MouseMoved(BPoint point, uint32 transit, const BMessage *dragMessage)
94 {
95 	if (dragMessage != NULL && transit == B_ENTERED_VIEW) {
96 		// Draw a red box around the inside of the view
97 		// to tell the user that this view accepts drops
98 		SetHighColor(220,0,0);
99 	 	SetPenSize(4);
100 		StrokeRect(Bounds());
101 	 	SetHighColor(0,0,0);
102 	} else if (dragMessage != NULL && transit == B_EXITED_VIEW)
103 		Invalidate();
104 }
105 
106 
107 void
108 TranslatorListView::SortItems()
109 {
110 	BListView::SortItems(&compare_items);
111 }
112 
113