]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/prtview/LoadPortalFileDialog.cpp
890658e14733d2929a1d9c627da74ddfe0aad447
[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( GtkWidget *widget, gpointer data ){
55         GtkWidget* file_sel;
56         char* filename = NULL;
57
58         file_sel = gtk_file_chooser_dialog_new ( "Locate portal (.prt) file", nullptr, GTK_FILE_CHOOSER_ACTION_OPEN,
59                                                                                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
60                                                                                          GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
61                                                                                          nullptr);
62
63         gtk_file_chooser_set_filename( GTK_FILE_CHOOSER(file_sel), portals.fn );
64
65         if (gtk_dialog_run (GTK_DIALOG (file_sel)) == GTK_RESPONSE_ACCEPT)
66         {
67                 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_sel));
68         }
69         ui::Widget(file_sel).destroy();
70
71         if ( filename != NULL ) {
72                 strcpy( portals.fn, filename );
73                 gtk_entry_set_text( GTK_ENTRY( data ), filename );
74                 g_free( filename );
75         }
76 }
77
78 int DoLoadPortalFileDialog(){
79         int loop = 1, ret = IDCANCEL;
80
81         auto dlg = ui::Window( ui::window_type::TOP );
82         gtk_window_set_title( dlg, "Load .prt" );
83         dlg.connect( "delete_event",
84                                                 G_CALLBACK( dialog_delete_callback ), NULL );
85         dlg.connect( "destroy",
86                                                 G_CALLBACK( gtk_widget_destroy ), NULL );
87         g_object_set_data( G_OBJECT( dlg ), "loop", &loop );
88         g_object_set_data( G_OBJECT( dlg ), "ret", &ret );
89
90         auto vbox = ui::VBox( FALSE, 5 );
91         vbox.show();
92         dlg.add(vbox);
93         gtk_container_set_border_width( GTK_CONTAINER( vbox ), 5 );
94
95         auto entry = ui::Entry(ui::New);
96         entry.show();
97         gtk_editable_set_editable( GTK_EDITABLE( entry ), FALSE );
98         vbox.pack_start( entry, FALSE, FALSE, 0 );
99
100         auto hbox = ui::HBox( FALSE, 5 );
101         hbox.show();
102         vbox.pack_start( hbox, FALSE, FALSE, 0 );
103
104         auto check3d = ui::CheckButton( "Show 3D" );
105         check3d.show();
106         hbox.pack_start( check3d, FALSE, FALSE, 0 );
107
108         auto check2d = ui::CheckButton( "Show 2D" );
109         check2d.show();
110         hbox.pack_start( check2d, FALSE, FALSE, 0 );
111
112         auto button = ui::Button( "Change" );
113         button.show();
114         hbox.pack_end(button, FALSE, FALSE, 0);
115         button.connect( "clicked", G_CALLBACK( change_clicked ), entry );
116         button.dimensions(60, -1);
117
118         hbox = ui::HBox( FALSE, 5 );
119         hbox.show();
120         vbox.pack_start( hbox, FALSE, FALSE, 0 );
121
122         button = ui::Button( "Cancel" );
123         button.show();
124         hbox.pack_end(button, FALSE, FALSE, 0);
125         button.connect( "clicked",
126                                                 G_CALLBACK( dialog_button_callback ), GINT_TO_POINTER( IDCANCEL ) );
127         button.dimensions(60, -1);
128
129         button = ui::Button( "OK" );
130         button.show();
131         hbox.pack_end(button, FALSE, FALSE, 0);
132         button.connect( "clicked",
133                                                 G_CALLBACK( dialog_button_callback ), GINT_TO_POINTER( IDOK ) );
134         button.dimensions(60, -1);
135
136         strcpy( portals.fn, GlobalRadiant().getMapName() );
137         char* fn = strrchr( portals.fn, '.' );
138         if ( fn != NULL ) {
139                 strcpy( fn, ".prt" );
140         }
141
142         StringOutputStream value( 256 );
143         value << portals.fn;
144         gtk_entry_set_text( GTK_ENTRY( entry ), value.c_str() );
145         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( check2d ), portals.show_2d );
146         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( check3d ), portals.show_3d );
147
148         gtk_grab_add( dlg );
149         dlg.show();
150
151         while ( loop )
152                 gtk_main_iteration();
153
154         if ( ret == IDOK ) {
155                 portals.Purge();
156
157                 portals.show_3d = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( check3d ) ) ? true : false;
158                 portals.show_2d = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( check2d ) ) ? true : false;
159         }
160
161         gtk_grab_remove( dlg );
162         dlg.destroy();
163
164         return ret;
165 }