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