Gdk::Pixbuf in TreeView.



Hi all,
I'm trying to add a pixbuf (created on the fly) into a treeview.
I've two columns a text and an image. When i add rows with appropriate
data, I'm seeing the text, but not the image.

Attached is the code. Can somebody point out where I'm doing wrong?

Thanks in advance,
Surya


=================== FILE main.cc ====================

//$Id: main.cc 836 2007-05-09 03:02:38Z jjongsma $ -*- c++ -*-

/* gtkmm example Copyright (C) 2002 gtkmm development team
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <gtkmm/main.h>
#include "examplewindow.h"

int main(int argc, char *argv[])
{
  Gtk::Main kit(argc, argv);

  ExampleWindow window;
  //Shows the window and returns when it is closed.
  Gtk::Main::run(window);

  return 0;
}
==================================================


================ FILE ExampleWindow.cc ====================
//$Id: examplewindow.cc 836 2007-05-09 03:02:38Z jjongsma $ -*- c++ -*-

/* gtkmm example Copyright (C) 2002 gtkmm development team
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <iostream>
#include "examplewindow.h"

ExampleWindow::ExampleWindow()
	: m_Button_Quit("Quit"), m_add ("Add")
{
	set_title("Gtk::TreeView (ListStore) example");
	set_border_width(5);
	set_default_size(400, 200);

	add (m_VBox);

	//Add the TreeView, inside a ScrolledWindow, with the button underneath:
	m_ScrolledWindow.add(m_TreeView);

	//Only show the scrollbars when they are necessary:
	m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);

	m_VBox.pack_start(m_ScrolledWindow);
	m_VBox.pack_start(m_ButtonBox, Gtk::PACK_SHRINK);

	m_ButtonBox.pack_start(m_add, Gtk::PACK_SHRINK);
	m_ButtonBox.pack_start(m_Button_Quit, Gtk::PACK_SHRINK);
	m_ButtonBox.set_border_width(5);
	m_ButtonBox.set_layout(Gtk::BUTTONBOX_SPREAD);
	m_Button_Quit.signal_clicked().connect( sigc::mem_fun(*this,
				&ExampleWindow::on_button_quit) );

	m_add.signal_clicked().connect( sigc::mem_fun(*this,
				&ExampleWindow::add_rows) );
	//Create the Tree model:
	m_refTreeModel = Gtk::ListStore::create(m_Columns);
	m_TreeView.set_model(m_refTreeModel);

	show_all_children();
}

void ExampleWindow :: add_rows (void)
{
	Gtk::TreeModel::Row row = *(m_refTreeModel->append());

	Glib::RefPtr<Gdk::Pixbuf> pixbuf ;
	Gdk::Color color ;

	color.set_rgb_p (1.0, 0.0, 0.0) ;
	pixbuf = create_from_color (color) ;
	row[m_Columns.m_name] = "Red";
	row[m_Columns.m_image] = pixbuf ;
	pixbufs.push_back (pixbuf) ;

	row = *(m_refTreeModel->append());
	color.set_rgb_p (0.0, 1.0, 0.0) ;
	pixbuf = create_from_color (color) ;
	row[m_Columns.m_name] = "Green";
	row[m_Columns.m_image] = pixbuf ;
	pixbufs.push_back (pixbuf) ;
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow :: on_realize (void)
{
	Gtk::CellRendererPixbuf *crpb = new Gtk::CellRendererPixbuf ;
	int cols = m_TreeView.append_column ("Color", *crpb) ;
	Gtk::TreeViewColumn* pc = m_TreeView.get_column (cols - 1) ;
	pc->add_attribute (crpb->property_pixbuf(), m_Columns.m_image) ;

	m_TreeView.append_column("Name", m_Columns.m_name);
	Gtk::Window::on_realize () ;
}

void ExampleWindow::on_button_quit()
{
	hide();
}

Glib::RefPtr<Gdk::Pixbuf> ExampleWindow :: create_from_color (const
Gdk::Color& c)
{
	int width (40), height (8) ;

	unsigned char* data = (unsigned char*) calloc(width*height, 4);
	Glib::RefPtr<Gdk::Pixmap> pixmap = Gdk::Pixmap::create (
			get_window(),
			width,
			height
			) ;

	Cairo::RefPtr<Cairo::Context> context = pixmap->create_cairo_context() ;

	context->set_source_rgb (c.get_red_p(), c.get_green_p(), c.get_blue_p()) ;
	context->rectangle (0, 0, width, height) ;
	context->paint() ;

	Glib::RefPtr<Gdk::Image> image = pixmap->get_image(0, 0, width, height) ;
	Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create (
			image, 0, 0, width, height) ;

	return pixbuf ;
}
======================================================

================ File ExampleWindow.h =========================
//$Id: examplewindow.h 705 2006-07-19 02:55:32Z jjongsma $ -*- c++ -*-

/* gtkmm example Copyright (C) 2002 gtkmm development team
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include <gtkmm.h>
#include <vector>
using namespace std ;

class ExampleWindow : public Gtk::Window
{
public:
  ExampleWindow();
  virtual ~ExampleWindow();

protected:
  //Signal handlers:
  virtual void on_button_quit();
  virtual void on_realize (void) ;

  //Tree model columns:
  class ModelColumns : public Gtk::TreeModel::ColumnRecord
  {
  public:

    ModelColumns()
    { add(m_image); add(m_name); }

    Gtk::TreeModelColumn< Glib::RefPtr<Gdk::Pixbuf> > m_image;
    Gtk::TreeModelColumn<Glib::ustring> m_name;
  };

  ModelColumns m_Columns;

  //Child widgets:
  Gtk::VBox m_VBox;

  Gtk::ScrolledWindow m_ScrolledWindow;
  Gtk::TreeView m_TreeView;
  Glib::RefPtr<Gtk::ListStore> m_refTreeModel;

  Gtk::HButtonBox m_ButtonBox;
  Gtk::Button m_Button_Quit;
  Gtk::Button m_add ;

private:

  void add_rows (void) ;
  Glib::RefPtr<Gdk::Pixbuf> create_from_color (const Gdk::Color& c) ;
  vector< Glib::RefPtr<Gdk::Pixbuf> > pixbufs ;
};

#endif //GTKMM_EXAMPLEWINDOW_H
=====================================


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