summaryrefslogtreecommitdiffstats
path: root/src/app/mainWindow.cpp
blob: ac054acb38dcc1fbae66d6476bd456ff4e131778 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
// (C) 2005 Max Howell ([email protected])
// See COPYING file for licensing information

#include "actions.h"
#include "analyzer.h"
#include "config.h"
#include "configure.h"
#include <cstdlib>
#include "debug.h"
#include "extern.h"       //dialog creation function definitions
#include "fullScreenAction.h"
#include <tdeapplication.h>
#include <tdecmdlineargs.h>
#include <kcursor.h>
#include <tdefiledialog.h>     //::open()
#include <tdeglobalsettings.h> //::timerEvent()
#include <tdeio/netaccess.h>
#include <ksqueezedtextlabel.h>
#include <kstatusbar.h>
#include <tdetoolbar.h>
#include <kurldrag.h>
#include <twin.h>
#include "mainWindow.h"
#include "playDialog.h"  //::play()
#include "playlistFile.h"
#include "mxcl.library.h"
#include <ntqcstring.h>
#include <ntqdesktopwidget.h>
#include <ntqevent.h>      //::stateChanged()
#include <ntqlayout.h>     //ctor
#include <ntqpopupmenu.h>  //because XMLGUI is poorly designed
#include <ntqobjectlist.h>
#include "slider.h"
#include "theStream.h"
#include "volumeAction.h"
#include "xineEngine.h"

#ifndef NO_XTEST_EXTENSION
extern "C"
{
   #include <X11/extensions/XTest.h>
   #include <X11/keysym.h>
}
#endif


namespace Codeine {


