Re: [Glade & Gtk-Perl] Adding widgets dynamically to windows



On Mon, 2002-04-15 at 17:22, Bhaskar S. Manda wrote:
I want to add a CListModel to a scrolled window. Glade doesn't have a
CListModel in its widget palette, so I see a few options.

1) Use custom widget from palette
The debug output of glade2perl indicates that custom widgets 
are ignored.

I don't know how to use custom widgets.


2) Build GUI dynamically with libglade
I don't see a perl module giving access to libbglade, so assuming that
glade2perl generates a libglade application, I try to modify the files it
generates. Of the generated files, if I edit the <projectname>UI.pm, I can
make it do whatever I want since I can access the widgets right after they are
created in the code. But if I add some widgets through the Glade UI and then
generate the code again, my modifications are obviously lost. So I need to
make changes in the <projectname>.pm file. 

One way around this is to split off individual dialogs into files that Glade
will not touch again, but then you can't add any more widgets through the UI.

Back to the <projectname>.pm file. With all_forms, I can (per Gtk::cookbook)
get at any widget. Since I need to essentially do a
scrolledwindow->add(widget), I get the scrolled window by adding a signal:

   on_scrolledwindow9_realize{
      my ($class, $data, $object, $instance, $event) = @_;

      my $form = $__PACKAGE__::all_forms->{$instance};
      my $window = $form->{'scrolledwindow9'};
      my $clistmodel = &sub_that_makes_clistmodel;
      $window->add($clistmodel);
   }
This doesn't work:
   widget is not of type Gtk::Widget at line xx (the line with window->add).

If I use the class that comes in, $class, which prints out as a
Gtk::ScrolledWindow,
   $class->add($clistmodel)
there is still the same error. So how can I do this?

Thanks,
-bhaskar

But I do know how to (sort of) use libglade: Gtk::GladeXML and IMHO it
is much better than what glade2perl generates (read: I can actually use
it).  At the end of this email is an example hello world app using Gnome
and Gtk::GladeXML.  Basically create the glade file the same way you
normally would, but don't build it.  Then you can say 

my $gladexmlobj = Gtk::GladeXML->new(
        'filename.glade',
        'widget to start building from' #normally a window
);

You can get any widget in the widget tree by saying 

my $ok_button = $gladexmlobj->get_widget('ok_button');

if you have a widget and want the widget tree so that you can get other
widgets on the window (say you are in a handler function that just got
the button pressed and wanted to update a label) you can say

my $gladexmlobj = $ok_button->get_widget_tree;
my $label       = $gladexmlobj->get_widget('label1');

or just

my $label = $ok_button->get_widget_tree->get_widget('label1');

if you don't need the tree for anything else.

I have found that once you have the container widget you can added
widgets in the normal way.  In my SQL editor I wrote:

<snip>
sub on_mi_run_activate {
        my  $app        = $_[0]->get_widget_tree;

        <snip />

        my  $nb         = $app->get_widget('notebook');

        <snip />

        my $sw = Gtk::ScrolledWindow->new;
        my $list = Gtk::CList->new_with_titles(@cols);

        <snip />

        $nb->append_page($sw, Gtk::Label->new('results '.++$sql_count));
        $sw->add($list);

        $nb->show_all;

        <snip />
}
</snip>

If you want to see the full source you can head over to
http://sourceforge.net/projects/gsqleditor/, but I warn you it is not
pretty.  When I wrote this I failed to understand about root widgets, so
I wrote a separate package and glade file for each dialog.  In addition
there is a POS install program I whipped up that modifies the source
code (to point it to the directory where you want the glade/module files
to go).

<perl file>
#!/usr/bin/perl

use strict;

#The brilliant Red Hat people put the modules
#here instead of in the site_perl directory
#which means that Perl 5.6.1 can't find them.
use lib "/usr/lib/perl5/5.6.0/i386-linux";
use Gnome;
use Gtk::GladeXML;

Gnome->init('helloworld');

my $app = Gtk::GladeXML->new('helloworld.glade', 'app');
$app->signal_autoconnect_from_package('main');

Gtk->main;

sub on_mi_exit_activate {
        Gtk->main_quit;
}

sub on_mi_about_activate {
        Gtk::GladeXML->new('helloworld.glade', 'about_box');
}

sub on_exit_clicked {
        Gtk->main_quit;
}

sub on_app_destroy {
        Gtk->main_quit;
}
</perl file>

<glade file>
<?xml version="1.0"?>
<GTK-Interface>

<project>
  <name>Helloworld</name>
  <program_name>helloworld</program_name>
  <directory></directory>
  <source_directory>src</source_directory>
  <pixmaps_directory>pixmaps</pixmaps_directory>
  <language>Perl</language>
  <gnome_support>True</gnome_support>
  <gettext_support>True</gettext_support>
</project>

