1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: window_manager.cpp,v 1.1 2004/01/03 23:31:34 asmax Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 #include "window_manager.hpp"
26 #include "generic_window.hpp"
27 #include "os_factory.hpp"
29 #include "../utils/position.hpp"
32 void WindowManager::registerWindow( GenericWindow *pWindow )
36 msg_Dbg( getIntf(), "WM: registering a NULL window" );
40 // Add the window to the set
41 m_allWindows.insert( pWindow );
45 void WindowManager::unregisterWindow( GenericWindow *pWindow )
47 // Erase every possible reference to the window
48 m_allWindows.erase( pWindow );
49 m_movingWindows.erase( pWindow );
50 m_dependencies.erase( pWindow );
54 void WindowManager::startMove( GenericWindow *pWindow )
56 // Rebuild the set of moving windows
57 m_movingWindows.clear();
58 buildDependSet( m_movingWindows, pWindow );
60 // Change the opacity of the moving windows
61 WinSet_t::const_iterator it;
62 for( it = m_movingWindows.begin(); it != m_movingWindows.end(); it++ )
64 (*it)->setOpacity( m_moveAlpha );
69 void WindowManager::stopMove()
71 WinSet_t::const_iterator itWin1, itWin2;
72 AncList_t::const_iterator itAnc1, itAnc2;
74 // Restore the opacity of the moving windows
75 WinSet_t::const_iterator it;
76 for( it = m_movingWindows.begin(); it != m_movingWindows.end(); it++ )
78 (*it)->setOpacity( m_alpha );
81 // Delete the dependencies
82 m_dependencies.clear();
84 // Now we rebuild the dependencies.
85 // Iterate through all the windows
86 for( itWin1 = m_allWindows.begin(); itWin1 != m_allWindows.end(); itWin1++ )
88 // Get the anchors of the window
89 const AncList_t &ancList1 = (*itWin1)->getAnchorList();
91 // Iterate through all the windows, starting with (*itWin1)
92 for( itWin2 = itWin1; itWin2 != m_allWindows.end(); itWin2++ )
94 // A window can't anchor itself...
95 if( (*itWin2) == (*itWin1) )
98 // Now, check for anchoring between the 2 windows
99 const AncList_t &ancList2 = (*itWin2)->getAnchorList();
100 for( itAnc1 = ancList1.begin(); itAnc1 != ancList1.end(); itAnc1++ )
102 for( itAnc2 = ancList2.begin();
103 itAnc2 != ancList2.end(); itAnc2++ )
105 if( (*itAnc1)->isHanging( **itAnc2 ) )
107 // (*itWin1) anchors (*itWin2)
108 m_dependencies[*itWin1].insert( *itWin2 );
110 else if( (*itAnc2)->isHanging( **itAnc1 ) )
112 // (*itWin2) anchors (*itWin1)
113 m_dependencies[*itWin2].insert( *itWin1 );
122 void WindowManager::move( GenericWindow *pWindow, int left, int top ) const
124 // Compute the real move offset
125 int xOffset = left - pWindow->getLeft();
126 int yOffset = top - pWindow->getTop();
128 // Check anchoring; this can change the values of xOffset and yOffset
129 checkAnchors( pWindow, xOffset, yOffset );
131 // Move all the windows
132 WinSet_t::const_iterator it;
133 for( it = m_movingWindows.begin(); it != m_movingWindows.end(); it++ )
135 (*it)->move( (*it)->getLeft() + xOffset, (*it)->getTop() + yOffset );
140 void WindowManager::raise( GenericWindow *pWindow )
142 // Build a set of windows anchored to pWindow
144 buildDependSet( winSet, pWindow );
146 // Raise the windows in the set
147 WinSet_t::const_iterator iter;
148 for( iter = winSet.begin(); iter != winSet.end(); iter++ )
155 void WindowManager::showAll() const
157 WinSet_t::const_iterator it;
158 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
161 (*it)->setOpacity( m_alpha );
166 void WindowManager::hideAll() const
168 WinSet_t::const_iterator it;
169 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
176 void WindowManager::toggleOnTop()
178 m_isOnTop = !m_isOnTop;
179 WinSet_t::const_iterator it;
180 for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
182 (*it)->toggleOnTop( m_isOnTop );
187 void WindowManager::buildDependSet( WinSet_t &rWinSet,
188 GenericWindow *pWindow )
190 // pWindow is in the set
191 rWinSet.insert( pWindow );
193 // Iterate through the anchored windows
194 const WinSet_t &anchored = m_dependencies[pWindow];
195 WinSet_t::const_iterator iter;
196 for( iter = anchored.begin(); iter != anchored.end(); iter++ )
198 // Check that the window isn't already in the set before adding it
199 if( rWinSet.find( *iter ) == rWinSet.end() )
201 buildDependSet( rWinSet, *iter );
207 void WindowManager::checkAnchors( GenericWindow *pWindow,
208 int &xOffset, int &yOffset ) const
210 WinSet_t::const_iterator itMov, itSta;
211 AncList_t::const_iterator itAncMov, itAncSta;
213 // Check magnetism with screen edges first (actually it is the work area)
214 Rect workArea = OSFactory::instance( getIntf() )->getWorkArea();
215 // Iterate through the moving windows
216 for( itMov = m_movingWindows.begin();
217 itMov != m_movingWindows.end(); itMov++ )
219 int newLeft = (*itMov)->getLeft() + xOffset;
220 int newTop = (*itMov)->getTop() + yOffset;
221 if( newLeft > workArea.getLeft() - m_magnet &&
222 newLeft < workArea.getLeft() + m_magnet )
224 xOffset = workArea.getLeft() - (*itMov)->getLeft();
226 if( newTop > workArea.getTop() - m_magnet &&
227 newTop < workArea.getTop() + m_magnet )
229 yOffset = workArea.getTop() - (*itMov)->getTop();
231 if( newLeft + (*itMov)->getWidth() > workArea.getRight() - m_magnet &&
232 newLeft + (*itMov)->getWidth() < workArea.getRight() + m_magnet )
234 xOffset = workArea.getRight() - (*itMov)->getLeft()
235 - (*itMov)->getWidth();
237 if( newTop + (*itMov)->getHeight() > workArea.getBottom() - m_magnet &&
238 newTop + (*itMov)->getHeight() < workArea.getBottom() + m_magnet )
240 yOffset = workArea.getBottom() - (*itMov)->getTop()
241 - (*itMov)->getHeight();
245 // Iterate through the moving windows
246 for( itMov = m_movingWindows.begin();
247 itMov != m_movingWindows.end(); itMov++ )
249 // Get the anchors of this moving window
250 const AncList_t &movAnchors = (*itMov)->getAnchorList();
252 // Iterate through the static windows
253 for( itSta = m_allWindows.begin();
254 itSta != m_allWindows.end(); itSta++ )
256 // Skip the moving windows
257 if( m_movingWindows.find( (*itSta) ) != m_movingWindows.end() )
262 // Get the anchors of this static window
263 const AncList_t &staAnchors = (*itSta)->getAnchorList();
265 // Check if there is an anchoring between one of the movAnchors
266 // and one of the staAnchors
267 for( itAncMov = movAnchors.begin();
268 itAncMov != movAnchors.end(); itAncMov++ )
270 for( itAncSta = staAnchors.begin();
271 itAncSta != staAnchors.end(); itAncSta++ )
273 if( (*itAncSta)->canHang( **itAncMov, xOffset, yOffset ) )
275 // We have found an anchoring!
276 // There is nothing to do here, since xOffset and
277 // yOffset are automatically modified by canHang()
279 // Don't check the other anchors, one is enough...
284 // Temporary variables
285 int xOffsetTemp = -xOffset;
286 int yOffsetTemp = -yOffset;
287 if( (*itAncMov)->canHang( **itAncSta, xOffsetTemp,
290 // We have found an anchoring!
291 // xOffsetTemp and yOffsetTemp have been updated,
292 // we just need to change xOffset and yOffset
293 xOffset = -xOffsetTemp;
294 yOffset = -yOffsetTemp;
296 // Don't check the other anchors, one is enough...