Re: Treeview with editable checkboxes besides items?



Murray Cumming wrote:
On Tue, 2007-06-19 at 12:03 +0200, Jef Driesen wrote:
I still don't understand why I need to update the model myself with a CellRendererToggle. Because for a CellRendererText, all I need is:

renderer->property_editable() = true;

No, that won't be enough.

This is why we have the append_column_editable() helper methods:
http://www.gtkmm.org/docs/gtkmm-2.4/docs/reference/html/classGtk_1_1TreeView.html#83d537226bbbb054aa792c03248e619f

I don't know why, but it *does* work here. I modified one of the examples of the gtkmm book to illustrate what I'm doing (see code below).

For the CellRendererToggle, I need to use "signal_toggled" to make the checkbox editable and the value of "property_activatable" seems to be irrelevant.

For the CellRendererText, I only need to set "property_editable" without connecting to "signal_edited".

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

class ExampleWindow : public Gtk::Window {
public:
   ExampleWindow();
   virtual ~ExampleWindow();
protected:
   // Signal handlers:
   virtual void on_button_quit();
   virtual void on_cell_toggled (const Glib::ustring& path);
virtual void on_cell_edited (const Glib::ustring& path, const Glib::ustring& text);

   // Tree model columns:
   class ModelColumns : public Gtk::TreeModel::ColumnRecord {
   public:
      ModelColumns()
      {
         add(active);
         add(name);
      }
      Gtk::TreeModelColumn<bool> active;
      Gtk::TreeModelColumn<Glib::ustring> name;
   };
   ModelColumns m_columns;

   //Child widgets:
   Gtk::VBox m_VBox;

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

   Gtk::HButtonBox m_ButtonBox;
   Gtk::Button m_Button_Quit;
};

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

    return 0;
}


ExampleWindow::ExampleWindow()
   : m_Button_Quit("Quit")
{
   set_title("Gtk::TreeView (TreeStore) 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_Button_Quit, Gtk::PACK_SHRINK);
   m_ButtonBox.set_border_width(5);
   m_ButtonBox.set_layout(Gtk::BUTTONBOX_END);
   m_Button_Quit.signal_clicked().connect(sigc::mem_fun(*this,
               &ExampleWindow::on_button_quit) );

   //Create the Tree model:
   m_refTreeModel = Gtk::TreeStore::create(m_columns);
   m_TreeView.set_model(m_refTreeModel);

   //Fill the TreeView's model
   Gtk::TreeModel::Row row = *(m_refTreeModel->append());
   row[m_columns.active] = false;
   row[m_columns.name] = "Billy Bob";

Gtk::TreeModel::Row childrow = *(m_refTreeModel->append(row.children()));
   childrow[m_columns.active] = true;
   childrow[m_columns.name] = "Billy Bob Junior";

   childrow = *(m_refTreeModel->append(row.children()));
   childrow[m_columns.active] = false;
   childrow[m_columns.name] = "Sue Bob";

   row = *(m_refTreeModel->append());
   row[m_columns.active] = true;
   row[m_columns.name] = "Joey Jojo";

   row = *(m_refTreeModel->append());
   row[m_columns.active] = true;
   row[m_columns.name] = "Rob McRoberts";

   childrow = *(m_refTreeModel->append(row.children()));
   childrow[m_columns.active] = false;
   childrow[m_columns.name] = "Xavier McRoberts";

   //Add the TreeView's view columns:
   //m_TreeView.append_column_editable ("Active", m_columns.active);
   //m_TreeView.append_column ("Name", m_columns.name);

Gtk::CellRendererToggle* renderer_active = Gtk::manage( new Gtk::CellRendererToggle());
   //renderer_active->property_activatable () = true;
renderer_active->signal_toggled().connect( sigc::mem_fun(*this, &ExampleWindow::on_cell_toggled));

Gtk::CellRendererText* renderer_name = Gtk::manage( new Gtk::CellRendererText() );
   renderer_name->property_editable() = true;
//renderer_name->signal_edited().connect( sigc::mem_fun(*this, &ExampleWindow::on_cell_edited));

Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column("Name") );
   column->pack_start(*renderer_active, false);
   column->pack_start(*renderer_name);
column->add_attribute(renderer_active->property_active (), m_columns.active);
   column->add_attribute(renderer_name->property_text (), m_columns.name);

   m_TreeView.append_column(*column);

   show_all_children();
}

ExampleWindow::~ExampleWindow()
{
}

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

void
ExampleWindow::on_cell_toggled (const Glib::ustring& path)
{
   Gtk::TreeModel::iterator iter = m_refTreeModel->get_iter (path);
   if (iter)
      (*iter)[m_columns.active] = !(*iter)[m_columns.active];
}

void
ExampleWindow::on_cell_edited (const Glib::ustring& path, const Glib::ustring& text)
{
   Gtk::TreeModel::iterator iter = m_refTreeModel->get_iter (path);
   if (iter)
      (*iter)[m_columns.name] = text;
}




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