libyui-gtk  2.44.5
 All Classes Functions
YGComboBox.cc
1 /********************************************************************
2  * YaST2-GTK - http://en.opensuse.org/YaST2-GTK *
3  ********************************************************************/
4 
5 #define YUILogComponent "gtk"
6 #include <yui/Libyui_config.h>
7 #include <YGUI.h>
8 #include "YGUtils.h"
9 #include "YComboBox.h"
10 #include "YGSelectionStore.h"
11 #include "YGWidget.h"
12 
13 class YGComboBox : public YComboBox, public YGLabeledWidget, public YGSelectionStore
14 {
15  public:
16  YGComboBox (YWidget *parent, const std::string &label, bool editable)
17  : YComboBox (NULL, label, editable),
18  YGLabeledWidget (this, parent, label, YD_HORIZ,
19  GTK_TYPE_COMBO_BOX, "has-entry", editable ? TRUE : FALSE, NULL),
20  YGSelectionStore (false)
21  {
22  const GType types[2] = { GDK_TYPE_PIXBUF, G_TYPE_STRING };
23  createStore (2, types);
24  gtk_combo_box_set_model (getComboBox(), getModel());
25 
26  GtkCellRenderer* cell = gtk_cell_renderer_pixbuf_new();
27  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (getWidget()), cell, FALSE);
28  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (getWidget()), cell,
29  "pixbuf", 0, NULL);
30 
31  if (editable)
32  gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (getWidget()), 1);
33  else {
34  cell = gtk_cell_renderer_text_new();
35  gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (getWidget()), cell, TRUE);
36  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (getWidget()), cell,
37  "text", 1, NULL);
38  }
39 
40  connect (getWidget(), "changed", G_CALLBACK (selected_changed_cb), this);
41  }
42 
43  inline GtkComboBox *getComboBox()
44  { return GTK_COMBO_BOX (getWidget()); }
45 
46  GtkEntry *getEntry()
47  { return GTK_ENTRY (gtk_bin_get_child (GTK_BIN (getWidget()))); }
48 
49  // YGSelectionModel
50 
51  void blockSelected() {}
52 
53  void doAddItem (YItem *item)
54  {
55  GtkTreeIter iter;
56  addRow (item, &iter);
57  setRowText (&iter, 0, item->iconName(), 1, item->label(), this);
58  if (item->selected())
59  doSelectItem (item, true);
60  }
61 
62  void doSelectItem (YItem *item, bool select)
63  {
64  if (select) {
65  BlockEvents block (this);
66  GtkTreeIter iter;
67  getTreeIter (item, &iter);
68  gtk_combo_box_set_active_iter (getComboBox(), &iter);
69  }
70  }
71 
72  void doDeselectAllItems()
73  {
74  BlockEvents block (this);
75  gtk_combo_box_set_active (getComboBox(), -1);
76  }
77 
78  YItem *doSelectedItem()
79  {
80  GtkTreeIter iter;
81  if (gtk_combo_box_get_active_iter (getComboBox(), &iter))
82  return getYItem (&iter);
83  return NULL;
84  }
85 
86  // YComboBox
87 
88  virtual std::string text()
89  {
90  GtkTreeIter iter;
91  gchar *str;
92  if (editable()) {
93  // HACK: this seems to be necessary
94  GtkWidget *entry = gtk_bin_get_child (GTK_BIN (getWidget()));
95  return gtk_entry_get_text (GTK_ENTRY (entry));
96  }
97  else
98  if (gtk_combo_box_get_active_iter (getComboBox(), &iter))
99  gtk_tree_model_get (getModel(), &iter, 1, &str, -1);
100  else
101  return "";
102  std::string ret (str);
103  g_free (str);
104  return ret;
105  }
106 
107  virtual void setText (const std::string &value)
108  {
109  BlockEvents block (this);
110  GtkTreeIter iter;
111  if (findLabel (1, value, &iter))
112  gtk_combo_box_set_active_iter (getComboBox(), &iter);
113  else
114  gtk_entry_set_text (getEntry(), value.c_str());
115  }
116 
117  virtual void setInputMaxLength (int length)
118  {
119  YComboBox::setInputMaxLength (length);
120  gtk_entry_set_width_chars (getEntry(), length);
121  }
122 
123  virtual void setValidChars (const std::string &validChars)
124  {
125  YComboBox::setValidChars (validChars);
126  YGUtils::setFilter (getEntry(), validChars);
127  }
128 
129  // callbacks
130  static void selected_changed_cb (GtkComboBox *widget, YGComboBox *pThis)
131  { pThis->emitEvent (YEvent::ValueChanged); }
132 
133  YGLABEL_WIDGET_IMPL (YComboBox)
134  YGSELECTION_WIDGET_IMPL (YComboBox)
135 };
136 
137 YComboBox *YGWidgetFactory::createComboBox (YWidget *parent, const std::string &label, bool editable)
138 {
139  return new YGComboBox (parent, label, editable);
140 }
141