0024975: Improve of preprocessor directives in header files to be equal to the file...
[occt.git] / src / Aspect / Aspect_DisplayConnection.hxx
1 // Copyright (c) 2013-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #ifndef Aspect_DisplayConnection_HeaderFile
15 #define Aspect_DisplayConnection_HeaderFile
16
17 #include <Standard_Transient.hxx>
18 #include <Aspect_XAtom.hxx>
19 #include <Aspect_FBConfig.hxx>
20
21 #include <TCollection_AsciiString.hxx>
22 #include <NCollection_DataMap.hxx>
23
24 struct Aspect_XDisplay;
25 struct Aspect_XVisualInfo;
26
27 //! This class creates and provides connection with X server.
28 //! Raises exception if can not connect to X server.
29 //! On Windows and Mac OS X (in case when Cocoa used) platforms this class does nothing.
30 //! WARRNING: Do not close display connection manually!
31 class Aspect_DisplayConnection : public Standard_Transient
32 {
33   DEFINE_STANDARD_RTTIEXT(Aspect_DisplayConnection, Standard_Transient)
34 public:
35
36   //! Default constructor. Creates connection with display name taken from "DISPLAY" environment variable
37   Standard_EXPORT Aspect_DisplayConnection();
38
39   //! Destructor. Close opened connection.
40   Standard_EXPORT virtual ~Aspect_DisplayConnection();
41
42   //! Constructor. Creates connection with display specified in theDisplayName.
43   //! Display name should be in format "hostname:number" or "hostname:number.screen_number", where:
44   //! hostname      - Specifies the name of the host machine on which the display is physically attached.
45   //! number        - Specifies the number of the display server on that host machine.
46   //! screen_number - Specifies the screen to be used on that server. Optional variable.
47   Standard_EXPORT Aspect_DisplayConnection (const TCollection_AsciiString& theDisplayName);
48
49   //! Constructor wrapping existing Display instance.
50   //! WARNING! it is a responsibility of application to keep this pointer
51   //! valid while Aspect_DisplayConnection is alive and to close Display when it is no more needed.
52   Standard_EXPORT Aspect_DisplayConnection (Aspect_XDisplay* theDisplay);
53
54   //! @return pointer to Display structure that serves as the connection to the X server.
55   Aspect_XDisplay* GetDisplayAspect() { return myDisplay; }
56
57   //! @return TRUE if X Display has been allocated by this class
58   Standard_Boolean IsOwnDisplay() const { return myIsOwnDisplay; }
59
60   //! @return identifier(atom) for custom named property associated with windows that use current connection to X server.
61   uint64_t GetAtom (const Aspect_XAtom theAtom) const
62   {
63     return myAtoms.Find (theAtom);
64   }
65
66   //! @return display name for this connection.
67   const TCollection_AsciiString& GetDisplayName() { return myDisplayName; }
68
69   //! Open connection with display specified in myDisplayName class field
70   //! or takes theDisplay parameter when it is not NULL.
71   //! WARNING! When external Display is specified, it is a responsibility of application
72   //! to keep this pointer valid while Aspect_DisplayConnection is alive
73   //! and to close Display when it is no more needed.
74   //! @param theDisplay external pointer to allocated Display, or NULL if new connection should be created
75   Standard_EXPORT void Init (Aspect_XDisplay* theDisplay);
76
77   //! Return default window visual or NULL when undefined.
78   Aspect_XVisualInfo* GetDefaultVisualInfo() const { return myDefVisualInfo; }
79
80   //! @return native Window FB config (GLXFBConfig on Xlib)
81   Aspect_FBConfig GetDefaultFBConfig() const { return myDefFBConfig; }
82
83   //! Set default window visual; the visual will be deallocated using XFree().
84   Standard_EXPORT void SetDefaultVisualInfo (Aspect_XVisualInfo* theVisual,
85                                              Aspect_FBConfig theFBConfig);
86
87 #ifdef X_PROTOCOL
88   //! Constructor wrapping existing Display instance.
89   //! WARNING! it is a responsibility of application to keep this pointer
90   //! valid while Aspect_DisplayConnection is alive and to close Display when it is no more needed.
91   Aspect_DisplayConnection (Display* theDisplay)
92   : Aspect_DisplayConnection ((Aspect_XDisplay* )theDisplay) {}
93
94   //! @return pointer to Display structure that serves as the connection to the X server.
95   Display* GetDisplay() { return (Display* )myDisplay; }
96
97   //! Return default window visual or NULL when undefined.
98   XVisualInfo* GetDefaultVisualInfoX() const { return (XVisualInfo* )myDefVisualInfo; }
99
100   //! Set default window visual; the visual will be deallocated using XFree().
101   void SetDefaultVisualInfo (XVisualInfo* theVisual,
102                              Aspect_FBConfig theFBConfig)
103   {
104     SetDefaultVisualInfo ((Aspect_XVisualInfo* )theVisual, theFBConfig);
105   }
106
107   //! @return identifier(atom) for custom named property associated with windows that use current connection to X server.
108   Atom GetAtomX (const Aspect_XAtom theAtom) const
109   {
110     return (Atom )GetAtom (theAtom);
111   }
112
113   //! Open connection with display specified in myDisplayName class field
114   //! or takes theDisplay parameter when it is not NULL.
115   void Init (Display* theDisplay)
116   {
117     Init ((Aspect_XDisplay* )theDisplay);
118   }
119 #endif
120
121 private:
122
123   Aspect_XDisplay*         myDisplay;
124   Aspect_XVisualInfo*      myDefVisualInfo;
125   Aspect_FBConfig          myDefFBConfig;
126   NCollection_DataMap<Aspect_XAtom, uint64_t> myAtoms;
127   TCollection_AsciiString  myDisplayName;
128   Standard_Boolean         myIsOwnDisplay;
129
130 private:
131
132   //! To protect the connection from closing copying allowed only through the handles.
133   Aspect_DisplayConnection            (const Aspect_DisplayConnection& );
134   Aspect_DisplayConnection& operator= (const Aspect_DisplayConnection& );
135
136 };
137
138 DEFINE_STANDARD_HANDLE (Aspect_DisplayConnection, Standard_Transient)
139
140 #endif // _Aspect_DisplayConnection_H__