On 3/20/07, Jef Driesen <[EMAIL PROTECTED]> wrote:
> I am writing my first gtkmm application (actually my first GUI
> application). To get familiar with the toolkit (both gtk and gtkmm), I
> studied the code of some existing applications. And in one of those
> applications I found a construct that I would like to use in my own
> application, but I don't know how to port the gtk code to gtkmm.
>
> The main application contains a treeview and a secondary dialog is used
> to edit the treeview entries. But if you try to edit an entry that is
> already "open", the existing dialog is activated instead of creating a
> new one (see code snippet below). How can I do this in C++?
>
> And how do I automatically destroy the dialog after clicking the (close)
> button?
>
> /* myobject_treeview.c */
>
> GtkWidget* main_window = ...;
> myobject* obj = ...;
> GtkWidget* dialog = myobject_dialog_new (main_window, obj);
> gtk_widget_show (dialog);
>
> /* myobject_dialog.c */
>
> static GHashTable *dialogs = NULL;
>
> GtkWidget* myobject_dialog_new (GtkWidget* parent, myobject* obj)
> {
> if (!dialogs) {
> dialogs = g_hash_table_new (NULL, NULL);
> }
>
> dialog = g_hash_table_lookup (dialogs, obj);
> if (dialog) {
> gtk_window_present (GTK_WINDOW (dialog));
> return dialog;
> }
>
> dialog = gtk_dialog_new ();
>
> g_hash_table_insert (dialogs, obj, dialog);
>
> g_signal_connect (close_button,
> "clicked",
> G_CALLBACK (myobject_dialog_close_clicked_cb),
> dialog);
>
> g_signal_connect (dialog,
> "destroy",
> G_CALLBACK (myobject_dialog_destroy_cb),
> obj);
>
> g_signal_connect_object (parent,
> "destroy",
> G_CALLBACK (myobject_dialog_parent_destroy_cb),
> dialog,
> 0);
> }
>
> static void
> myobject_dialog_destroy_cb (GtkWidget *parent, myobject *obj)
> {
> g_hash_table_remove (dialogs, obj);
> }
>
> static void
> myobject_dialog_parent_destroy_cb (GtkWidget *parent, GtkWidget *dialog)
> {
> gtk_widget_destroy (dialog);
> }
>
> static void
> myobject_dialog_close_clicked_cb (GtkWidget *button, GtkWidget *dialog)
> {
> gtk_widget_destroy (dialog);
> }
>
> _______________________________________________
> gtkmm-list mailing list
> [email protected]
> http://mail.gnome.org/mailman/listinfo/gtkmm-list
>
I think I'd make a column in your TreeModelColumnRecord a smart
pointer to the dialog.
I'm not certain what I'd do to delete the window when its closed. You
could connect to the dialog's signal_hide, bind the smart pointer to
the call back and search the tree view for the corresponding row, but
that seems rather inefficient. I suppose you could store weak pointers
in the tree view, the shared ptr's in a std::set and then your search
for the dialog to delete would be a bit faster.
Generally, I would only allow editing of a single row at a time to
prevent this but I could imagine there are times when having the
multiple row selection would be a good thing.
Anyway, HTH
Paul Davis
_______________________________________________
gtkmm-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/gtkmm-list