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