2 * Copyright 2015 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Axel Dörfler, <axeld@pinc-software.de>
10 #include "ServiceListItem.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
)
37 ServiceListItem::~ServiceListItem()
43 ServiceListItem::DrawItem(BView
* owner
, BRect bounds
, bool complete
)
47 rgb_color lowColor
= owner
->LowColor();
49 if (IsSelected() || complete
) {
51 owner
->SetHighColor(ui_color(B_LIST_SELECTED_BACKGROUND_COLOR
));
52 owner
->SetLowColor(owner
->HighColor());
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
);
73 textColor
= ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR
);
75 textColor
= ui_color(B_LIST_ITEM_TEXT_COLOR
);
77 owner
->SetHighColor(textColor
);
78 owner
->DrawString(fLabel
, namePoint
);
81 if (textColor
.red
+ textColor
.green
+ textColor
.blue
> 128 * 3)
82 owner
->SetHighColor(tint_color(textColor
, B_DARKEN_1_TINT
));
84 owner
->SetHighColor(tint_color(textColor
, B_LIGHTEN_1_TINT
));
86 owner
->DrawString(stateText
, statePoint
);
93 ServiceListItem::Update(BView
* owner
, const BFont
* font
)
96 fEnabled
= IsEnabled();
98 BListItem::Update(owner
, font
);
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
));
113 ServiceListItem::SettingsUpdated(uint32 type
)
115 if (type
== BNetworkSettings::kMsgServiceSettingsUpdated
) {
116 bool wasEnabled
= fEnabled
;
117 fEnabled
= IsEnabled();
118 if (wasEnabled
!= fEnabled
)
119 fOwner
->Invalidate();
125 ServiceListItem::IsEnabled()
127 return fSettings
.Service(fName
).IsRunning();