Search code examples
c#gtk3glade

GTK sub window cannot be reopened after closing


I've made a simple GTK project with Glade. It is a window with a single button that opens a sub-window when you click it. The problem is that if I close the sub-window by clicking the x and reopen it using the button, the sub-window becomes blank, even though I've implemented a Signal that should catch the destroy event. For testing purposes I've implemented a button that closes the sub-window using the same function. I can reopen the sub-window without any issue, if I use my implemented button.

I've done some research and apparently I need to set the sub-window to "HideOnDestroy", but I have no clue where or how to do that. Is there some setting that I've missed in Glade?

Glade file "layout.glade"

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
  <requires lib="gtk+" version="3.12"/>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <signal name="destroy" handler="Quit" swapped="no"/>
    <child>
      <object class="GtkButton" id="button1">
        <property name="label">gtk-open</property>
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="receives_default">True</property>
        <property name="use_stock">True</property>
        <signal name="clicked" handler="ShowInfo" swapped="no"/>
      </object>
    </child>
  </object>
  <object class="GtkWindow" id="window2">
    <property name="can_focus">False</property>
    <signal name="destroy" handler="Close" swapped="no"/>
    <child>
      <object class="GtkButton" id="button2">
        <property name="label">gtk-close</property>
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="receives_default">True</property>
        <property name="use_stock">True</property>
        <signal name="clicked" handler="Close" swapped="no"/>
      </object>
    </child>
  </object>
</interface>

Source code:

using System;
using Gtk;

namespace test
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Wind wind = new Wind();
        }
    }

    class Wind
    {
        Builder builder;

        public Wind()
        {
            Application.Init();
            builder = new Builder();
            builder.AddFromFile("../../layout.glade");
            builder.Autoconnect(this);
            Widget window = (Widget)builder.GetObject("window1");
            window.Show();
            Application.Run();
        }

        void ShowInfo(object o, EventArgs args)
        {
            Console.WriteLine("info");
            Widget window = (Widget)builder.GetObject("window2");
            window.Show();
        }

        void Close(object o, EventArgs args)
        {
            Console.WriteLine("close");
            Widget window = (Widget)builder.GetObject("window2");
            window.Hide();
        }

        void Quit(object o, EventArgs args)
        {
            Application.Quit();
        }
    }
}


Solution

  • The button was implemented correctly, but I used the wrong signal (and event arguments) for the x.

    Instead of the destory signal the delete-event signal has to be used with a different event handler.

    void Close(object o, DeleteEventArgs args)
    {
      Console.WriteLine("close");
      Widget window = (Widget)builder.GetObject("window2");//"(Widget)o" would also do
      args.RetVal = window.HideOnDelete();
    }