test2
[test_vsfilter.git] / src / filters / BaseClasses / winctrl.cpp
blob6bf72aaac0212d342782338a89debe6a895b03d0
1 //------------------------------------------------------------------------------
2 // File: WinCtrl.cpp
3 //
4 // Desc: DirectShow base classes - implements video control interface class.
5 //
6 // Copyright (c) 1992-2002 Microsoft Corporation. All rights reserved.
7 //------------------------------------------------------------------------------
10 #include <streams.h>
12 // The control interface methods require us to be connected
14 #define CheckConnected(pin,code) \
15 { \
16 if (pin == NULL) { \
17 ASSERT(!TEXT("Pin not set")); \
18 } else if (pin->IsConnected() == FALSE) { \
19 return (code); \
20 } \
23 // This checks to see whether the window has a drain. An application can in
24 // most environments set the owner/parent of windows so that they appear in
25 // a compound document context (for example). In this case, the application
26 // would probably like to be told of any keyboard/mouse messages. Therefore
27 // we pass these messages on untranslated, returning TRUE if we're successful
29 BOOL WINAPI PossiblyEatMessage(HWND hwndDrain, UINT uMsg, WPARAM wParam, LPARAM lParam)
31 if (hwndDrain != NULL && !InSendMessage())
33 switch (uMsg)
35 case WM_CHAR:
36 case WM_DEADCHAR:
37 case WM_KEYDOWN:
38 case WM_KEYUP:
39 case WM_LBUTTONDBLCLK:
40 case WM_LBUTTONDOWN:
41 case WM_LBUTTONUP:
42 case WM_MBUTTONDBLCLK:
43 case WM_MBUTTONDOWN:
44 case WM_MBUTTONUP:
45 case WM_MOUSEACTIVATE:
46 case WM_MOUSEMOVE:
47 // If we pass this on we don't get any mouse clicks
48 //case WM_NCHITTEST:
49 case WM_NCLBUTTONDBLCLK:
50 case WM_NCLBUTTONDOWN:
51 case WM_NCLBUTTONUP:
52 case WM_NCMBUTTONDBLCLK:
53 case WM_NCMBUTTONDOWN:
54 case WM_NCMBUTTONUP:
55 case WM_NCMOUSEMOVE:
56 case WM_NCRBUTTONDBLCLK:
57 case WM_NCRBUTTONDOWN:
58 case WM_NCRBUTTONUP:
59 case WM_RBUTTONDBLCLK:
60 case WM_RBUTTONDOWN:
61 case WM_RBUTTONUP:
62 case WM_SYSCHAR:
63 case WM_SYSDEADCHAR:
64 case WM_SYSKEYDOWN:
65 case WM_SYSKEYUP:
67 DbgLog((LOG_TRACE, 2, TEXT("Forwarding %x to drain")));
68 PostMessage(hwndDrain, uMsg, wParam, lParam);
70 return TRUE;
73 return FALSE;
77 // This class implements the IVideoWindow control functions (dual interface)
78 // we support a large number of properties and methods designed to allow the
79 // client (whether it be an automation controller or a C/C++ application) to
80 // set and get a number of window related properties such as it's position.
81 // We also support some methods that duplicate the properties but provide a
82 // more direct and efficient mechanism as many values may be changed in one
84 CBaseControlWindow::CBaseControlWindow(
85 CBaseFilter *pFilter, // Owning filter
86 CCritSec *pInterfaceLock, // Locking object
87 TCHAR *pName, // Object description
88 LPUNKNOWN pUnk, // Normal COM ownership
89 HRESULT *phr) : // OLE return code
91 CBaseVideoWindow(pName,pUnk),
92 m_pInterfaceLock(pInterfaceLock),
93 m_hwndOwner(NULL),
94 m_hwndDrain(NULL),
95 m_bAutoShow(TRUE),
96 m_pFilter(pFilter),
97 m_bCursorHidden(FALSE),
98 m_pPin(NULL)
100 ASSERT(m_pFilter);
101 ASSERT(m_pInterfaceLock);
102 ASSERT(phr);
103 m_BorderColour = VIDEO_COLOUR;
107 // Set the title caption on the base window, we don't do any field checking
108 // as we really don't care what title they intend to have. We can always get
109 // it back again later with GetWindowText. The only other complication is to
110 // do the necessary string conversions between ANSI and OLE Unicode strings
112 STDMETHODIMP CBaseControlWindow::put_Caption(BSTR strCaption)
114 CheckPointer(strCaption,E_POINTER);
115 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
116 #ifdef UNICODE
117 SetWindowText(m_hwnd, strCaption);
118 #else
119 CHAR Caption[CAPTION];
121 WideCharToMultiByte(CP_ACP,0,strCaption,-1,Caption,CAPTION,NULL,NULL);
122 SetWindowText(m_hwnd, Caption);
123 #endif
124 return NOERROR;
128 // Get the current base window title caption, once again we do no real field
129 // checking. We allocate a string for the window title to be filled in with
130 // which ensures the interface doesn't fiddle around with getting memory. A
131 // BSTR is a normal C string with the length at position (-1), we use the
132 // WriteBSTR helper function to create the caption to try and avoid OLE32
134 STDMETHODIMP CBaseControlWindow::get_Caption(BSTR *pstrCaption)
136 CheckPointer(pstrCaption,E_POINTER);
137 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
138 WCHAR WideCaption[CAPTION];
140 #ifdef UNICODE
141 GetWindowText(m_hwnd,WideCaption,CAPTION);
142 #else
143 // Convert the ASCII caption to a UNICODE string
145 TCHAR Caption[CAPTION];
146 GetWindowText(m_hwnd,Caption,CAPTION);
147 MultiByteToWideChar(CP_ACP,0,Caption,-1,WideCaption,CAPTION);
148 #endif
149 return WriteBSTR(pstrCaption,WideCaption);
153 // Set the window style using GWL_EXSTYLE
155 STDMETHODIMP CBaseControlWindow::put_WindowStyleEx(long WindowStyleEx)
157 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
159 // Should we be taking off WS_EX_TOPMOST
161 if (GetWindowLong(m_hwnd,GWL_EXSTYLE) & WS_EX_TOPMOST) {
162 if ((WindowStyleEx & WS_EX_TOPMOST) == 0) {
163 SendMessage(m_hwnd,m_ShowStageTop,(WPARAM) FALSE,(LPARAM) 0);
167 // Likewise should we be adding WS_EX_TOPMOST
169 if (WindowStyleEx & WS_EX_TOPMOST) {
170 SendMessage(m_hwnd,m_ShowStageTop,(WPARAM) TRUE,(LPARAM) 0);
171 WindowStyleEx &= (~WS_EX_TOPMOST);
172 if (WindowStyleEx == 0) return NOERROR;
174 return DoSetWindowStyle(WindowStyleEx,GWL_EXSTYLE);
178 // Gets the current GWL_EXSTYLE base window style
180 STDMETHODIMP CBaseControlWindow::get_WindowStyleEx(long *pWindowStyleEx)
182 CheckPointer(pWindowStyleEx,E_POINTER);
183 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
184 return DoGetWindowStyle(pWindowStyleEx,GWL_EXSTYLE);
188 // Set the window style using GWL_STYLE
190 STDMETHODIMP CBaseControlWindow::put_WindowStyle(long WindowStyle)
192 // These styles cannot be changed dynamically
194 if ((WindowStyle & WS_DISABLED) ||
195 (WindowStyle & WS_ICONIC) ||
196 (WindowStyle & WS_MAXIMIZE) ||
197 (WindowStyle & WS_MINIMIZE) ||
198 (WindowStyle & WS_HSCROLL) ||
199 (WindowStyle & WS_VSCROLL)) {
201 return E_INVALIDARG;
204 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
205 return DoSetWindowStyle(WindowStyle,GWL_STYLE);
209 // Get the current GWL_STYLE base window style
211 STDMETHODIMP CBaseControlWindow::get_WindowStyle(long *pWindowStyle)
213 CheckPointer(pWindowStyle,E_POINTER);
214 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
215 return DoGetWindowStyle(pWindowStyle,GWL_STYLE);
219 // Change the base window style or the extended styles depending on whether
220 // WindowLong is GWL_STYLE or GWL_EXSTYLE. We must call SetWindowPos to have
221 // the window displayed in it's new style after the change which is a little
222 // tricky if the window is not currently visible as we realise it offscreen.
223 // In most cases the client will call get_WindowStyle before they call this
224 // and then AND and OR in extra bit settings according to the requirements
226 HRESULT CBaseControlWindow::DoSetWindowStyle(long Style,long WindowLong)
228 RECT WindowRect;
230 // Get the window's visibility before setting the style
231 BOOL bVisible = IsWindowVisible(m_hwnd);
232 EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
234 // Set the new style flags for the window
235 SetWindowLong(m_hwnd,WindowLong,Style);
236 UINT WindowFlags = SWP_SHOWWINDOW | SWP_FRAMECHANGED | SWP_NOACTIVATE;
237 WindowFlags |= SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE;
239 // Show the window again in the current position
241 if (bVisible == TRUE) {
243 SetWindowPos(m_hwnd, // Base window handle
244 HWND_TOP, // Just a place holder
245 0,0,0,0, // Leave size and position
246 WindowFlags); // Just draw it again
248 return NOERROR;
251 // Move the window offscreen so the user doesn't see the changes
253 MoveWindow((HWND) m_hwnd, // Base window handle
254 GetSystemMetrics(SM_CXSCREEN), // Current desktop width
255 GetSystemMetrics(SM_CYSCREEN), // Likewise it's height
256 WIDTH(&WindowRect), // Use the same width
257 HEIGHT(&WindowRect), // Keep height same to
258 TRUE); // May as well repaint
260 // Now show the previously hidden window
262 SetWindowPos(m_hwnd, // Base window handle
263 HWND_TOP, // Just a place holder
264 0,0,0,0, // Leave size and position
265 WindowFlags); // Just draw it again
267 ShowWindow(m_hwnd,SW_HIDE);
269 if (GetParent(m_hwnd)) {
271 MapWindowPoints(HWND_DESKTOP, GetParent(m_hwnd), (LPPOINT)&WindowRect, 2);
274 MoveWindow((HWND) m_hwnd, // Base window handle
275 WindowRect.left, // Existing x coordinate
276 WindowRect.top, // Existing y coordinate
277 WIDTH(&WindowRect), // Use the same width
278 HEIGHT(&WindowRect), // Keep height same to
279 TRUE); // May as well repaint
281 return NOERROR;
285 // Get the current base window style (either GWL_STYLE or GWL_EXSTYLE)
287 HRESULT CBaseControlWindow::DoGetWindowStyle(long *pStyle,long WindowLong)
289 *pStyle = GetWindowLong(m_hwnd,WindowLong);
290 return NOERROR;
294 // Change the visibility of the base window, this takes the same parameters
295 // as the ShowWindow Win32 API does, so the client can have the window hidden
296 // or shown, minimised to an icon, or maximised to play in full screen mode
297 // We pass the request on to the base window to actually make the change
299 STDMETHODIMP CBaseControlWindow::put_WindowState(long WindowState)
301 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
302 DoShowWindow(WindowState);
303 return NOERROR;
307 // Get the current window state, this function returns a subset of the SW bit
308 // settings available in ShowWindow, if the window is visible then SW_SHOW is
309 // set, if it is hidden then the SW_HIDDEN is set, if it is either minimised
310 // or maximised then the SW_MINIMIZE or SW_MAXIMIZE is set respectively. The
311 // other SW bit settings are really set commands not readable output values
313 STDMETHODIMP CBaseControlWindow::get_WindowState(long *pWindowState)
315 CheckPointer(pWindowState,E_POINTER);
316 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
317 ASSERT(pWindowState);
318 *pWindowState = FALSE;
320 // Is the window visible, a window is termed visible if it is somewhere on
321 // the current desktop even if it is completely obscured by other windows
322 // so the flag is a style for each window set with the WS_VISIBLE bit
324 if (IsWindowVisible(m_hwnd) == TRUE) {
326 // Is the base window iconic
327 if (IsIconic(m_hwnd) == TRUE) {
328 *pWindowState |= SW_MINIMIZE;
331 // Has the window been maximised
332 else if (IsZoomed(m_hwnd) == TRUE) {
333 *pWindowState |= SW_MAXIMIZE;
336 // Window is normal
337 else {
338 *pWindowState |= SW_SHOW;
341 } else {
342 *pWindowState |= SW_HIDE;
344 return NOERROR;
348 // This makes sure that any palette we realise in the base window (through a
349 // media type or through the overlay interface) is done in the background and
350 // is therefore mapped to existing device entries rather than taking it over
351 // as it will do when we this window gets the keyboard focus. An application
352 // uses this to make sure it doesn't have it's palette removed by the window
354 STDMETHODIMP CBaseControlWindow::put_BackgroundPalette(long BackgroundPalette)
356 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
357 CAutoLock cWindowLock(&m_WindowLock);
359 // Check this is a valid automation boolean type
361 if (BackgroundPalette != OATRUE) {
362 if (BackgroundPalette != OAFALSE) {
363 return E_INVALIDARG;
367 // Make sure the window realises any palette it has again
369 m_bBackground = (BackgroundPalette == OATRUE ? TRUE : FALSE);
370 PostMessage(m_hwnd,m_RealizePalette,0,0);
371 PaintWindow(FALSE);
373 return NOERROR;
377 // This returns the current background realisation setting
379 STDMETHODIMP
380 CBaseControlWindow::get_BackgroundPalette(long *pBackgroundPalette)
382 CheckPointer(pBackgroundPalette,E_POINTER);
383 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
384 CAutoLock cWindowLock(&m_WindowLock);
386 // Get the current background palette setting
388 *pBackgroundPalette = (m_bBackground == TRUE ? OATRUE : OAFALSE);
389 return NOERROR;
393 // Change the visibility of the base window
395 STDMETHODIMP CBaseControlWindow::put_Visible(long Visible)
397 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
399 // Check this is a valid automation boolean type
401 if (Visible != OATRUE) {
402 if (Visible != OAFALSE) {
403 return E_INVALIDARG;
407 // Convert the boolean visibility into SW_SHOW and SW_HIDE
409 INT Mode = (Visible == OATRUE ? SW_SHOWNORMAL : SW_HIDE);
410 DoShowWindow(Mode);
411 return NOERROR;
415 // Return OATRUE if the window is currently visible otherwise OAFALSE
417 STDMETHODIMP CBaseControlWindow::get_Visible(long *pVisible)
419 CheckPointer(pVisible,E_POINTER);
420 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
422 // See if the base window has a WS_VISIBLE style - this will return TRUE
423 // even if the window is completely obscured by other desktop windows, we
424 // return FALSE if the window is not showing because of earlier calls
426 BOOL Mode = IsWindowVisible(m_hwnd);
427 *pVisible = (Mode == TRUE ? OATRUE : OAFALSE);
428 return NOERROR;
432 // Change the left position of the base window. This keeps the window width
433 // and height properties the same so it effectively shunts the window left or
434 // right accordingly - there is the Width property to change that dimension
436 STDMETHODIMP CBaseControlWindow::put_Left(long Left)
438 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
439 BOOL bSuccess;
440 RECT WindowRect;
442 // Get the current window position in a RECT
443 EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
445 if (GetParent(m_hwnd)) {
447 MapWindowPoints(HWND_DESKTOP, GetParent(m_hwnd), (LPPOINT)&WindowRect, 2);
450 // Adjust the coordinates ready for SetWindowPos, the window rectangle we
451 // get back from GetWindowRect is in left,top,right and bottom while the
452 // coordinates SetWindowPos wants are left,top,width and height values
454 WindowRect.bottom = WindowRect.bottom - WindowRect.top;
455 WindowRect.right = WindowRect.right - WindowRect.left;
456 UINT WindowFlags = SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOACTIVATE;
458 bSuccess = SetWindowPos(m_hwnd, // Window handle
459 HWND_TOP, // Put it at the top
460 Left, // New left position
461 WindowRect.top, // Leave top alone
462 WindowRect.right, // The WIDTH (not right)
463 WindowRect.bottom, // The HEIGHT (not bottom)
464 WindowFlags); // Show window options
466 if (bSuccess == FALSE) {
467 return E_INVALIDARG;
469 return NOERROR;
473 // Return the current base window left position
475 STDMETHODIMP CBaseControlWindow::get_Left(long *pLeft)
477 CheckPointer(pLeft,E_POINTER);
478 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
479 RECT WindowRect;
481 EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
482 *pLeft = WindowRect.left;
483 return NOERROR;
487 // Change the current width of the base window. This property complements the
488 // left position property so we must keep the left edge constant and expand or
489 // contract to the right, the alternative would be to change the left edge so
490 // keeping the right edge constant but this is maybe a little more intuitive
492 STDMETHODIMP CBaseControlWindow::put_Width(long Width)
494 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
495 BOOL bSuccess;
496 RECT WindowRect;
498 // Adjust the coordinates ready for SetWindowPos, the window rectangle we
499 // get back from GetWindowRect is in left,top,right and bottom while the
500 // coordinates SetWindowPos wants are left,top,width and height values
502 EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
504 if (GetParent(m_hwnd)) {
506 MapWindowPoints(HWND_DESKTOP, GetParent(m_hwnd), (LPPOINT)&WindowRect, 2);
509 WindowRect.bottom = WindowRect.bottom - WindowRect.top;
510 UINT WindowFlags = SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOACTIVATE;
512 // This seems to have a bug in that calling SetWindowPos on a window with
513 // just the width changing causes it to ignore the width that you pass in
514 // and sets it to a mimimum value of 110 pixels wide (Windows NT 3.51)
516 bSuccess = SetWindowPos(m_hwnd, // Window handle
517 HWND_TOP, // Put it at the top
518 WindowRect.left, // Leave left alone
519 WindowRect.top, // Leave top alone
520 Width, // New WIDTH dimension
521 WindowRect.bottom, // The HEIGHT (not bottom)
522 WindowFlags); // Show window options
524 if (bSuccess == FALSE) {
525 return E_INVALIDARG;
527 return NOERROR;
531 // Return the current base window width
533 STDMETHODIMP CBaseControlWindow::get_Width(long *pWidth)
535 CheckPointer(pWidth,E_POINTER);
536 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
537 RECT WindowRect;
539 EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
540 *pWidth = WindowRect.right - WindowRect.left;
541 return NOERROR;
545 // This allows the client program to change the top position for the window in
546 // the same way that changing the left position does not affect the width of
547 // the image so changing the top position does not affect the window height
549 STDMETHODIMP CBaseControlWindow::put_Top(long Top)
551 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
552 BOOL bSuccess;
553 RECT WindowRect;
555 // Get the current window position in a RECT
556 EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
558 if (GetParent(m_hwnd)) {
560 MapWindowPoints(HWND_DESKTOP, GetParent(m_hwnd), (LPPOINT)&WindowRect, 2);
563 // Adjust the coordinates ready for SetWindowPos, the window rectangle we
564 // get back from GetWindowRect is in left,top,right and bottom while the
565 // coordinates SetWindowPos wants are left,top,width and height values
567 WindowRect.bottom = WindowRect.bottom - WindowRect.top;
568 WindowRect.right = WindowRect.right - WindowRect.left;
569 UINT WindowFlags = SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOACTIVATE;
571 bSuccess = SetWindowPos(m_hwnd, // Window handle
572 HWND_TOP, // Put it at the top
573 WindowRect.left, // Leave left alone
574 Top, // New top position
575 WindowRect.right, // The WIDTH (not right)
576 WindowRect.bottom, // The HEIGHT (not bottom)
577 WindowFlags); // Show window flags
579 if (bSuccess == FALSE) {
580 return E_INVALIDARG;
582 return NOERROR;
586 // Return the current base window top position
588 STDMETHODIMP CBaseControlWindow::get_Top(long *pTop)
590 CheckPointer(pTop,E_POINTER);
591 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
592 RECT WindowRect;
594 EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
595 *pTop = WindowRect.top;
596 return NOERROR;
600 // Change the height of the window, this complements the top property so when
601 // we change this we must keep the top position for the base window, as said
602 // before we could keep the bottom and grow upwards although this is perhaps
603 // a little more intuitive since we already have a top position property
605 STDMETHODIMP CBaseControlWindow::put_Height(long Height)
607 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
608 BOOL bSuccess;
609 RECT WindowRect;
611 // Adjust the coordinates ready for SetWindowPos, the window rectangle we
612 // get back from GetWindowRect is in left,top,right and bottom while the
613 // coordinates SetWindowPos wants are left,top,width and height values
615 EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
617 if (GetParent(m_hwnd)) {
619 MapWindowPoints(HWND_DESKTOP, GetParent(m_hwnd), (LPPOINT)&WindowRect, 2);
622 WindowRect.right = WindowRect.right - WindowRect.left;
623 UINT WindowFlags = SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOACTIVATE;
625 bSuccess = SetWindowPos(m_hwnd, // Window handle
626 HWND_TOP, // Put it at the top
627 WindowRect.left, // Leave left alone
628 WindowRect.top, // Leave top alone
629 WindowRect.right, // The WIDTH (not right)
630 Height, // New height dimension
631 WindowFlags); // Show window flags
633 if (bSuccess == FALSE) {
634 return E_INVALIDARG;
636 return NOERROR;
640 // Return the current base window height
642 STDMETHODIMP CBaseControlWindow::get_Height(long *pHeight)
644 CheckPointer(pHeight,E_POINTER);
645 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
646 RECT WindowRect;
648 EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
649 *pHeight = WindowRect.bottom - WindowRect.top;
650 return NOERROR;
654 // This can be called to change the owning window. Setting the owner is done
655 // through this function, however to make the window a true child window the
656 // style must also be set to WS_CHILD. After resetting the owner to NULL an
657 // application should also set the style to WS_OVERLAPPED | WS_CLIPCHILDREN.
659 // We cannot lock the object here because the SetParent causes an interthread
660 // SendMessage to the owner window. If they are in GetState we will sit here
661 // incomplete with the critical section locked therefore blocking out source
662 // filter threads from accessing us. Because the source thread can't enter us
663 // it can't get buffers or call EndOfStream so the GetState will not complete
665 STDMETHODIMP CBaseControlWindow::put_Owner(OAHWND Owner)
667 // Check we are connected otherwise reject the call
669 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
670 m_hwndOwner = (HWND) Owner;
671 HWND hwndParent = m_hwndOwner;
673 // Add or remove WS_CHILD as appropriate
675 LONG Style = GetWindowLong(m_hwnd,GWL_STYLE);
676 if (Owner == NULL) {
677 Style &= (~WS_CHILD);
678 } else {
679 Style |= (WS_CHILD);
681 SetWindowLong(m_hwnd,GWL_STYLE,Style);
683 // Don't call this with the filter locked
685 SetParent(m_hwnd,hwndParent);
687 PaintWindow(TRUE);
688 NOTE1("Changed parent %lx",hwndParent);
690 return NOERROR;
694 // This complements the put_Owner to get the current owning window property
695 // we always return NOERROR although the returned window handle may be NULL
696 // to indicate no owning window (the desktop window doesn't qualify as one)
697 // If an application sets the owner we call SetParent, however that returns
698 // NULL until the WS_CHILD bit is set on, so we store the owner internally
700 STDMETHODIMP CBaseControlWindow::get_Owner(OAHWND *Owner)
702 CheckPointer(Owner,E_POINTER);
703 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
704 *Owner = (OAHWND) m_hwndOwner;
705 return NOERROR;
709 // And renderer supporting IVideoWindow may have an HWND set who will get any
710 // keyboard and mouse messages we receive posted on to them. This is separate
711 // from setting an owning window. By separating the two, applications may get
712 // messages sent on even when they have set no owner (perhaps it's maximised)
714 STDMETHODIMP CBaseControlWindow::put_MessageDrain(OAHWND Drain)
716 // Check we are connected otherwise reject the call
718 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
719 m_hwndDrain = (HWND) Drain;
720 return NOERROR;
724 // Return the current message drain
726 STDMETHODIMP CBaseControlWindow::get_MessageDrain(OAHWND *Drain)
728 CheckPointer(Drain,E_POINTER);
729 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
730 *Drain = (OAHWND) m_hwndDrain;
731 return NOERROR;
735 // This is called by the filter graph to inform us of a message we should know
736 // is being sent to our owning window. We have this because as a child window
737 // we do not get certain messages that are only sent to top level windows. We
738 // must see the palette changed/changing/query messages so that we know if we
739 // have the foreground palette or not. We pass the message on to our window
740 // using SendMessage - this will cause an interthread send message to occur
742 STDMETHODIMP
743 CBaseControlWindow::NotifyOwnerMessage(OAHWND hwnd, // Window handle
744 long uMsg, // Message ID
745 LONG_PTR wParam, // Parameters
746 LONG_PTR lParam) // for message
748 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
750 // Only interested in these Windows messages
752 switch (uMsg) {
754 case WM_SYSCOLORCHANGE:
755 case WM_PALETTECHANGED:
756 case WM_PALETTEISCHANGING:
757 case WM_QUERYNEWPALETTE:
758 case WM_DEVMODECHANGE:
759 case WM_DISPLAYCHANGE:
760 case WM_ACTIVATEAPP:
762 // If we do not have an owner then ignore
764 if (m_hwndOwner == NULL) {
765 return NOERROR;
767 SendMessage(m_hwnd,uMsg,(WPARAM)wParam,(LPARAM)lParam);
768 break;
770 // do NOT fwd WM_MOVE. the parameters are the location of the parent
771 // window, NOT what the renderer should be looking at. But we need
772 // to make sure the overlay is moved with the parent window, so we
773 // do this.
774 case WM_MOVE:
775 PostMessage(m_hwnd,WM_PAINT,0,0);
776 break;
778 return NOERROR;
782 // Allow an application to have us set the base window in the foreground. We
783 // have this because it is difficult for one thread to do do this to a window
784 // owned by another thread. We ask the base window class to do the real work
786 STDMETHODIMP CBaseControlWindow::SetWindowForeground(long Focus)
788 // Check this is a valid automation boolean type
790 if (Focus != OATRUE) {
791 if (Focus != OAFALSE) {
792 return E_INVALIDARG;
796 // We shouldn't lock as this sends a message
798 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
799 BOOL bFocus = (Focus == OATRUE ? TRUE : FALSE);
800 DoSetWindowForeground(bFocus);
802 return NOERROR;
806 // This allows a client to set the complete window size and position in one
807 // atomic operation. The same affect can be had by changing each dimension
808 // in turn through their individual properties although some flashing will
809 // occur as each of them gets updated (they are better set at design time)
811 STDMETHODIMP
812 CBaseControlWindow::SetWindowPosition(long Left,long Top,long Width,long Height)
814 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
815 BOOL bSuccess;
817 // Set the new size and position
818 UINT WindowFlags = SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOACTIVATE;
820 ASSERT(IsWindow(m_hwnd));
821 bSuccess = SetWindowPos(m_hwnd, // Window handle
822 HWND_TOP, // Put it at the top
823 Left, // Left position
824 Top, // Top position
825 Width, // Window width
826 Height, // Window height
827 WindowFlags); // Show window flags
828 ASSERT(bSuccess);
829 #ifdef DEBUG
830 DbgLog((LOG_TRACE, 1, TEXT("SWP failed error %d"), GetLastError()));
831 #endif
832 if (bSuccess == FALSE) {
833 return E_INVALIDARG;
835 return NOERROR;
839 // This complements the SetWindowPosition to return the current window place
840 // in device coordinates. As before the same information can be retrived by
841 // calling the property get functions individually but this is atomic and is
842 // therefore more suitable to a live environment rather than design time
844 STDMETHODIMP
845 CBaseControlWindow::GetWindowPosition(long *pLeft,long *pTop,long *pWidth,long *pHeight)
847 // Should check the pointers are not NULL
849 CheckPointer(pLeft,E_POINTER);
850 CheckPointer(pTop,E_POINTER);
851 CheckPointer(pWidth,E_POINTER);
852 CheckPointer(pHeight,E_POINTER);
853 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
854 RECT WindowRect;
856 // Get the current window coordinates
858 EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
860 // Convert the RECT into left,top,width and height values
862 *pLeft = WindowRect.left;
863 *pTop = WindowRect.top;
864 *pWidth = WindowRect.right - WindowRect.left;
865 *pHeight = WindowRect.bottom - WindowRect.top;
867 return NOERROR;
871 // When a window is maximised or iconic calling GetWindowPosition will return
872 // the current window position (likewise for the properties). However if the
873 // restored size (ie the size we'll return to when normally shown) is needed
874 // then this should be used. When in a normal position (neither iconic nor
875 // maximised) then this returns the same coordinates as GetWindowPosition
877 STDMETHODIMP
878 CBaseControlWindow::GetRestorePosition(long *pLeft,long *pTop,long *pWidth,long *pHeight)
880 // Should check the pointers are not NULL
882 CheckPointer(pLeft,E_POINTER);
883 CheckPointer(pTop,E_POINTER);
884 CheckPointer(pWidth,E_POINTER);
885 CheckPointer(pHeight,E_POINTER);
886 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
888 // Use GetWindowPlacement to find the restore position
890 WINDOWPLACEMENT Place;
891 Place.length = sizeof(WINDOWPLACEMENT);
892 EXECUTE_ASSERT(GetWindowPlacement(m_hwnd,&Place));
894 RECT WorkArea;
896 // We must take into account any task bar present
898 if (SystemParametersInfo(SPI_GETWORKAREA,0,&WorkArea,FALSE) == TRUE) {
899 if (GetParent(m_hwnd) == NULL) {
900 Place.rcNormalPosition.top += WorkArea.top;
901 Place.rcNormalPosition.bottom += WorkArea.top;
902 Place.rcNormalPosition.left += WorkArea.left;
903 Place.rcNormalPosition.right += WorkArea.left;
907 // Convert the RECT into left,top,width and height values
909 *pLeft = Place.rcNormalPosition.left;
910 *pTop = Place.rcNormalPosition.top;
911 *pWidth = Place.rcNormalPosition.right - Place.rcNormalPosition.left;
912 *pHeight = Place.rcNormalPosition.bottom - Place.rcNormalPosition.top;
914 return NOERROR;
918 // Return the current border colour, if we are playing something to a subset
919 // of the base window display there is an outside area exposed. The default
920 // action is to paint this colour in the Windows background colour (defined
921 // as value COLOR_WINDOW) We reset to this default when we're disconnected
923 STDMETHODIMP CBaseControlWindow::get_BorderColor(long *Color)
925 CheckPointer(Color,E_POINTER);
926 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
927 *Color = (long) m_BorderColour;
928 return NOERROR;
932 // This can be called to set the current border colour
934 STDMETHODIMP CBaseControlWindow::put_BorderColor(long Color)
936 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
938 // Have the window repainted with the new border colour
940 m_BorderColour = (COLORREF) Color;
941 PaintWindow(TRUE);
942 return NOERROR;
946 // Delegate fullscreen handling to plug in distributor
948 STDMETHODIMP CBaseControlWindow::get_FullScreenMode(long *FullScreenMode)
950 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
951 CheckPointer(FullScreenMode,E_POINTER);
952 return E_NOTIMPL;
956 // Delegate fullscreen handling to plug in distributor
958 STDMETHODIMP CBaseControlWindow::put_FullScreenMode(long FullScreenMode)
960 return E_NOTIMPL;
964 // This sets the auto show property, this property causes the base window to
965 // be displayed whenever we change state. This allows an application to have
966 // to do nothing to have the window appear but still allow them to change the
967 // default behaviour if for example they want to keep it hidden for longer
969 STDMETHODIMP CBaseControlWindow::put_AutoShow(long AutoShow)
971 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
973 // Check this is a valid automation boolean type
975 if (AutoShow != OATRUE) {
976 if (AutoShow != OAFALSE) {
977 return E_INVALIDARG;
981 m_bAutoShow = (AutoShow == OATRUE ? TRUE : FALSE);
982 return NOERROR;
986 // This can be called to get the current auto show flag. The flag is updated
987 // when we connect and disconnect and through this interface all of which are
988 // controlled and serialised by means of the main renderer critical section
990 STDMETHODIMP CBaseControlWindow::get_AutoShow(long *AutoShow)
992 CheckPointer(AutoShow,E_POINTER);
993 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
994 *AutoShow = (m_bAutoShow == TRUE ? OATRUE : OAFALSE);
995 return NOERROR;
999 // Return the minimum ideal image size for the current video. This may differ
1000 // to the actual video dimensions because we may be using DirectDraw hardware
1001 // that has specific stretching requirements. For example the Cirrus Logic
1002 // cards have a minimum stretch factor depending on the overlay surface size
1004 STDMETHODIMP
1005 CBaseControlWindow::GetMinIdealImageSize(long *pWidth,long *pHeight)
1007 CheckPointer(pWidth,E_POINTER);
1008 CheckPointer(pHeight,E_POINTER);
1009 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1010 FILTER_STATE State;
1012 // Must not be stopped for this to work correctly
1014 m_pFilter->GetState(0,&State);
1015 if (State == State_Stopped) {
1016 return VFW_E_WRONG_STATE;
1019 RECT DefaultRect = GetDefaultRect();
1020 *pWidth = WIDTH(&DefaultRect);
1021 *pHeight = HEIGHT(&DefaultRect);
1022 return NOERROR;
1026 // Return the maximum ideal image size for the current video. This may differ
1027 // to the actual video dimensions because we may be using DirectDraw hardware
1028 // that has specific stretching requirements. For example the Cirrus Logic
1029 // cards have a maximum stretch factor depending on the overlay surface size
1031 STDMETHODIMP
1032 CBaseControlWindow::GetMaxIdealImageSize(long *pWidth,long *pHeight)
1034 CheckPointer(pWidth,E_POINTER);
1035 CheckPointer(pHeight,E_POINTER);
1036 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1037 FILTER_STATE State;
1039 // Must not be stopped for this to work correctly
1041 m_pFilter->GetState(0,&State);
1042 if (State == State_Stopped) {
1043 return VFW_E_WRONG_STATE;
1046 RECT DefaultRect = GetDefaultRect();
1047 *pWidth = WIDTH(&DefaultRect);
1048 *pHeight = HEIGHT(&DefaultRect);
1049 return NOERROR;
1053 // Allow an application to hide the cursor on our window
1055 STDMETHODIMP
1056 CBaseControlWindow::HideCursor(long HideCursor)
1058 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1060 // Check this is a valid automation boolean type
1062 if (HideCursor != OATRUE) {
1063 if (HideCursor != OAFALSE) {
1064 return E_INVALIDARG;
1068 m_bCursorHidden = (HideCursor == OATRUE ? TRUE : FALSE);
1069 return NOERROR;
1073 // Returns whether we have the cursor hidden or not
1075 STDMETHODIMP CBaseControlWindow::IsCursorHidden(long *CursorHidden)
1077 CheckPointer(CursorHidden,E_POINTER);
1078 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1079 *CursorHidden = (m_bCursorHidden == TRUE ? OATRUE : OAFALSE);
1080 return NOERROR;
1084 // This class implements the IBasicVideo control functions (dual interface)
1085 // we support a large number of properties and methods designed to allow the
1086 // client (whether it be an automation controller or a C/C++ application) to
1087 // set and get a number of video related properties such as the native video
1088 // size. We support some methods that duplicate the properties but provide a
1089 // more direct and efficient mechanism as many values may be changed in one
1091 CBaseControlVideo::CBaseControlVideo(
1092 CBaseFilter *pFilter, // Owning filter
1093 CCritSec *pInterfaceLock, // Locking object
1094 TCHAR *pName, // Object description
1095 LPUNKNOWN pUnk, // Normal COM ownership
1096 HRESULT *phr) : // OLE return code
1098 CBaseBasicVideo(pName,pUnk),
1099 m_pFilter(pFilter),
1100 m_pInterfaceLock(pInterfaceLock),
1101 m_pPin(NULL)
1103 ASSERT(m_pFilter);
1104 ASSERT(m_pInterfaceLock);
1105 ASSERT(phr);
1108 // Return an approximate average time per frame
1110 STDMETHODIMP CBaseControlVideo::get_AvgTimePerFrame(REFTIME *pAvgTimePerFrame)
1112 CheckPointer(pAvgTimePerFrame,E_POINTER);
1113 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1114 CAutoLock cInterfaceLock(m_pInterfaceLock);
1116 VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
1117 if (pVideoInfo == NULL)
1118 return E_OUTOFMEMORY;
1119 COARefTime AvgTime(pVideoInfo->AvgTimePerFrame);
1120 *pAvgTimePerFrame = (REFTIME) AvgTime;
1122 return NOERROR;
1126 // Return an approximate bit rate for the video
1128 STDMETHODIMP CBaseControlVideo::get_BitRate(long *pBitRate)
1130 CheckPointer(pBitRate,E_POINTER);
1131 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1132 CAutoLock cInterfaceLock(m_pInterfaceLock);
1134 VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
1135 if (pVideoInfo == NULL)
1136 return E_OUTOFMEMORY;
1137 *pBitRate = pVideoInfo->dwBitRate;
1138 return NOERROR;
1142 // Return an approximate bit error rate
1144 STDMETHODIMP CBaseControlVideo::get_BitErrorRate(long *pBitErrorRate)
1146 CheckPointer(pBitErrorRate,E_POINTER);
1147 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1148 CAutoLock cInterfaceLock(m_pInterfaceLock);
1150 VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
1151 if (pVideoInfo == NULL)
1152 return E_OUTOFMEMORY;
1153 *pBitErrorRate = pVideoInfo->dwBitErrorRate;
1154 return NOERROR;
1158 // This returns the current video width
1160 STDMETHODIMP CBaseControlVideo::get_VideoWidth(long *pVideoWidth)
1162 CheckPointer(pVideoWidth,E_POINTER);
1163 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1164 CAutoLock cInterfaceLock(m_pInterfaceLock);
1166 VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
1167 if (pVideoInfo == NULL)
1168 return E_OUTOFMEMORY;
1169 *pVideoWidth = pVideoInfo->bmiHeader.biWidth;
1170 return NOERROR;
1174 // This returns the current video height
1176 STDMETHODIMP CBaseControlVideo::get_VideoHeight(long *pVideoHeight)
1178 CheckPointer(pVideoHeight,E_POINTER);
1179 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1180 CAutoLock cInterfaceLock(m_pInterfaceLock);
1182 VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
1183 if (pVideoInfo == NULL)
1184 return E_OUTOFMEMORY;
1185 *pVideoHeight = pVideoInfo->bmiHeader.biHeight;
1186 return NOERROR;
1190 // This returns the current palette the video is using as an array allocated
1191 // by the user. To remain consistent we use PALETTEENTRY fields to return the
1192 // colours in rather than RGBQUADs that multimedia decided to use. The memory
1193 // is allocated by the user so we simple copy each in turn. We check that the
1194 // number of entries requested and the start position offset are both valid
1195 // If the number of entries evaluates to zero then we return an S_FALSE code
1197 STDMETHODIMP CBaseControlVideo::GetVideoPaletteEntries(long StartIndex,
1198 long Entries,
1199 long *pRetrieved,
1200 long *pPalette)
1202 CheckPointer(pRetrieved,E_POINTER);
1203 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1204 CAutoLock cInterfaceLock(m_pInterfaceLock);
1205 CMediaType MediaType;
1207 // Get the video format from the derived class
1209 VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
1210 if (pVideoInfo == NULL)
1211 return E_OUTOFMEMORY;
1212 BITMAPINFOHEADER *pHeader = HEADER(pVideoInfo);
1214 // Is the current format palettised
1216 if (PALETTISED(pVideoInfo) == FALSE) {
1217 *pRetrieved = 0;
1218 return VFW_E_NO_PALETTE_AVAILABLE;
1221 // Do they just want to know how many are available
1223 if (pPalette == NULL) {
1224 *pRetrieved = pHeader->biClrUsed;
1225 return NOERROR;
1228 // Make sure the start position is a valid offset
1230 if (StartIndex >= (LONG) pHeader->biClrUsed || StartIndex < 0) {
1231 *pRetrieved = 0;
1232 return E_INVALIDARG;
1235 // Correct the number we can retrieve
1237 LONG Available = (LONG) pHeader->biClrUsed - StartIndex;
1238 *pRetrieved = max(0,min(Available,Entries));
1239 if (*pRetrieved == 0) {
1240 return S_FALSE;
1243 // Copy the palette entries to the output buffer
1245 PALETTEENTRY *pEntries = (PALETTEENTRY *) pPalette;
1246 RGBQUAD *pColours = COLORS(pVideoInfo) + StartIndex;
1248 for (LONG Count = 0;Count < *pRetrieved;Count++) {
1249 pEntries[Count].peRed = pColours[Count].rgbRed;
1250 pEntries[Count].peGreen = pColours[Count].rgbGreen;
1251 pEntries[Count].peBlue = pColours[Count].rgbBlue;
1252 pEntries[Count].peFlags = 0;
1254 return NOERROR;
1258 // This returns the current video dimensions as a method rather than a number
1259 // of individual property get calls. For the same reasons as said before we
1260 // cannot access the renderer media type directly as the window object thread
1261 // may be updating it since dynamic format changes may change these values
1263 STDMETHODIMP CBaseControlVideo::GetVideoSize(long *pWidth,long *pHeight)
1265 CheckPointer(pWidth,E_POINTER);
1266 CheckPointer(pHeight,E_POINTER);
1267 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1268 CAutoLock cInterfaceLock(m_pInterfaceLock);
1270 // Get the video format from the derived class
1271 VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
1272 if (pVideoInfo == NULL)
1273 return E_OUTOFMEMORY;
1274 *pWidth = pVideoInfo->bmiHeader.biWidth;
1275 *pHeight = pVideoInfo->bmiHeader.biHeight;
1276 return NOERROR;
1280 // Set the source video rectangle as left,top,right and bottom coordinates
1281 // rather than left,top,width and height as per OLE automation interfaces
1282 // Then pass the rectangle on to the window object to set the source
1284 STDMETHODIMP
1285 CBaseControlVideo::SetSourcePosition(long Left,long Top,long Width,long Height)
1287 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1288 CAutoLock cInterfaceLock(m_pInterfaceLock);
1289 RECT SourceRect;
1290 SourceRect.left = Left;
1291 SourceRect.top = Top;
1292 SourceRect.right = Left + Width;
1293 SourceRect.bottom = Top + Height;
1295 // Check the source rectangle is valid
1297 HRESULT hr = CheckSourceRect(&SourceRect);
1298 if (FAILED(hr)) {
1299 return hr;
1302 // Now set the source rectangle
1304 hr = SetSourceRect(&SourceRect);
1305 if (FAILED(hr)) {
1306 return hr;
1308 return OnUpdateRectangles();
1312 // Return the source rectangle in left,top,width and height rather than the
1313 // left,top,right and bottom values that RECT uses (and which the window
1314 // object returns through GetSourceRect) which requires a little work
1316 STDMETHODIMP
1317 CBaseControlVideo::GetSourcePosition(long *pLeft,long *pTop,long *pWidth,long *pHeight)
1319 CheckPointer(pLeft,E_POINTER);
1320 CheckPointer(pTop,E_POINTER);
1321 CheckPointer(pWidth,E_POINTER);
1322 CheckPointer(pHeight,E_POINTER);
1323 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1324 RECT SourceRect;
1326 CAutoLock cInterfaceLock(m_pInterfaceLock);
1327 GetSourceRect(&SourceRect);
1329 *pLeft = SourceRect.left;
1330 *pTop = SourceRect.top;
1331 *pWidth = WIDTH(&SourceRect);
1332 *pHeight = HEIGHT(&SourceRect);
1334 return NOERROR;
1338 // Set the video destination as left,top,right and bottom coordinates rather
1339 // than the left,top,width and height uses as per OLE automation interfaces
1340 // Then pass the rectangle on to the window object to set the destination
1342 STDMETHODIMP
1343 CBaseControlVideo::SetDestinationPosition(long Left,long Top,long Width,long Height)
1345 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1346 CAutoLock cInterfaceLock(m_pInterfaceLock);
1347 RECT DestinationRect;
1349 DestinationRect.left = Left;
1350 DestinationRect.top = Top;
1351 DestinationRect.right = Left + Width;
1352 DestinationRect.bottom = Top + Height;
1354 // Check the target rectangle is valid
1356 HRESULT hr = CheckTargetRect(&DestinationRect);
1357 if (FAILED(hr)) {
1358 return hr;
1361 // Now set the new target rectangle
1363 hr = SetTargetRect(&DestinationRect);
1364 if (FAILED(hr)) {
1365 return hr;
1367 return OnUpdateRectangles();
1371 // Return the destination rectangle in left,top,width and height rather than
1372 // the left,top,right and bottom values that RECT uses (and which the window
1373 // object returns through GetDestinationRect) which requires a little work
1375 STDMETHODIMP
1376 CBaseControlVideo::GetDestinationPosition(long *pLeft,long *pTop,long *pWidth,long *pHeight)
1378 // Should check the pointers are not NULL
1380 CheckPointer(pLeft,E_POINTER);
1381 CheckPointer(pTop,E_POINTER);
1382 CheckPointer(pWidth,E_POINTER);
1383 CheckPointer(pHeight,E_POINTER);
1384 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1385 RECT DestinationRect;
1387 CAutoLock cInterfaceLock(m_pInterfaceLock);
1388 GetTargetRect(&DestinationRect);
1390 *pLeft = DestinationRect.left;
1391 *pTop = DestinationRect.top;
1392 *pWidth = WIDTH(&DestinationRect);
1393 *pHeight = HEIGHT(&DestinationRect);
1395 return NOERROR;
1399 // Set the source left position, the source rectangle we get back from the
1400 // window object is a true rectangle in left,top,right and bottom positions
1401 // so all we have to do is to update the left position and pass it back. We
1402 // must keep the current width constant when we're updating this property
1404 STDMETHODIMP CBaseControlVideo::put_SourceLeft(long SourceLeft)
1406 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1407 CAutoLock cInterfaceLock(m_pInterfaceLock);
1408 RECT SourceRect;
1409 GetSourceRect(&SourceRect);
1410 SourceRect.right = SourceLeft + WIDTH(&SourceRect);
1411 SourceRect.left = SourceLeft;
1413 // Check the source rectangle is valid
1415 HRESULT hr = CheckSourceRect(&SourceRect);
1416 if (FAILED(hr)) {
1417 return hr;
1420 // Now set the source rectangle
1422 hr = SetSourceRect(&SourceRect);
1423 if (FAILED(hr)) {
1424 return hr;
1426 return OnUpdateRectangles();
1430 // Return the current left source video position
1432 STDMETHODIMP CBaseControlVideo::get_SourceLeft(long *pSourceLeft)
1434 CheckPointer(pSourceLeft,E_POINTER);
1435 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1436 CAutoLock cInterfaceLock(m_pInterfaceLock);
1437 RECT SourceRect;
1439 GetSourceRect(&SourceRect);
1440 *pSourceLeft = SourceRect.left;
1441 return NOERROR;
1445 // Set the source width, we get the current source rectangle and then update
1446 // the right position to be the left position (thereby keeping it constant)
1447 // plus the new source width we are passed in (it expands to the right)
1449 STDMETHODIMP CBaseControlVideo::put_SourceWidth(long SourceWidth)
1451 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1452 CAutoLock cInterfaceLock(m_pInterfaceLock);
1453 RECT SourceRect;
1454 GetSourceRect(&SourceRect);
1455 SourceRect.right = SourceRect.left + SourceWidth;
1457 // Check the source rectangle is valid
1459 HRESULT hr = CheckSourceRect(&SourceRect);
1460 if (FAILED(hr)) {
1461 return hr;
1464 // Now set the source rectangle
1466 hr = SetSourceRect(&SourceRect);
1467 if (FAILED(hr)) {
1468 return hr;
1470 return OnUpdateRectangles();
1474 // Return the current source width
1476 STDMETHODIMP CBaseControlVideo::get_SourceWidth(long *pSourceWidth)
1478 CheckPointer(pSourceWidth,E_POINTER);
1479 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1480 CAutoLock cInterfaceLock(m_pInterfaceLock);
1481 RECT SourceRect;
1483 GetSourceRect(&SourceRect);
1484 *pSourceWidth = WIDTH(&SourceRect);
1485 return NOERROR;
1489 // Set the source top position - changing this property does not affect the
1490 // current source height. So changing this shunts the source rectangle up and
1491 // down appropriately. Changing the height complements this functionality by
1492 // keeping the top position constant and simply changing the source height
1494 STDMETHODIMP CBaseControlVideo::put_SourceTop(long SourceTop)
1496 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1497 CAutoLock cInterfaceLock(m_pInterfaceLock);
1498 RECT SourceRect;
1499 GetSourceRect(&SourceRect);
1500 SourceRect.bottom = SourceTop + HEIGHT(&SourceRect);
1501 SourceRect.top = SourceTop;
1503 // Check the source rectangle is valid
1505 HRESULT hr = CheckSourceRect(&SourceRect);
1506 if (FAILED(hr)) {
1507 return hr;
1510 // Now set the source rectangle
1512 hr = SetSourceRect(&SourceRect);
1513 if (FAILED(hr)) {
1514 return hr;
1516 return OnUpdateRectangles();
1520 // Return the current top position
1522 STDMETHODIMP CBaseControlVideo::get_SourceTop(long *pSourceTop)
1524 CheckPointer(pSourceTop,E_POINTER);
1525 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1526 CAutoLock cInterfaceLock(m_pInterfaceLock);
1527 RECT SourceRect;
1529 GetSourceRect(&SourceRect);
1530 *pSourceTop = SourceRect.top;
1531 return NOERROR;
1535 // Set the source height
1537 STDMETHODIMP CBaseControlVideo::put_SourceHeight(long SourceHeight)
1539 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1540 CAutoLock cInterfaceLock(m_pInterfaceLock);
1541 RECT SourceRect;
1542 GetSourceRect(&SourceRect);
1543 SourceRect.bottom = SourceRect.top + SourceHeight;
1545 // Check the source rectangle is valid
1547 HRESULT hr = CheckSourceRect(&SourceRect);
1548 if (FAILED(hr)) {
1549 return hr;
1552 // Now set the source rectangle
1554 hr = SetSourceRect(&SourceRect);
1555 if (FAILED(hr)) {
1556 return hr;
1558 return OnUpdateRectangles();
1562 // Return the current source height
1564 STDMETHODIMP CBaseControlVideo::get_SourceHeight(long *pSourceHeight)
1566 CheckPointer(pSourceHeight,E_POINTER);
1567 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1568 CAutoLock cInterfaceLock(m_pInterfaceLock);
1569 RECT SourceRect;
1571 GetSourceRect(&SourceRect);
1572 *pSourceHeight = HEIGHT(&SourceRect);
1573 return NOERROR;
1577 // Set the target left position, the target rectangle we get back from the
1578 // window object is a true rectangle in left,top,right and bottom positions
1579 // so all we have to do is to update the left position and pass it back. We
1580 // must keep the current width constant when we're updating this property
1582 STDMETHODIMP CBaseControlVideo::put_DestinationLeft(long DestinationLeft)
1584 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1585 CAutoLock cInterfaceLock(m_pInterfaceLock);
1586 RECT DestinationRect;
1587 GetTargetRect(&DestinationRect);
1588 DestinationRect.right = DestinationLeft + WIDTH(&DestinationRect);
1589 DestinationRect.left = DestinationLeft;
1591 // Check the target rectangle is valid
1593 HRESULT hr = CheckTargetRect(&DestinationRect);
1594 if (FAILED(hr)) {
1595 return hr;
1598 // Now set the new target rectangle
1600 hr = SetTargetRect(&DestinationRect);
1601 if (FAILED(hr)) {
1602 return hr;
1604 return OnUpdateRectangles();
1608 // Return the left position for the destination rectangle
1610 STDMETHODIMP CBaseControlVideo::get_DestinationLeft(long *pDestinationLeft)
1612 CheckPointer(pDestinationLeft,E_POINTER);
1613 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1614 CAutoLock cInterfaceLock(m_pInterfaceLock);
1615 RECT DestinationRect;
1617 GetTargetRect(&DestinationRect);
1618 *pDestinationLeft = DestinationRect.left;
1619 return NOERROR;
1623 // Set the destination width
1625 STDMETHODIMP CBaseControlVideo::put_DestinationWidth(long DestinationWidth)
1627 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1628 CAutoLock cInterfaceLock(m_pInterfaceLock);
1629 RECT DestinationRect;
1630 GetTargetRect(&DestinationRect);
1631 DestinationRect.right = DestinationRect.left + DestinationWidth;
1633 // Check the target rectangle is valid
1635 HRESULT hr = CheckTargetRect(&DestinationRect);
1636 if (FAILED(hr)) {
1637 return hr;
1640 // Now set the new target rectangle
1642 hr = SetTargetRect(&DestinationRect);
1643 if (FAILED(hr)) {
1644 return hr;
1646 return OnUpdateRectangles();
1650 // Return the width for the destination rectangle
1652 STDMETHODIMP CBaseControlVideo::get_DestinationWidth(long *pDestinationWidth)
1654 CheckPointer(pDestinationWidth,E_POINTER);
1655 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1656 CAutoLock cInterfaceLock(m_pInterfaceLock);
1657 RECT DestinationRect;
1659 GetTargetRect(&DestinationRect);
1660 *pDestinationWidth = WIDTH(&DestinationRect);
1661 return NOERROR;
1665 // Set the target top position - changing this property does not affect the
1666 // current target height. So changing this shunts the target rectangle up and
1667 // down appropriately. Changing the height complements this functionality by
1668 // keeping the top position constant and simply changing the target height
1670 STDMETHODIMP CBaseControlVideo::put_DestinationTop(long DestinationTop)
1672 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1673 CAutoLock cInterfaceLock(m_pInterfaceLock);
1674 RECT DestinationRect;
1675 GetTargetRect(&DestinationRect);
1676 DestinationRect.bottom = DestinationTop + HEIGHT(&DestinationRect);
1677 DestinationRect.top = DestinationTop;
1679 // Check the target rectangle is valid
1681 HRESULT hr = CheckTargetRect(&DestinationRect);
1682 if (FAILED(hr)) {
1683 return hr;
1686 // Now set the new target rectangle
1688 hr = SetTargetRect(&DestinationRect);
1689 if (FAILED(hr)) {
1690 return hr;
1692 return OnUpdateRectangles();
1696 // Return the top position for the destination rectangle
1698 STDMETHODIMP CBaseControlVideo::get_DestinationTop(long *pDestinationTop)
1700 CheckPointer(pDestinationTop,E_POINTER);
1701 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1702 CAutoLock cInterfaceLock(m_pInterfaceLock);
1703 RECT DestinationRect;
1705 GetTargetRect(&DestinationRect);
1706 *pDestinationTop = DestinationRect.top;
1707 return NOERROR;
1711 // Set the destination height
1713 STDMETHODIMP CBaseControlVideo::put_DestinationHeight(long DestinationHeight)
1715 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1716 CAutoLock cInterfaceLock(m_pInterfaceLock);
1717 RECT DestinationRect;
1718 GetTargetRect(&DestinationRect);
1719 DestinationRect.bottom = DestinationRect.top + DestinationHeight;
1721 // Check the target rectangle is valid
1723 HRESULT hr = CheckTargetRect(&DestinationRect);
1724 if (FAILED(hr)) {
1725 return hr;
1728 // Now set the new target rectangle
1730 hr = SetTargetRect(&DestinationRect);
1731 if (FAILED(hr)) {
1732 return hr;
1734 return OnUpdateRectangles();
1738 // Return the height for the destination rectangle
1740 STDMETHODIMP CBaseControlVideo::get_DestinationHeight(long *pDestinationHeight)
1742 CheckPointer(pDestinationHeight,E_POINTER);
1743 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1744 CAutoLock cInterfaceLock(m_pInterfaceLock);
1745 RECT DestinationRect;
1747 GetTargetRect(&DestinationRect);
1748 *pDestinationHeight = HEIGHT(&DestinationRect);
1749 return NOERROR;
1753 // Reset the source rectangle to the full video dimensions
1755 STDMETHODIMP CBaseControlVideo::SetDefaultSourcePosition()
1757 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1758 CAutoLock cInterfaceLock(m_pInterfaceLock);
1759 HRESULT hr = SetDefaultSourceRect();
1760 if (FAILED(hr)) {
1761 return hr;
1763 return OnUpdateRectangles();
1767 // Return S_OK if we're using the default source otherwise S_FALSE
1769 STDMETHODIMP CBaseControlVideo::IsUsingDefaultSource()
1771 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1772 CAutoLock cInterfaceLock(m_pInterfaceLock);
1773 return IsDefaultSourceRect();
1777 // Reset the video renderer to use the entire playback area
1779 STDMETHODIMP CBaseControlVideo::SetDefaultDestinationPosition()
1781 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1782 CAutoLock cInterfaceLock(m_pInterfaceLock);
1783 HRESULT hr = SetDefaultTargetRect();
1784 if (FAILED(hr)) {
1785 return hr;
1787 return OnUpdateRectangles();
1791 // Return S_OK if we're using the default target otherwise S_FALSE
1793 STDMETHODIMP CBaseControlVideo::IsUsingDefaultDestination()
1795 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1796 CAutoLock cInterfaceLock(m_pInterfaceLock);
1797 return IsDefaultTargetRect();
1801 // Return a copy of the current image in the video renderer
1803 STDMETHODIMP
1804 CBaseControlVideo::GetCurrentImage(long *pBufferSize,long *pVideoImage)
1806 CheckPointer(pBufferSize,E_POINTER);
1807 CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
1808 CAutoLock cInterfaceLock(m_pInterfaceLock);
1809 FILTER_STATE State;
1811 // Make sure we are in a paused state
1813 if (pVideoImage != NULL) {
1814 m_pFilter->GetState(0,&State);
1815 if (State != State_Paused) {
1816 return VFW_E_NOT_PAUSED;
1818 return GetStaticImage(pBufferSize,pVideoImage);
1821 // Just return the memory required
1823 VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
1824 if (pVideoInfo == NULL)
1825 return E_OUTOFMEMORY;
1826 RECT SourceRect;
1827 GetSourceRect(&SourceRect);
1828 return GetImageSize(pVideoInfo,pBufferSize,&SourceRect);
1832 // An application has two ways of using GetCurrentImage, one is to pass a real
1833 // buffer which should be filled with the current image. The other is to pass
1834 // a NULL buffer pointer which is interpreted as asking us to return how much
1835 // memory is required for the image. The constraints for when the latter can
1836 // be called are much looser. To calculate the memory required we synthesize
1837 // a VIDEOINFO that takes into account the source rectangle that's being used
1839 HRESULT CBaseControlVideo::GetImageSize(VIDEOINFOHEADER *pVideoInfo,
1840 LONG *pBufferSize,
1841 RECT *pSourceRect)
1843 NOTE("Entering GetImageSize");
1844 ASSERT(pSourceRect);
1846 // Check we have the correct input parameters
1848 if (pSourceRect == NULL ||
1849 pVideoInfo == NULL ||
1850 pBufferSize == NULL) {
1852 return E_UNEXPECTED;
1855 // Is the data format compatible
1857 if (pVideoInfo->bmiHeader.biCompression != BI_RGB) {
1858 if (pVideoInfo->bmiHeader.biCompression != BI_BITFIELDS) {
1859 return E_INVALIDARG;
1863 ASSERT(IsRectEmpty(pSourceRect) == FALSE);
1865 BITMAPINFOHEADER bih;
1866 bih.biWidth = WIDTH(pSourceRect);
1867 bih.biHeight = HEIGHT(pSourceRect);
1868 bih.biBitCount = pVideoInfo->bmiHeader.biBitCount;
1869 LONG Size = DIBSIZE(bih);
1870 Size += GetBitmapFormatSize(HEADER(pVideoInfo)) - SIZE_PREHEADER;
1871 *pBufferSize = Size;
1873 return NOERROR;
1877 // Given an IMediaSample containing a linear buffer with an image and a type
1878 // describing the bitmap make a rendering of the image into the output buffer
1879 // This may be called by derived classes who render typical video images to
1880 // handle the IBasicVideo GetCurrentImage method. The pVideoImage pointer may
1881 // be NULL when passed to GetCurrentImage in which case GetImageSize will be
1882 // called instead, which will just do the calculation of the memory required
1884 HRESULT CBaseControlVideo::CopyImage(IMediaSample *pMediaSample,
1885 VIDEOINFOHEADER *pVideoInfo,
1886 LONG *pBufferSize,
1887 BYTE *pVideoImage,
1888 RECT *pSourceRect)
1890 NOTE("Entering CopyImage");
1891 ASSERT(pSourceRect);
1892 BYTE *pCurrentImage;
1894 // Check we have an image to copy
1896 if (pMediaSample == NULL || pSourceRect == NULL ||
1897 pVideoInfo == NULL || pVideoImage == NULL ||
1898 pBufferSize == NULL) {
1900 return E_UNEXPECTED;
1903 // Is the data format compatible
1905 if (pVideoInfo->bmiHeader.biCompression != BI_RGB) {
1906 if (pVideoInfo->bmiHeader.biCompression != BI_BITFIELDS) {
1907 return E_INVALIDARG;
1911 ASSERT(IsRectEmpty(pSourceRect) == FALSE);
1913 BITMAPINFOHEADER bih;
1914 bih.biWidth = WIDTH(pSourceRect);
1915 bih.biHeight = HEIGHT(pSourceRect);
1916 bih.biBitCount = pVideoInfo->bmiHeader.biBitCount;
1917 LONG Size = GetBitmapFormatSize(HEADER(pVideoInfo)) - SIZE_PREHEADER;
1918 LONG Total = Size + DIBSIZE(bih);
1920 // Make sure we have a large enough buffer
1922 if (*pBufferSize < Total) {
1923 return E_OUTOFMEMORY;
1926 // Copy the BITMAPINFO
1928 CopyMemory((PVOID)pVideoImage, (PVOID)&pVideoInfo->bmiHeader, Size);
1929 ((BITMAPINFOHEADER *)pVideoImage)->biWidth = WIDTH(pSourceRect);
1930 ((BITMAPINFOHEADER *)pVideoImage)->biHeight = HEIGHT(pSourceRect);
1931 ((BITMAPINFOHEADER *)pVideoImage)->biSizeImage = DIBSIZE(bih);
1932 BYTE *pImageData = pVideoImage + Size;
1934 // Get the pointer to it's image data
1936 HRESULT hr = pMediaSample->GetPointer(&pCurrentImage);
1937 if (FAILED(hr)) {
1938 return hr;
1941 // Now we are ready to start copying the source scan lines
1943 LONG ScanLine = (pVideoInfo->bmiHeader.biBitCount / 8) * WIDTH(pSourceRect);
1944 LONG LinesToSkip = pVideoInfo->bmiHeader.biHeight;
1945 LinesToSkip -= pSourceRect->top + HEIGHT(pSourceRect);
1946 pCurrentImage += LinesToSkip * DIBWIDTHBYTES(pVideoInfo->bmiHeader);
1947 pCurrentImage += pSourceRect->left * (pVideoInfo->bmiHeader.biBitCount / 8);
1949 // Even money on this GP faulting sometime...
1951 for (LONG Line = 0;Line < HEIGHT(pSourceRect);Line++) {
1952 CopyMemory((PVOID)pImageData, (PVOID)pCurrentImage, ScanLine);
1953 pImageData += DIBWIDTHBYTES(*(BITMAPINFOHEADER *)pVideoImage);
1954 pCurrentImage += DIBWIDTHBYTES(pVideoInfo->bmiHeader);
1956 return NOERROR;
1960 // Called when we change media types either during connection or dynamically
1961 // We inform the filter graph and therefore the application that the video
1962 // size may have changed, we don't bother looking to see if it really has as
1963 // we leave that to the application - the dimensions are the event parameters
1965 HRESULT CBaseControlVideo::OnVideoSizeChange()
1967 // Get the video format from the derived class
1969 VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
1970 if (pVideoInfo == NULL)
1971 return E_OUTOFMEMORY;
1972 WORD Width = (WORD) pVideoInfo->bmiHeader.biWidth;
1973 WORD Height = (WORD) pVideoInfo->bmiHeader.biHeight;
1975 return m_pFilter->NotifyEvent(EC_VIDEO_SIZE_CHANGED,
1976 MAKELPARAM(Width,Height),
1977 MAKEWPARAM(0,0));
1981 // Set the video source rectangle. We must check the source rectangle against
1982 // the actual video dimensions otherwise when we come to draw the pictures we
1983 // get access violations as GDI tries to touch data outside of the image data
1984 // Although we store the rectangle in left, top, right and bottom coordinates
1985 // instead of left, top, width and height as OLE uses we do take into account
1986 // that the rectangle is used up to, but not including, the right column and
1987 // bottom row of pixels, see the Win32 documentation on RECT for more details
1989 HRESULT CBaseControlVideo::CheckSourceRect(RECT *pSourceRect)
1991 CheckPointer(pSourceRect,E_POINTER);
1992 LONG Width,Height;
1993 GetVideoSize(&Width,&Height);
1995 // Check the coordinates are greater than zero
1996 // and that the rectangle is valid (left<right, top<bottom)
1998 if ((pSourceRect->left >= pSourceRect->right) ||
1999 (pSourceRect->left < 0) ||
2000 (pSourceRect->top >= pSourceRect->bottom) ||
2001 (pSourceRect->top < 0)) {
2003 return E_INVALIDARG;
2006 // Check the coordinates are less than the extents
2008 if ((pSourceRect->right > Width) ||
2009 (pSourceRect->bottom > Height)) {
2011 return E_INVALIDARG;
2013 return NOERROR;
2017 // Check the target rectangle has some valid coordinates, which amounts to
2018 // little more than checking the destination rectangle isn't empty. Derived
2019 // classes may call this when they have their SetTargetRect method called to
2020 // check the rectangle validity, we do not update the rectangles passed in
2021 // Although we store the rectangle in left, top, right and bottom coordinates
2022 // instead of left, top, width and height as OLE uses we do take into account
2023 // that the rectangle is used up to, but not including, the right column and
2024 // bottom row of pixels, see the Win32 documentation on RECT for more details
2026 HRESULT CBaseControlVideo::CheckTargetRect(RECT *pTargetRect)
2028 // Check the pointer is valid
2030 if (pTargetRect == NULL) {
2031 return E_POINTER;
2034 // These overflow the WIDTH and HEIGHT checks
2036 if (pTargetRect->left > pTargetRect->right ||
2037 pTargetRect->top > pTargetRect->bottom) {
2038 return E_INVALIDARG;
2041 // Check the rectangle has valid coordinates
2043 if (WIDTH(pTargetRect) <= 0 || HEIGHT(pTargetRect) <= 0) {
2044 return E_INVALIDARG;
2047 ASSERT(IsRectEmpty(pTargetRect) == FALSE);
2048 return NOERROR;