Personal COMBO BOX problem with keybord keys in popup wondow

View: New views
6 Messages — Rating Filter:   Alert me  

Personal COMBO BOX problem with keybord keys in popup wondow

by AngeloP :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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?

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

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


// the initial selection
$list = array('item 1', 'item 2', 'item 3', 'item 4');

$vbox->pack_start($hbox=new GtkHBox(), 0, 0);
$hbox->pack_start($label= new GtkLabel('Select: '), 0, 0);
$hbox->pack_start($entry =  new GtkEntry(), 0, 0);
$hbox->pack_start($button = new GtkButton('V'), 0, 0);

$button->set_size_request(18,18);
$button->connect('clicked', "on_button");

//========================================================================
function on_button($button) { // process button click
global $window, $entry;

  // == ENTRY POSITION
  $win_pos=$window->get_position();
  $x_field_pos=$entry->allocation->x;
  $y_field_pos=$entry->allocation->y;
  $x_shift=4;
  $y_shift=23+$entry->allocation->height;
 
  $x_win=$win_pos[0]+$x_field_pos+$x_shift;
  $y_win=$win_pos[1]+$y_field_pos+$y_shift;
 
  //==============================================
  // CREO LA FINESTRA
  $sub_list = &new GtkWindow(Gtk::WINDOW_POPUP);    
 
  $sub_list ->set_size_request(400,250);
  $sub_list ->set_uposition($x_win,$y_win);
  $sub_list ->set_modal(true);
  $sub_list ->set_transient_for($window);
  //=========================================
 
  $sub_list->add($vbox = new GtkVBox());
 
  // Set up a scroll window
  $scrolled_win = new GtkScrolledWindow();
  $scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
  $vbox->pack_start($scrolled_win);
// ==================================================  
// INSERISCO IL GTKTREEVIEW NEL SCROLLED WINDOW
$data=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")
      );
     
  display_table($scrolled_win, $data);

 
// =================================================  

 
  $vbox->pack_start($button = new GtkButton('Chiudi'), 0, 0);
  $button->connect('clicked', "on_destroy",$sub_list);
 
  $sub_list ->show_all();
     
}



function display_table($scrolled_win, $data) {
    // Creates the list store
    $model = new GtkListStore(Gtk::TYPE_STRING, Gtk::TYPE_STRING); // note 2
    $field_header = array('Row #', 'Description'); // note 3

    // Creates the view to display the list store
    $view = new GtkTreeView($model); // note 4
    $scrolled_win->add($view); // note 5

    // Creates columns
    for ($col=0; $col<count($field_header); ++$col) {
        $cell_renderer = new GtkCellRendererText();
        $column = new GtkTreeViewColumn($field_header[$col], $cell_renderer, 'text', $col);
        $view->append_column($column);
    }

    // pupulates the data
    for ($row=0; $row<count($data); ++$row) {
        $values = array();
        for ($col=0; $col<count($data[$row]); ++$col) {
            $values[] = $data[$row][$col];
        }
        $model->append($values); // note 6
    }
   
   
}





function on_destroy($button,$obj) {
  $obj->destroy();
}

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

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

by Christian Weiske :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Angelo,


> 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
Maybe GtkEntryCompletion is the right class for you.


Christian

