]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/findtexturedialog.cpp
201f2c67b477f9bea28705038b6d277db0880504
[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;
131         ui::Entry entry{nullptr};
132
133         auto dlg = ui::Window(create_floating_window( "Find / Replace Texture(s)", m_parent ));
134
135         hbox = ui::HBox( FALSE, 5 );
136         hbox.show();
137         dlg.add(hbox);
138         gtk_container_set_border_width( GTK_CONTAINER( hbox ), 5 );
139
140         vbox = ui::VBox( FALSE, 5 );
141         vbox.show();
142         gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( vbox ), TRUE, TRUE, 0 );
143
144         table = ui::Table( 2, 2, FALSE );
145         table.show();
146         gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
147         gtk_table_set_row_spacings( GTK_TABLE( table ), 5 );
148         gtk_table_set_col_spacings( GTK_TABLE( table ), 5 );
149
150         label = ui::Label( "Find:" );
151         label.show();
152         gtk_table_attach( GTK_TABLE( table ), label, 0, 1, 0, 1,
153                                           (GtkAttachOptions) ( GTK_FILL ),
154                                           (GtkAttachOptions) ( 0 ), 0, 0 );
155         gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
156
157         label = ui::Label( "Replace:" );
158         label.show();
159         gtk_table_attach( GTK_TABLE( table ), label, 0, 1, 1, 2,
160                                           (GtkAttachOptions) ( GTK_FILL ),
161                                           (GtkAttachOptions) ( 0 ), 0, 0 );
162         gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
163
164         entry = ui::Entry();
165         entry.show();
166         gtk_table_attach( GTK_TABLE( table ), entry, 1, 2, 0, 1,
167                                           (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
168                                           (GtkAttachOptions) ( 0 ), 0, 0 );
169         g_signal_connect( G_OBJECT( entry ), "focus_in_event",
170                                           G_CALLBACK( find_focus_in ), 0 );
171         AddDialogData( *GTK_ENTRY( entry ), m_strFind );
172         GlobalTextureEntryCompletion::instance().connect( entry );
173
174         entry = ui::Entry();
175         entry.show();
176         gtk_table_attach( GTK_TABLE( table ), entry, 1, 2, 1, 2,
177                                           (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
178                                           (GtkAttachOptions) ( 0 ), 0, 0 );
179         g_signal_connect( G_OBJECT( entry ), "focus_in_event",
180                                           G_CALLBACK( replace_focus_in ), 0 );
181         AddDialogData( *GTK_ENTRY( entry ), m_strReplace );
182         GlobalTextureEntryCompletion::instance().connect( entry );
183
184         check = ui::CheckButton( "Within selected brushes only" );
185         check.show();
186         gtk_box_pack_start( GTK_BOX( vbox ), check, TRUE, TRUE, 0 );
187         AddDialogData( *GTK_TOGGLE_BUTTON( check ), m_bSelectedOnly );
188
189         vbox = ui::VBox( FALSE, 5 );
190         vbox.show();
191         gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( vbox ), FALSE, FALSE, 0 );
192
193         button = ui::Button( "Apply" );
194         button.show();
195         gtk_box_pack_start( GTK_BOX( vbox ), button, FALSE, FALSE, 0 );
196         g_signal_connect( G_OBJECT( button ), "clicked",
197                                           G_CALLBACK( OnApply ), 0 );
198         gtk_widget_set_size_request( button, 60, -1 );
199
200         button = ui::Button( "Close" );
201         button.show();
202         gtk_box_pack_start( GTK_BOX( vbox ), button, FALSE, FALSE, 0 );
203         g_signal_connect( G_OBJECT( button ), "clicked",
204                                           G_CALLBACK( OnClose ), 0 );
205         gtk_widget_set_size_request( button, 60, -1 );
206
207         return dlg;
208 }
209
210 void FindTextureDialog::updateTextures( const char* name ){
211         if ( isOpen() ) {
212                 if ( g_bFindActive ) {
213                         setFindStr( name + 9 );
214                 }
215                 else
216                 {
217                         setReplaceStr( name + 9 );
218                 }
219         }
220 }
221
222 bool FindTextureDialog::isOpen(){
223         return gtk_widget_get_visible( g_FindTextureDialog.GetWidget() ) == TRUE;
224 }
225
226 void FindTextureDialog::setFindStr( const char* name ){
227         g_FindTextureDialog.exportData();
228         g_FindTextureDialog.m_strFind = name;
229         g_FindTextureDialog.importData();
230 }
231
232 void FindTextureDialog::setReplaceStr( const char* name ){
233         g_FindTextureDialog.exportData();
234         g_FindTextureDialog.m_strReplace = name;
235         g_FindTextureDialog.importData();
236 }
237
238 void FindTextureDialog::show(){
239         g_FindTextureDialog.ShowDlg();
240 }
241
242
243 void FindTextureDialog_constructWindow( ui::Window main_window ){
244         g_FindTextureDialog.constructWindow( main_window );
245 }
246
247 void FindTextureDialog_destroyWindow(){
248         g_FindTextureDialog.destroyWindow();
249 }
250
251 bool FindTextureDialog_isOpen(){
252         return g_FindTextureDialog.isOpen();
253 }
254
255 void FindTextureDialog_selectTexture( const char* name ){
256         g_FindTextureDialog.updateTextures( name );
257 }
258
259 void FindTextureDialog_Construct(){
260         GlobalCommands_insert( "FindReplaceTextures", FindTextureDialog::ShowCaller() );
261 }
262
263 void FindTextureDialog_Destroy(){
264 }