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