]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_generic.cpp
skins2: improve refresh of layouts
[vlc] / modules / gui / skins2 / controls / ctrl_generic.cpp
1 /*****************************************************************************
2  * ctrl_generic.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include "ctrl_generic.hpp"
26 #include "../src/generic_layout.hpp"
27 #include "../src/top_window.hpp"
28 #include "../src/os_graphics.hpp"
29 #include "../utils/position.hpp"
30 #include "../utils/var_bool.hpp"
31
32 #include <assert.h>
33
34
35 CtrlGeneric::CtrlGeneric( intf_thread_t *pIntf, const UString &rHelp,
36                           VarBool *pVisible):
37     SkinObject( pIntf ), m_pLayout( NULL ), m_pPosition( NULL ),
38     m_help( rHelp ), m_pVisible( pVisible )
39 {
40     // Observe the visibility variable
41     if( m_pVisible )
42     {
43         m_pVisible->addObserver( this );
44     }
45 }
46
47
48 CtrlGeneric::~CtrlGeneric()
49 {
50     if( m_pVisible )
51     {
52         m_pVisible->delObserver( this );
53     }
54 }
55
56
57 void CtrlGeneric::setLayout( GenericLayout *pLayout,
58                              const Position &rPosition )
59 {
60     assert( !m_pLayout && pLayout);
61
62     m_pLayout = pLayout;
63     m_pPosition = new Position( rPosition );
64     onPositionChange();
65 }
66
67 void CtrlGeneric::unsetLayout()
68 {
69     assert( m_pLayout );
70
71     delete m_pPosition;
72     m_pPosition = NULL;
73     m_pLayout = NULL;
74 }
75
76 void CtrlGeneric::notifyLayout( int width, int height,
77                                 int xOffSet, int yOffSet )
78 {
79     width = ( width > 0 ) ? width : m_pPosition->getWidth();
80     height = ( height > 0 ) ? height : m_pPosition->getHeight();
81
82     // Notify the layout
83     if( m_pLayout )
84     {
85         m_pLayout->onControlUpdate( *this, width, height, xOffSet, yOffSet );
86     }
87 }
88
89
90 void CtrlGeneric::notifyLayoutMaxSize( const Box *pImg1, const Box *pImg2 )
91 {
92     if( pImg1 == NULL )
93     {
94         if( pImg2 == NULL )
95         {
96             notifyLayout();
97         }
98         else
99         {
100             notifyLayout( pImg2->getWidth(), pImg2->getHeight() );
101         }
102     }
103     else
104     {
105         if( pImg2 == NULL )
106         {
107             notifyLayout( pImg1->getWidth(), pImg1->getHeight() );
108         }
109         else
110         {
111             notifyLayout( max( pImg1->getWidth(), pImg2->getWidth() ),
112                           max( pImg1->getHeight(), pImg2->getHeight() ) );
113         }
114     }
115 }
116
117
118 void CtrlGeneric::captureMouse() const
119 {
120     // Tell the layout we want to capture the mouse
121     if( m_pLayout )
122     {
123         m_pLayout->onControlCapture( *this );
124     }
125 }
126
127
128 void CtrlGeneric::releaseMouse() const
129 {
130     // Tell the layout we want to release the mouse
131     if( m_pLayout )
132     {
133         m_pLayout->onControlRelease( *this );
134     }
135 }
136
137
138 void CtrlGeneric::notifyTooltipChange() const
139 {
140     TopWindow *pWin = getWindow();
141     if( pWin )
142     {
143         // Notify the window
144         pWin->onTooltipChange( *this );
145     }
146 }
147
148
149 TopWindow *CtrlGeneric::getWindow() const
150 {
151     if( m_pLayout )
152     {
153         return m_pLayout->getWindow();
154     }
155     return NULL;
156 }
157
158
159 bool CtrlGeneric::isVisible() const
160 {
161     return !m_pVisible || m_pVisible->get();
162 }
163
164
165 void CtrlGeneric::onUpdate( Subject<VarBool> &rVariable, void *arg  )
166 {
167     // Is it the visibility variable ?
168     if( &rVariable == m_pVisible )
169     {
170         // Redraw the layout
171         notifyLayout();
172     }
173     else
174     {
175         // Call the user-defined callback
176         onVarBoolUpdate( (VarBool&)rVariable );
177     }
178 }
179