]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
Qt4 : remove useless files.
[vlc] / modules / gui / qt4 / main_interface.cpp
1 /*****************************************************************************
2  * main_interface.cpp : Main interface
3  ****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
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 "qt4.hpp"
26 #include "main_interface.hpp"
27 #include "input_manager.hpp"
28 #include "util/qvlcframe.hpp"
29 #include "util/customwidgets.hpp"
30 #include "dialogs_provider.hpp"
31 #include "components/interface_widgets.hpp"
32 #include "dialogs/playlist.hpp"
33 #include "menus.hpp"
34
35 #include <QMenuBar>
36 #include <QCloseEvent>
37 #include <QPushButton>
38 #include <QStatusBar>
39 #include <QKeyEvent>
40 #include <QUrl>
41 #include <QSystemTrayIcon>
42 #include <QSize>
43 #include <QMenu>
44
45 #include <assert.h>
46 #include <vlc_keys.h>
47 #include <vlc_vout.h>
48
49 #ifdef WIN32
50     #define PREF_W 410
51     #define PREF_H 121
52 #else
53     #define PREF_W 450
54     #define PREF_H 125
55 #endif
56
57 #define VISIBLE(i) (i && i->isVisible())
58
59 #define SET_WIDTH(i,j) i->widgetSize.setWidth(j)
60 #define SET_HEIGHT(i,j) i->widgetSize.setHeight(j)
61 #define SET_WH( i,j,k) i->widgetSize.setWidth(j); i->widgetSize.setHeight(k);
62
63 #define DS(i) i.width(),i.height()
64
65 #define DEBUG_COLOR 0
66
67 /* Callback prototypes */
68 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
69                         vlc_value_t old_val, vlc_value_t new_val, void *param );
70 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
71                        vlc_value_t old_val, vlc_value_t new_val, void *param );
72 static int InteractCallback( vlc_object_t *, const char *, vlc_value_t,
73                              vlc_value_t, void *);
74 /* Video handling */
75 static void *DoRequest( intf_thread_t *p_intf, vout_thread_t *p_vout,
76                         int *pi1, int *pi2, unsigned int*pi3,unsigned int*pi4)
77 {
78     return p_intf->p_sys->p_mi->requestVideo( p_vout, pi1, pi2, pi3, pi4 );
79 }
80 static void DoRelease( intf_thread_t *p_intf, void *p_win )
81 {
82     return p_intf->p_sys->p_mi->releaseVideo( p_win );
83 }
84 static int DoControl( intf_thread_t *p_intf, void *p_win, int i_q, va_list a )
85 {
86     return p_intf->p_sys->p_mi->controlVideo( p_win, i_q, a );
87 }
88
89 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
90 {
91     /* Variables initialisation */
92     need_components_update = false;
93     bgWidget = NULL; videoWidget = NULL; playlistWidget = NULL;
94     embeddedPlaylistWasActive = videoIsActive = false;
95
96     bool b_createSystray = false;
97     input_name = "";
98
99     /**
100      *  Configuration and settings
101      **/
102     settings = new QSettings( "VideoLAN", "VLC" );
103     settings->beginGroup( "MainWindow" );
104
105     /* Main settings */
106     setFocusPolicy( Qt::StrongFocus );
107     setAcceptDrops(true);
108     setWindowIcon( QApplication::windowIcon() );
109
110     /* Set The Video In emebedded Mode or not */
111     videoEmbeddedFlag = false;
112     if( config_GetInt( p_intf, "embedded-video" ) )
113         videoEmbeddedFlag = true;
114
115     alwaysVideoFlag = false;
116     if( videoEmbeddedFlag && config_GetInt( p_intf, "qt-always-video" ) )
117         alwaysVideoFlag = true;
118
119     /* Set the other interface settings */
120     playlistEmbeddedFlag = settings->value("playlist-embedded", true).toBool();
121     advControlsEnabled= settings->value( "adv-controls", false ).toBool();
122     visualSelectorEnabled= settings->value( "visual-selector", false ).toBool();
123
124 #if DEBUG_COLOR
125     QPalette palette2;
126     palette2.setColor(this->backgroundRole(), Qt::magenta);
127     setPalette(palette2);
128 #endif
129
130     /**
131      *  UI design
132      **/
133     setVLCWindowsTitle();
134     handleMainUi( settings );
135
136     /* Menu Bar */
137     QVLCMenu::createMenuBar( this, p_intf, playlistEmbeddedFlag,
138                              advControlsEnabled, visualSelectorEnabled );
139
140     /* Status Bar */
141     timeLabel = new QLabel;
142     nameLabel = new QLabel;
143     speedLabel = new QLabel( "1.0x" );
144     timeLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
145     speedLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
146     statusBar()->addWidget( nameLabel, 8 );
147     statusBar()->addPermanentWidget( speedLabel, 0 );
148     statusBar()->addPermanentWidget( timeLabel, 2 );
149     speedLabel->setContextMenuPolicy ( Qt::CustomContextMenu );
150     timeLabel->setContextMenuPolicy ( Qt::CustomContextMenu );
151     CONNECT( speedLabel, customContextMenuRequested( QPoint ),
152              this, showSpeedMenu( QPoint ) );
153     CONNECT( timeLabel, customContextMenuRequested( QPoint ),
154              this, showTimeMenu( QPoint ) );
155
156     /* Systray */
157     sysTray = NULL;
158     if( config_GetInt( p_intf, "qt-start-minimized") )
159     {
160         hide();
161         b_createSystray = true;
162     }
163     if( config_GetInt( p_intf, "qt-system-tray") )
164         b_createSystray = true;
165     if (QSystemTrayIcon::isSystemTrayAvailable() && b_createSystray )
166             createSystray();
167
168     /* Init input manager */
169     MainInputManager::getInstance( p_intf );
170     ON_TIMEOUT( updateOnTimer() );
171
172     /**
173      * CONNECTs
174      **/
175
176     /* Connect the input manager to the GUI elements it manages */
177     /* It is also connected to the control->slider */
178     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
179              this, setDisplay( float, int, int ) );
180
181     /* Naming in the controller */
182     CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
183              setName( QString ) );
184     if( config_GetInt( p_intf, "qt-system-tray" ) && sysTray )
185     {
186         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
187                  updateSystrayTooltipName( QString ) );
188     }
189     if( config_GetInt( p_intf, "qt-name-in-title" ) )
190     {
191         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
192              setVLCWindowsTitle( QString ) );
193     }
194
195     /* PLAY_STATUS */
196     CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
197     if( config_GetInt( p_intf, "qt-system-tray" ) && sysTray )
198     {
199         CONNECT( THEMIM->getIM(), statusChanged( int ), this,
200                  updateSystrayTooltipStatus( int ) );
201     }
202
203     /**
204      * Callbacks
205      **/
206     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
207     var_AddCallback( p_intf, "interaction", InteractCallback, this );
208     p_intf->b_interaction = VLC_TRUE;
209
210     /* Register callback for the intf-popupmenu variable */
211     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
212                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
213     if( p_playlist != NULL )
214     {
215         var_AddCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
216         var_AddCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
217         vlc_object_release( p_playlist );
218     }
219 }
220
221 MainInterface::~MainInterface()
222 {
223     /* Unregister callback for the intf-popupmenu variable */
224     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
225                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
226     if( p_playlist != NULL )
227     {
228         var_DelCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
229         var_DelCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
230         vlc_object_release( p_playlist );
231     }
232
233     settings->setValue( "playlist-embedded", playlistEmbeddedFlag );
234     settings->setValue( "adv-controls", advControlsEnabled );
235     settings->setValue( "pos", pos() );
236     settings->endGroup();
237     delete settings;
238     p_intf->b_interaction = VLC_FALSE;
239     var_DelCallback( p_intf, "interaction", InteractCallback, this );
240
241     p_intf->pf_request_window = NULL;
242     p_intf->pf_release_window = NULL;
243     p_intf->pf_control_window = NULL;
244 }
245
246 /*****************************
247  *   Main UI handling        *
248  *****************************/
249
250 /**
251  * Give the decorations of the Main Window a correct Name.
252  * If nothing is given, set it to VLC...
253  **/
254 void MainInterface::setVLCWindowsTitle( QString aTitle )
255 {
256     if( aTitle.isEmpty() )
257     {
258         setWindowTitle( qtr( "VLC media player" ) );
259     }
260     else
261     {
262         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
263     }
264 }
265
266 void MainInterface::handleMainUi( QSettings *settings )
267 {
268     QWidget *main = new QWidget( this );
269     mainLayout = new QVBoxLayout( main );
270     setCentralWidget( main );
271
272     /* Margins, spacing */
273     main->setContentsMargins( 0, 0, 0, 0 );
274     mainLayout->setMargin( 0 );
275
276     /* CONTROLS */
277     controls = new ControlsWidget( p_intf );
278
279     /* Configure the UI */
280     BUTTON_SET_IMG( controls->playlistButton, "" , playlist_icon.png,
281                         playlistEmbeddedFlag ?  qtr( "Show playlist" ) :
282                                                 qtr( "Open playlist" ) );
283     BUTTONACT( controls->playlistButton, playlist() );
284
285 #if DEBUG_COLOR
286     QPalette palette;
287     palette.setColor(main->backgroundRole(), Qt::green);
288     main->setPalette(palette);
289 #endif
290
291     /* Add the controls Widget */
292     mainLayout->addWidget( controls );
293
294     /* Set initial size */
295     resize ( PREF_W, PREF_H );
296
297     addSize = QSize( mainLayout->margin() * 2, PREF_H );
298
299     /* advanced Controls handling */
300     advControls = new AdvControlsWidget( p_intf );
301     mainLayout->insertWidget( 0, advControls );
302     advControls->updateGeometry();
303     if( !advControlsEnabled ) advControls->hide();
304     need_components_update = true;
305
306     /* Visualisation */
307     visualSelector = new VisualSelector( p_intf );
308     mainLayout->insertWidget( 0, visualSelector );
309     visualSelector->hide();
310
311     /* And video Outputs */
312     if( alwaysVideoFlag )
313     {
314         bgWidget = new BackgroundWidget( p_intf );
315         bgWidget->widgetSize = settings->value( "backgroundSize",
316                                            QSize( 300, 300 ) ).toSize();
317         bgWidget->resize( bgWidget->widgetSize );
318         bgWidget->updateGeometry();
319         mainLayout->insertWidget( 0, bgWidget );
320     }
321
322     if( videoEmbeddedFlag )
323     {
324         videoWidget = new VideoWidget( p_intf );
325         videoWidget->widgetSize = QSize( 1, 1 );
326         //videoWidget->resize( videoWidget->widgetSize );
327         mainLayout->insertWidget( 0, videoWidget );
328
329         p_intf->pf_request_window  = ::DoRequest;
330         p_intf->pf_release_window  = ::DoRelease;
331         p_intf->pf_control_window  = ::DoControl;
332     }
333
334     /* Finish the sizing */
335     setMinimumSize( PREF_W, addSize.height() );
336 }
337
338 /**********************************************************************
339  * Handling of the components
340  **********************************************************************/
341 void MainInterface::calculateInterfaceSize()
342 {
343     int width = 0, height = 0;
344     if( VISIBLE( bgWidget ) )
345     {
346         width = bgWidget->widgetSize.width();
347         height = bgWidget->widgetSize.height();
348         assert( !(playlistWidget && playlistWidget->isVisible() ) );
349     }
350     else if( VISIBLE( playlistWidget ) )
351     {
352         width = playlistWidget->widgetSize.width();
353         height = playlistWidget->widgetSize.height();
354     }
355     else if( videoIsActive )
356     {
357         width =  videoWidget->widgetSize.width() ;
358         height = videoWidget->widgetSize.height();
359     }
360     else
361     {
362         width = PREF_W - addSize.width();
363         height = PREF_H - addSize.height();
364     }
365     if( VISIBLE( visualSelector ) )
366         height += visualSelector->height();
367     if( VISIBLE( advControls) )
368     {
369         height += advControls->sizeHint().height();
370     }
371     mainSize = QSize( width + addSize.width(), height + addSize.height() );
372 }
373
374 void MainInterface::resizeEvent( QResizeEvent *e )
375 {
376     videoWidget->widgetSize.setWidth(  e->size().width() - addSize.width() );
377     if( videoWidget && videoIsActive && videoWidget->widgetSize.height() > 1 )
378     {
379         SET_WH( videoWidget, e->size().width() - addSize.width(),
380                              e->size().height()  - addSize.height() );
381         videoWidget->updateGeometry();
382     }
383     if( VISIBLE( playlistWidget ) )
384     {
385         SET_WH( playlistWidget , e->size().width() - addSize.width(),
386                                  e->size().height() - addSize.height() );
387         playlistWidget->updateGeometry();
388     }
389 }
390 /****************************************************************************
391  * Small right-click menus
392  ****************************************************************************/
393 void MainInterface::showSpeedMenu( QPoint pos )
394 {
395     QMenu menu( this );
396     menu.addAction( "Not Implemented Yet" );
397     menu.exec( QCursor::pos() );
398 }
399
400 void MainInterface::showTimeMenu( QPoint pos )
401 {
402     QMenu menu( this );
403     menu.addAction( "Not Implemented Yet" );
404     menu.exec( QCursor::pos() );
405 }
406
407 /*****************************************************************************
408  * Systray Icon and Systray Menu
409  *****************************************************************************/
410
411 /**
412  * Create a SystemTray icon and a menu that would go with it.
413  * Connects to a click handler on the icon.
414  **/
415 void MainInterface::createSystray()
416 {
417     QIcon iconVLC =  QIcon( QPixmap( ":/vlc128.png" ) );
418     sysTray = new QSystemTrayIcon( iconVLC, this );
419     sysTray->setToolTip( qtr( "VLC media player" ));
420
421     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
422     systrayMenu->setIcon( iconVLC );
423
424     QVLCMenu::updateSystrayMenu( this, p_intf, true );
425     sysTray->show();
426
427     CONNECT( sysTray, activated(  QSystemTrayIcon::ActivationReason ),
428             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
429 }
430
431 /**
432  * Update the menu of the Systray Icon.
433  * May be unneedded, since it just calls QVLCMenu::update
434  * FIXME !!!
435  **/
436 void MainInterface::updateSystrayMenu( int status )
437 {
438     QVLCMenu::updateSystrayMenu( this, p_intf ) ;
439 }
440
441 /**
442  * Updates the Systray Icon's menu and toggle the main interface
443  */
444 void MainInterface::toggleUpdateSystrayMenu()
445 {
446     if( isHidden() )
447     {
448         show();
449         activateWindow();
450     }
451     else
452     {
453 #ifdef WIN32
454         /* check if any visible window is above vlc in the z-order,
455          * but ignore the ones always on top */
456         WINDOWINFO wi;
457         HWND hwnd;
458         wi.cbSize = sizeof( WINDOWINFO );
459         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
460                 hwnd && !IsWindowVisible( hwnd );
461                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
462         if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
463                 (wi.dwExStyle&WS_EX_TOPMOST) )
464 #else
465         if( isActiveWindow() )
466 #endif
467         {
468             hide();
469         }
470         else
471         {
472             activateWindow();
473         }
474     }
475     QVLCMenu::updateSystrayMenu( this, p_intf );
476 }
477
478 void MainInterface::handleSystrayClick(
479                                     QSystemTrayIcon::ActivationReason reason )
480 {
481     switch( reason )
482     {
483         case QSystemTrayIcon::Trigger:
484             toggleUpdateSystrayMenu();
485             break;
486         case QSystemTrayIcon::MiddleClick:
487             sysTray->showMessage( qtr( "VLC media player" ),
488                     qtr( "Control menu for the player" ),
489                     QSystemTrayIcon::Information, 4000 );
490             break;
491     }
492 }
493
494 /**
495  * Updates the name of the systray Icon tooltip.
496  * Doesn't check if the systray exists, check before you call it.
497  * FIXME !!! Fusion with next function ?
498  **/
499 void MainInterface::updateSystrayTooltipName( QString name )
500 {
501     if( name.isEmpty() )
502     {
503         sysTray->setToolTip( qtr( "VLC media player" ) );
504     }
505     else
506     {
507         sysTray->setToolTip( name );
508     }
509 }
510
511 /**
512  * Updates the status of the systray Icon tooltip.
513  * Doesn't check if the systray exists, check before you call it.
514  **/
515 void MainInterface::updateSystrayTooltipStatus( int i_status )
516 {
517     switch( i_status )
518     {
519         case  0:
520             {
521                 sysTray->setToolTip( qtr( "VLC media player" ) );
522                 break;
523             }
524         case PLAYING_S:
525             {
526                 sysTray->setToolTip( input_name );
527                 //+ " - " + qtr( "Playing" ) );
528                 break;
529             }
530         case PAUSE_S:
531             {
532                 sysTray->setToolTip( input_name + " - "
533                         + qtr( "Paused") );
534                 break;
535             }
536     }
537 }
538 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
539                                    int *pi_y, unsigned int *pi_width,
540                                    unsigned int *pi_height )
541 {
542     void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
543     if( ret )
544     {
545         videoIsActive = true;
546         if( VISIBLE( playlistWidget ) )
547         {
548             embeddedPlaylistWasActive = true;
549 //            playlistWidget->hide();
550         }
551         bool bgWasVisible = false;
552         if( VISIBLE( bgWidget) )
553         {
554             bgWasVisible = true;
555             bgWidget->hide();
556         }
557         if( THEMIM->getIM()->hasVideo() || !bgWasVisible )
558         {
559             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
560         }
561         else /* Background widget available, use its size */
562         {
563             /* Ok, our visualizations are bad, so don't do this for the moment
564              * use the requested size anyway */
565             // videoWidget->widgetSize = bgWidget->widgeTSize;
566             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
567         }
568 //        videoWidget->updateGeometry(); /// FIXME: Needed ?
569         need_components_update = true;
570     }
571     return ret;
572 }
573
574 void MainInterface::releaseVideo( void *p_win )
575 {
576     videoWidget->release( p_win );
577     videoWidget->widgetSize = QSize( 0, 0 );
578     videoWidget->resize( videoWidget->widgetSize );
579
580     if( embeddedPlaylistWasActive )
581         playlistWidget->show();
582     else if( bgWidget )
583         bgWidget->show();
584
585     videoIsActive = false;
586     need_components_update = true;
587 }
588
589 class SetVideoOnTopQtEvent : public QEvent
590 {
591 public:
592     SetVideoOnTopQtEvent( bool _onTop ) :
593       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
594     {
595     }
596
597     bool OnTop() const
598     {
599         return onTop;
600     }
601
602 private:
603     bool onTop;
604 };
605
606 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
607 {
608     int i_ret = VLC_EGENERIC;
609     switch( i_query )
610     {
611         case VOUT_GET_SIZE:
612         {
613             unsigned int *pi_width  = va_arg( args, unsigned int * );
614             unsigned int *pi_height = va_arg( args, unsigned int * );
615             *pi_width = videoWidget->widgetSize.width();
616             *pi_height = videoWidget->widgetSize.height();
617             i_ret = VLC_SUCCESS;
618             break;
619         }
620         case VOUT_SET_SIZE:
621         {
622             unsigned int i_width  = va_arg( args, unsigned int );
623             unsigned int i_height = va_arg( args, unsigned int );
624             videoWidget->widgetSize = QSize( i_width, i_height );
625             // videoWidget->updateGeometry();
626             need_components_update = true;
627             i_ret = VLC_SUCCESS;
628             break;
629         }
630         case VOUT_SET_STAY_ON_TOP:
631         {
632             int i_arg = va_arg( args, int );
633             QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
634             i_ret = VLC_SUCCESS;
635             break;
636         }
637         default:
638             msg_Warn( p_intf, "unsupported control query" );
639             break;
640     }
641     return i_ret;
642 }
643
644 void MainInterface::advanced()
645 {
646     if( !VISIBLE( advControls ) )
647     {
648         advControls->show();
649         advControlsEnabled = true;
650     }
651     else
652     {
653         advControls->hide();
654         advControlsEnabled = false;
655     }
656     doComponentsUpdate();
657 }
658
659 void MainInterface::visual()
660 {
661     if( !VISIBLE( visualSelector) )
662     {
663         visualSelector->show();
664         if( !THEMIM->getIM()->hasVideo() )
665         {
666             /* Show the background widget */
667         }
668         visualSelectorEnabled = true;
669     }
670     else
671     {
672         /* Stop any currently running visualization */
673         visualSelector->hide();
674         visualSelectorEnabled = false;
675     }
676     doComponentsUpdate();
677 }
678
679 void MainInterface::playlist()
680 {
681     // Toggle the playlist dialog
682     if( !playlistEmbeddedFlag )
683     {
684         if( playlistWidget )
685         {
686             /// \todo Destroy it
687         }
688         THEDP->playlistDialog();
689         return;
690     }
691
692     if( !playlistWidget )
693     {
694         PlaylistDialog::killInstance();
695         playlistWidget = new PlaylistWidget( p_intf );
696         mainLayout->insertWidget( 0, playlistWidget );
697         playlistWidget->widgetSize = settings->value( "playlistSize",
698                                                QSize( 650, 310 ) ).toSize();
699         playlistWidget->hide();
700         if(bgWidget)
701         CONNECT( playlistWidget, artSet( QString ), bgWidget, setArt(QString) );
702     }
703     if( VISIBLE( playlistWidget ) )
704     {
705         playlistWidget->hide();
706         if( bgWidget ) bgWidget->show();
707         if( videoIsActive )
708         {
709             videoWidget->widgetSize = savedVideoSize;
710             videoWidget->resize( videoWidget->widgetSize );
711             videoWidget->updateGeometry();
712             if( bgWidget ) bgWidget->hide();
713         }
714     }
715     else
716     {
717         playlistWidget->show();
718         if( videoIsActive )
719         {
720             savedVideoSize = videoWidget->widgetSize;
721             videoWidget->widgetSize.setHeight( 0 );
722             videoWidget->resize( videoWidget->widgetSize );
723             videoWidget->updateGeometry();
724         }
725         if( VISIBLE( bgWidget ) ) bgWidget->hide();
726     }
727     doComponentsUpdate();
728 }
729
730 /* Video widget cannot do this synchronously as it runs in another thread */
731 /* Well, could it, actually ? Probably dangerous ... */
732 void MainInterface::doComponentsUpdate()
733 {
734     calculateInterfaceSize();
735     resize( mainSize );
736 }
737
738 void MainInterface::undockPlaylist()
739 {
740     if( playlistWidget )
741     {
742         playlistWidget->hide();
743         playlistWidget->deleteLater();
744         mainLayout->removeWidget( playlistWidget );
745         playlistWidget = NULL;
746         playlistEmbeddedFlag = false;
747
748         menuBar()->clear();
749         QVLCMenu::createMenuBar( this, p_intf, false, advControlsEnabled,
750                                  visualSelectorEnabled);
751
752         if( videoIsActive )
753         {
754             videoWidget->widgetSize = savedVideoSize;
755             videoWidget->resize( videoWidget->widgetSize );
756             videoWidget->updateGeometry();
757         }
758
759         doComponentsUpdate();
760         THEDP->playlistDialog();
761     }
762 }
763
764 void MainInterface::toggleMenus()
765 {
766     if( menuBar()->isVisible() ) menuBar()->hide();
767         else menuBar()->show();
768     msg_Dbg( p_intf, "I was there: \\_o<~~ coin coin" );
769 }
770
771 void MainInterface::customEvent( QEvent *event )
772 {
773     if( event->type() == PLDockEvent_Type )
774     {
775         PlaylistDialog::killInstance();
776         playlistEmbeddedFlag = true;
777         menuBar()->clear();
778         QVLCMenu::createMenuBar(this, p_intf, true, advControlsEnabled,
779                                 visualSelectorEnabled);
780         playlist();
781     }
782     else if ( event->type() == SetVideoOnTopEvent_Type )
783     {
784         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
785         if( p_event->OnTop() )
786             setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
787         else
788             setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
789         show(); /* necessary to apply window flags?? */
790     }
791 }
792
793
794 /************************************************************************
795  * D&D
796  ************************************************************************/
797 void MainInterface::dropEvent(QDropEvent *event)
798 {
799      const QMimeData *mimeData = event->mimeData();
800
801      /* D&D of a subtitles file, add it on the fly */
802      if( mimeData->urls().size() == 1 )
803      {
804         if( THEMIM->getIM()->hasInput() )
805         {
806             if( input_AddSubtitles( THEMIM->getInput(),
807                                     qtu( mimeData->urls()[0].toString() ),
808                                     VLC_TRUE ) )
809             {
810                 event->acceptProposedAction();
811                 return;
812             }
813         }
814      }
815      bool first = true;
816      foreach( QUrl url, mimeData->urls() ) {
817         QString s = url.toString();
818         if( s.length() > 0 ) {
819             playlist_Add( THEPL, qtu(s), NULL,
820                           PLAYLIST_APPEND | (first ? PLAYLIST_GO:0),
821                           PLAYLIST_END, VLC_TRUE, VLC_FALSE );
822             first = false;
823         }
824      }
825      event->acceptProposedAction();
826 }
827 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
828 {
829      event->acceptProposedAction();
830 }
831 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
832 {
833      event->acceptProposedAction();
834 }
835 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
836 {
837      event->accept();
838 }
839
840 /************************************************************************
841  * Other stuff
842  ************************************************************************/
843 void MainInterface::keyPressEvent( QKeyEvent *e )
844 {
845     int i_vlck = qtEventToVLCKey( e );
846     if( i_vlck >= 0 )
847     {
848         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
849         e->accept();
850     }
851     else
852         e->ignore();
853 }
854
855 void MainInterface::wheelEvent( QWheelEvent *e )
856 {
857     int i_vlckey = qtWheelEventToVLCKey( e );
858     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
859     e->accept();
860 }
861
862 void MainInterface::setDisplay( float pos, int time, int length )
863 {
864     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
865     secstotimestr( psz_length, length );
866     secstotimestr( psz_time, time );
867     QString title;
868     title.sprintf( "%s/%s", psz_time, psz_length );
869     timeLabel->setText( " "+title+" " );
870 }
871
872 void MainInterface::setName( QString name )
873 {
874     input_name = name;
875     nameLabel->setText( " " + name+" " );
876 }
877
878 void MainInterface::setStatus( int status )
879 {
880     controls->setStatus( status );
881     if( systrayMenu )
882         updateSystrayMenu( status );
883 }
884
885 void MainInterface::updateOnTimer()
886 {
887     /* \todo Make this event-driven */
888     // TO MOVE TO controls
889     advControls->enableInput( THEMIM->getIM()->hasInput() );
890     controls->enableInput( THEMIM->getIM()->hasInput() );
891     advControls->enableVideo( THEMIM->getIM()->hasVideo() );
892
893     if( intf_ShouldDie( p_intf ) )
894     {
895         QApplication::closeAllWindows();
896         QApplication::quit();
897     }
898     if( need_components_update )
899     {
900         doComponentsUpdate();
901         need_components_update = false;
902     }
903
904     controls->updateOnTimer();
905 }
906
907 void MainInterface::closeEvent( QCloseEvent *e )
908 {
909     hide();
910     vlc_object_kill( p_intf );
911 }
912
913 static int InteractCallback( vlc_object_t *p_this,
914                              const char *psz_var, vlc_value_t old_val,
915                              vlc_value_t new_val, void *param )
916 {
917     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
918     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
919     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
920     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
921     return VLC_SUCCESS;
922 }
923
924 /*****************************************************************************
925  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
926  *  We don't show the menu directly here because we don't want the
927  *  caller to block for a too long time.
928  *****************************************************************************/
929 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
930                         vlc_value_t old_val, vlc_value_t new_val, void *param )
931 {
932     intf_thread_t *p_intf = (intf_thread_t *)param;
933
934     if( p_intf->pf_show_dialog )
935     {
936         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
937                                 new_val.b_bool, 0 );
938     }
939
940     return VLC_SUCCESS;
941 }
942
943 /*****************************************************************************
944  * IntfShowCB: callback triggered by the intf-show playlist variable.
945  *****************************************************************************/
946 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
947                        vlc_value_t old_val, vlc_value_t new_val, void *param )
948 {
949     intf_thread_t *p_intf = (intf_thread_t *)param;
950     //p_intf->p_sys->b_intf_show = VLC_TRUE;
951
952     return VLC_SUCCESS;
953 }