]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/findtexturedialog.cpp
13fad1c2f4bfb3d95244405706d7447a4580898b
[xonotic/netradiant.git] / radiant / findtexturedialog.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 // Find/Replace textures dialogs
24 //
25 // Leonardo Zide (leo@lokigames.com)
26 //
27
28 #include "findtexturedialog.h"
29
30 #include "debugging/debugging.h"
31
32 #include "ishaders.h"
33
34 #include <gtk/gtk.h>
35
36 #include "gtkutil/window.h"
37 #include "stream/stringstream.h"
38
39 #include "commands.h"
40 #include "dialog.h"
41 #include "select.h"
42 #include "textureentry.h"
43
44
45
46 class FindTextureDialog : public Dialog
47 {
48 public:
49 static void setReplaceStr( const char* name );
50 static void setFindStr( const char* name );
51 static bool isOpen();
52 static void show();
53 typedef FreeCaller<&FindTextureDialog::show> ShowCaller;
54 static void updateTextures( const char* name );
55
56 FindTextureDialog();
57 virtual ~FindTextureDialog();
58 ui::Window BuildDialog();
59
60 void constructWindow( ui::Window parent ){
61         m_parent = parent;
62         Create();
63 }
64 void destroyWindow(){
65         Destroy();
66 }
67
68
69 bool m_bSelectedOnly;
70 CopiedString m_strFind;
71 CopiedString m_strReplace;
72 };
73
74 FindTextureDialog g_FindTextureDialog;
75 static bool g_bFindActive = true;
76
77 namespace
78 {
79 void FindTextureDialog_apply(){
80         StringOutputStream find( 256 );
81         StringOutputStream replace( 256 );
82
83         find << "textures/" << g_FindTextureDialog.m_strFind.c_str();
84         replace << "textures/" << g_FindTextureDialog.m_strReplace.c_str();
85         FindReplaceTextures( find.c_str(), replace.c_str(), g_FindTextureDialog.m_bSelectedOnly );
86 }
87
88 static void OnApply( ui::Widget widget, gpointer data ){
89         g_FindTextureDialog.exportData();
90         FindTextureDialog_apply();
91 }
92
93 static void OnFind( GtkWidget* widget, gpointer data ){
94         g_FindTextureDialog.exportData();
95         FindTextureDialog_apply();
96 }
97
98 static void OnOK( GtkWidget* widget, gpointer data ){
99         g_FindTextureDialog.exportData();
100         FindTextureDialog_apply();
101         g_FindTextureDialog.HideDlg();
102 }
103
104 static void OnClose( ui::Widget widget, gpointer data ){
105         g_FindTextureDialog.HideDlg();
106 }
107
108
109 static gint find_focus_in( ui::Widget widget, GdkEventFocus *event, gpointer data ){
110         g_bFindActive = true;
111         return FALSE;
112 }
113
114 static gint replace_focus_in( ui::Widget widget, GdkEventFocus *event, gpointer data ){
115         g_bFindActive = false;
116         return FALSE;
117 }
118 }
119
120 // =============================================================================
121 // FindTextureDialog class
122
123 FindTextureDialog::FindTextureDialog(){
124         m_bSelectedOnly = FALSE;
125 }
126
127 FindTextureDialog::~FindTextureDialog(){
128 }
129
130 ui::Window FindTextureDialog::BuildDialog(){
131         ui::Widget vbox, hbox, table, label;
132         ui::Widget button, check, entry;
133
134         ui::Window dlg = ui::Window(create_floating_window( "Find / Replace Texture(s)", m_parent ));
135
136         hbox = ui::HBox( FALSE, 5 );
137         gtk_widget_show( hbox );
138         gtk_container_add( GTK_CONTAINER( dlg ), GTK_WIDGET( hbox ) );
139         gtk_container_set_border_width( GTK_CONTAINER( hbox ), 5 );
140
141         vbox = ui::VBox( FALSE, 5 );
142         gtk_widget_show( vbox );
143         gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( vbox ), TRUE, TRUE, 0 );
144
145         table = ui::Table( 2, 2, FALSE );
146         gtk_widget_show( table );
147         gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
148         gtk_table_set_row_spacings( GTK_TABLE( table ), 5 );
149         gtk_table_set_col_spacings( GTK_TABLE( table ), 5 );
150
151         label = ui::Label( "Find:" );
152         gtk_widget_show( label );
153         gtk_table_attach( GTK_TABLE( table ), label, 0, 1, 0, 1,
154                                           (GtkAttachOptions) ( GTK_FILL ),
155                                           (GtkAttachOptions) ( 0 ), 0, 0 );
156         gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
157
158         label = ui::Label( "Replace:" );
159         gtk_widget_show( label );
160         gtk_table_attach( GTK_TABLE( table ), label, 0, 1, 1, 2,
161                                           (GtkAttachOptions) ( GTK_FILL ),
162                                           (GtkAttachOptions) ( 0 ), 0, 0 );
163         gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
164
165         entry = ui::Entry();
166         gtk_widget_show( entry );
167         gtk_table_attach( GTK_TABLE( table ), entry, 1, 2, 0, 1,
168                                           (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
169                                           (GtkAttachOptions) ( 0 ), 0, 0 );
170         g_signal_connect( G_OBJECT( entry ), "focus_in_event",
171                                           G_CALLBACK( find_focus_in ), 0 );
172         AddDialogData( *GTK_ENTRY( entry ), m_strFind );
173         GlobalTextureEntryCompletion::instance().connect( GTK_ENTRY( entry ) );
174
175         entry = ui::Entry();
176         gtk_widget_show( entry );
177         gtk_table_attach( GTK_TABLE( table ), entry, 1, 2, 1, 2,
178                                           (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
179                                           (GtkAttachOptions) ( 0 ), 0, 0 );
180         g_signal_connect( G_OBJECT( entry ), "focus_in_event",
181                                           G_CALLBACK( replace_focus_in ), 0 );
182         AddDialogData( *GTK_ENTRY( entry ), m_strReplace );
183         GlobalTextureEntryCompletion::instance().connect( GTK_ENTRY( entry ) );
184
185         check = ui::CheckButton( "Within selected brushes only" );
186         gtk_widget_show( check );
187         gtk_box_pack_start( GTK_BOX( vbox ), check, TRUE, TRUE, 0 );
188         AddDialogData( *GTK_TOGGLE_BUTTON( check ), m_bSelectedOnly );
189
190         vbox = ui::VBox( FALSE, 5 );
191         gtk_widget_show( vbox );
192         gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( vbox ), FALSE, FALSE, 0 );
193
194         button = ui::Button( "Apply" );
195         gtk_widget_show( button );
196         gtk_box_pack_start( GTK_BOX( vbox ), button, FALSE, FALSE, 0 );
197         g_signal_connect( G_OBJECT( button ), "clicked",
198                                           G_CALLBACK( OnApply ), 0 );
199         gtk_widget_set_size_request( button, 60, -1 );
200
201         button = ui::Button( "Close" );
202         gtk_widget_show( button );
203         gtk_box_pack_start( GTK_BOX( vbox ), button, FALSE, FALSE, 0 );
204         g_signal_connect( G_OBJECT( button ), "clicked",
205                                           G_CALLBACK( OnClose ), 0 );
206         gtk_widget_set_size_request( button, 60, -1 );
207
208         return dlg;
209 }
210
211 void FindTextureDialog::updateTextures( const char* name ){
212         if ( isOpen() ) {
213                 if ( g_bFindActive ) {
214                         setFindStr( name + 9 );
215                 }
216                 else
217                 {
218                         setReplaceStr( name + 9 );
219                 }
220         }
221 }
222
223 bool FindTextureDialog::isOpen(){
224         return gtk_widget_get_visible( g_FindTextureDialog.GetWidget() ) == TRUE;
225 }
226
227 void FindTextureDialog::setFindStr( const char* name ){
228         g_FindTextureDialog.exportData();
229         g_FindTextureDialog.m_strFind = name;
230         g_FindTextureDialog.importData();
231 }
232
233 void FindTextureDialog::setReplaceStr( const char* name ){
234         g_FindTextureDialog.exportData();
235         g_FindTextureDialog.m_strReplace = name;
236         g_FindTextureDialog.importData();
237 }
238
239 void FindTextureDialog::show(){
240         g_FindTextureDialog.ShowDlg();
241 }
242
243
244 void FindTextureDialog_constructWindow( ui::Window main_window ){
245         g_FindTextureDialog.constructWindow( main_window );
246 }
247
248 void FindTextureDialog_destroyWindow(){
249         g_FindTextureDialog.destroyWindow();
250 }
251
252 bool FindTextureDialog_isOpen(){
253         return g_FindTextureDialog.isOpen();
254 }
255
256 void FindTextureDialog_selectTexture( const char* name ){
257         g_FindTextureDialog.updateTextures( name );
258 }
259
260 void FindTextureDialog_Construct(){
261         GlobalCommands_insert( "FindReplaceTextures", FindTextureDialog::ShowCaller() );
262 }
263
264 void FindTextureDialog_Destroy(){
265 }