summaryrefslogtreecommitdiffstats
path: root/arts/builder/menumaker.cpp
blob: 38f28ace5e58647b1810fd46d5a3eb09b904adb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "menumaker.h"
#include <stdio.h>
#include <assert.h>
#include <arts/debug.h>

using namespace std;

MenuEntry::MenuEntry(MenuMaker *menumaker, KActionMenu *tqparent, const char *text)
	: menumaker(menumaker), text(text)
{
	action = new KAction(TQString::fromLocal8Bit(text));
	tqparent->insert(action);
	connect(action, TQT_SIGNAL(activated()), this, TQT_SLOT(activated()));
}

void MenuEntry::activated()
{
	menumaker->menuActivated(text);
}

MenuCategory::MenuCategory(const TQString& name, const char *prefix, KActionMenu *menu)
{
	_menu = menu;
	_name = name;
	_catchall = (strcmp(prefix,"*") == 0);
	addPrefix(prefix);
}

TQString MenuCategory::name()
{
	return _name;
}

bool MenuCategory::catchall()
{
	return _catchall;
}

void MenuCategory::addPrefix(const char *prefix)
{
	prefixList.push_back(prefix);
}

bool MenuCategory::matches(const char *item)
{
	string pattern = string(item)+"$";
	size_t patternlen = strlen(pattern.c_str());

	list<string>::iterator i;

	for(i=prefixList.begin();i != prefixList.end(); ++i)
	{
		const char *prefix = (*i).c_str();

		if(patternlen >= strlen(prefix))
		{
			if(strncmp(prefix,pattern.c_str(),strlen(prefix)) == 0) return true;
		}
	}
	return false;
}

KActionMenu *MenuCategory::menu()
{
	return _menu;
}

MenuMaker::MenuMaker(KActionMenu *root)
{
	categories.push_back(new MenuCategory("","",root));
}

// Add more specific categories later, more general categories first.
//
// for instance use
//  addCategory("&Synthesis", "Synth_");
//  addCategory("&Synthesis/&Waveforms", "Synth_WAVE");

void MenuMaker::addCategory(const TQString& name, const char *prefix)
{
	MenuCategory *mc = 0,*pc = 0;
	
	mc = lookupCategoryByName(name);
	if(mc)
	{
		mc->addPrefix(prefix);
		return; // already exists
	}

	pc = lookupCategoryByName(basename(name));
	if(pc)
	{
		KActionMenu *newMenu = new KActionMenu(catname(name));
		pc->menu()->insert(newMenu);
		/* 000 */
		/*connect(newMenu,TQT_SIGNAL(activated(int)),this,TQT_SLOT(menuactivated(int)));
		pc->menu()->insertItem(catname(name).c_str(), newMenu, CAT_MAGIC_ID);*/
		arts_debug("inserting a menu called '%s' in the tqparent menu '%s'",
			catname(name).local8Bit().data(),pc->name().local8Bit().data());
		categories.push_back(new MenuCategory(name,prefix,newMenu));
	}
	else
	{
		arts_debug("Parent category '%s' for '%s' is missing.",
				basename(name).local8Bit().data(),name.local8Bit().data());
	}
}

MenuCategory *MenuMaker::lookupCategoryByName(const TQString& name)
{
	MenuCategory *mc = 0;
	list<MenuCategory *>::iterator i;
	for(i=categories.begin();i != categories.end();++i)
	{
		if((*i)->name() == name) mc = (*i);
	}
	return mc;
}

void MenuMaker::addItem(const char *name, int index)
{
	MenuCategory *mc = 0;

	list<MenuCategory *>::iterator i;
	for(i=categories.begin();i != categories.end();++i)
		if((*i)->matches(name)) mc = (*i);
	assert(mc);		// root category should always match

	if(mc->name().isEmpty())
	{
		// if we hit the root category, it may be better to move the thing
		// into the catchall category (looks cleaner)
		for(i=categories.begin();i != categories.end();++i)
			if((*i)->catchall()) mc = (*i);
	}
	//mc->menu()->insertItem(name,index); 000
	//mc->menu()->insert(new KAction(name)); // index??
	new MenuEntry(this, mc->menu(), name);
	/*
	KAction *action = new KAction(TQString(name));
	mc->menu()->insert(action);
	*/
	//action->plug(mc->menu());
	//mc->menu()->insert(new KAction(name)); // index??
}

TQString MenuMaker::basename(const TQString& name)
{
	TQString result = "";

	int i = name.findRev('/');
	if(i != -1)
		result = name.left(i);

	arts_debug("basename(%s) => %s",name.local8Bit().data(),
									result.local8Bit().data());
	return result;
}

TQString MenuMaker::catname(const TQString& name)
{
	int i = name.findRev('/');
	if(i >= 0)
		return name.mid(i+1);

	return name;
}

void MenuMaker::clear()
{
	list<MenuCategory *>::iterator i;
	for(i=categories.begin();i != categories.end();++i)
	{
		/* 000
		KActionMenu *m = (*i)->menu();
		unsigned int k;

		k = 0;
		while(k<m->count())
		{
			if(m->idAt(k) != CAT_MAGIC_ID)
			{
				m->removeItemAt(k);
				k = 0;
			}
			else
			{
				k++;
			}
		}
		*/
	}
}

const char *MenuMaker::findID(int id)
{
	/* 000 ?
	list<MenuCategory *>::iterator i;
	for(i=categories.begin();i != categories.end();i++)
	{
		const char *name = (*i)->menu()->text(id);
		if(name) return(name);
	} */
	return 0;
}

void MenuMaker::menuActivated(const char *text)
{
	emit activated(text);
}

#include "menumaker.moc"