Remove <gtk/gtk.h> from most of radiant/*
[xonotic/netradiant.git] / radiant / feedback.cpp
1 /*
2    Copyright (C) 1999-2006 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 // classes used for describing geometry information from q3map feedback
26 //
27
28 #include "feedback.h"
29
30 #include "debugging/debugging.h"
31
32 #include "igl.h"
33 #include "iselection.h"
34
35 #include "map.h"
36 #include "dialog.h"
37 #include "mainframe.h"
38
39
40 CDbgDlg g_DbgDlg;
41
42 void Feedback_draw2D( VIEWTYPE viewType ){
43         g_DbgDlg.draw2D( viewType );
44 }
45
46 void CSelectMsg::saxStartElement( message_info_t *ctx, const xmlChar *name, const xmlChar **attrs ){
47         if ( string_equal( reinterpret_cast<const char*>( name ), "select" ) ) {
48                 // read the message
49                 ESelectState = SELECT_MESSAGE;
50         }
51         else
52         {
53                 // read the brush
54                 ASSERT_MESSAGE( string_equal( reinterpret_cast<const char*>( name ), "brush" ), "FEEDBACK PARSE ERROR" );
55                 ASSERT_MESSAGE( ESelectState == SELECT_MESSAGE, "FEEDBACK PARSE ERROR" );
56                 ESelectState = SELECT_BRUSH;
57                 globalOutputStream() << message.c_str() << '\n';
58         }
59 }
60
61 void CSelectMsg::saxEndElement( message_info_t *ctx, const xmlChar *name ){
62         if ( string_equal( reinterpret_cast<const char*>( name ), "select" ) ) {
63         }
64 }
65
66 void CSelectMsg::saxCharacters( message_info_t *ctx, const xmlChar *ch, int len ){
67         if ( ESelectState == SELECT_MESSAGE ) {
68                 message.write( reinterpret_cast<const char*>( ch ), len );
69         }
70         else
71         {
72                 brush.write( reinterpret_cast<const char*>( ch ), len );
73         }
74 }
75
76 IGL2DWindow* CSelectMsg::Highlight(){
77         GlobalSelectionSystem().setSelectedAll( false );
78         int entitynum, brushnum;
79         if ( sscanf( reinterpret_cast<const char*>( brush.c_str() ), "%i %i", &entitynum, &brushnum ) == 2 ) {
80                 SelectBrush( entitynum, brushnum );
81         }
82         return 0;
83 }
84
85 void CPointMsg::saxStartElement( message_info_t *ctx, const xmlChar *name, const xmlChar **attrs ){
86         if ( string_equal( reinterpret_cast<const char*>( name ), "pointmsg" ) ) {
87                 // read the message
88                 EPointState = POINT_MESSAGE;
89         }
90         else
91         {
92                 // read the brush
93                 ASSERT_MESSAGE( string_equal( reinterpret_cast<const char*>( name ), "point" ), "FEEDBACK PARSE ERROR" );
94                 ASSERT_MESSAGE( EPointState == POINT_MESSAGE, "FEEDBACK PARSE ERROR" );
95                 EPointState = POINT_POINT;
96                 globalOutputStream() << message.c_str() << '\n';
97         }
98 }
99
100 void CPointMsg::saxEndElement( message_info_t *ctx, const xmlChar *name ){
101         if ( string_equal( reinterpret_cast<const char*>( name ), "pointmsg" ) ) {
102         }
103         else if ( string_equal( reinterpret_cast<const char*>( name ), "point" ) ) {
104                 sscanf( point.c_str(), "%g %g %g", &( pt[0] ), &( pt[1] ), &( pt[2] ) );
105                 point.clear();
106         }
107 }
108
109 void CPointMsg::saxCharacters( message_info_t *ctx, const xmlChar *ch, int len ){
110         if ( EPointState == POINT_MESSAGE ) {
111                 message.write( reinterpret_cast<const char*>( ch ), len );
112         }
113         else
114         {
115                 ASSERT_MESSAGE( EPointState == POINT_POINT, "FEEDBACK PARSE ERROR" );
116                 point.write( reinterpret_cast<const char*>( ch ), len );
117         }
118 }
119
120 IGL2DWindow* CPointMsg::Highlight(){
121         return this;
122 }
123
124 void CPointMsg::DropHighlight(){
125 }
126
127 void CPointMsg::Draw2D( VIEWTYPE vt ){
128         int nDim1 = ( vt == YZ ) ? 1 : 0;
129         int nDim2 = ( vt == XY ) ? 1 : 2;
130         glPointSize( 4 );
131         glColor3f( 1.0f,0.0f,0.0f );
132         glBegin( GL_POINTS );
133         glVertex2f( pt[nDim1], pt[nDim2] );
134         glEnd();
135         glBegin( GL_LINE_LOOP );
136         glVertex2f( pt[nDim1] - 8, pt[nDim2] - 8 );
137         glVertex2f( pt[nDim1] + 8, pt[nDim2] - 8 );
138         glVertex2f( pt[nDim1] + 8, pt[nDim2] + 8 );
139         glVertex2f( pt[nDim1] - 8, pt[nDim2] + 8 );
140         glEnd();
141 }
142
143 void CWindingMsg::saxStartElement( message_info_t *ctx, const xmlChar *name, const xmlChar **attrs ){
144         if ( string_equal( reinterpret_cast<const char*>( name ), "windingmsg" ) ) {
145                 // read the message
146                 EPointState = WINDING_MESSAGE;
147         }
148         else
149         {
150                 // read the brush
151                 ASSERT_MESSAGE( string_equal( reinterpret_cast<const char*>( name ), "winding" ), "FEEDBACK PARSE ERROR" );
152                 ASSERT_MESSAGE( EPointState == WINDING_MESSAGE, "FEEDBACK PARSE ERROR" );
153                 EPointState = WINDING_WINDING;
154                 globalOutputStream() << message.c_str() << '\n';
155         }
156 }
157
158 void CWindingMsg::saxEndElement( message_info_t *ctx, const xmlChar *name ){
159         if ( string_equal( reinterpret_cast<const char*>( name ), "windingmsg" ) ) {
160         }
161         else if ( string_equal( reinterpret_cast<const char*>( name ), "winding" ) ) {
162                 const char* c = winding.c_str();
163                 sscanf( c, "%i ", &numpoints );
164
165                 int i = 0;
166                 for (; i < numpoints; i++ )
167                 {
168                         c = strchr( c + 1, '(' );
169                         if ( c ) { // even if we are given the number of points when the cycle begins .. don't trust it too much
170                                 sscanf( c, "(%g %g %g)", &wt[i][0], &wt[i][1], &wt[i][2] );
171                         }
172                         else{
173                                 break;
174                         }
175                 }
176                 numpoints = i;
177         }
178 }
179
180 void CWindingMsg::saxCharacters( message_info_t *ctx, const xmlChar *ch, int len ){
181         if ( EPointState == WINDING_MESSAGE ) {
182                 message.write( reinterpret_cast<const char*>( ch ), len );
183         }
184         else
185         {
186                 ASSERT_MESSAGE( EPointState == WINDING_WINDING, "FEEDBACK PARSE ERROR" );
187                 winding.write( reinterpret_cast<const char*>( ch ), len );
188         }
189 }
190
191 IGL2DWindow* CWindingMsg::Highlight(){
192         return this;
193 }
194
195 void CWindingMsg::DropHighlight(){
196 }
197
198 void CWindingMsg::Draw2D( VIEWTYPE vt ){
199         int i;
200
201         int nDim1 = ( vt == YZ ) ? 1 : 0;
202         int nDim2 = ( vt == XY ) ? 1 : 2;
203         glColor3f( 1.0f,0.f,0.0f );
204
205         glPointSize( 4 );
206         glBegin( GL_POINTS );
207         for ( i = 0; i < numpoints; i++ )
208                 glVertex2f( wt[i][nDim1], wt[i][nDim2] );
209         glEnd();
210         glPointSize( 1 );
211
212         glEnable( GL_BLEND );
213         glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
214         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
215         glColor4f( 0.133f,0.4f,1.0f,0.5f );
216         glBegin( GL_POLYGON );
217         for ( i = 0; i < numpoints; i++ )
218                 glVertex2f( wt[i][nDim1], wt[i][nDim2] );
219         glEnd();
220         glDisable( GL_BLEND );
221 }
222
223 // triggered when the user selects an entry in the feedback box
224 static void feedback_selection_changed( GtkTreeSelection* selection, gpointer data ){
225         g_DbgDlg.DropHighlight();
226
227         GtkTreeModel* model;
228         GtkTreeIter selected;
229         if ( gtk_tree_selection_get_selected( selection, &model, &selected ) ) {
230                 GtkTreePath* path = gtk_tree_model_get_path( model, &selected );
231                 g_DbgDlg.SetHighlight( gtk_tree_path_get_indices( path )[0] );
232                 gtk_tree_path_free( path );
233         }
234 }
235
236 void CDbgDlg::DropHighlight(){
237         if ( m_pHighlight != 0 ) {
238                 m_pHighlight->DropHighlight();
239                 m_pHighlight = 0;
240                 m_pDraw2D = 0;
241         }
242 }
243
244 void CDbgDlg::SetHighlight( gint row ){
245         ISAXHandler *h = GetElement( row );
246         if ( h != NULL ) {
247                 m_pDraw2D = h->Highlight();
248                 m_pHighlight = h;
249         }
250 }
251
252 ISAXHandler *CDbgDlg::GetElement( std::size_t row ){
253         return static_cast<ISAXHandler *>( g_ptr_array_index( m_pFeedbackElements, gint( row ) ) );
254 }
255
256 void CDbgDlg::Init(){
257         DropHighlight();
258
259         // free all the ISAXHandler*, clean it
260         while ( m_pFeedbackElements->len )
261         {
262                 static_cast<ISAXHandler *>( g_ptr_array_index( m_pFeedbackElements, 0 ) )->Release();
263                 g_ptr_array_remove_index( m_pFeedbackElements, 0 );
264         }
265
266         if ( m_clist != NULL ) {
267                 gtk_list_store_clear( m_clist );
268         }
269 }
270
271 void CDbgDlg::Push( ISAXHandler *pHandler ){
272         // push in the list
273         g_ptr_array_add( m_pFeedbackElements, (void *)pHandler );
274
275         if ( !GetWidget() ) {
276                 Create();
277         }
278
279         // put stuff in the list
280         gtk_list_store_clear( m_clist );
281         for ( std::size_t i = 0; i < static_cast<std::size_t>( m_pFeedbackElements->len ); ++i )
282         {
283                 GtkTreeIter iter;
284                 gtk_list_store_append( m_clist, &iter );
285                 gtk_list_store_set( m_clist, &iter, 0, GetElement( i )->getName(), -1 );
286         }
287
288         ShowDlg();
289 }
290
291 ui::Window CDbgDlg::BuildDialog(){
292         ui::Window window = MainFrame_getWindow().create_floating_window("Q3Map debug window" );
293
294         ui::Widget scr = ui::ScrolledWindow();
295         gtk_widget_show( scr );
296         gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( scr ) );
297         gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scr ), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC );
298         gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW( scr ), GTK_SHADOW_IN );
299
300         {
301                 ui::ListStore store = ui::ListStore(gtk_list_store_new( 1, G_TYPE_STRING ));
302
303                 ui::Widget view = ui::TreeView(ui::TreeModel( GTK_TREE_MODEL( store ) ));
304                 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW( view ), FALSE );
305
306                 {
307                         auto renderer = ui::CellRendererText();
308                         GtkTreeViewColumn* column = ui::TreeViewColumn( "", renderer, {{"text", 0}} );
309                         gtk_tree_view_append_column( GTK_TREE_VIEW( view ), column );
310                 }
311
312                 {
313                         GtkTreeSelection* selection = gtk_tree_view_get_selection( GTK_TREE_VIEW( view ) );
314                         gtk_tree_selection_set_mode( selection, GTK_SELECTION_BROWSE );
315                         g_signal_connect( G_OBJECT( selection ), "changed", G_CALLBACK( feedback_selection_changed ), NULL );
316                 }
317
318                 gtk_widget_show( view );
319
320                 gtk_container_add( GTK_CONTAINER( scr ), view );
321
322                 g_object_unref( G_OBJECT( store ) );
323
324                 m_clist = store;
325         }
326
327         return window;
328 }