Re: [gtk-list] Re: GTK--: Gtk_Notebook::append_page question



Tero Pulkkinen wrote:

> > > With all gtk-- containers, including notebooks, the ownership of the
> > > items inserted to it are NOT transferred to the container. Thus you
> > > must delete everything yourself what you allocate.
>
> Ionut Borcoman at home <borco@mailbox.ro> writes:
> > Than my style of appending is not good. :( And I have to keep a lot of
> > variables, otherwise not needed. The biggest problem is how to name all
> > this bunch of widgets ! :)
> >
> > And it means that the piece of code from bellow is also leaving some
> > memory that cannot be freed, isn't it ?
>
> Yuup.
>
> I usually put the widgets embedded to some object. This gets rid of
> extra memory allocation as all widgets embedded to one object only need
> one "malloc()" call. LIke this:
>
> class mainwindow : public Gtk_Window {
> public:
>    Gtk_Button ok_button, cancel_button, red_button;
> };
>
> This way destroying them becomes easier too as their lifetime
> is tied to the mainwindow's lifetime. Sometimes using pointer
> there too is useful to limit the size of mainwindow object - this
> way objects tend to become quite large. (but faster to create, not
> that it really matters anything here)
>
> >     Gtk_Label  *label;
> >     label = new Gtk_Label("xxx");
> >     table0.attach( label, 0, 1, row, row+1, GTK_FILL, GTK_FILL);
> >     label->set_alignment( 0.0, 0.5 );
> >     label->show();
>
> hmm.. maybe I should add such support and add a specific
> attach_and_move_ownership() method.. Anyone got good ideas for names
> of such methods?. This way using this design becomes easier..
>
> of course such version should be added also for other container add methods.

  Why not simpy use a (global or local up to your choice)
vector<Gtk_Label>?

I always use Tero's imbedding method so I never tried. Simply do:

#include <vector>

vector<Gtk_Label> labels;

...
some_procedure
{  labels.push_back(Gtk_Label("xyz"));
    Gtk_Label &recently_created_label=*--(labels.end()); // black STL
magic
    recently_created_label.set_alignment(...);
}

once labels quits it's scope (global variables after an exit) all Labels
are
freed automatically.

You might also use a multimap<int,Gtk_Label*> to store pointer to labels
and
free them according to an integer tag. On interest mail me.

STL makes your life easier (once you understood the basics ;-(  )

But I strongly prefer the embedded method since it is more object
oriented than
a bunch of global pointer arrays !!!
And OOP definitely makes your programs easier to maintain. (Sigh! look
at old C
code).

Christof

PS: You might take a look at glade-- and it's embedded handling of
notebook labels.
(one of the examples use a notebook)




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