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