Connect problem

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 281
Joined: Wed Jan 20, 2010 1:15 pm

Connect problem

Post by dkaip » Wed Feb 28, 2018 7:33 am

Hello
I have a myFrame class, and i am trying to delete Rect object but i failed.
Something i am doing wrong. OnDoubleClick do no react.
Any idea?
Thanks jim

Code: Select all

class shapes : public wxEvtHandler
...
class Rectangle: public Shape
....
class Circle: public Shape
...
class Line : public Shape
...
-----------------------
class myFrame 
...
myViewer* view;
vector<Shape*> shapes;

DrawRect(){

wxPaintDC dc(this);
Rectangle* r=new Rectangle();
r->setDC(&dc);
r->drawToDC();
    r->Connect
    (
        wxEVT_LEFT_DCLICK,
        wxMouseEventHandler(myFrame::OnDoubleClick),
        NULL,
        this
    );
shapes.push_back(r);
}

User avatar
T-Rex
Moderator
Moderator
Posts: 1198
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: Connect problem

Post by T-Rex » Wed Feb 28, 2018 9:57 am

You are adding the event handler to your Rect object. When you call Connect() it means like 'when `r` receives the mouse double click event, call the OnDoubleClick method of myFrame'.
So, now you have the Rect object which can handle double click events.
But you still didn't make the `r` object to receive these events. Events don't come from nowhere. Your frame receives the user input events by defaut, and `r` object does not. You need to add the `r` object to the chain of event handlers. Or pass the events from myFrame to `r` object (and other your shapes) manually.

See PushEventHandler() method of wxWindow.
If you decide to use this approach, then when you delete the `r` object, you need to remove it from the chain of event handlers by calling RemoveEventHandler()

But again, it looks like not a correct architecture. In general, your `model` objects, which store the document's structure and can be serialized/deserialized, should not handle the user input.

dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 281
Joined: Wed Jan 20, 2010 1:15 pm

Re: Connect problem

Post by dkaip » Wed Feb 28, 2018 12:18 pm

Ok, i am changing from myFrame to Rectangle or Shape but nothing, no response.

Code: Select all

      r->Connect
    (
        wxEVT_LEFT_DCLICK,
        wxMouseEventHandler(Shape::OnDoubleClick),
        NULL,
        this
    );

void Shape::OnDoubleClick(wxMouseEvent& event)
{
wxMessageBox("cccccccccccc");
}


OR

r->Connect
    (
        wxEVT_LEFT_DCLICK,
        wxMouseEventHandler(Rectangle::OnDoubleClick),
        NULL,
        this
    );

void Rectangle::OnDoubleClick(wxMouseEvent& event)
{
wxMessageBox("eeeeeeee");
}
There is a case to blame that the object being constructed, the square has no physical substance, ie it is not a frame but a shape just on wxPaintDC, so the object itself can not perceive that someone is acting on it. Is my thought correct or wrong? I'm trying to figure out what's going on.

coderrc
Earned some good credits
Earned some good credits
Posts: 141
Joined: Tue Nov 01, 2016 2:46 pm

Re: Connect problem

Post by coderrc » Wed Feb 28, 2018 1:26 pm

dkaip wrote:the square ... is not a frame but a shape just on wxPaintDC, so the object itself can not perceive that someone is acting on it. Is my thought correct or wrong?
you are correct. In this case, whatever the DC comes from (panel, frame, button, etc) will receive the event.
What I would do is make an interface IClickable that has a function called "bool HitTest(int x, int y)". Where params x and y are the coordinates of the
"click" and the return value indicates whether or not the shape's geometry contains that point. Your shape classes would then implement IClickable, and your Shape Container would loop through the list of shapes it contains, calling HitTest on each one. If hit test returns true, you can then call whatever function performs the appropriate action.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4541
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Connect problem

Post by ONEEYEMAN » Wed Feb 28, 2018 4:16 pm

Hi,
You might be better off using some kind of a shape library - wxShapeFramework or wxArt2D, if you need something like this.
Those libraries will handle clicks on the shapes, dragging the shapes and properly destroying the shapes.

Thank you.

Post Reply