subclassed anti-alias canvas, and disappearing text



Hi,

I made a little Tk directory subtree browser, and now I want to 
make a Gtk2 version. The basic idea is to make it look like a Gtk2 
treeview, with a background image.

In case you want to see what I'm trying to emulate, see
http://zentara.net/perlplay/modules/Tk-CanvasDirTree-0.04.tar.gz

Anyways the conversion is going pretty well, except I've run into a bug
with text not showing on the anti-alias canvas.

I've made the code text code, the same as in the Gnome2::Canvas demo.
It works when I use the plain subclassed canvas, but when I use
aa=>1 , I get a slew of errors

Pango-CRITICAL **: pango_renderer_draw_layout: assertion `PANGO_IS_RENDERER (renderer)' failed at 
./zz-Gtk-CanvasTest line 215.
GLib-GObject-WARNING **: invalid cast from `PangoCairoFcFontMap' to `PangoFT2FontMap' at ./zz-Gtk-CanvasTest 
line 215.
GLib-GObject-WARNING **: invalid uninstantiatable type `(null)' in cast to `PangoFT2Renderer' at 
./zz-Gtk-CanvasTest line 215.

I remember reading a post by muppet, about needing a special syntax
when making a subclassed anti-alias canvas, where you need to use
->new(aa=>1)  rather than  ->new_aa.  So I'm thinking that this may be a similar 
problem.

So if you run this in a directory containing a few subdirectories, you should see
the problem. The main package code is at the bottom.

How do I get the aa canvas to display the text ?

Thanks for looking.
zentara

#!/usr/bin/perl
use warnings;
use strict;

package CanvasDirTree;

our $VERSION = '0.01';

use warnings;
use strict;

use Gnome2::Canvas;
use File::Spec;
use POSIX qw(DBL_MAX);
use Glib ':constants';

use Glib::Object::Subclass
    Gnome2::Canvas::,
    
    signals => {
         # currently no signals defined
       },

  properties => [
      {
          pspec => Glib::ParamSpec->string ('ztree',
                                            'Ztree',
                                            'subdir tree',
                                            "", # default
                                            [qw/readable writable/]),
          get => sub {
              my ($self) = @_;

          },

          set => sub {
              my ($self, $newval) = @_;
          },
      },
   ],
;

sub INIT_INSTANCE
{
    my $self = shift;

    $self->{color_blue} = Gtk2::Gdk::Color->parse('blue');
    $self->{color_red} = Gtk2::Gdk::Color->parse('red');

    $self->make_trunk('.', 0); 

    return $self;
}


sub SET_PROPERTY
{
    my ($self, $pspec, $newval) = @_;

    my $param_name = $pspec->get_name();
    print "$param_name\n";
    return;
}

##############################################################
sub get_subdirs{
  my ($self, $dir) = @_;

    my @subdirs;
    opendir my $dh, $dir or warn $!;

    while ( my $file = readdir($dh) ) {
        next if $file =~ m[^\.{1,2}$];
         if(-d "$dir/$file"){ 
           push @subdirs, $file;       
         }else{ next }
     }

  return @subdirs;
}
###########################################################
sub check_depth_2{
   my ($self, $abs_path) = @_;
    
   my $put_ind = 0;
    opendir my $dh, $abs_path or warn $!;
       while ( my $file = readdir($dh) ) {
         next if $file =~ m[^\.{1,2}$];
             if(-d "$abs_path/$file"){ 
                 $put_ind = 1;
                 last;        
                 }
        }
 return $put_ind;
}
#############################################################


