gnope php gtk gui gnope.org  

extends GtkComboBoxEntry::new_text() ??

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



Joined: 11 Dec 2005
Posts: 78
Location: Gatineau, Quebec

PostPosted: Sat Dec 31, 2005 3:44 pm    Post subject: extends GtkComboBoxEntry::new_text() ?? Reply with quote

Hi all,

I want to create a new class based on GtkComboBoxEntry. In the constructor, what is the syntax to do the equivalent of this:

$myCombo = GtkComboBoxEntry::new_text();

I can't seem to find any sample code for this type of thing.

-Ron T.
Back to top
cweiske



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

PostPosted: Mon Jan 02, 2006 2:34 pm    Post subject: Re: extends GtkComboBoxEntry::new_text() ?? Reply with quote

ron_t wrote:
$myCombo = GtkComboBoxEntry::new_text();

Dev_Inspector tells me that that method should exist. Doesn't it?
Back to top
ron_t



Joined: 11 Dec 2005
Posts: 78
Location: Gatineau, Quebec

PostPosted: Mon Jan 02, 2006 11:53 pm    Post subject: Re: extends GtkComboBoxEntry::new_text() ?? Reply with quote

cweiske wrote:
ron_t wrote:
$myCombo = GtkComboBoxEntry::new_text();

Dev_Inspector tells me that that method should exist. Doesn't it?


Yes. The problem is, however, to be able to extend the class and create a text-only GtkComboBoxEntry.

What I've figured out so far for a constructor (by looking at the C source) is this:


Code:

function __construct()
   {
      $store = new GtkListStore(Gtk::TYPE_STRING);
      parent::__construct();
      $this->set_model($store);
         }


But it doesn't work. When I add items to the drop-down list, the correct number of items is added, but they're all blank. Also, when I try to access them from within the class like this:

Code:


   function showList()
   {
      for($i = 0; $i < $this->listLength; $i++)
      {
         $this->set_active($i);
         $currentText = $this->get_active_text();
         print("i = $i, current is $currentText\n");
      }
   } // showList()


I get this error:

