]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/findtexturedialog.cpp
more eol-style
[xonotic/netradiant.git] / radiant / findtexturedialog.cpp
1 /*
2 Copyright (C) 1999-2007 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 "stdafx.h"
29 #include "findtexturedialog.h"
30
31 FindTextureDialog g_TexFindDlg;
32 FindTextureDialog& g_dlgFind = g_TexFindDlg;
33 static bool g_bFindActive = true;
34
35 static void OnApply(GtkWidget *widget, gpointer data) 
36 {
37   g_dlgFind.UpdateData(TRUE);
38   FindReplaceTextures(g_dlgFind.m_strFind, g_dlgFind.m_strReplace,
39     g_dlgFind.m_bSelectedOnly, g_dlgFind.m_bForce, FALSE);
40 }
41
42 static void OnFind(GtkWidget *widget, gpointer data) 
43 {
44   g_dlgFind.UpdateData(TRUE);
45   FindReplaceTextures(g_dlgFind.m_strFind, g_dlgFind.m_strReplace,
46     g_dlgFind.m_bSelectedOnly, FALSE, TRUE);
47 }
48
49 static void OnOK(GtkWidget *widget, gpointer data) 
50 {
51   g_dlgFind.UpdateData(TRUE);
52   FindReplaceTextures(g_dlgFind.m_strFind, g_dlgFind.m_strReplace,
53     g_dlgFind.m_bSelectedOnly, g_dlgFind.m_bForce, FALSE);
54   g_dlgFind.HideDlg ();
55 }
56
57 static void OnClose(GtkWidget *widget, gpointer data) 
58 {
59   g_dlgFind.HideDlg ();
60 }
61
62 static void popup_selected (GtkWidget *widget, gpointer data) 
63 {
64   gchar *str;
65
66   gtk_label_get (GTK_LABEL (GTK_BIN (widget)->child), &str);
67   gtk_entry_set_text (GTK_ENTRY (data), str);
68 }
69
70 static void find_clicked (GtkWidget *widget, gpointer data) 
71 {
72   GtkWidget *menu, *item;
73   menu = gtk_menu_new ();
74
75   for (int i = 0; i < QERApp_GetActiveShaderCount (); i++)
76   {
77     IShader *pShader = QERApp_ActiveShader_ForIndex (i);
78
79     item = gtk_menu_item_new_with_label (pShader->getName ());
80     gtk_signal_connect (GTK_OBJECT (item), "activate", GTK_SIGNAL_FUNC (popup_selected), data);
81     gtk_widget_show (item);
82     gtk_menu_append (GTK_MENU (menu), item);
83   }
84
85   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, 1, GDK_CURRENT_TIME); 
86 }
87
88 static gint find_focus_in (GtkWidget *widget, GdkEventFocus *event, gpointer data)
89 {
90   g_bFindActive = true;
91   return FALSE;
92 }
93
94 static gint replace_focus_in (GtkWidget *widget, GdkEventFocus *event, gpointer data)
95 {
96   g_bFindActive = false;
97   return FALSE;
98 }
99
100 // =============================================================================
101 // FindTextureDialog class
102
103 FindTextureDialog::FindTextureDialog ()
104 {
105   m_bSelectedOnly = FALSE;
106   m_strFind = "";
107   m_strReplace = "";
108   m_bForce = FALSE;
109   m_bLive = TRUE;
110 }
111
112 FindTextureDialog::~FindTextureDialog ()
113 {
114 }
115
116 void FindTextureDialog::BuildDialog ()
117 {
118   GtkWidget *dlg, *vbox, *hbox, *table, *label;
119   GtkWidget *button, *check, *entry, *arrow;
120
121   dlg = m_pWidget;
122   gtk_window_set_title (GTK_WINDOW (dlg), "Find / Replace Texture(s)");
123   gtk_window_set_transient_for (GTK_WINDOW (dlg), GTK_WINDOW (g_pParentWnd->m_pWidget));
124
125   hbox = gtk_hbox_new (FALSE, 5);
126   gtk_widget_show (hbox);
127   gtk_container_add (GTK_CONTAINER (dlg), hbox);
128   gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
129
130   vbox = gtk_vbox_new (FALSE, 5);
131   gtk_widget_show (vbox);
132   gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
133
134   table = gtk_table_new (2, 3, FALSE);
135   gtk_widget_show (table);
136   gtk_box_pack_start (GTK_BOX (vbox), table, TRUE, TRUE, 0);
137   gtk_table_set_row_spacings (GTK_TABLE (table), 5);
138   gtk_table_set_col_spacings (GTK_TABLE (table), 5);
139
140   label = gtk_label_new ("Find:");
141   gtk_widget_show (label);
142   gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
143                     (GtkAttachOptions) (GTK_FILL),
144                     (GtkAttachOptions) (0), 0, 0);
145   gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
146
147   label = gtk_label_new ("Replace:");
148   gtk_widget_show (label);
149   gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2,
150                     (GtkAttachOptions) (GTK_FILL),
151                     (GtkAttachOptions) (0), 0, 0);
152   gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
153
154   entry = gtk_entry_new ();
155   gtk_widget_show (entry);
156   gtk_table_attach (GTK_TABLE (table), entry, 1, 2, 0, 1,
157                     (GtkAttachOptions) (GTK_EXPAND|GTK_FILL),
158                     (GtkAttachOptions) (0), 0, 0);
159   gtk_signal_connect (GTK_OBJECT (entry), "focus_in_event",
160                       GTK_SIGNAL_FUNC (find_focus_in), NULL);
161   AddDialogData (entry, &m_strFind, DLG_ENTRY_TEXT);
162
163   button = gtk_button_new ();
164   gtk_widget_show (button);
165   gtk_table_attach (GTK_TABLE (table), button, 2, 3, 0, 1,
166                     (GtkAttachOptions) (GTK_FILL),
167                     (GtkAttachOptions) (GTK_FILL), 0, 0);
168   gtk_signal_connect (GTK_OBJECT (button), "clicked",
169                       GTK_SIGNAL_FUNC (find_clicked), entry);
170
171   arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
172   gtk_widget_show (arrow);
173   gtk_container_add (GTK_CONTAINER (button), arrow);
174
175   entry = gtk_entry_new ();
176   gtk_widget_show (entry);
177   gtk_table_attach (GTK_TABLE (table), entry, 1, 2, 1, 2,
178                     (GtkAttachOptions) (GTK_EXPAND|GTK_FILL),
179                     (GtkAttachOptions) (0), 0, 0);
180   gtk_signal_connect (GTK_OBJECT (entry), "focus_in_event",
181                       GTK_SIGNAL_FUNC (replace_focus_in), NULL);
182   AddDialogData (entry, &m_strReplace, DLG_ENTRY_TEXT);
183
184   button = gtk_button_new ();
185   gtk_widget_show (button);
186   gtk_table_attach (GTK_TABLE (table), button, 2, 3, 1, 2,
187                     (GtkAttachOptions) (GTK_FILL),
188                     (GtkAttachOptions) (GTK_FILL), 0, 0);
189   gtk_signal_connect (GTK_OBJECT (button), "clicked",
190                       GTK_SIGNAL_FUNC (find_clicked), entry);
191
192   arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
193   gtk_widget_show (arrow);
194   gtk_container_add (GTK_CONTAINER (button), arrow);
195
196   check = gtk_check_button_new_with_label ("Use selected brushes only");
197   gtk_widget_show (check);
198   gtk_box_pack_start (GTK_BOX (vbox), check, TRUE, TRUE, 0);
199   AddDialogData (check, &m_bSelectedOnly, DLG_CHECK_BOOL);
200
201   check = gtk_check_button_new_with_label ("Replace everywhere (selected/active), don't test against Find");
202   gtk_widget_show (check);
203   gtk_box_pack_start (GTK_BOX (vbox), check, TRUE, TRUE, 0);
204   AddDialogData (check, &m_bForce, DLG_CHECK_BOOL);
205
206   check = gtk_check_button_new_with_label ("Live updates from Texture/Camera windows");
207   gtk_widget_show (check);
208   gtk_box_pack_start (GTK_BOX (vbox), check, TRUE, TRUE, 0);
209   AddDialogData (check, &m_bLive, DLG_CHECK_BOOL);
210
211   vbox = gtk_vbox_new (FALSE, 5);
212   gtk_widget_show (vbox);
213   gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
214
215   button = gtk_button_new_with_label( "Find" );
216   gtk_widget_show( button );
217   gtk_box_pack_start( GTK_BOX( vbox ), button, FALSE, FALSE, 0 );
218   gtk_signal_connect( GTK_OBJECT( button ), "clicked",
219                       GTK_SIGNAL_FUNC( OnFind ), NULL );
220   gtk_widget_set_usize( button, 60, -2 );
221
222   button = gtk_button_new_with_label ("OK");
223   gtk_widget_show (button);
224   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
225   gtk_signal_connect (GTK_OBJECT (button), "clicked",
226                       GTK_SIGNAL_FUNC (OnOK), NULL);
227   gtk_widget_set_usize (button, 60, -2);
228
229   button = gtk_button_new_with_label ("Apply");
230   gtk_widget_show (button);
231   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
232   gtk_signal_connect (GTK_OBJECT (button), "clicked",
233                       GTK_SIGNAL_FUNC (OnApply), NULL);
234   gtk_widget_set_usize (button, 60, -2);
235
236   button = gtk_button_new_with_label ("Close");
237   gtk_widget_show (button);
238   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
239   gtk_signal_connect (GTK_OBJECT (button), "clicked",
240                       GTK_SIGNAL_FUNC (OnClose), NULL);
241   gtk_widget_set_usize (button, 60, -2);
242
243   gtk_widget_show (dlg);
244 }
245
246 void FindTextureDialog::updateTextures(const char *p)
247 {
248   if (isOpen())
249   {
250     if (g_bFindActive)
251     {
252       setFindStr(p);
253     }
254     else
255     {
256       setReplaceStr(p);
257     }
258   }
259 }
260
261 bool FindTextureDialog::isOpen()
262 {
263   return (g_dlgFind.m_pWidget == NULL || GTK_WIDGET_VISIBLE (g_dlgFind.m_pWidget) == FALSE) ? false : true;
264 }
265
266 void FindTextureDialog::setFindStr(const char * p)
267 {
268   g_dlgFind.UpdateData(TRUE);
269   if (g_dlgFind.m_bLive)
270   {
271     g_dlgFind.m_strFind = p;
272     g_dlgFind.UpdateData(FALSE);
273   }
274 }
275
276 void FindTextureDialog::setReplaceStr(const char * p)
277 {
278   g_dlgFind.UpdateData(TRUE);
279   if (g_dlgFind.m_bLive)
280   {
281     g_dlgFind.m_strReplace = p;
282     g_dlgFind.UpdateData(FALSE);
283   }
284 }
285
286 void FindTextureDialog::show()
287 {
288   g_dlgFind.ShowDlg ();
289 }