applied wxGLApp patch (#464500)
[wxWidgets.git] / include / wx / gtk / glcanvas.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: glcanvas.h
3 // Purpose: wxGLCanvas, for using OpenGL/Mesa with wxWindows and GTK
4 // Author: Robert Roebling
5 // Modified by:
6 // Created: 17/8/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma interface "glcanvas.h"
14 #endif
15
16 #ifndef _WX_GLCANVAS_H_
17 #define _WX_GLCANVAS_H_
18
19 #include "wx/defs.h"
20
21 #if wxUSE_GLCANVAS
22
23 #include "wx/scrolwin.h"
24 #include "wx/app.h"
25
26 extern "C" {
27 #include <GL/gl.h>
28 #include <GL/glx.h>
29 #include <GL/glu.h>
30 }
31
32 //---------------------------------------------------------------------------
33 // Constants for attriblist
34 //---------------------------------------------------------------------------
35
36 enum
37 {
38 WX_GL_RGBA=1, /* use true color palette */
39 WX_GL_BUFFER_SIZE, /* bits for buffer if not WX_GL_RGBA */
40 WX_GL_LEVEL, /* 0 for main buffer, >0 for overlay, <0 for underlay */
41 WX_GL_DOUBLEBUFFER, /* use doublebuffer */
42 WX_GL_STEREO, /* use stereoscopic display */
43 WX_GL_AUX_BUFFERS, /* number of auxiliary buffers */
44 WX_GL_MIN_RED, /* use red buffer with most bits (> MIN_RED bits) */
45 WX_GL_MIN_GREEN, /* use green buffer with most bits (> MIN_GREEN bits) */
46 WX_GL_MIN_BLUE, /* use blue buffer with most bits (> MIN_BLUE bits) */
47 WX_GL_MIN_ALPHA, /* use blue buffer with most bits (> MIN_ALPHA bits) */
48 WX_GL_DEPTH_SIZE, /* bits for Z-buffer (0,16,32) */
49 WX_GL_STENCIL_SIZE, /* bits for stencil buffer */
50 WX_GL_MIN_ACCUM_RED, /* use red accum buffer with most bits (> MIN_ACCUM_RED bits) */
51 WX_GL_MIN_ACCUM_GREEN, /* use green buffer with most bits (> MIN_ACCUM_GREEN bits) */
52 WX_GL_MIN_ACCUM_BLUE, /* use blue buffer with most bits (> MIN_ACCUM_BLUE bits) */
53 WX_GL_MIN_ACCUM_ALPHA /* use blue buffer with most bits (> MIN_ACCUM_ALPHA bits) */
54 };
55
56 //---------------------------------------------------------------------------
57 // classes
58 //---------------------------------------------------------------------------
59
60 class WXDLLEXPORT wxGLContext;
61 class WXDLLEXPORT wxGLCanvas;
62
63 //---------------------------------------------------------------------------
64 // wxGLContext
65 //---------------------------------------------------------------------------
66
67
68 class WXDLLEXPORT wxGLContext: public wxObject
69 {
70 public:
71 wxGLContext( bool isRGB, wxWindow *win, const wxPalette& palette = wxNullPalette );
72 wxGLContext(
73 bool WXUNUSED(isRGB), wxWindow *win,
74 const wxPalette& WXUNUSED(palette),
75 const wxGLContext *other /* for sharing display lists */
76 );
77 ~wxGLContext();
78
79 void SetCurrent();
80 void SetColour(const char *colour);
81 void SwapBuffers();
82
83 void SetupPixelFormat();
84 void SetupPalette(const wxPalette& palette);
85 wxPalette CreateDefaultPalette();
86
87 inline wxPalette* GetPalette() const { return (wxPalette*) & m_palette; }
88 inline wxWindow* GetWindow() const { return m_window; }
89 inline GtkWidget* GetWidget() const { return m_widget; }
90 inline GLXContext GetContext() const { return m_glContext; }
91
92 public:
93 GLXContext m_glContext;
94
95 GtkWidget *m_widget;
96 wxPalette m_palette;
97 wxWindow* m_window;
98
99 private:
100 DECLARE_CLASS(wxGLContext)
101 };
102
103 //---------------------------------------------------------------------------
104 // wxGLCanvas
105 //---------------------------------------------------------------------------
106
107 class WXDLLEXPORT wxGLCanvas: public wxScrolledWindow
108 {
109 public:
110 inline wxGLCanvas() {
111 m_glContext = (wxGLContext*) NULL;
112 m_sharedContext = (wxGLContext*) NULL;
113 m_glWidget = (GtkWidget*) NULL;
114 m_vi = (void*) NULL;
115 m_exposed = FALSE;
116 }
117 wxGLCanvas( wxWindow *parent, wxWindowID id = -1,
118 const wxPoint& pos = wxDefaultPosition,
119 const wxSize& size = wxDefaultSize,
120 long style = 0, const wxString& name = "GLCanvas",
121 int *attribList = (int*) NULL,
122 const wxPalette& palette = wxNullPalette );
123 wxGLCanvas( wxWindow *parent, const wxGLContext *shared = (wxGLContext *)NULL,
124 wxWindowID id = -1,
125 const wxPoint& pos = wxDefaultPosition,
126 const wxSize& size = wxDefaultSize,
127 long style = 0, const wxString& name = "GLCanvas",
128 int *attribList = (int*) NULL,
129 const wxPalette& palette = wxNullPalette );
130 wxGLCanvas( wxWindow *parent, const wxGLCanvas *shared = (wxGLCanvas *)NULL,
131 wxWindowID id = -1,
132 const wxPoint& pos = wxDefaultPosition,
133 const wxSize& size = wxDefaultSize,
134 long style = 0, const wxString& name = "GLCanvas",
135 int *attribList = (int*) NULL,
136 const wxPalette& palette = wxNullPalette );
137
138 bool Create( wxWindow *parent,
139 const wxGLContext *shared = (wxGLContext*)NULL,
140 const wxGLCanvas *shared_context_of = (wxGLCanvas*)NULL,
141 wxWindowID id = -1,
142 const wxPoint& pos = wxDefaultPosition,
143 const wxSize& size = wxDefaultSize,
144 long style = 0, const wxString& name = "GLCanvas",
145 int *attribList = (int*) NULL,
146 const wxPalette& palette = wxNullPalette );
147
148 ~wxGLCanvas();
149
150 void SetCurrent();
151 void SetColour(const char *colour);
152 void SwapBuffers();
153
154 void OnSize(wxSizeEvent& event);
155
156 void OnInternalIdle();
157
158 inline wxGLContext* GetContext() const { return m_glContext; }
159
160 // implementation
161
162 wxGLContext *m_glContext,
163 *m_sharedContext;
164 wxGLCanvas *m_sharedContextOf;
165 void *m_vi; // actually an XVisualInfo*
166 bool m_canFreeVi;
167 GtkWidget *m_glWidget;
168 bool m_exposed;
169
170 // returns an XVisualInfo* based on desired GL attributes;
171 // returns NULL if an appropriate visual is not found. The
172 // caller is reponsible for using XFree() to deallocate
173 // the returned structure.
174 static void* ChooseGLVisual(int *attribList);
175
176 private:
177 DECLARE_EVENT_TABLE()
178 DECLARE_CLASS(wxGLCanvas)
179 };
180
181
182 //---------------------------------------------------------------------------
183 // wxGLApp
184 //---------------------------------------------------------------------------
185
186 class WXDLLEXPORT wxGLApp: public wxApp
187 {
188 public:
189 wxGLApp() : wxApp() { }
190 ~wxGLApp();
191
192 // use this in the constructor of the user-derived wxGLApp class to select
193 // an appropriate X visual for GL. Returns TRUE if an appropriate visual
194 // is found - and sets m_glVisualInfo; FALSE otherwise.
195 bool InitGLVisual(int *attribList);
196
197 private:
198 DECLARE_DYNAMIC_CLASS(wxGLApp)
199 };
200
201
202 #endif
203 // wxUSE_GLCANVAS
204
205 #endif
206 // _WX_GLCANVAS_H_
207