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"
34 WindowManager::WindowManager( intf_thread_t *pIntf ):
35 SkinObject( pIntf ), m_magnet( 0 ), m_alpha( 255 ), m_moveAlpha( 255 ),
36 m_opacityEnabled( false ), m_opacity( 255 ), m_direction( kNone ),
37 m_maximizeRect(0, 0, 50, 50), 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" );
44 // transparency switched on or off by user
45 m_opacityEnabled = var_InheritBool( getIntf(), "skins2-transparency" );
47 // opacity overridden by user
48 m_opacity = 255 * var_InheritFloat( getIntf(), "qt-opacity" );
52 WindowManager::~WindowManager()
58 void WindowManager::registerWindow( TopWindow &rWindow )
60 // Add the window to the set
61 m_allWindows.insert( &rWindow );
65 void WindowManager::unregisterWindow( TopWindow &rWindow )
67 // Erase every possible reference to the window
68 m_allWindows.erase( &rWindow );
69 m_movingWindows.erase( &rWindow );
70 m_dependencies.erase( &rWindow );
74 void WindowManager::startMove( TopWindow &rWindow )
76 // Rebuild the set of moving windows
77 m_movingWindows.clear();
78 buildDependSet( m_movingWindows, &rWindow );
80 if( isOpacityNeeded() )
82 // Change the opacity of the moving windows
83 WinSet_t::const_iterator it;
84 for( it = m_movingWindows.begin(); it != m_movingWindows.end(); ++it )
86 (*it)->setOpacity( m_moveAlpha );
92 void WindowManager::stopMove()
94 WinSet_t::const_iterator itWin1, itWin2;
95 AncList_t::const_iterator itAnc1, itAnc2;
97 if( isOpacityNeeded() )
99 // Restore the opacity of the moving windows
100 WinSet_t::const_iterator it;
101 for( it = m_movingWindows.begin(); it != m_movingWindows.end(); ++it )
103 (*it)->setOpacity( m_alpha );
107 // Delete the dependencies
108 m_dependencies.clear();
110 // Now we rebuild the dependencies.
111 // Iterate through all the windows
112 for( itWin1 = m_allWindows.begin(); itWin1 != m_allWindows.end(); ++itWin1 )
114 // Get the anchors of the layout associated to the window
115 const AncList_t &ancList1 =
116 (*itWin1)->getActiveLayout().getAnchorList();
118 // Iterate through all the windows, starting with (*itWin1)
119 for( itWin2 = itWin1; itWin2 != m_allWindows.end(); ++itWin2 )
121 // A window can't anchor itself...
122 if( (*itWin2) == (*itWin1) )
125 // Now, check for anchoring between the 2 windows
126 const AncList_t &ancList2 =
127 (*itWin2)->getActiveLayout().getAnchorList();
128 for( itAnc1 = ancList1.begin(); itAnc1 != ancList1.end(); ++itAnc1 )
130 for( itAnc2 = ancList2.begin();
131 itAnc2 != ancList2.end(); ++itAnc2 )
133 if( (*itAnc1)->isHanging( **itAnc2 ) )
135 // (*itWin1) anchors (*itWin2)
136 m_dependencies[*itWin1].insert( *itWin2 );
138 else if( (*itAnc2)->isHanging( **itAnc1 ) )
140 // (*itWin2) anchors (*itWin1)
141 m_dependencies[*itWin2].insert( *itWin1 );
150 void WindowManager::move( TopWindow &rWindow, int left, int top ) const
152 // Compute the real move offset
153 int xOffset = left - rWindow.getLeft();
154 int yOffset = top - rWindow.getTop();
156 // Check anchoring; this can change the values of xOffset and yOffset
157 checkAnchors( &rWindow, xOffset, yOffset );
159 // Move all the windows
160 WinSet_t::const_iterator it;
161 for( it = m_movingWindows.begin(); it != m_movingWindows.end(); ++it )
163 (*it)->move( (*it)->getLeft() + xOffset, (*it)->getTop() + yOffset );
168 void WindowManager::startResize( GenericLayout &rLayout, Direction_t direction )
170 m_direction = direction;
172 // Rebuild the set of moving windows.
173 // From the resized window, we only take into account the anchors which
174 // are mobile with the current type of resizing, and that are hanging a
175 // window. The hanged windows will come will all their dependencies.
177 m_resizeMovingE.clear();
178 m_resizeMovingS.clear();
179 m_resizeMovingSE.clear();
181 WinSet_t::const_iterator itWin;
182 AncList_t::const_iterator itAnc1, itAnc2;
183 // Get the anchors of the layout
184 const AncList_t &ancList1 = rLayout.getAnchorList();
186 // Iterate through all the hanged windows
187 for( itWin = m_dependencies[rLayout.getWindow()].begin();
188 itWin != m_dependencies[rLayout.getWindow()].end(); ++itWin )
190 // Now, check for anchoring between the 2 windows
191 const AncList_t &ancList2 =
192 (*itWin)->getActiveLayout().getAnchorList();
193 for( itAnc1 = ancList1.begin(); itAnc1 != ancList1.end(); ++itAnc1 )
195 for( itAnc2 = ancList2.begin();
196 itAnc2 != ancList2.end(); ++itAnc2 )
198 if( (*itAnc1)->isHanging( **itAnc2 ) )
200 // Add the dependencies of the hanged window to one of the
201 // lists of moving windows
202 Position::Ref_t aRefPos =
203 (*itAnc1)->getPosition().getRefLeftTop();
204 if( aRefPos == Position::kRightTop )
205 buildDependSet( m_resizeMovingE, *itWin );
206 else if( aRefPos == Position::kLeftBottom )
207 buildDependSet( m_resizeMovingS, *itWin );
208 else if( aRefPos == Position::kRightBottom )
209 buildDependSet( m_resizeMovingSE, *itWin );
216 // The checkAnchors() method will need to have m_movingWindows properly set
217 // so let's insert in it the contents of the other sets
218 m_movingWindows.clear();
219 m_movingWindows.insert( rLayout.getWindow() );
220 m_movingWindows.insert( m_resizeMovingE.begin(), m_resizeMovingE.end() );
221 m_movingWindows.insert( m_resizeMovingS.begin(), m_resizeMovingS.end() );
222 m_movingWindows.insert( m_resizeMovingSE.begin(), m_resizeMovingSE.end() );
226 void WindowManager::stopResize()
228 // Nothing different from stopMove(), luckily
233 void WindowManager::resize( GenericLayout &rLayout,
234 int width, int height ) const
236 // TODO: handle anchored windows
237 // Compute the real resizing offset
238 int xOffset = width - rLayout.getWidth();
239 int yOffset = height - rLayout.getHeight();
241 // Check anchoring; this can change the values of xOffset and yOffset
242 checkAnchors( rLayout.getWindow(), xOffset, yOffset );
243 if( m_direction == kResizeS )
245 if( m_direction == kResizeE )
248 int newWidth = rLayout.getWidth() + xOffset;
249 int newHeight = rLayout.getHeight() + yOffset;
252 if( newWidth < rLayout.getMinWidth() )
254 newWidth = rLayout.getMinWidth();
256 if( newWidth > rLayout.getMaxWidth() )
258 newWidth = rLayout.getMaxWidth();
260 if( newHeight < rLayout.getMinHeight() )
262 newHeight = rLayout.getMinHeight();
264 if( newHeight > rLayout.getMaxHeight() )
266 newHeight = rLayout.getMaxHeight();
269 if( newWidth == rLayout.getWidth() && newHeight == rLayout.getHeight() )
274 // New offset, after the last corrections
275 int xNewOffset = newWidth - rLayout.getWidth();
276 int yNewOffset = newHeight - rLayout.getHeight();
278 // Do the actual resizing
279 rLayout.resize( newWidth, newHeight );
281 // Move all the anchored windows
282 WinSet_t::const_iterator it;
283 if( m_direction == kResizeE ||
284 m_direction == kResizeSE )
286 for( it = m_resizeMovingE.begin(); it != m_resizeMovingE.end(); ++it )
288 (*it)->move( (*it)->getLeft() + xNewOffset,
292 if( m_direction == kResizeE ||
293 m_direction == kResizeSE )
295 for( it = m_resizeMovingS.begin(); it != m_resizeMovingS.end(); ++it )
297 (*it)->move( (*it)->getLeft(),
298 (*it)->getTop( )+ yNewOffset );
301 if( m_direction == kResizeE ||
302 m_direction == kResizeS ||
303 m_direction == kResizeSE )
305 for( it = m_resizeMovingSE.begin(); it != m_resizeMovingSE.end(); ++it )
307 (*it)->move( (*it)->getLeft() + xNewOffset,
308 (*it)->getTop() + yNewOffset );
314 void WindowManager::maximize( TopWindow &rWindow )
316 // Save the current position/size of the window, to be able to restore it
317 m_maximizeRect = SkinsRect( rWindow.getLeft(), rWindow.getTop(),
318 rWindow.getLeft() + rWindow.getWidth(),
319 rWindow.getTop() + rWindow.getHeight() );
321 SkinsRect workArea = OSFactory::instance( getIntf() )->getWorkArea();
323 startMove( rWindow );
324 move( rWindow, workArea.getLeft(), workArea.getTop() );
327 // FIXME: Ugly const_cast
328 GenericLayout &rLayout = (GenericLayout&)rWindow.getActiveLayout();
329 startResize( rLayout, kResizeSE );
330 resize( rLayout, workArea.getWidth(), workArea.getHeight() );
332 rWindow.m_pVarMaximized->set( true );
334 // Make the window unmovable by unregistering it
335 // unregisterWindow( rWindow );
339 void WindowManager::unmaximize( TopWindow &rWindow )
341 // Register the window to allow moving it
342 // registerWindow( rWindow );
345 // FIXME: Ugly const_cast
346 GenericLayout &rLayout = (GenericLayout&)rWindow.getActiveLayout();
347 startResize( rLayout, kResizeSE );
348 resize( rLayout, m_maximizeRect.getWidth(), m_maximizeRect.getHeight() );
351 startMove( rWindow );
352 move( rWindow, m_maximizeRect.getLeft(), m_maximizeRect.getTop() );
354 rWindow.m_pVarMaximized->set( false );
358 void WindowManager::synchVisibility() const
360 WinSet_t::const_iterator it;
361 for( it = m_allWindows.begin(); it != m_allWindows.end(); ++it )
363 // Show the window if it has to be visible
364 if( (*it)->getVisibleVar().get() )
372 void WindowManager::saveVisibility()
374 WinSet_t::const_iterator it;
375 m_savedWindows.clear();
376 for( it = m_allWindows.begin(); it != m_allWindows.end(); ++it )
378 // Remember the window if it is visible
379 if( (*it)->getVisibleVar().get() )
381 m_savedWindows.insert( *it );
387 void WindowManager::restoreVisibility() const
389 // Warning in case we never called saveVisibility()
390 if( m_savedWindows.size() == 0 )
392 msg_Warn( getIntf(), "restoring visibility for no window" );
395 WinSet_t::const_iterator it;
396 for( it = m_savedWindows.begin(); it != m_savedWindows.end(); ++it )
403 void WindowManager::raiseAll() const
405 // Raise all the windows
406 WinSet_t::const_iterator it;
407 for( it = m_allWindows.begin(); it != m_allWindows.end(); ++it )
414 void WindowManager::showAll( bool firstTime ) const
416 // Show all the windows
417 WinSet_t::const_iterator it;
418 for( it = m_allWindows.begin(); it != m_allWindows.end(); ++it )
420 // When the theme is opened for the first time,
421 // only show the window if set as visible in the XML
422 if( (*it)->getInitialVisibility() || !firstTime )
430 void WindowManager::show( TopWindow &rWindow ) const
434 if( isOpacityNeeded() )
435 rWindow.setOpacity( m_alpha );
439 void WindowManager::hideAll() const
441 WinSet_t::const_iterator it;
442 for( it = m_allWindows.begin(); it != m_allWindows.end(); ++it )
449 void WindowManager::setOnTop( bool b_ontop )
451 // Update the boolean variable
452 VarBoolImpl *pVarOnTop = (VarBoolImpl*)m_cVarOnTop.get();
453 pVarOnTop->set( b_ontop );
455 // set/unset the "on top" status
456 WinSet_t::const_iterator it;
457 for( it = m_allWindows.begin(); it != m_allWindows.end(); ++it )
459 (*it)->toggleOnTop( b_ontop );
464 void WindowManager::toggleOnTop()
466 VarBoolImpl *pVarOnTop = (VarBoolImpl*)m_cVarOnTop.get();
468 setOnTop( !pVarOnTop->get() );
472 void WindowManager::buildDependSet( WinSet_t &rWinSet,
475 // pWindow is in the set
476 rWinSet.insert( pWindow );
478 // Iterate through the anchored windows
479 const WinSet_t &anchored = m_dependencies[pWindow];
480 WinSet_t::const_iterator iter;
481 for( iter = anchored.begin(); iter != anchored.end(); ++iter )
483 // Check that the window isn't already in the set before adding it
484 if( rWinSet.find( *iter ) == rWinSet.end() )
486 buildDependSet( rWinSet, *iter );
492 void WindowManager::checkAnchors( TopWindow *pWindow,
493 int &xOffset, int &yOffset ) const
496 WinSet_t::const_iterator itMov, itSta;
497 AncList_t::const_iterator itAncMov, itAncSta;
499 // Check magnetism with screen edges first (actually it is the work area)
500 SkinsRect workArea = OSFactory::instance( getIntf() )->getWorkArea();
501 // Iterate through the moving windows
502 for( itMov = m_movingWindows.begin();
503 itMov != m_movingWindows.end(); ++itMov )
505 // Skip the invisible windows
506 if( ! (*itMov)->getVisibleVar().get() )
511 int newLeft = (*itMov)->getLeft() + xOffset;
512 int newTop = (*itMov)->getTop() + yOffset;
513 if( newLeft > workArea.getLeft() - m_magnet &&
514 newLeft < workArea.getLeft() + m_magnet )
516 xOffset = workArea.getLeft() - (*itMov)->getLeft();
518 if( newTop > workArea.getTop() - m_magnet &&
519 newTop < workArea.getTop() + m_magnet )
521 yOffset = workArea.getTop() - (*itMov)->getTop();
523 int right = workArea.getLeft() + workArea.getWidth();
524 if( newLeft + (*itMov)->getWidth() > right - m_magnet &&
525 newLeft + (*itMov)->getWidth() < right + m_magnet )
527 xOffset = right - (*itMov)->getLeft() - (*itMov)->getWidth();
529 int bottom = workArea.getTop() + workArea.getHeight();
530 if( newTop + (*itMov)->getHeight() > bottom - m_magnet &&
531 newTop + (*itMov)->getHeight() < bottom + m_magnet )
533 yOffset = bottom - (*itMov)->getTop() - (*itMov)->getHeight();
537 // Iterate through the moving windows
538 for( itMov = m_movingWindows.begin();
539 itMov != m_movingWindows.end(); ++itMov )
541 // Skip the invisible windows
542 if( ! (*itMov)->getVisibleVar().get() )
547 // Get the anchors in the main layout of this moving window
548 const AncList_t &movAnchors =
549 (*itMov)->getActiveLayout().getAnchorList();
551 // Iterate through the static windows
552 for( itSta = m_allWindows.begin();
553 itSta != m_allWindows.end(); ++itSta )
555 // Skip the moving windows and the invisible ones
556 if( m_movingWindows.find( (*itSta) ) != m_movingWindows.end() ||
557 ! (*itSta)->getVisibleVar().get() )
562 // Get the anchors in the main layout of this static window
563 const AncList_t &staAnchors =
564 (*itSta)->getActiveLayout().getAnchorList();
566 // Check if there is an anchoring between one of the movAnchors
567 // and one of the staAnchors
568 for( itAncMov = movAnchors.begin();
569 itAncMov != movAnchors.end(); ++itAncMov )
571 for( itAncSta = staAnchors.begin();
572 itAncSta != staAnchors.end(); ++itAncSta )
574 if( (*itAncSta)->canHang( **itAncMov, xOffset, yOffset ) )
576 // We have found an anchoring!
577 // There is nothing to do here, since xOffset and
578 // yOffset are automatically modified by canHang()
580 // Don't check the other anchors, one is enough...
585 // Temporary variables
586 int xOffsetTemp = -xOffset;
587 int yOffsetTemp = -yOffset;
588 if( (*itAncMov)->canHang( **itAncSta, xOffsetTemp,
591 // We have found an anchoring!
592 // xOffsetTemp and yOffsetTemp have been updated,
593 // we just need to change xOffset and yOffset
594 xOffset = -xOffsetTemp;
595 yOffset = -yOffsetTemp;
597 // Don't check the other anchors, one is enough...
608 void WindowManager::createTooltip( const GenericFont &rTipFont )
610 // Create the tooltip window
613 m_pTooltip = new Tooltip( getIntf(), rTipFont, 500 );
617 msg_Warn( getIntf(), "tooltip already created!" );
622 void WindowManager::showTooltip()
631 void WindowManager::hideTooltip()
640 void WindowManager::addLayout( TopWindow &rWindow, GenericLayout &rLayout )
642 rWindow.setActiveLayout( &rLayout );
646 void WindowManager::setActiveLayout( TopWindow &rWindow,
647 GenericLayout &rLayout )
649 rWindow.setActiveLayout( &rLayout );
650 // Rebuild the dependencies