Re: [gtkmm] Any easy way to write a cell renderer with a combo box it it.



Thanks for the hint. 

Finally I worked out my combo cell renderer after
studying the example codes. 
Since I wrap a combo directly and thus I do not have
to handle the popup issues, the codes are much simpler
than in the gtkmm example. 
But there is still one thing I do not understand. 
What is the "Glib::ObjectBase (typeid
(CMy_Combo_Editable))" for(marked in the soruces
below)? If I remove that it, I got an error that says:

(test: 4937): Gtk - CRITICAL **: file
gtktreeviewcolumn.c: line 2500
(gtk_tree_view_column_cell_process_action):assertion`
GTK_IS_CELL_EDITABLE (*editable_widget)' failed

It looks to me the line registers the class to
somewhere. Is that right? I found a lot of mystery
stuffs(to me) like this in the gtkmm source codes, and
it was really hard to figure out a big image. Is there
any document around about these "gtkmm internals" ?

The source of my combo renderer is following. The
"spinbox" cell renderer can also be 
implemented in this way, and is actually easier(Do not
need an extra editable widget). 

Cheers,
Dan
++++++++++++++++++++++++++++++++++++++++++++++++++++++
class CMy_Combo_Editable:
public Gtk::EventBox,public Gtk::CellEditable
{
        private:
            Gtk::Combo combo_;
        public:
            CMy_Combo_Editable (std::list <
std::string > &items):
            Glib::ObjectBase (typeid
(CMy_Combo_Editable)),///xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                Gtk::EventBox (),
                Gtk::CellEditable ()
            {
                get_entry ()->set_has_frame (false);
                combo_.set_popdown_strings (items);
                combo_.show ();
                add (combo_);
            }
            Gtk::Entry * get_entry ()
            {
                return combo_.get_entry ();
            }
            void set_editable (bool editable)
            {
                get_entry ()->set_editable (editable);
            }
        protected:
            virtual void start_editing_vfunc (GdkEvent
* event)
            {
                get_entry ()->start_editing (event);
            }
            virtual void on_editing_done ()
            {
                get_entry ()->editing_done ();
            }
 };

class CMy_Combo_CellRenderer:public 
Gtk::CellRendererText
{
    public:
        void add_item (std::string str)
        {
            items.push_back (item);
        };
        void clear ()
        {
            items.clear ();
        };
    private:
        std::list < std::string > items;
    protected:
        virtual Gtk::CellEditable *
start_editing_vfunc (GdkEvent * event,
            Gtk::Widget & widget, const Glib::ustring
& path,
            const Gdk::Rectangle & background_area,
            const Gdk::Rectangle & cell_area,
            Gtk::CellRendererStateflags)
        {
            if (!property_editable ())
                return NULL;
            CMy_Combo_Editable * combo =
                Gtk::manage (new CMy_Combo_Editable
(items));
            Glib::ustring text = property_text ();
            Gtk::Entry * entry = combo->get_entry ();
            entry->property_editable () = true;
            entry->set_text (text);
            entry->select_region (0, -1);
            entry->signal_editing_done().connect
(bind(SigC::slot (*this, &CMy_Combo_CellRenderer::
on_edited), entry, path));
            combo->show ();
            return combo;
        }
        virtual void on_edited (Gtk::Entry * entry,
Glib::ustring path)
        {
            Glib::ustring old_text = property_text ();
            Glib::ustring new_text = entry->get_text
();
            if (new_text.compare (old_text) != 0)
            {
                edited (path, new_text);
            }
        }
};

/*******************************************/
void treeView_add_column(Gtk::TreeView& treeview,
const Glib::ustring& title, Gtk::CellRendererText*
render, Gtk::TreeModelColumn<ColumnType>&
model_column)
{
	int cols_count=treeview.append_column(title,
*render);
	Gtk::TreeViewColumn* pColumn =
treeview.get_column(cols_count-1);
	if(pColumn)
pColumn->add_attribute(render->property_text(),model_column);

Gtk::TreeView_Private::_connect_auto_store_editable_signal_handler(&treeview,render,model_column);
};
/*******Usage*********************/
CMy_Combo_CellRenderer* render0=Gtk::manage(new
CMy_Combo_CellRenderer());
render0->add_item("XYZ");
render0->add_item("XYZKMK");
treeView_add_column(m_list_table, "Students", render0,
m_list_columns.m_student_name);

--- Daniel Elstner <daniel elstner gmx net> wrote:
> On Fre, 2003-01-10 at 02:05, ZHOU DX wrote:
> > Hi everyone, 
> > 
> > Since Gtk::combo is neither a Gtk::CellEditable or
> > Gtk::Editable, it is not straightforward to fit it
> > into the Treeview interface as a cell renderer. I
> > tried to write a new "editable" widget that wraps
> a
> > combo, but it looks like I have to write a
> > correponding widget_Class to declare the widget as
> > editable which is really a pain.  
> > Anyone knows a easy way to do that?
> 
> You have to implement your own cell renderer. 
> Luckily, it has already
> been done: have a look at
> examples/cellrenderercustom/
> 
> --Daniel
> 
> 


=====
----------------------------------------------------------
Have fun!
__________________________________________________________

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com



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