2 * Copyright 2004-2015 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Alexander von Gluck IV, kallisti5@unixzen.com
9 * John Scipione, jscipione@gmail.com
13 #include "InterfaceListItem.h"
17 #include <Application.h>
20 #include <ControlLook.h>
21 #include <IconUtils.h>
22 #include <OutlineListView.h>
23 #include <Resources.h>
30 #undef B_TRANSLATION_CONTEXT
31 #define B_TRANSLATION_CONTEXT "InterfaceListItem"
34 InterfaceListItem::InterfaceListItem(const char* name
,
35 BNetworkInterfaceType type
)
46 fInterface
.SetTo(name
);
51 InterfaceListItem::~InterfaceListItem()
57 // #pragma mark - InterfaceListItem public methods
61 InterfaceListItem::DrawItem(BView
* owner
, BRect bounds
, bool complete
)
65 rgb_color lowColor
= owner
->LowColor();
67 if (IsSelected() || complete
) {
69 owner
->SetHighColor(ui_color(B_LIST_SELECTED_BACKGROUND_COLOR
));
70 owner
->SetLowColor(owner
->HighColor());
72 owner
->SetHighColor(lowColor
);
74 owner
->FillRect(bounds
);
77 BBitmap
* stateIcon
= _StateIcon();
78 const char* stateText
= _StateText();
80 // Set the initial bounds of item contents
81 BPoint iconPoint
= bounds
.LeftTop()
82 + BPoint(be_control_look
->DefaultLabelSpacing(), 2);
83 BPoint statePoint
= bounds
.RightTop() + BPoint(0, fFirstLineOffset
)
84 - BPoint(be_plain_font
->StringWidth(stateText
)
85 + be_control_look
->DefaultLabelSpacing(), 0);
86 BPoint namePoint
= bounds
.LeftTop()
87 + BPoint(ICON_SIZE
+ (be_control_look
->DefaultLabelSpacing() * 2),
91 owner
->SetDrawingMode(B_OP_ALPHA
);
92 owner
->SetBlendingMode(B_CONSTANT_ALPHA
, B_ALPHA_OVERLAY
);
93 owner
->SetHighColor(0, 0, 0, 32);
95 owner
->SetDrawingMode(B_OP_OVER
);
97 owner
->DrawBitmapAsync(fIcon
, iconPoint
);
98 owner
->DrawBitmapAsync(stateIcon
, iconPoint
);
103 textColor
= ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR
);
105 textColor
= ui_color(B_LIST_ITEM_TEXT_COLOR
);
107 if (textColor
.red
+ textColor
.green
+ textColor
.blue
> 128 * 3)
108 owner
->SetHighColor(tint_color(textColor
, B_DARKEN_1_TINT
));
110 owner
->SetHighColor(tint_color(textColor
, B_LIGHTEN_1_TINT
));
113 owner
->SetHighColor(ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR
));
115 owner
->SetHighColor(ui_color(B_LIST_ITEM_TEXT_COLOR
));
118 owner
->SetFont(be_bold_font
);
120 owner
->DrawString(fDeviceName
, namePoint
);
121 owner
->SetFont(be_plain_font
);
122 owner
->DrawString(stateText
, statePoint
);
124 BPoint linePoint
= bounds
.LeftTop()
125 + BPoint(ICON_SIZE
+ (be_control_look
->DefaultLabelSpacing() * 2),
126 fFirstLineOffset
+ fLineOffset
);
127 owner
->DrawString(fSubtitle
, linePoint
);
134 InterfaceListItem::Update(BView
* owner
, const BFont
* font
)
136 BListItem::Update(owner
, font
);
138 font
->GetHeight(&height
);
140 float lineHeight
= ceilf(height
.ascent
) + ceilf(height
.descent
)
141 + ceilf(height
.leading
);
143 fFirstLineOffset
= 2 + ceilf(height
.ascent
+ height
.leading
/ 2);
144 fLineOffset
= lineHeight
;
148 SetWidth(fIcon
->Bounds().Width() + 36
149 + be_control_look
->DefaultLabelSpacing()
150 + be_bold_font
->StringWidth(fDeviceName
.String())
151 + owner
->StringWidth(_StateText()));
152 SetHeight(std::max(2 * lineHeight
+ 4, fIcon
->Bounds().Height() + 4));
153 // either to the text height or icon height, whichever is taller
158 InterfaceListItem::ConfigurationUpdated(const BMessage
& message
)
164 // #pragma mark - InterfaceListItem private methods
168 InterfaceListItem::_Init()
172 case B_NETWORK_INTERFACE_TYPE_WIFI
:
173 _PopulateBitmaps("wifi");
175 case B_NETWORK_INTERFACE_TYPE_ETHERNET
:
176 _PopulateBitmaps("ether");
183 InterfaceListItem::_PopulateBitmaps(const char* mediaType
)
185 const uint8
* interfaceHVIF
;
186 const uint8
* offlineHVIF
;
187 const uint8
* pendingHVIF
;
188 const uint8
* onlineHVIF
;
190 BBitmap
* interfaceBitmap
= NULL
;
192 BResources
* resources
= BApplication::AppResources();
196 // Try specific interface icon?
197 interfaceHVIF
= (const uint8
*)resources
->LoadResource(
198 B_VECTOR_ICON_TYPE
, Name(), &iconSize
);
200 if (interfaceHVIF
== NULL
&& mediaType
!= NULL
)
201 // Not found, try interface media type?
202 interfaceHVIF
= (const uint8
*)resources
->LoadResource(
203 B_VECTOR_ICON_TYPE
, mediaType
, &iconSize
);
204 if (interfaceHVIF
== NULL
)
205 // Not found, try default interface icon?
206 interfaceHVIF
= (const uint8
*)resources
->LoadResource(
207 B_VECTOR_ICON_TYPE
, "ether", &iconSize
);
210 // Now build the bitmap
211 interfaceBitmap
= new BBitmap(BRect(0, 0, ICON_SIZE
, ICON_SIZE
),
213 if (BIconUtils::GetVectorIcon(interfaceHVIF
,
214 iconSize
, interfaceBitmap
) == B_OK
)
215 fIcon
= interfaceBitmap
;
217 delete interfaceBitmap
;
220 // Load possible state icons
221 offlineHVIF
= (const uint8
*)resources
->LoadResource(
222 B_VECTOR_ICON_TYPE
, "offline", &iconSize
);
225 fIconOffline
= new BBitmap(BRect(0, 0, ICON_SIZE
, ICON_SIZE
),
227 BIconUtils::GetVectorIcon(offlineHVIF
, iconSize
, fIconOffline
);
230 pendingHVIF
= (const uint8
*)resources
->LoadResource(
231 B_VECTOR_ICON_TYPE
, "pending", &iconSize
);
234 fIconPending
= new BBitmap(BRect(0, 0, ICON_SIZE
, ICON_SIZE
),
236 BIconUtils::GetVectorIcon(pendingHVIF
, iconSize
, fIconPending
);
239 onlineHVIF
= (const uint8
*)resources
->LoadResource(
240 B_VECTOR_ICON_TYPE
, "online", &iconSize
);
243 fIconOnline
= new BBitmap(BRect(0, 0, ICON_SIZE
, ICON_SIZE
),
245 BIconUtils::GetVectorIcon(onlineHVIF
, iconSize
, fIconOnline
);
251 InterfaceListItem::_UpdateState()
253 fDeviceName
= Name();
254 fDeviceName
.RemoveFirst("/dev/net/");
256 fDisabled
= (fInterface
.Flags() & IFF_UP
) == 0;
257 fHasLink
= fInterface
.HasLink();
258 fConnecting
= (fInterface
.Flags() & IFF_CONFIGURING
) != 0;
261 case B_NETWORK_INTERFACE_TYPE_WIFI
:
262 fSubtitle
= B_TRANSLATE("Wireless device");
264 case B_NETWORK_INTERFACE_TYPE_ETHERNET
:
265 fSubtitle
= B_TRANSLATE("Ethernet device");
267 case B_NETWORK_INTERFACE_TYPE_DIAL_UP
:
268 fSubtitle
= B_TRANSLATE("Dial-up connection");
270 case B_NETWORK_INTERFACE_TYPE_VPN
:
271 fSubtitle
= B_TRANSLATE("VPN connection");
280 InterfaceListItem::_StateIcon() const
287 // } else if ((fSettings->IPAddr(AF_INET).IsEmpty()
288 // && fSettings->IPAddr(AF_INET6).IsEmpty())
289 // && (fSettings->AutoConfigure(AF_INET)
290 // || fSettings->AutoConfigure(AF_INET6))) {
291 // interfaceState = "connecting" B_UTF8_ELLIPSIS;
292 // stateIcon = fIconPending;
299 InterfaceListItem::_StateText() const
302 return B_TRANSLATE("disabled");
303 if (!fInterface
.HasLink())
304 return B_TRANSLATE("no link");
307 // } else if ((fSettings->IPAddr(AF_INET).IsEmpty()
308 // && fSettings->IPAddr(AF_INET6).IsEmpty())
309 // && (fSettings->AutoConfigure(AF_INET)
310 // || fSettings->AutoConfigure(AF_INET6))) {
311 // interfaceState = "connecting" B_UTF8_ELLIPSIS;
312 // stateIcon = fIconPending;
314 return B_TRANSLATE("connected");