--
PHP-GTK General Mailing List (http://gtk.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


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

by AngeloP :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I've tested but gtk GtkEntryCompletion but it hs has a single column I think or no?!

and then the problem is this...

I have a combo for customers and a combo  for articles
customers are 1500 and articles are 5000 circa in the same form
and I don't want to load an array with all customers
and all articles in memory . So I would create a custom combo widget with 2 columns
( is better to choose the right code ) and a pager ( for example key arrow up and key arrow down ) to load only 10 record  at  time...





Christian Weiske wrote:
Angelo,


> 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
Maybe GtkEntryCompletion is the right class for you.


Christian

--
PHP-GTK General Mailing List (http://gtk.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Resolved a problem but there is another problem

by AngeloP :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Try this code and then see line nr 144 145 modify... commenting line
I've a problem if I select the line with a enter key not with a mouse..
what is the difference?

ERROR

(prog_combo01.phpw:2200): GLib-GObject-WARNING **: invalid cast from `GtkTreeView' to `GtkWindow'


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

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


// the initial selection
$list = array('item 1', 'item 2', 'item 3', 'item 4');

$vbox->pack_start($hbox=new GtkHBox(), 0, 0);
$hbox->pack_start($label= new GtkLabel('Select: '), 0, 0);
$hbox->pack_start($entry =  new GtkEntry(), 0, 0);
$hbox->pack_start($button = new GtkButton('V'), 0, 0);
$GLOBALS["entry"]=$entry;

$button->set_size_request(18,18);
$button->connect('clicked', "on_button");

//========================================================================
      function on_button($button) { // process button click
      global $window, $entry;
     
        // == ENTRY POSITION
        $win_pos=$window->get_position();
        $x_field_pos=$entry->allocation->x;
        $y_field_pos=$entry->allocation->y;
        $x_shift=4;
        $y_shift=23+$entry->allocation->height;
       
        $x_win=$win_pos[0]+$x_field_pos+$x_shift;
        $y_win=$win_pos[1]+$y_field_pos+$y_shift;
       
        //==============================================
        // CREO LA FINESTRA
        //$sub_win = &new GtkWindow(Gtk::WINDOW_POPUP);    
        $sub_win = &new GtkWindow();
        $GLOBALS["sub_win"]=$sub_win;        
        $sub_win ->set_decorated(false);
     
     
        $sub_win ->set_size_request(400,250);
        $sub_win ->set_uposition($x_win,$y_win);
        $sub_win ->set_modal(true);
        $sub_win ->set_transient_for($window);
        //=========================================
       
        $sub_win->add($vbox = new GtkVBox());
       
        // Set up a scroll window
        $scrolled_win = new GtkScrolledWindow();
        $scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
        $vbox->pack_start($scrolled_win);
      // ==================================================  
      // INSERISCO IL GTKTREEVIEW NEL SCROLLED WINDOW
      $data=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")
            );
           
        display_table($scrolled_win, $data);
     
       
      // =================================================  
     
       
        $vbox->pack_start($button = new GtkButton('Chiudi'), 0, 0);
        $button->connect('clicked', "on_destroy", $sub_win);
       
        $sub_win ->show_all();
       
      }



      function display_table($scrolled_win, $data) {
          // Creates the list store
          $model = new GtkListStore(Gtk::TYPE_STRING, Gtk::TYPE_STRING); // note 2
          $field_header = array('Row #', 'Description'); // note 3
     
          // Creates the view to display the list store
          $view = new GtkTreeView($model); // note 4
          $scrolled_win->add($view); // note 5
     
          // Creates columns
          for ($col=0; $col<count($field_header); ++$col) {
              $cell_renderer = new GtkCellRendererText();
              $column = new GtkTreeViewColumn($field_header[$col], $cell_renderer, 'text', $col);
              $view->append_column($column);
          }
     
          // pupulates the data
          for ($row=0; $row<count($data); ++$row) {
              $values = array();
              for ($col=0; $col<count($data[$row]); ++$col) {
                  $values[] = $data[$row][$col];
              }
              $model->append($values); // note 6
          }
         $view->connect('button-press-event', 'on_view_button_press', $view); // note 1
          $view->connect('select-cursor-row', 'on_cursor_row', $view); // note 1
      }
     


      function on_view_button_press($widget, $event, $view) {
     
          if ( $event->type==Gdk::_2BUTTON_PRESS) {  // note 2
             
              on_cursor_row ($widget, $event, $view);
              // Uncomment these line
              //$GLOBALS["sub_win"]->destroy();
          }
           
      }


        function on_cursor_row ($widget, $event, $view) {
         
              $selection = $view->get_selection();
              list($model, $iter) = $selection->get_selected();
              $itemnum = $model->get_value($iter, 0); // note 3
              $desc = $model->get_value($iter, 1);
             
              $GLOBALS["entry"]->set_text($itemnum);
              // PROBLEM WITH ENTER KEY
              // Comment the next line
              $GLOBALS["sub_win"]->destroy();


             echo "ATTENZIONE Codice: $itemnum Desrizione : $desc\n";
           return false;
           
      }

function on_destroy($button,$obj) {  
  $obj->destroy();
}

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

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

by kksou :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

Re: Resolved a problem but there is another problem

by kksou :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

AngeloP wrote:
Try this code and then see line nr 144 145 modify... commenting line
I've a problem if I select the line with a enter key not with a mouse..
what is the difference?

ERROR

(prog_combo01.phpw:2200): GLib-GObject-WARNING **: invalid cast from `GtkTreeView' to `GtkWindow'
In case you want to stick to your original solution, just change your line 146
    $GLOBALS["sub_win"]->destroy();
to:
    Gtk::idle_add('on_destroy', $GLOBALS["sub_win"], $GLOBALS["sub_win"]);

and it should solve the problem.

For some reason, php-gtk2 needs to do some "clean-up" after calling your signal handler "on_cursor_row". However, you "destroyed" the popup window right inside the signal handler. Hence the error message. By using idle_add, you allow php-gtk2 to do any necessary clean-up, and you destroy the dialog in your other signal handler "on_destroy".

Regards,
/kksou

p.s. here's your original solution, with only the above-mentioned line changed:

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

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


// the initial selection
$list = array('item 1', 'item 2', 'item 3', 'item 4');

$vbox->pack_start($hbox=new GtkHBox(), 0, 0);
$hbox->pack_start($label= new GtkLabel('Select: '), 0, 0);
$hbox->pack_start($entry =  new GtkEntry(), 0, 0);
$hbox->pack_start($button = new GtkButton('V'), 0, 0);
$GLOBALS["entry"]=$entry;

$button->set_size_request(18,18);
$button->connect('clicked', "on_button");

//========================================================================
function on_button($button) { // process button click
        global $window, $entry;

        // == ENTRY POSITION
        $win_pos=$window->get_position();
        $x_field_pos=$entry->allocation->x;
        $y_field_pos=$entry->allocation->y;
        $x_shift=4;
        $y_shift=23+$entry->allocation->height;

        $x_win=$win_pos[0]+$x_field_pos+$x_shift;
        $y_win=$win_pos[1]+$y_field_pos+$y_shift;

        //==============================================
        // CREO LA FINESTRA
        //$sub_win = &new GtkWindow(Gtk::WINDOW_POPUP);
        $sub_win = new GtkWindow();
        global $t_win;
        $t_win = $sub_win;
        $GLOBALS["sub_win"]=$sub_win;
        $sub_win ->set_decorated(false);


        $sub_win ->set_size_request(400,250);
        $sub_win ->set_uposition($x_win,$y_win);
        $sub_win ->set_modal(true);
        $sub_win ->set_transient_for($window);
        //=========================================

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

        // Set up a scroll window
        $scrolled_win = new GtkScrolledWindow();
        $scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
        $vbox->pack_start($scrolled_win);
        // ==================================================
        // INSERISCO IL GTKTREEVIEW NEL SCROLLED WINDOW
        $data=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")
        );

        display_table($scrolled_win, $data);


        // =================================================


        $vbox->pack_start($button = new GtkButton('Chiudi'), 0, 0);
        $button->connect('clicked', "on_destroy", $sub_win);

        $sub_win ->show_all();

}



