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
23 // Base dialog class, provides a way to run modal dialogs and
24 // set/get the widget values in member variables.
26 // Leonardo Zide (leo@lokigames.com)
41 // =============================================================================
45 m_pDataList = (GSList*)NULL;
54 free( m_pDataList->data );
55 m_pDataList = g_slist_remove( m_pDataList, m_pDataList->data );
58 if ( m_pWidget != NULL ) {
59 gtk_widget_destroy( m_pWidget );
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(){
67 gtk_widget_show( m_pWidget );
70 void Dialog::HideDlg(){
72 gtk_widget_hide( m_pWidget );
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 );
81 void Dialog::Create(){
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 );
96 void Dialog::Destroy(){
97 if ( m_pWidget != NULL ) {
98 gtk_widget_destroy( m_pWidget );
103 void Dialog::AddDialogData( GtkObject *object, void *buf, DLG_DATA_TYPE type ){
106 data = (DLG_DATA*)qmalloc( sizeof( DLG_DATA ) );
107 data->object = object;
111 m_pDataList = g_slist_append( m_pDataList, data );
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 ) );
119 void Dialog::UpdateData( bool retrieve ){
125 for ( lst = m_pDataList; lst != NULL; lst = g_slist_next( lst ) )
127 data = (DLG_DATA*)lst->data;
129 switch ( data->type )
132 *(bool*)data->buffer = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( data->object ) );
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 ) ) ) {
143 ( *(int*)data->buffer )--;
150 str = (Str*)data->buffer;
151 txt = gtk_entry_get_text( GTK_ENTRY( data->object ) );
154 case DLG_ENTRY_FLOAT:
155 *(float*)data->buffer = atof( gtk_entry_get_text( GTK_ENTRY( data->object ) ) );
158 *(int*)data->buffer = atoi( gtk_entry_get_text( GTK_ENTRY( data->object ) ) );
161 *(float*)data->buffer = gtk_spin_button_get_value_as_float( GTK_SPIN_BUTTON( data->object ) );
164 *(int*)data->buffer = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON( data->object ) );
167 *(int*)data->buffer = (int) GTK_ADJUSTMENT( data->object )->value;
171 GList *lst = GTK_LIST( GTK_COMBO( data->object )->list )->children;
176 *(int*)data->buffer = -1;
177 entry = gtk_entry_get_text( GTK_ENTRY( GTK_COMBO( data->object )->entry ) );
179 for ( i = 0; lst != NULL; lst = g_list_next( lst ) )
181 gtk_label_get( GTK_LABEL( GTK_BIN( lst->data )->child ), &label );
183 if ( strcmp( label, entry ) == 0 ) {
184 *(int*)data->buffer = i;
191 case DLG_COMBO_BOX_INT: {
192 *(int*)data->buffer = gtk_combo_box_get_active( GTK_COMBO_BOX( data->object ) );
201 for ( lst = m_pDataList; lst != NULL; lst = g_slist_next( lst ) )
203 data = (DLG_DATA*)lst->data;
205 switch ( data->type )
208 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( data->object ), *(bool*)data->buffer );
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 );
219 str = (Str*)data->buffer;
220 const char *txt = str->GetBuffer();
221 gtk_entry_set_text( GTK_ENTRY( data->object ), txt );
223 case DLG_ENTRY_FLOAT:
224 sprintf( buf, "%g", ( *(float*)data->buffer ) );
225 gtk_entry_set_text( GTK_ENTRY( data->object ), buf );
228 sprintf( buf, "%d", ( *(int*)data->buffer ) );
229 gtk_entry_set_text( GTK_ENTRY( data->object ), buf );
232 gtk_spin_button_set_value( GTK_SPIN_BUTTON( data->object ), ( *(float*)data->buffer ) );
235 gtk_spin_button_set_value( GTK_SPIN_BUTTON( data->object ), ( *(int*)data->buffer ) );
238 gtk_adjustment_set_value( GTK_ADJUSTMENT( data->object ), ( *(int*)data->buffer ) );
240 case DLG_COMBO_INT: {
241 GList *lst = GTK_LIST( GTK_COMBO( data->object )->list )->children;
244 if ( *(int*)data->buffer != -1 ) {
245 lst = g_list_nth( lst, *(int*)data->buffer );
247 gtk_label_get( GTK_LABEL( GTK_BIN( lst->data )->child ), &entry );
251 gtk_entry_set_text( GTK_ENTRY( GTK_COMBO( data->object )->entry ), entry );
254 gtk_entry_set_text( GTK_ENTRY( GTK_COMBO( data->object )->entry ), "" );
258 case DLG_COMBO_BOX_INT: {
259 gtk_combo_box_set_active( GTK_COMBO_BOX( data->object ), *(int*)data->buffer );
267 void Dialog::EndModal( int code ) {
272 int Dialog::DoModal(){
278 gtk_grab_add( m_pWidget );
279 gtk_widget_show( m_pWidget );
283 gtk_main_iteration();
286 if ( m_pWidget != NULL ) {
289 gtk_grab_remove( m_pWidget );
290 gtk_widget_hide( m_pWidget );
292 PostModal( m_nReturn );