]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bkgrnd2d/dialog.cpp
uncrustify! now the code is only ugly on the *inside*
[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         ( (CBackgroundDialogPage *)data )->Browse();
72 }
73
74 static void reload_callback( GtkWidget *widget, gpointer data ){
75         ( (CBackgroundDialogPage *)data )->Reload();
76 }
77
78 static void size_sel_callback( GtkWidget *widget, gpointer data ){
79         CBackgroundDialogPage *pPage = (CBackgroundDialogPage *)data;
80         if ( pPage->m_pImage->SetExtentsSel() ) {
81                 pPage->SetPosLabel();
82         }
83 }
84
85 static void size_mm_callback( GtkWidget *widget, gpointer data ){
86         CBackgroundDialogPage *pPage = (CBackgroundDialogPage *)data;
87         if ( pPage->m_pImage->SetExtentsMM() ) {
88                 pPage->SetPosLabel();
89         }
90 }
91
92 static void alpha_adjust_callback( GtkWidget *widget, gpointer data ){
93         CBackgroundDialogPage *pPage = (CBackgroundDialogPage *)data;
94         pPage->m_pImage->m_alpha = (float)gtk_range_get_value( GTK_RANGE( widget ) );
95         g_FuncTable.m_pfnSysUpdateWindows( W_XY );
96 }
97
98 void CBackgroundDialogPage::Reload(){
99         if ( m_bValidFile ) {
100                 m_pImage->Load( gtk_label_get_text( GTK_LABEL( m_pFileLabel ) ) );
101         }
102 }
103
104 void CBackgroundDialogPage::Browse(){
105         char browsedir[PATH_MAX];
106         const char *ct;
107         const char *newfile;
108         char *t;
109
110         //TODO GetMapName saves the map. eeep!
111         //also with no map, returns unnamed.map, otherwise returns full path
112 //      Syn_Printf(MSG_PREFIX "GetMapName() %s\n",
113 //                              g_FuncTable.m_pfnGetMapName());
114
115         ct = g_FuncTable.m_pfnReadProjectKey( "basepath" );
116         // TODO shouldn't need this stuff
117         if ( !ct || !strlen( ct ) ) {
118                 Syn_Printf( MSG_PREFIX "basepath = NULL or empty\n" );
119                 return;
120         }
121         Syn_Printf( MSG_PREFIX "basepath: %s\n",ct );
122         if ( strlen( ct ) >= PATH_MAX ) {
123                 Syn_Printf( MSG_PREFIX "base game dir too long\n" );
124                 return;
125         }
126
127         strcpy( browsedir,ct );
128         // make sure we have a trailing /
129         if ( browsedir[strlen( browsedir ) - 1] != '/' ) {
130                 strcat( browsedir,"/" );
131         }
132
133         //if we dont have a file yet, don't try to use it for default dir
134         if ( m_bValidFile ) {
135                 // filename should always be a nice clean unix style relative path
136                 ct = gtk_label_get_text( GTK_LABEL( m_pFileLabel ) );
137                 strcat( browsedir,ct );
138                 Syn_Printf( MSG_PREFIX "full path: %s\n",browsedir );
139
140                 // lop off the file part
141                 t = browsedir + strlen( browsedir ) - 1;
142                 while ( t != browsedir && *t != '/' )
143                         t--;
144                 *t = 0;
145         }
146         Syn_Printf( MSG_PREFIX "browse directory %s\n",browsedir );
147
148 //does NOT need freeing contrary to include/qerplugin.h comments
149 //TODO bug/patch for comments
150 //TODO patern gets fucked up sometimes if empty
151         newfile = g_FuncTable.m_pfnFileDialog( pDialogWnd,TRUE,
152                                                                                    _( "Load Background Image" ),browsedir,FILETYPE_KEY, NULL );
153         if ( !newfile ) {
154                 Syn_Printf( MSG_PREFIX "newfile = NULL\n" );
155                 return;
156         }
157         Syn_Printf( MSG_PREFIX "newfile: %s\n",newfile );
158         newfile = g_FileSystemTable.m_pfnExtractRelativePath( newfile );
159
160         if ( !newfile ) {
161                 Syn_Printf( MSG_PREFIX "newfile = NULL\n" );
162                 return;
163         }
164         Syn_Printf( MSG_PREFIX "newfile: %s\n",newfile );
165
166         if ( m_pImage->Load( newfile ) ) {
167                 m_bValidFile = true;
168                 gtk_label_set_text( GTK_LABEL( m_pFileLabel ),newfile );
169         }
170 }
171
172 void CBackgroundDialogPage::SetPosLabel(){
173         char s[64];
174         snprintf( s, sizeof( s ) - 1, _( "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         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         gtk_notebook_append_page( GTK_NOTEBOOK( notebook ), m_pWidget, m_pTabLabel );
297 }
298
299 // dialog global callbacks
300 /*
301    static gint expose_callback( GtkWidget *widget, gpointer data )
302    {
303     return FALSE;
304    }
305  */
306
307 static void response_callback( GtkWidget *widget, gint response, gpointer data ){
308         if ( response == GTK_RESPONSE_CLOSE ) {
309                 gtk_widget_hide( pDialogWnd );
310         }
311 }
312
313 static gint close_callback( GtkWidget *widget, gpointer data ){
314         gtk_widget_hide( pDialogWnd );
315         return TRUE;
316 }
317
318 void InitBackgroundDialog(){
319         CBackgroundDialogPage *pPage;
320
321         pDialogWnd = gtk_dialog_new_with_buttons( _( "Background Images" ),
322                                                                                           GTK_WINDOW( g_pMainWidget ),
323                                                                                           (GtkDialogFlags)( GTK_DIALOG_DESTROY_WITH_PARENT ),
324                                                   // TODO dialog with no buttons
325                                                   //                                                                              GTK_STOCK_CLOSE,
326                                                   //                                                                              GTK_RESPONSE_CLOSE,
327                                                                                           NULL );
328         gtk_signal_connect( GTK_OBJECT( pDialogWnd ), "delete_event",
329                                                 GTK_SIGNAL_FUNC( close_callback ), NULL );
330         gtk_signal_connect( GTK_OBJECT( pDialogWnd ), "response",
331                                                 GTK_SIGNAL_FUNC( response_callback ), NULL );
332 //  gtk_signal_connect( GTK_OBJECT (pDialogWnd), "expose_event", GTK_SIGNAL_FUNC( ci_expose ), NULL );
333
334         pTooltips = gtk_tooltips_new();
335
336         pNotebook = gtk_notebook_new();
337         pPage = new CBackgroundDialogPage( XY );
338         pPage->Append( pNotebook );
339         pPage = new CBackgroundDialogPage( XZ );
340         pPage->Append( pNotebook );
341         pPage = new CBackgroundDialogPage( YZ );
342         pPage->Append( pNotebook );
343
344         gtk_box_pack_start( GTK_BOX( GTK_DIALOG( pDialogWnd )->vbox ), pNotebook, TRUE, TRUE, 0 );
345
346         gtk_widget_show( pNotebook );
347
348         gtk_widget_realize( pDialogWnd );
349 }
350
351 void ShowBackgroundDialog(){
352         gtk_window_present( GTK_WINDOW( pDialogWnd ) );
353 }
354
355 void ShowBackgroundDialogPG( int page ){
356         gtk_notebook_set_current_page( GTK_NOTEBOOK( pNotebook ),page );
357         ShowBackgroundDialog();
358 }