SFML community forums

Help => Window => Topic started by: GabrielS on June 21, 2013, 09:01:42 pm

Title: [SOLVED] sf::Event::LostFocus
Post by: GabrielS on June 21, 2013, 09:01:42 pm
Hello community =)

I have a question about the event : sf::Event::LostFocus and sf::Event::GainFocus.
I have 2 sf::RenderWindow opened. Let's call them window1 and window2.
Window1 is permanently opened, and window2 can open itself from interactions with window1 ( if you click on a button from window1, you will see window2 appear, "partially on" window1).
That works well and I get focus on window2 when opening it.

However, if, from here, I click on window1, I do not get an event lost focus from window2. Clicking on an other application exactly the SAME way will give me the event 'lostfocus'.
I do not know if this is a problem in my program or a problem from SFML library. Please help me, I have no idea how to correct this bug,

Thanks in advance,

Gabriel
Title: Re: sf::Event::LostFocus
Post by: Laurent on June 21, 2013, 09:08:48 pm
Can you please provide a complete and minimal example that reproduces the problem?
Title: Re: sf::Event::LostFocus
Post by: GabrielS on June 21, 2013, 09:45:56 pm
I'll try to give you a really simplify version of the issue I am encountering. This is just to show you where is my problem :

// windowMain and windowAux are two sf::renderwindow created
while (windowMain.isOpen())
{
    sf::Event event;
    while (windowMain.pollEvent(event))
    {
        switch (event.type)
        {
            case sf::Event::Closed: // closing using the cross button of the window
                windowMain.close(); // close the main window and gets out of this loop
                break;
            case sf::Event::MouseButtonPressed: // clicking
                createWindowAux(windowAux); // pops windowAux
                windowAux.display();
                break;
            default:
                break;
        }
    }
    while (windowAux.pollEvent(event)) // loop on editing/reading attributes
    {
        switch (event.type)
        {
            case sf::Event::Closed:
                windowAux.clear(COLOR_BACKGROUND_WINDOW);
                windowAux.close();
                break;

            case sf::Event::LostFocus:
                cout << "LOSTFOCUS" << endl;
                break;

            case sf::Event::GainedFocus :
                windowAux.clear(COLOR_BACKGROUND_WINDOW);
                // add objects here
                windowAux.display();
                cout << "GAINFOCUS" << endl;
                break;
            case sf::Event::Resized :
                cout << "RESIZED" << endl;
                break;
            default:
                break;
        }
    }
}
 

I just re-explain the problem I got (not so easy to explain sorry). By clicking on the mainWindow while the windowAux is opened, i do not get the event LOSTFOCUS in window2 (maybe I need to make 2 distincts event but I do not think so - events should be all equal at same time I suppose).
However, clicking on an other window - (not from SFML) as MicrosoftVisualStudios (for instance) - will give me the lostfocus event.

Also, this is the code for the creation of the window :
void createWindowForAttributes(sf::RenderWindow& window)
{
        if (!window.isOpen()) // if not already opened, creation of the window
        {
                window.create(sf::VideoMode(WIDTH_WINDOW_ATTRIBUTE,
                        HEIGHT_WINDOW_ATTRIBUTE),
                        "Editing", sf::Style::Titlebar | sf::Style::Close);
        }
        window.setTitle("Editing");
        window.setSize(sf::Vector2u(WIDTH_WINDOW_ATTRIBUTE,
                HEIGHT_WINDOW_ATTRIBUTE));
        window.clear(COLOR_BACKGROUND_WINDOW);
}
 

Thanks,
Tell me if you need anything else for the explanation,

Gabriel
Title: Re: sf::Event::LostFocus
Post by: Laurent on June 21, 2013, 10:36:02 pm
I perfectly understood your problem, but I need a code to test, so that I can either confirm that it's a bug, or tell you what you did wrong ;)
Title: Re: sf::Event::LostFocus
Post by: GabrielS on June 21, 2013, 10:40:56 pm
The example I give you should present the problem I am talking about. I have no idea where it comes from tho :/
Title: Re: sf::Event::LostFocus
Post by: Laurent on June 21, 2013, 10:44:11 pm
If possible, post a complete and minimal example rather than incomplete parts of your original code.

The problem is about focus events with two windows: write a simple main which creates two windows and show the focus events ;)
Title: Re: sf::Event::LostFocus
Post by: Nexus on June 21, 2013, 11:46:05 pm
To know how the minimal example should look, check out this post (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368).
Title: Re: sf::Event::LostFocus
Post by: GabrielS on June 24, 2013, 03:19:39 pm
Hello,

I managed to get exactly the same issue (wasn't so easy actually, I do believe there's a problem of mine somewhere - Well here is the minimal code :
#include <iostream>
#include <string>

using namespace std;

void createWindowAux(sf::RenderWindow& window)
{
        if (!window.isOpen())
        window.create(sf::VideoMode(400,400),
                "2nd window", sf::Style::Titlebar | sf::Style::Close);
}

 int main ()
{
        sf ::RenderWindow windowMain;
        windowMain.create(sf::VideoMode(700,800), "Simple Test");
        sf::RenderWindow windowAux;
        bool mIsComponentActive = false;

    while (windowMain.isOpen())
    {
        sf::Event event;
        while (windowMain.pollEvent(event))
        {
            switch (event.type)
                        {
                case sf::Event::Closed: // closing using the cross button of the window
                                        windowMain.close(); // close the main window and gets out of this loop
                                break;

                                case sf::Event::MouseButtonPressed:
                                        mIsComponentActive = false;
                                        sf::Vector2i localPosition = sf::Mouse::getPosition(windowMain);
                                        if (localPosition.y < 30)
                                        {
                                                mIsComponentActive = true;
                                                createWindowAux(windowAux);
                                        }
                                        windowAux.display();
                                break;
                        }
                }
                while (windowAux.pollEvent(event))
        {
                        if (mIsComponentActive)
                        {
                                switch (event.type)
                                {
                                        case sf::Event::Closed: // closing using the cross button of the window
                                                windowAux.close(); // close the main window and gets out of this loop
                                        break;

                                        case sf::Event::LostFocus:
                                                cout << "LOSTFOCUS" << endl;
                                                windowAux.clear(COLOR_BACKGROUND_WINDOW);
                                        break;

                                        case sf::Event::GainedFocus :
                                                windowAux.clear(COLOR_BACKGROUND_WINDOW);
                                                // put objects here
                                                windowAux.display();
                                                cout << "GAINFOCUS" << endl;
                                        break;
                                }
                        }
                }
        }

    return 0;
}

The sequence of events I am doing to get the 'bug' is the following one :

Now, do exactly the same things, but instead of clicking on windowMain to get back to this window, just click on another program opened : this time you get the even 'LOSTFOCUS'.

I hope it's clear enough. Again, the bug is not that easy to explain too, please ask me questions if you do not understand something I'm saying.

Thanks

Edit : I do see the bug... It comes from my boolean. Mmhh, I'll try to manage this ;-)