function display_table($scrolled_win, $data) {
        // Creates the list store
        $model = new GtkListStore(Gtk::TYPE_STRING, Gtk::TYPE_STRING); // note 2
        $field_header = array('Row #', 'Description'); // note 3

        // Creates the view to display the list store
        $view = new GtkTreeView($model); // note 4
        $scrolled_win->add($view); // note 5

        // Creates columns
        for ($col=0; $col<count($field_header); ++$col) {
                $cell_renderer = new GtkCellRendererText();
                $column = new GtkTreeViewColumn($field_header[$col], $cell_renderer, 'text', $col);
                $view->append_column($column);
        }

        // pupulates the data
        for ($row=0; $row<count($data); ++$row) {
                $values = array();
                for ($col=0; $col<count($data[$row]); ++$col) {
                        $values[] = $data[$row][$col];
                }
                $model->append($values); // note 6
        }
        $view->connect('button-press-event', 'on_view_button_press', $view); // note 1
        $view->connect('select-cursor-row', 'on_cursor_row', $view); // note 1
}



function on_view_button_press($widget, $event, $view) {

        if ( $event->type==Gdk::_2BUTTON_PRESS) {  // note 2

                on_cursor_row ($widget, $event, $view);
                // Uncomment these line
                //$GLOBALS["sub_win"]->destroy();
        }

}


function on_cursor_row ($widget, $event, $view) {
        $selection = $view->get_selection();
        list($model, $iter) = $selection->get_selected();
        $itemnum = $model->get_value($iter, 0); // note 3
        $desc = $model->get_value($iter, 1);
        $GLOBALS["entry"]->set_text($itemnum.' '.$desc);
        // PROBLEM WITH ENTER KEY
        // Comment the next line
        #$GLOBALS["sub_win"]->destroy();
        Gtk::idle_add('on_destroy', $GLOBALS["sub_win"], $GLOBALS["sub_win"]);

        echo "ATTENZIONE Codice: $itemnum Desrizione : $desc\n";

        return false;
}

function on_destroy($button,$obj) {
        $obj->destroy();
}

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