xref: /haiku/src/preferences/network/ServiceListItem.cpp (revision 6aff37d1c79e20748c683ae224bd629f88a5b0be)
1 /*
2  * Copyright 2015 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler, <axeld@pinc-software.de>
7  */
8 
9 
10 #include "ServiceListItem.h"
11 
12 #include <Catalog.h>
13 #include <ControlLook.h>
14 
15 
16 static const char* kEnabledState = "on";
17 static const char* kDisabledState = "off";
18 
19 
20 #undef B_TRANSLATION_CONTEXT
21 #define B_TRANSLATION_CONTEXT "ServiceListItem"
22 
23 
24 ServiceListItem::ServiceListItem(const char* name, const char* label,
25 	const BNetworkSettings& settings)
26 	:
27 	fName(name),
28 	fLabel(label),
29 	fSettings(settings),
30 	fOwner(NULL),
31 	fLineOffset(0),
32 	fEnabled(false)
33 {
34 }
35 
36 
37 ServiceListItem::~ServiceListItem()
38 {
39 }
40 
41 
42 void
43 ServiceListItem::DrawItem(BView* owner, BRect bounds, bool complete)
44 {
45 	owner->PushState();
46 
47 	rgb_color lowColor = owner->LowColor();
48 
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);
55 
56 		owner->FillRect(bounds);
57 	}
58 
59 	const char* stateText = fEnabled ? B_TRANSLATE(kEnabledState)
60 		: B_TRANSLATE(kDisabledState);
61 
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);
68 
69 	owner->SetDrawingMode(B_OP_OVER);
70 
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);
76 
77 	owner->SetHighColor(textColor);
78 	owner->DrawString(fLabel, namePoint);
79 
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));
85 	}
86 	owner->DrawString(stateText, statePoint);
87 
88 	owner->PopState();
89 }
90 
91 
92 void
93 ServiceListItem::Update(BView* owner, const BFont* font)
94 {
95 	fOwner = owner;
96 	fEnabled = IsEnabled();
97 
98 	BListItem::Update(owner, font);
99 	font_height height;
100 	font->GetHeight(&height);
101 
102 	fLineOffset = 2 + ceilf(height.ascent + height.leading / 2);
103 
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));
109 }
110 
111 
112 void
113 ServiceListItem::SettingsUpdated(uint32 type)
114 {
115 	if (type == BNetworkSettings::kMsgServiceSettingsUpdated) {
116 		bool wasEnabled = fEnabled;
117 		fEnabled = IsEnabled();
118 		if (wasEnabled != fEnabled)
119 			fOwner->Invalidate();
120 	}
121 }
122 
123 
124 bool
125 ServiceListItem::IsEnabled()
126 {
127 	return fSettings.Service(fName).IsRunning();
128 }
129