2007-11-04 03:51:54 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
Copyright (C) 1999-2007 id Software, Inc. and contributors.
|
|
|
|
For a list of contributors, see the accompanying CONTRIBUTORS file.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This file is part of GtkRadiant.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GtkRadiant; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Base dialog class, provides a way to run modal dialogs and
|
|
|
|
// set/get the widget values in member variables.
|
|
|
|
//
|
|
|
|
// Leonardo Zide (leo@lokigames.com)
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2017-03-23 08:37:50 +00:00
|
|
|
GObject *object;
|
2012-03-17 20:01:54 +00:00
|
|
|
void *buffer;
|
|
|
|
DLG_DATA_TYPE type;
|
2007-11-04 03:51:54 +00:00
|
|
|
} DLG_DATA;
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
// Dialog class
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Dialog::Dialog (){
|
|
|
|
m_pDataList = (GSList*)NULL;
|
|
|
|
m_nReturn = IDCANCEL;
|
|
|
|
m_bNeedBuild = true;
|
2013-06-16 02:03:14 +00:00
|
|
|
m_nLoop = false;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Dialog::~Dialog (){
|
|
|
|
while ( m_pDataList )
|
|
|
|
{
|
|
|
|
free( m_pDataList->data );
|
|
|
|
m_pDataList = g_slist_remove( m_pDataList, m_pDataList->data );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( m_pWidget != NULL ) {
|
|
|
|
gtk_widget_destroy( m_pWidget );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// i suspect that this is redundant - gtk manages to remember the data stored in its widgets across a hide/show
|
2012-03-17 20:01:54 +00:00
|
|
|
void Dialog::ShowDlg(){
|
|
|
|
Create();
|
|
|
|
UpdateData( FALSE );
|
|
|
|
gtk_widget_show( m_pWidget );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void Dialog::HideDlg(){
|
|
|
|
UpdateData( TRUE );
|
|
|
|
gtk_widget_hide( m_pWidget );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static gint delete_event_callback( GtkWidget *widget, GdkEvent* event, gpointer data ){
|
|
|
|
reinterpret_cast<Dialog*>( data )->HideDlg();
|
|
|
|
reinterpret_cast<Dialog*>( data )->EndModal( IDCANCEL );
|
|
|
|
return TRUE;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void Dialog::Create(){
|
|
|
|
if ( m_bNeedBuild ) {
|
|
|
|
m_pWidget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
|
2017-03-15 10:58:36 +00:00
|
|
|
g_signal_connect( G_OBJECT( m_pWidget ), "delete-event",
|
|
|
|
G_CALLBACK( delete_event_callback ), this );
|
|
|
|
g_signal_connect( G_OBJECT( m_pWidget ), "destroy",
|
|
|
|
G_CALLBACK( gtk_widget_destroy ), NULL );
|
2012-03-17 20:01:54 +00:00
|
|
|
g_object_set_data( G_OBJECT( m_pWidget ), "loop", &m_nLoop );
|
|
|
|
g_object_set_data( G_OBJECT( m_pWidget ), "ret", &m_nReturn );
|
|
|
|
|
|
|
|
BuildDialog();
|
|
|
|
m_bNeedBuild = false;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void Dialog::Destroy(){
|
|
|
|
if ( m_pWidget != NULL ) {
|
|
|
|
gtk_widget_destroy( m_pWidget );
|
|
|
|
m_pWidget = NULL;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 08:37:50 +00:00
|
|
|
void Dialog::AddDialogData( GObject *object, void *buf, DLG_DATA_TYPE type ){
|
2012-03-17 20:01:54 +00:00
|
|
|
DLG_DATA *data;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
data = (DLG_DATA*)qmalloc( sizeof( DLG_DATA ) );
|
|
|
|
data->object = object;
|
|
|
|
data->buffer = buf;
|
|
|
|
data->type = type;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
m_pDataList = g_slist_append( m_pDataList, data );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2007-12-25 00:10:45 +00:00
|
|
|
void Dialog::AddModalButton( GtkWidget *widget, int ret ) {
|
2017-03-15 10:58:36 +00:00
|
|
|
g_signal_connect( G_OBJECT( widget ), "clicked",
|
|
|
|
G_CALLBACK( dialog_button_callback ), GINT_TO_POINTER( ret ) );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void Dialog::UpdateData( bool retrieve ){
|
|
|
|
DLG_DATA *data;
|
|
|
|
GSList *lst;
|
|
|
|
|
|
|
|
if ( retrieve ) {
|
|
|
|
for ( lst = m_pDataList; lst != NULL; lst = g_slist_next( lst ) )
|
|
|
|
{
|
|
|
|
data = (DLG_DATA*)lst->data;
|
|
|
|
|
|
|
|
switch ( data->type )
|
|
|
|
{
|
|
|
|
case DLG_CHECK_BOOL:
|
|
|
|
*(bool*)data->buffer = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( data->object ) );
|
|
|
|
break;
|
|
|
|
case DLG_RADIO_INT:
|
|
|
|
{
|
2017-03-10 21:44:05 +00:00
|
|
|
GSList *radio = gtk_radio_button_get_group( GTK_RADIO_BUTTON( data->object ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
*(int*)data->buffer = g_slist_length( radio ) - 1;
|
|
|
|
for (; radio; radio = g_slist_next( radio ) )
|
|
|
|
if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( radio->data ) ) ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
( *(int*)data->buffer )--;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case DLG_ENTRY_TEXT:
|
|
|
|
{
|
|
|
|
const char *txt;
|
|
|
|
Str* str;
|
|
|
|
str = (Str*)data->buffer;
|
|
|
|
txt = gtk_entry_get_text( GTK_ENTRY( data->object ) );
|
|
|
|
*str = txt;
|
|
|
|
} break;
|
|
|
|
case DLG_SPIN_FLOAT:
|
2017-03-10 21:44:05 +00:00
|
|
|
*(float*)data->buffer = gtk_spin_button_get_value( GTK_SPIN_BUTTON( data->object ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
break;
|
|
|
|
case DLG_SPIN_INT:
|
|
|
|
*(int*)data->buffer = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON( data->object ) );
|
|
|
|
break;
|
|
|
|
case DLG_ADJ_INT:
|
2017-03-10 21:44:05 +00:00
|
|
|
*(int*)data->buffer = (int) gtk_adjustment_get_value( GTK_ADJUSTMENT( data->object ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
break;
|
|
|
|
case DLG_COMBO_BOX_INT: {
|
|
|
|
*(int*)data->buffer = gtk_combo_box_get_active( GTK_COMBO_BOX( data->object ) );
|
|
|
|
}
|
2008-04-12 23:01:39 +00:00
|
|
|
break;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2008-04-12 23:01:39 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for ( lst = m_pDataList; lst != NULL; lst = g_slist_next( lst ) )
|
|
|
|
{
|
|
|
|
data = (DLG_DATA*)lst->data;
|
|
|
|
|
|
|
|
switch ( data->type )
|
|
|
|
{
|
|
|
|
case DLG_CHECK_BOOL:
|
|
|
|
gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( data->object ), *(bool*)data->buffer );
|
|
|
|
break;
|
|
|
|
case DLG_RADIO_INT:
|
|
|
|
{
|
2017-03-10 21:44:05 +00:00
|
|
|
GSList *radio = gtk_radio_button_get_group( GTK_RADIO_BUTTON( data->object ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
gpointer btn = g_slist_nth_data( radio, g_slist_length( radio ) - ( *(int*)data->buffer ) - 1 );
|
|
|
|
gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( btn ), TRUE );
|
|
|
|
} break;
|
|
|
|
case DLG_ENTRY_TEXT:
|
|
|
|
{
|
|
|
|
Str* str;
|
|
|
|
str = (Str*)data->buffer;
|
|
|
|
const char *txt = str->GetBuffer();
|
|
|
|
gtk_entry_set_text( GTK_ENTRY( data->object ), txt );
|
|
|
|
} break;
|
|
|
|
case DLG_SPIN_FLOAT:
|
|
|
|
gtk_spin_button_set_value( GTK_SPIN_BUTTON( data->object ), ( *(float*)data->buffer ) );
|
|
|
|
break;
|
|
|
|
case DLG_SPIN_INT:
|
|
|
|
gtk_spin_button_set_value( GTK_SPIN_BUTTON( data->object ), ( *(int*)data->buffer ) );
|
|
|
|
break;
|
|
|
|
case DLG_ADJ_INT:
|
|
|
|
gtk_adjustment_set_value( GTK_ADJUSTMENT( data->object ), ( *(int*)data->buffer ) );
|
|
|
|
break;
|
|
|
|
case DLG_COMBO_BOX_INT: {
|
|
|
|
gtk_combo_box_set_active( GTK_COMBO_BOX( data->object ), *(int*)data->buffer );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2007-12-25 00:10:45 +00:00
|
|
|
void Dialog::EndModal( int code ) {
|
2013-06-16 02:03:14 +00:00
|
|
|
m_nLoop = false;
|
2012-03-17 20:01:54 +00:00
|
|
|
m_nReturn = code;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int Dialog::DoModal(){
|
|
|
|
Create();
|
|
|
|
UpdateData( FALSE );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
PreModal();
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
gtk_grab_add( m_pWidget );
|
|
|
|
gtk_widget_show( m_pWidget );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2013-06-16 02:03:14 +00:00
|
|
|
m_nLoop = true;
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( m_nLoop ) {
|
|
|
|
gtk_main_iteration();
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( m_pWidget != NULL ) {
|
|
|
|
UpdateData( TRUE );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
gtk_grab_remove( m_pWidget );
|
|
|
|
gtk_widget_hide( m_pWidget );
|
|
|
|
}
|
|
|
|
PostModal( m_nReturn );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return m_nReturn;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|