CL_Component *CL_GUIManager_Generic::tab_previous()
{
	// First try to find component by tab id:
	CL_Component *tab_component = owner->find_tab_id(--current_tab_id);
	if (current_tab_id == -1) // reached beginning, start from end
	{
		current_tab_id = find_highest_tab_id(owner);
		tab_component = owner->find_tab_id(current_tab_id);
	}
	if (tab_component)
	{
		set_focus(tab_component);
		return comp_focus;
	}

	if(comp_focus == 0)
		return 0;

	// No tab ids - fall back to tree walking:
	CL_Component *cur = comp_focus;
	while (true)
	{
		CL_Component *parent = cur->get_parent();
		if (parent == 0) break;

		// Search siblings:
		std::list<CL_Component *>::const_iterator it;
		for (it = parent->get_children().begin(); it != parent->get_children().end(); it++)
		{
			if (*it == cur)
			{
				// no more siblings
				if (it == parent->get_children().begin()) break;

				it--;

				// give sibling focus
				set_focus(*it);
				return comp_focus;
			}
		}

		// Was last sibling, continue search in parent (uncles)
		cur = parent;
	}

	// walked through all components. Restart at end component:
	cur = owner;
	while (!cur->get_children().empty()) cur = cur->get_children().back();
	set_focus(cur);
	return comp_focus;
}
CL_Component *CL_GUIManager_Generic::tab_next()
{
	// First try to find component by tab id:
	CL_Component *tab_component = owner->find_tab_id(++current_tab_id);
	if (tab_component == 0) // reached end, start from beginning
	{
		current_tab_id = 0;
		tab_component = owner->find_tab_id(current_tab_id);
	}
	if (tab_component)
	{
		set_focus(tab_component);
		return comp_focus;
	}

	if(comp_focus == 0)
		return 0;
		
	// No tab ids - fall back to tree walking:
	if (comp_focus->get_children().empty()) // tab to sibling or uncle
	{
		CL_Component *cur = comp_focus;

		while (true)
		{
			CL_Component *parent = cur->get_parent();
			if (parent == 0) break;

			// Search siblings:
			std::list<CL_Component *>::const_iterator it;
			for (it = parent->get_children().begin(); it != parent->get_children().end(); it++)
			{
				if (*it == cur)
				{
					it++;

					// no more siblings
					if (it == parent->get_children().end()) break;

					// give sibling focus
					set_focus(*it);
					return comp_focus;
				}
			}

			// Was last sibling, continue search in parent (uncles)
			cur = parent;
		}

		// if we are top node (no siblings), and got no children:
		if (cur == comp_focus) return comp_focus;

		// walked through all components. Restart at first component:
		set_focus(owner);
		return comp_focus;
	}
	else // tab to children
	{
		// give first child focus
		set_focus(comp_focus->get_children().front());
		return comp_focus;
	}
}