1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
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.
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.
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 *****************************************************************************/
25 #include "window_manager.hpp"
26 #include "generic_layout.hpp"
27 #include "generic_window.hpp"
28 #include "os_factory.hpp"
30 #include "tooltip.hpp"
31 #include "../utils/position.hpp"
32 #include "../src/var_manager.hpp"
35 WindowManager::WindowManager( intf_thread_t *pIntf ):
36 SkinObject( pIntf ), m_magnet( 0 ), m_pTooltip( NULL ), m_pPopup( NULL )
38 // Create and register a variable for the "on top" status
39 VarManager *pVarManager = VarManager::instance( getIntf() );
40 m_cVarOnTop = VariablePtr( new VarBoolImpl( getIntf() ) );
41 pVarManager->registerVar( m_cVarOnTop, "vlc.isOnTop" );
45 WindowManager::~WindowManager()
51 void WindowManager::registerWindow( TopWindow &rWindow )
53 // Add the window to the set
54 m_allWindows.insert( &rWindow );
58 void WindowManager::unregisterWindow( TopWindow &rWindow )
60 // Erase every possible reference to the window
61 m_allWindows.erase( &rWindow );
62 m_movingWindows.erase( &rWindow );
63 m_dependencies.erase( &rWindow );
67 void WindowManager::startMove( TopWindow &rWindow )
69 // Rebuild the set of moving windows
70 m_movingWindows.clear();
71 buildDependSet( m_movingWindows, &rWindow );
74 if( config_GetInt( getIntf(), "skins2-transparency" ) )
76 // Change the opacity of the moving windows
77 WinSet_t::const_iterator it;
78 for( it = m_movingWindows.begin(); it != m_movingWindows.end(); it++ )
80 (*it)->setOpacity( m_moveAlpha );
83 // FIXME: We need to refresh the windows, because if 2 windows overlap
84 // and one of them becomes transparent, the other one is not refreshed
85 // automatically. I don't know why... -- Ipkiss
86 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
88 (*it)->refresh( 0, 0, (*it)->getWidth(), (*it)->getHeight() );
95 void WindowManager::stopMove()
97 WinSet_t::const_iterator itWin1, itWin2;
98 AncList_t::const_iterator itAnc1, itAnc2;
101 if( config_GetInt( getIntf(), "skins2-transparency" ) )
103 // Restore the opacity of the moving windows
104 WinSet_t::const_iterator it;
105 for( it = m_movingWindows.begin(); it != m_movingWindows.end(); it++ )
107 (*it)->setOpacity( m_alpha );
112 // Delete the dependencies
113 m_dependencies.clear();
115 // Now we rebuild the dependencies.
116 // Iterate through all the windows
117 for( itWin1 = m_allWindows.begin(); itWin1 != m_allWindows.end(); itWin1++ )
119 // Get the anchors of the layout associated to the window
120 const AncList_t &ancList1 =
121 (*itWin1)->getActiveLayout().getAnchorList();
123 // Iterate through all the windows, starting with (*itWin1)
124 for( itWin2 = itWin1; itWin2 != m_allWindows.end(); itWin2++ )
126 // A window can't anchor itself...
127 if( (*itWin2) == (*itWin1) )
130 // Now, check for anchoring between the 2 windows
131 const AncList_t &ancList2 =
132 (*itWin2)->getActiveLayout().getAnchorList();
133 for( itAnc1 = ancList1.begin(); itAnc1 != ancList1.end(); itAnc1++ )
135 for( itAnc2 = ancList2.begin();
136 itAnc2 != ancList2.end(); itAnc2++ )
138 if( (*itAnc1)->isHanging( **itAnc2 ) )
140 // (*itWin1) anchors (*itWin2)
141 m_dependencies[*itWin1].insert( *itWin2 );
143 else if( (*itAnc2)->isHanging( **itAnc1 ) )
145 // (*itWin2) anchors (*itWin1)
146 m_dependencies[*itWin2].insert( *itWin1 );
155 void WindowManager::move( TopWindow &rWindow, int left, int top ) const
157 // Compute the real move offset
158 int xOffset = left - rWindow.getLeft();
159 int yOffset = top - rWindow.getTop();
161 // Check anchoring; this can change the values of xOffset and yOffset
162 checkAnchors( &rWindow, xOffset, yOffset );
164 // Move all the windows
165 WinSet_t::const_iterator it;
166 for( it = m_movingWindows.begin(); it != m_movingWindows.end(); it++ )
168 (*it)->move( (*it)->getLeft() + xOffset, (*it)->getTop() + yOffset );
173 void WindowManager::synchVisibility() const
175 WinSet_t::const_iterator it;
176 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
178 // Show the window if it has to be visible
179 if( (*it)->getVisibleVar().get() )
187 void WindowManager::raiseAll() const
189 // Raise all the windows
190 WinSet_t::const_iterator it;
191 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
198 void WindowManager::showAll( bool firstTime ) const
200 // Show all the windows
201 WinSet_t::const_iterator it;
202 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
204 // When the theme is opened for the first time,
205 // only show the window if set as visible in the XML
206 if ((*it)->isVisible() || !firstTime)
210 (*it)->setOpacity( m_alpha );
215 void WindowManager::hideAll() const
217 WinSet_t::const_iterator it;
218 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
225 void WindowManager::toggleOnTop()
227 // Update the boolean variable
228 VarBoolImpl *pVarOnTop = (VarBoolImpl*)m_cVarOnTop.get();
229 pVarOnTop->set( !pVarOnTop->get() );
231 // Toggle the "on top" status
232 WinSet_t::const_iterator it;
233 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
235 (*it)->toggleOnTop( pVarOnTop->get() );
240 void WindowManager::buildDependSet( WinSet_t &rWinSet,
243 // pWindow is in the set
244 rWinSet.insert( pWindow );
246 // Iterate through the anchored windows
247 const WinSet_t &anchored = m_dependencies[pWindow];
248 WinSet_t::const_iterator iter;
249 for( iter = anchored.begin(); iter != anchored.end(); iter++ )
251 // Check that the window isn't already in the set before adding it
252 if( rWinSet.find( *iter ) == rWinSet.end() )
254 buildDependSet( rWinSet, *iter );
260 void WindowManager::checkAnchors( TopWindow *pWindow,
261 int &xOffset, int &yOffset ) const
263 WinSet_t::const_iterator itMov, itSta;
264 AncList_t::const_iterator itAncMov, itAncSta;
266 // Check magnetism with screen edges first (actually it is the work area)
267 Rect workArea = OSFactory::instance( getIntf() )->getWorkArea();
268 // Iterate through the moving windows
269 for( itMov = m_movingWindows.begin();
270 itMov != m_movingWindows.end(); itMov++ )
272 // Skip the invisible windows
273 if( ! (*itMov)->getVisibleVar().get() )
278 int newLeft = (*itMov)->getLeft() + xOffset;
279 int newTop = (*itMov)->getTop() + yOffset;
280 if( newLeft > workArea.getLeft() - m_magnet &&
281 newLeft < workArea.getLeft() + m_magnet )
283 xOffset = workArea.getLeft() - (*itMov)->getLeft();
285 if( newTop > workArea.getTop() - m_magnet &&
286 newTop < workArea.getTop() + m_magnet )
288 yOffset = workArea.getTop() - (*itMov)->getTop();
290 if( newLeft + (*itMov)->getWidth() > workArea.getRight() - m_magnet &&
291 newLeft + (*itMov)->getWidth() < workArea.getRight() + m_magnet )
293 xOffset = workArea.getRight() - (*itMov)->getLeft()
294 - (*itMov)->getWidth();
296 if( newTop + (*itMov)->getHeight() > workArea.getBottom() - m_magnet &&
297 newTop + (*itMov)->getHeight() < workArea.getBottom() + m_magnet )
299 yOffset = workArea.getBottom() - (*itMov)->getTop()
300 - (*itMov)->getHeight();
304 // Iterate through the moving windows
305 for( itMov = m_movingWindows.begin();
306 itMov != m_movingWindows.end(); itMov++ )
308 // Skip the invisible windows
309 if( ! (*itMov)->getVisibleVar().get() )
314 // Get the anchors in the main layout of this moving window
315 const AncList_t &movAnchors =
316 (*itMov)->getActiveLayout().getAnchorList();
318 // Iterate through the static windows
319 for( itSta = m_allWindows.begin();
320 itSta != m_allWindows.end(); itSta++ )
322 // Skip the moving windows and the invisible ones
323 if( m_movingWindows.find( (*itSta) ) != m_movingWindows.end() ||
324 ! (*itSta)->getVisibleVar().get() )
329 // Get the anchors in the main layout of this static window
330 const AncList_t &staAnchors =
331 (*itSta)->getActiveLayout().getAnchorList();
333 // Check if there is an anchoring between one of the movAnchors
334 // and one of the staAnchors
335 for( itAncMov = movAnchors.begin();
336 itAncMov != movAnchors.end(); itAncMov++ )
338 for( itAncSta = staAnchors.begin();
339 itAncSta != staAnchors.end(); itAncSta++ )
341 if( (*itAncSta)->canHang( **itAncMov, xOffset, yOffset ) )
343 // We have found an anchoring!
344 // There is nothing to do here, since xOffset and
345 // yOffset are automatically modified by canHang()
347 // Don't check the other anchors, one is enough...
352 // Temporary variables
353 int xOffsetTemp = -xOffset;
354 int yOffsetTemp = -yOffset;
355 if( (*itAncMov)->canHang( **itAncSta, xOffsetTemp,
358 // We have found an anchoring!
359 // xOffsetTemp and yOffsetTemp have been updated,
360 // we just need to change xOffset and yOffset
361 xOffset = -xOffsetTemp;
362 yOffset = -yOffsetTemp;
364 // Don't check the other anchors, one is enough...
375 void WindowManager::createTooltip( const GenericFont &rTipFont )
377 // Create the tooltip window
380 m_pTooltip = new Tooltip( getIntf(), rTipFont, 500 );
384 msg_Warn( getIntf(), "Tooltip already created!");
389 void WindowManager::showTooltip()
398 void WindowManager::hideTooltip()
407 void WindowManager::addLayout( TopWindow &rWindow, GenericLayout &rLayout )
409 rWindow.setActiveLayout( &rLayout );
413 void WindowManager::setActiveLayout( TopWindow &rWindow,
414 GenericLayout &rLayout )
416 rWindow.setActiveLayout( &rLayout );
417 // Rebuild the dependencies