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