GTK Prevent X close button [Solved]

Linux specific questions.
exagonx
Posts: 314
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: GTK Prevent X close button [Solved]

Post by exagonx »

TJF wrote: Apr 01, 2022 9:44
If you don't trust me, then try to understand the docs for the GtkWidget "delete-event" signal a
If I don't trust you its no answer here.

I ask here and I talk with you because I trust you and I know your experience and knowledge are better than mine.

Anyway I have to study why this appen to my software and why stil working even mistake its on.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: GTK Prevent X close button [Solved]

Post by TJF »

Hopefully you make progress on your studies. Some more hints:

For callbacks the compiler can not help by doing any type checking. It's under the responsibility of the coder to care about the matching calling convention.

The code you're promoting contains several miss-matches in the calling signature. This results in a malfunction that can not get reproduced (fail depends on the values left over on the stack). That's the most worst kind of bug, a horrible issue to debug.

A further problem is the SHARED variable in your callback. The code is working for one window only. Soon you'll need a similar callback for the GtkAboutDialog widget, and perhaps for further dialogs. Do you want to code one special callback for each window?

You can/should replace your code GTK_WIDGET(mysecondwindow) by the first parameter named widget.

And when you fixed the calling signature, you'll end up with the same callback already existing in GTK (named gtk_widget_hide_on_delete).
exagonx
Posts: 314
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: GTK Prevent X close button [Solved]

Post by exagonx »

TJF wrote: Apr 02, 2022 12:57 Hopefully you make progress on your studies
Hi TJF Yes, unfortunately I have come to these conclusions, to remedy the problem I have currently disabled the X close button directly from GLADE, unfortunately I cannot understand how to interact on an interface designed by glade where it contains more windows.

Applying the code to a specific widget when it is re-created crashes the whole program.

XML "example_glade.ui"

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
  <requires lib="gtk+" version="3.24"/>
  <object class="GtkWindow" id="Main_Window">
    <property name="name">Main_Window</property>
    <property name="can-focus">False</property>
    <child>
      <object class="GtkFixed">
        <property name="visible">True</property>
        <property name="can-focus">False</property>
        <child>
          <object class="GtkButton" id="Open_Sub_Window">
            <property name="label" translatable="yes">Open Sub Window</property>
            <property name="name">Open_Sub_Window</property>
            <property name="width-request">100</property>
            <property name="height-request">80</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
            <signal name="clicked" handler="on_Open_Sub_Window_clicked" swapped="no"/>
          </object>
          <packing>
            <property name="x">258</property>
            <property name="y">93</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkWindow" id="Sub_Window">
    <property name="name">Sub_Window</property>
    <property name="can-focus">False</property>
    <signal name="delete-event" handler="on_Sub_Window_delete_event" swapped="no"/>
    <child>
      <object class="GtkFixed">
        <property name="visible">True</property>
        <property name="can-focus">False</property>
      </object>
    </child>
  </object>
</interface>

Program Source code "example_glade.bas"

Code: Select all



CONST PROJ_NAME = "exampl_glade" '                                             >
CONST PROJ_DESC = "" '                                                         >
CONST PROJ_VERS = "0.0" '                                                      >
CONST PROJ_YEAR = "2022" '                                                     >
CONST PROJ_AUTH = "" '                                                         >
CONST PROJ_MAIL = "" '                                                         >
CONST PROJ_WEBS = "" '                                                         >
CONST PROJ_LICE = "" '                                                         >

    #DEFINE __USE_GTK3__ '                            GTK-3 / GTK-3 Bibliothek >
    #INCLUDE ONCE "gtk/gtk.bi" '                 GTK+library / GTK+ Bibliothek >
    gtk_init(@__FB_ARGC__, @__FB_ARGV__) '             start GKT / GTK starten >
    #INCLUDE ONCE "libintl.bi" '                          load lib / Lib laden >
    bindtextdomain(PROJ_NAME, EXEPATH & "/locale") '               path / Pfad >
    bind_textdomain_codeset(PROJ_NAME, "UTF-8") '   set encoding / Zeichensatz >
    textdomain(PROJ_NAME) '                               Filename / Dateiname >

#IFDEF __FB_WIN32__
  '#LIBPATH "C:/opt/Gtk+" ' place path to your GTK-dev here
  '#LIBPATH "C:/opt/GTK-3.10.0/lib"
  #LIBPATH "C:/msys64/mingw64/lib"
  #DEFINE SLASH "\"
  #DEFINE NL !"\r\n"
  #DEFINE ORDNER "C:\project\"
  #DEFINE EXAMPLE "C:\programs\" & PROJ_NAME & "\bin\" & PROJ_NAME & " "