   /// @see codeine.h
   TQWidget *mainWindow() { return kapp->mainWidget(); }


MainWindow::MainWindow()
      : TDEMainWindow()
      , m_positionSlider( new Slider( this, 65535 ) )
      , m_timeLabel( new TQLabel( " 0:00:00 ", this ) )
      , m_titleLabel( new KSqueezedTextLabel( this ) )
{
   DEBUG_BLOCK

   clearWFlags( WDestructiveClose ); //we are allocated on the stack

   kapp->setMainWidget( this );

   new VideoWindow( this );
   setCentralWidget( videoWindow() );
   setFocusProxy( videoWindow() ); // essential! See VideoWindow::event(), TQEvent::FocusOut

   // these have no affect beccause "KDE Knows Best" FFS
   setDockEnabled( toolBar(), TQt::DockRight, false ); //doesn't make sense due to our large horizontal slider
   setDockEnabled( toolBar(), TQt::DockLeft, false ); //as above

   m_titleLabel->setMargin( 2 );
   m_timeLabel->setFont( TDEGlobalSettings::fixedFont() );
   m_timeLabel->setAlignment( AlignCenter );
   m_timeLabel->setMinimumSize( m_timeLabel->sizeHint() );

   // work around a bug in KStatusBar
   // sizeHint width of statusbar seems to get stupidly large quickly
   statusBar()->setSizePolicy( TQSizePolicy::Ignored, TQSizePolicy::Maximum );

   statusBar()->addWidget( m_titleLabel, 1, false );
   statusBar()->addWidget( m_analyzer = new Analyzer::Block( this ), 0, true );
   statusBar()->addWidget( m_timeLabel, 0, true );
   setupActions();
   setupGUI();
   setStandardToolBarMenuEnabled( false ); //bah to setupGUI()!
   toolBar()->show(); //it's possible it would be hidden, but we don't want that as no UI way to show it!

   // only show dvd button when playing a dvd
   {
      struct KdeIsTehSuck : public TQObject
      {
         virtual bool eventFilter( TQObject*, TQEvent *e )
         {
            if (e->type() != TQEvent::LayoutHint)
               return false;

            // basically, KDE shows all tool-buttons, even if they are
            // hidden after it does any layout operation. Yay for KDE. Yay.
            TQWidget *button = (TQWidget*)((TDEMainWindow*)mainWindow())->toolBar()->child( "toolbutton_toggle_dvd_menu" );
            if (button)
               button->setShown( TheStream::url().protocol() == "dvd" );
            return false;
         }
      } *o;
      o = new KdeIsTehSuck;
      toolBar()->installEventFilter( o );
      insertChild( o );
   }

   {
      TQPopupMenu *menu = 0, *settings = static_cast<TQPopupMenu*>(factory()->container( "settings", this ));
      int id = SubtitleChannelsMenuItemId, index = 0;

      #define make_menu( name, text ) \
            menu = new TQPopupMenu( this, name ); \
            menu->setCheckable( true ); \
            connect( menu, SIGNAL(activated( int )), engine(), SLOT(setStreamParameter( int )) ); \
            connect( menu, SIGNAL(aboutToShow()), SLOT(aboutToShowMenu()) ); \
            settings->insertItem( text, menu, id, index ); \
            settings->setItemEnabled( id, false ); \
            id++, index++;

      make_menu( "subtitle_channels_menu", i18n( "&Subtitles" ) );
      make_menu( "audio_channels_menu", i18n( "A&udio Channels" ) );
      make_menu( "aspect_ratio_menu", i18n( "Aspect &Ratio" ) );
      #undef make_menu

      Codeine::insertAspectRatioMenuItems( menu ); //so we don't have to include xine.h here

      settings->insertSeparator( index );
   }

   TQObjectList *list = toolBar()->queryList( "TDEToolBarButton" );
   if (list->isEmpty()) {
      MessageBox::error( i18n(
            "<qt>" PRETTY_NAME " could not load its interface, this probably means that " PRETTY_NAME " is not "
            "installed to the correct prefix. If you installed from packages please contact the packager, if "
            "you installed from source please try running the <b>configure</b> script again like this: "
            "<pre> % ./configure --prefix=`tde-config --prefix`</pre>" ) );

      std::exit( 1 );
   }
   delete list;

   KXMLGUIClient::stateChanged( "empty" );

   TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
   if( args->count() || args->isSet( "play-dvd" ) || kapp->isRestored() )
      //we need to resize the window, so we can't show the window yet
      init();
   else {
      //"faster" startup
      //TODO if we have a size stored for this video, do the "faster" route
      TQTimer::singleShot( 0, this, SLOT(init()) );
      TQApplication::setOverrideCursor( KCursor::waitCursor() ); }
}

void
MainWindow::init()
{
   DEBUG_BLOCK

   connect( engine(), SIGNAL(statusMessage( const TQString& )), this, SLOT(engineMessage( const TQString& )) );
   connect( engine(), SIGNAL(stateChanged( Engine::State )), this, SLOT(engineStateChanged( Engine::State )) );
   connect( engine(), SIGNAL(channelsChanged( const TQStringList& )), this, SLOT(setChannels( const TQStringList& )) );
   connect( engine(), SIGNAL(titleChanged( const TQString& )), m_titleLabel, SLOT(setText( const TQString& )) );
   connect( m_positionSlider, SIGNAL(valueChanged( int )), this, SLOT(showTime( int )) );

   if( !engine()->init() ) {
      KMessageBox::error( this, i18n(
         "<qt>xine could not be successfully initialised. " PRETTY_NAME " will now exit. "
         "You can try to identify what is wrong with your xine installation using the <b>xine-check</b> command at a command-prompt.") );
      std::exit( 2 );
   }

   //would be dangerous for these to65535 happen before the videoWindow() is initialised
   setAcceptDrops( true );
   connect( m_positionSlider, SIGNAL(sliderReleased( uint )), engine(), SLOT(seek( uint )) );
   connect( statusBar(), SIGNAL(messageChanged( const TQString& )), engine(), SLOT(showOSD( const TQString& )) );

   TQApplication::restoreOverrideCursor();

   if( !kapp->isRestored() ) {
      TDECmdLineArgs &args = *TDECmdLineArgs::parsedArgs();
      if (args.isSet( "play-dvd" ))
         open( "dvd:/" );
      else if (args.count() > 0 ) {
         open( args.url( 0 ) );
         args.clear();
         adjustSize(); //will resize us to reflect the videoWindow's sizeHint()
      }
      else
         //show the welcome dialog
         playMedia( true ); // true = show in style of welcome dialog
   }
   else
      //session management must be done after the videoWindow() has been initialised
      restore( 1, false );

   //don't do until videoWindow() is initialised!
   startTimer( 50 );
}

MainWindow::~MainWindow()
{
   DEBUG_FUNC_INFO

   hide(); //so we appear to have quit, and then sound fades out below

   delete videoWindow(); //fades out sound in dtor
}

bool
MainWindow::queryExit()
{
   if( toggleAction( "fullscreen" )->isChecked() ) {
      // there seems to be no other way to stop TDEMainWindow
      // saving the window state without any controls
      fullScreenToggled( false );
      showNormal();
      TQApplication::sendPostedEvents( this, 0 );
      // otherwise TDEMainWindow saves the screensize as maximised
      Codeine::MessageBox::sorry(
            "This annoying messagebox is to get round a bug in either KDE or TQt. "
            "Just press OK and Codeine will quit." );
      //NOTE not actually needed
      saveAutoSaveSettings();
      hide();
   }

   return true;
}

void
MainWindow::setupActions()
{
   DEBUG_BLOCK

   TDEActionCollection * const ac = actionCollection();

   KStdAction::quit( kapp, SLOT(quit()), ac );
   KStdAction::open( this, SLOT(playMedia()), ac, "play_media" )->setText( i18n("Play &Media...") );
   connect( new FullScreenAction( this, ac ), SIGNAL(toggled( bool )), SLOT(fullScreenToggled( bool )) );

   new PlayAction( this, SLOT(play()), ac );
   new TDEAction( i18n("Stop"), "player_stop", Key_S, engine(), SLOT(stop()), ac, "stop" );

   new TDEToggleAction( i18n("Record"), "player_record", CTRL + Key_R, engine(), SLOT(record()), ac, "record" );

   new TDEAction( i18n("Reset Video Scale"), "viewmag1", Key_Equal, videoWindow(), SLOT(resetZoom()), ac, "reset_zoom" );
   new TDEAction( i18n("Media Information"), "messagebox_info", Key_I, this, SLOT(streamInformation()), ac, "information" );
   new TDEAction( i18n("Menu Toggle"), "dvd_unmount", Key_R, engine(), SLOT(toggleDVDMenu()), ac, "toggle_dvd_menu" );
   new TDEAction( i18n("&Capture Frame"), "frame_image", Key_C, this, SLOT(captureFrame()), ac, "capture_frame" );

   new TDEAction( i18n("Video Settings..."), "configure", Key_V, this, SLOT(configure()), ac, "video_settings" );
   new TDEAction( i18n("Configure xine..."), "configure", 0, this, SLOT(configure()), ac, "xine_settings" );

   (new KWidgetAction( m_positionSlider, i18n("Position Slider"), 0, 0, 0, ac, "position_slider" ))->setAutoSized( true );

   new VolumeAction( toolBar(), ac );
}

void
MainWindow::saveProperties( TDEConfig *config )
{
   config->writeEntry( "url", TheStream::url().url() );
   config->writeEntry( "time", engine()->time() );
}

void
MainWindow::readProperties( TDEConfig *config )
{
   if( engine()->load( config->readPathEntry( "url" ) ) )
      engine()->play( config->readNumEntry( "time" ) );
}

void
MainWindow::timerEvent( TQTimerEvent* )
{
   static int counter = 0;

   if( engine()->state() == Engine::Playing ) {
      ++counter &= 1023;

      m_positionSlider->setValue( engine()->position() );
      if( !m_positionSlider->isEnabled() && counter % 10 == 0 ) // 0.5 seconds
         // usually the slider emits a signal that updates the timeLabel
         // but not if the slider isn't moving because there is no length
         showTime();

      #ifndef NO_XTEST_EXTENSION
      if( counter == 0 /*1020*/ ) { // 51 seconds //do at 0 to ensure screensaver doesn't happen before 51 seconds is up (somehow)
         const bool isOnThisDesktop = KWin::windowInfo( winId() ).isOnDesktop( KWin::currentDesktop() );

         if( videoWindow()->isVisible() && isOnThisDesktop ) {
            int key = XKeysymToKeycode( x11Display(), XK_Shift_R );

            XTestFakeKeyEvent( x11Display(), key, true, CurrentTime );
            XTestFakeKeyEvent( x11Display(), key, false, CurrentTime );
            XSync( x11Display(), false );
         }
      }
      #endif
   }
}

void
MainWindow::showTime( int pos )
{
   #define zeroPad( n ) n < 10 ? TQString("0%1").arg( n ) : TQString::number( n )

   const int ms = (pos == -1) ? engine()->time() : int(engine()->length() * (pos / 65535.0));
   const int s  = ms / 1000;
   const int m  =  s / 60;
   const int h  =  m / 60;

   TQString time = zeroPad( s % 60 ); //seconds
   time.prepend( ':' );
   time.prepend( zeroPad( m % 60 ) ); //minutes
   time.prepend( ':' );
   time.prepend( TQString::number( h ) ); //hours

   m_timeLabel->setText( time );
}

void
MainWindow::engineMessage( const TQString &message )
{
   statusBar()->message( message, 3500 );
}

bool
MainWindow::open( const KURL &url )
{
   DEBUG_BLOCK
   debug() << url << endl;

   if( load( url ) ) {
      const int offset = TheStream::hasProfile()
            // adjust offset if we have session history for this video
            ? TheStream::profile()->readNumEntry( "Position", 0 )
            : 0;

      return engine()->play( offset );
   }

   return false;
}

bool
MainWindow::load( const KURL &url )
{
    //FileWatch the file that is opened

   if( url.isEmpty() ) {
      MessageBox::sorry( i18n( "Codeine was asked to open an empty URL; it cannot." ) );
      return false;
   }

   PlaylistFile playlist( url );
   if( playlist.isPlaylist() ) {
      //TODO: problem is we return out of the function
      //statusBar()->message( i18n("Parsing playlist file...") );

      if( playlist.isValid() )
         return engine()->load( playlist.firstUrl() );
      else {
         MessageBox::sorry( playlist.error() );
         return false;
      }
   }

   if (url.protocol() == "media") {
      #define UDS_LOCAL_PATH (72 | TDEIO::UDS_STRING)
      TDEIO::UDSEntry e;
      if (!TDEIO::NetAccess::stat( url, e, 0 ))
         MessageBox::sorry( "There was an internal error with the media slave..." );
      else {
         TDEIO::UDSEntry::ConstIterator end = e.end();
         for (TDEIO::UDSEntry::ConstIterator it = e.begin(); it != end; ++it)
            if ((*it).m_uds == UDS_LOCAL_PATH && !(*it).m_str.isEmpty())
               return engine()->load( KURL::fromPathOrURL( (*it).m_str ) );
      }
   }

   //let xine handle invalid, etc, KURLS
   //TODO it handles non-existant files with bad error message
   return engine()->load( url );
}

void
MainWindow::play()
{
   switch( engine()->state() ) {
   case Engine::Loaded:
      engine()->play();
      break;

   case Engine::Playing:
   case Engine::Paused:
      engine()->pause();
      break;

   case Engine::Empty:
   default:
      playMedia();
      break;
   }
}

void
MainWindow::playMedia( bool show_welcome_dialog )
{
   PlayDialog dialog( this, show_welcome_dialog );

   switch( dialog.exec() ) {
   case PlayDialog::FILE: {
      const TQString filter = engine()->fileFilter() + '|' + i18n("Supported Media Formats") + "\n*|" + i18n("All Files");
      const KURL url = KFileDialog::getOpenURL( ":default", filter, this, i18n("Select A File To Play") );
      open( url );
      } break;
   case PlayDialog::RECENT_FILE:
      open( dialog.url() );
      break;
   case PlayDialog::CDDA:
      open( "cdda:/1" );
      break;
   case PlayDialog::VCD:
      open( "vcd://" ); // one / is not enough
      break;
   case PlayDialog::DVD:
      open( "dvd:/" );
      break;
   }
}

class FullScreenToolBarHandler : TQObject
{
   TDEToolBar *m_toolbar;
   int m_timer_id;
   bool m_stay_hidden_for_a_bit;
   TQPoint m_home;

public:
   FullScreenToolBarHandler( TDEMainWindow *parent )
         : TQObject( parent )
         , m_toolbar( parent->toolBar() )
         , m_timer_id( 0 )
         , m_stay_hidden_for_a_bit( false )
   {
      DEBUG_BLOCK

      parent->installEventFilter( this );
      m_toolbar->installEventFilter( this );
   }

