Is it possible to make event inside event handler?

May 18, 2013 at 8:23 AM
Hi Jinhao!
Today I tried to yield to your advice about nana::gui::dragger.
But I needed use it like it:
int main()
{
    nana::gui::form f;
    nana::gui::picture p(f,nana::size(100,100));
    p.background(nana::color_t(0xFF0000));
    std::unique_ptr<nana::gui::dragger> dragger;
    p.make_event<nana::gui::events::mouse_down>([&]
    {
        dragger = std::unique_ptr<nana::gui::dragger>(new nana::gui::dragger);
        dragger->trigger(p);
        dragger->target(p);
    });
    p.make_event<nana::gui::events::mouse_up>([&]
    {
        dragger = nullptr;
    });
    f.show();
    nana::gui::exec();
}
This snippet doesn't work.And I think it's because dragger tries to make the same events
inside event handler.Is it intented?And in this case should I use nana::gui::API::capture_window or something else?

Thank you.
Coordinator
May 18, 2013 at 8:49 AM
No, configuring the dragger in mouse_down handler does not make sense. Because when mouse_down occurs, the dragger is empty, then you set trigger/target window for the dragger, but when mouse_up occurs, the dragger is released.
I have no idea why you need to use the dragger like this way. Could you please explain your demand?
May 18, 2013 at 9:09 AM
Oh really.Sorry I was wrong.
I'm writing something like solitaire.And when user clicks on the card, a bunch of card will be created.And user drad'n'drops it to some column,where this bunch will bound to the column.(Sorry for my english).So before user clicks on the card bunch didn't exsist.And after user release mouse bunch will not exist.
And I thought that bunch would receive mouse_down event!
Sorry if I spent your time,I'm just learning.

Thank you.
May 18, 2013 at 9:14 AM
And one more thing,I don't know is it important,but this code doesn't work if I comment this line "dragger = nullptr;".
Coordinator
May 18, 2013 at 9:52 AM
commenting the "dragger = nullptr" is useless, becuase it will be destroyed in mouse_down by renew.

In this suituation, you can move the bunch of cards like this.
std::unique_ptr<nana::gui::dragger> dragger(new nana::gui::dragger);
dragger->target(p);
dragger->trigger(p);

p.make_event<nana::gui::events::mouse_move>([&]
{
    //move your bunch of cards here.
});

p.make_event<events::mouse_up>([&]
{
    if(/*if the Card should not be movable anymore*/)
        dragger = nullptr;
});
Maybe your circumstance is complicated, you can try to solve it by using API::capture_window()
nana::point pos;
nana::point pos_p;
p.make_event<nana::gui::events::mouse_down>([&]
{
    pos = API::cursor_position();
    pos_p = p.pos();
    API::capture_window(p, true);
});

p.make_event<nana::gui::events::mouse_move>([&](const eventinfo& ei)
{
    if(ei.mouse.left_button)    //Drag
    {
        auto tmp = API::cursor_position();
        //Move the card
        p.move(pos_p.x + tmp.x - pos.x, pos_p.y + tmp.y - pos.y);

        //move your bunch of cards here.
    }
});

p.make_event<events::mouse_up>([&]
{
    API::capture_window(p, false);
});
Hope this help.
May 18, 2013 at 10:01 AM
Thank you,understood.I will try to use capture_window.