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