[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]
Working gtk_widget_destroy around
- From: Carlos Pereira <carlos pehoe civil ist utl pt>
- To: gtk-app-devel-list redhat com
- Subject: Working gtk_widget_destroy around
- Date: Thu, 24 Feb 2000 18:27:36 GMT
>I have a dialog box with some entries and once the dialog box is destroyed
>all the pointers to the entries are again unitialized (and so on the
>global variables in whose I saved the entries values are lost). How could
>I workaround this, i.e., how can I keep the values that were saved on the
>global variables? Can you understand what I mean?
Try to avoid global variables as much as possible: as bigger and
more complex is your app, more problems you are likely to get,
apart from name_space pollution, they can hide structural
design flaws from you, which might become apparent later on.
About your problem, as usual, there are two ways of dealing with this:
1) create a structure, put all your entries inside, you can even
put the dialog widget inside, and pass the structure in your call backs.
This way, when you destroy the dialog you have the information in a
safe place.
2) attach other widgets to your dialog widget using:
gtk_object_set_data (GTK_OBJECT (dialog), "your_entry", entry);
and get them back with:
entry = (GtkWidget *) gtk_object_get_data (GTK_OBJECT (dialog), "your_entry");
If you have an app with many dialogs, say, 50, 100, then the first
option becomes a nightmare, because you would have to create a
different structure for each particular dialog collection of data,
and you would be responsible for freeing the memory for all these
structures.
The second option works very nicely because this is not a critical
time consuming operation. If you have 80 dialogs in your app, and
each of them has, say 10-15 entries, a couple of radio buttons and
such, in my humble opinion the second option is much better than
the first.
In my case, I attach all my data to the dialog widget, put the
address of the dialog in a structure that contains all my data for
each top window, and pass this structure as a pointer to my
call backs. This is simple and gives me all the flexibility I need:
1) If the user press Cancel, I have only to remove the dialog
widget, because all the entries are attached to it, and GTk
is responsible for freeing the memory they use.
2) If the user presses Ok, I transfer all the data from the dialog
to my own formats, if the format is ok, handle it and destroy
the dialog. If the format is not ok, I just ask the user to
modify its data, so in this case the dialog is not immediately
destroyed.
3) If the user decides to close the top window, or even to close
the entire app, with a "delete_event" signal, when the dialog is
up, I can destroy the dialog widget and the attached data before
closing, because I saved its address in my own engine. This would not
be possible if I had passed only the address of the dialog.
This is just my opinion though...
Carlos Pereira
[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]