#ELSEIF DEFINED(__FB_UNIX__)
  #DEFINE SLASH "/"
  #DEFINE NL !"\n"
  #DEFINE ORDNER "~/project/"
  #DEFINE EXAMPLE "/home/user/" & PROJ_NAME & "/" & PROJ_NAME & " "
#ELSE
  #ERROR platform not supported / nicht unterstuetzt!
#ENDIF





SCOPE
  VAR er = gtk_check_version_(3, 0, 0)
  IF er THEN
    ?"Fehler/Error (GTK-Version):"
    ?*er
    END 1
  END IF
END SCOPE

DIM SHARED AS GtkBuilder PTR XML
DIM SHARED AS GObject PTR _
  Main_Window, Open_Sub_Window, Sub_Window

XML = gtk_builder_new()

SCOPE
DIM AS GError PTR meld
IF 0 = gtk_builder_add_from_file(XML, "example_glade.ui", @meld) THEN
  WITH *meld
    ?"Fehler/Error (GTK-Builder):"
    ?*.message
  END WITH
  g_error_free(meld)
  END 2
END IF
END SCOPE

Main_Window = gtk_builder_get_object(XML, "Main_Window")
Open_Sub_Window = gtk_builder_get_object(XML, "Open_Sub_Window")
Sub_Window = gtk_builder_get_object(XML, "Sub_Window")

g_signal_connect(G_OBJECT(Sub_Window), "delete-event", G_CALLBACK(@Sub_Window), NULL)

SUB on_Open_Sub_Window_clicked CDECL ALIAS "on_Open_Sub_Window_clicked" ( _
  BYVAL widget AS GtkWidget PTR, _
  BYVAL user_data AS gpointer) EXPORT ' Standard-Parameterliste

' place your source code here / eigenen Quelltext hier einfuegen
gtk_widget_show(GTK_WIDGET(Sub_Window))
END SUB

SUB on_Sub_Window_delete_event CDECL ALIAS "on_Sub_Window_delete_event" ( _
  BYVAL widget AS GtkWidget PTR, _
  BYVAL user_data AS gpointer) EXPORT ' Standard-Parameterliste

' place your source code here / eigenen Quelltext hier einfuegen
Print "Nothing"

END SUB


    gtk_builder_connect_signals(XML, 0) '                     Signale anbinden >
    g_object_unref(XML) '                       dereference / Referenz abbauen >
    gtk_widget_show_all(GTK_WIDGET(Main_Window)) '     Hauptfenster darstellen >
    gtk_main() '                                     main loop / Hauptschleife >






TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: GTK Prevent X close button [Solved]

Post by TJF »

You're inert against any piece of advice! Won't you understand, or can't you understand?

Try this:

XML "example_glade.ui"

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
  <requires lib="gtk+" version="3.24"/>
  <object class="GtkWindow" id="Main_Window">
    <property name="name">Main_Window</property>
    <property name="can-focus">False</property>
    <signal name="destroy-event" handler="gtk_main_quit" swapped="no"/>
    <child>
      <object class="GtkFixed">
        <property name="visible">True</property>
        <property name="can-focus">False</property>
        <child>
          <object class="GtkButton" id="Open_Sub_Window">
            <property name="label" translatable="yes">Open Sub Window</property>
            <property name="name">Open_Sub_Window</property>
            <property name="width-request">100</property>
            <property name="height-request">80</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
            <signal name="clicked" handler="gtk_widget_show" object="Sub_Window" swapped="yes"/>
          </object>
          <packing>
            <property name="x">258</property>
            <property name="y">93</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkWindow" id="Sub_Window">
    <property name="name">Sub_Window</property>
    <property name="can-focus">False</property>
    <signal name="delete-event" handler="gtk_widget_hide_on_delete" swapped="no"/>
    <child>
      <object class="GtkFixed">
        <property name="visible">True</property>
        <property name="can-focus">False</property>
      </object>
    </child>
  </object>
</interface>
Program Source code "example_glade.bas"

Code: Select all

' testing Glade
'
' borrowed some code from https://github.com/DTJF/GladeToBac code sketcher

