home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / mmstereo / mltcd.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  15.9 KB  |  351 lines

  1. /******************************************************************************
  2. * .FILE:         mltcd.cpp                                                    *
  3. *                                                                             *
  4. * .DESCRIPTION:  Multimedia Stereo Sample:             Class Implementation   *
  5. *                                                                             *
  6. * .CLASSES:      CD                                                           *
  7. *                CDHandler                                                    *
  8. *                CDObserver                                                   *
  9. *                                                                             *
  10. * .COPYRIGHT:                                                                 *
  11. *    Licensed Material - Program-Property of IBM                              *
  12. *    (C) Copyright IBM Corp. 1992, 1996 - All Rights Reserved                 *
  13. *                                                                             *
  14. * .DISCLAIMER:                                                                *
  15. *   The following [enclosed] code is sample code created by IBM               *
  16. *   Corporation.  This sample code is not part of any standard IBM product    *
  17. *   and is provided to you solely for the purpose of assisting you in the     *
  18. *   development of your applications.  The code is provided 'AS IS',          *
  19. *   without warranty of any kind.  IBM shall not be liable for any damages    *
  20. *   arising out of your use of the sample code, even if they have been        *
  21. *   advised of the possibility of such damages.                               *
  22. *                                                                             *
  23. * .NOTE: WE RECOMMEND USING A FIXED SPACE FONT TO LOOK AT THE SOURCE          *
  24. *                                                                             *
  25. ******************************************************************************/
  26. #include <ibase.hpp>
  27. #include <ireslib.hpp>
  28. #include "mmstereo.hpp"
  29. #include "mltcd.hpp"
  30.  
  31. /*------------------------------------------------------------------------------
  32. | CD::CD                                                                       |
  33. |                                                                              |
  34. |                                                                              |
  35. ------------------------------------------------------------------------------*/
  36. CD::CD( IMMAudioCD*       cd,
  37.         unsigned long     windowid,
  38.         IWindow*          parent,
  39.         IWindow*          owner)
  40.    : IMMPlayerPanel(windowid,parent,owner,IMMDevice::audioCD),
  41.      readout     (READOUTID, this,this),
  42.      discReadout (DISCREADOUTID, this,this),
  43.      name        (CDNAMEID, this, this),
  44.      pCDPlayer(cd),
  45.      observer(0),
  46.      handler(0)
  47. {
  48.    IResourceLibrary reslib;
  49.    setPlayableDevice(pCDPlayer);
  50.  
  51.    eject = new IAnimatedButton(EJECTID,this,this,IRectangle(),
  52.                      ICustomButton::autoLatch |
  53.                      IWindow::visible | IAnimatedButton::animateWhenLatched);
  54.    trackF= new IAnimatedButton(TRACKFID,this,this,IRectangle(),
  55.                      IWindow::visible | IAnimatedButton::animateWhenLatched);
  56.    trackB= new IAnimatedButton(TRACKBID,this,this,IRectangle(),
  57.                      IWindow::visible | IAnimatedButton::animateWhenLatched);
  58.    scanF = new IAnimatedButton(SCANFID,this,this,IRectangle(),
  59.                      ICustomButton::autoLatch |
  60.                      ICustomButton::latchable |
  61.                      IWindow::visible | IAnimatedButton::animateWhenLatched);
  62.    scanB = new IAnimatedButton(SCANBID,this,this,IRectangle(),
  63.                      ICustomButton::autoLatch |
  64.                      ICustomButton::latchable |
  65.                      IWindow::visible | IAnimatedButton::animateWhenLatched);
  66.  
  67.    //Add the additional buttons to the player panel.
  68.    addToCell(trackB   ,     7, 1, 1, 1);
  69.    addToCell(trackF   ,     8, 1, 1, 1);
  70.    addToCell(scanB    ,    10, 1, 1, 1);
  71.    addToCell(scanF    ,    11, 1, 1, 1);
  72.    addToCell(&readout ,    13, 1, 1, 2);
  73.    addToCell(&discReadout ,13, 3, 1, 1);
  74.    addToCell(eject    ,    15, 1, 1, 1);
  75.    addToCell(&name    ,     2, 3, 12, 1);
  76.    setRowHeight(4,4);
  77.  
  78.    //Put the bitmaps on the buttons.
  79.    trackB->setBitmaps(IAnimatedButton::trackReverse);
  80.    trackF->setBitmaps(IAnimatedButton::trackAdvance);
  81.    scanB->setBitmaps(IAnimatedButton::scanBackward);
  82.    scanF->setBitmaps(IAnimatedButton::scanForward);
  83.    eject->setBitmaps(IAnimatedButton::eject);
  84.  
  85.  
  86.    //Set up the title
  87.    name.setText(TITLE_CD_PLAYER);
  88.    name.setForegroundColor(IColor(IColor::red));
  89.  
  90.    //Set up the display
  91.    readout.setForegroundColor(IColor(0,160,0));
  92.    readout.setBackgroundColor(IColor(IColor::black));
  93.    readout.setText(reslib.loadString(STR_INIT_DISP));
  94.    readout.setLimit(24);
  95.  
  96.    //Set up the disc and track display
  97.    discReadout.setForegroundColor(IColor(0,160,0));
  98.    discReadout.setBackgroundColor(IColor(IColor::black));
  99.    discReadout.setLimit(24);
  100.    discReadout.setText(reslib.loadString(STR_NO_TITLE));
  101.  
  102.    if (pCDPlayer)
  103.    {
  104.       handler  = new CDHandler();
  105.       handler->handleEventsFor(this);
  106.       observer = new CDObserver(*this);
  107.       observer->handleNotificationsFor(*pCDPlayer);
  108.       if (!pCDPlayer->isMediaPresent())
  109.       {
  110.          disable();
  111.          eject->latch();
  112.          discReadout.setText(reslib.loadString(STR_INSERT));
  113.       }
  114.    }
  115.    else
  116.      discReadout.setText(reslib.loadString(STR_NO_DEVICE));
  117.  
  118. }
  119.  
  120.  
  121. /*------------------------------------------------------------------------------
  122. | CD::~CD                                                                      |
  123. |                                                                              |
  124. |                                                                              |
  125. ------------------------------------------------------------------------------*/
  126. CD::~CD()
  127. {
  128.   if (observer)
  129.      observer->stopHandlingNotificationsFor(*pCDPlayer);
  130.   if (handler)
  131.      handler->stopHandlingEventsFor(this);
  132. }
  133.  
  134. /*------------------------------------------------------------------------------
  135. | CD::scanForwardButton                                                        |
  136. |                                                                              |
  137. |                                                                              |
  138. ------------------------------------------------------------------------------*/
  139. IAnimatedButton* CD::scanForwardButton () const
  140. {
  141.   return scanF;
  142. }
  143.  
  144. /*------------------------------------------------------------------------------
  145. | CD::scanBackwardButton                                                       |
  146. |                                                                              |
  147. |                                                                              |
  148. ------------------------------------------------------------------------------*/
  149. IAnimatedButton* CD::scanBackwardButton () const
  150. {
  151.   return scanB;
  152. }
  153.  
  154. /*------------------------------------------------------------------------------
  155. | CD::ejectButton                                                              |
  156. |                                                                              |
  157. |                                                                              |
  158. ------------------------------------------------------------------------------*/
  159. IAnimatedButton* CD::ejectButton () const
  160. {
  161.   return eject;
  162. }
  163.  
  164. /*------------------------------------------------------------------------------
  165. | CD::CDPlayer                                                                 |
  166. |                                                                              |
  167. |                                                                              |
  168. ------------------------------------------------------------------------------*/
  169. IMMAudioCD* CD::cdPlayer () const
  170. {
  171.   return pCDPlayer;
  172. }
  173.  
  174. /*------------------------------------------------------------------------------
  175. | CDHandler::CDHandler                                                         |
  176. |                                                                              |
  177. |                                                                              |
  178. ------------------------------------------------------------------------------*/
  179. CDHandler::CDHandler()
  180.           : CDHandler::Inherited()
  181. {}
  182.  
  183. /*------------------------------------------------------------------------------
  184. | CDHandler::command                                                           |
  185. |                                                                              |
  186. ------------------------------------------------------------------------------*/
  187. IBase::Boolean CDHandler::command(ICommandEvent& event)
  188. {
  189.   Boolean handled = false;
  190.   CD* panel = 0;
  191.  
  192.   switch (event.commandId())
  193.   {
  194.     case EJECTID:
  195.          panel = (CD*) (event.window());
  196.          if (panel->cdPlayer())
  197.          {
  198.             if (panel->cdPlayer()->isMediaPresent())
  199.                panel->cdPlayer()->openDoor();
  200.          }
  201.          handled=true;
  202.          break;
  203.     case TRACKBID:
  204.          panel = (CD*) (event.window());
  205.          panel->cdPlayer()->trackBackward();
  206.          handled=true;
  207.          break;
  208.     case TRACKFID:
  209.          panel = (CD*) (event.window());
  210.          panel->cdPlayer()->trackForward();
  211.          handled=true;
  212.          break;
  213.     case SCANFID:
  214.          panel = (CD*) (event.window());
  215.          if (panel->scanForwardButton()->isLatched())
  216.             panel->cdPlayer()->startScanningForward();
  217.          handled=true;
  218.          break;
  219.     case SCANBID:
  220.          panel = (CD*) (event.window());
  221.          if (panel->scanBackwardButton()->isLatched())
  222.             panel->cdPlayer()->startScanningBackward();
  223.          handled=true;
  224.          break;
  225.     default:
  226.          handled = Inherited::command(event);
  227.          break;
  228.   } /* endswitch */
  229.   return handled;
  230. }
  231.  
  232. /*------------------------------------------------------------------------------
  233. | CDHandler::rewind                                                            |
  234. |                                                                              |
  235. ------------------------------------------------------------------------------*/
  236. IBase::Boolean CDHandler::rewind(const IMMPlayerPanel& panel)
  237. {
  238.   IResourceLibrary reslib;
  239.   IMMAudioCD* device = (IMMAudioCD*)(panel.playableDevice());
  240.   CD* cdpanel = (CD*)(&panel);
  241.   Boolean ret = Inherited::rewind(panel);
  242.   IMMAudioCDContents contents = device->contents();
  243.   IMMTrackMinSecFrameTime time(contents,device->position());
  244.   cdpanel->readout.setText(IString(reslib.loadString(STR_TRACK)) +
  245.                   IString(time.track()).rightJustify(2,'0') +
  246.                   IString(reslib.loadString(STR_PAD)) +
  247.                   IString(time.minutes()).rightJustify(2,'0') +
  248.                   IString(reslib.loadString(STR_COLON)) +
  249.                   IString(time.seconds()).rightJustify(2,'0'));
  250.   return ret;
  251. }
  252.  
  253. /*------------------------------------------------------------------------------
  254. | CDHandler::fastForward                                                       |
  255. |                                                                              |
  256. ------------------------------------------------------------------------------*/
  257. IBase::Boolean CDHandler::fastForward(const IMMPlayerPanel& panel)
  258. {
  259.   IResourceLibrary reslib;
  260.   IMMAudioCD* device = (IMMAudioCD*)(panel.playableDevice());
  261.   CD* cdpanel = (CD*)(&panel);
  262.   Boolean ret = Inherited::fastForward(panel);
  263.   IMMAudioCDContents contents = device->contents();
  264.   IMMTrackMinSecFrameTime time(contents,device->position());
  265.   cdpanel->readout.setText(IString(reslib.loadString(STR_TRACK)) +
  266.                   IString(time.track()).rightJustify(2,'0') +
  267.                   IString(reslib.loadString(STR_PAD)) +
  268.                   IString(time.minutes()).rightJustify(2,'0') +
  269.                   IString(reslib.loadString(STR_COLON)) +
  270.                   IString(time.seconds()).rightJustify(2,'0'));
  271.   return ret;
  272. }
  273.  
  274. /*------------------------------------------------------------------------------
  275. | CDObserver::CDObserver                                                       |
  276. |                                                                              |
  277. |                                                                              |
  278. ------------------------------------------------------------------------------*/
  279. CDObserver::CDObserver(CD& cdPanel)
  280.           : CDObserver::Inherited(),
  281.           panel(cdPanel)
  282. {}
  283.  
  284. /*-------------------------------------------------------------------------------
  285. | CDObserver::dispatchNotificationEvent                                        |
  286. |                                                                              |
  287. |                                                                              |
  288. ------------------------------------------------------------------------------*/
  289. CDObserver& CDObserver::dispatchNotificationEvent(const INotificationEvent& event)
  290. {
  291.    IResourceLibrary reslib;
  292.    if (event.notificationId() == IMMAudioCD::positionTimerId)
  293.    {
  294.       IMMTrackMinSecFrameTime* time = (IMMTrackMinSecFrameTime*)(event.eventData().asUnsignedLong());
  295.       panel.readout.setText(IString(reslib.loadString(STR_TRACK)) +
  296.                       IString(time->track()).rightJustify(2,'0') +
  297.                       IString(reslib.loadString(STR_PAD)) +
  298.                       IString(time->hours()).rightJustify(2,'0') +
  299.                       IString(reslib.loadString(STR_COLON)) +
  300.                       IString(time->minutes()).rightJustify(2,'0') +
  301.                       IString(reslib.loadString(STR_COLON)) +
  302.                       IString(time->seconds()).rightJustify(2,'0'));
  303.       if (!panel.playButton()->isLatched())
  304.          panel.playButton()->latch();
  305.    } /* endif */
  306.    else if (event.notificationId() == IMMAudioCD::trackStartedId)
  307.    {
  308.       IMMTrackMinSecFrameTime* time = (IMMTrackMinSecFrameTime*)(event.eventData().asUnsignedLong());
  309.       panel.readout.setText(IString(reslib.loadString(STR_TRACK)) +
  310.                       IString(time->track()).rightJustify(2,'0') +
  311.                       IString(reslib.loadString(STR_PAD)) +
  312.                       IString(time->hours()).rightJustify(2,'0') +
  313.                       IString(reslib.loadString(STR_COLON)) +
  314.                       IString(time->minutes()).rightJustify(2,'0') +
  315.                       IString(reslib.loadString(STR_COLON)) +
  316.                       IString(time->seconds()).rightJustify(2,'0'));
  317.       IString discName( panel.cdPlayer()->discTitle() );
  318.       IString trackName( panel.cdPlayer()->trackTitle(time->track()) );
  319.       if ( discName == "" )
  320.         discName = reslib.loadString( STR_NO_DISC_TITLE );
  321.       if ( trackName == "" )
  322.         trackName = reslib.loadString( STR_NO_TRACK_TITLE );
  323.       panel.discReadout.setText(discName + reslib.loadString(STR_COLON) +
  324.                                 trackName);
  325.       if (!panel.playButton()->isLatched())
  326.          if (panel.cdPlayer()->mode()==IMMDevice::playing)
  327.          {
  328.             panel.playButton()->latch();
  329.          }
  330.    } /* endif */
  331.    else if (event.notificationId() == IMMDevice::commandNotifyId)
  332.    {
  333.       IMMNotifyEvent* notifyEvent = (IMMNotifyEvent*)(event.eventData().asUnsignedLong());
  334.       if (notifyEvent->command() == IMMNotifyEvent::play)
  335.          panel.playButton()->unlatch();
  336.    } /* endif */
  337.    else if (event.notificationId() == IMMRemovableMedia::mediaLoadedId)
  338.    {
  339.       if (event.eventData().asUnsignedLong())
  340.       {
  341.          panel.enable();
  342.          panel.ejectButton()->unlatch();
  343.       } else {
  344.          panel.disable();
  345.          panel.ejectButton()->latch();
  346.          panel.discReadout.setText(IString(reslib.loadString(STR_INSERT)));
  347.       }
  348.    } /* endif */
  349.    return *this;
  350. }
  351.