GTK+ IOStream  Beta
<< GTK+ >> add C++ IOStream operators to GTK+. Now with extra abilities ... like network serialisation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ComboBoxText.H
Go to the documentation of this file.
1 /* Copyright 2000-2013 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16  */
17 #ifndef COMBOBOXTEXT_H_
18 #define COMBOBOXTEXT_H_
19 
20 #include <Container.H>
21 #include <string.h>
22 
33 class ComboBoxText : public Container {
34 public:
37  ComboBoxText(void) {
38 #if GTK_MAJOR_VERSION==2 && GTK_MINOR_VERSION<24
39  GtkListStore *model = gtk_list_store_new (1, G_TYPE_STRING);
40  widget = gtk_combo_box_new_with_model(GTK_TREE_MODEL (model));
41  GtkCellRenderer *cell_renderer = gtk_cell_renderer_text_new ();
42  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (widget), cell_renderer, TRUE);
43  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (widget), cell_renderer, "text", 0, NULL);
44 #else
45  widget = gtk_combo_box_text_new();
46 #endif
47 
48  }
49 
52  void setChangedCallback(GCallback callBack, void *data){
53  g_signal_connect( G_OBJECT( widget ), "changed" ,G_CALLBACK(callBack), data);
54  }
55 
59  void getSelection(int &value) {
60  value=gtk_combo_box_get_active(GTK_COMBO_BOX(widget));
61  }
62 
66  void getSelection(char *value) {
67  #if GTK_MAJOR_VERSION==2 && GTK_MINOR_VERSION<24
68  gchar *val=gtk_combo_box_get_active_text(GTK_COMBO_BOX(widget));
69  #else
70  gchar *val=gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(widget));
71  #endif
72  strcpy(value,val);
73  g_free(val);
74  }
75 
79  void getSelection(string &value) {
80  #if GTK_MAJOR_VERSION==2 && GTK_MINOR_VERSION<24
81  gchar *val=gtk_combo_box_get_active_text(GTK_COMBO_BOX(widget));
82  #else
83  gchar *val=gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(widget));
84  #endif
85  value=val;
86  g_free(val);
87  }
88 
92  void setSelection(int which){
93  gtk_combo_box_set_active(GTK_COMBO_BOX(widget), which);
94  }
95 
101  int setSelection(char *text){
102  return setSelection(string(text));
103  }
104 
105  int setSelection(string text){
106  int ret=-1;
107  GtkTreeIter iter;
108  gboolean valid;
109  GtkTreeModel *model=gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
110  if (model==NULL){
111  cerr<<"ComboBoxText::setSelection : A value GtkTreeModel is not associated with this GtkComboBoxText"<<endl;
112  return ret;
113  }
114  valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model), &iter); // Get first row in list store
115  int which=0;
116  cout<<valid<<endl;
117  while (valid) {
118  char *val;
119  gtk_tree_model_get(model, &iter, 0, &val, -1);
120  cout<<val<<endl;
121  if (strcmp(text.c_str(),val)==0){
122  ret=valid=0;
123  setSelection(which);
124  } else
125  valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(model), &iter); // Make iter point to the next row in the list store
126  which++;
127  g_free(val);
128  }
129  return ret;
130  }
131 
141  #if GTK_MAJOR_VERSION==2 && GTK_MINOR_VERSION<24
142  gtk_combo_box_append_text(GTK_COMBO_BOX(widget), text);
143  #else
144  gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(widget), text);
145  #endif
146  return *this;
147  }
148 
157  ComboBoxText& operator<<(string text){
158  #if GTK_MAJOR_VERSION==2 && GTK_MINOR_VERSION<24
159  gtk_combo_box_append_text(GTK_COMBO_BOX(widget), text.c_str());
160  #else
161  gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(widget), text.c_str());
162  #endif
163  return *this;
164  }
165 };
166 #endif //COMBOBOXTEXT_H_