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

Re: Resolved a problem but there is another problem

by AngeloP :: Rate this Message:

Reply to Author | View in Thread

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();
?>

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