Search code examples
cgtkgtk3cairo

GTK+3 disable the close icon present in a window (c program)


I'm developing a GUI in Linux (Ubuntu 16.04 - WM: Gnome) using GTK+3 and the graphic library cairo.

After clicked on a push button (Plot), using the instruction of cairo I draw a red square on a new top window where I put a GtkDrawingArea.

In this window I also put a push button (Cancel) that clicked, hide the window. In this way, if I re-push "Plot", the red square reappear.

The issue is the "x" icon present in the top bar of the window. If (no me) a user push this x, the window disapper and if he re-push the "Plot" an error is reported.

The question is: it is possible avoid this possible cause of error? (remove this "x" from the top bar of the window or in some way disable its functionality).

I tryed to find alone a solution and the possibility found are:

1 - Remove from the window the property of "decorated". The top bar disapper (so also the x) but is not possible move the window on the screen

2 - Using the function gtk_window_set_deletable(window, FALSE) (used before to show the window), but the x is always there and pushing it the window is destroyed.

If you think that can be useful, I can report the code. I'm waiting your suggestion.


Solution

  • Edit:

    Now we know what you want to achieve: display a separate window but avoid destroying it so you can display it again. You already have in the "Cancel" button of your secondary window the logic to hide it.

    The cleanest solution is to just do the same: when the user tries to close the secondary window, hide it instead. This way the user is not frustrated of seeing something that apparently doesn't work as expected. Hidden or closed, it's different for you but it's the same for the user.

    So you just need to connect to the delete-event of that secondary window, and hide it. There's even no need to create a specific callback for that, GTK+ provides it for you: you just need to connect the delete-event to gtk_widget_hide_on_delete. To display the window again, just call gtk_widget_show_all on it.

    Original answer:

    I realize the plot

    "realize" is a term that has a defined meaning in GTK+. Don't use it out of context, and try to use an alternate term if you're not talking about widget realization.

    What I would like is to remove this "x" from the top bar of the window or in some way disable its functionality.

    Do you understand this is ultra annoying for a user and defeats a unified user experience? Would you like to use applications that do random different things?

    Anyway, one way of disabling the closing button is to connect to the delete-event and return TRUE there to stop the propagation of the event. The button will still be there but do nothing, so you will have to kill the app to exit it.

    To make the button disappear, gtk_window_set_deletable will ask the Window Manager to do that, but we'd need some code to know what's wrong with your attempt.