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