   bool eventFilter( TQObject *o, TQEvent *e )
   {
      if (o == parent() && e->type() == TQEvent::MouseMove) {
         killTimer( m_timer_id );

         TQMouseEvent const * const me = (TQMouseEvent*)e;
         if (m_stay_hidden_for_a_bit) {
            // wait for a small pause before showing the toolbar again
            // usage = user removes mouse from toolbar after using it
            // toolbar disappears (usage is over) but usually we show
            // toolbar immediately when mouse is moved.. so we need this hack

            // HACK if user thrusts mouse to top, we assume they really want the toolbar
            // back. Is hack as 80% of users have at top, but 20% at bottom, we don't cater
            // for the 20% as lots more code, for now.
            if (me->pos().y() < m_toolbar->height())
               goto show_toolbar;

            m_timer_id = startTimer( 100 );
         }
         else {
            if (m_toolbar->isHidden()) {
               if (m_home.isNull())
                  m_home = me->pos();
               else if ((m_home - me->pos()).manhattanLength() > 6)
                  // then cursor has moved far enough to trigger show toolbar
show_toolbar:
                  m_toolbar->show(),
                  m_home = TQPoint();
               else
                  // cursor hasn't moved far enough yet
                  // don't reset timer below, return instead
                  return false;
            }

            // reset the hide timer
            m_timer_id = startTimer( VideoWindow::CURSOR_HIDE_TIMEOUT );
         }
      }

      if (o == parent() && e->type() == TQEvent::Resize)
      {
         //we aren't managed by mainWindow when at FullScreen
         videoWindow()->move( 0, 0 );
         videoWindow()->resize( ((TQWidget*)o)->size() );
         videoWindow()->lower();
      }

      if (o == m_toolbar)
         switch (e->type()) {
            case TQEvent::Enter:
               m_stay_hidden_for_a_bit = false;
               killTimer( m_timer_id );
            break;

            case TQEvent::Leave:
               m_toolbar->hide();
               m_stay_hidden_for_a_bit = true;
               killTimer( m_timer_id );
               m_timer_id = startTimer( 100 );
            break;

            default: break;
         }

      return false;
   }

