Touble clearing GtkComboBoxEntry text list

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

Touble clearing GtkComboBoxEntry text list

by Bernard Fouché :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi List.

Yet another newbie question! I'm now building a list of choices in a
GtkComboxEntry widget. These choices are calculated on the fly.

First time setting them thru $this->Widget->insert_text() is no problem.
Then I want to clear the list of choices and make a new one.

Currently, I use:

$pos=50;
while($pos>0)
  $this->Widget->remove_text($pos--);

Instead of:

$this->Widget->clear(); // This will clear all entries and ignore calls
to insert_text()!

Then I build the entries:

$this->Widget->insert_text(0,"No target selected yet.");
$pos=1;
foreach($Targets as $key => $val)
  $this->Widget->insert_text($pos++,$val);

$this->Widget->set_active(0);

If I call clear() before inserting text entries, the list stays empty,
even if calls to insert_text() are done. I don't know how I can count
entries, hence I use the value '50' for manual clearing with
remove_text(), something I don't like ('50' is totally arbitrary).

What did I miss this time?

Thanks!

  Bernard

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


Re: Touble clearing GtkComboBoxEntry text list

by kksou :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Bernard Fouché wrote:
I'm now building a list of choices in a
GtkComboxEntry widget. These choices are calculated on the fly.

If I call clear() before inserting text entries, the list stays empty,
even if calls to insert_text() are done. I don't know how I can count
entries, hence I use the value '50' for manual clearing with
remove_text(), something I don't like ('50' is totally arbitrary).

What did I miss this time?
In order to hide the complexities of using some of the widgets, php-gtk sometimes provide certain convenience methods to make it easier to use. In the case of GtkComboBox, these methods include append_text() , prepend_text() , insert_text() and remove_text().

However, when you want to do more advanced stuff, such as changing the options on-the-fly, you need to access its underlying model directly.

A GtkComboBox is just like a GtkTreeView. It adopts the data-view model. The data is stored is a data model (e.g. GtkListStore), and the associating view displays the data.

The following example shows the basic setup using the data-view model for combobox:
http://www.kksou.com/php-gtk2/articles/setup-and-process-GtkComboBox.php

And the following example expands on the above example to show how to change the options on the fly:
http://www.kksou.com/php-gtk2/articles/change-options-of-pulldown-menu-on-the-fly-using-GtkComboBox.php

Regards,
/kksou



Re: Touble clearing GtkComboBoxEntry text list

by kksou :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Oops, just realized that you're using GtkComboBoxEntry.

There're some slight differences. The approach is the same, though.

Here's the corresponding examples using GtkComboBoxEntry instead of GtkComboBox:

http://www.kksou.com/php-gtk2/articles/setup-and-process-GtkComboBoxEntry---Part-2---using-data-view-model.php

http://www.kksou.com/php-gtk2/articles/change-options-of-pulldown-menu-on-the-fly-using-GtkComboBoxEntry.php

Regards,
/kksou

Re: Touble clearing GtkComboBoxEntry text list, some Glade related problems

by Bernard Fouché :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks kksou for your feedback.

Unfortunately, since my layout is done with Glade-2, such examples do
not help me much.

I've been told (by FGM, thanks to him/her ? : -) ) that Glade-3 is
available but did not install it yet. I don't want to manually make the
layout from PHP since it will take too much time as I'm still moving
widgets around.

For those using Glade-2, I found that problems that may seem related to
PHP-GTK2 are also related to the xml file generated by Glade.

For instance if one defines a GtkComboBoxEntry in Glade-2 and never
enter anything in the 'item' field, then the XML <property
name="items"></property> line is not generated by Glade and then when
loading it with new GladeXML() won't allow to you call later
GtkComboBoxEntry->insert_text() ! Is it a problem with PHP-GTK2 or with
Glade (or both!) , I can't say, I don't know enough about both of them.

So you have to enter stuff in the item field in Glade-2 for your
GtkComboBoxEntry, remove it, save the glade file that this time will
have the needed property and then ->insert_text () works from PHP :-(

As long as workarounds are available I can go on so I won't complain
much ;-)

  Bernard

kksou wrote:

