]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/window_manager.cpp
Copyright fixes
[vlc] / modules / gui / skins2 / src / window_manager.cpp
1 /*****************************************************************************
2  * window_manager.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN (Centrale Réseaux) and its contributors
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #include "window_manager.hpp"
26 #include "generic_layout.hpp"
27 #include "generic_window.hpp"
28 #include "os_factory.hpp"
29 #include "anchor.hpp"
30 #include "tooltip.hpp"
31 #include "../utils/position.hpp"
32 #include "../src/var_manager.hpp"
33
34
35 WindowManager::WindowManager( intf_thread_t *pIntf ):
36     SkinObject( pIntf ), m_magnet( 0 ), m_pTooltip( NULL )
37 {
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" );
42 }
43
44
45 WindowManager::~WindowManager()
46 {
47     delete m_pTooltip;
48 }
49
50
51 void WindowManager::registerWindow( TopWindow &rWindow )
52 {
53     // Add the window to the set
54     m_allWindows.insert( &rWindow );
55 }
56
57
58 void WindowManager::unregisterWindow( TopWindow &rWindow )
59 {
60     // Erase every possible reference to the window
61     m_allWindows.erase( &rWindow );
62     m_movingWindows.erase( &rWindow );
63     m_dependencies.erase( &rWindow );
64 }
65
66
67 void WindowManager::startMove( TopWindow &rWindow )
68 {
69     // Rebuild the set of moving windows
70     m_movingWindows.clear();
71     buildDependSet( m_movingWindows, &rWindow );
72
73 #ifdef WIN32
74     if( config_GetInt( getIntf(), "skins2-transparency" ) )
75     {
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++ )
79         {
80             (*it)->setOpacity( m_moveAlpha );
81         }
82
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++ )
87         {
88             (*it)->refresh( 0, 0, (*it)->getWidth(), (*it)->getHeight() );
89         }
90     }
91 #endif
92 }
93
94
95 void WindowManager::stopMove()
96 {
97     WinSet_t::const_iterator itWin1, itWin2;
98     AncList_t::const_iterator itAnc1, itAnc2;
99
100 #ifdef WIN32
101     if( config_GetInt( getIntf(), "skins2-transparency" ) )
102     {
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++ )
106         {
107             (*it)->setOpacity( m_alpha );
108         }
109     }
110 #endif
111
112     // Delete the dependencies
113     m_dependencies.clear();
114
115     // Now we rebuild the dependencies.
116     // Iterate through all the windows
117     for( itWin1 = m_allWindows.begin(); itWin1 != m_allWindows.end(); itWin1++ )
118     {
119         // Get the anchors of the layout associated to the window
120         const AncList_t &ancList1 =
121             (*itWin1)->getActiveLayout().getAnchorList();
122
123         // Iterate through all the windows, starting with (*itWin1)
124         for( itWin2 = itWin1; itWin2 != m_allWindows.end(); itWin2++ )
125         {
126             // A window can't anchor itself...
127             if( (*itWin2) == (*itWin1) )
128                 continue;
129
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++ )
134             {
135                 for( itAnc2 = ancList2.begin();
136                      itAnc2 != ancList2.end(); itAnc2++ )
137                 {
138                     if( (*itAnc1)->isHanging( **itAnc2 ) )
139                     {
140                         // (*itWin1) anchors (*itWin2)
141                         m_dependencies[*itWin1].insert( *itWin2 );
142                     }
143                     else if( (*itAnc2)->isHanging( **itAnc1 ) )
144                     {
145                         // (*itWin2) anchors (*itWin1)
146                         m_dependencies[*itWin2].insert( *itWin1 );
147                     }
148                 }
149             }
150         }
151     }
152 }
153
154
155 void WindowManager::move( TopWindow &rWindow, int left, int top ) const
156 {
157     // Compute the real move offset
158     int xOffset = left - rWindow.getLeft();
159     int yOffset = top - rWindow.getTop();
160
161     // Check anchoring; this can change the values of xOffset and yOffset
162     checkAnchors( &rWindow, xOffset, yOffset );
163
164     // Move all the windows
165     WinSet_t::const_iterator it;
166     for( it = m_movingWindows.begin(); it != m_movingWindows.end(); it++ )
167     {
168         (*it)->move( (*it)->getLeft() + xOffset, (*it)->getTop() + yOffset );
169     }
170 }
171
172
173 void WindowManager::synchVisibility() const
174 {
175     WinSet_t::const_iterator it;
176     for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
177     {
178         // Show the window if it has to be visible
179         if( (*it)->getVisibleVar().get() )
180         {
181             (*it)->innerShow();
182         }
183     }
184 }
185
186
187 void WindowManager::raiseAll() const
188 {
189     // Raise all the windows
190     WinSet_t::const_iterator it;
191     for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
192     {
193         (*it)->raise();
194     }
195 }
196
197
198 void WindowManager::showAll() const
199 {
200     // Show all the windows
201     WinSet_t::const_iterator it;
202     for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
203     {
204         (*it)->show();
205         (*it)->setOpacity( m_alpha );
206     }
207 }
208
209
210 void WindowManager::hideAll() const
211 {
212     WinSet_t::const_iterator it;
213     for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
214     {
215         (*it)->hide();
216     }
217 }
218
219
220 void WindowManager::toggleOnTop()
221 {
222     // Update the boolean variable
223     VarBoolImpl *pVarOnTop = (VarBoolImpl*)m_cVarOnTop.get();
224     pVarOnTop->set( !pVarOnTop->get() );
225
226     // Toggle the "on top" status
227     WinSet_t::const_iterator it;
228     for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
229     {
230         (*it)->toggleOnTop( pVarOnTop->get() );
231     }
232 }
233
234
235 void WindowManager::buildDependSet( WinSet_t &rWinSet,
236                                     TopWindow *pWindow )
237 {
238     // pWindow is in the set
239     rWinSet.insert( pWindow );
240
241     // Iterate through the anchored windows
242     const WinSet_t &anchored = m_dependencies[pWindow];
243     WinSet_t::const_iterator iter;
244     for( iter = anchored.begin(); iter != anchored.end(); iter++ )
245     {
246         // Check that the window isn't already in the set before adding it
247         if( rWinSet.find( *iter ) == rWinSet.end() )
248         {
249             buildDependSet( rWinSet, *iter );
250         }
251     }
252 }
253
254
255 void WindowManager::checkAnchors( TopWindow *pWindow,
256                                   int &xOffset, int &yOffset ) const
257 {
258     WinSet_t::const_iterator itMov, itSta;
259     AncList_t::const_iterator itAncMov, itAncSta;
260
261     // Check magnetism with screen edges first (actually it is the work area)
262     Rect workArea = OSFactory::instance( getIntf() )->getWorkArea();
263     // Iterate through the moving windows
264     for( itMov = m_movingWindows.begin();
265          itMov != m_movingWindows.end(); itMov++ )
266     {
267         int newLeft = (*itMov)->getLeft() + xOffset;
268         int newTop = (*itMov)->getTop() + yOffset;
269         if( newLeft > workArea.getLeft() - m_magnet &&
270             newLeft < workArea.getLeft() + m_magnet )
271         {
272             xOffset = workArea.getLeft() - (*itMov)->getLeft();
273         }
274         if( newTop > workArea.getTop() - m_magnet &&
275             newTop < workArea.getTop() + m_magnet )
276         {
277             yOffset = workArea.getTop() - (*itMov)->getTop();
278         }
279         if( newLeft + (*itMov)->getWidth() > workArea.getRight() - m_magnet &&
280             newLeft + (*itMov)->getWidth() < workArea.getRight() + m_magnet )
281         {
282             xOffset = workArea.getRight() - (*itMov)->getLeft()
283                       - (*itMov)->getWidth();
284         }
285         if( newTop + (*itMov)->getHeight() > workArea.getBottom() - m_magnet &&
286             newTop + (*itMov)->getHeight() <  workArea.getBottom() + m_magnet )
287         {
288             yOffset =  workArea.getBottom() - (*itMov)->getTop()
289                        - (*itMov)->getHeight();
290         }
291     }
292
293     // Iterate through the moving windows
294     for( itMov = m_movingWindows.begin();
295          itMov != m_movingWindows.end(); itMov++ )
296     {
297         // Skip the invisible windows
298         if( ! (*itMov)->getVisibleVar().get() )
299         {
300             continue;
301         }
302
303         // Get the anchors in the main layout of this moving window
304         const AncList_t &movAnchors =
305             (*itMov)->getActiveLayout().getAnchorList();
306
307         // Iterate through the static windows
308         for( itSta = m_allWindows.begin();
309              itSta != m_allWindows.end(); itSta++ )
310         {
311             // Skip the moving windows and the invisible ones
312             if( m_movingWindows.find( (*itSta) ) != m_movingWindows.end() ||
313                 ! (*itSta)->getVisibleVar().get() )
314             {
315                 continue;
316             }
317
318             // Get the anchors in the main layout of this static window
319             const AncList_t &staAnchors =
320                 (*itSta)->getActiveLayout().getAnchorList();
321
322             // Check if there is an anchoring between one of the movAnchors
323             // and one of the staAnchors
324             for( itAncMov = movAnchors.begin();
325                  itAncMov != movAnchors.end(); itAncMov++ )
326             {
327                 for( itAncSta = staAnchors.begin();
328                      itAncSta != staAnchors.end(); itAncSta++ )
329                 {
330                     if( (*itAncSta)->canHang( **itAncMov, xOffset, yOffset ) )
331                     {
332                         // We have found an anchoring!
333                         // There is nothing to do here, since xOffset and
334                         // yOffset are automatically modified by canHang()
335
336                         // Don't check the other anchors, one is enough...
337                         return;
338                     }
339                     else
340                     {
341                         // Temporary variables
342                         int xOffsetTemp = -xOffset;
343                         int yOffsetTemp = -yOffset;
344                         if( (*itAncMov)->canHang( **itAncSta, xOffsetTemp,
345                                                   yOffsetTemp ) )
346                         {
347                             // We have found an anchoring!
348                             // xOffsetTemp and yOffsetTemp have been updated,
349                             // we just need to change xOffset and yOffset
350                             xOffset = -xOffsetTemp;
351                             yOffset = -yOffsetTemp;
352
353                             // Don't check the other anchors, one is enough...
354                             return;
355                         }
356                     }
357                 }
358             }
359         }
360     }
361 }
362
363
364 void WindowManager::createTooltip( const GenericFont &rTipFont )
365 {
366     // Create the tooltip window
367     if( !m_pTooltip )
368     {
369         m_pTooltip = new Tooltip( getIntf(), rTipFont, 500 );
370     }
371     else
372     {
373         msg_Warn( getIntf(), "Tooltip already created!");
374     }
375 }
376
377
378 void WindowManager::showTooltip()
379 {
380     if( m_pTooltip )
381     {
382         m_pTooltip->show();
383     }
384 }
385
386
387 void WindowManager::hideTooltip()
388 {
389     if( m_pTooltip )
390     {
391         m_pTooltip->hide();
392     }
393 }
394
395
396 void WindowManager::addLayout( TopWindow &rWindow, GenericLayout &rLayout )
397 {
398     rWindow.setActiveLayout( &rLayout );
399 }
400
401
402 void WindowManager::setActiveLayout( TopWindow &rWindow,
403                                      GenericLayout &rLayout )
404 {
405     rWindow.setActiveLayout( &rLayout );
406     // Rebuild the dependencies
407     stopMove();
408 }