   void timerEvent( TQTimerEvent* )
   {
      if (m_stay_hidden_for_a_bit)
         ;

      else if (!m_toolbar->hasMouse())
         m_toolbar->hide();

      m_stay_hidden_for_a_bit = false;
   }
};


void
MainWindow::fullScreenToggled( bool isFullScreen )
{
   static FullScreenToolBarHandler *s_handler;

   DEBUG_FUNC_INFO

   if( isFullScreen )
      toolBar()->setPalette( palette() ), // due to 2px spacing in TQMainWindow :(
      setPaletteBackgroundColor( TQt::black ); // due to 2px spacing
   else
      toolBar()->unsetPalette(),
      unsetPalette();

   toolBar()->setMovingEnabled( !isFullScreen );
   toolBar()->setHidden( isFullScreen && engine()->state() == Engine::Playing );

   reinterpret_cast<TQWidget*>(menuBar())->setHidden( isFullScreen );
   statusBar()->setHidden( isFullScreen );

   setMouseTracking( isFullScreen ); /// @see mouseMoveEvent()

   if (isFullScreen)
      s_handler = new FullScreenToolBarHandler( this );
   else
      delete s_handler;

   // prevent videoWindow() moving around when mouse moves
   setCentralWidget( isFullScreen ? 0 : videoWindow() );
}

void
MainWindow::configure()
{
   const TQCString sender = this->sender()->name();

   if( sender == "video_settings" )
      Codeine::showVideoSettingsDialog( this );

   else if( sender == "xine_settings" )
      Codeine::showXineConfigurationDialog( this, *engine() );
}

void
MainWindow::streamInformation()
{
   MessageBox::information( TheStream::information(), i18n("Media Information") );
}

void
MainWindow::setChannels( const TQStringList &channels )
{
   DEBUG_FUNC_INFO

   //TODO -1 = auto

   TQStringList::ConstIterator it = channels.begin();

   TQPopupMenu *menu = (TQPopupMenu*)child( (*it).latin1() );
   menu->clear();

   menu->insertItem( i18n("&Determine Automatically"), 1 );
   menu->insertSeparator();

   //the id is crucial, since the slot this menu is connected to requires
   //that information to set the correct channel
   //NOTE we subtract 2 in xineEngine because TQMenuData doesn't allow negative id
   int id = 2;
   ++it;
   for( TQStringList::ConstIterator const end = channels.end(); it != end; ++it, ++id )
      menu->insertItem( *it, id );

   menu->insertSeparator();
   menu->insertItem( i18n("&Off"), 0 );

   id = channels.first() == "subtitle_channels_menu" ? SubtitleChannelsMenuItemId : AudioChannelsMenuItemId;
   MainWindow::menu( "settings" )->setItemEnabled( id, channels.count() > 1 );
}

void
MainWindow::aboutToShowMenu()
{
   TQPopupMenu *menu = (TQPopupMenu*)sender();
   TQCString name( sender() ? sender()->name() : 0 );

   // uncheck all items first
   for( uint x = 0; x < menu->count(); ++x )
      menu->setItemChecked( menu->idAt( x ), false );

   int id;
   if( name == "subtitle_channels_menu" )
      id = TheStream::subtitleChannel() + 2;
   else if( name == "audio_channels_menu" )
      id = TheStream::audioChannel() + 2;
   else
      id = TheStream::aspectRatio();

   menu->setItemChecked( id, true );
}

void
MainWindow::dragEnterEvent( TQDragEnterEvent *e )
{
   e->accept( KURLDrag::canDecode( e ) );
}

void
MainWindow::dropEvent( TQDropEvent *e )
{
   KURL::List list;
   KURLDrag::decode( e, list );

   if( !list.isEmpty() )
      open( list.first() );
   else
      engineMessage( i18n("Sorry, no media was found in the drop") );
}

void
MainWindow::keyPressEvent( TQKeyEvent *e )
{
   #define seek( step ) { \
         const int new_pos = m_positionSlider->value() step; \
         engine()->seek( new_pos > 0 ? (uint)new_pos : 0 ); \
      }

   switch( e->key() )
   {
      case TQt::Key_Left:  seek( -500 ); break;
      case TQt::Key_Right: seek( +500 ); break;
      case Key_Escape:    KWin::clearState( winId(), NET::FullScreen );
      default: ;
   }

   #undef seek
}

TQPopupMenu*
MainWindow::menu( const char *name )
{
   // KXMLGUI is "really good".
   return static_cast<TQPopupMenu*>(factory()->container( name, this ));
}


/// Convenience class for other classes that need access to the actionCollection
TDEActionCollection*
actionCollection()
{
   return static_cast<MainWindow*>(kapp->mainWidget())->actionCollection();
}

/// Convenience class for other classes that need access to the actions
TDEAction*
action( const char *name )
{
   #define QT_FATAL_ASSERT

   MainWindow *mainWindow = 0;
   TDEActionCollection *actionCollection = 0;
   TDEAction *action = 0;

   if( mainWindow = (MainWindow*)kapp->mainWidget() )
      if( actionCollection = mainWindow->actionCollection() )
         action = actionCollection->action( name );

   Q_ASSERT( mainWindow );
   Q_ASSERT( actionCollection );
   Q_ASSERT( action );

   return action;
}

} //namespace Codeine