docs/ikteam: Delete most files.
[haiku.git] / src / preferences / network / ServiceListItem.cpp
blob604fe434f3dbe28522e70e0bcd22865a16667fd1
1 /*
2 * Copyright 2015 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Axel Dörfler, <axeld@pinc-software.de>
7 */
10 #include "ServiceListItem.h"
12 #include <Catalog.h>
13 #include <ControlLook.h>
16 static const char* kEnabledState = "on";
17 static const char* kDisabledState = "off";
20 #undef B_TRANSLATION_CONTEXT
21 #define B_TRANSLATION_CONTEXT "ServiceListItem"
24 ServiceListItem::ServiceListItem(const char* name, const char* label,
25 const BNetworkSettings& settings)
27 fName(name),
28 fLabel(label),
29 fSettings(settings),
30 fOwner(NULL),
31 fLineOffset(0),
32 fEnabled(false)
37 ServiceListItem::~ServiceListItem()
42 void
43 ServiceListItem::DrawItem(BView* owner, BRect bounds, bool complete)
45 owner->PushState();
47 rgb_color lowColor = owner->LowColor();
49 if (IsSelected() || complete) {
50 if (IsSelected()) {
51 owner->SetHighColor(ui_color(B_LIST_SELECTED_BACKGROUND_COLOR));
52 owner->SetLowColor(owner->HighColor());
53 } else
54 owner->SetHighColor(lowColor);
56 owner->FillRect(bounds);
59 const char* stateText = fEnabled ? B_TRANSLATE(kEnabledState)
60 : B_TRANSLATE(kDisabledState);
62 // Set the initial bounds of item contents
63 BPoint statePoint = bounds.RightTop() + BPoint(0, fLineOffset)
64 - BPoint(be_plain_font->StringWidth(stateText)
65 + be_control_look->DefaultLabelSpacing(), 0);
66 BPoint namePoint = bounds.LeftTop()
67 + BPoint(be_control_look->DefaultLabelSpacing(), fLineOffset);
69 owner->SetDrawingMode(B_OP_OVER);
71 rgb_color textColor;
72 if (IsSelected())
73 textColor = ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR);
74 else
75 textColor = ui_color(B_LIST_ITEM_TEXT_COLOR);
77 owner->SetHighColor(textColor);
78 owner->DrawString(fLabel, namePoint);
80 if (!fEnabled) {
81 if (textColor.red + textColor.green + textColor.blue > 128 * 3)
82 owner->SetHighColor(tint_color(textColor, B_DARKEN_1_TINT));
83 else
84 owner->SetHighColor(tint_color(textColor, B_LIGHTEN_1_TINT));
86 owner->DrawString(stateText, statePoint);
88 owner->PopState();
92 void
93 ServiceListItem::Update(BView* owner, const BFont* font)
95 fOwner = owner;
96 fEnabled = IsEnabled();
98 BListItem::Update(owner, font);
99 font_height height;
100 font->GetHeight(&height);
102 fLineOffset = 2 + ceilf(height.ascent + height.leading / 2);
104 float maxStateWidth = std::max(font->StringWidth(B_TRANSLATE(kEnabledState)),
105 font->StringWidth(B_TRANSLATE(kDisabledState)));
106 SetWidth(font->StringWidth(fLabel)
107 + 3 * be_control_look->DefaultLabelSpacing() + maxStateWidth);
108 SetHeight(4 + ceilf(height.ascent + height.leading + height.descent));
112 void
113 ServiceListItem::SettingsUpdated(uint32 type)
115 if (type == BNetworkSettings::kMsgServiceSettingsUpdated) {
116 bool wasEnabled = fEnabled;
117 fEnabled = IsEnabled();
118 if (wasEnabled != fEnabled)
119 fOwner->Invalidate();
124 bool
125 ServiceListItem::IsEnabled()
127 return fSettings.Service(fName).IsRunning();