xref: /haiku/src/add-ons/network_settings/dialup/InterfaceUtils.cpp (revision b08627f310bb2e80bca50176e7a758182384735a)
1 /*
2  * Copyright 2003-2004 Waldemar Kornewald. All rights reserved.
3  * Copyright 2017 Haiku, Inc. All rights reserved.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 
8 #include "InterfaceUtils.h"
9 
10 #include "DialUpAddon.h"
11 #include <ListView.h>
12 #include <Menu.h>
13 #include <MenuItem.h>
14 #include <Screen.h>
15 #include <String.h>
16 #include <ListItem.h>	// Contains StringItem class declaration
17 #include <Window.h>
18 
19 
20 BPoint
21 center_on_screen(BRect rect, BWindow *window)
22 {
23 	BRect screenFrame = (BScreen(window).Frame());
24 	BPoint point((screenFrame.Width() - rect.Width()) / 2.0,
25 		(screenFrame.Height() - rect.Height()) / 2.0);
26 	if(!screenFrame.Contains(point))
27 		point.Set(0, 0);
28 
29 	return point;
30 }
31 
32 
33 int32
34 FindNextMenuInsertionIndex(BMenu *menu, const char *name, int32 index)
35 {
36 	BMenuItem *item;
37 	for(; index < menu->CountItems(); index++) {
38 		item = menu->ItemAt(index);
39 		if(item && strcasecmp(name, item->Label()) <= 0)
40 			return index;
41 	}
42 
43 	return index;
44 }
45 
46 
47 int32
48 FindNextListInsertionIndex(BListView *list, const char *name)
49 {
50 	int32 index = 0;
51 	BStringItem *item;
52 	for(; index < list->CountItems(); index++) {
53 		item = static_cast<BStringItem*>(list->ItemAt(index));
54 		if(item && strcasecmp(name, item->Text()) <= 0)
55 			return index;
56 	}
57 
58 	return index;
59 }
60 
61 
62 void
63 AddAddonsToMenu(const BMessage *source, BMenu *menu, const char *type, uint32 what)
64 {
65 	DialUpAddon *addon;
66 	for(int32 index = 0; source->FindPointer(type, index,
67 			reinterpret_cast<void**>(&addon)) == B_OK; index++) {
68 		if(!addon || (!addon->FriendlyName() && !addon->TechnicalName()))
69 			continue;
70 
71 		BMessage *message = new BMessage(what);
72 		message->AddPointer("Addon", addon);
73 
74 		BString name;
75 		if(addon->TechnicalName()) {
76 			name << addon->TechnicalName();
77 			if(addon->FriendlyName())
78 				name << " (";
79 		}
80 		if(addon->FriendlyName()) {
81 			name << addon->FriendlyName();
82 			if(addon->TechnicalName())
83 				name << ")";
84 		}
85 
86 		int32 insertAt = FindNextMenuInsertionIndex(menu, name.String());
87 		menu->AddItem(new BMenuItem(name.String(), message), insertAt);
88 	}
89 }
90