2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
23 // Find/Replace textures dialogs
25 // Leonardo Zide (leo@lokigames.com)
28 #include "findtexturedialog.h"
30 #include "debugging/debugging.h"
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>
46 #include "gtkutil/window.h"
47 #include "stream/stringstream.h"
52 #include "textureentry.h"
56 class FindTextureDialog : public Dialog
59 static void setReplaceStr(const char* name);
60 static void setFindStr(const char* name);
63 static void updateTextures(const char* name);
66 virtual ~FindTextureDialog();
67 GtkWindow* BuildDialog();
69 void constructWindow(GtkWindow* parent)
81 CopiedString m_strFind;
82 CopiedString m_strReplace;
85 FindTextureDialog g_FindTextureDialog;
86 static bool g_bFindActive = true;
90 void FindTextureDialog_apply()
92 StringOutputStream find(256);
93 find << "textures/" << g_FindTextureDialog.m_strFind.c_str();
94 StringOutputStream replace(256);
95 replace << "textures/" << g_FindTextureDialog.m_strReplace.c_str();
96 FindReplaceTextures(find.c_str(), replace.c_str(), g_FindTextureDialog.m_bSelectedOnly);
99 static void OnApply(GtkWidget* widget, gpointer data)
101 g_FindTextureDialog.exportData();
102 FindTextureDialog_apply();
105 static void OnFind(GtkWidget* widget, gpointer data)
107 g_FindTextureDialog.exportData();
108 FindTextureDialog_apply();
111 static void OnOK(GtkWidget* widget, gpointer data)
113 g_FindTextureDialog.exportData();
114 FindTextureDialog_apply();
115 g_FindTextureDialog.HideDlg();
118 static void OnClose(GtkWidget* widget, gpointer data)
120 g_FindTextureDialog.HideDlg();
124 static gint find_focus_in (GtkWidget* widget, GdkEventFocus *event, gpointer data)
126 g_bFindActive = true;
130 static gint replace_focus_in (GtkWidget* widget, GdkEventFocus *event, gpointer data)
132 g_bFindActive = false;
137 // =============================================================================
138 // FindTextureDialog class
140 FindTextureDialog::FindTextureDialog()
142 m_bSelectedOnly = FALSE;
145 FindTextureDialog::~FindTextureDialog()
149 GtkWindow* FindTextureDialog::BuildDialog()
151 GtkWidget* vbox, *hbox, *table, *label;
152 GtkWidget* button, *check, *entry;
154 GtkWindow* dlg = create_floating_window("Find / Replace Texture(s)", m_parent);
156 hbox = gtk_hbox_new (FALSE, 5);
157 gtk_widget_show (hbox);
158 gtk_container_add(GTK_CONTAINER(dlg), GTK_WIDGET(hbox));
159 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
161 vbox = gtk_vbox_new (FALSE, 5);
162 gtk_widget_show (vbox);
163 gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(vbox), TRUE, TRUE, 0);
165 table = gtk_table_new (2, 2, FALSE);
166 gtk_widget_show (table);
167 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(table), TRUE, TRUE, 0);
168 gtk_table_set_row_spacings (GTK_TABLE (table), 5);
169 gtk_table_set_col_spacings (GTK_TABLE (table), 5);
171 label = gtk_label_new ("Find:");
172 gtk_widget_show (label);
173 gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
174 (GtkAttachOptions) (GTK_FILL),
175 (GtkAttachOptions) (0), 0, 0);
176 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
178 label = gtk_label_new ("Replace:");
179 gtk_widget_show (label);
180 gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2,
181 (GtkAttachOptions) (GTK_FILL),
182 (GtkAttachOptions) (0), 0, 0);
183 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
185 entry = gtk_entry_new();
186 gtk_widget_show (entry);
187 gtk_table_attach (GTK_TABLE (table), entry, 1, 2, 0, 1,
188 (GtkAttachOptions) (GTK_EXPAND|GTK_FILL),
189 (GtkAttachOptions) (0), 0, 0);
190 g_signal_connect(G_OBJECT(entry), "focus_in_event",
191 G_CALLBACK(find_focus_in), 0);
192 AddDialogData(*GTK_ENTRY(entry), m_strFind);
193 GlobalTextureEntryCompletion::instance().connect(GTK_ENTRY(entry));
195 entry = gtk_entry_new();
196 gtk_widget_show (entry);
197 gtk_table_attach (GTK_TABLE (table), entry, 1, 2, 1, 2,
198 (GtkAttachOptions) (GTK_EXPAND|GTK_FILL),
199 (GtkAttachOptions) (0), 0, 0);
200 g_signal_connect(G_OBJECT(entry), "focus_in_event",
201 G_CALLBACK(replace_focus_in), 0);
202 AddDialogData(*GTK_ENTRY(entry), m_strReplace);
203 GlobalTextureEntryCompletion::instance().connect(GTK_ENTRY(entry));
205 check = gtk_check_button_new_with_label ("Within selected brushes only");
206 gtk_widget_show (check);
207 gtk_box_pack_start(GTK_BOX(vbox), check, TRUE, TRUE, 0);
208 AddDialogData(*GTK_TOGGLE_BUTTON(check), m_bSelectedOnly);
210 vbox = gtk_vbox_new (FALSE, 5);
211 gtk_widget_show (vbox);
212 gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(vbox), FALSE, FALSE, 0);
214 button = gtk_button_new_with_label ("Apply");
215 gtk_widget_show (button);
216 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
217 g_signal_connect(G_OBJECT(button), "clicked",
218 G_CALLBACK(OnApply), 0);
219 gtk_widget_set_usize (button, 60, -2);
221 button = gtk_button_new_with_label ("Close");
222 gtk_widget_show (button);
223 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
224 g_signal_connect(G_OBJECT(button), "clicked",
225 G_CALLBACK(OnClose), 0);
226 gtk_widget_set_usize (button, 60, -2);
231 void FindTextureDialog::updateTextures(const char* name)
237 setFindStr(name + 9);
241 setReplaceStr(name + 9);
246 bool FindTextureDialog::isOpen()
248 return GTK_WIDGET_VISIBLE(g_FindTextureDialog.GetWidget()) == TRUE;
251 void FindTextureDialog::setFindStr(const char* name)
253 g_FindTextureDialog.exportData();
254 g_FindTextureDialog.m_strFind = name;
255 g_FindTextureDialog.importData();
258 void FindTextureDialog::setReplaceStr(const char* name)
260 g_FindTextureDialog.exportData();
261 g_FindTextureDialog.m_strReplace = name;
262 g_FindTextureDialog.importData();
265 void FindTextureDialog::show()
267 g_FindTextureDialog.ShowDlg();
271 void FindTextureDialog_constructWindow(GtkWindow* main_window)
273 g_FindTextureDialog.constructWindow(main_window);
276 void FindTextureDialog_destroyWindow()
278 g_FindTextureDialog.destroyWindow();
281 bool FindTextureDialog_isOpen()
283 return g_FindTextureDialog.isOpen();
286 void FindTextureDialog_selectTexture(const char* name)
288 g_FindTextureDialog.updateTextures(name);
291 void FindTextureDialog_Construct()
293 GlobalCommands_insert("FindReplaceTextures", FreeCaller<FindTextureDialog::show>());
296 void FindTextureDialog_Destroy()