« Return to Thread: Personal COMBO BOX problem with keybord keys in popup wondow

Re: Personal COMBO BOX problem with keybord keys in popup wondow

by kksou :: Rate this Message:

Reply to Author | View in Thread

AngeloP wrote:
I'm trying to build i new combo with 2 columns in list.

So I've used a gtkentry in a main window witha button to open a list
the list is into a popup window without decorations

The list is opened with button and then I would choose the row with
cursor keys but I'm not able to use the keyboard

Anyone can help me?
Instead of manually creating popup menu, why not use the standard GtkComboBox.

GtkComboBox uses the same underlying mechanism as that of GtkTreeView. So it's possible to set up GtkComboBox to display two or more columns.

Below is a sample code using GtkComboBox, in which case all standard keyboards are supported (arrow keys, Home End, etc.):

<?php
$window = new GtkWindow();
$window->connect_simple('destroy', array( 'Gtk', 'main_quit'));
$window->set_size_request(400,150);

$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Setup pulldown menu with 2 columns - Part 1\n".
"using GtkComboBox");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$title->set_justify(Gtk::JUSTIFY_CENTER);
$alignment = new GtkAlignment(0.5, 0, 0, 0);
$alignment->add($title);
$vbox->pack_start($alignment, 0, 0);
$vbox->pack_start(new GtkLabel(), 0, 0);

// the selection
$list = array(
    array("01","Ferro"),
    array("02","Acciaio"),
    array("03","Tungsteno"),
    array("042","Rame"),
    array("044","Piombo"),
    array("05","Alluminio"),
    array("06","Carbonio"),
    array("07","Uranio"),
    array("08","Cobalto"),
    array("07","Plutonio"),
    array("08","Ottone"),
    array("09","Zama"),
    array("10","Molibdeno"),
    array("11","Bronzo"),
    array("12","Oro"),
    array("13","Argento"),
    array("14","Platino"),
    array("15","Berillio")
);

$vbox->pack_start($hbox=new GtkHBox(), 0, 0);
$hbox->pack_start(new GtkLabel('Select: '), 0, 0);

// Setup combobox
$combobox = new GtkComboBox();
$model = new GtkListStore(Gtk::TYPE_STRING, Gtk::TYPE_STRING); // note 1
$combobox->set_model($model);

$cellRenderer0 = new GtkCellRendererText(); // note 2
$combobox->pack_start($cellRenderer0);
$combobox->set_cell_data_func($cellRenderer0, "format_col0", 0); // note 3

$cellRenderer1 = new GtkCellRendererText(); // note 4
$combobox->pack_start($cellRenderer1);

$combobox->set_attributes($cellRenderer1, 'text', 1); // note 5
$combobox->connect('changed', 'on_change'); // note 6

// populate data
foreach($list as $data) {
    $model->append($data); // note 7
}

$hbox->pack_start($combobox, 0, 0);
$hbox->pack_start(new GtkLabel('  '), 0, 0);
$hbox->pack_start($button = new GtkButton('Submit'), 0, 0);
$button->set_size_request(60, 24);
$button->connect('clicked', "on_button", $combobox);

function format_col0($column, $cell, $model, $iter, $col_num) {
    $col0 = $model->get_value($iter, 0);
    $cell->set_property('text', $col0.'  '); // note 8
}

function on_button($button, $combobox) {
    $model = $combobox->get_model();
    $id = $model->get_value($combobox->get_active_iter(), 0);
    $desc = $model->get_value($combobox->get_active_iter(), 1);
    echo "Submit button pressed. Selection = $id ($desc)\n";
}

function on_change($combobox) {
    $model = $combobox->get_model();
    $id = $model->get_value($combobox->get_active_iter(), 0);
    $desc = $model->get_value($combobox->get_active_iter(), 1);
    echo "You have selected: $id ($desc)!\n";
}

$window->show_all();
Gtk::main();
?>

Sample output:


More explanation at:
http://www.kksou.com/php-gtk2/articles/setup-pulldown-menu-with-2-columns---Part-1.php

Regards,
/kksou

 « Return to Thread: Personal COMBO BOX problem with keybord keys in popup wondow