Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GTK C - How to edit function of close window button (the X button in the top right corner)?

Tags:

c

window

gtk

I am wondering how to edit the "close" button (or the minimize/maximize buttons) in the top right corner of a window that was created with the GTK library. I am trying to remove the user's ability to destroy this window and only allow the top level window to destroy it so I want the X button (close window) in the top right corner to only hide the window instead of close it - still allowing it to run in the background.

I am somewhat new to gtk and I have gone through a few beginner tutorials in terms of creating windows and adding buttons but nothing very advanced.

I am assuming this can be accomplished by using a gtk_window_hide call on the window in place of the current functionality of the X button but I am not sure where to use it because the functions for the default buttons dont appear to be easily accessible.

like image 572
Alex Avatar asked Jul 11 '11 12:07

Alex


2 Answers

In GTK you listen for signals the widgets send out. In other languages like Java (in which you might be more familiar with the terminology), those are often called Events.

If an event occurs, like "deleting" the widget, there is the corresponding signal triggered, to which you might apply by connecting with g_signal_connect and the like.

I suggest you to install devhelp for a good documentation/online help for GTK.

This little code should keep you going, i hope it is self-explaining to you.

    #include <stdio.h>
    #include <gtk/gtk.h>
    #include <stdlib.h>

    int
    main (int argc, char **argv)
    {
      GtkWidget *window;

      gtk_init (&argc, &argv);

      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      g_signal_connect (window, "delete_event", G_CALLBACK (gtk_window_iconify), NULL);

      gtk_widget_show (window);
      gtk_main ();

      return EXIT_SUCCESS;
    }
like image 189
Daniel Leschkowski Avatar answered Sep 23 '22 03:09

Daniel Leschkowski


This is what gtk_widget_hide_on_delete is used for.

g_signal_connect (window, "delete-event", G_CALLBACK (gtk_widget_hide_on_delete), NULL);

Your child window will that way only be hidden. Just put a menu or toolbutton in your main window to be able to show/hide it again.

The "delete-event" is the one called when the "close" button of the window manager is clicked.

like image 38
liberforce Avatar answered Sep 22 '22 03:09

liberforce