(test_dropdown_class2.php:1916): Gtk-CRITICAL **: gtk_entry_set_text: assertion
`text != NULL' failed

This may not be very clear without seeing the entire class I've built and the test rig that goes with it. Let me know if seeing those would help at all.

What I seem to need to do is cast the GtkListStore as a GtkTreeModel, but I don't see any way to do that.

-Ron T.
Back to top
cweiske



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

PostPosted: Mon Jan 02, 2006 11:56 pm    Post subject: Re: extends GtkComboBoxEntry::new_text() ?? Reply with quote

ron_t wrote:
What I seem to need to do is cast the GtkListStore as a GtkTreeModel, but I don't see any way to do that.

Ron, the GtkListStore class implements the GtkTreeModel interface - that means you don't need to cast it; it is a tree model automatically.
Back to top
ron_t



Joined: 11 Dec 2005
Posts: 78
Location: Gatineau, Quebec

PostPosted: Tue Jan 03, 2006 2:06 am    Post subject: Re: extends GtkComboBoxEntry::new_text() ?? Reply with quote

cweiske wrote:
ron_t wrote:
What I seem to need to do is cast the GtkListStore as a GtkTreeModel, but I don't see any way to do that.

Ron, the GtkListStore class implements the GtkTreeModel interface - that means you don't need to cast it; it is a tree model automatically.


So, in theory, this should work. Obviously I'm missing something. When I did this without trying to put it all into a class, it worked. If I post the code, would you take a look at it and tell me if anything glaring jumps out at you?

-Ron T.
Back to top
karlbowden



Joined: 18 Dec 2005
Posts: 21
Location: Taree, NSW, Australia

PostPosted: Mon Feb 06, 2006 12:51 am    Post subject: Reply with quote

Ok, I'm wondering about this too now.

Anybody know how to attach a model to a GtkComboBox and have the combobox display the contents. Eg:

Code:

class GtkComboBoxNew extends GtkComboBox {
    function __construct () {
        parent::__construct ();
        $this->set_model ($this->mod1 = new GtkListStore (Gtk::TYPE_STRING, Gtk::TYPE_LONG));
        $this->mod1->append (array ('Sample TextItem1', 1));
        $this->mod1->append (array ('Sample TextItem2', 2));
    }
}



Being able to chose which column in the model to display would be very handy in being able to create an indexed or paired combo box.
Back to top
karlbowden



Joined: 18 Dec 2005
Posts: 21
Location: Taree, NSW, Australia

PostPosted: Mon Feb 06, 2006 3:40 am    Post subject: Reply with quote

Ok, found the key to getting this to work:
Code:
$combo_box->pack_start ($cr = new GtkCellRendererText(), false);
$combo_box->set_attributes ($cr, 'text', <COLUMN NUMBER>);


Somebody also might find this code snippett handy too:
It just creates a new class that allows indexed pairs for the GtkComboBox.

It's a bit messy, but it gives you the idea and feel free to extend or adapt it for your own code.

Usage:
Code:
$win1 = new GtkWindow ();
$win1->add ($c = new GtkComboIndexed ());
$c->ndx_initialize (array (2 => 'Item1', 4 => 'Item2', 5 => 'Item3'));
$c->ndx_append (7, 'Item4');
$c->ndx_prepend (3, "Item-1");
$c->ndx_append (8, 'Item5');
$c->ndx_prepend (1, 'Item-2');
$c->ndx_set (6, 1, 'item5');
$c->connect ('changed', 'cbdebug');
function cbdebug ($obj) {
  echo $obj->get_active_pos () . " - " . $obj->get_active_ndx () . " - " . $obj->get_active_text () . "\n";
}


Class
Code:
class GtkComboIndexed extends GtkComboBox {
    var $ndx = array ();
    function __construct () {
        parent::__construct ();
        $this->set_model ($this->mod1 = new GtkListStore (Gtk::TYPE_LONG, Gtk::TYPE_STRING));
        $this->pack_start ($this->cr = new GtkCellRendererText(), 1);
        $this->set_attributes ($this->cr, 'text', 1);
    }       

    function ndx_initialize ($ary) {
        $this->ndx_clear ();
        foreach ($ary as $a1 => $a2)
          $this->ndx_append ($a1, $a2);
    }
   
    function ndx_clear () {
        $this->ndx = array ();
        $this->mod1->clear ();
    }
         
    function ndx_append ($ndx, $txt) {
        end ($this->ndx);
        $pos = key ($this->ndx) + 1;
        $this->ndx[$pos][0] = $this->mod1->append (array ($ndx, $txt));
        $this->ndx[$pos][1] = $ndx;
        $this->ndx[$pos][2] = $txt;
    }
   
    function ndx_prepend ($ndx, $txt) {
        $pos = -1;
        $this->ndx[$pos][0] = $this->mod1->prepend (array ($ndx, $txt));
        $this->ndx[$pos][1] = $ndx;
        $this->ndx[$pos][2] = $txt;
        ksort ($this->ndx);

        // Reset the positions
        foreach ($this->ndx as $a1)
          $ndx2[] = $a1;
        $this->ndx = $ndx2;
    }

    function ndx_set ($pos, $ndx, $txt) {
        if (!isset ($this->ndx[$pos]))
          $this->ndx[$pos][0] = $this->mod1->insert ($pos);
        $this->ndx[$pos][1] = $ndx;
        $this->ndx[$pos][2] = $txt;
        $this->mod1->set ($this->ndx[$pos][0], 0, $ndx, 1, $txt);
    }

    function get_active_text () {
        return ($this->ndx[$this->get_active ()][2]);
    }
    function get_active_ndx () {
        return ($this->ndx[$this->get_active ()][1]);
    }
    function get_active_pos () {
        return $this->get_active ();
    }
    function get_active_iter () {
        return ($this->ndx[$this->get_active ()][0]);
    }
}
Back to top
tronic



Joined: 21 May 2006
Posts: 2

PostPosted: Sun May 21, 2006 8:17 pm    Post subject: Reply with quote

Hi everyone,

I'm sorry to bring this old Thread up again, but it somehow matches the problem i have and actually does not really solve it (at least for me).

I want to write a class which extends from GtkComboBox. This works fine as long as I only call the normal ComboBox constructor inside the constructor of the new class. But I wanted to call the GtkComboBox::new_text() constructor and I just can't find a way to do this. Confused

Thanks in advance
Back to top
cweiske



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

PostPosted: Sun May 21, 2006 11:03 pm    Post subject: Reply with quote

Using the new_text static method from an own class isn't that easy, as it is no "real" constructor. What you can do is implementing the things this method does yourself; it's basically just setting up the model correctly. Look at the Gtk sourcecode; it's at cvs.gnome.org
Back to top
tronic



Joined: 21 May 2006
Posts: 2

PostPosted: Thu May 25, 2006 12:55 pm    Post subject: Reply with quote

Hi,
thanks for you answer. I managed to find the code but i wasn't able to find out what it implements.
Then I read karlbowdens posts more closely and after some tries I was able to get my own ComboBox class running. It took me some time to realize that you have to do this:
Code:
$combo_box->pack_start ($cr = new GtkCellRendererText(), false);
$combo_box->set_attributes ($cr, 'text', <COLUMN NUMBER>)

after appending entries to the ComboBox. Smile
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.