gnope php gtk gui gnope.org  

GtkTreeModel::foreach Question

 
Post new topic   Reply to topic    gnope.org Forum Index -> PHP GTK2 Beginners
View previous topic :: View next topic  
Author Message
SurfaceTension



Joined: 30 May 2006
Posts: 5

PostPosted: Fri Jun 16, 2006 9:17 pm    Post subject: GtkTreeModel::foreach Question Reply with quote

Hi again. Thanks all to your very helpful answers to my last question. I now have another "how do I do this" question, which is:

How do I capture return values from a callback function called with the foreach method? More specifically, from the code below, how do I capture the "i did it" string?

Code:

<?php

$store = new GtkTreeStore(Gtk::TYPE_STRING);
$store->append(null,array("foo"));
$store->append(null,array("bar"));
$store->append(null,array("hello"));
$store->append(null,array("world"));
$store->append(null,array("some"));
$store->append(null,array("more"));
$store->append(null,array("junk"));

$wnd  = new GtkWindow();

$view = new GtkTreeView($store);
$view->append_column(
    new GtkTreeViewColumn('col 1', new GtkCellRendererText(), 'text', 0)
);

$vbox = new GtkVBox(false,5);
$vbox->pack_start($view,false,false,2);

$btn = new GtkButton("DoIt!");
$btn->connect_simple('clicked','btn_callback',$store);
$vbox->pack_start($btn,false,false,2);

$wnd->add($vbox);
$wnd->connect_simple('destroy', array('gtk', 'main_quit'));
$wnd->show_all();
Gtk::main();

function btn_callback($store) {
   $store->foreach('store_foreach');
}

function store_foreach($store,$path,$iter) {
   print($store->get_value($iter,0) . "\n");
   if ($store->get_value($iter,0) == "hello")
      return("i did it");
}

?>
Back to top
cweiske



Joined: 08 Dec 2005
Posts: 454
Location: Leipzig/Germany

PostPosted: Fri Jun 16, 2006 9:42 pm    Post subject: Reply with quote

- You could use a global variable and store your data in there
- You could do
Code:
foreach ($store as $row) {
    //do something
}

- In a class, use a class variable.
Back to top
SurfaceTension



Joined: 30 May 2006
Posts: 5

PostPosted: Fri Jun 16, 2006 9:59 pm    Post subject: Reply with quote

Thanks. Actually, to solve the problem that prompted this question I did use a class property to store the value. I was just wondering if there was a more efficient solution.
Back to top
cweiske



Joined: 08 Dec 2005
Posts: 454
Location: Leipzig/Germany

PostPosted: Fri Jun 16, 2006 10:05 pm    Post subject: Reply with quote

Using the php foreach() language construct should be more efficient, as no callback methods are called.
Back to top
SurfaceTension



Joined: 30 May 2006
Posts: 5

PostPosted: Fri Jun 16, 2006 10:22 pm    Post subject: Reply with quote

Hmmmm...I changed the above code to use this function:

Code:
function btn_callback($store) {
//   $store->foreach('store_foreach');
print("here\n"); //test that callback is being called
   foreach($store as $row)
      print("stuff");
}


And it isn't appearing as though the foreach is even being called.

For the record, I'm developing on WinXP using PHP 5.1.2. Could the problem be that I don't have the latest php-gtk dll files? The most recent ones downloaded with Gnope are dated 12/2/2005.
Back to top
cweiske



Joined: 08 Dec 2005
Posts: 454
Location: Leipzig/Germany

PostPosted: Fri Jun 16, 2006 10:28 pm    Post subject: Reply with quote

Look in the announcements section on how to update the php-gtk2.dll file
Back to top
Display posts from previous:   
Post new topic   Reply to topic    gnope.org Forum Index -> PHP GTK2 Beginners All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Templates enhanced by DigiWiki. Hosting by Tradebit.