The syntax of Gnome2::Canvas::Line?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

The syntax of Gnome2::Canvas::Line?

walt-2
Hi list,

This is an example from the tarball of how to draw a line:

Gnome2::Canvas::Item->new ($group,
                           'Gnome2::Canvas::Line',
                           points => [200.0, 0.0, 200.0, 450.0],
                           fill_color => 'black',
                           width_units => 4.0);

My puzzle is why the syntax is any different from Rectangle, i.e.
why use 'points' instead of x1,y1,x2,y2?

Second, the use of brackets instead of () to describe points is
a complete surprise to me.  I'm new to perl and I've never seen
that syntax used in any of the perldocs I've read (so far).
What's the difference in this context between [] and ()?

Thanks.

_______________________________________________
gtk-perl-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: The syntax of Gnome2::Canvas::Line?

Sergei Steshenko-2


Applications From Scratch: http://appsfromscratch.berlios.de/


--- On Sat, 6/14/08, walt <[hidden email]> wrote:

> From: walt <[hidden email]>
> Subject: The syntax of Gnome2::Canvas::Line?
> To: [hidden email]
> Date: Saturday, June 14, 2008, 7:57 AM
> Hi list,
>
> This is an example from the tarball of how to draw a line:
>
> Gnome2::Canvas::Item->new ($group,
>   'Gnome2::Canvas::Line',
>                   points => [200.0, 0.0, 200.0,
> 450.0],
>   fill_color => 'black',
>   width_units => 4.0);
>
> My puzzle is why the syntax is any different from
> Rectangle, i.e.
> why use 'points' instead of x1,y1,x2,y2?
>
> Second, the use of brackets instead of () to describe
> points is
> a complete surprise to me.  I'm new to perl and
> I've never seen
> that syntax used in any of the perldocs I've read (so
> far).
> What's the difference in this context between [] and
> ()?
>
> Thanks.
>
> _______________________________________________
> gtk-perl-list mailing list
> [hidden email]
> http://mail.gnome.org/mailman/listinfo/gtk-perl-list


(...) means list; [...] means anonymous array reference.

You'll have to read a tutorial on Perl references, but before you need
to understand Perl in its referenceless part.

Probably dealing with GUI stuff is too complex for a newbie.

Regards,
  Sergei.


     
_______________________________________________
gtk-perl-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: The syntax of Gnome2::Canvas::Line?

zentara-2
In reply to this post by walt-2
On Sat, 14 Jun 2008 07:57:52 -0700
walt <[hidden email]> wrote:

>Hi list,
>
>This is an example from the tarball of how to draw a line:
>
>Gnome2::Canvas::Item->new ($group,
>   'Gnome2::Canvas::Line',
>                   points => [200.0, 0.0, 200.0, 450.0],
>   fill_color => 'black',
>   width_units => 4.0);
>
>My puzzle is why the syntax is any different from Rectangle, i.e.
>why use 'points' instead of x1,y1,x2,y2?
>
>Second, the use of brackets instead of () to describe points is
>a complete surprise to me.  I'm new to perl and I've never seen
>that syntax used in any of the perldocs I've read (so far).
>What's the difference in this context between [] and ()?

Look at this example. The points option allows you to change them later
by modifying the arrayref.  The difference from the rect is you can have
as many point pairs as you want.  In general, Lines on a canvas is a general purpose
curve.  The curve can be smoothed or angular (different canvas widgets handle
smotthing differently, Tk and Zinc will let you smooth any set of line points to make
bezier curves.)

I just posted a recent question about making polylines on a Goo::Canvas,
and it was a bit tricky, because it didn't setup the points option by default,
requiring you to create the polyline with the points undef, and then set the points
option after creation

my $line = Goo::Canvas::Polyline->new(                                                    
        $root, FALSE,                                                                    
         undef, # points need to be set after creation                                    
        'stroke-color' => 'black',                                                        
        'line-width' => 3,                                                                
        'start-arrow' => TRUE,                                                            
        'end-arrow' => TRUE,                                                              
        'arrow-tip-length' => 3,                                                          
        'arrow-length' => 4,                                                              
        'arrow-width' => 3.5                                                              
    );                                                                                    
                                                                                         
