GladeXML and "widget is not of type Gtk::Widget"
- From: Tom Jennings <tomj wps com>
- To: gtk-perl-list gnome org
- Subject: GladeXML and "widget is not of type Gtk::Widget"
- Date: Thu, 19 Dec 2002 19:22:57 -0800 (PST)
This may be one of those it-goes-away-in-the-morning type problems,
but I'm quite stumped.
I'm trying to puzzle out GladeXML so I can use it on a new project.
Iv'e taken the "CList" example from the Gtk Perl Tutorial and
making the *tiniest* incremental changes to create the widgets
from GladeXML.
The crux of the problem is this:
$window= (new window...) # creates window OK
$vbox= $app-> get_widget ('vbox'); # no error here,
$window-> add( $vbox ); # FAILS
This error results:
widget is not of type Gtk::Widget at ./test line 39.
What fails is the "window-> add" method call. If I change the
code to $vbox= new Gtk::VBox... it works. Which I thought gladexml
'get_widget' returned type Gtk::Widget?
The XML definition is:
<widget>
<class>GtkVBox</class>
<name>vbox</name>
</widget>
The actual source and XML follows. I de-heirarchicalized (sic)
the XML for testing. I realize GladXML has no "CList" widget,
I'll do it with old List.
Thanks for any insight, and sorry for the long message.
tomj
HACKED DEMO -- see "# tomj FAILS"
#!/usr/bin/perl -w
use Gtk;
use strict;
use Gnome;
use Gtk::GladeXML; # tomj
set_locale Gtk;
init Gtk;
my $false = 0;
my $true = 1;
my $window;
my $vbox;
my $hbox;
my $scrolled_window;
my $clist;
my $button_add;
my $button_clear;
my $button_hide_show;
my $titles_visible = $true;
my @titles = ( "Ingredients", "Amount" );
# Create the window
#$window = new Gtk::Window( "toplevel" ); # tomj
my $app= Gtk::GladeXML->new('test.glade', 'welcome'); # tomj
$window= $app-> get_widget ('welcome'); # tomj
$window->set_usize( 300, 150 );
$window->signal_connect( "destroy", sub { Gtk->exit( 0 ); } );
#$vbox = new Gtk::VBox( $false, 5 ); # tomj
$vbox= $app-> get_widget ('vbox'); # tomj
$window->add( $vbox ); # tomj FAILS
$vbox->border_width( 5 );
$vbox->show();
# Create a scrolled window to pack the CList widget into
$scrolled_window = new Gtk::ScrolledWindow( undef, undef );
$vbox->pack_start( $scrolled_window, $true, $true, 0 );
$scrolled_window->set_policy( 'automatic', 'always' );
$scrolled_window->show();
# Create the CList. For this example we use 2 columns
$clist = new_with_titles Gtk::CList( @titles );
#$clist= $app-> get_widget ('repList');
# When a selection is made, we want to know about it. The callback
# used is selection_made, and its code can be found further down
$clist->signal_connect( "select_row", \&selection_made );
# It isn't necessary to shadow the border, but it looks nice :)
$clist->set_shadow_type( 'out' );
# What however is important, is that we set the column widths as
# they will never be right otherwise. Note that the columns are
# numbered from 0 and up (to 1 in this case).
$clist->set_column_width( 0, 150 );
# Add the CList widget to the vertical box and show it.
$scrolled_window->add( $clist );
$clist->show();
# Create the buttons and add them to the window.
$hbox = new Gtk::HBox( $false, 0 );
$vbox->pack_start( $hbox, $false, $true, 0 );
$hbox->show();
$button_add = new Gtk::Button( "Add List" );
$button_clear = new Gtk::Button( "Clear List" );
$button_hide_show = new Gtk::Button( "Hide/Show Titles" );
$hbox->pack_start( $button_add, $true, $true, 0 );
$hbox->pack_start( $button_clear, $true, $true, 0 );
$hbox->pack_start( $button_hide_show, $true, $true, 0 );
# Connect our callbacks to the three buttons
$button_add->signal_connect( "clicked", \&button_add_clicked, $clist );
$button_clear->signal_connect( "clicked", sub { $clist->clear(); } );
$button_hide_show->signal_connect( "clicked", \&button_hide_show_clicked, $clist );
$button_add->show();
$button_clear->show();
$button_hide_show->show();
$window->show();
main Gtk;
exit( 0 );
### Subroutines
# User clicked the "Add List" button.
sub button_add_clicked
{
my ( $widget, $clist ) = @_;
# Something silly to add to the list. 4 rows of 2 columns each.
my @drink = ( [ Milk => "3 Oz" ],
[ Water => "6 l" ],
[ Carrots => "2" ],
[ Snakes => "55" ] );
# Here we do the actual adding of the text. It's done once for each row.
for my $i ( 0..3 )
{
$clist->append( @{$drink[ $i ]} );
}
# For those who may not have understood the above, here is a brief
# explanation. @drink is a list of references to anonymouse lists
# (references are scalars holding a memory address, similar to
# pointers). This is used because Perl doesn't directly support
# two-dimensional arrays. Because of this, $drink[$i] is not a
# list, but a reference to one, so we must dereference it by
# enclosing it like this: @{ reference } ( we use @ because it is a
# reference to a list. For further information on references, see
# the perlref(1) man page, or chapter 4 of _Programming Perl_.
return;
}
# The user clicked the "Hide/Show titles" button.
sub button_hide_show_clicked
{
my ( $widget, $clist ) = @_;
if ( $titles_visible )
{
# Hide the titles and set the flag to 1
$clist->column_titles_hide();
$titles_visible++;
}
else
{
# Show the titles and reset flag to 0
$clist->column_titles_show();
$titles_visible--;
}
}
# If we come here, then the user has selected a row in the list.
sub selection_made
{
my ( $clist, $row, $column, $event, @data ) = @_;
my $text;
# Get the text that is stored in the selected row and column
# which was clicked in. We will receive it as a pointer in the
# argument text.
$text = $clist->get_text( $row, $column );
# Just prints some information about the selected row
print( "You selected row $row. More specifically, you clicked in\n" );
print( "column $column, and the text in this cell is:\n" );
print( " $text\n\n" );
return;
}
<?xml version="1.0"?>
<GTK-Interface>
<widget>
<class>GtkWindow</class>
<name>welcome</name>
<title>welcome</title>
</widget>
<widget>
<class>GtkVBox</class>
<name>vbox</name>
</widget>
<widget>
<class>GtkScrolledWindow</class>
<name>listWindow</name>
<hscrollbar_policy>GTK_POLICY_AUTOMATIC</hscrollbar_policy>
<vscrollbar_policy>GTK_POLICY_AUTOMATIC</vscrollbar_policy>
<hupdate_policy>GTK_UPDATE_CONTINUOUS</hupdate_policy>
<vupdate_policy>GTK_UPDATE_CONTINUOUS</vupdate_policy>
</widget>
<widget>
<class>CList</class>
<name>repList</name>
<border_width>5</border_width>
<handle_size>10</handle_size>
<gutter_size>6</gutter_size>
<position>171</position>
</widget>
</GTK-Interface>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]