File: menu_generic.cpp

package info (click to toggle)
clanlib 1.0~svn3827-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 24,600 kB
  • sloc: cpp: 101,591; xml: 6,410; makefile: 1,743; ansic: 463; perl: 424; php: 247; sh: 53
file content (429 lines) | stat: -rw-r--r-- 9,311 bytes parent folder | download | duplicates (7)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
**  ClanLib SDK
**  Copyright (c) 1997-2005 The ClanLib Team
**
**  This software is provided 'as-is', without any express or implied
**  warranty.  In no event will the authors be held liable for any damages
**  arising from the use of this software.
**
**  Permission is granted to anyone to use this software for any purpose,
**  including commercial applications, and to alter it and redistribute it
**  freely, subject to the following restrictions:
**
**  1. The origin of this software must not be misrepresented; you must not
**     claim that you wrote the original software. If you use this software
**     in a product, an acknowledgment in the product documentation would be
**     appreciated but is not required.
**  2. Altered source versions must be plainly marked as such, and must not be
**     misrepresented as being the original software.
**  3. This notice may not be removed or altered from any source distribution.
**
**  Note: Some of the libraries ClanLib may link to may have additional
**  requirements or restrictions.
**
**  File Author(s):
**
**    Magnus Norddahl
**    (if your name is missing here, please add it)
*/

#include "precomp.h"
#include "API/gui.h"
#include "API/signals.h"
#include "menu_generic.h"
#include "API/GUI/menu.h"
#include "API/GUI/menu_node.h"
#include "API/Display/mouse.h"

CL_Menu_Generic::CL_Menu_Generic(
	CL_Menu *self,
	bool vertical,
	CL_MenuNode *parent_node)
:
	parent_node(parent_node),
	vertical(vertical),
	menu_open(true),
	auto_resize(true),
	submenu_open(false),
	click_to_open(false),
	collapse_root(false),
	menu(self)
{
	if( parent_node == 0 )
	{
		slots.connect( CL_Mouse::sig_key_up(), this, &CL_Menu_Generic::on_mouse_up);
		click_to_open = true;
	}

	slots.connect( menu->sig_move(), this, &CL_Menu_Generic::on_menu_moved );

	if( vertical ) collapse();
}

CL_Menu_Generic::~CL_Menu_Generic()
{
	// todo: delete?

	std::list<CL_MenuNode *>::iterator it;
	for( it = children.begin(); it != children.end(); ++it )
	{
		delete (*it);
	}
}

/////////////////////////////////////////////////////////////////////////////
// Attributes:

CL_Menu *CL_Menu_Generic::get_root_menu()
{
	CL_Menu *test_parent = menu;
	
	while( test_parent != NULL )
	{
		if( test_parent->get_parent_node() == NULL )
			return test_parent;
		else
			test_parent = test_parent->get_parent_node()->get_parent_menu();
	}

	return NULL;
}

int CL_Menu_Generic::get_items_width()
{
	int width = 0;

	std::list<CL_MenuNode*>::iterator it;
	for( it = children.begin(); it != children.end(); ++it )
	{
		const CL_Rect &pos = (*it)->get_data()->get_position();
		int child_width = (*it)->get_data()->get_width();

		if( vertical )
		{
			if( pos.left + child_width > width )
				width = pos.left + child_width;
		}
		else
		{
			width += child_width;
		}

	}
	
	return width;
}

int CL_Menu_Generic::get_items_height()
{
	int height = 0;
	
	if( !children.empty() )
	{
		std::list<CL_MenuNode*>::iterator it;
		for( it = children.begin(); it != children.end(); ++it )
		{
			const CL_Rect &pos = (*it)->get_data()->get_position();

			if( vertical )
			{
				height += pos.get_height();
			}
			else
			{
				if( pos.get_height() > height )
					height = pos.get_height();
			}
		}
	}
	
	return height;
}

bool CL_Menu_Generic::has_mouse_in_submenus()
{
	std::list<CL_MenuNode*>::iterator it;
	for( it = children.begin(); it != children.end(); ++it )
	{
		if( (*it)->has_mouse_over() )
			return true;
	
		if( (*it)->has_mouse_in_submenus() )
			return true;
	}

	return false;
}

bool CL_Menu_Generic::has_mouse_over()
{
	if( menu->CL_Component::has_mouse_over() )
		return true;
	
	std::list<CL_MenuNode*>::iterator it;
	for( it = children.begin(); it != children.end(); ++it )
	{
		if( (*it)->has_mouse_over() )
			return true;
	}
	
	return false;
}

CL_Menu *CL_Menu_Generic::get_menu( std::vector<std::string> path )
{
	bool final_step = false;
	std::string wanted = path.front();

	path.erase(path.begin());
	if( path.empty() )
		final_step = true;
	
	std::list<CL_MenuNode*>::iterator it;
	for( it = children.begin(); it != children.end(); ++it )
	{
		CL_MenuNode *node = (*it);
		
		if( node->has_submenu() )
		{
			if( node->get_name() == wanted )
			{
				if( final_step )
					return node->get_submenu();
				else
					return node->get_submenu()->impl->get_menu(path);
			}
		}
	}

	return 0;
}

