2 Copyright (C) 2003 Reed Mideke.
4 This file is part of GtkRadiant.
6 GtkRadiant is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 GtkRadiant is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GtkRadiant; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 // bkgrnd2d Plugin dialog
24 // Code by reyalP aka Reed Mideke
26 // Based on various other plugins
34 // spaces to make label nice and big
35 #define NO_FILE_MSG " (no file loaded) "
37 static GtkWidget *pDialogWnd;
38 static GtkWidget *pNotebook;
39 static GtkTooltips *pTooltips;
41 class CBackgroundDialogPage
45 GtkWidget *m_pTabLabel;
46 GtkWidget *m_pFileLabel;
47 GtkWidget *m_pPosLabel;
52 CBackgroundImage *m_pImage;
53 CBackgroundDialogPage( VIEWTYPE vt );
54 void Append(GtkWidget *notebook);
58 // ~BackgroundDialogPage();
62 // dialog page callbacks
63 static void browse_callback( GtkWidget *widget, gpointer data )
65 ((CBackgroundDialogPage *)data)->Browse();
68 static void reload_callback( GtkWidget *widget, gpointer data )
70 ((CBackgroundDialogPage *)data)->Reload();
73 static void size_sel_callback( GtkWidget *widget, gpointer data )
75 CBackgroundDialogPage *pPage = (CBackgroundDialogPage *)data;
76 if (pPage->m_pImage->SetExtentsSel())
80 static void size_mm_callback( GtkWidget *widget, gpointer data )
82 CBackgroundDialogPage *pPage = (CBackgroundDialogPage *)data;
83 if(pPage->m_pImage->SetExtentsMM())
87 static void alpha_adjust_callback( GtkWidget *widget, gpointer data )
89 CBackgroundDialogPage *pPage = (CBackgroundDialogPage *)data;
90 pPage->m_pImage->m_alpha = (float)gtk_range_get_value (GTK_RANGE(widget));
91 g_FuncTable.m_pfnSysUpdateWindows(W_XY);
94 void CBackgroundDialogPage::Reload()
97 m_pImage->Load(gtk_label_get_text(GTK_LABEL(m_pFileLabel)));
100 void CBackgroundDialogPage::Browse()
102 char browsedir[PATH_MAX];
107 //TODO GetMapName saves the map. eeep!
108 //also with no map, returns unnamed.map, otherwise returns full path
109 // Syn_Printf(MSG_PREFIX "GetMapName() %s\n",
110 // g_FuncTable.m_pfnGetMapName());
112 ct = g_FuncTable.m_pfnReadProjectKey("basepath");
113 // TODO shouldn't need this stuff
114 if(!ct || !strlen(ct)) {
115 Syn_Printf(MSG_PREFIX "basepath = NULL or empty\n");
118 Syn_Printf(MSG_PREFIX "basepath: %s\n",ct);
119 if(strlen(ct) >= PATH_MAX) {
120 Syn_Printf(MSG_PREFIX "base game dir too long\n");
124 strcpy(browsedir,ct);
125 // make sure we have a trailing /
126 if(browsedir[strlen(browsedir) - 1] != '/')
127 strcat(browsedir,"/");
129 //if we dont have a file yet, don't try to use it for default dir
131 // filename should always be a nice clean unix style relative path
132 ct = gtk_label_get_text(GTK_LABEL(m_pFileLabel));
133 strcat(browsedir,ct);
134 Syn_Printf(MSG_PREFIX "full path: %s\n",browsedir);
136 // lop off the file part
137 t = browsedir + strlen(browsedir) - 1;
138 while (t != browsedir && *t != '/')
142 Syn_Printf(MSG_PREFIX "browse directory %s\n",browsedir);
144 //does NOT need freeing contrary to include/qerplugin.h comments
145 //TODO bug/patch for comments
146 //TODO patern gets fucked up sometimes if empty
147 //http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=915
148 newfile = g_FuncTable.m_pfnFileDialog(pDialogWnd,TRUE,
149 "Load Background Image",browsedir,FILETYPE_KEY);
151 Syn_Printf(MSG_PREFIX "newfile = NULL\n");
154 Syn_Printf(MSG_PREFIX "newfile: %s\n",newfile);
155 newfile = g_FileSystemTable.m_pfnExtractRelativePath(newfile);
158 Syn_Printf(MSG_PREFIX "newfile = NULL\n");
161 Syn_Printf(MSG_PREFIX "newfile: %s\n",newfile);
163 if(m_pImage->Load(newfile)) {
165 gtk_label_set_text(GTK_LABEL(m_pFileLabel),newfile);
169 void CBackgroundDialogPage::SetPosLabel()
172 // TODO no snprintf ?
173 sprintf(s, "Size/Position (%d,%d) (%d,%d)",(int)(m_pImage->m_xmin),
174 (int)(m_pImage->m_ymin),(int)(m_pImage->m_xmax),(int)(m_pImage->m_ymax));
175 gtk_label_set_text(GTK_LABEL(m_pPosLabel),s);
178 CBackgroundDialogPage::CBackgroundDialogPage(VIEWTYPE vt )
186 m_bValidFile = false;
191 m_pTabLabel = gtk_label_new("X/Y");
192 m_pImage = &backgroundXY;
195 m_pTabLabel = gtk_label_new("X/Z");
196 m_pImage = &backgroundXZ;
199 m_pTabLabel = gtk_label_new("Y/Z");
200 m_pImage = &backgroundYZ;
203 // A vbox to hold everything
204 m_pWidget = gtk_vbox_new(FALSE,0);
205 // Frame for file row
206 frame = gtk_frame_new("File");
207 gtk_box_pack_start (GTK_BOX (m_pWidget),frame, FALSE, FALSE, 2);
209 // hbox for first row
210 hbox = gtk_hbox_new(FALSE,5);
211 gtk_container_set_border_width(GTK_CONTAINER (hbox),4);
212 gtk_container_add (GTK_CONTAINER (frame), hbox);
214 // label to display filename
215 m_pFileLabel = gtk_label_new(NO_FILE_MSG);
216 gtk_label_set_selectable(GTK_LABEL(m_pFileLabel),TRUE);
217 //TODO set min size ? done with spaces right now
218 gtk_box_pack_start (GTK_BOX (hbox),m_pFileLabel, TRUE, TRUE, 5);
220 gtk_widget_show (m_pFileLabel);
222 w = gtk_button_new_with_label ("Browse...");
223 g_signal_connect (G_OBJECT (w), "clicked", G_CALLBACK (browse_callback),
225 gtk_box_pack_start (GTK_BOX (hbox),w, FALSE, FALSE, 5);
226 gtk_tooltips_set_tip (pTooltips, w, "Select a file", NULL);
229 w = gtk_button_new_with_label ("Reload");
230 g_signal_connect (G_OBJECT (w), "clicked", G_CALLBACK (reload_callback),
232 // TODO disable until we have file
233 // gtk_widget_set_sensitive(w,FALSE);
234 gtk_tooltips_set_tip (pTooltips, w, "Reload current file", NULL);
235 gtk_box_pack_start (GTK_BOX (hbox),w, FALSE, FALSE, 5);
238 gtk_widget_show (hbox);
239 gtk_widget_show (frame);
241 // second row (rendering options)
242 frame = gtk_frame_new("Rendering");
243 gtk_box_pack_start (GTK_BOX (m_pWidget),frame, FALSE, FALSE, 2);
245 hbox = gtk_hbox_new(FALSE,5);
246 gtk_container_set_border_width(GTK_CONTAINER (hbox),4);
247 gtk_container_add (GTK_CONTAINER (frame), hbox);
249 w = gtk_label_new("Vertex alpha:");
250 gtk_box_pack_start (GTK_BOX (hbox),w, FALSE, FALSE, 5);
253 w = gtk_hscale_new_with_range(0.0,1.0,0.01);
254 gtk_range_set_value(GTK_RANGE(w),0.5);
255 gtk_scale_set_value_pos(GTK_SCALE(w),GTK_POS_LEFT);
256 g_signal_connect (G_OBJECT (w), "value-changed",
257 G_CALLBACK (alpha_adjust_callback), (gpointer)this);
258 gtk_box_pack_start (GTK_BOX (hbox),w, TRUE, TRUE, 5);
259 gtk_tooltips_set_tip (pTooltips, w, "Set image transparancy", NULL);
262 gtk_widget_show (hbox);
263 gtk_widget_show (frame);
264 // Third row (size and position)
265 frame = gtk_frame_new("Size/Position (undefined)");
266 m_pPosLabel = gtk_frame_get_label_widget (GTK_FRAME(frame));
267 gtk_box_pack_start ( GTK_BOX (m_pWidget), frame, FALSE, FALSE, 2);
269 hbox = gtk_hbox_new(FALSE,5);
270 gtk_container_add (GTK_CONTAINER (frame), hbox);
271 gtk_container_set_border_width(GTK_CONTAINER (hbox),4);
273 w = gtk_button_new_with_label ("from selection");
274 gtk_box_pack_start (GTK_BOX (hbox),w, TRUE, FALSE, 5);
275 g_signal_connect (G_OBJECT (w), "clicked", G_CALLBACK (size_sel_callback),
277 gtk_tooltips_set_tip (pTooltips, w, "Set the size of the image to the bounding rectangle of all selected brushes and entities", NULL);
281 w = gtk_button_new_with_label ("from map mins/maxs");
282 gtk_box_pack_start ( GTK_BOX (hbox),w, TRUE, FALSE, 2);
283 g_signal_connect (G_OBJECT (w), "clicked", G_CALLBACK (size_mm_callback),
285 gtk_tooltips_set_tip (pTooltips, w, "Set the size of the image using the mapcoordsmins and mapcoordsmaxs keys of the worldspawn entity", NULL);
289 gtk_widget_show (hbox);
290 gtk_widget_show (frame);
292 gtk_widget_show ( m_pWidget );
295 void CBackgroundDialogPage::Append(GtkWidget *notebook)
297 gtk_notebook_append_page( GTK_NOTEBOOK(notebook), m_pWidget, m_pTabLabel);
300 // dialog global callbacks
302 static gint expose_callback( GtkWidget *widget, gpointer data )
308 static void response_callback( GtkWidget *widget, gint response, gpointer data )
310 if( response == GTK_RESPONSE_CLOSE )
311 gtk_widget_hide( pDialogWnd );
314 static gint close_callback( GtkWidget *widget, gpointer data )
316 gtk_widget_hide( pDialogWnd );
320 void InitBackgroundDialog()
322 CBackgroundDialogPage *pPage;
324 pDialogWnd = gtk_dialog_new_with_buttons ("Background Images",
325 GTK_WINDOW(g_pMainWidget),
326 (GtkDialogFlags)(GTK_DIALOG_DESTROY_WITH_PARENT),
327 // TODO dialog with no buttons
329 // GTK_RESPONSE_CLOSE,
331 gtk_signal_connect( GTK_OBJECT (pDialogWnd), "delete_event",
332 GTK_SIGNAL_FUNC( close_callback ), NULL );
333 gtk_signal_connect( GTK_OBJECT (pDialogWnd), "response",
334 GTK_SIGNAL_FUNC( response_callback ), NULL );
335 // gtk_signal_connect( GTK_OBJECT (pDialogWnd), "expose_event", GTK_SIGNAL_FUNC( ci_expose ), NULL );
337 pTooltips = gtk_tooltips_new();
339 pNotebook = gtk_notebook_new();
340 pPage = new CBackgroundDialogPage(XY);
341 pPage->Append(pNotebook);
342 pPage = new CBackgroundDialogPage(XZ);
343 pPage->Append(pNotebook);
344 pPage = new CBackgroundDialogPage(YZ);
345 pPage->Append(pNotebook);
347 gtk_box_pack_start (GTK_BOX (GTK_DIALOG(pDialogWnd)->vbox), pNotebook, TRUE, TRUE, 0);
349 gtk_widget_show ( pNotebook );
351 gtk_widget_realize( pDialogWnd );
354 void ShowBackgroundDialog()
356 gtk_window_present( GTK_WINDOW(pDialogWnd) );
359 void ShowBackgroundDialogPG(int page)
361 gtk_notebook_set_current_page(GTK_NOTEBOOK(pNotebook),page);
362 ShowBackgroundDialog();