]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/window_manager.cpp
* modules/gui/skins2/*:
[vlc] / modules / gui / skins2 / src / window_manager.cpp
1 /*****************************************************************************
2  * window_manager.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 "var_manager.hpp"
32 #include "../utils/position.hpp"
33
34
35 WindowManager::WindowManager( intf_thread_t *pIntf ):
36     SkinObject( pIntf ), m_magnet( 0 ), m_direction( kNone ),
37     m_pTooltip( NULL ), m_pPopup( NULL )
38 {
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" );
43 }
44
45
46 WindowManager::~WindowManager()
47 {
48     delete m_pTooltip;
49 }
50
51
52 void WindowManager::registerWindow( TopWindow &rWindow )
53 {
54     // Add the window to the set
55     m_allWindows.insert( &rWindow );
56 }
57
58
59 void WindowManager::unregisterWindow( TopWindow &rWindow )
60 {
61     // Erase every possible reference to the window
62     m_allWindows.erase( &rWindow );
63     m_movingWindows.erase( &rWindow );
64     m_dependencies.erase( &rWindow );
65 }
66
67
68 void WindowManager::startMove( TopWindow &rWindow )
69 {
70     // Rebuild the set of moving windows
71     m_movingWindows.clear();
72     buildDependSet( m_movingWindows, &rWindow );
73
74 #ifdef WIN32
75     if( config_GetInt( getIntf(), "skins2-transparency" ) )
76     {
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++ )
80         {
81             (*it)->setOpacity( m_moveAlpha );
82         }
83
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++ )
88         {
89             (*it)->refresh( 0, 0, (*it)->getWidth(), (*it)->getHeight() );
90         }
91     }
92 #endif
93 }
94
95
96 void WindowManager::stopMove()
97 {
98     WinSet_t::const_iterator itWin1, itWin2;
99     AncList_t::const_iterator itAnc1, itAnc2;
100
101 #ifdef WIN32
102     if( config_GetInt( getIntf(), "skins2-transparency" ) )
103     {
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++ )
107         {
108             (*it)->setOpacity( m_alpha );
109         }
110     }
111 #endif
112
113     // Delete the dependencies
114     m_dependencies.clear();
115
116     // Now we rebuild the dependencies.
117     // Iterate through all the windows
118     for( itWin1 = m_allWindows.begin(); itWin1 != m_allWindows.end(); itWin1++ )
119     {
120         // Get the anchors of the layout associated to the window
121         const AncList_t &ancList1 =
122             (*itWin1)->getActiveLayout().getAnchorList();
123
124         // Iterate through all the windows, starting with (*itWin1)
125         for( itWin2 = itWin1; itWin2 != m_allWindows.end(); itWin2++ )
126         {
127             // A window can't anchor itself...
128             if( (*itWin2) == (*itWin1) )
129                 continue;
130
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++ )
135             {
136                 for( itAnc2 = ancList2.begin();
137                      itAnc2 != ancList2.end(); itAnc2++ )
138                 {
139                     if( (*itAnc1)->isHanging( **itAnc2 ) )
140                     {
141                         // (*itWin1) anchors (*itWin2)
142                         m_dependencies[*itWin1].insert( *itWin2 );
143                     }
144                     else if( (*itAnc2)->isHanging( **itAnc1 ) )
145                     {
146                         // (*itWin2) anchors (*itWin1)
147                         m_dependencies[*itWin2].insert( *itWin1 );
148                     }
149                 }
150             }
151         }
152     }
153 }
154
155
156 void WindowManager::move( TopWindow &rWindow, int left, int top ) const
157 {
158     // Compute the real move offset
159     int xOffset = left - rWindow.getLeft();
160     int yOffset = top - rWindow.getTop();
161
162     // Check anchoring; this can change the values of xOffset and yOffset
163     checkAnchors( &rWindow, xOffset, yOffset );
164
165     // Move all the windows
166     WinSet_t::const_iterator it;
167     for( it = m_movingWindows.begin(); it != m_movingWindows.end(); it++ )
168     {
169         (*it)->move( (*it)->getLeft() + xOffset, (*it)->getTop() + yOffset );
170     }
171 }
172
173
174 void WindowManager::startResize( GenericLayout &rLayout, Direction_t direction )
175 {
176     m_direction = direction;
177
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.
182
183     m_resizeMovingE.clear();
184     m_resizeMovingS.clear();
185     m_resizeMovingSE.clear();
186
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();
191
192     // Iterate through all the hanged windows
193     for( itWin = m_dependencies[rLayout.getWindow()].begin();
194          itWin != m_dependencies[rLayout.getWindow()].end(); itWin++ )
195     {
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++ )
200         {
201             for( itAnc2 = ancList2.begin();
202                  itAnc2 != ancList2.end(); itAnc2++ )
203             {
204                 if( (*itAnc1)->isHanging( **itAnc2 ) )
205                 {
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 );
216                     break;
217                 }
218             }
219         }
220     }
221
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() );
229 }
230
231
232 void WindowManager::stopResize()
233 {
234     // Nothing different from stopMove(), luckily
235     stopMove();
236 }
237
238
239 void WindowManager::resize( GenericLayout &rLayout,
240                             int width, int height ) const
241 {
242     // TODO: handle anchored windows
243     // Compute the real resizing offset
244     int xOffset = width - rLayout.getWidth();
245     int yOffset = height - rLayout.getHeight();
246
247     // Check anchoring; this can change the values of xOffset and yOffset
248     checkAnchors( rLayout.getWindow(), xOffset, yOffset );
249     if( m_direction == kResizeS )
250         xOffset = 0;
251     if( m_direction == kResizeE )
252         yOffset = 0;
253
254     int newWidth = rLayout.getWidth() + xOffset;
255     int newHeight = rLayout.getHeight() + yOffset;
256
257     // Check boundaries
258     if( newWidth < rLayout.getMinWidth() )
259     {
260         newWidth = rLayout.getMinWidth();
261     }
262     if( newWidth > rLayout.getMaxWidth() )
263     {
264         newWidth = rLayout.getMaxWidth();
265     }
266     if( newHeight < rLayout.getMinHeight() )
267     {
268         newHeight = rLayout.getMinHeight();
269     }
270     if( newHeight > rLayout.getMaxHeight() )
271     {
272         newHeight = rLayout.getMaxHeight();
273     }
274
275     if( newWidth == rLayout.getWidth() && newHeight == rLayout.getHeight() )
276     {
277         return;
278     }
279
280     // New offset, after the last corrections
281     int xNewOffset = newWidth - rLayout.getWidth();
282     int yNewOffset = newHeight - rLayout.getHeight();
283
284     // Do the actual resizing
285     rLayout.resize( newWidth, newHeight );
286
287     // Move all the anchored windows
288     WinSet_t::const_iterator it;
289     if( m_direction == kResizeE ||
290         m_direction == kResizeSE )
291     {
292         for( it = m_resizeMovingE.begin(); it != m_resizeMovingE.end(); it++ )
293         {
294             (*it)->move( (*it)->getLeft() + xNewOffset,
295                          (*it)->getTop() );
296         }
297     }
298     if( m_direction == kResizeE ||
299         m_direction == kResizeSE )
300     {
301         for( it = m_resizeMovingS.begin(); it != m_resizeMovingS.end(); it++ )
302         {
303             (*it)->move( (*it)->getLeft(),
304                          (*it)->getTop( )+ yNewOffset );
305         }
306     }
307     if( m_direction == kResizeE ||
308         m_direction == kResizeS ||
309         m_direction == kResizeSE )
310     {
311         for( it = m_resizeMovingSE.begin(); it != m_resizeMovingSE.end(); it++ )
312         {
313             (*it)->move( (*it)->getLeft() + xNewOffset,
314                          (*it)->getTop() + yNewOffset );
315         }
316     }
317 }
318
319
320 void WindowManager::synchVisibility() const
321 {
322     WinSet_t::const_iterator it;
323     for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
324     {
325         // Show the window if it has to be visible
326         if( (*it)->getVisibleVar().get() )
327         {
328             (*it)->innerShow();
329         }
330     }
331 }
332
333
334 void WindowManager::raiseAll() const
335 {
336     // Raise all the windows
337     WinSet_t::const_iterator it;
338     for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
339     {
340         (*it)->raise();
341     }
342 }
343
344
345 void WindowManager::showAll( bool firstTime ) const
346 {
347     // Show all the windows
348     WinSet_t::const_iterator it;
349     for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
350     {
351         // When the theme is opened for the first time,
352         // only show the window if set as visible in the XML
353         if( (*it)->isVisible() || !firstTime )
354         {
355             (*it)->show();
356         }
357         (*it)->setOpacity( m_alpha );
358     }
359 }
360
361
362 void WindowManager::hideAll() const
363 {
364     WinSet_t::const_iterator it;
365     for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
366     {
367         (*it)->hide();
368     }
369 }
370
371
372 void WindowManager::toggleOnTop()
373 {
374     // Update the boolean variable
375     VarBoolImpl *pVarOnTop = (VarBoolImpl*)m_cVarOnTop.get();
376     pVarOnTop->set( !pVarOnTop->get() );
377
378     // Toggle the "on top" status
379     WinSet_t::const_iterator it;
380     for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
381     {
382         (*it)->toggleOnTop( pVarOnTop->get() );
383     }
384 }
385
386
387 void WindowManager::buildDependSet( WinSet_t &rWinSet,
388                                     TopWindow *pWindow )
389 {
390     // pWindow is in the set
391     rWinSet.insert( pWindow );
392
393     // Iterate through the anchored windows
394     const WinSet_t &anchored = m_dependencies[pWindow];
395     WinSet_t::const_iterator iter;
396     for( iter = anchored.begin(); iter != anchored.end(); iter++ )
397     {
398         // Check that the window isn't already in the set before adding it
399         if( rWinSet.find( *iter ) == rWinSet.end() )
400         {
401             buildDependSet( rWinSet, *iter );
402         }
403     }
404 }
405
406
407 void WindowManager::checkAnchors( TopWindow *pWindow,
408                                   int &xOffset, int &yOffset ) const
409 {
410     WinSet_t::const_iterator itMov, itSta;
411     AncList_t::const_iterator itAncMov, itAncSta;
412
413     // Check magnetism with screen edges first (actually it is the work area)
414     Rect workArea = OSFactory::instance( getIntf() )->getWorkArea();
415     // Iterate through the moving windows
416     for( itMov = m_movingWindows.begin();
417          itMov != m_movingWindows.end(); itMov++ )
418     {
419         // Skip the invisible windows
420         if( ! (*itMov)->getVisibleVar().get() )
421         {
422             continue;
423         }
424
425         int newLeft = (*itMov)->getLeft() + xOffset;
426         int newTop = (*itMov)->getTop() + yOffset;
427         if( newLeft > workArea.getLeft() - m_magnet &&
428             newLeft < workArea.getLeft() + m_magnet )
429         {
430             xOffset = workArea.getLeft() - (*itMov)->getLeft();
431         }
432         if( newTop > workArea.getTop() - m_magnet &&
433             newTop < workArea.getTop() + m_magnet )
434         {
435             yOffset = workArea.getTop() - (*itMov)->getTop();
436         }
437         if( newLeft + (*itMov)->getWidth() > workArea.getRight() - m_magnet &&
438             newLeft + (*itMov)->getWidth() < workArea.getRight() + m_magnet )
439         {
440             xOffset = workArea.getRight() - (*itMov)->getLeft()
441                       - (*itMov)->getWidth();
442         }
443         if( newTop + (*itMov)->getHeight() > workArea.getBottom() - m_magnet &&
444             newTop + (*itMov)->getHeight() <  workArea.getBottom() + m_magnet )
445         {
446             yOffset =  workArea.getBottom() - (*itMov)->getTop()
447                        - (*itMov)->getHeight();
448         }
449     }
450
451     // Iterate through the moving windows
452     for( itMov = m_movingWindows.begin();
453          itMov != m_movingWindows.end(); itMov++ )
454     {
455         // Skip the invisible windows
456         if( ! (*itMov)->getVisibleVar().get() )
457         {
458             continue;
459         }
460
461         // Get the anchors in the main layout of this moving window
462         const AncList_t &movAnchors =
463             (*itMov)->getActiveLayout().getAnchorList();
464
465         // Iterate through the static windows
466         for( itSta = m_allWindows.begin();
467              itSta != m_allWindows.end(); itSta++ )
468         {
469             // Skip the moving windows and the invisible ones
470             if( m_movingWindows.find( (*itSta) ) != m_movingWindows.end() ||
471                 ! (*itSta)->getVisibleVar().get() )
472             {
473                 continue;
474             }
475
476             // Get the anchors in the main layout of this static window
477             const AncList_t &staAnchors =
478                 (*itSta)->getActiveLayout().getAnchorList();
479
480             // Check if there is an anchoring between one of the movAnchors
481             // and one of the staAnchors
482             for( itAncMov = movAnchors.begin();
483                  itAncMov != movAnchors.end(); itAncMov++ )
484             {
485                 for( itAncSta = staAnchors.begin();
486                      itAncSta != staAnchors.end(); itAncSta++ )
487                 {
488                     if( (*itAncSta)->canHang( **itAncMov, xOffset, yOffset ) )
489                     {
490                         // We have found an anchoring!
491                         // There is nothing to do here, since xOffset and
492                         // yOffset are automatically modified by canHang()
493
494                         // Don't check the other anchors, one is enough...
495                         return;
496                     }
497                     else
498                     {
499                         // Temporary variables
500                         int xOffsetTemp = -xOffset;
501                         int yOffsetTemp = -yOffset;
502                         if( (*itAncMov)->canHang( **itAncSta, xOffsetTemp,
503                                                   yOffsetTemp ) )
504                         {
505                             // We have found an anchoring!
506                             // xOffsetTemp and yOffsetTemp have been updated,
507                             // we just need to change xOffset and yOffset
508                             xOffset = -xOffsetTemp;
509                             yOffset = -yOffsetTemp;
510
511                             // Don't check the other anchors, one is enough...
512                             return;
513                         }
514                     }
515                 }
516             }
517         }
518     }
519 }
520
521
522 void WindowManager::createTooltip( const GenericFont &rTipFont )
523 {
524     // Create the tooltip window
525     if( !m_pTooltip )
526     {
527         m_pTooltip = new Tooltip( getIntf(), rTipFont, 500 );
528     }
529     else
530     {
531         msg_Warn( getIntf(), "tooltip already created!" );
532     }
533 }
534
535
536 void WindowManager::showTooltip()
537 {
538     if( m_pTooltip )
539     {
540         m_pTooltip->show();
541     }
542 }
543
544
545 void WindowManager::hideTooltip()
546 {
547     if( m_pTooltip )
548     {
549         m_pTooltip->hide();
550     }
551 }
552
553
554 void WindowManager::addLayout( TopWindow &rWindow, GenericLayout &rLayout )
555 {
556     rWindow.setActiveLayout( &rLayout );
557 }
558
559
560 void WindowManager::setActiveLayout( TopWindow &rWindow,
561                                      GenericLayout &rLayout )
562 {
563     rWindow.setActiveLayout( &rLayout );
564     // Rebuild the dependencies
565     stopMove();
566 }