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