# setting after line creation, sets the 'points' property by name                        
$line->set(points => $points);                                                            
                                   
But.............
Gnome2::Canvas::Line makes it easier by setting the points option as default.

#!/usr/bin/perl
use warnings;
use strict;
use Gtk2 -init;
use Gnome2::Canvas;

my $window   = Gtk2::Window->new;
$window->signal_connect( destroy => sub { exit } );

my $scroller = Gtk2::ScrolledWindow->new;
my $canvas   = Gnome2::Canvas->new();
$scroller->add( $canvas );
$window->add( $scroller );
$window->set_default_size( 500, 500 );

#if scrollregion is smaller than default size, odd
# coordinate problems occur
#$canvas->set_scroll_region( -20, -20, 400, 400 ); #bad

$canvas->set_scroll_region( 0, 0, 700, 700 );
$window->show_all;

my $root = $canvas->root;

#$root->move(-50,-50);

my $text = Gnome2::Canvas::Item->new(
    $root, 'Gnome2::Canvas::Text',
   x          => 20,
   y          => 15,
   fill_color => 'black',
   font       => 'Sans 14',
   anchor     => 'GTK_ANCHOR_NW',
   text       => 'Click to add waypoint'
);

$canvas->signal_connect (event => \&event_handler);

my $points = [10,10,100,100,250,50];

my $line2= Gnome2::Canvas::Item->new ($root,
                'Gnome2::Canvas::Line',
          points => $points,
                fill_color => "red",
                width_units => 3.0,
                cap_style => 'projecting',
          join_style => 'miter',
                );

my $p = $line2->get('points');
print "@$p\n";
my ($x,$y) = @$p;


Gtk2->main;

##############################

sub event_handler{
     my ( $widget, $event ) = @_;
#     print $widget ,' ',$event->type,"\n";

    if ( $event->type eq "button-press" ) {
        print 'x->',$event->x,'  ','y->',$event->y,"\n";

      my ($x,$y) = ($event->x,$event->y);
#      my (undef, $x, $y, $state) =  $event->window->get_pointer;
     
      print "$x  $y\n";
     
        push @$points,$event->x , $event->y;
        $line2->set(points=>$points);

        my $p = $line2->get('points');
        print "@$p\n";
 
    }    
}

__END__


zentara

--
I'm not really a human, but I play one on earth.
http://zentara.net/CandyGram_for_Mongo.html 
_______________________________________________
gtk-perl-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: The syntax of Gnome2::Canvas::Line?

walt-2
zentara wrote:
> ...
>
> I just posted a recent question about making polylines on a Goo::Canvas,
> ...

Yes, thanks!  Without it I don't think I would have found the example I
cited in my question.

