b311480e |
1 | // Created on: 1994-07-27 |
2 | // Created by: Remi LEQUETTE |
3 | // Copyright (c) 1994-1999 Matra Datavision |
973c2be1 |
4 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
5 | // |
973c2be1 |
6 | // This file is part of Open CASCADE Technology software library. |
b311480e |
7 | // |
d5f74e42 |
8 | // This library is free software; you can redistribute it and/or modify it under |
9 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
10 | // by the Free Software Foundation, with special exception defined in the file |
11 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
12 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
13 | // |
973c2be1 |
14 | // Alternatively, this file may be used under the terms of Open CASCADE |
15 | // commercial license or contractual agreement. |
7fd59977 |
16 | |
17 | #ifndef Draw_Window_HeaderFile |
18 | #define Draw_Window_HeaderFile |
19 | |
b69e576a |
20 | #if defined(_WIN32) |
21 | #include <windows.h> |
22 | #endif |
23 | |
24 | #include <Aspect_Drawable.hxx> |
25 | #include <NCollection_Vec2.hxx> |
7fd59977 |
26 | #include <Standard_Boolean.hxx> |
27 | #include <Standard_Integer.hxx> |
ad03c234 |
28 | #include <TCollection_AsciiString.hxx> |
7fd59977 |
29 | |
b69e576a |
30 | #include <memory> |
7fd59977 |
31 | |
32 | const Standard_Integer MAXCOLOR = 15; |
33 | |
b69e576a |
34 | //! Segment definition. |
35 | struct Draw_XSegment |
7fd59977 |
36 | { |
b69e576a |
37 | NCollection_Vec2<short> Points[2]; // same as XSegment |
7fd59977 |
38 | |
b69e576a |
39 | NCollection_Vec2<short>& operator[](int theIndex) { return Points[theIndex]; } |
40 | const NCollection_Vec2<short>& operator[](int theIndex) const { return Points[theIndex]; } |
7fd59977 |
41 | |
b69e576a |
42 | void Init (Standard_Integer theXStart, Standard_Integer theYStart, Standard_Integer theXEnd, Standard_Integer theYEnd) |
43 | { |
44 | Points[0].SetValues ((short )theXStart, (short )theYStart); |
45 | Points[1].SetValues ((short )theXEnd, (short )theYEnd); |
46 | } |
7fd59977 |
47 | }; |
48 | |
b69e576a |
49 | #if defined(_WIN32) |
7fd59977 |
50 | |
b69e576a |
51 | #define DRAWCLASS L"DRAWWINDOW" |
52 | #define DRAWTITLE L"Draw View" |
7fd59977 |
53 | |
b69e576a |
54 | enum console_semaphore_value |
55 | { |
56 | STOP_CONSOLE, |
57 | WAIT_CONSOLE_COMMAND, |
58 | HAS_CONSOLE_COMMAND |
59 | }; |
7fd59977 |
60 | |
b69e576a |
61 | // global variable describing console state |
62 | extern console_semaphore_value volatile console_semaphore; |
7fd59977 |
63 | |
b69e576a |
64 | // Console command buffer |
65 | #define DRAW_COMMAND_SIZE 1000 |
66 | extern wchar_t console_command[DRAW_COMMAND_SIZE + 1]; |
7fd59977 |
67 | |
b69e576a |
68 | // PROCEDURE DE DRAW WINDOW |
69 | Standard_EXPORT Standard_Boolean Init_Appli(HINSTANCE,HINSTANCE,int,HWND&); |
70 | Standard_EXPORT void Run_Appli(HWND); |
71 | Standard_EXPORT void Destroy_Appli(HINSTANCE); |
7fd59977 |
72 | |
b69e576a |
73 | #else |
7fd59977 |
74 | |
b69e576a |
75 | //! Run the application. |
76 | //! interp will be called to interpret a command and return True if the command is complete. |
77 | void Run_Appli (Standard_Boolean (*inteprete) (const char*)); |
67d97f0e |
78 | |
b69e576a |
79 | //! Initialize application. |
80 | Standard_Boolean Init_Appli(); |
67d97f0e |
81 | |
b69e576a |
82 | //! Destroy application. |
83 | void Destroy_Appli(); |
67d97f0e |
84 | |
b69e576a |
85 | #if defined(HAVE_XLIB) |
67d97f0e |
86 | |
b69e576a |
87 | typedef unsigned long Window; |
88 | typedef unsigned long Pixmap; |
89 | typedef unsigned long Drawable; |
67d97f0e |
90 | |
b69e576a |
91 | #elif defined(__APPLE__) |
67d97f0e |
92 | #ifdef __OBJC__ |
93 | @class NSView; |
94 | @class NSWindow; |
95 | @class NSImage; |
96 | @class Draw_CocoaView; |
7fd59977 |
97 | #else |
67d97f0e |
98 | struct NSView; |
99 | struct NSWindow; |
100 | struct NSImage; |
101 | struct Draw_CocoaView; |
102 | #endif |
b69e576a |
103 | #endif |
67d97f0e |
104 | |
b69e576a |
105 | #endif |
67d97f0e |
106 | |
b69e576a |
107 | //! Draw window. |
67d97f0e |
108 | class Draw_Window |
109 | { |
b69e576a |
110 | public: |
67d97f0e |
111 | |
b69e576a |
112 | //! Type of the callback function that is to be passed to the method AddCallbackBeforeTerminate(). |
a0fc422a |
113 | typedef void (*FCallbackBeforeTerminate)(); |
114 | |
b69e576a |
115 | //! This method registers a callback function that will be called just before exit. |
116 | //! This is useful especially for Windows platform, on which Draw is normally self-terminated instead of exiting. |
117 | Standard_EXPORT static void AddCallbackBeforeTerminate (FCallbackBeforeTerminate theCB); |
67d97f0e |
118 | |
b69e576a |
119 | //! Just in case method for un-registering a callback previously registered by AddCallbackBeforeTerminate(). |
120 | Standard_EXPORT static void RemoveCallbackBeforeTerminate (FCallbackBeforeTerminate theCB); |
67d97f0e |
121 | |
b69e576a |
122 | //! @sa SetColor() |
123 | Standard_EXPORT static Standard_Boolean DefineColor (const Standard_Integer theIndex, |
124 | const char* theColorName); |
67d97f0e |
125 | |
b69e576a |
126 | //! XFlush() wrapper (X11), has no effect on other platforms. |
127 | Standard_EXPORT static void Flush(); |
67d97f0e |
128 | |
b69e576a |
129 | public: |
67d97f0e |
130 | |
b69e576a |
131 | //! Destructor. |
132 | Standard_EXPORT virtual ~Draw_Window(); |
67d97f0e |
133 | |
b69e576a |
134 | //! Get window position. |
135 | Standard_EXPORT void GetPosition (Standard_Integer& thePosX, |
136 | Standard_Integer& thePosY); |
67d97f0e |
137 | |
b69e576a |
138 | //! Set window position. |
139 | Standard_EXPORT void SetPosition (Standard_Integer theNewXpos, |
140 | Standard_Integer theNewYpos); |
67d97f0e |
141 | |
b69e576a |
142 | //! Return window height. |
143 | Standard_EXPORT Standard_Integer HeightWin() const; |
67d97f0e |
144 | |
b69e576a |
145 | //! Return window width. |
146 | Standard_EXPORT Standard_Integer WidthWin() const; |
67d97f0e |
147 | |
b69e576a |
148 | //! Set window dimensions. |
149 | Standard_EXPORT void SetDimension (Standard_Integer theNewDx, |
150 | Standard_Integer theNewDy); |
67d97f0e |
151 | |
b69e576a |
152 | //! Return window title. |
153 | Standard_EXPORT TCollection_AsciiString GetTitle() const; |
67d97f0e |
154 | |
b69e576a |
155 | //! Set window title. |
156 | Standard_EXPORT void SetTitle (const TCollection_AsciiString& theTitle); |
67d97f0e |
157 | |
b69e576a |
158 | //! Return true if window is displayed on the screen. |
159 | Standard_EXPORT bool IsMapped() const; |
67d97f0e |
160 | |
b69e576a |
161 | //! Display window on the screen. |
162 | Standard_EXPORT void DisplayWindow(); |
67d97f0e |
163 | |
b69e576a |
164 | //! Hide window. |
165 | Standard_EXPORT void Hide(); |
7fd59977 |
166 | |
b69e576a |
167 | //! Destroy window. |
168 | Standard_EXPORT void Destroy(); |
7fd59977 |
169 | |
b69e576a |
170 | //! Clear window content. |
171 | Standard_EXPORT void Clear(); |
7fd59977 |
172 | |
b69e576a |
173 | //! Returns Standard_True if off-screen image buffer is being used |
174 | Standard_Boolean GetUseBuffer() const { return myUseBuffer; } |
7fd59977 |
175 | |
b69e576a |
176 | // Turns on/off usage of off-screen image buffer (can be used for redrawing optimization) |
177 | Standard_EXPORT void SetUseBuffer (Standard_Boolean theToUse); |
7fd59977 |
178 | |
b69e576a |
179 | //! Set active color index for further paintings. |
180 | //! @sa DefineColor() |
181 | Standard_EXPORT void SetColor (Standard_Integer theColor); |
7fd59977 |
182 | |
b69e576a |
183 | //! Set active paint mode (3 for COPY; 6 for XOR). |
184 | Standard_EXPORT void SetMode (Standard_Integer theMode); |
a0fc422a |
185 | |
b69e576a |
186 | //! Draw the string. |
187 | Standard_EXPORT void DrawString (Standard_Integer theX, Standard_Integer theY, |
188 | const char* theText); |
a0fc422a |
189 | |
b69e576a |
190 | //! Draw array of segments. |
191 | Standard_EXPORT void DrawSegments (const Draw_XSegment* theSegments, |
192 | Standard_Integer theNumberOfElements); |
7fd59977 |
193 | |
b69e576a |
194 | //! Redraw window content. |
195 | Standard_EXPORT void Redraw(); |
7fd59977 |
196 | |
b69e576a |
197 | //! Save snapshot. |
198 | Standard_EXPORT Standard_Boolean Save (const char* theFileName) const; |
7fd59977 |
199 | |
b69e576a |
200 | //! Perform window exposing. |
201 | virtual void WExpose() = 0; |
7fd59977 |
202 | |
b69e576a |
203 | //! (Re)initializes off-screen image buffer according to current window size. |
204 | Standard_EXPORT void InitBuffer(); |
7fd59977 |
205 | |
b69e576a |
206 | protected: |
7fd59977 |
207 | |
b69e576a |
208 | //! Main constructor. |
209 | //! @param theTitle [in] window title |
210 | //! @param theXY [in] top-left position |
211 | //! @param theSize [in] window dimensions |
212 | //! @param theParent [in] optional native parent window |
213 | //! @param theWin [in] optional native window |
214 | Standard_EXPORT Draw_Window (const char* theTitle, |
215 | const NCollection_Vec2<int>& theXY, |
216 | const NCollection_Vec2<int>& theSize, |
217 | Aspect_Drawable theParent, |
218 | Aspect_Drawable theWin); |
7fd59977 |
219 | |
b69e576a |
220 | //! Initialize the window. |
221 | Standard_EXPORT void init (const NCollection_Vec2<int>& theXY, |
222 | const NCollection_Vec2<int>& theSize); |
7fd59977 |
223 | |
b69e576a |
224 | public: |
7fd59977 |
225 | |
b69e576a |
226 | #if defined(_WIN32) |
227 | Standard_Boolean IsEqualWindows (HANDLE theWindow) { return myWindow == theWindow; } |
7fd59977 |
228 | |
b69e576a |
229 | Standard_EXPORT static void SelectWait (HANDLE& theWindow, int& theX, int& theY, int& theButton); |
230 | Standard_EXPORT static void SelectNoWait (HANDLE& theWindow, int& theX, int& theY, int& theButton); |
7fd59977 |
231 | // Procedure de fenetre |
b69e576a |
232 | Standard_EXPORT static LRESULT APIENTRY DrawProc (HWND, UINT, WPARAM, LPARAM); |
233 | static HWND hWndClientMDI; |
234 | #elif defined(HAVE_XLIB) |
235 | Standard_Boolean IsEqualWindows (Window theWindow) { return myWindow == theWindow; } |
236 | |
237 | //! Event structure. |
238 | struct Draw_XEvent |
239 | { |
240 | Standard_Integer type; |
241 | Window window; |
242 | Standard_Integer button; |
243 | Standard_Integer x; |
244 | Standard_Integer y; |
245 | }; |
246 | |
247 | //! Retrieve event. |
248 | static void GetNextEvent (Draw_XEvent& theEvent); |
249 | |
250 | void Wait (Standard_Boolean theToWait = Standard_True); |
251 | #elif defined(__APPLE__) |
252 | Standard_Boolean IsEqualWindows (const Standard_Integer& theWindowNumber); |
7fd59977 |
253 | |
b69e576a |
254 | static void GetNextEvent (Standard_Boolean theWait, |
255 | Standard_Integer& theWindowNumber, |
256 | Standard_Integer& theX, |
257 | Standard_Integer& theY, |
258 | Standard_Integer& theButton); |
259 | #endif |
7fd59977 |
260 | |
b69e576a |
261 | private: |
7fd59977 |
262 | |
b69e576a |
263 | #if defined(_WIN32) |
264 | Standard_EXPORT static HWND createDrawWindow (HWND , int ); |
265 | Standard_EXPORT HDC getMemDC (HDC theWinDC); |
266 | Standard_EXPORT void releaseMemDC(HDC theMemDC); |
267 | #elif defined(HAVE_XLIB) |
268 | Drawable GetDrawable() const { return myUseBuffer ? myImageBuffer : myWindow; } |
269 | struct Base_Window; // opaque structure with extra Xlib parameters |
270 | #endif |
7fd59977 |
271 | |
272 | private: |
b69e576a |
273 | |
274 | #if defined(_WIN32) |
275 | HWND myWindow; //!< native window |
276 | HBITMAP myMemHbm; |
277 | HBITMAP myOldHbm; |
7fd59977 |
278 | Standard_Integer myCurrPen; |
279 | Standard_Integer myCurrMode; |
b69e576a |
280 | #elif defined(HAVE_XLIB) |
281 | Window myWindow; //!< native window |
282 | Window myMother; //!< parent native window |
283 | Pixmap myImageBuffer; |
284 | std::unique_ptr<Base_Window> myBase; |
285 | #elif defined(__APPLE__) |
286 | NSWindow* myWindow; //!< native window |
287 | Draw_CocoaView* myView; |
288 | NSImage* myImageBuffer; |
289 | #else |
290 | Aspect_Drawable myWindow; |
7fd59977 |
291 | #endif |
b69e576a |
292 | Standard_Integer myCurrentColor; |
293 | Standard_Boolean myUseBuffer; |
7fd59977 |
294 | |
b69e576a |
295 | }; |
296 | |
297 | #endif // Draw_Window_HeaderFile |