Click here to Skip to main content
11,805,604 members (56,083 online)
Click here to Skip to main content

EvaLayout, Lay it be!

, 1 Jan 2008 CPOL 110.1K 2.7K 78
Rate this:
Please Sign up or sign in to vote.
An efficient and flexible layout mananger.
Who's gonna layout this?


Would it not be great configuring and switching dynamically, the layout of an application, as it were a property like a Color or a Font? Is there any layout manager that can do it?

By layout, we mean arranging and resizing the GUI components in a dialog. This can be done manually (e.g. using an editor), or automatically by a software piece called a layout manager. In both cases, laying out has always been something difficult to configure, change, and in most cases, impossible to be switched dynamically. Difficult until now, of course! In this article, we will see how easy it is to layout with EvaLayout.

Note that the flexibility and configuration of layouts, as it will be described, plays not only an important role in the look of the application, but also when modifying it, coding variants, and in the implementation of generic and rich components.


There are lots of layout managers, and also quite a different number of approaches. EvaLayout belongs to the group of grid layouts, like GridBag layout (Java, Swing), Grig layout (Java, Eclipse, SWT), and specially TableLayout by Daniel Barbalace, and for C++ in Windows, the layout manager of Erwin Tratar and the one that I found very original from Marc Richarme.

The concepts of columns, rows, filling space, spanning columns and rows etc., are the same in all grid layouts. What is new in EvaLayout is the easy, clear, and flexible way of representing the whole layout information in a single text and, not less important, the decoupling between layout information and physical components that makes possible the advantages mentioned in the introduction.

Layout Info and Text Format in EvaLayout

Let us see the rules for defining an EvaLayout.

EvaLayout places all components within the cells of a grid - or table - of n columns and m rows. For the whole grid, the following can be defined:

  • A symmetric (same for left and right) horizontal margin
  • A symmetric (same for top and bottom) vertical margin
  • A horizontal gap (space between two adjacent columns)
  • A vertical gap (space between two adjacent rows)

For columns and rows, a header for each one determines its behaviour. There are three possibilities for these headers:

  • A (default): The size of the column/row will be adapted to the minimum required by the components in this column/row. That means, the maximum of the default sizes.
  • X: For expandable column/row. All expandable columns/rows will share the remaining space equally.
  • Number: Representing the width/height in pixels that the column/row has to have.

And finally, each cell in the grid may contain one of the following things:

  • blank, nothing is specified.
  • a name, it represents a logical name of a component, the cell where this name appears is the upper-left cell of the component.
  • sign -, a component that wants to occupy more cells at its right side (spanning columns) has to use this symbol in those cells.
  • sign +, a component that wants to occupy more cells at lower rows (spanning rows) has to use this symbol in that cells.

All this is given to the layout manager object (EvaLayoutManager) in a single text where the information is separated by commas. In the first line, the general margins and gaps are given, and the rest is for specifying the grid or the table. Note that the white spaces or blanks are only to make it more readable, they are actually not needed by EvaLayoutManager. Let us see an example. The text:

EvaLayout, 10, 10, 5, 5

grid,    75    ,    X    ,    A   ,
   A, boton1   , memo    ,    -   ,
   A, boton2   ,   +     ,        ,
   A, boton3   ,   +     ,        ,
   X,          ,   +     ,        ,
   A, text1    ,   -     , boton4 ,

describes a layout with:

  • 10 pixels of left, right, upper, and bottom margins
  • 5 pixels of horizontal and vertical gaps
  • A first column of 75 pixels, a second one expandable, and a third one adaptable
  • Five rows, all adaptable except the one that is expandable
  • A memo component that spans vertically 3 rows and horizontally 1 column
  • A text component that spans horizontally 1 column

The whole design could look like:

design layout

and the resultant dialog is:

resultant dialog

Now, we will see how to implement it in a C++ Windows application. We will show how to do it using the Windows API as it is done in the demo project, but it is also possible to do it in a MFC application and there is a how-to text that explains it.

Using the Code

For the implementation, we will need the following objects (note that except EvaLayoutInfo, the rest could be local objects):

#include <span class="code-string">"EvaLayoutManager.h"</span>

#include <span class="code-string">"EvaLine.h"</span>
#include <span class="code-string">"EvaUnit.h"</span>
#include <span class="code-string">"EvaFile.h"</span>

// to load layout info from a file
EvaFile eFile;
// to load layout info from a file
// or to store more layout info's
EvaUnit eUnit;
// layout info object
Eva     eLayoutInfo;

// the layout manager
EvaLayoutManager manager;

We divide the steps to use EvaLayoutManager, in four:

  • Preparing or loading the layout Info
  • Setting the layout info into the manager
  • Declaring the real components to handle
  • Doing the layout (positioning and resizing the components)

Preparing or Loading the Layout Info

