How do I correctly make something like a desktop component?

Are you writing your own components and need help with how to set them up or have questions about the components you are deriving from ? Ask them here.
Post Reply
NeomerArcana
In need of some credit
In need of some credit
Posts: 4
Joined: Mon Jun 08, 2015 6:05 am

How do I correctly make something like a desktop component?

Post by NeomerArcana »

I have an idea where let's just assume it's a bunch of notes that are scattered on a desktop.

This desktop can be scrolled and within it you can move around the notes.

So far, I have my notes, and they can intercept mouse down and mouse up events to draw in a different colour (so that I can see they've clicked).

However, when it comes to making them move along with the mouse, I have a feeling I'm about to go about it the wrong way. I was going to intercept the move event and have the "notes" follow the mouse.

But I think there's probably some problems with that, like if the mouse moves too quick.

Does my "desktop" need to actually handle the repositioning of the notes instead of the notes themselves? So far I'm unable to do this because the "notes" are intercepting the click events before the "desktop" receives them.

I haven't been able to find any of the samples that do something like this.
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How do I correctly make something like a desktop component?

Post by doublemax »

However, when it comes to making them move along with the mouse, I have a feeling I'm about to go about it the wrong way. I was going to intercept the move event and have the "notes" follow the mouse.
That's the correct way to do this.
But I think there's probably some problems with that, like if the mouse moves too quick.
Probably just a little bug with the coordinate calculations.

Here's a relevant code snippet from the "shaped" sample that comes with wxWidgets. It should demonstrate what you need:

Code: Select all

void ShapedFrame::OnLeftDown(wxMouseEvent& evt)
{
    CaptureMouse();
    wxPoint pos = ClientToScreen(evt.GetPosition());
    wxPoint origin = GetPosition();
    int dx =  pos.x - origin.x;
    int dy = pos.y - origin.y;
    m_delta = wxPoint(dx, dy);
}

void ShapedFrame::OnLeftUp(wxMouseEvent& WXUNUSED(evt))
{
    if (HasCapture())
    {
        ReleaseMouse();
    }
}

void ShapedFrame::OnMouseMove(wxMouseEvent& evt)
{
    wxPoint pt = evt.GetPosition();
    if (evt.Dragging() && evt.LeftIsDown())
    {
        wxPoint pos = ClientToScreen(pt);
        Move(wxPoint(pos.x - m_delta.x, pos.y - m_delta.y));
    }
}
Use the source, Luke!
NeomerArcana
In need of some credit
In need of some credit
Posts: 4
Joined: Mon Jun 08, 2015 6:05 am

Re: How do I correctly make something like a desktop component?

Post by NeomerArcana »

Oh awesome, CaptureMouse() is what does the trick. Thanks a lot.
Post Reply