XRC control client position is always (0,0)

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
bigpilot
I live to help wx-kind
I live to help wx-kind
Posts: 167
Joined: Tue Sep 19, 2006 8:33 am

XRC control client position is always (0,0)

Post by bigpilot » Mon Jul 06, 2020 7:46 am

I'm using wxPython to develop a form which has been constructed using XRC. On this form there are a couple of RichTextControls which are attached using AttachUnknownControl(). The form displays correctly, but when I attempt to retrieve the (client) position of the RichtText controls I always get back (0,0).

The XRC controls not created using AttachUnknownControl() do report their correct client position.

Has anyone had the same experience? How can I get the correct client position for the RichText controls?

P.S.: why doesn't XRC have support for RichText controls?
Soon to be world famous ;)

DavidHart
Site Admin
Site Admin
Posts: 3991
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Re: XRC control client position is always (0,0)

Post by DavidHart » Mon Jul 06, 2020 11:39 am

Hi,

(I've no idea about the wxPython/AttachUnknownControl aspects of this question.)
P.S.: why doesn't XRC have support for RichText controls?
Doesn't it? The wx3.0.5 source contains src/xrc/xh_richtext.cpp. In fact, so does wx2.8.12.

Regards,

David

bigpilot
I live to help wx-kind
I live to help wx-kind
Posts: 167
Joined: Tue Sep 19, 2006 8:33 am

Re: XRC control client position is always (0,0)

Post by bigpilot » Mon Jul 06, 2020 4:26 pm

Looking at the implementation of AttachUnknownControl (src\xrc\xmlres.cpp):

Code: Select all

bool wxXmlResource::AttachUnknownControl(const wxString& name,
                                         wxWindow *control, wxWindow *parent)
{
    if (parent == NULL)
        parent = control->GetParent();
    wxWindow *container = parent->FindWindow(name + wxT("_container"));
    if (!container)
    {
        wxLogError("Cannot find container for unknown control '%s'.", name);
        return false;
    }
    return control->Reparent(container);
}
and the code of Reparent() (src\common\wincmn.cpp)

Code: Select all

bool wxWindowBase::Reparent(wxWindowBase *newParent)
{
    wxWindow *oldParent = GetParent();
    if ( newParent == oldParent )
    {
        // nothing done
        return false;
    }

    const bool oldEnabledState = IsEnabled();

    // unlink this window from the existing parent.
    if ( oldParent )
    {
        oldParent->RemoveChild(this);
    }
    else
    {
        wxTopLevelWindows.DeleteObject((wxWindow *)this);
    }

    // add it to the new one
    if ( newParent )
    {
        newParent->AddChild(this);
    }
    else
    {
        wxTopLevelWindows.Append((wxWindow *)this);
    }

    // We need to notify window (and its subwindows) if by changing the parent
    // we also change our enabled/disabled status.
    const bool newEnabledState = IsEnabled();
    if ( newEnabledState != oldEnabledState )
    {
        NotifyWindowOnEnableChange(newEnabledState);
    }

    return true;
}
Why is it reparenting to the "container"?
Soon to be world famous ;)

bigpilot
I live to help wx-kind
I live to help wx-kind
Posts: 167
Joined: Tue Sep 19, 2006 8:33 am

Re: XRC control client position is always (0,0)

Post by bigpilot » Mon Jul 06, 2020 5:29 pm

I'm starting to understand the problem. When the XRC system sees an object of type "unknown" with name <name> it creates a panel with the name <name>_container and puts that in as a sort of placeholder.

When you call AttachUnknownControl() your control is actually added to the <name>_container wxPanel and the client position you get back is actually the position relative to the <name>_container, which is (0,0).

Now the question seems to be: how do I get the client position of my control relative to the parent of the <name>_container wxPanel?
Soon to be world famous ;)

bigpilot
I live to help wx-kind
I live to help wx-kind
Posts: 167
Joined: Tue Sep 19, 2006 8:33 am

Re: XRC control client position is always (0,0)

Post by bigpilot » Mon Jul 06, 2020 6:02 pm

Now the question seems to be: how do I get the client position of my control relative to the parent of the <name>_container wxPanel?
To answer my own question:

controlA = control I want the client coordinates of relative to WindowB

pos = controlA.GetScreenPosition()
clientPos = WindowB.ScreenToClient(pos)

clientPos is the client position of ControlA as if it had WindowB as its parent.
Soon to be world famous ;)

bigpilot
I live to help wx-kind
I live to help wx-kind
Posts: 167
Joined: Tue Sep 19, 2006 8:33 am

Re: XRC control client position is always (0,0)

Post by bigpilot » Mon Jul 06, 2020 6:05 pm

DavidHart wrote:
Mon Jul 06, 2020 11:39 am
Hi,

(I've no idea about the wxPython/AttachUnknownControl aspects of this question.)
P.S.: why doesn't XRC have support for RichText controls?
Doesn't it? The wx3.0.5 source contains src/xrc/xh_richtext.cpp. In fact, so does wx2.8.12.

Regards,

David
Thanks David.

I created an issue with the wxFormBuilder team: https://github.com/wxFormBuilder/wxForm ... issues/618
Soon to be world famous ;)

Post Reply