generic_layout.cpp 6.36 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * generic_layout.cpp
 *****************************************************************************
 * Copyright (C) 2003 VideoLAN
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Authors: Cyril Deguet     <asmax@via.ecp.fr>
 *          Olivier Teulire <ipkiss@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#include "generic_layout.hpp"
26
#include "top_window.hpp"
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
#include "os_factory.hpp"
#include "os_graphics.hpp"
#include "../controls/ctrl_generic.hpp"


GenericLayout::GenericLayout( intf_thread_t *pIntf, int width, int height,
                              int minWidth, int maxWidth, int minHeight,
                              int maxHeight ):
    SkinObject( pIntf ), m_pWindow( NULL ), m_width( width ),
    m_height( height ), m_minWidth( minWidth ), m_maxWidth( maxWidth ),
    m_minHeight( minHeight ), m_maxHeight( maxHeight )
{
    // Get the OSFactory
    OSFactory *pOsFactory = OSFactory::instance( getIntf() );
    // Create the graphics buffer
    m_pImage = pOsFactory->createOSGraphics( width, height );
}


GenericLayout::~GenericLayout()
{
    if( m_pImage )
    {
        delete m_pImage;
    }
}


55
void GenericLayout::setWindow( TopWindow *pWindow )
56 57 58 59 60 61 62 63
{
    m_pWindow = pWindow;
}


void GenericLayout::onControlCapture( const CtrlGeneric &rCtrl )
{
    // Just forward the request to the window
64
    TopWindow *pWindow = getWindow();
65 66 67 68 69 70 71 72 73 74
    if( pWindow )
    {
        pWindow->onControlCapture( rCtrl );
    }
}


void GenericLayout::onControlRelease( const CtrlGeneric &rCtrl )
{
    // Just forward the request to the window
75
    TopWindow *pWindow = getWindow();
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
    if( pWindow )
    {
        pWindow->onControlRelease( rCtrl );
    }
}


void GenericLayout::addControl( CtrlGeneric *pControl,
                              const Position &rPosition, int layer )
{
    if( pControl )
    {
        // Associate this layout to the control
        pControl->setLayout( this, rPosition );

        // Draw the control
        pControl->draw( *m_pImage, rPosition.getLeft(), rPosition.getTop() );

        // Add the control in the list.
        // This list must remain sorted by layer order 
        list<LayeredControl>::iterator it;
        for( it = m_controlList.begin(); it != m_controlList.end(); it++ )
        {
            if( layer < (*it).m_layer )
            {
                m_controlList.insert( it, LayeredControl( pControl, layer ) );
                break;
            }
        }
        // If this control is in front of all the previous ones
        if( it == m_controlList.end() )
        {
            m_controlList.push_back( LayeredControl( pControl, layer ) );
        }
    }
    else
    {
        msg_Dbg( getIntf(), "Adding NULL control in the layout" );
    }
}


const list<LayeredControl> &GenericLayout::getControlList() const
{
    return m_controlList;
}


124 125
void GenericLayout::onControlUpdate( const CtrlGeneric &rCtrl,
                                     int width, int height )
126
{
127 128 129 130 131 132 133 134 135 136 137 138
    // The size was not specified (or invalid)
    if( width <= 0 || height <= 0 )
    {
        refreshAll();
        return;
    }

    const Position *pPos = rCtrl.getPosition();
    if( pPos )
    {
        refreshRect( pPos->getLeft(), pPos->getTop(), width, height );
    }
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
}


void GenericLayout::resize( int width, int height )
{
    if( width == m_width && height == m_height )
    {
        return;
    }

    // Update the window size
    m_width = width;
    m_height = height;

    // Recreate a new image
    if( m_pImage )
    {
        delete m_pImage;
        OSFactory *pOsFactory = OSFactory::instance( getIntf() );
        m_pImage = pOsFactory->createOSGraphics( width, height );
    }

    // Notify all the controls that the size has changed and redraw them
    list<LayeredControl>::const_iterator iter;
    for( iter = m_controlList.begin(); iter != m_controlList.end(); iter++ )
    {
        (*iter).m_pControl->onResize();
        const Position *pPos = (*iter).m_pControl->getPosition();
        if( pPos )
        {
            (*iter).m_pControl->draw( *m_pImage, pPos->getLeft(),
                                      pPos->getTop() );
        }
    }

    // Resize and refresh the associated window
175
    TopWindow *pWindow = getWindow();
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    if( pWindow )
    {
        // Resize the window
        pWindow->refresh( 0, 0, width, height );
        pWindow->resize( width, height );
        pWindow->refresh( 0, 0, width, height );

        // Change the shape of the window and redraw it
        pWindow->updateShape();
        pWindow->refresh( 0, 0, width, height );
    }
}


void GenericLayout::refreshAll()
191 192 193 194 195 196
{
    refreshRect( 0, 0, m_width, m_height );
}


void GenericLayout::refreshRect( int x, int y, int width, int height )
197 198 199 200 201
{
    // Draw all the controls of the layout
    list<LayeredControl>::const_iterator iter;
    for( iter = m_controlList.begin(); iter != m_controlList.end(); iter++ )
    {
202 203 204
        CtrlGeneric *pCtrl = (*iter).m_pControl;
        const Position *pPos = pCtrl->getPosition();
        if( pCtrl->isVisible() && pPos )
205
        {
206
            pCtrl->draw( *m_pImage, pPos->getLeft(), pPos->getTop() );
207 208 209 210
        }
    }

    // Refresh the associated window
211
    TopWindow *pWindow = getWindow();
212 213
    if( pWindow )
    {
214 215 216 217 218 219 220 221 222 223 224
        // Check boundaries
        if( x < 0 )
            x = 0;
        if( y < 0)
            y = 0;
        if( x + width > m_width )
            width = m_width - x;
        if( y + height > m_height )
            height = m_height - y;

        pWindow->refresh( x, y, width, height );
225 226 227
    }
}

228 229 230 231 232 233 234 235 236 237 238 239

const list<Anchor*>& GenericLayout::getAnchorList() const
{
    return m_anchorList;
}


void GenericLayout::addAnchor( Anchor *pAnchor )
{
    m_anchorList.push_back( pAnchor );
}