home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / mac / SiteBldr / AMOVIE / SDK / _SETUP / COMMON.Z / gargprop.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-19  |  8.7 KB  |  343 lines

  1. //==========================================================================;
  2. //
  3. //  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. //  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. //  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. //  PURPOSE.
  7. //
  8. //  Copyright (c) 1992 - 1996  Microsoft Corporation.  All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------;
  11. //
  12. // GargProp.cpp
  13. //
  14.  
  15. #include <streams.h>
  16. #include <commctrl.h>
  17. #include <olectl.h>
  18. #include <memory.h>
  19. #include <math.h>
  20.  
  21. #include "resource.h"
  22. #include "garguids.h"
  23. #include "igargle.h"
  24. #include "gargle.h"
  25. #include "gargprop.h"
  26.  
  27. // *
  28. // * CGargleProperties
  29. // *
  30.  
  31.  
  32. //
  33. // CreateInstance
  34. //
  35. //
  36. CUnknown *CGargleProperties::CreateInstance(LPUNKNOWN lpunk, HRESULT *phr)
  37. {
  38.  
  39.     CUnknown *punk = new CGargleProperties(lpunk, phr);
  40.     if (punk == NULL) {
  41.         *phr = E_OUTOFMEMORY;
  42.     }
  43.  
  44.     return punk;
  45. } // Createinstance
  46.  
  47.  
  48. //
  49. // CGargleProperties::Constructor
  50. //
  51. // initialise a CGargleProperties object.
  52. CGargleProperties::CGargleProperties(LPUNKNOWN lpunk, HRESULT *phr)
  53.     : CBasePropertyPage( NAME("Gargle Property Page")
  54.                        , lpunk, phr, IDD_GARGPROP, IDS_NAME)
  55.     , m_pGargle(NULL)
  56. {
  57.  
  58. } // (constructor)
  59.  
  60.  
  61. void SetButtonText(HWND hwnd, int iShape)
  62. {
  63.     // Set button
  64.     if (iShape==0) {
  65.         SetDlgItemText(hwnd, IDB_SQUARE_TRIANGLE, "Triangle wave->square");
  66.     } else {
  67.         SetDlgItemText(hwnd, IDB_SQUARE_TRIANGLE, "Square wave->triangle");
  68.     }
  69.  
  70. }
  71.  
  72.  
  73. // Convert a frequency f (the gargle rate) to a slider position
  74. // f runs from 1 to 1000.
  75. // p runs from 0 to 300.
  76. // f = 10**(p/100)
  77. // p = 100*log10(f)
  78. int ConvertToPosition(int f)
  79. {
  80.     // protect against stupidity - should not occur
  81.     if (f<1) f = 1;
  82.     if (f>1000) f = 1000;
  83.  
  84.     double x = f;
  85.     x = 100.0*log10(x);
  86.  
  87.     // protect against rounding at the ends
  88.     int p = (int)x;
  89.     if (p<0) p = 0;
  90.     if (p>300) p = 300;
  91.  
  92.     return p;
  93. }
  94.  
  95.  
  96. // Convert a slider position p to a frequency (gargle rate)
  97. // f runs from 1 to 1000.
  98. // p runs from 0 to 300.
  99. // f = 10**(p/100)
  100. // p = 100*log10(f)
  101. int ConvertToFrequency(int p)
  102. {
  103.     // protect against stupidity - should not occur
  104.     if (p<0) p = 0;
  105.     if (p>300) p = 300;
  106.  
  107.     double x = p;
  108.     x = pow(10, x/100.0);
  109.  
  110.     // protect against rounding at the ends
  111.     int f = (int)x;
  112.     if (f<1) f = 1;
  113.     if (f>1000) f = 1000;
  114.  
  115.     return f;
  116. }
  117.  
  118.  
  119.  
  120. BOOL CGargleProperties::OnReceiveMessage
  121.                             (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  122. {
  123.     UNREFERENCED_PARAMETER(lParam);
  124.     switch (uMsg) {
  125.         case WM_INITDIALOG:
  126.  
  127.             m_hwndSlider = CreateSlider(hwnd);
  128.             ASSERT(m_hwndSlider);
  129.             return TRUE;    // I don't call setfocus...
  130.  
  131.         case WM_VSCROLL:
  132.             ASSERT(m_hwndSlider);
  133.             OnSliderNotification(wParam);
  134.             return TRUE;
  135.  
  136.         case WM_COMMAND:
  137.             if (LOWORD(wParam) == IDB_SQUARE_TRIANGLE) {
  138.  
  139.                 // Cycle through shapes.  See note below
  140.                 int iShape;
  141.                 m_pGargle->get_GargleShape(&iShape);  // find out what we now have
  142.                 iShape = 1-iShape;                            // change shapes
  143.                 m_pGargle->put_GargleShape(iShape);
  144.  
  145.                 SetButtonText(hwnd, iShape);
  146.  
  147.  
  148.             } else if (LOWORD(wParam) == IDB_DEFAULT) {
  149.                 // Set our control
  150.                 m_pGargle->put_DefaultGargleRate();
  151.                 m_pGargle->put_GargleShape(0);
  152.  
  153.                 // Set the slider position on the screen
  154.                 int iPos;
  155.                 m_pGargle->get_GargleRate(&iPos);
  156.                 iPos = ConvertToPosition(iPos);
  157.                 SendMessage(m_hwndSlider, TBM_SETPOS, TRUE, iPos);
  158.  
  159.                 // Set the button text
  160.                 int iShape;
  161.                 m_pGargle->get_GargleShape(&iShape);  // find out what we now have
  162.                 SetButtonText(hwnd, iShape);
  163.             } else {
  164.                 char Buff[100];
  165.                 wsprintf(Buff, "wParam=%08x", wParam);
  166.                 DbgBreak(Buff);
  167.             }
  168.             return TRUE;
  169.  
  170.         case WM_DESTROY:
  171.             DestroyWindow(m_hwndSlider);
  172.             return TRUE;
  173.  
  174.         default:
  175.             return FALSE;
  176.  
  177.     }
  178.  
  179. } // OnReceiveMessage
  180.  
  181.  
  182.  
  183. //
  184. // OnConnect
  185. //
  186. HRESULT CGargleProperties::OnConnect(IUnknown * punk)
  187. {
  188.     //
  189.     // Get IGargle interface
  190.     //
  191.  
  192.     if (punk == NULL) {
  193.         DbgBreak("You can't call me with a NULL pointer!!");
  194.         return(E_POINTER);
  195.     }
  196.  
  197.     ASSERT(m_pGargle == NULL);
  198.  
  199.     HRESULT hr = punk->QueryInterface(IID_IGargle, (void **) &m_pGargle);
  200.     if (FAILED(hr)) {
  201.         DbgBreak("Can't get IGargle interface.");
  202.         return E_NOINTERFACE;
  203.     }
  204.  
  205.     ASSERT(m_pGargle);
  206.  
  207.     m_pGargle->get_GargleRate(&m_iGargleRate);
  208.     m_pGargle->get_GargleShape(&m_iGargleShape);
  209.     return NOERROR;
  210. } // OnConnect
  211.  
  212.  
  213. //
  214. // OnDisconnect
  215. //
  216. HRESULT CGargleProperties::OnDisconnect()
  217. {
  218.     //
  219.     // Release the interface
  220.     //
  221.     if (m_pGargle == NULL) {
  222.         return( E_UNEXPECTED);
  223.     }
  224.  
  225.     m_pGargle->Release();
  226.     m_pGargle = NULL;
  227.  
  228.     return(NOERROR);
  229. } // OnDisconnect
  230.  
  231.  
  232.  
  233. //
  234. // OnDeactivate
  235. //
  236. // Destroy the dialog
  237. HRESULT CGargleProperties::OnDeactivate(void) {
  238.  
  239.     //
  240.     // Remember the Gargle Rate and shape for the next Activate() call
  241.     //
  242.     m_pGargle->get_GargleRate(&m_iGargleRate);
  243.     m_pGargle->get_GargleShape(&m_iGargleShape);
  244.  
  245.     return NOERROR;
  246. } // OnDeactivate
  247.  
  248.  
  249.  
  250. //
  251. // CreateSlider
  252. //
  253. // Create the slider (common control) to allow the user to
  254. // adjust the gargling rate
  255. HWND CGargleProperties::CreateSlider(HWND hwndParent)
  256. {
  257.  
  258.     LONG XUnit = GetDialogBaseUnits();
  259.     LONG YUnit = XUnit>>16;
  260.     XUnit = XUnit & 0x0000ffff;
  261.  
  262.     HWND hwndSlider = CreateWindow( TRACKBAR_CLASS
  263.                                   , TEXT("")
  264.                                   , WS_CHILD | WS_VISIBLE | TBS_VERT | TBS_BOTH
  265.                                   , 15*XUnit          // x
  266.                                   , 0                 // y
  267.                                   , 5*XUnit           // width
  268.                                   , 5*YUnit           // Height
  269.                                   , hwndParent
  270.                                   , NULL              // menu
  271.                                   , g_hInst
  272.                                   , NULL              // CLIENTCREATESTRUCT
  273.                                   );
  274.     if (hwndSlider == NULL) {
  275.         DWORD dwErr = GetLastError();
  276.         DbgLog((LOG_ERROR, 1
  277.                , TEXT("Could not create window.  error code: 0x%x"), dwErr));
  278.         return NULL;
  279.     }
  280.  
  281.     // Set the Range
  282.     SendMessage(hwndSlider, TBM_SETRANGE, TRUE, MAKELONG(0, 300) );
  283.  
  284.     // The range is 0..300 which is converted into Hz as 10**(p/100)
  285.     // where p is the slider position.  SeeConvertToFrequency() above.
  286.  
  287.     // This is how to set a tick at the default of 10Hz
  288.     // which corresponds to 100 on the log scale.
  289.     //  we put another one at 100Hz which corresponds to 200 on the log scale.
  290.     SendMessage(hwndSlider, TBM_SETTIC, 0, 100L);
  291.     SendMessage(hwndSlider, TBM_SETTIC, 0, 200L);
  292.  
  293.     // Set the slider position according to the value we obtain from
  294.     // initialisation or from the last Deactivate() call.
  295.  
  296.     int iPos = ConvertToPosition(m_iGargleRate);
  297.     SendMessage(hwndSlider, TBM_SETPOS, TRUE, iPos);
  298.     SetButtonText(hwndParent, m_iGargleShape);
  299.  
  300.     return hwndSlider;
  301. }  // CreateSlider
  302.  
  303.  
  304. //
  305. // OnSliderNotification
  306. //
  307. // Handle the notification meesages from the slider control
  308. void CGargleProperties::OnSliderNotification(WPARAM wParam)
  309. {
  310.     int iPos;
  311.     switch (wParam) {
  312.     case TB_BOTTOM:
  313.         iPos = ConvertToPosition(MinGargleRate);
  314.         SendMessage(m_hwndSlider, TBM_SETPOS, TRUE, (LPARAM) iPos);
  315.         break;
  316.  
  317.     case TB_TOP:
  318.         iPos = ConvertToPosition(MaxGargleRate);
  319.         SendMessage(m_hwndSlider, TBM_SETPOS, TRUE, (LPARAM) iPos);
  320.         break;
  321.  
  322.     case TB_PAGEDOWN:
  323.     case TB_PAGEUP:
  324.         break;
  325.  
  326.     case TB_THUMBPOSITION:
  327.     case TB_ENDTRACK: {
  328.             int iRate = (int) SendMessage(m_hwndSlider, TBM_GETPOS, 0, 0L);
  329.             iRate = ConvertToFrequency(iRate);
  330.             m_pGargle->put_GargleRate(iRate);
  331.         }
  332.         break;
  333.  
  334.     case TB_THUMBTRACK: // default handling of these messages is ok.
  335.     case TB_LINEDOWN:
  336.     case TB_LINEUP:
  337.         break;
  338.     }
  339. } // OnSliderNotification
  340.  
  341. #pragma warning(disable: 4514) // "unreferenced inline function has been removed"
  342.  
  343.