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