Conquering Glade combo boxes

Conquering Glade combo boxes – How to use glade combos with this class

Conquering Glade combo boxes

While designing your user interface with Glade is really convenient and easy, it doesn't allow you to use custom classes as this combo here. Thanks to the MVC model in Gtk2, you still can use this class's power by using the same model as Gtk2_IndexedComboBox uses internally: Gtk2_IndexedComboBox_Model.

Glade file to use

<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd">

<glade-interface>

<widget class="GtkWindow" id="wndTest">
  <property name="visible">True</property>
  <property name="title" translatable="yes">Gtk2_IndexedComboBox_Model test</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="resizable">True</property>
  <property name="destroy_with_parent">False</property>
  <property name="decorated">True</property>
  <property name="skip_taskbar_hint">False</property>
  <property name="skip_pager_hint">False</property>
  <property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>
  <property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
  <signal name="destroy" handler="Gtk::main_quit"/>

  <child>
    <widget class="GtkComboBox" id="cmbNormal">
      <property name="visible">True</property>
    </widget>
  </child>
</widget>

</glade-interface>

PHP code


<?php
require_once 'Gtk2/IndexedComboBox/Model.php';

$glade = new GladeXML(dirname(__FILE__) . '/glade.glade');
$glade->signal_autoconnect();

$combo = $glade->get_widget('cmbNormal');
$combo->connect('changed', 'comboChanged');
//show the second column only
$renderer = new GtkCellRendererText();
$combo->pack_start($renderer);
$combo->set_attributes($renderer, 'text', 1);

$mod = new Gtk2_IndexedComboBox_Model();
$combo->set_model($mod);

$mod->append(1, 'One');
$mod->append_array(array(2 => 'Two', 3 => 'Three'));
$mod->prepend(4, 'Four');

function comboChanged($combo)
{
    $nActive = $combo->get_active();
    $iter    = $combo->get_model()->get_iter($nActive);

    $key     = $combo->get_model()->get_key($iter);
    $text    = $combo->get_model()->get_text($iter);

    echo 'Selected: ' . $key . ' => ' . $text . "\r\n";
}

Gtk::main();
?>

The important thing to remember is that you need to setup your own cell renderer, and tell it which column in the model shall be displayed. Beside that, you just do the normal model-creation-and-setting via set_model().

All data manipulation methods of Gtk2_IndexedComboBox you saw in the previous example are available in the model, Gtk2_IndexedComboBox_Model. Only get_active_key() and get_active_text() cannot be used on the model, since this doesn't know anything about selections. Retrieve the selected GtkTreeIter by combining the calls of get_active() and get_iter(), and pass this iter object to get_key() and get_text().

    Поддержать сайт на родительском проекте КГБ