YaST/Development/Modules/Icon

From openSUSE

Contents

About

This article describes how Icon YCP module can be used in YaST source code.

The Icon YCP module can be found in yast2-2.16.16 and later.

What does it do?

The Icon module simplifies creation of dialogs with icons.

The First Example

   // Icon_example1.ycp
   {
       import "Icon";
   
       textdomain "test";
   
       term icon1 = Icon::Image ("warning", $["id":`my_warning, "label":_("My Warning")]);
       y2milestone ("%1", icon1);
   
       UI::OpenDialog (
           `opt (`decorated),
           `MarginBox (3, 1, `VBox (
               `Label (`opt(`boldFont), _("Bank Account Information")),
               `HBox (icon1, `Label(_("A huge sum of money has been sent to your bank account!"))),
               `HBox (`PushButton (_("Oh, yes!")))
           ))
       );
       UI::UserInput();
       UI::CloseDialog();
   }

Running the example logs this into /var/log/YaST2/y2log:

 `Image (`id (`my_warning), "/usr/share/YaST2/theme/current/icons/32x32/apps/msg_warning.png", "My Warning")

Then it opens this pop-up message:

Image:YaST-YCP_Icon-Message001.png

The Second Example

   // Icon_example2.ycp
   {
       import "Icon";
   
       textdomain "test";
   
       // adds margin 1 on the left and margin 2 on the right side of this image
       term icon2 = Icon::Image ("info", $["margin_left":1, "margin_right":2]);
       y2milestone ("%1", icon2);
   
       UI::OpenDialog (
           `opt (`decorated),
           `MarginBox (3, 1, `VBox (
               `Label (`opt(`boldFont), _("Alert!")),
               `HBox (icon2, `Label(_("The police is going to check your account transactions."))),
               `HBox (`PushButton (_("Oh, no!")))
           ))
       );
       UI::UserInput();
       UI::CloseDialog();
   }

Running the example logs this into /var/log/YaST2/y2log:

 `HBox (
     `HSpacing (1),
     `Image (`id ("icon_id_info"), "/usr/share/YaST2/theme/current/icons/32x32/apps/msg_info.png", "info"),
     `HSpacing (2)
 )

Then it opens this pop-up message:

Image:YaST-YCP_Icon-Message002.png

Most-Used Icons

The most used icons will be probably for warning, error, and info. That's why these functions were implemented (suggested by jsuchome at yast-devel@opensuse.org):

  • Icon::Warning()
  • Icon::Error()
  • Icon::Info()

See this example:

 {
   import "Icon";
 
   textdomain "test";
 
   UI::OpenDialog (
       `opt (`decorated),
       `VBox (
           `MarginBox (2,1, `HBox (
               `HWeight(1, `VBox (`Label (_("Warning")), Icon::Warning())),
               `HWeight(1, `VBox (`Label (_("Error")),   Icon::Error())),
               `HWeight(1, `VBox (`Label (_("Info")),    Icon::Info()))
           )),
           `PushButton (_("&OK"))
       )
   );
   UI::UserInput();
   UI::CloseDialog();
 }

and the output:

Image:YaST-YCP_Icon-Message003.png

More Functions

  • Icon::IconPath (type) - returns a path to image defined by the first paramater
  • Icon::Simple (type) - returns fully defined `Image() term defined by the first parameter