usb_ecm: Use the current configuration instead of a fixed one.
[haiku.git] / src / preferences / media / MediaListItem.cpp
blob5d1b0967d446d61d4b25446775db6268ac5c9978
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
14 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
15 #include "MediaListItem.h"
17 #include <string.h>
19 #include <MediaAddOn.h>
20 #include <View.h>
22 #include "MediaIcons.h"
23 #include "MediaWindow.h"
24 #include "MidiSettingsView.h"
27 #define kITEM_MARGIN 1
28 #define GREATER_THAN -1
29 #define LESS_THAN 1
32 MediaIcons* MediaListItem::sIcons = NULL;
35 struct MediaListItem::Renderer {
36 Renderer()
38 fTitle(NULL),
39 fPrimaryIcon(NULL),
40 fSecondaryIcon(NULL),
41 fDoubleInsets(true),
42 fSelected(false)
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)
50 if (!fPrimaryIcon)
51 fPrimaryIcon = icon;
52 else {
53 fSecondaryIcon = fPrimaryIcon;
54 fPrimaryIcon = icon;
58 void SetTitle(const char* title)
60 fTitle = title;
63 void SetSelected(bool selected)
65 fSelected = selected;
68 // set whether or not to leave enough room for two icons,
69 // defaults to true.
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) {
81 if (fSelected)
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);
122 float ItemWidth()
124 float width = 4.0f;
125 // left margin
127 float iconSpace = MediaIcons::sBounds.Width() + 1.0f;
128 if (fDoubleInsets)
129 iconSpace *= 2.0f;
130 width += iconSpace;
131 width += 8.0f;
132 // space between icons and text
134 width += be_plain_font->StringWidth(fTitle) + 16.0f;
135 return width;
138 private:
140 const char* fTitle;
141 BBitmap* fPrimaryIcon;
142 BBitmap* fSecondaryIcon;
143 bool fDoubleInsets;
144 bool fSelected;
148 MediaListItem::MediaListItem()
150 BListItem((uint32)0)
155 void
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);
167 Renderer renderer;
168 renderer.SetTitle(Label());
169 SetRenderParameters(renderer);
170 SetWidth(renderer.ItemWidth());
174 void
175 MediaListItem::DrawItem(BView* owner, BRect frame, bool complete)
177 Renderer renderer;
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)
200 MediaListItem(),
201 fNodeInfo(node),
202 fMediaType(type),
203 fIsDefaultInput(false),
204 fIsDefaultOutput(false)
209 void
210 NodeListItem::SetRenderParameters(MediaListItem::Renderer& renderer)
212 MediaIcons::IconSet* iconSet = &Icons()->videoIcons;
213 if (fMediaType == MediaListItem::AUDIO_TYPE)
214 iconSet = &Icons()->audioIcons;
216 if (fIsDefaultInput)
217 renderer.AddIcon(&iconSet->inputIcon);
218 if (fIsDefaultOutput)
219 renderer.AddIcon(&iconSet->outputIcon);
223 const char*
224 NodeListItem::Label()
226 return fNodeInfo->name;
230 void
231 NodeListItem::SetMediaType(media_type type)
233 fMediaType = type;
237 void
238 NodeListItem::SetDefaultOutput(bool isDefault)
240 fIsDefaultOutput = isDefault;
244 void
245 NodeListItem::SetDefaultInput(bool isDefault)
247 fIsDefaultInput = isDefault;
251 void
252 NodeListItem::AlterWindow(MediaWindow* window)
254 window->SelectNode(fNodeInfo);
258 void
259 NodeListItem::Accept(MediaListItem::Visitor& visitor)
261 visitor.Visit(this);
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)
282 void
283 NodeListItem::Comparator::Visit(NodeListItem* item)
285 result = GREATER_THAN;
287 if (fTarget->Type() != item->Type() && fTarget->Type() == VIDEO_TYPE)
288 result = LESS_THAN;
289 else
290 result = strcmp(fTarget->Label(), item->Label());
294 void
295 NodeListItem::Comparator::Visit(DeviceListItem* item)
297 result = LESS_THAN;
298 if (fTarget->Type() != item->Type() && fTarget->Type() == AUDIO_TYPE)
299 result = GREATER_THAN;
303 void
304 NodeListItem::Comparator::Visit(AudioMixerListItem* item)
306 result = LESS_THAN;
310 void
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)
323 MediaListItem(),
324 fTitle(title),
325 fMediaType(type)
330 void
331 DeviceListItem::Accept(MediaListItem::Visitor& visitor)
333 visitor.Visit(this);
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)
354 void
355 DeviceListItem::Comparator::Visit(NodeListItem* item)
357 result = GREATER_THAN;
358 if (fTarget->Type() != item->Type() && fTarget->Type() == AUDIO_TYPE)
359 result = LESS_THAN;
363 void
364 DeviceListItem::Comparator::Visit(DeviceListItem* item)
366 result = LESS_THAN;
367 if (fTarget->Type() == AUDIO_TYPE)
368 result = GREATER_THAN;
372 void
373 DeviceListItem::Comparator::Visit(AudioMixerListItem* item)
375 result = LESS_THAN;
376 if (fTarget->Type() == AUDIO_TYPE)
377 result = GREATER_THAN;
381 void
382 DeviceListItem::Comparator::Visit(MidiListItem* item)
384 result = LESS_THAN;
388 void
389 DeviceListItem::SetRenderParameters(Renderer& renderer)
391 renderer.AddIcon(&Icons()->devicesIcon);
392 renderer.UseDoubleInset(false);
396 void
397 DeviceListItem::AlterWindow(MediaWindow* window)
399 if (fMediaType == MediaListItem::AUDIO_TYPE)
400 window->SelectAudioSettings(fTitle);
401 else
402 window->SelectVideoSettings(fTitle);
406 // #pragma mark - AudioMixerListItem
409 AudioMixerListItem::AudioMixerListItem(const char* title)
411 MediaListItem(),
412 fTitle(title)
417 void
418 AudioMixerListItem::AlterWindow(MediaWindow* window)
420 window->SelectAudioMixer(fTitle);
424 void
425 AudioMixerListItem::Accept(MediaListItem::Visitor& visitor)
427 visitor.Visit(this);
432 AudioMixerListItem::CompareWith(MediaListItem* item)
434 Comparator comparator(this);
435 item->Accept(comparator);
436 return comparator.result;
440 AudioMixerListItem::Comparator::Comparator(AudioMixerListItem* compareOthersTo)
442 result(0),
443 fTarget(compareOthersTo)
448 void
449 AudioMixerListItem::Comparator::Visit(NodeListItem* item)
451 result = GREATER_THAN;
455 void
456 AudioMixerListItem::Comparator::Visit(DeviceListItem* item)
458 result = GREATER_THAN;
459 if (item->Type() == AUDIO_TYPE)
460 result = LESS_THAN;
464 void
465 AudioMixerListItem::Comparator::Visit(AudioMixerListItem* item)
467 result = 0;
471 void
472 AudioMixerListItem::Comparator::Visit(MidiListItem* item)
474 result = GREATER_THAN;
478 void
479 AudioMixerListItem::SetRenderParameters(Renderer& renderer)
481 renderer.AddIcon(&Icons()->mixerIcon);
485 // #pragma mark - MidiListItem
487 MidiListItem::MidiListItem(const char* title)
489 MediaListItem(),
490 fTitle(title)
495 void
496 MidiListItem::AlterWindow(MediaWindow* window)
498 window->SelectMidiSettings(fTitle);
502 const char*
503 MidiListItem::Label()
505 return "MIDI";
509 void
510 MidiListItem::Accept(MediaListItem::Visitor& visitor)
512 visitor.Visit(this);
517 MidiListItem::CompareWith(MediaListItem* item)
519 Comparator comparator(this);
520 item->Accept(comparator);
521 return comparator.result;
525 MidiListItem::Comparator::Comparator(MidiListItem* compareOthersTo)
527 result(0),
528 fTarget(compareOthersTo)
533 void
534 MidiListItem::Comparator::Visit(NodeListItem* item)
536 result = GREATER_THAN;
540 void
541 MidiListItem::Comparator::Visit(DeviceListItem* item)
543 result = GREATER_THAN;
544 if (item->Type() == AUDIO_TYPE)
545 result = LESS_THAN;
549 void
550 MidiListItem::Comparator::Visit(AudioMixerListItem* item)
552 result = LESS_THAN;
556 void
557 MidiListItem::Comparator::Visit(MidiListItem* item)
559 result = 0;
563 void
564 MidiListItem::SetRenderParameters(Renderer& renderer)
566 // TODO: Create a nice icon
567 renderer.AddIcon(&Icons()->mixerIcon);