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_direction( kNone ),
36 m_maximizeRect(0, 0, 50, 50),
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::maximize( TopWindow &rWindow )
322 // Save the current position/size of the window, to be able to restore it
323 m_maximizeRect = Rect( rWindow.getLeft(), rWindow.getTop(),
324 rWindow.getLeft() + rWindow.getWidth(),
325 rWindow.getTop() + rWindow.getHeight() );
327 Rect workArea = OSFactory::instance( getIntf() )->getWorkArea();
329 startMove( rWindow );
330 move( rWindow, workArea.getLeft(), workArea.getTop() );
333 // FIXME: Ugly const_cast
334 GenericLayout &rLayout = (GenericLayout&)rWindow.getActiveLayout();
335 startResize( rLayout, kResizeSE );
336 resize( rLayout, workArea.getWidth(), workArea.getHeight() );
338 rWindow.m_pVarMaximized->set( true );
340 // Make the window unmovable by unregistering it
341 // unregisterWindow( rWindow );
345 void WindowManager::unmaximize( TopWindow &rWindow )
347 // Register the window to allow moving it
348 // registerWindow( rWindow );
351 // FIXME: Ugly const_cast
352 GenericLayout &rLayout = (GenericLayout&)rWindow.getActiveLayout();
353 startResize( rLayout, kResizeSE );
354 resize( rLayout, m_maximizeRect.getWidth(), m_maximizeRect.getHeight() );
357 startMove( rWindow );
358 move( rWindow, m_maximizeRect.getLeft(), m_maximizeRect.getTop() );
360 rWindow.m_pVarMaximized->set( false );
364 void WindowManager::synchVisibility() const
366 WinSet_t::const_iterator it;
367 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
369 // Show the window if it has to be visible
370 if( (*it)->getVisibleVar().get() )
378 void WindowManager::saveVisibility()
380 WinSet_t::const_iterator it;
381 m_savedWindows.clear();
382 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
384 // Remember the window if it is visible
385 if( (*it)->getVisibleVar().get() )
387 m_savedWindows.insert( *it );
393 void WindowManager::restoreVisibility() const
395 // Warning in case we never called saveVisibility()
396 if( m_savedWindows.size() == 0 )
398 msg_Warn( getIntf(), "restoring visibility for no window" );
401 WinSet_t::const_iterator it;
402 for( it = m_savedWindows.begin(); it != m_savedWindows.end(); it++)
409 void WindowManager::raiseAll() const
411 // Raise all the windows
412 WinSet_t::const_iterator it;
413 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
420 void WindowManager::showAll( bool firstTime ) const
422 // Show all the windows
423 WinSet_t::const_iterator it;
424 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
426 // When the theme is opened for the first time,
427 // only show the window if set as visible in the XML
428 if( (*it)->isVisible() || !firstTime )
432 (*it)->setOpacity( m_alpha );
437 void WindowManager::hideAll() const
439 WinSet_t::const_iterator it;
440 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
447 void WindowManager::toggleOnTop()
449 // Update the boolean variable
450 VarBoolImpl *pVarOnTop = (VarBoolImpl*)m_cVarOnTop.get();
451 pVarOnTop->set( !pVarOnTop->get() );
453 // Toggle the "on top" status
454 WinSet_t::const_iterator it;
455 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
457 (*it)->toggleOnTop( pVarOnTop->get() );
462 void WindowManager::buildDependSet( WinSet_t &rWinSet,
465 // pWindow is in the set
466 rWinSet.insert( pWindow );
468 // Iterate through the anchored windows
469 const WinSet_t &anchored = m_dependencies[pWindow];
470 WinSet_t::const_iterator iter;
471 for( iter = anchored.begin(); iter != anchored.end(); iter++ )
473 // Check that the window isn't already in the set before adding it
474 if( rWinSet.find( *iter ) == rWinSet.end() )
476 buildDependSet( rWinSet, *iter );
482 void WindowManager::checkAnchors( TopWindow *pWindow,
483 int &xOffset, int &yOffset ) const
485 WinSet_t::const_iterator itMov, itSta;
486 AncList_t::const_iterator itAncMov, itAncSta;
488 // Check magnetism with screen edges first (actually it is the work area)
489 Rect workArea = OSFactory::instance( getIntf() )->getWorkArea();
490 // Iterate through the moving windows
491 for( itMov = m_movingWindows.begin();
492 itMov != m_movingWindows.end(); itMov++ )
494 // Skip the invisible windows
495 if( ! (*itMov)->getVisibleVar().get() )
500 int newLeft = (*itMov)->getLeft() + xOffset;
501 int newTop = (*itMov)->getTop() + yOffset;
502 if( newLeft > workArea.getLeft() - m_magnet &&
503 newLeft < workArea.getLeft() + m_magnet )
505 xOffset = workArea.getLeft() - (*itMov)->getLeft();
507 if( newTop > workArea.getTop() - m_magnet &&
508 newTop < workArea.getTop() + m_magnet )
510 yOffset = workArea.getTop() - (*itMov)->getTop();
512 int right = workArea.getLeft() + workArea.getWidth();
513 if( newLeft + (*itMov)->getWidth() > right - m_magnet &&
514 newLeft + (*itMov)->getWidth() < right + m_magnet )
516 xOffset = right - (*itMov)->getLeft() - (*itMov)->getWidth();
518 int bottom = workArea.getTop() + workArea.getHeight();
519 if( newTop + (*itMov)->getHeight() > bottom - m_magnet &&
520 newTop + (*itMov)->getHeight() < bottom + m_magnet )
522 yOffset = bottom - (*itMov)->getTop() - (*itMov)->getHeight();
526 // Iterate through the moving windows
527 for( itMov = m_movingWindows.begin();
528 itMov != m_movingWindows.end(); itMov++ )
530 // Skip the invisible windows
531 if( ! (*itMov)->getVisibleVar().get() )
536 // Get the anchors in the main layout of this moving window
537 const AncList_t &movAnchors =
538 (*itMov)->getActiveLayout().getAnchorList();
540 // Iterate through the static windows
541 for( itSta = m_allWindows.begin();
542 itSta != m_allWindows.end(); itSta++ )
544 // Skip the moving windows and the invisible ones
545 if( m_movingWindows.find( (*itSta) ) != m_movingWindows.end() ||
546 ! (*itSta)->getVisibleVar().get() )
551 // Get the anchors in the main layout of this static window
552 const AncList_t &staAnchors =
553 (*itSta)->getActiveLayout().getAnchorList();
555 // Check if there is an anchoring between one of the movAnchors
556 // and one of the staAnchors
557 for( itAncMov = movAnchors.begin();
558 itAncMov != movAnchors.end(); itAncMov++ )
560 for( itAncSta = staAnchors.begin();
561 itAncSta != staAnchors.end(); itAncSta++ )
563 if( (*itAncSta)->canHang( **itAncMov, xOffset, yOffset ) )
565 // We have found an anchoring!
566 // There is nothing to do here, since xOffset and
567 // yOffset are automatically modified by canHang()
569 // Don't check the other anchors, one is enough...
574 // Temporary variables
575 int xOffsetTemp = -xOffset;
576 int yOffsetTemp = -yOffset;
577 if( (*itAncMov)->canHang( **itAncSta, xOffsetTemp,
580 // We have found an anchoring!
581 // xOffsetTemp and yOffsetTemp have been updated,
582 // we just need to change xOffset and yOffset
583 xOffset = -xOffsetTemp;
584 yOffset = -yOffsetTemp;
586 // Don't check the other anchors, one is enough...
597 void WindowManager::createTooltip( const GenericFont &rTipFont )
599 // Create the tooltip window
602 m_pTooltip = new Tooltip( getIntf(), rTipFont, 500 );
606 msg_Warn( getIntf(), "tooltip already created!" );
611 void WindowManager::showTooltip()
620 void WindowManager::hideTooltip()
629 void WindowManager::addLayout( TopWindow &rWindow, GenericLayout &rLayout )
631 rWindow.setActiveLayout( &rLayout );
635 void WindowManager::setActiveLayout( TopWindow &rWindow,
636 GenericLayout &rLayout )
638 rWindow.setActiveLayout( &rLayout );
639 // Rebuild the dependencies