]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/window_manager.cpp
b2ccf99913ed0479a4ce7d85e55b10cccb9020c3
[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
33
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 )
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::maximize( TopWindow &rWindow )
321 {
322     // Save the current position/size of the window, to be able to restore it
323     m_maximizeRect = SkinsRect( rWindow.getLeft(), rWindow.getTop(),
324                                rWindow.getLeft() + rWindow.getWidth(),
325                                rWindow.getTop() + rWindow.getHeight() );
326
327     SkinsRect workArea = OSFactory::instance( getIntf() )->getWorkArea();
328     // Move the window
329     startMove( rWindow );
330     move( rWindow, workArea.getLeft(), workArea.getTop() );
331     stopMove();
332     // Now resize it
333     // FIXME: Ugly const_cast
334     GenericLayout &rLayout = (GenericLayout&)rWindow.getActiveLayout();
335     startResize( rLayout, kResizeSE );
336     resize( rLayout, workArea.getWidth(), workArea.getHeight() );
337     stopResize();
338     rWindow.m_pVarMaximized->set( true );
339
340     // Make the window unmovable by unregistering it
341 //     unregisterWindow( rWindow );
342 }
343
344
345 void WindowManager::unmaximize( TopWindow &rWindow )
346 {
347     // Register the window to allow moving it
348 //     registerWindow( rWindow );
349
350     // Resize the window
351     // FIXME: Ugly const_cast
352     GenericLayout &rLayout = (GenericLayout&)rWindow.getActiveLayout();
353     startResize( rLayout, kResizeSE );
354     resize( rLayout, m_maximizeRect.getWidth(), m_maximizeRect.getHeight() );
355     stopResize();
356     // Now move it
357     startMove( rWindow );
358     move( rWindow, m_maximizeRect.getLeft(), m_maximizeRect.getTop() );
359     stopMove();
360     rWindow.m_pVarMaximized->set( false );
361 }
362
363
364 void WindowManager::synchVisibility() const
365 {
366     WinSet_t::const_iterator it;
367     for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
368     {
369         // Show the window if it has to be visible
370         if( (*it)->getVisibleVar().get() )
371         {
372             (*it)->innerShow();
373         }
374     }
375 }
376
377
378 void WindowManager::saveVisibility()
379 {
380     WinSet_t::const_iterator it;
381     m_savedWindows.clear();
382     for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
383     {
384         // Remember the window if it is visible
385         if( (*it)->getVisibleVar().get() )
386         {
387             m_savedWindows.insert( *it );
388         }
389     }
390 }
391
392
393 void WindowManager::restoreVisibility() const
394 {
395     // Warning in case we never called saveVisibility()
396     if( m_savedWindows.size() == 0 )
397     {
398         msg_Warn( getIntf(), "restoring visibility for no window" );
399     }
400
401     WinSet_t::const_iterator it;
402     for( it = m_savedWindows.begin(); it != m_savedWindows.end(); it++)
403     {
404         (*it)->show();
405     }
406 }
407
408
409 void WindowManager::raiseAll() const
410 {
411     // Raise all the windows
412     WinSet_t::const_iterator it;
413     for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
414     {
415         (*it)->raise();
416     }
417 }
418
419
420 void WindowManager::showAll( bool firstTime ) const
421 {
422     // Show all the windows
423     WinSet_t::const_iterator it;
424     for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
425     {
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 )
429         {
430             (*it)->show();
431         }
432         (*it)->setOpacity( m_alpha );
433     }
434 }
435
436
437 void WindowManager::hideAll() const
438 {
439     WinSet_t::const_iterator it;
440     for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
441     {
442         (*it)->hide();
443     }
444 }
445
446
447 void WindowManager::toggleOnTop()
448 {
449     // Update the boolean variable
450     VarBoolImpl *pVarOnTop = (VarBoolImpl*)m_cVarOnTop.get();
451     pVarOnTop->set( !pVarOnTop->get() );
452
453     // Toggle the "on top" status
454     WinSet_t::const_iterator it;
455     for( it = m_allWindows.begin(); it != m_allWindows.end(); it++ )
456     {
457         (*it)->toggleOnTop( pVarOnTop->get() );
458     }
459 }
460
461
462 void WindowManager::buildDependSet( WinSet_t &rWinSet,
463                                     TopWindow *pWindow )
464 {
465     // pWindow is in the set
466     rWinSet.insert( pWindow );
467
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++ )
472     {
473         // Check that the window isn't already in the set before adding it
474         if( rWinSet.find( *iter ) == rWinSet.end() )
475         {
476             buildDependSet( rWinSet, *iter );
477         }
478     }
479 }
480
481
482 void WindowManager::checkAnchors( TopWindow *pWindow,
483                                   int &xOffset, int &yOffset ) const
484 {
485     WinSet_t::const_iterator itMov, itSta;
486     AncList_t::const_iterator itAncMov, itAncSta;
487
488     // Check magnetism with screen edges first (actually it is the work area)
489     SkinsRect workArea = OSFactory::instance( getIntf() )->getWorkArea();
490     // Iterate through the moving windows
491     for( itMov = m_movingWindows.begin();
492          itMov != m_movingWindows.end(); itMov++ )
493     {
494         // Skip the invisible windows
495         if( ! (*itMov)->getVisibleVar().get() )
496         {
497             continue;
498         }
499
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 )
504         {
505             xOffset = workArea.getLeft() - (*itMov)->getLeft();
506         }
507         if( newTop > workArea.getTop() - m_magnet &&
508             newTop < workArea.getTop() + m_magnet )
509         {
510             yOffset = workArea.getTop() - (*itMov)->getTop();
511         }
512         int right = workArea.getLeft() + workArea.getWidth();
513         if( newLeft + (*itMov)->getWidth() > right - m_magnet &&
514             newLeft + (*itMov)->getWidth() < right + m_magnet )
515         {
516             xOffset = right - (*itMov)->getLeft() - (*itMov)->getWidth();
517         }
518         int bottom = workArea.getTop() + workArea.getHeight();
519         if( newTop + (*itMov)->getHeight() > bottom - m_magnet &&
520             newTop + (*itMov)->getHeight() <  bottom + m_magnet )
521         {
522             yOffset =  bottom - (*itMov)->getTop() - (*itMov)->getHeight();
523         }
524     }
525
526     // Iterate through the moving windows
527     for( itMov = m_movingWindows.begin();
528          itMov != m_movingWindows.end(); itMov++ )
529     {
530         // Skip the invisible windows
531         if( ! (*itMov)->getVisibleVar().get() )
532         {
533             continue;
534         }
535
536         // Get the anchors in the main layout of this moving window
537         const AncList_t &movAnchors =
538             (*itMov)->getActiveLayout().getAnchorList();
539
540         // Iterate through the static windows
541         for( itSta = m_allWindows.begin();
542              itSta != m_allWindows.end(); itSta++ )
543         {
544             // Skip the moving windows and the invisible ones
545             if( m_movingWindows.find( (*itSta) ) != m_movingWindows.end() ||
546                 ! (*itSta)->getVisibleVar().get() )
547             {
548                 continue;
549             }
550
551             // Get the anchors in the main layout of this static window
552             const AncList_t &staAnchors =
553                 (*itSta)->getActiveLayout().getAnchorList();
554
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++ )
559             {
560                 for( itAncSta = staAnchors.begin();
561                      itAncSta != staAnchors.end(); itAncSta++ )
562                 {
563                     if( (*itAncSta)->canHang( **itAncMov, xOffset, yOffset ) )
564                     {
565                         // We have found an anchoring!
566                         // There is nothing to do here, since xOffset and
567                         // yOffset are automatically modified by canHang()
568
569                         // Don't check the other anchors, one is enough...
570                         return;
571                     }
572                     else
573                     {
574                         // Temporary variables
575                         int xOffsetTemp = -xOffset;
576                         int yOffsetTemp = -yOffset;
577                         if( (*itAncMov)->canHang( **itAncSta, xOffsetTemp,
578                                                   yOffsetTemp ) )
579                         {
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;
585
586                             // Don't check the other anchors, one is enough...
587                             return;
588                         }
589                     }
590                 }
591             }
592         }
593     }
594 }
595
596
597 void WindowManager::createTooltip( const GenericFont &rTipFont )
598 {
599     // Create the tooltip window
600     if( !m_pTooltip )
601     {
602         m_pTooltip = new Tooltip( getIntf(), rTipFont, 500 );
603     }
604     else
605     {
606         msg_Warn( getIntf(), "tooltip already created!" );
607     }
608 }
609
610
611 void WindowManager::showTooltip()
612 {
613     if( m_pTooltip )
614     {
615         m_pTooltip->show();
616     }
617 }
618
619
620 void WindowManager::hideTooltip()
621 {
622     if( m_pTooltip )
623     {
624         m_pTooltip->hide();
625     }
626 }
627
628
629 void WindowManager::addLayout( TopWindow &rWindow, GenericLayout &rLayout )
630 {
631     rWindow.setActiveLayout( &rLayout );
632 }
633
634
635 void WindowManager::setActiveLayout( TopWindow &rWindow,
636                                      GenericLayout &rLayout )
637 {
638     rWindow.setActiveLayout( &rLayout );
639     // Rebuild the dependencies
640     stopMove();
641 }