Fixing Linux SCons build.
[xonotic/netradiant.git] / contrib / bkgrnd2d / dialog.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 // bkgrnd2d Plugin dialog
24 //
25 // Code by reyalP aka Reed Mideke
26 //
27 // Based on various other plugins
28 //
29
30 #include <gtk/gtk.h>
31
32 #include "bkgrnd2d.h"
33 #include "dialog.h"
34 #include <glib/gi18n.h>
35
36 // spaces to make label nice and big
37 #define NO_FILE_MSG "        (no file loaded)        "
38
39 #ifdef _WIN32
40   // TTimo: THIS IS UGLY
41 #define snprintf _snprintf
42 #endif
43
44 static GtkWidget *pDialogWnd;
45 static GtkWidget *pNotebook;
46 static GtkTooltips *pTooltips;
47
48 class CBackgroundDialogPage
49 {
50 private:
51         GtkWidget *m_pWidget;
52         GtkWidget *m_pTabLabel;
53         GtkWidget *m_pFileLabel;
54         GtkWidget *m_pPosLabel;
55         VIEWTYPE m_vt;
56         bool m_bValidFile;
57
58 public:
59         CBackgroundImage *m_pImage;
60         CBackgroundDialogPage( VIEWTYPE vt );
61         void Append(GtkWidget *notebook);
62         void Browse();
63         void Reload();
64         void SetPosLabel();
65 //  ~BackgroundDialogPage();
66 };
67
68
69 // dialog page callbacks
70 static void browse_callback( GtkWidget *widget, gpointer data )
71 {
72         ((CBackgroundDialogPage *)data)->Browse();
73 }
74
75 static void reload_callback( GtkWidget *widget, gpointer data )
76 {
77         ((CBackgroundDialogPage *)data)->Reload();
78 }
79
80 static void size_sel_callback( GtkWidget *widget, gpointer data )
81 {
82         CBackgroundDialogPage *pPage = (CBackgroundDialogPage *)data;
83         if (pPage->m_pImage->SetExtentsSel())
84                 pPage->SetPosLabel();
85 }
86
87 static void size_mm_callback( GtkWidget *widget, gpointer data )
88 {
89         CBackgroundDialogPage *pPage = (CBackgroundDialogPage *)data;
90         if(pPage->m_pImage->SetExtentsMM())
91                 pPage->SetPosLabel();
92 }
93
94 static void alpha_adjust_callback( GtkWidget *widget, gpointer data )
95 {
96         CBackgroundDialogPage *pPage = (CBackgroundDialogPage *)data;
97         pPage->m_pImage->m_alpha = (float)gtk_range_get_value (GTK_RANGE(widget));
98         g_FuncTable.m_pfnSysUpdateWindows(W_XY);
99 }
100
101 void CBackgroundDialogPage::Reload()
102 {
103         if(m_bValidFile)
104                 m_pImage->Load(gtk_label_get_text(GTK_LABEL(m_pFileLabel)));
105 }
106
107 void CBackgroundDialogPage::Browse()
108 {
109         char browsedir[PATH_MAX];
110         const char *ct;
111         const char *newfile;
112         char *t;
113
114         //TODO GetMapName saves the map. eeep!
115         //also with no map, returns unnamed.map, otherwise returns full path
116 //      Syn_Printf(MSG_PREFIX "GetMapName() %s\n",
117 //                              g_FuncTable.m_pfnGetMapName());
118
119         ct = g_FuncTable.m_pfnReadProjectKey("basepath");
120         // TODO shouldn't need this stuff
121         if(!ct || !strlen(ct)) {
122                 Syn_Printf(MSG_PREFIX "basepath = NULL or empty\n");
123                 return;
124         }
125         Syn_Printf(MSG_PREFIX "basepath: %s\n",ct);
126         if(strlen(ct) >= PATH_MAX) {
127                 Syn_Printf(MSG_PREFIX "base game dir too long\n");
128                 return;
129         }
130
131         strcpy(browsedir,ct);
132         // make sure we have a trailing /
133         if(browsedir[strlen(browsedir) - 1] != '/')
134                 strcat(browsedir,"/");
135
136         //if we dont have a file yet, don't try to use it for default dir
137         if(m_bValidFile) {
138         // filename should always be a nice clean unix style relative path
139                 ct = gtk_label_get_text(GTK_LABEL(m_pFileLabel));
140                 strcat(browsedir,ct);
141                 Syn_Printf(MSG_PREFIX "full path: %s\n",browsedir);
142
143                 // lop off the file part
144                 t = browsedir + strlen(browsedir) - 1;
145                 while (t != browsedir && *t != '/')
146                         t--;
147                 *t = 0;
148         }
149         Syn_Printf(MSG_PREFIX "browse directory %s\n",browsedir);
150
151 //does NOT need freeing contrary to include/qerplugin.h comments
152 //TODO bug/patch for comments
153 //TODO patern gets fucked up sometimes if empty
154         newfile = g_FuncTable.m_pfnFileDialog(pDialogWnd,TRUE,
155                                                                   _("Load Background Image"),browsedir,FILETYPE_KEY, NULL);
156         if(!newfile) {
157                 Syn_Printf(MSG_PREFIX "newfile = NULL\n");
158                 return;
159         }
160         Syn_Printf(MSG_PREFIX "newfile: %s\n",newfile);
161         newfile = g_FileSystemTable.m_pfnExtractRelativePath(newfile);
162
163         if(!newfile) {
164                 Syn_Printf(MSG_PREFIX "newfile = NULL\n");
165                 return;
166         }
167         Syn_Printf(MSG_PREFIX "newfile: %s\n",newfile);
168
169         if(m_pImage->Load(newfile)) {
170                 m_bValidFile = true;
171                 gtk_label_set_text(GTK_LABEL(m_pFileLabel),newfile);
172         }
173 }
174
175 void CBackgroundDialogPage::SetPosLabel()
176 {
177         char s[64];
178         snprintf(s, sizeof(s) - 1, _("Size/Position (%d,%d) (%d,%d)"),(int)(m_pImage->m_xmin),
179                         (int)(m_pImage->m_ymin),(int)(m_pImage->m_xmax),(int)(m_pImage->m_ymax));
180         gtk_label_set_text(GTK_LABEL(m_pPosLabel),s);
181 }
182
183 CBackgroundDialogPage::CBackgroundDialogPage(VIEWTYPE vt )
184 {
185         GtkWidget *frame;
186         GtkWidget *hbox;
187         GtkWidget *w;
188
189         m_vt = vt;
190
191         m_bValidFile = false;
192
193         switch(m_vt)
194         {
195                 case XY:
196                         m_pTabLabel = gtk_label_new(_("X/Y"));
197                         m_pImage = &backgroundXY;
198                         break;
199                 case XZ:
200                         m_pTabLabel = gtk_label_new(_("X/Z"));
201                         m_pImage = &backgroundXZ;
202                         break;
203                 case YZ:
204                         m_pTabLabel = gtk_label_new(_("Y/Z"));
205                         m_pImage = &backgroundYZ;
206                         break;
207         }
208 // A vbox to hold everything
209         m_pWidget = gtk_vbox_new(FALSE,0);
210 // Frame for file row
211         frame = gtk_frame_new(_("File"));
212         gtk_box_pack_start (GTK_BOX (m_pWidget),frame, FALSE, FALSE, 2);
213
214 // hbox for first row
215         hbox = gtk_hbox_new(FALSE,5);
216         gtk_container_set_border_width(GTK_CONTAINER (hbox),4);
217         gtk_container_add (GTK_CONTAINER (frame), hbox);
218
219 // label to display filename
220         m_pFileLabel  = gtk_label_new(NO_FILE_MSG);
221         gtk_label_set_selectable(GTK_LABEL(m_pFileLabel),TRUE);
222 //TODO set min size ? done with spaces right now
223         gtk_box_pack_start (GTK_BOX (hbox),m_pFileLabel, TRUE, TRUE, 5);
224
225   gtk_widget_show (m_pFileLabel);
226
227         w = gtk_button_new_with_label ("Browse...");
228         g_signal_connect (G_OBJECT (w), "clicked", G_CALLBACK (browse_callback),
229                                                               (gpointer)this);
230         gtk_box_pack_start (GTK_BOX (hbox),w, FALSE, FALSE, 5);
231         gtk_tooltips_set_tip (pTooltips, w, "Select a file", NULL);
232   gtk_widget_show (w);
233
234         w = gtk_button_new_with_label ("Reload");
235         g_signal_connect (G_OBJECT (w), "clicked", G_CALLBACK (reload_callback),
236                                                               (gpointer)this);
237         // TODO disable until we have file
238         // gtk_widget_set_sensitive(w,FALSE);
239         gtk_tooltips_set_tip (pTooltips, w, "Reload current file", NULL);
240         gtk_box_pack_start (GTK_BOX (hbox),w, FALSE, FALSE, 5);
241   gtk_widget_show (w);
242
243         gtk_widget_show (hbox);
244         gtk_widget_show (frame);
245
246 // second row (rendering options)
247         frame = gtk_frame_new(_("Rendering"));
248         gtk_box_pack_start (GTK_BOX (m_pWidget),frame, FALSE, FALSE, 2);
249
250         hbox = gtk_hbox_new(FALSE,5);
251         gtk_container_set_border_width(GTK_CONTAINER (hbox),4);
252         gtk_container_add (GTK_CONTAINER (frame), hbox);
253
254         w = gtk_label_new(_("Vertex alpha:"));
255         gtk_box_pack_start (GTK_BOX (hbox),w, FALSE, FALSE, 5);
256   gtk_widget_show (w);
257
258         w = gtk_hscale_new_with_range(0.0,1.0,0.01);
259         gtk_range_set_value(GTK_RANGE(w),0.5);
260         gtk_scale_set_value_pos(GTK_SCALE(w),GTK_POS_LEFT);
261         g_signal_connect (G_OBJECT (w), "value-changed",
262                           G_CALLBACK (alpha_adjust_callback), (gpointer)this);
263         gtk_box_pack_start (GTK_BOX (hbox),w, TRUE, TRUE, 5);
264         gtk_tooltips_set_tip (pTooltips, w, _("Set image transparancy"), NULL);
265   gtk_widget_show (w);
266
267         gtk_widget_show (hbox);
268         gtk_widget_show (frame);
269 // Third row (size and position)
270         frame = gtk_frame_new(_("Size/Position (undefined)"));
271         m_pPosLabel = gtk_frame_get_label_widget (GTK_FRAME(frame));
272         gtk_box_pack_start ( GTK_BOX (m_pWidget), frame, FALSE, FALSE, 2);
273
274         hbox = gtk_hbox_new(FALSE,5);
275         gtk_container_add (GTK_CONTAINER (frame), hbox);
276         gtk_container_set_border_width(GTK_CONTAINER (hbox),4);
277
278         w = gtk_button_new_with_label (_("from selection"));
279         gtk_box_pack_start (GTK_BOX (hbox),w, TRUE, FALSE, 5);
280         g_signal_connect (G_OBJECT (w), "clicked", G_CALLBACK (size_sel_callback),
281                                                                 (gpointer)this);
282         gtk_tooltips_set_tip (pTooltips, w, _("Set the size of the image to the bounding rectangle of all selected brushes and entities"), NULL);
283   gtk_widget_show (w);
284
285         if(m_vt == XY) {
286                 w = gtk_button_new_with_label (_("from map mins/maxs"));
287                 gtk_box_pack_start ( GTK_BOX (hbox),w, TRUE, FALSE, 2);
288                 g_signal_connect (G_OBJECT (w), "clicked", G_CALLBACK (size_mm_callback),
289                                                                 (gpointer)this);
290                 gtk_tooltips_set_tip (pTooltips, w, _("Set the size of the image using the mapcoordsmins and mapcoordsmaxs keys of the worldspawn entity"), NULL);
291                 gtk_widget_show (w);
292         }
293
294         gtk_widget_show (hbox);
295         gtk_widget_show (frame);
296
297   gtk_widget_show ( m_pWidget );
298 }
299
300 void CBackgroundDialogPage::Append(GtkWidget *notebook)
301 {
302         gtk_notebook_append_page( GTK_NOTEBOOK(notebook), m_pWidget, m_pTabLabel);
303 }
304
305 // dialog global callbacks
306 /*
307 static gint expose_callback( GtkWidget *widget, gpointer data )
308 {
309         return FALSE;
310 }
311 */
312
313 static void response_callback( GtkWidget *widget, gint response, gpointer data )
314 {
315         if( response == GTK_RESPONSE_CLOSE )
316                 gtk_widget_hide( pDialogWnd );
317 }
318
319 static gint close_callback( GtkWidget *widget, gpointer data )
320 {
321         gtk_widget_hide( pDialogWnd );
322         return TRUE;
323 }
324
325 void InitBackgroundDialog()
326 {
327         CBackgroundDialogPage *pPage;
328
329         pDialogWnd = gtk_dialog_new_with_buttons (_("Background Images"),
330                               GTK_WINDOW(g_pMainWidget),
331                               (GtkDialogFlags)(GTK_DIALOG_DESTROY_WITH_PARENT),
332         // TODO dialog with no buttons
333         //                                                                                GTK_STOCK_CLOSE,
334         //                                                                                GTK_RESPONSE_CLOSE,
335                               NULL);
336         gtk_signal_connect( GTK_OBJECT (pDialogWnd), "delete_event",
337                                     GTK_SIGNAL_FUNC( close_callback ), NULL );
338         gtk_signal_connect( GTK_OBJECT (pDialogWnd), "response",
339                                   GTK_SIGNAL_FUNC( response_callback ), NULL );
340 //      gtk_signal_connect( GTK_OBJECT (pDialogWnd), "expose_event", GTK_SIGNAL_FUNC( ci_expose ), NULL );
341
342         pTooltips = gtk_tooltips_new();
343
344         pNotebook = gtk_notebook_new();
345         pPage = new CBackgroundDialogPage(XY);
346         pPage->Append(pNotebook);
347         pPage = new CBackgroundDialogPage(XZ);
348         pPage->Append(pNotebook);
349         pPage = new CBackgroundDialogPage(YZ);
350         pPage->Append(pNotebook);
351
352         gtk_box_pack_start (GTK_BOX (GTK_DIALOG(pDialogWnd)->vbox), pNotebook, TRUE, TRUE, 0);
353
354   gtk_widget_show ( pNotebook );
355
356         gtk_widget_realize( pDialogWnd );
357 }
358
359 void ShowBackgroundDialog()
360 {
361         gtk_window_present( GTK_WINDOW(pDialogWnd) );
362 }
363
364 void ShowBackgroundDialogPG(int page)
365 {
366         gtk_notebook_set_current_page(GTK_NOTEBOOK(pNotebook),page);
367         ShowBackgroundDialog();
368 }
369