# What should the relation between parent and child GUI components be?

This seems to happen every time I create any sort of GUI. I have trouble figuring out how child classes should communicate to their siblings.

It's a general problem, but it's probably easier to use a concrete example (hopefully that's still okay for this forum? I know it's different from StackOverflow): I have a window that contains 4 panels. Right now, I simply have the children as member objects of the parent.

Keypresses change all 4 panels, so I have the child objects throw the event back to their parent, which does the logic, then makes the appropriate calls for each panel.

However, mouse events are different. They change the position of a camera (owned by the parent), which every panel uses to render itself. So I must recompute a few matrices, in order to render each panel. The matrices are different for each panel. The problem I'm having is: when do I give the children the matrices?

I think I have three possibilities:

1. When reading a mouse event, compute the matrices and call setter methods for each panel. However, sometimes the matrix will be replaced before the window renders.
2. Give the child panel a reference to the parent, and during the render call, request the matrix from the parent (either by method or member variable).
3. Modify child.render() so that it accepts two matrix parameters, and make parent.render() call them appropriately.

#2 seems good, because I'm not unnecessarily setting the matrix in the child window. But it also requires me to mess with forward declarations for member objects, which seems like bad practice. And #3 I'm not quite sure how to implement, because render is called by many things other than my code (un-minimizing, for example), and I can't touch them. So I suppose it is #1?

Is there a design pattern people usually follow for this? Am I just overthinking, and #1 is actually completely OK?

• most gui framework I've worked with used #2 – ratchet freak May 24 '13 at 21:53
• Set a flag in the child in the setter, and then use an invalidation routine (what that looks like depends on what you are writing your GUI in) that will only render when it makes sense. So interim matrices are discarded. – Amy Blankenship May 27 '13 at 13:05
• What do you mean by an invalidation routine? – Henry Swanson May 27 '13 at 14:10

If I got your right, during the render call, the child needs a service which provides some matrixes, right? It does not really matter that it is the parent which provides that matrixes. So create an interface like IMatrixProvider, give a reference to an object of type IMatrixProvider during construction time to your panels and call that service when you need it within the rendermethod. Your parent object can implement that interface, if the matrixes are provided from there.