/*************************************************************************
	Calculate size and position for a tab button
*************************************************************************/
void TabControl::calculateTabButtonSizePosition(size_t index)
{
    TabButton* btn = d_tabButtonVector [index];
    // relative height is always 1.0 for buttons since they are embedded in a
    // panel of the correct height already
    btn->setHeight(cegui_reldim(1.0f));
    btn->setYPosition(cegui_absdim(0.0f));
    // x position is based on previous button
    if (!index)
        // First button
        btn->setXPosition(cegui_absdim(d_firstTabOffset));
    else
    {
		Window* prevButton = d_tabButtonVector [index - 1];

		// position is prev pos + width
        btn->setXPosition(prevButton->getArea().d_max.d_x);
    }
    // Width is based on font size (expressed as absolute)
    Font* fnt = btn->getFont();
    btn->setWidth(cegui_absdim(fnt->getTextExtent(btn->getText())) +
                        getTabTextPadding() + getTabTextPadding());

    float left_x = btn->getXPosition ().d_offset;
    btn->setVisible ((left_x < getPixelSize ().d_width) &&
                     (left_x + btn->getPixelSize ().d_width > 0));
    btn->invalidate();
}
/*************************************************************************
Make tab visible implementation
*************************************************************************/
void TabControl::makeTabVisible_impl(Window* wnd)
{
    TabButton *tb = 0;

    for (size_t i = 0; i < d_tabButtonVector.size (); ++i)
    {
        // get corresponding tab button and content window
        tb = d_tabButtonVector [i];
        Window* child = tb->getTargetWindow();
        if (child == wnd)
            break;
        tb = 0;
    }

    if (!tb)
        return;

    float ww = getPixelSize ().d_width;
    float x = tb->getXPosition().asAbsolute (ww);
    float w = tb->getPixelSize ().d_width;
    float lx = 0, rx = ww;

    Window *scrollLeftBtn = 0, *scrollRightBtn = 0;
    String name = getName() + ButtonScrollLeftSuffix;
    if (WindowManager::getSingleton().isWindowPresent (name))
    {
        scrollLeftBtn = WindowManager::getSingleton().getWindow (name);
        lx = scrollLeftBtn->getArea ().d_max.d_x.asAbsolute (ww);
        scrollLeftBtn->setWantsMultiClickEvents (false);
    }

    name = getName() + ButtonScrollRightSuffix;
    if (WindowManager::getSingleton().isWindowPresent (name))
    {
        scrollRightBtn = WindowManager::getSingleton().getWindow (name);
        rx = scrollRightBtn->getXPosition ().asAbsolute (ww);
        scrollRightBtn->setWantsMultiClickEvents (false);
    }

    if (x < lx)
        d_firstTabOffset += lx - x;
    else
    {
        if (x + w <= rx)
            return; // nothing to do

        d_firstTabOffset += rx - (x + w);
    }

    performChildWindowLayout ();
}