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 "var_manager.hpp"
32 #include "../utils/position.hpp"
35 WindowManager::WindowManager( intf_thread_t *pIntf ):
36 SkinObject( pIntf ), m_magnet( 0 ), m_direction( kNone ),
37 m_pTooltip( NULL ), m_pPopup( NULL )
39 // Create and register a variable for the "on top" status
40 VarManager *pVarManager = VarManager::instance( getIntf() );
41 m_cVarOnTop = VariablePtr( new VarBoolImpl( getIntf() ) );
42 pVarManager->registerVar( m_cVarOnTop, "vlc.isOnTop" );
46 WindowManager::~WindowManager()
52 void WindowManager::registerWindow( TopWindow &rWindow )
54 // Add the window to the set
55 m_allWindows.insert( &rWindow );
59 void WindowManager::unregisterWindow( TopWindow &rWindow )
61 // Erase every possible reference to the window
62 m_allWindows.erase( &rWindow );
63 m_movingWindows.erase( &rWindow );
64 m_dependencies.erase( &rWindow );
68 void WindowManager::startMove( TopWindow &rWindow )
70 // Rebuild the set of moving windows
71 m_movingWindows.clear();
72 buildDependSet( m_movingWindows, &rWindow );
75 if( config_GetInt( getIntf(), "skins2-transparency" ) )
77 // Change the opacity of the moving windows
78 WinSet_t::const_iterator it;
79 for( it = m_movingWindows.begin(); it != m_movingWindows.end(); it++ )
81 (*it)->setOpacity( m_moveAlpha );
84 // FIXME: We need to refresh the windows, because if 2 windows overlap
85 // and one of them becomes transparent, the other one is not refreshed
86 // automatically. I don't know why... -- Ipkiss
87 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
89 (*it)->refresh( 0, 0, (*it)->getWidth(), (*it)->getHeight() );
96 void WindowManager::stopMove()
98 WinSet_t::const_iterator itWin1, itWin2;
99 AncList_t::const_iterator itAnc1, itAnc2;
102 if( config_GetInt( getIntf(), "skins2-transparency" ) )
104 // Restore the opacity of the moving windows
105 WinSet_t::const_iterator it;
106 for( it = m_movingWindows.begin(); it != m_movingWindows.end(); it++ )
108 (*it)->setOpacity( m_alpha );
113 // Delete the dependencies
114 m_dependencies.clear();
116 // Now we rebuild the dependencies.
117 // Iterate through all the windows
118 for( itWin1 = m_allWindows.begin(); itWin1 != m_allWindows.end(); itWin1++ )
120 // Get the anchors of the layout associated to the window
121 const AncList_t &ancList1 =
122 (*itWin1)->getActiveLayout().getAnchorList();
124 // Iterate through all the windows, starting with (*itWin1)
125 for( itWin2 = itWin1; itWin2 != m_allWindows.end(); itWin2++ )
127 // A window can't anchor itself...
128 if( (*itWin2) == (*itWin1) )
131 // Now, check for anchoring between the 2 windows
132 const AncList_t &ancList2 =
133 (*itWin2)->getActiveLayout().getAnchorList();
134 for( itAnc1 = ancList1.begin(); itAnc1 != ancList1.end(); itAnc1++ )
136 for( itAnc2 = ancList2.begin();
137 itAnc2 != ancList2.end(); itAnc2++ )
139 if( (*itAnc1)->isHanging( **itAnc2 ) )
141 // (*itWin1) anchors (*itWin2)
142 m_dependencies[*itWin1].insert( *itWin2 );
144 else if( (*itAnc2)->isHanging( **itAnc1 ) )
146 // (*itWin2) anchors (*itWin1)
147 m_dependencies[*itWin2].insert( *itWin1 );
156 void WindowManager::move( TopWindow &rWindow, int left, int top ) const
158 // Compute the real move offset
159 int xOffset = left - rWindow.getLeft();
160 int yOffset = top - rWindow.getTop();
162 // Check anchoring; this can change the values of xOffset and yOffset
163 checkAnchors( &rWindow, xOffset, yOffset );
165 // Move all the windows
166 WinSet_t::const_iterator it;
167 for( it = m_movingWindows.begin(); it != m_movingWindows.end(); it++ )
169 (*it)->move( (*it)->getLeft() + xOffset, (*it)->getTop() + yOffset );
174 void WindowManager::startResize( GenericLayout &rLayout, Direction_t direction )
176 m_direction = direction;
178 // Rebuild the set of moving windows.
179 // From the resized window, we only take into account the anchors which
180 // are mobile with the current type of resizing, and that are hanging a
181 // window. The hanged windows will come will all their dependencies.
183 m_resizeMovingE.clear();
184 m_resizeMovingS.clear();
185 m_resizeMovingSE.clear();
187 WinSet_t::const_iterator itWin;
188 AncList_t::const_iterator itAnc1, itAnc2;
189 // Get the anchors of the layout
190 const AncList_t &ancList1 = rLayout.getAnchorList();
192 // Iterate through all the hanged windows
193 for( itWin = m_dependencies[rLayout.getWindow()].begin();
194 itWin != m_dependencies[rLayout.getWindow()].end(); itWin++ )
196 // Now, check for anchoring between the 2 windows
197 const AncList_t &ancList2 =
198 (*itWin)->getActiveLayout().getAnchorList();
199 for( itAnc1 = ancList1.begin(); itAnc1 != ancList1.end(); itAnc1++ )
201 for( itAnc2 = ancList2.begin();
202 itAnc2 != ancList2.end(); itAnc2++ )
204 if( (*itAnc1)->isHanging( **itAnc2 ) )
206 // Add the dependencies of the hanged window to one of the
207 // lists of moving windows
208 Position::Ref_t aRefPos =
209 (*itAnc1)->getPosition().getRefLeftTop();
210 if( aRefPos == Position::kRightTop )
211 buildDependSet( m_resizeMovingE, *itWin );
212 else if( aRefPos == Position::kLeftBottom )
213 buildDependSet( m_resizeMovingS, *itWin );
214 else if( aRefPos == Position::kRightBottom )
215 buildDependSet( m_resizeMovingSE, *itWin );
222 // The checkAnchors() method will need to have m_movingWindows properly set
223 // so let's insert in it the contents of the other sets
224 m_movingWindows.clear();
225 m_movingWindows.insert( rLayout.getWindow() );
226 m_movingWindows.insert( m_resizeMovingE.begin(), m_resizeMovingE.end() );
227 m_movingWindows.insert( m_resizeMovingS.begin(), m_resizeMovingS.end() );
228 m_movingWindows.insert( m_resizeMovingSE.begin(), m_resizeMovingSE.end() );
232 void WindowManager::stopResize()
234 // Nothing different from stopMove(), luckily
239 void WindowManager::resize( GenericLayout &rLayout,
240 int width, int height ) const
242 // TODO: handle anchored windows
243 // Compute the real resizing offset
244 int xOffset = width - rLayout.getWidth();
245 int yOffset = height - rLayout.getHeight();
247 // Check anchoring; this can change the values of xOffset and yOffset
248 checkAnchors( rLayout.getWindow(), xOffset, yOffset );
249 if( m_direction == kResizeS )
251 if( m_direction == kResizeE )
254 int newWidth = rLayout.getWidth() + xOffset;
255 int newHeight = rLayout.getHeight() + yOffset;
258 if( newWidth < rLayout.getMinWidth() )
260 newWidth = rLayout.getMinWidth();
262 if( newWidth > rLayout.getMaxWidth() )
264 newWidth = rLayout.getMaxWidth();
266 if( newHeight < rLayout.getMinHeight() )
268 newHeight = rLayout.getMinHeight();
270 if( newHeight > rLayout.getMaxHeight() )
272 newHeight = rLayout.getMaxHeight();
275 if( newWidth == rLayout.getWidth() && newHeight == rLayout.getHeight() )
280 // New offset, after the last corrections
281 int xNewOffset = newWidth - rLayout.getWidth();
282 int yNewOffset = newHeight - rLayout.getHeight();
284 // Do the actual resizing
285 rLayout.resize( newWidth, newHeight );
287 // Move all the anchored windows
288 WinSet_t::const_iterator it;
289 if( m_direction == kResizeE ||
290 m_direction == kResizeSE )
292 for( it = m_resizeMovingE.begin(); it != m_resizeMovingE.end(); it++ )
294 (*it)->move( (*it)->getLeft() + xNewOffset,
298 if( m_direction == kResizeE ||
299 m_direction == kResizeSE )
301 for( it = m_resizeMovingS.begin(); it != m_resizeMovingS.end(); it++ )
303 (*it)->move( (*it)->getLeft(),
304 (*it)->getTop( )+ yNewOffset );
307 if( m_direction == kResizeE ||
308 m_direction == kResizeS ||
309 m_direction == kResizeSE )
311 for( it = m_resizeMovingSE.begin(); it != m_resizeMovingSE.end(); it++ )
313 (*it)->move( (*it)->getLeft() + xNewOffset,
314 (*it)->getTop() + yNewOffset );
320 void WindowManager::synchVisibility() const
322 WinSet_t::const_iterator it;
323 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
325 // Show the window if it has to be visible
326 if( (*it)->getVisibleVar().get() )
334 void WindowManager::saveVisibility()
336 WinSet_t::const_iterator it;
337 m_savedWindows.clear();
338 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
340 // Remember the window if it is visible
341 if( (*it)->getVisibleVar().get() )
343 m_savedWindows.insert( *it );
349 void WindowManager::restoreVisibility() const
351 // Warning in case we never called saveVisibility()
352 if( m_savedWindows.size() == 0 )
354 msg_Warn( getIntf(), "restoring visibility for no window" );
357 WinSet_t::const_iterator it;
358 for( it = m_savedWindows.begin(); it != m_savedWindows.end(); it++)
365 void WindowManager::raiseAll() const
367 // Raise all the windows
368 WinSet_t::const_iterator it;
369 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
376 void WindowManager::showAll( bool firstTime ) const
378 // Show all the windows
379 WinSet_t::const_iterator it;
380 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
382 // When the theme is opened for the first time,
383 // only show the window if set as visible in the XML
384 if( (*it)->isVisible() || !firstTime )
388 (*it)->setOpacity( m_alpha );
393 void WindowManager::hideAll() const
395 WinSet_t::const_iterator it;
396 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
403 void WindowManager::toggleOnTop()
405 // Update the boolean variable
406 VarBoolImpl *pVarOnTop = (VarBoolImpl*)m_cVarOnTop.get();
407 pVarOnTop->set( !pVarOnTop->get() );
409 // Toggle the "on top" status
410 WinSet_t::const_iterator it;
411 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
413 (*it)->toggleOnTop( pVarOnTop->get() );
418 void WindowManager::buildDependSet( WinSet_t &rWinSet,
421 // pWindow is in the set
422 rWinSet.insert( pWindow );
424 // Iterate through the anchored windows
425 const WinSet_t &anchored = m_dependencies[pWindow];
426 WinSet_t::const_iterator iter;
427 for( iter = anchored.begin(); iter != anchored.end(); iter++ )
429 // Check that the window isn't already in the set before adding it
430 if( rWinSet.find( *iter ) == rWinSet.end() )
432 buildDependSet( rWinSet, *iter );
438 void WindowManager::checkAnchors( TopWindow *pWindow,
439 int &xOffset, int &yOffset ) const
441 WinSet_t::const_iterator itMov, itSta;
442 AncList_t::const_iterator itAncMov, itAncSta;
444 // Check magnetism with screen edges first (actually it is the work area)
445 Rect workArea = OSFactory::instance( getIntf() )->getWorkArea();
446 // Iterate through the moving windows
447 for( itMov = m_movingWindows.begin();
448 itMov != m_movingWindows.end(); itMov++ )
450 // Skip the invisible windows
451 if( ! (*itMov)->getVisibleVar().get() )
456 int newLeft = (*itMov)->getLeft() + xOffset;
457 int newTop = (*itMov)->getTop() + yOffset;
458 if( newLeft > workArea.getLeft() - m_magnet &&
459 newLeft < workArea.getLeft() + m_magnet )
461 xOffset = workArea.getLeft() - (*itMov)->getLeft();
463 if( newTop > workArea.getTop() - m_magnet &&
464 newTop < workArea.getTop() + m_magnet )
466 yOffset = workArea.getTop() - (*itMov)->getTop();
468 if( newLeft + (*itMov)->getWidth() > workArea.getRight() - m_magnet &&
469 newLeft + (*itMov)->getWidth() < workArea.getRight() + m_magnet )
471 xOffset = workArea.getRight() - (*itMov)->getLeft()
472 - (*itMov)->getWidth();
474 if( newTop + (*itMov)->getHeight() > workArea.getBottom() - m_magnet &&
475 newTop + (*itMov)->getHeight() < workArea.getBottom() + m_magnet )
477 yOffset = workArea.getBottom() - (*itMov)->getTop()
478 - (*itMov)->getHeight();
482 // Iterate through the moving windows
483 for( itMov = m_movingWindows.begin();
484 itMov != m_movingWindows.end(); itMov++ )
486 // Skip the invisible windows
487 if( ! (*itMov)->getVisibleVar().get() )
492 // Get the anchors in the main layout of this moving window
493 const AncList_t &movAnchors =
494 (*itMov)->getActiveLayout().getAnchorList();
496 // Iterate through the static windows
497 for( itSta = m_allWindows.begin();
498 itSta != m_allWindows.end(); itSta++ )
500 // Skip the moving windows and the invisible ones
501 if( m_movingWindows.find( (*itSta) ) != m_movingWindows.end() ||
502 ! (*itSta)->getVisibleVar().get() )
507 // Get the anchors in the main layout of this static window
508 const AncList_t &staAnchors =
509 (*itSta)->getActiveLayout().getAnchorList();
511 // Check if there is an anchoring between one of the movAnchors
512 // and one of the staAnchors
513 for( itAncMov = movAnchors.begin();
514 itAncMov != movAnchors.end(); itAncMov++ )
516 for( itAncSta = staAnchors.begin();
517 itAncSta != staAnchors.end(); itAncSta++ )
519 if( (*itAncSta)->canHang( **itAncMov, xOffset, yOffset ) )
521 // We have found an anchoring!
522 // There is nothing to do here, since xOffset and
523 // yOffset are automatically modified by canHang()
525 // Don't check the other anchors, one is enough...
530 // Temporary variables
531 int xOffsetTemp = -xOffset;
532 int yOffsetTemp = -yOffset;
533 if( (*itAncMov)->canHang( **itAncSta, xOffsetTemp,
536 // We have found an anchoring!
537 // xOffsetTemp and yOffsetTemp have been updated,
538 // we just need to change xOffset and yOffset
539 xOffset = -xOffsetTemp;
540 yOffset = -yOffsetTemp;
542 // Don't check the other anchors, one is enough...
553 void WindowManager::createTooltip( const GenericFont &rTipFont )
555 // Create the tooltip window
558 m_pTooltip = new Tooltip( getIntf(), rTipFont, 500 );
562 msg_Warn( getIntf(), "tooltip already created!" );
567 void WindowManager::showTooltip()
576 void WindowManager::hideTooltip()
585 void WindowManager::addLayout( TopWindow &rWindow, GenericLayout &rLayout )
587 rWindow.setActiveLayout( &rLayout );
591 void WindowManager::setActiveLayout( TopWindow &rWindow,
592 GenericLayout &rLayout )
594 rWindow.setActiveLayout( &rLayout );
595 // Rebuild the dependencies