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