gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
ComboBoxText.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 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  void init(void) {
35 #if GTK_MAJOR_VERSION==2 && GTK_MINOR_VERSION<24
36  GtkListStore *model = gtk_list_store_new (1, G_TYPE_STRING);
37  widget = gtk_combo_box_new_with_model(GTK_TREE_MODEL (model));
38  GtkCellRenderer *cell_renderer = gtk_cell_renderer_text_new ();
39  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (widget), cell_renderer, TRUE);
40  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (widget), cell_renderer, "text", 0, NULL);
41 #else
42  widget = gtk_combo_box_text_new();
43 #endif
44 
45  }
46 public:
49  ComboBoxText(void) {
50  init();
51  }
52 
57  ComboBoxText(GCallback callBack, void *data) {
58  init();
59  setChangedCallback(callBack, data);
60  }
61 
66  gulong setChangedCallback(GCallback callBack, void *data) {
67  return g_signal_connect( G_OBJECT( widget ), "changed" ,G_CALLBACK(callBack), data);
68  }
69 
73  void removeChangedCallback(gulong id) {
74  return g_signal_handler_disconnect( G_OBJECT( widget ), id);
75  }
76 
80  void getSelection(int &value) {
81  value=gtk_combo_box_get_active(GTK_COMBO_BOX(widget));
82  }
83 
87  void getSelection(char *value) {
88 #if GTK_MAJOR_VERSION==2 && GTK_MINOR_VERSION<24
89  gchar *val=gtk_combo_box_get_active_text(GTK_COMBO_BOX(widget));
90 #else
91  gchar *val=gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(widget));
92 #endif
93  strcpy(value,val);
94  g_free(val);
95  }
96 
100  void getSelection(string &value) {
101 #if GTK_MAJOR_VERSION==2 && GTK_MINOR_VERSION<24
102  gchar *val=gtk_combo_box_get_active_text(GTK_COMBO_BOX(widget));
103 #else
104  gchar *val=gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(widget));
105 #endif
106  value=val;
107  g_free(val);
108  }
109 
114  static void getSelection(GtkWidget *wid, string &value) {
115 #if GTK_MAJOR_VERSION==2 && GTK_MINOR_VERSION<24
116  gchar *val=gtk_combo_box_get_active_text(GTK_COMBO_BOX(wid));
117 #else
118  gchar *val=gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(wid));
119 #endif
120  value=val;
121  g_free(val);
122  }
123 
127  void setSelection(int which) {
128  gtk_combo_box_set_active(GTK_COMBO_BOX(widget), which);
129  }
130 
136  int setSelection(const char *text) {
137  return setSelection(string(text));
138  }
139 
140  int setSelection(string text) {
141  int ret=-1;
142  GtkTreeIter iter;
143  gboolean valid;
144  GtkTreeModel *model=gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
145  if (model==NULL) {
146  cerr<<"ComboBoxText::setSelection : A value GtkTreeModel is not associated with this GtkComboBoxText"<<endl;
147  return ret;
148  }
149  valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model), &iter); // Get first row in list store
150  int which=0;
151  cout<<valid<<endl;
152  while (valid) {
153  char *val;
154  gtk_tree_model_get(model, &iter, 0, &val, -1);
155  //cout<<val<<endl;
156  if (strcmp(text.c_str(),val)==0) {
157  ret=valid=0;
158  setSelection(which);
159  } else
160  valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(model), &iter); // Make iter point to the next row in the list store
161  which++;
162  g_free(val);
163  }
164  return ret;
165  }
166 
175  ComboBoxText& operator<<(const char*text) {
176 #if GTK_MAJOR_VERSION==2 && GTK_MINOR_VERSION<24
177  gtk_combo_box_append_text(GTK_COMBO_BOX(widget), text);
178 #else
179  gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(widget), text);
180 #endif
181  return *this;
182  }
183 
192  ComboBoxText& operator<<(string text) {
193 #if GTK_MAJOR_VERSION==2 && GTK_MINOR_VERSION<24
194  gtk_combo_box_append_text(GTK_COMBO_BOX(widget), text.c_str());
195 #else
196  gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(widget), text.c_str());
197 #endif
198  return *this;
199  }
200 };
201 #endif //COMBOBOXTEXT_H_
ComboBoxText(void)
Definition: ComboBoxText.H:49
static void getSelection(GtkWidget *wid, string &value)
Definition: ComboBoxText.H:114
int setSelection(const char *text)
Definition: ComboBoxText.H:136
void removeChangedCallback(gulong id)
Definition: ComboBoxText.H:73
ComboBoxText & operator<<(const char *text)
Definition: ComboBoxText.H:175
void init(void)
Definition: ComboBoxText.H:34
void getSelection(string &value)
Definition: ComboBoxText.H:100
void setSelection(int which)
Definition: ComboBoxText.H:127
ComboBoxText & operator<<(string text)
Definition: ComboBoxText.H:192
ComboBoxText(GCallback callBack, void *data)
Definition: ComboBoxText.H:57
int setSelection(string text)
Definition: ComboBoxText.H:140
GtkWidget * widget
The container based widget.
Definition: Widget.H:33
gulong setChangedCallback(GCallback callBack, void *data)
Definition: ComboBoxText.H:66
void getSelection(int &value)
Definition: ComboBoxText.H:80
void getSelection(char *value)
Definition: ComboBoxText.H:87
gtkIOStream: /tmp/gtkiostream/include/ComboBoxText.H Source File
GTK+ IOStream  Beta