0022627: Change OCCT memory management defaults
[occt.git] / src / WNT / WNT_Window.cxx
CommitLineData
7fd59977 1// File: WNT_Window.cxx
2// Created:
3// Author:
4// Modifications: PLOTNIKOV Eugeny at July 1998 (BUC60286)
5// TCL at October 2000 (G002)
6// new method SetBackground(CString aName)
7// Copyright: Matra Datavision 1998
8
9// include windows.h first to have all definitions available
10#include <windows.h>
11
12// GG 07/03/00 Add MMSize() method
13
14#include <WNT_Window.ixx>
15
16#include <Image_PixMap.hxx>
17#include <Aspect_Convert.hxx>
18
19#include <stdio.h>
20extern "C" void _debug_break ( char* );
21//************************************************************************//
22//***//
23// callback function to manage window's background
24extern LRESULT CALLBACK WNT_WndProc (
25 HWND, UINT, WPARAM, LPARAM
26 );
27//***//
28#define SELECT_PALETTE(hDC) \
29 HPALETTE hOldPal; \
30 \
31 if ( myExtraData.hPal ) { \
32 \
33 hOldPal = SelectPalette ( hDC, myExtraData.hPal, FALSE ); \
34 \
35 if ( RealizePalette ( hDC ) ) \
36 \
37 UpdateColors ( hDC ); \
38 \
39 } // end if
40//***//
41#define UNSELECT_PALETTE(hDC) \
42 if ( myExtraData.hPal ) \
43 \
44 SelectPalette ( hDC, hOldPal, FALSE );
45
46HBITMAP LoadImageFromFile ( Handle( WNT_GraphicDevice )&, char*, HDC = NULL );
47
48//***//
49//************************* Constructor **********************************//
50//***//
51// TODO - Here is enough of duplicated code in 1st and second constructors!
52WNT_Window::WNT_Window (const Handle(WNT_GraphicDevice)& theDevice,
53 const Standard_CString theTitle,
54 const Handle(WNT_WClass)& theClass,
55 const WNT_Dword& theStyle,
56 const Quantity_Parameter theXc,
57 const Quantity_Parameter theYc,
58 const Quantity_Parameter theWidth,
59 const Quantity_Parameter theHeight,
60 const Quantity_NameOfColor theBackColor,
61 const Aspect_Handle theParent,
62 const Aspect_Handle theMenu,
63 const Standard_Address theClientStruct)
64: Aspect_Window (theDevice),
65 myWClass (theClass)
66{
67 ZeroMemory (&myExtraData, sizeof (WNT_WindowData));
68 DWORD dwStyle = theStyle;
69 const Handle(WNT_GraphicDevice)& aDev = Handle(WNT_GraphicDevice)::DownCast (MyGraphicDevice);
70
71 if (theXc < 0.0 || theXc > 1.0 || theYc < 0.0 || theYc > 1.0 ||
72 theWidth <= 0.0 || theHeight <= 0.0)
73 {
74 Aspect_WindowDefinitionError::Raise ("Coordinate(s) out of range");
75 }
76
77 Standard_Integer aParentSizeX, aParentSizeY;
78 if (theParent)
79 {
80 RECT aRect;
81 GetClientRect ((HWND )theParent, &aRect);
82 aParentSizeX = aRect.right;
83 aParentSizeY = aRect.bottom;
84 }
85 else
86 {
87 aDev->DisplaySize (aParentSizeX, aParentSizeY);
88 }
89
90 Standard_Integer aPxWidth, aPxHeight;
91 Aspect_Convert::ConvertCoordinates (aParentSizeX, aParentSizeY,
92 theXc, theYc, theWidth, theHeight,
93 aXLeft, aYTop, aPxWidth, aPxHeight);
94 Aspect_Convert::FitIn (aParentSizeX, aParentSizeY,
95 aXLeft, aYTop, aPxWidth, aPxHeight);
96 aXRight = aXLeft + aPxWidth;
97 aYBottom = aYTop + aPxHeight;
98
99 if (theParent && !(theStyle & WS_CHILD))
100 {
101 dwStyle |= WS_CHILD | WS_CLIPSIBLINGS;
102 }
103 else if (!theParent && !(theStyle & WS_CLIPCHILDREN))
104 {
105 dwStyle |= WS_CLIPCHILDREN;
106 }
107
108 if (dwStyle & WS_OVERLAPPEDWINDOW)
109 {
110 // include decorations in the window dimensions
111 // to reproduce same behaviour of Xw_Window.
112 aXLeft -= GetSystemMetrics(SM_CXSIZEFRAME);
113 aYTop -= GetSystemMetrics(SM_CYSIZEFRAME) + GetSystemMetrics(SM_CYCAPTION);
114 aXRight += GetSystemMetrics(SM_CXSIZEFRAME);
115 aYBottom += GetSystemMetrics(SM_CYSIZEFRAME);
116 }
117
118 myHWindow = CreateWindow (
119 myWClass->Name(), // window's class name
120 theTitle, // window's name
121 dwStyle, // window's style
122 aXLeft, aYTop, // window's coordinates
123 (aXRight - aXLeft), (aYBottom - aYTop),
124 (HWND )theParent, // window's parent
125 (HMENU )theMenu, // window's menu
126 (HINSTANCE )myWClass->Instance(), // application's instance
127 theClientStruct); // pointer to CLIENTCREATESTRUCT
128 if (!myHWindow)
129 {
130 Aspect_WindowDefinitionError::Raise ("Unable to create window");
131 }
132
133 myDoubleBuffer = Standard_False;
134 myHPixmap = 0;
135 myHParentWindow = theParent;
136 myWndProc = myWClass->WndProc();
137 SetBackground (theBackColor);
138 SetWindowLongPtr ((HWND )myHWindow, GWLP_WNDPROC, (LONG_PTR )WNT_WndProc);
139 myUsrData = (Standard_Address )SetWindowLongPtr ((HWND )myHWindow, GWLP_USERDATA, (LONG_PTR )&myExtraData);
140
141 myExtraData.WNT_Window_Ptr = (void* )this;
142 myExtraData.hPal = (HPALETTE )aDev->HPalette();
143
144 myImages = new WNT_ImageManager (this);
145 myFormat = WNT_TOI_XWD;
146
147 SetFlags(WDF_NOERASEBKGRND);
148}
149
150WNT_Window::WNT_Window (const Handle(WNT_GraphicDevice)& theDevice,
151 const Standard_CString theTitle,
152 const Handle(WNT_WClass)& theClass,
153 const WNT_Dword& theStyle,
154 const Standard_Integer thePxLeft,
155 const Standard_Integer thePxTop,
156 const Standard_Integer thePxWidth,
157 const Standard_Integer thePxHeight,
158 const Quantity_NameOfColor theBackColor,
159 const Aspect_Handle theParent,
160 const Aspect_Handle theMenu,
161 const Standard_Address theClientStruct)
162: Aspect_Window (theDevice),
163 aXLeft (thePxLeft),
164 aYTop (thePxTop),
165 aXRight (thePxLeft + thePxWidth),
166 aYBottom (thePxTop + thePxHeight),
167 myWClass (theClass)
168{
169 ZeroMemory (&myExtraData, sizeof (WNT_WindowData));
170 DWORD dwStyle = theStyle;
171 const Handle(WNT_GraphicDevice)& aDev = Handle(WNT_GraphicDevice)::DownCast (MyGraphicDevice);
172
173 if (thePxWidth <= 0 || thePxHeight <= 0)
174 {
175 Aspect_WindowDefinitionError::Raise ("Coordinate(s) out of range");
176 }
177
178 if (theParent && !(theStyle & WS_CHILD))
179 {
180 dwStyle |= WS_CHILD | WS_CLIPSIBLINGS;
181 }
182 else if (!theParent && !(theStyle & WS_CLIPCHILDREN))
183 {
184 dwStyle |= WS_CLIPCHILDREN;
185 }
186
187 if (dwStyle & WS_OVERLAPPEDWINDOW)
188 {
189 // include decorations in the window dimensions
190 // to reproduce same behaviour of Xw_Window.
191 aXLeft -= GetSystemMetrics(SM_CXSIZEFRAME);
192 aYTop -= GetSystemMetrics(SM_CYSIZEFRAME) + GetSystemMetrics(SM_CYCAPTION);
193 aXRight += GetSystemMetrics(SM_CXSIZEFRAME);
194 aYBottom += GetSystemMetrics(SM_CYSIZEFRAME);
195 }
196
197 myHWindow = CreateWindow (
198 myWClass->Name(), // window's class name
199 theTitle, // window's name
200 dwStyle, // window's style
201 aXLeft, aYTop, // window's coordinates
202 (aXRight - aXLeft), (aYBottom - aYTop),
203 (HWND )theParent, // window's parent
204 (HMENU )theMenu, // window's menu
205 (HINSTANCE )myWClass->Instance(), // application's instance
206 theClientStruct); // pointer to CLIENTCREATESTRUCT
207 if (!myHWindow)
208 {
209 Aspect_WindowDefinitionError::Raise ("Unable to create window");
210 }
211
212 myDoubleBuffer = Standard_False;
213 myHPixmap = 0;
214 myHParentWindow = theParent;
215 myWndProc = myWClass->WndProc();
216 SetBackground (theBackColor);
217 SetWindowLongPtr ((HWND )myHWindow, GWLP_WNDPROC, (LONG_PTR )WNT_WndProc);
218 myUsrData = (Standard_Address )SetWindowLongPtr ((HWND )myHWindow, GWLP_USERDATA, (LONG_PTR )&myExtraData);
219
220 myExtraData.WNT_Window_Ptr = (void* )this;
221 myExtraData.hPal = (HPALETTE )aDev->HPalette();
222
223 myImages = new WNT_ImageManager (this);
224 myFormat = WNT_TOI_XWD;
225
226 SetFlags (WDF_NOERASEBKGRND);
227}
228
229//***//
230//************************* Constructor **********************************//
231//***//
232WNT_Window :: WNT_Window (
233 const Handle( WNT_GraphicDevice )& aDevice,
234 const Aspect_Handle aHandle,
235 const Quantity_NameOfColor aBackColor
236 ) : Aspect_Window ( aDevice ) {
237
238 doCreate ( aDevice, aHandle, aBackColor );
239
240 /* Bug OCC20596 */
241 SetFlags(WDF_NOERASEBKGRND);
242
243} // end constructor
244//***//
245//************************* Constructor **********************************//
246//***//
247WNT_Window :: WNT_Window (
248 const Handle( WNT_GraphicDevice )& aDevice,
249 const Standard_Integer aPart1,
250 const Standard_Integer aPart2,
251 const Quantity_NameOfColor aBackColor
252 ) : Aspect_Window ( aDevice ) {
253
254 Aspect_Handle aHandle = ( Aspect_Handle )( ( aPart1 << 16 ) + aPart2 );
255
256 doCreate ( aDevice, aHandle, aBackColor );
257
258 /* Bug OCC20596 */
259 SetFlags(WDF_NOERASEBKGRND);
260
261} // end constructor
262//***//
263//***************************** Destroy **********************************//
264//***//
265void WNT_Window :: Destroy () {
266
267 if ( MyHBackground ) DeleteObject ( MyHBackground ); // delete background brush
268 if ( myHPixmap ) DeleteObject ( myHPixmap ); // . . . and double buffer pixmap if exists
269
270 if ( myHWindow ) {
271
272 if (myUsrData != Standard_Address(-1))
273 {
274 SetWindowLongPtr ((HWND )myHWindow, GWLP_USERDATA, (LONG_PTR )myUsrData);
275 }
276
277 if (myWndProc != NULL)
278 {
279 SetWindowLongPtr ((HWND )myHWindow, GWLP_WNDPROC, (LONG_PTR )myWndProc);
280 }
281
282 if ( !( myExtraData.dwFlags & WDF_FOREIGN ) )
283
284 DestroyWindow ( ( HWND )myHWindow );
285
286 } // end if
287
288} // end WNT_Window :: Destroy
289//***//
290//************************ SetBackground (1) *****************************//
291//***//
292void WNT_Window :: SetBackground ( const Aspect_Background& Background ) {
293
294 SetBackground ( Background.Color ().Name () );
295
296} // end WNT_Window :: SetBackground
297//***//
298//************************ SetBackground (2) *****************************//
299//***//
300void WNT_Window :: SetBackground ( const Quantity_NameOfColor BackColor ) {
301
302 const Handle( WNT_GraphicDevice )& dev = Handle( WNT_GraphicDevice ) ::
303 DownCast ( MyGraphicDevice );
304
305 if ( MyHBackground )
306 DeleteObject ( MyHBackground );
307
308 MyBackground.SetColor ( BackColor );
309 MyHBackground = CreateSolidBrush (
310 dev -> SetColor ( MyBackground.Color () )
311 );
312
313} // end WNT_Window :: SetBackground
314//***//
315//************************ SetBackground (3) *****************************//
316//***//
317void WNT_Window :: SetBackground ( const Aspect_Handle aBackPixmap ) {
318
319 Aspect_Handle bitmap = CreatePatternBrush ( ( HBITMAP )aBackPixmap );
320
321 if( bitmap ) {
322 if ( MyHBackground ) DeleteObject ( MyHBackground );
323 MyHBackground = bitmap;
324 MyBackgroundImage.Clear();
325 MyBackgroundFillMethod = Aspect_FM_NONE;
326 }
327
328} // end WNT_Window :: SetBackground
329//***//
330//************************ SetBackground (4) *****************************//
331//***//
332Standard_Boolean WNT_Window::SetBackground( const Standard_CString aName,
333 const Aspect_FillMethod aMethod ) {
334 Standard_Boolean status = Standard_False;
335
336 if( !aName ) return Standard_False;
337
338 Handle( WNT_GraphicDevice ) gdev =
339 *( ( Handle_WNT_GraphicDevice * )& MyGraphicDevice );
340 if ( MyHBackground )
341 DeleteObject( MyHBackground );
342 MyHBackground = 0;
343
344 HBITMAP hBmp = LoadImageFromFile( gdev, (Standard_PCharacter)aName );
345
346 if( hBmp ) {
347 status = Standard_True;
348 MyBackgroundImage = aName;
349 MyBackgroundFillMethod = aMethod;
350 MyHBackground = CreatePatternBrush( hBmp );
351 }
352
353 return status;
354} // end WNT_Window :: SetBackground
355 //***//
356//************************ SetBackground (5) *****************************//
357//***//
358void WNT_Window::SetBackground( const Quantity_Color& color ) {
359
360 const Handle( WNT_GraphicDevice )& dev =
361 Handle( WNT_GraphicDevice )::DownCast ( MyGraphicDevice );
362
363 if ( MyHBackground )
364 DeleteObject( MyHBackground );
365
366 MyBackground.SetColor( color );
367 MyHBackground = CreateSolidBrush( dev->SetColor( color ) );
368
369} // end WNT_Window :: SetBackground
370
371//************************ SetBackground (6) *****************************//
372//***//
373void WNT_Window::SetBackground (const Aspect_GradientBackground& GrBackground) {
374
375 Quantity_Color Color1, Color2;
376 GrBackground.Colors(Color1,Color2);
377 SetBackground(Color1,Color2, GrBackground.BgGradientFillMethod());
378
379} // end WNT_Window :: SetBackground
380
381//************************ SetBackground (7) *****************************//
382//***//
383void WNT_Window::SetBackground( const Quantity_Color& color1,
384 const Quantity_Color& color2,
385 const Aspect_GradientFillMethod aMethod){
386
387 Standard_Integer bcolor = Standard_Integer( color1.Name() ) + Standard_Integer( color2.Name() );
388 if( ( bcolor >= 0 ) )
389 MyGradientBackground.SetColors( color1, color2, aMethod ) ;
390} // end WNT_Window :: SetBackground
391
392//**************************** SetCursor *********************************//
393//***//
394void WNT_Window :: SetCursor ( const Aspect_Handle aCursor ) const {
395
396#ifndef _WIN64
397 SetClassLong ( ( HWND )myHWindow, GCL_HCURSOR, ( LONG )aCursor );
398#else
399 SetClassLong ( ( HWND )myHWindow, GCLP_HCURSOR, ( LONG )aCursor );
400#endif
401
402} // end WNT_Window :: SetCursor
403//***//
404//**************************** SetIcon ***********************************//
405//***//
406void WNT_Window :: SetIcon (
407 const Aspect_Handle anIcon,
408 const Standard_CString aName
409 ) {
410
411 if ( !myIcon.IsNull () )
412
413 myIcon.Nullify ();
414
415 myIcon = new WNT_Icon ( aName, anIcon, 0 );
416
417} // end WNT_Window :: SetIcon
418//***//
419//**************************** SetIconName *******************************//
420//***//
421void WNT_Window :: SetIconName ( const Standard_CString aName ) {
422
423 myIcon -> SetName ( aName );
424
425} // end WNT_Window :: SetIconName
426//***//
427//************************** SetDoubleBuffer *****************************//
428//***//
429void WNT_Window :: SetDoubleBuffer ( const Standard_Boolean DBmode ) {
430
431 if ( DBmode )
432
433 if ( !BackingStore () )
434
435 Aspect_WindowError :: Raise ( "Unable to set double buffer" );
436
437 else {
438
439 myDoubleBuffer = Standard_True;
440 myExtraData.hBmp = ( HBITMAP )myHPixmap;
441
442 } // end else
443
444 else {
445
446 myDoubleBuffer = Standard_False;
447 myExtraData.hBmp = NULL;
448
449 } // end else
450
451} // end WNT_Window :: SetDoubleBuffer
452//***//
453//***************************** Flush ************************************//
454//***//
455void WNT_Window :: Flush () const {
456
457 if ( myDoubleBuffer ) Restore ();
458
459} // end WNT_Window :: Flush
460//***//
461//***************************** IsMapped *********************************//
462//***//
463Standard_Boolean WNT_Window :: IsMapped () const {
464 if (IsVirtual()) {
587630c5 465 return Standard_True;
7fd59977 466 }
467
468 WINDOWPLACEMENT wp;
469
470 wp.length = sizeof ( WINDOWPLACEMENT );
471 GetWindowPlacement ( ( HWND )myHWindow, &wp );
472
473 return !( wp.showCmd == SW_HIDE || wp.showCmd == SW_MINIMIZE );
474} // WNT_Window :: IsMapped
475//***//
476//***************************** Map (1) **********************************//
477//***//
478void WNT_Window :: Map () const {
479 if (IsVirtual()) {
480 return;
481 }
482 Map ( SW_SHOW );
483} // end WNT_Window :: Map
484//***//
485//***************************** Map (2) **********************************//
486//***//
487void WNT_Window :: Map ( const Standard_Integer aMapMode ) const {
488 if (IsVirtual()) {
489 return;
490 }
491 ShowWindow ( ( HWND )myHWindow, aMapMode );
492 UpdateWindow ( ( HWND )myHWindow );
493
494} // end WNT_Window :: Map
495//***//
496//**************************** Unmap *************************************//
497//***//
498void WNT_Window :: Unmap () const {
499
500 Map ( SW_HIDE );
501
502} // end WNT_Window :: Unmap
503//***//
504//**************************** DoResize **********************************//
505//***//
506Aspect_TypeOfResize WNT_Window :: DoResize () const {
507
508 int mask = 0;
509 Aspect_TypeOfResize mode = Aspect_TOR_UNKNOWN;
510 WINDOWPLACEMENT wp;
511
512 wp.length = sizeof ( WINDOWPLACEMENT );
513 GetWindowPlacement ( ( HWND )myHWindow, &wp );
514
515 if ( wp.showCmd != SW_SHOWMINIMIZED ) {
516
517 if ( Abs ( wp.rcNormalPosition.left - aXLeft ) > 2 ) mask |= 1;
518 if ( Abs ( wp.rcNormalPosition.right - aXRight ) > 2 ) mask |= 2;
519 if ( Abs ( wp.rcNormalPosition.top - aYTop ) > 2 ) mask |= 4;
520 if ( Abs ( wp.rcNormalPosition.bottom - aYBottom ) > 2 ) mask |= 8;
521
522 switch ( mask ) {
523
524 case 0:
525
526 mode = Aspect_TOR_NO_BORDER;
527
528 break;
529
530 case 1:
531
532 mode = Aspect_TOR_LEFT_BORDER;
533
534 break;
535
536 case 2:
537
538 mode = Aspect_TOR_RIGHT_BORDER;
539
540 break;
541
542 case 4:
543
544 mode = Aspect_TOR_TOP_BORDER;
545
546 break;
547
548 case 5:
549
550 mode = Aspect_TOR_LEFT_AND_TOP_BORDER;
551
552 break;
553
554 case 6:
555
556 mode = Aspect_TOR_TOP_AND_RIGHT_BORDER;
557
558 break;
559
560 case 8:
561
562 mode = Aspect_TOR_BOTTOM_BORDER;
563
564 break;
565
566 case 9:
567
568 mode = Aspect_TOR_BOTTOM_AND_LEFT_BORDER;
569
570 break;
571
572 case 10:
573
574 mode = Aspect_TOR_RIGHT_AND_BOTTOM_BORDER;
575
576 break;
577
578 default:
579
580 break;
581
582 } // end switch
583
584 *( ( Standard_Integer* )&aXLeft ) = wp.rcNormalPosition.left;
585 *( ( Standard_Integer* )&aXRight ) = wp.rcNormalPosition.right;
586 *( ( Standard_Integer* )&aYTop ) = wp.rcNormalPosition.top;
587 *( ( Standard_Integer* )&aYBottom ) = wp.rcNormalPosition.bottom;
588
589 if ( myDoubleBuffer ) {
590
591 DeleteObject ( myHPixmap );
592 *( ( Aspect_Handle* )&myHPixmap ) = 0;
593
594 if ( !BackingStore () )
595
596 Aspect_WindowError :: Raise ( "WNT_Window::DoResize (): Unable to set double buffer" );
597
598 } // end if
599
600 } // end if
601
602 return mode;
603
604} // end WNT_Window :: DoResize
605
606//***//
607//**************************** DoMapping **********************************//
608//***//
609Standard_Boolean WNT_Window :: DoMapping () const {
610// DO nothing on WNT.
611 return Standard_True;
612}
613
614//***//
615//**************************** Clear *************************************//
616//***//
617void WNT_Window :: Clear () const {
618
619 RECT r;
620
621 GetClientRect ( ( HWND )myHWindow, &r );
622 ClearArea ( r.right / 2, r.bottom / 2, r.right, r.bottom );
623
624} // end WNT_Window :: Clear
625//***//
626//**************************** ClearArea *********************************//
627//***//
628void WNT_Window :: ClearArea (
629 const Standard_Integer Xc,
630 const Standard_Integer Yc,
631 const Standard_Integer Width,
632 const Standard_Integer Height
633 ) const {
634
635 RECT r;
636 HDC hDC = GetDC ( ( HWND )myHWindow );
637
638 r.left = Xc - Width / 2;
639 r.right = r.left + Width;
640 r.top = Yc - Height / 2;
641 r.bottom = r.top + Height;
642
643 if ( myDoubleBuffer && myHPixmap ) {
644
645 HDC hDCmem = CreateCompatibleDC ( hDC );
646
647 SELECT_PALETTE( hDCmem );
648
649 SelectObject ( hDCmem, myHPixmap );
650 FillRect ( hDCmem, &r, ( HBRUSH )MyHBackground );
651
652 UNSELECT_PALETTE( hDCmem );
653
654 DeleteDC ( hDCmem );
655
656 } else {
657
658 SELECT_PALETTE( hDC );
659
660 FillRect ( hDC, &r, ( HBRUSH )MyHBackground );
661
662 UNSELECT_PALETTE( hDC );
663
664 } // end else
665
666 ReleaseDC ( ( HWND )myHWindow, hDC );
667
668} // end WNT_Window :: ClearArea
669//***//
670//**************************** Restore ***********************************//
671//***//
672void WNT_Window :: Restore () const {
673
674 RECT r;
675
676 if ( myHPixmap ) {
677
678 GetClientRect ( ( HWND )myHWindow, &r );
679 RestoreArea (
680 r.right / 2, r.bottom - r.bottom / 2, r.right, r.bottom
681 );
682
683 } // end if
684
685} // end WNT_Window :: Restore
686//***//
687//************************* Restore Area *********************************//
688//***//
689void WNT_Window :: RestoreArea (
690 const Standard_Integer Xc,
691 const Standard_Integer Yc,
692 const Standard_Integer Width,
693 const Standard_Integer Height
694 ) const {
695
696 HDC hDC, hDCmem;
697 int x, y;
698
699 if ( myHPixmap ) {
700
701 hDC = GetDC ( ( HWND )myHWindow );
702 hDCmem = CreateCompatibleDC ( hDC );
703
704 x = Xc - Width / 2;
705 y = Yc - Height / 2;
706
707 SelectObject ( hDCmem, myHPixmap );
708 BitBlt ( hDC, x, y, Width, Height, hDCmem, x, y, SRCCOPY );
709
710 DeleteDC ( hDCmem );
711 ReleaseDC ( ( HWND )myHWindow, hDC );
712
713 } // end if
714
715} // end WNT_Window :: RestoreArea
716//***//
717//****************************** Dump ************************************//
718//***//
719Standard_Boolean WNT_Window::Dump (const Standard_CString theFilename,
720 const Standard_Real theGammaValue) const
721{
722 return ToPixMap()->Dump (theFilename, theGammaValue);
723} // end WNT_Window :: Dump
724//***//
725//*************************** DumpArea ***********************************//
726//***//
727Standard_Boolean WNT_Window::DumpArea (const Standard_CString theFilename,
728 const Standard_Integer Xc, const Standard_Integer Yc,
729 const Standard_Integer Width, const Standard_Integer Height,
730 const Standard_Real aGammaValue) const
731{
732// *** Gamma correction must be implemented also under WNT ...
733 Standard_Integer x = Xc - Width / 2;
734 Standard_Integer y = Yc - Height / 2;
735 return (myDoubleBuffer) ?
736 myImages->SaveBuffer (theFilename, x, y, Width, Height) :
737 myImages->Save (theFilename, x, y, Width, Height);
738
739} // end WNT_Window :: DumpArea
740//***//
741
742static Handle(Image_PixMap) ConvertBitmap (HBITMAP theHBitmap)
743{
744 Handle(Image_PixMap) anImagePixMap;
745 // Copy data from HBITMAP
746 BITMAP aBitmap;
747
748 // Get informations about the bitmap
749 GetObject (theHBitmap, sizeof(BITMAP), (LPSTR )&aBitmap);
750 Standard_Integer aWidth = aBitmap.bmWidth;
751 Standard_Integer aHeight = aBitmap.bmHeight;
752
753 // Setup image data
754 BITMAPINFOHEADER aBitmapInfo;
755 memset (&aBitmapInfo, 0, sizeof(BITMAPINFOHEADER));
756 aBitmapInfo.biSize = sizeof(BITMAPINFOHEADER);
757 aBitmapInfo.biWidth = aWidth;
758 aBitmapInfo.biHeight = aHeight; // positive means bottom-up!
759 aBitmapInfo.biPlanes = 1;
760 aBitmapInfo.biBitCount = 32;
761 aBitmapInfo.biCompression = BI_RGB;
762
763 Standard_Integer aBytesPerLine = aWidth * 4;
764 Standard_Byte* aDataPtr = new Standard_Byte[aBytesPerLine * aHeight];
765
766 // Copy the pixels
767 HDC aDC = GetDC (NULL);
768 Standard_Boolean isSuccess
769 = GetDIBits (aDC, // handle to DC
770 theHBitmap, // handle to bitmap
771 0, // first scan line to set
772 aHeight, // number of scan lines to copy
773 aDataPtr, // array for bitmap bits
774 (LPBITMAPINFO )&aBitmapInfo, // bitmap data info
775 DIB_RGB_COLORS // RGB
776 ) != 0;
777
778 if (isSuccess)
779 {
780 anImagePixMap = new Image_PixMap (aDataPtr,
781 aWidth, aHeight,
782 aBytesPerLine,
783 aBitmapInfo.biBitCount,
784 Standard_False); // bottom-up!
785 }
786 // Release dump memory
787 delete[] aDataPtr;
788 ReleaseDC (NULL, aDC);
789 return anImagePixMap;
790}
791
792Handle(Aspect_PixMap) WNT_Window::ToPixMap() const
793{
794 if (myDoubleBuffer && myHPixmap)
795 {
796 return ConvertBitmap ((HBITMAP )myHPixmap);
797 }
798
799 RECT aRect;
800 GetClientRect ((HWND )myHWindow, &aRect);
801 int aWidth = aRect.right - aRect.left;
802 int aHeight = aRect.bottom - aRect.top;
803
804 // Prepare the DCs
805 HDC aDstDC = GetDC (NULL);
806 HDC aSrcDC = GetDC ((HWND )myHWindow); // we copy only client area
807 HDC aMemDC = CreateCompatibleDC (aDstDC);
808
809 // Copy the screen to the bitmap
810 HBITMAP anHBitmapDump = CreateCompatibleBitmap (aDstDC, aWidth, aHeight);
811 HBITMAP anHBitmapOld = (HBITMAP )SelectObject (aMemDC, anHBitmapDump);
812 BitBlt (aMemDC, 0, 0, aWidth, aHeight, aSrcDC, 0, 0, SRCCOPY);
813
814 Handle(Image_PixMap) anImagePixMap = ConvertBitmap (anHBitmapDump);
815
816 // Free objects
817 DeleteObject (SelectObject (aMemDC, anHBitmapOld));
818 DeleteDC (aMemDC);
819
820 return anImagePixMap;
821}
822
823//****************************** Load ************************************//
824//***//
825Standard_Boolean WNT_Window :: Load ( const Standard_CString aFilename ) const {
826
827 RECT r;
828
829 GetClientRect ( ( HWND )myHWindow, &r );
830
831 return LoadArea ( aFilename, r.right / 2, r.bottom / 2, r.right, r.bottom );
832
833} // end WNT_Window :: Load
834//***//
835//*************************** LoadArea ***********************************//
836//***//
837Standard_Boolean WNT_Window :: LoadArea (
838 const Standard_CString aFilename,
839 const Standard_Integer Xc,
840 const Standard_Integer Yc,
841 const Standard_Integer Width,
842 const Standard_Integer Height
843 ) const {
844
845 Standard_Integer idx;
846 Standard_Boolean status;
847
848 status = ( idx = myImages -> Load ( aFilename ) ) ? Standard_True : Standard_False;
849
850 if ( status ) myImages -> Draw ( idx, Xc, Yc, Width, Height );
851
852 return status;
853
854} // end WNT_Window :: LoadArea
855//***//
856//*************************** BackingStore *******************************//
857//***//
858Standard_Boolean WNT_Window :: BackingStore () const {
859
860 Standard_Boolean retVal = Standard_False;;
861 HDC hDC;
862 RECT r;
863
864 if ( myHPixmap )
865
866 retVal = Standard_True;
867
868 else {
869
870 hDC = GetDC ( ( HWND )myHWindow );
871
872 GetClientRect ( ( HWND )myHWindow, &r );
873 *( ( Aspect_Handle* )&myHPixmap ) = CreateCompatibleBitmap ( hDC, r.right, r.bottom );
874
875 if ( myHPixmap ) {
876
877 HDC hDCmem;
878
879 hDCmem = CreateCompatibleDC ( hDC );
880
881 SELECT_PALETTE( hDCmem );
882
883 SelectObject ( hDCmem, myHPixmap );
884 FillRect ( hDCmem, &r, ( HBRUSH )MyHBackground );
885
886 UNSELECT_PALETTE( hDCmem );
887
888 DeleteDC ( hDCmem );
889
890 retVal = Standard_True;
891
892 } // end if
893
894 ReleaseDC ( ( HWND )myHWindow, hDC );
895
896 } // end else
897
898 return retVal;
899
900} // end WNT_Window :: BackingStore
901//***//
902//******************************* Ratio **********************************//
903//***//
904Quantity_Ratio WNT_Window :: Ratio () const {
905
906 RECT r;
907
908 GetClientRect ( ( HWND )myHWindow, &r );
909
910 return ( Quantity_Ratio )( ( Quantity_Ratio )r.right / ( Quantity_Ratio )r.bottom );
911
912} // end WNT_Window :: Ratio
913//***//
914//**************************** Position (1) ******************************//
915//***//
916void WNT_Window :: Position (
917 Quantity_Parameter& X1, Quantity_Parameter& Y1,
918 Quantity_Parameter& X2, Quantity_Parameter& Y2
919 ) const {
920
921 POINT ptl, ptr;
922 RECT pr, cr;
923 const Handle( WNT_GraphicDevice )& dev = Handle( WNT_GraphicDevice ) ::
924 DownCast ( MyGraphicDevice );
925
926 if ( myHParentWindow )
927
928 GetClientRect ( ( HWND )myHParentWindow, &pr );
929
930 else {
931
932 int w, h;
933
934 dev -> DisplaySize ( w, h );
935
936 pr.right = w;
937 pr.bottom = h;
938
939 } // end else
940
941 GetClientRect ( ( HWND )myHWindow, &cr );
942
943 ptl.x = ptl.y = 0;
944 ClientToScreen ( ( HWND )myHWindow, &ptl );
945 cr.left = ptl.x;
946 cr.top = ptl.y;
947
948 ptr.x = cr.right;
949 ptr.y = cr.bottom;
950 ClientToScreen ( ( HWND )myHWindow, &ptr );
951 cr.right = ptr.x;
952 cr.bottom = ptr.y;
953
954 if ( myHParentWindow ) {
955
956 ScreenToClient ( ( HWND )myHParentWindow, &ptl );
957 ScreenToClient ( ( HWND )myHParentWindow, &ptr );
958 cr.left = ptl.x;
959 cr.top = ptl.y;
960 cr.right = ptr.x;
961 cr.bottom = ptr.y;
962
963 } // end if
964
965 X1 = ( Quantity_Parameter )cr.left / ( Quantity_Parameter )pr.right;
966 X2 = ( Quantity_Parameter )cr.right / ( Quantity_Parameter )pr.right;
967 Y1 = 1. - ( Quantity_Parameter )cr.top / ( Quantity_Parameter )pr.bottom;
968 Y2 = 1. - ( Quantity_Parameter )cr.bottom / ( Quantity_Parameter )pr.bottom;
969
970} // end WNT_Window :: Position
971//***//
972//**************************** Position (2) ******************************//
973//***//
974void WNT_Window :: Position (
975 Standard_Integer& X1, Standard_Integer& Y1,
976 Standard_Integer& X2, Standard_Integer& Y2
977 ) const {
978
979 POINT ptl, ptr;
980 RECT r;
981
982 GetClientRect ( ( HWND )myHWindow, &r );
983
984 ptl.x = ptl.y = 0;
985 ClientToScreen ( ( HWND )myHWindow, &ptl );
986 ptr.x = r.right;
987 ptr.y = r.bottom;
988 ClientToScreen ( ( HWND )myHWindow, &ptr );
989
990 if ( myHParentWindow ) {
991
992 ScreenToClient ( ( HWND )myHParentWindow, &ptl );
993 ScreenToClient ( ( HWND )myHParentWindow, &ptr );
994
995 } // end if
996
997 X1 = ptl.x;
998 X2 = ptr.x;
999 Y1 = ptl.y;
1000 Y2 = ptr.y;
1001
1002} // end WNT_Window :: Position
1003//***//
1004//******************************* Size (1) *******************************//
1005//***//
1006void WNT_Window :: Size (
1007 Quantity_Parameter& Width, Quantity_Parameter& Height
1008 ) const {
1009
1010 RECT pr, wr;
1011 const Handle( WNT_GraphicDevice )& dev = Handle( WNT_GraphicDevice ) ::
1012 DownCast ( MyGraphicDevice );
1013
1014 if ( myHParentWindow )
1015
1016 GetClientRect ( ( HWND )myHParentWindow, &pr );
1017
1018 else {
1019
1020 int w, h;
1021
1022 dev -> DisplaySize ( w, h );
1023
1024 pr.right = w;
1025 pr.bottom = h;
1026
1027 } // end else
1028
1029 GetClientRect ( ( HWND )myHWindow, &wr );
1030
1031 Width = ( Quantity_Parameter )wr.right / ( Quantity_Parameter )pr.right;
1032 Height = ( Quantity_Parameter )wr.bottom / ( Quantity_Parameter )pr.bottom;
1033
1034} // end WNT_Window :: Size
1035//***//
1036//******************************* Size (2) *******************************//
1037//***//
1038void WNT_Window :: Size (
1039 Standard_Integer& Width, Standard_Integer& Height
1040 ) const {
1041
1042 RECT r;
1043
1044 GetClientRect ( ( HWND )myHWindow, &r );
1045
1046 Width = r.right;
1047 Height = r.bottom;
1048
1049} // end WNT_Window :: Size
1050//***//
1051//******************************* MMSize (1) *******************************//
1052//***//
1053void WNT_Window :: MMSize (
1054 Standard_Real& Width, Standard_Real& Height
1055 ) const {
1056
1057 const Handle( WNT_GraphicDevice )& dev = Handle( WNT_GraphicDevice ) ::
1058 DownCast ( MyGraphicDevice );
1059
1060 int w, h;
1061 dev -> DisplaySize ( w, h );
1062 Standard_Real mmw,mmh;
1063 dev -> DisplaySize ( mmw, mmh );
1064 Standard_Real mmratio = (mmw/w + mmh/h)/2.;
1065
1066 RECT wr;
1067 GetClientRect ( ( HWND )myHWindow, &wr );
1068
1069 Width = mmratio * wr.right;
1070 Height = mmratio * wr.bottom;
1071
1072} // end WNT_Window :: MMSize
1073//***//
1074//**************************** Convert (1) *******************************//
1075//***//
1076Quantity_Parameter WNT_Window :: Convert (
1077 const Standard_Integer PV
1078 ) const {
1079
1080 int w, h;
1081 const Handle( WNT_GraphicDevice )& dev = Handle( WNT_GraphicDevice ) ::
1082 DownCast ( MyGraphicDevice );
1083
1084 dev -> DisplaySize ( w, h );
1085
1086 return ( Quantity_Parameter )( PV / Min ( w, h ) );
1087
1088} // end WNT_Window :: Convert
1089//***//
1090//**************************** Convert (2) *******************************//
1091//***//
1092Standard_Integer WNT_Window :: Convert (
1093 const Quantity_Parameter DV
1094 ) const {
1095
1096 int w, h;
1097 const Handle( WNT_GraphicDevice )& dev = Handle( WNT_GraphicDevice ) ::
1098 DownCast ( MyGraphicDevice );
1099
1100 dev -> DisplaySize ( w, h );
1101
1102 return ( Standard_Integer )( DV * Min ( w, h ) );
1103
1104} // end WNT_Window :: Convert
1105//***//
1106//**************************** Convert (3) *******************************//
1107//***//
1108
1109void WNT_Window :: Convert (
1110 const Standard_Integer PX, const Standard_Integer PY,
1111 Quantity_Parameter& DX, Quantity_Parameter& DY
1112 ) const {
1113
1114 int w, h;
1115 const Handle( WNT_GraphicDevice )& dev = Handle( WNT_GraphicDevice ) ::
1116 DownCast ( MyGraphicDevice );
1117
1118 dev -> DisplaySize ( w, h );
1119
1120 DX = ( Quantity_Parameter )PX / ( Quantity_Parameter )w;
1121 DY = 1. - ( Quantity_Parameter )PY / ( Quantity_Parameter )h;
1122
1123} // end WNT_Window :: Convert
1124//***//
1125//**************************** Convert (4) *******************************//
1126//***//
1127void WNT_Window :: Convert (
1128 const Quantity_Parameter DX, const Quantity_Parameter DY,
1129 Standard_Integer& PX, Standard_Integer& PY
1130 ) const {
1131
1132 int w, h;
1133 const Handle( WNT_GraphicDevice )& dev = Handle( WNT_GraphicDevice ) ::
1134 DownCast ( MyGraphicDevice );
1135
1136 dev -> DisplaySize ( w, h );
1137
1138 PX = ( Standard_Integer )( DX * ( Quantity_Parameter )w );
1139 PY = ( Standard_Integer )( ( 1. - DY ) * ( Quantity_Parameter )h );
1140
1141} // end WNT_Window :: Convert
1142//***//
1143//******************************* SetPos *********************************//
1144//***//
1145void WNT_Window :: SetPos (
1146 const Standard_Integer X, const Standard_Integer Y,
1147 const Standard_Integer X1, const Standard_Integer Y1
1148 ) {
1149
1150 aXLeft = X;
1151 aYTop = Y;
1152 aXRight = X1;
1153 aYBottom = Y1;
1154
1155} // end WNT_Window :: SetPos
1156//***//
1157//*************************** SetOutputFormat ****************************//
1158//***//
1159void WNT_Window :: SetOutputFormat ( const WNT_TypeOfImage aFormat ) {
1160
1161 myFormat = aFormat;
1162 myImages -> SetFormat ( aFormat );
1163
1164} // end WNT_Window :: SetOutputFormat
1165//***//
1166//**************************** SetFlags **********************************//
1167//***//
1168void WNT_Window :: SetFlags ( const Standard_Integer aFlags ) {
1169
1170 myExtraData.dwFlags |= aFlags;
1171
1172} // end WNT_Window :: SetFlags
1173//***//
1174//*************************** ResetFlags *********************************//
1175//***//
1176void WNT_Window :: ResetFlags ( const Standard_Integer aFlags ) {
1177
1178 myExtraData.dwFlags &= ~aFlags;
1179
1180} // end WNT_Window :: ResetFlags
1181//***//
1182//*************************** doCreate **********************************//
1183//***//
1184void WNT_Window :: doCreate (
1185 const Handle( WNT_GraphicDevice )& aDevice,
1186 const Aspect_Handle aHandle,
1187 const Quantity_NameOfColor aBackColor
1188 ) {
1189
1190 LONG uData;
1191 WNDPROC wProc;
1192 WINDOWPLACEMENT wp;
1193
1194 const Handle( WNT_GraphicDevice )& dev = Handle( WNT_GraphicDevice ) ::
1195 DownCast ( MyGraphicDevice );
1196
1197 ZeroMemory ( &myExtraData, sizeof ( WNT_WindowData ) );
1198
1199 myHWindow = aHandle;
1200 myHParentWindow = GetParent ( ( HWND )aHandle );
1201 myDoubleBuffer = Standard_False;
1202 myHPixmap = NULL;
1203 wProc = (WNDPROC )GetWindowLongPtr ((HWND )aHandle, GWLP_WNDPROC);
1204 uData = GetWindowLongPtr ((HWND )aHandle, GWLP_USERDATA);
1205 myUsrData = Standard_Address(-1);
1206 myWndProc = NULL;
1207
1208 if ( wProc != &WNT_WndProc ) {
1209
1210 myWndProc = wProc;
1211
1212 SetBackground ( aBackColor );
1213
1214 myExtraData.WNT_Window_Ptr = ( void* )this;
1215 myExtraData.hPal = ( HPALETTE )dev -> HPalette ();
1216
1217 if ( uData != ( LONG )&myExtraData )
1218 {
1219 myUsrData = (Standard_Address )SetWindowLongPtr ((HWND )myHWindow, GWLP_USERDATA, (LONG_PTR )&myExtraData);
1220 }
1221
1222 if (myWndProc != NULL)
1223 {
1224 SetWindowLongPtr ((HWND )myHWindow, GWLP_WNDPROC, (LONG_PTR )WNT_WndProc);
1225 }
1226
1227 } // end if
1228
1229 myExtraData.dwFlags = WDF_FOREIGN;
1230
1231 myImages = new WNT_ImageManager ( this );
1232 myFormat = WNT_TOI_XWD;
1233
1234 wp.length = sizeof ( WINDOWPLACEMENT );
1235 GetWindowPlacement ( ( HWND )myHWindow, &wp );
1236
1237 aXLeft = wp.rcNormalPosition.left;
1238 aYTop = wp.rcNormalPosition.top;
1239 aXRight = wp.rcNormalPosition.right;
1240 aYBottom = wp.rcNormalPosition.bottom;
1241
1242} // end WNT_Window :: doCreate
1243//***//
1244//************************************************************************//