1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 // include windows.h first to have all definitions available
18 #include <WNT_Window.hxx>
20 #if defined(_WIN32) && !defined(OCCT_UWP)
22 #include <Aspect_Convert.hxx>
23 #include <Aspect_WindowDefinitionError.hxx>
24 #include <Aspect_WindowError.hxx>
25 #include <Standard_Type.hxx>
26 #include <TCollection_ExtendedString.hxx>
27 #include <WNT_WClass.hxx>
29 IMPLEMENT_STANDARD_RTTIEXT(WNT_Window, Aspect_Window)
31 // =======================================================================
32 // function : WNT_Window
34 // =======================================================================
35 WNT_Window::WNT_Window (const Standard_CString theTitle,
36 const Handle(WNT_WClass)& theClass,
37 const WNT_Dword& theStyle,
38 const Standard_Integer thePxLeft,
39 const Standard_Integer thePxTop,
40 const Standard_Integer thePxWidth,
41 const Standard_Integer thePxHeight,
42 const Quantity_NameOfColor theBackColor,
43 const Aspect_Handle theParent,
44 const Aspect_Handle theMenu,
45 const Standard_Address theClientStruct)
49 aXRight (thePxLeft + thePxWidth),
50 aYBottom (thePxTop + thePxHeight),
52 myIsForeign (Standard_False)
54 if (thePxWidth <= 0 || thePxHeight <= 0)
56 throw Aspect_WindowDefinitionError("Coordinate(s) out of range");
59 DWORD aStyle = theStyle;
60 if (theParent && !(theStyle & WS_CHILD))
62 aStyle |= WS_CHILD | WS_CLIPSIBLINGS;
64 else if (!theParent && !(theStyle & WS_CLIPCHILDREN))
66 aStyle |= WS_CLIPCHILDREN;
69 // include decorations in the window dimensions to reproduce same behavior of Xw_Window
72 aRect.bottom = aYBottom;
74 aRect.right = aXRight;
75 AdjustWindowRect (&aRect, aStyle, theMenu != NULL ? TRUE : FALSE);
78 aXRight = aRect.right;
79 aYBottom = aRect.bottom;
81 const TCollection_ExtendedString aTitleW (theTitle);
82 const TCollection_ExtendedString aClassNameW (myWClass->Name());
83 myHWindow = CreateWindowW (aClassNameW.ToWideString(), aTitleW.ToWideString(),
86 (aXRight - aXLeft), (aYBottom - aYTop),
89 (HINSTANCE )myWClass->Instance(),
93 throw Aspect_WindowDefinitionError("Unable to create window");
96 myHParentWindow = theParent;
97 SetBackground (theBackColor);
100 // =======================================================================
101 // function : WNT_Window
103 // =======================================================================
104 WNT_Window::WNT_Window (const Aspect_Handle theHandle,
105 const Quantity_NameOfColor theBackColor)
106 : myIsForeign (Standard_True)
108 myHWindow = theHandle;
109 myHParentWindow = GetParent ((HWND )theHandle);
111 SetBackground (theBackColor);
113 WINDOWPLACEMENT aPlace;
114 aPlace.length = sizeof (WINDOWPLACEMENT);
115 ::GetWindowPlacement ((HWND )myHWindow, &aPlace);
117 aXLeft = aPlace.rcNormalPosition.left;
118 aYTop = aPlace.rcNormalPosition.top;
119 aXRight = aPlace.rcNormalPosition.right;
120 aYBottom = aPlace.rcNormalPosition.bottom;
123 // =======================================================================
124 // function : ~WNT_Window
126 // =======================================================================
127 WNT_Window::~WNT_Window()
129 if (myHWindow == NULL
135 DestroyWindow ((HWND )myHWindow);
136 myIsForeign = Standard_False;
139 // =======================================================================
140 // function : SetCursor
142 // =======================================================================
143 void WNT_Window::SetCursor (const Aspect_Handle theCursor) const
145 ::SetClassLongPtrW ((HWND )myHWindow, GCLP_HCURSOR, (LONG_PTR )theCursor);
148 // =======================================================================
149 // function : IsMapped
151 // =======================================================================
152 Standard_Boolean WNT_Window::IsMapped() const
156 return Standard_True;
159 WINDOWPLACEMENT aPlace;
160 aPlace.length = sizeof (WINDOWPLACEMENT);
161 ::GetWindowPlacement ((HWND )myHWindow, &aPlace);
162 return !(aPlace.showCmd == SW_HIDE
163 || aPlace.showCmd == SW_MINIMIZE);
166 // =======================================================================
169 // =======================================================================
170 void WNT_Window::Map() const
178 // =======================================================================
181 // =======================================================================
182 void WNT_Window::Map (const Standard_Integer theMapMode) const
189 ::ShowWindow ((HWND )myHWindow, theMapMode);
190 ::UpdateWindow ((HWND )myHWindow);
193 // =======================================================================
196 // =======================================================================
197 void WNT_Window::Unmap() const
202 // =======================================================================
203 // function : DoResize
205 // =======================================================================
206 Aspect_TypeOfResize WNT_Window::DoResize() const
210 return Aspect_TOR_UNKNOWN;
214 Aspect_TypeOfResize mode = Aspect_TOR_UNKNOWN;
217 wp.length = sizeof ( WINDOWPLACEMENT );
218 GetWindowPlacement ( ( HWND )myHWindow, &wp );
220 if (wp.showCmd != SW_SHOWMINIMIZED)
222 if (Abs ((int )wp.rcNormalPosition.left - aXLeft ) > 2) mask |= 1;
223 if (Abs ((int )wp.rcNormalPosition.right - aXRight ) > 2) mask |= 2;
224 if (Abs ((int )wp.rcNormalPosition.top - aYTop ) > 2) mask |= 4;
225 if (Abs ((int )wp.rcNormalPosition.bottom - aYBottom) > 2) mask |= 8;
230 mode = Aspect_TOR_NO_BORDER;
233 mode = Aspect_TOR_LEFT_BORDER;
236 mode = Aspect_TOR_RIGHT_BORDER;
239 mode = Aspect_TOR_TOP_BORDER;
242 mode = Aspect_TOR_LEFT_AND_TOP_BORDER;
245 mode = Aspect_TOR_TOP_AND_RIGHT_BORDER;
248 mode = Aspect_TOR_BOTTOM_BORDER;
251 mode = Aspect_TOR_BOTTOM_AND_LEFT_BORDER;
254 mode = Aspect_TOR_RIGHT_AND_BOTTOM_BORDER;
260 *((Standard_Integer* )&aXLeft ) = wp.rcNormalPosition.left;
261 *((Standard_Integer* )&aXRight ) = wp.rcNormalPosition.right;
262 *((Standard_Integer* )&aYTop ) = wp.rcNormalPosition.top;
263 *((Standard_Integer* )&aYBottom) = wp.rcNormalPosition.bottom;
269 // =======================================================================
272 // =======================================================================
273 Standard_Real WNT_Window::Ratio() const
277 return Standard_Real(aXRight - aXLeft)/ Standard_Real(aYBottom - aYTop);
281 GetClientRect ((HWND )myHWindow, &aRect);
282 return Standard_Real(aRect.right - aRect.left) / Standard_Real(aRect.bottom - aRect.top);
285 // =======================================================================
286 // function : Position
288 // =======================================================================
289 void WNT_Window::Position (Standard_Integer& theX1, Standard_Integer& theY1,
290 Standard_Integer& theX2, Standard_Integer& theY2) const
302 ::GetClientRect ((HWND )myHWindow, &aRect);
304 POINT aPntLeft, aPntRight;
305 aPntLeft.x = aPntLeft.y = 0;
306 ::ClientToScreen ((HWND )myHWindow, &aPntLeft);
307 aPntRight.x = aRect.right;
308 aPntRight.y = aRect.bottom;
309 ::ClientToScreen ((HWND )myHWindow, &aPntRight);
311 if (myHParentWindow != NULL)
313 ::ScreenToClient ((HWND )myHParentWindow, &aPntLeft);
314 ::ScreenToClient ((HWND )myHParentWindow, &aPntRight);
323 // =======================================================================
326 // =======================================================================
327 void WNT_Window::Size (Standard_Integer& theWidth,
328 Standard_Integer& theHeight) const
332 theWidth = aXRight - aXLeft;
333 theHeight = aYBottom - aYTop;
338 ::GetClientRect ((HWND )myHWindow, &aRect);
339 theWidth = aRect.right;
340 theHeight = aRect.bottom;
343 // =======================================================================
346 // =======================================================================
347 void WNT_Window::SetPos (const Standard_Integer theX, const Standard_Integer theY,
348 const Standard_Integer theX1, const Standard_Integer theY1)