2007-11-04 03:51:54 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
Copyright (c) 2001, Loki software, inc.
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
Redistributions of source code must retain the above copyright notice, this list
|
|
|
|
of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
other materials provided with the distribution.
|
|
|
|
|
|
|
|
Neither the name of Loki software nor the names of its contributors may be used
|
|
|
|
to endorse or promote products derived from this software without specific prior
|
|
|
|
written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// GLWindow - Base class for the small views used by Radiant
|
|
|
|
//
|
|
|
|
// Leonardo Zide (leo@lokigames.com
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "glwidget.h"
|
|
|
|
#include "glwindow.h"
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
// static functions
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static void realize( GtkWidget *widget, gpointer data ){
|
|
|
|
GLWindow *wnd = (GLWindow*)data;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
wnd->OnCreate();
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static gint expose( GtkWidget *widget, GdkEventExpose *event, gpointer data ){
|
|
|
|
GLWindow *wnd = (GLWindow*)data;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( event->count > 0 ) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
#endif
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !g_pParentWnd->IsSleeping() ) {
|
|
|
|
wnd->OnExpose();
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return TRUE;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static void button_press( GtkWidget *widget, GdkEventButton *event, gpointer data ){
|
|
|
|
GLWindow *wnd = (GLWindow*)data;
|
|
|
|
guint32 flags = 0;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
gdk_pointer_grab( widget->window, FALSE,
|
|
|
|
(GdkEventMask)( GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK ),
|
|
|
|
NULL, NULL, GDK_CURRENT_TIME );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
gtk_window_set_focus( GTK_WINDOW( g_pParentWnd->m_pWidget ), widget );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
switch ( event->button )
|
|
|
|
{
|
|
|
|
case 1: flags |= MK_LBUTTON; break;
|
|
|
|
case 2: flags |= MK_MBUTTON; break;
|
|
|
|
case 3: flags |= MK_RBUTTON; break;
|
|
|
|
#if !GTK_CHECK_VERSION( 1,3,0 )
|
|
|
|
case 4: wnd->OnMouseWheel( true ); break;
|
|
|
|
case 5: wnd->OnMouseWheel( false ); break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ( event->state & GDK_CONTROL_MASK ) != 0 ) {
|
|
|
|
flags |= MK_CONTROL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ( event->state & GDK_SHIFT_MASK ) != 0 ) {
|
|
|
|
flags |= MK_SHIFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( event->type == GDK_BUTTON_PRESS ) {
|
|
|
|
switch ( event->button )
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
wnd->OnLButtonDown( flags, (int)event->x, (int)event->y ); break;
|
|
|
|
case 2:
|
|
|
|
wnd->OnMButtonDown( flags, (int)event->x, (int)event->y ); break;
|
|
|
|
case 3:
|
|
|
|
wnd->OnRButtonDown( flags, (int)event->x, (int)event->y ); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( event->type == GDK_2BUTTON_PRESS ) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static void button_release( GtkWidget *widget, GdkEventButton *event, gpointer data ){
|
|
|
|
GLWindow *wnd = (GLWindow*)data;
|
|
|
|
guint32 flags = 0;
|
|
|
|
|
|
|
|
gdk_pointer_ungrab( GDK_CURRENT_TIME );
|
|
|
|
|
|
|
|
if ( ( event->state & GDK_CONTROL_MASK ) != 0 ) {
|
|
|
|
flags |= MK_CONTROL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ( event->state & GDK_SHIFT_MASK ) != 0 ) {
|
|
|
|
flags |= MK_SHIFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( event->button )
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
wnd->OnLButtonUp( flags, (int)event->x, (int)event->y ); break;
|
|
|
|
case 2:
|
|
|
|
wnd->OnMButtonUp( flags, (int)event->x, (int)event->y ); break;
|
|
|
|
case 3:
|
|
|
|
wnd->OnRButtonUp( flags, (int)event->x, (int)event->y ); break;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static void motion( GtkWidget *widget, GdkEventMotion *event, gpointer data ){
|
|
|
|
GLWindow *wnd = (GLWindow*)data;
|
|
|
|
guint32 flags = 0;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( event->state & GDK_BUTTON1_MASK ) != 0 ) {
|
|
|
|
flags |= MK_LBUTTON;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( event->state & GDK_BUTTON2_MASK ) != 0 ) {
|
|
|
|
flags |= MK_MBUTTON;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( event->state & GDK_BUTTON3_MASK ) != 0 ) {
|
|
|
|
flags |= MK_RBUTTON;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( event->state & GDK_CONTROL_MASK ) != 0 ) {
|
|
|
|
flags |= MK_CONTROL;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( event->state & GDK_SHIFT_MASK ) != 0 ) {
|
|
|
|
flags |= MK_SHIFT;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
wnd->OnMouseMove( flags, (int)event->x, (int)event->y );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static void resize( GtkWidget *widget, GtkAllocation *allocation, gpointer data ){
|
|
|
|
GLWindow *wnd = (GLWindow*)data;
|
|
|
|
wnd->OnSize( allocation->width, allocation->height );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static gint timer( gpointer data ){
|
|
|
|
GLWindow *wnd = (GLWindow*)data;
|
|
|
|
wnd->OnTimer();
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return TRUE;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//! GtkGLExt port.
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
static void create_context (GtkWidget *widget, gpointer data)
|
|
|
|
{
|
|
|
|
if (g_qeglobals_gui.d_glBase == NULL)
|
2007-11-04 03:51:54 +00:00
|
|
|
g_qeglobals_gui.d_glBase = widget;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static void destroy_context (GtkWidget *widget, gpointer data)
|
|
|
|
{
|
|
|
|
if (g_qeglobals_gui.d_glBase == widget)
|
2007-11-04 03:51:54 +00:00
|
|
|
g_qeglobals_gui.d_glBase = NULL;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
*/
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#if GTK_CHECK_VERSION( 1,3,0 )
|
2007-11-04 03:51:54 +00:00
|
|
|
static gint scroll_event( GtkWidget *widget,
|
2012-03-17 20:01:54 +00:00
|
|
|
GdkEventScroll *event,
|
|
|
|
gpointer data ){
|
|
|
|
GLWindow *wnd = (GLWindow*)data;
|
|
|
|
wnd->OnMouseWheel( ( event->direction == GDK_SCROLL_UP ) ? true : false );
|
|
|
|
return TRUE;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
// GLWindow class
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
//#define DBG_GLWINDOW
|
|
|
|
#endif
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GLWindow::GLWindow ( bool zbuffer ){
|
|
|
|
m_nTimer = 0;
|
|
|
|
m_bMouseCapture = FALSE;
|
|
|
|
m_pParent = NULL;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
m_pWidget = gtk_glwidget_new( zbuffer, g_qeglobals_gui.d_glBase );
|
|
|
|
GTK_WIDGET_SET_FLAGS( m_pWidget, GTK_CAN_FOCUS );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
#ifdef DBG_GLWINDOW
|
2012-03-17 20:01:54 +00:00
|
|
|
Sys_Printf( "GLWindow::GLWindow m_pWidget = %p\n", m_pWidget );
|
2007-11-04 03:51:54 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//! GtkGLExt port.
|
|
|
|
//#if defined (__linux__) || defined (__APPLE__)
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( g_qeglobals_gui.d_glBase == NULL ) {
|
|
|
|
g_qeglobals_gui.d_glBase = m_pWidget;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
//#endif
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#if GTK_CHECK_VERSION( 1,3,0 )
|
|
|
|
gtk_widget_set_events( m_pWidget, GDK_DESTROY | GDK_EXPOSURE_MASK |
|
|
|
|
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_SCROLL_MASK );
|
2007-11-04 03:51:54 +00:00
|
|
|
#else
|
2012-03-17 20:01:54 +00:00
|
|
|
gtk_widget_set_events( m_pWidget, GDK_DESTROY | GDK_EXPOSURE_MASK |
|
|
|
|
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK );
|
2007-11-04 03:51:54 +00:00
|
|
|
#endif
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
// Connect signal handlers
|
|
|
|
gtk_signal_connect( GTK_OBJECT( m_pWidget ), "realize", GTK_SIGNAL_FUNC( realize ), this );
|
|
|
|
gtk_signal_connect( GTK_OBJECT( m_pWidget ), "expose_event", GTK_SIGNAL_FUNC( expose ), this );
|
|
|
|
gtk_signal_connect( GTK_OBJECT( m_pWidget ), "motion_notify_event", GTK_SIGNAL_FUNC( motion ), this );
|
|
|
|
gtk_signal_connect( GTK_OBJECT( m_pWidget ), "button_press_event", GTK_SIGNAL_FUNC( button_press ), this );
|
|
|
|
gtk_signal_connect( GTK_OBJECT( m_pWidget ), "button_release_event",GTK_SIGNAL_FUNC( button_release ), this );
|
|
|
|
gtk_signal_connect( GTK_OBJECT( m_pWidget ), "size_allocate", GTK_SIGNAL_FUNC( resize ), this );
|
2007-11-04 03:51:54 +00:00
|
|
|
//! GtkGLExt port.
|
|
|
|
// gtk_signal_connect (GTK_OBJECT (m_pWidget), "create_context", GTK_SIGNAL_FUNC (create_context), this);
|
|
|
|
// gtk_signal_connect (GTK_OBJECT (m_pWidget), "destroy_context", GTK_SIGNAL_FUNC (destroy_context), this);
|
2012-03-17 20:01:54 +00:00
|
|
|
#if GTK_CHECK_VERSION( 1,3,0 )
|
|
|
|
gtk_signal_connect( GTK_OBJECT( m_pWidget ), "scroll_event", GTK_SIGNAL_FUNC( scroll_event ), this );
|
2007-11-04 03:51:54 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GLWindow::~GLWindow (){
|
2007-11-04 03:51:54 +00:00
|
|
|
#ifdef DBG_GLWINDOW
|
2012-03-17 20:01:54 +00:00
|
|
|
Sys_Printf( "GLWindow::~GLWindow m_pWidget = %p\n", m_pWidget );
|
2007-11-04 03:51:54 +00:00
|
|
|
#endif
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( m_pWidget && GTK_IS_WIDGET( m_pWidget ) ) {
|
|
|
|
gtk_widget_destroy( m_pWidget );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void GLWindow::DestroyContext(){
|
|
|
|
gtk_glwidget_destroy_context( m_pWidget );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void GLWindow::CreateContext(){
|
|
|
|
gtk_glwidget_create_context( m_pWidget );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void GLWindow::SetTimer( guint millisec ){
|
|
|
|
m_nTimer = gtk_timeout_add( millisec, timer, this );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void GLWindow::KillTimer(){
|
|
|
|
gtk_timeout_remove( m_nTimer );
|
|
|
|
m_nTimer = 0;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
bool GLWindow::MakeCurrent(){
|
|
|
|
return gtk_glwidget_make_current( m_pWidget );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void GLWindow::SwapBuffers(){
|
|
|
|
gtk_glwidget_swap_buffers( m_pWidget );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|