]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/dialog.cpp
uncrustify! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / radiant / dialog.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 // Base dialog class, provides a way to run modal dialogs and
24 // set/get the widget values in member variables.
25 //
26 // Leonardo Zide (leo@lokigames.com)
27 //
28
29 #include "stdafx.h"
30 #include <gtk/gtk.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33
34 typedef struct
35 {
36         GtkObject     *object;
37         void          *buffer;
38         DLG_DATA_TYPE type;
39 } DLG_DATA;
40
41 // =============================================================================
42 // Dialog class
43
44 Dialog::Dialog (){
45         m_pDataList = (GSList*)NULL;
46         m_nReturn = IDCANCEL;
47         m_bNeedBuild = true;
48         m_nLoop = 0;
49 }
50
51 Dialog::~Dialog (){
52         while ( m_pDataList )
53         {
54                 free( m_pDataList->data );
55                 m_pDataList = g_slist_remove( m_pDataList, m_pDataList->data );
56         }
57
58         if ( m_pWidget != NULL ) {
59                 gtk_widget_destroy( m_pWidget );
60         }
61 }
62
63 // i suspect that this is redundant - gtk manages to remember the data stored in its widgets across a hide/show
64 void Dialog::ShowDlg(){
65         Create();
66         UpdateData( FALSE );
67         gtk_widget_show( m_pWidget );
68 }
69
70 void Dialog::HideDlg(){
71         UpdateData( TRUE );
72         gtk_widget_hide( m_pWidget );
73 }
74
75 static gint delete_event_callback( GtkWidget *widget, GdkEvent* event, gpointer data ){
76         reinterpret_cast<Dialog*>( data )->HideDlg();
77         reinterpret_cast<Dialog*>( data )->EndModal( IDCANCEL );
78         return TRUE;
79 }
80
81 void Dialog::Create(){
82         if ( m_bNeedBuild ) {
83                 m_pWidget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
84                 gtk_signal_connect( GTK_OBJECT( m_pWidget ), "delete_event",
85                                                         GTK_SIGNAL_FUNC( delete_event_callback ), this );
86                 gtk_signal_connect( GTK_OBJECT( m_pWidget ), "destroy",
87                                                         GTK_SIGNAL_FUNC( gtk_widget_destroy ), NULL );
88                 g_object_set_data( G_OBJECT( m_pWidget ), "loop", &m_nLoop );
89                 g_object_set_data( G_OBJECT( m_pWidget ), "ret", &m_nReturn );
90
91                 BuildDialog();
92                 m_bNeedBuild = false;
93         }
94 }
95
96 void Dialog::Destroy(){
97         if ( m_pWidget != NULL ) {
98                 gtk_widget_destroy( m_pWidget );
99                 m_pWidget = NULL;
100         }
101 }
102
103 void Dialog::AddDialogData( GtkObject *object, void *buf, DLG_DATA_TYPE type ){
104         DLG_DATA *data;
105
106         data = (DLG_DATA*)qmalloc( sizeof( DLG_DATA ) );
107         data->object = object;
108         data->buffer = buf;
109         data->type = type;
110
111         m_pDataList = g_slist_append( m_pDataList, data );
112 }
113
114 void Dialog::AddModalButton( GtkWidget *widget, int ret ) {
115         gtk_signal_connect( GTK_OBJECT( widget ), "clicked",
116                                                 GTK_SIGNAL_FUNC( dialog_button_callback ), GINT_TO_POINTER( ret ) );
117 }
118
119 void Dialog::UpdateData( bool retrieve ){
120         DLG_DATA *data;
121         GSList *lst;
122         char buf[32];
123
124         if ( retrieve ) {
125                 for ( lst = m_pDataList; lst != NULL; lst = g_slist_next( lst ) )
126                 {
127                         data = (DLG_DATA*)lst->data;
128
129                         switch ( data->type )
130                         {
131                         case DLG_CHECK_BOOL:
132                                 *(bool*)data->buffer = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( data->object ) );
133                                 break;
134                         case DLG_RADIO_INT:
135                         {
136                                 GSList *radio = gtk_radio_button_group( GTK_RADIO_BUTTON( data->object ) );
137                                 *(int*)data->buffer = g_slist_length( radio ) - 1;
138                                 for (; radio; radio = g_slist_next( radio ) )
139                                         if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( radio->data ) ) ) {
140                                                 break;
141                                         }
142                                         else{
143                                                 ( *(int*)data->buffer )--;
144                                         }
145                         } break;
146                         case DLG_ENTRY_TEXT:
147                         {
148                                 const char *txt;
149                                 Str* str;
150                                 str = (Str*)data->buffer;
151                                 txt = gtk_entry_get_text( GTK_ENTRY( data->object ) );
152                                 *str = txt;
153                         } break;
154                         case DLG_ENTRY_FLOAT:
155                                 *(float*)data->buffer = atof( gtk_entry_get_text( GTK_ENTRY( data->object ) ) );
156                                 break;
157                         case DLG_ENTRY_INT:
158                                 *(int*)data->buffer = atoi( gtk_entry_get_text( GTK_ENTRY( data->object ) ) );
159                                 break;
160                         case DLG_SPIN_FLOAT:
161                                 *(float*)data->buffer = gtk_spin_button_get_value_as_float( GTK_SPIN_BUTTON( data->object ) );
162                                 break;
163                         case DLG_SPIN_INT:
164                                 *(int*)data->buffer = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON( data->object ) );
165                                 break;
166                         case DLG_ADJ_INT:
167                                 *(int*)data->buffer = (int) GTK_ADJUSTMENT( data->object )->value;
168                                 break;
169                         case DLG_COMBO_INT:
170                         {
171                                 GList *lst = GTK_LIST( GTK_COMBO( data->object )->list )->children;
172                                 char *label;
173                                 const char *entry;
174                                 int i;
175
176                                 *(int*)data->buffer = -1;
177                                 entry = gtk_entry_get_text( GTK_ENTRY( GTK_COMBO( data->object )->entry ) );
178
179                                 for ( i = 0; lst != NULL; lst = g_list_next( lst ) )
180                                 {
181                                         gtk_label_get( GTK_LABEL( GTK_BIN( lst->data )->child ), &label );
182
183                                         if ( strcmp( label, entry ) == 0 ) {
184                                                 *(int*)data->buffer = i;
185                                                 break;
186                                         }
187                                         i++;
188                                 }
189                         }
190                         break;
191                         case DLG_COMBO_BOX_INT: {
192                                 *(int*)data->buffer = gtk_combo_box_get_active( GTK_COMBO_BOX( data->object ) );
193                         }
194                         break;
195
196                         }
197                 }
198         }
199         else
200         {
201                 for ( lst = m_pDataList; lst != NULL; lst = g_slist_next( lst ) )
202                 {
203                         data = (DLG_DATA*)lst->data;
204
205                         switch ( data->type )
206                         {
207                         case DLG_CHECK_BOOL:
208                                 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( data->object ), *(bool*)data->buffer );
209                                 break;
210                         case DLG_RADIO_INT:
211                         {
212                                 GSList *radio = gtk_radio_button_group( GTK_RADIO_BUTTON( data->object ) );
213                                 gpointer btn =  g_slist_nth_data( radio, g_slist_length( radio ) - ( *(int*)data->buffer ) - 1 );
214                                 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( btn ), TRUE );
215                         } break;
216                         case DLG_ENTRY_TEXT:
217                         {
218                                 Str* str;
219                                 str = (Str*)data->buffer;
220                                 const char *txt = str->GetBuffer();
221                                 gtk_entry_set_text( GTK_ENTRY( data->object ), txt );
222                         } break;
223                         case DLG_ENTRY_FLOAT:
224                                 sprintf( buf, "%g", ( *(float*)data->buffer ) );
225                                 gtk_entry_set_text( GTK_ENTRY( data->object ), buf );
226                                 break;
227                         case DLG_ENTRY_INT:
228                                 sprintf( buf, "%d", ( *(int*)data->buffer ) );
229                                 gtk_entry_set_text( GTK_ENTRY( data->object ), buf );
230                                 break;
231                         case DLG_SPIN_FLOAT:
232                                 gtk_spin_button_set_value( GTK_SPIN_BUTTON( data->object ), ( *(float*)data->buffer ) );
233                                 break;
234                         case DLG_SPIN_INT:
235                                 gtk_spin_button_set_value( GTK_SPIN_BUTTON( data->object ), ( *(int*)data->buffer ) );
236                                 break;
237                         case DLG_ADJ_INT:
238                                 gtk_adjustment_set_value( GTK_ADJUSTMENT( data->object ), ( *(int*)data->buffer ) );
239                                 break;
240                         case DLG_COMBO_INT: {
241                                 GList *lst = GTK_LIST( GTK_COMBO( data->object )->list )->children;
242                                 char *entry = NULL;
243
244                                 if ( *(int*)data->buffer != -1 ) {
245                                         lst = g_list_nth( lst, *(int*)data->buffer );
246                                         if ( lst != NULL ) {
247                                                 gtk_label_get( GTK_LABEL( GTK_BIN( lst->data )->child ), &entry );
248                                         }
249                                 }
250                                 if ( entry ) {
251                                         gtk_entry_set_text( GTK_ENTRY( GTK_COMBO( data->object )->entry ), entry );
252                                 }
253                                 else{
254                                         gtk_entry_set_text( GTK_ENTRY( GTK_COMBO( data->object )->entry ), "" );
255                                 }
256                         }
257                         break;
258                         case DLG_COMBO_BOX_INT: {
259                                 gtk_combo_box_set_active( GTK_COMBO_BOX( data->object ), *(int*)data->buffer );
260                         }
261                         break;
262                         }
263                 }
264         }
265 }
266
267 void Dialog::EndModal( int code ) {
268         m_nLoop = 0;
269         m_nReturn = code;
270 }
271
272 int Dialog::DoModal(){
273         Create();
274         UpdateData( FALSE );
275
276         PreModal();
277
278         gtk_grab_add( m_pWidget );
279         gtk_widget_show( m_pWidget );
280
281         m_nLoop = 1;
282         while ( m_nLoop ) {
283                 gtk_main_iteration();
284         }
285
286         if ( m_pWidget != NULL ) {
287                 UpdateData( TRUE );
288
289                 gtk_grab_remove( m_pWidget );
290                 gtk_widget_hide( m_pWidget );
291         }
292         PostModal( m_nReturn );
293
294         return m_nReturn;
295 }