home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow / BaseClasses / winctrl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  64.8 KB  |  1,975 lines

  1. //------------------------------------------------------------------------------
  2. // File: WinCtrl.cpp
  3. //
  4. // Desc: DirectShow base classes - implements video control interface class.
  5. //
  6. // Copyright (c) 1992-2001 Microsoft Corporation.  All rights reserved.
  7. //------------------------------------------------------------------------------
  8.  
  9.  
  10. #include <streams.h>
  11.  
  12. // The control interface methods require us to be connected
  13.  
  14. #define CheckConnected(pin,code)                    \
  15. {                                                   \
  16.     if(pin == NULL) {                               \
  17.         ASSERT(!TEXT("Pin not set"));               \
  18.     }                                               \
  19.     else if(pin->IsConnected() == FALSE) {          \
  20.         return (code);                              \
  21.     }                                               \
  22. }
  23.  
  24. // This checks to see whether the window has a drain. An application can in
  25. // most environments set the owner/parent of windows so that they appear in
  26. // a compound document context (for example). In this case, the application
  27. // would probably like to be told of any keyboard/mouse messages. Therefore
  28. // we pass these messages on untranslated, returning TRUE if we're successful
  29.  
  30. BOOL WINAPI PossiblyEatMessage(HWND hwndDrain, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  31.     if(hwndDrain != NULL && !InSendMessage()) {
  32.         switch(uMsg) {
  33.             case WM_CHAR:
  34.             case WM_DEADCHAR:
  35.             case WM_KEYDOWN:
  36.             case WM_KEYUP:
  37.             case WM_LBUTTONDBLCLK:
  38.             case WM_LBUTTONDOWN:
  39.             case WM_LBUTTONUP:
  40.             case WM_MBUTTONDBLCLK:
  41.             case WM_MBUTTONDOWN:
  42.             case WM_MBUTTONUP:
  43.             case WM_MOUSEACTIVATE:
  44.             case WM_MOUSEMOVE:
  45.                 // If we pass this on we don't get any mouse clicks
  46.                 //case WM_NCHITTEST:
  47.             case WM_NCLBUTTONDBLCLK:
  48.             case WM_NCLBUTTONDOWN:
  49.             case WM_NCLBUTTONUP:
  50.             case WM_NCMBUTTONDBLCLK:
  51.             case WM_NCMBUTTONDOWN:
  52.             case WM_NCMBUTTONUP:
  53.             case WM_NCMOUSEMOVE:
  54.             case WM_NCRBUTTONDBLCLK:
  55.             case WM_NCRBUTTONDOWN:
  56.             case WM_NCRBUTTONUP:
  57.             case WM_RBUTTONDBLCLK:
  58.             case WM_RBUTTONDOWN:
  59.             case WM_RBUTTONUP:
  60.             case WM_SYSCHAR:
  61.             case WM_SYSDEADCHAR:
  62.             case WM_SYSKEYDOWN:
  63.             case WM_SYSKEYUP:
  64.  
  65.                 DbgLog((LOG_TRACE, 2, TEXT("Forwarding %x to drain")));
  66.                 PostMessage(hwndDrain, uMsg, wParam, lParam);
  67.  
  68.                 return TRUE;
  69.         }
  70.     }
  71.     return FALSE;
  72. }
  73.  
  74.  
  75. // This class implements the IVideoWindow control functions (dual interface)
  76. // we support a large number of properties and methods designed to allow the
  77. // client (whether it be an automation controller or a C/C++ application) to
  78. // set and get a number of window related properties such as it's position.
  79. // We also support some methods that duplicate the properties but provide a
  80. // more direct and efficient mechanism as many values may be changed in one
  81.  
  82. CBaseControlWindow::CBaseControlWindow(
  83.                         CBaseFilter *pFilter,        // Owning filter
  84.                         CCritSec *pInterfaceLock,    // Locking object
  85.                         TCHAR *pName,                // Object description
  86.                         LPUNKNOWN pUnk,              // Normal COM ownership
  87.                         HRESULT *phr) :              // OLE return code
  88.  
  89.     CBaseVideoWindow(pName,pUnk),
  90.     m_pInterfaceLock(pInterfaceLock),
  91.     m_hwndOwner(NULL),
  92.     m_hwndDrain(NULL),
  93.     m_bAutoShow(TRUE),
  94.     m_pFilter(pFilter),
  95.     m_bCursorHidden(FALSE),
  96.     m_pPin(NULL) {
  97.     ASSERT(m_pFilter);
  98.     ASSERT(m_pInterfaceLock);
  99.     ASSERT(phr);
  100.     m_BorderColour = VIDEO_COLOUR;
  101. }
  102.  
  103.  
  104. // Set the title caption on the base window, we don't do any field checking
  105. // as we really don't care what title they intend to have. We can always get
  106. // it back again later with GetWindowText. The only other complication is to
  107. // do the necessary string conversions between ANSI and OLE Unicode strings
  108.  
  109. STDMETHODIMP CBaseControlWindow::put_Caption(BSTR strCaption) {
  110.     CheckPointer(strCaption,E_POINTER);
  111.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  112. #ifdef UNICODE
  113.     SetWindowText(m_hwnd, strCaption);
  114. #else
  115.     CHAR Caption[CAPTION];
  116.  
  117.     WideCharToMultiByte(CP_ACP,0,strCaption,-1,Caption,CAPTION,NULL,NULL);
  118.     SetWindowText(m_hwnd, Caption);
  119. #endif
  120.     return NOERROR;
  121. }
  122.  
  123.  
  124. // Get the current base window title caption, once again we do no real field
  125. // checking. We allocate a string for the window title to be filled in with
  126. // which ensures the interface doesn't fiddle around with getting memory. A
  127. // BSTR is a normal C string with the length at position (-1), we use the
  128. // WriteBSTR helper function to create the caption to try and avoid OLE32
  129.  
  130. STDMETHODIMP CBaseControlWindow::get_Caption(BSTR *pstrCaption) {
  131.     CheckPointer(pstrCaption,E_POINTER);
  132.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  133.     WCHAR WideCaption[CAPTION];
  134.  
  135. #ifdef UNICODE
  136.     GetWindowText(m_hwnd,WideCaption,CAPTION);
  137. #else
  138.     // Convert the ASCII caption to a UNICODE string
  139.  
  140.     TCHAR Caption[CAPTION];
  141.     GetWindowText(m_hwnd,Caption,CAPTION);
  142.     MultiByteToWideChar(CP_ACP,0,Caption,-1,WideCaption,CAPTION);
  143. #endif
  144.     return WriteBSTR(pstrCaption,WideCaption);
  145. }
  146.  
  147.  
  148. // Set the window style using GWL_EXSTYLE
  149.  
  150. STDMETHODIMP CBaseControlWindow::put_WindowStyleEx(long WindowStyleEx) {
  151.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  152.  
  153.     // Should we be taking off WS_EX_TOPMOST
  154.  
  155.     if(GetWindowLong(m_hwnd,GWL_EXSTYLE) & WS_EX_TOPMOST) {
  156.         if((WindowStyleEx & WS_EX_TOPMOST) == 0) {
  157.             SendMessage(m_hwnd,m_ShowStageTop,(WPARAM) FALSE,(LPARAM) 0);
  158.         }
  159.     }
  160.  
  161.     // Likewise should we be adding WS_EX_TOPMOST
  162.  
  163.     if(WindowStyleEx & WS_EX_TOPMOST) {
  164.         SendMessage(m_hwnd,m_ShowStageTop,(WPARAM) TRUE,(LPARAM) 0);
  165.         WindowStyleEx &= (~WS_EX_TOPMOST);
  166.         if(WindowStyleEx == 0) return NOERROR;
  167.     }
  168.     return DoSetWindowStyle(WindowStyleEx,GWL_EXSTYLE);
  169. }
  170.  
  171.  
  172. // Gets the current GWL_EXSTYLE base window style
  173.  
  174. STDMETHODIMP CBaseControlWindow::get_WindowStyleEx(long *pWindowStyleEx) {
  175.     CheckPointer(pWindowStyleEx,E_POINTER);
  176.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  177.     return DoGetWindowStyle(pWindowStyleEx,GWL_EXSTYLE);
  178. }
  179.  
  180.  
  181. // Set the window style using GWL_STYLE
  182.  
  183. STDMETHODIMP CBaseControlWindow::put_WindowStyle(long WindowStyle) {
  184.     // These styles cannot be changed dynamically
  185.  
  186.     if((WindowStyle & WS_DISABLED) ||
  187.         (WindowStyle & WS_ICONIC) ||
  188.         (WindowStyle & WS_MAXIMIZE) ||
  189.         (WindowStyle & WS_MINIMIZE) ||
  190.         (WindowStyle & WS_HSCROLL) ||
  191.         (WindowStyle & WS_VSCROLL)) {
  192.  
  193.         return E_INVALIDARG;
  194.     }
  195.  
  196.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  197.     return DoSetWindowStyle(WindowStyle,GWL_STYLE);
  198. }
  199.  
  200.  
  201. // Get the current GWL_STYLE base window style
  202.  
  203. STDMETHODIMP CBaseControlWindow::get_WindowStyle(long *pWindowStyle) {
  204.     CheckPointer(pWindowStyle,E_POINTER);
  205.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  206.     return DoGetWindowStyle(pWindowStyle,GWL_STYLE);
  207. }
  208.  
  209.  
  210. // Change the base window style or the extended styles depending on whether
  211. // WindowLong is GWL_STYLE or GWL_EXSTYLE. We must call SetWindowPos to have
  212. // the window displayed in it's new style after the change which is a little
  213. // tricky if the window is not currently visible as we realise it offscreen.
  214. // In most cases the client will call get_WindowStyle before they call this
  215. // and then AND and OR in extra bit settings according to the requirements
  216.  
  217. HRESULT CBaseControlWindow::DoSetWindowStyle(long Style,long WindowLong) {
  218.     RECT WindowRect;
  219.  
  220.     // Get the window's visibility before setting the style
  221.     BOOL bVisible = IsWindowVisible(m_hwnd);
  222.     EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
  223.  
  224.     // Set the new style flags for the window
  225.     SetWindowLong(m_hwnd,WindowLong,Style);
  226.     UINT WindowFlags = SWP_SHOWWINDOW | SWP_FRAMECHANGED | SWP_NOACTIVATE;
  227.     WindowFlags |= SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE;
  228.  
  229.     // Show the window again in the current position
  230.  
  231.     if(bVisible == TRUE) {
  232.  
  233.         SetWindowPos(m_hwnd,            // Base window handle
  234.             HWND_TOP,          // Just a place holder
  235.             0,0,0,0,           // Leave size and position
  236.             WindowFlags);      // Just draw it again
  237.  
  238.         return NOERROR;
  239.     }
  240.  
  241.     // Move the window offscreen so the user doesn't see the changes
  242.  
  243.     MoveWindow((HWND) m_hwnd,                     // Base window handle
  244.         GetSystemMetrics(SM_CXSCREEN),     // Current desktop width
  245.         GetSystemMetrics(SM_CYSCREEN),     // Likewise it's height
  246.         WIDTH(&WindowRect),                // Use the same width
  247.         HEIGHT(&WindowRect),               // Keep height same to
  248.         TRUE);                             // May as well repaint
  249.  
  250.     // Now show the previously hidden window
  251.  
  252.     SetWindowPos(m_hwnd,            // Base window handle
  253.         HWND_TOP,          // Just a place holder
  254.         0,0,0,0,           // Leave size and position
  255.         WindowFlags);      // Just draw it again
  256.  
  257.     ShowWindow(m_hwnd,SW_HIDE);
  258.  
  259.     if(GetParent(m_hwnd)) {
  260.  
  261.         MapWindowPoints(HWND_DESKTOP, GetParent(m_hwnd), (LPPOINT)&WindowRect, 2);
  262.     }
  263.  
  264.     MoveWindow((HWND) m_hwnd,        // Base window handle
  265.         WindowRect.left,      // Existing x coordinate
  266.         WindowRect.top,       // Existing y coordinate
  267.         WIDTH(&WindowRect),   // Use the same width
  268.         HEIGHT(&WindowRect),  // Keep height same to
  269.         TRUE);                // May as well repaint
  270.  
  271.     return NOERROR;
  272. }
  273.  
  274.  
  275. // Get the current base window style (either GWL_STYLE or GWL_EXSTYLE)
  276.  
  277. HRESULT CBaseControlWindow::DoGetWindowStyle(long *pStyle,long WindowLong) {
  278.     *pStyle = GetWindowLong(m_hwnd,WindowLong);
  279.     return NOERROR;
  280. }
  281.  
  282.  
  283. // Change the visibility of the base window, this takes the same parameters
  284. // as the ShowWindow Win32 API does, so the client can have the window hidden
  285. // or shown, minimised to an icon, or maximised to play in full screen mode
  286. // We pass the request on to the base window to actually make the change
  287.  
  288. STDMETHODIMP CBaseControlWindow::put_WindowState(long WindowState) {
  289.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  290.     DoShowWindow(WindowState);
  291.     return NOERROR;
  292. }
  293.  
  294.  
  295. // Get the current window state, this function returns a subset of the SW bit
  296. // settings available in ShowWindow, if the window is visible then SW_SHOW is
  297. // set, if it is hidden then the SW_HIDDEN is set, if it is either minimised
  298. // or maximised then the SW_MINIMIZE or SW_MAXIMIZE is set respectively. The
  299. // other SW bit settings are really set commands not readable output values
  300.  
  301. STDMETHODIMP CBaseControlWindow::get_WindowState(long *pWindowState) {
  302.     CheckPointer(pWindowState,E_POINTER);
  303.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  304.     ASSERT(pWindowState);
  305.     *pWindowState = FALSE;
  306.  
  307.     // Is the window visible, a window is termed visible if it is somewhere on
  308.     // the current desktop even if it is completely obscured by other windows
  309.     // so the flag is a style for each window set with the WS_VISIBLE bit
  310.  
  311.     if(IsWindowVisible(m_hwnd) == TRUE) {
  312.  
  313.         // Is the base window iconic
  314.         if(IsIconic(m_hwnd) == TRUE) {
  315.             *pWindowState |= SW_MINIMIZE;
  316.         }
  317.  
  318.         // Has the window been maximised
  319.         else if(IsZoomed(m_hwnd) == TRUE) {
  320.             *pWindowState |= SW_MAXIMIZE;
  321.         }
  322.  
  323.         // Window is normal
  324.         else {
  325.             *pWindowState |= SW_SHOW;
  326.         }
  327.  
  328.     }
  329.     else {
  330.         *pWindowState |= SW_HIDE;
  331.     }
  332.     return NOERROR;
  333. }
  334.  
  335.  
  336. // This makes sure that any palette we realise in the base window (through a
  337. // media type or through the overlay interface) is done in the background and
  338. // is therefore mapped to existing device entries rather than taking it over
  339. // as it will do when we this window gets the keyboard focus. An application
  340. // uses this to make sure it doesn't have it's palette removed by the window
  341.  
  342. STDMETHODIMP CBaseControlWindow::put_BackgroundPalette(long BackgroundPalette) {
  343.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  344.     CAutoLock cWindowLock(&m_WindowLock);
  345.  
  346.     // Check this is a valid automation boolean type
  347.  
  348.     if(BackgroundPalette != OATRUE) {
  349.         if(BackgroundPalette != OAFALSE) {
  350.             return E_INVALIDARG;
  351.         }
  352.     }
  353.  
  354.     // Make sure the window realises any palette it has again
  355.  
  356.     m_bBackground = (BYTE) (BackgroundPalette == OATRUE ? TRUE : FALSE);
  357.     PostMessage(m_hwnd,m_RealizePalette,0,0);
  358.     PaintWindow(FALSE);
  359.  
  360.     return NOERROR;
  361. }
  362.  
  363.  
  364. // This returns the current background realisation setting
  365.  
  366. STDMETHODIMP
  367. CBaseControlWindow::get_BackgroundPalette(long *pBackgroundPalette) {
  368.     CheckPointer(pBackgroundPalette,E_POINTER);
  369.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  370.     CAutoLock cWindowLock(&m_WindowLock);
  371.  
  372.     // Get the current background palette setting
  373.  
  374.     *pBackgroundPalette = (m_bBackground == TRUE ? OATRUE : OAFALSE);
  375.     return NOERROR;
  376. }
  377.  
  378.  
  379. // Change the visibility of the base window
  380.  
  381. STDMETHODIMP CBaseControlWindow::put_Visible(long Visible) {
  382.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  383.  
  384.     // Check this is a valid automation boolean type
  385.  
  386.     if(Visible != OATRUE) {
  387.         if(Visible != OAFALSE) {
  388.             return E_INVALIDARG;
  389.         }
  390.     }
  391.  
  392.     // Convert the boolean visibility into SW_SHOW and SW_HIDE
  393.  
  394.     INT Mode = (Visible == OATRUE ? SW_SHOWNORMAL : SW_HIDE);
  395.     DoShowWindow(Mode);
  396.     return NOERROR;
  397. }
  398.  
  399.  
  400. // Return OATRUE if the window is currently visible otherwise OAFALSE
  401.  
  402. STDMETHODIMP CBaseControlWindow::get_Visible(long *pVisible) {
  403.     CheckPointer(pVisible,E_POINTER);
  404.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  405.  
  406.     // See if the base window has a WS_VISIBLE style - this will return TRUE
  407.     // even if the window is completely obscured by other desktop windows, we
  408.     // return FALSE if the window is not showing because of earlier calls
  409.  
  410.     BOOL Mode = IsWindowVisible(m_hwnd);
  411.     *pVisible = (Mode == TRUE ? OATRUE : OAFALSE);
  412.     return NOERROR;
  413. }
  414.  
  415.  
  416. // Change the left position of the base window. This keeps the window width
  417. // and height properties the same so it effectively shunts the window left or
  418. // right accordingly - there is the Width property to change that dimension
  419.  
  420. STDMETHODIMP CBaseControlWindow::put_Left(long Left) {
  421.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  422.     BOOL bSuccess;
  423.     RECT WindowRect;
  424.  
  425.     // Get the current window position in a RECT
  426.     EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
  427.  
  428.     if(GetParent(m_hwnd)) {
  429.  
  430.         MapWindowPoints(HWND_DESKTOP, GetParent(m_hwnd), (LPPOINT)&WindowRect, 2);
  431.     }
  432.  
  433.     // Adjust the coordinates ready for SetWindowPos, the window rectangle we
  434.     // get back from GetWindowRect is in left,top,right and bottom while the
  435.     // coordinates SetWindowPos wants are left,top,width and height values
  436.  
  437.     WindowRect.bottom = WindowRect.bottom - WindowRect.top;
  438.     WindowRect.right = WindowRect.right - WindowRect.left;
  439.     UINT WindowFlags = SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOACTIVATE;
  440.  
  441.     bSuccess = SetWindowPos(m_hwnd,                // Window handle
  442.         HWND_TOP,              // Put it at the top
  443.         Left,                  // New left position
  444.         WindowRect.top,        // Leave top alone
  445.         WindowRect.right,      // The WIDTH (not right)
  446.         WindowRect.bottom,     // The HEIGHT (not bottom)
  447.         WindowFlags);          // Show window options
  448.  
  449.     if(bSuccess == FALSE) {
  450.         return E_INVALIDARG;
  451.     }
  452.     return NOERROR;
  453. }
  454.  
  455.  
  456. // Return the current base window left position
  457.  
  458. STDMETHODIMP CBaseControlWindow::get_Left(long *pLeft) {
  459.     CheckPointer(pLeft,E_POINTER);
  460.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  461.     RECT WindowRect;
  462.  
  463.     EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
  464.     *pLeft = WindowRect.left;
  465.     return NOERROR;
  466. }
  467.  
  468.  
  469. // Change the current width of the base window. This property complements the
  470. // left position property so we must keep the left edge constant and expand or
  471. // contract to the right, the alternative would be to change the left edge so
  472. // keeping the right edge constant but this is maybe a little more intuitive
  473.  
  474. STDMETHODIMP CBaseControlWindow::put_Width(long Width) {
  475.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  476.     BOOL bSuccess;
  477.     RECT WindowRect;
  478.  
  479.     // Adjust the coordinates ready for SetWindowPos, the window rectangle we
  480.     // get back from GetWindowRect is in left,top,right and bottom while the
  481.     // coordinates SetWindowPos wants are left,top,width and height values
  482.  
  483.     EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
  484.  
  485.     if(GetParent(m_hwnd)) {
  486.  
  487.         MapWindowPoints(HWND_DESKTOP, GetParent(m_hwnd), (LPPOINT)&WindowRect, 2);
  488.     }
  489.  
  490.     WindowRect.bottom = WindowRect.bottom - WindowRect.top;
  491.     UINT WindowFlags = SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOACTIVATE;
  492.  
  493.     // This seems to have a bug in that calling SetWindowPos on a window with
  494.     // just the width changing causes it to ignore the width that you pass in
  495.     // and sets it to a mimimum value of 110 pixels wide (Windows NT 3.51)
  496.  
  497.     bSuccess = SetWindowPos(m_hwnd,                // Window handle
  498.         HWND_TOP,              // Put it at the top
  499.         WindowRect.left,       // Leave left alone
  500.         WindowRect.top,        // Leave top alone
  501.         Width,                 // New WIDTH dimension
  502.         WindowRect.bottom,     // The HEIGHT (not bottom)
  503.         WindowFlags);          // Show window options
  504.  
  505.     if(bSuccess == FALSE) {
  506.         return E_INVALIDARG;
  507.     }
  508.     return NOERROR;
  509. }
  510.  
  511.  
  512. // Return the current base window width
  513.  
  514. STDMETHODIMP CBaseControlWindow::get_Width(long *pWidth) {
  515.     CheckPointer(pWidth,E_POINTER);
  516.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  517.     RECT WindowRect;
  518.  
  519.     EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
  520.     *pWidth = WindowRect.right - WindowRect.left;
  521.     return NOERROR;
  522. }
  523.  
  524.  
  525. // This allows the client program to change the top position for the window in
  526. // the same way that changing the left position does not affect the width of
  527. // the image so changing the top position does not affect the window height
  528.  
  529. STDMETHODIMP CBaseControlWindow::put_Top(long Top) {
  530.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  531.     BOOL bSuccess;
  532.     RECT WindowRect;
  533.  
  534.     // Get the current window position in a RECT
  535.     EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
  536.  
  537.     if(GetParent(m_hwnd)) {
  538.  
  539.         MapWindowPoints(HWND_DESKTOP, GetParent(m_hwnd), (LPPOINT)&WindowRect, 2);
  540.     }
  541.  
  542.     // Adjust the coordinates ready for SetWindowPos, the window rectangle we
  543.     // get back from GetWindowRect is in left,top,right and bottom while the
  544.     // coordinates SetWindowPos wants are left,top,width and height values
  545.  
  546.     WindowRect.bottom = WindowRect.bottom - WindowRect.top;
  547.     WindowRect.right = WindowRect.right - WindowRect.left;
  548.     UINT WindowFlags = SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOACTIVATE;
  549.  
  550.     bSuccess = SetWindowPos(m_hwnd,                // Window handle
  551.         HWND_TOP,              // Put it at the top
  552.         WindowRect.left,       // Leave left alone
  553.         Top,                   // New top position
  554.         WindowRect.right,      // The WIDTH (not right)
  555.         WindowRect.bottom,     // The HEIGHT (not bottom)
  556.         WindowFlags);          // Show window flags
  557.  
  558.     if(bSuccess == FALSE) {
  559.         return E_INVALIDARG;
  560.     }
  561.     return NOERROR;
  562. }
  563.  
  564.  
  565. // Return the current base window top position
  566.  
  567. STDMETHODIMP CBaseControlWindow::get_Top(long *pTop) {
  568.     CheckPointer(pTop,E_POINTER);
  569.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  570.     RECT WindowRect;
  571.  
  572.     EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
  573.     *pTop = WindowRect.top;
  574.     return NOERROR;
  575. }
  576.  
  577.  
  578. // Change the height of the window, this complements the top property so when
  579. // we change this we must keep the top position for the base window, as said
  580. // before we could keep the bottom and grow upwards although this is perhaps
  581. // a little more intuitive since we already have a top position property
  582.  
  583. STDMETHODIMP CBaseControlWindow::put_Height(long Height) {
  584.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  585.     BOOL bSuccess;
  586.     RECT WindowRect;
  587.  
  588.     // Adjust the coordinates ready for SetWindowPos, the window rectangle we
  589.     // get back from GetWindowRect is in left,top,right and bottom while the
  590.     // coordinates SetWindowPos wants are left,top,width and height values
  591.  
  592.     EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
  593.  
  594.     if(GetParent(m_hwnd)) {
  595.  
  596.         MapWindowPoints(HWND_DESKTOP, GetParent(m_hwnd), (LPPOINT)&WindowRect, 2);
  597.     }
  598.  
  599.     WindowRect.right = WindowRect.right - WindowRect.left;
  600.     UINT WindowFlags = SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOACTIVATE;
  601.  
  602.     bSuccess = SetWindowPos(m_hwnd,                // Window handle
  603.         HWND_TOP,              // Put it at the top
  604.         WindowRect.left,       // Leave left alone
  605.         WindowRect.top,        // Leave top alone
  606.         WindowRect.right,      // The WIDTH (not right)
  607.         Height,                // New height dimension
  608.         WindowFlags);          // Show window flags
  609.  
  610.     if(bSuccess == FALSE) {
  611.         return E_INVALIDARG;
  612.     }
  613.     return NOERROR;
  614. }
  615.  
  616.  
  617. // Return the current base window height
  618.  
  619. STDMETHODIMP CBaseControlWindow::get_Height(long *pHeight) {
  620.     CheckPointer(pHeight,E_POINTER);
  621.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  622.     RECT WindowRect;
  623.  
  624.     EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
  625.     *pHeight = WindowRect.bottom - WindowRect.top;
  626.     return NOERROR;
  627. }
  628.  
  629.  
  630. // This can be called to change the owning window. Setting the owner is done
  631. // through this function, however to make the window a true child window the
  632. // style must also be set to WS_CHILD. After resetting the owner to NULL an
  633. // application should also set the style to WS_OVERLAPPED | WS_CLIPCHILDREN.
  634.  
  635. // We cannot lock the object here because the SetParent causes an interthread
  636. // SendMessage to the owner window. If they are in GetState we will sit here
  637. // incomplete with the critical section locked therefore blocking out source
  638. // filter threads from accessing us. Because the source thread can't enter us
  639. // it can't get buffers or call EndOfStream so the GetState will not complete
  640.  
  641. STDMETHODIMP CBaseControlWindow::put_Owner(OAHWND Owner) {
  642.     // Check we are connected otherwise reject the call
  643.  
  644.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  645.     m_hwndOwner = (HWND) Owner;
  646.     HWND hwndParent = m_hwndOwner;
  647.  
  648.     // Add or remove WS_CHILD as appropriate
  649.  
  650.     LONG Style = GetWindowLong(m_hwnd,GWL_STYLE);
  651.     if(Owner == NULL) {
  652.         Style &= (~WS_CHILD);
  653.     }
  654.     else {
  655.         Style |= (WS_CHILD);
  656.     }
  657.     SetWindowLong(m_hwnd,GWL_STYLE,Style);
  658.  
  659.     // Don't call this with the filter locked
  660.  
  661.     SetParent(m_hwnd,hwndParent);
  662.  
  663.     PaintWindow(TRUE);
  664.     NOTE1("Changed parent %lx",hwndParent);
  665.  
  666.     return NOERROR;
  667. }
  668.  
  669.  
  670. // This complements the put_Owner to get the current owning window property
  671. // we always return NOERROR although the returned window handle may be NULL
  672. // to indicate no owning window (the desktop window doesn't qualify as one)
  673. // If an application sets the owner we call SetParent, however that returns
  674. // NULL until the WS_CHILD bit is set on, so we store the owner internally
  675.  
  676. STDMETHODIMP CBaseControlWindow::get_Owner(OAHWND *Owner) {
  677.     CheckPointer(Owner,E_POINTER);
  678.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  679.     *Owner = (OAHWND) m_hwndOwner;
  680.     return NOERROR;
  681. }
  682.  
  683.  
  684. // And renderer supporting IVideoWindow may have an HWND set who will get any
  685. // keyboard and mouse messages we receive posted on to them. This is separate
  686. // from setting an owning window. By separating the two, applications may get
  687. // messages sent on even when they have set no owner (perhaps it's maximised)
  688.  
  689. STDMETHODIMP CBaseControlWindow::put_MessageDrain(OAHWND Drain) {
  690.     // Check we are connected otherwise reject the call
  691.  
  692.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  693.     m_hwndDrain = (HWND) Drain;
  694.     return NOERROR;
  695. }
  696.  
  697.  
  698. // Return the current message drain
  699.  
  700. STDMETHODIMP CBaseControlWindow::get_MessageDrain(OAHWND *Drain) {
  701.     CheckPointer(Drain,E_POINTER);
  702.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  703.     *Drain = (OAHWND) m_hwndDrain;
  704.     return NOERROR;
  705. }
  706.  
  707.  
  708. // This is called by the filter graph to inform us of a message we should know
  709. // is being sent to our owning window. We have this because as a child window
  710. // we do not get certain messages that are only sent to top level windows. We
  711. // must see the palette changed/changing/query messages so that we know if we
  712. // have the foreground palette or not. We pass the message on to our window
  713. // using SendMessage - this will cause an interthread send message to occur
  714.  
  715. STDMETHODIMP
  716. CBaseControlWindow::NotifyOwnerMessage(OAHWND hwnd,    // Window handle
  717.                                        long uMsg,    // Message ID
  718.                                        LONG_PTR wParam,  // Parameters
  719.                                        LONG_PTR lParam)  // for message
  720. {
  721.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  722.  
  723.     // Only interested in these Windows messages
  724.  
  725.     switch(uMsg) {
  726.  
  727.         case WM_SYSCOLORCHANGE:
  728.         case WM_PALETTECHANGED:
  729.         case WM_PALETTEISCHANGING:
  730.         case WM_QUERYNEWPALETTE:
  731.         case WM_DEVMODECHANGE:
  732.         case WM_DISPLAYCHANGE:
  733.         case WM_ACTIVATEAPP:
  734.  
  735.             // If we do not have an owner then ignore
  736.  
  737.             if(m_hwndOwner == NULL) {
  738.                 return NOERROR;
  739.             }
  740.             SendMessage(m_hwnd,uMsg,(WPARAM)wParam,(LPARAM)lParam);
  741.             break;
  742.  
  743.         // do NOT fwd WM_MOVE. the parameters are the location of the parent
  744.         // window, NOT what the renderer should be looking at.  But we need
  745.         // to make sure the overlay is moved with the parent window, so we
  746.         // do this.
  747.         case WM_MOVE:
  748.             PostMessage(m_hwnd,WM_PAINT,0,0);
  749.             break;
  750.     }
  751.     return NOERROR;
  752. }
  753.  
  754.  
  755. // Allow an application to have us set the base window in the foreground. We
  756. // have this because it is difficult for one thread to do do this to a window
  757. // owned by another thread. We ask the base window class to do the real work
  758.  
  759. STDMETHODIMP CBaseControlWindow::SetWindowForeground(long Focus) {
  760.     // Check this is a valid automation boolean type
  761.  
  762.     if(Focus != OATRUE) {
  763.         if(Focus != OAFALSE) {
  764.             return E_INVALIDARG;
  765.         }
  766.     }
  767.  
  768.     // We shouldn't lock as this sends a message
  769.  
  770.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  771.     BOOL bFocus = (Focus == OATRUE ? TRUE : FALSE);
  772.     DoSetWindowForeground(bFocus);
  773.  
  774.     return NOERROR;
  775. }
  776.  
  777.  
  778. // This allows a client to set the complete window size and position in one
  779. // atomic operation. The same affect can be had by changing each dimension
  780. // in turn through their individual properties although some flashing will
  781. // occur as each of them gets updated (they are better set at design time)
  782.  
  783. STDMETHODIMP
  784. CBaseControlWindow::SetWindowPosition(long Left,long Top,long Width,long Height) {
  785.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  786.     BOOL bSuccess;
  787.  
  788.     // Set the new size and position
  789.     UINT WindowFlags = SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOACTIVATE;
  790.  
  791.     ASSERT(IsWindow(m_hwnd));
  792.     bSuccess = SetWindowPos(m_hwnd,         // Window handle
  793.         HWND_TOP,       // Put it at the top
  794.         Left,           // Left position
  795.         Top,            // Top position
  796.         Width,          // Window width
  797.         Height,         // Window height
  798.         WindowFlags);   // Show window flags
  799.     ASSERT(bSuccess);
  800. #ifdef DEBUG
  801.     DbgLog((LOG_TRACE, 1, TEXT("SWP failed error %d"), GetLastError()));
  802. #endif
  803.     if(bSuccess == FALSE) {
  804.         return E_INVALIDARG;
  805.     }
  806.     return NOERROR;
  807. }
  808.  
  809.  
  810. // This complements the SetWindowPosition to return the current window place
  811. // in device coordinates. As before the same information can be retrived by
  812. // calling the property get functions individually but this is atomic and is
  813. // therefore more suitable to a live environment rather than design time
  814.  
  815. STDMETHODIMP
  816. CBaseControlWindow::GetWindowPosition(long *pLeft,long *pTop,long *pWidth,long *pHeight) {
  817.     // Should check the pointers are not NULL
  818.  
  819.     CheckPointer(pLeft,E_POINTER);
  820.     CheckPointer(pTop,E_POINTER);
  821.     CheckPointer(pWidth,E_POINTER);
  822.     CheckPointer(pHeight,E_POINTER);
  823.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  824.     RECT WindowRect;
  825.  
  826.     // Get the current window coordinates
  827.  
  828.     EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
  829.  
  830.     // Convert the RECT into left,top,width and height values
  831.  
  832.     *pLeft = WindowRect.left;
  833.     *pTop = WindowRect.top;
  834.     *pWidth = WindowRect.right - WindowRect.left;
  835.     *pHeight = WindowRect.bottom - WindowRect.top;
  836.  
  837.     return NOERROR;
  838. }
  839.  
  840.  
  841. // When a window is maximised or iconic calling GetWindowPosition will return
  842. // the current window position (likewise for the properties). However if the
  843. // restored size (ie the size we'll return to when normally shown) is needed
  844. // then this should be used. When in a normal position (neither iconic nor
  845. // maximised) then this returns the same coordinates as GetWindowPosition
  846.  
  847. STDMETHODIMP
  848. CBaseControlWindow::GetRestorePosition(long *pLeft,long *pTop,long *pWidth,long *pHeight) {
  849.     // Should check the pointers are not NULL
  850.  
  851.     CheckPointer(pLeft,E_POINTER);
  852.     CheckPointer(pTop,E_POINTER);
  853.     CheckPointer(pWidth,E_POINTER);
  854.     CheckPointer(pHeight,E_POINTER);
  855.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  856.  
  857.     // Use GetWindowPlacement to find the restore position
  858.  
  859.     WINDOWPLACEMENT Place;
  860.     Place.length = sizeof(WINDOWPLACEMENT);
  861.     EXECUTE_ASSERT(GetWindowPlacement(m_hwnd,&Place));
  862.  
  863.     RECT WorkArea;
  864.  
  865.     // We must take into account any task bar present
  866.  
  867.     if(SystemParametersInfo(SPI_GETWORKAREA,0,&WorkArea,FALSE) == TRUE) {
  868.         if(GetParent(m_hwnd) == NULL) {
  869.             Place.rcNormalPosition.top += WorkArea.top;
  870.             Place.rcNormalPosition.bottom += WorkArea.top;
  871.             Place.rcNormalPosition.left += WorkArea.left;
  872.             Place.rcNormalPosition.right += WorkArea.left;
  873.         }
  874.     }
  875.  
  876.     // Convert the RECT into left,top,width and height values
  877.  
  878.     *pLeft = Place.rcNormalPosition.left;
  879.     *pTop = Place.rcNormalPosition.top;
  880.     *pWidth = Place.rcNormalPosition.right - Place.rcNormalPosition.left;
  881.     *pHeight = Place.rcNormalPosition.bottom - Place.rcNormalPosition.top;
  882.  
  883.     return NOERROR;
  884. }
  885.  
  886.  
  887. // Return the current border colour, if we are playing something to a subset
  888. // of the base window display there is an outside area exposed. The default
  889. // action is to paint this colour in the Windows background colour (defined
  890. // as value COLOR_WINDOW) We reset to this default when we're disconnected
  891.  
  892. STDMETHODIMP CBaseControlWindow::get_BorderColor(long *Color) {
  893.     CheckPointer(Color,E_POINTER);
  894.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  895.     *Color = (long) m_BorderColour;
  896.     return NOERROR;
  897. }
  898.  
  899.  
  900. // This can be called to set the current border colour
  901.  
  902. STDMETHODIMP CBaseControlWindow::put_BorderColor(long Color) {
  903.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  904.  
  905.     // Have the window repainted with the new border colour
  906.  
  907.     m_BorderColour = (COLORREF) Color;
  908.     PaintWindow(TRUE);
  909.     return NOERROR;
  910. }
  911.  
  912.  
  913. // Delegate fullscreen handling to plug in distributor
  914.  
  915. STDMETHODIMP CBaseControlWindow::get_FullScreenMode(long *FullScreenMode) {
  916.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  917.     CheckPointer(FullScreenMode,E_POINTER);
  918.     return E_NOTIMPL;
  919. }
  920.  
  921.  
  922. // Delegate fullscreen handling to plug in distributor
  923.  
  924. STDMETHODIMP CBaseControlWindow::put_FullScreenMode(long FullScreenMode) {
  925.     return E_NOTIMPL;
  926. }
  927.  
  928.  
  929. // This sets the auto show property, this property causes the base window to
  930. // be displayed whenever we change state. This allows an application to have
  931. // to do nothing to have the window appear but still allow them to change the
  932. // default behaviour if for example they want to keep it hidden for longer
  933.  
  934. STDMETHODIMP CBaseControlWindow::put_AutoShow(long AutoShow) {
  935.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  936.  
  937.     // Check this is a valid automation boolean type
  938.  
  939.     if(AutoShow != OATRUE) {
  940.         if(AutoShow != OAFALSE) {
  941.             return E_INVALIDARG;
  942.         }
  943.     }
  944.  
  945.     m_bAutoShow = (AutoShow == OATRUE ? TRUE : FALSE);
  946.     return NOERROR;
  947. }
  948.  
  949.  
  950. // This can be called to get the current auto show flag. The flag is updated
  951. // when we connect and disconnect and through this interface all of which are
  952. // controlled and serialised by means of the main renderer critical section
  953.  
  954. STDMETHODIMP CBaseControlWindow::get_AutoShow(long *AutoShow) {
  955.     CheckPointer(AutoShow,E_POINTER);
  956.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  957.     *AutoShow = (m_bAutoShow == TRUE ? OATRUE : OAFALSE);
  958.     return NOERROR;
  959. }
  960.  
  961.  
  962. // Return the minimum ideal image size for the current video. This may differ
  963. // to the actual video dimensions because we may be using DirectDraw hardware
  964. // that has specific stretching requirements. For example the Cirrus Logic
  965. // cards have a minimum stretch factor depending on the overlay surface size
  966.  
  967. STDMETHODIMP
  968. CBaseControlWindow::GetMinIdealImageSize(long *pWidth,long *pHeight) {
  969.     CheckPointer(pWidth,E_POINTER);
  970.     CheckPointer(pHeight,E_POINTER);
  971.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  972.     FILTER_STATE State;
  973.  
  974.     // Must not be stopped for this to work correctly
  975.  
  976.     m_pFilter->GetState(0,&State);
  977.     if(State == State_Stopped) {
  978.         return VFW_E_WRONG_STATE;
  979.     }
  980.  
  981.     RECT DefaultRect = GetDefaultRect();
  982.     *pWidth = WIDTH(&DefaultRect);
  983.     *pHeight = HEIGHT(&DefaultRect);
  984.     return NOERROR;
  985. }
  986.  
  987.  
  988. // Return the maximum ideal image size for the current video. This may differ
  989. // to the actual video dimensions because we may be using DirectDraw hardware
  990. // that has specific stretching requirements. For example the Cirrus Logic
  991. // cards have a maximum stretch factor depending on the overlay surface size
  992.  
  993. STDMETHODIMP
  994. CBaseControlWindow::GetMaxIdealImageSize(long *pWidth,long *pHeight) {
  995.     CheckPointer(pWidth,E_POINTER);
  996.     CheckPointer(pHeight,E_POINTER);
  997.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  998.     FILTER_STATE State;
  999.  
  1000.     // Must not be stopped for this to work correctly
  1001.  
  1002.     m_pFilter->GetState(0,&State);
  1003.     if(State == State_Stopped) {
  1004.         return VFW_E_WRONG_STATE;
  1005.     }
  1006.  
  1007.     RECT DefaultRect = GetDefaultRect();
  1008.     *pWidth = WIDTH(&DefaultRect);
  1009.     *pHeight = HEIGHT(&DefaultRect);
  1010.     return NOERROR;
  1011. }
  1012.  
  1013.  
  1014. // Allow an application to hide the cursor on our window
  1015.  
  1016. STDMETHODIMP
  1017. CBaseControlWindow::HideCursor(long HideCursor) {
  1018.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1019.  
  1020.     // Check this is a valid automation boolean type
  1021.  
  1022.     if(HideCursor != OATRUE) {
  1023.         if(HideCursor != OAFALSE) {
  1024.             return E_INVALIDARG;
  1025.         }
  1026.     }
  1027.  
  1028.     m_bCursorHidden = (HideCursor == OATRUE ? TRUE : FALSE);
  1029.     return NOERROR;
  1030. }
  1031.  
  1032.  
  1033. // Returns whether we have the cursor hidden or not
  1034.  
  1035. STDMETHODIMP CBaseControlWindow::IsCursorHidden(long *CursorHidden) {
  1036.     CheckPointer(CursorHidden,E_POINTER);
  1037.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1038.     *CursorHidden = (m_bCursorHidden == TRUE ? OATRUE : OAFALSE);
  1039.     return NOERROR;
  1040. }
  1041.  
  1042.  
  1043. // This class implements the IBasicVideo control functions (dual interface)
  1044. // we support a large number of properties and methods designed to allow the
  1045. // client (whether it be an automation controller or a C/C++ application) to
  1046. // set and get a number of video related properties such as the native video
  1047. // size. We support some methods that duplicate the properties but provide a
  1048. // more direct and efficient mechanism as many values may be changed in one
  1049.  
  1050. CBaseControlVideo::CBaseControlVideo(
  1051.                         CBaseFilter *pFilter,        // Owning filter
  1052.                         CCritSec *pInterfaceLock,    // Locking object
  1053.                         TCHAR *pName,                // Object description
  1054.                         LPUNKNOWN pUnk,              // Normal COM ownership
  1055.                         HRESULT *phr) :              // OLE return code
  1056.  
  1057.     CBaseBasicVideo(pName,pUnk),
  1058.     m_pFilter(pFilter),
  1059.     m_pInterfaceLock(pInterfaceLock),
  1060.     m_pPin(NULL) {
  1061.     ASSERT(m_pFilter);
  1062.     ASSERT(m_pInterfaceLock);
  1063.     ASSERT(phr);
  1064. }
  1065.  
  1066. // Return an approximate average time per frame
  1067.  
  1068. STDMETHODIMP CBaseControlVideo::get_AvgTimePerFrame(REFTIME *pAvgTimePerFrame) {
  1069.     CheckPointer(pAvgTimePerFrame,E_POINTER);
  1070.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1071.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1072.  
  1073.     VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
  1074.     if(pVideoInfo == NULL)
  1075.         return E_OUTOFMEMORY;
  1076.     COARefTime AvgTime(pVideoInfo->AvgTimePerFrame);
  1077.     *pAvgTimePerFrame = (REFTIME) AvgTime;
  1078.  
  1079.     return NOERROR;
  1080. }
  1081.  
  1082.  
  1083. // Return an approximate bit rate for the video
  1084.  
  1085. STDMETHODIMP CBaseControlVideo::get_BitRate(long *pBitRate) {
  1086.     CheckPointer(pBitRate,E_POINTER);
  1087.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1088.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1089.  
  1090.     VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
  1091.     if(pVideoInfo == NULL)
  1092.         return E_OUTOFMEMORY;
  1093.     *pBitRate = pVideoInfo->dwBitRate;
  1094.     return NOERROR;
  1095. }
  1096.  
  1097.  
  1098. // Return an approximate bit error rate
  1099.  
  1100. STDMETHODIMP CBaseControlVideo::get_BitErrorRate(long *pBitErrorRate) {
  1101.     CheckPointer(pBitErrorRate,E_POINTER);
  1102.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1103.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1104.  
  1105.     VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
  1106.     if(pVideoInfo == NULL)
  1107.         return E_OUTOFMEMORY;
  1108.     *pBitErrorRate = pVideoInfo->dwBitErrorRate;
  1109.     return NOERROR;
  1110. }
  1111.  
  1112.  
  1113. // This returns the current video width
  1114.  
  1115. STDMETHODIMP CBaseControlVideo::get_VideoWidth(long *pVideoWidth) {
  1116.     CheckPointer(pVideoWidth,E_POINTER);
  1117.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1118.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1119.  
  1120.     VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
  1121.     if(pVideoInfo == NULL)
  1122.         return E_OUTOFMEMORY;
  1123.     *pVideoWidth = pVideoInfo->bmiHeader.biWidth;
  1124.     return NOERROR;
  1125. }
  1126.  
  1127.  
  1128. // This returns the current video height
  1129.  
  1130. STDMETHODIMP CBaseControlVideo::get_VideoHeight(long *pVideoHeight) {
  1131.     CheckPointer(pVideoHeight,E_POINTER);
  1132.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1133.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1134.  
  1135.     VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
  1136.     if(pVideoInfo == NULL)
  1137.         return E_OUTOFMEMORY;
  1138.     *pVideoHeight = pVideoInfo->bmiHeader.biHeight;
  1139.     return NOERROR;
  1140. }
  1141.  
  1142.  
  1143. // This returns the current palette the video is using as an array allocated
  1144. // by the user. To remain consistent we use PALETTEENTRY fields to return the
  1145. // colours in rather than RGBQUADs that multimedia decided to use. The memory
  1146. // is allocated by the user so we simple copy each in turn. We check that the
  1147. // number of entries requested and the start position offset are both valid
  1148. // If the number of entries evaluates to zero then we return an S_FALSE code
  1149.  
  1150. STDMETHODIMP CBaseControlVideo::GetVideoPaletteEntries(long StartIndex,
  1151.                                                        long Entries,
  1152.                                                        long *pRetrieved,
  1153.                                                        long *pPalette) {
  1154.     CheckPointer(pRetrieved,E_POINTER);
  1155.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1156.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1157.     CMediaType MediaType;
  1158.  
  1159.     // Get the video format from the derived class
  1160.  
  1161.     VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
  1162.     if(pVideoInfo == NULL)
  1163.         return E_OUTOFMEMORY;
  1164.     BITMAPINFOHEADER *pHeader = HEADER(pVideoInfo);
  1165.  
  1166.     // Is the current format palettised
  1167.  
  1168.     if(PALETTISED(pVideoInfo) == FALSE) {
  1169.         *pRetrieved = 0;
  1170.         return VFW_E_NO_PALETTE_AVAILABLE;
  1171.     }
  1172.  
  1173.     // Do they just want to know how many are available
  1174.  
  1175.     if(pPalette == NULL) {
  1176.         *pRetrieved = pHeader->biClrUsed;
  1177.         return NOERROR;
  1178.     }
  1179.  
  1180.     // Make sure the start position is a valid offset
  1181.  
  1182.     if(StartIndex >= (LONG) pHeader->biClrUsed || StartIndex < 0) {
  1183.         *pRetrieved = 0;
  1184.         return E_INVALIDARG;
  1185.     }
  1186.  
  1187.     // Correct the number we can retrieve
  1188.  
  1189.     LONG Available = (LONG) pHeader->biClrUsed - StartIndex;
  1190.     *pRetrieved = max(0,min(Available,Entries));
  1191.     if(*pRetrieved == 0) {
  1192.         return S_FALSE;
  1193.     }
  1194.  
  1195.     // Copy the palette entries to the output buffer
  1196.  
  1197.     PALETTEENTRY *pEntries = (PALETTEENTRY *) pPalette;
  1198.     RGBQUAD *pColours = COLORS(pVideoInfo) + StartIndex;
  1199.  
  1200.     for(LONG Count = 0;Count < *pRetrieved;Count++) {
  1201.         pEntries[Count].peRed = pColours[Count].rgbRed;
  1202.         pEntries[Count].peGreen = pColours[Count].rgbGreen;
  1203.         pEntries[Count].peBlue = pColours[Count].rgbBlue;
  1204.         pEntries[Count].peFlags = 0;
  1205.     }
  1206.     return NOERROR;
  1207. }
  1208.  
  1209.  
  1210. // This returns the current video dimensions as a method rather than a number
  1211. // of individual property get calls. For the same reasons as said before we
  1212. // cannot access the renderer media type directly as the window object thread
  1213. // may be updating it since dynamic format changes may change these values
  1214.  
  1215. STDMETHODIMP CBaseControlVideo::GetVideoSize(long *pWidth,long *pHeight) {
  1216.     CheckPointer(pWidth,E_POINTER);
  1217.     CheckPointer(pHeight,E_POINTER);
  1218.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1219.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1220.  
  1221.     // Get the video format from the derived class
  1222.     VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
  1223.     if(pVideoInfo == NULL)
  1224.         return E_OUTOFMEMORY;
  1225.     *pWidth = pVideoInfo->bmiHeader.biWidth;
  1226.     *pHeight = pVideoInfo->bmiHeader.biHeight;
  1227.     return NOERROR;
  1228. }
  1229.  
  1230.  
  1231. // Set the source video rectangle as left,top,right and bottom coordinates
  1232. // rather than left,top,width and height as per OLE automation interfaces
  1233. // Then pass the rectangle on to the window object to set the source
  1234.  
  1235. STDMETHODIMP
  1236. CBaseControlVideo::SetSourcePosition(long Left,long Top,long Width,long Height) {
  1237.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1238.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1239.     RECT SourceRect;
  1240.     SourceRect.left = Left;
  1241.     SourceRect.top = Top;
  1242.     SourceRect.right = Left + Width;
  1243.     SourceRect.bottom = Top + Height;
  1244.  
  1245.     // Check the source rectangle is valid
  1246.  
  1247.     HRESULT hr = CheckSourceRect(&SourceRect);
  1248.     if(FAILED(hr)) {
  1249.         return hr;
  1250.     }
  1251.  
  1252.     // Now set the source rectangle
  1253.  
  1254.     hr = SetSourceRect(&SourceRect);
  1255.     if(FAILED(hr)) {
  1256.         return hr;
  1257.     }
  1258.     return OnUpdateRectangles();
  1259. }
  1260.  
  1261.  
  1262. // Return the source rectangle in left,top,width and height rather than the
  1263. // left,top,right and bottom values that RECT uses (and which the window
  1264. // object returns through GetSourceRect) which requires a little work
  1265.  
  1266. STDMETHODIMP
  1267. CBaseControlVideo::GetSourcePosition(long *pLeft,long *pTop,long *pWidth,long *pHeight) {
  1268.     // Should check the pointers are non NULL
  1269.  
  1270.     CheckPointer(pLeft,E_POINTER);
  1271.     CheckPointer(pTop,E_POINTER);
  1272.     CheckPointer(pWidth,E_POINTER);
  1273.     CheckPointer(pHeight,E_POINTER);
  1274.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1275.     RECT SourceRect;
  1276.  
  1277.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1278.     GetSourceRect(&SourceRect);
  1279.  
  1280.     *pLeft = SourceRect.left;
  1281.     *pTop = SourceRect.top;
  1282.     *pWidth = WIDTH(&SourceRect);
  1283.     *pHeight = HEIGHT(&SourceRect);
  1284.  
  1285.     return NOERROR;
  1286. }
  1287.  
  1288.  
  1289. // Set the video destination as left,top,right and bottom coordinates rather
  1290. // than the left,top,width and height uses as per OLE automation interfaces
  1291. // Then pass the rectangle on to the window object to set the destination
  1292.  
  1293. STDMETHODIMP
  1294. CBaseControlVideo::SetDestinationPosition(long Left,long Top,long Width,long Height) {
  1295.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1296.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1297.     RECT DestinationRect;
  1298.  
  1299.     DestinationRect.left = Left;
  1300.     DestinationRect.top = Top;
  1301.     DestinationRect.right = Left + Width;
  1302.     DestinationRect.bottom = Top + Height;
  1303.  
  1304.     // Check the target rectangle is valid
  1305.  
  1306.     HRESULT hr = CheckTargetRect(&DestinationRect);
  1307.     if(FAILED(hr)) {
  1308.         return hr;
  1309.     }
  1310.  
  1311.     // Now set the new target rectangle
  1312.  
  1313.     hr = SetTargetRect(&DestinationRect);
  1314.     if(FAILED(hr)) {
  1315.         return hr;
  1316.     }
  1317.     return OnUpdateRectangles();
  1318. }
  1319.  
  1320.  
  1321. // Return the destination rectangle in left,top,width and height rather than
  1322. // the left,top,right and bottom values that RECT uses (and which the window
  1323. // object returns through GetDestinationRect) which requires a little work
  1324.  
  1325. STDMETHODIMP
  1326. CBaseControlVideo::GetDestinationPosition(long *pLeft,long *pTop,long *pWidth,long *pHeight) {
  1327.     // Should check the pointers are not NULL
  1328.  
  1329.     CheckPointer(pLeft,E_POINTER);
  1330.     CheckPointer(pTop,E_POINTER);
  1331.     CheckPointer(pWidth,E_POINTER);
  1332.     CheckPointer(pHeight,E_POINTER);
  1333.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1334.     RECT DestinationRect;
  1335.  
  1336.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1337.     GetTargetRect(&DestinationRect);
  1338.  
  1339.     *pLeft = DestinationRect.left;
  1340.     *pTop = DestinationRect.top;
  1341.     *pWidth = WIDTH(&DestinationRect);
  1342.     *pHeight = HEIGHT(&DestinationRect);
  1343.  
  1344.     return NOERROR;
  1345. }
  1346.  
  1347.  
  1348. // Set the source left position, the source rectangle we get back from the
  1349. // window object is a true rectangle in left,top,right and bottom positions
  1350. // so all we have to do is to update the left position and pass it back. We
  1351. // must keep the current width constant when we're updating this property
  1352.  
  1353. STDMETHODIMP CBaseControlVideo::put_SourceLeft(long SourceLeft) {
  1354.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1355.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1356.     RECT SourceRect;
  1357.     GetSourceRect(&SourceRect);
  1358.     SourceRect.right = SourceLeft + WIDTH(&SourceRect);
  1359.     SourceRect.left = SourceLeft;
  1360.  
  1361.     // Check the source rectangle is valid
  1362.  
  1363.     HRESULT hr = CheckSourceRect(&SourceRect);
  1364.     if(FAILED(hr)) {
  1365.         return hr;
  1366.     }
  1367.  
  1368.     // Now set the source rectangle
  1369.  
  1370.     hr = SetSourceRect(&SourceRect);
  1371.     if(FAILED(hr)) {
  1372.         return hr;
  1373.     }
  1374.     return OnUpdateRectangles();
  1375. }
  1376.  
  1377.  
  1378. // Return the current left source video position
  1379.  
  1380. STDMETHODIMP CBaseControlVideo::get_SourceLeft(long *pSourceLeft) {
  1381.     CheckPointer(pSourceLeft,E_POINTER);
  1382.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1383.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1384.     RECT SourceRect;
  1385.  
  1386.     GetSourceRect(&SourceRect);
  1387.     *pSourceLeft = SourceRect.left;
  1388.     return NOERROR;
  1389. }
  1390.  
  1391.  
  1392. // Set the source width, we get the current source rectangle and then update
  1393. // the right position to be the left position (thereby keeping it constant)
  1394. // plus the new source width we are passed in (it expands to the right)
  1395.  
  1396. STDMETHODIMP CBaseControlVideo::put_SourceWidth(long SourceWidth) {
  1397.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1398.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1399.     RECT SourceRect;
  1400.     GetSourceRect(&SourceRect);
  1401.     SourceRect.right = SourceRect.left + SourceWidth;
  1402.  
  1403.     // Check the source rectangle is valid
  1404.  
  1405.     HRESULT hr = CheckSourceRect(&SourceRect);
  1406.     if(FAILED(hr)) {
  1407.         return hr;
  1408.     }
  1409.  
  1410.     // Now set the source rectangle
  1411.  
  1412.     hr = SetSourceRect(&SourceRect);
  1413.     if(FAILED(hr)) {
  1414.         return hr;
  1415.     }
  1416.     return OnUpdateRectangles();
  1417. }
  1418.  
  1419.  
  1420. // Return the current source width
  1421.  
  1422. STDMETHODIMP CBaseControlVideo::get_SourceWidth(long *pSourceWidth) {
  1423.     CheckPointer(pSourceWidth,E_POINTER);
  1424.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1425.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1426.     RECT SourceRect;
  1427.  
  1428.     GetSourceRect(&SourceRect);
  1429.     *pSourceWidth = WIDTH(&SourceRect);
  1430.     return NOERROR;
  1431. }
  1432.  
  1433.  
  1434. // Set the source top position - changing this property does not affect the
  1435. // current source height. So changing this shunts the source rectangle up and
  1436. // down appropriately. Changing the height complements this functionality by
  1437. // keeping the top position constant and simply changing the source height
  1438.  
  1439. STDMETHODIMP CBaseControlVideo::put_SourceTop(long SourceTop) {
  1440.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1441.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1442.     RECT SourceRect;
  1443.     GetSourceRect(&SourceRect);
  1444.     SourceRect.bottom = SourceTop + HEIGHT(&SourceRect);
  1445.     SourceRect.top = SourceTop;
  1446.  
  1447.     // Check the source rectangle is valid
  1448.  
  1449.     HRESULT hr = CheckSourceRect(&SourceRect);
  1450.     if(FAILED(hr)) {
  1451.         return hr;
  1452.     }
  1453.  
  1454.     // Now set the source rectangle
  1455.  
  1456.     hr = SetSourceRect(&SourceRect);
  1457.     if(FAILED(hr)) {
  1458.         return hr;
  1459.     }
  1460.     return OnUpdateRectangles();
  1461. }
  1462.  
  1463.  
  1464. // Return the current top position
  1465.  
  1466. STDMETHODIMP CBaseControlVideo::get_SourceTop(long *pSourceTop) {
  1467.     CheckPointer(pSourceTop,E_POINTER);
  1468.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1469.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1470.     RECT SourceRect;
  1471.  
  1472.     GetSourceRect(&SourceRect);
  1473.     *pSourceTop = SourceRect.top;
  1474.     return NOERROR;
  1475. }
  1476.  
  1477.  
  1478. // Set the source height
  1479.  
  1480. STDMETHODIMP CBaseControlVideo::put_SourceHeight(long SourceHeight) {
  1481.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1482.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1483.     RECT SourceRect;
  1484.     GetSourceRect(&SourceRect);
  1485.     SourceRect.bottom = SourceRect.top + SourceHeight;
  1486.  
  1487.     // Check the source rectangle is valid
  1488.  
  1489.     HRESULT hr = CheckSourceRect(&SourceRect);
  1490.     if(FAILED(hr)) {
  1491.         return hr;
  1492.     }
  1493.  
  1494.     // Now set the source rectangle
  1495.  
  1496.     hr = SetSourceRect(&SourceRect);
  1497.     if(FAILED(hr)) {
  1498.         return hr;
  1499.     }
  1500.     return OnUpdateRectangles();
  1501. }
  1502.  
  1503.  
  1504. // Return the current source height
  1505.  
  1506. STDMETHODIMP CBaseControlVideo::get_SourceHeight(long *pSourceHeight) {
  1507.     CheckPointer(pSourceHeight,E_POINTER);
  1508.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1509.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1510.     RECT SourceRect;
  1511.  
  1512.     GetSourceRect(&SourceRect);
  1513.     *pSourceHeight = HEIGHT(&SourceRect);
  1514.     return NOERROR;
  1515. }
  1516.  
  1517.  
  1518. // Set the target left position, the target rectangle we get back from the
  1519. // window object is a true rectangle in left,top,right and bottom positions
  1520. // so all we have to do is to update the left position and pass it back. We
  1521. // must keep the current width constant when we're updating this property
  1522.  
  1523. STDMETHODIMP CBaseControlVideo::put_DestinationLeft(long DestinationLeft) {
  1524.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1525.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1526.     RECT DestinationRect;
  1527.     GetTargetRect(&DestinationRect);
  1528.     DestinationRect.right = DestinationLeft + WIDTH(&DestinationRect);
  1529.     DestinationRect.left = DestinationLeft;
  1530.  
  1531.     // Check the target rectangle is valid
  1532.  
  1533.     HRESULT hr = CheckTargetRect(&DestinationRect);
  1534.     if(FAILED(hr)) {
  1535.         return hr;
  1536.     }
  1537.  
  1538.     // Now set the new target rectangle
  1539.  
  1540.     hr = SetTargetRect(&DestinationRect);
  1541.     if(FAILED(hr)) {
  1542.         return hr;
  1543.     }
  1544.     return OnUpdateRectangles();
  1545. }
  1546.  
  1547.  
  1548. // Return the left position for the destination rectangle
  1549.  
  1550. STDMETHODIMP CBaseControlVideo::get_DestinationLeft(long *pDestinationLeft) {
  1551.     CheckPointer(pDestinationLeft,E_POINTER);
  1552.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1553.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1554.     RECT DestinationRect;
  1555.  
  1556.     GetTargetRect(&DestinationRect);
  1557.     *pDestinationLeft = DestinationRect.left;
  1558.     return NOERROR;
  1559. }
  1560.  
  1561.  
  1562. // Set the destination width
  1563.  
  1564. STDMETHODIMP CBaseControlVideo::put_DestinationWidth(long DestinationWidth) {
  1565.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1566.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1567.     RECT DestinationRect;
  1568.     GetTargetRect(&DestinationRect);
  1569.     DestinationRect.right = DestinationRect.left + DestinationWidth;
  1570.  
  1571.     // Check the target rectangle is valid
  1572.  
  1573.     HRESULT hr = CheckTargetRect(&DestinationRect);
  1574.     if(FAILED(hr)) {
  1575.         return hr;
  1576.     }
  1577.  
  1578.     // Now set the new target rectangle
  1579.  
  1580.     hr = SetTargetRect(&DestinationRect);
  1581.     if(FAILED(hr)) {
  1582.         return hr;
  1583.     }
  1584.     return OnUpdateRectangles();
  1585. }
  1586.  
  1587.  
  1588. // Return the width for the destination rectangle
  1589.  
  1590. STDMETHODIMP CBaseControlVideo::get_DestinationWidth(long *pDestinationWidth) {
  1591.     CheckPointer(pDestinationWidth,E_POINTER);
  1592.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1593.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1594.     RECT DestinationRect;
  1595.  
  1596.     GetTargetRect(&DestinationRect);
  1597.     *pDestinationWidth = WIDTH(&DestinationRect);
  1598.     return NOERROR;
  1599. }
  1600.  
  1601.  
  1602. // Set the target top position - changing this property does not affect the
  1603. // current target height. So changing this shunts the target rectangle up and
  1604. // down appropriately. Changing the height complements this functionality by
  1605. // keeping the top position constant and simply changing the target height
  1606.  
  1607. STDMETHODIMP CBaseControlVideo::put_DestinationTop(long DestinationTop) {
  1608.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1609.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1610.     RECT DestinationRect;
  1611.     GetTargetRect(&DestinationRect);
  1612.     DestinationRect.bottom = DestinationTop + HEIGHT(&DestinationRect);
  1613.     DestinationRect.top = DestinationTop;
  1614.  
  1615.     // Check the target rectangle is valid
  1616.  
  1617.     HRESULT hr = CheckTargetRect(&DestinationRect);
  1618.     if(FAILED(hr)) {
  1619.         return hr;
  1620.     }
  1621.  
  1622.     // Now set the new target rectangle
  1623.  
  1624.     hr = SetTargetRect(&DestinationRect);
  1625.     if(FAILED(hr)) {
  1626.         return hr;
  1627.     }
  1628.     return OnUpdateRectangles();
  1629. }
  1630.  
  1631.  
  1632. // Return the top position for the destination rectangle
  1633.  
  1634. STDMETHODIMP CBaseControlVideo::get_DestinationTop(long *pDestinationTop) {
  1635.     CheckPointer(pDestinationTop,E_POINTER);
  1636.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1637.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1638.     RECT DestinationRect;
  1639.  
  1640.     GetTargetRect(&DestinationRect);
  1641.     *pDestinationTop = DestinationRect.top;
  1642.     return NOERROR;
  1643. }
  1644.  
  1645.  
  1646. // Set the destination height
  1647.  
  1648. STDMETHODIMP CBaseControlVideo::put_DestinationHeight(long DestinationHeight) {
  1649.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1650.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1651.     RECT DestinationRect;
  1652.     GetTargetRect(&DestinationRect);
  1653.     DestinationRect.bottom = DestinationRect.top + DestinationHeight;
  1654.  
  1655.     // Check the target rectangle is valid
  1656.  
  1657.     HRESULT hr = CheckTargetRect(&DestinationRect);
  1658.     if(FAILED(hr)) {
  1659.         return hr;
  1660.     }
  1661.  
  1662.     // Now set the new target rectangle
  1663.  
  1664.     hr = SetTargetRect(&DestinationRect);
  1665.     if(FAILED(hr)) {
  1666.         return hr;
  1667.     }
  1668.     return OnUpdateRectangles();
  1669. }
  1670.  
  1671.  
  1672. // Return the height for the destination rectangle
  1673.  
  1674. STDMETHODIMP CBaseControlVideo::get_DestinationHeight(long *pDestinationHeight) {
  1675.     CheckPointer(pDestinationHeight,E_POINTER);
  1676.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1677.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1678.     RECT DestinationRect;
  1679.  
  1680.     GetTargetRect(&DestinationRect);
  1681.     *pDestinationHeight = HEIGHT(&DestinationRect);
  1682.     return NOERROR;
  1683. }
  1684.  
  1685.  
  1686. // Reset the source rectangle to the full video dimensions
  1687.  
  1688. STDMETHODIMP CBaseControlVideo::SetDefaultSourcePosition() {
  1689.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1690.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1691.     HRESULT hr = SetDefaultSourceRect();
  1692.     if(FAILED(hr)) {
  1693.         return hr;
  1694.     }
  1695.     return OnUpdateRectangles();
  1696. }
  1697.  
  1698.  
  1699. // Return S_OK if we're using the default source otherwise S_FALSE
  1700.  
  1701. STDMETHODIMP CBaseControlVideo::IsUsingDefaultSource() {
  1702.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1703.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1704.     return IsDefaultSourceRect();
  1705. }
  1706.  
  1707.  
  1708. // Reset the video renderer to use the entire playback area
  1709.  
  1710. STDMETHODIMP CBaseControlVideo::SetDefaultDestinationPosition() {
  1711.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1712.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1713.     HRESULT hr = SetDefaultTargetRect();
  1714.     if(FAILED(hr)) {
  1715.         return hr;
  1716.     }
  1717.     return OnUpdateRectangles();
  1718. }
  1719.  
  1720.  
  1721. // Return S_OK if we're using the default target otherwise S_FALSE
  1722.  
  1723. STDMETHODIMP CBaseControlVideo::IsUsingDefaultDestination() {
  1724.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1725.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1726.     return IsDefaultTargetRect();
  1727. }
  1728.  
  1729.  
  1730. // Return a copy of the current image in the video renderer
  1731.  
  1732. STDMETHODIMP
  1733. CBaseControlVideo::GetCurrentImage(long *pBufferSize,long *pVideoImage) {
  1734.     CheckPointer(pBufferSize,E_POINTER);
  1735.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1736.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1737.     FILTER_STATE State;
  1738.  
  1739.     // Make sure we are in a paused state
  1740.  
  1741.     if(pVideoImage != NULL) {
  1742.         m_pFilter->GetState(0,&State);
  1743.         if(State != State_Paused) {
  1744.             return VFW_E_NOT_PAUSED;
  1745.         }
  1746.         return GetStaticImage(pBufferSize,pVideoImage);
  1747.     }
  1748.  
  1749.     // Just return the memory required
  1750.  
  1751.     VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
  1752.     if(pVideoInfo == NULL)
  1753.         return E_OUTOFMEMORY;
  1754.     RECT SourceRect;
  1755.     GetSourceRect(&SourceRect);
  1756.     return GetImageSize(pVideoInfo,pBufferSize,&SourceRect);
  1757. }
  1758.  
  1759.  
  1760. // An application has two ways of using GetCurrentImage, one is to pass a real
  1761. // buffer which should be filled with the current image. The other is to pass
  1762. // a NULL buffer pointer which is interpreted as asking us to return how much
  1763. // memory is required for the image. The constraints for when the latter can
  1764. // be called are much looser. To calculate the memory required we synthesize
  1765. // a VIDEOINFO that takes into account the source rectangle that's being used
  1766.  
  1767. HRESULT CBaseControlVideo::GetImageSize(VIDEOINFOHEADER *pVideoInfo,
  1768.                                         LONG *pBufferSize,
  1769.                                         RECT *pSourceRect) {
  1770.     NOTE("Entering GetImageSize");
  1771.     ASSERT(pSourceRect);
  1772.  
  1773.     // Check we have the correct input parameters
  1774.  
  1775.     if(pSourceRect == NULL ||
  1776.         pVideoInfo == NULL ||
  1777.         pBufferSize == NULL) {
  1778.  
  1779.         return E_UNEXPECTED;
  1780.     }
  1781.  
  1782.     // Is the data format compatible
  1783.  
  1784.     if(pVideoInfo->bmiHeader.biCompression != BI_RGB) {
  1785.         if(pVideoInfo->bmiHeader.biCompression != BI_BITFIELDS) {
  1786.             return E_INVALIDARG;
  1787.         }
  1788.     }
  1789.  
  1790.     ASSERT(IsRectEmpty(pSourceRect) == FALSE);
  1791.  
  1792.     BITMAPINFOHEADER bih;
  1793.     bih.biWidth = WIDTH(pSourceRect);
  1794.     bih.biHeight = HEIGHT(pSourceRect);
  1795.     bih.biBitCount = pVideoInfo->bmiHeader.biBitCount;
  1796.     LONG Size = DIBSIZE(bih);
  1797.     Size += GetBitmapFormatSize(HEADER(pVideoInfo)) - SIZE_PREHEADER;
  1798.     *pBufferSize = Size;
  1799.  
  1800.     return NOERROR;
  1801. }
  1802.  
  1803.  
  1804. // Given an IMediaSample containing a linear buffer with an image and a type
  1805. // describing the bitmap make a rendering of the image into the output buffer
  1806. // This may be called by derived classes who render typical video images to
  1807. // handle the IBasicVideo GetCurrentImage method. The pVideoImage pointer may
  1808. // be NULL when passed to GetCurrentImage in which case GetImageSize will be
  1809. // called instead, which will just do the calculation of the memory required
  1810.  
  1811. HRESULT CBaseControlVideo::CopyImage(IMediaSample *pMediaSample,
  1812.                                      VIDEOINFOHEADER *pVideoInfo,
  1813.                                      LONG *pBufferSize,
  1814.                                      BYTE *pVideoImage,
  1815.                                      RECT *pSourceRect) {
  1816.     NOTE("Entering CopyImage");
  1817.     ASSERT(pSourceRect);
  1818.     BYTE *pCurrentImage;
  1819.  
  1820.     // Check we have an image to copy
  1821.  
  1822.     if(pMediaSample == NULL || pSourceRect == NULL ||
  1823.         pVideoInfo == NULL || pVideoImage == NULL ||
  1824.         pBufferSize == NULL) {
  1825.  
  1826.         return E_UNEXPECTED;
  1827.     }
  1828.  
  1829.     // Is the data format compatible
  1830.  
  1831.     if(pVideoInfo->bmiHeader.biCompression != BI_RGB) {
  1832.         if(pVideoInfo->bmiHeader.biCompression != BI_BITFIELDS) {
  1833.             return E_INVALIDARG;
  1834.         }
  1835.     }
  1836.  
  1837.     ASSERT(IsRectEmpty(pSourceRect) == FALSE);
  1838.  
  1839.     BITMAPINFOHEADER bih;
  1840.     bih.biWidth = WIDTH(pSourceRect);
  1841.     bih.biHeight = HEIGHT(pSourceRect);
  1842.     bih.biBitCount = pVideoInfo->bmiHeader.biBitCount;
  1843.     LONG Size = GetBitmapFormatSize(HEADER(pVideoInfo)) - SIZE_PREHEADER;
  1844.     LONG Total = Size + DIBSIZE(bih);
  1845.  
  1846.     // Make sure we have a large enough buffer
  1847.  
  1848.     if(*pBufferSize < Total) {
  1849.         return E_OUTOFMEMORY;
  1850.     }
  1851.  
  1852.     // Copy the BITMAPINFO
  1853.  
  1854.     CopyMemory((PVOID)pVideoImage, (PVOID)&pVideoInfo->bmiHeader, Size);
  1855.     ((BITMAPINFOHEADER *)pVideoImage)->biWidth = WIDTH(pSourceRect);
  1856.     ((BITMAPINFOHEADER *)pVideoImage)->biHeight = HEIGHT(pSourceRect);
  1857.     ((BITMAPINFOHEADER *)pVideoImage)->biSizeImage = DIBSIZE(bih);
  1858.     BYTE *pImageData = pVideoImage + Size;
  1859.  
  1860.     // Get the pointer to it's image data
  1861.  
  1862.     HRESULT hr = pMediaSample->GetPointer(&pCurrentImage);
  1863.     if(FAILED(hr)) {
  1864.         return hr;
  1865.     }
  1866.  
  1867.     // Now we are ready to start copying the source scan lines
  1868.  
  1869.     LONG ScanLine = (pVideoInfo->bmiHeader.biBitCount / 8) * WIDTH(pSourceRect);
  1870.     LONG LinesToSkip = pVideoInfo->bmiHeader.biHeight;
  1871.     LinesToSkip -= pSourceRect->top + HEIGHT(pSourceRect);
  1872.     pCurrentImage += LinesToSkip * DIBWIDTHBYTES(pVideoInfo->bmiHeader);
  1873.     pCurrentImage += pSourceRect->left * (pVideoInfo->bmiHeader.biBitCount / 8);
  1874.  
  1875.     // Even money on this GP faulting sometime...
  1876.  
  1877.     for(LONG Line = 0;Line < HEIGHT(pSourceRect);Line++) {
  1878.         CopyMemory((PVOID)pImageData, (PVOID)pCurrentImage, ScanLine);
  1879.         pImageData += DIBWIDTHBYTES(*(BITMAPINFOHEADER *)pVideoImage);
  1880.         pCurrentImage += DIBWIDTHBYTES(pVideoInfo->bmiHeader);
  1881.     }
  1882.     return NOERROR;
  1883. }
  1884.  
  1885.  
  1886. // Called when we change media types either during connection or dynamically
  1887. // We inform the filter graph and therefore the application that the video
  1888. // size may have changed, we don't bother looking to see if it really has as
  1889. // we leave that to the application - the dimensions are the event parameters
  1890.  
  1891. HRESULT CBaseControlVideo::OnVideoSizeChange() {
  1892.     // Get the video format from the derived class
  1893.  
  1894.     VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
  1895.     if(pVideoInfo == NULL)
  1896.         return E_OUTOFMEMORY;
  1897.     WORD Width = (WORD) pVideoInfo->bmiHeader.biWidth;
  1898.     WORD Height = (WORD) pVideoInfo->bmiHeader.biHeight;
  1899.  
  1900.     return m_pFilter->NotifyEvent(EC_VIDEO_SIZE_CHANGED,
  1901.         MAKELPARAM(Width,Height),
  1902.         MAKEWPARAM(0,0));
  1903. }
  1904.  
  1905.  
  1906. // Set the video source rectangle. We must check the source rectangle against
  1907. // the actual video dimensions otherwise when we come to draw the pictures we
  1908. // get access violations as GDI tries to touch data outside of the image data
  1909. // Although we store the rectangle in left, top, right and bottom coordinates
  1910. // instead of left, top, width and height as OLE uses we do take into account
  1911. // that the rectangle is used up to, but not including, the right column and
  1912. // bottom row of pixels, see the Win32 documentation on RECT for more details
  1913.  
  1914. HRESULT CBaseControlVideo::CheckSourceRect(RECT *pSourceRect) {
  1915.     CheckPointer(pSourceRect,E_POINTER);
  1916.     LONG Width,Height;
  1917.     GetVideoSize(&Width,&Height);
  1918.  
  1919.     // Check the coordinates are greater than zero
  1920.     // and that the rectangle is valid (left<right, top<bottom)
  1921.  
  1922.     if((pSourceRect->left >= pSourceRect->right) ||
  1923.         (pSourceRect->left < 0) ||
  1924.         (pSourceRect->top >= pSourceRect->bottom) ||
  1925.         (pSourceRect->top < 0)) {
  1926.  
  1927.         return E_INVALIDARG;
  1928.     }
  1929.  
  1930.     // Check the coordinates are less than the extents
  1931.  
  1932.     if((pSourceRect->right > Width) ||
  1933.         (pSourceRect->bottom > Height)) {
  1934.  
  1935.         return E_INVALIDARG;
  1936.     }
  1937.     return NOERROR;
  1938. }
  1939.  
  1940.  
  1941. // Check the target rectangle has some valid coordinates, which amounts to
  1942. // little more than checking the destination rectangle isn't empty. Derived
  1943. // classes may call this when they have their SetTargetRect method called to
  1944. // check the rectangle validity, we do not update the rectangles passed in
  1945. // Although we store the rectangle in left, top, right and bottom coordinates
  1946. // instead of left, top, width and height as OLE uses we do take into account
  1947. // that the rectangle is used up to, but not including, the right column and
  1948. // bottom row of pixels, see the Win32 documentation on RECT for more details
  1949.  
  1950. HRESULT CBaseControlVideo::CheckTargetRect(RECT *pTargetRect) {
  1951.     // Check the pointer is valid
  1952.  
  1953.     if(pTargetRect == NULL) {
  1954.         return E_POINTER;
  1955.     }
  1956.  
  1957.     // These overflow the WIDTH and HEIGHT checks
  1958.  
  1959.     if(pTargetRect->left > pTargetRect->right ||
  1960.         pTargetRect->top > pTargetRect->bottom) {
  1961.         return E_INVALIDARG;
  1962.     }
  1963.  
  1964.     // Check the rectangle has valid coordinates
  1965.  
  1966.     if(WIDTH(pTargetRect) <= 0 || HEIGHT(pTargetRect) <= 0) {
  1967.         return E_INVALIDARG;
  1968.     }
  1969.  
  1970.     ASSERT(IsRectEmpty(pTargetRect) == FALSE);
  1971.     return NOERROR;
  1972. }
  1973.  
  1974.  
  1975.