]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
Qt4 - use a docked playlist, remove dead code, don't reinvent the wheel, remove the...
[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     speedLabel = new QLabel( "1.00x" );
148     speedLabel->setContextMenuPolicy ( Qt::CustomContextMenu );
149     timeLabel->setContextMenuPolicy ( Qt::CustomContextMenu );
150
151     /* Styling those labels */
152     timeLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
153     speedLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
154     nameLabel->setFrameStyle( QFrame::Sunken | QFrame::StyledPanel);
155
156     /* and adding those */
157     statusBar()->addWidget( nameLabel, 8 );
158     statusBar()->addPermanentWidget( speedLabel, 0 );
159     statusBar()->addPermanentWidget( timeLabel, 2 );
160
161     /* timeLabel behaviour:
162        - double clicking opens the goto time dialog 
163        - right-clicking and clicking just toggle between remaining and
164          elapsed time.*/
165     CONNECT( timeLabel, timeLabelClicked(), this, toggleTimeDisplay() );
166     CONNECT( timeLabel, customContextMenuRequested( QPoint ),
167              this, toggleTimeDisplay() );
168     CONNECT( timeLabel, timeLabelDoubleClicked(), THEDP, gotoTimeDialog() );
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         toggleMenus();
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     // DEBUG FIXME
262     hide();
263 }
264
265 MainInterface::~MainInterface()
266 {
267     /* Unregister callback for the intf-popupmenu variable */
268     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
269                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
270     if( p_playlist != NULL )
271     {
272         var_DelCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
273         var_DelCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
274         vlc_object_release( p_playlist );
275     }
276
277     settings->setValue( "playlist-embedded", !dockPL->isFloating() );
278     settings->setValue( "adv-controls", getControlsVisibilityStatus() & CONTROLS_ADVANCED );
279     settings->setValue( "pos", pos() );
280     settings->endGroup();
281     delete settings;
282     p_intf->b_interaction = VLC_FALSE;
283     var_DelCallback( p_intf, "interaction", InteractCallback, this );
284
285     p_intf->pf_request_window = NULL;
286     p_intf->pf_release_window = NULL;
287     p_intf->pf_control_window = NULL;
288 }
289
290 /*****************************
291  *   Main UI handling        *
292  *****************************/
293
294 /**
295  * Give the decorations of the Main Window a correct Name.
296  * If nothing is given, set it to VLC...
297  **/
298 void MainInterface::setVLCWindowsTitle( QString aTitle )
299 {
300     if( aTitle.isEmpty() )
301     {
302         setWindowTitle( qtr( "VLC media player" ) );
303     }
304     else
305     {
306         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
307     }
308 }
309
310
311 void MainInterface::handleMainUi( QSettings *settings )
312 {
313     /* Create the main Widget and the mainLayout */
314     QWidget *main = new QWidget( this );
315     mainLayout = new QVBoxLayout( main );
316     setCentralWidget( main );
317
318     /* Margins, spacing */
319     main->setContentsMargins( 0, 0, 0, 0 );
320     mainLayout->setMargin( 0 );
321
322     /* Create the CONTROLS Widget */
323     controls = new ControlsWidget( p_intf,
324                    settings->value( "adv-controls", false ).toBool() );
325
326     /* Configure the Controls, the playlist button doesn't trigger THEDP
327        but the toggle from this MainInterface */
328     BUTTONACT( controls->playlistButton, togglePlaylist() );
329
330     /* Add the controls Widget to the main Widget */
331     mainLayout->addWidget( controls );
332
333     /* Create the Speed Control Widget */
334     speedControl = new SpeedControlWidget( p_intf );
335     speedControlMenu = new QMenu( this );
336     QWidgetAction *widgetAction = new QWidgetAction( this );
337     widgetAction->setDefaultWidget( speedControl );
338     speedControlMenu->addAction( widgetAction );
339
340     /* Set initial size */
341     resize( PREF_W, PREF_H );
342     addSize = QSize( mainLayout->margin() * 2, PREF_H );
343
344     /* Visualisation */
345     visualSelector = new VisualSelector( p_intf );
346     mainLayout->insertWidget( 0, visualSelector );
347     visualSelector->hide();
348
349     /* And video Outputs */
350     if( alwaysVideoFlag )
351     {
352         bgWidget = new BackgroundWidget( p_intf );
353         bgWidget->widgetSize = settings->value( "backgroundSize",
354                                            QSize( 300, 300 ) ).toSize();
355         bgWidget->resize( bgWidget->widgetSize );
356         bgWidget->updateGeometry();
357         mainLayout->insertWidget( 0, bgWidget );
358     }
359
360     if( videoEmbeddedFlag )
361     {
362         videoWidget = new VideoWidget( p_intf );
363         videoWidget->widgetSize = QSize( 1, 1 );
364         //videoWidget->resize( videoWidget->widgetSize );
365         mainLayout->insertWidget( 0, videoWidget );
366
367         p_intf->pf_request_window  = ::DoRequest;
368         p_intf->pf_release_window  = ::DoRelease;
369         p_intf->pf_control_window  = ::DoControl;
370     }
371
372     /* Finish the sizing */
373     setMinimumSize( PREF_W, addSize.height() );
374 }
375
376 void MainInterface::debug()
377 {
378     msg_Dbg( p_intf, "size: %i - %i", controls->size().height(), controls->size().width() );
379     msg_Dbg( p_intf, "sizeHint: %i - %i", controls->sizeHint().height(), controls->sizeHint().width() );
380 }
381 /**********************************************************************
382  * Handling of sizing of the components
383  **********************************************************************/
384 void MainInterface::calculateInterfaceSize()
385 {
386     int width = 0, height = 0;
387     if( VISIBLE( bgWidget ) )
388     {
389         width = bgWidget->widgetSize.width();
390         height = bgWidget->widgetSize.height();
391 //        assert( !(playlistWidget));// && playlistWidget->isVisible() ) );
392     }
393     else if( VISIBLE( playlistWidget ) )
394     {
395         width = playlistWidget->widgetSize.width();
396         height = playlistWidget->widgetSize.height();
397     }
398     else if( videoIsActive )
399     {
400         width =  videoWidget->widgetSize.width() ;
401         height = videoWidget->widgetSize.height();
402     }
403     else
404     {
405         width = PREF_W - addSize.width();
406         height = PREF_H - addSize.height();
407     }
408     if( VISIBLE( visualSelector ) )
409         height += visualSelector->height();
410 /*    if( VISIBLE( advControls) )
411     {
412         height += advControls->sizeHint().height();
413     }*/
414     mainSize = QSize( width + addSize.width(), height + addSize.height() );
415 }
416
417 void MainInterface::resizeEvent( QResizeEvent *e )
418 {
419     if( videoWidget )
420         videoWidget->widgetSize.setWidth( e->size().width() - addSize.width() );
421     if( videoWidget && videoIsActive && videoWidget->widgetSize.height() > 1 )
422     {
423         SET_WH( videoWidget, e->size().width() - addSize.width(),
424                              e->size().height()  - addSize.height() );
425         videoWidget->updateGeometry();
426     }
427     if( VISIBLE( playlistWidget ) )
428     {
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 menus
437  ****************************************************************************/
438 void MainInterface::showSpeedMenu( QPoint pos )
439 {
440     speedControlMenu->exec( QCursor::pos() - pos + QPoint( 0, speedLabel->height() ) );
441 }
442
443 /****************************************************************************
444  * Video Handling
445  ****************************************************************************/
446 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
447                                    int *pi_y, unsigned int *pi_width,
448                                    unsigned int *pi_height )
449 {
450     void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
451     if( ret )
452     {
453         videoIsActive = true;
454         if( VISIBLE( playlistWidget ) )
455         {
456             embeddedPlaylistWasActive = true;
457 //            playlistWidget->hide();
458         }
459         bool bgWasVisible = false;
460         if( VISIBLE( bgWidget) )
461         {
462             bgWasVisible = true;
463             bgWidget->hide();
464         }
465         if( THEMIM->getIM()->hasVideo() || !bgWasVisible )
466         {
467             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
468         }
469         else /* Background widget available, use its size */
470         {
471             /* Ok, our visualizations are bad, so don't do this for the moment
472              * use the requested size anyway */
473             // videoWidget->widgetSize = bgWidget->widgeTSize;
474             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
475         }
476         videoWidget->updateGeometry(); // Needed for deinterlace
477         need_components_update = true;
478     }
479     return ret;
480 }
481
482 void MainInterface::releaseVideo( void *p_win )
483 {
484     videoWidget->release( p_win );
485     videoWidget->widgetSize = QSize( 0, 0 );
486     videoWidget->resize( videoWidget->widgetSize );
487
488     if( embeddedPlaylistWasActive )
489         playlistWidget->show();
490     else if( bgWidget )
491         bgWidget->show();
492
493     videoIsActive = false;
494     need_components_update = true;
495 }
496
497 class SetVideoOnTopQtEvent : public QEvent
498 {
499 public:
500     SetVideoOnTopQtEvent( bool _onTop ) :
501       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
502     {
503     }
504
505     bool OnTop() const
506     {
507         return onTop;
508     }
509
510 private:
511     bool onTop;
512 };
513
514 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
515 {
516     int i_ret = VLC_EGENERIC;
517     switch( i_query )
518     {
519         case VOUT_GET_SIZE:
520         {
521             unsigned int *pi_width  = va_arg( args, unsigned int * );
522             unsigned int *pi_height = va_arg( args, unsigned int * );
523             *pi_width = videoWidget->widgetSize.width();
524             *pi_height = videoWidget->widgetSize.height();
525             i_ret = VLC_SUCCESS;
526             break;
527         }
528         case VOUT_SET_SIZE:
529         {
530             unsigned int i_width  = va_arg( args, unsigned int );
531             unsigned int i_height = va_arg( args, unsigned int );
532             videoWidget->widgetSize = QSize( i_width, i_height );
533             // videoWidget->updateGeometry();
534             need_components_update = true;
535             i_ret = VLC_SUCCESS;
536             break;
537         }
538         case VOUT_SET_STAY_ON_TOP:
539         {
540             int i_arg = va_arg( args, int );
541             QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
542             i_ret = VLC_SUCCESS;
543             break;
544         }
545         default:
546             msg_Warn( p_intf, "unsupported control query" );
547             break;
548     }
549     return i_ret;
550 }
551
552 /*****************************************************************************
553  * Playlist, Visualisation and Menus handling
554  *****************************************************************************/
555 /**
556  * Toggle the playlist widget or dialog
557  **/
558 void MainInterface::togglePlaylist()
559 {
560     /* If no playlist exist, then create one and attach it to the DockPL*/
561     if( !playlistWidget )
562     {
563         msg_Dbg( p_intf, "Creating a new playlist" );
564         playlistWidget = new PlaylistWidget( p_intf );
565         if(bgWidget)
566             CONNECT( playlistWidget, artSet( QString ), bgWidget, setArt(QString) );
567
568         playlistWidget->widgetSize = settings->value( "playlistSize",
569                                                QSize( 650, 310 ) ).toSize();
570         /* Add it to the parent DockWidget */
571         dockPL->setWidget( playlistWidget );
572
573         /* Add the dock to the main Interface */
574         addDockWidget( Qt::BottomDockWidgetArea, dockPL );
575
576         msg_Dbg( p_intf, "Creating a new playlist" );
577
578         /* Make the playlist floating is requested. Default is not. */
579         if( !settings->value( "playlist-embedded", true ).toBool() );
580         {
581             msg_Dbg( p_intf, "we don't want it inside");
582             dockPL->setFloating( true );
583         }
584
585     }
586     else
587     {
588     /* toggle the display */
589        TOGGLEV( dockPL );
590     }
591
592 #if 0  // Toggle the playlist dialog if not embedded and return
593     if( !playlistEmbeddedFlag )
594     {
595         if( playlistWidget )
596         {
597             /// \todo Destroy it
598         }
599         return;
600     }
601
602     // Create the playlist Widget and destroy the existing dialog
603     if( !playlistWidget )
604     {
605         PlaylistDialog::killInstance();
606         mainLayout->insertWidget( 0, playlistWidget );
607         playlistWidget->hide();
608     }
609
610     // And toggle visibility
611     if( VISIBLE( playlistWidget ) )
612     {
613         playlistWidget->hide();
614         if( bgWidget ) bgWidget->show();
615         if( videoIsActive )
616         {
617             videoWidget->widgetSize = savedVideoSize;
618             videoWidget->resize( videoWidget->widgetSize );
619             videoWidget->updateGeometry();
620             if( bgWidget ) bgWidget->hide();
621         }
622     }
623     else
624     {
625         playlistWidget->show();
626         if( videoIsActive )
627         {
628             savedVideoSize = videoWidget->widgetSize;
629             videoWidget->widgetSize.setHeight( 0 );
630             videoWidget->resize( videoWidget->widgetSize );
631             videoWidget->updateGeometry();
632         }
633         if( VISIBLE( bgWidget ) ) bgWidget->hide();
634     }
635 #endif
636     doComponentsUpdate();
637 }
638
639 void MainInterface::undockPlaylist()
640 {
641     dockPL->setFloating( true );
642 #if 0
643     if( playlistWidget )
644     {
645         playlistWidget->hide();
646         playlistWidget->deleteLater();
647         mainLayout->removeWidget( playlistWidget );
648         playlistWidget = NULL;
649         playlistEmbeddedFlag = false;
650
651         menuBar()->clear();
652         QVLCMenu::createMenuBar( this, p_intf, false, visualSelectorEnabled);
653
654         if( videoIsActive )
655         {
656             videoWidget->widgetSize = savedVideoSize;
657             videoWidget->resize( videoWidget->widgetSize );
658             videoWidget->updateGeometry();
659         }
660
661         doComponentsUpdate();
662         THEDP->playlistDialog();
663     }
664 #endif
665 }
666
667 #if 0
668 void MainInterface::visual()
669 {
670     if( !VISIBLE( visualSelector) )
671     {
672         visualSelector->show();
673         if( !THEMIM->getIM()->hasVideo() )
674         {
675             /* Show the background widget */
676         }
677         visualSelectorEnabled = true;
678     }
679     else
680     {
681         /* Stop any currently running visualization */
682         visualSelector->hide();
683         visualSelectorEnabled = false;
684     }
685     doComponentsUpdate();
686 }
687 #endif
688
689 void MainInterface::toggleMenus()
690 {
691     TOGGLEV( controls );
692     TOGGLEV( statusBar() );
693     updateGeometry();
694 }
695
696 /* Video widget cannot do this synchronously as it runs in another thread */
697 /* Well, could it, actually ? Probably dangerous ... */
698 void MainInterface::doComponentsUpdate()
699 {
700     calculateInterfaceSize();
701     resize( mainSize );
702 }
703
704 void MainInterface::toggleAdvanced()
705 {
706     controls->toggleAdvanced();
707 }
708
709 int MainInterface::getControlsVisibilityStatus()
710 {
711     return( (controls->isVisible() ? CONTROLS_VISIBLE : CONTROLS_HIDDEN )
712                 + CONTROLS_ADVANCED * controls->b_advancedVisible );
713 }
714
715 /************************************************************************
716  * Other stuff
717  ************************************************************************/
718 void MainInterface::setDisplayPosition( float pos, int time, int length )
719 {
720     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
721     secstotimestr( psz_length, length );
722     secstotimestr( psz_time, b_remainingTime ? length - time : time );
723
724     QString title; title.sprintf( "%s/%s", psz_time, psz_length );
725     /* Add a minus to remaining time*/
726     if( b_remainingTime ) timeLabel->setText( " -"+title+" " );
727     else timeLabel->setText( " "+title+" " );
728 }
729
730 void MainInterface::toggleTimeDisplay()
731 {
732     b_remainingTime = ( b_remainingTime ? false : true );
733 }
734
735 void MainInterface::setName( QString name )
736 {
737     input_name = name; /* store it for the QSystray use */
738     /* Display it in the status bar, but also as a Tooltip in case it doesn't
739        fit in the label */
740     nameLabel->setText( " " + name + " " );
741     nameLabel->setToolTip( " " + name +" " );
742 }
743
744 void MainInterface::setStatus( int status )
745 {
746     /* Forward the status to the controls to toggle Play/Pause */
747     controls->setStatus( status );
748     /* And in the systray for the menu */
749     if( sysTray )
750         QVLCMenu::updateSystrayMenu( this, p_intf );
751 }
752
753 void MainInterface::setRate( int rate )
754 {
755     QString str;
756     str.setNum( ( 1000 / (double)rate), 'f', 2 );
757     str.append( "x" );
758     speedLabel->setText( str );
759     speedControl->updateControls( rate );
760 }
761
762 void MainInterface::updateOnTimer()
763 {
764     /* \todo Make this event-driven */
765     if( intf_ShouldDie( p_intf ) )
766     {
767         QApplication::closeAllWindows();
768         QApplication::quit();
769     }
770     if( need_components_update )
771     {
772         doComponentsUpdate();
773         need_components_update = false;
774     }
775
776     controls->updateOnTimer();
777 }
778
779 /*****************************************************************************
780  * Systray Icon and Systray Menu
781  *****************************************************************************/
782
783 /**
784  * Create a SystemTray icon and a menu that would go with it.
785  * Connects to a click handler on the icon.
786  **/
787 void MainInterface::createSystray()
788 {
789     QIcon iconVLC =  QIcon( QPixmap( ":/vlc128.png" ) );
790     sysTray = new QSystemTrayIcon( iconVLC, this );
791     sysTray->setToolTip( qtr( "VLC media player" ));
792
793     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
794     systrayMenu->setIcon( iconVLC );
795
796     QVLCMenu::updateSystrayMenu( this, p_intf, true );
797     sysTray->show();
798
799     CONNECT( sysTray, activated( QSystemTrayIcon::ActivationReason ),
800             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
801 }
802
803 /**
804  * Updates the Systray Icon's menu and toggle the main interface
805  */
806 void MainInterface::toggleUpdateSystrayMenu()
807 {
808     if( isHidden() )
809     {
810         show();
811         activateWindow();
812     }
813     else if( isMinimized() )
814     {
815         showNormal();
816         activateWindow();
817     }
818     else
819     {
820 #ifdef WIN32
821         /* check if any visible window is above vlc in the z-order,
822          * but ignore the ones always on top */
823         WINDOWINFO wi;
824         HWND hwnd;
825         wi.cbSize = sizeof( WINDOWINFO );
826         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
827                 hwnd && !IsWindowVisible( hwnd );
828                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
829         if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
830                 (wi.dwExStyle&WS_EX_TOPMOST) )
831 #else
832         if( isActiveWindow() )
833 #endif
834         {
835             hide();
836         }
837         else
838         {
839             activateWindow();
840         }
841     }
842     QVLCMenu::updateSystrayMenu( this, p_intf );
843 }
844
845 void MainInterface::handleSystrayClick(
846                                     QSystemTrayIcon::ActivationReason reason )
847 {
848     switch( reason )
849     {
850         case QSystemTrayIcon::Trigger:
851             toggleUpdateSystrayMenu();
852             break;
853         case QSystemTrayIcon::MiddleClick:
854             sysTray->showMessage( qtr( "VLC media player" ),
855                     qtr( "Control menu for the player" ),
856                     QSystemTrayIcon::Information, 4000 );
857             break;
858     }
859 }
860
861 /**
862  * Updates the name of the systray Icon tooltip.
863  * Doesn't check if the systray exists, check before you call it.
864  **/
865 void MainInterface::updateSystrayTooltipName( QString name )
866 {
867     if( name.isEmpty() )
868     {
869         sysTray->setToolTip( qtr( "VLC media player" ) );
870     }
871     else
872     {
873         sysTray->setToolTip( name );
874         if( notificationEnabled && ( isHidden() || isMinimized() ) )
875         {
876             sysTray->showMessage( qtr( "VLC media player" ), name,
877                     QSystemTrayIcon::NoIcon, 4000 );
878         }
879     }
880 }
881
882 /**
883  * Updates the status of the systray Icon tooltip.
884  * Doesn't check if the systray exists, check before you call it.
885  **/
886 void MainInterface::updateSystrayTooltipStatus( int i_status )
887 {
888     switch( i_status )
889     {
890         case  0:
891             {
892                 sysTray->setToolTip( qtr( "VLC media player" ) );
893                 break;
894             }
895         case PLAYING_S:
896             {
897                 sysTray->setToolTip( input_name );
898                 break;
899             }
900         case PAUSE_S:
901             {
902                 sysTray->setToolTip( input_name + " - "
903                         + qtr( "Paused") );
904                 break;
905             }
906     }
907 }
908
909 /************************************************************************
910  * D&D Events
911  ************************************************************************/
912 void MainInterface::dropEvent(QDropEvent *event)
913 {
914      const QMimeData *mimeData = event->mimeData();
915
916      /* D&D of a subtitles file, add it on the fly */
917      if( mimeData->urls().size() == 1 )
918      {
919         if( THEMIM->getIM()->hasInput() )
920         {
921             if( input_AddSubtitles( THEMIM->getInput(),
922                                     qtu( mimeData->urls()[0].toString() ),
923                                     VLC_TRUE ) )
924             {
925                 event->acceptProposedAction();
926                 return;
927             }
928         }
929      }
930      bool first = true;
931      foreach( QUrl url, mimeData->urls() ) {
932         QString s = url.toString();
933         if( s.length() > 0 ) {
934             playlist_Add( THEPL, qtu(s), NULL,
935                           PLAYLIST_APPEND | (first ? PLAYLIST_GO:0),
936                           PLAYLIST_END, VLC_TRUE, VLC_FALSE );
937             first = false;
938         }
939      }
940      event->acceptProposedAction();
941 }
942 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
943 {
944      event->acceptProposedAction();
945 }
946 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
947 {
948      event->acceptProposedAction();
949 }
950 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
951 {
952      event->accept();
953 }
954
955 /************************************************************************
956  * Events stuff
957  ************************************************************************/
958 void MainInterface::customEvent( QEvent *event )
959 {
960 #if 0
961     if( event->type() == PLDockEvent_Type )
962     {
963         PlaylistDialog::killInstance();
964         playlistEmbeddedFlag = true;
965         menuBar()->clear();
966         QVLCMenu::createMenuBar(this, p_intf, true, visualSelectorEnabled);
967         togglePlaylist();
968     }
969 #endif
970     /*else */
971     if ( event->type() == SetVideoOnTopEvent_Type )
972     {
973         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
974         if( p_event->OnTop() )
975             setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
976         else
977             setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
978         show(); /* necessary to apply window flags?? */
979     }
980 }
981
982 void MainInterface::keyPressEvent( QKeyEvent *e )
983 {
984     int i_vlck = qtEventToVLCKey( e );
985     if( i_vlck >= 0 )
986     {
987         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
988         e->accept();
989     }
990     else
991         e->ignore();
992 }
993
994 void MainInterface::wheelEvent( QWheelEvent *e )
995 {
996     int i_vlckey = qtWheelEventToVLCKey( e );
997     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
998     e->accept();
999 }
1000
1001 void MainInterface::closeEvent( QCloseEvent *e )
1002 {
1003     hide();
1004     vlc_object_kill( p_intf );
1005 }
1006
1007 /*****************************************************************************
1008  * Callbacks
1009  *****************************************************************************/
1010 static int InteractCallback( vlc_object_t *p_this,
1011                              const char *psz_var, vlc_value_t old_val,
1012                              vlc_value_t new_val, void *param )
1013 {
1014     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
1015     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
1016     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
1017     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
1018     return VLC_SUCCESS;
1019 }
1020
1021 /*****************************************************************************
1022  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
1023  *  We don't show the menu directly here because we don't want the
1024  *  caller to block for a too long time.
1025  *****************************************************************************/
1026 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
1027                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1028 {
1029     intf_thread_t *p_intf = (intf_thread_t *)param;
1030
1031     if( p_intf->pf_show_dialog )
1032     {
1033         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
1034                                 new_val.b_bool, 0 );
1035     }
1036
1037     return VLC_SUCCESS;
1038 }
1039
1040 /*****************************************************************************
1041  * IntfShowCB: callback triggered by the intf-show playlist variable.
1042  *****************************************************************************/
1043 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
1044                        vlc_value_t old_val, vlc_value_t new_val, void *param )
1045 {
1046     intf_thread_t *p_intf = (intf_thread_t *)param;
1047     //p_intf->p_sys->b_intf_show = VLC_TRUE;
1048
1049     return VLC_SUCCESS;
1050 }