2 Copyright (c) 2001, Loki software, inc.
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
8 Redistributions of source code must retain the above copyright notice, this list
9 of conditions and the following disclaimer.
11 Redistributions in binary form must reproduce the above copyright notice, this
12 list of conditions and the following disclaimer in the documentation and/or
13 other materials provided with the distribution.
15 Neither the name of Loki software nor the names of its contributors may be used
16 to endorse or promote products derived from this software without specific prior
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
23 DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 // GLWindow - Base class for the small views used by Radiant
34 // Leonardo Zide (leo@lokigames.com
41 // =============================================================================
44 static void realize( GtkWidget *widget, gpointer data ){
45 GLWindow *wnd = (GLWindow*)data;
50 static gint expose( GtkWidget *widget, GdkEventExpose *event, gpointer data ){
51 GLWindow *wnd = (GLWindow*)data;
54 if ( event->count > 0 ) {
59 if ( !g_pParentWnd->IsSleeping() ) {
66 static void button_press( GtkWidget *widget, GdkEventButton *event, gpointer data ){
67 GLWindow *wnd = (GLWindow*)data;
70 gdk_pointer_grab( widget->window, FALSE,
71 (GdkEventMask)( GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK ),
72 NULL, NULL, GDK_CURRENT_TIME );
74 gtk_window_set_focus( GTK_WINDOW( g_pParentWnd->m_pWidget ), widget );
76 switch ( event->button )
78 case 1: flags |= MK_LBUTTON; break;
79 case 2: flags |= MK_MBUTTON; break;
80 case 3: flags |= MK_RBUTTON; break;
81 #if !GTK_CHECK_VERSION( 1,3,0 )
82 case 4: wnd->OnMouseWheel( true ); break;
83 case 5: wnd->OnMouseWheel( false ); break;
87 if ( ( event->state & GDK_CONTROL_MASK ) != 0 ) {
91 if ( ( event->state & GDK_SHIFT_MASK ) != 0 ) {
95 if ( event->type == GDK_BUTTON_PRESS ) {
96 switch ( event->button )
99 wnd->OnLButtonDown( flags, (int)event->x, (int)event->y ); break;
101 wnd->OnMButtonDown( flags, (int)event->x, (int)event->y ); break;
103 wnd->OnRButtonDown( flags, (int)event->x, (int)event->y ); break;
106 else if ( event->type == GDK_2BUTTON_PRESS ) {
111 static void button_release( GtkWidget *widget, GdkEventButton *event, gpointer data ){
112 GLWindow *wnd = (GLWindow*)data;
115 gdk_pointer_ungrab( GDK_CURRENT_TIME );
117 if ( ( event->state & GDK_CONTROL_MASK ) != 0 ) {
121 if ( ( event->state & GDK_SHIFT_MASK ) != 0 ) {
125 switch ( event->button )
128 wnd->OnLButtonUp( flags, (int)event->x, (int)event->y ); break;
130 wnd->OnMButtonUp( flags, (int)event->x, (int)event->y ); break;
132 wnd->OnRButtonUp( flags, (int)event->x, (int)event->y ); break;
136 static void motion( GtkWidget *widget, GdkEventMotion *event, gpointer data ){
137 GLWindow *wnd = (GLWindow*)data;
140 if ( ( event->state & GDK_BUTTON1_MASK ) != 0 ) {
144 if ( ( event->state & GDK_BUTTON2_MASK ) != 0 ) {
148 if ( ( event->state & GDK_BUTTON3_MASK ) != 0 ) {
152 if ( ( event->state & GDK_CONTROL_MASK ) != 0 ) {
156 if ( ( event->state & GDK_SHIFT_MASK ) != 0 ) {
160 wnd->OnMouseMove( flags, (int)event->x, (int)event->y );
163 static void resize( GtkWidget *widget, GtkAllocation *allocation, gpointer data ){
164 GLWindow *wnd = (GLWindow*)data;
165 wnd->OnSize( allocation->width, allocation->height );
168 static gint timer( gpointer data ){
169 GLWindow *wnd = (GLWindow*)data;
177 static void create_context (GtkWidget *widget, gpointer data)
179 if (g_qeglobals_gui.d_glBase == NULL)
180 g_qeglobals_gui.d_glBase = widget;
183 static void destroy_context (GtkWidget *widget, gpointer data)
185 if (g_qeglobals_gui.d_glBase == widget)
186 g_qeglobals_gui.d_glBase = NULL;
190 #if GTK_CHECK_VERSION( 1,3,0 )
191 static gint scroll_event( GtkWidget *widget,
192 GdkEventScroll *event,
194 GLWindow *wnd = (GLWindow*)data;
195 wnd->OnMouseWheel( ( event->direction == GDK_SCROLL_UP ) ? true : false );
200 // =============================================================================
204 //#define DBG_GLWINDOW
207 GLWindow::GLWindow ( bool zbuffer ){
209 m_bMouseCapture = FALSE;
212 m_pWidget = gtk_glwidget_new( zbuffer, g_qeglobals_gui.d_glBase );
213 GTK_WIDGET_SET_FLAGS( m_pWidget, GTK_CAN_FOCUS );
216 Sys_Printf( "GLWindow::GLWindow m_pWidget = %p\n", m_pWidget );
220 //#if defined (__linux__) || defined (__APPLE__)
221 if ( g_qeglobals_gui.d_glBase == NULL ) {
222 g_qeglobals_gui.d_glBase = m_pWidget;
226 #if GTK_CHECK_VERSION( 1,3,0 )
227 gtk_widget_set_events( m_pWidget, GDK_DESTROY | GDK_EXPOSURE_MASK |
228 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_SCROLL_MASK );
230 gtk_widget_set_events( m_pWidget, GDK_DESTROY | GDK_EXPOSURE_MASK |
231 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK );
234 // Connect signal handlers
235 gtk_signal_connect( GTK_OBJECT( m_pWidget ), "realize", GTK_SIGNAL_FUNC( realize ), this );
236 gtk_signal_connect( GTK_OBJECT( m_pWidget ), "expose_event", GTK_SIGNAL_FUNC( expose ), this );
237 gtk_signal_connect( GTK_OBJECT( m_pWidget ), "motion_notify_event", GTK_SIGNAL_FUNC( motion ), this );
238 gtk_signal_connect( GTK_OBJECT( m_pWidget ), "button_press_event", GTK_SIGNAL_FUNC( button_press ), this );
239 gtk_signal_connect( GTK_OBJECT( m_pWidget ), "button_release_event",GTK_SIGNAL_FUNC( button_release ), this );
240 gtk_signal_connect( GTK_OBJECT( m_pWidget ), "size_allocate", GTK_SIGNAL_FUNC( resize ), this );
242 // gtk_signal_connect (GTK_OBJECT (m_pWidget), "create_context", GTK_SIGNAL_FUNC (create_context), this);
243 // gtk_signal_connect (GTK_OBJECT (m_pWidget), "destroy_context", GTK_SIGNAL_FUNC (destroy_context), this);
244 #if GTK_CHECK_VERSION( 1,3,0 )
245 gtk_signal_connect( GTK_OBJECT( m_pWidget ), "scroll_event", GTK_SIGNAL_FUNC( scroll_event ), this );
249 GLWindow::~GLWindow (){
251 Sys_Printf( "GLWindow::~GLWindow m_pWidget = %p\n", m_pWidget );
254 if ( m_pWidget && GTK_IS_WIDGET( m_pWidget ) ) {
255 gtk_widget_destroy( m_pWidget );
259 void GLWindow::DestroyContext(){
260 gtk_glwidget_destroy_context( m_pWidget );
263 void GLWindow::CreateContext(){
264 gtk_glwidget_create_context( m_pWidget );
267 void GLWindow::SetTimer( guint millisec ){
268 m_nTimer = gtk_timeout_add( millisec, timer, this );
271 void GLWindow::KillTimer(){
272 gtk_timeout_remove( m_nTimer );
276 bool GLWindow::MakeCurrent(){
277 return gtk_glwidget_make_current( m_pWidget );
280 void GLWindow::SwapBuffers(){
281 gtk_glwidget_swap_buffers( m_pWidget );