The preparation can be done, for instance, in the message WM_INITDIALOG, and consists in setting at least an Eva object or, in order to load more layouts, an EvaUnit object that can be used as if it were an array of Evas. This preparation can be done in two ways:

  1. Directly:

    BOOL CALLBACK MainDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
          case WM_INITDIALOG:
             Eva layInfo;
             layInfo.addLine (EvaLine ("EvaLayout, 10, 10, 5, 5"));
             layInfo.addLine (EvaLine ("  grid,    75    ,    X    ,    A   ,"));
             layInfo.addLine (EvaLine ("     A, boton1   , memo    ,    -   ,"));
             layInfo.addLine (EvaLine ("     A, boton2   ,   +     ,        ,"));
             layInfo.addLine (EvaLine ("     A, boton3   ,   +     ,        ,"));
             layInfo.addLine (EvaLine ("     X,          ,   +     ,        ,"));
             layInfo.addLine (EvaLine ("     A, edit1    ,   -     , boton4 ,"));
  2. From a file (Eva format, see the file WinLayouts.eva in the demo project):

    BOOL CALLBACK MainDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
          case WM_INITDIALOG:
             EvaFile eFile;
             EvaUnit eUnit;
             Eva     eLayInfo;
             eFile.load (eUnit, "WinLayouts.eva", "data");
             layInfo = eUnit[0];

Setting the Layout Info into the Manager

This is just a call to the method setLayout of the layout manager. The message WM_INITDIALOG is a good place for that too, but note that this can also be performed wherever and whenever, to dynamically change the layout. After doing that, you should force a repaint, or better, a resize message. In the demo project, it is done somehow "tricky" but I think effectively.

   // force WM_SIZE (resize) here!

Declaring the Real Components to Handle

This step associates the physical window handle of the component with its logical name given in the layout info. This is thought to be done once but it could be carried out more times. The components that appear in our layout info, and all the components associated with the dialog are to be added here. The layout manager shows or hides the components according to the layout info, but it has to know all of them.

manager.removeComponents(); // if appropriate

manager.addComponent("memo",    GetDlgItem(hDlg, ID_MEMO));
manager.addComponent("boton1",  GetDlgItem(hDlg, ID_BUTTON_1));
manager.addComponent("boton2",  GetDlgItem(hDlg, ID_BUTTON_2));
manager.addComponent("boton3",  GetDlgItem(hDlg, ID_BUTTON_3));

Doing the Layout (Positioning and Resizing the Components)

It should be enough to do it in the WM_SIZE message handler.

              WPARAM wParam, LPARAM lParam)
      case WM_SIZE:
            manager.doLayout(wParam, lParam);

Conclusions and Future Versions

In spite of its simplicity, this layout can do almost everything that is reasonably needed in normal applications. An improvement would be to allow easy composition of layouts, that is, allowing the layout info to reference not only components but also other layout info. This feature actually works for Java with EvaLayout; for C++ and Windows, I have an idea about how to do it. I hope that will come out in a next version soon.


  • 22.04.2006 - First release
  • 02.01.2008 - Fixed some bugs and added a cleaner MFC example


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

I have a Telecomunications Engineering degree but since I learnt the first language (Pascal), almost I haven't stopped developing software. Mainly with C and C++ but I have touched many other languages and scripts. I use also to develop for fun (reinventing the wheel, of course!), currently almost all of such programs and tools are written in java (visit

You may also be interested in...

Comments and Discussions

