forked from krkrz/baseclasses
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwinutil.cpp
2746 lines (2113 loc) · 91.5 KB
/
winutil.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
// File: WinUtil.cpp
//
// Desc: DirectShow base classes - implements generic window handler class.
//
// Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------
#include <streams.h>
#include <limits.h>
#include <dvdmedia.h>
#include <strsafe.h>
#include <checkbmi.h>
static UINT MsgDestroy;
// Constructor
CBaseWindow::CBaseWindow(BOOL bDoGetDC, bool bDoPostToDestroy) :
m_hInstance(g_hInst),
m_hwnd(NULL),
m_hdc(NULL),
m_bActivated(FALSE),
m_pClassName(NULL),
m_ClassStyles(0),
m_WindowStyles(0),
m_WindowStylesEx(0),
m_ShowStageMessage(0),
m_ShowStageTop(0),
m_MemoryDC(NULL),
m_hPalette(NULL),
m_bBackground(FALSE),
#ifdef DEBUG
m_bRealizing(FALSE),
#endif
m_bNoRealize(FALSE),
m_bDoPostToDestroy(bDoPostToDestroy)
{
m_bDoGetDC = bDoGetDC;
}
// Prepare a window by spinning off a worker thread to do the creation and
// also poll the message input queue. We leave this to be called by derived
// classes because they might want to override methods like MessageLoop and
// InitialiseWindow, if we do this during construction they'll ALWAYS call
// this base class methods. We make the worker thread create the window so
// it owns it rather than the filter graph thread which is constructing us
HRESULT CBaseWindow::PrepareWindow()
{
if (m_hwnd) return NOERROR;
ASSERT(m_hwnd == NULL);
ASSERT(m_hdc == NULL);
// Get the derived object's window and class styles
m_pClassName = GetClassWindowStyles(&m_ClassStyles,
&m_WindowStyles,
&m_WindowStylesEx);
if (m_pClassName == NULL) {
return E_FAIL;
}
// Register our special private messages
m_ShowStageMessage = RegisterWindowMessage(SHOWSTAGE);
// RegisterWindowMessage() returns 0 if an error occurs.
if (0 == m_ShowStageMessage) {
return AmGetLastErrorToHResult();
}
m_ShowStageTop = RegisterWindowMessage(SHOWSTAGETOP);
if (0 == m_ShowStageTop) {
return AmGetLastErrorToHResult();
}
m_RealizePalette = RegisterWindowMessage(REALIZEPALETTE);
if (0 == m_RealizePalette) {
return AmGetLastErrorToHResult();
}
MsgDestroy = RegisterWindowMessage(TEXT("AM_DESTROY"));
if (0 == MsgDestroy) {
return AmGetLastErrorToHResult();
}
return DoCreateWindow();
}
// Destructor just a placeholder so that we know it becomes virtual
// Derived classes MUST call DoneWithWindow in their destructors so
// that no messages arrive after the derived class constructor ends
#ifdef DEBUG
CBaseWindow::~CBaseWindow()
{
ASSERT(m_hwnd == NULL);
ASSERT(m_hdc == NULL);
}
#endif
// We use the sync worker event to have the window destroyed. All we do is
// signal the event and wait on the window thread handle. Trying to send it
// messages causes too many problems, furthermore to be on the safe side we
// just wait on the thread handle while it returns WAIT_TIMEOUT or there is
// a sent message to process on this thread. If the constructor failed to
// create the thread in the first place then the loop will get terminated
HRESULT CBaseWindow::DoneWithWindow()
{
if (!IsWindow(m_hwnd) || (GetWindowThreadProcessId(m_hwnd, NULL) != GetCurrentThreadId())) {
if (IsWindow(m_hwnd)) {
// This code should only be executed if the window exists and if the window's
// messages are processed on a different thread.
ASSERT(GetWindowThreadProcessId(m_hwnd, NULL) != GetCurrentThreadId());
if (m_bDoPostToDestroy) {
HRESULT hr = S_OK;
CAMEvent m_evDone(FALSE, &hr);
if (FAILED(hr)) {
return hr;
}
// We must post a message to destroy the window
// That way we can't be in the middle of processing a
// message posted to our window when we do go away
// Sending a message gives less synchronization.
PostMessage(m_hwnd, MsgDestroy, (WPARAM)(HANDLE)m_evDone, 0);
WaitDispatchingMessages(m_evDone, INFINITE);
} else {
SendMessage(m_hwnd, MsgDestroy, 0, 0);
}
}
//
// This is not a leak, the window manager automatically free's
// hdc's that were got via GetDC, which is the case here.
// We set it to NULL so that we don't get any asserts later.
//
m_hdc = NULL;
//
// We need to free this DC though because USER32 does not know
// anything about it.
//
if (m_MemoryDC)
{
EXECUTE_ASSERT(DeleteDC(m_MemoryDC));
m_MemoryDC = NULL;
}
// Reset the window variables
m_hwnd = NULL;
return NOERROR;
}
const HWND hwnd = m_hwnd;
if (hwnd == NULL) {
return NOERROR;
}
InactivateWindow();
NOTE("Inactivated");
// Reset the window styles before destruction
SetWindowLong(hwnd,GWL_STYLE,m_WindowStyles);
ASSERT(GetParent(hwnd) == NULL);
NOTE1("Reset window styles %d",m_WindowStyles);
// UnintialiseWindow sets m_hwnd to NULL so save a copy
UninitialiseWindow();
DbgLog((LOG_TRACE, 2, TEXT("Destroying 0x%8.8X"), hwnd));
if (!DestroyWindow(hwnd)) {
DbgLog((LOG_TRACE, 0, TEXT("DestroyWindow %8.8X failed code %d"),
hwnd, GetLastError()));
DbgBreak("");
}
// Reset our state so we can be prepared again
m_pClassName = NULL;
m_ClassStyles = 0;
m_WindowStyles = 0;
m_WindowStylesEx = 0;
m_ShowStageMessage = 0;
m_ShowStageTop = 0;
return NOERROR;
}
// Called at the end to put the window in an inactive state. The pending list
// will always have been cleared by this time so event if the worker thread
// gets has been signaled and gets in to render something it will find both
// the state has been changed and that there are no available sample images
// Since we wait on the window thread to complete we don't lock the object
HRESULT CBaseWindow::InactivateWindow()
{
// Has the window been activated
if (m_bActivated == FALSE) {
return S_FALSE;
}
m_bActivated = FALSE;
ShowWindow(m_hwnd,SW_HIDE);
return NOERROR;
}
HRESULT CBaseWindow::CompleteConnect()
{
m_bActivated = FALSE;
return NOERROR;
}
// This displays a normal window. We ask the base window class for default
// sizes which unless overriden will return DEFWIDTH and DEFHEIGHT. We go
// through a couple of extra hoops to get the client area the right size
// as the object specifies which accounts for the AdjustWindowRectEx calls
// We also DWORD align the left and top coordinates of the window here to
// maximise the chance of being able to use DCI/DirectDraw primary surface
HRESULT CBaseWindow::ActivateWindow()
{
// Has the window been sized and positioned already
if (m_bActivated == TRUE || GetParent(m_hwnd) != NULL) {
SetWindowPos(m_hwnd, // Our window handle
HWND_TOP, // Put it at the top
0, 0, 0, 0, // Leave in current position
SWP_NOMOVE | // Don't change it's place
SWP_NOSIZE); // Change Z-order only
m_bActivated = TRUE;
return S_FALSE;
}
// Calculate the desired client rectangle
RECT WindowRect, ClientRect = GetDefaultRect();
GetWindowRect(m_hwnd,&WindowRect);
AdjustWindowRectEx(&ClientRect,GetWindowLong(m_hwnd,GWL_STYLE),
FALSE,GetWindowLong(m_hwnd,GWL_EXSTYLE));
// Align left and top edges on DWORD boundaries
UINT WindowFlags = (SWP_NOACTIVATE | SWP_FRAMECHANGED);
WindowRect.left -= (WindowRect.left & 3);
WindowRect.top -= (WindowRect.top & 3);
SetWindowPos(m_hwnd, // Window handle
HWND_TOP, // Put it at the top
WindowRect.left, // Align left edge
WindowRect.top, // And also top place
WIDTH(&ClientRect), // Horizontal size
HEIGHT(&ClientRect), // Vertical size
WindowFlags); // Don't show window
m_bActivated = TRUE;
return NOERROR;
}
// This can be used to DWORD align the window for maximum performance
HRESULT CBaseWindow::PerformanceAlignWindow()
{
RECT ClientRect,WindowRect;
GetWindowRect(m_hwnd,&WindowRect);
ASSERT(m_bActivated == TRUE);
// Don't do this if we're owned
if (GetParent(m_hwnd)) {
return NOERROR;
}
// Align left and top edges on DWORD boundaries
GetClientRect(m_hwnd, &ClientRect);
MapWindowPoints(m_hwnd, HWND_DESKTOP, (LPPOINT) &ClientRect, 2);
WindowRect.left -= (ClientRect.left & 3);
WindowRect.top -= (ClientRect.top & 3);
UINT WindowFlags = (SWP_NOACTIVATE | SWP_NOSIZE);
SetWindowPos(m_hwnd, // Window handle
HWND_TOP, // Put it at the top
WindowRect.left, // Align left edge
WindowRect.top, // And also top place
(int) 0,(int) 0, // Ignore these sizes
WindowFlags); // Don't show window
return NOERROR;
}
// Install a palette into the base window - we may be called by a different
// thread to the one that owns the window. We have to be careful how we do
// the palette realisation as we could be a different thread to the window
// which would cause an inter thread send message. Therefore we realise the
// palette by sending it a special message but without the window locked
HRESULT CBaseWindow::SetPalette(HPALETTE hPalette)
{
// We must own the window lock during the change
{
CAutoLock cWindowLock(&m_WindowLock);
CAutoLock cPaletteLock(&m_PaletteLock);
ASSERT(hPalette);
m_hPalette = hPalette;
}
return SetPalette();
}
HRESULT CBaseWindow::SetPalette()
{
if (!m_bNoRealize) {
SendMessage(m_hwnd, m_RealizePalette, 0, 0);
return S_OK;
} else {
// Just select the palette
ASSERT(m_hdc);
ASSERT(m_MemoryDC);
CAutoLock cPaletteLock(&m_PaletteLock);
SelectPalette(m_hdc,m_hPalette,m_bBackground);
SelectPalette(m_MemoryDC,m_hPalette,m_bBackground);
return S_OK;
}
}
void CBaseWindow::UnsetPalette()
{
CAutoLock cWindowLock(&m_WindowLock);
CAutoLock cPaletteLock(&m_PaletteLock);
// Get a standard VGA colour palette
HPALETTE hPalette = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
ASSERT(hPalette);
SelectPalette(GetWindowHDC(), hPalette, TRUE);
SelectPalette(GetMemoryHDC(), hPalette, TRUE);
m_hPalette = NULL;
}
void CBaseWindow::LockPaletteLock()
{
m_PaletteLock.Lock();
}
void CBaseWindow::UnlockPaletteLock()
{
m_PaletteLock.Unlock();
}
// Realise our palettes in the window and device contexts
HRESULT CBaseWindow::DoRealisePalette(BOOL bForceBackground)
{
{
CAutoLock cPaletteLock(&m_PaletteLock);
if (m_hPalette == NULL) {
return NOERROR;
}
// Realize the palette on the window thread
ASSERT(m_hdc);
ASSERT(m_MemoryDC);
SelectPalette(m_hdc,m_hPalette,m_bBackground || bForceBackground);
SelectPalette(m_MemoryDC,m_hPalette,m_bBackground);
}
// If we grab a critical section here we can deadlock
// with the window thread because one of the side effects
// of RealizePalette is to send a WM_PALETTECHANGED message
// to every window in the system. In our handling
// of WM_PALETTECHANGED we used to grab this CS too.
// The really bad case is when our renderer calls DoRealisePalette()
// while we're in the middle of processing a palette change
// for another window.
// So don't hold the critical section while actually realising
// the palette. In any case USER is meant to manage palette
// handling - we shouldn't have to serialize everything as well
ASSERT(CritCheckOut(&m_WindowLock));
ASSERT(CritCheckOut(&m_PaletteLock));
EXECUTE_ASSERT(RealizePalette(m_hdc) != GDI_ERROR);
EXECUTE_ASSERT(RealizePalette(m_MemoryDC) != GDI_ERROR);
return (GdiFlush() == FALSE ? S_FALSE : S_OK);
}
// This is the global window procedure
LRESULT CALLBACK WndProc(HWND hwnd, // Window handle
UINT uMsg, // Message ID
WPARAM wParam, // First parameter
LPARAM lParam) // Other parameter
{
// Get the window long that holds our window object pointer
// If it is NULL then we are initialising the window in which
// case the object pointer has been passed in the window creation
// structure. IF we get any messages before WM_NCCREATE we will
// pass them to DefWindowProc.
CBaseWindow *pBaseWindow = _GetWindowLongPtr<CBaseWindow*>(hwnd,0);
if (pBaseWindow == NULL) {
// Get the structure pointer from the create struct.
// We can only do this for WM_NCCREATE which should be one of
// the first messages we receive. Anything before this will
// have to be passed to DefWindowProc (i.e. WM_GETMINMAXINFO)
// If the message is WM_NCCREATE we set our pBaseWindow pointer
// and will then place it in the window structure
// turn off WS_EX_LAYOUTRTL style for quartz windows
if (uMsg == WM_NCCREATE) {
SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) & ~0x400000);
}
if ((uMsg != WM_NCCREATE)
|| (NULL == (pBaseWindow = *(CBaseWindow**) ((LPCREATESTRUCT)lParam)->lpCreateParams)))
{
return(DefWindowProc(hwnd, uMsg, wParam, lParam));
}
// Set the window LONG to be the object who created us
#ifdef DEBUG
SetLastError(0); // because of the way SetWindowLong works
#endif
LONG_PTR rc = _SetWindowLongPtr(hwnd, (DWORD) 0, pBaseWindow);
#ifdef DEBUG
if (0 == rc) {
// SetWindowLong MIGHT have failed. (Read the docs which admit
// that it is awkward to work out if you have had an error.)
LONG lasterror = GetLastError();
ASSERT(0 == lasterror);
// If this is not the case we have not set the pBaseWindow pointer
// into the window structure and we will blow up.
}
#endif
}
// See if this is the packet of death
if (uMsg == MsgDestroy && uMsg != 0) {
pBaseWindow->DoneWithWindow();
if (pBaseWindow->m_bDoPostToDestroy) {
EXECUTE_ASSERT(SetEvent((HANDLE)wParam));
}
return 0;
}
return pBaseWindow->OnReceiveMessage(hwnd,uMsg,wParam,lParam);
}
// When the window size changes we adjust our member variables that
// contain the dimensions of the client rectangle for our window so
// that we come to render an image we will know whether to stretch
BOOL CBaseWindow::OnSize(LONG Width, LONG Height)
{
m_Width = Width;
m_Height = Height;
return TRUE;
}
// This function handles the WM_CLOSE message
BOOL CBaseWindow::OnClose()
{
ShowWindow(m_hwnd,SW_HIDE);
return TRUE;
}
// This is called by the worker window thread when it receives a terminate
// message from the window object destructor to delete all the resources we
// allocated during initialisation. By the time the worker thread exits all
// processing will have been completed as the source filter disconnection
// flushes the image pending sample, therefore the GdiFlush should succeed
HRESULT CBaseWindow::UninitialiseWindow()
{
// Have we already cleaned up
if (m_hwnd == NULL) {
ASSERT(m_hdc == NULL);
ASSERT(m_MemoryDC == NULL);
return NOERROR;
}
// Release the window resources
EXECUTE_ASSERT(GdiFlush());
if (m_hdc)
{
EXECUTE_ASSERT(ReleaseDC(m_hwnd,m_hdc));
m_hdc = NULL;
}
if (m_MemoryDC)
{
EXECUTE_ASSERT(DeleteDC(m_MemoryDC));
m_MemoryDC = NULL;
}
// Reset the window variables
m_hwnd = NULL;
return NOERROR;
}
// This is called by the worker window thread after it has created the main
// window and it wants to initialise the rest of the owner objects window
// variables such as the device contexts. We execute this function with the
// critical section still locked. Nothing in this function must generate any
// SendMessage calls to the window because this is executing on the window
// thread so the message will never be processed and we will deadlock
HRESULT CBaseWindow::InitialiseWindow(HWND hwnd)
{
// Initialise the window variables
ASSERT(IsWindow(hwnd));
m_hwnd = hwnd;
if (m_bDoGetDC)
{
EXECUTE_ASSERT(m_hdc = GetDC(hwnd));
EXECUTE_ASSERT(m_MemoryDC = CreateCompatibleDC(m_hdc));
EXECUTE_ASSERT(SetStretchBltMode(m_hdc,COLORONCOLOR));
EXECUTE_ASSERT(SetStretchBltMode(m_MemoryDC,COLORONCOLOR));
}
return NOERROR;
}
HRESULT CBaseWindow::DoCreateWindow()
{
WNDCLASS wndclass; // Used to register classes
BOOL bRegistered; // Is this class registered
HWND hwnd; // Handle to our window
bRegistered = GetClassInfo(m_hInstance, // Module instance
m_pClassName, // Window class
&wndclass); // Info structure
// if the window is to be used for drawing puposes and we are getting a DC
// for the entire lifetime of the window then changes the class style to do
// say so. If we don't set this flag then the DC comes from the cache and is
// really bad.
if (m_bDoGetDC)
{
m_ClassStyles |= CS_OWNDC;
}
if (bRegistered == FALSE) {
// Register the renderer window class
wndclass.lpszClassName = m_pClassName;
wndclass.style = m_ClassStyles;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = sizeof(CBaseWindow *);
wndclass.hInstance = m_hInstance;
wndclass.hIcon = NULL;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) NULL;
wndclass.lpszMenuName = NULL;
RegisterClass(&wndclass);
}
// Create the frame window. Pass the pBaseWindow information in the
// CreateStruct which allows our message handling loop to get hold of
// the pBaseWindow pointer.
CBaseWindow *pBaseWindow = this; // The owner window object
hwnd = CreateWindowEx(m_WindowStylesEx, // Extended styles
m_pClassName, // Registered name
TEXT("ActiveMovie Window"), // Window title
m_WindowStyles, // Window styles
CW_USEDEFAULT, // Start x position
CW_USEDEFAULT, // Start y position
DEFWIDTH, // Window width
DEFHEIGHT, // Window height
NULL, // Parent handle
NULL, // Menu handle
m_hInstance, // Instance handle
&pBaseWindow); // Creation data
// If we failed signal an error to the object constructor (based on the
// last Win32 error on this thread) then signal the constructor thread
// to continue, release the mutex to let others have a go and exit
if (hwnd == NULL) {
DWORD Error = GetLastError();
return AmHresultFromWin32(Error);
}
// Check the window LONG is the object who created us
ASSERT(GetWindowLongPtr(hwnd, 0) == (LONG_PTR)this);
// Initialise the window and then signal the constructor so that it can
// continue and then finally unlock the object's critical section. The
// window class is left registered even after we terminate the thread
// as we don't know when the last window has been closed. So we allow
// the operating system to free the class resources as appropriate
InitialiseWindow(hwnd);
DbgLog((LOG_TRACE, 2, TEXT("Created window class (%s) HWND(%8.8X)"),
m_pClassName, hwnd));
return S_OK;
}
// The base class provides some default handling and calls DefWindowProc
LRESULT CBaseWindow::OnReceiveMessage(HWND hwnd, // Window handle
UINT uMsg, // Message ID
WPARAM wParam, // First parameter
LPARAM lParam) // Other parameter
{
ASSERT(IsWindow(hwnd));
if (PossiblyEatMessage(uMsg, wParam, lParam))
return 0;
// This is sent by the IVideoWindow SetWindowForeground method. If the
// window is invisible we will show it and make it topmost without the
// foreground focus. If the window is visible it will also be made the
// topmost window without the foreground focus. If wParam is TRUE then
// for both cases the window will be forced into the foreground focus
if (uMsg == m_ShowStageMessage) {
BOOL bVisible = IsWindowVisible(hwnd);
SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW |
(bVisible ? SWP_NOACTIVATE : 0));
// Should we bring the window to the foreground
if (wParam == TRUE) {
SetForegroundWindow(hwnd);
}
return (LRESULT) 1;
}
// When we go fullscreen we have to add the WS_EX_TOPMOST style to the
// video window so that it comes out above any task bar (this is more
// relevant to WindowsNT than Windows95). However the SetWindowPos call
// must be on the same thread as that which created the window. The
// wParam parameter can be TRUE or FALSE to set and reset the topmost
if (uMsg == m_ShowStageTop) {
HWND HwndTop = (wParam == TRUE ? HWND_TOPMOST : HWND_NOTOPMOST);
BOOL bVisible = IsWindowVisible(hwnd);
SetWindowPos(hwnd, HwndTop, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE |
(wParam == TRUE ? SWP_SHOWWINDOW : 0) |
(bVisible ? SWP_NOACTIVATE : 0));
return (LRESULT) 1;
}
// New palette stuff
if (uMsg == m_RealizePalette) {
ASSERT(m_hwnd == hwnd);
return OnPaletteChange(m_hwnd,WM_QUERYNEWPALETTE);
}
switch (uMsg) {
// Repaint the window if the system colours change
case WM_SYSCOLORCHANGE:
InvalidateRect(hwnd,NULL,FALSE);
return (LRESULT) 1;
// Somebody has changed the palette
case WM_PALETTECHANGED:
OnPaletteChange((HWND)wParam,uMsg);
return (LRESULT) 0;
// We are about to receive the keyboard focus so we ask GDI to realise
// our logical palette again and hopefully it will be fully installed
// without any mapping having to be done during any picture rendering
case WM_QUERYNEWPALETTE:
ASSERT(m_hwnd == hwnd);
return OnPaletteChange(m_hwnd,uMsg);
// do NOT fwd WM_MOVE. the parameters are the location of the parent
// window, NOT what the renderer should be looking at. But we need
// to make sure the overlay is moved with the parent window, so we
// do this.
case WM_MOVE:
if (IsWindowVisible(m_hwnd)) {
PostMessage(m_hwnd,WM_PAINT,0,0);
}
break;
// Store the width and height as useful base class members
case WM_SIZE:
OnSize(LOWORD(lParam), HIWORD(lParam));
return (LRESULT) 0;
// Intercept the WM_CLOSE messages to hide the window
case WM_CLOSE:
OnClose();
return (LRESULT) 0;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
// This handles the Windows palette change messages - if we do realise our
// palette then we return TRUE otherwise we return FALSE. If our window is
// foreground application then we should get first choice of colours in the
// system palette entries. We get best performance when our logical palette
// includes the standard VGA colours (at the beginning and end) otherwise
// GDI may have to map from our palette to the device palette while drawing
LRESULT CBaseWindow::OnPaletteChange(HWND hwnd,UINT Message)
{
// First check we are not changing the palette during closedown
if (m_hwnd == NULL || hwnd == NULL) {
return (LRESULT) 0;
}
ASSERT(!m_bRealizing);
// Should we realise our palette again
if ((Message == WM_QUERYNEWPALETTE || hwnd != m_hwnd)) {
// It seems that even if we're invisible that we can get asked
// to realize our palette and this can cause really ugly side-effects
// Seems like there's another bug but this masks it a least for the
// shutting down case.
if (!IsWindowVisible(m_hwnd)) {
DbgLog((LOG_TRACE, 1, TEXT("Realizing when invisible!")));
return (LRESULT) 0;
}
// Avoid recursion with multiple graphs in the same app
#ifdef DEBUG
m_bRealizing = TRUE;
#endif
DoRealisePalette(Message != WM_QUERYNEWPALETTE);
#ifdef DEBUG
m_bRealizing = FALSE;
#endif
// Should we redraw the window with the new palette
if (Message == WM_PALETTECHANGED) {
InvalidateRect(m_hwnd,NULL,FALSE);
}
}
return (LRESULT) 1;
}
// Determine if the window exists.
bool CBaseWindow::WindowExists()
{
return !!IsWindow(m_hwnd);
}
// Return the default window rectangle
RECT CBaseWindow::GetDefaultRect()
{
RECT DefaultRect = {0,0,DEFWIDTH,DEFHEIGHT};
ASSERT(m_hwnd);
// ASSERT(m_hdc);
return DefaultRect;
}
// Return the current window width
LONG CBaseWindow::GetWindowWidth()
{
ASSERT(m_hwnd);
// ASSERT(m_hdc);
return m_Width;
}
// Return the current window height
LONG CBaseWindow::GetWindowHeight()
{
ASSERT(m_hwnd);
// ASSERT(m_hdc);
return m_Height;
}
// Return the window handle
HWND CBaseWindow::GetWindowHWND()
{
ASSERT(m_hwnd);
// ASSERT(m_hdc);
return m_hwnd;
}
// Return the window drawing device context
HDC CBaseWindow::GetWindowHDC()
{
ASSERT(m_hwnd);
ASSERT(m_hdc);
return m_hdc;
}
// Return the offscreen window drawing device context
HDC CBaseWindow::GetMemoryHDC()
{
ASSERT(m_hwnd);
ASSERT(m_MemoryDC);
return m_MemoryDC;
}
#ifdef DEBUG
HPALETTE CBaseWindow::GetPalette()
{
// The palette lock should always be held when accessing
// m_hPalette.
ASSERT(CritCheckIn(&m_PaletteLock));
return m_hPalette;
}
#endif // DEBUG
// This is available to clients who want to change the window visiblity. It's
// little more than an indirection to the Win32 ShowWindow although these is
// some benefit in going through here as this function may change sometime
HRESULT CBaseWindow::DoShowWindow(LONG ShowCmd)
{
ShowWindow(m_hwnd,ShowCmd);
return NOERROR;
}
// Generate a WM_PAINT message for the video window
void CBaseWindow::PaintWindow(BOOL bErase)
{
InvalidateRect(m_hwnd,NULL,bErase);
}
// Allow an application to have us set the video window in the foreground. We
// have this because it is difficult for one thread to do do this to a window
// owned by another thread. Rather than expose the message we use to execute
// the inter thread send message we provide the interface function. All we do
// is to SendMessage to the video window renderer thread with a WM_SHOWSTAGE
void CBaseWindow::DoSetWindowForeground(BOOL bFocus)
{
SendMessage(m_hwnd,m_ShowStageMessage,(WPARAM) bFocus,(LPARAM) 0);
}
// Constructor initialises the owning object pointer. Since we are a worker
// class for the main window object we have relatively few state variables to
// look after. We are given device context handles to use later on as well as
// the source and destination rectangles (but reset them here just in case)
CDrawImage::CDrawImage(__inout CBaseWindow *pBaseWindow) :
m_pBaseWindow(pBaseWindow),
m_hdc(NULL),
m_MemoryDC(NULL),
m_bStretch(FALSE),
m_pMediaType(NULL),
m_bUsingImageAllocator(FALSE)
{
ASSERT(pBaseWindow);
ResetPaletteVersion();
SetRectEmpty(&m_TargetRect);
SetRectEmpty(&m_SourceRect);
m_perfidRenderTime = MSR_REGISTER(TEXT("Single Blt time"));
}
// Overlay the image time stamps on the picture. Access to this method is
// serialised by the caller. We display the sample start and end times on
// top of the video using TextOut on the device context we are handed. If
// there isn't enough room in the window for the times we don't show them
void CDrawImage::DisplaySampleTimes(IMediaSample *pSample)
{
#ifdef DEBUG
//
// Only allow the "annoying" time messages if the users has turned the
// logging "way up"
//
BOOL bAccept = DbgCheckModuleLevel(LOG_TRACE, 5);
if (bAccept == FALSE) {
return;
}
#endif
TCHAR szTimes[TIMELENGTH]; // Time stamp strings
ASSERT(pSample); // Quick sanity check
RECT ClientRect; // Client window size
SIZE Size; // Size of text output
// Get the time stamps and window size
pSample->GetTime((REFERENCE_TIME*)&m_StartSample, (REFERENCE_TIME*)&m_EndSample);
HWND hwnd = m_pBaseWindow->GetWindowHWND();
EXECUTE_ASSERT(GetClientRect(hwnd,&ClientRect));
// Format the sample time stamps
(void)StringCchPrintf(szTimes,NUMELMS(szTimes),TEXT("%08d : %08d"),
m_StartSample.Millisecs(),
m_EndSample.Millisecs());
ASSERT(lstrlen(szTimes) < TIMELENGTH);
// Put the times in the middle at the bottom of the window
GetTextExtentPoint32(m_hdc,szTimes,lstrlen(szTimes),&Size);
INT XPos = ((ClientRect.right - ClientRect.left) - Size.cx) / 2;
INT YPos = ((ClientRect.bottom - ClientRect.top) - Size.cy) * 4 / 5;
// Check the window is big enough to have sample times displayed
if ((XPos > 0) && (YPos > 0)) {
TextOut(m_hdc,XPos,YPos,szTimes,lstrlen(szTimes));
}
}
// This is called when the drawing code sees that the image has a down level
// palette cookie. We simply call the SetDIBColorTable Windows API with the
// palette that is found after the BITMAPINFOHEADER - we return no errors
void CDrawImage::UpdateColourTable(HDC hdc,__in BITMAPINFOHEADER *pbmi)
{
ASSERT(pbmi->biClrUsed);
RGBQUAD *pColourTable = (RGBQUAD *)(pbmi+1);
// Set the new palette in the device context
UINT uiReturn = SetDIBColorTable(hdc,(UINT) 0,
pbmi->biClrUsed,
pColourTable);