Ejemplo n.º 1
0
	void Menu::addChild(Widget* w)
	{
		if(!w->isMenuItem())
			throw Exception(Exception::ERR_INVALID_CHILD,"Cannot add non-MenuItem to a menu!","Menu::addChild");

		// Set link to parent Menu
		MenuItem* i = dynamic_cast<MenuItem*>(w);
		i->notifyMenuParent(this);

		// Set link to parent ToolBar
		i->notifyToolBarParent(mDesc->toolBar);

		// Set link to parent ContextMenu
		i->notifyContextMenuParent(mDesc->contextMenu);

		i->_setGUIManager(mDesc->guiManager);
		i->_setSheet(mDesc->sheet);
		i->setHeight(mDesc->menu_itemHeight);

		// We cannot add the widget as a child, the texture position and drawing will be incorrect.
		int itemIndex = i->getIndex();

		// Adding Item to End of List
		if((itemIndex < 0) || (itemIndex >= static_cast<int>(mItems.size())))
		{
			// Update Index
			i->setIndex(static_cast<int>(mItems.size()));
			
			// Update Position
			if(!mItems.empty())
			{
				Point p = mItems.back()->getPosition();
				p.y += i->getSize().height;
				i->setPosition(p);
			}

			mItems.push_back(i);
		}
		// Inserting Item into the List
		else
		{
			int count = 0;
			for(std::list<MenuItem*>::iterator it = mItems.begin(); it != mItems.end(); ++it)
			{
				if(count == itemIndex)
				{
					// Insert Item into List.  All items after this will need to have
					// index and position updated.

					Point p = (*it)->getPosition();

					mItems.insert(it,i);
					i->setPosition(p);

					break;
				}

				++count;
			}

			count = 0;
			Point p;
			for(std::list<MenuItem*>::iterator it = mItems.begin(); it != mItems.end(); ++it)
			{
				// Update Index
				(*it)->setIndex(count);
				// Update Position
				(*it)->setPosition(p);

				p.y += (*it)->getHeight();

				++count;
			}
		}
		
		// Add to the windows list of MenuItems
		mMenuPanel->addWidget(i);

		if(i->getClass() == "Menu")
			mSubMenus.push_back(dynamic_cast<Menu*>(i));
	}