]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/findtexturedialog.cpp
Radiant:
[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_set_tooltip_text( label, "Empty = search mode" );
174         gtk_widget_show( label );
175         gtk_table_attach( GTK_TABLE( table ), label, 0, 1, 1, 2,
176                                           (GtkAttachOptions) ( GTK_FILL ),
177                                           (GtkAttachOptions) ( 0 ), 0, 0 );
178         gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
179
180         entry = gtk_entry_new();
181         gtk_widget_show( entry );
182         gtk_table_attach( GTK_TABLE( table ), entry, 1, 2, 0, 1,
183                                           (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
184                                           (GtkAttachOptions) ( 0 ), 0, 0 );
185         g_signal_connect( G_OBJECT( entry ), "focus_in_event",
186                                           G_CALLBACK( find_focus_in ), 0 );
187         AddDialogData( *GTK_ENTRY( entry ), m_strFind );
188         GlobalTextureEntryCompletion::instance().connect( GTK_ENTRY( entry ) );
189
190         entry = gtk_entry_new();
191         gtk_widget_set_tooltip_text( entry, "Empty = search mode" );
192         gtk_widget_show( entry );
193         gtk_table_attach( GTK_TABLE( table ), entry, 1, 2, 1, 2,
194                                           (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
195                                           (GtkAttachOptions) ( 0 ), 0, 0 );
196         g_signal_connect( G_OBJECT( entry ), "focus_in_event",
197                                           G_CALLBACK( replace_focus_in ), 0 );
198         AddDialogData( *GTK_ENTRY( entry ), m_strReplace );
199         GlobalTextureEntryCompletion::instance().connect( GTK_ENTRY( entry ) );
200
201         check = gtk_check_button_new_with_label( "Within selected brushes only" );
202         gtk_widget_show( check );
203         gtk_box_pack_start( GTK_BOX( vbox ), check, TRUE, TRUE, 0 );
204         AddDialogData( *GTK_TOGGLE_BUTTON( check ), m_bSelectedOnly );
205
206         vbox = gtk_vbox_new( FALSE, 5 );
207         gtk_widget_show( vbox );
208         gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( vbox ), FALSE, FALSE, 0 );
209
210         button = gtk_button_new_with_label( "Apply" );
211         gtk_widget_show( button );
212         gtk_box_pack_start( GTK_BOX( vbox ), button, FALSE, FALSE, 0 );
213         g_signal_connect( G_OBJECT( button ), "clicked",
214                                           G_CALLBACK( OnApply ), 0 );
215         gtk_widget_set_usize( button, 60, -2 );
216
217         button = gtk_button_new_with_label( "Close" );
218         gtk_widget_show( button );
219         gtk_box_pack_start( GTK_BOX( vbox ), button, FALSE, FALSE, 0 );
220         g_signal_connect( G_OBJECT( button ), "clicked",
221                                           G_CALLBACK( OnClose ), 0 );
222         gtk_widget_set_usize( button, 60, -2 );
223
224         return dlg;
225 }
226
227 void FindTextureDialog::updateTextures( const char* name ){
228         if ( isOpen() ) {
229                 if ( g_bFindActive ) {
230                         setFindStr( name + 9 );
231                 }
232                 else
233                 {
234                         setReplaceStr( name + 9 );
235                 }
236         }
237 }
238
239 bool FindTextureDialog::isOpen(){
240         return GTK_WIDGET_VISIBLE( g_FindTextureDialog.GetWidget() ) == TRUE;
241 }
242
243 void FindTextureDialog::setFindStr( const char* name ){
244         g_FindTextureDialog.exportData();
245         g_FindTextureDialog.m_strFind = name;
246         g_FindTextureDialog.importData();
247 }
248
249 void FindTextureDialog::setReplaceStr( const char* name ){
250         g_FindTextureDialog.exportData();
251         g_FindTextureDialog.m_strReplace = name;
252         g_FindTextureDialog.importData();
253 }
254
255 void FindTextureDialog::show(){
256         // 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
257         g_FindTextureDialog.m_position_tracker.sync( g_FindTextureDialog.GetWidget() );
258         g_FindTextureDialog.ShowDlg();
259         gtk_window_present( g_FindTextureDialog.GetWidget() );
260 }
261
262
263 void FindTextureDialog_constructWindow( GtkWindow* main_window ){
264         g_FindTextureDialog.constructWindow( main_window );
265 }
266
267 void FindTextureDialog_destroyWindow(){
268         g_FindTextureDialog.destroyWindow();
269 }
270
271 bool FindTextureDialog_isOpen(){
272         return g_FindTextureDialog.isOpen();
273 }
274
275 void FindTextureDialog_selectTexture( const char* name ){
276         g_FindTextureDialog.updateTextures( name );
277 }
278
279 #include "preferencesystem.h"
280
281 void FindTextureDialog_Construct(){
282         GlobalCommands_insert( "FindReplaceTextures", FindTextureDialog::ShowCaller() );
283         GlobalPreferenceSystem().registerPreference( "FindReplacehWnd", WindowPositionTrackerImportStringCaller( g_FindTextureDialog.m_position_tracker ), WindowPositionTrackerExportStringCaller( g_FindTextureDialog.m_position_tracker ) );
284 }
285
286 void FindTextureDialog_Destroy(){
287 }