]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/findtexturedialog.cpp
limited win32 stack-trace size
[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   static void updateTextures(const char* name);
64
65   FindTextureDialog();
66   virtual ~FindTextureDialog();
67   GtkWindow* BuildDialog();
68
69   void constructWindow(GtkWindow* parent)
70   {
71     m_parent = parent;
72     Create();
73   }
74   void destroyWindow()
75   {
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   {
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);
97   }
98
99   static void OnApply(GtkWidget* widget, gpointer data) 
100   {
101     g_FindTextureDialog.exportData();
102     FindTextureDialog_apply();
103   }
104
105   static void OnFind(GtkWidget* widget, gpointer data) 
106   {
107     g_FindTextureDialog.exportData();
108     FindTextureDialog_apply();
109   }
110
111   static void OnOK(GtkWidget* widget, gpointer data) 
112   {
113     g_FindTextureDialog.exportData();
114     FindTextureDialog_apply();
115     g_FindTextureDialog.HideDlg();
116   }
117
118   static void OnClose(GtkWidget* widget, gpointer data) 
119   {
120     g_FindTextureDialog.HideDlg();
121   }
122
123
124   static gint find_focus_in (GtkWidget* widget, GdkEventFocus *event, gpointer data)
125   {
126     g_bFindActive = true;
127     return FALSE;
128   }
129
130   static gint replace_focus_in (GtkWidget* widget, GdkEventFocus *event, gpointer data)
131   {
132     g_bFindActive = false;
133     return FALSE;
134   }
135 }
136
137 // =============================================================================
138 // FindTextureDialog class
139
140 FindTextureDialog::FindTextureDialog()
141 {
142   m_bSelectedOnly = FALSE;
143 }
144
145 FindTextureDialog::~FindTextureDialog()
146 {
147 }
148
149 GtkWindow* FindTextureDialog::BuildDialog()
150 {
151   GtkWidget* vbox, *hbox, *table, *label;
152   GtkWidget* button, *check, *entry;
153
154   GtkWindow* dlg = create_floating_window("Find / Replace Texture(s)", m_parent);
155
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);
160
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);
164
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);
170
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);
177
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);
184
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));
194
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));
204
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);
209
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);
213
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);
220
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);
227
228   return dlg;
229 }
230
231 void FindTextureDialog::updateTextures(const char* name)
232 {
233   if (isOpen())
234   {
235     if (g_bFindActive)
236     {
237       setFindStr(name + 9);
238     }
239     else
240     {
241       setReplaceStr(name + 9);
242     }
243   }
244 }
245
246 bool FindTextureDialog::isOpen()
247 {
248   return GTK_WIDGET_VISIBLE(g_FindTextureDialog.GetWidget()) == TRUE;
249 }
250
251 void FindTextureDialog::setFindStr(const char* name)
252 {
253   g_FindTextureDialog.exportData();
254   g_FindTextureDialog.m_strFind = name;
255   g_FindTextureDialog.importData();
256 }
257
258 void FindTextureDialog::setReplaceStr(const char* name)
259 {
260   g_FindTextureDialog.exportData();
261   g_FindTextureDialog.m_strReplace = name;
262   g_FindTextureDialog.importData();
263 }
264
265 void FindTextureDialog::show()
266 {
267   g_FindTextureDialog.ShowDlg();
268 }
269
270
271 void FindTextureDialog_constructWindow(GtkWindow* main_window)
272 {
273   g_FindTextureDialog.constructWindow(main_window);
274 }
275
276 void FindTextureDialog_destroyWindow()
277 {
278   g_FindTextureDialog.destroyWindow();
279 }
280
281 bool FindTextureDialog_isOpen()
282 {
283   return g_FindTextureDialog.isOpen();
284 }
285
286 void FindTextureDialog_selectTexture(const char* name)
287 {
288   g_FindTextureDialog.updateTextures(name);
289 }
290
291 void FindTextureDialog_Construct()
292 {
293   GlobalCommands_insert("FindReplaceTextures", FreeCaller<FindTextureDialog::show>());
294 }
295
296 void FindTextureDialog_Destroy()
297 {
298 }
299