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