Qt4 - Hide/Show from taskBar for Windows.
[vlc.git] / 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  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include "qt4.hpp"
25 #include "main_interface.hpp"
26 #include "input_manager.hpp"
27 #include "util/input_slider.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
150     /* Systray */
151     sysTray = NULL;
152     if( config_GetInt( p_intf, "qt-start-minimized") )
153     {
154         hide();
155         b_createSystray = true;
156     }
157     if( config_GetInt( p_intf, "qt-system-tray") )
158         b_createSystray = true;
159     if (QSystemTrayIcon::isSystemTrayAvailable() && b_createSystray )
160             createSystray();
161
162     /* Init input manager */
163     MainInputManager::getInstance( p_intf );
164     ON_TIMEOUT( updateOnTimer() );
165
166     /**
167      * CONNECTs
168      **/
169
170     /* Volume control */
171     CONNECT( ui.volumeSlider, valueChanged(int), this, updateVolume(int) );
172
173     /* Connect the input manager to the GUI elements it manages */
174     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
175              slider, setPosition( float,int, int ) );
176     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
177              this, setDisplay( float, int, int ) );
178
179     /* Naming in the controller */
180     CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
181              setName( QString ) );
182     if( config_GetInt( p_intf, "qt-system-tray" ) && sysTray )
183     {
184         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
185                  updateSystrayTooltipName( QString ) );
186     }
187     if( config_GetInt( p_intf, "qt-name-in-title" ) )
188     {
189         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
190              setVLCWindowsTitle( QString ) );
191     }
192
193     /* PLAY_STATUS */
194     CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
195     CONNECT( THEMIM->getIM(), navigationChanged( int ),
196              this, setNavigation(int) );
197     if( config_GetInt( p_intf, "qt-system-tray" ) && sysTray )
198     {
199         CONNECT( THEMIM->getIM(), statusChanged( int ), this,
200                  updateSystrayTooltipStatus( int ) );
201     }
202     CONNECT( slider, sliderDragged( float ),
203              THEMIM->getIM(), sliderUpdate( float ) );
204
205     /* Buttons */
206     CONNECT( ui.prevSectionButton, clicked(), THEMIM->getIM(),
207              sectionPrev() );
208     CONNECT( ui.nextSectionButton, clicked(), THEMIM->getIM(),
209              sectionNext() );
210     CONNECT( ui.menuButton, clicked(), THEMIM->getIM(),
211              sectionMenu() );
212
213     /**
214      * Callbacks
215      **/
216     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
217     var_AddCallback( p_intf, "interaction", InteractCallback, this );
218     p_intf->b_interaction = VLC_TRUE;
219
220     /* Register callback for the intf-popupmenu variable */
221     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
222                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
223     if( p_playlist != NULL )
224     {
225         var_AddCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
226         var_AddCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
227         vlc_object_release( p_playlist );
228     }
229 }
230
231 MainInterface::~MainInterface()
232 {
233     /* Unregister callback for the intf-popupmenu variable */
234     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
235                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
236     if( p_playlist != NULL )
237     {
238         var_DelCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
239         var_DelCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
240         vlc_object_release( p_playlist );
241     }
242
243     settings->setValue( "playlist-embedded", playlistEmbeddedFlag );
244     settings->setValue( "adv-controls", advControlsEnabled );
245     settings->setValue( "pos", pos() );
246     settings->endGroup();
247     delete settings;
248     p_intf->b_interaction = VLC_FALSE;
249     var_DelCallback( p_intf, "interaction", InteractCallback, this );
250
251     p_intf->pf_request_window = NULL;
252     p_intf->pf_release_window = NULL;
253     p_intf->pf_control_window = NULL;
254 }
255
256 /*****************************
257  *   Main UI handling        *
258  *****************************/
259
260 /**
261  * Give the decorations of the Main Window a correct Name.
262  * If nothing is given, set it to VLC...
263  **/
264 void MainInterface::setVLCWindowsTitle( QString aTitle )
265 {
266     if( aTitle.isEmpty() )
267     {
268         setWindowTitle( qtr( "VLC media player" ) );
269     }
270     else
271     {
272         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
273     }
274 }
275
276 void MainInterface::handleMainUi( QSettings *settings )
277 {
278     QWidget *main = new QWidget( this );
279     QVBoxLayout *mainLayout = new QVBoxLayout( main );
280     setCentralWidget( main );
281
282     /* Margins, spacing */
283     main->setContentsMargins( 0, 0, 0, 0 );
284     mainLayout->setMargin( 0 );
285
286     /* CONTROLS */
287     QWidget *controls = new QWidget;
288     ui.setupUi( controls );
289
290     /* Configure the UI */
291     slider = new InputSlider( Qt::Horizontal, NULL );
292     mainLayout->insertWidget( 0, slider );
293     ui.discFrame->hide();
294
295     BUTTON_SET_IMG( ui.prevSectionButton, "", previous.png, "" );
296     BUTTON_SET_IMG( ui.nextSectionButton, "", next.png, "" );
297     BUTTON_SET_IMG( ui.menuButton, "", previous.png, "" );
298
299     BUTTON_SET_ACT_I( ui.prevButton, "" , previous.png,
300                       qtr("Previous"), prev() );
301     BUTTON_SET_ACT_I( ui.nextButton, "", next.png, qtr("Next"), next() );
302     BUTTON_SET_ACT_I( ui.playButton, "", play.png, qtr("Play"), play() );
303     BUTTON_SET_ACT_I( ui.stopButton, "", stop.png, qtr("Stop"), stop() );
304
305     /* Volume */
306     ui.volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
307     ui.volumeSlider->setMaximum( 100 );
308     ui.volMuteLabel->setToolTip( qtr( "Mute" ) );
309     VolumeClickHandler *h = new VolumeClickHandler( p_intf, this );
310     ui.volMuteLabel->installEventFilter(h);
311     ui.volumeSlider->setFocusPolicy( Qt::NoFocus );
312
313     BUTTON_SET_IMG( ui.playlistButton, "" , playlist_icon.png,
314                         playlistEmbeddedFlag ?  qtr( "Show playlist" ) :
315                                                 qtr( "Open playlist" ) );
316     BUTTONACT( ui.playlistButton, playlist() );
317
318 #if DEBUG_COLOR
319     QPalette palette;
320     palette.setColor(main->backgroundRole(), Qt::green);
321     main->setPalette(palette);
322 #endif
323
324     /* Add the controls Widget */
325     mainLayout->addWidget( controls );
326
327     /* Set initial size */
328     resize ( PREF_W, PREF_H );
329
330     addSize = QSize( mainLayout->margin() * 2, PREF_H );
331
332     /* advanced Controls handling */
333     advControls = new ControlsWidget( p_intf );
334     mainLayout->insertWidget( 0, advControls );
335     advControls->updateGeometry();
336     if( !advControlsEnabled ) advControls->hide();
337     need_components_update = true;
338
339     /* Visualisation */
340     visualSelector = new VisualSelector( p_intf );
341     mainLayout->insertWidget( 0, visualSelector );
342     visualSelector->hide();
343
344     /* And video Outputs */
345     if( alwaysVideoFlag )
346     {
347         bgWidget = new BackgroundWidget( p_intf );
348         bgWidget->widgetSize = settings->value( "backgroundSize",
349                                            QSize( 200, 200 ) ).toSize();
350         bgWidget->resize( bgWidget->widgetSize );
351         bgWidget->updateGeometry();
352         mainLayout->insertWidget( 0, bgWidget );
353     }
354
355     if( videoEmbeddedFlag )
356     {
357         videoWidget = new VideoWidget( p_intf );
358         videoWidget->widgetSize = QSize( 1, 1 );
359         //videoWidget->resize( videoWidget->widgetSize );
360         mainLayout->insertWidget( 0, videoWidget );
361
362         p_intf->pf_request_window  = ::DoRequest;
363         p_intf->pf_release_window  = ::DoRelease;
364         p_intf->pf_control_window  = ::DoControl;
365     }
366
367     /* Finish the sizing */
368     setMinimumSize( PREF_W, addSize.height() );
369 }
370
371 /**********************************************************************
372  * Handling of the components
373  **********************************************************************/
374 void MainInterface::calculateInterfaceSize()
375 {
376     int width = 0, height = 0;
377     if( VISIBLE( bgWidget ) )
378     {
379         width = bgWidget->widgetSize.width();
380         height = bgWidget->widgetSize.height();
381         assert( !(playlistWidget && playlistWidget->isVisible() ) );
382     }
383     else if( VISIBLE( playlistWidget ) )
384     {
385         width = playlistWidget->widgetSize.width();
386         height = playlistWidget->widgetSize.height();
387     }
388     else if( videoIsActive )
389     {
390         width =  videoWidget->widgetSize.width() ;
391         height = videoWidget->widgetSize.height();
392     }
393     else
394     {
395         width = PREF_W - addSize.width();
396         height = PREF_H - addSize.height();
397     }
398     if( VISIBLE( visualSelector ) )
399         height += visualSelector->height();
400     if( VISIBLE( advControls) )
401     {
402         height += advControls->sizeHint().height();
403     }
404     mainSize = QSize( width + addSize.width(), height + addSize.height() );
405 }
406
407 void MainInterface::resizeEvent( QResizeEvent *e )
408 {
409     videoWidget->widgetSize.setWidth(  e->size().width() - addSize.width() );
410     if( videoWidget && videoIsActive && videoWidget->widgetSize.height() > 1 )
411     {
412         SET_WH( videoWidget, e->size().width() - addSize.width(),
413                              e->size().height()  - addSize.height() );
414         videoWidget->updateGeometry();
415     }
416     if( VISIBLE( playlistWidget ) )
417     {
418         SET_WH( playlistWidget , e->size().width() - addSize.width(),
419                                  e->size().height() - addSize.height() );
420         playlistWidget->updateGeometry();
421     }
422 }
423
424 /*****************************************************************************
425  * Systray Icon and Systray Menu
426  *****************************************************************************/
427
428 /**
429  * Create a SystemTray icon and a menu that would go with it.
430  * Connects to a click handler on the icon.
431  **/
432 void MainInterface::createSystray()
433 {
434     QIcon iconVLC =  QIcon( QPixmap( ":/vlc128.png" ) );
435     sysTray = new QSystemTrayIcon( iconVLC, this );
436     sysTray->setToolTip( qtr( "VLC media player" ));
437
438     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
439     systrayMenu->setIcon( iconVLC );
440
441     QVLCMenu::updateSystrayMenu( this, p_intf, true );
442     sysTray->show();
443
444     CONNECT( sysTray, activated(  QSystemTrayIcon::ActivationReason ),
445             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
446 }
447
448 /**
449  * Update the menu of the Systray Icon.
450  * May be unneedded, since it just calls QVLCMenu::update
451  * FIXME !!!
452  **/
453 void MainInterface::updateSystrayMenu( int status )
454 {
455     QVLCMenu::updateSystrayMenu( this, p_intf ) ;
456 }
457
458 /**
459  * Updates the Systray Icon's menu and toggle the main interface
460  */
461 void MainInterface::toggleUpdateSystrayMenu()
462 {
463     if( isHidden() )
464     {
465         show();
466         activateWindow();
467     }
468     else
469     {
470 #ifdef WIN32
471         /* check if any visible window is above vlc in the z-order,
472          * but ignore the ones always on top */
473         WINDOWINFO wi;
474         HWND hwnd;
475         wi.cbSize = sizeof( WINDOWINFO );
476         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
477                 hwnd && !IsWindowVisible( hwnd );
478                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
479         if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
480                 (wi.dwExStyle&WS_EX_TOPMOST) )
481 #else
482         if( isActiveWindow() )
483 #endif
484         {
485             hide();
486         }
487         else
488         {
489             activateWindow();
490         }
491     }
492     QVLCMenu::updateSystrayMenu( this, p_intf );
493 }
494
495 void MainInterface::handleSystrayClick(
496                                     QSystemTrayIcon::ActivationReason reason )
497 {
498     switch( reason )
499     {
500         case QSystemTrayIcon::Trigger:
501             toggleUpdateSystrayMenu();
502             break;
503         case QSystemTrayIcon::MiddleClick:
504             sysTray->showMessage( qtr( "VLC media player" ),
505                     qtr( "Control menu for the player" ),
506                     QSystemTrayIcon::Information, 4000 );
507             break;
508     }
509 }
510
511 /**
512  * Updates the name of the systray Icon tooltip.
513  * Doesn't check if the systray exists, check before you call it.
514  * FIXME !!! Fusion with next function ?
515  **/
516 void MainInterface::updateSystrayTooltipName( QString name )
517 {
518     if( name.isEmpty() )
519     {
520         sysTray->setToolTip( qtr( "VLC media player" ) );
521     }
522     else
523     {
524         sysTray->setToolTip( name );
525     }
526 }
527
528 /**
529  * Updates the status of the systray Icon tooltip.
530  * Doesn't check if the systray exists, check before you call it.
531  **/
532 void MainInterface::updateSystrayTooltipStatus( int i_status )
533 {
534     switch( i_status )
535     {
536         case  0:
537             {
538                 sysTray->setToolTip( qtr( "VLC media player" ) );
539                 break;
540             }
541         case PLAYING_S:
542             {
543                 sysTray->setToolTip( input_name );
544                 //+ " - " + qtr( "Playing" ) );
545                 break;
546             }
547         case PAUSE_S:
548             {
549                 sysTray->setToolTip( input_name + " - "
550                         + qtr( "Paused") );
551                 break;
552             }
553     }
554 }
555 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
556                                    int *pi_y, unsigned int *pi_width,
557                                    unsigned int *pi_height )
558 {
559     void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
560     if( ret )
561     {
562         videoIsActive = true;
563         if( VISIBLE( playlistWidget ) )
564         {
565             embeddedPlaylistWasActive = true;
566 //            playlistWidget->hide();
567         }
568         bool bgWasVisible = false;
569         if( VISIBLE( bgWidget) )
570         {
571             bgWasVisible = true;
572 //            bgWidget->hide();
573         }
574         if( THEMIM->getIM()->hasVideo() || !bgWasVisible )
575         {
576             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
577         }
578         else /* Background widget available, use its size */
579         {
580             /* Ok, our visualizations are bad, so don't do this for the moment
581              * use the requested size anyway */
582             // videoWidget->widgetSize = bgWidget->widgeTSize;
583             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
584         }
585 //        videoWidget->updateGeometry(); /// FIXME: Needed ?
586         need_components_update = true;
587     }
588     return ret;
589 }
590
591 void MainInterface::releaseVideo( void *p_win )
592 {
593     videoWidget->release( p_win );
594     videoWidget->widgetSize = QSize( 0, 0 );
595     videoWidget->resize( videoWidget->widgetSize );
596
597     if( embeddedPlaylistWasActive )
598         ;//playlistWidget->show();
599     else if( bgWidget )
600         ;//bgWidget->show();
601
602     videoIsActive = false;
603     need_components_update = true;
604 }
605
606 class SetVideoOnTopQtEvent : public QEvent
607 {
608 public:
609     SetVideoOnTopQtEvent( bool _onTop ) :
610       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
611     {
612     }
613
614     bool OnTop() const
615     {
616         return onTop;
617     }
618
619 private:
620     bool onTop;
621 };
622
623 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
624 {
625     int i_ret = VLC_EGENERIC;
626     switch( i_query )
627     {
628         case VOUT_GET_SIZE:
629         {
630             unsigned int *pi_width  = va_arg( args, unsigned int * );
631             unsigned int *pi_height = va_arg( args, unsigned int * );
632             *pi_width = videoWidget->widgetSize.width();
633             *pi_height = videoWidget->widgetSize.height();
634             i_ret = VLC_SUCCESS;
635             break;
636         }
637         case VOUT_SET_SIZE:
638         {
639             unsigned int i_width  = va_arg( args, unsigned int );
640             unsigned int i_height = va_arg( args, unsigned int );
641             videoWidget->widgetSize = QSize( i_width, i_height );
642             // videoWidget->updateGeometry();
643             need_components_update = true;
644             i_ret = VLC_SUCCESS;
645             break;
646         }
647         case VOUT_SET_STAY_ON_TOP:
648         {
649             int i_arg = va_arg( args, int );
650             QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
651             i_ret = VLC_SUCCESS;
652             break;
653         }
654         default:
655             msg_Warn( p_intf, "unsupported control query" );
656             break;
657     }
658     return i_ret;
659 }
660
661 void MainInterface::advanced()
662 {
663     if( !VISIBLE( advControls ) )
664     {
665         advControls->show();
666         advControlsEnabled = true;
667     }
668     else
669     {
670         advControls->hide();
671         advControlsEnabled = false;
672     }
673     doComponentsUpdate();
674 }
675
676 void MainInterface::visual()
677 {
678     if( !VISIBLE( visualSelector) )
679     {
680         visualSelector->show();
681         if( !THEMIM->getIM()->hasVideo() )
682         {
683             /* Show the background widget */
684         }
685         visualSelectorEnabled = true;
686     }
687     else
688     {
689         /* Stop any currently running visualization */
690         visualSelector->hide();
691         visualSelectorEnabled = false;
692     }
693     doComponentsUpdate();
694 }
695
696 void MainInterface::playlist()
697 {
698     // Toggle the playlist dialog
699     if( !playlistEmbeddedFlag )
700     {
701         if( playlistWidget )
702         {
703             /// \todo Destroy it
704         }
705         THEDP->playlistDialog();
706         return;
707     }
708
709     if( !playlistWidget )
710     {
711         PlaylistDialog::killInstance();
712         playlistWidget = new PlaylistWidget( p_intf );
713         ui.vboxLayout->insertWidget( 0, playlistWidget );
714         playlistWidget->widgetSize = settings->value( "playlistSize",
715                                                QSize( 650, 310 ) ).toSize();
716         playlistWidget->hide();
717     }
718     if( VISIBLE( playlistWidget ) )
719     {
720         playlistWidget->hide();
721         if( videoIsActive )
722         {
723             videoWidget->widgetSize = savedVideoSize;
724             videoWidget->resize( videoWidget->widgetSize );
725             videoWidget->updateGeometry();
726         }
727         if( bgWidget ) bgWidget->show();
728     }
729     else
730     {
731         playlistWidget->show();
732         if( videoIsActive )
733         {
734             savedVideoSize = videoWidget->widgetSize;
735             videoWidget->widgetSize.setHeight( 0 );
736             videoWidget->resize( videoWidget->widgetSize );
737             videoWidget->updateGeometry();
738         }
739         if( VISIBLE( bgWidget ) ) bgWidget->hide();
740     }
741     doComponentsUpdate();
742 }
743
744 /* Video widget cannot do this synchronously as it runs in another thread */
745 /* Well, could it, actually ? Probably dangerous ... */
746 void MainInterface::doComponentsUpdate()
747 {
748     calculateInterfaceSize();
749     resize( mainSize );
750 }
751
752 void MainInterface::undockPlaylist()
753 {
754     if( playlistWidget )
755     {
756         playlistWidget->hide();
757         playlistWidget->deleteLater();
758         ui.vboxLayout->removeWidget( playlistWidget );
759         playlistWidget = NULL;
760         playlistEmbeddedFlag = false;
761
762         menuBar()->clear();
763         QVLCMenu::createMenuBar( this, p_intf, false, advControlsEnabled,
764                                  visualSelectorEnabled);
765
766         if( videoIsActive )
767         {
768             videoWidget->widgetSize = savedVideoSize;
769             videoWidget->resize( videoWidget->widgetSize );
770             videoWidget->updateGeometry();
771         }
772
773         doComponentsUpdate();
774         THEDP->playlistDialog();
775     }
776 }
777
778 void MainInterface::toggleMenus()
779 {
780     if( menuBar()->isVisible() ) menuBar()->hide();
781         else menuBar()->show();
782     msg_Dbg( p_intf, "I was there: \\_o<~~ coin coin" );
783 }
784
785 void MainInterface::customEvent( QEvent *event )
786 {
787     if( event->type() == PLDockEvent_Type )
788     {
789         PlaylistDialog::killInstance();
790         playlistEmbeddedFlag = true;
791         menuBar()->clear();
792         QVLCMenu::createMenuBar(this, p_intf, true, advControlsEnabled,
793                                 visualSelectorEnabled);
794         playlist();
795     }
796     else if ( event->type() == SetVideoOnTopEvent_Type )
797     {
798         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
799         if( p_event->OnTop() )
800             setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
801         else
802             setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
803         show(); /* necessary to apply window flags?? */
804     }
805 }
806
807
808 /************************************************************************
809  * D&D
810  ************************************************************************/
811 void MainInterface::dropEvent(QDropEvent *event)
812 {
813      const QMimeData *mimeData = event->mimeData();
814
815      /* D&D of a subtitles file, add it on the fly */
816      if( mimeData->urls().size() == 1 )
817      {
818         if( THEMIM->getIM()->hasInput() )
819         {
820             if( input_AddSubtitles( THEMIM->getInput(),
821                                     qtu( mimeData->urls()[0].toString() ),
822                                     VLC_TRUE ) )
823             {
824                 event->acceptProposedAction();
825                 return;
826             }
827         }
828      }
829      bool first = true;
830      foreach( QUrl url, mimeData->urls() ) {
831         QString s = url.toString();
832         if( s.length() > 0 ) {
833             playlist_Add( THEPL, qtu(s), NULL,
834                           PLAYLIST_APPEND | (first ? PLAYLIST_GO:0),
835                           PLAYLIST_END, VLC_TRUE, VLC_FALSE );
836             first = false;
837         }
838      }
839      event->acceptProposedAction();
840 }
841 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
842 {
843      event->acceptProposedAction();
844 }
845 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
846 {
847      event->acceptProposedAction();
848 }
849 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
850 {
851      event->accept();
852 }
853
854 /************************************************************************
855  * Other stuff
856  ************************************************************************/
857 void MainInterface::keyPressEvent( QKeyEvent *e )
858 {
859     int i_vlck = qtEventToVLCKey( e );
860     if( i_vlck >= 0 )
861     {
862         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
863         e->accept();
864     }
865     else
866         e->ignore();
867 }
868
869 void MainInterface::wheelEvent( QWheelEvent *e )
870 {
871     int i_vlckey = qtWheelEventToVLCKey( e );
872     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
873     e->accept();
874 }
875
876 void MainInterface::stop()
877 {
878     THEMIM->stop();
879 }
880
881 void MainInterface::play()
882 {
883     if( playlist_IsEmpty(THEPL) )
884     {
885         /* The playlist is empty, open a file requester */
886         THEDP->openFileDialog();
887         setStatus( 0 );
888         return;
889     }
890     THEMIM->togglePlayPause();
891 }
892
893 void MainInterface::prev()
894 {
895     THEMIM->prev();
896 }
897
898 void MainInterface::next()
899 {
900     THEMIM->next();
901 }
902
903 void MainInterface::setDisplay( float pos, int time, int length )
904 {
905     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
906     secstotimestr( psz_length, length );
907     secstotimestr( psz_time, time );
908     QString title;
909     title.sprintf( "%s/%s", psz_time, psz_length );
910     timeLabel->setText( " "+title+" " );
911 }
912
913 void MainInterface::setName( QString name )
914 {
915     input_name = name;
916     nameLabel->setText( " " + name+" " );
917 }
918
919 void MainInterface::setStatus( int status )
920 {
921     if( status == 1 ) // Playing
922         ui.playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
923     else
924         ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
925     if( systrayMenu )
926         updateSystrayMenu( status );
927 }
928
929 #define HELP_MENU N_("Menu")
930 #define HELP_PCH N_("Previous chapter")
931 #define HELP_NCH N_("Next chapter")
932 #define HELP_PTR N_("Previous track")
933 #define HELP_NTR N_("Next track")
934
935 void MainInterface::setNavigation( int navigation )
936 {
937     // 1 = chapter, 2 = title, 0 = no
938     if( navigation == 0 )
939     {
940         ui.discFrame->hide();
941     } else if( navigation == 1 ) {
942         ui.prevSectionButton->show();
943         ui.prevSectionButton->setToolTip( qfu(HELP_PCH) );
944         ui.nextSectionButton->show();
945         ui.nextSectionButton->setToolTip( qfu(HELP_NCH) );
946         ui.menuButton->show();
947         ui.discFrame->show();
948     } else {
949         ui.prevSectionButton->show();
950         ui.prevSectionButton->setToolTip( qfu(HELP_PCH) );
951         ui.nextSectionButton->show();
952         ui.nextSectionButton->setToolTip( qfu(HELP_NCH) );
953         ui.menuButton->hide();
954         ui.discFrame->show();
955     }
956 }
957
958 static bool b_my_volume;
959
960 void MainInterface::updateOnTimer()
961 {
962     /* \todo Make this event-driven */
963     advControls->enableInput( THEMIM->getIM()->hasInput() );
964     advControls->enableVideo( THEMIM->getIM()->hasVideo() );
965
966     if( intf_ShouldDie( p_intf ) )
967     {
968         QApplication::closeAllWindows();
969         QApplication::quit();
970     }
971     if( need_components_update )
972     {
973         doComponentsUpdate();
974         need_components_update = false;
975     }
976
977     audio_volume_t i_volume;
978     aout_VolumeGet( p_intf, &i_volume );
979     i_volume = (i_volume *  200 )/ AOUT_VOLUME_MAX ;
980     int i_gauge = ui.volumeSlider->value();
981     b_my_volume = false;
982     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
983     {
984         b_my_volume = true;
985         ui.volumeSlider->setValue( i_volume );
986         b_my_volume = false;
987     }
988 }
989
990 void MainInterface::closeEvent( QCloseEvent *e )
991 {
992     hide();
993     vlc_object_kill( p_intf );
994 }
995
996 void MainInterface::updateVolume( int sliderVolume )
997 {
998     if( !b_my_volume )
999     {
1000         int i_res = sliderVolume * AOUT_VOLUME_MAX /
1001                             (2*ui.volumeSlider->maximum() );
1002         aout_VolumeSet( p_intf, i_res );
1003     }
1004 }
1005
1006 static int InteractCallback( vlc_object_t *p_this,
1007                              const char *psz_var, vlc_value_t old_val,
1008                              vlc_value_t new_val, void *param )
1009 {
1010     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
1011     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
1012     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
1013     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
1014     return VLC_SUCCESS;
1015 }
1016
1017 /*****************************************************************************
1018  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
1019  *  We don't show the menu directly here because we don't want the
1020  *  caller to block for a too long time.
1021  *****************************************************************************/
1022 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
1023                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1024 {
1025     intf_thread_t *p_intf = (intf_thread_t *)param;
1026
1027     if( p_intf->pf_show_dialog )
1028     {
1029         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
1030                                 new_val.b_bool, 0 );
1031     }
1032
1033     return VLC_SUCCESS;
1034 }
1035
1036 /*****************************************************************************
1037  * IntfShowCB: callback triggered by the intf-show playlist variable.
1038  *****************************************************************************/
1039 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
1040                        vlc_value_t old_val, vlc_value_t new_val, void *param )
1041 {
1042     intf_thread_t *p_intf = (intf_thread_t *)param;
1043     //p_intf->p_sys->b_intf_show = VLC_TRUE;
1044
1045     return VLC_SUCCESS;
1046 }