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