Re: Colormaps in pixbufs which are in treeview



On Thu, 08 Feb 2007 13:33:11 +0200
Ari Jolma <ari jolma TKK FI> wrote:

I have this piece of code:

$pm = Gtk2::Gdk::Pixmap->new(undef,10,10,24);
$gc = new Gtk2::Gdk::GC $pm;
$gc->set_rgb_fg_color(Gtk2::Gdk::Color->new(0,0,0));
$pm->draw_rectangle($gc,1,0,0,10,10);
$pb = Gtk2::Gdk::Pixbuf->get_from_drawable($pm,undef,0,0,0,0,10,10);

The idea is to create small (10x10) pixbufs with varying colors for 
inserting them into the model of a treeview. The code works in Linux and 
Windows - usually, but in a specific Windows environment the following 
error occurs:

Gdk-WARNING **: gdk_gc_set_rgb_fg_color() and gdk_gc_set_rgb_bg_color() can
only be used on GC's with a colormap. A GC will have a colormap
if it is created for a drawable with a colormap, or if a
colormap has been set explicitly with gdk_gc_set_colormap.
Gdk-WARNING **: ../../../gtk+/gdk/gdkpixbuf-drawable.c:1249: Source 
drawable has
no colormap; either pass in a colormap, or set the colormap on the 
drawable wit
h gdk_drawable_set_colormap()

It looks like I need to call $gc->set_colormap($color_map), and 
$color_map = Gtk2::Gdk::Colormap->new ($visual, $allocate) before that, 
but what should I use for $visual and $allocate?

How do I test those cases where I need to do that?

Hi, I don't think you test for it, you just always satisfy it by always
providing a colormap and gc derived from the Gdk window.

sub draw_poly{ 
  my($widget,$points,$color) = @_; 
  # see Gdk::Gdk::Window, Gtk2::Gdk::Drawable, Gtk2::Gdk::GC 
    my $colormap = $widget->window->get_colormap; 
    my $gc = $widget->{gc} || new Gtk2::Gdk::GC $widget->window; 
    $gc->set_foreground(get_color($colormap, $color)); 
    $widget->window->draw_polygon($gc,1, @$points); 
}                                                                                         
                 

It seemed that always doing this for each call was redundent,
so I used something like this, gleaned from 
some examples from the module.  What you do is keep a hash
of the colormaps, and gc's.

my %gc; 
my %colormap; 
my %allocated_colors; 
my $linecolor  = 'red'; 
my $axiscolor  = 'black'; 
my %background =( 'm' => 'white', 
                                 't' => 'cornsilk', 
                                 'l' => 'lightcyan', 
                                 'c' => 'blue', );    

# sample
 my $color = Gtk2::Gdk::Color->new ($r,$g,$b); 

 &mydraw_points('m',[$x1,$y1,                                                      
                                        $x1,$y1-1,                                                     
                                        $x1,$y1+1,                                                     
                                        ], $color );         


sub mydraw_points{ 
    my ( $id, $coords, $color ) = @_; 
    $colormap{ $id }->alloc_color( $color, TRUE, TRUE ); 
    $gc{$id}->set_foreground( $color );                                                    
                                                                                          
   $pixmap{ $id }->draw_points( $gc{ $id }, @$coords );
   $area{ $id }->queue_draw; 
   return 0; }       

sub get_color{ 
  my ( $name, $id ) = @_; 
  $id ||='m'; 
  my $ret; if ( $ret = $allocated_colors{ $name } ) { return $ret } 
  my $color =
  Gtk2::Gdk::Color->parse( $name ); $colormap{ $id }->alloc_color( $color, TRUE, TRUE ); 
  $allocated_colors{ $name } = $color; 
return $color; 
}                                                                                         
##########################################################        

Since this all has a bunch of interactions which are hard to follow,
I've attached an example. There is a server and client. Start the
server, then the client.  You can look at the server code to see
how I created and saved the hashes.

zentara


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

Attachment: client-test-sine
Description: Binary data

Attachment: server-graph2-axis
Description: Binary data



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