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

  1. /******************************************************************************
  2. * .FILE:         mltwave.cpp                                                  *
  3. *                                                                             *
  4. * .DESCRIPTION:  Multimedia Stereo Sample:             Class Implementation   *
  5. *                                                                             *
  6. * .CLASSES:      WAVE                                                         *
  7. *                WaveHandler                                                  *
  8. *                WaveObserver                                                 *
  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 "mltwave.hpp"
  30.  
  31. /*------------------------------------------------------------------------------
  32. | WAVE::WAVE                                                                   |
  33. |                                                                              |
  34. |                                                                              |
  35. ------------------------------------------------------------------------------*/
  36. WAVE::WAVE( IMMWaveAudio*     aWavePlayer,
  37.             unsigned long     windowid,
  38.             IWindow*          parent,
  39.             IWindow*          owner)
  40.    : IMMPlayerPanel(windowid,parent,owner,IMMDevice::waveAudio),
  41.      readout     (WAVEREADOUTID, this,this),
  42.      name        (WAVENAMEID, this, this),
  43.      loadit      (LOADID,this,this,IRectangle(),
  44.                   IWindow::visible | IAnimatedButton::animateWhenLatched),
  45.      saveButton  (SAVEID,this,this),
  46.      lineIn      (WAVELINEINID,this,this),
  47.      microphone  (WAVEMICROPHONEID,this,this),
  48.      pWavePlayer(aWavePlayer),
  49.      observer(0),
  50.      handler(0)
  51. {
  52.  
  53.    IResourceLibrary reslib;
  54.    setPlayableDevice(pWavePlayer);
  55.  
  56.    rec = new IAnimatedButton(RECID,this,this,IRectangle(),
  57.                              ICustomButton::autoLatch |
  58.                              ICustomButton::latchable |
  59.                              IWindow::visible | IAnimatedButton::animateWhenLatched);
  60.    saveButton.setText(reslib.loadString(STR_SAVE));
  61.    lineIn.enableAutoSelect();
  62.    lineIn.setText(STR_LINE_IN);
  63.    microphone.enableAutoSelect();
  64.    microphone.setText(STR_MIC);
  65.    microphone.select();
  66.  
  67.    //Add the additional buttons to the player panel.
  68.    addToCell(rec      ,     7, 1, 1, 2);
  69.    addToCell(&lineIn   ,    8, 1, 1, 1);
  70.    addToCell(µphone,   8, 2, 1, 1);
  71.    addToCell(&saveButton,   9, 1, 1, 1);
  72.    addToCell(&readout ,    11, 1, 1, 2);
  73.    addToCell(&loadit  ,    13, 1, 1, 2);
  74.    addToCell(&name    ,     2, 3, 11, 1);
  75.    rec->disable();
  76.    playButton()->disable();
  77.    saveButton.disable();
  78.    rewindButton()->disable();
  79.    fastForwardButton()->disable();
  80.  
  81.    //Put the bitmaps on the buttons.
  82.    rec->setBitmaps(IAnimatedButton::record);
  83.    loadit.setBitmaps(IAnimatedButton::eject);
  84.  
  85.    //Put text on the buttons.
  86.    rec->setText(STR_RECORD);
  87.    loadit.setText(STR_LOAD);
  88.  
  89.    //Set up the title
  90.    name.setText(TITLE_WAV_PLAYER);
  91.    name.setForegroundColor(IColor(IColor::red));
  92.  
  93.    //Set up the display
  94.    readout.setForegroundColor(IColor(0,160,0));
  95.    readout.setBackgroundColor(IColor(IColor::black));
  96.    readout.setText(reslib.loadString(STR_LTIME_DEF));
  97.    readout.setLimit(22);
  98.  
  99.    if (pWavePlayer)
  100.    {
  101.       handler  = new WaveHandler();
  102.       handler->handleEventsFor(this);
  103.       observer = new WaveObserver(*this);
  104.       observer->handleNotificationsFor(*pWavePlayer);
  105.    }
  106. }
  107.  
  108.  
  109. /*------------------------------------------------------------------------------
  110. | WAVE::~WAVE                                                                      |
  111. |                                                                              |
  112. |                                                                              |
  113. ------------------------------------------------------------------------------*/
  114. WAVE::~WAVE()
  115. {
  116.   if (observer)
  117.      observer->stopHandlingNotificationsFor(*pWavePlayer);
  118.   if (handler)
  119.      handler->stopHandlingEventsFor(this);
  120. }
  121.  
  122. /*------------------------------------------------------------------------------
  123. | WAVE::recordButton                                                           |
  124. |                                                                              |
  125. |                                                                              |
  126. ------------------------------------------------------------------------------*/
  127. IAnimatedButton* WAVE::recordButton () const
  128. {
  129.   return rec;
  130. }
  131.  
  132. /*------------------------------------------------------------------------------
  133. | WAVE::wavePlayer                                                             |
  134. |                                                                              |
  135. |                                                                              |
  136. ------------------------------------------------------------------------------*/
  137. IMMWaveAudio* WAVE::wavePlayer () const
  138. {
  139.   return pWavePlayer;
  140. }
  141.  
  142.  
  143. /*------------------------------------------------------------------------------
  144. | WaveHandler::WaveHandler                                                     |
  145. |                                                                              |
  146. |                                                                              |
  147. ------------------------------------------------------------------------------*/
  148. WaveHandler::WaveHandler()
  149.           : WaveHandler::Inherited()
  150. {}
  151.  
  152. /*------------------------------------------------------------------------------
  153. | WaveHandler::command                                                         |
  154. |                                                                              |
  155. ------------------------------------------------------------------------------*/
  156. IBase::Boolean WaveHandler::command(ICommandEvent& event)
  157. {
  158.   Boolean handled = false;
  159.   WAVE* panel = 0;
  160.   IResourceLibrary reslib;
  161.   switch (event.commandId())
  162.   {
  163.     case LOADID:
  164.          {
  165.          panel = (WAVE*) (event.window());
  166.          IFileDialog::Settings fdSettings;
  167.          fdSettings.setTitle(STR_LOAD_NEW);
  168.          fdSettings.setFileName(reslib.loadString(STR_WAV_EXT));
  169.          IFileDialog fd(panel->desktopWindow(),panel,fdSettings);
  170.          if (fd.pressedOK())
  171.          {
  172.             panel->wavePlayer()->loadOnThread(fd.fileName());
  173.             stop(*panel);
  174.             panel->wavePlayer()->startPositionTracking(IMMTime(3000));
  175.             panel->recordButton()->enable();
  176.             panel->playButton()->enable();
  177.             panel->rewindButton()->enable();
  178.             panel->fastForwardButton()->enable();
  179.             panel->saveButton.enable();
  180.          }
  181.          }
  182.          handled=true;
  183.          break;
  184.     case SAVEID:
  185.          panel = (WAVE*) (event.window());
  186.          stop(*panel);
  187.          panel->wavePlayer()->save();
  188.          handled=true;
  189.          break;
  190.     case RECID:
  191.          panel = (WAVE*) (event.window());
  192.          panel->stopButton()->enable();
  193.          panel->pauseButton()->disable();
  194.          panel->playButton()->disable();
  195.          panel->rewindButton()->disable();
  196.          panel->fastForwardButton()->disable();
  197.          if (panel->lineIn.isSelected())
  198.             panel->wavePlayer()->enableConnector(IMMDevice::lineIn);
  199.          else
  200.             panel->wavePlayer()->enableConnector(IMMDevice::microphones);
  201.          panel->wavePlayer()->acquire(IMMDevice::isolatedExclusive);
  202.          panel->wavePlayer()->record();
  203.          handled = true;
  204.          break;
  205.     default:
  206.          handled = Inherited::command(event);
  207.          break;
  208.   } /* endswitch */
  209.   return handled;
  210. }
  211.  
  212. /*------------------------------------------------------------------------------
  213. | WaveHandler::stop                                                            |
  214. |                                                                              |
  215. ------------------------------------------------------------------------------*/
  216. IBase::Boolean WaveHandler::stop(const IMMPlayerPanel& panel)
  217. {
  218.   panel.playButton()->enable();
  219.   panel.rewindButton()->enable();
  220.   panel.fastForwardButton()->enable();
  221.   Boolean retval = Inherited::stop(panel);
  222.   panel.playableDevice()->release();
  223.   return retval;
  224. }
  225.  
  226. /*------------------------------------------------------------------------------
  227. | WaveObserver::WaveObserver                                                   |
  228. |                                                                              |
  229. |                                                                              |
  230. ------------------------------------------------------------------------------*/
  231. WaveObserver::WaveObserver(WAVE& wavePanel)
  232.           : WaveObserver::Inherited(),
  233.           panel(wavePanel)
  234. {}
  235.  
  236. /*------------------------------------------------------------------------------
  237. | WaveObserver::dispatchNotificationEvent                                      |
  238. |                                                                              |
  239. |                                                                              |
  240. ------------------------------------------------------------------------------*/
  241. WaveObserver& WaveObserver::dispatchNotificationEvent(const INotificationEvent& event)
  242. {
  243.    IResourceLibrary reslib;
  244.    if (event.notificationId() == IMMDevice::positionChangeId)
  245.    {
  246.       IMMPositionChangeEvent* positionEvent = (IMMPositionChangeEvent*)(event.eventData().asUnsignedLong());
  247.  
  248.       IMMTime time(positionEvent->position());
  249.       panel.readout.setText(
  250.                       IString(time.hours()).rightJustify(2,'0') +
  251.                       reslib.loadString(STR_COLON) +
  252.                       IString(time.minutes()).rightJustify(2,'0') +
  253.                       reslib.loadString(STR_COLON) +
  254.                       IString(time.seconds()).rightJustify(2,'0'));
  255.       if (!panel.playButton()->isLatched())
  256.          if (panel.wavePlayer()->mode()==IMMDevice::playing)
  257.             panel.playButton()->latch();
  258.    } /* endif */
  259.    if (event.notificationId() == IMMDevice::commandNotifyId)
  260.    {
  261.       IMMNotifyEvent* notifyEvent = (IMMNotifyEvent*)(event.eventData().asUnsignedLong());
  262.       if (notifyEvent->command() == IMMNotifyEvent::play)
  263.          panel.playButton()->unlatch();
  264.       if (notifyEvent->command() == IMMNotifyEvent::record)
  265.          panel.recordButton()->unlatch();
  266.    } /* endif */
  267.    return *this;
  268. }
  269.