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 typedef FreeCaller<&FindTextureDialog::show> ShowCaller;
64 static void updateTextures(const char* name);
67 virtual ~FindTextureDialog();
68 GtkWindow* BuildDialog();
70 void constructWindow(GtkWindow* parent)
82 CopiedString m_strFind;
83 CopiedString m_strReplace;
86 FindTextureDialog g_FindTextureDialog;
87 static bool g_bFindActive = true;
91 void FindTextureDialog_apply()
93 StringOutputStream find(256);
94 StringOutputStream replace(256);
96 find << "textures/" << g_FindTextureDialog.m_strFind.c_str();
97 replace << "textures/" << g_FindTextureDialog.m_strReplace.c_str();
98 FindReplaceTextures(find.c_str(), replace.c_str(), g_FindTextureDialog.m_bSelectedOnly);
101 static void OnApply(GtkWidget* widget, gpointer data)
103 g_FindTextureDialog.exportData();
104 FindTextureDialog_apply();
107 static void OnFind(GtkWidget* widget, gpointer data)
109 g_FindTextureDialog.exportData();
110 FindTextureDialog_apply();
113 static void OnOK(GtkWidget* widget, gpointer data)
115 g_FindTextureDialog.exportData();
116 FindTextureDialog_apply();
117 g_FindTextureDialog.HideDlg();
120 static void OnClose(GtkWidget* widget, gpointer data)
122 g_FindTextureDialog.HideDlg();
126 static gint find_focus_in (GtkWidget* widget, GdkEventFocus *event, gpointer data)
128 g_bFindActive = true;
132 static gint replace_focus_in (GtkWidget* widget, GdkEventFocus *event, gpointer data)
134 g_bFindActive = false;
139 // =============================================================================
140 // FindTextureDialog class
142 FindTextureDialog::FindTextureDialog()
144 m_bSelectedOnly = FALSE;
147 FindTextureDialog::~FindTextureDialog()
151 GtkWindow* FindTextureDialog::BuildDialog()
153 GtkWidget* vbox, *hbox, *table, *label;
154 GtkWidget* button, *check, *entry;
156 GtkWindow* dlg = create_floating_window("Find / Replace Texture(s)", m_parent);
158 hbox = gtk_hbox_new (FALSE, 5);
159 gtk_widget_show (hbox);
160 gtk_container_add(GTK_CONTAINER(dlg), GTK_WIDGET(hbox));
161 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
163 vbox = gtk_vbox_new (FALSE, 5);
164 gtk_widget_show (vbox);
165 gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(vbox), TRUE, TRUE, 0);
167 table = gtk_table_new (2, 2, FALSE);
168 gtk_widget_show (table);
169 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(table), TRUE, TRUE, 0);
170 gtk_table_set_row_spacings (GTK_TABLE (table), 5);
171 gtk_table_set_col_spacings (GTK_TABLE (table), 5);
173 label = gtk_label_new ("Find:");
174 gtk_widget_show (label);
175 gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
176 (GtkAttachOptions) (GTK_FILL),
177 (GtkAttachOptions) (0), 0, 0);
178 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
180 label = gtk_label_new ("Replace:");
181 gtk_widget_show (label);
182 gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2,
183 (GtkAttachOptions) (GTK_FILL),
184 (GtkAttachOptions) (0), 0, 0);
185 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
187 entry = gtk_entry_new();
188 gtk_widget_show (entry);
189 gtk_table_attach (GTK_TABLE (table), entry, 1, 2, 0, 1,
190 (GtkAttachOptions) (GTK_EXPAND|GTK_FILL),
191 (GtkAttachOptions) (0), 0, 0);
192 g_signal_connect(G_OBJECT(entry), "focus_in_event",
193 G_CALLBACK(find_focus_in), 0);
194 AddDialogData(*GTK_ENTRY(entry), m_strFind);
195 GlobalTextureEntryCompletion::instance().connect(GTK_ENTRY(entry));
197 entry = gtk_entry_new();
198 gtk_widget_show (entry);
199 gtk_table_attach (GTK_TABLE (table), entry, 1, 2, 1, 2,
200 (GtkAttachOptions) (GTK_EXPAND|GTK_FILL),
201 (GtkAttachOptions) (0), 0, 0);
202 g_signal_connect(G_OBJECT(entry), "focus_in_event",
203 G_CALLBACK(replace_focus_in), 0);
204 AddDialogData(*GTK_ENTRY(entry), m_strReplace);
205 GlobalTextureEntryCompletion::instance().connect(GTK_ENTRY(entry));
207 check = gtk_check_button_new_with_label ("Within selected brushes only");
208 gtk_widget_show (check);
209 gtk_box_pack_start(GTK_BOX(vbox), check, TRUE, TRUE, 0);
210 AddDialogData(*GTK_TOGGLE_BUTTON(check), m_bSelectedOnly);
212 vbox = gtk_vbox_new (FALSE, 5);
213 gtk_widget_show (vbox);
214 gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(vbox), FALSE, FALSE, 0);
216 button = gtk_button_new_with_label ("Apply");
217 gtk_widget_show (button);
218 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
219 g_signal_connect(G_OBJECT(button), "clicked",
220 G_CALLBACK(OnApply), 0);
221 gtk_widget_set_usize (button, 60, -2);
223 button = gtk_button_new_with_label ("Close");
224 gtk_widget_show (button);
225 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
226 g_signal_connect(G_OBJECT(button), "clicked",
227 G_CALLBACK(OnClose), 0);
228 gtk_widget_set_usize (button, 60, -2);
233 void FindTextureDialog::updateTextures(const char* name)
239 setFindStr(name + 9);
243 setReplaceStr(name + 9);
248 bool FindTextureDialog::isOpen()
250 return GTK_WIDGET_VISIBLE(g_FindTextureDialog.GetWidget()) == TRUE;
253 void FindTextureDialog::setFindStr(const char* name)
255 g_FindTextureDialog.exportData();
256 g_FindTextureDialog.m_strFind = name;
257 g_FindTextureDialog.importData();
260 void FindTextureDialog::setReplaceStr(const char* name)
262 g_FindTextureDialog.exportData();
263 g_FindTextureDialog.m_strReplace = name;
264 g_FindTextureDialog.importData();
267 void FindTextureDialog::show()
269 g_FindTextureDialog.ShowDlg();
273 void FindTextureDialog_constructWindow(GtkWindow* main_window)
275 g_FindTextureDialog.constructWindow(main_window);
278 void FindTextureDialog_destroyWindow()
280 g_FindTextureDialog.destroyWindow();
283 bool FindTextureDialog_isOpen()
285 return g_FindTextureDialog.isOpen();
288 void FindTextureDialog_selectTexture(const char* name)
290 g_FindTextureDialog.updateTextures(name);
293 void FindTextureDialog_Construct()
295 GlobalCommands_insert("FindReplaceTextures", FindTextureDialog::ShowCaller());
298 void FindTextureDialog_Destroy()