]> git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/findtexturedialog.cpp
b3425d57f40f85a72c33d47354bf9953356c4ea5
[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 WindowPositionTracker m_position_tracker;
50 static void setReplaceStr( const char* name );
51 static void setFindStr( const char* name );
52 static bool isOpen();
53 static void show();
54 typedef FreeCaller<void(), &FindTextureDialog::show> ShowCaller;
55 static void updateTextures( const char* name );
56
57 FindTextureDialog();
58 virtual ~FindTextureDialog();
59 ui::Window BuildDialog();
60
61 void constructWindow( ui::Window parent ){
62         m_parent = parent;
63         Create();
64 }
65 void destroyWindow(){
66         Destroy();
67 }
68
69
70 bool m_bSelectedOnly;
71 CopiedString m_strFind;
72 CopiedString m_strReplace;
73 };
74
75 FindTextureDialog g_FindTextureDialog;
76 static bool g_bFindActive = true;
77
78 namespace
79 {
80 void FindTextureDialog_apply(){
81         StringOutputStream find( 256 );
82         StringOutputStream replace( 256 );
83
84         find << "textures/" << g_FindTextureDialog.m_strFind.c_str();
85         replace << "textures/" << g_FindTextureDialog.m_strReplace.c_str();
86         FindReplaceTextures( find.c_str(), replace.c_str(), g_FindTextureDialog.m_bSelectedOnly );
87 }
88
89 static void OnApply( ui::Widget widget, gpointer data ){
90         g_FindTextureDialog.exportData();
91         FindTextureDialog_apply();
92 }
93
94 static void OnFind( ui::Widget widget, gpointer data ){
95         g_FindTextureDialog.exportData();
96         FindTextureDialog_apply();
97 }
98
99 static void OnOK( ui::Widget widget, gpointer data ){
100         g_FindTextureDialog.exportData();
101         FindTextureDialog_apply();
102         g_FindTextureDialog.HideDlg();
103 }
104
105 static void OnClose( ui::Widget widget, gpointer data ){
106         g_FindTextureDialog.HideDlg();
107 }
108
109
110 static gint find_focus_in( ui::Widget widget, GdkEventFocus *event, gpointer data ){
111         g_bFindActive = true;
112         return FALSE;
113 }
114
115 static gint replace_focus_in( ui::Widget widget, GdkEventFocus *event, gpointer data ){
116         g_bFindActive = false;
117         return FALSE;
118 }
119 }
120
121 // =============================================================================
122 // FindTextureDialog class
123
124 FindTextureDialog::FindTextureDialog(){
125         m_bSelectedOnly = FALSE;
126         //m_position_tracker.setPosition( c_default_window_pos );
127 }
128
129 FindTextureDialog::~FindTextureDialog(){
130 }
131
132 ui::Window FindTextureDialog::BuildDialog(){
133     ui::Widget label{ui::null};
134         ui::Widget button{ui::null};
135         ui::Entry entry{ui::null};
136
137         auto dlg = ui::Window(create_floating_window( "Find / Replace Texture(s)", m_parent ));
138
139         m_position_tracker.connect( dlg );
140
141         auto hbox = ui::HBox( FALSE, 5 );
142         hbox.show();
143         dlg.add(hbox);
144         gtk_container_set_border_width( GTK_CONTAINER( hbox ), 5 );
145
146         auto vbox = ui::VBox( FALSE, 5 );
147         vbox.show();
148         hbox.pack_start( vbox, TRUE, TRUE, 0 );
149
150     auto table = ui::Table(2, 2, FALSE);
151         table.show();
152         vbox.pack_start( table, TRUE, TRUE, 0 );
153     gtk_table_set_row_spacings(table, 5);
154     gtk_table_set_col_spacings(table, 5);
155
156         label = ui::Label( "Find:" );
157         label.show();
158     table.attach(label, {0, 1, 0, 1}, {GTK_FILL, 0});
159         gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
160
161         label = ui::Label( "Replace:" );
162         label.show();
163     table.attach(label, {0, 1, 1, 2}, {GTK_FILL, 0});
164         gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
165
166         entry = ui::Entry(ui::New);
167         entry.show();
168     table.attach(entry, {1, 2, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
169         entry.connect( "focus_in_event",
170                                           G_CALLBACK( find_focus_in ), 0 );
171         AddDialogData( entry, m_strFind );
172         GlobalTextureEntryCompletion::instance().connect( entry );
173
174         entry = ui::Entry(ui::New);
175         entry.show();
176     table.attach(entry, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
177         entry.connect( "focus_in_event",
178                                           G_CALLBACK( replace_focus_in ), 0 );
179         AddDialogData( entry, m_strReplace );
180         GlobalTextureEntryCompletion::instance().connect( entry );
181
182         auto check = ui::CheckButton( "Within selected brushes only" );
183         check.show();
184         vbox.pack_start( check, TRUE, TRUE, 0 );
185         AddDialogData( check, m_bSelectedOnly );
186
187         vbox = ui::VBox( FALSE, 5 );
188         vbox.show();
189         hbox.pack_start( vbox, FALSE, FALSE, 0 );
190
191         button = ui::Button( "Apply" );
192         button.show();
193         vbox.pack_start( button, FALSE, FALSE, 0 );
194         button.connect( "clicked",
195                                           G_CALLBACK( OnApply ), 0 );
196         button.dimensions(60, -1);
197
198         button = ui::Button( "Close" );
199         button.show();
200         vbox.pack_start( button, FALSE, FALSE, 0 );
201         button.connect( "clicked",
202                                           G_CALLBACK( OnClose ), 0 );
203         button.dimensions(60, -1);
204
205         return dlg;
206 }
207
208 void FindTextureDialog::updateTextures( const char* name ){
209         if ( isOpen() ) {
210                 if ( g_bFindActive ) {
211                         setFindStr( name + 9 );
212                 }
213                 else
214                 {
215                         setReplaceStr( name + 9 );
216                 }
217         }
218 }
219
220 bool FindTextureDialog::isOpen(){
221         return g_FindTextureDialog.GetWidget().visible();
222 }
223
224 void FindTextureDialog::setFindStr( const char* name ){
225         g_FindTextureDialog.exportData();
226         g_FindTextureDialog.m_strFind = name;
227         g_FindTextureDialog.importData();
228 }
229
230 void FindTextureDialog::setReplaceStr( const char* name ){
231         g_FindTextureDialog.exportData();
232         g_FindTextureDialog.m_strReplace = name;
233         g_FindTextureDialog.importData();
234 }
235
236 void FindTextureDialog::show(){
237         // 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
238         g_FindTextureDialog.m_position_tracker.sync( g_FindTextureDialog.GetWidget() );
239         g_FindTextureDialog.ShowDlg();
240         gtk_window_present( g_FindTextureDialog.GetWidget() );
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 #include "preferencesystem.h"
261
262 void FindTextureDialog_Construct(){
263         GlobalCommands_insert( "FindReplaceTextures", FindTextureDialog::ShowCaller() );
264         GlobalPreferenceSystem().registerPreference( "FindReplacehWnd", WindowPositionTrackerImportStringCaller( g_FindTextureDialog.m_position_tracker ), WindowPositionTrackerExportStringCaller( g_FindTextureDialog.m_position_tracker ) );
265 }
266
267 void FindTextureDialog_Destroy(){
268 }