3
votes

I'm creating and SDL program in which some functions open GTK+ windows. The main window is an SDL window and the GTK+ windows are mostly dialog boxes. The main() function opens the SDL window normally and has a while loop with SDL events like usually in SDL. Some SDL events call functions that open GTK+ windows like GTK+ windows usually are opened and that have the same structure as the main() has in a GTK+ program.

All windows open as they should, the problem is about closing the GTK+ windows. When I close a GTK+ window, it stays opened until I close the main SDL window. The only thing that happens when I close the GTK+ window is that it doesn't do anything more after it's closed, so for example if I minimize it and then maximize it again, it becomes empty. When I close it, the SDL window also reacts to events as it should, like if the GTK+ window didn't exist. So everything is just as if the GTK+ window was closed except that it's still visible. I have a g_signal_connect(G_OBJECT(window),"delete-event",G_CALLBACK(gtk_main_quit),NULL); line in each function that opens a GTK+ window, so that's not the problem.

How can I do so that the GTK+ window closes but not the SDL window when I click on the close button in the GTK+ window?

This is the structure of the code (the GTK window is an About dialog box here):

#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <gtk/gtk.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include <dirent.h>
#include <unistd.h>
#ifdef WINDOWS
    #include <windows.h>
#endif

void openGtkWindow(){
    GtkWidget *aboutWindow = gtk_about_dialog_new();
    //Write things in the About window
    g_signal_connect(G_OBJECT(aboutWindow),"delete-event",G_CALLBACK(gtk_main_quit),NULL);
    gtk_widget_show(aboutWindow);
    gtk_main();
}

int main(int argc,char *argv[]){
    gtk_init(&argc,&argv);
    SDL_Surface *screen;
    SDL_Event event;
    SDL_Init(SDL_INIT_VIDEO);
    putenv("SDL_VIDEO_CENTERED=center");
    SDL_WM_SetCaption("SDL window",NULL);
    SDL_WM_SetIcon(IMG_Load("icon.png"),NULL);
    screen = SDL_SetVideoMode(600,400,32,SDL_HWSURFACE | SDL_DOUBLEBUF);
    //Draw things in the SDL window
    SDL_Flip(screen);
    int continuer = 1;
    while(continuer){
        SDL_WaitEvent(&event);
        switch(event.type){
            case SDL_QUIT:
                continuer = 0;
                break;
            case SDL_MOUSEBUTTONUP:
                if(event.button.button == SDL_BUTTON_LEFT){
                    if(/*Test if the mouse is inside the About button*/){
                        openGtkWindow();
                    }
                }
                break;
        }
    }
    SDL_Quit();
    return 0;
}
1
Nice to see you liked my suggestion of GTK+ in the other thread :) Are you handling all GTK+ events by manually running all necessary gtk_main_iteration()s as mame98 showed? If so, then (A) closing the GtkWindow should by default cause it to be fully destroyed, and (B) that callback to gtk_main_quit() can't do anything (useful) as you haven't first handed off control to any gtk_main() proper. The symptom you mentioned does sound like maybe GTK+ is not getting any/enough to itself to actually destroy, or at least redraw, the GtkWindow - so the window manager gets left with a 'ghost'.underscore_d
Would be helpfull to see the whole code. Also, do you stay in the SAME loop after terminating the GTK window? maybe you stopp handeling events after calling gtk_widget_destroy?mame98
Yeah, that'll avoid us having to speculate in the dark!underscore_d
@mame98 I added the structure of my code.Donald Duck
As you are using a gtk Dialog, try gtk_dialog_run..mame98

1 Answers

0
votes

I think you need to handle the GTK events in your while loop (I asume you do not call gtk_main and you stay in your custom while loop).

You can do this by using something similar to this:

while (running)
{
    while (gtk_events_pending()) // are there GTK events to handle
        gtk_main_iteration();    //   - If so, handle them

    // Your SDL Code
}