adbbd131616356e64b9981f8d97a049822d1c01b
[occt.git] / src / Draw / Draw_Window.hxx
1 // Created on: 1994-07-27
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1994-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #ifndef Draw_Window_HeaderFile
18 #define Draw_Window_HeaderFile
19
20 #include <Standard_Boolean.hxx>
21 #include <Standard_Integer.hxx>
22 #include <TCollection_AsciiString.hxx>
23
24 #if !defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
25
26 const Standard_Integer MAXCOLOR = 15;
27
28 typedef unsigned long Window;
29 typedef unsigned long Pixmap;
30 typedef unsigned long Drawable;
31
32 // Definition de la class Base_Window (Definie dans Draw_Window.cxx)
33 //===================================
34 struct Base_Window;
35
36 // Definition de la classe Segment
37 //================================
38 struct Segment
39 {
40   short xx1;
41   short yy1;
42   short xx2;
43   short yy2;
44
45   void Init(short x1, short y1, short x2, short y2) {
46     xx1 = x1; yy1 = y1; xx2 = x2; yy2 = y2;
47   }
48
49 };
50
51 // Definition de la structure Event
52 //=================================
53 typedef struct Event
54 {
55   Standard_Integer type;
56   Window window;
57   Standard_Integer button;
58   Standard_Integer x;
59   Standard_Integer y;
60 } Event;
61
62 // Definition de la classe Draw_Window
63 //====================================
64 class Draw_Window
65 {
66   public :
67
68     /**
69      * Type of the callback function that is to be passed to the method
70      * AddCallbackBeforeTerminate().
71      */
72     typedef void (*FCallbackBeforeTerminate)();
73
74     /**
75      * This method registers a callback function that will be called just before exit.
76      * This is usefull especially for Windows platform, on which Draw is normally 
77      * self-terminated instead of exiting.
78      */
79     Standard_EXPORT static void AddCallbackBeforeTerminate(FCallbackBeforeTerminate theCB);
80
81     /**
82      * Just in case method for un-registering a callback previously registered by
83      * AddCallbackBeforeTerminate()
84      */
85     Standard_EXPORT static void RemoveCallbackBeforeTerminate(FCallbackBeforeTerminate theCB);
86
87     Draw_Window (); // the window is not initialized
88     Draw_Window (const char* title,
89                  Standard_Integer X, Standard_Integer Y = 0,
90                  Standard_Integer DX = 50, Standard_Integer DY = 50);
91
92     Draw_Window (Window mother);
93     Draw_Window (Window mother,char* title,
94                  Standard_Integer X = 0, Standard_Integer Y = 0,
95                  Standard_Integer DX = 50, Standard_Integer DY = 50);
96     Draw_Window(const char *window);
97
98     void Init (Standard_Integer X = 0, Standard_Integer Y = 0,
99                Standard_Integer DX = 50, Standard_Integer DY = 50);
100
101     void Init (Window mother,
102                Standard_Integer X = 0, Standard_Integer Y = 0,
103                Standard_Integer DX = 50, Standard_Integer DY = 50);
104
105
106     void StopWinManager();
107
108     void SetPosition (Standard_Integer NewXpos,
109                       Standard_Integer NewYpos);
110
111     void SetDimension(Standard_Integer NewDx,
112                       Standard_Integer NewDy);
113
114     void GetPosition(Standard_Integer &PosX,
115                      Standard_Integer &PosY);
116
117     Standard_Integer HeightWin() const;
118     Standard_Integer WidthWin() const;
119
120     void SetTitle (const TCollection_AsciiString& theTitle);
121     TCollection_AsciiString GetTitle() const;
122
123     //! Return true if window is displayed on the screen.
124     bool IsMapped() const;
125     void DisplayWindow();
126     void Hide();
127     void Destroy();
128     void Clear();
129     void Wait(Standard_Boolean wait = Standard_True);
130
131     Drawable GetDrawable() const;
132     // Initializes off-screen image buffer according to current window size
133     void InitBuffer();
134
135     static Standard_Boolean DefineColor(const Standard_Integer, const char*);
136     void SetColor(int);
137     void SetMode(int);
138     void DrawString(int, int, char*);
139     void DrawSegments(Segment* ,int);
140     void Redraw();
141     static void Flush();
142
143     // save snapshot
144     Standard_Boolean Save(const char* theFileName) const;
145
146     virtual ~Draw_Window ();
147
148     // X Event management
149     virtual void WExpose();
150     virtual void WButtonPress(const Standard_Integer X,
151                               const Standard_Integer Y,
152                               const Standard_Integer& button);
153     virtual void WButtonRelease(const Standard_Integer X,
154                                 const Standard_Integer Y,
155                                 const Standard_Integer& button);
156     //virtual void WKeyPress(char, KeySym& );
157     virtual void WMotionNotify(const Standard_Integer X,
158                                const Standard_Integer Y);
159
160     virtual void WConfigureNotify(const Standard_Integer X,
161                                   const Standard_Integer Y,
162                                   const Standard_Integer dx,
163                                   const Standard_Integer dy);
164
165     virtual void WUnmapNotify();
166
167     Base_Window& base;
168     Window win;
169     Window myMother; // default : myMother is the root window
170     Pixmap myBuffer;
171
172     static Draw_Window* firstWindow;
173     Draw_Window* next;
174     Draw_Window* previous;
175
176     Standard_Boolean myUseBuffer;
177     Standard_Boolean withWindowManager;
178
179 };
180
181 //======================================================
182 // funtion : Run_Appli
183 // purpose : run the application
184 //           interp will be called to interpret a command
185 //           and return True if the command is complete
186 //======================================================
187
188 void Run_Appli(Standard_Boolean (*inteprete) (const char*));
189
190 //======================================================
191 // funtion : Init_Appli
192 // purpose :
193 //======================================================
194 Standard_Boolean Init_Appli();
195
196 //======================================================
197 // funtion : Destroy_Appli()
198 // purpose :
199 //======================================================
200 void Destroy_Appli();
201
202 //======================================================
203 // funtion : GetNextEvent()
204 // purpose :
205 //======================================================
206 void GetNextEvent(Event&);
207
208 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
209
210 const Standard_Integer MAXCOLOR = 15;
211
212 struct Segment
213 {
214   Standard_Integer myXStart;
215   Standard_Integer myYStart;
216   Standard_Integer myXEnd;
217   Standard_Integer myYEnd;
218
219   void Init(short theXStart, short theYStart, short theXEnd, short theYEnd) {
220     myXStart = theXStart; myYStart = theYStart; myXEnd = theXEnd; myYEnd = theYEnd;
221   }
222
223 };
224
225 #ifdef __OBJC__
226   @class NSView;
227   @class NSWindow;
228   @class NSImage;
229   @class Draw_CocoaView;
230 #else
231   struct NSView;
232   struct NSWindow;
233   struct NSImage;
234   struct Draw_CocoaView;
235 #endif
236
237
238 class Draw_Window
239 {
240   public :
241
242   /**
243    * Type of the callback function that is to be passed to the method
244    * AddCallbackBeforeTerminate().
245    */
246   typedef void (*FCallbackBeforeTerminate)();
247
248   /**
249    * This method registers a callback function that will be called just before exit.
250    * This is usefull especially for Windows platform, on which Draw is normally 
251    * self-terminated instead of exiting.
252    */
253   Standard_EXPORT static void AddCallbackBeforeTerminate(FCallbackBeforeTerminate theCB);
254
255   /**
256    * Just in case method for un-registering a callback previously registered by
257    * AddCallbackBeforeTerminate()
258    */
259   Standard_EXPORT static void RemoveCallbackBeforeTerminate(FCallbackBeforeTerminate theCB);
260
261   Draw_Window (); // the window is not initialized
262   Draw_Window (Standard_CString        theTitle,
263                const Standard_Integer& theXLeft = 0,  const Standard_Integer& theYTop   = 0,
264                const Standard_Integer& theWidth = 50, const Standard_Integer& theHeight = 50);
265
266   Draw_Window (NSWindow*               theWindow,     Standard_CString        theTitle,
267                const Standard_Integer& theXLeft = 0,  const Standard_Integer& theYTop   = 0,
268                const Standard_Integer& theWidth = 50, const Standard_Integer& theHeight = 50);
269
270   void Init (const Standard_Integer& theXLeft = 0,  const Standard_Integer& theYLeft  = 0,
271              const Standard_Integer& theWidth = 50, const Standard_Integer& theHeight = 50);
272
273   virtual ~Draw_Window ();
274
275   void SetPosition (const Standard_Integer& theNewXpos,
276                     const Standard_Integer& theNewYpos);
277
278   void SetDimension (const Standard_Integer& theNewWidth,
279                      const Standard_Integer& theNewHeight);
280
281   void GetPosition (Standard_Integer &thePosX,
282                     Standard_Integer &thePosY);
283
284   Standard_Integer HeightWin() const;
285   Standard_Integer WidthWin()  const;
286
287   void  SetTitle (const TCollection_AsciiString& theTitle);
288   TCollection_AsciiString GetTitle() const;
289
290   //! Return true if window is displayed on the screen.
291   bool IsMapped() const;
292   void DisplayWindow();
293   void Hide();
294   void Destroy();
295   void Clear();
296
297   void InitBuffer();
298
299   static Standard_Boolean DefineColor (const Standard_Integer&, Standard_CString);
300   void SetColor     (const Standard_Integer& theColor);
301   void SetMode      (const Standard_Integer& theMode);
302   void DrawString   (const Standard_Integer& theX, const Standard_Integer& theY, char* theText);
303   void DrawSegments (Segment* theSegment, const Standard_Integer& theNumberOfElements);
304   void Redraw();
305   static void Flush();
306   
307   // save snapshot
308   Standard_Boolean Save (Standard_CString theFileName) const;
309   
310   Standard_Boolean IsEqualWindows (const Standard_Integer& theWindowNumber);
311
312 private:
313   NSWindow*        myWindow;
314   Draw_CocoaView*  myView;
315   NSImage*         myImageBuffer;
316   Standard_Boolean myUseBuffer;
317   Standard_Integer myCurrentColor;
318
319   static Draw_Window* firstWindow;
320   Draw_Window*        nextWindow;
321   Draw_Window*        previousWindow;
322
323 };
324
325 //======================================================
326 // funtion : Run_Appli
327 // purpose : run the application
328 //           interp will be called to interpret a command
329 //           and return True if the command is complete
330 //======================================================
331
332 void Run_Appli(Standard_Boolean (*inteprete) (const char*));
333
334 //======================================================
335 // funtion : Init_Appli
336 // purpose :
337 //======================================================
338 Standard_Boolean Init_Appli();
339
340 //======================================================
341 // funtion : Destroy_Appli()
342 // purpose :
343 //======================================================
344 void Destroy_Appli();
345
346 //======================================================
347 // funtion : GetNextEvent()
348 // purpose :
349 //======================================================
350 void GetNextEvent (Standard_Boolean  theWait,
351                    Standard_Integer& theWindowNumber,
352                    Standard_Integer& theX,
353                    Standard_Integer& theY,
354                    Standard_Integer& theButton);
355 #else
356
357 // Specifique WNT
358
359 #include <windows.h>
360
361 #define DRAWCLASS L"DRAWWINDOW"
362 #define DRAWTITLE L"Draw View"
363 #define MAXCOLOR  15
364
365 #if !defined(__Draw_API) && !defined(HAVE_NO_DLL)
366 # ifdef __Draw_DLL
367 #  define __Draw_API __declspec( dllexport )
368 # else
369 #  define __Draw_API __declspec( dllimport )
370 # endif
371 #endif
372
373 // definition de la classe Segment
374
375 class DrawWindow;
376 class Segment
377 {
378   friend class DrawWindow;
379   public :
380     //constructeur
381     Segment () {}
382     //destructeur
383     ~Segment () {}
384
385     //methods
386     void Init(Standard_Integer,
387               Standard_Integer,
388               Standard_Integer,
389               Standard_Integer);
390   private:
391     //atributs :
392     Standard_Integer x1;
393     Standard_Integer y1;
394     Standard_Integer x2;
395     Standard_Integer y2;
396 };
397
398 //definition de la classe DRAWWINDOW
399
400 class DrawWindow
401 {
402   //constructeur
403 public:
404
405   /**
406    * Type of the callback function that is to be passed to the method
407    * AddCallbackBeforeTerminate().
408    */
409   typedef void (*FCallbackBeforeTerminate)();
410
411   /**
412    * This method registers a callback function that will be called just before exit.
413    * This is usefull especially for Windows platform, on which Draw is normally 
414    * self-terminated instead of exiting.
415    */
416   Standard_EXPORT static void AddCallbackBeforeTerminate(FCallbackBeforeTerminate theCB);
417
418   /**
419    * Just in case method for un-registering a callback previously registered by
420    * AddCallbackBeforeTerminate()
421    */
422   Standard_EXPORT static void RemoveCallbackBeforeTerminate(FCallbackBeforeTerminate theCB);
423
424   __Draw_API DrawWindow();
425   __Draw_API DrawWindow(const char*, Standard_Integer, Standard_Integer,
426                         Standard_Integer, Standard_Integer);
427   __Draw_API DrawWindow(const char*, Standard_Integer, Standard_Integer,
428                         Standard_Integer, Standard_Integer, HWND);
429   //destructeur
430   __Draw_API virtual ~DrawWindow();
431
432   //methods
433 public:
434   __Draw_API void Init(Standard_Integer, Standard_Integer,
435                        Standard_Integer, Standard_Integer);
436
437   __Draw_API void SetUseBuffer(Standard_Boolean);
438   // Turns on/off usage of off-screen image buffer (can be used for redrawing optimization)
439
440   Standard_Boolean GetUseBuffer() const { return myUseBuffer; }
441   // Returns Standard_True if off-screen image buffer is being used
442
443   //taille et position
444   __Draw_API void SetPosition (Standard_Integer,Standard_Integer);
445   __Draw_API void SetDimension(Standard_Integer,Standard_Integer);
446   __Draw_API void GetPosition (Standard_Integer&,Standard_Integer&);
447   __Draw_API Standard_Integer HeightWin() const;
448   __Draw_API Standard_Integer WidthWin()  const;
449
450   //Title
451   __Draw_API void SetTitle (const TCollection_AsciiString& );
452   __Draw_API TCollection_AsciiString GetTitle() const;
453
454   //Affichage
455     //! Return true if window is displayed on the screen.
456     bool IsMapped() const;
457   __Draw_API void DisplayWindow();
458   __Draw_API void Hide();
459   __Draw_API void Destroy();
460   __Draw_API void Clear();
461   static void Flush() {} ;
462
463   // save snapshot
464   __Draw_API Standard_Boolean Save(const char* theFileName) const;
465
466   //Dessin
467   __Draw_API void DrawString(int,int,char*);
468   __Draw_API void DrawSegments(Segment*,int);
469
470   __Draw_API void InitBuffer();
471   // Initializes off-screen image buffer according to current window size
472
473   __Draw_API void Redraw();
474   // Copies an image from memory buffer to screen
475
476   //Couleur
477   __Draw_API void SetColor(Standard_Integer);
478   __Draw_API void SetMode(int);
479   __Draw_API static Standard_Boolean DefineColor ( const Standard_Integer,const char*);
480
481   //Gestion des Messages
482   __Draw_API virtual void WExpose ();
483   __Draw_API virtual void WButtonPress(const Standard_Integer,const Standard_Integer,
484                                        const Standard_Integer&);
485   __Draw_API virtual void WButtonRelease(const Standard_Integer,const Standard_Integer,
486                                          const Standard_Integer&);
487   __Draw_API virtual void WMotionNotify(const Standard_Integer,const Standard_Integer);
488   __Draw_API virtual void WConfigureNotify(const Standard_Integer,const Standard_Integer,
489                                            const Standard_Integer,const Standard_Integer);
490   __Draw_API virtual void WUnmapNotify();
491
492   //Gestion souris
493   __Draw_API static void SelectWait   (HANDLE&,int&,int&,int&);
494   __Draw_API static void SelectNoWait (HANDLE&,int&,int&,int&);
495
496   // Procedure de fenetre
497   __Draw_API static LRESULT APIENTRY DrawProc (HWND,UINT,WPARAM,LPARAM);
498
499 private:
500
501   __Draw_API static HWND CreateDrawWindow(HWND,int);
502   __Draw_API HDC  GetMemDC(HDC);
503   __Draw_API void ReleaseMemDC(HDC);
504
505   //atributs
506 public:
507   HWND win;
508   static HWND hWndClientMDI;
509
510 private:
511   static DrawWindow* firstWindow;
512   DrawWindow* next;
513   DrawWindow* previous;
514   HBITMAP myMemHbm;
515   HBITMAP myOldHbm;
516   Standard_Boolean myUseBuffer;
517   Standard_Integer myCurrPen;
518   Standard_Integer myCurrMode;
519 };
520
521 typedef DrawWindow Draw_Window;
522 typedef enum {
523   STOP_CONSOLE,
524   WAIT_CONSOLE_COMMAND,
525   HAS_CONSOLE_COMMAND} console_semaphore_value;
526
527 // PROCEDURE DE DRAW WINDOW
528
529 __Draw_API Standard_Boolean Init_Appli(HINSTANCE,HINSTANCE,int,HWND&);
530 __Draw_API void Run_Appli(HWND);
531 __Draw_API void Destroy_Appli(HINSTANCE);
532
533 #endif
534
535 #endif