Combo - with pager and search Try please
| From: | AngeloP | Date: | Sun, 25 Feb 2007 14:30:04 +0000 |
| Subject: | Combo - with pager and search Try please | ||
| Groups: | php.gtk.general | ||
| Request: | Send a blank email to php-gtk-general+get-7706@lists.php.net to get a copy of this message | ||
Thanks kksou for helping me in the first idea PERSONAl COMBO. Gtk2 is
great!!!
Now I've modified the code so it is possible
to use a pager ( pageUp and PageDow Keys )
to navigate into a short list of materials
instead to load all rows
In this example you can see 10 key at time then, with pgup or pgdown, you
can refresh
the list with previors/next 10 row
If you type a caracter into entry field for example "C" and then open pull
down menu
you can see the list began with the code that begin with "C".
The description is now in a label that is added by the class
Here I've use an array but the sample is to use it with a database
connection.
There are also some problem when clear treeview
(combo_entry03.phpw:464): Gtk-CRITICAL **: _gtk_tree_view_find_path:
assertion `node != NULL' failed
but it seems to work by any way .
I'm using this code under winxp and the GtkDialog even if it is "modal" is
possible the move the main window
so I've setted sensitive and decorated flags of main window to false.
There is another way to make it ?
Ciao to all from italy
Code:
<?php
$window = new GtkWindow();
$window->connect_simple('destroy', array( 'Gtk', 'main_quit'));
$window->set_size_request(640,480);
$window->add($vbox = new GtkVBox());
// display title
$title = new GtkLabel("Setup pulldown menu with 2 columns using self-defined
combobox and a pager into list \n".
" PageUp And Page down to navigate. Use also F4 to open pull down Menu\n".
"Digit the first letter for example 'L' before push F4");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#000000"));
$title->set_size_request(-1, 60);
$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);
$vbox->pack_start($hbox=new GtkHBox(), 0, 0);
$hbox->pack_start(new GtkLabel('Select: '), 0, 0);
// Setup combobox
$combobox = new ComboBox1($window); // note 1
$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_submit_button", $combobox);
$vbox->pack_start($hbox=new GtkHBox(), 0, 0);
$hbox->pack_start(new GtkLabel('Select: '), 0, 0);
// Setup combobox
$combobox1 = new ComboBox1($window); // note 1
$hbox->pack_start($combobox1, 0, 0);
$hbox->pack_start(new GtkLabel(' '), 0, 0);
$hbox->pack_start($button1 = new GtkButton('Submit'), 0, 0);
$button1->set_size_request(60, 24);
$button1->connect('clicked', "on_submit_button", $combobox1);
$window->show_all();
Gtk::main();
function on_submit_button($button, $combobox) {
$selection = $combobox->entry->get_text();
print "selection = $selection\n";
}
// ============ CLASS
=======================================================
class ComboBox1 extends GtkHBox {
function __construct($parent_window) {
parent::__construct();
$this->parent_window = $parent_window;
$this->entry = new GtkEntry();
//$this->entry = GtkComboBoxEntry::new_text();
$this->entry->set_size_request(120, -1);
$this->pack_start($this->entry, 0, 0);
$this->pack_start($this->button = new GtkButton('V'), 0, 0);
$this->pack_start(new Gtklabel(' '), 0, 0);
$this->descr = new Gtklabel('');
$this->descr ->set_size_request(200, 23);
$this->descr ->set_alignment(0,0);
$this->pack_start($this->descr , 0,0);
$this->button->connect('clicked', array(&$this, 'on_button'));
//
note 2
$this->entry->connect('key-press-event', array(&$this,
'on_keypress_entry')); // note 3
$this->row_x_page=9;
}
function on_keypress_entry($widget, $event) {
// Open menu On F4
if ($event->keyval == 65473) {
//alert("Fuzione F4");
$this->button->clicked();
}
return false;
}
function on_button($button) { // process button click
$this->parent_window->set_sensitive(false);
$this->parent_window->set_decorated(false);
// Pick the value from Entry if exist
$text=$this->entry->get_text();
// Select data from a data base or Array and build pull down menu
$data=Select_Data($text);
// == look for ENTRY POSITION
$win_pos=$this->parent_window->get_position();
$x_field_pos=$this->entry->allocation->x;
$y_field_pos=$this->entry->allocation->y;
//x4-y23
$x_shift=0;
$y_shift=0+$this->entry->allocation->height;
$x_win=$win_pos[0]+$x_field_pos+$x_shift;
$y_win=$win_pos[1]+$y_field_pos+$y_shift;
$this->dialog = new GtkDialog(null, $this->parent_window,
Gtk::DIALOG_MODAL|Gtk::DIALOG_NO_SEPARATOR); // note 4
$tooltips = new GtkTooltips();
$tooltips->set_tip($this->dialog, 'PgUp PrevPage\nPgDown NexPage');
$this->dialog->set_decorated(false);
$this->dialog->set_size_request(300, 170);
$this->dialog->set_uposition($x_win,$y_win);
$scrolled_win = new GtkScrolledWindow();
$scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC,
Gtk::POLICY_AUTOMATIC);
$this->dialog->vbox->pack_start($scrolled_win);
$this->display_table ($scrolled_win, $data);
$this->dialog->action_area->set_size_request(-1, 0);
$this->dialog->connect('key-press-event', array(&$this,
'on_keypress'));
$this->dialog ->show_all();
$this->dialog->run();
$this->parent_window->set_sensitive(true);
$this->parent_window->set_decorated(true);
$this->entry->grab_focus();
}
//====================================================================================
// This function create the tables
function display_table($scrolled_win, $data) {
// Creates the list store
$model = new GtkListStore(Gtk::TYPE_STRING, Gtk::TYPE_STRING);
$field_header = array('Row #', 'Description');
$field_justification = array(0, 0);
// Creates the view to display the list store
$this->view = new GtkTreeView($model);
$scrolled_win->add($this->view);
// Creates the columns
for ($col=0; $col<count($field_header); ++$col) {
$cell_renderer = new GtkCellRendererText();
$cell_renderer->set_property("xalign",
$field_justification[$col]);
$column = new GtkTreeViewColumn($field_header[$col],
$cell_renderer, 'text', $col);
$column->set_alignment($field_justification[$col]);
$column->set_sort_column_id($col);
// set the header font and color
$label = new GtkLabel($field_header[$col]);
$label->modify_font(new PangoFontDescription("Arial Bold"));
$label->modify_fg(Gtk::STATE_NORMAL,
GdkColor::parse("#0000FF"));
$column->set_widget($label);
$label->show();
// setup self-defined function to display alternate row color
$column->set_cell_data_func($cell_renderer, array(&$this,
"format_col"), $col);
$this->view->append_column($column);
}
$this->populate_data($model,$data);
$this->view->set_headers_visible(0);
$this->view->connect('button-press-event', array(&$this,
'on_buttonpress'));
// setup selection
$selection = $this->view->get_selection();
$selection->connect('changed', array(&$this, 'on_selection'));
}
function populate_data($model,$data){
// 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);
}
}
function format_col($column, $cell, $model, $iter, $col_num) {
return; // note 5
$path = $model->get_path($iter);
$row_num = $path[0];
$row_color = ($row_num%2==1) ? '#dddddd' : '#ffffff';
$cell->set_property('cell-background', $row_color);
}
function on_selection($selection) {
list($model, $iter) = $selection->get_selected();
if ($iter==null) return;
$id = $model->get_value($iter, 0);
$desc = $model->get_value($iter, 1);
print "You have selected $id: $desc\n";
return $id;
}
/*
Here there is the number of row to paginate
*/
function on_keypress($widget, $event) {
if ($event->keyval==65293) { #return
$this->get_selection();
} else {
// PAGINA SU
if ($event->keyval==65365)
{
$selection = $this->view->get_selection();
list($model, $arPaths) = $selection->get_selected_rows();
$rowsel=$arPaths[0][0];
$item=$this->on_selection($selection) ;
print "The row selected is $rowsel and Key PG-UP the
last item is $item \n";
if ($rowsel == 0 ){
$data=Select_Data($item,-1);
print_r($data);
$model->clear();
$this->populate_data($model,$data);
}
}
//PAGINA GIU
if ($event->keyval==65366)
{
$selection = $this->view->get_selection();
list($model, $arPaths) = $selection->get_selected_rows();
$rowsel=$arPaths[0][0];
$item=$this->on_selection($selection) ;
print "The row selected is $rowsel and Key PG-DOWN the
last item is $item \n";
if ($rowsel == $this->row_x_page){
$data=Select_Data($item,1);
print_r($data);
$model->clear();
$this->populate_data($model,$data);
}
}
return false;
}
}
// =================================================
function on_buttonpress($widget, $event) {
if ($event->type==Gdk::_2BUTTON_PRESS) {
$this->get_selection();
} else {
return false;
}
}
function get_selection() {
$selection = $this->view->get_selection();
list($model, $iter) = $selection->get_selected();
if ($iter==null) return;
$id = $model->get_value($iter, 0);
$desc = $model->get_value($iter, 1);
$this->entry->set_text($id); // note 6
$this->descr->set_text($desc);
$this->dialog->destroy(); // note 7
}
} // END CLASS
function Select_Data($input=null,$direction=1){
/*
Extract data from a source now here
we use an array but thi array may come out from a select in a database
*/
$array=read_data_from_file ( );
// Test on data source
if(empty($array)){
// Return an empty line
$array=array(array("",""));
}else{
$number_of_rows=10;
if($input)
{
$key=Search_For_Value_In_Array($array,$input);
if($direction==1){
$data=array_slice ($array, $key, $number_of_rows);
}else{
$key-=$number_of_rows;
if($key<0){
$key=0;
}
$data=array_slice ($array, $key, $number_of_rows);
}
}
else
{
$data=array_slice ($array, 0, $number_of_rows);
}
}
return $data;
}
// If you use a database Substituite this with query
function read_data_from_file ( ){
$data=array();
// get a web page into an array and print it out
$fcontents = file ('metals.txt');
while (list ($line_num, $line) = each ($fcontents)) {
$data[]=explode("\t",ereg_replace("\r|\n","",$line));
}
return $data;
}
// This is not necessary if you use a select into a database
function Search_For_Value_In_Array($array,$input){
while (list ($key, $col1) = each ($array))
{
list($cod,$descr)=$col1;
if(eregi($input,$cod))
{
break;
}
}
return $key;
}
?>
--
View this message in context: http://www.nabble.com/Combo---with-pager-and-search-Try-please-tf3287386.html#a9144143
Sent from the Php - GTK - General mailing list archive at Nabble.com.