1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
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.
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.
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.
19 // include windows.h first to have all definitions available
22 // GG 07/03/00 Add MMSize() method
24 #include <WNT_Window.ixx>
26 #include <Image_AlienPixMap.hxx>
27 #include <Aspect_Convert.hxx>
30 extern "C" void _debug_break ( char* );
31 //************************************************************************//
33 // callback function to manage window's background
34 extern LRESULT CALLBACK WNT_WndProc (
35 HWND, UINT, WPARAM, LPARAM
38 WNT_Window::WNT_Window (const Standard_CString theTitle,
39 const Handle(WNT_WClass)& theClass,
40 const WNT_Dword& theStyle,
41 const Standard_Integer thePxLeft,
42 const Standard_Integer thePxTop,
43 const Standard_Integer thePxWidth,
44 const Standard_Integer thePxHeight,
45 const Quantity_NameOfColor theBackColor,
46 const Aspect_Handle theParent,
47 const Aspect_Handle theMenu,
48 const Standard_Address theClientStruct)
52 aXRight (thePxLeft + thePxWidth),
53 aYBottom (thePxTop + thePxHeight),
56 ZeroMemory (&myExtraData, sizeof (WNT_WindowData));
57 DWORD dwStyle = theStyle;
59 if (thePxWidth <= 0 || thePxHeight <= 0)
61 Aspect_WindowDefinitionError::Raise ("Coordinate(s) out of range");
64 if (theParent && !(theStyle & WS_CHILD))
66 dwStyle |= WS_CHILD | WS_CLIPSIBLINGS;
68 else if (!theParent && !(theStyle & WS_CLIPCHILDREN))
70 dwStyle |= WS_CLIPCHILDREN;
73 // include decorations in the window dimensions
74 // to reproduce same behaviour of Xw_Window.
77 aRect.bottom = aYBottom;
79 aRect.right = aXRight;
80 AdjustWindowRect (&aRect, dwStyle, theMenu != NULL ? TRUE : FALSE);
83 aXRight = aRect.right;
84 aYBottom = aRect.bottom;
86 myHWindow = CreateWindow (
87 myWClass->Name(), // window's class name
88 theTitle, // window's name
89 dwStyle, // window's style
90 aXLeft, aYTop, // window's coordinates
91 (aXRight - aXLeft), (aYBottom - aYTop),
92 (HWND )theParent, // window's parent
93 (HMENU )theMenu, // window's menu
94 (HINSTANCE )myWClass->Instance(), // application's instance
95 theClientStruct); // pointer to CLIENTCREATESTRUCT
98 Aspect_WindowDefinitionError::Raise ("Unable to create window");
101 myHParentWindow = theParent;
102 SetBackground (theBackColor);
104 myUsrData = (Standard_Address )SetWindowLongPtr ((HWND )myHWindow, GWLP_USERDATA, (LONG_PTR )&myExtraData);
106 myExtraData.WNT_Window_Ptr = (void* )this;
108 SetFlags (WDF_NOERASEBKGRND);
112 //************************* Constructor **********************************//
114 WNT_Window :: WNT_Window (
115 const Aspect_Handle aHandle,
116 const Quantity_NameOfColor aBackColor
119 doCreate (aHandle, aBackColor);
122 SetFlags(WDF_NOERASEBKGRND);
126 //************************* Constructor **********************************//
128 WNT_Window :: WNT_Window (
129 const Standard_Integer aPart1,
130 const Standard_Integer aPart2,
131 const Quantity_NameOfColor aBackColor
134 Aspect_Handle aHandle = ( Aspect_Handle )( ( aPart1 << 16 ) + aPart2 );
136 doCreate (aHandle, aBackColor);
139 SetFlags(WDF_NOERASEBKGRND);
143 //***************************** Destroy **********************************//
145 void WNT_Window :: Destroy ()
149 if (myUsrData != Standard_Address(-1))
151 SetWindowLongPtr ((HWND )myHWindow, GWLP_USERDATA, (LONG_PTR )myUsrData);
154 if (!( myExtraData.dwFlags & WDF_FOREIGN))
156 DestroyWindow ((HWND )myHWindow);
159 } // end WNT_Window :: Destroy
161 //**************************** SetCursor *********************************//
163 void WNT_Window :: SetCursor ( const Aspect_Handle aCursor ) const {
166 SetClassLong ( ( HWND )myHWindow, GCL_HCURSOR, ( LONG )aCursor );
168 SetClassLong ( ( HWND )myHWindow, GCLP_HCURSOR, ( LONG )aCursor );
171 } // end WNT_Window :: SetCursor
174 //***************************** IsMapped *********************************//
176 Standard_Boolean WNT_Window :: IsMapped () const {
178 return Standard_True;
183 wp.length = sizeof ( WINDOWPLACEMENT );
184 GetWindowPlacement ( ( HWND )myHWindow, &wp );
186 return !( wp.showCmd == SW_HIDE || wp.showCmd == SW_MINIMIZE );
187 } // WNT_Window :: IsMapped
190 //***************************** Map (1) **********************************//
192 void WNT_Window :: Map () const {
197 } // end WNT_Window :: Map
200 //***************************** Map (2) **********************************//
202 void WNT_Window :: Map ( const Standard_Integer aMapMode ) const {
206 ShowWindow ( ( HWND )myHWindow, aMapMode );
207 UpdateWindow ( ( HWND )myHWindow );
209 } // end WNT_Window :: Map
212 //**************************** Unmap *************************************//
214 void WNT_Window :: Unmap () const {
218 } // end WNT_Window :: Unmap
221 //**************************** DoResize **********************************//
223 Aspect_TypeOfResize WNT_Window :: DoResize () const
226 Aspect_TypeOfResize mode = Aspect_TOR_UNKNOWN;
229 wp.length = sizeof ( WINDOWPLACEMENT );
230 GetWindowPlacement ( ( HWND )myHWindow, &wp );
232 if (wp.showCmd != SW_SHOWMINIMIZED)
234 if (Abs (wp.rcNormalPosition.left - aXLeft ) > 2) mask |= 1;
235 if (Abs (wp.rcNormalPosition.right - aXRight ) > 2) mask |= 2;
236 if (Abs (wp.rcNormalPosition.top - aYTop ) > 2) mask |= 4;
237 if (Abs (wp.rcNormalPosition.bottom - aYBottom) > 2) mask |= 8;
242 mode = Aspect_TOR_NO_BORDER;
245 mode = Aspect_TOR_LEFT_BORDER;
248 mode = Aspect_TOR_RIGHT_BORDER;
251 mode = Aspect_TOR_TOP_BORDER;
254 mode = Aspect_TOR_LEFT_AND_TOP_BORDER;
257 mode = Aspect_TOR_TOP_AND_RIGHT_BORDER;
260 mode = Aspect_TOR_BOTTOM_BORDER;
263 mode = Aspect_TOR_BOTTOM_AND_LEFT_BORDER;
266 mode = Aspect_TOR_RIGHT_AND_BOTTOM_BORDER;
272 *((Standard_Integer* )&aXLeft ) = wp.rcNormalPosition.left;
273 *((Standard_Integer* )&aXRight ) = wp.rcNormalPosition.right;
274 *((Standard_Integer* )&aYTop ) = wp.rcNormalPosition.top;
275 *((Standard_Integer* )&aYBottom) = wp.rcNormalPosition.bottom;
280 } // end WNT_Window :: DoResize
283 //**************************** DoMapping **********************************//
285 Standard_Boolean WNT_Window :: DoMapping () const {
286 // DO nothing on WNT.
287 return Standard_True;
290 static Standard_Boolean ConvertBitmap (HBITMAP theHBitmap,
291 Image_PixMap& thePixMap)
293 // Get informations about the bitmap
295 if (GetObject (theHBitmap, sizeof(BITMAP), (LPSTR )&aBitmap) == 0)
297 return Standard_False;
300 const Standard_Size aSizeRowBytes = Standard_Size(aBitmap.bmWidth) * 4;
301 if (!thePixMap.InitTrash (Image_PixMap::ImgBGR32, Standard_Size(aBitmap.bmWidth), Standard_Size(aBitmap.bmHeight), aSizeRowBytes))
303 return Standard_False;
305 thePixMap.SetTopDown (false);
308 BITMAPINFOHEADER aBitmapInfo;
309 memset (&aBitmapInfo, 0, sizeof(BITMAPINFOHEADER));
310 aBitmapInfo.biSize = sizeof(BITMAPINFOHEADER);
311 aBitmapInfo.biWidth = aBitmap.bmWidth;
312 aBitmapInfo.biHeight = aBitmap.bmHeight; // positive means bottom-up!
313 aBitmapInfo.biPlanes = 1;
314 aBitmapInfo.biBitCount = 32; // use 32bit for automatic word-alignment per row
315 aBitmapInfo.biCompression = BI_RGB;
318 HDC aDC = GetDC (NULL);
319 Standard_Boolean isSuccess = GetDIBits (aDC, theHBitmap,
320 0, // first scan line to set
321 aBitmap.bmHeight, // number of scan lines to copy
322 thePixMap.ChangeData(), // array for bitmap bits
323 (LPBITMAPINFO )&aBitmapInfo, // bitmap data info
324 DIB_RGB_COLORS) != 0;
325 ReleaseDC (NULL, aDC);
330 //******************************* Ratio **********************************//
332 Quantity_Ratio WNT_Window :: Ratio () const {
336 GetClientRect ( ( HWND )myHWindow, &r );
338 return ( Quantity_Ratio )( ( Quantity_Ratio )r.right / ( Quantity_Ratio )r.bottom );
340 } // end WNT_Window :: Ratio
343 //**************************** Position (2) ******************************//
345 void WNT_Window :: Position (
346 Standard_Integer& X1, Standard_Integer& Y1,
347 Standard_Integer& X2, Standard_Integer& Y2
353 GetClientRect ( ( HWND )myHWindow, &r );
356 ClientToScreen ( ( HWND )myHWindow, &ptl );
359 ClientToScreen ( ( HWND )myHWindow, &ptr );
361 if ( myHParentWindow ) {
363 ScreenToClient ( ( HWND )myHParentWindow, &ptl );
364 ScreenToClient ( ( HWND )myHParentWindow, &ptr );
373 } // end WNT_Window :: Position
376 //******************************* Size (2) *******************************//
378 void WNT_Window :: Size (
379 Standard_Integer& Width, Standard_Integer& Height
384 GetClientRect ( ( HWND )myHWindow, &r );
389 } // end WNT_Window :: Size
392 //******************************* SetPos *********************************//
394 void WNT_Window :: SetPos (
395 const Standard_Integer X, const Standard_Integer Y,
396 const Standard_Integer X1, const Standard_Integer Y1
404 } // end WNT_Window :: SetPos
407 //**************************** SetFlags **********************************//
409 void WNT_Window :: SetFlags ( const Standard_Integer aFlags ) {
411 myExtraData.dwFlags |= aFlags;
413 } // end WNT_Window :: SetFlags
416 //*************************** ResetFlags *********************************//
418 void WNT_Window :: ResetFlags ( const Standard_Integer aFlags ) {
420 myExtraData.dwFlags &= ~aFlags;
422 } // end WNT_Window :: ResetFlags
425 //*************************** doCreate **********************************//
427 void WNT_Window :: doCreate (
428 const Aspect_Handle aHandle,
429 const Quantity_NameOfColor aBackColor
435 ZeroMemory (&myExtraData, sizeof (WNT_WindowData));
438 myHParentWindow = GetParent ((HWND )aHandle);
439 uData = GetWindowLongPtr ((HWND )aHandle, GWLP_USERDATA);
440 myUsrData = Standard_Address(-1);
442 SetBackground (aBackColor);
444 myExtraData.WNT_Window_Ptr = (void* )this;
446 if (uData != (LONG )&myExtraData)
448 myUsrData = (Standard_Address )SetWindowLongPtr ((HWND )myHWindow, GWLP_USERDATA, (LONG_PTR )&myExtraData);
451 myExtraData.dwFlags = WDF_FOREIGN;
453 wp.length = sizeof (WINDOWPLACEMENT);
454 GetWindowPlacement ((HWND )myHWindow, &wp);
456 aXLeft = wp.rcNormalPosition.left;
457 aYTop = wp.rcNormalPosition.top;
458 aXRight = wp.rcNormalPosition.right;
459 aYBottom = wp.rcNormalPosition.bottom;
460 } // end WNT_Window :: doCreate