- Radiant is now Vista compatible (Aero must be disabled)
[xonotic/netradiant.git] / contrib / bkgrnd2d / dialog.cpp
1 /*
2 Copyright (C) 2003 Reed Mideke.
3
4 This file is part of GtkRadiant.
5
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.
10
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.
15
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
19 */
20
21 //
22 // bkgrnd2d Plugin dialog
23 //
24 // Code by reyalP aka Reed Mideke
25 //
26 // Based on various other plugins
27 //
28
29 #include <gtk/gtk.h>
30
31 #include "bkgrnd2d.h"
32 #include "dialog.h"
33
34 // spaces to make label nice and big
35 #define NO_FILE_MSG "        (no file loaded)        "
36
37 static GtkWidget *pDialogWnd;
38 static GtkWidget *pNotebook;
39 static GtkTooltips *pTooltips;
40
41 class CBackgroundDialogPage
42 {
43 private:
44         GtkWidget *m_pWidget;
45         GtkWidget *m_pTabLabel;
46         GtkWidget *m_pFileLabel;
47         GtkWidget *m_pPosLabel;
48         VIEWTYPE m_vt;
49         bool m_bValidFile;
50
51 public:
52         CBackgroundImage *m_pImage;
53         CBackgroundDialogPage( VIEWTYPE vt );
54         void Append(GtkWidget *notebook);
55         void Browse();
56         void Reload();
57         void SetPosLabel();
58 //  ~BackgroundDialogPage();
59 };
60
61
62 // dialog page callbacks
63 static void browse_callback( GtkWidget *widget, gpointer data )
64 {
65         ((CBackgroundDialogPage *)data)->Browse();
66 }
67
68 static void reload_callback( GtkWidget *widget, gpointer data )
69 {
70         ((CBackgroundDialogPage *)data)->Reload();
71 }
72
73 static void size_sel_callback( GtkWidget *widget, gpointer data )
74 {
75         CBackgroundDialogPage *pPage = (CBackgroundDialogPage *)data;
76         if (pPage->m_pImage->SetExtentsSel())
77                 pPage->SetPosLabel();
78 }
79
80 static void size_mm_callback( GtkWidget *widget, gpointer data )
81 {
82         CBackgroundDialogPage *pPage = (CBackgroundDialogPage *)data;
83         if(pPage->m_pImage->SetExtentsMM())
84                 pPage->SetPosLabel();
85 }
86
87 static void alpha_adjust_callback( GtkWidget *widget, gpointer data )
88 {
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);
92 }
93
94 void CBackgroundDialogPage::Reload()
95 {
96         if(m_bValidFile)
97                 m_pImage->Load(gtk_label_get_text(GTK_LABEL(m_pFileLabel)));
98 }
99
100 void CBackgroundDialogPage::Browse()
101 {
102         char browsedir[PATH_MAX];
103         const char *ct;
104         const char *newfile;
105         char *t;
106         
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());
111         
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");
116                 return;
117         }
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");
121                 return;
122         }
123
124         strcpy(browsedir,ct);
125         // make sure we have a trailing / 
126         if(browsedir[strlen(browsedir) - 1] != '/')
127                 strcat(browsedir,"/");
128
129         //if we dont have a file yet, don't try to use it for default dir
130         if(m_bValidFile) {
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);
135
136                 // lop off the file part
137                 t = browsedir + strlen(browsedir) - 1;
138                 while (t != browsedir && *t != '/') 
139                         t--;
140                 *t = 0;
141         }
142         Syn_Printf(MSG_PREFIX "browse directory %s\n",browsedir);
143         
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);
150         if(!newfile) {
151                 Syn_Printf(MSG_PREFIX "newfile = NULL\n");
152                 return;
153         }
154         Syn_Printf(MSG_PREFIX "newfile: %s\n",newfile);
155         newfile = g_FileSystemTable.m_pfnExtractRelativePath(newfile);
156
157         if(!newfile) {
158                 Syn_Printf(MSG_PREFIX "newfile = NULL\n");
159                 return;
160         }
161         Syn_Printf(MSG_PREFIX "newfile: %s\n",newfile);
162
163         if(m_pImage->Load(newfile)) {
164                 m_bValidFile = true;
165                 gtk_label_set_text(GTK_LABEL(m_pFileLabel),newfile);
166         }
167 }
168
169 void CBackgroundDialogPage::SetPosLabel()
170 {
171         char s[64];
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);
176 }
177
178 CBackgroundDialogPage::CBackgroundDialogPage(VIEWTYPE vt )
179 {
180         GtkWidget *frame;
181         GtkWidget *hbox;
182         GtkWidget *w;
183
184         m_vt = vt;
185
186         m_bValidFile = false;
187
188         switch(m_vt)
189         {
190                 case XY:
191                         m_pTabLabel = gtk_label_new("X/Y");
192                         m_pImage = &backgroundXY;
193                         break;
194                 case XZ:
195                         m_pTabLabel = gtk_label_new("X/Z");
196                         m_pImage = &backgroundXZ;
197                         break;
198                 case YZ:
199                         m_pTabLabel = gtk_label_new("Y/Z");
200                         m_pImage = &backgroundYZ;
201                         break;
202         }
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);
208
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);
213
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);
219
220   gtk_widget_show (m_pFileLabel);
221
222         w = gtk_button_new_with_label ("Browse...");
223         g_signal_connect (G_OBJECT (w), "clicked", G_CALLBACK (browse_callback),
224                                                               (gpointer)this);
225         gtk_box_pack_start (GTK_BOX (hbox),w, FALSE, FALSE, 5);
226         gtk_tooltips_set_tip (pTooltips, w, "Select a file", NULL);
227   gtk_widget_show (w);
228         
229         w = gtk_button_new_with_label ("Reload");
230         g_signal_connect (G_OBJECT (w), "clicked", G_CALLBACK (reload_callback),
231                                                               (gpointer)this);
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);
236   gtk_widget_show (w);
237
238         gtk_widget_show (hbox);
239         gtk_widget_show (frame);
240
241 // second row (rendering options)
242         frame = gtk_frame_new("Rendering");
243         gtk_box_pack_start (GTK_BOX (m_pWidget),frame, FALSE, FALSE, 2);
244
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);
248
249         w = gtk_label_new("Vertex alpha:");
250         gtk_box_pack_start (GTK_BOX (hbox),w, FALSE, FALSE, 5);
251   gtk_widget_show (w);
252
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);
260   gtk_widget_show (w);
261
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);
268
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);
272
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),
276                                                                 (gpointer)this);
277         gtk_tooltips_set_tip (pTooltips, w, "Set the size of the image to the bounding rectangle of all selected brushes and entities", NULL);
278   gtk_widget_show (w);
279
280         if(m_vt == XY) {
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),
284                                                                 (gpointer)this);
285                 gtk_tooltips_set_tip (pTooltips, w, "Set the size of the image using the mapcoordsmins and mapcoordsmaxs keys of the worldspawn entity", NULL);
286                 gtk_widget_show (w);
287         }
288
289         gtk_widget_show (hbox);
290         gtk_widget_show (frame);
291
292   gtk_widget_show ( m_pWidget );
293 }
294
295 void CBackgroundDialogPage::Append(GtkWidget *notebook)
296 {
297         gtk_notebook_append_page( GTK_NOTEBOOK(notebook), m_pWidget, m_pTabLabel);
298 }
299
300 // dialog global callbacks
301 /*
302 static gint expose_callback( GtkWidget *widget, gpointer data )
303 {
304         return FALSE;
305 }
306 */
307
308 static void response_callback( GtkWidget *widget, gint response, gpointer data )
309 {
310         if( response == GTK_RESPONSE_CLOSE )
311                 gtk_widget_hide( pDialogWnd );
312 }
313
314 static gint close_callback( GtkWidget *widget, gpointer data )
315 {
316         gtk_widget_hide( pDialogWnd );
317         return TRUE;
318 }
319
320 void InitBackgroundDialog()
321 {
322         CBackgroundDialogPage *pPage;
323
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
328         //                                                                                GTK_STOCK_CLOSE,
329         //                                                                                GTK_RESPONSE_CLOSE,
330                               NULL);
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 );
336
337         pTooltips = gtk_tooltips_new();
338
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);
346
347         gtk_box_pack_start (GTK_BOX (GTK_DIALOG(pDialogWnd)->vbox), pNotebook, TRUE, TRUE, 0);
348
349   gtk_widget_show ( pNotebook );
350         
351         gtk_widget_realize( pDialogWnd );
352 }
353
354 void ShowBackgroundDialog()
355 {
356         gtk_window_present( GTK_WINDOW(pDialogWnd) );
357 }
358
359 void ShowBackgroundDialogPG(int page)
360 {
361         gtk_notebook_set_current_page(GTK_NOTEBOOK(pNotebook),page);
362         ShowBackgroundDialog();
363 }
364