#!/usr/bin/perl -w

package Gtk2::CellRendererPixText;

use strict;
use warnings;
use Glib qw(G_PARAM_READWRITE);
use Gtk2;
use Glib::Object::Subclass
	"Gtk2::CellRenderer",
	properties => [
	  Glib::ParamSpec->object ('pixbuf',
	                           'Pixbuf',
	                           'The pixbuf to display',
				   'Gtk2::Gdk::Pixbuf', G_PARAM_READWRITE),
	  Glib::ParamSpec->string ('text',
	                           'Text',
	                           'The text string to display',
				   "", G_PARAM_READWRITE),
	  Glib::ParamSpec->boolean('weight_set',
	                           'Weight set',
	                           'Should the text be printed in bold?',
				   0, G_PARAM_READWRITE),
	  Glib::ParamSpec->string ('font-string',
	                           'Font String',
	                           'The font used',
				   'Fixedsys 12', G_PARAM_READWRITE),
	],
  ;


sub INIT_INSTANCE {
	my $self = shift;
}

sub MAX { $_[0] > $_[1] ? $_[0] : $_[1] }

sub GET_SIZE {
	my ($cell, $widget, $cell_area) = @_;
	my ($x_offset, $y_offset) = (0, 0);
	
	my $width  = 100;
	my $height = 25;
	
#	if ($cell_area) {
#		$x_offset = $cell->get ('xalign') * ($cell_area->width - $width);
#		$x_offset = MAX ($x_offset, 0);
#		
#		$y_offset = $cell->get ('yalign') * ($cell_area->height - $height);
#		$y_offset = MAX ($y_offset, 0);
#	}
	
	return ($x_offset, $y_offset, $width, $height);
}

sub RENDER {
	my ($cell, $window, $widget, $background_area, $cell_area, $expose_area, $flags) = @_;
	my $pix_width  = $cell->get('pixbuf')->get_width;
	my $pix_height = $cell->get('pixbuf')->get_height;

	if ($pix_width > 1) {
		$window->draw_pixbuf(
				Gtk2::Gdk::GC->new(Gtk2::Gdk::Pixmap->new(undef,$pix_width,$pix_height,8)),
				$cell->get("pixbuf"),
				0,
				0,
				$cell_area->x(),
				$cell_area->y() + int(($cell_area->height - $pix_height)/2),
				$pix_width,
				$pix_height,
				"GDK_RGB_DITHER_NONE",
				0,
				0
		);
	} else {
		$pix_width=0;
	}

	my $font = Gtk2::Pango::FontDescription->from_string($cell->get("font-string"));
	if ($cell->{'weight_set'}) {
		$font->set_weight('PANGO_WEIGHT_BOLD');
	}
	my $layout = $widget->create_pango_layout("");
	$layout->set_text($cell->{'text'});
	$layout->set_font_description($font);

	$widget->get_style->paint_layout($window,
	                "normal",
	                1,
	                $cell_area,
	                $widget,
	                "cellrenderertext",
	                $cell_area->x() + $pix_width + 1,
	                $cell_area->y(),
	                $layout
	);
}

package main;

use strict;
use Glib qw(TRUE FALSE);
use Gtk2 -init;

use constant {
	PIX_COL => 0,
	TEXT_COL  => 1,
	BOLD_COL  => 2,
};

sub create_view_and_model {
	my $treestore = Gtk2::TreeStore->new(qw(Gtk2::Gdk::Pixbuf Glib::String Glib::Uint));
	
	my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file('image.xpm');
	
	my $iter = $treestore->append(undef);
	$treestore->set ($iter, PIX_COL, $pixbuf, TEXT_COL, "Row 1", BOLD_COL, 1);
	$pixbuf = Gtk2::Gdk::Pixbuf->new('GDK_COLORSPACE_RGB', 1, 8, 1, 1);
	$iter = $treestore->append($iter);
	$treestore->set ($iter, PIX_COL, $pixbuf, TEXT_COL, "Row 2", BOLD_COL, 1);
	$iter = $treestore->append(undef);
	$treestore->set ($iter, PIX_COL, $pixbuf, TEXT_COL, "Row 3", BOLD_COL, 0);
	
	my $view = Gtk2::TreeView->new ($treestore);
	
	my $renderer = Gtk2::CellRendererPixText->new;
	$renderer->set('font-string' => 'Luxi Sans 11');
	my $col = Gtk2::TreeViewColumn->new_with_attributes("Pixbuf and text", $renderer, "pixbuf" => PIX_COL, "text" => TEXT_COL, "weight_set" => BOLD_COL);
	$view->append_column ($col);
	
	return $view;
}


my $window = Gtk2::Window->new;
$window->set_default_size (150, 100);
$window->signal_connect (delete_event => sub {Gtk2->main_quit});

my $view = create_view_and_model();

$window->add ($view);

$window->show_all;

Gtk2->main;


