Event driven editable rectangle vector class

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: 334
Joined: Wed Jan 20, 2010 1:15 pm

Event driven editable rectangle vector class

Post by dkaip »

Hello.
I am trying to write an editable rectangle vector class.
With DClick on rectangle the rectangle object must delete.
That is the delete method must be in rect class as well as event must be in same class.
But how i must write the rect class?
In the way i have write the class, compiler says that there is no Connect function in object rect* r( r->Connect).
So how i must write class? There is some sample to see?
Thanks
Jim

Code: Select all

class rect : public wxEvtHandler
{
public:
    rect(wxWindow *parent,wxDC& dc, wxPoint& startPoint, wxPoint& endPoint)
{
    //ctor
    dc.SetPen( wxPen( wxColour( 128, 0, 0 ), 3 ));
    dc.SetBrush(*wxTRANSPARENT_BRUSH);
    dc.DrawRectangle(startPoint.x,startPoint.y,endPoint.x-startPoint.x,endPoint.y-startPoint.y);
}

    virtual ~rect(){};

    void deleterect( wxMouseEvent& event )
    {
        wxMessageBox("ok i just delete rectangle");
    }
protected:

private:

};


class myview : public wxScrolledWindow
{
friend rect;
public:
    myview(wxWindow *parent);
private:
    void UpdateBitmap();
    void OnMouseDown(wxMouseEvent& event);
    void OnMouseUp(wxMouseEvent& event);
    void OnMouseMove(wxMouseEvent& event);
    void OnMouseCaptureLost(wxMouseCaptureLostEvent& event);
    wxStaticBitmap *m_content;
    bool draggingPage;
    wxPoint startpoint;
    wxPoint endpoint;
    DECLARE_EVENT_TABLE()
    rect* r;
};

BEGIN_EVENT_TABLE(myview, wxScrolledWindow)
    EVT_LEFT_DOWN(myview::OnMouseDown)
    EVT_LEFT_UP(myview::OnMouseUp)
    EVT_MOTION(myview::OnMouseMove)
    EVT_MOUSE_CAPTURE_LOST(myview::OnMouseCaptureLost)
END_EVENT_TABLE()

myview::myview(wxWindow *parent)
    : wxScrolledWindow(parent,
                       wxID_ANY,
                       wxDefaultPosition, wxDefaultSize,
                       wxFULL_REPAINT_ON_RESIZE)
{

    wxBitmap dummyBitmap(16, 16);
    m_content = new wxStaticBitmap(this, wxID_ANY, dummyBitmap);

    wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
    sizer->Add(m_content, wxSizerFlags(1).Expand());
    SetSizer(sizer);
    m_content->Connect
    (
        wxEVT_LEFT_DOWN,
        wxMouseEventHandler(myview::OnMouseDown),
        NULL,
        this
    );

    r->Connect
    (
        wxEVT_LEFT_DCLICK,
        wxMouseEventHandler( rect::deleterect ),
        NULL,
        this
    );

}

.................

void myview::OnMouseDown(wxMouseEvent& event)
{
    wxPoint view_origin;
    GetViewStart(&view_origin.x, &view_origin.y);
    wxPoint pos = event.GetPosition();
    draggingPage = true;
    startpoint = pos;
    event.Skip();
    CaptureMouse();
}


void myview::OnMouseUp(wxMouseEvent& event)
{
    const wxPoint pos = event.GetPosition();
    draggingPage = false;
    endpoint = pos;
    ReleaseMouse();
    event.Skip();
    wxPaintDC dc(this);
    r=new rect(this,dc,startpoint,endpoint);
}
User avatar
T-Rex
Moderator
Moderator
Posts: 1248
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: Event driven editable rectangle vector class

Post by T-Rex »

Looks just like an incorrect app's architecture.
1. Rectangle should be responsible only for drawing itself.
2. The ScrollView should be responsible for mouse clicks. In this case the event handler will know about the `r` member variable, you can just delete the `r` object and reset the pointer to nullptr.
3. You should not delete the current object `this` from its member function.
4. Most likely, your rectangle class should not be derived from wxEvtHandler because it looks like an overhead, as long as you want to implement only the logic mentioned in your sample code.

You probably can take a look at this code. It also has an implementation of draggable selection rectangle.
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 334
Joined: Wed Jan 20, 2010 1:15 pm

Re: Event driven editable rectangle vector class

Post by dkaip »

Thanks for advice. I just put your code to run for study, but #include <tif_config.h> don't exist.
Must be put code in certain place inside wxWidgets3.0.3 folder for TIFF resources?
I am trying to under stud what is the architecture of design CAD, like autocad.
How objects are construct and are editable, for example moving a rectangle, change grips etc...
Jim.
User avatar
T-Rex
Moderator
Moderator
Posts: 1248
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: Event driven editable rectangle vector class

Post by T-Rex »

dkaip wrote:]#include <tif_config.h> don't exist.
%WXWIN%\src\tiff\libtiff - add this to include directories
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 334
Joined: Wed Jan 20, 2010 1:15 pm

Re: Event driven editable rectangle vector class

Post by dkaip »

Ok now it runs, but dont open files. I am in Mint linux, wxWidgets3.0.3
It says
../src/common/image.cpp(1746): assert "IsOk()" failed in GetHeight(): invalid image
Jim
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 334
Joined: Wed Jan 20, 2010 1:15 pm

Re: Event driven editable rectangle vector class

Post by dkaip »

Sorry but it does not work. But is no matter, i will find why.
The code does not have a method to edit object, that i am looking for.
For example a line that after creation i can move or delete or something else.
A graphic that is not an image but an editing object, a shape.
I think that http://www.angelfire.com/tx4/cus/shapes/cpp.html site is well to study...
Thanks
Jim
User avatar
T-Rex
Moderator
Moderator
Posts: 1248
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: Event driven editable rectangle vector class

Post by T-Rex »

You can write a method to edit object by yourself, I think.
I gave you the link as a reference of how you can implement what you want in a manner that will work more or less fine, without the intention of making the work instead of you.
And yes, you can use any 3rd-party source code, which implements the logic of working with primitives, to solve your problem.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Event driven editable rectangle vector class

Post by ONEEYEMAN »

Hi,
Take a look at the wxShapeFramework. It might give you an idea of how to implement some things.

And there are others - check https://wiki.wxwidgets.org/WxFAQ#How_ca ... d_nodes.3F.

Thank you.
Post Reply