]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/prtview/LoadPortalFileDialog.cpp
Merge branch 'mschwan/gdef-inline-fix' into 'master'
[xonotic/netradiant.git] / contrib / prtview / LoadPortalFileDialog.cpp
1 /*
2    PrtView plugin for GtkRadiant
3    Copyright (C) 2001 Geoffrey Dewan, Loki software and qeradiant.com
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 // LoadPortalFileDialog.cpp : implementation file
21 //
22
23 #include "LoadPortalFileDialog.h"
24
25 #include <gtk/gtk.h>
26 #include <gtkutil/pointer.h>
27 #include "stream/stringstream.h"
28 #include "convert.h"
29 #include "gtkutil/pointer.h"
30
31 #include "qerplugin.h"
32
33 #include "prtview.h"
34 #include "portals.h"
35
36 static void dialog_button_callback( ui::Widget widget, gpointer data ){
37         int *loop, *ret;
38
39         auto parent = widget.window();
40         loop = (int*)g_object_get_data( G_OBJECT( parent ), "loop" );
41         ret = (int*)g_object_get_data( G_OBJECT( parent ), "ret" );
42
43         *loop = 0;
44         *ret = gpointer_to_int( data );
45 }
46
47 static gint dialog_delete_callback( ui::Widget widget, GdkEvent* event, gpointer data ){
48         widget.hide();
49         int *loop = (int *) g_object_get_data(G_OBJECT(widget), "loop");
50         *loop = 0;
51         return TRUE;
52 }
53
54 static void change_clicked(ui::Widget widget, gpointer data ){
55         char* filename = NULL;
56
57         auto file_sel = ui::Widget::from(gtk_file_chooser_dialog_new ( "Locate portal (.prt) file", nullptr, GTK_FILE_CHOOSER_ACTION_OPEN,
58                                                                                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
59                                                                                          GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
60                                                                                          nullptr));
61
62         gtk_file_chooser_set_filename( GTK_FILE_CHOOSER(file_sel), portals.fn );
63
64         if (gtk_dialog_run (GTK_DIALOG (file_sel)) == GTK_RESPONSE_ACCEPT)
65         {
66                 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_sel));
67         }
68         ui::Widget(file_sel).destroy();
69
70         if ( filename != NULL ) {
71                 strcpy( portals.fn, filename );
72                 gtk_entry_set_text( GTK_ENTRY( data ), filename );
73                 g_free( filename );
74         }
75 }
76
77 int DoLoadPortalFileDialog(){
78         int loop = 1, ret = IDCANCEL;
79
80         auto dlg = ui::Window( ui::window_type::TOP );
81         gtk_window_set_title( dlg, "Load .prt" );
82         dlg.connect( "delete_event",
83                                                 G_CALLBACK( dialog_delete_callback ), NULL );
84         dlg.connect( "destroy",
85                                                 G_CALLBACK( gtk_widget_destroy ), NULL );
86         g_object_set_data( G_OBJECT( dlg ), "loop", &loop );
87         g_object_set_data( G_OBJECT( dlg ), "ret", &ret );
88
89         auto vbox = ui::VBox( FALSE, 5 );
90         vbox.show();
91         dlg.add(vbox);
92         gtk_container_set_border_width( GTK_CONTAINER( vbox ), 5 );
93
94         auto entry = ui::Entry(ui::New);
95         entry.show();
96         gtk_editable_set_editable( GTK_EDITABLE( entry ), FALSE );
97         vbox.pack_start( entry, FALSE, FALSE, 0 );
98
99         auto hbox = ui::HBox( FALSE, 5 );
100         hbox.show();
101         vbox.pack_start( hbox, FALSE, FALSE, 0 );
102
103         auto check3d = ui::CheckButton( "Show 3D" );
104         check3d.show();
105         hbox.pack_start( check3d, FALSE, FALSE, 0 );
106
107         auto check2d = ui::CheckButton( "Show 2D" );
108         check2d.show();
109         hbox.pack_start( check2d, FALSE, FALSE, 0 );
110
111         auto button = ui::Button( "Change" );
112         button.show();
113         hbox.pack_end(button, FALSE, FALSE, 0);
114         button.connect( "clicked", G_CALLBACK( change_clicked ), entry );
115         button.dimensions(60, -1);
116
117         hbox = ui::HBox( FALSE, 5 );
118         hbox.show();
119         vbox.pack_start( hbox, FALSE, FALSE, 0 );
120
121         button = ui::Button( "Cancel" );
122         button.show();
123         hbox.pack_end(button, FALSE, FALSE, 0);
124         button.connect( "clicked",
125                                                 G_CALLBACK( dialog_button_callback ), GINT_TO_POINTER( IDCANCEL ) );
126         button.dimensions(60, -1);
127
128         button = ui::Button( "OK" );
129         button.show();
130         hbox.pack_end(button, FALSE, FALSE, 0);
131         button.connect( "clicked",
132                                                 G_CALLBACK( dialog_button_callback ), GINT_TO_POINTER( IDOK ) );
133         button.dimensions(60, -1);
134
135         strcpy( portals.fn, GlobalRadiant().getMapName() );
136         char* fn = strrchr( portals.fn, '.' );
137         if ( fn != NULL ) {
138                 strcpy( fn, ".prt" );
139         }
140
141         StringOutputStream value( 256 );
142         value << portals.fn;
143         gtk_entry_set_text( GTK_ENTRY( entry ), value.c_str() );
144         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( check2d ), portals.show_2d );
145         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( check3d ), portals.show_3d );
146
147         gtk_grab_add( dlg );
148         dlg.show();
149
150         while ( loop )
151                 gtk_main_iteration();
152
153         if ( ret == IDOK ) {
154                 portals.Purge();
155
156                 portals.show_3d = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( check3d ) ) ? true : false;
157                 portals.show_2d = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( check2d ) ) ? true : false;
158         }
159
160         gtk_grab_remove( dlg );
161         dlg.destroy();
162
163         return ret;
164 }