GTK: wrap GTK_WIDGET
[xonotic/netradiant.git] / contrib / brushexport / callbacks.cpp
1 #include <glib.h>
2 #include <gtk/gtk.h>
3 #include <set>
4
5 #include "qerplugin.h"
6 #include "debugging/debugging.h"
7 #include "support.h"
8 #include "export.h"
9
10 // stuff from interface.cpp
11 void DestroyWindow();
12
13
14 namespace callbacks {
15
16 void OnDestroy( ui::Widget w, gpointer data ){
17         DestroyWindow();
18 }
19
20 void OnExportClicked( ui::Button button, gpointer user_data ){
21         ui::Widget window = lookup_widget( button , "w_plugplug2" );
22         ASSERT_TRUE( window );
23         const char* cpath = GlobalRadiant().m_pfnFileDialog( window, false, "Save as Obj", 0, 0, false, false, true );
24         if ( !cpath ) {
25                 return;
26         }
27
28         std::string path( cpath );
29
30         // get ignore list from ui
31         std::set<std::string> ignore;
32
33         GtkTreeView* view = GTK_TREE_VIEW( lookup_widget( button , "t_materialist" ) );
34         ui::ListStore list = ui::ListStore(GTK_LIST_STORE( gtk_tree_view_get_model( view ) ));
35
36         GtkTreeIter iter;
37         gboolean valid = gtk_tree_model_get_iter_first( GTK_TREE_MODEL( list ), &iter );
38         while ( valid )
39         {
40                 gchar* data;
41                 gtk_tree_model_get( GTK_TREE_MODEL( list ), &iter, 0, &data, -1 );
42                 globalOutputStream() << data << "\n";
43                 ignore.insert( std::string( data ) );
44                 g_free( data );
45                 valid = gtk_tree_model_iter_next( GTK_TREE_MODEL( list ), &iter );
46         }
47
48         for ( std::set<std::string>::iterator it( ignore.begin() ); it != ignore.end(); ++it )
49                 globalOutputStream() << it->c_str() << "\n";
50
51         // collapse mode
52         collapsemode mode = COLLAPSE_NONE;
53
54         GtkWidget* radio = lookup_widget( button , "r_collapse" );
55         ASSERT_NOTNULL( radio );
56
57         if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( radio ) ) ) {
58                 mode = COLLAPSE_ALL;
59         }
60         else
61         {
62                 radio = lookup_widget( button , "r_collapsebymaterial" );
63                 ASSERT_NOTNULL( radio );
64                 if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( radio ) ) ) {
65                         mode = COLLAPSE_BY_MATERIAL;
66                 }
67                 else
68                 {
69                         radio = lookup_widget( button , "r_nocollapse" );
70                         ASSERT_NOTNULL( radio );
71                         ASSERT_TRUE( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( radio ) ) );
72                         mode = COLLAPSE_NONE;
73                 }
74         }
75
76         // export materials?
77         GtkWidget* toggle = lookup_widget( button , "t_exportmaterials" );
78         ASSERT_NOTNULL( toggle );
79
80         bool exportmat = FALSE;
81
82         if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( toggle ) ) ) {
83                 exportmat = TRUE;
84         }
85
86         // limit material names?
87         toggle = lookup_widget( button , "t_limitmatnames" );
88         ASSERT_NOTNULL( toggle );
89
90         bool limitMatNames = FALSE;
91
92         if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( toggle ) ) && exportmat ) {
93                 limitMatNames = TRUE;
94         }
95
96         // create objects instead of groups?
97         toggle = lookup_widget( button , "t_objects" );
98         ASSERT_NOTNULL( toggle );
99
100         bool objects = FALSE;
101
102         if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( toggle ) )  && exportmat ) {
103                 objects = TRUE;
104         }
105
106         // export
107         ExportSelection( ignore, mode, exportmat, path, limitMatNames, objects );
108 }
109
110 void OnAddMaterial( ui::Button button, gpointer user_data ){
111         GtkEntry* edit = GTK_ENTRY( lookup_widget( button , "ed_materialname" ) );
112         ASSERT_NOTNULL( edit );
113
114         const gchar* name = gtk_entry_get_text( edit );
115         if ( g_utf8_strlen( name, -1 ) > 0 ) {
116                 ui::ListStore list = ui::ListStore(GTK_LIST_STORE( gtk_tree_view_get_model( GTK_TREE_VIEW( lookup_widget( button , "t_materialist" ) ) ) ));
117                 list.append(0, name);
118                 gtk_entry_set_text( edit, "" );
119         }
120 }
121
122 void OnRemoveMaterial( ui::Button button, gpointer user_data ){
123         GtkTreeView* view = GTK_TREE_VIEW( lookup_widget( button , "t_materialist" ) );
124         ui::ListStore list = ui::ListStore(GTK_LIST_STORE( gtk_tree_view_get_model( view ) ));
125         GtkTreeSelection* sel = gtk_tree_view_get_selection( view );
126
127         GtkTreeIter iter;
128         if ( gtk_tree_selection_get_selected( sel, 0, &iter ) ) {
129                 gtk_list_store_remove( list, &iter );
130         }
131 }
132
133 void OnExportMatClicked( ui::Button button, gpointer user_data ){
134         GtkWidget* toggleLimit = lookup_widget( button , "t_limitmatnames" );
135         GtkWidget* toggleObject = lookup_widget( button , "t_objects" );
136
137         if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( button ) ) ) {
138                 gtk_widget_set_sensitive( toggleLimit , TRUE );
139                 gtk_widget_set_sensitive( toggleObject , TRUE );
140         }
141         else {
142                 gtk_widget_set_sensitive( toggleLimit , FALSE );
143                 gtk_widget_set_sensitive( toggleObject , FALSE );
144         }
145 }
146
147 } // callbacks