> Gnome2::Canvas::Line makes it easier by setting the points option as default.
>
> ...
> my $points = [10,10,100,100,250,50];
>
> my $line2= Gnome2::Canvas::Item->new ($root,
> 'Gnome2::Canvas::Line',
> points =>  $points,

Hm.  That example would have been very helpful if I'd seen it earlier.
I don't see it in the sourceforge online docs -- did you write it yourself?

Speaking of docs, the online docs do refer to Gnome2::Canvas::Points. Does
that imply the existence of a Points.pod file somewhere?  I can't find it
anywhere online or in the Canvas tarball.

Thanks also to Sergei for a very helpful reply.  The README has lots to
say about anonymous references and hashes that will help me a lot.  I'm
off to read it now.

_______________________________________________
gtk-perl-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: The syntax of Gnome2::Canvas::Line?

zentara-2
On Sat, 14 Jun 2008 11:37:04 -0700
walt <[hidden email]> wrote:

>zentara wrote:
>> ...
>>
>> I just posted a recent question about making polylines on a Goo::Canvas,
>> ...
>
>Yes, thanks!  Without it I don't think I would have found the example I
>cited in my question.
>
>> Gnome2::Canvas::Line makes it easier by setting the points option as default.
>>
>> ...
>> my $points = [10,10,100,100,250,50];
>>
>> my $line2= Gnome2::Canvas::Item->new ($root,
>> 'Gnome2::Canvas::Line',
>> points =>  $points,
>
>Hm.  That example would have been very helpful if I'd seen it earlier.
>I don't see it in the sourceforge online docs -- did you write it yourself?

I took it out of the demo, and modified it.  The demos are where
you find the real code examples.  I just take what is in the demo, and start
experimenting.... working around error messages, until I find what works.

>
>Speaking of docs, the online docs do refer to Gnome2::Canvas::Points. Does
>that imply the existence of a Points.pod file somewhere?  I can't find it
>anywhere online or in the Canvas tarball.

I havn't seen docs for Gnome2::Canvas::Points. But I would assume it is just
an object wrapper for the array reference of point pairs.

Goo::Canvas has a doc for Points, but it is basically empty. As you can see from my
PolyLine waypoint example, Goo::Canvas::Points is just an object wrapper around
the array_ref. It only has the new method, and dosn't seem to allow modification.
The problem being that a Goo PolyLine needs a Points object to modify the line,
it won't just let you redefine the array_ref. Whereas the Gnome2::Canvas::Line
does allow modification of the array_ref without the object wrapper.
 But the Goo is still in development, so I let it slide. :-)  
I'm just glad the author has made a Perl module for it.


zentara



--
I'm not really a human, but I play one on earth.
http://zentara.net/CandyGram_for_Mongo.html 
_______________________________________________
gtk-perl-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: The syntax of Gnome2::Canvas::Line?

muppet-6
In reply to this post by walt-2

On Jun 14, 2008, at 2:37 PM, walt wrote:

> Speaking of docs, the online docs do refer to  
> Gnome2::Canvas::Points. Does
> that imply the existence of a Points.pod file somewhere?  I can't  
> find it
> anywhere online or in the Canvas tarball.

GnomeCanvas's C API has a Point data structure, and methods to deal  
with arrays of these things.  In perl, such stuff is really annoying,  
because it's much easier and more natural to use native perl arrays  
and data structures.  So, the bindings map the GnomeCanvasPoint to an  
array reference, and the Gnome::Canvas::Points package exists only as  
an implementation detail --- it should be hidden from the docs.  Oops.


http://gtk2-perl.cvs.sourceforge.net/gtk2-perl/gtk2-perl-xs/GnomeCanvas/xs/GnomeCanvasUtil.xs?revision=1.7&view=markup


If the [] style arrays are giving you trouble, i recommend reading  
perlref and the Data Structures Cookbook, perldsc.  You really can't  
use gtk2-perl effectively without understanding perl data structures  
intimately.


--
I've been using emacs for 20+ years and have barely touched lisp.  I  
wouldn't know lambda calculus if it took all its clothes off and waved  
a placard that reads "I am lambda calculus" in blinking 48-point Comic  
Sans.
   -- Dave Hodgkinson, on london.pm

_______________________________________________
gtk-perl-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: The syntax of Gnome2::Canvas::Line?

walt-2
muppet wrote:

> ...
> If the [] style arrays are giving you trouble, i recommend reading
> perlref and the Data Structures Cookbook, perldsc. You really can't use
> gtk2-perl effectively without understanding perl data structures
> intimately.

Wow, muppet, Sergei, zentara, you folks are wonderful.  Thanks to your
great help I'm now looking at a bar chart of the Dow Jones Industrial
Average from 1929 to today :o)  Of course I still have a long way to
go, but to get this far in less than a week is beyond my expectations.

Many of my remaining questions are about perl, not about gtk, so where
would you go for advice when/if you can't understand the perl docs?

_______________________________________________
gtk-perl-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: The syntax of Gnome2::Canvas::Line?

Grant McLean
On Sun, 2008-06-15 at 14:36 -0700, walt wrote:
> Many of my remaining questions are about perl, not about gtk, so where
> would you go for advice when/if you can't understand the perl docs?

