1 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
3 // Copyright (c) 2003, OpenBeOS
5 // This software is part of the OpenBeOS distribution and is covered
6 // by the OpenBeOS license.
9 // File: MediaListItem.cpp
10 // Author: Sikosis, Jérôme Duval
11 // Description: Media Preferences
12 // Created : June 25, 2003
14 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
15 #include "MediaListItem.h"
19 #include <MediaAddOn.h>
22 #include "MediaIcons.h"
23 #include "MediaWindow.h"
24 #include "MidiSettingsView.h"
27 #define kITEM_MARGIN 1
28 #define GREATER_THAN -1
32 MediaIcons
* MediaListItem::sIcons
= NULL
;
35 struct MediaListItem::Renderer
{
46 // The first icon added is drawn next to the label,
47 // the second is drawn to the right of the label.
48 void AddIcon(BBitmap
* icon
)
53 fSecondaryIcon
= fPrimaryIcon
;
58 void SetTitle(const char* title
)
63 void SetSelected(bool selected
)
68 // set whether or not to leave enough room for two icons,
70 void UseDoubleInset(bool doubleInset
)
72 fDoubleInsets
= doubleInset
;
75 void Render(BView
* onto
, BRect frame
, bool complete
= false)
77 const rgb_color lowColor
= onto
->LowColor();
78 const rgb_color highColor
= onto
->HighColor();
80 if (fSelected
|| complete
) {
82 onto
->SetLowColor(ui_color(B_LIST_SELECTED_BACKGROUND_COLOR
));
83 onto
->FillRect(frame
, B_SOLID_LOW
);
86 BPoint
point(frame
.left
+ 4.0f
,
87 frame
.top
+ (frame
.Height() - MediaIcons::sBounds
.Height()) / 2.0f
);
89 BRect
iconFrame(MediaIcons::IconRectAt(point
+ BPoint(1, 0)));
91 onto
->SetDrawingMode(B_OP_OVER
);
92 if (fPrimaryIcon
&& !fDoubleInsets
) {
93 onto
->DrawBitmap(fPrimaryIcon
, iconFrame
);
94 point
.x
= iconFrame
.right
+ 1;
95 } else if (fSecondaryIcon
) {
96 onto
->DrawBitmap(fSecondaryIcon
, iconFrame
);
99 iconFrame
= MediaIcons::IconRectAt(iconFrame
.RightTop() + BPoint(1, 0));
101 if (fDoubleInsets
&& fPrimaryIcon
) {
102 onto
->DrawBitmap(fPrimaryIcon
, iconFrame
);
103 point
.x
= iconFrame
.right
+ 1;
106 onto
->SetDrawingMode(B_OP_COPY
);
108 BFont font
= be_plain_font
;
109 font_height fontInfo
;
110 font
.GetHeight(&fontInfo
);
112 onto
->SetFont(&font
);
113 onto
->MovePenTo(point
.x
+ 8, frame
.top
114 + fontInfo
.ascent
+ (frame
.Height()
115 - ceilf(fontInfo
.ascent
+ fontInfo
.descent
)) / 2.0f
);
116 onto
->DrawString(fTitle
);
118 onto
->SetHighColor(highColor
);
119 onto
->SetLowColor(lowColor
);
127 float iconSpace
= MediaIcons::sBounds
.Width() + 1.0f
;
132 // space between icons and text
134 width
+= be_plain_font
->StringWidth(fTitle
) + 16.0f
;
141 BBitmap
* fPrimaryIcon
;
142 BBitmap
* fSecondaryIcon
;
148 MediaListItem::MediaListItem()
156 MediaListItem::Update(BView
* owner
, const BFont
* font
)
158 // we need to override the update method so we can make sure our
159 // list item size doesn't change
160 BListItem::Update(owner
, font
);
162 float iconHeight
= MediaIcons::sBounds
.Height() + 1;
163 if ((Height() < iconHeight
+ kITEM_MARGIN
* 2)) {
164 SetHeight(iconHeight
+ kITEM_MARGIN
* 2);
168 renderer
.SetTitle(Label());
169 SetRenderParameters(renderer
);
170 SetWidth(renderer
.ItemWidth());
175 MediaListItem::DrawItem(BView
* owner
, BRect frame
, bool complete
)
178 renderer
.SetSelected(IsSelected());
179 renderer
.SetTitle(Label());
180 SetRenderParameters(renderer
);
181 renderer
.Render(owner
, frame
, complete
);
186 MediaListItem::Compare(const void* itemOne
, const void* itemTwo
)
188 MediaListItem
* firstItem
= *(MediaListItem
**)itemOne
;
189 MediaListItem
* secondItem
= *(MediaListItem
**)itemTwo
;
191 return firstItem
->CompareWith(secondItem
);
195 // #pragma mark - NodeListItem
198 NodeListItem::NodeListItem(const dormant_node_info
* node
, media_type type
)
203 fIsDefaultInput(false),
204 fIsDefaultOutput(false)
210 NodeListItem::SetRenderParameters(MediaListItem::Renderer
& renderer
)
212 MediaIcons::IconSet
* iconSet
= &Icons()->videoIcons
;
213 if (fMediaType
== MediaListItem::AUDIO_TYPE
)
214 iconSet
= &Icons()->audioIcons
;
217 renderer
.AddIcon(&iconSet
->inputIcon
);
218 if (fIsDefaultOutput
)
219 renderer
.AddIcon(&iconSet
->outputIcon
);
224 NodeListItem::Label()
226 return fNodeInfo
->name
;
231 NodeListItem::SetMediaType(media_type type
)
238 NodeListItem::SetDefaultOutput(bool isDefault
)
240 fIsDefaultOutput
= isDefault
;
245 NodeListItem::SetDefaultInput(bool isDefault
)
247 fIsDefaultInput
= isDefault
;
252 NodeListItem::AlterWindow(MediaWindow
* window
)
254 window
->SelectNode(fNodeInfo
);
259 NodeListItem::Accept(MediaListItem::Visitor
& visitor
)
266 NodeListItem::CompareWith(MediaListItem
* item
)
268 Comparator
comparator(this);
269 item
->Accept(comparator
);
270 return comparator
.result
;
274 NodeListItem::Comparator::Comparator(NodeListItem
* compareOthersTo
)
276 result(GREATER_THAN
),
277 fTarget(compareOthersTo
)
283 NodeListItem::Comparator::Visit(NodeListItem
* item
)
285 result
= GREATER_THAN
;
287 if (fTarget
->Type() != item
->Type() && fTarget
->Type() == VIDEO_TYPE
)
290 result
= strcmp(fTarget
->Label(), item
->Label());
295 NodeListItem::Comparator::Visit(DeviceListItem
* item
)
298 if (fTarget
->Type() != item
->Type() && fTarget
->Type() == AUDIO_TYPE
)
299 result
= GREATER_THAN
;
304 NodeListItem::Comparator::Visit(AudioMixerListItem
* item
)
311 NodeListItem::Comparator::Visit(MidiListItem
* item
)
313 result
= GREATER_THAN
;
317 // #pragma mark - DeviceListItem
320 DeviceListItem::DeviceListItem(const char* title
,
321 MediaListItem::media_type type
)
331 DeviceListItem::Accept(MediaListItem::Visitor
& visitor
)
338 DeviceListItem::CompareWith(MediaListItem
* item
)
340 Comparator
comparator(this);
341 item
->Accept(comparator
);
342 return comparator
.result
;
346 DeviceListItem::Comparator::Comparator(DeviceListItem
* compareOthersTo
)
348 result(GREATER_THAN
),
349 fTarget(compareOthersTo
)
355 DeviceListItem::Comparator::Visit(NodeListItem
* item
)
357 result
= GREATER_THAN
;
358 if (fTarget
->Type() != item
->Type() && fTarget
->Type() == AUDIO_TYPE
)
364 DeviceListItem::Comparator::Visit(DeviceListItem
* item
)
367 if (fTarget
->Type() == AUDIO_TYPE
)
368 result
= GREATER_THAN
;
373 DeviceListItem::Comparator::Visit(AudioMixerListItem
* item
)
376 if (fTarget
->Type() == AUDIO_TYPE
)
377 result
= GREATER_THAN
;
382 DeviceListItem::Comparator::Visit(MidiListItem
* item
)
389 DeviceListItem::SetRenderParameters(Renderer
& renderer
)
391 renderer
.AddIcon(&Icons()->devicesIcon
);
392 renderer
.UseDoubleInset(false);
397 DeviceListItem::AlterWindow(MediaWindow
* window
)
399 if (fMediaType
== MediaListItem::AUDIO_TYPE
)
400 window
->SelectAudioSettings(fTitle
);
402 window
->SelectVideoSettings(fTitle
);
406 // #pragma mark - AudioMixerListItem
409 AudioMixerListItem::AudioMixerListItem(const char* title
)
418 AudioMixerListItem::AlterWindow(MediaWindow
* window
)
420 window
->SelectAudioMixer(fTitle
);
425 AudioMixerListItem::Accept(MediaListItem::Visitor
& visitor
)
432 AudioMixerListItem::CompareWith(MediaListItem
* item
)
434 Comparator
comparator(this);
435 item
->Accept(comparator
);
436 return comparator
.result
;
440 AudioMixerListItem::Comparator::Comparator(AudioMixerListItem
* compareOthersTo
)
443 fTarget(compareOthersTo
)
449 AudioMixerListItem::Comparator::Visit(NodeListItem
* item
)
451 result
= GREATER_THAN
;
456 AudioMixerListItem::Comparator::Visit(DeviceListItem
* item
)
458 result
= GREATER_THAN
;
459 if (item
->Type() == AUDIO_TYPE
)
465 AudioMixerListItem::Comparator::Visit(AudioMixerListItem
* item
)
472 AudioMixerListItem::Comparator::Visit(MidiListItem
* item
)
474 result
= GREATER_THAN
;
479 AudioMixerListItem::SetRenderParameters(Renderer
& renderer
)
481 renderer
.AddIcon(&Icons()->mixerIcon
);
485 // #pragma mark - MidiListItem
487 MidiListItem::MidiListItem(const char* title
)
496 MidiListItem::AlterWindow(MediaWindow
* window
)
498 window
->SelectMidiSettings(fTitle
);
503 MidiListItem::Label()
510 MidiListItem::Accept(MediaListItem::Visitor
& visitor
)
517 MidiListItem::CompareWith(MediaListItem
* item
)
519 Comparator
comparator(this);
520 item
->Accept(comparator
);
521 return comparator
.result
;
525 MidiListItem::Comparator::Comparator(MidiListItem
* compareOthersTo
)
528 fTarget(compareOthersTo
)
534 MidiListItem::Comparator::Visit(NodeListItem
* item
)
536 result
= GREATER_THAN
;
541 MidiListItem::Comparator::Visit(DeviceListItem
* item
)
543 result
= GREATER_THAN
;
544 if (item
->Type() == AUDIO_TYPE
)
550 MidiListItem::Comparator::Visit(AudioMixerListItem
* item
)
557 MidiListItem::Comparator::Visit(MidiListItem
* item
)
564 MidiListItem::SetRenderParameters(Renderer
& renderer
)
566 // TODO: Create a nice icon
567 renderer
.AddIcon(&Icons()->mixerIcon
);