CONST PROJ_NAME = "exampl_glade" '                                             >
CONST PROJ_DESC = "" '                                                         >
CONST PROJ_VERS = "0.0" '                                                      >
CONST PROJ_YEAR = "2022" '                                                     >
CONST PROJ_AUTH = "" '                                                         >
CONST PROJ_MAIL = "" '                                                         >
CONST PROJ_WEBS = "" '                                                         >
CONST PROJ_LICE = "" '                                                         >

    #DEFINE __USE_GTK3__ '                            GTK-3 / GTK-3 Bibliothek >
    #INCLUDE ONCE "gtk/gtk.bi" '                 GTK+library / GTK+ Bibliothek >
    gtk_init(@__FB_ARGC__, @__FB_ARGV__) '             start GKT / GTK starten >
    #INCLUDE ONCE "libintl.bi" '                          load lib / Lib laden >
    bindtextdomain(PROJ_NAME, EXEPATH & "/locale") '               path / Pfad >
    bind_textdomain_codeset(PROJ_NAME, "UTF-8") '   set encoding / Zeichensatz >
    textdomain(PROJ_NAME) '                               Filename / Dateiname >

#IFDEF __FB_WIN32__
  '#LIBPATH "C:/opt/Gtk+" ' place path to your GTK-dev here
  '#LIBPATH "C:/opt/GTK-3.10.0/lib"
  #LIBPATH "C:/msys64/mingw64/lib"
  #DEFINE SLASH "\"
  #DEFINE NL !"\r\n"
  #DEFINE ORDNER "C:\project\"
  #DEFINE EXAMPLE "C:\programs\" & PROJ_NAME & "\bin\" & PROJ_NAME & " "
#ELSEIF DEFINED(__FB_UNIX__)
  #DEFINE SLASH "/"
  #DEFINE NL !"\n"
  #DEFINE ORDNER "~/project/"
  #DEFINE EXAMPLE "/home/user/" & PROJ_NAME & "/" & PROJ_NAME & " "
#ELSE
  #ERROR platform not supported / nicht unterstuetzt!
#ENDIF

SCOPE
  VAR er = gtk_check_version_(3, 24, 0)
  IF er THEN
    ?"Fehler/Error (GTK-Version):"
    ?*er
    END 1
  END IF
END SCOPE

VAR XML = gtk_builder_new()

SCOPE
DIM AS GError PTR meld
IF 0 = gtk_builder_add_from_file(XML, "example_glade.ui", @meld) THEN
  WITH *meld
    ?"Fehler/Error (GTK-Builder):"
    ?*.message
  END WITH
  g_error_free(meld)
  END 2
END IF
END SCOPE

VAR Main_Window = gtk_builder_get_object(XML, "Main_Window")

    gtk_builder_connect_signals(XML, 0) '                     Signale anbinden >
    g_object_unref(XML) '                       dereference / Referenz abbauen >
    gtk_widget_show_all(GTK_WIDGET(Main_Window)) '     Hauptfenster darstellen >
    gtk_main() '                                     main loop / Hauptschleife >
exagonx
Posts: 314
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: GTK Prevent X close button [Solved]

Post by exagonx »

TJF wrote: Apr 02, 2022 14:01 You're inert against any piece of advice! Won't you understand, or can't you understand?
OMG it was that simple, I feel like an stupid and I sure am.

TJF thanks to the example was really enlightening.
Forgive me if I am slow to understand for such a simple thing I have no justification.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: GTK Prevent X close button [Solved]

Post by TJF »

One more hint:

Don't name your widget. Currently you don't need properties like

Code: Select all

    ...
    <property name="name">Main_Window</property>
    ...
    <property name="name">Sub_Window</property>
    ...
This attribute is mainly used when implementing the GtkBuildable interface on a type. At your level the id tag is sufficient to identify the widget. Everything else is just confusing and error-prone.
exagonx
Posts: 314
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: GTK Prevent X close button [Solved]

Post by exagonx »

TJF wrote: Apr 02, 2022 17:38 One more hint:

Don't name your widget.
Thank you for the help and the hint.

Currently I have given a name to the widget because in addition to managing the closing event with a SUB that saves the changed data, I also needed it because it must be shown if certain conditions are true.

I hope this does not lead to any problems in the future.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: GTK Prevent X close button [Solved]

Post by TJF »

OK, you're the expert and I need advice!

I don't understand why we need those name properties for the window widgets, but not for the button. What will we miss if we erase those lines from the XML file?
exagonx
Posts: 314
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: GTK Prevent X close button [Solved]

Post by exagonx »

TJF wrote: Apr 03, 2022 5:52 OK, you're the expert and I need advice!

I don't understand why we need those name properties for the window widgets, but not for the button. What will we miss if we erase those lines from the XML file?
What you mean ?
Post Reply