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