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