home *** CD-ROM | disk | FTP | other *** search
/ Geek 6 / Geek-006.iso / linux / video / xmovie-1.5.3.tar.gz / xmovie-1.5.3.tar / xmovie-1.5.3 / xmovie / mwindowgui.C < prev    next >
C/C++ Source or Header  |  2000-11-29  |  10KB  |  531 lines

  1. #include "asset.h"
  2. #include "colormodels.h"
  3. #include "errorbox.h"
  4. #include "file.h"
  5. #include "keys.h"
  6. #include "mainwindow.h"
  7. #include "mainmenu.h"
  8. #include "mwindowgui.h"
  9. #include "playbackengine.h"
  10. #include "theme.h"
  11. #include "units.h"
  12. #include "vframe.h"
  13. #include <string.h>
  14.  
  15. MWindowGUI::MWindowGUI(MainWindow *mwindow, int x, int y, int w, int h)
  16.  : BC_Window("XMovie", 
  17.      x, 
  18.     y, 
  19.     w, 
  20.     h, 
  21.     32, 
  22.     32, 
  23.     1, 
  24.     0, 
  25.     1)
  26. {
  27.     this->mwindow = mwindow;
  28.     bitmap = 0;
  29.     yuv_bitmap = 0;
  30. }
  31.  
  32. MWindowGUI::~MWindowGUI()
  33. {
  34.     if(bitmap) delete bitmap;
  35.     if(yuv_bitmap) delete yuv_bitmap;
  36. }
  37.  
  38. int MWindowGUI::create_objects()
  39. {
  40.     int w = get_w(), h = get_h();
  41.     add_tool(menu = new MainMenu(mwindow, get_w()));
  42.     menu->create_objects();
  43.  
  44.     mwindow->theme->update_positions(mwindow, this, w, h);
  45.     mwindow->theme->draw_mwindow_bg(mwindow, this);
  46.  
  47.     add_tool(canvas = new MainCanvas(mwindow, 
  48.         0, 
  49.         menu->get_h(), 
  50.         mwindow->canvas_w, 
  51.         mwindow->canvas_h));
  52.     canvas->draw_top_background(this, 0, 0, canvas->get_w(), canvas->get_h());
  53.     canvas->flash();
  54.     add_tool(time_title = new BC_Title(mwindow->theme->time_x, 
  55.         mwindow->theme->time_y, 
  56.         "", 
  57.         MEDIUM_7SEGMENT, 
  58.         BLACK));
  59.     add_tool(playbutton = new PlayButton(mwindow, 
  60.         mwindow->theme->play_x, 
  61.         mwindow->theme->play_y));
  62.     add_tool(backbutton = new FrameBackButton(mwindow, 
  63.         mwindow->theme->frameback_x, 
  64.         mwindow->theme->frameback_y));
  65.     add_tool(forwardbutton = new FrameForwardButton(mwindow, 
  66.         mwindow->theme->framefwd_x, 
  67.         mwindow->theme->framefwd_y));
  68.     add_tool(scrollbar = new MainScrollbar(mwindow, 
  69.         mwindow->theme->scroll_x, 
  70.         mwindow->theme->scroll_y, 
  71.         mwindow->theme->scroll_w));
  72.     return 0;
  73. }
  74.  
  75. int MWindowGUI::resize_scrollbar()
  76. {
  77.     scrollbar->update(
  78.         scrollbar->get_w(), 
  79.         mwindow->current_percentage * SCROLLBAR_LENGTH, 
  80.         0, 
  81.         SCROLLBAR_LENGTH);
  82.     return 0;
  83. }
  84.  
  85.  
  86. int MWindowGUI::resize_widgets()
  87. {
  88.     mwindow->theme->draw_mwindow_bg(mwindow, this);
  89.     canvas->reposition_window(mwindow->theme->canvas_x, 
  90.         mwindow->theme->canvas_y, 
  91.         mwindow->canvas_w, 
  92.         mwindow->canvas_h);
  93.     canvas->draw_top_background(this, 0, 0, canvas->get_w(), canvas->get_h());
  94.     time_title->reposition_window(mwindow->theme->time_x, mwindow->theme->time_y);
  95.     playbutton->reposition_window(mwindow->theme->play_x, mwindow->theme->play_y);
  96.     backbutton->reposition_window(mwindow->theme->frameback_x, mwindow->theme->frameback_y);
  97.     forwardbutton->reposition_window(mwindow->theme->framefwd_x, mwindow->theme->framefwd_y);
  98.     scrollbar->reposition_window(mwindow->theme->scroll_x, 
  99.         mwindow->theme->scroll_y, 
  100.         mwindow->theme->scroll_w, 
  101.         scrollbar->get_h());
  102.     resize_scrollbar();
  103.     return 0;
  104. }
  105.  
  106. int MWindowGUI::resize_canvas(int w, int h)
  107. {
  108.     mwindow->theme->update_positions(mwindow, this, w, h);
  109.     resize_window(w, h);
  110.     resize_widgets();
  111.     return 0;
  112. }
  113.  
  114. int MWindowGUI::resize_event(int w, int h)
  115. {
  116.     if(mwindow->asset && mwindow->asset->video_data && !yuv_bitmap)
  117.     {
  118.         unlock_window();
  119.         mwindow->stop_playback();
  120.         lock_window();
  121.     }
  122.  
  123. // fix dimensions for canvas size
  124.     mwindow->theme->update_positions(mwindow, this, w, h);
  125.     resize_window(w, h);
  126.  
  127.     resize_widgets();
  128.  
  129.     if(!canvas->video_is_on() && mwindow->video_file)
  130.     {
  131.         mwindow->flash_frame(mwindow->current_percentage);
  132.         canvas->flash();
  133.     }
  134.  
  135.     return 0;
  136. }
  137.  
  138. int MWindowGUI::start_playback()
  139. {
  140.     canvas->start_video();
  141.  
  142. // YUV strategy
  143.     if(mwindow->video_file->yuv_copy_possible())
  144.     {
  145.         int in_x, in_y, in_w, in_h;
  146.  
  147.         mwindow->get_yuv_cropping(in_x, in_y, in_w, in_h);
  148.         yuv_bitmap = canvas->new_bitmap(in_w, 
  149.                     in_h, 
  150.                     BC_YUV420P);
  151.         playback_colormodel = BC_YUV420P;
  152.     }
  153. // Other strategies
  154.     else
  155.     {
  156.         if(!bitmap)
  157.         {
  158.             bitmap = canvas->new_bitmap(canvas->get_w(), canvas->get_h());
  159.         }
  160.         playback_colormodel = get_color_model();
  161.     }
  162.  
  163.     return 0;
  164. }
  165.  
  166. int MWindowGUI::stop_playback()
  167. {
  168. // Delete YUV strategy
  169.     if(playback_colormodel == BC_YUV420P)
  170.     {
  171.         delete yuv_bitmap;
  172.         yuv_bitmap = 0;
  173.     }
  174.  
  175.     canvas->stop_video();
  176.     return 0;
  177. }
  178.  
  179.  
  180. int MWindowGUI::flash_frame(VFrame* frame)
  181. {
  182. // Flash YUV buffer
  183.     if(yuv_bitmap)
  184.     {
  185.         canvas->draw_bitmap(yuv_bitmap, 
  186.             1, 
  187.             0, 
  188.             0, 
  189.             canvas->get_w(), 
  190.             canvas->get_h());
  191.         return 0;
  192.     }
  193.  
  194. // Test for resize
  195.     if(bitmap && 
  196.         (bitmap->get_w() != canvas->get_w() ||
  197.         bitmap->get_h() != canvas->get_h()))
  198.     {
  199.         delete bitmap;
  200.         bitmap = 0;
  201.     }
  202.  
  203.     if(!bitmap)
  204.     {
  205.         bitmap = canvas->new_bitmap(canvas->get_w(), canvas->get_h());
  206.     }
  207.  
  208.     if(frame)
  209.     {
  210.         bitmap->read_frame(frame, 
  211.             0,
  212.             0,
  213.             canvas->get_w(), 
  214.             canvas->get_h(),
  215.             0);
  216.     }
  217.  
  218.     canvas->draw_bitmap(bitmap, 
  219.         0, 
  220.         0, 
  221.         0, 
  222.         canvas->get_w(), 
  223.         canvas->get_h());
  224.     return 0;
  225. }
  226.  
  227. int MWindowGUI::close_event()
  228. {
  229.     mwindow->exit_cleanly();
  230.     set_done(0);
  231.     return 0;
  232. }
  233.  
  234. int MWindowGUI::update_position()
  235. {
  236.     char string[1024];
  237.     scrollbar->update((long)(mwindow->current_percentage * SCROLLBAR_LENGTH));
  238.     Units::totext(string, (long)(mwindow->current_time * 1000), 
  239.         1000, 
  240.         TIME_HMS2, 
  241.         30, 
  242.         16);
  243.     time_title->update(string);
  244.     return 0;
  245. }
  246.  
  247. MainScrollbar::MainScrollbar(MainWindow *mwindow, int x, int y, int w)
  248.  : BC_PercentageSlider(x, 
  249.      y, 
  250.     0, 
  251.     w, 
  252.     w, 
  253.     0, 
  254.     SCROLLBAR_LENGTH, 
  255.     0, 
  256.     0)
  257. {
  258.     this->mwindow = mwindow;
  259.     set_tooltip("Seek to a position");
  260. }
  261.  
  262. MainScrollbar::~MainScrollbar()
  263. {
  264. }
  265.  
  266. int MainScrollbar::handle_event()
  267. {
  268.     int result = 0;
  269.     if(mwindow->asset)
  270.     {
  271.         if(get_keypress())
  272.         {
  273.             if(get_keypress() == LEFT)
  274.             {
  275.                 mwindow->gui->unlock_window();
  276.                 mwindow->stop_playback();
  277.                 mwindow->gui->lock_window();
  278.                 mwindow->frame_backward();
  279.                 result = 1;
  280.             }
  281.             else
  282.             if(get_keypress() == RIGHT)
  283.             {
  284.                 mwindow->gui->unlock_window();
  285.                 mwindow->stop_playback();
  286.                 mwindow->gui->lock_window();
  287.                 mwindow->frame_forward();
  288.                 result = 1;
  289.             }
  290.         }
  291.         else
  292.         {
  293.             mwindow->gui->unlock_window();
  294.             mwindow->stop_playback();
  295.             mwindow->gui->lock_window();
  296.             mwindow->current_percentage = (double)get_value() / get_length();
  297.             mwindow->flash_frame(mwindow->current_percentage);
  298.             mwindow->gui->canvas->flash();
  299.             result = 1;
  300.         }
  301.     }
  302.     return result;
  303. }
  304.  
  305. PlayButton::PlayButton(MainWindow *mwindow, int x, int y)
  306.  : BC_Button(x, y, mwindow->theme->play)
  307. {
  308.     set_tooltip("Play movie");
  309.     this->mwindow = mwindow;
  310.     mode = 0;
  311. }
  312.  
  313. PlayButton::~PlayButton()
  314. {
  315. }
  316.  
  317. int PlayButton::set_mode(int mode)
  318. {
  319.     this->mode = mode;
  320.     switch(mode)
  321.     {
  322.         case 0:
  323.             update_bitmaps(mwindow->theme->play);
  324.             set_tooltip("Play movie");
  325.             break;
  326.         
  327.         case 1:
  328.             update_bitmaps(mwindow->theme->pause);
  329.             set_tooltip("Pause movie");
  330.             break;
  331.     }
  332.     return 0;
  333. }
  334.  
  335. int PlayButton::handle_event()
  336. {
  337.     if(mode == 1)
  338.     {
  339.         mwindow->gui->unlock_window();
  340.         mwindow->stop_playback();
  341.         mwindow->gui->lock_window();
  342.         set_mode(0);
  343.     }
  344.     else
  345.     {
  346.         set_mode(1);
  347.         mwindow->start_playback();
  348.     }
  349.     return 1;
  350. }
  351.  
  352. int PlayButton::keypress_event()
  353. {
  354.     if(get_keypress() == ' ') 
  355.     { 
  356.         handle_event(); 
  357.         return 1; 
  358.     }
  359.     return 0;
  360. }
  361.  
  362.  
  363. FrameBackButton::FrameBackButton(MainWindow *mwindow, int x, int y)
  364.  : BC_Button(x, y, mwindow->theme->frame_bck)
  365. {
  366.     this->mwindow = mwindow;
  367.     set_tooltip("Backward a frame");
  368. }
  369.  
  370. FrameBackButton::~FrameBackButton()
  371. {
  372. }
  373. int FrameBackButton::handle_event()
  374. {
  375.     mwindow->gui->unlock_window();
  376.     mwindow->stop_playback();
  377.     mwindow->gui->lock_window();
  378.     mwindow->frame_backward();
  379.     return 1;
  380. }
  381.  
  382. int FrameBackButton::keypress_event()
  383. {
  384.     if(get_keypress() == LEFT) 
  385.     { 
  386.         handle_event(); 
  387.         return 1; 
  388.     }
  389.     return 0;
  390. }
  391.  
  392. FrameForwardButton::FrameForwardButton(MainWindow *mwindow, int x, int y)
  393.  : BC_Button(x, y, mwindow->theme->frame_fwd)
  394. {
  395.     this->mwindow = mwindow;
  396.     set_tooltip("Forward a frame");
  397. }
  398.  
  399. FrameForwardButton::~FrameForwardButton()
  400. {
  401. }
  402. int FrameForwardButton::handle_event()
  403. {
  404.     mwindow->gui->unlock_window();
  405.     mwindow->stop_playback();
  406.     mwindow->gui->lock_window();
  407.     mwindow->frame_forward();
  408.     return 1;
  409. }
  410.  
  411. int FrameForwardButton::keypress_event()
  412. {
  413.     if(get_keypress() == RIGHT) 
  414.     { 
  415.         handle_event(); 
  416.         return 1; 
  417.     }
  418.     return 0;
  419. }
  420.  
  421.  
  422.  
  423. ErrorThread::ErrorThread(MWindowGUI *gui) : Thread()
  424. {
  425.     this->gui = gui;
  426. }
  427.  
  428. ErrorThread::~ErrorThread()
  429. {
  430. }
  431.  
  432. int ErrorThread::show_error(char *text)
  433. {
  434.     strcpy(this->text, text);
  435.     this->x = gui->get_abs_cursor_x();
  436.     this->y = gui->get_abs_cursor_y();
  437.     Thread::synchronous = 0;
  438.     startup_lock.lock();
  439.     start();
  440.     startup_lock.lock();
  441.     startup_lock.unlock();
  442.     return 0;
  443. }
  444.  
  445. void ErrorThread::run()
  446. {
  447.     ErrorBox box("XMovie: Error", 
  448.         x < INFINITY ? x : gui->get_abs_cursor_x(), 
  449.         y < INFINITY ? y : gui->get_abs_cursor_y());
  450.     box.create_objects(text);
  451.     startup_lock.unlock();
  452.     box.run_window();
  453. }
  454.  
  455.  
  456.  
  457. MainCanvas::MainCanvas(MainWindow *mwindow, int x, int y, int w, int h)
  458.  : BC_SubWindow(x, y, w, h, -1)
  459. {
  460.     this->mwindow = mwindow;
  461. }
  462.  
  463. MainCanvas::~MainCanvas()
  464. {
  465. }
  466.  
  467.  
  468.  
  469. PlaybackScroll::PlaybackScroll(MainWindow *mwindow)
  470.  : Thread()
  471. {
  472.     this->mwindow = mwindow;
  473. }
  474.  
  475. PlaybackScroll::~PlaybackScroll()
  476. {
  477. }
  478.  
  479.  
  480. int PlaybackScroll::start_playback()
  481. {
  482.     synchronous = 1;
  483.     interrupt = 0;
  484.     startup_lock.lock();
  485.     start();
  486.     return 0;
  487. }
  488.  
  489. int PlaybackScroll::stop_playback()
  490. {
  491.     interrupt = 1;
  492.     return 0;
  493. }
  494.  
  495. int PlaybackScroll::wait_for_startup()
  496. {
  497.     startup_lock.lock();
  498.     startup_lock.unlock();
  499.     return 0;
  500. }
  501.  
  502. int PlaybackScroll::wait_for_completion()
  503. {
  504.     join();
  505.     return 0;
  506. }
  507.  
  508. void PlaybackScroll::run()
  509. {
  510.     double percentage, seconds;
  511.     Timer timer;
  512.  
  513.     startup_lock.unlock();
  514.     while(!interrupt)
  515.     {
  516.         mwindow->engine->current_position(percentage, seconds);
  517.  
  518.         if(percentage >= 0 && percentage > mwindow->current_percentage)
  519.         {
  520.             mwindow->current_percentage = percentage;
  521.             mwindow->current_time = seconds;
  522.             mwindow->gui->lock_window();
  523.             mwindow->gui->update_position();
  524.             mwindow->gui->unlock_window();
  525.         }
  526.         timer.delay(250);
  527.     }
  528. }
  529.  
  530.  
  531.