1 (edited by belfer 2017-02-01 01:04:00)

Topic: Custom widgets that use other widgets

Hi, so my question is. How do I go about making a custom widget that encapsulates other widgets? In the CustomWidget example, you do all the layout via the InvalidateImpl function. However, I want to treat my widget only as a container for more widgets. Basically, I have one box where I pack all my widgets, this box gets added to the widget which derives from the Container class, I then handle the logic in the class itself. There seem to be problems with this approach as I'm not getting the right allocation for the custom widget. Is this implementation a bad design or am I missing something?

The code looks something like this:

Custom::Ptr Custom::Create()
    auto ptr = Ptr(new Custom());
    return ptr;

Then setup looks like this:

void Custom::setup()
    m_mainbox = sfg::Box::Create(sfg::Box::Orientation::HORIZONTAL);
    m_mainbox->Pack(sfg::Button::Create("Test"), true, true);

There is only one button in the box, both should expand and fill all of the allocated area.
But even if the parent where the custom widget was added has a big allocation the button remains at the size of its requisition.

I should also mention that I calculate the requisition of the custom widget by just returning the requisition of the main box, i.e:

sf::Vector2f Custom::CalculateRequisition()
    return m_mainbox->GetRequisition();

2 (edited by belfer 2017-02-01 04:57:51)

Re: Custom widgets that use other widgets

So one way to go about this is to make the constructor in sfg::Box protected and then derive from that instead, that way I don't have to keep a Box::Ptr and just use the instance itself.

I'm actually going to keep a separate fork with the modifications I need. I might make a pull request later on if I think you might benefit from the design choices I'm going to make.

Re: Custom widgets that use other widgets

Nobody prevents you from taking the source code of one of the built in classes and basing your own project-specific code off of that. This way, you don't have to keep running around with a custom version of the library and have a hard time keeping it up to date when master gets updated.

Re: Custom widgets that use other widgets

Apologies for the late reply, I considered doing that but I felt like the changes were too small to rewrite an entire class. Also, I'm thinking of modifying other aspects of the library so it made sense to keep a separate repo. I'll try and avoid making the custom version a dependency for my program (rather than the main one), however, I understand that you might be busy and commits to the main repo might take some time to be pushed. For now, I will maintain my own repo, and as I mentioned before I will make a pull request for things that should have been included out of the box following your policies on extensibility.