2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 //-----------------------------------------------------------------------------
25 // implementation of IMessaging specific interface
30 CPtrArray l_Listeners[RADIANT_MSGCOUNT];
31 CPtrArray l_WindowListeners;
32 CXYWndWrapper l_XYWndWrapper;
34 // CGtkWindow implementation -------------------------------------
36 static void button_press (GtkWidget *widget, GdkEventButton *event, gpointer data)
38 IWindowListener *pListen = static_cast<IWindowListener *>(data);
39 switch (event->button)
42 pListen->OnLButtonDown(event->state, event->x, event->y); break;
44 pListen->OnRButtonDown(event->state, event->x, event->y); break;
48 static void button_release (GtkWidget *widget, GdkEventButton *event, gpointer data)
50 IWindowListener *pListen = static_cast<IWindowListener *>(data);
51 switch (event->button)
54 pListen->OnLButtonUp(event->state, event->x, event->y); break;
56 pListen->OnRButtonUp(event->state, event->x, event->y); break;
60 static void motion (GtkWidget *widget, GdkEventMotion *event, gpointer data)
62 IWindowListener *pListen = static_cast<IWindowListener *>(data);
63 pListen->OnMouseMove(event->state, event->x, event->y);
66 static gint expose (GtkWidget *widget, GdkEventExpose *event, gpointer data)
71 CGtkWindow *pWindow = static_cast<CGtkWindow *>(data);
77 // we use the string versions of the keys for now..
78 static gint keypress (GtkWidget* widget, GdkEventKey* event, gpointer data)
82 IWindowListener *pListen = static_cast<IWindowListener *>(data);
83 ret = pListen->OnKeyPressed(gdk_keyval_name(event->keyval));
86 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event");
91 // close_widget is not hooked on the listener but on the CGtkWindow object to handle the closure
92 static gint close_widget (GtkWidget *widget, GdkEvent* event, gpointer data)
94 CGtkWindow *pWindow = static_cast<CGtkWindow *>(data);
100 void CGtkWindow::DoExpose()
102 gtk_glwidget_make_current(m_pGLWidget);
103 if (m_pListen->Paint())
104 gtk_glwidget_swap_buffers(m_pGLWidget);
107 void CGtkWindow::Redraw()
109 gtk_widget_queue_draw(m_pGLWidget);
112 void CGtkWindow::Close()
114 // similar to a destructor, except we warn first
116 m_pListen->DecRef(); m_pListen = NULL;
117 gtk_widget_destroy(m_pWnd); m_pWnd = NULL;
120 bool CGtkWindow::Show()
122 // check we got everything and are reading to instanciate
123 if (m_nWidthParam == 0 || m_nHeightParam == 0)
125 Sys_FPrintf(SYS_ERR, "Height and Width params not set in CGtkWindow::Show\n");
130 Sys_FPrintf(SYS_ERR, "No listener set in CGtkWindow::Show\n");
134 // seems all good, here we go
135 m_pWnd = gtk_window_new (GTK_WINDOW_TOPLEVEL);
136 gtk_window_set_title (GTK_WINDOW (m_pWnd), m_Name.GetBuffer());
137 gtk_window_set_default_size (GTK_WINDOW (m_pWnd), m_nWidthParam, m_nHeightParam);
138 gtk_widget_show (m_pWnd);
140 // GL widget creation
141 m_pGLWidget = gtk_glwidget_new(FALSE, g_qeglobals_gui.d_glBase);
143 gtk_widget_set_events (m_pGLWidget, GDK_DESTROY | GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
144 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK);
146 // Connect signal handlers
147 gtk_signal_connect (GTK_OBJECT (m_pGLWidget), "expose_event", GTK_SIGNAL_FUNC (expose), this);
148 gtk_signal_connect (GTK_OBJECT (m_pGLWidget), "motion_notify_event",
149 GTK_SIGNAL_FUNC (motion), m_pListen);
150 gtk_signal_connect (GTK_OBJECT (m_pGLWidget), "button_press_event",
151 GTK_SIGNAL_FUNC (button_press), m_pListen);
152 gtk_signal_connect (GTK_OBJECT (m_pGLWidget), "button_release_event",
153 GTK_SIGNAL_FUNC (button_release), m_pListen);
155 gtk_signal_connect (GTK_OBJECT (m_pWnd), "delete_event", GTK_SIGNAL_FUNC (close_widget), this);
156 gtk_signal_connect (GTK_OBJECT (m_pWnd), "key_press_event",
157 GTK_SIGNAL_FUNC (keypress), m_pListen);
159 gtk_widget_show (m_pGLWidget);
160 gtk_container_add (GTK_CONTAINER (m_pWnd), m_pGLWidget);
165 IWindow* WINAPI QERApp_CreateGLWindow()
167 return new CGtkWindow;
170 void WINAPI QERApp_HookWindow(IWindowListener* pListen)
172 l_WindowListeners.Add( pListen );
176 void WINAPI QERApp_UnHookWindow(IWindowListener* pListen)
178 for ( int i = 0; i < l_WindowListeners.GetSize(); i++ )
180 if (l_WindowListeners.GetAt(i) == pListen)
182 l_WindowListeners.RemoveAt(i);
188 Sys_Printf("WARNING: IWindowListener not found in QERApp_UnHookWindow\n");
192 void DispatchOnMouseMove(guint32 nFlags, int x, int y)
194 for( int i = 0; i < l_WindowListeners.GetSize(); i++ )
195 static_cast<IWindowListener*>(l_WindowListeners.GetAt(i))->OnMouseMove( nFlags, x, y );
198 bool DispatchOnLButtonDown(guint32 nFlags, int x, int y)
200 for( int i = 0; i < l_WindowListeners.GetSize(); i++ )
201 if (static_cast<IWindowListener*>(l_WindowListeners.GetAt(i))->OnLButtonDown( nFlags, x, y ))
206 bool DispatchOnLButtonUp(guint32 nFlags, int x, int y)
208 for( int i = 0; i < l_WindowListeners.GetSize(); i++ )
209 if (static_cast<IWindowListener*>(l_WindowListeners.GetAt(i))->OnLButtonUp( nFlags, x, y ))
214 void WINAPI QERApp_HookListener(IListener* pListen, int Msg)
217 if (Msg >= RADIANT_MSGCOUNT)
219 Sys_Printf("ERROR: bad index in QERApp_HookListener\n");
223 l_Listeners[Msg].Add( pListen );
227 int WINAPI QERApp_UnHookListener(IListener* pListen)
230 for( int i = 0; i<RADIANT_MSGCOUNT; i++ )
231 for( int j = 0; j<l_Listeners[i].GetSize(); j++ )
232 if (l_Listeners[i].GetAt(j) == pListen)
234 l_Listeners[i].RemoveAt(j);
241 void DispatchRadiantMsg( int Msg )
244 if (Msg >= RADIANT_MSGCOUNT)
246 Sys_Printf("ERROR: bad index in DispatchRadiantMsg\n");
250 for(int i = 0; i<l_Listeners[Msg].GetSize(); i++)
251 static_cast<IListener *>(l_Listeners[Msg].GetAt(i))->DispatchRadiantMsg(Msg);
254 void CXYWndWrapper::SnapToGrid( int x1, int y1, vec3_t pt )
256 int height = g_pParentWnd->ActiveXY()->GetWidget()->allocation.height;
257 g_pParentWnd->ActiveXY()->SnapToPoint( x1, height - 1 - y1, pt );
260 VIEWTYPE CXYWndWrapper::GetViewType( void )
262 return (VIEWTYPE)g_pParentWnd->ActiveXY()->GetViewType();
265 IXYWndWrapper* WINAPI QERApp_GetXYWndWrapper()
267 return &l_XYWndWrapper;