Ensemble Graphics Toolkit: Static Grid

Last modified by Microchip on 2024/01/08 16:27

Introduction

In this training, you will construct a StaticGrid class to set the size and position of child widgets on the window of the WVGA Display. You will learn how to add a new button widget to a cell of the StaticGrid.

Steps:

  • Create a New Source File
  • Construct a Static Grid
  • Add a New Button

Prerequisites

You have prepared the Host PC with all the development software tools and Ensemble Graphics Toolkit source code as explained in:

You have installed and prepared the Eclipse IDE for C/C++ Developers as explained in:

Create a New Source File

In this section, you will create a new grid.cpp source file that you will build in later sections demonstrating the StaticGrid class.

​In this training topic, you will use EgtProject and its settings you entered in the training topic: Ensemble Graphics Toolkit: First Application using Eclipse IDE

Start the Eclipse IDE

Eclipse will ask you to select a directory for your workspace. Accept the default directory and click on the Launch button.

Eclipse IDE launcher

Eclipse will launch the previous workspace you configured.

Eclipse Previous Workspace

Back to Top


You will be adding a new source file with the name: grid.cpp. If you have a *.cpp source file from a previous Ensemble Graphics Toolkit training topic, perform the following steps:

Right-click in the *.cpp window and select Resource Configurations > Exclude from Build…

The Exclude from build window will open:

Eclipse exclude from build

Select Debug and Release and click on the OK button.

The selected *.cpp will be excluded from the build.

Back to Top


Right-click on EgtProject and select Clean Project.

Back to Top


Right-click on EgtProject and select New > Source File.

The New Source File window will open:

Grid new source file

Back to Top


Enter grid.cpp into the Source file: text box. Click on the Finish button.

A new source file tab grid.cpp is created within the EgtProject.

Back to Top


Enter the following source code to the grid.cpp window pane:

#include <egt/ui>

using namespace egt;

int main(int argc, const char ** argv)
{
    Application app;
    TopWindow window;

    StaticGrid grid(Rect(20, 20, 750, 440),
                    StaticGrid::GridSize(3, 3), 10);

    window.add(grid);

   for (auto i = 1; i <= 7; ++i)
    {
       auto button = std::make_shared < Button > ("Button " +
                                              std::to_string(i));
        grid.add(expand(button));
    }

    window.show();
   return app.run();
}

Back to Top


Save your program by selecting File > Save. You may also save by typing CTRL+S.

You have completed adding a new source file to EgtProject.

Back to Top

Construct a Static Grid

To build the project, hover over EgtProject, right-click and select Build Project from the menu. Or you can click on the Build icon.

The Console window (bottom pane) will display the build progress.

Back to Top


To set run properties, hover over EgtProject, right-click and select Run As > Run Configurations… from the menu.

Back to Top


In the left-hand pane, select EgtProject Debug (under C/C++ Remote Application).

Grid debug configuration

Back to Top


In the Run Configurations window, enter the following in the Remote Absolute File Path for C/C++ Application: text box:

/root/grid

​You may see the previous program /root/???? here. Replace with /root/grid.

This is the location the grid.cpp executable will be loaded and run on the target.

Back to Top


Click on the Apply button.

Back to Top


Click on the Run button.

Back to Top


Observe the WVGA Display on the target:

The grid.cpp executable is running remotely on the target.

Observe a static grid with 7 buttons arranged with 3 rows and 3 columns. The buttons will expand to the width and height of its cell.

Observe the WVGA Display on the target

StaticGrid class

From ~/egt/docs/html/annotated.html, click on the StaticGrid class. All the functions, attributes and constructor associated with the class are documented here.

Back to Top


To stop the program, press the Stop button (upper left hand, just below the menu bar).

​Before you start another session, be sure to stop the current session.

Back to Top

Add a New Button

In this section, you will modify the grid.cpp source code to add a new button to a specific cell in the grid.

Modify the grid.cpp source code by adding the following lines of code as shown.

#include <egt/ui>

using namespace egt;

int main(int argc, const char ** argv)
{
    Application app;
    TopWindow window;

    StaticGrid grid(Rect(20, 20, 750, 440),
                    StaticGrid::GridSize(3, 3), 10);

    window.add(grid);

   for (auto i = 1; i <= 7; ++i)
    {
       auto button = std::make_shared < Button > ("Button " +
                                              std::to_string(i));
        grid.add(expand(button));
    }

   //Add new button to grid at cell position (0,3)

    Button newbutton("Button 8");
    grid.add(expand(newbutton),0,3);

    window.show();
   return app.run();
}

Back to Top


Save your program by selecting File > Save. You may also save by pressing CTRL + S.

Back to Top


To build the project, hover over EgtProject, right-click, and select Build Project from the menu. Or you can click on the Build icon.

The Console window (bottom pane) will display the build progress.

Back to Top


Click on the Run button.

Back to Top


Observe the WVGA Display on the target:

The grid.cpp executable is running remotely on the target.

Observe a new button widget added to the cell (0, 3) in a static grid with eight buttons arranged with four rows and three columns. The buttons will expand to the width and height of its cell.

Observe the WVGA Display on the target

Back to Top


To stop the program, press the Stop button (upper left hand, just below the menu bar).

​Before you start another session, be sure to stop the current session.

Back to Top

Summary

In this training, you explored the construction of the StaticGrid class to size and position of the child widgets on the window of the WVGA Display. You also learned how to add a new Button widget to a cell.

Back to Top

What’s Next?

There’s plenty more to learn. Here are some additional Ensemble Graphics Toolkit training resources to help you gain more knowledge and skills:

Back to Top