home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / IOC / MMSTEREO / MLTCD.CPP < prev    next >
Text File  |  1995-05-01  |  14KB  |  330 lines

  1. #include "mltcd.hpp"
  2.  
  3. /*------------------------------------------------------------------------------
  4. | CD::CD                                                                       |
  5. |                                                                              |
  6. |                                                                              |
  7. ------------------------------------------------------------------------------*/
  8. CD::CD( IMMAudioCD*       cd,
  9.         unsigned long     windowid,
  10.         IWindow*          parent,
  11.         IWindow*          owner)
  12.    : IMMPlayerPanel(windowid,parent,owner,IMMDevice::audioCD),
  13.      readout     (READOUTID, this,this),
  14.      discReadout (DISCREADOUTID, this,this),
  15.      name        (CDNAMEID, this, this),
  16.      pCDPlayer(cd),
  17.      observer(0),
  18.      handler(0)
  19. {
  20.    setPlayableDevice(pCDPlayer);
  21.  
  22.    eject = new IAnimatedButton(EJECTID,this,this,IRectangle(),
  23.                      ICustomButton::autoLatch |
  24.                      IWindow::visible | IAnimatedButton::animateWhenLatched);
  25.    trackF= new IAnimatedButton(TRACKFID,this,this,IRectangle(),
  26.                      IWindow::visible | IAnimatedButton::animateWhenLatched);
  27.    trackB= new IAnimatedButton(TRACKBID,this,this,IRectangle(),
  28.                      IWindow::visible | IAnimatedButton::animateWhenLatched);
  29.    scanF = new IAnimatedButton(SCANFID,this,this,IRectangle(),
  30.                      ICustomButton::autoLatch |
  31.                      ICustomButton::latchable |
  32.                      IWindow::visible | IAnimatedButton::animateWhenLatched);
  33.    scanB = new IAnimatedButton(SCANBID,this,this,IRectangle(),
  34.                      ICustomButton::autoLatch |
  35.                      ICustomButton::latchable |
  36.                      IWindow::visible | IAnimatedButton::animateWhenLatched);
  37.  
  38.    //Add the additional buttons to the player panel.
  39.    addToCell(trackB   ,     7, 1, 1, 1);
  40.    addToCell(trackF   ,     8, 1, 1, 1);
  41.    addToCell(scanB    ,    10, 1, 1, 1);
  42.    addToCell(scanF    ,    11, 1, 1, 1);
  43.    addToCell(&readout ,    13, 1, 1, 2);
  44.    addToCell(&discReadout ,13, 3, 1, 1);
  45.    addToCell(eject    ,    15, 1, 1, 1);
  46.    addToCell(&name    ,     2, 3, 5, 1);
  47.    setRowHeight(4,4);
  48.  
  49.    //Put the bitmaps on the buttons.
  50.    trackB->setBitmaps(IAnimatedButton::trackReverse);
  51.    trackF->setBitmaps(IAnimatedButton::trackAdvance);
  52.    scanB->setBitmaps(IAnimatedButton::scanBackward);
  53.    scanF->setBitmaps(IAnimatedButton::scanForward);
  54.    eject->setBitmaps(IAnimatedButton::eject);
  55.  
  56.    //Put text on the buttons. The \n cause the
  57.    //following text to be on a new line.
  58.    //trackB->setText("Track\nReverse");
  59.    //trackF->setText("Track\nAdvance");
  60.    //scanB->setText("Scan\nBackward");
  61.    //scanF->setText("Scan\nForward");
  62.    //eject->setText("Eject&Load");
  63.  
  64.    //Set up the title
  65.    name.setText("Class Library CD Player");
  66.    name.setForegroundColor(IColor(IColor::red));
  67.  
  68.    //Set up the display
  69.    IFont("LED",10).setWindowFont(&readout);
  70.    readout.setForegroundColor(IColor(0,160,0));
  71.    readout.setBackgroundColor(IColor(IColor::black));
  72.    readout.setText("TRACK 00 MIN:SEC 00:00");
  73.    readout.setLimit(24);
  74.  
  75.    //Set up the disc and track display
  76.    discReadout.setForegroundColor(IColor(0,160,0));
  77.    discReadout.setBackgroundColor(IColor(IColor::black));
  78.    discReadout.setLimit(24);
  79.    discReadout.setText("No CD Title: No Track Title");
  80.  
  81.    if (pCDPlayer)
  82.    {
  83.       handler  = new CDHandler();
  84.       handler->handleEventsFor(this);
  85.       observer = new CDObserver(*this);
  86.       observer->handleNotificationsFor(*pCDPlayer);
  87.       if (!pCDPlayer->isMediaPresent())
  88.       {
  89.          disable();
  90.          eject->latch();
  91.          discReadout.setText("Insert an Audio CD");
  92.       }
  93.    }
  94.    else
  95.      discReadout.setText("Could Not Open CD Device");
  96. }
  97.  
  98.  
  99. /*------------------------------------------------------------------------------
  100. | CD::~CD                                                                      |
  101. |                                                                              |
  102. |                                                                              |
  103. ------------------------------------------------------------------------------*/
  104. CD::~CD()
  105. {
  106.   if (observer)
  107.      observer->stopHandlingNotificationsFor(*pCDPlayer);
  108.   if (handler)
  109.      handler->stopHandlingEventsFor(this);
  110. }
  111.  
  112. /*------------------------------------------------------------------------------
  113. | CD::scanForwardButton                                                        |
  114. |                                                                              |
  115. |                                                                              |
  116. ------------------------------------------------------------------------------*/
  117. IAnimatedButton* CD::scanForwardButton () const
  118. {
  119.   return scanF;
  120. }
  121.  
  122. /*------------------------------------------------------------------------------
  123. | CD::scanBackwardButton                                                       |
  124. |                                                                              |
  125. |                                                                              |
  126. ------------------------------------------------------------------------------*/
  127. IAnimatedButton* CD::scanBackwardButton () const
  128. {
  129.   return scanB;
  130. }
  131.  
  132. /*------------------------------------------------------------------------------
  133. | CD::ejectButton                                                              |
  134. |                                                                              |
  135. |                                                                              |
  136. ------------------------------------------------------------------------------*/
  137. IAnimatedButton* CD::ejectButton () const
  138. {
  139.   return eject;
  140. }
  141.  
  142. /*------------------------------------------------------------------------------
  143. | CD::CDPlayer                                                                 |
  144. |                                                                              |
  145. |                                                                              |
  146. ------------------------------------------------------------------------------*/
  147. IMMAudioCD* CD::cdPlayer () const
  148. {
  149.   return pCDPlayer;
  150. }
  151.  
  152. /*------------------------------------------------------------------------------
  153. | CDHandler::CDHandler                                                         |
  154. |                                                                              |
  155. |                                                                              |
  156. ------------------------------------------------------------------------------*/
  157. CDHandler::CDHandler()
  158.           : CDHandler::Inherited()
  159. {}
  160.  
  161. /*------------------------------------------------------------------------------
  162. | CDHandler::command                                                           |
  163. |                                                                              |
  164. ------------------------------------------------------------------------------*/
  165. Boolean CDHandler::command(ICommandEvent& event)
  166. {
  167.   Boolean handled = false;
  168.   CD* panel = 0;
  169.  
  170.   switch (event.commandId())
  171.   {
  172.     case EJECTID:
  173.          panel = (CD*) (event.window());
  174.          if (panel->cdPlayer())
  175.             if (panel->cdPlayer()->isMediaPresent())
  176.                panel->cdPlayer()->openDoor();
  177.          handled=true;
  178.          break;
  179.     case TRACKBID:
  180.          panel = (CD*) (event.window());
  181.          panel->cdPlayer()->trackBackward();
  182.          handled=true;
  183.          break;
  184.     case TRACKFID:
  185.          panel = (CD*) (event.window());
  186.          panel->cdPlayer()->trackForward();
  187.          handled=true;
  188.          break;
  189.     case SCANFID:
  190.          panel = (CD*) (event.window());
  191.          if (panel->scanForwardButton()->isLatched())
  192.             panel->cdPlayer()->startScanningForward();
  193.          else
  194.             panel->cdPlayer()->stop();
  195.          handled=true;
  196.          break;
  197.     case SCANBID:
  198.          panel = (CD*) (event.window());
  199.          if (panel->scanBackwardButton()->isLatched())
  200.             panel->cdPlayer()->startScanningBackward();
  201.          else
  202.             panel->cdPlayer()->stop();
  203.          handled=true;
  204.          break;
  205.     default:
  206.          handled = Inherited::command(event);
  207.          break;
  208.   } /* endswitch */
  209.   return handled;
  210. }
  211.  
  212. /*------------------------------------------------------------------------------
  213. | CDHandler::rewind                                                            |
  214. |                                                                              |
  215. ------------------------------------------------------------------------------*/
  216. Boolean CDHandler::rewind(const IMMPlayerPanel& panel)
  217. {
  218.   IMMAudioCD* device = (IMMAudioCD*)(panel.playableDevice());
  219.   CD* cdpanel = (CD*)(&panel);
  220.   Boolean ret = Inherited::rewind(panel);
  221.   IMMAudioCDContents contents = device->contents();
  222.   IMMTrackMinSecFrameTime time(contents,device->position());
  223.   cdpanel->readout.setText(IString("TRACK ") +
  224.                   IString(time.track()).rightJustify(2,'0') +
  225.                   IString(" MIN:SEC ") +
  226.                   IString(time.minutes()).rightJustify(2,'0') +
  227.                   IString(":") +
  228.                   IString(time.seconds()).rightJustify(2,'0'));
  229.   return ret;
  230. }
  231.  
  232. /*------------------------------------------------------------------------------
  233. | CDHandler::fastForward                                                       |
  234. |                                                                              |
  235. ------------------------------------------------------------------------------*/
  236. Boolean CDHandler::fastForward(const IMMPlayerPanel& panel)
  237. {
  238.   IMMAudioCD* device = (IMMAudioCD*)(panel.playableDevice());
  239.   CD* cdpanel = (CD*)(&panel);
  240.   Boolean ret = Inherited::fastForward(panel);
  241.   IMMAudioCDContents contents = device->contents();
  242.   IMMTrackMinSecFrameTime time(contents,device->position());
  243.   cdpanel->readout.setText(IString("TRACK ") +
  244.                   IString(time.track()).rightJustify(2,'0') +
  245.                   IString(" MIN:SEC ") +
  246.                   IString(time.minutes()).rightJustify(2,'0') +
  247.                   IString(":") +
  248.                   IString(time.seconds()).rightJustify(2,'0'));
  249.   return ret;
  250. }
  251.  
  252. /*------------------------------------------------------------------------------
  253. | CDObserver::CDObserver                                                       |
  254. |                                                                              |
  255. |                                                                              |
  256. ------------------------------------------------------------------------------*/
  257. CDObserver::CDObserver(CD& cdPanel)
  258.           : CDObserver::Inherited(),
  259.           panel(cdPanel)
  260. {}
  261.  
  262. /*-------------------------------------------------------------------------------
  263. | CDObserver::dispatchNotificationEvent                                        |
  264. |                                                                              |
  265. |                                                                              |
  266. ------------------------------------------------------------------------------*/
  267. CDObserver& CDObserver::dispatchNotificationEvent(const INotificationEvent& event)
  268. {
  269.    if (event.notificationId() == IMMDevice::positionChangeId)
  270.    {
  271.       IMMPositionChangeEvent* positionEvent = (IMMPositionChangeEvent*)(event.eventData().asUnsignedLong());
  272.       IMMAudioCDContents temp = panel.cdPlayer()->contents();
  273.       IMMTrackMinSecFrameTime time(temp,positionEvent->position());
  274.       panel.readout.setText(IString("TRACK ") +
  275.                       IString(time.track()).rightJustify(2,'0') +
  276.                       IString(" MIN:SEC ") +
  277.                       IString(time.minutes()).rightJustify(2,'0') +
  278.                       IString(":") +
  279.                       IString(time.seconds()).rightJustify(2,'0'));
  280.       if (!panel.playButton()->isLatched())
  281.          panel.playButton()->latch();
  282.    } /* endif */
  283.    if (event.notificationId() == IMMAudioCD::positionTimerId)
  284.    {
  285.       IMMTrackMinSecFrameTime* time = (IMMTrackMinSecFrameTime*)(event.eventData().asUnsignedLong());
  286.       panel.readout.setText(IString("TRACK ") +
  287.                       IString(time->track()).rightJustify(2,'0') +
  288.                       IString(" MIN:SEC ") +
  289.                       IString(time->minutes()).rightJustify(2,'0') +
  290.                       IString(":") +
  291.                       IString(time->seconds()).rightJustify(2,'0'));
  292.       if (!panel.playButton()->isLatched())
  293.          panel.playButton()->latch();
  294.    } /* endif */
  295.    else if (event.notificationId() == IMMAudioCD::trackStartedId)
  296.    {
  297.       IMMTrackMinSecFrameTime* time = (IMMTrackMinSecFrameTime*)(event.eventData().asUnsignedLong());
  298.       panel.readout.setText(IString("TRACK ") +
  299.                       IString(time->track()).rightJustify(2,'0') +
  300.                       IString(" MIN:SEC ") +
  301.                       IString(time->minutes()).rightJustify(2,'0') +
  302.                       IString(":") +
  303.                       IString(time->seconds()).rightJustify(2,'0'));
  304.       panel.discReadout.setText(panel.cdPlayer()->discTitle() + ":" +
  305.                                 panel.cdPlayer()->trackTitle(time->track()));
  306.       if (!panel.playButton()->isLatched())
  307.          if (panel.cdPlayer()->mode()==IMMDevice::playing)
  308.             panel.playButton()->latch();
  309.    } /* endif */
  310.    else if (event.notificationId() == IMMDevice::commandNotifyId)
  311.    {
  312.       IMMNotifyEvent* notifyEvent = (IMMNotifyEvent*)(event.eventData().asUnsignedLong());
  313.       if (notifyEvent->command() == IMMNotifyEvent::play)
  314.          panel.playButton()->unlatch();
  315.    } /* endif */
  316.    else if (event.notificationId() == IMMRemovableMedia::mediaLoadedId)
  317.    {
  318.       if (event.eventData().asUnsignedLong())
  319.       {
  320.          panel.enable();
  321.          panel.ejectButton()->unlatch();
  322.       } else {
  323.          panel.disable();
  324.          panel.ejectButton()->latch();
  325.          panel.discReadout.setText("Insert an Audio CD");
  326.       }
  327.    } /* endif */
  328.    return *this;
  329. }
  330.