xref: /haiku/src/preferences/media/MediaListItem.cpp (revision a381c8a06378de22ff08adf4282b4e3f7e50d250)
1 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2 //
3 //	Copyright (c) 2003, OpenBeOS
4 //
5 //  This software is part of the OpenBeOS distribution and is covered
6 //  by the OpenBeOS license.
7 //
8 //
9 //  File:        MediaListItem.cpp
10 //  Author:      Sikosis, Jérôme Duval
11 //  Description: Media Preferences
12 //  Created :    June 25, 2003
13 //
14 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
15 
16 
17 // Includes -------------------------------------------------------------------------------------------------- //
18 #include <string.h>
19 
20 #include <View.h>
21 
22 #include "MediaListItem.h"
23 
24 #define kITEM_MARGIN					  1
25 
26 // MediaListItem - Constructor
27 MediaListItem::MediaListItem(dormant_node_info *info, uint32 level, bool isVideo, BList *icons, uint32 modifiers)
28 	: BListItem(level),
29 	fIsAudioMixer(false),
30 	fIsVideo(isVideo),
31 	fIsDefaultInput(false),
32 	fIsDefaultOutput(false)
33 {
34 	fIcons = icons;
35 	fInfo = info;
36 	fLabel = fInfo->name;
37 
38 	SetHeight(16 + kITEM_MARGIN);
39 }
40 
41 MediaListItem::MediaListItem(const char *label, uint32 level, bool isVideo, BList *icons, uint32 modifiers)
42 	: BListItem(level),
43 	fLabel(label),
44 	fIsAudioMixer(false),
45 	fIsVideo(isVideo),
46 	fIsDefaultInput(false),
47 	fIsDefaultOutput(false)
48 {
49 	fIcons = icons;
50 	fInfo = NULL;
51 
52 	SetHeight(16 + kITEM_MARGIN);
53 }
54 
55 MediaListItem::~MediaListItem()
56 {
57 }
58 //--------------------------------------------------------------------------------------------------------------//
59 
60 
61 //MediaListItem - DrawItem
62 void
63 MediaListItem::DrawItem(BView *owner, BRect frame, bool complete)
64 {
65 	rgb_color kHighlight = { 140,140,140,0 };
66 	rgb_color kBlack = { 0,0,0,0 };
67 
68 	BRect r(frame);
69 
70 	if (IsSelected() || complete) {
71 		rgb_color color;
72 		if (IsSelected()) {
73 			color = kHighlight;
74 		} else {
75 			color = owner->ViewColor();
76 		}
77 		owner->SetHighColor(color);
78 		owner->SetLowColor(color);
79 		owner->FillRect(r);
80 		owner->SetHighColor(kBlack);
81 	} else {
82 		owner->SetLowColor(owner->ViewColor());
83 	}
84 
85 	frame.left += 4;
86 	BRect iconFrame(frame);
87 	iconFrame.Set(iconFrame.left, iconFrame.top+1, iconFrame.left+15, iconFrame.top+16);
88 	uint32 index = 0;
89 	if (OutlineLevel()==0 || (fIsDefaultInput && fIsDefaultOutput)) {
90 		if (fIsDefaultInput && fIsVideo)
91 			index = 4;
92 		else if (fIsDefaultInput && !fIsVideo)
93 			index = 2;
94 		owner->SetDrawingMode(B_OP_OVER);
95 		owner->DrawBitmap(static_cast<BBitmap*>(fIcons->ItemAt(index)), iconFrame);
96 		owner->SetDrawingMode(B_OP_COPY);
97 	}
98 	iconFrame.OffsetBy(16, 0);
99 	if (fIsDefaultInput || fIsDefaultOutput || fIsAudioMixer) {
100 		if (fIsAudioMixer)
101 			index = 1;
102 		else if (fIsDefaultOutput) {
103 			if (fIsVideo)
104 				index = 5;
105 			else
106 				index = 3;
107 		} else {
108 			if (fIsVideo)
109 				index = 4;
110 			else
111 				index = 2;
112 		}
113 		owner->SetDrawingMode(B_OP_OVER);
114 		owner->DrawBitmap(static_cast<BBitmap*>(fIcons->ItemAt(index)), iconFrame);
115 		owner->SetDrawingMode(B_OP_COPY);
116 	}
117 
118 	frame.left += 16 * (OutlineLevel() + 1);
119 	owner->SetHighColor(kBlack);
120 
121 	BFont		font = be_plain_font;
122 	font_height	finfo;
123 	font.GetHeight(&finfo);
124 	owner->SetFont(&font);
125 	owner->MovePenTo(frame.left+8, frame.top + ((frame.Height() - (finfo.ascent + finfo.descent + finfo.leading)) / 2) +
126 					(finfo.ascent + finfo.descent) - 1);
127 	owner->DrawString(fLabel);
128 }
129 //--------------------------------------------------------------------------------------------------------------//
130 
131 void
132 MediaListItem::SetDefault(bool isDefault, bool isInput)
133 {
134 	if (isInput)
135 		fIsDefaultInput = isDefault;
136 	else
137 		fIsDefaultOutput = isDefault;
138 }
139 
140 void
141 MediaListItem::SetAudioMixer(bool isAudioMixer)
142 {
143 	fIsAudioMixer = isAudioMixer;
144 }
145 
146 void
147 MediaListItem::Update(BView *owner, const BFont *finfo)
148 {
149 	// we need to override the update method so we can make sure are
150 	// list item size doesn't change
151 	BListItem::Update(owner, finfo);
152 	if ((Height() < 16 + kITEM_MARGIN)) {
153 		SetHeight(16 + kITEM_MARGIN);
154 	}
155 }
156 
157 int
158 MediaListItem::Compare(const void *firstArg, const void *secondArg)
159 {
160 	const MediaListItem *item1 = *static_cast<const MediaListItem * const *>(firstArg);
161 	const MediaListItem *item2 = *static_cast<const MediaListItem * const *>(secondArg);
162 	if (item1->fIsVideo != item2->fIsVideo)
163 		return item1->fIsVideo ? 1 : -1;
164 	if (item1->OutlineLevel()!=item2->OutlineLevel())
165 		return item1->OutlineLevel()>item2->OutlineLevel() ? 1 : -1;
166 	if (item1->fIsAudioMixer!=item2->fIsAudioMixer)
167 		return item2->fIsAudioMixer ? 1 : -1;
168 	return strcmp(item1->fLabel, item2->fLabel);
169 }
170