> Oops, just realized that you're using GtkComboBoxEntry.
>
> There're some slight differences. The approach is the same, though.
>
> Here's the corresponding examples using GtkComboBoxEntry instead of
> GtkComboBox:
>
> http://www.kksou.com/php-gtk2/articles/setup-and-process-GtkComboBoxEntry---Part-2---using-data-view-model.php
>
> http://www.kksou.com/php-gtk2/articles/change-options-of-pulldown-menu-on-the-fly-using-GtkComboBoxEntry.php
>
> Regards,
> /kksou
>
>  

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


Re: Touble clearing GtkComboBoxEntry text list, some Glade related problems

by kksou :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Bernard Fouché wrote:
Unfortunately, since my layout is done with Glade-2, such examples do
not help me much.
Glade is a wonderful tool that helps one layout the widget. But no matter how good Glade is, it won't do the programming part for you.

Anyway, below is the Glade version of the same example:
http://www.kksou.com/php-gtk2/articles/change-options-of-pulldown-menu-on-the-fly-using-GtkComboBoxEntry.php

Just wanted to show you that Glade merely "simplifies" the GUI portion. The core php-gtk2 codes (that's does the signal handling and re-populating of combobox entries) are exactly the same.

Also, as you work on more combobox/comboboxentry, you will find that eventually you will need to resort to handling models directly. It's cleaner and more powerful. For example, using insert_text(), you can only store one column of data. With models, you could store and display more than one columns of data in the pulldown menu. You could also use GtkTreeModelFilter and GtkTreeModelSort for comboboxes.

Regards,
/kksou

<?php
// How to change options of pulldown menu on the fly
// using GtkComboBoxEntry
// same as http://www.kksou.com/php-gtk2/articles/
// change-options-of-pulldown-menu-on-the-fly-using-GtkComboBoxEntry.php
// but using glade

$glade = new GladeXML('0297.glade');

// the options for pulldown menu
$list['US'] = array('US-1', 'US-2', 'US-3', 'US-4');
$list['Europe'] = array('Europe-1', 'Europe-2', 'Europe-3',
        'Europe-4', 'Europe-5', 'Europe-6');

// setup model
$model = new GtkListStore(Gtk::TYPE_STRING);
$combobox = $glade->get_widget('comboboxentry1');
$combobox->clear();
$combobox->set_model($model);
$cell_renderer = new GtkCellRendererText();
$combobox->pack_start($cell_renderer);
$combobox->set_attributes($cell_renderer, 'text', 0);
populate('Europe');

$glade->signal_autoconnect();
Gtk::main();

function on_submit($button) {
        global $combobox;
        $selection = $combobox->get_child()->get_text();
        echo "You have selected: $selection!\n";
}

function on_button($button) {
        $continent = $button->get_label();
        echo "continent: $continent\n";
        populate($continent);
}

function populate($continent) {
        global $model, $list;
        $model->clear();
        foreach($list[$continent] as $choice) {
                $model->append(array($choice));
        }
}

?>

<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd">

<glade-interface>

<widget class="GtkWindow" id="window1">
  <property name="width_request">400</property>
  <property name="height_request">180</property>
  <property name="visible">True</property>
  <property name="title" translatable="yes">window1</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="default_width">400</property>
  <property name="default_height">180</property>
  <property name="resizable">True</property>
  <property name="destroy_with_parent">False</property>
  <property name="decorated">True</property>
  <property name="skip_taskbar_hint">False</property>
  <property name="skip_pager_hint">False</property>
  <property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>
  <property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
  <property name="focus_on_map">True</property>
  <property name="urgency_hint">False</property>

  <child>
    <widget class="GtkVBox" id="vbox1">
      <property name="visible">True</property>
      <property name="homogeneous">False</property>
      <property name="spacing">0</property>

      <child>
        <widget class="GtkLabel" id="label1">
          <property name="height_request">40</property>
          <property name="visible">True</property>
          <property name="label" translatable="yes"><span font_desc="Times New Roman Italic 10" foreground="#0000ff">Change GtkComboBoxEntry options on the fly</span></property>
          <property name="use_underline">False</property>
          <property name="use_markup">True</property>
          <property name="justify">GTK_JUSTIFY_CENTER</property>
          <property name="wrap">False</property>
          <property name="selectable">False</property>
          <property name="xalign">0.5</property>
          <property name="yalign">0.5</property>
          <property name="xpad">0</property>
          <property name="ypad">0</property>
          <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
          <property name="width_chars">-1</property>
          <property name="single_line_mode">False</property>
          <property name="angle">0</property>
        </widget>
        <packing>
          <property name="padding">0</property>
          <property name="expand">False</property>
          <property name="fill">False</property>
        </packing>
      </child>

      <child>
        <widget class="GtkHBox" id="hbox1">
          <property name="visible">True</property>
          <property name="homogeneous">False</property>
          <property name="spacing">0</property>

          <child>
            <widget class="GtkLabel" id="label2">
              <property name="visible">True</property>
              <property name="label" translatable="yes">Continent: </property>
              <property name="use_underline">False</property>
              <property name="use_markup">False</property>
              <property name="justify">GTK_JUSTIFY_LEFT</property>
              <property name="wrap">False</property>
              <property name="selectable">False</property>
              <property name="xalign">0.5</property>
              <property name="yalign">0.5</property>
              <property name="xpad">0</property>
              <property name="ypad">0</property>
              <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
              <property name="width_chars">-1</property>
              <property name="single_line_mode">False</property>
              <property name="angle">0</property>
            </widget>
            <packing>
              <property name="padding">0</property>
              <property name="expand">False</property>
              <property name="fill">False</property>
            </packing>
          </child>

          <child>
            <widget class="GtkButton" id="button1">
              <property name="width_request">80</property>
              <property name="visible">True</property>
              <property name="can_focus">True</property>
              <property name="label" translatable="yes">US</property>
              <property name="use_underline">True</property>
              <property name="relief">GTK_RELIEF_NORMAL</property>
              <property name="focus_on_click">True</property>
              <signal name="clicked" handler="on_button" last_modification_time="Wed, 01 Aug 2007 05:15:18 GMT"/>
            </widget>
            <packing>
              <property name="padding">0</property>
              <property name="expand">False</property>
              <property name="fill">False</property>
            </packing>
          </child>

          <child>
            <widget class="GtkButton" id="button2">
              <property name="width_request">80</property>
              <property name="visible">True</property>
              <property name="can_focus">True</property>
              <property name="label" translatable="yes">Europe</property>
              <property name="use_underline">True</property>
              <property name="relief">GTK_RELIEF_NORMAL</property>
              <property name="focus_on_click">True</property>
              <signal name="clicked" handler="on_button" last_modification_time="Wed, 01 Aug 2007 05:15:26 GMT"/>
            </widget>
            <packing>
              <property name="padding">0</property>
              <property name="expand">False</property>
              <property name="fill">False</property>
            </packing>
          </child>
        </widget>
        <packing>
          <property name="padding">0</property>
          <property name="expand">False</property>
          <property name="fill">False</property>
        </packing>
      </child>

      <child>
        <widget class="GtkVBox" id="vbox2">
          <property name="height_request">10</property>
          <property name="visible">True</property>
          <property name="homogeneous">False</property>
          <property name="spacing">0</property>

          <child>
            <placeholder/>
          </child>
        </widget>
        <packing>
          <property name="padding">0</property>
          <property name="expand">False</property>
          <property name="fill">False</property>
        </packing>
      </child>

      <child>
        <widget class="GtkHBox" id="hbox2">
          <property name="visible">True</property>
          <property name="homogeneous">False</property>
          <property name="spacing">0</property>

          <child>
            <widget class="GtkLabel" id="label3">
              <property name="visible">True</property>
              <property name="label" translatable="yes">Select: </property>
              <property name="use_underline">False</property>
              <property name="use_markup">False</property>
              <property name="justify">GTK_JUSTIFY_LEFT</property>
              <property name="wrap">False</property>
              <property name="selectable">False</property>
              <property name="xalign">0.5</property>
              <property name="yalign">0.5</property>
              <property name="xpad">0</property>
              <property name="ypad">0</property>
              <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
              <property name="width_chars">-1</property>
              <property name="single_line_mode">False</property>
              <property name="angle">0</property>
            </widget>
            <packing>
              <property name="padding">0</property>
              <property name="expand">False</property>
              <property name="fill">False</property>
            </packing>
          </child>

          <child>
            <widget class="GtkComboBoxEntry" id="comboboxentry1">
              <property name="visible">True</property>
              <property name="items" translatable="yes">
</property>
              <property name="add_tearoffs">False</property>
              <property name="has_frame">True</property>
              <property name="focus_on_click">True</property>
            </widget>
            <packing>
              <property name="padding">0</property>
              <property name="expand">False</property>
              <property name="fill">True</property>
            </packing>
          </child>

          <child>
            <widget class="GtkLabel" id="label4">
              <property name="visible">True</property>
              <property name="label" translatable="yes"> </property>
              <property name="use_underline">False</property>
              <property name="use_markup">False</property>
              <property name="justify">GTK_JUSTIFY_LEFT</property>
              <property name="wrap">False</property>
              <property name="selectable">False</property>
              <property name="xalign">0.5</property>
              <property name="yalign">0.5</property>
              <property name="xpad">0</property>
              <property name="ypad">0</property>
              <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
              <property name="width_chars">-1</property>
              <property name="single_line_mode">False</property>
              <property name="angle">0</property>
            </widget>
            <packing>
              <property name="padding">0</property>
              <property name="expand">False</property>
              <property name="fill">False</property>
            </packing>
          </child>

          <child>
            <widget class="GtkButton" id="button3">
              <property name="visible">True</property>
              <property name="can_focus">True</property>
              <property name="label" translatable="yes">Submit</property>
              <property name="use_underline">True</property>
              <property name="relief">GTK_RELIEF_NORMAL</property>
              <property name="focus_on_click">True</property>
              <signal name="clicked" handler="on_submit" last_modification_time="Wed, 01 Aug 2007 05:16:14 GMT"/>
            </widget>
            <packing>
              <property name="padding">0</property>
              <property name="expand">False</property>
              <property name="fill">False</property>
            </packing>
          </child>
        </widget>
        <packing>
          <property name="padding">0</property>
          <property name="expand">False</property>
          <property name="fill">True</property>
        </packing>
      </child>
    </widget>
  </child>
</widget>

</glade-interface>




Re: Touble clearing GtkComboBoxEntry text list, some Glade related problems

by Bernard Fouché :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

kksou wrote:

> Bernard Fouché wrote:
>  
>> Unfortunately, since my layout is done with Glade-2, such examples do
>> not help me much.
>>
>>    
>
> Glade is a wonderful tool that helps one layout the widget. But no matter
> how good Glade is, it won't do the programming part for you.
>
> Anyway, below is the Glade version of the same example:
> http://www.kksou.com/php-gtk2/articles/change-options-of-pulldown-menu-on-the-fly-using-GtkComboBoxEntry.php
>
> Just wanted to show you that Glade merely "simplifies" the GUI portion. The
> core php-gtk2 codes (that's does the signal handling and re-populating of
> combobox entries) are exactly the same.
>
> Also, as you work on more combobox/comboboxentry, you will find that
> eventually you will need to resort to handling models directly. It's cleaner
> and more powerful. For example, using insert_text(), you can only store one
> column of data. With models, you could store and display more than one
> columns of data in the pulldown menu. You could also use GtkTreeModelFilter
> and GtkTreeModelSort for comboboxes.
>
> Regards,
> /kksou
>
> [.. example code snipped .. ]
I know that, and eventually I can fiddle Glade defined widgets from my
code and focus only on parts that need fine tuning, maybe 10% of the
widgets involved during application 'birth'.

Later, once my layout is finalized and the application fits the required
needs, these 10% may grow bigger.

But if I started by manually coding all needed widgets it would have
taken more time for a lesser result.

Glade gives a big speed up and you can use it with the people that will
use the final application instead of writing code, show the result, then
go back write code again, this only for layout details.

So I'll keep using Glade as much as I can until I'm very close to finish
the application.

  Bernard

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