With regard to understanding references, there is a references tutorial
in the documentation that ships with Perl.  You can read it with this
command:

  perldoc perlreftut

Or online here:

  http://search.cpan.org/dist/perl/pod/perlreftut.pod

As far as asking questions goes, I'd heartily recommend Perl Monks.
It's perhaps not the world's most beautifully designed web site but
there are plenty of knowledgeable folk in many different timezones eager
to answer questions around the clock:

  http://www.perlmonks.org/

Chances are your question has been answered before, so you might find an
answer with Super Search:

  http://www.perlmonks.org/index.pl?node=Super%20Search

Failing that, you can go to the 'Seekers of Perl Wisdom' section and
pose your question there:

  http://www.perlmonks.org/index.pl?node=Seekers%20of%20Perl%20Wisdom

Cheers
Grant

_______________________________________________
gtk-perl-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: The syntax of Gnome2::Canvas::Line?

Sergei Steshenko-2
In reply to this post by walt-2


Applications From Scratch: http://appsfromscratch.berlios.de/


--- On Sun, 6/15/08, walt <[hidden email]> wrote:

> From: walt <[hidden email]>
> Subject: Re: The syntax of Gnome2::Canvas::Line?
> To: [hidden email]
> Date: Sunday, June 15, 2008, 2:36 PM
> muppet wrote:
>
> > ...
> > If the [] style arrays are giving you trouble, i
> recommend reading
> > perlref and the Data Structures Cookbook, perldsc. You
> really can't use
> > gtk2-perl effectively without understanding perl data
> structures
> > intimately.
>
> Wow, muppet, Sergei, zentara, you folks are wonderful.
> Thanks to your
> great help I'm now looking at a bar chart of the Dow
> Jones Industrial
> Average from 1929 to today :o)  Of course I still have a
> long way to
> go, but to get this far in less than a week is beyond my
> expectations.
>
> Many of my remaining questions are about perl, not about
> gtk, so where
> would you go for advice when/if you can't understand
> the perl docs?
>
> _______________________________________________
> gtk-perl-list mailing list
> [hidden email]
> http://mail.gnome.org/mailman/listinfo/gtk-perl-list

Personal advice WRT references: for the beginning, forget about "->"
as a way to dereference.

Remember just _one_ simple rule: reference inside a pair of curlies
behaves like regular name.

Examples:

use strict;

my %hash =
  (
  one => 1,
  two => 2
  );

warn "\$hash{one}=$hash{one}"; will print 1

my $hash_ref = \%hash;

warn "\${\$hash_ref}{one}=${$hash_ref}"; # will also print 1
.

The point is that

hash is equivalent to {$hash_ref}

- as I wrote above.

The same rule applies to array and code references.

Regards,
  Sergei.


     
_______________________________________________
gtk-perl-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: The syntax of Gnome2::Canvas::Line?

walt-2
In reply to this post by Grant McLean
Grant McLean wrote:
> On Sun, 2008-06-15 at 14:36 -0700, walt wrote:
>> Many of my remaining questions are about perl, not about gtk, so where
>> would you go for advice when/if you can't understand the perl docs?

> ...
> As far as asking questions goes, I'd heartily recommend Perl Monks.
> It's perhaps not the world's most beautifully designed web site but
> there are plenty of knowledgeable folk in many different timezones eager
> to answer questions around the clock:
>    http://www.perlmonks.org/
 > ...

I just posted my first question there and by the time I finished fumbling
around with re-formatting and updating my question it already had three
long and thoughtful answers!  When I saw that number '3 replies' I just
assumed I had accidentally replied to myself instead of updating, but
no, there were three real replies.  I'm completely flabbergasted.

Feels like I've found a new home in the perl community.  I regret now
that I put off learning perl for so long just because I have a nerdy
friend who complained that he'd never written a single line of perl
without the reference manual propped open in front of the monitor :o)

Many thanks, guys!

_______________________________________________
gtk-perl-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list