wierd sizer behaviour after seting foreground color Topic is solved

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
metalinspired
In need of some credit
In need of some credit
Posts: 3
Joined: Sun Jul 14, 2013 2:00 pm

wierd sizer behaviour after seting foreground color

Post by metalinspired » Sun Jul 14, 2013 2:46 pm

Hi,
I've been using wxWidgets for some time now but (fortunately) never needed to ask a question until now.
I'm writin a component to emulate MS Outlook list of emails and I've managed to make it working but with a glitch.
I derived VScrolledWindow and I set a sizer in it and add components to the sizer.
Component is derived from wxPanel and has sizer that has few wxStaticText in it.
Also it has function that paints bottom border and if specific item is selected changes its background.
The problem I'm facing is when trying to change text color and it's not connected to color it self.
If I use SetForegroundColour on wxPanel nothing hapens and if I explicitly use SetForegroundColour on every wxStaticText the sizer in wxPanel no longer resizes as it should.
The sizer will not resize to smaller width than the width it had before changing color.

User avatar
doublemax
Moderator
Moderator
Posts: 14992
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wierd sizer behaviour after seting foreground color

Post by doublemax » Sun Jul 14, 2013 4:46 pm

Platform, wxWidgets version?

Somehow it doesn't make sense to me to use wxVScrolledWindow together with sizers. Usually i'd except to use either wxVScrolledWindow (without sizers) or a normal wxScrolledWindow with sizers.

I also find it hard to believe that setting the color of a control changes its behavior. Can you try to build a minimal sample that shows the problem?
Use the source, Luke!

metalinspired
In need of some credit
In need of some credit
Posts: 3
Joined: Sun Jul 14, 2013 2:00 pm

Re: wierd sizer behaviour after seting foreground color

Post by metalinspired » Sun Jul 14, 2013 5:12 pm

Hi doublemax,

It makes sense to me because it allows me to implement required behavior quite easily.
Sizer takes care of laying child components and wxVScrolledWindow takes care of scrolling.
I'm using Kubuntu 13.04 and wxWidgets 2.9.4.
It's hard to believe for me too that this could happen and that's why I spent whole day searching what am I doing wrong but I can't find it.
Here is a partial copy of code that I wrote for items that are added to sizer in wxVScrolledWindow:

Code: Select all

PanelListItem::PanelListItem(wxWindow* parent, const int id, const wxString& name, const wxString& region)
    : wxPanel(parent, wxID_ANY)
{
    wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);

    m_text_main = new wxStaticText(this, wxID_ANY, name, wxDefaultPosition, wxDefaultSize, wxST_ELLIPSIZE_END | wxST_NO_AUTORESIZE);
    sizer->Add(m_text_main, 0, wxALL | wxEXPAND, 5);

    m_text_other = new wxStaticText(this, wxID_ANY, region, wxDefaultPosition, wxDefaultSize, wxST_ELLIPSIZE_END | wxST_NO_AUTORESIZE | wxALIGN_RIGHT);
    sizer->Add(m_text_other, 0, wxALL | wxEXPAND, 5);

    SetSizer(sizer);

    Bind(wxEVT_PAINT, &PanelListItem::OnPaint, this);
}

void PanelListItem::OnPaint(wxPaintEvent& event)
{
    wxPaintDC dc(this);
    wxRect rect;
    rect = GetRect();

    if (m_selected)
    {
        SetChildColor(*wxWHITE);
        SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT));
    }
    else
    {
        SetChildColor(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
        SetBackgroundColour(*wxWHITE);
    }
    // paint border only if not last in list
    if (m_last == false)
    {
        wxPen pen(*wxBLACK, 1);
        dc.SetPen(pen);
        dc.DrawLine(0,rect.height-1, rect.width, rect.height-1);
    }
}

void PanelListItem::SetChildColor(wxColor color)
{
    m_text_main->SetForegroundColour(color);
    m_text_other->SetForegroundColour(color);
}
I'm pretty sure I'm taking a wrong approach with something but I can't figure out what.
And in case you're wondering why don't I simply use wxDC::DrawText() it's because wxStaticText has implemented ability to add ellipsis to long text.

Edit: I forgot to mention that there is a function that when called sets item as selected (or removes selection) and then calls Refresh() (which in turn will cause OnPaint() to be called) and it should repaint children but like I said in my first post calling SetForegroundColour() (I would put it instead of SetChildColor() call) doesn't affect children at all and that is why I wrote SetChildColor() but that approach messes up sizer bahaviour

User avatar
doublemax
Moderator
Moderator
Posts: 14992
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wierd sizer behaviour after seting foreground color

Post by doublemax » Sun Jul 14, 2013 5:47 pm

And in case you're wondering why don't I simply use wxDC::DrawText() it's because wxStaticText has implemented ability to add ellipsis to long text.
If not using wxStaticText would solve your problem, there is wxControl::Ellipsize().

This is a static(!) method.
http://docs.wxwidgets.org/trunk/classwx ... 9f84ef4ed1
Use the source, Luke!

metalinspired
In need of some credit
In need of some credit
Posts: 3
Joined: Sun Jul 14, 2013 2:00 pm

Re: wierd sizer behaviour after seting foreground color

Post by metalinspired » Sun Jul 14, 2013 5:55 pm

WTF :shock: :shock: :shock:
I must get glasses ASAP, I was sure that there should be static method that delivers that functionality but I didn't see it.
Hell man you made me feel so stupid right now #-o
This will simplify things so much as I won't need to make all wxStaticText children events propagate to their parent.
Thank you =D>

Post Reply