| 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 ui::Window pDialogWnd{ui::null}; |
| 38 | static GtkWidget *pNotebook; |
| 39 | static GtkTooltips *pTooltips; |
| 40 | |
| 41 | class CBackgroundDialogPage |
| 42 | { |
| 43 | private: |
| 44 | GtkWidget *m_pWidget; |
| 45 | GtkWidget *m_pTabLabel; |
| 46 | ui::Label 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 | m_pFileLabel.text(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 | m_pPosLabel.text(s); |
| 172 | } |
| 173 | |
| 174 | CBackgroundDialogPage::CBackgroundDialogPage( VIEWTYPE vt ){ |
| 175 | GtkWidget *w; |
| 176 | |
| 177 | m_vt = vt; |
| 178 | |
| 179 | m_bValidFile = false; |
| 180 | |
| 181 | switch ( m_vt ) |
| 182 | { |
| 183 | case XY: |
| 184 | m_pTabLabel = ui::Label( "X/Y" ); |
| 185 | m_pImage = &backgroundXY; |
| 186 | break; |
| 187 | case XZ: |
| 188 | m_pTabLabel = ui::Label( "X/Z" ); |
| 189 | m_pImage = &backgroundXZ; |
| 190 | break; |
| 191 | case YZ: |
| 192 | m_pTabLabel = ui::Label( "Y/Z" ); |
| 193 | m_pImage = &backgroundYZ; |
| 194 | break; |
| 195 | } |
| 196 | // A vbox to hold everything |
| 197 | m_pWidget = ui::VBox( FALSE,0 ); |
| 198 | // Frame for file row |
| 199 | auto frame = ui::Frame( "File" ); |
| 200 | m_pWidget.pack_start(frame, FALSE, FALSE, 2 ); |
| 201 | |
| 202 | // hbox for first row |
| 203 | auto hbox = ui::HBox( FALSE,5 ); |
| 204 | gtk_container_set_border_width( GTK_CONTAINER( hbox ),4 ); |
| 205 | frame.add(hbox); |
| 206 | |
| 207 | // label to display filename |
| 208 | m_pFileLabel = ui::Label( NO_FILE_MSG ); |
| 209 | gtk_label_set_selectable( GTK_LABEL( m_pFileLabel ),TRUE ); |
| 210 | //TODO set min size ? done with spaces right now |
| 211 | hbox.pack_start(m_pFileLabel, TRUE, TRUE, 5 ); |
| 212 | |
| 213 | m_pFileLabel.show(); |
| 214 | |
| 215 | w = ui::Button( "Browse..." ); |
| 216 | w.connect( "clicked", G_CALLBACK( browse_callback ), ( gpointer ) this ); |
| 217 | hbox.pack_start(w, FALSE, FALSE, 5 ); |
| 218 | gtk_tooltips_set_tip( pTooltips, w, "Select a file", NULL ); |
| 219 | w.show(); |
| 220 | |
| 221 | w = ui::Button( "Reload" ); |
| 222 | w.connect( "clicked", G_CALLBACK( reload_callback ), ( gpointer ) this ); |
| 223 | // TODO disable until we have file |
| 224 | // gtk_widget_set_sensitive(w,FALSE); |
| 225 | gtk_tooltips_set_tip( pTooltips, w, "Reload current file", NULL ); |
| 226 | hbox.pack_start(w, FALSE, FALSE, 5 ); |
| 227 | w.show(); |
| 228 | |
| 229 | hbox.show(); |
| 230 | frame.show(); |
| 231 | |
| 232 | // second row (rendering options) |
| 233 | frame = ui::Frame( "Rendering" ); |
| 234 | m_pWidget.pack_start(frame, FALSE, FALSE, 2 ); |
| 235 | |
| 236 | hbox = ui::HBox( FALSE,5 ); |
| 237 | gtk_container_set_border_width( GTK_CONTAINER( hbox ),4 ); |
| 238 | frame.add(hbox); |
| 239 | |
| 240 | w = ui::Label( "Vertex alpha:" ); |
| 241 | hbox.pack_start(w, FALSE, FALSE, 5 ); |
| 242 | w.show(); |
| 243 | |
| 244 | w = ui::HScale( 0.0, 1.0, 0.01 ); |
| 245 | gtk_range_set_value( GTK_RANGE( w ),0.5 ); |
| 246 | gtk_scale_set_value_pos( GTK_SCALE( w ),GTK_POS_LEFT ); |
| 247 | w.connect( "value-changed", G_CALLBACK( alpha_adjust_callback ), ( gpointer ) this ); |
| 248 | hbox.pack_start(w, TRUE, TRUE, 5 ); |
| 249 | gtk_tooltips_set_tip( pTooltips, w, "Set image transparancy", NULL ); |
| 250 | w.show(); |
| 251 | |
| 252 | hbox.show(); |
| 253 | frame.show(); |
| 254 | // Third row (size and position) |
| 255 | frame = ui::Frame( "Size/Position (undefined)" ); |
| 256 | m_pPosLabel = gtk_frame_get_label_widget( GTK_FRAME( frame ) ); |
| 257 | m_pWidget.pack_start( frame, FALSE, FALSE, 2 ); |
| 258 | |
| 259 | hbox = ui::HBox( FALSE,5 ); |
| 260 | frame.add(hbox); |
| 261 | gtk_container_set_border_width( GTK_CONTAINER( hbox ),4 ); |
| 262 | |
| 263 | w = ui::Button( "from selection" ); |
| 264 | hbox.pack_start(w, TRUE, FALSE, 5 ); |
| 265 | w.connect( "clicked", G_CALLBACK( size_sel_callback ), ( gpointer ) this ); |
| 266 | gtk_tooltips_set_tip( pTooltips, w, "Set the size of the image to the bounding rectangle of all selected brushes and entities", NULL ); |
| 267 | w.show(); |
| 268 | |
| 269 | if ( m_vt == XY ) { |
| 270 | w = ui::Button( "from map mins/maxs" ); |
| 271 | hbox.pack_start(w, TRUE, FALSE, 2 ); |
| 272 | w.connect( "clicked", G_CALLBACK( size_mm_callback ), ( gpointer ) this ); |
| 273 | gtk_tooltips_set_tip( pTooltips, w, "Set the size of the image using the mapcoordsmins and mapcoordsmaxs keys of the worldspawn entity", NULL ); |
| 274 | w.show(); |
| 275 | } |
| 276 | |
| 277 | hbox.show(); |
| 278 | frame.show(); |
| 279 | |
| 280 | m_pWidget.show(); |
| 281 | } |
| 282 | |
| 283 | void CBackgroundDialogPage::Append( GtkWidget *notebook ){ |
| 284 | gtk_notebook_append_page( GTK_NOTEBOOK( notebook ), m_pWidget, m_pTabLabel ); |
| 285 | } |
| 286 | |
| 287 | // dialog global callbacks |
| 288 | /* |
| 289 | static gint expose_callback( GtkWidget *widget, gpointer data ) |
| 290 | { |
| 291 | return FALSE; |
| 292 | } |
| 293 | */ |
| 294 | |
| 295 | static void response_callback( GtkWidget *widget, gint response, gpointer data ){ |
| 296 | if ( response == GTK_RESPONSE_CLOSE ) { |
| 297 | gtk_widget_hide( pDialogWnd ); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | static gint close_callback( GtkWidget *widget, gpointer data ){ |
| 302 | gtk_widget_hide( pDialogWnd ); |
| 303 | return TRUE; |
| 304 | } |
| 305 | |
| 306 | void InitBackgroundDialog(){ |
| 307 | CBackgroundDialogPage *pPage; |
| 308 | |
| 309 | pDialogWnd = gtk_dialog_new_with_buttons( "Background Images", |
| 310 | g_pMainWidget, |
| 311 | (GtkDialogFlags)( GTK_DIALOG_DESTROY_WITH_PARENT ), |
| 312 | // TODO dialog with no buttons |
| 313 | // GTK_STOCK_CLOSE, |
| 314 | // GTK_RESPONSE_CLOSE, |
| 315 | NULL ); |
| 316 | pDialogWnd.connect( "delete_event", G_CALLBACK( close_callback ), NULL ); |
| 317 | pDialogWnd.connect( "response", G_CALLBACK( response_callback ), NULL ); |
| 318 | // pDialogWnd.connect( "expose_event", G_CALLBACK( ci_expose ), NULL ); |
| 319 | |
| 320 | pTooltips = gtk_tooltips_new(); |
| 321 | |
| 322 | pNotebook = gtk_notebook_new(); |
| 323 | pPage = new CBackgroundDialogPage( XY ); |
| 324 | pPage->Append( pNotebook ); |
| 325 | pPage = new CBackgroundDialogPage( XZ ); |
| 326 | pPage->Append( pNotebook ); |
| 327 | pPage = new CBackgroundDialogPage( YZ ); |
| 328 | pPage->Append( pNotebook ); |
| 329 | |
| 330 | gtk_box_pack_start( GTK_BOX( GTK_DIALOG( pDialogWnd )->vbox ), pNotebook, TRUE, TRUE, 0 ); |
| 331 | |
| 332 | pNotebook.show(); |
| 333 | |
| 334 | gtk_widget_realize( pDialogWnd ); |
| 335 | } |
| 336 | |
| 337 | void ShowBackgroundDialog(){ |
| 338 | gtk_window_present( pDialogWnd ); |
| 339 | } |
| 340 | |
| 341 | void ShowBackgroundDialogPG( int page ){ |
| 342 | gtk_notebook_set_current_page( GTK_NOTEBOOK( pNotebook ),page ); |
| 343 | ShowBackgroundDialog(); |
| 344 | } |