Wrap GtkListStore
[xonotic/netradiant.git] / contrib / brushexport / interface.cpp
1 #include <gdk/gdkkeysyms.h>
2 #include <gtk/gtk.h>
3
4 #include "debugging/debugging.h"
5 #include "callbacks.h"
6 #include "support.h"
7
8 #define GLADE_HOOKUP_OBJECT( component,widget,name ) \
9         g_object_set_data_full( G_OBJECT( component ), name, \
10                                                         g_object_ref( widget ), (GDestroyNotify) g_object_unref )
11
12 #define GLADE_HOOKUP_OBJECT_NO_REF( component,widget,name )     \
13         g_object_set_data( G_OBJECT( component ), name, widget )
14
15 // created by glade
16 GtkWidget*
17 create_w_plugplug2( void ){
18         GtkWidget *w_plugplug2;
19         GtkWidget *vbox1;
20         GtkWidget *hbox2;
21         GtkWidget *vbox4;
22         GtkWidget *r_collapse;
23         GSList *r_collapse_group = NULL;
24         GtkWidget *r_collapsebymaterial;
25         GtkWidget *r_nocollapse;
26         GtkWidget *vbox3;
27         GtkWidget *b_export;
28         GtkWidget *b_close;
29         GtkWidget *vbox2;
30         GtkWidget *label1;
31         GtkWidget *scrolledwindow1;
32         GtkWidget *t_materialist;
33         GtkWidget *ed_materialname;
34         GtkWidget *hbox1;
35         GtkWidget *b_addmaterial;
36         GtkWidget *b_removematerial;
37         GtkWidget *t_exportmaterials;
38         GtkWidget *t_limitmatnames;
39         GtkWidget *t_objects;
40
41         w_plugplug2 = ui::Window( ui::window_type::TOP );
42         gtk_widget_set_name( w_plugplug2, "w_plugplug2" );
43         gtk_window_set_title( GTK_WINDOW( w_plugplug2 ), "BrushExport-Plugin 3.0 by namespace" );
44         gtk_window_set_position( GTK_WINDOW( w_plugplug2 ), GTK_WIN_POS_CENTER );
45         gtk_window_set_destroy_with_parent( GTK_WINDOW( w_plugplug2 ), TRUE );
46
47         vbox1 = ui::VBox( FALSE, 0 );
48         gtk_widget_set_name( vbox1, "vbox1" );
49         gtk_widget_show( vbox1 );
50         gtk_container_add( GTK_CONTAINER( w_plugplug2 ), vbox1 );
51         gtk_container_set_border_width( GTK_CONTAINER( vbox1 ), 5 );
52
53         hbox2 = ui::HBox( TRUE, 5 );
54         gtk_widget_set_name( hbox2, "hbox2" );
55         gtk_widget_show( hbox2 );
56         gtk_box_pack_start( GTK_BOX( vbox1 ), hbox2, FALSE, FALSE, 0 );
57         gtk_container_set_border_width( GTK_CONTAINER( hbox2 ), 5 );
58
59         vbox4 = ui::VBox( TRUE, 0 );
60         gtk_widget_set_name( vbox4, "vbox4" );
61         gtk_widget_show( vbox4 );
62         gtk_box_pack_start( GTK_BOX( hbox2 ), vbox4, TRUE, FALSE, 0 );
63
64         r_collapse = gtk_radio_button_new_with_mnemonic( NULL, "Collapse mesh" );
65         gtk_widget_set_name( r_collapse, "r_collapse" );
66         gtk_widget_set_tooltip_text(r_collapse, "Collapse all brushes into a single group");
67         gtk_widget_show( r_collapse );
68         gtk_box_pack_start( GTK_BOX( vbox4 ), r_collapse, FALSE, FALSE, 0 );
69         gtk_radio_button_set_group( GTK_RADIO_BUTTON( r_collapse ), r_collapse_group );
70         r_collapse_group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( r_collapse ) );
71
72         r_collapsebymaterial = gtk_radio_button_new_with_mnemonic( NULL, "Collapse by material" );
73         gtk_widget_set_name( r_collapsebymaterial, "r_collapsebymaterial" );
74         gtk_widget_set_tooltip_text(r_collapsebymaterial, "Collapse into groups by material");
75         gtk_widget_show( r_collapsebymaterial );
76         gtk_box_pack_start( GTK_BOX( vbox4 ), r_collapsebymaterial, FALSE, FALSE, 0 );
77         gtk_radio_button_set_group( GTK_RADIO_BUTTON( r_collapsebymaterial ), r_collapse_group );
78         r_collapse_group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( r_collapsebymaterial ) );
79
80         r_nocollapse = gtk_radio_button_new_with_mnemonic( NULL, "Don't collapse" );
81         gtk_widget_set_name( r_nocollapse, "r_nocollapse" );
82         gtk_widget_set_tooltip_text(r_nocollapse, "Every brush is stored in its own group");
83         gtk_widget_show( r_nocollapse );
84         gtk_box_pack_start( GTK_BOX( vbox4 ), r_nocollapse, FALSE, FALSE, 0 );
85         gtk_radio_button_set_group( GTK_RADIO_BUTTON( r_nocollapse ), r_collapse_group );
86         r_collapse_group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( r_nocollapse ) );
87
88         vbox3 = ui::VBox( FALSE, 0 );
89         gtk_widget_set_name( vbox3, "vbox3" );
90         gtk_widget_show( vbox3 );
91         gtk_box_pack_start( GTK_BOX( hbox2 ), vbox3, FALSE, FALSE, 0 );
92
93         b_export = gtk_button_new_from_stock( "gtk-save" );
94         gtk_widget_set_name( b_export, "b_export" );
95         gtk_widget_show( b_export );
96         gtk_box_pack_start( GTK_BOX( vbox3 ), b_export, TRUE, FALSE, 0 );
97         gtk_container_set_border_width( GTK_CONTAINER( b_export ), 5 );
98
99         b_close = gtk_button_new_from_stock( "gtk-cancel" );
100         gtk_widget_set_name( b_close, "b_close" );
101         gtk_widget_show( b_close );
102         gtk_box_pack_start( GTK_BOX( vbox3 ), b_close, TRUE, FALSE, 0 );
103         gtk_container_set_border_width( GTK_CONTAINER( b_close ), 5 );
104
105         vbox2 = ui::VBox( FALSE, 5 );
106         gtk_widget_set_name( vbox2, "vbox2" );
107         gtk_widget_show( vbox2 );
108         gtk_box_pack_start( GTK_BOX( vbox1 ), vbox2, TRUE, TRUE, 0 );
109         gtk_container_set_border_width( GTK_CONTAINER( vbox2 ), 2 );
110
111         label1 = ui::Label( "Ignored materials:" );
112         gtk_widget_set_name( label1, "label1" );
113         gtk_widget_show( label1 );
114         gtk_box_pack_start( GTK_BOX( vbox2 ), label1, FALSE, FALSE, 0 );
115
116         scrolledwindow1 = ui::ScrolledWindow();
117         gtk_widget_set_name( scrolledwindow1, "scrolledwindow1" );
118         gtk_widget_show( scrolledwindow1 );
119         gtk_box_pack_start( GTK_BOX( vbox2 ), scrolledwindow1, TRUE, TRUE, 0 );
120         gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scrolledwindow1 ), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
121         gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW( scrolledwindow1 ), GTK_SHADOW_IN );
122
123         t_materialist = ui::TreeView();
124         gtk_widget_set_name( t_materialist, "t_materialist" );
125         gtk_widget_show( t_materialist );
126         gtk_container_add( GTK_CONTAINER( scrolledwindow1 ), t_materialist );
127         gtk_tree_view_set_headers_visible( GTK_TREE_VIEW( t_materialist ), FALSE );
128         gtk_tree_view_set_enable_search( GTK_TREE_VIEW( t_materialist ), FALSE );
129
130         ed_materialname = ui::Entry();
131         gtk_widget_set_name( ed_materialname, "ed_materialname" );
132         gtk_widget_show( ed_materialname );
133         gtk_box_pack_start( GTK_BOX( vbox2 ), ed_materialname, FALSE, FALSE, 0 );
134
135         hbox1 = ui::HBox( TRUE, 0 );
136         gtk_widget_set_name( hbox1, "hbox1" );
137         gtk_widget_show( hbox1 );
138         gtk_box_pack_start( GTK_BOX( vbox2 ), hbox1, FALSE, FALSE, 0 );
139
140         b_addmaterial = gtk_button_new_from_stock( "gtk-add" );
141         gtk_widget_set_name( b_addmaterial, "b_addmaterial" );
142         gtk_widget_show( b_addmaterial );
143         gtk_box_pack_start( GTK_BOX( hbox1 ), b_addmaterial, FALSE, FALSE, 0 );
144
145         b_removematerial = gtk_button_new_from_stock( "gtk-remove" );
146         gtk_widget_set_name( b_removematerial, "b_removematerial" );
147         gtk_widget_show( b_removematerial );
148         gtk_box_pack_start( GTK_BOX( hbox1 ), b_removematerial, FALSE, FALSE, 0 );
149
150         t_limitmatnames = gtk_check_button_new_with_mnemonic( "Use short material names (max. 20 chars)" );
151         gtk_widget_set_name( t_limitmatnames, "t_limitmatnames" );
152         gtk_widget_show( t_limitmatnames );
153         gtk_box_pack_end( GTK_BOX( vbox2 ), t_limitmatnames, FALSE, FALSE, 0 );
154
155         t_objects = gtk_check_button_new_with_mnemonic( "Create (o)bjects instead of (g)roups" );
156         gtk_widget_set_name( t_objects, "t_objects" );
157         gtk_widget_show( t_objects );
158         gtk_box_pack_end( GTK_BOX( vbox2 ), t_objects, FALSE, FALSE, 0 );
159
160         t_exportmaterials = gtk_check_button_new_with_mnemonic( "Create material information (.mtl file)" );
161         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( t_exportmaterials ), true );
162         gtk_widget_set_name( t_exportmaterials, "t_exportmaterials" );
163         gtk_widget_show( t_exportmaterials );
164         gtk_box_pack_end( GTK_BOX( vbox2 ), t_exportmaterials, FALSE, FALSE, 10 );
165
166         using namespace callbacks;
167         g_signal_connect( G_OBJECT( w_plugplug2 ), "destroy", G_CALLBACK( OnDestroy ), NULL );
168         g_signal_connect_swapped( G_OBJECT( b_close ), "clicked", G_CALLBACK( OnDestroy ), NULL );
169
170         g_signal_connect( ( gpointer )b_export, "clicked", G_CALLBACK( OnExportClicked ), NULL );
171         g_signal_connect( ( gpointer )b_addmaterial, "clicked", G_CALLBACK( OnAddMaterial ), NULL );
172         g_signal_connect( ( gpointer )b_removematerial, "clicked", G_CALLBACK( OnRemoveMaterial ), NULL );
173         g_signal_connect( ( gpointer )t_exportmaterials, "clicked", G_CALLBACK( OnExportMatClicked ), NULL );
174
175         /* Store pointers to all widgets, for use by lookup_widget(). */
176         GLADE_HOOKUP_OBJECT_NO_REF( w_plugplug2, w_plugplug2, "w_plugplug2" );
177         GLADE_HOOKUP_OBJECT( w_plugplug2, vbox1, "vbox1" );
178         GLADE_HOOKUP_OBJECT( w_plugplug2, hbox2, "hbox2" );
179         GLADE_HOOKUP_OBJECT( w_plugplug2, vbox4, "vbox4" );
180         GLADE_HOOKUP_OBJECT( w_plugplug2, r_collapse, "r_collapse" );
181         GLADE_HOOKUP_OBJECT( w_plugplug2, r_collapsebymaterial, "r_collapsebymaterial" );
182         GLADE_HOOKUP_OBJECT( w_plugplug2, r_nocollapse, "r_nocollapse" );
183         GLADE_HOOKUP_OBJECT( w_plugplug2, vbox3, "vbox3" );
184         GLADE_HOOKUP_OBJECT( w_plugplug2, b_export, "b_export" );
185         GLADE_HOOKUP_OBJECT( w_plugplug2, b_close, "b_close" );
186         GLADE_HOOKUP_OBJECT( w_plugplug2, vbox2, "vbox2" );
187         GLADE_HOOKUP_OBJECT( w_plugplug2, label1, "label1" );
188         GLADE_HOOKUP_OBJECT( w_plugplug2, scrolledwindow1, "scrolledwindow1" );
189         GLADE_HOOKUP_OBJECT( w_plugplug2, t_materialist, "t_materialist" );
190         GLADE_HOOKUP_OBJECT( w_plugplug2, ed_materialname, "ed_materialname" );
191         GLADE_HOOKUP_OBJECT( w_plugplug2, hbox1, "hbox1" );
192         GLADE_HOOKUP_OBJECT( w_plugplug2, b_addmaterial, "b_addmaterial" );
193         GLADE_HOOKUP_OBJECT( w_plugplug2, b_removematerial, "b_removematerial" );
194         GLADE_HOOKUP_OBJECT( w_plugplug2, t_exportmaterials, "t_exportmaterials" );
195         GLADE_HOOKUP_OBJECT( w_plugplug2, t_limitmatnames, "t_limitmatnames" );
196         GLADE_HOOKUP_OBJECT( w_plugplug2, t_objects, "t_objects" );
197
198         return w_plugplug2;
199 }
200
201 // global main window, is 0 when not created
202 GtkWidget* g_brushexp_window = 0;
203
204 // spawn plugin window (and make sure it got destroyed first or never created)
205 void CreateWindow( void ){
206         ASSERT_TRUE( !g_brushexp_window );
207
208         GtkWidget* wnd = create_w_plugplug2();
209
210         // column & renderer
211         GtkTreeViewColumn* col = gtk_tree_view_column_new();
212         gtk_tree_view_column_set_title( col, "materials" );
213         gtk_tree_view_append_column( GTK_TREE_VIEW( lookup_widget( wnd, "t_materialist" ) ), col );
214         auto renderer = ui::CellRendererText();
215         gtk_tree_view_insert_column_with_attributes( GTK_TREE_VIEW( lookup_widget( wnd, "t_materialist" ) ), -1, "", renderer, "text", 0, NULL );
216
217         // list store
218         ui::ListStore ignorelist = ui::ListStore(gtk_list_store_new( 1, G_TYPE_STRING ));
219         gtk_tree_view_set_model( GTK_TREE_VIEW( lookup_widget( wnd, "t_materialist" ) ), GTK_TREE_MODEL( ignorelist ) );
220         g_object_unref( ignorelist );
221
222         gtk_widget_show_all( wnd );
223         g_brushexp_window = wnd;
224 }
225
226 void DestroyWindow( void ){
227         ASSERT_NOTNULL( g_brushexp_window );
228         gtk_widget_destroy( g_brushexp_window );
229         g_brushexp_window = 0;
230 }
231
232 bool IsWindowOpen( void ){
233         return g_brushexp_window != 0;
234 }