]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/window_manager.cpp
Remove vlc_object_attach()
[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_alpha( 255 ), m_moveAlpha( 255 ),
36     m_opacityEnabled( false ), m_opacity( 255 ), m_direction( kNone ),
37     m_maximizeRect(0, 0, 50, 50), 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     // transparency switched on or off by user
45     m_opacityEnabled = var_InheritBool( getIntf(), "skins2-transparency" );
46
47     // opacity overridden by user
48     m_opacity = 255 * var_InheritFloat( getIntf(), "qt-opacity" );
49 }
50
51
52 WindowManager::~WindowManager()
53 {
54     delete m_pTooltip;
55 }
56
57
58 void WindowManager::registerWindow( TopWindow &rWindow )
59 {
60     // Add the window to the set
61     m_allWindows.insert( &rWindow );
62 }
63
64
65 void WindowManager::unregisterWindow( TopWindow &rWindow )
66 {
67     // Erase every possible reference to the window
68     m_allWindows.erase( &rWindow );
69     m_movingWindows.erase( &rWindow );
70     m_dependencies.erase( &rWindow );
71 }
72
73
74 void WindowManager::startMove( TopWindow &rWindow )
75 {
76     // Rebuild the set of moving windows
77     m_movingWindows.clear();
78     buildDependSet( m_movingWindows, &rWindow );
79
80     if( isOpacityNeeded() )
81     {
82         // Change the opacity of the moving windows
83         WinSet_t::const_iterator it;
84         for( it = m_movingWindows.begin(); it != m_movingWindows.end(); ++it )
85         {
86             (*it)->setOpacity( m_moveAlpha );
87         }
88     }
89 }
90
91
92 void WindowManager::stopMove()
93 {
94     WinSet_t::const_iterator itWin1, itWin2;
95     AncList_t::const_iterator itAnc1, itAnc2;
96
97     if( isOpacityNeeded() )
98     {
99         // Restore the opacity of the moving windows
100         WinSet_t::const_iterator it;
101         for( it = m_movingWindows.begin(); it != m_movingWindows.end(); ++it )
102         {
103             (*it)->setOpacity( m_alpha );
104         }
105     }
106
107     // Delete the dependencies
108     m_dependencies.clear();
109
110     // Now we rebuild the dependencies.
111     // Iterate through all the windows
112     for( itWin1 = m_allWindows.begin(); itWin1 != m_allWindows.end(); ++itWin1 )
113     {
114         // Get the anchors of the layout associated to the window
115         const AncList_t &ancList1 =
116             (*itWin1)->getActiveLayout().getAnchorList();
117
118         // Iterate through all the windows, starting with (*itWin1)
119         for( itWin2 = itWin1; itWin2 != m_allWindows.end(); ++itWin2 )
120         {
121             // A window can't anchor itself...
122             if( (*itWin2) == (*itWin1) )
123                 continue;
124
125             // Now, check for anchoring between the 2 windows
126             const AncList_t &ancList2 =
127                 (*itWin2)->getActiveLayout().getAnchorList();
128             for( itAnc1 = ancList1.begin(); itAnc1 != ancList1.end(); ++itAnc1 )
129             {
130                 for( itAnc2 = ancList2.begin();
131                      itAnc2 != ancList2.end(); ++itAnc2 )
132                 {
133                     if( (*itAnc1)->isHanging( **itAnc2 ) )
134                     {
135                         // (*itWin1) anchors (*itWin2)
136                         m_dependencies[*itWin1].insert( *itWin2 );
137                     }
138                     else if( (*itAnc2)->isHanging( **itAnc1 ) )
139                     {
140                         // (*itWin2) anchors (*itWin1)
141                         m_dependencies[*itWin2].insert( *itWin1 );
142                     }
143                 }
144             }
145         }
146     }
147 }
148
149
150 void WindowManager::move( TopWindow &rWindow, int left, int top ) const
151 {
152     // Compute the real move offset
153     int xOffset = left - rWindow.getLeft();
154     int yOffset = top - rWindow.getTop();
155
156     // Check anchoring; this can change the values of xOffset and yOffset
157     checkAnchors( &rWindow, xOffset, yOffset );
158
159     // Move all the windows
160     WinSet_t::const_iterator it;
161     for( it = m_movingWindows.begin(); it != m_movingWindows.end(); ++it )
162     {
163         (*it)->move( (*it)->getLeft() + xOffset, (*it)->getTop() + yOffset );
164     }
165 }
166
167
168 void WindowManager::startResize( GenericLayout &rLayout, Direction_t direction )
169 {
170     m_direction = direction;
171
172     // Rebuild the set of moving windows.
173     // From the resized window, we only take into account the anchors which
174     // are mobile with the current type of resizing, and that are hanging a
175     // window. The hanged windows will come will all their dependencies.
176
177     m_resizeMovingE.clear();
178     m_resizeMovingS.clear();
179     m_resizeMovingSE.clear();
180
181     WinSet_t::const_iterator itWin;
182     AncList_t::const_iterator itAnc1, itAnc2;
183     // Get the anchors of the layout
184     const AncList_t &ancList1 = rLayout.getAnchorList();
185
186     // Iterate through all the hanged windows
187     for( itWin = m_dependencies[rLayout.getWindow()].begin();
188          itWin != m_dependencies[rLayout.getWindow()].end(); ++itWin )
189     {
190         // Now, check for anchoring between the 2 windows
191         const AncList_t &ancList2 =
192             (*itWin)->getActiveLayout().getAnchorList();
193         for( itAnc1 = ancList1.begin(); itAnc1 != ancList1.end(); ++itAnc1 )
194         {
195             for( itAnc2 = ancList2.begin();
196                  itAnc2 != ancList2.end(); ++itAnc2 )
197             {
198                 if( (*itAnc1)->isHanging( **itAnc2 ) )
199                 {
200                     // Add the dependencies of the hanged window to one of the
201                     // lists of moving windows
202                     Position::Ref_t aRefPos =
203                         (*itAnc1)->getPosition().getRefLeftTop();
204                     if( aRefPos == Position::kRightTop )
205                         buildDependSet( m_resizeMovingE, *itWin );
206                     else if( aRefPos == Position::kLeftBottom )
207                         buildDependSet( m_resizeMovingS, *itWin );
208                     else if( aRefPos == Position::kRightBottom )
209                         buildDependSet( m_resizeMovingSE, *itWin );
210                     break;
211                 }
212             }
213         }
214     }
215
216     // The checkAnchors() method will need to have m_movingWindows properly set
217     // so let's insert in it the contents of the other sets
218     m_movingWindows.clear();
219     m_movingWindows.insert( rLayout.getWindow() );
220     m_movingWindows.insert( m_resizeMovingE.begin(), m_resizeMovingE.end() );
221     m_movingWindows.insert( m_resizeMovingS.begin(), m_resizeMovingS.end() );
222     m_movingWindows.insert( m_resizeMovingSE.begin(), m_resizeMovingSE.end() );
223 }
224
225
226 void WindowManager::stopResize()
227 {
228     // Nothing different from stopMove(), luckily
229     stopMove();
230 }
231
232
233 void WindowManager::resize( GenericLayout &rLayout,
234                             int width, int height ) const
235 {
236     // TODO: handle anchored windows
237     // Compute the real resizing offset
238     int xOffset = width - rLayout.getWidth();
239     int yOffset = height - rLayout.getHeight();
240
241     // Check anchoring; this can change the values of xOffset and yOffset
242     checkAnchors( rLayout.getWindow(), xOffset, yOffset );
243     if( m_direction == kResizeS )
244         xOffset = 0;
245     if( m_direction == kResizeE )
246         yOffset = 0;
247
248     int newWidth = rLayout.getWidth() + xOffset;
249     int newHeight = rLayout.getHeight() + yOffset;
250
251     // Check boundaries
252     if( newWidth < rLayout.getMinWidth() )
253     {
254         newWidth = rLayout.getMinWidth();
255     }
256     if( newWidth > rLayout.getMaxWidth() )
257     {
258         newWidth = rLayout.getMaxWidth();
259     }
260     if( newHeight < rLayout.getMinHeight() )
261     {
262         newHeight = rLayout.getMinHeight();
263     }
264     if( newHeight > rLayout.getMaxHeight() )
265     {
266         newHeight = rLayout.getMaxHeight();
267     }
268
269     if( newWidth == rLayout.getWidth() && newHeight == rLayout.getHeight() )
270     {
271         return;
272     }
273
274     // New offset, after the last corrections
275     int xNewOffset = newWidth - rLayout.getWidth();
276     int yNewOffset = newHeight - rLayout.getHeight();
277
278     // Do the actual resizing
279     rLayout.resize( newWidth, newHeight );
280
281     // Move all the anchored windows
282     WinSet_t::const_iterator it;
283     if( m_direction == kResizeE ||
284         m_direction == kResizeSE )
285     {
286         for( it = m_resizeMovingE.begin(); it != m_resizeMovingE.end(); ++it )
287         {
288             (*it)->move( (*it)->getLeft() + xNewOffset,
289                          (*it)->getTop() );
290         }
291     }
292     if( m_direction == kResizeE ||
293         m_direction == kResizeSE )
294     {
295         for( it = m_resizeMovingS.begin(); it != m_resizeMovingS.end(); ++it )
296         {
297             (*it)->move( (*it)->getLeft(),
298                          (*it)->getTop( )+ yNewOffset );
299         }
300     }
301     if( m_direction == kResizeE ||
302         m_direction == kResizeS ||
303         m_direction == kResizeSE )
304     {
305         for( it = m_resizeMovingSE.begin(); it != m_resizeMovingSE.end(); ++it )
306         {
307             (*it)->move( (*it)->getLeft() + xNewOffset,
308                          (*it)->getTop() + yNewOffset );
309         }
310     }
311 }
312
313
314 void WindowManager::maximize( TopWindow &rWindow )
315 {
316     // Save the current position/size of the window, to be able to restore it
317     m_maximizeRect = SkinsRect( rWindow.getLeft(), rWindow.getTop(),
318                                rWindow.getLeft() + rWindow.getWidth(),
319                                rWindow.getTop() + rWindow.getHeight() );
320
321     SkinsRect workArea = OSFactory::instance( getIntf() )->getWorkArea();
322     // Move the window
323     startMove( rWindow );
324     move( rWindow, workArea.getLeft(), workArea.getTop() );
325     stopMove();
326     // Now resize it
327     // FIXME: Ugly const_cast
328     GenericLayout &rLayout = (GenericLayout&)rWindow.getActiveLayout();
329     startResize( rLayout, kResizeSE );
330     resize( rLayout, workArea.getWidth(), workArea.getHeight() );
331     stopResize();
332     rWindow.m_pVarMaximized->set( true );
333
334     // Make the window unmovable by unregistering it
335 //     unregisterWindow( rWindow );
336 }
337
338
339 void WindowManager::unmaximize( TopWindow &rWindow )
340 {
341     // Register the window to allow moving it
342 //     registerWindow( rWindow );
343
344     // Resize the window
345     // FIXME: Ugly const_cast
346     GenericLayout &rLayout = (GenericLayout&)rWindow.getActiveLayout();
347     startResize( rLayout, kResizeSE );
348     resize( rLayout, m_maximizeRect.getWidth(), m_maximizeRect.getHeight() );
349     stopResize();
350     // Now move it
351     startMove( rWindow );
352     move( rWindow, m_maximizeRect.getLeft(), m_maximizeRect.getTop() );
353     stopMove();
354     rWindow.m_pVarMaximized->set( false );
355 }
356
357
358 void WindowManager::synchVisibility() const
359 {
360     WinSet_t::const_iterator it;
361     for( it = m_allWindows.begin(); it != m_allWindows.end(); ++it )
362     {
363         // Show the window if it has to be visible
364         if( (*it)->getVisibleVar().get() )
365         {
366             (*it)->innerShow();
367         }
368     }
369 }
370
371
372 void WindowManager::saveVisibility()
373 {
374     WinSet_t::const_iterator it;
375     m_savedWindows.clear();
376     for( it = m_allWindows.begin(); it != m_allWindows.end(); ++it )
377     {
378         // Remember the window if it is visible
379         if( (*it)->getVisibleVar().get() )
380         {
381             m_savedWindows.insert( *it );
382         }
383     }
384 }
385
386
387 void WindowManager::restoreVisibility() const
388 {
389     // Warning in case we never called saveVisibility()
390     if( m_savedWindows.size() == 0 )
391     {
392         msg_Warn( getIntf(), "restoring visibility for no window" );
393     }
394
395     WinSet_t::const_iterator it;
396     for( it = m_savedWindows.begin(); it != m_savedWindows.end(); ++it )
397     {
398         (*it)->show();
399     }
400 }
401
402
403 void WindowManager::raiseAll() const
404 {
405     // Raise all the windows
406     WinSet_t::const_iterator it;
407     for( it = m_allWindows.begin(); it != m_allWindows.end(); ++it )
408     {
409         (*it)->raise();
410     }
411 }
412
413
414 void WindowManager::showAll( bool firstTime ) const
415 {
416     // Show all the windows
417     WinSet_t::const_iterator it;
418     for( it = m_allWindows.begin(); it != m_allWindows.end(); ++it )
419     {
420         // When the theme is opened for the first time,
421         // only show the window if set as visible in the XML
422         if( (*it)->getInitialVisibility() || !firstTime )
423         {
424             (*it)->show();
425         }
426     }
427 }
428
429
430 void WindowManager::show( TopWindow &rWindow ) const
431 {
432     rWindow.show();
433
434     if( isOpacityNeeded() )
435         rWindow.setOpacity( m_alpha );
436 }
437
438
439 void WindowManager::hideAll() const
440 {
441     WinSet_t::const_iterator it;
442     for( it = m_allWindows.begin(); it != m_allWindows.end(); ++it )
443     {
444         (*it)->hide();
445     }
446 }
447
448
449 void WindowManager::setOnTop( bool b_ontop )
450 {
451     // Update the boolean variable
452     VarBoolImpl *pVarOnTop = (VarBoolImpl*)m_cVarOnTop.get();
453     pVarOnTop->set( b_ontop );
454
455     // set/unset the "on top" status
456     WinSet_t::const_iterator it;
457     for( it = m_allWindows.begin(); it != m_allWindows.end(); ++it )
458     {
459         (*it)->toggleOnTop( b_ontop );
460     }
461 }
462
463
464 void WindowManager::toggleOnTop()
465 {
466     VarBoolImpl *pVarOnTop = (VarBoolImpl*)m_cVarOnTop.get();
467
468     setOnTop( !pVarOnTop->get() );
469 }
470
471
472 void WindowManager::buildDependSet( WinSet_t &rWinSet,
473                                     TopWindow *pWindow )
474 {
475     // pWindow is in the set
476     rWinSet.insert( pWindow );
477
478     // Iterate through the anchored windows
479     const WinSet_t &anchored = m_dependencies[pWindow];
480     WinSet_t::const_iterator iter;
481     for( iter = anchored.begin(); iter != anchored.end(); ++iter )
482     {
483         // Check that the window isn't already in the set before adding it
484         if( rWinSet.find( *iter ) == rWinSet.end() )
485         {
486             buildDependSet( rWinSet, *iter );
487         }
488     }
489 }
490
491
492 void WindowManager::checkAnchors( TopWindow *pWindow,
493                                   int &xOffset, int &yOffset ) const
494 {
495     (void)pWindow;
496     WinSet_t::const_iterator itMov, itSta;
497     AncList_t::const_iterator itAncMov, itAncSta;
498
499     // Check magnetism with screen edges first (actually it is the work area)
500     SkinsRect workArea = OSFactory::instance( getIntf() )->getWorkArea();
501     // Iterate through the moving windows
502     for( itMov = m_movingWindows.begin();
503          itMov != m_movingWindows.end(); ++itMov )
504     {
505         // Skip the invisible windows
506         if( ! (*itMov)->getVisibleVar().get() )
507         {
508             continue;
509         }
510
511         int newLeft = (*itMov)->getLeft() + xOffset;
512         int newTop = (*itMov)->getTop() + yOffset;
513         if( newLeft > workArea.getLeft() - m_magnet &&
514             newLeft < workArea.getLeft() + m_magnet )
515         {
516             xOffset = workArea.getLeft() - (*itMov)->getLeft();
517         }
518         if( newTop > workArea.getTop() - m_magnet &&
519             newTop < workArea.getTop() + m_magnet )
520         {
521             yOffset = workArea.getTop() - (*itMov)->getTop();
522         }
523         int right = workArea.getLeft() + workArea.getWidth();
524         if( newLeft + (*itMov)->getWidth() > right - m_magnet &&
525             newLeft + (*itMov)->getWidth() < right + m_magnet )
526         {
527             xOffset = right - (*itMov)->getLeft() - (*itMov)->getWidth();
528         }
529         int bottom = workArea.getTop() + workArea.getHeight();
530         if( newTop + (*itMov)->getHeight() > bottom - m_magnet &&
531             newTop + (*itMov)->getHeight() <  bottom + m_magnet )
532         {
533             yOffset =  bottom - (*itMov)->getTop() - (*itMov)->getHeight();
534         }
535     }
536
537     // Iterate through the moving windows
538     for( itMov = m_movingWindows.begin();
539          itMov != m_movingWindows.end(); ++itMov )
540     {
541         // Skip the invisible windows
542         if( ! (*itMov)->getVisibleVar().get() )
543         {
544             continue;
545         }
546
547         // Get the anchors in the main layout of this moving window
548         const AncList_t &movAnchors =
549             (*itMov)->getActiveLayout().getAnchorList();
550
551         // Iterate through the static windows
552         for( itSta = m_allWindows.begin();
553              itSta != m_allWindows.end(); ++itSta )
554         {
555             // Skip the moving windows and the invisible ones
556             if( m_movingWindows.find( (*itSta) ) != m_movingWindows.end() ||
557                 ! (*itSta)->getVisibleVar().get() )
558             {
559                 continue;
560             }
561
562             // Get the anchors in the main layout of this static window
563             const AncList_t &staAnchors =
564                 (*itSta)->getActiveLayout().getAnchorList();
565
566             // Check if there is an anchoring between one of the movAnchors
567             // and one of the staAnchors
568             for( itAncMov = movAnchors.begin();
569                  itAncMov != movAnchors.end(); ++itAncMov )
570             {
571                 for( itAncSta = staAnchors.begin();
572                      itAncSta != staAnchors.end(); ++itAncSta )
573                 {
574                     if( (*itAncSta)->canHang( **itAncMov, xOffset, yOffset ) )
575                     {
576                         // We have found an anchoring!
577                         // There is nothing to do here, since xOffset and
578                         // yOffset are automatically modified by canHang()
579
580                         // Don't check the other anchors, one is enough...
581                         return;
582                     }
583                     else
584                     {
585                         // Temporary variables
586                         int xOffsetTemp = -xOffset;
587                         int yOffsetTemp = -yOffset;
588                         if( (*itAncMov)->canHang( **itAncSta, xOffsetTemp,
589                                                   yOffsetTemp ) )
590                         {
591                             // We have found an anchoring!
592                             // xOffsetTemp and yOffsetTemp have been updated,
593                             // we just need to change xOffset and yOffset
594                             xOffset = -xOffsetTemp;
595                             yOffset = -yOffsetTemp;
596
597                             // Don't check the other anchors, one is enough...
598                             return;
599                         }
600                     }
601                 }
602             }
603         }
604     }
605 }
606
607
608 void WindowManager::createTooltip( const GenericFont &rTipFont )
609 {
610     // Create the tooltip window
611     if( !m_pTooltip )
612     {
613         m_pTooltip = new Tooltip( getIntf(), rTipFont, 500 );
614     }
615     else
616     {
617         msg_Warn( getIntf(), "tooltip already created!" );
618     }
619 }
620
621
622 void WindowManager::showTooltip()
623 {
624     if( m_pTooltip )
625     {
626         m_pTooltip->show();
627     }
628 }
629
630
631 void WindowManager::hideTooltip()
632 {
633     if( m_pTooltip )
634     {
635         m_pTooltip->hide();
636     }
637 }
638
639
640 void WindowManager::addLayout( TopWindow &rWindow, GenericLayout &rLayout )
641 {
642     rWindow.setActiveLayout( &rLayout );
643 }
644
645
646 void WindowManager::setActiveLayout( TopWindow &rWindow,
647                                      GenericLayout &rLayout )
648 {
649     rWindow.setActiveLayout( &rLayout );
650     // Rebuild the dependencies
651     stopMove();
652 }
653