CL_MenuNode *CL_Menu_Generic::get_node( std::vector<std::string> path )
{
	bool final_step = false;
	std::string wanted = path.front();
	
	path.erase(path.begin());
	if( path.empty() )
		final_step = true;

	std::list<CL_MenuNode*>::iterator it;
	for( it = children.begin(); it != children.end(); ++it )
	{
		CL_MenuNode *node = (*it);

		if( node->get_name() == wanted )
		{
			if( final_step )
			{
				return node;
			}
			else if( node->has_submenu() )
			{
				return node->get_submenu()->impl->get_node(path);
			}
		}
	}

	return 0;
}

CL_Component *CL_Menu_Generic::get_item( std::vector<std::string> path )
{
	return get_node(path)->get_data();
}

/////////////////////////////////////////////////////////////////////////////
// Operations:

void CL_Menu_Generic::add_node(CL_MenuNode *node)
{
	node->set_parent_menu(menu);	
	children.push_back(node);
}

void CL_Menu_Generic::remove_node(CL_MenuNode *node)
{
	std::list<CL_MenuNode *>::iterator it;
	for( it = children.begin(); it != children.end(); ++it )
	{
		if( (*it) == node )
			it = children.erase(it);
	}
	
	CL_Menu *root = get_root_menu();
	CL_Rect pos = root->get_position();
	root->reposition(pos.left, pos.top);
}

void CL_Menu_Generic::open()
{
	menu->show(true);
	menu_open = true;
	if( menu->get_parent_node() )
		menu->get_parent_node()->get_parent_menu()->impl->submenu_open = true;
}

void CL_Menu_Generic::collapse()
{
	collapse_submenus();

	menu->show(false);
	menu_open = false;
}

void CL_Menu_Generic::collapse_submenus()
{
	std::list<CL_MenuNode*>::iterator it;
	for( it = children.begin(); it != children.end(); ++it )
	{
		(*it)->collapse();
	}

	submenu_open = false;
}	

CL_Menu *CL_Menu_Generic::create_menu(
	std::vector<std::string> path,
	std::vector<std::string> labels )
{
	if( path.empty() )
		return menu->get_root_menu();

	bool final_step = false;
	CL_MenuNode *wanted_node = 0;

	std::list<CL_MenuNode*>::iterator it;
	for( it = children.begin(); it != children.end(); ++it )
	{
		if( (*it)->get_name() == path.front() )
		{
			wanted_node = (*it);
		}
	}

	std::string wanted_name = path.front();
	std::string wanted_label = labels.front();

	path.erase(path.begin());
	labels.erase(labels.begin());

	if( path.empty() )
		final_step = true;

	if( wanted_node != 0 )
	{
		if( wanted_node->has_submenu() ) // node & submenu exsists
		{
			if( final_step )
				return wanted_node->get_submenu();
			else
				return wanted_node->get_submenu()->impl->create_menu(path, labels);
		}
		else 
		{
			// submenu doesn't exsist, create one.
			CL_Menu *submenu = new CL_Menu(wanted_node, menu->get_root_menu()->get_parent() );
			
			if( final_step ) return submenu;
			else return submenu->impl->create_menu(path, labels);
		}
	}
	else
	{
		// create node, item & submenu.

		// node
		wanted_node = new CL_MenuNode(menu);
		wanted_node->set_name(wanted_name);

		// item (submenu label)
		CL_MenuItem *item = new CL_MenuItem(wanted_label, wanted_node);

		if( wanted_node->get_parent_menu()->get_parent_node() == 0 )
		{
			item->set_use_icon(false);
			item->find_preferred_size();
		}

		// submenu
		CL_Menu *submenu = new CL_Menu(wanted_node, menu->get_root_menu()->get_parent());
		
		if( final_step ) return submenu;
		else return submenu->impl->create_menu(path,labels);
	}

	return 0;
}


CL_MenuNode *CL_Menu_Generic::create_node(
	std::vector<std::string> path,
	std::vector<std::string> labels )
{
	CL_MenuNode *node = menu->get_root_menu()->impl->get_node(path);

	if( node )
		return node;

	std::string node_name = path.back();
	path.pop_back();
	labels.pop_back();

	CL_Menu *parent_menu = create_menu(path, labels);

	node = new CL_MenuNode(parent_menu);
	node->set_name(node_name);

	return node;
}

CL_MenuNode *CL_Menu_Generic::create_item(
	std::vector<std::string> path,
	std::vector<std::string> labels)
{
	std::string item_name = labels.back();
	CL_MenuNode *parent_node = create_node(path, labels);
	// The newly created item is added to the parent_node in the constructor
	new CL_MenuItem(item_name, parent_node );

	return parent_node;
}

CL_MenuNode *CL_Menu_Generic::create_toggle_item(
	std::vector<std::string> path,
	std::vector<std::string> labels)
{
	CL_MenuNode *node = create_item(path,labels);
	CL_MenuItem *item = static_cast<CL_MenuItem*>(node->get_data());

	item->set_toggling(true);
	node->set_close_on_click(false);

	return node;
}


/////////////////////////////////////////////////////////////////////////////
// Callbacks:

// on_mouse_up() is connected only to the root menu. It will collapse
// the menus if a click outside the menus takes place.
void CL_Menu_Generic::on_mouse_up(const CL_InputEvent&)
{
	if( submenu_open && !menu->has_mouse_in_submenus() )
	{
		collapse_submenus();
		click_to_open = true;
	}

	if( parent_node	== 0 && collapse_root
		&& !menu->has_mouse_over() && !menu->has_mouse_in_submenus()  )
		collapse();
}

void CL_Menu_Generic::on_menu_moved(int old_x, int old_y)
{
	menu->reposition();
}