<widget>
  <class>GnomeApp</class>
  <name>app</name>
  <signal>
    <name>destroy</name>
    <handler>on_app_destroy</handler>
    <last_modification_time>Sun, 14 Apr 2002 18:43:11 GMT</last_modification_time>
  </signal>
  <title>Helloworld</title>
  <type>GTK_WINDOW_TOPLEVEL</type>
  <position>GTK_WIN_POS_NONE</position>
  <modal>False</modal>
  <allow_shrink>False</allow_shrink>
  <allow_grow>True</allow_grow>
  <auto_shrink>False</auto_shrink>
  <enable_layout_config>True</enable_layout_config>

  <widget>
    <class>GnomeDock</class>
    <child_name>GnomeApp:dock</child_name>
    <name>dock1</name>
    <allow_floating>True</allow_floating>
    <child>
      <padding>0</padding>
      <expand>True</expand>
      <fill>True</fill>
    </child>

    <widget>
      <class>GnomeDockItem</class>
      <name>dockitem2</name>
      <border_width>2</border_width>
      <placement>GNOME_DOCK_TOP</placement>
      <band>0</band>
      <position>0</position>
      <offset>0</offset>
      <locked>False</locked>
      <exclusive>True</exclusive>
      <never_floating>False</never_floating>
      <never_vertical>True</never_vertical>
      <never_horizontal>False</never_horizontal>
      <shadow_type>GTK_SHADOW_OUT</shadow_type>

      <widget>
        <class>GtkMenuBar</class>
        <name>menubar1</name>
        <shadow_type>GTK_SHADOW_NONE</shadow_type>

        <widget>
          <class>GtkMenuItem</class>
          <name>file1</name>
          <stock_item>GNOMEUIINFO_MENU_FILE_TREE</stock_item>

          <widget>
            <class>GtkMenu</class>
            <name>file1_menu</name>

            <widget>
              <class>GtkPixmapMenuItem</class>
              <name>mi_exit</name>
              <signal>
                <name>activate</name>
                <handler>on_mi_exit_activate</handler>
                <last_modification_time>Sun, 14 Apr 2002 18:20:21 GMT</last_modification_time>
              </signal>
              <stock_item>GNOMEUIINFO_MENU_EXIT_ITEM</stock_item>
            </widget>
          </widget>
        </widget>

        <widget>
          <class>GtkMenuItem</class>
          <name>help1</name>
          <stock_item>GNOMEUIINFO_MENU_HELP_TREE</stock_item>

          <widget>
            <class>GtkMenu</class>
            <name>help1_menu</name>

            <widget>
              <class>GtkPixmapMenuItem</class>
              <name>mi_about</name>
              <signal>
                <name>activate</name>
                <handler>on_mi_about_activate</handler>
                <last_modification_time>Sun, 14 Apr 2002 18:20:21 GMT</last_modification_time>
              </signal>
              <stock_item>GNOMEUIINFO_MENU_ABOUT_ITEM</stock_item>
            </widget>
          </widget>
        </widget>
      </widget>
    </widget>

    <widget>
      <class>GnomeDockItem</class>
      <name>dockitem3</name>
      <border_width>1</border_width>
      <placement>GNOME_DOCK_TOP</placement>
      <band>1</band>
      <position>0</position>
      <offset>0</offset>
      <locked>False</locked>
      <exclusive>True</exclusive>
      <never_floating>False</never_floating>
      <never_vertical>False</never_vertical>
      <never_horizontal>False</never_horizontal>
      <shadow_type>GTK_SHADOW_OUT</shadow_type>

      <widget>
        <class>GtkToolbar</class>
        <name>toolbar1</name>
        <border_width>1</border_width>
        <orientation>GTK_ORIENTATION_HORIZONTAL</orientation>
        <type>GTK_TOOLBAR_BOTH</type>
        <space_size>16</space_size>
        <space_style>GTK_TOOLBAR_SPACE_LINE</space_style>
        <relief>GTK_RELIEF_NONE</relief>
        <tooltips>True</tooltips>

        <widget>
          <class>GtkButton</class>
          <child_name>Toolbar:button</child_name>
          <name>exit</name>
          <signal>
            <name>clicked</name>
            <handler>on_exit_clicked</handler>
            <last_modification_time>Sun, 14 Apr 2002 18:19:22 GMT</last_modification_time>
          </signal>
          <label>Exit</label>
          <stock_pixmap>GNOME_STOCK_PIXMAP_EXIT</stock_pixmap>
        </widget>
      </widget>
    </widget>

    <widget>
      <class>GtkLabel</class>
      <child_name>GnomeDock:contents</child_name>
      <name>label1</name>
      <label>Hello World</label>
      <justify>GTK_JUSTIFY_CENTER</justify>
      <wrap>False</wrap>
      <xalign>0.5</xalign>
      <yalign>0.5</yalign>
      <xpad>0</xpad>
      <ypad>0</ypad>
    </widget>
  </widget>

  <widget>
    <class>GnomeAppBar</class>
    <child_name>GnomeApp:appbar</child_name>
    <name>appbar1</name>
    <has_progress>True</has_progress>
    <has_status>True</has_status>
    <child>
      <padding>0</padding>
      <expand>True</expand>
      <fill>True</fill>
    </child>
  </widget>
</widget>

<widget>
  <class>GnomeAbout</class>
  <name>about_box</name>
  <modal>True</modal>
  <copyright>Copyleft 2002</copyright>
  <authors>Chas Owens
</authors>
  <comments>hahahhahah, today Hello, tomorrow the world.</comments>
</widget>

</GTK-Interface>
</glade file>

-- 
Today is Setting Orange the 32nd day of Discord in the YOLD 3168
Kallisti!

Missile Address: 33:48:3.521N  84:23:34.786W




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]