CEBL  2.1
DataSourceCombo.cpp
Go to the documentation of this file.
1 
9 #include "CEBLViewGTK.hpp"
10 
11 #include "DataSourceCombo.hpp"
12 #include "cppR/cppR.hpp"
13 #include "Exceptions.hpp"
14 using namespace std;
15 
16 
17 //constructor
19 {
20  this->view = view;
21  this->updating_view = false;
22 }
23 
25 {
26 
27 }
28 
29 
30 //create a new combo widget
32 {
33  GtkWidget* combo = gtk_combo_box_new_text();
34 
35  this->combos.push_back(combo);
36  this->num_sources.push_back(0);
37  this->updateComboBox(combos.size()-1);
38 
39  g_signal_connect(G_OBJECT(combo),
40  "changed",
41  G_CALLBACK(CB_changeSource),
42  (gpointer) this);
43  return combo;
44 }
45 
46 //update the view from the model
48 {
49  this->updating_view = true;
50  for(unsigned int i=0;i<combos.size();i++)
51  {
52  this->updateComboBox(i);
53  }
54  this->updating_view = false;
55 }
56 
57 
58 //update a combo box from the model
59 void DataSourceCombo::updateComboBox(int index)
60 {
61  GtkWidget * combo = combos[index];
62  int num_sources = this->num_sources[index];
63 
64  //remove all the previous sources
65  for(int i=num_sources-1;i>=0;i--)
66  {
67  gtk_combo_box_remove_text(GTK_COMBO_BOX(combo),i);
68  }
69 
70  //get sources
71  //re-add removed sources to combo box
72  //find and store selected source and activate
73  std::vector<string> sources = view->getModel()->dataGetSources();
74  num_sources = sources.size();
75  int selected = view->getModel()->dataGetSource();
76  for(int i=0;i<num_sources;i++)
77  {
78  gtk_combo_box_append_text(GTK_COMBO_BOX(combo),sources[i].c_str());
79  if(i == selected)
80  this->selected_source = sources[i];
81  }
82 
83  gtk_combo_box_set_active(GTK_COMBO_BOX(combo),selected);
84 
85  this->num_sources[index] = num_sources;
86 
87 }
88 
89 //returns the name of the selected source
91 {
92  return this->selected_source;
93 }
94 
95 //CALLBACKS
96 
97 /*
98  * Sets data source on combo box event
99  */
100 void DataSourceCombo::CB_changeSource(GtkWidget * w, gpointer data)
101 {
102 
103  DataSourceCombo * d = (DataSourceCombo*)data;
104  if(!d->updating_view)
105  {
106  //get source number and its string
107  int source = gtk_combo_box_get_active(GTK_COMBO_BOX(w));
108  d->selected_source = gtk_combo_box_get_active_text(GTK_COMBO_BOX(w));
109 
110  //set the source and update the gui to reflect selection
111  // (combo box and data source tab)
112  d->view->getModel()->dataSetSource(source);
113  d->view->updateView();
114  d->updateView();
115  }
116 }
117