180. How to setup pulldown menu with 2 columns - Part 2 - using GtkEntryCompletion?

Problem

A standard GtkEntryCompletion displays only one column of data as shown in the example How to setup GtkEntry with auto completion?

You would like to have a auto-completion entry that displays two columns of data as shown below:

How to setup pulldown menu with 2 columns - Part 2 - using GtkEntryCompletion?


Solution


Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
44   
45   
46   
47   
48   
49   
50   
51   
52   
53   
54   
55   
56   
57   
58   
59   
60   
61   
62   
63   
64   
65   
66   
67   
68   
69   
70   
71   
72   
73   
74   
75   
76   
77   
78   
79   
80   
81   
82   
83   
84   
85   
86   
87   
88   
89   
90   
91   
92   
93   
94   
95   
96   
97   
98   
99   
100   
101   
102   
103   
106   
107   
108   
109   
110   
111   
114   
115   
116   
117   
118   
119   
120   
<?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 2\n".
"using GtkEntryCompletion");
$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('AL'=>"Alabama",
        'AK'=>"Alaska",
        'AZ'=>"Arizona",
        'AR'=>"Arkansas",
        'CA'=>"California",
        'CO'=>"Colorado",
        'CT'=>"Connecticut",
        'DE'=>"Delaware",
        'DC'=>"District Of Columbia",
        'FL'=>"Florida",
        'GA'=>"Georgia",
        'HI'=>"Hawaii",
        'ID'=>"Idaho",
        'IL'=>"Illinois",
        'IN'=>"Indiana",
        'IA'=>"Iowa",
        'KS'=>"Kansas",
        'KY'=>"Kentucky",
        'LA'=>"Louisiana",
        'ME'=>"Maine",
        'MD'=>"Maryland",
        'MA'=>"Massachusetts",
        'MI'=>"Michigan",
        'MN'=>"Minnesota",
        'MS'=>"Mississippi",
        'MO'=>"Missouri",
        'MT'=>"Montana",
        'NE'=>"Nebraska",
        'NV'=>"Nevada",
        'NH'=>"New Hampshire",
        'NJ'=>"New Jersey",
        'NM'=>"New Mexico",
        'NY'=>"New York",
        'NC'=>"North Carolina",
        'ND'=>"North Dakota",
        'OH'=>"Ohio",
        'OK'=>"Oklahoma",
        'OR'=>"Oregon",
        'PA'=>"Pennsylvania",
        'RI'=>"Rhode Island",
        'SC'=>"South Carolina",
        'SD'=>"South Dakota",
        'TN'=>"Tennessee",
        'TX'=>"Texas",
        'UT'=>"Utah",
        'VT'=>"Vermont",
        'VA'=>"Virginia",
        'WA'=>"Washington",
        'WV'=>"West Virginia",
        'WI'=>"Wisconsin",
        'WY'=>"Wyoming");

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

// setup GtkEntryCompletion
if (defined("GObject::TYPE_STRING")) {
    $model = new GtkListStore(GObject::TYPE_STRING, GObject::TYPE_STRING);
} else {
    $model = new GtkListStore(Gtk::TYPE_STRING, Gtk::TYPE_STRING);
}
$completion = new GtkEntryCompletion;
$completion->set_model($model);
$completion->set_text_column(0); // note 1

$cellRenderer1 = new GtkCellRendererText(); // note 2
$completion->pack_start($cellRenderer1); // note 3
$completion->set_attributes($cellRenderer1, 'text', 1); // note 4

$entry = new GtkEntry();
$entry->set_completion($completion);

// Stuff the choices in the model
foreach($list as $state_code=>$state_name) {
    $model->append(array($state_code, $state_name));
}

// Set up an hbox to contain the combobox as well as the Submit button
$hbox->pack_start($entry, 0, 0);
$hbox->pack_start(new GtkLabel('  '), 0, 0);
$hbox->pack_start($button = new GtkButton('Submit'), 0, 0);
$button->set_size_request(60, 24);

// Set up the event handler to respond to button click
$button->connect('clicked', "on_button", $entry);

// The callback function that is called when user clicks the submit button
function on_button($button, $entry) {
    $selection = $entry->get_text();
    echo "You have selected: $selection!\n";
}

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

Output

As shown above.
 

Explanation

The above sample code is based on How to setup GtkEntry with auto completion? and How to setup pulldown menu with 2 columns - Part 1 - using GtkComboBox?

What's new here:

  1. This is the first column to be displayed in the auto-completion box.
  2. Create a GtkCellRenderer for display of the second column of data.
  3. Pack this GtkCellRenderer into the GtkEntryCompletion.
  4. Inform php-gtk2 to display the second column of data for this GtkCellRenderer.

Related Links

Add comment


Security code
Refresh