sub make_trunk{
   my ($self, $dir, $level) = @_;

   my $x = 5; my $y = 15;

   my @subdirs = $self->get_subdirs( $dir  ); 
   my $abs_root = File::Spec->rel2abs( $dir );
   #for windows compat
   $abs_root =~ tr#\\#/#; 

   #handle special case when toplevel is / or C:/, D:/, etc
   if($abs_root eq '/'){$abs_root = ''}
   #for windows compat
   if ( $abs_root =~ m#^([ABCDEFGHIJKLMNOPQRSTUVWXYZ])\:\/$#  )
               {$abs_root = "$1:"} 

   #add a static entry for the topdir 
   my $root_tag;
   if($abs_root eq ''){$root_tag = '/'}else{ $root_tag = $abs_root }

   my $root = $self->root();
   $self->{'f_width'} = 15;
   $self->{'f_height'} = 15;


   my $topline = Gnome2::Canvas::Item->new ($root, 'Gnome2::Canvas::Line',
                   points => [
                 $x , $y - .8 * $self->{'f_height'},
                 $x + $self->{'f_height'}, $y - .8 * $self->{'f_height'},
                 $x + $self->{'f_height'}, $y - .4 * $self->{'f_height'},
                    ],
                   width_pixels => 2,
                   first_arrowhead => FALSE,
                   last_arrowhead => TRUE,
                   arrow_shape_a => 6.0,
                   arrow_shape_b => 6.0,
                   arrow_shape_c => 4.0,
                   fill_color_rgba => 0xff0000ff,
                 );


   my $max = scalar (@subdirs);
   my $count = 0;

    foreach my $subdir ( sort @subdirs ){
          my $abs_path = "$abs_root/$subdir";
          #see if any depth 2 subdir exists
          my $put_ind = $self->check_depth_2($abs_path);      

     if( $put_ind ){
       my $ind = Gnome2::Canvas::Item->new ($root, 'Gnome2::Canvas::Polygon',
                   points => [
                   $x + .1 * $self->{'f_width'} ,  $y + $y * $count - .3 * $self->{'f_height'}, 
                   $x +  .5 * $self->{'f_width'},  $y + $y * $count,
                   $x + .1 * $self->{'f_width'},  $y + $y * $count + .3 * $self->{'f_height'} ,
                    ],
                  fill_color_rgba => 0x0000ff80,
                  outline_color => 'black');


        $ind->signal_connect(event=>sub {print "I got an event!\n"; });
       }

        my $text = Gnome2::Canvas::Item->new ($root, 'Gnome2::Canvas::Text',
                                   text => $subdir,
                                   font => 'Sans 12',
                                   x => $x + .8 * $self->{'f_width'}, 
                                   y => $y + $y * $count + (.5 *$self->{'f_height'}),
                                   anchor => 'w', 
                                   fill_color => 'black');

        $count++;
   }
    
} # end make_trunk 
############################################################################    

1; # required at end of module
######################################################################

package main;

use strict;
use Gtk2 '-init';
$|++;

my $window = Gtk2::Window->new();
my $sw = Gtk2::ScrolledWindow->new();
 $sw->set_shadow_type ('etched-out');
 $sw->set_policy ('automatic', 'always');
 $sw->set_placement('top-right');
 $sw->set_size_request (130, 300);
 $sw->set_border_width(0);

 
 my $black = Gtk2::Gdk::Color->new (0x0000,0x0000,0x0000);
 my $white = Gtk2::Gdk::Color->new (0xFFFF,0xFFFF,0xFFFF);

##############################################################
  ### set this to use aa and errors flow #########################
 #for an aa canvas, need to use aa=>1, not new_aa
 #my $ztree = CanvasDirTree->new( aa=>1  );
 my $ztree = CanvasDirTree->new( );

 $ztree->set_scroll_region(0,0,700,700);

 $ztree->modify_bg('normal',$white);
 $ztree->set_center_scroll_region (0);

# $ztree->set_pixels_per_unit(5);

 $sw->add($ztree);
 $window->signal_connect('destroy'=>sub { Gtk2->main_quit(); });
 $window->set_default_size(450,350);
 $window->add($sw);
 $window->show_all();

 Gtk2->main();
__END__


-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html



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