]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/ui.cpp
misc fixes
[xonotic/netradiant.git] / radiant / ui.cpp
1 /*
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
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.
11
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.
16
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
20 */
21
22 //-----------------------------------------------------------------------------
23 //
24 // DESCRIPTION:
25 // implementation of IMessaging specific interface
26 // 
27
28 #include "stdafx.h"
29
30 CPtrArray l_Listeners[RADIANT_MSGCOUNT];
31 CPtrArray l_WindowListeners;
32 CXYWndWrapper l_XYWndWrapper;
33
34 // CGtkWindow implementation -------------------------------------
35
36 static void button_press (GtkWidget *widget, GdkEventButton *event, gpointer data)
37 {
38   IWindowListener *pListen = static_cast<IWindowListener *>(data);
39   switch (event->button)
40   {
41   case 1:
42     pListen->OnLButtonDown(event->state, event->x, event->y); break;
43   case 3:
44     pListen->OnRButtonDown(event->state, event->x, event->y); break;
45   }
46 }
47
48 static void button_release (GtkWidget *widget, GdkEventButton *event, gpointer data)
49 {
50   IWindowListener *pListen = static_cast<IWindowListener *>(data);
51   switch (event->button)
52   {
53   case 1:
54     pListen->OnLButtonUp(event->state, event->x, event->y); break;
55   case 3:
56     pListen->OnRButtonUp(event->state, event->x, event->y); break;
57   }
58 }
59
60 static void motion (GtkWidget *widget, GdkEventMotion *event, gpointer data)
61 {
62   IWindowListener *pListen = static_cast<IWindowListener *>(data);
63   pListen->OnMouseMove(event->state, event->x, event->y);
64 }
65
66 static gint expose (GtkWidget *widget, GdkEventExpose *event, gpointer data)
67 {
68   if (event->count > 0)
69     return TRUE;
70
71   CGtkWindow *pWindow = static_cast<CGtkWindow *>(data);
72   pWindow->DoExpose();
73
74   return TRUE;
75 }
76
77 // we use the string versions of the keys for now..
78 static gint keypress (GtkWidget* widget, GdkEventKey* event, gpointer data)
79 {
80   gint ret;
81
82   IWindowListener *pListen = static_cast<IWindowListener *>(data);
83   ret = pListen->OnKeyPressed(gdk_keyval_name(event->keyval));
84   if (ret)
85   {
86     gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event");
87   }
88   return ret;
89 }
90
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)
93 {
94   CGtkWindow *pWindow = static_cast<CGtkWindow *>(data);
95   pWindow->Close();
96
97   return TRUE;
98 }
99
100 void CGtkWindow::DoExpose()
101 {
102   gtk_glwidget_make_current(m_pGLWidget);
103   if (m_pListen->Paint())
104     gtk_glwidget_swap_buffers(m_pGLWidget);
105 }
106
107 void CGtkWindow::Redraw()
108 {
109   gtk_widget_queue_draw(m_pGLWidget);
110 }
111
112 void CGtkWindow::Close()
113 {
114   // similar to a destructor, except we warn first
115   m_pListen->Close();
116   m_pListen->DecRef(); m_pListen = NULL;
117   gtk_widget_destroy(m_pWnd); m_pWnd = NULL;
118 }
119
120 bool CGtkWindow::Show()
121 {
122   // check we got everything and are reading to instanciate
123   if (m_nWidthParam == 0 || m_nHeightParam == 0)
124   {
125     Sys_FPrintf(SYS_ERR, "Height and Width params not set in CGtkWindow::Show\n");
126     return false;
127   }
128   if (!m_pListen)
129   {
130     Sys_FPrintf(SYS_ERR, "No listener set in CGtkWindow::Show\n");
131     return false;
132   }
133
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);
139
140   // GL widget creation
141   m_pGLWidget = gtk_glwidget_new(FALSE, g_qeglobals_gui.d_glBase);
142
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);
145
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);
154
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);
158
159   gtk_widget_show (m_pGLWidget);
160   gtk_container_add (GTK_CONTAINER (m_pWnd), m_pGLWidget);
161
162   return true;
163 }
164
165 IWindow* WINAPI QERApp_CreateGLWindow()
166 {
167   return new CGtkWindow;
168 }
169
170 void WINAPI QERApp_HookWindow(IWindowListener* pListen)
171 {
172   l_WindowListeners.Add( pListen );
173   pListen->IncRef();
174 }
175
176 void WINAPI QERApp_UnHookWindow(IWindowListener* pListen)
177 {
178   for ( int i = 0; i < l_WindowListeners.GetSize(); i++ )
179   {
180     if (l_WindowListeners.GetAt(i) == pListen)
181     {
182       l_WindowListeners.RemoveAt(i);
183       pListen->DecRef();
184       return;
185     }
186   }
187 #ifdef _DEBUG
188   Sys_Printf("WARNING: IWindowListener not found in QERApp_UnHookWindow\n");
189 #endif
190 }
191
192 void DispatchOnMouseMove(guint32 nFlags, int x, int y)
193 {
194   for( int i = 0; i < l_WindowListeners.GetSize(); i++ )
195     static_cast<IWindowListener*>(l_WindowListeners.GetAt(i))->OnMouseMove( nFlags, x, y );
196 }
197
198 bool DispatchOnLButtonDown(guint32 nFlags, int x, int y)
199 {
200   for( int i = 0; i < l_WindowListeners.GetSize(); i++ )
201     if (static_cast<IWindowListener*>(l_WindowListeners.GetAt(i))->OnLButtonDown( nFlags, x, y ))
202       return true;
203   return false;
204 }
205
206 bool DispatchOnLButtonUp(guint32 nFlags, int x, int y)
207 {
208   for( int i = 0; i < l_WindowListeners.GetSize(); i++ )
209     if (static_cast<IWindowListener*>(l_WindowListeners.GetAt(i))->OnLButtonUp( nFlags, x, y ))
210       return true;
211   return false;
212 }
213
214 void WINAPI QERApp_HookListener(IListener* pListen, int Msg)
215 {
216 #ifdef _DEBUG
217   if (Msg >= RADIANT_MSGCOUNT)
218   {
219     Sys_Printf("ERROR: bad index in QERApp_HookListener\n");
220     return;
221   }
222 #endif
223   l_Listeners[Msg].Add( pListen );
224   pListen->IncRef();
225 }
226
227 int  WINAPI QERApp_UnHookListener(IListener* pListen)
228 {
229   int count = 0;
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)
233       {
234         l_Listeners[i].RemoveAt(j);
235         pListen->DecRef();
236         count++;
237       }
238   return count;
239 }
240
241 void DispatchRadiantMsg( int Msg )
242 {
243 #ifdef _DEBUG
244   if (Msg >= RADIANT_MSGCOUNT)
245   {
246     Sys_Printf("ERROR: bad index in DispatchRadiantMsg\n");
247     return;
248   }
249 #endif
250   for(int i = 0; i<l_Listeners[Msg].GetSize(); i++)
251     static_cast<IListener *>(l_Listeners[Msg].GetAt(i))->DispatchRadiantMsg(Msg);
252 }
253
254 void CXYWndWrapper::SnapToGrid( int x1, int y1, vec3_t pt )
255 {
256   int height = g_pParentWnd->ActiveXY()->GetWidget()->allocation.height;
257   g_pParentWnd->ActiveXY()->SnapToPoint( x1, height - 1 - y1, pt );
258 }
259
260 VIEWTYPE CXYWndWrapper::GetViewType( void )
261 {
262   return (VIEWTYPE)g_pParentWnd->ActiveXY()->GetViewType();
263 }
264
265 IXYWndWrapper* WINAPI QERApp_GetXYWndWrapper()
266 {
267   return &l_XYWndWrapper;
268 }