BugCComboBox can not be pull down if... Pin
dunniu29-Jul-15 3:38
memberdunniu29-Jul-15 3:38 
GeneralRe: CComboBox can not be pull down if... Pin
dunniu30-Jul-15 19:58
memberdunniu30-Jul-15 19:58 
GeneralMy vote of 5 Pin
tang100071-Aug-12 3:42
membertang100071-Aug-12 3:42 
GeneralMy vote of 5 Pin
Rajiv Bhagwat26-Jul-12 4:39
memberRajiv Bhagwat26-Jul-12 4:39 
GeneralDesign idea Pin
v_srinu_26_f25-Apr-09 17:24
memberv_srinu_26_f25-Apr-09 17:24 
GeneralRe: Design idea [modified] Pin
Alejandro Xalabarder29-Apr-09 9:33
memberAlejandro Xalabarder29-Apr-09 9:33 
QuestionAlignment within a cell for component (L C R FILL T C B FILL) Pin
v_srinu_26_f25-Apr-09 17:15
memberv_srinu_26_f25-Apr-09 17:15 
AnswerRe: Alignment within a cell for component (L C R FILL T C B FILL) [modified] Pin
Alejandro Xalabarder29-Apr-09 8:51
memberAlejandro Xalabarder29-Apr-09 8:51 
GeneralA Bug! Pin
Ali Khanlarkhani5-Nov-07 3:07
memberAli Khanlarkhani5-Nov-07 3:07 
GeneralRe: A Bug! Pin
Alejandro Xalabarder6-Nov-07 12:21
memberAlejandro Xalabarder6-Nov-07 12:21 
GeneralIt is not a bug Pin
Alejandro Xalabarder29-Dec-07 10:11
memberAlejandro Xalabarder29-Dec-07 10:11 
NewsBug fix: memory leak and code for MFC Pin
$@m10-Jun-07 6:04
member$@m10-Jun-07 6:04 
NewsAnother solution Pin
$@m10-Jun-07 23:09
member$@m10-Jun-07 23:09 
GeneralRe: Bug fix: memory leak and code for MFC Pin
Alejandro Xalabarder6-Nov-07 13:13
memberAlejandro Xalabarder6-Nov-07 13:13 
GeneralFixed in new Code. Pin
Alejandro Xalabarder29-Dec-07 9:38
memberAlejandro Xalabarder29-Dec-07 9:38 
GeneralRe: Fixed in new Code. Pin
$@m1-Jan-08 21:33
member$@m1-Jan-08 21:33 
GeneralRe: Fixed in new Code. Pin
Alejandro Xalabarder2-Jan-08 12:18
memberAlejandro Xalabarder2-Jan-08 12:18 
QuestionWhat about removing a line? Pin
Hamed Mosavi20-Jul-06 15:29
memberHamed Mosavi20-Jul-06 15:29 
AnswerRe: What about removing a line? [modified] Pin
Alejandro Xalabarder22-Jul-06 0:52
memberAlejandro Xalabarder22-Jul-06 0:52 
What you are describing is clearly a change in the layout, hide or show controls even without changing other control's position in EvaLayout is also a change in the layout. The good new is that changing the layout information with EvaLayout is a trivial thing: The whole information is given to EvaLayoutManager using a simple text. With EvaLayout first - and only once -you define all controls that your application might show giving them a name (method EvaLayoutManager::addComponent), then the layout information is a text where the name of the control might appear or not (visible or not visible) and according to some simple rules its position and size are calculated. You can have or dynamically build as much texts (layout infos) as you want and they may be set at any time (method EvaLayoutManager::setLayout).

To see in one minute how easy is to create such a text (new layout for an application) do the following:

- download the demo exe from the article. It is a zip file with an exe and an text file that contain three Layout infos for the demo
- run the demo, the controls are laid out following the first layout info, then using the menu "File / Switch layout" the rest might be set
- edit the text file (WinLayouts.eva) and add a new layout (after #data#)


---, X
A, boton1
A, boton2
X, memo

- run again the application and now your new layout is shown first.

This flexibility allows you not only dynamic layouting as you mention in your message but also build, maintain and improve complex layouts.


-- modified at 7:19 Sunday 23rd July, 2006
GeneralRe: What about removing a line? Pin
Hamed Mosavi25-Jul-06 23:21
memberHamed Mosavi25-Jul-06 23:21 
GeneralWarnings Pin
Alejandro Xalabarder26-Jul-06 10:24
memberAlejandro Xalabarder26-Jul-06 10:24 
QuestionA problem in MFC Version!!! Pin
sPhinX16-May-06 17:10
membersPhinX16-May-06 17:10 
AnswerRe: A problem in MFC Version!!! Pin
Alejandro Xalabarder18-May-06 11:09
memberAlejandro Xalabarder18-May-06 11:09 
GeneralRe: A problem in MFC Version!!! Pin
sPhinX18-May-06 15:51
membersPhinX18-May-06 15:51 
AnswerRe: A problem in MFC Version!!! Pin
Alejandro Xalabarder19-May-06 0:15
memberAlejandro Xalabarder19-May-06 0:15 
GeneralNot so easy ... [modifed] Pin
Alejandro Xalabarder20-May-06 0:19
memberAlejandro Xalabarder20-May-06 0:19 
GeneralFixed in new Code. No need of a special version for MFC Pin
Alejandro Xalabarder29-Dec-07 9:33
memberAlejandro Xalabarder29-Dec-07 9:33 
NewsBug and Fix with more expandable rows Pin
Alejandro Xalabarder14-May-06 4:00
memberAlejandro Xalabarder14-May-06 4:00 
GeneralFixed in new Code. Pin
Alejandro Xalabarder29-Dec-07 10:15
memberAlejandro Xalabarder29-Dec-07 10:15 
QuestionWhat about group boxes? Pin
khb29-Apr-06 0:59
memberkhb29-Apr-06 0:59 
AnswerRe: What about group boxes? Pin
Alejandro Xalabarder29-Apr-06 18:21
memberAlejandro Xalabarder29-Apr-06 18:21 
GeneralRe: What about group boxes? Pin
khb30-Apr-06 23:13
memberkhb30-Apr-06 23:13 
GeneralHehe, I made something similar in OpenGL Pin
Kochise24-Apr-06 22:31
memberKochise24-Apr-06 22:31 
Generaldynamic size based on name/content length would be nice... Pin
Fred W3-May-06 1:27
memberFred W3-May-06 1:27 
GeneralRe: dynamic size based on name/content length would be nice... Pin
Alejandro Xalabarder3-May-06 9:04
memberAlejandro Xalabarder3-May-06 9:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151002.1 | Last Updated 2 Jan 2008
Article Copyright 2006 by Alejandro Xalabarder
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid