2 Copyright (C) 1999-2006 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 // Find/Replace textures dialogs
25 // Leonardo Zide (leo@lokigames.com)
28 #include "findtexturedialog.h"
30 #include "debugging/debugging.h"
34 #include <gtk/gtkhbox.h>
35 #include <gtk/gtkentry.h>
36 #include <gtk/gtkvbox.h>
37 #include <gtk/gtkframe.h>
38 #include <gtk/gtklabel.h>
39 #include <gtk/gtktable.h>
40 #include <gtk/gtkbutton.h>
41 #include <gtk/gtktogglebutton.h>
42 #include <gtk/gtkcheckbutton.h>
43 #include <gtk/gtkmenuitem.h>
44 #include <gtk/gtkarrow.h>
46 #include "gtkutil/window.h"
47 #include "stream/stringstream.h"
52 #include "textureentry.h"
56 class FindTextureDialog : public Dialog
59 WindowPositionTracker m_position_tracker;
60 static void setReplaceStr( const char* name );
61 static void setFindStr( const char* name );
64 typedef FreeCaller<&FindTextureDialog::show> ShowCaller;
65 static void updateTextures( const char* name );
68 virtual ~FindTextureDialog();
69 GtkWindow* BuildDialog();
71 void constructWindow( GtkWindow* parent ){
81 CopiedString m_strFind;
82 CopiedString m_strReplace;
85 FindTextureDialog g_FindTextureDialog;
86 static bool g_bFindActive = true;
90 void FindTextureDialog_apply(){
91 StringOutputStream find( 256 );
92 StringOutputStream replace( 256 );
94 find << "textures/" << g_FindTextureDialog.m_strFind.c_str();
95 replace << "textures/" << g_FindTextureDialog.m_strReplace.c_str();
96 FindReplaceTextures( find.c_str(), replace.c_str(), g_FindTextureDialog.m_bSelectedOnly );
99 static void OnApply( GtkWidget* widget, gpointer data ){
100 g_FindTextureDialog.exportData();
101 FindTextureDialog_apply();
104 static void OnFind( GtkWidget* widget, gpointer data ){
105 g_FindTextureDialog.exportData();
106 FindTextureDialog_apply();
109 static void OnOK( GtkWidget* widget, gpointer data ){
110 g_FindTextureDialog.exportData();
111 FindTextureDialog_apply();
112 g_FindTextureDialog.HideDlg();
115 static void OnClose( GtkWidget* widget, gpointer data ){
116 g_FindTextureDialog.HideDlg();
120 static gint find_focus_in( GtkWidget* widget, GdkEventFocus *event, gpointer data ){
121 g_bFindActive = true;
125 static gint replace_focus_in( GtkWidget* widget, GdkEventFocus *event, gpointer data ){
126 g_bFindActive = false;
131 // =============================================================================
132 // FindTextureDialog class
134 FindTextureDialog::FindTextureDialog(){
135 m_bSelectedOnly = FALSE;
136 //m_position_tracker.setPosition( c_default_window_pos );
139 FindTextureDialog::~FindTextureDialog(){
142 GtkWindow* FindTextureDialog::BuildDialog(){
143 GtkWidget* vbox, *hbox, *table, *label;
144 GtkWidget* button, *check, *entry;
146 GtkWindow* dlg = create_floating_window( "Find / Replace Texture(s)", m_parent );
148 m_position_tracker.connect( dlg );
150 hbox = gtk_hbox_new( FALSE, 5 );
151 gtk_widget_show( hbox );
152 gtk_container_add( GTK_CONTAINER( dlg ), GTK_WIDGET( hbox ) );
153 gtk_container_set_border_width( GTK_CONTAINER( hbox ), 5 );
155 vbox = gtk_vbox_new( FALSE, 5 );
156 gtk_widget_show( vbox );
157 gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( vbox ), TRUE, TRUE, 0 );
159 table = gtk_table_new( 2, 2, FALSE );
160 gtk_widget_show( table );
161 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
162 gtk_table_set_row_spacings( GTK_TABLE( table ), 5 );
163 gtk_table_set_col_spacings( GTK_TABLE( table ), 5 );
165 label = gtk_label_new( "Find:" );
166 gtk_widget_show( label );
167 gtk_table_attach( GTK_TABLE( table ), label, 0, 1, 0, 1,
168 (GtkAttachOptions) ( GTK_FILL ),
169 (GtkAttachOptions) ( 0 ), 0, 0 );
170 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
172 label = gtk_label_new( "Replace:" );
173 gtk_widget_show( label );
174 gtk_table_attach( GTK_TABLE( table ), label, 0, 1, 1, 2,
175 (GtkAttachOptions) ( GTK_FILL ),
176 (GtkAttachOptions) ( 0 ), 0, 0 );
177 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
179 entry = gtk_entry_new();
180 gtk_widget_show( entry );
181 gtk_table_attach( GTK_TABLE( table ), entry, 1, 2, 0, 1,
182 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
183 (GtkAttachOptions) ( 0 ), 0, 0 );
184 g_signal_connect( G_OBJECT( entry ), "focus_in_event",
185 G_CALLBACK( find_focus_in ), 0 );
186 AddDialogData( *GTK_ENTRY( entry ), m_strFind );
187 GlobalTextureEntryCompletion::instance().connect( GTK_ENTRY( entry ) );
189 entry = gtk_entry_new();
190 gtk_widget_show( entry );
191 gtk_table_attach( GTK_TABLE( table ), entry, 1, 2, 1, 2,
192 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
193 (GtkAttachOptions) ( 0 ), 0, 0 );
194 g_signal_connect( G_OBJECT( entry ), "focus_in_event",
195 G_CALLBACK( replace_focus_in ), 0 );
196 AddDialogData( *GTK_ENTRY( entry ), m_strReplace );
197 GlobalTextureEntryCompletion::instance().connect( GTK_ENTRY( entry ) );
199 check = gtk_check_button_new_with_label( "Within selected brushes only" );
200 gtk_widget_show( check );
201 gtk_box_pack_start( GTK_BOX( vbox ), check, TRUE, TRUE, 0 );
202 AddDialogData( *GTK_TOGGLE_BUTTON( check ), m_bSelectedOnly );
204 vbox = gtk_vbox_new( FALSE, 5 );
205 gtk_widget_show( vbox );
206 gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( vbox ), FALSE, FALSE, 0 );
208 button = gtk_button_new_with_label( "Apply" );
209 gtk_widget_show( button );
210 gtk_box_pack_start( GTK_BOX( vbox ), button, FALSE, FALSE, 0 );
211 g_signal_connect( G_OBJECT( button ), "clicked",
212 G_CALLBACK( OnApply ), 0 );
213 gtk_widget_set_usize( button, 60, -2 );
215 button = gtk_button_new_with_label( "Close" );
216 gtk_widget_show( button );
217 gtk_box_pack_start( GTK_BOX( vbox ), button, FALSE, FALSE, 0 );
218 g_signal_connect( G_OBJECT( button ), "clicked",
219 G_CALLBACK( OnClose ), 0 );
220 gtk_widget_set_usize( button, 60, -2 );
225 void FindTextureDialog::updateTextures( const char* name ){
227 if ( g_bFindActive ) {
228 setFindStr( name + 9 );
232 setReplaceStr( name + 9 );
237 bool FindTextureDialog::isOpen(){
238 return GTK_WIDGET_VISIBLE( g_FindTextureDialog.GetWidget() ) == TRUE;
241 void FindTextureDialog::setFindStr( const char* name ){
242 g_FindTextureDialog.exportData();
243 g_FindTextureDialog.m_strFind = name;
244 g_FindTextureDialog.importData();
247 void FindTextureDialog::setReplaceStr( const char* name ){
248 g_FindTextureDialog.exportData();
249 g_FindTextureDialog.m_strReplace = name;
250 g_FindTextureDialog.importData();
253 void FindTextureDialog::show(){
254 // workaround for strange gtk behaviour - modifying the contents of a window while it is not visible causes the window position to change without sending a configure_event
255 g_FindTextureDialog.m_position_tracker.sync( g_FindTextureDialog.GetWidget() );
256 g_FindTextureDialog.ShowDlg();
257 gtk_window_present( g_FindTextureDialog.GetWidget() );
261 void FindTextureDialog_constructWindow( GtkWindow* main_window ){
262 g_FindTextureDialog.constructWindow( main_window );
265 void FindTextureDialog_destroyWindow(){
266 g_FindTextureDialog.destroyWindow();
269 bool FindTextureDialog_isOpen(){
270 return g_FindTextureDialog.isOpen();
273 void FindTextureDialog_selectTexture( const char* name ){
274 g_FindTextureDialog.updateTextures( name );
277 #include "preferencesystem.h"
279 void FindTextureDialog_Construct(){
280 GlobalCommands_insert( "FindReplaceTextures", FindTextureDialog::ShowCaller() );
281 GlobalPreferenceSystem().registerPreference( "FindReplacehWnd", WindowPositionTrackerImportStringCaller( g_FindTextureDialog.m_position_tracker ), WindowPositionTrackerExportStringCaller( g_FindTextureDialog.m_position_tracker ) );
284 void FindTextureDialog_Destroy(){