]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
8b6530c5ddecba9d8669369944ca28352114829d
[vlc] / modules / gui / qt4 / main_interface.cpp
1 /*****************************************************************************
2  * main_interface.cpp : Main interface
3  ****************************************************************************
4  * Copyright (C) 2006-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *          Ilkka Ollakka <ileoo@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "qt4.hpp"
31
32 #include "main_interface.hpp"
33 #include "input_manager.hpp"
34 #include "actions_manager.hpp"
35 #include "extensions_manager.hpp" // killInstance
36
37 #include "util/customwidgets.hpp"
38 #include "util/qt_dirs.hpp"
39
40 #include "components/interface_widgets.hpp"
41 #include "components/controller.hpp"
42 #include "components/playlist/playlist.hpp"
43 #include "dialogs/external.hpp"
44 #include "dialogs/firstrun.hpp"
45
46 #include "menus.hpp"
47 #include "recents.hpp"
48
49 #include <QCloseEvent>
50 #include <QKeyEvent>
51
52 #include <QUrl>
53 #include <QSize>
54 #include <QDate>
55
56 #include <QMenu>
57 #include <QMenuBar>
58 #include <QStatusBar>
59 #include <QLabel>
60 #include <QGroupBox>
61 #include <QPushButton>
62 #include <QStackedWidget>
63
64 #ifdef WIN32
65  #include <vlc_windows_interfaces.h>
66  #include <QBitmap>
67 #endif
68
69 #include <assert.h>
70
71 #include <vlc_keys.h> /* Wheel event */
72 #include <vlc_vout_window.h>
73 #include <vlc_vout_display.h>
74
75 // #define DEBUG_INTF
76 /* Callback prototypes */
77 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
78                         vlc_value_t old_val, vlc_value_t new_val, void *param );
79 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
80                        vlc_value_t old_val, vlc_value_t new_val, void *param );
81
82 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
83 {
84     /* Variables initialisation */
85     // need_components_update = false;
86     bgWidget             = NULL;
87     videoWidget          = NULL;
88     playlistWidget       = NULL;
89 #ifndef HAVE_MAEMO
90     sysTray              = NULL;
91 #endif
92     fullscreenControls   = NULL;
93     cryptedLabel         = NULL;
94     controls             = NULL;
95     inputC               = NULL;
96
97     b_hideAfterCreation  = false;
98     playlistVisible      = false; // FIXME remove
99     input_name           = "";
100
101     i_bg_height          = 0;
102
103     /* Ask for Privacy */
104     FirstRun::CheckAndRun( this, p_intf );
105
106     /**
107      *  Configuration and settings
108      *  Pre-building of interface
109      **/
110     /* Main settings */
111     setFocusPolicy( Qt::StrongFocus );
112     setAcceptDrops( true );
113     setWindowRole( "vlc-main" );
114     setWindowIcon( QApplication::windowIcon() );
115     setWindowOpacity( var_InheritFloat( p_intf, "qt-opacity" ) );
116
117     /* Set The Video In emebedded Mode or not */
118     videoEmbeddedFlag = var_InheritBool( p_intf, "embedded-video" );
119
120     /* Does the interface resize to video size or the opposite */
121     b_keep_size = !var_InheritBool( p_intf, "qt-video-autoresize" );
122
123     /* Are we in the enhanced always-video mode or not ? */
124     i_visualmode = var_InheritInteger( p_intf, "qt-display-mode" );
125
126     /* Do we want anoying popups or not */
127     notificationEnabled = (bool)var_InheritBool( p_intf, "qt-notification" );
128
129     /* Set the other interface settings */
130     settings = getSettings();
131     settings->beginGroup( "MainWindow" );
132
133     /**
134      * Retrieve saved sizes for main window
135      *   mainBasedSize = based window size for normal mode
136      *                  (no video, no background)
137      *   mainVideoSize = window size with video (all modes)
138      **/
139     mainBasedSize = settings->value( "mainBasedSize", QSize( 350, 120 ) ).toSize();
140     mainVideoSize = settings->value( "mainVideoSize", QSize( 400, 300 ) ).toSize();
141
142
143     /**************
144      * Status Bar *
145      **************/
146     createStatusBar();
147
148     /**************************
149      *  UI and Widgets design
150      **************************/
151     setVLCWindowsTitle();
152     createMainWidget( settings );
153
154     /************
155      * Menu Bar *
156      ************/
157     QVLCMenu::createMenuBar( this, p_intf );
158     CONNECT( THEMIM->getIM(), voutListChanged( vout_thread_t **, int ),
159              this, destroyPopupMenu() );
160
161     /*********************************
162      * Create the Systray Management *
163      *********************************/
164     initSystray();
165
166     /********************
167      * Input Manager    *
168      ********************/
169     MainInputManager::getInstance( p_intf );
170
171 #ifdef WIN32
172     taskbar_wmsg = RegisterWindowMessage("TaskbarButtonCreated");
173 #endif
174
175     /************************************************************
176      * Connect the input manager to the GUI elements it manages *
177      ************************************************************/
178     /**
179      * Connects on nameChanged()
180      * Those connects are different because options can impeach them to trigger.
181      **/
182     /* Main Interface statusbar */
183     CONNECT( THEMIM->getIM(), nameChanged( const QString& ),
184              this, setName( const QString& ) );
185     /* and systray */
186 #ifndef HAVE_MAEMO
187     if( sysTray )
188     {
189         CONNECT( THEMIM->getIM(), nameChanged( const QString& ),
190                  this, updateSystrayTooltipName( const QString& ) );
191     }
192 #endif
193     /* and title of the Main Interface*/
194     if( var_InheritBool( p_intf, "qt-name-in-title" ) )
195     {
196         CONNECT( THEMIM->getIM(), nameChanged( const QString& ),
197                  this, setVLCWindowsTitle( const QString& ) );
198     }
199
200     /**
201      * CONNECTS on PLAY_STATUS
202      **/
203     /* Status on the systray */
204 #ifndef HAVE_MAEMO
205     if( sysTray )
206     {
207         CONNECT( THEMIM->getIM(), statusChanged( int ),
208                  this, updateSystrayTooltipStatus( int ) );
209     }
210 #endif
211
212     /* END CONNECTS ON IM */
213
214     /************
215      * Callbacks
216      ************/
217     var_AddCallback( p_intf->p_libvlc, "intf-show", IntfShowCB, p_intf );
218
219     /* Register callback for the intf-popupmenu variable */
220     var_AddCallback( p_intf->p_libvlc, "intf-popupmenu", PopupMenuCB, p_intf );
221
222
223     /* VideoWidget connects for asynchronous calls */
224     connect( this, SIGNAL(askGetVideo(WId*,int*,int*,unsigned*,unsigned *)),
225              this, SLOT(getVideoSlot(WId*,int*,int*,unsigned*,unsigned*)),
226              Qt::BlockingQueuedConnection );
227     connect( this, SIGNAL(askReleaseVideo( void )),
228              this, SLOT(releaseVideoSlot( void )),
229              Qt::BlockingQueuedConnection );
230
231     if( videoWidget )
232     {
233         CONNECT( this, askVideoToResize( unsigned int, unsigned int ),
234                  videoWidget, SetSizing( unsigned int, unsigned int ) );
235         CONNECT( this, askVideoSetFullScreen( bool ),
236                  videoWidget, SetFullScreen( bool ) );
237         CONNECT( videoWidget, keyPressed( QKeyEvent * ),
238                  this, handleKeyPress( QKeyEvent * ) );
239     }
240
241     CONNECT( this, askUpdate(), this, doComponentsUpdate() );
242     CONNECT( THEDP, toolBarConfUpdated(), this, recreateToolbars() );
243
244         /* Enable the popup menu in the MI */
245     setContextMenuPolicy( Qt::CustomContextMenu );
246     CONNECT( this, customContextMenuRequested( const QPoint& ),
247              this, popupMenu( const QPoint& ) );
248
249     /** END of CONNECTS**/
250
251
252     /**** FINAL SIZING and placement of interface */
253     settings->beginGroup( "MainWindow" );
254     QVLCTools::restoreWidgetPosition( settings, this, QSize(380, 60) );
255
256     /* resize to previously saved main window size if appicable */
257     if( b_keep_size )
258     {
259        if( i_visualmode == QT_ALWAYS_VIDEO_MODE ||
260            i_visualmode == QT_MINIMAL_MODE )
261        {
262            resize( mainVideoSize );
263        }
264        else
265        {
266            resize( mainBasedSize );
267        }
268     }
269
270     msg_Dbg( p_intf, "%i", stackCentralOldState );
271     /* Playlist */
272     if( settings->value( "playlist-visible", 0 ).toInt() )
273         togglePlaylist();
274     settings->endGroup();
275
276     /* Final sizing and showing */
277     setVisible( !b_hideAfterCreation );
278     //setMinimumSize( QSize( 0, 0 ) );
279     //    setMinimumWidth( __MAX( controls->sizeHint().width(),
280     //                          menuBar()->sizeHint().width() ) );
281
282     debug();
283     /* And switch to minimal view if needed
284        Must be called after the show() */
285     if( i_visualmode == QT_MINIMAL_MODE )
286         toggleMinimalView( true );
287
288     /* Update the geometry : It is useful if you switch between
289        qt-display-modes */
290     updateGeometry();
291     resize( sizeHint() );
292 }
293
294 MainInterface::~MainInterface()
295 {
296     msg_Dbg( p_intf, "Destroying the main interface" );
297
298     /* Unsure we hide the videoWidget before destroying it */
299     if( stackCentralOldState == VIDEO_TAB )
300         showBg();
301
302     /* Save playlist state */
303     if( playlistWidget )
304     {
305         if( !isDocked() )
306             QVLCTools::saveWidgetPosition( p_intf, "Playlist", playlistWidget );
307
308         delete playlistWidget;
309     }
310
311 #ifdef WIN32
312     if( himl )
313         ImageList_Destroy( himl );
314     if(p_taskbl)
315         p_taskbl->vt->Release(p_taskbl);
316     CoUninitialize();
317 #endif
318
319     /* Be sure to kill the actionsManager... FIXME */
320     ActionsManager::killInstance();
321
322     /* Idem, FIXME */
323     ExtensionsManager::killInstance();
324
325     /* Delete the FSC controller */
326     delete fullscreenControls;
327
328     /* Save states */
329     settings->beginGroup( "MainWindow" );
330     settings->setValue( "pl-dock-status", (int)i_pl_dock );
331     settings->setValue( "playlist-visible", (int)playlistVisible );
332     settings->setValue( "adv-controls",
333                         getControlsVisibilityStatus() & CONTROLS_ADVANCED );
334
335     settings->setValue( "mainBasedSize", mainBasedSize );
336     settings->setValue( "mainVideoSize", mainVideoSize );
337
338     if( bgWidget )
339         settings->setValue( "backgroundSize", bgWidget->size() );
340
341     /* Save this size */
342     QVLCTools::saveWidgetPosition(settings, this);
343     settings->endGroup();
344
345     /* Unregister callbacks */
346     var_DelCallback( p_intf->p_libvlc, "intf-show", IntfShowCB, p_intf );
347     var_DelCallback( p_intf->p_libvlc, "intf-popupmenu", PopupMenuCB, p_intf );
348
349     p_intf->p_sys->p_mi = NULL;
350 }
351
352 /*****************************
353  *   Main UI handling        *
354  *****************************/
355 void MainInterface::recreateToolbars()
356 {
357     //msg_Dbg( p_intf, "Recreating the toolbars" );
358     settings->beginGroup( "MainWindow" );
359     delete controls;
360     delete inputC;
361
362     controls = new ControlsWidget( p_intf, false, this ); /* FIXME */
363     CONNECT( controls, advancedControlsToggled( bool ),
364              this, doComponentsUpdate() );
365     CONNECT( controls, sizeChanged(),
366              this, doComponentsUpdate() );
367
368     inputC = new InputControlsWidget( p_intf, this );
369
370     mainLayout->insertWidget( 2, inputC );
371     mainLayout->insertWidget( settings->value( "ToolbarPos", 0 ).toInt() ? 0: 3,
372                               controls );
373     settings->endGroup();
374 }
375
376 void MainInterface::createMainWidget( QSettings *settings )
377 {
378     /* Create the main Widget and the mainLayout */
379     QWidget *main = new QWidget;
380     setCentralWidget( main );
381     mainLayout = new QVBoxLayout( main );
382
383     /* Margins, spacing */
384     main->setContentsMargins( 0, 0, 0, 0 );
385     mainLayout->setSpacing( 0 ); mainLayout->setMargin( 0 );
386
387     /* */
388     stackCentralW = new QStackedWidget( main );
389
390     /* Bg Cone */
391     bgWidget = new BackgroundWidget( p_intf );
392     bgWidget->resize(
393             settings->value( "backgroundSize", QSize( 300, 200 ) ).toSize() );
394     bgWidget->updateGeometry();
395     stackCentralW->insertWidget( BACKG_TAB, bgWidget );
396
397
398     /* And video Outputs */
399     if( videoEmbeddedFlag )
400     {
401         videoWidget = new VideoWidget( p_intf );
402         stackCentralW->insertWidget( VIDEO_TAB, videoWidget );
403     }
404     mainLayout->insertWidget( 1, stackCentralW, 100 );
405
406
407     /* Create the CONTROLS Widget */
408     controls = new ControlsWidget( p_intf,
409                    settings->value( "adv-controls", false ).toBool(), this );
410     CONNECT( controls, advancedControlsToggled( bool ),
411              this, doComponentsUpdate() );
412     CONNECT( controls, sizeChanged(),
413              this, doComponentsUpdate() );
414     inputC = new InputControlsWidget( p_intf, this );
415
416     if( i_visualmode != QT_ALWAYS_VIDEO_MODE &&
417         i_visualmode != QT_MINIMAL_MODE )
418     {
419         hideStackWidget();
420         stackCentralOldState = HIDDEN_TAB;
421     }
422     else
423     {
424         showTab( BACKG_TAB );
425         stackCentralOldState = BACKG_TAB;
426     }
427
428
429     //mainLayout->setRowStretch( 1, 10 );
430     mainLayout->insertWidget( 2, inputC );
431     mainLayout->insertWidget( settings->value( "ToolbarPos", 0 ).toInt() ? 0: 3,
432                               controls );
433
434     /* Visualisation */
435     /* Disabled for now, they SUCK */
436     #if 0
437     visualSelector = new VisualSelector( p_intf );
438     mainLayout->insertWidget( 0, visualSelector );
439     visualSelector->hide();
440     #endif
441
442     /* Finish the sizing */
443     main->updateGeometry();
444
445     getSettings()->endGroup();
446 #ifdef WIN32
447     if ( depth() > 8 )
448 #endif
449     /* Create the FULLSCREEN CONTROLS Widget */
450     if( var_InheritBool( p_intf, "qt-fs-controller" ) )
451     {
452         fullscreenControls = new FullscreenControllerWidget( p_intf, this );
453         CONNECT( fullscreenControls, keyPressed( QKeyEvent * ),
454                  this, handleKeyPress( QKeyEvent * ) );
455     }
456 }
457
458 inline void MainInterface::initSystray()
459 {
460 #ifndef HAVE_MAEMO
461     bool b_systrayAvailable = QSystemTrayIcon::isSystemTrayAvailable();
462     bool b_systrayWanted = var_InheritBool( p_intf, "qt-system-tray" );
463
464     if( var_InheritBool( p_intf, "qt-start-minimized") )
465     {
466         if( b_systrayAvailable )
467         {
468             b_systrayWanted = true;
469             b_hideAfterCreation = true;
470         }
471         else
472             msg_Err( p_intf, "cannot start minimized without system tray bar" );
473     }
474
475     if( b_systrayAvailable && b_systrayWanted )
476         createSystray();
477 #endif
478 }
479
480 inline void MainInterface::createStatusBar()
481 {
482     /****************
483      *  Status Bar  *
484      ****************/
485     /* Widgets Creation*/
486     QStatusBar *statusBarr = statusBar();
487
488     TimeLabel *timeLabel = new TimeLabel( p_intf );
489     nameLabel = new QLabel( this );
490     nameLabel->setTextInteractionFlags( Qt::TextSelectableByMouse
491                                       | Qt::TextSelectableByKeyboard );
492     SpeedLabel *speedLabel = new SpeedLabel( p_intf, "1.00x", this );
493
494     /* Styling those labels */
495     timeLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
496     speedLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
497     nameLabel->setFrameStyle( QFrame::Sunken | QFrame::StyledPanel);
498
499     /* and adding those */
500     statusBarr->addWidget( nameLabel, 8 );
501     statusBarr->addPermanentWidget( speedLabel, 0 );
502     statusBarr->addPermanentWidget( timeLabel, 0 );
503
504     /* timeLabel behaviour:
505        - double clicking opens the goto time dialog
506        - right-clicking and clicking just toggle between remaining and
507          elapsed time.*/
508     CONNECT( timeLabel, timeLabelDoubleClicked(), THEDP, gotoTimeDialog() );
509
510     CONNECT( THEMIM->getIM(), encryptionChanged( bool ),
511              this, showCryptedLabel( bool ) );
512 }
513
514 #ifdef WIN32
515 void MainInterface::createTaskBarButtons()
516 {
517     taskbar_wmsg = WM_NULL;
518     /*Here is the code for the taskbar thumb buttons
519     FIXME:We need pretty buttons in 16x16 px that are handled correctly by masks in Qt
520     FIXME:the play button's picture doesn't changed to pause when clicked
521     */
522     OSVERSIONINFO winVer;
523     winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
524     if( GetVersionEx(&winVer) && winVer.dwMajorVersion > 5 )
525     {
526         if(himl = ImageList_Create( 15, //cx
527                                     18, //cy
528                                     ILC_COLOR,//flags
529                                     4,//initial nb of images
530                                     0//nb of images that can be added
531                                     ))
532         {
533             QPixmap img   = QPixmap(":/toolbar/previous_b");
534             QPixmap img2  = QPixmap(":/toolbar/pause_b");
535             QPixmap img3  = QPixmap(":/toolbar/play_b");
536             QPixmap img4  = QPixmap(":/toolbar/next_b");
537             QBitmap mask  = img.createMaskFromColor(Qt::transparent);
538             QBitmap mask2 = img2.createMaskFromColor(Qt::transparent);
539             QBitmap mask3 = img3.createMaskFromColor(Qt::transparent);
540             QBitmap mask4 = img4.createMaskFromColor(Qt::transparent);
541
542             if(-1 == ImageList_Add(himl, img.toWinHBITMAP(QPixmap::PremultipliedAlpha),mask.toWinHBITMAP()))
543                 msg_Err( p_intf, "ImageList_Add failed" );
544             if(-1 == ImageList_Add(himl, img2.toWinHBITMAP(QPixmap::PremultipliedAlpha),mask2.toWinHBITMAP()))
545                 msg_Err( p_intf, "ImageList_Add failed" );
546             if(-1 == ImageList_Add(himl, img3.toWinHBITMAP(QPixmap::PremultipliedAlpha),mask3.toWinHBITMAP()))
547                 msg_Err( p_intf, "ImageList_Add failed" );
548             if(-1 == ImageList_Add(himl, img4.toWinHBITMAP(QPixmap::PremultipliedAlpha),mask4.toWinHBITMAP()))
549                 msg_Err( p_intf, "ImageList_Add failed" );
550         }
551
552         CoInitialize( 0 );
553
554         if( S_OK == CoCreateInstance( &clsid_ITaskbarList,
555                     NULL, CLSCTX_INPROC_SERVER,
556                     &IID_ITaskbarList3,
557                     (void **)&p_taskbl) )
558         {
559             p_taskbl->vt->HrInit(p_taskbl);
560
561             // Define an array of two buttons. These buttons provide images through an
562             // image list and also provide tooltips.
563             DWORD dwMask = THB_BITMAP | THB_FLAGS;
564
565             THUMBBUTTON thbButtons[3];
566             thbButtons[0].dwMask = dwMask;
567             thbButtons[0].iId = 0;
568             thbButtons[0].iBitmap = 0;
569             thbButtons[0].dwFlags = THBF_HIDDEN;
570
571             thbButtons[1].dwMask = dwMask;
572             thbButtons[1].iId = 1;
573             thbButtons[1].iBitmap = 2;
574             thbButtons[1].dwFlags = THBF_HIDDEN;
575
576             thbButtons[2].dwMask = dwMask;
577             thbButtons[2].iId = 2;
578             thbButtons[2].iBitmap = 3;
579             thbButtons[2].dwFlags = THBF_HIDDEN;
580
581             HRESULT hr = p_taskbl->vt->ThumbBarSetImageList(p_taskbl, winId(), himl );
582             if(S_OK != hr)
583                 msg_Err( p_intf, "ThumbBarSetImageList failed with error %08x", hr );
584             else
585             {
586                 hr = p_taskbl->vt->ThumbBarAddButtons(p_taskbl, winId(), 3, thbButtons);
587                 if(S_OK != hr)
588                     msg_Err( p_intf, "ThumbBarAddButtons failed with error %08x", hr );
589             }
590             CONNECT( THEMIM->getIM(), statusChanged( int ), this, changeThumbbarButtons( int ) );
591         }
592     }
593     else
594     {
595         himl = NULL;
596         p_taskbl = NULL;
597     }
598 }
599
600 bool MainInterface::winEvent ( MSG * msg, long * result )
601 {
602     if (msg->message == taskbar_wmsg)
603     {
604         //We received the taskbarbuttoncreated, now we can really create th buttons
605         createTaskBarButtons();
606     }
607
608     short cmd;
609     switch( msg->message )
610     {
611         case WM_COMMAND:
612             if (HIWORD(msg->wParam) == THBN_CLICKED)
613             {
614                 switch(LOWORD(msg->wParam))
615                 {
616                     case 0:
617                         THEMIM->prev();
618                         break;
619                     case 1:
620                         THEMIM->togglePlayPause();
621                         break;
622                     case 2:
623                         THEMIM->next();
624                         break;
625                 }
626             }
627             break;
628         case WM_APPCOMMAND:
629             cmd = GET_APPCOMMAND_LPARAM(msg->lParam);
630             switch(cmd)
631             {
632                 case APPCOMMAND_MEDIA_PLAY_PAUSE:
633                     THEMIM->togglePlayPause();
634                     break;
635                 case APPCOMMAND_MEDIA_PLAY:
636                     THEMIM->play();
637                     break;
638                 case APPCOMMAND_MEDIA_PAUSE:
639                     THEMIM->pause();
640                     break;
641                 case APPCOMMAND_MEDIA_PREVIOUSTRACK:
642                     THEMIM->prev();
643                     break;
644                 case APPCOMMAND_MEDIA_NEXTTRACK:
645                     THEMIM->next();
646                     break;
647                 case APPCOMMAND_MEDIA_STOP:
648                     THEMIM->stop();
649                     break;
650                 case APPCOMMAND_VOLUME_DOWN:
651                     THEAM->AudioDown();
652                     break;
653                 case APPCOMMAND_VOLUME_UP:
654                     THEAM->AudioUp();
655                     break;
656                 case APPCOMMAND_VOLUME_MUTE:
657                     THEAM->toggleMuteAudio();
658                     break;
659                 default:
660                      msg_Dbg( p_intf, "unknown APPCOMMAND = %d", cmd);
661                      break;
662             }
663             break;
664     }
665     return false;
666 }
667 #endif
668
669 /**********************************************************************
670  * Handling of sizing of the components
671  **********************************************************************/
672
673 /* This function is probably wrong, but we don't have many many choices...
674    Since we can't know from the playlist Widget if we are inside a dock or not,
675    because the playlist Widget can be called by THEDP, as a separate windows for
676    the skins.
677    Maybe the other solution is to redefine the sizeHint() of the playlist and
678    ask _parent->isFloating()...
679    If you think this would be better, please FIXME it...
680 */
681
682 QSize MainInterface::sizeHint() const
683 {
684 #if 0
685     if( b_keep_size )
686     {
687         if( i_visualmode == QT_ALWAYS_VIDEO_MODE ||
688             i_visualmode == QT_MINIMAL_MODE )
689         {
690                 return mainVideoSize;
691         }
692         else
693         {
694             if( VISIBLE( bgWidget) ||
695                 ( videoIsActive && videoWidget->isVisible() )
696               )
697                 return mainVideoSize;
698             else
699                 return mainBasedSize;
700         }
701     }
702 #endif
703
704     int nwidth  = __MAX( controls->sizeHint().width(),
705                          menuBar()->sizeHint().width() );
706
707     int nheight = controls->isVisible() ?
708                   controls->size().height()
709                   + inputC->size().height()
710                   + menuBar()->size().height()
711                   + statusBar()->size().height()
712                   : 0 ;
713
714     if( stackCentralW->isVisible() )
715         nheight += stackCentralW->height();
716         nwidth  = __MAX( nwidth, stackCentralW->width() );
717
718 /*    if( VISIBLE( bgWidget ) )
719     {
720         msg_Warn( p_intf, "Hello here" );
721         if( i_bg_height )
722             nheight += i_bg_height;
723         else
724             nheight += bgWidget->size().height();
725         nwidth  = __MAX( nwidth, bgWidget->size().width() );
726     }
727     else if( videoIsActive && videoWidget->isVisible() )
728     {
729         msg_Warn( p_intf, "Hello there" );
730         nheight += videoWidget->sizeHint().height();
731         nwidth  = __MAX( nwidth, videoWidget->sizeHint().width() );
732     }*/
733 #if 0
734     if( !dockPL->isFloating() && dockPL->isVisible() && dockPL->widget()  )
735     {
736         nheight += dockPL->size().height();
737         nwidth = __MAX( nwidth, dockPL->size().width() );
738         msg_Warn( p_intf, "3 %i %i", nheight, nwidth );
739     }
740 #endif
741     return QSize( nwidth, nheight );
742 }
743
744
745 /* Video widget cannot do this synchronously as it runs in another thread */
746 /* Well, could it, actually ? Probably dangerous ... */
747
748 /* This function is called:
749    - toggling of minimal View
750    - through askUpdate() by Vout thread request video and resize video (zoom)
751    - Advanced buttons toggled
752  */
753 void MainInterface::doComponentsUpdate()
754 {
755     if( isFullScreen() || isMaximized() ) return;
756
757 //    msg_Warn( p_intf, "Updating the geometry" );
758     /* Here we resize to sizeHint() and not adjustsize because we want
759        the videoWidget to be exactly the correctSize */
760
761 #ifndef NDEBUG
762     debug();
763 #endif
764     /* This is WRONG, but I believe there is a Qt bug here */
765     setMinimumSize( 0, 0 );
766     resize( sizeHint() );
767
768     //adjustSize() ; /* This is not needed, but might help in the future */
769 }
770
771 void MainInterface::debug()
772 {
773 #ifndef NDEBUG
774     msg_Dbg( p_intf, "Stack Size: %i - %i", stackCentralW->size().height(), size().width() );
775     if( videoEmbeddedFlag )
776         msg_Dbg( p_intf, "Stack Size: %i - %i",
777                  stackCentralW->widget( VIDEO_TAB )->size().height(),
778                  stackCentralW->widget( VIDEO_TAB )->size().width() );
779     else
780         msg_Dbg( p_intf, "no embedded video" );
781
782     msg_Dbg( p_intf, "size: %i - %i", size().height(), size().width() );
783     msg_Dbg( p_intf, "sizeHint: %i - %i", sizeHint().height(), sizeHint().width() );
784     //msg_Dbg( p_intf, "maximumsize: %i - %i", maximumSize().height(), maximumSize().width() );
785
786     msg_Dbg( p_intf, "Stack minimumsize: %i - %i", stackCentralW->minimumSize().height(), stackCentralW->minimumSize().width() );
787     msg_Dbg( p_intf, "Controls minimumsize: %i - %i", controls->minimumSize().height(), controls->minimumSize().width() );
788     msg_Dbg( p_intf, "Central minimumsize: %i - %i", centralWidget()->minimumSize().height(), centralWidget()->minimumSize().width() );
789     msg_Dbg( p_intf, "Menu minimumsize: %i - %i", menuBar()->minimumSize().height(), menuBar()->minimumSize().width() );
790     msg_Dbg( p_intf, "Input minimuSize: %i - %i", inputC->minimumSize().height(), inputC->minimumSize().width() );
791     msg_Dbg( p_intf, "Status minimumsize: %i - %i", statusBar()->minimumSize().height(), statusBar()->minimumSize().width() );
792     msg_Dbg( p_intf, "minimumsize: %i - %i", minimumSize().height(), minimumSize().width() );
793
794     /*if( videoWidget && videoWidget->isVisible() )
795     {
796         msg_Dbg( p_intf, "size: %i - %i", size().height(), size().width() );
797         msg_Dbg( p_intf, "sizeHint: %i - %i", sizeHint().height(), sizeHint().width() );
798     }*/
799 #endif
800 }
801
802 inline void MainInterface::showTab( int i_tab )
803 {
804 #ifdef DEBUG_INTF
805     msg_Err( p_intf, "showTab %i", i_tab );
806     msg_Warn( p_intf, "Old stackCentralOldState %i", stackCentralOldState );
807 #endif
808     stackCentralOldState = stackCentralW->isVisible() ? stackCentralW->currentIndex()
809                                           : HIDDEN_TAB;
810 #ifdef DEBUG_INTF
811     msg_Warn( p_intf, "State change %i %i",  stackCentralW->currentIndex(), i_tab );
812 #endif
813
814     if( i_visualmode == QT_NORMAL_MODE )
815     {
816         stackCentralW->setVisible( i_tab != HIDDEN_TAB );
817         doComponentsUpdate(); // resize the player
818     }
819     else
820         if( i_tab == HIDDEN_TAB ) i_tab == BACKG_TAB;
821
822     stackCentralW->setCurrentIndex( i_tab );
823
824 #ifdef DEBUG_INTF
825     msg_Warn( p_intf, "New stackCentralOldState %i", stackCentralOldState );
826 #endif
827 }
828
829 inline void MainInterface::restoreStackOldWidget()
830 {
831 #ifdef DEBUG_INTF
832     msg_Warn( p_intf, "Old stackCentralOldState %i", stackCentralOldState );
833 #endif
834     int temp = stackCentralW->isVisible() ? stackCentralW->currentIndex()
835                                           : HIDDEN_TAB;
836     stackCentralW->setCurrentIndex( stackCentralOldState );
837     if( i_visualmode == QT_NORMAL_MODE )
838     {
839         stackCentralW->setVisible( stackCentralOldState != HIDDEN_TAB );
840         doComponentsUpdate(); // resize the player
841     }
842
843     stackCentralOldState = temp;
844 #ifdef DEBUG_INTF
845     msg_Warn( p_intf, "Debug %i %i", temp, stackCentralW->currentIndex() );
846 #endif
847 }
848
849 void MainInterface::destroyPopupMenu()
850 {
851     QVLCMenu::PopupMenu( p_intf, false );
852 }
853
854 void MainInterface::toggleFSC()
855 {
856    if( !fullscreenControls ) return;
857
858    IMEvent *eShow = new IMEvent( FullscreenControlToggle_Type, 0 );
859    QApplication::postEvent( fullscreenControls, eShow );
860 }
861
862 void MainInterface::popupMenu( const QPoint &p )
863 {
864     /* Ow, that's ugly: don't show the popup menu if cursor over
865      * the main menu bar or the status bar */
866     if( !childAt( p ) || ( ( childAt( p ) != menuBar() )
867                         && ( childAt( p )->parentWidget() != statusBar() ) ) )
868         QVLCMenu::PopupMenu( p_intf, true );
869 }
870
871 /****************************************************************************
872  * Video Handling
873  ****************************************************************************/
874
875 /* This event is used to deal with the fullscreen and always on top
876    issue conflict (bug in wx) */
877 class SetVideoOnTopQtEvent : public QEvent
878 {
879 public:
880     SetVideoOnTopQtEvent( bool _onTop ) :
881       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
882     {}
883
884     bool OnTop() const { return onTop; }
885
886 private:
887     bool onTop;
888 };
889
890 /**
891  * NOTE:
892  * You must note change the state of this object or other Qt4 UI objects,
893  * from the video output thread - only from the Qt4 UI main loop thread.
894  * All window provider queries must be handled through signals or events.
895  * That's why we have all those emit statements...
896  */
897 WId MainInterface::getVideo( int *pi_x, int *pi_y,
898                              unsigned int *pi_width, unsigned int *pi_height )
899 {
900     if( !videoWidget )
901         return 0;
902
903     /* This is a blocking call signal. Results are returned through pointers.
904      * Beware of deadlocks! */
905     WId id;
906     emit askGetVideo( &id, pi_x, pi_y, pi_width, pi_height );
907     return id;
908 }
909
910 void MainInterface::getVideoSlot( WId *p_id, int *pi_x, int *pi_y,
911                                   unsigned *pi_width, unsigned *pi_height )
912 {
913     /* Request the videoWidget */
914     WId ret = videoWidget->request( pi_x, pi_y,
915                                     pi_width, pi_height, b_keep_size );
916     *p_id = ret;
917     if( ret ) /* The videoWidget is available */
918     {
919         /* ask videoWidget to show */
920         videoWidget->SetSizing( *pi_width, *pi_height );
921
922         /* Consider the video active now */
923         showVideo();
924
925         stackCentralW->resize( *pi_width, *pi_height );
926
927         emit askUpdate();
928     }
929 }
930
931
932
933 /* Asynchronous call from the WindowClose function */
934 void MainInterface::releaseVideo( void )
935 {
936     emit askReleaseVideo( );
937 }
938
939 /* Function that is CONNECTED to the previous emit */
940 void MainInterface::releaseVideoSlot( void )
941 {
942     videoWidget->release( );
943
944     restoreStackOldWidget();
945
946     /* Try to resize, except when you are in Fullscreen mode */
947 //    doComponentsUpdate();
948 }
949
950 /* Asynchronous call from WindowControl function */
951 int MainInterface::controlVideo( int i_query, va_list args )
952 {
953     /* Debug to check if VOUT_WINDOW_SET_SIZE is called, because this is broken now */
954     msg_Warn( p_intf, "Control Video: %i", i_query );
955     switch( i_query )
956     {
957     case VOUT_WINDOW_SET_SIZE:
958     {
959         unsigned int i_width  = va_arg( args, unsigned int );
960         unsigned int i_height = va_arg( args, unsigned int );
961         emit askVideoToResize( i_width, i_height );
962         emit askUpdate();
963         return VLC_EGENERIC;
964     }
965     case VOUT_WINDOW_SET_STATE:
966     {
967         unsigned i_arg = va_arg( args, unsigned );
968         unsigned on_top = i_arg & VOUT_WINDOW_STATE_ABOVE;
969         QApplication::postEvent( this, new SetVideoOnTopQtEvent( on_top ) );
970         return VLC_SUCCESS;
971     }
972     case VOUT_WINDOW_SET_FULLSCREEN:
973     {
974         bool b_fs = va_arg( args, int );
975         emit askVideoSetFullScreen( b_fs );
976         return VLC_SUCCESS;
977     }
978     default:
979         msg_Warn( p_intf, "unsupported control query" );
980         return VLC_EGENERIC;
981     }
982 }
983
984 /*****************************************************************************
985  * Playlist, Visualisation and Menus handling
986  *****************************************************************************/
987 /**
988  * Toggle the playlist widget or dialog
989  **/
990 void MainInterface::createPlaylist( bool b_show )
991 {
992     playlistWidget = new PlaylistWidget( p_intf, this );
993
994     i_pl_dock = PL_BOTTOM;
995     /* i_pl_dock = (pl_dock_e)getSettings()
996       ->value( "pl-dock-status", PL_UNDOCKED ).toInt(); */
997
998     if( i_pl_dock == PL_UNDOCKED )
999     {
1000         playlistWidget->setWindowFlags( Qt::Window );
1001
1002         /* This will restore the geometry but will not work for position,
1003            because of parenting */
1004         QVLCTools::restoreWidgetPosition( p_intf, "Playlist",
1005                 playlistWidget, QSize( 600, 300 ) );
1006     }
1007     else
1008     {
1009 #ifdef DEBUG_INTF
1010         msg_Warn( p_intf, "Here %i", stackCentralW->currentIndex() );
1011 #endif
1012         stackCentralW->insertWidget( PLAYL_TAB, playlistWidget );
1013 #ifdef DEBUG_INTF
1014         msg_Warn( p_intf, "Here %i", stackCentralW->currentIndex() );
1015 #endif
1016     }
1017
1018     if( b_show )
1019     {
1020         playlistVisible = true;
1021         stackCentralW->show();
1022     }
1023 }
1024
1025 void MainInterface::togglePlaylist()
1026 {
1027 #ifdef DEBUG_INTF
1028     msg_Warn( p_intf, "Here toggling %i %i", stackCentralW->currentIndex(), stackCentralOldState );
1029 #endif
1030     if( !playlistWidget )
1031     {
1032         createPlaylist( true );
1033     }
1034 #ifdef DEBUG_INTF
1035     msg_Warn( p_intf, "Here toggling %i %i", stackCentralW->currentIndex(), stackCentralOldState );
1036
1037 #endif
1038     if( i_pl_dock != PL_UNDOCKED )
1039     {
1040         /* Playlist not visible */
1041         if( stackCentralW->currentIndex() != PLAYL_TAB )
1042         {
1043             showTab( PLAYL_TAB );
1044             stackCentralW->show();
1045         }
1046         else
1047         {
1048             restoreStackOldWidget();
1049         }
1050         playlistVisible = ( stackCentralW->currentIndex() == PLAYL_TAB );
1051         //doComponentsUpdate(); //resize( sizeHint() );
1052     }
1053 }
1054
1055 /* Function called from the menu to undock the playlist */
1056 void MainInterface::undockPlaylist()
1057 {
1058 //    dockPL->setFloating( true );
1059 //    adjustSize();
1060 }
1061
1062 void MainInterface::dockPlaylist( pl_dock_e i_pos )
1063 {
1064 }
1065
1066 void MainInterface::toggleMinimalView( bool b_switch )
1067 {
1068     if( i_visualmode != QT_ALWAYS_VIDEO_MODE &&
1069         i_visualmode != QT_MINIMAL_MODE )
1070     { /* NORMAL MODE then */
1071         stackCentralW->show();
1072         if( !videoWidget || stackCentralW->currentIndex() != VIDEO_TAB )
1073         {
1074             showBg();
1075         }
1076         else
1077         {
1078             /* If video is visible, then toggle the status of bgWidget */
1079             //bgWasVisible = !bgWasVisible;
1080             if( stackCentralOldState == BACKG_TAB )
1081                 stackCentralOldState = HIDDEN_TAB;
1082             else
1083                 stackCentralOldState = BACKG_TAB;
1084         }
1085     }
1086
1087     i_bg_height = stackCentralW->height();
1088
1089     menuBar()->setVisible( !b_switch );
1090     controls->setVisible( !b_switch );
1091     statusBar()->setVisible( !b_switch );
1092     inputC->setVisible( !b_switch );
1093
1094     doComponentsUpdate();
1095
1096     emit minimalViewToggled( b_switch );
1097 }
1098
1099 /* toggling advanced controls buttons */
1100 void MainInterface::toggleAdvanced()
1101 {
1102     controls->toggleAdvanced();
1103 //    if( fullscreenControls ) fullscreenControls->toggleAdvanced();
1104 }
1105
1106 /* Get the visibility status of the controls (hidden or not, advanced or not) */
1107 int MainInterface::getControlsVisibilityStatus()
1108 {
1109     return( (controls->isVisible() ? CONTROLS_VISIBLE : CONTROLS_HIDDEN )
1110                 + CONTROLS_ADVANCED * controls->b_advancedVisible );
1111 }
1112
1113 #if 0
1114 void MainInterface::visual()
1115 {
1116     if( !VISIBLE( visualSelector) )
1117     {
1118         visualSelector->show();
1119         if( !THEMIM->getIM()->hasVideo() )
1120         {
1121             /* Show the background widget */
1122         }
1123         visualSelectorEnabled = true;
1124     }
1125     else
1126     {
1127         /* Stop any currently running visualization */
1128         visualSelector->hide();
1129         visualSelectorEnabled = false;
1130     }
1131     doComponentsUpdate();
1132 }
1133 #endif
1134
1135 /************************************************************************
1136  * Other stuff
1137  ************************************************************************/
1138 void MainInterface::setName( const QString& name )
1139 {
1140     input_name = name; /* store it for the QSystray use */
1141     /* Display it in the status bar, but also as a Tooltip in case it doesn't
1142        fit in the label */
1143     nameLabel->setText( " " + name + " " );
1144     nameLabel->setToolTip( " " + name +" " );
1145 }
1146
1147 /**
1148  * Give the decorations of the Main Window a correct Name.
1149  * If nothing is given, set it to VLC...
1150  **/
1151 void MainInterface::setVLCWindowsTitle( const QString& aTitle )
1152 {
1153     if( aTitle.isEmpty() )
1154     {
1155         setWindowTitle( qtr( "VLC media player" ) );
1156     }
1157     else
1158     {
1159         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
1160     }
1161 }
1162
1163 void MainInterface::showCryptedLabel( bool b_show )
1164 {
1165     if( cryptedLabel == NULL )
1166     {
1167         cryptedLabel = new QLabel;
1168         // The lock icon is not the right one for DRM protection/scrambled.
1169         //cryptedLabel->setPixmap( QPixmap( ":/lock" ) );
1170         cryptedLabel->setText( "DRM" );
1171         statusBar()->addWidget( cryptedLabel );
1172     }
1173
1174     cryptedLabel->setVisible( b_show );
1175 }
1176
1177 /*****************************************************************************
1178  * Systray Icon and Systray Menu
1179  *****************************************************************************/
1180 #ifndef HAVE_MAEMO
1181 /**
1182  * Create a SystemTray icon and a menu that would go with it.
1183  * Connects to a click handler on the icon.
1184  **/
1185 void MainInterface::createSystray()
1186 {
1187     QIcon iconVLC;
1188     if( QDate::currentDate().dayOfYear() >= 354 )
1189         iconVLC =  QIcon( ":/logo/vlc128-christmas.png" );
1190     else
1191         iconVLC =  QIcon( ":/logo/vlc128.png" );
1192     sysTray = new QSystemTrayIcon( iconVLC, this );
1193     sysTray->setToolTip( qtr( "VLC media player" ));
1194
1195     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
1196     systrayMenu->setIcon( iconVLC );
1197
1198     QVLCMenu::updateSystrayMenu( this, p_intf, true );
1199     sysTray->show();
1200
1201     CONNECT( sysTray, activated( QSystemTrayIcon::ActivationReason ),
1202             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
1203 }
1204
1205 /**
1206  * Updates the Systray Icon's menu and toggle the main interface
1207  */
1208 void MainInterface::toggleUpdateSystrayMenu()
1209 {
1210     /* If hidden, show it */
1211     if( isHidden() )
1212     {
1213         show();
1214         activateWindow();
1215     }
1216     else if( isMinimized() )
1217     {
1218         /* Minimized */
1219         showNormal();
1220         activateWindow();
1221     }
1222     else
1223     {
1224         /* Visible (possibly under other windows) */
1225 #ifdef WIN32
1226         /* check if any visible window is above vlc in the z-order,
1227          * but ignore the ones always on top
1228          * and the ones which can't be activated */
1229         WINDOWINFO wi;
1230         HWND hwnd;
1231         wi.cbSize = sizeof( WINDOWINFO );
1232         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
1233                 hwnd && ( !IsWindowVisible( hwnd ) ||
1234                     ( GetWindowInfo( hwnd, &wi ) &&
1235                       (wi.dwExStyle&WS_EX_NOACTIVATE) ) );
1236                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
1237             if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
1238                 (wi.dwExStyle&WS_EX_TOPMOST) )
1239             {
1240                 hide();
1241             }
1242             else
1243             {
1244                 activateWindow();
1245             }
1246 #else
1247         hide();
1248 #endif
1249     }
1250     QVLCMenu::updateSystrayMenu( this, p_intf );
1251 }
1252
1253 void MainInterface::handleSystrayClick(
1254                                     QSystemTrayIcon::ActivationReason reason )
1255 {
1256     switch( reason )
1257     {
1258         case QSystemTrayIcon::Trigger:
1259         case QSystemTrayIcon::DoubleClick:
1260             toggleUpdateSystrayMenu();
1261             break;
1262         case QSystemTrayIcon::MiddleClick:
1263             sysTray->showMessage( qtr( "VLC media player" ),
1264                     qtr( "Control menu for the player" ),
1265                     QSystemTrayIcon::Information, 3000 );
1266             break;
1267         default:
1268             break;
1269     }
1270 }
1271
1272 /**
1273  * Updates the name of the systray Icon tooltip.
1274  * Doesn't check if the systray exists, check before you call it.
1275  **/
1276 void MainInterface::updateSystrayTooltipName( const QString& name )
1277 {
1278     if( name.isEmpty() )
1279     {
1280         sysTray->setToolTip( qtr( "VLC media player" ) );
1281     }
1282     else
1283     {
1284         sysTray->setToolTip( name );
1285         if( notificationEnabled && ( isHidden() || isMinimized() ) )
1286         {
1287             sysTray->showMessage( qtr( "VLC media player" ), name,
1288                     QSystemTrayIcon::NoIcon, 3000 );
1289         }
1290     }
1291
1292     QVLCMenu::updateSystrayMenu( this, p_intf );
1293 }
1294
1295 /**
1296  * Updates the status of the systray Icon tooltip.
1297  * Doesn't check if the systray exists, check before you call it.
1298  **/
1299 void MainInterface::updateSystrayTooltipStatus( int i_status )
1300 {
1301     switch( i_status )
1302     {
1303         case  0:
1304         case  END_S:
1305             {
1306                 sysTray->setToolTip( qtr( "VLC media player" ) );
1307                 break;
1308             }
1309         case PLAYING_S:
1310             {
1311                 sysTray->setToolTip( input_name );
1312                 break;
1313             }
1314         case PAUSE_S:
1315             {
1316                 sysTray->setToolTip( input_name + " - "
1317                         + qtr( "Paused") );
1318                 break;
1319             }
1320     }
1321     QVLCMenu::updateSystrayMenu( this, p_intf );
1322 }
1323 #endif
1324
1325 /************************************************************************
1326  * D&D Events
1327  ************************************************************************/
1328 void MainInterface::dropEvent(QDropEvent *event)
1329 {
1330     dropEventPlay( event, true );
1331 }
1332
1333 void MainInterface::dropEventPlay( QDropEvent *event, bool b_play )
1334 {
1335     event->setDropAction( Qt::CopyAction );
1336     if( !event->possibleActions() & Qt::CopyAction )
1337         return;
1338
1339     const QMimeData *mimeData = event->mimeData();
1340
1341     /* D&D of a subtitles file, add it on the fly */
1342     if( mimeData->urls().size() == 1 && THEMIM->getIM()->hasInput() )
1343     {
1344         if( !input_AddSubtitle( THEMIM->getInput(),
1345                  qtu( toNativeSeparators( mimeData->urls()[0].toLocalFile() ) ),
1346                  true ) )
1347         {
1348             event->accept();
1349             return;
1350         }
1351     }
1352
1353     bool first = b_play;
1354     foreach( const QUrl &url, mimeData->urls() )
1355     {
1356         QString s = toNativeSeparators( url.toLocalFile() );
1357
1358         if( s.length() > 0 ) {
1359             char* psz_uri = make_URI( qtu(s) );
1360             playlist_Add( THEPL, psz_uri, NULL,
1361                           PLAYLIST_APPEND | (first ? PLAYLIST_GO: PLAYLIST_PREPARSE),
1362                           PLAYLIST_END, true, pl_Unlocked );
1363             free( psz_uri );
1364             first = false;
1365             RecentsMRL::getInstance( p_intf )->addRecent( s );
1366         }
1367     }
1368     event->accept();
1369 }
1370 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
1371 {
1372      event->acceptProposedAction();
1373 }
1374 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
1375 {
1376      event->acceptProposedAction();
1377 }
1378 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
1379 {
1380      event->accept();
1381 }
1382
1383 /************************************************************************
1384  * Events stuff
1385  ************************************************************************/
1386 void MainInterface::customEvent( QEvent *event )
1387 {
1388 #if 0
1389     if( event->type() == PLDockEvent_Type )
1390     {
1391         PlaylistDialog::killInstance();
1392         playlistEmbeddedFlag = true;
1393         menuBar()->clear();
1394         QVLCMenu::createMenuBar(this, p_intf, true, visualSelectorEnabled);
1395         togglePlaylist();
1396     }
1397 #endif
1398     /*else */
1399     if ( event->type() == (int)SetVideoOnTopEvent_Type )
1400     {
1401         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
1402         if( p_event->OnTop() )
1403             setWindowFlags( windowFlags() | Qt::WindowStaysOnTopHint );
1404         else
1405             setWindowFlags( windowFlags() & ~Qt::WindowStaysOnTopHint );
1406         show(); /* necessary to apply window flags */
1407     }
1408 }
1409
1410 void MainInterface::keyPressEvent( QKeyEvent *e )
1411 {
1412     handleKeyPress( e );
1413 }
1414
1415 void MainInterface::handleKeyPress( QKeyEvent *e )
1416 {
1417     if( ( e->modifiers() &  Qt::ControlModifier ) && ( e->key() == Qt::Key_H )
1418           && !menuBar()->isVisible() )
1419     {
1420         toggleMinimalView( false );
1421         e->accept();
1422     }
1423
1424     int i_vlck = qtEventToVLCKey( e );
1425     if( i_vlck > 0 )
1426     {
1427         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1428         e->accept();
1429     }
1430     else
1431         e->ignore();
1432 }
1433
1434 void MainInterface::resizeEvent( QResizeEvent * event )
1435 {
1436 #if 0
1437     if( b_keep_size )
1438     {
1439         if( i_visualmode == QT_ALWAYS_VIDEO_MODE ||
1440             i_visualmode == QT_MINIMAL_MODE )
1441         {
1442                 mainVideoSize = size();
1443         }
1444         else
1445         {
1446             if( VISIBLE( bgWidget) ||
1447                 ( videoIsActive && videoWidget->isVisible() )
1448               )
1449                 mainVideoSize = size();
1450             else
1451                 mainBasedSize = size();
1452         }
1453     }
1454 #endif
1455     QVLCMW::resizeEvent( event );
1456     msg_Dbg( p_intf, "Resize Event, height: %i", size().height() );
1457 }
1458
1459 void MainInterface::wheelEvent( QWheelEvent *e )
1460 {
1461     int i_vlckey = qtWheelEventToVLCKey( e );
1462     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
1463     e->accept();
1464 }
1465
1466 void MainInterface::closeEvent( QCloseEvent *e )
1467 {
1468     e->accept();
1469     hide();
1470     THEDP->quit();
1471 }
1472
1473 void MainInterface::toggleFullScreen( void )
1474 {
1475     if( isFullScreen() )
1476     {
1477         showNormal();
1478         emit askUpdate(); // Needed if video was launched after the F11
1479         emit fullscreenInterfaceToggled( false );
1480     }
1481     else
1482     {
1483         showFullScreen();
1484         emit fullscreenInterfaceToggled( true );
1485     }
1486
1487 }
1488
1489 //moc doesn't know about #ifdef, so we have to build this method for every platform
1490 void MainInterface::changeThumbbarButtons( int i_status)
1491 {
1492 #ifdef WIN32
1493     // Define an array of three buttons. These buttons provide images through an
1494     // image list and also provide tooltips.
1495     DWORD dwMask = THB_BITMAP | THB_FLAGS;
1496
1497     THUMBBUTTON thbButtons[3];
1498     //prev
1499     thbButtons[0].dwMask = dwMask;
1500     thbButtons[0].iId = 0;
1501     thbButtons[0].iBitmap = 0;
1502
1503     //play/pause
1504     thbButtons[1].dwMask = dwMask;
1505     thbButtons[1].iId = 1;
1506
1507     //next
1508     thbButtons[2].dwMask = dwMask;
1509     thbButtons[2].iId = 2;
1510     thbButtons[2].iBitmap = 3;
1511
1512     switch( i_status )
1513     {
1514         case PLAYING_S:
1515             {
1516                 thbButtons[0].dwFlags = THBF_ENABLED;
1517                 thbButtons[1].dwFlags = THBF_ENABLED;
1518                 thbButtons[2].dwFlags = THBF_ENABLED;
1519                 thbButtons[1].iBitmap = 1;
1520                 break;
1521             }
1522         case PAUSE_S:
1523             {
1524                 thbButtons[0].dwFlags = THBF_ENABLED;
1525                 thbButtons[1].dwFlags = THBF_ENABLED;
1526                 thbButtons[2].dwFlags = THBF_ENABLED;
1527                 thbButtons[1].iBitmap = 2;
1528                 break;
1529             }
1530         default:
1531             return;
1532     }
1533     HRESULT hr =  p_taskbl->vt->ThumbBarUpdateButtons(p_taskbl, this->winId(), 3, thbButtons);
1534     if(S_OK != hr)
1535         msg_Err( p_intf, "ThumbBarUpdateButtons failed with error %08x", hr );
1536 #else
1537     ;
1538 #endif
1539 }
1540
1541 /*****************************************************************************
1542  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
1543  *  We don't show the menu directly here because we don't want the
1544  *  caller to block for a too long time.
1545  *****************************************************************************/
1546 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
1547                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1548 {
1549     intf_thread_t *p_intf = (intf_thread_t *)param;
1550
1551     if( p_intf->pf_show_dialog )
1552     {
1553         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
1554                                 new_val.b_bool, NULL );
1555     }
1556
1557     return VLC_SUCCESS;
1558 }
1559
1560 /*****************************************************************************
1561  * IntfShowCB: callback triggered by the intf-show libvlc variable.
1562  *****************************************************************************/
1563 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
1564                        vlc_value_t old_val, vlc_value_t new_val, void *param )
1565 {
1566     intf_thread_t *p_intf = (intf_thread_t *)param;
1567     p_intf->p_sys->p_mi->toggleFSC();
1568
1569     /* Show event */
1570      return VLC_SUCCESS;
1571 }