Setting bounds of child window with respect to parent window


#1

+--------- ------------------+
|      A    |      |                 |
|---------+ B   |                 |
|---------------+                |   
|              C                     |
+----------------------------+

As you can see there are three rectangles 'A', 'B' and 'C'.

'A' is the smallest rectangle. 'B' is also a rectangle which is enclosing 'A' and 'C' is also a rectangle which is enclosing 'B'

'C' is the main window which is resizable. I want to create 'B' and 'A' window as shown and even if the main window is resized the child windows should not move from top left position but can stretch.

I am new to GUI programming so need some help here. Below is my code which is doing something as below:

+--------- -----------------+
|                                   |
|                  +------------+
|                  |                |
+---------------------------+

Code below is creating the child window in bottom right which is what i don't want.

MainComponent.cpp

void MainContentComponent::resized()
{
    if(draw_component) {
        draw_component->setBounds(0, 0, getParentHeight()/4, getParentWidth()/4);
        draw_component->updateTransform();
   }
}

In above setBounds where is 0, 0 co-ordinates in the rectangle?

 

ChildComponent.cpp

void childComp::resized ()
{
    setBounds ((int) (0.1f * getParentWidth()), (int) (0.1f * getParentHeight()), (int) (0.5f * getParentWidth()), (int) (0.7f * getParentHeight()));
    repaint();
}

 

Can you please give some code for doing what i intend to do as shown in the above diagrams with "A", "B" and "C" rectangle? I went through all the examples but couldn't infer how to do this.

 


#2

The resized() method is called when your component changes size, so obviously it's a really bad idea to change the component's size in there! Surprising that you didn't just end up with a recursive call + crash.

And what on earth is updateTransform()? If you're setting component transforms then you have to be prepared for the fact that the transform geometry is relative to the parent, so if the transformed comp has bounds that aren't at the origin, things can get a bit complicated. If you're still learning about setting component bounds, best to completely avoid transforms!


#3

​It should be easy...

  • 0,0 is the top left. 
  • Stick to having the parent component control it's children to start with.  
  • Call setBounds on the child from its parents resize method. 

And it should all work out.

You can have a child change its own size, but if you are just starting out, I'd manage the size from the parent for now.  Like jules says, you can get in a loop ...