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