1/* -----------------------------------------------------------------------
2 * Copyright (c) 2004 Waldemar Kornewald, Waldemar.Kornewald@web.de
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 * ----------------------------------------------------------------------- */
22
23#include "InterfaceUtils.h"
24
25#include "DialUpAddon.h"
26#include <ListView.h>
27#include <Menu.h>
28#include <MenuItem.h>
29#include <Screen.h>
30#include <String.h>
31#include <ListItem.h>	// Contains StringItem class declaration
32#include <Window.h>
33
34
35BPoint
36center_on_screen(BRect rect, BWindow *window)
37{
38	BRect screenFrame = (BScreen(window).Frame());
39	BPoint point((screenFrame.Width() - rect.Width()) / 2.0,
40		(screenFrame.Height() - rect.Height()) / 2.0);
41	if(!screenFrame.Contains(point))
42		point.Set(0, 0);
43
44	return point;
45}
46
47
48int32
49FindNextMenuInsertionIndex(BMenu *menu, const char *name, int32 index)
50{
51	BMenuItem *item;
52	for(; index < menu->CountItems(); index++) {
53		item = menu->ItemAt(index);
54		if(item && strcasecmp(name, item->Label()) <= 0)
55			return index;
56	}
57
58	return index;
59}
60
61
62int32
63FindNextListInsertionIndex(BListView *list, const char *name)
64{
65	int32 index = 0;
66	BStringItem *item;
67	for(; index < list->CountItems(); index++) {
68		item = static_cast<BStringItem*>(list->ItemAt(index));
69		if(item && strcasecmp(name, item->Text()) <= 0)
70			return index;
71	}
72
73	return index;
74}
75
76
77void
78AddAddonsToMenu(const BMessage *source, BMenu *menu, const char *type, uint32 what)
79{
80	DialUpAddon *addon;
81	for(int32 index = 0; source->FindPointer(type, index,
82			reinterpret_cast<void**>(&addon)) == B_OK; index++) {
83		if(!addon || (!addon->FriendlyName() && !addon->TechnicalName()))
84			continue;
85
86		BMessage *message = new BMessage(what);
87		message->AddPointer("Addon", addon);
88
89		BString name;
90		if(addon->TechnicalName()) {
91			name << addon->TechnicalName();
92			if(addon->FriendlyName())
93				name << " (";
94		}
95		if(addon->FriendlyName()) {
96			name << addon->FriendlyName();
97			if(addon->TechnicalName())
98				name << ")";
99		}
100
101		int32 insertAt = FindNextMenuInsertionIndex(menu, name.String());
102		menu->AddItem(new BMenuItem(name.String(), message), insertAt);
103	}
104}
105