Ensemble Graphics Toolkit: Themes

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

Introduction

In this training, you will learn how to set a default theme, set a global theme, change an existing theme, and create a custom theme.

Steps:

  • Create a New Source File
  • Set a Default Theme
  • Set a Global Theme
  • Change an Existing Theme
  • Create a Custom Theme

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 be creating a new theme.cpp source file that you will build in later sections demonstrating the BoxSizer class.

​You will use EgtProject and its settings you entered in the "Ensemble Graphics Toolkit: First Application Using Eclipse IDE™" training.

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 themes.cpp. If you have a *.cpp source file from a previous Ensemble Graphics Toolkit training, 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:

Theme: New Source File

Back to Top


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

A new themes.cpp source file tab is created within EgtProject.

Back to Top


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

#include <egt/ui>
using namespace egt;
using namespace std;
int main(int argc, const char ** argv) {
    Application app;
    TopWindow win;

    VerticalBoxSizer vsizer;
    win.add(expand(vsizer));

    HorizontalBoxSizer hsizer1;
    vsizer.add(expand(hsizer1));
    Button button1(hsizer1, "Button 1");
    CheckBox checkbox1(hsizer1, "Disable Button 1");

    HorizontalBoxSizer hsizer2;
    vsizer.add(expand(hsizer2));
    Button button2(hsizer2, "Button 2");
    CheckBox checkbox2(hsizer2, "Disable Button 2");

    win.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

Set a Default Theme

In this section, you will build the source code you entered in the previous section and observe the default theme that is set.

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).

Theme: 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/theme

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

This is the location where the sizer.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 theme.cpp executable is running remotely on the target.

This is the default theme that is set when no other theme parameters are set.

Default Theme

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

Set a Global Theme

In this section, you will modify the theme.cpp source code to set a global theme.

Modify the theme.cpp source code by adding the following lines of code as shown in the accompanying code example:

#include <egt/themes/midnight.h>
using namespace egt;
using namespace std;
int main(int argc, const char ** argv) {
    Application app;
   auto new_global_theme = make_shared<MidnightTheme>();
    global_theme(new_global_theme);
    TopWindow win;

    VerticalBoxSizer vsizer;
    win.add(expand(vsizer));

    HorizontalBoxSizer hsizer1;
    vsizer.add(expand(hsizer1));
    Button button1(hsizer1, "Button 1");
    CheckBox checkbox1(hsizer1, "Disable Button 1");

    HorizontalBoxSizer hsizer2;
    vsizer.add(expand(hsizer2));
    Button button2(hsizer2, "Button 2");
    CheckBox checkbox2(hsizer2, "Disable Button 2");

    win.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. Alternatively, 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 theme.cpp executable is running remotely on the target.

Observe that Button 1, Button 2, and their checkboxes have changed color. Also, the background color has changed. The new global theme is inherited by all the child widgets. You can check the Disable buttons to observe the change in color for their disabled state.

Global Theme

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

Change an Existing Theme

In this section, you will modify the theme.cpp source code from the previous section to change a global theme.

Modify the theme.cpp source code by adding the following lines of code as shown in the accompanying code example:

#include <egt/themes/midnight.h>
using namespace egt;
using namespace std;
int main(int argc, const char ** argv) {
    Application app;
   auto new_global_theme = make_shared<MidnightTheme>();
    global_theme(new_global_theme);
    TopWindow win;

    VerticalBoxSizer vsizer;
    win.add(expand(vsizer));

    HorizontalBoxSizer hsizer1;
    vsizer.add(expand(hsizer1));
    Button button1(hsizer1, "Button 1");
    CheckBox checkbox1(hsizer1, "Disable Button 1");

    HorizontalBoxSizer hsizer2;
    vsizer.add(expand(hsizer2));
    Button button2(hsizer2, "Button 2");
    CheckBox checkbox2(hsizer2, "Disable Button 2");

   auto new_theme = win.theme();
    Font new_font(35, Font::Weight::bold);
    new_theme.font(new_font);
    win.theme(new_theme);

    win.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. Alternatively, 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 theme.cpp executable is running remotely on the target.

Observe that the font in both the button widget and the checkboxes has changed. The font is larger and the weight id changed to bold. You have changed an existing property of the global midnight theme.

Changing an existing Theme

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.

Create a Custom Theme

In this section, you will modify the theme.cpp source code from the previous section to create a custom theme and apply it to the Horizontal Box Sizer 2.

Modify the theme.cpp source code by adding the following lines of code as shown in the accompanying code example:

#include <egt/themes/midnight.h>
using namespace egt;
using namespace std;
int main(int argc, const char ** argv) {
    Application app;
   auto new_global_theme = make_shared<MidnightTheme>();
    global_theme(new_global_theme);
    TopWindow win;

    VerticalBoxSizer vsizer;
    win.add(expand(vsizer));

    HorizontalBoxSizer hsizer1;
    vsizer.add(expand(hsizer1));
    Button button1(hsizer1, "Button 1");
    CheckBox checkbox1(hsizer1, "Disable Button 1");

    HorizontalBoxSizer hsizer2;
    vsizer.add(expand(hsizer2));
    Button button2(hsizer2, "Button 2");
    CheckBox checkbox2(hsizer2, "Disable Button 2");

   auto new_theme = win.theme();
    Font new_font(35, Font::Weight::bold);
    new_theme.font(new_font);
    win.theme(new_theme);

struct CustomTheme : public MidnightTheme
    {
       void apply() override
        {
            Theme::apply();
            palette().set(Palette::ColorId::button_bg,
                          Palette:: GroupId::normal,
                          Palette::pink);
            palette().set(Palette::ColorId::button_fg,
                          Palette::GroupId::normal,
                          Palette::pink);
            palette().set(Palette::ColorId::button_fg,
                          Palette::GroupId::checked,
                          Palette::pink);
            palette().set(Palette::ColorId::button_bg,
                          Palette::GroupId::disabled,
                          Palette::tan);
            font().size(30);
        }
    } custom_theme;

    custom_theme.apply();
    hsizer2.theme(custom_theme);

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

GroupId

GroupId is used to define a category of patterns that usually relate to the state of a widget. States can be normal, active, disabled, or checked. 

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 theme.cpp executable is running remotely on the target.

Observe the horizontal box sizer 2 has a new look. The Button 2 foreground color for the normal state has changed.

Custom Theme

Back to Top


Check Disable Button 1 and Disable Button 2 to observe the new background color when the buttons are disabled.

Custom Theme: new background color

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.

​Theme class

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

global_theme()

If the top-level window does not have a custom theme, it will use the default global_theme(). This function sets the global theme properties and the ability to modify them.

global_theme(std::shared_ptr<Theme> theme)

This function sets the global theme. This will disable any pre-existing theme instance. Remember to add the respective include file for the theme if using EGT’s custom themes.

Widget::theme()

Themes are inherited by default from the widget’s parent. This function helps to break the inheritance and set its own theme.

Back to Top

Summary

In this training, you learned how to set a default theme, set a global theme, change an existing theme, and create a custom theme.

Back to Top

Learn More

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